访问损坏的共享库

ice*_*y96 4 x86 assembly 32bit-64bit

这里是代码cpuid2.s

#cpuid2.s view the cpuid vendor id string using c library calls
.section .data
output:
    .asciz "The processor Vendor ID is '%s'\n"

.section .bss
    .lcomm buffer, 12

.section .text
.global _start
_start:
    movl $0, %eax
    cpuid
    movl $buffer, %edi
    movl %ebx, (%edi)
    movl %edx, 4(%edi)
    movl %ecx, 8(%edi)
    push $buffer
    push $output
    call printf
    addl $8, %esp
    push $0
    call exit
Run Code Online (Sandbox Code Playgroud)

我按如下方式组装、链接和运行它:

as -o cpuid2.o cpuid2.s
ld -dynamic-linker /lib/ld-linux.so.2 -o cpuid2 -lc cpuid2.o
./cpuid2
bash: ./cpuid2: Accessing a corrupted shared library
Run Code Online (Sandbox Code Playgroud)

我已经在 StackOverflow 上搜索过这个错误。我发现this question与我的相似。我尝试了@rasion 给出的方法。像这样:

as -32 -o cpuid2.o cpuid2.s
ld -melf_i386 -L/lib -lc -o cpuid2 cpuid2.o
ld: cannot find -lc
Run Code Online (Sandbox Code Playgroud)

他的回答并没有解决我的问题。我希望有一个人可以帮助我。

我在 GNU 汇编器中使用 AT&T 语法。

我的电脑有 64 位 Ubuntu 14.04。

Jay*_*nek 6

正如您已经意识到的那样,您正在尝试在 64 位机器上为 32 位机器编译程序集。使用您复制和粘贴的命令,您让asld知道您正在编译 32 位的东西。

您遇到的问题是您没有可用于链接的 32 位版本的 libc。

apt-get install libc6:i386 libc6-dev-i386
Run Code Online (Sandbox Code Playgroud)

然后用以下代码组装代码:

as --32 -o cpuid2.o cpuid2.s
Run Code Online (Sandbox Code Playgroud)

最后将其链接到:

ld -m elf_i386 -dynamic-linker /lib/ld-linux.so.2 -o cpuid2 -lc cpuid2.o
Run Code Online (Sandbox Code Playgroud)

然后它应该工作:

[jkominek@kyatt /tmp]$ ./cpuid2 
The processor Vendor ID is 'GenuineIntel'
Run Code Online (Sandbox Code Playgroud)