重定位 R_X86_64_32 针对 `.data' 不能在创建共享对象时使用;

Mir*_*ang 8 c assembly ld

我写了下面的汇编代码,它可以直接通过as和ld构建pass。

as cpuid.s -o cpuid.o
ld cpuid.o -o cpuid
Run Code Online (Sandbox Code Playgroud)

但是当我使用gcc来完成整个过程时。我遇到以下错误。

$ gcc cpuid.s -o cpuid
/tmp/cctNMsIU.o: In function `_start':
(.text+0x0): multiple definition of `_start'
/usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/Scrt1.o:(.text+0x0): first defined here
/usr/bin/ld: /tmp/cctNMsIU.o: relocation R_X86_64_32 against `.data' can not be used when making a shared object; recompile with -fPIC
/usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/Scrt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
/usr/bin/ld: final link failed: Invalid operation
collect2: error: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)

然后我将 _start 修改为 main,并将 -fPIC 添加到 gcc 参数。但这并不能解决我的 ld 错误。错误消息更改为以下。

$ gcc cpuid.s -o cpuid
/usr/bin/ld: /tmp/ccYCG80T.o: relocation R_X86_64_32 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
Run Code Online (Sandbox Code Playgroud)

由于我不创建共享对象,因此我不明白其含义。我只想制作一个可执行的二进制文件。

    .section .data
output:

    .ascii "The processor Vendor ID is 'xxxxxxxxxxxx'\n"

    .section .text

    .global _start

_start:

    movl $0, %eax

    cpuid

    movl $output, %edi
    movl %ebx, 28(%edi)
    movl %edx, 32(%edi)
    movl %ecx, 36(%edi)

    movl $4, %eax
    movl $1, %ebx
    movl $output, %ecx
    movl $42, %edx
    int $0x80

    movl $1, %eax
    movl $0, %ebx
    int $0x80
Run Code Online (Sandbox Code Playgroud)

如果我将上面的代码修改为下面,它是否正确或对 64 位 asm 编程有一些副作用?

         .section .data
 output:
         .ascii "The processor Vendor ID is 'xxxxxxxxxxxx'\n"

         .section .text

         .global main
 main:
         movq $0, %rax
         cpuid

         lea output(%rip), %rdi
         movl %ebx, 28(%rdi)
         movl %edx, 32(%rdi)
         movl %ecx, 36(%rdi)
         movq %rdi, %r10

         movq $1, %rax
         movq $1, %rdi
         movq %r10, %rsi
         movq $42, %rdx
         syscall
Run Code Online (Sandbox Code Playgroud)

R..*_*R.. 5

正如评论所指出的,您可以通过将您的程序链接为非 PIE 来解决此问题,但最好将您的 asm 修复为与位置无关。如果是 32 位 x86 代码,那就有点难看了。本指令:

    movl $output, %edi
Run Code Online (Sandbox Code Playgroud)

会成为:

    call 1f
1:  pop %edi
    add $output-1b, %edi
Run Code Online (Sandbox Code Playgroud)

对于 64 位,它更干净。代替:

    movq $output, %rdi
Run Code Online (Sandbox Code Playgroud)

你会写:

    lea output(%rip), %rdi
Run Code Online (Sandbox Code Playgroud)