1

我正在使用 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,但找不到等效选项。

有人知道我怎样才能避免这个问题吗?

提前致谢。

4

1 回答 1

3

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

于 2014-03-27T16:22:11.407 回答