How is gcc adding debug symbols by default?

I noticed that gcc on NixOS adds debug symbols to my compiled executables by default. I tried passing the -g0 option to no avail. The only way to remove them is to pass -s or use strip.

Given that builds get stripped automatically in nix, I understand that debug symbols might be added automatically on purpose. I am however unable to figure out how this is done. I looked in:

  1. options passed to gcc (see reproducer below)
  2. stdenv and build-support scripts
  3. gcc package definition
  4. various C and debug sections of the manual

but could not find anything explaining that behavior. Any pointers would be greatly appreciated.

Here is a minimal reproducer showing the behavior. It uses the -v option to gcc to try and spot options that could turn on debugging (I could not see any):

$ cat test.sh
#!/run/current-system/sw/bin/bash

gcc=$(readlink -f $(which gcc))
echo gcc=$gcc
echo "int main() {}" > main.c
# Start from an empty environment with `env -i`
env -i $gcc -v -g0 main.c
# Notice that the debug symbols are there.
# (Also verified that gdb does not complain about missing debug symbols.)
objdump -h a.out | grep .debug
$ ./test.sh 
gcc=/nix/store/ryy81g8zv6c8fh0j10m6fdynrgrn239k-gcc-wrapper-9.3.0/bin/gcc
Using built-in specs.
COLLECT_GCC=/nix/store/fvf3qjqa5qpcjjkq37pb6ypnk1mzhf5h-gcc-9.3.0/bin/gcc
COLLECT_LTO_WRAPPER=/nix/store/fvf3qjqa5qpcjjkq37pb6ypnk1mzhf5h-gcc-9.3.0/libexec/gcc/x86_64-unknown-linux-gnu/9.3.0/lto-wrapper
Target: x86_64-unknown-linux-gnu
Configured with: 
Thread model: posix
gcc version 9.3.0 (GCC) 
COLLECT_GCC_OPTIONS='-v' '-g0' '-B' '/nix/store/c10296m7xgm3ksibcklb2xf48jr635x3-gcc-9.3.0-lib/lib' '-B' '/nix/store/0c7c96gikmzv87i7lv3vq5s1cmfjd6zf-glibc-2.31-74/lib/' '-idirafter' '/nix/store/ch44wa8qqss0dm04lnvf5n7mfh23kmrv-glibc-2.31-74-dev/include' '-idirafter' '/nix/store/fvf3qjqa5qpcjjkq37pb6ypnk1mzhf5h-gcc-9.3.0/lib/gcc/x86_64-unknown-linux-gnu/9.3.0/include-fixed' '-B' '/nix/store/ryy81g8zv6c8fh0j10m6fdynrgrn239k-gcc-wrapper-9.3.0/bin/' '-L/nix/store/0c7c96gikmzv87i7lv3vq5s1cmfjd6zf-glibc-2.31-74/lib' '-L/nix/store/c10296m7xgm3ksibcklb2xf48jr635x3-gcc-9.3.0-lib/lib' '-mtune=generic' '-march=x86-64'
 /nix/store/fvf3qjqa5qpcjjkq37pb6ypnk1mzhf5h-gcc-9.3.0/libexec/gcc/x86_64-unknown-linux-gnu/9.3.0/cc1 -quiet -v -idirafter /nix/store/ch44wa8qqss0dm04lnvf5n7mfh23kmrv-glibc-2.31-74-dev/include -idirafter /nix/store/fvf3qjqa5qpcjjkq37pb6ypnk1mzhf5h-gcc-9.3.0/lib/gcc/x86_64-unknown-linux-gnu/9.3.0/include-fixed main.c -quiet -dumpbase main.c -mtune=generic -march=x86-64 -auxbase main -g0 -version -o /tmp/cc4kR5S0.s
GNU C17 (GCC) version 9.3.0 (x86_64-unknown-linux-gnu)
        compiled by GNU C version 9.3.0, GMP version 6.2.0, MPFR version 4.1.0, MPC version 1.2.0, isl version isl-0.17.1-GMP

GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
ignoring nonexistent directory "/nix/store/fvf3qjqa5qpcjjkq37pb6ypnk1mzhf5h-gcc-9.3.0/lib/gcc/x86_64-unknown-linux-gnu/9.3.0/../../../../x86_64-unknown-linux-gnu/include"
ignoring duplicate directory "/nix/store/ch44wa8qqss0dm04lnvf5n7mfh23kmrv-glibc-2.31-74-dev/include"
ignoring duplicate directory "/nix/store/fvf3qjqa5qpcjjkq37pb6ypnk1mzhf5h-gcc-9.3.0/lib/gcc/x86_64-unknown-linux-gnu/9.3.0/include-fixed"
#include "..." search starts here:
#include <...> search starts here:
 /nix/store/fvf3qjqa5qpcjjkq37pb6ypnk1mzhf5h-gcc-9.3.0/lib/gcc/x86_64-unknown-linux-gnu/9.3.0/include
 /nix/store/fvf3qjqa5qpcjjkq37pb6ypnk1mzhf5h-gcc-9.3.0/include
 /nix/store/fvf3qjqa5qpcjjkq37pb6ypnk1mzhf5h-gcc-9.3.0/lib/gcc/x86_64-unknown-linux-gnu/9.3.0/include-fixed
 /nix/store/ch44wa8qqss0dm04lnvf5n7mfh23kmrv-glibc-2.31-74-dev/include
End of search list.
GNU C17 (GCC) version 9.3.0 (x86_64-unknown-linux-gnu)
        compiled by GNU C version 9.3.0, GMP version 6.2.0, MPFR version 4.1.0, MPC version 1.2.0, isl version isl-0.17.1-GMP

GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: b12d137b95ea690c42a03dbb614eae9a
COLLECT_GCC_OPTIONS='-v' '-g0' '-B' '/nix/store/c10296m7xgm3ksibcklb2xf48jr635x3-gcc-9.3.0-lib/lib' '-B' '/nix/store/0c7c96gikmzv87i7lv3vq5s1cmfjd6zf-glibc-2.31-74/lib/' '-idirafter' '/nix/store/ch44wa8qqss0dm04lnvf5n7mfh23kmrv-glibc-2.31-74-dev/include' '-idirafter' '/nix/store/fvf3qjqa5qpcjjkq37pb6ypnk1mzhf5h-gcc-9.3.0/lib/gcc/x86_64-unknown-linux-gnu/9.3.0/include-fixed' '-B' '/nix/store/ryy81g8zv6c8fh0j10m6fdynrgrn239k-gcc-wrapper-9.3.0/bin/' '-L/nix/store/0c7c96gikmzv87i7lv3vq5s1cmfjd6zf-glibc-2.31-74/lib' '-L/nix/store/c10296m7xgm3ksibcklb2xf48jr635x3-gcc-9.3.0-lib/lib' '-mtune=generic' '-march=x86-64'
 /nix/store/ryy81g8zv6c8fh0j10m6fdynrgrn239k-gcc-wrapper-9.3.0/bin/as -v --64 -o /tmp/ccgVJSm0.o /tmp/cc4kR5S0.s
