【读书笔记】《程序员的自我修养》----ELF格式

dwarf(ebugging With Attributed Record Formats)格式参考:
http://www.dwarfstd.org/
http://blog.csdn.net/js072110/article/details/44153303  (好)
http://blog.csdn.net/wuhui_gdnt/article/details/7283483

http://blog.csdn.net/wuhui_gdnt/article/details/7283529

一、概念

linux下的可执行文件为ELFELF格式,ELF格式主要分为以下几类
1.可重定位文件(Relocateable)这类文件包含了代码和数据,可以被用来链接成可执行文件或共享目标文件,静态链接库也可以归为这一类,如.o文件。
2.可执行文件(Executable file)这类文件包含了直接执行的程序,如/bin/bash等。
3.共享目录(shared object file)链接器可以使用这种文件跟其他的可重定位文件和共享目标文件链接,产生新的目标文件;动态链接器可以将几个共享目标文件与可执行文件结合,作为进程映像的一部分来运行,如glibc***.so。
4.核心转储文件(core dump file)当进程意外终止时,系统可以将该进程的地址空间内容及终止时的一些其他信息转储到核心转储文件。

二、几个命令及属性

objdump 命令
#objdump -h SimpleSection.o
参数-h就是把ELF文件的各个段的基本信息打印出来。
参数-x是把更多的信息打印出来
参数-s是可以将所有段的内容以十六进制的方式打印出来

参数-d是可以将所有包含指令的段反汇编

size 命令
#size SimpleSection.o
   text    data     bss     dec     hex filename
    174       8       4     186      ba SimpleSection.o
用来查看代码段、数据段、bss段的长度

readelf 命令
#readelf -h SimpleSection.o
参数-h是查看ELF文件头

#readelf -h SimpleSection.o
如下:
ELF Header:
  Magic:   7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00
  Class:                             ELF64
  Data:                              2's complement, little endian
  Version:                           1 (current)
  OS/ABI:                            UNIX - System V
  ABI Version:                       0
  Type:                              REL (Relocatable file)
  Machine:                           Advanced Micro Devices X86-64
  Version:                           0x1
  Entry point address:               0x0
  Start of program headers:          0 (bytes into file)
  Start of section headers:          400 (bytes into file)
  Flags:                             0x0
  Size of this header:               64 (bytes)
  Size of program headers:           0 (bytes)
  Number of program headers:         0
  Size of section headers:           64 (bytes)
  Number of section headers:         13
  Section header string table index: 10


#readelf -h SimpleSection
如下:
ELF Header:
  Magic:   7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00
  Class:                             ELF64
  Data:                              2's complement, little endian
  Version:                           1 (current)
  OS/ABI:                            UNIX - System V
  ABI Version:                       0
  Type:                              EXEC (Executable file)
  Machine:                           Advanced Micro Devices X86-64
  Version:                           0x1
  Entry point address:               0x4003e0
  Start of program headers:          64 (bytes into file)
  Start of section headers:          2616 (bytes into file)
  Flags:                             0x0
  Size of this header:               64 (bytes)
  Size of program headers:           56 (bytes)
  Number of program headers:         8
  Size of section headers:           64 (bytes)
  Number of section headers:         30
  Section header string table index: 27

段属性:
CONTETS:表示该段在文件中存在。如果.bss段没有该属性,表示它时机上在ELF中不存在内容。
ALLOC
LOAD
RELOC
READONLY
CODE


.data段:包含已经初始化的全局变量和已经初始化的局部静态变量
.rodata段:包含只读数据段,如printf中的"%d\n"的信息。
.bss段:存放未初始化的全局变量和未初始化的局部静态变量
符号表(COMMON符号):有些编译器只预留一个未定义的全局变量符号。


自定义段:可以在全局变量或函数之前加上__attribute__((section("name")))
比如__attribute__((section("FOO"))) int global=42;
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值