我的 kernel.c
文件看起来像这样
void main() {
char *video_memory = (char *)0xb8000;
*video_memory = 'X';
}
我编译并链接了这两个命令:
gcc -ffreestanding -c ./src/kernel/kernel.c -o kernel.o -m32 -mtune=i386 -fno-pie
ld -o kernel.bin -Ttext=0x1000 kernel.o --oformat binary -m elf_i386 --strip-all -nmagic
它产生了一个 128MB 的 kernel.bin
但是,当我在 ld
命令中删除 -Ttext=0x1000
时,它生成了一个 120B 的 kernel.bin。
如果我理解正确,-Ttext=0x1000
将向所有内存引用添加偏移量 0x1000
。为什么会产生如此大的不同?
最佳答案
答案在@Peter Cordes 和@old_timer 的评论中。
首先将其构建为 elf 并使用 readelf -a
,我发现在 0x080480d4
处有一个段(从 0x1000 开始大约 128MB)包含 .note。 gnu.property
.
所以我添加了这个链接器脚本来丢弃 .note.gnu.property
/DISCARD/ : {
*(.note.gnu.property)
}
关于assembly - ld with -Ttext=0x1000 使文件非常大,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69601952/