【问题标题】:nasm issue relocation R_X86_64_PC32 shared librarynasm 问题重定位 R_X86_64_PC32 共享库
【发布时间】:2014-05-06 15:33:15
【问题描述】:

我正在使用 nasm 64 将 .S 编译为 .o,然后使用 gcc 创建一个共享库,如下所示:

nasm -f elf64 source/strlen.S
nasm -f elf64 source/strchr.S
nasm -f elf64 source/memset.S
nasm -f elf64 source/strcspn.S
nasm -f elf64 source/rindex.S
nasm -f elf64 source/strpbrk.S
nasm -f elf64 source/strcmp.S
nasm -f elf64 source/strncmp.S
nasm -f elf64 source/strcasecmp.S
/usr/bin/gcc -shared ./source/strlen.o ./source/strchr.o ./source/memset.o ./source/strcspn.o ./source/rindex.o ./source/strpbrk.o ./source/strcmp.o ./source/strncmp.o ./source/strcasecmp.o -o libasm.so

source/rindex.S 调用 source/strlen.S 中的函数 strlen 编译行报错:

/usr/bin/ld: ./source/rindex.o: relocation R_X86_64_PC32 against symbol `strlen' can not be used when making a shared object; recompile with -fPIC

在编译 .S 时,我可以将 -fPIC 选项与 gcc 一起使用,但我使用的是 nasm,但找不到等效选项。

有人知道我可以如何避免这个问题吗?

提前致谢。

【问题讨论】:

  • 你是“编译器”(手工生成 asm)。您可以避免使用 32 位绝对地址(如现代 PIE 可执行文件:32-bit absolute addresses no longer allowed in x86-64 Linux?)。如果你想尊重符号插入的东西,你也需要手动完成,通过 PLT 或 GOT 甚至是你自己的静态存储,除非你想像 ELF visibility = hidden 一样对待它。

标签: assembly gcc x86-64 nasm fpic


【解决方案1】:

您需要确保编写与位置无关的代码。您可能会发现 DEFAULT RELREL 关键字本身很有帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-12
    • 1970-01-01
    • 1970-01-01
    • 2020-01-26
    • 1970-01-01
    • 2016-01-15
    • 2021-05-23
    • 1970-01-01
    相关资源
    最近更新 更多