Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call the c library from assembly code on Linux?

I'm trying to compile a little program in Linux assembly on Intel architecture. I want to use some functions of the C library, but it doesn't link.

Here is my assembly program :

.text
.globl main

main:
    pushl $512
    call malloc
    addl $4, %esp

    mov $1, %eax
    mov $0, %ebx
    int $0x80

I'm compiling with

as --32 -o output.o output.asm

here, everything goes fine. And then when i'm linking with

ld -static -m elf_i386 -o a.out output.o -lc

, I got these errors :

(.text+0x1b8): undefined reference to _Unwind_Resume' /usr/lib32/libc.a(iofclose.o):(.eh_frame+0x167): undefined reference to__gcc_personality_v0' /usr/lib32/libc.a(iofflush.o): In function fflush': (.text+0xd7): undefined reference to_Unwind_Resume' /usr/lib32/libc.a(iofflush.o):(.eh_frame+0xdf): undefined reference to __gcc_personality_v0' /usr/lib32/libc.a(iofputs.o): In function fputs': (.text+0x108): undefined reference to _Unwind_Resume' /usr/lib32/libc.a(iofputs.o):(.eh_frame+0xdf): undefined reference to __gcc_personality_v0' /usr/lib32/libc.a(iofwrite.o): In function `fwrite':

(I've another errors, but it is enough to see the problem, I think)

I saw some solutions indicating that I should link with -lgcc but on my computer the library is not found...

Does someone have an idea ?

like image 725
Baptiste Wicht Avatar asked Jul 19 '11 21:07

Baptiste Wicht


1 Answers

glibc requires certain initialization code to be statically linked with the executable. The easiest way to do this is to link using gcc:

gcc -static -o a.out output.o

You can see exactly what is being linked in by passing -v to gcc as well.

like image 73
bdonlan Avatar answered Sep 28 '22 00:09

bdonlan