在windows 10下,使用交叉编译工具链linaro
,全名gcc-linaro-7.2.1-2017.11-i686-mingw32_aarch64-elf
(可上网搜索),编译如下代码片段:
// file test.cpp
// ASM_DEFINE_LOCAL_SYM and ASM_DEFINE_GLOBAL_SYM defines assembler symbol,
// one is local and the other is global, as their name indicated
#define ASM_DEFINE_LOCAL_SYM(sym) __asm__ __volatile__(#sym ":\n\t")
#define ASM_DEFINE_GLOBAL_SYM(sym) __asm__ __volatile__(".global " #sym " \n\t;" #sym ":\n\t")
void testIfLocalSymWrongs()
{
kout << "func address = " <<reinterpret_cast<uint64_t>(testIfLocalSymWrongs) << "\n";
extern char local[];
extern char global[];
ASM_DEFINE_LOCAL_SYM(local);
ASM_DEFINE_GLOBAL_SYM(global);
kout << "local = " << reinterpret_cast<uint64_t>(local) << "\n";
kout << "global = " << reinterpret_cast<uint64_t>(global) << "\n";
}
代码不完整,因为我正在裸机环境中测试迷你内核。在上面的代码中,kout
只是将字符写入串行端口(使用硬件时raspberry pi 3
)或控制台(使用模拟器时QEMU
),但它不需要要完整,我想。无论如何,编译命令是aarch64-elf-g++ -fPIC test.cpp .... -o test
,并用于aarch64-elf-objcopy
生成kernel.test.img
,其中仅包含二进制代码,其他数据(如 elf 标头)被剥离。
在 QEMU 上运行它:qemu-system-aarch64 -machine virt,gic-version=3 -cpu cortex-a53 -smp 1 -m 1G -nographic -serial stdio -bios kernel.test.img
给出以下输出:
func address = 3c64
local = 3c64
global = 3cc0
而您可以看到func address
与 相同local
,与local
不同global
,这完全不是我们所期望的。
核心问题是当你使用-fPIC
编译时,局部符号和全局符号在它们应该相同的时候却有不同的值。
也许aarch64-elf-g++
生成了错误的 .got
部分?但我不确定,谁能解释一下?