跳过正文

ELF文件解析

·1247 字·6 分钟
jiladahe1997
作者
jiladahe1997
To see the world as it is, and to love it

参考:
#

可执行文件相关的命令和工具: https://linuxtools-rst.readthedocs.io/zh_CN/latest/tool/index.html



简介
#

在单片机编程中,最终得到的是一个 axf/hex/bin 格式的文件,这个文件怎么在CPU上执行的?里面的各种分段又是怎么回事呢?



文件格式区分
#

*.ELF(Executable and Linkable Format): https://en.wikipedia.org/wiki/Executable_and_Linkable_Format
.axf:
.bin: 从axf文件中精简出来的文件,大小远远小于axf。
.hex: 另外一种格式的bin文件。 https://www.keil.com/support/docs/1584/
.PE: portable executable,windows下的elf



.ELF执行原理:
#

.ELF只是一种文件格式,具体的执行逻辑是交给执行者实现的。

  • 对于STM32,执行者就是cortex-M核
  • 对于Linux或者windows操作系统内核,执行者是uboot
  • 对于Linux或者Windows操作系统,执行者是Linux内核或者Windows内核。

举个栗子:ELF文件的执行过程:
#

1.STM32的固件:
#

  • STM32的固件有三种格式:axf、hex、bin。 前两种都是elf格式,最后的叫做binary格式。
  • elf格式中的信息有:header、地址、代码、数据、符号表;binary格式中的信息有:代码、数据
  • 因此当我们使用jflash烧录时:
    1. 如果是elf文件,jflash会解析此elf文件,将代码和数据烧写到elf中指定的地址
    2. 如果是bin文件,则要求用户手动指定地址,并将代码和数据烧写到指定地址。

2.Linux的uboot和kernel image:
#

  • 先通过一定的方法(根据芯片厂商指定)将uboot烧录到指定为止(flash、emmc)
  • uboot再去读取并解析 kernel image,通常是zImage格式
  • uboot将zImage中的代码和数据拷贝到ddr中

FPIC
#

FPIC的参考资料:

stackoverflow an example of fpic https://stackoverflow.com/questions/5311515/gcc-fpic-option

这里值得一提的是,zImage的代码在编译时一定是PIC(Position Independent Code)的,即位置独立,zImage可以加载到任何位置。 PIC的实现原理是,在调用函数时,比如 b funA时,编译器会使用相对地址,例如b .+0x10这样的方法。虽然会增加一次额外的计算,降低一点点效率。


举例
#

使用readelf命令读取的keil编译之后的文件如下:

[root@iZ2zefbvkcoj7o7ledf4oqZ ~]# readelf -a MultipleDisastersRTU.axf 
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:                              EXEC (Executable file)
  Machine:                           ARM
  Version:                           0x1
  Entry point address:               0x8020299
  Start of program headers:          1203160 (bytes into file)
  Start of section headers:          1203192 (bytes into file)
  Flags:                             0x5000002, Version5 EABI, <unknown>
  Size of this header:               52 (bytes)
  Size of program headers:           32 (bytes)
  Number of program headers:         1
  Size of section headers:           40 (bytes)
  Number of section headers:         15
  Section header string table index: 14

Section Headers:
  [Nr] Name              Type            Addr     Off    Size   ES Flg Lk Inf Al
  [ 0]                   NULL            00000000 000000 000000 00      0   0  0
  [ 1] ER_IROM1          PROGBITS        08020000 000040 03f82c 00  AX  0   0  8
  [ 2] RW_IRAM2          PROGBITS        24000000 03f870 000230 00  WA  0   0 16
  [ 3] RW_IRAM2          NOBITS          24002760 03faa0 0412f8 00  WA  0   0 16
  [ 4] .debug_abbrev     PROGBITS        00000000 03faa0 008d1f 00      0   0  1
  [ 5] .debug_frame      PROGBITS        00000000 0487bf 00ad1c 00      0   0  1
  [ 6] .debug_info       PROGBITS        00000000 0534db 046070 00      0   0  1
  [ 7] .debug_line       PROGBITS        00000000 09954b 03ceca 00      0   0  1
  [ 8] .debug_str        PROGBITS        00000000 0d6415 033bf5 00      0   0  1
  [ 9] .debug_ranges     PROGBITS        00000000 10a00a 0037d0 00      0   0  1
  [10] .symtab           SYMTAB          00000000 10d7dc 00ce00 10     11 2192  4
  [11] .strtab           STRTAB          00000000 11a5dc 00a894 00      0   0  1
  [12] .note             NOTE            00000000 124e70 00001c 00      0   0  4
  [13] .comment          PROGBITS        00000000 124e8c 000cc4 00      0   0  1
  [14] .shstrtab         STRTAB          00000000 125b50 000088 00      0   0  1
Key to Flags:
  W (write), A (alloc), X (execute), M (merge), S (strings), I (info),
  L (link order), O (extra OS processing required), G (group), T (TLS),
  C (compressed), x (unknown), o (OS specific), E (exclude),
  y (purecode), p (processor specific)

