Keeping only certain sections and converting to a non-ELF format in one step produces corrupted output. Reproduction case below. The converted PE file is not expected to be useful, but demonstrates the issue.
% cat hello.c #include <stdio.h> int main(int argc, char *argv[]) { puts("hello world"); } % cc hello.c
% elfcopy/elfcopy -j .ctors -j .text -j .data a.out a.elf % elfcopy/elfcopy --output-target efi-app-x86_64 a.elf a.efi % objdump -h a.efi a.efi: file format efi-app-x86_64 Sections: Idx Name Size VMA LMA File off Algn 0 .text 000002b8 00000000004004e0 00000000004004e0 00000200 2**2 CONTENTS, ALLOC, LOAD, READONLY, CODE 1 .ctors 00000010 0000000000600860 0000000000600860 00000600 2**2 CONTENTS, ALLOC, LOAD, DATA 2 .data 0000001c 0000000000600a30 0000000000600a30 00000800 2**2 CONTENTS, ALLOC, LOAD, DATA
% elfcopy/elfcopy -j .ctors -j .text -j .data --output-target efi-app-x86_64 a.out a.efi % objdump -h a.efi a.efi: file format efi-app-x86_64 Sections: Idx Name Size VMA LMA File off Algn 0 .interp 000002b8 00000000004004e0 00000000004004e0 00000200 2**2 CONTENTS, ALLOC, LOAD, READONLY, CODE 1 p 00000010 0000000000600860 0000000000600860 00000600 2**2 CONTENTS, ALLOC, LOAD, DATA 2 .tag 0000001c 0000000000600a30 0000000000600a30 00000800 2**2 CONTENTS, ALLOC, LOAD, DATA
In this case the section contents appear to be valid, only the names are corrupt. In other cases at least addresses are also incorrect.
See LLVM PR 23214, comment 37 for the original case that found this issue.
Diff:
Thanks! This seems to be a long standing bug. Should be fixed by [r3507].
Related
Commit: [r3507]
Confirmed, thank you for the fix!
One other difference I noticed during the investigation that prompted this ticket is that GNU objcopy maintains a relationship (modulo 2K alignment) between the VMA and file offset, while elfcopy does not. GNU objcopy also drops the READONLY flag on .data. I'm not sure what PE specs say about either of these, and whether it's important or not.
For example from GNU objcopy:
From elfcopy: