简体   繁体   English

将C函数库链接到现代64位Linux中的x86汇编程序

[英]Linking C function lib to x86 assembly program in modern 64bit Linux

I'm going through a book focusing on x86 programming (Professional Assembly Language, WROX 2005). 我正在读一本专注于x86编程的书(Professional Assembly Language,WROX 2005)。 I had some problems last night and I was hoping to sort this out before returning home today so I can get a running-start and continue the text. 昨晚我遇到了一些问题,我希望在今天返回家乡之前先解决一下问题,这样我才能开始工作并继续阅读本文。 My machine runs x64 Ubuntu (11.04 if I'm not mistaken) so the text focusing on 32bit x86 is slightly 'outdated' (I have to add --32 when assembling etc). 我的机器运行的是x64 Ubuntu(如果我没有记错的话,则为11.04),因此专注于32位x86的文本有些“过时”(组装时我必须添加--32等)。

I am trying to dynamically link C-library functions with my assembly program but I am unsuccesfull (below commands are from memory). 我正在尝试将C库函数与我的汇编程序动态链接,但是我不成功(以下命令来自内存)。

ld -dynamic-linking /lib/ld-linux.so.2 -o complex -lc complex.o -m elf_i386

Running the above command in Linux gives me the message that it can't understand -lc. 在Linux上运行上述命令会给我消息,它无法理解-lc。 Okay, so I removed it. 好的,所以我将其删除。

ld -dynamic-linking /lib/ld-linux.so.2 -o complex complex.o -m elf_i386

I then get the notification that 'printf' is not recognised. 然后,我得到通知,提示“ printf”未被识别。 The hopes was for the dynamic linker to link to the library but it does not seem to do so. 希望动态链接器链接到库,但似乎没有这样做。 Going to \\lib\\ I could not locate ld-linux.so.2 (strangely it didn't give me an error on this) but I did locate ld-linux-86-64.so.2. 转到\\ lib \\我找不到ld-linux.so.2(奇怪的是它没有给我一个错误),但是我确实找到了ld-linux-86-64.so.2。 My code is 32bit but I thought what the heck, let's try this: 我的代码是32位的,但我想这到底是什么,让我们尝试一下:

ld -dynamic-linking /lib/ld-linux-86-64.so.2 -o complex complex.o -m elf_i386

Still it gave the same error that 'call printf' was not recognized. 仍然会出现相同的错误,即无法识别“调用printf”。

Need help dynamically linking C library functions with my 32bit Assembly program using 64bit Linux and standard GNU tools. 需要帮助,使用64位Linux和标准GNU工具将C库函数与我的32位汇编程序动态链接。

Sounds like you need to install the 32-bit C-runtime. 听起来您需要安装32位C运行时。 Under Fedora this is: 在Fedora下是:

yum install glibc-devel.i686

But I don't know the name of the equivalent Ubunutu package; 但是我不知道等效的Ubunutu软件包的名称; perhaps: 也许:

apt-get install libc6-dev-i386

It is almost always a bad idea to try to construct a ld command line yourself. 尝试自己构建ld命令行几乎总是一个坏主意。 Let GCC do it for you; 让GCC为您做; it automatically handles all sorts of subtleties that you don't want to have to worry about. 它会自动处理您不需要担心的各种微妙问题。 For a 32-bit program, you do need one special command line switch, -m32 : 对于32位程序,您确实需要一个特殊的命令行开关-m32

gcc -m32 -o complex complex.o

If you have more .o files, just stack them up at the end. 如果您有更多.o文件,只需将它们堆叠在最后即可。 If you need to link against any system libraries other than libc, put appropriate -lwhatever options after all the object files. 如果你需要比对其他的libc任何系统库链接,把适当的-lwhatever所有目标文件选择。

trojanfoe is also correct; trojanfoe也正确; the 32-bit toolchain is an optional component. 32位工具链是可选组件。 But you need more than just the 32-bit C library. 但是,您不仅需要32位C库。 Try this first: 首先尝试:

apt-get install gcc-multilib

it should pull in most of what you need. 它可以满足您的大部分需求。

Try the following order please(suppose your code fil is try.s): 请尝试以下命令(假设您的代码文件为try.s):

as  --32 -g -o try.o   try.s
ld -m elf_i386  -dynamic-linker /lib/ld-linux.so.2 -lc  -o try try.o

For x86-64 format executable file: 对于x86-64格式的可执行文件:

as   -g -o try.o   try.s
ld  -dynamic-linker  /lib64/ld-linux-x86-64.so.2 -lc  -o try try.o

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM