首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 其他教程 > 其他相关 >

第3章 目标资料(ELF格式)

2013-01-28 
第3章 目标文件(ELF格式)int printf(const char* format, ...)int global_init_var84int global_uninit

第3章 目标文件(ELF格式)
int printf(const char* format, ...);int global_init_var=84;int global_uninit_var;void func1(int i){printf("%d\n", i);}int main(void){static int static_var=85;static int static_var2;int a=1;int b;func1(static_var+static_var2+a+b);return a;}

?

经过: $ gcc -c SimpleSection.c ? ?预处理 -> 编译(产生汇编代码) -> 汇编(产生obj文件),尚未链接。

此时得到SimpleSection.o,下面均是对SimpleSection.o的观察分析:

?

[hadoop@sam1 test]$ readelf -h SimpleSection.o ?==> 查看ELF格式的目标文件 "File Header"

ELF Header:

??Magic: ? 7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00?

??Class: ? ? ? ? ? ? ? ? ? ? ? ? ? ? ELF32

??Data: ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?2's complement, little endian

??Version: ? ? ? ? ? ? ? ? ? ? ? ? ? 1 (current)

??OS/ABI: ? ? ? ? ? ? ? ? ? ? ? ? ? ?UNIX - System V

??ABI Version: ? ? ? ? ? ? ? ? ? ? ? 0

??Type: ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?REL (Relocatable file)

??Machine: ? ? ? ? ? ? ? ? ? ? ? ? ? Intel 80386

??Version: ? ? ? ? ? ? ? ? ? ? ? ? ? 0x1

??Entry point address: ? ? ? ? ? ? ? 0x0

??Start of program headers: ? ? ? ? ?0 (bytes into file)

??Start of section headers: ? ? ? ? ?272 (bytes into file)

??Flags: ? ? ? ? ? ? ? ? ? ? ? ? ? ? 0x0

??Size of this header: ? ? ? ? ? ? ? 52 (bytes)

??Size of program headers: ? ? ? ? ? 0 (bytes)

??Number of program headers: ? ? ? ? 0

??Size of section headers: ? ? ? ? ? 40 (bytes)

??Number of section headers: ? ? ? ? 11

??Section header string table index: 8

?

[hadoop@sam1 test]$ readelf -S SimpleSection.o ? ==> 查看ELF格式的目标文件 "Section Header Table"

There are 11 section headers, starting at offset 0x110:

?

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 ? ? ? ?00000000 000034 000050 00 ?AX ?0 ? 0 ?4

??[ 2] .rel.text ? ? ? ? REL ? ? ? ? ? ? 00000000 000420 000028 08 ? ? ?9 ? 1 ?4

==>.rel.text:对于必须要重定位的代码段和数据段,都会有一个相应的重定位表——因为链接器处理目标文件时,须要对目标文件中“对绝对地址引用的位置”进行重定位。

??[ 3] .data ? ? ? ? ? ? PROGBITS ? ? ? ?00000000 000084 000008 00 ?WA ?0 ? 0 ?4

??[ 4] .bss ? ? ? ? ? ? ?NOBITS ? ? ? ? ?00000000 00008c 000004 00 ?WA ?0 ? 0 ?4

??[ 5] .rodata ? ? ? ? ? PROGBITS ? ? ? ?00000000 00008c 000004 00 ? A ?0 ? 0 ?1

??[ 6] .comment ? ? ? ? ?PROGBITS ? ? ? ?00000000 000090 00002d 01 ?MS ?0 ? 0 ?1

??[ 7] .note.GNU-stack ? PROGBITS ? ? ? ?00000000 0000bd 000000 00 ? ? ?0 ? 0 ?1

??[ 8] .shstrtab ? ? ? ? STRTAB ? ? ? ? ?00000000 0000bd 000051 00 ? ? ?0 ? 0 ?1

==> .shstrtab段表字符串表:保存段表中用到的字符串,如“段名”

??[ 9] .symtab ? ? ? ? ? SYMTAB ? ? ? ? ?00000000 0002c8 0000f0 10 ? ? 10 ?10 ?4

==> .symtab符号表:目标文件中的“函数”和“变量”统称为符号(Symbol)

??[10] .strtab ? ? ? ? ? STRTAB ? ? ? ? ?00000000 0003b8 000066 00 ? ? ?0 ? 0 ?1

==> .strtab字符串表:保存普通的字符串

Key to Flags:

??W (write), A (alloc), X (execute), M (merge), S (strings)

??I (info), L (link order), G (group), T (TLS), E (exclude), x (unknown)

??O (extra OS processing required) o (OS specific), p (processor specific)

?

与上面类似地,还有:

[hadoop@sam1 test]$ objdump -h SimpleSection.o

SimpleSection.o: ? ? file format elf32-i386

?

Sections:

Idx Name ? ? ? ? ?Size ? ? ?VMA ? ? ? LMA ? ? ? File off ?Algn

??0 .text ? ? ? ? 00000050 ?00000000 ?00000000 ?00000034 ?2**2

?? ? ? ? ? ? ? ? ?CONTENTS, ALLOC, LOAD, RELOC, READONLY, CODE

??1 .data ? ? ? ? 00000008 ?00000000 ?00000000 ?00000084 ?2**2

?? ? ? ? ? ? ? ? ?CONTENTS, ALLOC, LOAD, DATA

??2 .bss ? ? ? ? ?00000004 ?00000000 ?00000000 ?0000008c ?2**2

?? ? ? ? ? ? ? ? ?ALLOC

??3 .rodata ? ? ? 00000004 ?00000000 ?00000000 ?0000008c ?2**0

?? ? ? ? ? ? ? ? ?CONTENTS, ALLOC, LOAD, READONLY, DATA

??4 .comment ? ? ?0000002d ?00000000 ?00000000 ?00000090 ?2**0

?? ? ? ? ? ? ? ? ?CONTENTS, READONLY

??5 .note.GNU-stack 00000000 ?00000000 ?00000000 ?000000bd ?2**0

?? ? ? ? ? ? ? ? ?CONTENTS, READONLY

[hadoop@sam1 test]$ readelf -s SimpleSection.o ==> 查看ELF格式目标文件的“符号表”(符号表也是目标文件中的一个段,即.symtab这个段)?

Symbol table '.symtab' contains 15 entries:

?? Num: ? ?Value ?Size Type ? ?Bind ? Vis ? ? ?Ndx Name

?? ? 0: 00000000 ? ? 0 NOTYPE ?LOCAL ?DEFAULT ?UND?

?? ? 1: 00000000 ? ? 0 FILE ? ?LOCAL ?DEFAULT ?ABS SimpleSection.c

?? ? 2: 00000000 ? ? 0 SECTION LOCAL ?DEFAULT ? ?1?

?? ? 3: 00000000 ? ? 0 SECTION LOCAL ?DEFAULT ? ?3?

?? ? 4: 00000000 ? ? 0 SECTION LOCAL ?DEFAULT ? ?4?

?? ? 5: 00000000 ? ? 0 SECTION LOCAL ?DEFAULT ? ?5?

?? ? 6: 00000004 ? ? 4 OBJECT ?LOCAL ?DEFAULT ? ?3 static_var.1222

?? ? 7: 00000000 ? ? 4 OBJECT ?LOCAL ?DEFAULT ? ?4 static_var2.1223

?? ? 8: 00000000 ? ? 0 SECTION LOCAL ?DEFAULT ? ?7?

?? ? 9: 00000000 ? ? 0 SECTION LOCAL ?DEFAULT ? ?6?

?? ?10: 00000000 ? ? 4 OBJECT ?GLOBAL DEFAULT ? ?3 global_init_var

?? ?11: 00000004 ? ? 4 OBJECT ?GLOBAL DEFAULT ?COM global_uninit_var

?? ?12: 00000000 ? ?27 FUNC ? ?GLOBAL DEFAULT ? ?1 func1

?? ?13: 00000000 ? ? 0 NOTYPE ?GLOBAL DEFAULT ?UND printf

?? ?14: 0000001b ? ?53 FUNC ? ?GLOBAL DEFAULT ? ?1 main

?

?

热点排行