链接NASM程序以在Mac OS X上运行

5

我在为MacOS链接nasm程序时遇到了一些问题:

GLOBAL _start
SEGMENT .text
_start:
    mov ax, 5
    mov bx, ax
    mov [a], ebx
SEGMENT .data
a   DW 0
t2  DW 0

fry$ nasm -f elf  test.asm
fry$ ld -o test test.o -arch i386
ld: warning: in test.o, file was built for unsupported file format which is not the architecture being linked (i386)
ld: could not find entry point "start" (perhaps missing crt1.

fry$ nasm -f macho  test.asm
fry$ ld -o test test.o -arch i386
ld: could not find entry point "start" (perhaps missing crt1.o)

有人能帮我吗?

3个回答

7

Mac OS X链接器无法链接ELF对象。它仅适用于Mach-O可执行文件格式。除非您想弄清楚如何翻译对象文件,否则最好编写适用于Mac OS X汇编程序的代码。

编辑:如@Fry在下面的评论中提到的那样,您可以让nasm输出Mach-O对象。在这种情况下,问题很简单-在源文件中的两个位置上都删除_start前面的_。结果可以成功链接。


fry$ nasm -f macho test.asm fry$ ld -o test test.o -arch i386 ld: 找不到入口点“start”(可能缺少crt1.o) - Constantine Fry
@Fry,已编辑并提供您所需的答案。另外,请接受一些答案。 - Carl Norum

6
nasm -f macho test.asm

ld -e _start -o test test.o

1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接