Build binary with debug info.
gcc -g ...
g++ -g ...List sections using objdump.
objdump --section-headers binarySample: Build binary with debuginfo.
int sum(int a, int b)
{
return a + b;
}
int main()
{
sum(1, 2);
}
#-------------------------------
gcc -g demo.c -o demo
objdump --section-headers demo
#-------------------------------
Binary with debuginfo.
0. note.gnu.build-id
1. interp
2. gnu.hash
3. dynsym
4. dynstr
5. gnu.version
6. gnu.version_r
7. rela.dyn
8. rela.plt
9. init
10. plt
11. text
12. fini
13. rodata
14. eh_frame_hdr
15. eh_frame
16. note.ABI-tag
17. init_array
18. fini_array
19. dynamic
20. got
21. got.plt
22. data
23. bss
24. comment
25. annobin.notes
26. gnu.build.attributes
#-----------------------
27. debug_aranges
28. debug_info
29. debug_abbrev
30. debug_line
31. debug_str
32. debug_line_str
GDB allows to split the debuginfo of a binary into a separate file. The purpose is to optimize binary size. When debugging is needed later, the debug information can be reattached.
Extract debuginfo into a file.
objcopy --only-keep-debug binary debuginfoRemove debuginfo from binary.
strip --strip-debug binarySample: Strip a binary.
#-------------------------------
gcc -g demo.c -o demo
objcopy --only-keep-debug demo demo.debuginfo
strip --strip-debug demo
objdump --section-headers demo
#-------------------------------
Without debuginfo.
0. note.gnu.build-id
1. interp
2. gnu.hash
3. dynsym
4. dynstr
5. gnu.version
6. gnu.version_r
7. rela.dyn
8. rela.plt
9. init
10. plt
11. text
12. fini
13. rodata
14. eh_frame_hdr
15. eh_frame
16. note.ABI-tag
17. init_array
18. fini_array
19. dynamic
20. got
21. got.plt
22. data
23. bss
24. comment
25. annobin.notes
26. gnu.build.attributes
Creates a .gnu_debuglink section which contains a reference to debuginfo file and adds it to the binary file.
objcopy --add-gnu-debuglink=debuginfo binarySample: Attach debuginfo to binary
objcopy --add-gnu-debuglink=demo.debuginfo demoWith debuglink.
0. note.gnu.build-id
1. interp
2. gnu.hash
.. .....
24. comment
25. annobin.notes
26. gnu.build.attributes
#-----------------------
27. gnu_debuglink
Default, GDB searches for debuginfo files in the below directories.
However, we can change the search directories.
set debug-file-directory dirs
show debug-file-directory