GNU Assembler: Accessing a Corrupted Shared Library Error

I am reading a book: "Professional Assembly Language".
I want to use dynamic linking...
I am using AT&T syntax with GNU assembler.
My computer has Ubuntu 12.04 (on a 64 bit system).
I am trying to display a vendor id string using cpuid and
printf C assembler opcode instead of Linux system calls.

Code hello.s

 .code32
   .section .data
 output:
   .asciz "The processor Vendor ID is '%s'\n"

 .section .bss
   .lcomm buffer, 12

 .section .text
   .globl _start
 _start:
   movl $0, %eax
   cpuid
   movl $buffer, %edi
   movl %ebx, (%edi)
   movl %edx, 4(%edi)
   movl %ecx, 8(%edi)
   pushl $buffer
   pushl $output
   call printf
   addl $8, %esp
   pushl $0
   call exit

      

I ran the following commands:

> $ as -o hello.o hello.s
> $ ld -dynamic-linker /lib/ld-linux.so.2 -o hello -lc hello.o
> $ ./hello
  bash: ./hello: Accessing a corrupted shared library

      

But I am getting the error shown above, something about a corrupted shared library (which is the error I want to solve)
I don't care about the code (because I understand it)
I just want to learn how to use dynamic linking using assembly code and GAS.
So, if you have assembly code that uses dynamic linking, I appreciate if you can show me the "how" and "ld" commands that you are using.

PS: In case anyone wants to understand what the code is doing:
The CPUID instruction uses one registration value as input.
The EAX register is used to determine that the information is generated by the CPUID command.
Depending on the value of the EAX register, the CPUID command will issue various information about the processor to the EBX, ECX and EDX registers.
The information is returned as a series of bit and flag values, which must be interpreted in their own meaning

This code uses the null option (movl $ 0,% eax) to get the simple vendor ID string from the processor. When a zero value is placed in the EAX register and the CPUID instruction, the processor returns the vendor ID string to the EBX, EDX, and ECX registers as follows:

❑ EBX contains the low 4 bytes of the string.
❑ EDX contains the middle 4 bytes of the string.
❑ ECX contains the last 4 bytes of the string.

String values ​​are placed in registers in little-endian format;
This code uses the standard C library functions: printf and exit instead of linux system calls.

+3


source to share


1 answer


I tried it like below and it works:

as -32 -o hello.o hello.s
ld -melf_i386 -L/lib -lc -o hello hello.o

      



By the way, on my machine it complains about the absence /usr/lib/libc.so.1

, after I made a link to the symbol /usr/lib/libc.so.1

on /lib/ld-linux.so.2

, it works.
create 32-bit ELF on 64-bit Linux, we need glibc.i686

or glibc.i386

.

0


source







All Articles