简体   繁体   English

linux程序集“collect2:ld返回1退出状态”

[英]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

Using C Library/runtime 使用C库/运行时

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.

相关问题 Visual Studio 2015 / Linux扩展与Cygwin一起产生“ collect2:错误:ld返回1退出状态” - Visual Studio 2015/Linux extension produces a “collect2 : error : ld returned 1 exit status” with Cygwin collect2:错误:ld 返回 1 个退出状态(-lcudnn) - collect2: error: ld returned 1 exit status (-lcudnn) java对`main'collect2的未定义引用:ld返回1退出状态 - java undefined reference to `main' collect2: ld returned 1 exit status 如何解决'collect2:ld返回1退出状态'? - How to solve 'collect2: ld returned 1 exit status '? 多个定义collect2:错误:ld在C中返回了1个退出状态 - Multiple definition collect2: error: ld returned 1 exit status in C MulVAL 中的错误:`mylval' 的多重定义; collect2: 错误: ld 返回 1 个退出状态 - Error in MulVAL: multiple definition of `mylval'; collect2: error: ld returned 1 exit status 编译文件时出错 collect2:错误:ld返回1退出状态 - Error when compiling file | collect2: error: ld returned 1 exit status golang with cgo 抛出错误 collect2: error: ld returns 1 exit status - golang with cgo throws error collect2: error: ld returned 1 exit status /usr/bin/ld: 找不到 -lm collect2: 错误:ld 返回 1 退出状态 g++ 12 Ubuntu 20.4.4 - /usr/bin/ld: cannot find -lm collect2: error: ld returned 1 exit status g++ 12 Ubuntu 20.4.4 Debian使用IPP编译OpenCV示例:/ usr / bin / ld:找不到-llibsvml.so collect2:error:ld返回1退出状态 - Debian compiling OpenCV examples with IPP: /usr/bin/ld: cannot find -llibsvml.so collect2: error: ld returned 1 exit status
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM