Skip to content

Commit 4cd391c

Browse files
committed
fix link config
1 parent 7e8ee8a commit 4cd391c

4 files changed

Lines changed: 55 additions & 25 deletions

File tree

.cargo/config.toml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
[build]
22
# 缺省目标可以在命令行覆盖,例如:--target x86_64-unknown-none
33
# !!!注意:若要执行用户态程序,需注释下一行的默认target, 因为裸机目标不带例如std的标准库,会导致编译错误
4-
# target = "x86_64-unknown-none"
4+
target = "x86_64-unknown-none"
55

66
[target.x86_64-unknown-none]
7-
linker = "ld.lld"
8-
rustflags = ["-C", "link-arg=-nostartfiles"]
7+
rustflags = [
8+
"-C", "link-arg=-Tkernel/ld/link.ld",
9+
"-C", "relocation-model=static",
10+
"-C", "no-redzone=yes",
11+
]
912

1013
[target.riscv64gc-unknown-none-elf]
1114
linker = "ld.lld"

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,26 @@ bash scripts/qemu.sh
127127
==============================
128128
RINT KERNEL: init OK
129129
```
130+
131+
### debug记录
132+
133+
#### 启动内核iso报错:panic:lower half PHDRs not allowed
134+
找到ELF文件:
135+
```bash
136+
$ file build/iso/boot/kernel.bin
137+
build/iso/boot/kernel.bin: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, with debug_info, not stripped
138+
```
139+
查看虚拟地址:
140+
```bash
141+
$ readelf -W -l build/iso/boot/kernel.bin
142+
```
143+
PHDR在低位
144+
```bash
145+
Program Headers:
146+
Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align
147+
PHDR 0x000040 0x0000000000200040 ......
148+
```
149+
更改link.ld使得其在高位
130150
</details>
131151

132152

kernel/.cargo/config.toml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,14 @@ target = "x86_64-rint.json"
33

44
[target."x86_64-rint.json"]
55
rustflags = [
6-
"-Clink-arg=-Tkernel/ld/link.ld",
6+
# 关键:脚本路径相对于 kernel 包根目录
7+
"-Clink-arg=-Tld/link.ld",
8+
# 建议:静态重定位,避免产生不适合内核的绝对寻址/PLT
9+
"-C", "relocation-model=static",
10+
"-C", "no-redzone=yes",
711
]
812

13+
914
[unstable]
1015
build-std = ["core", "compiler_builtins", "alloc"]
1116
build-std-features = ["compiler-builtins-mem"]

kernel/ld/link.ld

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,39 @@
1-
ENTRY(_start) /* 告诉链接器程序入口符号是 _start(在 kernel/src/main.rs 的汇编里) */
1+
/* 高半区虚拟基址与物理加载基址 */
2+
KERNEL_VMA = 0xFFFFFFFF80200000; /* 高半区虚拟地址(示例) */
3+
KERNEL_LMA = 0x00200000; /* 物理加载地址 2MiB */
24

3-
KERNEL_PHYS_BASE = 0x200000; /* 物理装载地址(2 MiB 对齐,方便跳过固件/引导器区域) */
4-
KERNEL_VIRT_BASE = 0xffffffff80000000; /* 高半区映射基址;Limine 不允许低半区 PHDRs */
5+
ENTRY(_start)
56

6-
PHDRS {
7-
text PT_LOAD FLAGS(0x5); /* 可加载、可读可执行:代码段 */
8-
rodata PT_LOAD FLAGS(0x4); /* 可加载、只读:常量段 */
9-
data PT_LOAD FLAGS(0x6); /* 可加载、可读可写:数据和 bss 段 */
7+
PHDRS
8+
{
9+
text PT_LOAD FLAGS(5); /* R X */
10+
rodata PT_LOAD FLAGS(4); /* R */
11+
data PT_LOAD FLAGS(6); /* R W */
1012
}
1113

1214
SECTIONS
1315
{
14-
/* SECTIONS 中段的出现顺序就是布局顺序。
15-
先 .text,其起始偏移为 0(对齐到 0x1000);
16-
.rodata 紧随 .text 末尾并按 0x1000 对齐;
17-
.data 紧随 .rodata;.bss 紧随 .data */
18-
/* 将“虚拟地址基准”(VMA)放到高半区;实际文件装载的物理地址(LMA)仍在 2MiB */
19-
. = KERNEL_VIRT_BASE;
16+
/* 把链接地址(VMA)移动到高半区 */
17+
. = KERNEL_VMA;
2018

21-
.text : ALIGN(0x1000) {
19+
.text : ALIGN(16)
20+
{
2221
KEEP(*(.text._start))
2322
*(.text .text.*)
24-
} :text AT(KERNEL_PHYS_BASE + (ADDR(.text) - KERNEL_VIRT_BASE))
23+
} :text AT(ADDR(.text) - KERNEL_VMA + KERNEL_LMA)
2524

26-
.rodata : ALIGN(0x1000) {
25+
.rodata : ALIGN(16)
26+
{
2727
*(.rodata .rodata.*)
28-
} :rodata AT(KERNEL_PHYS_BASE + (ADDR(.rodata) - KERNEL_VIRT_BASE))
28+
} :rodata AT(ADDR(.rodata) - KERNEL_VMA + KERNEL_LMA)
2929

30-
.data : ALIGN(0x1000) {
30+
.data : ALIGN(16)
31+
{
3132
*(.data .data.*)
32-
} :data AT(KERNEL_PHYS_BASE + (ADDR(.data) - KERNEL_VIRT_BASE))
33+
} :data AT(ADDR(.data) - KERNEL_VMA + KERNEL_LMA)
3334

34-
.bss : ALIGN(0x1000) {
35+
.bss : ALIGN(16)
36+
{
3537
*(.bss .bss.* COMMON)
36-
} :data AT(KERNEL_PHYS_BASE + (ADDR(.bss) - KERNEL_VIRT_BASE))
38+
} :data AT(ADDR(.bss) - KERNEL_VMA + KERNEL_LMA)
3739
}

0 commit comments

Comments
 (0)