GNU assembler version 2.31.1 (x86_64-unknown-linux-gnu) using BFD version (GNU Binutils) 2.31.1
COMPILER_PATH=/nix/store/c10296m7xgm3ksibcklb2xf48jr635x3-gcc-9.3.0-lib/lib/:/nix/store/0c7c96gikmzv87i7lv3vq5s1cmfjd6zf-glibc-2.31-74/lib/:/nix/store/ryy81g8zv6c8fh0j10m6fdynrgrn239k-gcc-wrapper-9.3.0/bin/:/nix/store/fvf3qjqa5qpcjjkq37pb6ypnk1mzhf5h-gcc-9.3.0/libexec/gcc/x86_64-unknown-linux-gnu/9.3.0/:/nix/store/fvf3qjqa5qpcjjkq37pb6ypnk1mzhf5h-gcc-9.3.0/libexec/gcc/x86_64-unknown-linux-gnu/9.3.0/:/nix/store/fvf3qjqa5qpcjjkq37pb6ypnk1mzhf5h-gcc-9.3.0/libexec/gcc/x86_64-unknown-linux-gnu/:/nix/store/fvf3qjqa5qpcjjkq37pb6ypnk1mzhf5h-gcc-9.3.0/lib/gcc/x86_64-unknown-linux-gnu/9.3.0/:/nix/store/fvf3qjqa5qpcjjkq37pb6ypnk1mzhf5h-gcc-9.3.0/lib/gcc/x86_64-unknown-linux-gnu/
LIBRARY_PATH=/nix/store/c10296m7xgm3ksibcklb2xf48jr635x3-gcc-9.3.0-lib/lib/:/nix/store/0c7c96gikmzv87i7lv3vq5s1cmfjd6zf-glibc-2.31-74/lib/:/nix/store/ryy81g8zv6c8fh0j10m6fdynrgrn239k-gcc-wrapper-9.3.0/bin/:/nix/store/fvf3qjqa5qpcjjkq37pb6ypnk1mzhf5h-gcc-9.3.0/lib/gcc/x86_64-unknown-linux-gnu/9.3.0/:/nix/store/fvf3qjqa5qpcjjkq37pb6ypnk1mzhf5h-gcc-9.3.0/lib/gcc/x86_64-unknown-linux-gnu/9.3.0/../../../../lib64/:/nix/store/fvf3qjqa5qpcjjkq37pb6ypnk1mzhf5h-gcc-9.3.0/lib/gcc/x86_64-unknown-linux-gnu/9.3.0/../../../
COLLECT_GCC_OPTIONS='-v' '-g0' '-B' '/nix/store/c10296m7xgm3ksibcklb2xf48jr635x3-gcc-9.3.0-lib/lib' '-B' '/nix/store/0c7c96gikmzv87i7lv3vq5s1cmfjd6zf-glibc-2.31-74/lib/' '-idirafter' '/nix/store/ch44wa8qqss0dm04lnvf5n7mfh23kmrv-glibc-2.31-74-dev/include' '-idirafter' '/nix/store/fvf3qjqa5qpcjjkq37pb6ypnk1mzhf5h-gcc-9.3.0/lib/gcc/x86_64-unknown-linux-gnu/9.3.0/include-fixed' '-B' '/nix/store/ryy81g8zv6c8fh0j10m6fdynrgrn239k-gcc-wrapper-9.3.0/bin/' '-L/nix/store/0c7c96gikmzv87i7lv3vq5s1cmfjd6zf-glibc-2.31-74/lib' '-L/nix/store/c10296m7xgm3ksibcklb2xf48jr635x3-gcc-9.3.0-lib/lib' '-mtune=generic' '-march=x86-64'
 /nix/store/fvf3qjqa5qpcjjkq37pb6ypnk1mzhf5h-gcc-9.3.0/libexec/gcc/x86_64-unknown-linux-gnu/9.3.0/collect2 -plugin /nix/store/fvf3qjqa5qpcjjkq37pb6ypnk1mzhf5h-gcc-9.3.0/libexec/gcc/x86_64-unknown-linux-gnu/9.3.0/liblto_plugin.so -plugin-opt=/nix/store/fvf3qjqa5qpcjjkq37pb6ypnk1mzhf5h-gcc-9.3.0/libexec/gcc/x86_64-unknown-linux-gnu/9.3.0/lto-wrapper -plugin-opt=-fresolution=/tmp/cc5wvg41.res -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lgcc_s -plugin-opt=-pass-through=-lc -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lgcc_s --eh-frame-hdr -m elf_x86_64 -dynamic-linker /nix/store/0c7c96gikmzv87i7lv3vq5s1cmfjd6zf-glibc-2.31-74/lib64/ld-linux-x86-64.so.2 /nix/store/0c7c96gikmzv87i7lv3vq5s1cmfjd6zf-glibc-2.31-74/lib/crt1.o /nix/store/0c7c96gikmzv87i7lv3vq5s1cmfjd6zf-glibc-2.31-74/lib/crti.o /nix/store/fvf3qjqa5qpcjjkq37pb6ypnk1mzhf5h-gcc-9.3.0/lib/gcc/x86_64-unknown-linux-gnu/9.3.0/crtbegin.o -L/nix/store/0c7c96gikmzv87i7lv3vq5s1cmfjd6zf-glibc-2.31-74/lib -L/nix/store/c10296m7xgm3ksibcklb2xf48jr635x3-gcc-9.3.0-lib/lib -L/nix/store/c10296m7xgm3ksibcklb2xf48jr635x3-gcc-9.3.0-lib/lib -L/nix/store/0c7c96gikmzv87i7lv3vq5s1cmfjd6zf-glibc-2.31-74/lib -L/nix/store/ryy81g8zv6c8fh0j10m6fdynrgrn239k-gcc-wrapper-9.3.0/bin -L/nix/store/fvf3qjqa5qpcjjkq37pb6ypnk1mzhf5h-gcc-9.3.0/lib/gcc/x86_64-unknown-linux-gnu/9.3.0 -L/nix/store/fvf3qjqa5qpcjjkq37pb6ypnk1mzhf5h-gcc-9.3.0/lib/gcc/x86_64-unknown-linux-gnu/9.3.0/../../../../lib64 -L/nix/store/fvf3qjqa5qpcjjkq37pb6ypnk1mzhf5h-gcc-9.3.0/lib/gcc/x86_64-unknown-linux-gnu/9.3.0/../../.. -dynamic-linker /nix/store/0c7c96gikmzv87i7lv3vq5s1cmfjd6zf-glibc-2.31-74/lib/ld-linux-x86-64.so.2 /tmp/ccgVJSm0.o -lgcc --push-state --as-needed -lgcc_s --pop-state -lc -lgcc --push-state --as-needed -lgcc_s --pop-state /nix/store/fvf3qjqa5qpcjjkq37pb6ypnk1mzhf5h-gcc-9.3.0/lib/gcc/x86_64-unknown-linux-gnu/9.3.0/crtend.o /nix/store/0c7c96gikmzv87i7lv3vq5s1cmfjd6zf-glibc-2.31-74/lib/crtn.o
