https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96971

            Bug ID: 96971
           Summary: libbacktrace identifies object format as pecoff
                    instead of macho
           Product: gcc
           Version: 11.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libbacktrace
          Assignee: unassigned at gcc dot gnu.org
          Reporter: fxcoudert at gcc dot gnu.org
                CC: ian at gcc dot gnu.org
  Target Milestone: ---

This is happening on the current port of GCC to aarch64-apple-darwin20
(https://github.com/iains/gcc-darwin-arm64/issues/25) but I believe the issue
is more general.

The file format detection is done by running the libbacktrace/filetype.awk AWK
script on an object file, generated by the compiler. During a bootstrap on
aarch64-apple-darwin20, I see a weird behaviour:

../../ibin/libbacktrace/config.log:libbacktrace_cv_sys_filetype=pecoff
../../ibin/prev-libbacktrace/config.log:libbacktrace_cv_sys_filetype=macho
../../ibin/stage1-libbacktrace/config.log:libbacktrace_cv_sys_filetype=macho

so the stage 3 libbacktrace is getting pecoff instead of macho. Yet, it is a
correct macho object file, with the right signature:

$ xxd /tmp/conftest.17440.o                                      
00000000: cffa edfe 0c00 0001 0000 0000 0100 0000  ................
00000010: 0400 0000 3804 0000 0020 0000 0000 0000  ....8.... ......
00000020: 1900 0000 b803 0000 0000 0000 0000 0000  ................
00000030: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000040: 2c02 0000 0000 0000 5804 0000 0000 0000  ,.......X.......
00000050: 2802 0000 0000 0000 0700 0000 0700 0000  (...............
00000060: 0b00 0000 0000 0000 5f5f 7465 7874 0000  ........__text..
00000070: 0000 0000 0000 0000 5f5f 5445 5854 0000  ........__TEXT..
00000080: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000090: 0000 0000 0000 0000 5804 0000 0000 0000  ........X.......
000000a0: 0000 0000 0000 0000 0000 0080 0000 0000  ................
000000b0: 0000 0000 0000 0000 5f5f 7465 7874 5f73  ........__text_s
000000c0: 7461 7274 7570 0000 5f5f 5445 5854 0000  tartup..__TEXT..
000000d0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
000000e0: 0800 0000 0000 0000 5804 0000 0400 0000  ........X.......
000000f0: 0000 0000 0000 0000 0004 0080 0000 0000  ................
00000100: 0000 0000 0000 0000 5f5f 636f 6d6d 6f6e  ........__common
00000110: 0000 0000 0000 0000 5f5f 4441 5441 0000  ........__DATA..
00000120: 0000 0000 0000 0000 2802 0000 0000 0000  ........(.......
00000130: 0400 0000 0000 0000 0000 0000 0200 0000  ................
00000140: 0000 0000 0000 0000 0100 0000 0000 0000  ................
00000150: 0000 0000 0000 0000 5f5f 6465 6275 675f  ........__debug_
00000160: 696e 666f 0000 0000 5f5f 4457 4152 4600  info....__DWARF.
00000170: 0000 0000 0000 0000 0800 0000 0000 0000  ................
00000180: ed00 0000 0000 0000 6004 0000 0000 0000  ........`.......
00000190: 8006 0000 0300 0000 0000 0002 0000 0000  ................
000001a0: 0000 0000 0000 0000 5f5f 6465 6275 675f  ........__debug_
000001b0: 6162 6272 6576 0000 5f5f 4457 4152 4600  abbrev..__DWARF.
000001c0: 0000 0000 0000 0000 f500 0000 0000 0000  ................
000001d0: 5700 0000 0000 0000 4d05 0000 0000 0000  W.......M.......
000001e0: 0000 0000 0000 0000 0000 0002 0000 0000  ................
000001f0: 0000 0000 0000 0000 5f5f 6465 6275 675f  ........__debug_
00000200: 7075 626e 616d 6573 5f5f 4457 4152 4600  pubnames__DWARF.
00000210: 0000 0000 0000 0000 4c01 0000 0000 0000  ........L.......
00000220: 2700 0000 0000 0000 a405 0000 0000 0000  '...............
00000230: 0000 0000 0000 0000 0000 0002 0000 0000  ................
00000240: 0000 0000 0000 0000 5f5f 6465 6275 675f  ........__debug_
00000250: 7075 6274 7970 6573 5f5f 4457 4152 4600  pubtypes__DWARF.
00000260: 0000 0000 0000 0000 7301 0000 0000 0000  ........s.......
00000270: 1a00 0000 0000 0000 cb05 0000 0000 0000  ................
00000280: 0000 0000 0000 0000 0000 0002 0000 0000  ................
00000290: 0000 0000 0000 0000 5f5f 6465 6275 675f  ........__debug_
000002a0: 6172 616e 6765 7300 5f5f 4457 4152 4600  aranges.__DWARF.
000002b0: 0000 0000 0000 0000 8d01 0000 0000 0000  ................
000002c0: 3000 0000 0000 0000 e505 0000 0000 0000  0...............
000002d0: 9806 0000 0100 0000 0000 0002 0000 0000  ................
000002e0: 0000 0000 0000 0000 5f5f 6465 6275 675f  ........__debug_
000002f0: 6c69 6e65 0000 0000 5f5f 4457 4152 4600  line....__DWARF.
00000300: 0000 0000 0000 0000 bd01 0000 0000 0000  ................
00000310: 4a00 0000 0000 0000 1506 0000 0000 0000  J...............
00000320: a006 0000 0100 0000 0000 0002 0000 0000  ................
00000330: 0000 0000 0000 0000 5f5f 6465 6275 675f  ........__debug_
00000340: 7374 7200 0000 0000 5f5f 4457 4152 4600  str.....__DWARF.
00000350: 0000 0000 0000 0000 0702 0000 0000 0000  ................
00000360: 0000 0000 0000 0000 5f06 0000 0000 0000  ........_.......
00000370: 0000 0000 0000 0000 0000 0002 0000 0000  ................
00000380: 0000 0000 0000 0000 5f5f 636f 6d70 6163  ........__compac
00000390: 745f 756e 7769 6e64 5f5f 4c44 0000 0000  t_unwind__LD....
000003a0: 0000 0000 0000 0000 0802 0000 0000 0000  ................
000003b0: 2000 0000 0000 0000 6006 0000 0300 0000   .......`.......
000003c0: a806 0000 0100 0000 0000 0002 0000 0000  ................
000003d0: 0000 0000 0000 0000 3200 0000 1800 0000  ........2.......
000003e0: 0100 0000 0000 0b00 0000 0000 0000 0000  ................
000003f0: 0200 0000 1800 0000 b006 0000 0900 0000  ................
00000400: 4007 0000 3400 0000 0b00 0000 5000 0000  @...4.......P...
00000410: 0000 0000 0700 0000 0700 0000 0200 0000  ................
00000420: 0900 0000 0000 0000 0000 0000 0000 0000  ................
00000430: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000440: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000450: 0000 0000 0000 0000 0000 8052 c003 5fd6  ...........R.._.
00000460: e900 0000 0200 0000 0000 0801 474e 5520  ............GNU 
00000470: 4331 3720 3131 2e30 2e30 2032 3032 3030  C17 11.0.0 20200
00000480: 3930 3720 2865 7870 6572 696d 656e 7461  907 (experimenta
00000490: 6c29 202d 6650 4943 202d 6d6d 6163 6f73  l) -fPIC -mmacos
000004a0: 782d 7665 7273 696f 6e2d 6d69 6e3d 3131  x-version-min=11
000004b0: 2e30 2e30 202d 6d6c 6974 746c 652d 656e  .0.0 -mlittle-en
000004c0: 6469 616e 202d 6d61 6269 3d6c 7036 3420  dian -mabi=lp64 
000004d0: 2d67 202d 4f32 0001 636f 6e66 7465 7374  -g -O2..conftest
000004e0: 2e63 002f 5573 6572 732f 6678 2f69 6269  .c./Users/fx/ibi
000004f0: 6e2f 6c69 6262 6163 6b74 7261 6365 0001  n/libbacktrace..
00000500: 0000 0000 0269 0001 1d05 b900 0000 0109  .....i..........
00000510: 0328 0200 0000 0000 0003 0405 696e 7400  .(..........int.
00000520: 0401 6d61 696e 0001 1f01 b900 0000 0000  ..main..........
00000530: 0000 0000 0000 0800 0000 0000 0000 028f  ................
00000540: 0005 6a00 0121 05b9 0000 0000 0001 1101  ..j..!..........
00000550: 2508 130b 0308 1b08 b442 0c10 0600 0002  %........B......
00000560: 3400 0308 3a0b 3b0b 390b 4913 3f0c 020a  4...:.;.9.I.?...
00000570: 0000 0324 000b 0b3e 0b03 0800 0004 2e01  ...$...>........
00000580: 3f0c 0308 3a0b 3b0b 390b 4913 1101 1201  ?...:.;.9.I.....
00000590: 400a 0000 0534 0003 083a 0b3b 0b39 0b49  @....4...:.;.9.I
000005a0: 1300 0000 2300 0000 0200 0000 0000 ed00  ....#...........
000005b0: 0000 a400 0000 6900 a400 0000 6900 c000  ......i.....i...
000005c0: 0000 6d61 696e 0000 0000 0016 0000 0002  ..main..........
000005d0: 0000 0000 00ed 0000 00b9 0000 0069 6e74  .............int
000005e0: 0000 0000 002c 0000 0002 0000 0000 0008  .....,..........
000005f0: 0000 0000 0000 0000 0000 0000 0008 0000  ................
00000600: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000610: 0000 0000 0046 0000 0004 0022 0000 0001  .....F....."....
00000620: 0101 fb0e 0d00 0101 0101 0000 0001 0000  ................
00000630: 0100 636f 6e66 7465 7374 2e63 0000 0000  ..conftest.c....
00000640: 0005 0100 0902 0000 0000 0000 0000 031f  ................
00000650: 0113 0503 1313 0501 0613 0208 0001 0100  ................
00000660: 0000 0000 0000 0000 0800 0000 0000 0002  ................
00000670: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000680: d600 0000 0200 0006 ce00 0000 0200 0006  ................
00000690: b100 0000 0300 0006 1000 0000 0200 0006  ................
000006a0: 3100 0000 0200 0006 0000 0000 0200 0006  1...............
000006b0: 2e00 0000 0e01 0000 0000 0000 0000 0000  ................
000006c0: 2800 0000 0e02 0000 0000 0000 0000 0000  (...............
000006d0: 2200 0000 0e03 0000 2802 0000 0000 0000  ".......(.......
000006e0: 1c00 0000 0e06 0000 4c01 0000 0000 0000  ........L.......
000006f0: 1600 0000 0e07 0000 7301 0000 0000 0000  ........s.......
00000700: 1000 0000 0e08 0000 8d01 0000 0000 0000  ................
00000710: 0a00 0000 0e0b 0000 0802 0000 0000 0000  ................
00000720: 0700 0000 0f03 0000 2802 0000 0000 0000  ........(.......
00000730: 0100 0000 0f02 0000 0000 0000 0000 0000  ................
00000740: 005f 6d61 696e 005f 6900 6c74 6d70 3600  ._main._i.ltmp6.
00000750: 6c74 6d70 3500 6c74 6d70 3400 6c74 6d70  ltmp5.ltmp4.ltmp
00000760: 3300 6c74 6d70 3200 6c74 6d70 3100 6c74  3.ltmp2.ltmp1.lt
00000770: 6d70 3000                                mp0.

but some bytes later in the file appear to be matching the PECOFF signature.

The filetype.awk script needs to be adapted, I see two possibilities:
- either the check for signatures is made to match only the beginning of the
file, with ^ at the beginning of each pattern
- or the macho tests are moved above the other ones, since their signature is
more specific (and less likely to happen by random in another object file)

I have tested the second solution, and it works fine:

diff --git a/libbacktrace/filetype.awk b/libbacktrace/filetype.awk
index 14d91581f7e..5cc31a07f46 100644
--- a/libbacktrace/filetype.awk
+++ b/libbacktrace/filetype.awk
@@ -1,13 +1,13 @@
 # An awk script to determine the type of a file.
-/\177ELF\001/      { if (NR == 1) { print "elf32"; exit } }
-/\177ELF\002/      { if (NR == 1) { print "elf64"; exit } }
-/\114\001/         { if (NR == 1) { print "pecoff"; exit } }
-/\144\206/         { if (NR == 1) { print "pecoff"; exit } }
-/\001\337/         { if (NR == 1) { print "xcoff32"; exit } }
-/\001\367/         { if (NR == 1) { print "xcoff64"; exit } }
 /\376\355\372\316/ { if (NR == 1) { print "macho"; exit } }
 /\316\372\355\376/ { if (NR == 1) { print "macho"; exit } }
 /\376\355\372\317/ { if (NR == 1) { print "macho"; exit } }
 /\317\372\355\376/ { if (NR == 1) { print "macho"; exit } }
 /\312\376\272\276/ { if (NR == 1) { print "macho"; exit } }
 /\276\272\376\312/ { if (NR == 1) { print "macho"; exit } }
+/\177ELF\001/      { if (NR == 1) { print "elf32"; exit } }
+/\177ELF\002/      { if (NR == 1) { print "elf64"; exit } }
+/\114\001/         { if (NR == 1) { print "pecoff"; exit } }
+/\144\206/         { if (NR == 1) { print "pecoff"; exit } }
+/\001\337/         { if (NR == 1) { print "xcoff32"; exit } }
+/\001\367/         { if (NR == 1) { print "xcoff64"; exit } }

Reply via email to