linux 大会 "collect2: ld returned 1 exit status"

linux assembly "collect2: ld returned 1 exit status"

我在一个在线编码网站上输入了源代码。

但是我在源代码下面有错误。

我想我省略了"main"。

自从学了intel assmbly,不知道怎么改

你能帮帮我吗?

感谢您提前帮助我。

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: 在函数 _start': (.text+0x18): undefined reference tomain' collect2: ld 返回 1 退出状态

如果您只是将 GCC 作为 link 用户使用,而不关心 C 运行时,那么您可以通过将 -nostdlib 传递给 gcc 来排除它(正如杰斯特指出的那样)。然后您需要提供一个 _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

你 assemble 和 link 是这样的:

nasm -f elf file.asm
gcc -m32 -nostdlib -o file file.o

或者您可以 link 直接使用 ld 这样您也可以这样做:

nasm -f elf file.asm
ld -melf_i386 -o file file.o

这将生成一个名为 file

的 32 位 Linux 可执行文件

使用 C Library/runtime

虽然我认为以下内容不是您想要的,但有人可能会发现以下信息有用:

您可以使用 GCC 并将 C 库重命名为 _STARTmain. C 运行时包含一个名为 _start 的入口点,它处理初始化,然后调用一个名为 main 的函数。您可以利用 C 库,但 main 必须正确设置堆栈框架并正确清理它,并且 return 自 main 完成后将被视为 C 函数。代码看起来像这样:

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

这个例子使用int 0x80系统调用来写标准输出,并使用Cprintf来做同样的事情。您可以 assemble 和 link 到名为 file 的可执行文件,其中:

nasm -f elf file.asm
gcc -m32 -o file file.o