【问题标题】:Accessing a corrupted shared library访问损坏的共享库
【发布时间】:2015-08-05 20:14:39
【问题描述】:

这里是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

我是这样组装、链接和运行它的:

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

我已经在 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

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

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

我的电脑有 64 位 Ubuntu 14.04。

【问题讨论】:

    标签: assembly x86 32bit-64bit


    【解决方案1】:

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

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

    apt-get install libc6:i386 libc6-dev-i386
    

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

    as --32 -o cpuid2.o cpuid2.s
    

    最后链接到:

    ld -m elf_i386 -dynamic-linker /lib/ld-linux.so.2 -o cpuid2 -lc cpuid2.o
    

    那么它应该可以工作了:

    [jkominek@kyatt /tmp]$ ./cpuid2 
    The processor Vendor ID is 'GenuineIntel'
    

    【讨论】:

    • @zhengzhiliu 对不起,也试试安装 libc6-dev-i386。我最初设法从问题中省略了它。 (现在已经包含在上面了。)
    【解决方案2】:

    如果你想使用 libc,你应该使用入口点 main 而不是 _start。确保您已经安装了gcc-multilib,并且只需使用 gcc 编译和链接:gcc -o cpuid2 cpuid2.s。这应该会自动为您做所有正确的事情。

    【讨论】:

      猜你喜欢
      • 2020-09-26
      • 1970-01-01
      • 1970-01-01
      • 2017-03-07
      • 1970-01-01
      • 1970-01-01
      • 2021-11-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多