0

在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部分?但我不确定,谁能解释一下?

4

1 回答 1

0

一天后解决了这个问题,抱歉这么晚才回答。-fPIC这是编译选项和之间的区别-pie,当您使用后者替换前者时,程序将给出正确的(如预期的)输出。

因此,当使用交叉工具链(如aarch64-elf-g++, 来生成)进行编译时position independent code,您更喜欢-pie而不是-fPIC.

请自行测试,如果可能,请回复此答案。

于 2018-03-14T13:50:09.810 回答