[英]linux assembly “collect2: ld returned 1 exit status”
I entered the source code on a web site of online coding. 我在网上编码的网站上输入了源代码。
But I got error below the source code. 但是我在源代码下面得到了错误。
I think I omitted "main". 我想我省略了“主”。
Since I learned Intel assmbly, I don't know how to fix it. 由于我学习了Intel assmbly,我不知道如何修复它。
Could you help me? 你可以帮帮我吗?
Thank you for helping me in advance. 谢谢你提前帮助我。
SECTION .DATA
hello: db 'Hello world!',10
helloLen: equ $-hello
SECTION .TEXT
GLOBAL _START
_START:
; Write 'Hello world!' to the screen
mov eax,4 ; 'write' system call
mov ebx,1 ; file descriptor 1 = screen
mov ecx,hello ; string to write
mov edx,helloLen ; length of string to write
int 80h ; call the kernel
; Terminate program
mov eax,1 ; 'exit' system call
mov ebx,0 ; exit with error code 0
int 80h ; call the kernel
/usr/lib/gcc/i686-linux-gnu/4.6/../../../i386-linux-gnu/crt1.o: In function _start': (.text+0x18): undefined reference to
main' collect2: ld returned 1 exit status /usr/lib/gcc/i686-linux-gnu/4.6/../../../i386-linux-gnu/crt1.o:函数
_start': (.text+0x18): undefined reference to
main的_start': (.text+0x18): undefined reference to
'collect2:ld返回1退出状态
If you are just using GCC as a linker and don't care about the C runtime then you can exclude it (as Jester pointed out) by passing -nostdlib
to gcc . 如果您只是使用GCC作为链接器并且不关心C运行时,那么您可以通过将
-nostdlib
传递给gcc来排除它(如Jester指出的那样)。 You then need to provide a _start
symbol so the code would look like: 然后,您需要提供
_start
符号,以便代码看起来像:
SECTION .DATA
hello: db 'Hello world!',10
helloLen: equ $-hello
SECTION .TEXT
GLOBAL _start
_start:
; Write 'Hello world!' to the screen
mov eax,4 ; 'write' system call
mov ebx,1 ; file descriptor 1 = screen
mov ecx,hello ; string to write
mov edx,helloLen ; length of string to write
int 80h ; call the kernel
; Terminate program
mov eax,1 ; 'exit' system call
mov ebx,0 ; exit with error code 0
int 80h ; call the kernel
You'd assemble and link it like this: 你会像这样组装和链接它:
nasm -f elf file.asm
gcc -m32 -nostdlib -o file file.o
Alternatively you can link directly with ld
so you could also do this: 或者,您可以直接链接到
ld
这样您也可以这样做:
nasm -f elf file.asm
ld -melf_i386 -o file file.o
This would generate a 32-bit Linux executable called file
这将生成一个名为
file
的32位Linux可执行file
Although I don't think the following is what you intended, someone may find the following information useful: 虽然我不认为以下是您的意图,但有人可能会发现以下信息有用:
You can use GCC and have the C library available to your assembly code by renaming _START
to main
. 您可以使用GCC并通过将
_START
重命名为main
来使C库可用于汇编代码。 The C runtime contains an entry point called _start
that handles initialization and then calls a function called main
. C运行时包含一个名为
_start
的入口点,它处理初始化,然后调用一个名为main
的函数。 You can take advantage of the C library but main
has to setup the stack frame correctly and properly clean it up and return when finished since main
will be treated as a C function. 您可以利用C库,但
main
必须正确设置堆栈框架并正确清理它并在完成后返回,因为main
将被视为C函数。 The code would look something like this: 代码看起来像这样:
EXTERN printf ; Tell the assembler printf is provided outside our file
SECTION .DATA
hello: db 'Hello world!',10,0 ; Null terminate for printf
helloLen: equ $-hello-1 ; Exclude null by reducing len by 1
SECTION .TEXT
GLOBAL main
; main is now a C function
main:
push ebp ; Setup stack frame
mov ebp, esp
push ebx ; We need to preserve EBX (C Calling convention)
; Write 'Hello world!' to the screen
mov eax,4 ; 'write' system call
mov ebx,1 ; file descriptor 1 = screen
mov ecx,hello ; string to write
mov edx,helloLen ; length of string to write
int 80h ; call the kernel
; Write 'Hello World!' with C printf
push hello ; push the address of string to print
call printf ; call printf in C library
add esp, 4 ; Restore stack pointer
; push hello pushed 4 bytes on stack
mov eax, 0x0 ; Return value of 0
pop ebx ; Restore EBX
leave
ret ; Return to C runtime which will cleanup and exit
This example uses both int 0x80
system call to write standard output, and uses C printf
to do the same. 此示例使用
int 0x80
系统调用来编写标准输出,并使用C printf
执行相同操作。 You can assemble and link to an executable called file
with: 您可以使用以下命令汇编和链接到名为
file
的可执行file
nasm -f elf file.asm
gcc -m32 -o file file.o
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.