c-错误:LOAD段未覆盖PHDR段



我正在学习如何从文本链接脚本"从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文件;main.c

void test() {}
int main(int argc, char *argv[])
{
asm("mov %eax, 0x1n"
"mov %ebx, 0x0n"
"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段覆盖。

大多数段类型与至少一个LOAD段重叠,因为它们描述了加载数据的位置。我能想到的唯一例外是PT_GNU_STACK段。

最新更新