情况
-
环境
Arch Linux x86-64 (4.2.3-1-ARCH)
-
GCC
海湾合作委员会(海湾合作委员会)5.2.0
-
Command
gcc -Wall -g -o asm_printf asm_printf.s
-
Error
/usr/bin/ld: /tmp/cct4fa.o: Relocation R_X86_64_32S against '.data' can not be used when making a shared object; recompile with -fPIC
/tmp/cct4fa.o:err adding symbols: Bad value
collect2: error: ld returned 1 exit status
-
Code
.section .data
msg:
.asciz "Printf In Assembly!!\n"
.section .text
.globl main
main:
pushq $msg
call printf
addq $8 %esp
pushq $0
call exit
Question
我尝试使用gcc
编译上面的程序Code使用上面的命令部分Command部分并最终出现错误Error部分。
Note我就是not编译共享库。
- 这是什么错误?
- 我该如何解决?
具体错误是由于push
指令仅支持 32 位立即数,而您尝试将其用于 64 位地址。
然而,整个代码是错误的。目前尚不清楚您需要 32 位代码还是 64 位代码。大部分代码似乎都是 32 位的,除了pushq
所以我假设你真的想要 32 位代码。为此,将所有这些更改为push
(无论如何这是一个好主意)并使用进行编译gcc -m32
。另外,您只需要从堆栈中删除 4 个字节,因此使用addl $4, %esp
。 (谢谢@雇佣俄罗斯人指出这一点。)
64 位调用约定与 32 位不同,因此要创建 64 位版本,您必须进行更多更改。因为我假设您确实想要 32 位,所以这里仅用于说明:
.section .data
msg:
.asciz "Printf In Assembly!!\n"
.section .text
.globl main
main:
subq $8, %rsp
leaq msg(%rip), %rdi
xor %al, %al
call printf
xor %edi, %edi
call exit
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)