COLLECT_GCC_OPTIONS='-v' '-g0' '-B' '/nix/store/c10296m7xgm3ksibcklb2xf48jr635x3-gcc-9.3.0-lib/lib' '-B' '/nix/store/0c7c96gikmzv87i7lv3vq5s1cmfjd6zf-glibc-2.31-74/lib/' '-idirafter' '/nix/store/ch44wa8qqss0dm04lnvf5n7mfh23kmrv-glibc-2.31-74-dev/include' '-idirafter' '/nix/store/fvf3qjqa5qpcjjkq37pb6ypnk1mzhf5h-gcc-9.3.0/lib/gcc/x86_64-unknown-linux-gnu/9.3.0/include-fixed' '-B' '/nix/store/ryy81g8zv6c8fh0j10m6fdynrgrn239k-gcc-wrapper-9.3.0/bin/' '-L/nix/store/0c7c96gikmzv87i7lv3vq5s1cmfjd6zf-glibc-2.31-74/lib' '-L/nix/store/c10296m7xgm3ksibcklb2xf48jr635x3-gcc-9.3.0-lib/lib' '-mtune=generic' '-march=x86-64'
 23 .debug_aranges 000000d0  0000000000000000  0000000000000000  00003050  2**4
 24 .debug_info   000000b2  0000000000000000  0000000000000000  00003120  2**0
 25 .debug_abbrev 0000006d  0000000000000000  0000000000000000  000031d2  2**0
 26 .debug_line   00000149  0000000000000000  0000000000000000  0000323f  2**0
 27 .debug_str    00000163  0000000000000000  0000000000000000  00003388  2**0
 28 .debug_ranges 00000080  0000000000000000  0000000000000000  000034f0  2**4

Maybe it’s the compiler wrapper?

The gcc wrapper that nix uses to build everything will add debug info to builds. If you’re using the standard mkDerivation helper will allow you to retain the debug information by saying dontStrip = true;, otherwise part of the fixupPhase will strip the binaries.

I tried passing the -g0 option to no avail

You can try NIX_CFLAGS_COMPILE

1 Like

My understanding is that gcc -v shows me what options were passed to gcc “proper”, as a way to detect what the wrapper did. I used NIX_DEBUG=1 to get similar results, but in a nicer form, but I do not see anything that would instruct gcc to add the debug symbols:

$ cat test.sh 
#!/run/current-system/sw/bin/bash

