问题
Here is the code of 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
I assemble, link, and run it as so:
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
I've searched StackOverflow for this error. I found this question which is similar to mine. And I tried the method given by @rasion. Like this:
as -32 -o cpuid2.o cpuid2.s
ld -melf_i386 -L/lib -lc -o cpuid2 cpuid2.o
ld: cannot find -lc
His answer doesn't solve my problem. I hope someone can help me.
I'm using AT&T syntax with GNU assembler.
My computer has 64 bit Ubuntu 14.04.
回答1:
As you've sort of realized, you're trying to compile assembly for a 32 bit machine, on a 64 bit machine. With the commands you copied and pasted, you're letting as
and ld
know that you're compiling something 32 bit.
The issue you've run into is that you don't have a 32 bit version of libc available to link against.
apt-get install libc6:i386 libc6-dev-i386
Then assemble the code with:
as --32 -o cpuid2.o cpuid2.s
and finally link it with:
ld -m elf_i386 -dynamic-linker /lib/ld-linux.so.2 -o cpuid2 -lc cpuid2.o
Then it should work:
[jkominek@kyatt /tmp]$ ./cpuid2
The processor Vendor ID is 'GenuineIntel'
回答2:
If you want to use libc, you should use entry point main
not _start
. Make sure you have gcc-multilib
installed, and simply use gcc to compile and link: gcc -o cpuid2 cpuid2.s
. That should automatically do all the right things for you.
来源:https://stackoverflow.com/questions/30419857/accessing-a-corrupted-shared-library