ELF 文件结构分析 (5)

[ 2009-12-04 22:22:20 | 作者: yuhen ]
字号: | |
7. Section .text

.text 段中保存了所有函数的执行代码,我们看看 main() 的反汇编代码和 .text 数据对比。
$ objdump -d hello | less

080483e4 <main>:
    80483e4:       55  
    80483e5:       89 e5
    80483e7:       83 e4 f0
    80483ea:       83 ec 10
    80483ed:       c7 04 24 c0 84 04 08
    80483f4:       e8 1f ff ff ff
    80483f9:       b8 00 00 00 00
    80483fe:       c9
    80483ff:       c3

08048400 <__libc_csu_fini>:
    8048400:       55
    8048401:       89 e5
    8048403:       5d     
    8048404:       c3     
    8048405:       8d 74 26 00 
    8048409:       8d bc 27 00 00 00 00 
$ readelf -x .text hello

Hex dump of section '.text':
    ... ...
    0x080483e0 ******** 5589e583 e4f083ec 10c70424
    0x080483f0 c0840408 e81fffff ffb80000 0000c9c3
    0x08048400 5589e55d c38d7426 008dbc27 00000000
    ... ...

通过对比数据,我们会发现 .text 段中只保存了所有函数机器码,并没有其他的信息,包括函数名称、起始位置等等。那么反编译时如何确定某个函数的名称以及具体位置和长度呢?这其实就是我们前面提到的符号表的作用了。
$ readelf -s hello

... ...
Symbol table '.symtab' contains 74 entries:
    Num:    Value  Size Type    Bind   Vis      Ndx Name
    ... ...
    72: 080483e4    28 FUNC    GLOBAL DEFAULT   14 main
    ... ...

Type = FUNC 表明该记录是个函数,起始位置就是 Value:080483e4,代码长度 28(0x1c) 字节,存储在索引号为 14 的段中。怎么样?这回对上了吧。不过有个问题,很显然反编译和符号表中给出的都是虚拟地址,我们如何确定代码在文件中的实际位置呢?

公式:VA - Section Address + Offset = 实际文件中的位置
$ readelf -S hello

There are 38 section headers, starting at offset 0x1680:

Section Headers:
    [Nr] Name              Type            Addr     Off    Size   ES Flg Lk Inf Al
    ... ...
    [14] .text             PROGBITS        08048330 000330 00016c 00  AX  0   0 16
    ... ...

0x080483e4 - 0x08048330 + 0x000330 = 0x3E4

验证一下。
$ xxd -g 1 -s 0x3e4 -l 0x1c hello

00003e4: 55 89 e5 83 e4 f0 83 ec 10 c7 04 24 c0 84 04 08
00003f4: e8 1f ff ff ff b8 00 00 00 00 c9 c3 

如果用 strip 命令删除了符号表,反汇编效果就比较悲惨了,都挤到 .text 段,正经的入门级汇编编码风格啊。 [cry]
$ strip hello

$ objdump -d hello | less

Disassembly of section .text:

... ...

08048330 <.text>:
    8048330:       31 ed 
    8048332:       5e  
    8048333:       89 e1
    ... ...
    80483e4:       55   ; 可怜的 <main> 就是从这嘎达开始的
    80483e5:       89 e5   
    80483e7:       83 e4 f0
    80483ea:       83 ec 10 
    80483ed:       c7 04 24 c0 84 04 08
    80483f4:       e8 1f ff ff ff 
    80483f9:       b8 00 00 00 00
    80483fe:       c9       
    80483ff:       c3   

    8048400:       55   ; 这家伙是 <__libc_csu_fini>
    8048401:       89 e5
    8048403:       5d 
    8048404:       c3    
    8048405:       8d 74 26 00 
    8048409:       8d bc 27 00 00 00 00 
    ... ...
[最后修改由 yuhen, 于 2010-08-01 09:59:25]
评论Feed 评论Feed: http://www.rainsts.net/feed.asp?q=comment&id=890

这篇日志没有评论。

发表评论
表情图标
[smile] [confused] [cool] [cry]
[eek] [angry] [wink] [sweat]
[lol] [stun] [razz] [redface]
[rolleyes] [sad] [yes] [no]
[heart] [star] [music] [idea]
UBB代码
转换链接
表情图标
悄悄话
用户名:   密码:  
验证码 * 请输入验证码