gcc=$(readlink -f $(which gcc))
echo gcc=$gcc
echo "int main() {}" > main.c
env -i NIX_DEBUG=1 $gcc -g0 main.c
objdump -h a.out | grep .debug
$ ./test.sh
gcc=/nix/store/ryy81g8zv6c8fh0j10m6fdynrgrn239k-gcc-wrapper-9.3.0/bin/gcc
HARDENING: disabled flags: fortify stackprotector strictoverflow format pic pie
extra flags before to /nix/store/fvf3qjqa5qpcjjkq37pb6ypnk1mzhf5h-gcc-9.3.0/bin/gcc:
  -Wl\,-dynamic-linker
  -Wl\,/nix/store/0c7c96gikmzv87i7lv3vq5s1cmfjd6zf-glibc-2.31-74/lib/ld-linux-x86-64.so.2
original flags to /nix/store/fvf3qjqa5qpcjjkq37pb6ypnk1mzhf5h-gcc-9.3.0/bin/gcc:
  -g0
  main.c
extra flags after to /nix/store/fvf3qjqa5qpcjjkq37pb6ypnk1mzhf5h-gcc-9.3.0/bin/gcc:
  -B/nix/store/c10296m7xgm3ksibcklb2xf48jr635x3-gcc-9.3.0-lib/lib
  -B/nix/store/0c7c96gikmzv87i7lv3vq5s1cmfjd6zf-glibc-2.31-74/lib/
  -idirafter
  /nix/store/ch44wa8qqss0dm04lnvf5n7mfh23kmrv-glibc-2.31-74-dev/include
  -idirafter
  /nix/store/fvf3qjqa5qpcjjkq37pb6ypnk1mzhf5h-gcc-9.3.0/lib/gcc/x86_64-unknown-linux-gnu/9.3.0/include-fixed
  -B/nix/store/ryy81g8zv6c8fh0j10m6fdynrgrn239k-gcc-wrapper-9.3.0/bin/
  -L/nix/store/0c7c96gikmzv87i7lv3vq5s1cmfjd6zf-glibc-2.31-74/lib
  -L/nix/store/c10296m7xgm3ksibcklb2xf48jr635x3-gcc-9.3.0-lib/lib
HARDENING: disabled flags: bindnow relro pie
extra flags before to /nix/store/vfqlryhvm8063hs7ax9k2vb8wmch5v0v-binutils-2.31.1/bin/ld:
  ''
