Assembly 程序集多重定义
给出代码Assembly 程序集多重定义,assembly,Assembly,给出代码 section .data msg db "Hello, world!",0xA len equ $ - msg section .text ;we must export the entry point to the ELF linker or
section .data
msg db "Hello, world!",0xA
len equ $ - msg
section .text
;we must export the entry point to the ELF linker or
global _start
_start:
mov eax,4
mov ebx,1
mov ecx,msg
mov edx,len
int 0x80
mov eax,1
xor ebx,ebx
int 0x80
当尝试运行它时,它将与命令一起显示
linux1[8]% nasm -f elf -l hello.lst hello.asm
linux1[9]% ls
hello.asm hello.lst hello.o
linux1[10]% gcc -o hello hello.o
hello.o: In function `_start':
hello.asm:(.text+0x0): multiple definition of `_start'
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../lib64/crt1.o:(.text+0x0): first defined here
hello.o: could not read symbols: File in wrong format
collect2: ld returned 1 exit status
如何解决多重定义问题?我只定义了_start一次,怎么会是多重定义呢?
谢谢您正在使用
gcc
进行链接,默认情况下会添加C库,这些库需要入口点main
,并且已经包含调用main
的\u start
。这就是为什么你有多重定义
如果您不需要C库(在本代码中不需要),但仍然希望使用gcc进行链接,请尝试gcc-nostlib-m32-o hello.o
错误格式
错误是由于试图从32位对象文件生成64位可执行文件。添加-m32
修复了这一问题,因此您可以获得32位可执行文件(因为您的代码是32位的)。如果您打算创建一个64位程序,请将-f elf64
用于nasm
,当然还可以编写64位兼容的代码。(我向上投票)。当前代码与64位兼容,因为保留int 0x80
是为了向后兼容(但如果仅针对64位,则应转换为系统调用)。他们肯定同意必须确保代码保持64位兼容。是的,尽管这取决于msg
是否在32位范围内(默认情况下是这样)。