这是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。
最佳答案
正如您已经意识到的那样,您正在尝试在 64 位机器上为 32 位机器编译程序集。使用您复制和粘贴的命令,您可以让 as
和 ld
知道你正在编译 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'
关于assembly - 访问损坏的共享库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30419857/