【发布时间】:2019-04-14 10:21:27
【问题描述】:
我找不到足够的信息来使用 as 和 ld 在程序集中创建共享对象。
我想创建一个共享库,其中包含一些带变量的函数(.data 部分)。但是当我的代码中有一个 .data 段时, ld 不会接受 -shared 选项。 (在制作共享对象时,不能使用针对 '.data' 的重定位 R_X86_64_32S;使用 -fPIC 重新编译)。当然,必须可以在共享对象代码中使用局部变量,但如果不使用 -fPIC 选项编译 C 代码,我找不到有关如何执行此操作的信息。
my_lib.s:
.globl print_info
.data
output:
.ascii "The processor vendor ID is 'xxxxxxxxxxxx'\n"
.bss
.text
print_info:
xor %eax, %eax
cpuid
movq $output, %rdi
movl %ebx, 28(%rdi)
movl %edx, 32(%rdi)
movl %ecx, 36(%rdi)
movl $1, %eax
movl $1, %edi
movq $output, %rsi
movl $42, %edx
ret
caller.s:
.globl _start
.data
.bss
.text
_start:
# some code to load shared library
# with open() and mmap() syscalls
subq $8, %rsp # align stack
call print_info
addq $8, %rsp
# some code here
movl $60, %eax # exit program
xor %edi, %edi
syscall
as my_lib.s -omy_lib.o
as caller.s -ocaller.o
不会产生错误。不过
ld -shared my_lib.o
礼物:
ld: my_lib.o: relocation R_X86_64_32S against `.data' can not be used when making a shared object; recompile with -fPIC
ld: final link failed: nonrepresentable section on output
我能找到的关于该主题的所有信息,都使用 C 代码和 gcc 进行编译和链接。任何人都可以展示如何在不使用 C 编码和使用 gcc 的情况下进行操作吗?
【问题讨论】:
标签: shared-libraries ld gnu-assembler