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-+' , 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
[ ] NULL
[ ] .text PROGBITS AX
[ ] .data PROGBITS 0000a4 WA
[ ] .bss NOBITS 0000a8 WA
[ ] .stab PROGBITS 0000a8 0c

[ ] .comment PROGBITS 0002e8
[ ] .stabstr STRTAB 0002fa 0004de

[ ] .shstrtab STRTAB 0007d8
[ ] .symtab SYMTAB 0009ac c
[ ] .strtab STRTAB 000abc

没有-g的节头表

Section Headers:
[Nr] Name Type Addr Off Size ES Flg Lk Inf Al
[ ] NULL
[ ] .text PROGBITS AX
[ ] .data PROGBITS 0000a4 WA
[ ] .bss NOBITS 0000a8 WA
[ ] .comment PROGBITS 0000a8
[ ] .shstrtab STRTAB 0000ba
[ ] .symtab SYMTAB 0000f0 a
[ ] .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个项目

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
[ ] NULL
[ ] .text PROGBITS AX
[ ] .data PROGBITS 0000f8 WA
[ ] .bss NOBITS 0000fc WA
[ ] .rodata PROGBITS A
[ ] .line PROGBITS 0000fc 00009e
[ ] .debug_pubnames PROGBITS 00019a 00002d
[ ] .debug_aranges PROGBITS 0001c7
[ ] .debug PROGBITS 0001f8 00025c

[ ] .comment PROGBITS
[] .shstrtab STRTAB
[] .symtab SYMTAB 0006d8 f
[] .strtab STRTAB

可以用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

-     HdrSym
SO ../src/main.c
OPT gcc2_compiled.
LSYM int:t(,)=r(,);-;;
LSYM char:t(,)=r(,);;;
LSYM long int:t(,)=r(,);-;;
LSYM unsigned int:t(,)=r(,);;-;
LSYM long unsigned int:t(,)=r(,);;-;
LSYM long long int:t(,)=r(,);;-;
LSYM long long unsigned int:t(,)=r(,);;-;
LSYM short int:t(,)=r(,);-;;
LSYM short unsigned int:t(,)=r(,);;;
LSYM signed char:t(,)=r(,);-;;
LSYM unsigned char:t(,)=r(,);;;
LSYM float:t(,)=r(,);;;
LSYM double:t(,)=r(,);;;
LSYM long double:t(,)=r(,);;;
LSYM complex int:t(,)=s8real:(,),,;imag:(,),,;;
LSYM complex float:t(,)=R3;;;
LSYM complex double:t(,)=R4;;;
LSYM complex long double:t(,)=R4;;;
LSYM __builtin_va_list:t(,)=*(,)=(,)
LSYM _Bool:t(,)=eFalse:,True:,;
BINCL 00000ce2 ../src/main.c
FUN main:F(,)
SLINE
SLINE
SLINE
LSYM fffffff0 d:(,)
LBRAC
RBRAC
GSYM var_data:G(,)
GSYM var_bss:G(,)
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. echarts 使用时,常见配置

    1.隐藏x轴,y轴,网格线 "yAxis": [ { //就是一月份这个显示为一个线段,而不是数轴那种一个点点 "show" : true, "bou ...

  2. 2018南京现场赛K 随机输出

    题目链接:http://codeforces.com/gym/101981/attachments n和m太小,空地联通无环,总步数太大,直接随机输出5w个方向 #include<iostrea ...

  3. Window同一电脑配置多个git公钥

    前言 配置多个本地ssh-key之前,先初始化下GIt环境哦! 可以参照:https://www.cnblogs.com/poloyy/p/12185132.html 执行前两步就好啦 本地生成两个s ...

  4. limit_choices_to

    # 多对多 老师 teachers = models.ManyToManyField(verbose_name='任课老师', to='UserInfo', related_name="ab ...

  5. 各种反弹shell方法总结

    获取shell的方法总结: shell分为两种,一种是正向shell,另一种是反向shell.如果客户端连接服务器,客户端主动连接服务器,就称为正向shell.如果客户端连接服务器,服务器想要获得客户 ...

  6. Java反射的常见用法

    反射的常见用法有三类,第一类是“查看”,比如输入某个类的属性方法等信息,第二类是“装载“,比如装载指定的类到内存里,第三类是“调用”,比如通过传入参数,调用指定的方法. 1 查看属性的修饰符.类型和名 ...

  7. C#反射与特性(九):全网最全-解析反射

    目录 1,判断类型 1.1 类和委托 1.2 值类型 1.3 接口 1.4 数组 2, 类型成员 2.1 类 2.2 委托 2.3 接口 [微信平台,此文仅授权<NCC 开源社区>订阅号发 ...

  8. ios---二维码的扫描

    二维码扫描 使用ios的AVFoundation框架实现二维码扫描 第一步:设置相机访问权限:在Info.plist添加Privacy - Camera Usage Description权限 第二步 ...

  9. js---描述链表

    js描述链表 有些情况下js的数组结构在实际使用中速度很慢,此时可以考虑用链表来代替它: //链表类 function Node(element){ this.element=element; thi ...

  10. Docker(一) 简介

    简介 Docker是一款针对程序开发人员和系统管理员来开发.部署.运行应用的一款虚拟化平台.Docker 可以让你像使用集装箱一样快速的组合成应用,并且可以像运输标准集装箱一样,尽可能的屏蔽代码层面的 ...