ELF文件认知(二)可重定位目标文件

chen@ubuntu:~$ readelf -h hello.o
ELF Header:
  Magic:   7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00 
  Class:                             ELF64
  Data:                              2's complement, little endian
  Version:                           1 (current)
  OS/ABI:                            UNIX - System V
  ABI Version:                       0
  Type:                              REL (Relocatable file)
  Machine:                           Advanced Micro Devices X86-64
  Version:                           0x1
  Entry point address:               0x0
  Start of program headers:          0 (bytes into file)
  Start of section headers:          672 (bytes into file)
  Flags:                             0x0
  Size of this header:               64 (bytes)
  Size of program headers:           0 (bytes)
  Number of program headers:         0
  Size of section headers:           64 (bytes)
  Number of section headers:         13
  Section header string table index: 10
chen@ubuntu:~$ hexdump -x hello.o -n 52
0000000    457f    464c    0102    0001    0000    0000    0000    0000
0000010    0001    003e    0001    0000    0000    0000    0000    0000
0000020    0000    0000    0000    0000    02a0    0000    0000    0000
*
0000034

说明:
7f 45 4c 46(0x45,0x4c,0x46是'e','l','f'对应的ascii编码),表示这是一个ELF对象
接下来的一个字节02为64位(01表示是一个32位对象),接下来的一个字节01表示是小端法表示,再接下来的一个字节01表示文件头版本。剩下的默认都设置为0
e_type值为0x0001,表示是一个可重定位目标文件。e_machine值为0x003e,表示是x86-64处理器体系结构。e_version值为0x00000001,表示是当前版本。
typedef struct
{
  unsigned char	e_ident[EI_NIDENT];	/* Magic number and other info */
  Elf64_Half	e_type;			/* Object file type */
  Elf64_Half	e_machine;		/* Architecture */
  Elf64_Word	e_version;		/* Object file version */
  Elf64_Addr	e_entry;		/* Entry point virtual address */
  Elf64_Off	e_phoff;		/* Program header table file offset */
  Elf64_Off	e_shoff;		/* Section header table file offset */
  Elf64_Word	e_flags;		/* Processor-specific flags */
  Elf64_Half	e_ehsize;		/* ELF header size in bytes */
  Elf64_Half	e_phentsize;		/* Program header table entry size */
  Elf64_Half	e_phnum;		/* Program header table entry count */
  Elf64_Half	e_shentsize;		/* Section header table entry size */
  Elf64_Half	e_shnum;		/* Section header table entry count */
  Elf64_Half	e_shstrndx;		/* Section header string table index */
} Elf64_Ehdr

subl /usr/include/elf.h         //查看ELF表头的数据结构


参考:
https://www.cnblogs.com/EliteDci/p/5578901.html

原文地址:https://www.cnblogs.com/zuoanfengxi/p/12652590.html