Bug 23299 - macOS: objcopy renders object files unusable
Summary: macOS: objcopy renders object files unusable
Status: RESOLVED FIXED
Alias: None
Product: binutils
Classification: Unclassified
Component: binutils (show other bugs)
Version: 2.31
: P2 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2018-06-17 00:59 UTC by mephi42
Modified: 2018-06-20 09:48 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Last reconfirmed:


Attachments
initial object file (239 bytes, application/octet-stream)
2018-06-18 15:48 UTC, mephi42
Details
corrupted object file (237 bytes, application/octet-stream)
2018-06-18 15:49 UTC, mephi42
Details
Proposed patch (1.79 KB, patch)
2018-06-19 17:19 UTC, Nick Clifton
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description mephi42 2018-06-17 00:59:55 UTC
$ 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.
Comment 1 Nick Clifton 2018-06-18 12:17:37 UTC
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
Comment 2 mephi42 2018-06-18 15:48:43 UTC
Created attachment 11080 [details]
initial object file
Comment 3 mephi42 2018-06-18 15:49:27 UTC
Created attachment 11081 [details]
corrupted object file
Comment 4 Nick Clifton 2018-06-19 17:19:51 UTC
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.
Comment 5 mephi42 2018-06-19 23:05:52 UTC
With your patch the problem is gone. Thanks!!
Comment 6 Sourceware Commits 2018-06-20 09:44:08 UTC
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.
Comment 7 Nick Clifton 2018-06-20 09:48:28 UTC
Hi Mephi,

  Great - I have applied the patch to the sources.

Cheers
  Nick