Accessing a corrupted shared library
Here is the code 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 build, link and run it like this:
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 searched StackOverflow for this error. I found this question that 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. Hope someone can help me.
I am using AT&T syntax with GNU assembler.
My computer has 64 bit Ubuntu 14.04.
source to share
As you can imagine, you are trying to compile an assembly for a 32-bit machine on a 64-bit machine. With the commands you copied and pasted, you let as
and ld
know that you are compiling something 32-bit.
The problem you are facing is that you don't have a 32 bit libc to reference.
apt-get install libc6:i386 libc6-dev-i386
Then compile 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'
source to share