自制 x86-64 内核(47):动态链接器基础

这一章实现了对 PIE(Position-Independent Executable)程序的支持,也就是不加 -static 编译出来的"动态"程序。 问题:PIE 和静态 ELF 的区别 用 musl-gcc 编译一个简单的 hello world: # 静态(之前一直用的) musl-gcc -static -O2 -o hello hello.c # 动态(PIE) musl-gcc -O2 -o hello_pie hello.c file 查看: hello: ELF 64-bit LSB executable, x86-64, statically linked hello_pie: ELF 64-bit LSB pie executable, x86-64, dynamically linked 区别在于: 属性 静态 (ET_EXEC) PIE (ET_DYN) e_type 2 3 虚地址 固定(如 0x400000) 从 0 开始,需加载时指定 base 重定位 无 .rela.dyn 表 外部依赖 无 通常有(但 musl PIE 已内嵌 libc) 为什么 musl PIE 没有"真正的"动态链接? musl-gcc 默认把所有 libc 代码静态链接进去,只是编译为 PIE 格式(支持 ASLR)。所以运行时: ...

June 3, 2026 · 1 min · 大飞
京ICP备14031575号-3