如何将C目标文件与汇编语言的目标文件链接? [英] How to link a C object file with a Assembly Language object file?
问题描述
我有链接一个从C源文件生成的2目标文件从汇编语言源文件生成,其中一个又一个的麻烦。
C源$ C $ C:
// main2.c
EXTERN INT strlength(字符*);
诠释主(){
字符*测试=你好;
INT NUM = strlength(试验);
返回NUM;
}
汇编源$ C $ C:
#strlength.s
.INCLUDELinux32.s.section伪的.text
.globl strlength
.TYPE strlength,@function
strlength:
pushl%EBP
MOVL%ESP,EBP%
MOVL $ 0%ECX
MOVL 8(EBP%),EDX%
read_next_byte:
MOVB(%EDX),%人
CMPB $ END_OF_FILE,%人
JLE结束
含EDX%
含%ECX
JMP read_next_byte
结束:
MOVL%ECX,EAX%
popl%EBP
RET
当我编译和运行使用gcc的''是这样的:
GCC main2.c strlength.s -m32 -o测试
。/测试
回声$?
我得到5这是正确的。然而,当我编译/汇编分开,然后用LD链接
像这样的:
作为strlength.s --32 -o strlength.o
CC main2.c -m32 -o main2.o
LD -melf_i386 -e主要main2.o strlength.o -o测试
。/测试
我得到一个分段错误。是什么原因造成的?我不能跟随c正确调用约定100%?
LD -melf_i386 -e主要main2.o strlength.o -o测试
块引用>不要那样做。做到这一点,而不是:
的gcc -m32 main2.o strlength.o -o测试
(你可能不应该打电话给你测试exectuable
测试
,因为它可能与冲突/斌/测试
标准对大多数UNIX系统。)说明:UNIX二进制文件做的不的一般开始于
执行主
。他们在一个叫_start
功能,它来源于crt1.o
或类似的(C运行时启动)开始执行。的这的文件是libc中的一部分,它安排各种初始化,您的应用程序的正常启动时必需的。您程序实际上并不需要的从任何
的libc
的,这就是为什么你能够将它与链接LD
。不过,考虑你的
主
返回后会发生什么。的常的,在crt1.o
的code将执行(等效)退出(主(ARGC,ARGV ));
。既然你没有crt1.o
链接,没有人这样做,最终的退出
为你,所以$ C $ ç返回到...未定义的位置,并迅速崩溃。I am having trouble linking 2 object files one of which was generated from an Assembly Language Source File and another that was generated from a C Source file.
C source code:
//main2.c extern int strlength(char *); int main(){ char * test = "hello"; int num = strlength(test); return num; }
Assembly source code:
#strlength.s .include "Linux32.s" .section .text .globl strlength .type strlength, @function strlength: pushl %ebp movl %esp, %ebp movl $0, %ecx movl 8(%ebp), %edx read_next_byte: movb (%edx), %al cmpb $END_OF_FILE, %al jle end incl %edx incl %ecx jmp read_next_byte end: movl %ecx, %eax popl %ebp ret
When I compile and run using 'gcc' like this:
gcc main2.c strlength.s -m32 -o test ./test echo $?
I get 5 which is correct. However when I compile/assemble separately and then link with 'ld' like this:
as strlength.s --32 -o strlength.o cc main2.c -m32 -o main2.o ld -melf_i386 -e main main2.o strlength.o -o test ./test
I get a segmentation fault. What is causing this? Am I not following the C calling convention 100% correctly?
解决方案
ld -melf_i386 -e main main2.o strlength.o -o test
Don't do that. Do this instead:
gcc -m32 main2.o strlength.o -o test
(You should probably not call your test exectuable
test
, as it may conflict with the/bin/test
, standard on most UNIX systems.)Explanation: UNIX binaries do not generally start executing at
main
. They start executing in a function called_start
, which comes fromcrt1.o
or similar ("C Runtime startup"). That file is part of libc, and it arranges for various initializations, required for the proper start up of your application.Your program actually doesn't require anything from
libc
, which is why you were able to link it withld
.However, consider what happens after your
main
returns. Normally, the code incrt1.o
will execute (equivalent of)exit(main(argc, argv));
. Since you linked withoutcrt1.o
, there is nobody to do that finalexit
for you, so the code returns to ... undefined location and promptly crashes.这篇关于如何将C目标文件与汇编语言的目标文件链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!