如何链接ELF32汇编和C文件?
我用 C 编写了一个简单的汇编软件(nasm)和一个简单的应用程序。 我的 C 代码从汇编代码调用函数,但我不知道如何编译 C 代码而不收到来自“extern int Sum();”的“未定义引用”错误线。
C 代码:
#include <stdio.h>
extern int Sum();
main()
{
int a1, a2, x;
x = Sum(a1, a2);
printf("value of x is: %d\n", x);
}
汇编代码:
global _Sum
_Sum:
push ebp
mov ebp, esp
mov eax, [ebp+8]
mov ecx, [ebp+12]
add eax, ecx
pop ebp
ret
我如何分别编译这两个文件,然后将它们链接到一个统一的文件中?
I wrote a simple piece of Assembly software (nasm) and a simple application in C.
My C code calls a function from the Assembly code, but I don't know how to compile the C code without receiving a 'undefined reference' error from the 'extern int Sum();' line.
C code:
#include <stdio.h>
extern int Sum();
main()
{
int a1, a2, x;
x = Sum(a1, a2);
printf("value of x is: %d\n", x);
}
Assembly code:
global _Sum
_Sum:
push ebp
mov ebp, esp
mov eax, [ebp+8]
mov ecx, [ebp+12]
add eax, ecx
pop ebp
ret
How would i compile these two files separately and link them together into one unified file afterwards?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您应该汇编 .asm 文件以获得 .o (对象)文件,正如您所说的那样。然后您应该编译(但不链接).c 文件以获得另一个 .o 文件,如下所示:
然后您应该将它们链接在一起,如下所示:
然后链接器将立即使用所有目标代码运行,并且应该是能够解析它需要的参考。
You should assemble the .asm file to get a .o (object) file, as you said you've done. Then you should compile (but not link) the .c file to get another .o file, like this:
Then you should link them together, like this:
Then the linker will be run with all the object code at once, and should be able to resolve the references it needs.
在与 ELF 一起使用的 ABI 中,C 名称不会与首字母
_
混淆。In ABI used with ELF, C names aren't mangled with an initial
_
.对于来这里搜索的人:查看代码并在此处预订 http://www.drpaulcarter。 com/pcasm/index.php :通过 此处 :您要引用的文件是first.asm - (根据体系结构编译它:下面的 32 位示例)
For someone who comes searching here: Check out the code and book over here http://www.drpaulcarter.com/pcasm/index.php : Downloadthe linux examples over here : The file you would want to refer is first.asm - (Compile it according to architecture: 32 bits example below )
有两种方法可以解决这个问题,将 C 源代码编译为 C(不是 C++),要将其编译为 C,您必须为其提供扩展名 .c
第二种方法,在 C++ 代码(扩展名 .cpp)中,名称被破坏,当您使用十六进制编辑器编译 C++ 代码,在目标文件中查找并搜索 Sum。在我的例子中,名称变为
_Z3Sumii
。如果将汇编源代码中的名称更改为此,您会发现它可以工作。另外,将 C++ 源代码中的
extern
语句更改为以下内容,因为它需要两个int
类型的参数,例如extern int Sum(int, int);编译
:
There are two possibilities to solve this, compile the C source as C (NOT C++), to compile it as C you must give it the extension .c
The second way, in C++ code (extension .cpp) names are mangled, when you compile the C++ code use a hex editor to look in the object file and search for Sum. In my case the name becomes
_Z3Sumii
. If you change the name in the assembler source to this, you'll find that it works.Also change the
extern
statement in the C++ source to the following because it takes two parameters of typeint
, for example,extern int Sum(int, int);
To compile:
最原始的方法是使用 GCC 工具链获取 C 程序的汇编代码,然后手动汇编两个汇编文件并链接它们。
The most native way is to use GCC toolchain to obtain assembler code of your C program, then manually assembly both assembler files and link them.
根据您的情况:
您可能需要
elf32
In your circumstance:
you may need
elf32