original flags to /nix/store/vfqlryhvm8063hs7ax9k2vb8wmch5v0v-binutils-2.31.1/bin/ld:
  -plugin
  /nix/store/fvf3qjqa5qpcjjkq37pb6ypnk1mzhf5h-gcc-9.3.0/libexec/gcc/x86_64-unknown-linux-gnu/9.3.0/liblto_plugin.so
  -plugin-opt=/nix/store/fvf3qjqa5qpcjjkq37pb6ypnk1mzhf5h-gcc-9.3.0/libexec/gcc/x86_64-unknown-linux-gnu/9.3.0/lto-wrapper
  -plugin-opt=-fresolution=/tmp/cc5331uH.res
  -plugin-opt=-pass-through=-lgcc
  -plugin-opt=-pass-through=-lgcc_s
  -plugin-opt=-pass-through=-lc
  -plugin-opt=-pass-through=-lgcc
  -plugin-opt=-pass-through=-lgcc_s
  --eh-frame-hdr
  -m
  elf_x86_64
  -dynamic-linker
  /nix/store/0c7c96gikmzv87i7lv3vq5s1cmfjd6zf-glibc-2.31-74/lib64/ld-linux-x86-64.so.2
  /nix/store/0c7c96gikmzv87i7lv3vq5s1cmfjd6zf-glibc-2.31-74/lib/crt1.o
  /nix/store/0c7c96gikmzv87i7lv3vq5s1cmfjd6zf-glibc-2.31-74/lib/crti.o
  /nix/store/fvf3qjqa5qpcjjkq37pb6ypnk1mzhf5h-gcc-9.3.0/lib/gcc/x86_64-unknown-linux-gnu/9.3.0/crtbegin.o
  -L/nix/store/0c7c96gikmzv87i7lv3vq5s1cmfjd6zf-glibc-2.31-74/lib
  -L/nix/store/c10296m7xgm3ksibcklb2xf48jr635x3-gcc-9.3.0-lib/lib
  -L/nix/store/c10296m7xgm3ksibcklb2xf48jr635x3-gcc-9.3.0-lib/lib
  -L/nix/store/0c7c96gikmzv87i7lv3vq5s1cmfjd6zf-glibc-2.31-74/lib
  -L/nix/store/ryy81g8zv6c8fh0j10m6fdynrgrn239k-gcc-wrapper-9.3.0/bin
  -L/nix/store/fvf3qjqa5qpcjjkq37pb6ypnk1mzhf5h-gcc-9.3.0/lib/gcc/x86_64-unknown-linux-gnu/9.3.0
  -L/nix/store/fvf3qjqa5qpcjjkq37pb6ypnk1mzhf5h-gcc-9.3.0/lib/gcc/x86_64-unknown-linux-gnu/9.3.0/../../../../lib64
  -L/nix/store/fvf3qjqa5qpcjjkq37pb6ypnk1mzhf5h-gcc-9.3.0/lib/gcc/x86_64-unknown-linux-gnu/9.3.0/../../..
  -dynamic-linker
  /nix/store/0c7c96gikmzv87i7lv3vq5s1cmfjd6zf-glibc-2.31-74/lib/ld-linux-x86-64.so.2
  /tmp/ccgCgnlF.o
  -lgcc
  --push-state
  --as-needed
  -lgcc_s
  --pop-state
  -lc
  -lgcc
  --push-state
  --as-needed
  -lgcc_s
  --pop-state
  /nix/store/fvf3qjqa5qpcjjkq37pb6ypnk1mzhf5h-gcc-9.3.0/lib/gcc/x86_64-unknown-linux-gnu/9.3.0/crtend.o
  /nix/store/0c7c96gikmzv87i7lv3vq5s1cmfjd6zf-glibc-2.31-74/lib/crtn.o
extra flags after to /nix/store/vfqlryhvm8063hs7ax9k2vb8wmch5v0v-binutils-2.31.1/bin/ld:
  -rpath
  /nix/store/0c7c96gikmzv87i7lv3vq5s1cmfjd6zf-glibc-2.31-74/lib
  -rpath
  /nix/store/c10296m7xgm3ksibcklb2xf48jr635x3-gcc-9.3.0-lib/lib
 23 .debug_aranges 000000d0  0000000000000000  0000000000000000  00003050  2**4
 24 .debug_info   000000b2  0000000000000000  0000000000000000  00003120  2**0
 25 .debug_abbrev 0000006d  0000000000000000  0000000000000000  000031d2  2**0
 26 .debug_line   00000149  0000000000000000  0000000000000000  0000323f  2**0
 27 .debug_str    00000163  0000000000000000  0000000000000000  00003388  2**0
 28 .debug_ranges 00000080  0000000000000000  0000000000000000  000034f0  2**4

I am trying to figure how this is happening exactly. I do not see anything in the wrapper that instructs gcc to emit debug symbols, nor something that explains why -g0 would be ignored.

This does not seem to work either:

$ cat test.sh  
#!/run/current-system/sw/bin/bash

gcc=$(readlink -f $(which gcc))
echo gcc=$gcc
echo "int main() {}" > main.c
env -i NIX_CFLAGS_COMPILE=-g0 $gcc main.c
objdump -h a.out | grep .debug
$ ./test.sh 
gcc=/nix/store/ryy81g8zv6c8fh0j10m6fdynrgrn239k-gcc-wrapper-9.3.0/bin/gcc
 23 .debug_aranges 000000d0  0000000000000000  0000000000000000  00003050  2**4
 24 .debug_info   000000b2  0000000000000000  0000000000000000  00003120  2**0
 25 .debug_abbrev 0000006d  0000000000000000  0000000000000000  000031d2  2**0
 26 .debug_line   00000149  0000000000000000  0000000000000000  0000323f  2**0
 27 .debug_str    00000163  0000000000000000  0000000000000000  00003388  2**0
 28 .debug_ranges 00000080  0000000000000000  0000000000000000  000034f0  2**4

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.
3 Likes