c - 错误 : PHDR segment not covered by LOAD segment

标签 c gnu elf linker-scripts

我正在从文本“从 0 到 1 的操作系统”中学习链接脚本,并且在文本中他们展示了一个使用关键字 PHDRS 的示例;

ENTRY(main);
    
PHDRS
{
    headers PT_PHDR FILEHDR PHDRS;
    code PT_LOAD FILEHDR;
}
    
SECTIONS /*top level command that declares a list of custom program sections, ld provides set of such instructions.*/
{
    . = 0x10000; /* set location counter to address 0x10000, base address for subsequent commands */
    .text : { *(.text) } :code /* final .text section begins at address 0x1000, combines all .text sections from obj files into one final*/
    . = 0x8000000; 
    .data : { *(.data) } /*wildcard means all obj file .data sections*/
    .bss : { *(.bss) }
}
用于链接一个c文件;主文件
void test() {}

int main(int argc, char *argv[])
{
    asm("mov %eax, 0x1\n"
         "mov %ebx, 0x0\n"
         "int $0x80");
}
然后将 main.c 编译为目标文件;
gcc -m32 -g -c main.c
但是,当使用链接描述文件时;
ld -m elf_i386 -o main -T main.lds main.o
ld: main: error: PHDR segment not covered by LOAD segment
产生了错误,尽管当我更改 PT_LOAD 段以包含 PHDRS 关键字时,
然后链接器正常工作。
然后使用readelf检查后;
readelf -l main

Elf file type is EXEC (Executable file)
Entry point 0x10010
There are 2 program headers, starting at offset 52

Program Headers:
  Type           Offset   VirtAddr   PhysAddr   FileSiz MemSiz  Flg Align
  PHDR           0x000000 0x0000f000 0x0000f000 0x00074 0x00074 R   0x4
  LOAD           0x000000 0x0000f000 0x0000f000 0x7ff100c 0x7ff100c RWE 0x1000

 Section to Segment mapping:
  Segment Sections...
   00     
   01     .text .text.__x86.get_pc_thunk.ax .eh_frame .got.plt 
PT_PHDR 段和 PT_LOAD 段从同一个 VA 开始。
有没有办法编写链接描述文件以便将段分开?
还有为什么会出现这个错误?

最佳答案

程序头在运行时需要,所以它们必须被加载,因此需要被 PT_LOAD 覆盖。部分。
大多数 segmentation 类型与至少一个 LOAD 重叠段,因为它们描述了加载数据的位置。我能想到的唯一异常(exception)是 PT_GNU_STACK部分。

关于c - 错误 : PHDR segment not covered by LOAD segment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63420037/

相关文章:

c - WIFEXITED WIFEXITSTATUS 宏

linux - unix、BSD、Mac OS X、linux 和 GNU 之间有什么关系?

c++ - put_time() 忽略时区偏移量的转换说明符 'z'

c++ - flens lapack : GNU GCC Version 4. 7 或更高要求!我的 mac 有

linux - .rodata 部分加载到可执行页面

linux-kernel - 如何将Linux内核Bin转为ELF格式

c - 可执行文件之间有什么区别?

c - AVL 树删除导致程序崩溃

c - 字符串和位如何在 C 中工作?

c - 是否应该使用强制转换来截断长变量?