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格式的调试信息。这两个是同时存在是不允许的。

  1. -g
  2. Produce debugging information in the operating system's native format (stabs, COFF,
  3. XCOFF, or DWARF). GDB can work with this debugging information.
  4.  
  5. On most systems that use stabs format, `-g' enables use of extra debugging information
  6. that only GDB can use; this extra information makes debugging work better in GDB but
  7. will probably make other debuggers crash or refuse to read the program. If you want to
  8. control for certain whether to generate the extra information, use `-gstabs+' , `-gstabs' ,
  9. `-gxcoff+' , `-gxcoff' , `-gdwarf-+' , or `-gdwarf-1' (see below).
  10. Unlike most other C compilers, GCC allows you to use `-g' with `-O' . The shortcuts taken
  11. by optimized code may occasionally produce surprising results: some variables you
  12. declared may not exist at all; flow of control may briefly move where you did not expect it;
  13. some statements may not be executed because they compute constant results or their values
  14. were already at hand; some statements may execute in different places because they were
  15. moved out of loops.

添加-g选项后,生成的elf文件节头表如下:stab长度为0x240=576,stabstr长度为0x4de=1246,共1822字节

原来的elf为839字节,新elf为2787字节。增加的debug信息比原来的全部elf还多,1822>839。

  1. Section Headers:
  2. [Nr] Name Type Addr Off Size ES Flg Lk Inf Al
  3. [ ] NULL
  4. [ ] .text PROGBITS AX
  5. [ ] .data PROGBITS 0000a4 WA
  6. [ ] .bss NOBITS 0000a8 WA
  7. [ ] .stab PROGBITS 0000a8 0c
  8. [ ] .comment PROGBITS 0002e8
  9. [ ] .stabstr STRTAB 0002fa 0004de
  10. [ ] .shstrtab STRTAB 0007d8
  11. [ ] .symtab SYMTAB 0009ac c
  12. [ ] .strtab STRTAB 000abc

没有-g的节头表

  1. Section Headers:
  2. [Nr] Name Type Addr Off Size ES Flg Lk Inf Al
  3. [ ] NULL
  4. [ ] .text PROGBITS AX
  5. [ ] .data PROGBITS 0000a4 WA
  6. [ ] .bss NOBITS 0000a8 WA
  7. [ ] .comment PROGBITS 0000a8
  8. [ ] .shstrtab STRTAB 0000ba
  9. [ ] .symtab SYMTAB 0000f0 a
  10. [ ] .strtab STRTAB

        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个项目

  1. struct internal_nlist {
  2. unsigned long n_strx; /* index into string table of name */
  3. unsigned char n_type; /* type of symbol */
  4. unsigned char n_other; /* misc info (usually empty) */
  5. unsigned short n_desc; /* description field */
  6. bfd_vma n_value; /* value of symbol */
  7. };

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个。

  1. Section Headers:
  2. [Nr] Name Type Addr Off Size ES Flg Lk Inf Al
  3. [ ] NULL
  4. [ ] .text PROGBITS AX
  5. [ ] .data PROGBITS 0000f8 WA
  6. [ ] .bss NOBITS 0000fc WA
  7. [ ] .rodata PROGBITS A
  8. [ ] .line PROGBITS 0000fc 00009e
  9. [ ] .debug_pubnames PROGBITS 00019a 00002d
  10. [ ] .debug_aranges PROGBITS 0001c7
  11. [ ] .debug PROGBITS 0001f8 00025c
  12. [ ] .comment PROGBITS
  13. [] .shstrtab STRTAB
  14. [] .symtab SYMTAB 0006d8 f
  15. [] .strtab STRTAB

可以用objdump输出stab信息,以下是某个elf文件输出的stab信息,但有一堆看着没用的东西,使用如下命令

objdump -G main.elf > debug.txt

  1. main.elf: file format elf32-sparc
  2.  
  3. Contents of .stab section:
  4.  
  5. Symnum n_type n_othr n_desc n_value n_strx String
  6.  
  7. - HdrSym
  8. SO ../src/main.c
  9. OPT gcc2_compiled.
  10. LSYM int:t(,)=r(,);-;;
  11. LSYM char:t(,)=r(,);;;
  12. LSYM long int:t(,)=r(,);-;;
  13. LSYM unsigned int:t(,)=r(,);;-;
  14. LSYM long unsigned int:t(,)=r(,);;-;
  15. LSYM long long int:t(,)=r(,);;-;
  16. LSYM long long unsigned int:t(,)=r(,);;-;
  17. LSYM short int:t(,)=r(,);-;;
  18. LSYM short unsigned int:t(,)=r(,);;;
  19. LSYM signed char:t(,)=r(,);-;;
  20. LSYM unsigned char:t(,)=r(,);;;
  21. LSYM float:t(,)=r(,);;;
  22. LSYM double:t(,)=r(,);;;
  23. LSYM long double:t(,)=r(,);;;
  24. LSYM complex int:t(,)=s8real:(,),,;imag:(,),,;;
  25. LSYM complex float:t(,)=R3;;;
  26. LSYM complex double:t(,)=R4;;;
  27. LSYM complex long double:t(,)=R4;;;
  28. LSYM __builtin_va_list:t(,)=*(,)=(,)
  29. LSYM _Bool:t(,)=eFalse:,True:,;
  30. BINCL 00000ce2 ../src/main.c
  31. FUN main:F(,)
  32. SLINE
  33. SLINE
  34. SLINE
  35. LSYM fffffff0 d:(,)
  36. LBRAC
  37. RBRAC
  38. GSYM var_data:G(,)
  39. GSYM var_bss:G(,)
  40. SO

