一些常用代码分析工具的使用

gcc -S hello.c -o hello.s

objdump -h hello.o

 

hello.o:     file format elf32-i386

Sections:
Idx Name          Size      VMA       LMA       File off  Algn
  0 .text         00000026  00000000  00000000  00000034  2**2   // 代码段
                  CONTENTS, ALLOC, LOAD, RELOC, READONLY, CODE
  1 .data         00000000  00000000  00000000  0000005c  2**2   //已初始化的全局变量和局部镜头变量
                  CONTENTS, ALLOC, LOAD, DATA
  2 .bss          00000000  00000000  00000000  0000005c  2**2     // 未初始化的全局变量和局部镜头变量,实际大小0
                  ALLOC
  3 .rodata       0000000c  00000000  00000000  0000005c  2**0     //只读数据段
                  CONTENTS, ALLOC, LOAD, READONLY, DATA
  4 .comment      0000002a  00000000  00000000  00000068  2**0
                  CONTENTS, READONLY
  5 .note.GNU-stack 00000000  00000000  00000000  00000092  2**0
                  CONTENTS, READONLY

 

 

 

size hello.o

 text    data     bss     dec     hex filename
     50       0       0      50      32 hello.o

 

 

两边大小有点不一致,呵呵。

 

 

readelf -h hello.o

 

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:          228 (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

 

readelf -S hello.o

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

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 000026 00  AX  0   0  4
  [ 2] .rel.text         REL             00000000 000350 000010 08      9   1  4        // 需要重新定位的
  [ 3] .data             PROGBITS        00000000 00005c 000000 00  WA  0   0  4
  [ 4] .bss              NOBITS          00000000 00005c 000000 00  WA  0   0  4
  [ 5] .rodata           PROGBITS        00000000 00005c 00000c 00   A  0   0  1
  [ 6] .comment          PROGBITS        00000000 000068 00002a 00      0   0  1
  [ 7] .note.GNU-stack   PROGBITS        00000000 000092 000000 00      0   0  1
  [ 8] .shstrtab         STRTAB          00000000 000092 000051 00      0   0  1
  [ 9] .symtab           SYMTAB          00000000 00029c 0000a0 10     10   8  4
  [10] .strtab           STRTAB          00000000 00033c 000013 00      0   0  1
Key to Flags:
  W (write), A (alloc), X (execute), M (merge), S (strings)
  I (info), L (link order), G (group), x (unknown)
  O (extra OS processing required) o (OS specific), p (processor specific)

 

 

Objdump -r  hello.o

 重定位表信息:表示需要外部link的函数和数据

更好的方法用:

 readelf -s hello.o

Symbol table '.symtab' contains 11 entries:
   Num:    Value  Size Type    Bind   Vis      Ndx Name
     0: 00000000     0 NOTYPE  LOCAL  DEFAULT  UND
     1: 00000000     0 FILE    LOCAL  DEFAULT  ABS hello.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: 00000000     0 SECTION LOCAL  DEFAULT    7
     7: 00000000     0 SECTION LOCAL  DEFAULT    6
     8: 00000000    46 FUNC    GLOBAL DEFAULT    1 main
     9: 00000000     0 NOTYPE  GLOBAL DEFAULT  UND foo
    10: 00000000     0 NOTYPE  GLOBAL DEFAULT  UND puts

 

里面UND 的函数就是需要外部link的函数

readelf -s ×.a | grep "UND"

 

 

你可能感兴趣的:(File,header,table,processing,工具,代码分析)