gcc编译选项可以设置生成调试信息,
debug信息格式有stabs,coff,xcoff,dwarf。
常用的有两种格式,stab和dwarf,stab较早,dwarf较新。两种格式介绍:https://www.ibm.com/developerworks/cn/opensource/os-debugging/
单独-g可以生成只有gdb能识别的额外辅助信息,如果不想生成这些辅助信息,可以使用-gstabs等选项,只生成stabs调试信息,不生成额外辅助信息。
调试选项-g和优化选项-O一起使用的话,可能会导致调试信息与最终不一致,比如,定义的变量被优化掉了等,因此,-g时最好不用-O。
-g Produce debugging information in the operating system‘s native format (stabs, COFF, XCOFF, or DWARF). GDB can work with this debugging information. On most systems that use stabs format, `-g‘ enables use of extra debugging information that only GDB can use; this extra information makes debugging work better in GDB but will probably make other debuggers crash or refuse to read the program. If you want to control for certain whether to generate the extra information, use `-gstabs+‘ , `-gstabs‘ , `-gxcoff+‘ , `-gxcoff‘ , `-gdwarf-1+‘ , or `-gdwarf-1‘ (see below). Unlike most other C compilers, GCC allows you to use `-g‘ with `-O‘ . The shortcuts taken by optimized code may occasionally produce surprising results: some variables you declared may not exist at all; flow of control may briefly move where you did not expect it; some statements may not be executed because they compute constant results or their values were already at hand; some statements may execute in different places because they were moved out of loops.
添加-g选项后,生成的elf文件节头表如下:stab长度为0x240=576,stabstr长度为0x4de=1246,共1822字节
原来的elf为839字节,新elf为2787字节。增加的debug信息比原来的全部elf,1822>839。
Section Headers: [Nr] Name Type Addr Off Size ES Flg Lk Inf Al [ 0] NULL 00000000 000000 000000 00 0 0 0 [ 1] .text PROGBITS 70000000 000054 000050 00 AX 0 0 4 [ 2] .data PROGBITS 70000050 0000a4 000004 00 WA 0 0 4 [ 3] .bss NOBITS 70000054 0000a8 000004 00 WA 0 0 4 [ 4] .stab PROGBITS 00000000 0000a8 000240 0c 6 0 4 [ 5] .comment PROGBITS 00000000 0002e8 000012 00 0 0 1 [ 6] .stabstr STRTAB 00000000 0002fa 0004de 00 0 0 1 [ 7] .shstrtab STRTAB 00000000 0007d8 000044 00 0 0 1 [ 8] .symtab SYMTAB 00000000 0009ac 000110 10 9 c 4 [ 9] .strtab STRTAB 00000000 000abc 000027 00 0 0 1
原文:https://www.cnblogs.com/yanhc/p/12311009.html