我正在尝试使用 NASM 和 GCC 制作一个程序:
global main
extern puts
section .data
hi db 'hello', 0
section .text
main:
push hi
call puts
ret
我正在构建:
nasm -f elf64 main.asm
gcc main.o -o main
rm main.o
I get:
/usr/bin/ld: main.o: relocation R_X86_64_32S against `.data' can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld: final link failed: Nonrepresentable section on output
collect2: error: ld returned 1 exit status
从示例中存在奇怪的堆栈操作来判断,我有一种感觉我做错了一些事情this https://stackoverflow.com/questions/33476098/gcc-assemby-error-relocation-r-x86-64-32s-against-data and this https://www.csee.umbc.edu/portal/help/nasm/sample_64.shtml#fib_64m.asm。我找不到任何实际的文档explains不过,出于某种原因(事实上,我几乎找不到任何关于使用 NASM 进行 64 位开发的有用文档,这使得我迄今为止所做的一切都变得非常痛苦),并添加类似的事情对我的错误输出没有任何影响。
Update:
我一直在看this http://www.nasm.us/doc/nasmdo11.html and this https://stackoverflow.com/questions/48071280/nasm-symbol-printf-causes-overflow-in-r-x86-64-pc32-relocation.
如果我将 main 更改为:
push rbp
mov rax,0
mov rdi, hi
call [puts wrt ..got]
pop rbp
mov rax,0
ret
它编译得很好,但是在运行而不是实际打印时给了我一个段错误。我也不明白为什么我要推拉价值rbp
从堆栈中,以及为什么rax
在这种情况下, 的值很重要。