$ cat main.c int main() {} $ gcc -c main.c $ gcc main.o -o main $ binutils-gdb/binutils/objcopy main.o main1.o $ gcc main1.o -o main1 ld: object file main1.o was built for different x86_64 sub-type (-2147483645) than link command line (3) file 'main1.o' for architecture x86_64 Culprit: --- a/bfd/mach-o-x86-64.c +++ b/bfd/mach-o-x86-64.c @@ -54,7 +54,7 @@ bfd_mach_o_x86_64_mkobject (bfd *abfd) mdata->header.magic = BFD_MACH_O_MH_MAGIC_64; mdata->header.cputype = BFD_MACH_O_CPU_TYPE_X86_64; mdata->header.cpusubtype = - BFD_MACH_O_CPU_SUBTYPE_X86_ALL | BFD_MACH_O_CPU_SUBTYPE_LIB64; + BFD_MACH_O_CPU_SUBTYPE_X86_ALL; mdata->header.byteorder = BFD_ENDIAN_LITTLE; mdata->header.version = 2; I'm not proposing to commit this, since I don't know what this flag is for - the following patch just demonstrates that removing it makes things work in this particular case. The proper fix might be to copy cpusubtype from ibfd to obfd, but I'm not sure what would be the right place for this.
Hi Mephi, > $ binutils-gdb/binutils/objcopy main.o main1.o Please could you upload a copy of the main.o file so that I can run some experiments ? Cheers Nick
Created attachment 11080 [details] initial object file
Created attachment 11081 [details] corrupted object file
Created attachment 11084 [details] Proposed patch Hi Mephi, Please could you try out this patch and let me know if it works for you ? Cheers Nick PS. The patch also improves objdump so the the -x option will print out the contents of the mach-o file header.
With your patch the problem is gone. Thanks!!
The master branch has been updated by Nick Clifton <nickc@sourceware.org>: https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=c6643fcc058d6b4aebca75818fbbb705837a9fa3 commit c6643fcc058d6b4aebca75818fbbb705837a9fa3 Author: Nick Clifton <nickc@redhat.com> Date: Wed Jun 20 10:43:00 2018 +0100 Stop objcopy from corrupting mach-o files. PR 23299 * mach-o.c (cputype): New function. (cpusubtype): New function. (bfd_mach_o_bfd_print_private_data): New function. Dispalys the values in the MACH-O file header. (bfd_mach_o_bfd_copy_private_header_data): Copy the cputype and cpusubtype fields from the input bfd's mach-o header to the output bfd. * mach-o-target.c (bfd_mach_o_bfd_print_private_bfd_data): Redefine to bfd_mach_o_bfd_print_private_data. * mach-o.h (bfd_mach_o_bfd_print_private_bfd_data): Prototype.
Hi Mephi, Great - I have applied the patch to the sources. Cheers Nick