GCC-Assemby 错误:针对'.data'的重新定位R_X86_64_32S



情况


  1. 环境

    Arch Linux x86-64(4.2.3-1-Arch)

  2. GCC

    gcc(gcc)5.2.0

  3. 命令

    gcc -Wall -g -o asm_printf asm_printf.s 
    
  4. 错误

    /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
    
  5. 代码

    .section .data
    msg:
        .asciz "Printf In Assembly!!n"
    .section .text
    .globl main
    main:
        pushq $msg
        call printf
        addq $8 %esp
        pushq $0
        call exit
    

问题


我试图使用gcc使用上面command部分中的命令编译上面Code中的程序,但最终在error章节中出现错误。

请注意我不是编译共享库。

  1. 这个错误是什么
  2. 我该怎么解决这个问题

特定错误是由于push指令仅支持32位立即数,而您试图将其用于64位地址。

然而,整个代码是错误的。目前还不清楚您想要的是32位代码还是64位代码。除了pushq之外,大多数代码似乎都是32位的,所以我想你真的想要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

最新更新