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生成操作系统固有的格式,对于sparc来说,使用-g会生成stab格式。
如果使用-g -gdwarf-2,则会生成dwarf-2格式的调试信息,不再生成stab格式的。这两个选项允许同时存在。
如果使用-gstabs -dwarf-2,则后一个-dwarf-2会并忽略,因为和stabs冲突,即生成stabs格式的调试信息。这两个是同时存在是不允许的。
-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
没有-g的节头表
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] .comment PROGBITS 00000000 0000a8 000012 00 0 0 1 [ 5] .shstrtab STRTAB 00000000 0000ba 000035 00 0 0 1 [ 6] .symtab SYMTAB 00000000 000230 0000f0 10 7 a 4 [ 7] .strtab STRTAB 00000000 000320 000027 00 0 0 1
shstrtab symtab section hdr
旧elf,839 0x35=53 0xf0=240 320
新elf,2787 0x44=68 0x110=272 400
增加量, 15 32 80
debug 1822+15+32+80=1949
2787-839=1948
stab大小为0x240=578,每个项目12字节,如下,共48个项目
struct internal_nlist { unsigned long n_strx; /* index into string table of name */ unsigned char n_type; /* type of symbol */ unsigned char n_other; /* misc info (usually empty) */ unsigned short n_desc; /* description field */ bfd_vma n_value; /* value of symbol */ };
stab段,0x8a开始,12字节一个项目,看不明白。
The “stabs” debug format: http://www.sm.luth.se/csee/courses/smd/D0013E/doc/stabs.pdf
链接:https://pan.baidu.com/s/1Fv3zcqNhScUdaHp4PyAYsQ
提取码:by76
增加-gdwarf选项后,生成的dwarf格式的节多了5个。
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 000074 000084 00 AX 0 0 4 [ 2] .data PROGBITS 70000084 0000f8 000004 00 WA 0 0 4 [ 3] .bss NOBITS 70000088 0000fc 000000 00 WA 0 0 1 [ 4] .rodata PROGBITS 00000000 000074 000000 00 A 0 0 1 [ 5] .line PROGBITS 00000000 0000fc 00009e 00 0 0 1 [ 6] .debug_pubnames PROGBITS 00000000 00019a 00002d 00 0 0 1 [ 7] .debug_aranges PROGBITS 00000000 0001c7 000031 00 0 0 1 [ 8] .debug PROGBITS 00000000 0001f8 00025c 00 0 0 4 [ 9] .comment PROGBITS 00000000 000454 000012 00 0 0 1 [10] .shstrtab STRTAB 00000000 000466 000069 00 0 0 1 [11] .symtab SYMTAB 00000000 0006d8 000130 10 12 f 4 [12] .strtab STRTAB 00000000 000808 000022 00 0 0 1
可以用objdump输出stab信息,以下是某个elf文件输出的stab信息,但有一堆看着没用的东西,使用如下命令
objdump -G main.elf > debug.txt
main.elf: file format elf32-sparc Contents of .stab section: Symnum n_type n_othr n_desc n_value n_strx String -1 HdrSym 0 33 00000335 1 0 SO 0 0 70000034 8 ../src/main.c 1 OPT 0 0 00000000 22 gcc2_compiled. 2 LSYM 0 0 00000000 37 int:t(0,1)=r(0,1);-2147483648;2147483647; 3 LSYM 0 0 00000000 79 char:t(0,2)=r(0,2);0;127; 4 LSYM 0 0 00000000 105 long int:t(0,3)=r(0,3);-2147483648;2147483647; 5 LSYM 0 0 00000000 152 unsigned int:t(0,4)=r(0,4);0;-1; 6 LSYM 0 0 00000000 185 long unsigned int:t(0,5)=r(0,5);0;-1; 7 LSYM 0 0 00000000 223 long long int:t(0,6)=r(0,6);0;-1; 8 LSYM 0 0 00000000 257 long long unsigned int:t(0,7)=r(0,7);0;-1; 9 LSYM 0 0 00000000 300 short int:t(0,8)=r(0,8);-32768;32767; 10 LSYM 0 0 00000000 338 short unsigned int:t(0,9)=r(0,9);0;65535; 11 LSYM 0 0 00000000 380 signed char:t(0,10)=r(0,10);-128;127; 12 LSYM 0 0 00000000 418 unsigned char:t(0,11)=r(0,11);0;255; 13 LSYM 0 0 00000000 455 float:t(0,12)=r(0,1);4;0; 14 LSYM 0 0 00000000 481 double:t(0,13)=r(0,1);8;0; 15 LSYM 0 0 00000000 508 long double:t(0,14)=r(0,1);8;0; 16 LSYM 0 0 00000000 540 complex int:t(0,15)=s8real:(0,1),0,32;imag:(0,1),32,32;; 17 LSYM 0 0 00000000 597 complex float:t(0,16)=R3;8;0; 18 LSYM 0 0 00000000 627 complex double:t(0,17)=R4;16;0; 19 LSYM 0 0 00000000 659 complex long double:t(0,18)=R4;16;0; 20 LSYM 0 0 00000000 696 __builtin_va_list:t(0,19)=*(0,20)=(0,20) 21 LSYM 0 0 00000000 737 _Bool:t(0,21)=eFalse:0,True:1,; 22 BINCL 0 0 00000ce2 8 ../src/main.c 23 FUN 0 0 70000034 769 main:F(0,1) 24 SLINE 0 7 00000000 0 25 SLINE 0 12 00000004 0 26 SLINE 0 13 00000008 0 27 LSYM 0 0 fffffff0 781 d:(0,13) 28 LBRAC 0 0 00000004 0 29 RBRAC 0 0 00000008 0 30 GSYM 0 0 00000000 790 var_data:G(0,1) 31 GSYM 0 0 00000000 806 var_bss:G(0,1) 32 SO 0 0 70000048 0