How to call library c from assembly code on Linux?

I am trying to compile a small program in a Linux assembly on Intel architecture. I want to use some functions of the C library, but this is not related.

Here is my build program:

.text .globl main main: pushl $512 call malloc addl $4, %esp mov $1, %eax mov $0, %ebx int $0x80 

I am compiling with

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

everything is going well here. And then when I contact

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

I got the following errors:

(. text + 0x1b8): _Unwind_Resume' /usr/lib32/libc.a(iofclose.o):(.eh_frame+0x167): undefined reference to __ gcc_personality_v0 '/usr/lib32/libc.a(iofflush. _unwind_Resume' /usr/lib32/libc.a o): fflush': (.text+0xd7): undefined reference to function fflush': (.text+0xd7): undefined reference to _Unwind_Resume "/ usr / lib 32 / 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 link to _Unwind_Resume' /usr/lib32/libc.a(iofputs.o):(.eh_frame+0xdf): undefined reference to __ gcc_personality_v0 '/ usr / lib32 / libc.a (iofwrite.o): function `FWRITE':

(I have one more error, but this is enough to see the problem, I think)

I saw some solutions indicating that I should reference -lgcc, but no library was found on my computer ...

Does anyone have an idea?

+6
source share
3 answers

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

 gcc -static -o a.out output.o 

You can see exactly what is connected by switching -v to gcc .

+4
source

I had the same problem, so I did

 # gcc -static -o a.out hello.o -v 

which gave me information on what to include, then I could reference ld:

 # ld -static -o hello -L`gcc -print-file-name=` /usr/lib/gcc/x86_64-linux-gnu/4.4.7/../../../x86_64-linux-gnu/crt1.o /usr/lib/gcc/x86_64-linux-gnu/4.4.7/../../../x86_64-linux-gnu/crti.o hello.o /usr/lib/gcc/x86_64-linux-gnu/4.4.7/../../../x86_64-linux-gnu/crtn.o /usr/lib/gcc/x86_64-linux-gnu/4.4.7/crtbeginT.o /usr/lib/gcc/x86_64-linux-gnu/4.4.7/crtend.o --start-group -lc -lgcc -lgcc_eh --end-group 
+3
source

I usually let gcc do something instead of using ld directly. When you have an object, just gcc object.o -o executable

+1
source

Source: https://habr.com/ru/post/893106/


All Articles