问题描述
|
我在Linux机器上的几个二进制文件上使用了readelf,在程序头文件中看到了令我惊讶的东西。该示例来自\'ld \'实用程序,但是我使用gcc编译的任何示例也会发生此示例。
PHDR 0x000034 0x08048034 0x08048034 0x00120 0x00120 R E 0x4
该段跨越整个程序头。为什么被标记为可执行文件?它不包含机器代码。但是,为什么还要在标题中显示呢?我真的不想要它在我的程序映像中。
解决方法
指向PHDR的PHDR告诉加载程序,PHDR本身应映射到进程地址空间,以使程序本身可以访问它们。
这主要用于动态链接。
内存被标记为可执行的原因是因为PHDR小于一页,并且位于可执行代码的开头附近。如果PHDR的许可权与程序文本的许可权不同,则链接程序必须在它们之间插入填充。
, 主要的文件ELF头位于此处,可轻松找到存储其他节的文件中的偏移量。然后,每个子标题在其部分中描述数据。
主要的ELF标头如下所示:
/* ELF File Header */
typedef struct
{
unsigned char e_ident[EI_NIDENT]; /* Magic number and other info */
Elf32_Half e_type; /* Object file type */
Elf32_Half e_machine; /* Architecture */
Elf32_Word e_version; /* Object file version */
Elf32_Addr e_entry; /* Entry point virtual address */
Elf32_Off e_phoff; /* Program header table file offset */
Elf32_Off e_shoff; /* Section header table file offset */
Elf32_Word e_flags; /* Processor-specific flags */
Elf32_Half e_ehsize; /* ELF header size in bytes */
Elf32_Half e_phentsize; /* Program header table entry size */
Elf32_Half e_phnum; /* Program header table entry count */
Elf32_Half e_shentsize; /* Section header table entry size */
Elf32_Half e_shnum; /* Section header table entry count */
Elf32_Half e_shstrndx; /* Section header string table index */
} Elf32_Ehdr;
程序标头存在于此,因为它们描述了ELF可执行文件的可执行部分。
该程序的下一部分是
ELF程序标头。这些
描述程序的各个部分
包含可执行程序代码
映射到程序地址
加载时的空间。
/* Program segment header. */
typedef struct
{
Elf32_Word p_type; /* Segment type */
Elf32_Off p_offset; /* Segment file offset */
Elf32_Addr p_vaddr; /* Segment virtual address */
Elf32_Addr p_paddr; /* Segment physical address */
Elf32_Word p_filesz; /* Segment size in file */
Elf32_Word p_memsz; /* Segment size in memory */
Elf32_Word p_flags; /* Segment flags */
Elf32_Word p_align; /* Segment alignment */
} Elf32_Phdr;
这是从这里拿来的