I got to the bottom of it. Thanks @jonringer and @futile for your pointers.
gcc
does not add debug information related to the program I compile. The ELF section headers I see are actually from other object files which get linked into the final executable, such as Core OS C Runtime objects (crti.o
, crtn.o
, etc.).
The following snippet shows that gcc
does not indeed add the debug symbols during compilation (-c
), unless asked to with -g
:
$ cat test.sh
#!/run/current-system/sw/bin/bash
echo "int main() {}" > main.c
test_case() {
option=$1
if [[ $# -eq 0 ]]; then
msg="Without option"
gcc -c main.c
else
msg="With $option"
gcc $option -c main.c
fi
echo -n "$msg: "
if objdump -h main.o | grep -q .debug; then
echo "debug symbols"
else
echo "no debug symbols"
fi
}
test_case
test_case -g
test_case -g0
$ ./test.sh
Without option: no debug symbols
With -g: debug symbols
With -g0: no debug symbols
We can similarly observed that passing -g
does increase the size of the .debug_str
section from 0x163
to 0x1b5
and that -g0
is the same as not passing anything:
$ gcc main.c && objdump -h a.out | grep .debug_str
27 .debug_str 00000163 0000000000000000 0000000000000000 00003388 2**0
$ gcc -g main.c && objdump -h a.out | grep .debug_str
27 .debug_str 000001b5 0000000000000000 0000000000000000 00003481 2**0
$ gcc -g0 main.c && objdump -h a.out | grep .debug_str
27 .debug_str 00000163 0000000000000000 0000000000000000 00003388 2**0
And when looking at the content of the .debug_str
sections, we see the CRT-related information when not passing -g
, and the addition of some symbols such as main
when passing -g
:
$ gcc main.c && objdump -s -j .debug_str a.out
a.out: file format elf64-x86-64
Contents of section .debug_str:
0000 2e2e2f73 79736465 70732f78 38365f36 ../sysdeps/x86_6
0010 342f7374 6172742e 53002f62 75696c64 4/start.S./build
0020 2f676c69 62632d32 2e33312f 63737500 /glibc-2.31/csu.
0030 474e5520 41532032 2e33312e 3100696e GNU AS 2.31.1.in
0040 69742e63 00474e55 20433131 20382e33 it.c.GNU C11 8.3
0050 2e30202d 6d74756e 653d6765 6e657269 .0 -mtune=generi
0060 63202d6d 61726368 3d783836 2d363420 c -march=x86-64
0070 2d67202d 67676462 202d4f32 202d7374 -g -ggdb -O2 -st
0080 643d676e 75313120 2d666e6f 2d737472 d=gnu11 -fno-str
0090 6963742d 6f766572 666c6f77 202d6650 ict-overflow -fP
00a0 4943202d 66676e75 38392d69 6e6c696e IC -fgnu89-inlin
00b0 65202d66 6d657267 652d616c 6c2d636f e -fmerge-all-co
00c0 6e737461 6e747320 2d66726f 756e6469 nstants -froundi
00d0 6e672d6d 61746820 2d666d61 74682d65 ng-math -fmath-e
00e0 72726e6f 202d666e 6f2d7374 61636b2d rrno -fno-stack-
00f0 70726f74 6563746f 72202d66 746c732d protector -ftls-
0100 6d6f6465 6c3d696e 69746961 6c2d6578 model=initial-ex
0110 6563005f 494f5f73 7464696e 5f757365 ec._IO_stdin_use
0120 64007374 61746963 2d72656c 6f632e63 d.static-reloc.c
0130 002e2e2f 73797364 6570732f 7838365f .../sysdeps/x86_
0140 36342f63 7274692e 53002e2e 2f737973 64/crti.S.../sys
0150 64657073 2f783836 5f36342f 6372746e deps/x86_64/crtn
0160 2e5300 .S.
$ gcc -g main.c && objdump -s -j .debug_str a.out
a.out: file format elf64-x86-64
Contents of section .debug_str:
0000 2e2e2f73 79736465 70732f78 38365f36 ../sysdeps/x86_6
0010 342f7374 6172742e 53002f62 75696c64 4/start.S./build
0020 2f676c69 62632d32 2e33312f 63737500 /glibc-2.31/csu.
0030 474e5520 41532032 2e33312e 3100696e GNU AS 2.31.1.in
0040 69742e63 00474e55 20433131 20382e33 it.c.GNU C11 8.3
0050 2e30202d 6d74756e 653d6765 6e657269 .0 -mtune=generi
0060 63202d6d 61726368 3d783836 2d363420 c -march=x86-64
0070 2d67202d 67676462 202d4f32 202d7374 -g -ggdb -O2 -st
0080 643d676e 75313120 2d666e6f 2d737472 d=gnu11 -fno-str
0090 6963742d 6f766572 666c6f77 202d6650 ict-overflow -fP
00a0 4943202d 66676e75 38392d69 6e6c696e IC -fgnu89-inlin
00b0 65202d66 6d657267 652d616c 6c2d636f e -fmerge-all-co
00c0 6e737461 6e747320 2d66726f 756e6469 nstants -froundi
00d0 6e672d6d 61746820 2d666d61 74682d65 ng-math -fmath-e
00e0 72726e6f 202d666e 6f2d7374 61636b2d rrno -fno-stack-
00f0 70726f74 6563746f 72202d66 746c732d protector -ftls-
0100 6d6f6465 6c3d696e 69746961 6c2d6578 model=initial-ex
0110 6563005f 494f5f73 7464696e 5f757365 ec._IO_stdin_use
0120 64007374 61746963 2d72656c 6f632e63 d.static-reloc.c
0130 002e2e2f 73797364 6570732f 7838365f .../sysdeps/x86_
0140 36342f63 7274692e 53002f68 6f6d652f 64/crti.S./home/
0150 6464622f 746d702f 6763632d 64656275 ddb/tmp/gcc-debu
0160 67006d61 696e006d 61696e2e 6300474e g.main.main.c.GN
0170 55204331 3720392e 332e3020 2d6d7475 U C17 9.3.0 -mtu
0180 6e653d67 656e6572 6963202d 6d617263 ne=generic -marc
0190 683d7838 362d3634 202d6700 2e2e2f73 h=x86-64 -g.../s
01a0 79736465 70732f78 38365f36 342f6372 ysdeps/x86_64/cr
01b0 746e2e53 00 tn.S.