ELF文件之八——使用链接脚本-2个函数-data-bss-temp-call-debug信息的更多相关文章

  1. ELF文件之四——使用链接脚本-2个函数-data

    main.c ; int main() { ; } int add() { ; } main.o 反汇编可以看到多了.text节的反汇编,存储的是全局变量的初始化数值 main.o对比,text段后面 ...

  2. ELF文件之七——使用链接脚本-2个函数-data-bss-temp-call

    main.c int enable; ; int main() { int temp; add(); ; } int add() { ; } o反汇编的地址都是0起始,elf的地址都是映射后的地址. ...

  3. ELF文件之三——使用链接脚本-2个函数

    main.c int main() { ; } int add() { ; } main.o的比较 与之二相比,section header offset由0x90变为0xA4,增加0x14,即add ...

  4. ELF文件之六——使用链接脚本-2个函数-data-bss-temp

    main.c int enable; ; int main() { int temp; ; } int add() { ; } elf反汇编结果如下,可以看出main函数中的栈多开了8字节,虽然局部变 ...

  5. ELF文件之五——使用链接脚本-2个函数-data-bss

    main.c int enable; ; int main() { ; } int add() { ; } bss段在elf中虽然size是4,但并不占filesize,节头表如下图所示 程序头中,项 ...

  6. ELF文件之九——使用链接脚本-2个函数-data-bss-temp-call-debug信息-struct

    main.c int enable; ; struct aaa { int membera; char memberb; }s_aaa; int main() { int temp; add(); d ...

  7. 裸板驱动总结(makefile+lds链接脚本+裸板调试)

    在裸板2440中,当我们使用nand启动时,2440会自动将前4k字节复制到内部sram中,如下图所示: 然而此时的SDRAM.nandflash的控制时序等都还没初始化,所以我们就只能使用前0~40 ...

  8. arm裸板驱动总结(makefile+lds链接脚本+裸板调试)

    在裸板2440中,当我们使用nand启动时,2440会自动将前4k字节复制到内部sram中,如下图所示: 然而此时的SDRAM.nandflash的控制时序等都还没初始化,所以我们就只能使用前0~40 ...

  9. 四、u-boot 链接脚本

    4.1 C语言中的段 编译器在编译程序的时候,将程序中的所有的元素分成了一些组成部分,各部分构成一个段,所以说段是可执行程序的组成部分. 代码段:代码段就是程序中的可执行部分,直观理解代码段就是函数堆 ...

随机推荐

  1. Asp.Net Core 3.0 Kestrel服务器下 高性能 WebSocket Server

    最近研究.net core 的各种高性能类型,内存池之类的东西,基于kestrel 服务器的websocket ,写个例子练下手 把原生的Websocket用ArrayPool<T>,Me ...

  2. 怎么将文件夹上传到GitHub上

    1. 在GitHub上新建一个仓库地址: http://github.com/......git 2. 在需要上传的文件夹目录下,运行 git   init  初始化git: 3. 运行git  ad ...

  3. ntelliJ IDEA添加注释常用的快捷键

    IDEA可以使用快捷键添加行注释Ctrl+/.块注释Ctrl+Shift+/,还可以快速生成类注释.方法注释等,下面就介绍这几种快捷键的用法

  4. Office系列(2)---提取Office文件(Word、PPT)中的所有图片

    回顾一下上文结尾的问题:如何给文档设置一个合适的封面图?其中一个解决方案就是,获取Office文件内部的图片作为封面.这里就详细介绍下获取图片的几种方式,以及他们各自的优缺点. PS:因为之前用VST ...

  5. SpringMvc简单使用

    SpringMvc框架的简单使用 第一步:导入依赖 <dependencies> <dependency> <groupId>org.springframework ...

  6. pip install 清华源加速

    经常要通过pip install安装需要的包,但是每当下载的文件比较大时,网速不够快,会导致报错.所以采用清华源来加速 清华大学开源软件镜像站 https://mirrors.tuna.tsinghu ...

  7. 使用doxygen

    Getting started with Doxygen 可执行文件doxygen是解析源文件并生成文档的主程序. 另外, 也可以使用可执行文件doxywizard, 是用于编辑配置文件, 以及在图形 ...

  8. [HNOI2008]Cards(dp,Burnside引理)

    Burnside引理: 参考自 某大佬对Burnside引理和Polya定理的讲解 相关概念 群:在数学中,群表示一个拥有满足封闭性.满足结合律.有单位元.有逆元的二元运算的代数结构. 置换群:由有限 ...

  9. [Java] The imoprt XXX cannot be resolved

    不知道为什么突然报错,但是可以成功编译. 通过import一个未使用的包来消除error,猜测是插件的问题,具体原因未知. import 未使用的包后,error消失.

  10. Educational Codeforces Round 80 (Rated for Div. 2)部分题解

    A. Deadline 题目链接 题目大意 给你\(n,d\)两个数,问是否存在\(x\)使得\(x+\frac{d}{x+1}\leq n\),其中\(\frac{d}{x+1}\)向上取整. 解题 ...