如何在Linux上从汇编代码调用c库?
我正在尝试在 Intel 架构上的 Linux 汇编中编译一个小程序。我想使用C库的一些函数,但它没有链接。
这是我的汇编程序:
.text
.globl main
main:
pushl $512
call malloc
addl $4, %esp
mov $1, %eax
mov $0, %ebx
int $0x80
我正在此处进行编译
as --32 -o output.o output.asm
,一切顺利。然后当我链接时
ld -static -m elf_i386 -o a.out output.o -lc
,我收到以下错误:
(.text+0x1b8):对
_Unwind_Resume' 的未定义引用 /usr/lib32/libc.a(iofclose.o):(.eh_frame+0x167): 未定义的引用 到 __gcc_personality_v0' /usr/lib32/libc.a(iofflush.o):在函数中
fflush': (.text+0xd7): 对 _Unwind_Resume' 的未定义引用 /usr/lib32/libc.a(iofflush.o):(.eh_frame+0xdf): 未定义的引用
__gcc_personality_v0' /usr/lib32/libc.a(iofputs.o):在函数中
fputs': (.text+0x108): 对_Unwind_Resume' 的未定义引用 /usr/lib32/libc.a(iofputs.o):(.eh_frame+0xdf): 未定义的引用 __gcc_personality_v0' /usr/lib32/libc.a(iofwrite.o):在函数中 `fwrite':
我还有另一个错误,但我认为这足以看到问题)
我看到一些解决方案表明我应该与 -lgcc 链接,但在我的计算机上找不到库...
有人有吗一个想法?
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'
__gcc_personality_v0' /usr/lib32/libc.a(iofflush.o): In function
/usr/lib32/libc.a(iofclose.o):(.eh_frame+0x167): undefined reference
tofflush': (.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'
__gcc_personality_v0' /usr/lib32/libc.a(iofwrite.o): In function
/usr/lib32/libc.a(iofputs.o):(.eh_frame+0xdf): undefined reference to
`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 ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
glibc 需要某些初始化代码与可执行文件静态链接。最简单的方法是使用 gcc 进行链接:
您也可以通过将
-v
传递给gcc
来准确查看链接的内容。glibc requires certain initialization code to be statically linked with the executable. The easiest way to do this is to link using gcc:
You can see exactly what is being linked in by passing
-v
togcc
as well.我遇到了同样的问题,所以我这样做了,
这给了我有关要包含的内容的信息,然后我可以使用 ld 进行链接:
I've had same issue, so I did
which gave me information about what to include, then I could link using ld:
我通常让 gcc 来做这件事,而不是直接使用 ld。获得对象后,只需
gcc object.o -oexecutable
I usually let gcc do the thing instead of using directly ld. Once you have the object, just
gcc object.o -o executable