There are no section groups in this file.

Program Headers:
  Type           Offset   VirtAddr   PhysAddr   FileSiz MemSiz  Flg Align
  LOAD           0x000040 0x08020000 0x08020000 0x3fa60 0x83288 RWE 0x10

 Section to Segment mapping:
  Segment Sections...
   00     ER_IROM1 

There is no dynamic section in this file.

There are no relocations in this file.

There are no unwind sections in this file.

Symbol table '.symtab' contains 3296 entries:
   Num:    Value  Size Type    Bind   Vis      Ndx Name
     0: 00000000     0 NOTYPE  LOCAL  DEFAULT  UND 
     1: 08020000     0 NOTYPE  LOCAL  DEFAULT    1 $d.realdata
     2: 08020298     0 NOTYPE  LOCAL  DEFAULT    1 $t
     3: 080202a4     0 NOTYPE  LOCAL  DEFAULT    1 $d
     4: 080202ac     0 NOTYPE  LOCAL  DEFAULT    1 $t
     5: 080202c8     0 NOTYPE  LOCAL  DEFAULT    1 $d
     6: 080202d0     0 NOTYPE  LOCAL  DEFAULT    1 $t
     7: 080203ea     0 NOTYPE  LOCAL  DEFAULT    1 $d
     8: 08020404     0 NOTYPE  LOCAL  DEFAULT    1 $t
     9: 0802050a     0 NOTYPE  LOCAL  DEFAULT    1 $d
    10: 0802051c     0 NOTYPE  LOCAL  DEFAULT    1 $t
    11: 0802069c     0 NOTYPE  LOCAL  DEFAULT    1 $d
    12: 080206a4     0 NOTYPE  LOCAL  DEFAULT    1 $t
    13: 0802072e     0 NOTYPE  LOCAL  DEFAULT    1 $d
    14: 08020734     0 NOTYPE  LOCAL  DEFAULT    1 $t
    15: 080207ba     0 NOTYPE  LOCAL  DEFAULT    1 $d
    16: 080207c0     0 NOTYPE  LOCAL  DEFAULT    1 $t
    17: 08020e98     0 NOTYPE  LOCAL  DEFAULT    1 $d
    18: 08020ea4     0 NOTYPE  LOCAL  DEFAULT    1 $t
    19: 08020ec4     0 NOTYPE  LOCAL  DEFAULT    1 $d
    20: 08020ecc     0 NOTYPE  LOCAL  DEFAULT    1 $t
    21: 08020fa8     0 NOTYPE  LOCAL  DEFAULT    1 $d
    22: 08020fb4     0 NOTYPE  LOCAL  DEFAULT    1 $t
    23: 080213b0     0 NOTYPE  LOCAL  DEFAULT    1 $d
    24: 080213b8     0 NOTYPE  LOCAL  DEFAULT    1 $t
    25: 080213e0     0 NOTYPE  LOCAL  DEFAULT    1 $d
    26: 080213e4     0 NOTYPE  LOCAL  DEFAULT    1 $t
    27: 0802173c     0 NOTYPE  LOCAL  DEFAULT    1 $d
    28: 08021740     0 NOTYPE  LOCAL  DEFAULT    1 $t
    29: 080219b0     0 NOTYPE  LOCAL  DEFAULT    1 $t.6
    30: 08022c34     0 NOTYPE  LOCAL  DEFAULT    1 $d.11
    31: 08022c38     0 NOTYPE  LOCAL  DEFAULT    1 $t.1
    32: 08022f88     0 NOTYPE  LOCAL  DEFAULT    1 $d.3
    33: 08022fa0     0 NOTYPE  LOCAL  DEFAULT    1 $t.6
    34: 0802301c     0 NOTYPE  LOCAL  DEFAULT    1 $d.7
    35: 08023020     0 NOTYPE  LOCAL  DEFAULT    1 $t.23
    36: 08023894     0 NOTYPE  LOCAL  DEFAULT    1 $d.2
    37: 08023898     0 NOTYPE  LOCAL  DEFAULT    1 $t.3
    38: 080238e8     0 NOTYPE  LOCAL  DEFAULT    1 $d.4
    39: 080238ec     0 NOTYPE  LOCAL  DEFAULT    1 $t.5
    40: 08024014     0 NOTYPE  LOCAL  DEFAULT    1 $d.9
    41: 08024018     0 NOTYPE  LOCAL  DEFAULT    1 $t.0
    42: 0802a386     0 NOTYPE  LOCAL  DEFAULT    1 $d.7
    43: 0802a38a     0 NOTYPE  LOCAL  DEFAULT    1 $t.8
    44: 0802b130     0 NOTYPE  LOCAL  DEFAULT    1 $d.18
    45: 0802b140     0 NOTYPE  LOCAL  DEFAULT    1 $t.19
    46: 0802b3e0     0 NOTYPE  LOCAL  DEFAULT    1 $d.20
    47: 0802b3f0     0 NOTYPE  LOCAL  DEFAULT    1 $t.21
    48: 0802b690     0 NOTYPE  LOCAL  DEFAULT    1 $d.22
    49: 0802b6a0     0 NOTYPE  LOCAL  DEFAULT    1 $t.14
    50: 0802b6ce     0 NOTYPE  LOCAL  DEFAULT    1 $d.15
    51: 0802b6d4     0 NOTYPE  LOCAL  DEFAULT    1 $t.16
    52: 0802bd6c     0 NOTYPE  LOCAL  DEFAULT    1 $d.1
    53: 0802bd72     0 NOTYPE  LOCAL  DEFAULT    1 $t.2
    54: 0802c312     0 NOTYPE  LOCAL  DEFAULT    1 $d.3
    55: 0802c316     0 NOTYPE  LOCAL  DEFAULT    1 $t.4
    56: 0802c50a     0 NOTYPE  LOCAL  DEFAULT    1 $d.5
    57: 0802c534     0 NOTYPE  LOCAL  DEFAULT    1 $t.6
    58: 0802c5ae     0 NOTYPE  LOCAL  DEFAULT    1 $d.7
    59: 0802c5b4     0 NOTYPE  LOCAL  DEFAULT    1 $t.8
    60: 0802c630     0 NOTYPE  LOCAL  DEFAULT    1 $d.9
    61: 0802c636     0 NOTYPE  LOCAL  DEFAULT    1 $t.10
    62: 0802d198     0 NOTYPE  LOCAL  DEFAULT    1 $d.4
    63: 0802d1ca     0 NOTYPE  LOCAL  DEFAULT    1 $t.5
    64: 0802d47c     0 NOTYPE  LOCAL  DEFAULT    1 $d.6
    65: 0802d490     0 NOTYPE  LOCAL  DEFAULT    1 $t.1
    66: 0803090a     0 NOTYPE  LOCAL  DEFAULT    1 $d.16
    67: 08030910     0 NOTYPE  LOCAL  DEFAULT    1 $t.17
    68: 08033c24     0 NOTYPE  LOCAL  DEFAULT    1 $d.5
    69: 08033c28     0 NOTYPE  LOCAL  DEFAULT    1 $t.6
    70: 08033ff8     0 NOTYPE  LOCAL  DEFAULT    1 $d.2

  3275: 2402a13c     4 OBJECT  GLOBAL HIDDEN     3 rt_interrupt_to_thread
  3276: 2402b144     8 OBJECT  GLOBAL HIDDEN     3 rt_thread_defunct
  3277: 2402b14c    64 OBJECT  GLOBAL HIDDEN     3 rt_thread_priority_table
  3278: 2402b18c     4 OBJECT  GLOBAL HIDDEN     3 rt_thread_ready_priority_
  3279: 2402b290     4 OBJECT  GLOBAL HIDDEN     3 rt_thread_switch_interrup
  3280: 2402e5cc     4 OBJECT  GLOBAL HIDDEN     3 shell
  3281: 2402e5d0     4 OBJECT  GLOBAL HIDDEN     3 signal_intensity
  3282: 2403366d   800 OBJECT  GLOBAL HIDDEN     3 ucMCoilBuf
  3283: 2403398d   200 OBJECT  GLOBAL HIDDEN     3 ucMDiscInBuf
  3284: 24033c74    32 OBJECT  GLOBAL HIDDEN     3 update_start_event
  3285: 240364ca     2 OBJECT  GLOBAL HIDDEN     3 usMCoilStart
  3286: 240364cc     2 OBJECT  GLOBAL HIDDEN     3 usMDiscInStart
  3287: 240364ce 20000 OBJECT  GLOBAL HIDDEN     3 usMRegHoldBuf
  3288: 2403b2ee     2 OBJECT  GLOBAL HIDDEN     3 usMRegHoldStart
  3289: 2403b2f0 20000 OBJECT  GLOBAL HIDDEN     3 usMRegInBuf
  3290: 24040110     2 OBJECT  GLOBAL HIDDEN     3 usMRegInStart
  3291: 240429f0     4 OBJECT  GLOBAL HIDDEN     3 uwTick
  3292: 24042a00    12 OBJECT  GLOBAL HIDDEN     3 wdg
  3293: 24042a10     4 OBJECT  GLOBAL HIDDEN     3 wdg_ptr
  3294: 24042a14     1 OBJECT  GLOBAL HIDDEN     3 wkup_from_sleep_mode
  3295: 24043a58     0 OBJECT  GLOBAL HIDDEN     3 __initial_sp

No version information found in this file.

Displaying notes found in: .note
  Owner                 Data size       Description
  ARM                  0x0000000c       Unknown note type: (0x40000000)
   description data: 00 00 00 00 00 00 00 00 00 00 00 00 
[root@iZ2zefbvkcoj7o7ledf4oqZ ~]# ls


参考资料:
#

ELF_Executable_and_Linkable_Format_diagram_by_Ange_Albertini.png

【RM 之一 ELF 文件、镜像(Image)文件、可执行文件、对象文件 详解】https://blog.csdn.net/ZCShouCSDN/article/details/100048461



ELF相关工具:
#

图片.png