我正在读这篇文章,有一次它给了我这个nasm程序:
; tiny.asm
BITS 32
GLOBAL main
SECTION .text
main:
mov eax, 42
ret
Run Code Online (Sandbox Code Playgroud)
并告诉我运行以下命令:
$ nasm -f elf tiny.asm
$ gcc -Wall -s tiny.o
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
ld: warning: option -s is obsolete and being ignored
ld: warning: ignoring file tiny.o, file was built for unsupported file format which is not the architecture being linked (x86_64)
Undefined symbols for architecture x86_64:
"_main", referenced from:
start in crt1.10.6.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)
我冒昧地猜测可能是什么问题,并将BITS行更改为:
BITS 64
Run Code Online (Sandbox Code Playgroud)
但是当我跑步时,nasm -f elf tiny.asm我得到:
tiny.asm:2: error: `64' is not a valid segment size; must be 16 or 32
Run Code Online (Sandbox Code Playgroud)
如何修改代码以在我的机器上运行?
编辑:
我从评论中获取了Alex的建议并下载了更新的版本.然而,
./nasm-2.09.10/nasm -f elf tiny.asm
Run Code Online (Sandbox Code Playgroud)
抱怨
tiny.asm:2: error: elf32 output format does not support 64-bit code
Run Code Online (Sandbox Code Playgroud)
另一方面,
./nasm-2.09.10/nasm -f elf64 tiny.asm
gcc -Wall -s tiny.o
Run Code Online (Sandbox Code Playgroud)
抱怨
ld: warning: ignoring file tiny.o, file was built for unsupported file format which is not the architecture being linked (x86_64)
Undefined symbols for architecture x86_64:
"_main", referenced from:
start in crt1.10.6.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)
jup*_*p0r 14
为了让您的示例正常工作,您必须进行特定于操作系统X的调整:主要方法前缀为OS X链接器的_:
; tiny.asm
BITS 32
GLOBAL _main
SECTION .text
_main:
mov eax, 42
ret
Run Code Online (Sandbox Code Playgroud)
第二是你必须使用mach文件格式:
nasm -f macho tiny.asm
Run Code Online (Sandbox Code Playgroud)
现在您可以链接它(使用-m32表示32位目标文件):
gcc -m32 tiny.o
Run Code Online (Sandbox Code Playgroud)