2015-10-19 105 views
0

對象文件的readelf輸出:不正確的函數尺寸

.text段結構我理解
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 fp16.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 NOTYPE LOCAL DEFAULT 1 $t 
    6: 00000001 194 FUNC LOCAL DEFAULT 1 __gnu_f2h_internal 
    7: 00000010  0 NOTYPE LOCAL DEFAULT 5 $d 
    8: 00000000  0 SECTION LOCAL DEFAULT 5 
    9: 00000000  0 SECTION LOCAL DEFAULT 7 
    10: 000000c5 78 FUNC GLOBAL HIDDEN  1 __gnu_h2f_internal 
    11: 00000115  4 FUNC GLOBAL HIDDEN  1 __gnu_f2h_ieee 
    12: 00000119  4 FUNC GLOBAL HIDDEN  1 __gnu_h2f_ieee 
    13: 0000011d  4 FUNC GLOBAL HIDDEN  1 __gnu_f2h_alternative 
    14: 00000121  4 FUNC GLOBAL HIDDEN  1 __gnu_h2f_alternative 

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 000124 00 AX 0 0 4 
    [ 2] .rel.text   REL    00000000 00058c 000010 08  9 1 4 
    [ 3] .data    PROGBITS  00000000 000158 000000 00 WA 0 0 1 
    [ 4] .bss    NOBITS   00000000 000158 000000 00 WA 0 0 1 
    [ 5] .debug_frame  PROGBITS  00000000 000158 00008c 00  0 0 4 
    [ 6] .rel.debug_frame REL    00000000 00059c 000060 08  9 5 4 
    [ 7] .ARM.attributes ARM_ATTRIBUTES 00000000 0001e4 00002f 00  0 0 1 
    [ 8] .shstrtab   STRTAB   00000000 000213 000051 00  0 0 1 
    [ 9] .symtab   SYMTAB   00000000 00041c 0000f0 10  10 10 4 
    [10] .strtab   STRTAB   00000000 00050c 00007e 00  0 0 1 

Relocation section '.rel.text' at offset 0x58c contains 2 entries: 
Offset  Info Type   Sym.Value Sym. Name 
0000011a 00000a66 R_ARM_THM_JUMP11 000000c5 __gnu_h2f_internal 
00000122 00000a66 R_ARM_THM_JUMP11 000000c5 __gnu_h2f_internal 

Relocation section '.rel.debug_frame' at offset 0x59c contains 12 entries: 
Offset  Info Type   Sym.Value Sym. Name 
00000014 00000802 R_ARM_ABS32  00000000 .debug_frame 
00000018 00000202 R_ARM_ABS32  00000000 .text 
00000040 00000802 R_ARM_ABS32  00000000 .debug_frame 
00000044 00000202 R_ARM_ABS32  00000000 .text 
00000050 00000802 R_ARM_ABS32  00000000 .debug_frame 
00000054 00000202 R_ARM_ABS32  00000000 .text 
00000060 00000802 R_ARM_ABS32  00000000 .debug_frame 
00000064 00000202 R_ARM_ABS32  00000000 .text 
00000070 00000802 R_ARM_ABS32  00000000 .debug_frame 
00000074 00000202 R_ARM_ABS32  00000000 .text 
00000080 00000802 R_ARM_ABS32  00000000 .debug_frame 
00000084 00000202 R_ARM_ABS32  00000000 .text 

.text段具有的量0x124

0x0: unknown byte 
0x1-0xC3: __gnu_f2h_internal 
0xC3-0xC5: two unknown bytes between those functions (btw what are those?) 
0xC5-0x113: __gnu_h2f_internal 
0x113-0x115: two unknown bytes between those functions 
0x115-0x119: __gnu_f2h_ieee 
0x119-0x11D: __gnu_h2f_ieee 
0x11D-0x121: __gnu_f2h_alternative 
0x121-0x125: __gnu_h2f_alternative // section is only 0x124, what happened to the missing byte? 
大小

請注意,段大小爲0x124,最後一個函數在0x125處結束,發生什麼情況會丟失字節?

謝謝。

回答

1

從技術上講,你的「丟失字節」是0x0處的那個。

請注意,您正在查看符號的,即運行時函數地址(如果您的.text部分VMA不是0,這將更加清晰)。由於它們是Thumb函數,因此地址的位設置爲0,以便處理器在調用它們時切換到Thumb模式;這些指令的實際位置仍然是半字對齊的,即0x0,0xc4,0x114等,因爲否則它們不能被執行(你會對未對齊的PC採取錯誤)。根據the ARM ELF spec關於STT_FUNC符號的說明去掉第0位,以獲得與該符號相對應的指令的實際VMA,然後減去該部分的開始,並且應該具有與對象文件本身中相同的相對偏移量。

<offset in section> = (<symbol value> & ~1) - <section VMA> 

額外的半字填充後的一些功能只是確保每個符號字對齊 - 有可能是這種種原因,而是出現在腦海的第一個就是adr指令將無法正常工作,如果他們不是。

+0

「區段加載地址」您是指段執行時的虛擬地址?你能舉一個例子來說明如何獲得偏移量?以及如何獲得最後一個函數的正確數據('__gnu_h2f_alternative') – Koten

+0

是的,對不起,在我精確地使用了確切的術語後,我發現「加載地址」可能含糊不清地提示了LMA,當時我確實指的是VMA。在你的例子中,'__gnu_h2f_alternative'看起來是由4個字節(1或2個Thumb指令)組成的,在.text中偏移量爲0x120到0x123,據推測在實際文件中佔用0x154到0x157。 – Notlikethat

+0

感謝您的幫助解釋,爲每個函數獲取正確的原始數據是否正確? 'std :: vector vec; (vec.begin(),section_offset + symbol [i] .st_value&〜1,section_offset + symbol [i] .st_size);' – Koten