Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/assembly/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Macos Can';使用ld在Mac OS X中创建t-link程序集文件_Macos_Assembly_Nasm_Ld_Object Files - Fatal编程技术网

Macos Can';使用ld在Mac OS X中创建t-link程序集文件

Macos Can';使用ld在Mac OS X中创建t-link程序集文件,macos,assembly,nasm,ld,object-files,Macos,Assembly,Nasm,Ld,Object Files,我正在尝试使用64位MacOSXLion运行一个基本的汇编文件,使用默认情况下使用Xcode安装的nasm和ld 我已经编写了一个程序集文件,它打印一个字符,并且我使用nasm构建了它 nasm-f elf-o程序.o main.asm 但是,当我将其与ld链接时,它会失败,并出现许多错误/警告: ld-o程序program.o ld: warning: -arch not specified ld: warning: -macosx_version_min not specificed, as


我正在尝试使用64位MacOSXLion运行一个基本的汇编文件,使用默认情况下使用Xcode安装的nasm和ld

我已经编写了一个程序集文件,它打印一个字符,并且我使用nasm构建了它

nasm-f elf-o程序.o main.asm

但是,当我将其与ld链接时,它会失败,并出现许多错误/警告:

ld-o程序program.o

ld: warning: -arch not specified
ld: warning: -macosx_version_min not specificed, assuming 10.7
ld: warning: ignoring file program.o, file was built for unsupported file format which is not the architecture being linked (x86_64)
ld: warning: symbol dyld_stub_binder not found, normally in libSystem.dylib
ld: entry point (start) undefined.  Usually in crt1.o for inferred architecture x86_64
因此,我试图纠正其中一些问题,但毫无结果

以下是我尝试过的一件事:

ld-arch i386-e\u start-o program.o

ld: warning: -arch not specified
ld: warning: -macosx_version_min not specificed, assuming 10.7
ld: warning: ignoring file program.o, file was built for unsupported file format which is not the architecture being linked (x86_64)
ld: warning: symbol dyld_stub_binder not found, normally in libSystem.dylib
ld: entry point (start) undefined.  Usually in crt1.o for inferred architecture x86_64
我以为这会管用,但我错了

如何使对象文件成为nasm和ld同意的兼容体系结构?

另外,您如何定义程序中的入口点(现在我正在
中使用
全局启动
。文本部分
,它位于
\u启动
上方,似乎没有多大用处。)

对于如何使用ld成功地将一个对象文件链接到一个二进制文件,我有点困惑,我想我只是缺少了一些代码(或nasm或ld的参数),使他们同意


感谢您的帮助。

只让
gcc
帮您完成繁重的工作可能比直接驾驶
ld
更容易,例如

$ gcc -m32 program.o -o program

好的,看看您的示例,我假设您使用的是通用nasm或linux汇编教程。
首先需要注意的是nasm创建的二进制格式。
你的帖子说:

ld: warning: ignoring file program.o, file was built for unsupported file format which is not the architecture being linked (x86_64)
这是'-f elf'参数的结果,该参数告诉nasm您想要一个32位elf对象(例如linux)。但是既然你在OSX上,你想要的是一个Mach-O对象

请尝试以下操作:

nasm -f macho64 -o program.o main.asm
gcc -o program program.o
或者,如果不想创建32位二进制文件:

nasm -f macho32 -o program.o main.asm
gcc -m32 -o program program.o

关于\u start符号-如果您不想创建一个能够 要使用提供的libc系统功能,则不应在al处使用\u start。 它是ld将查找的默认入口点,通常在您的libc/libsystem中提供

我建议您尝试将代码中的\u start替换为“u main” 并像上面的例子一样链接它

nasm的基于libc的通用程序集模板可能如下所示:

;---------------------------------------------------
.section text
;---------------------------------------------------
use32             ; use64 if you create 64bit code
global _main      ; export the symbol so ld can find it

_main:
    push ebp
    mov  ebp, esp ; create a basic stack frame

    [your code here]

    pop ebp       ; restore original stack
    mov eax, 0    ; store the return code for main in eax
    ret           ; exit the program

除此之外,我应该提到,您在OSX上执行的任何调用都需要使用对齐的堆栈帧,否则您的代码将崩溃。

关于这方面也有一些很好的教程-请尝试搜索OSX汇编指南。

您需要使用
全局开始
开始:
无下划线。此外,您不应将
elf
用作拱门。下面是我用来在Mac OS X上组装x86-64 NASM程序的bash脚本:

#!/bin/bash

if [[ -n "$1" && -f "$1" ]]; then
    filename="$1"
    base="${filename%%.*}"
    ext="${filename##*.}"

    nasm -f macho64 -Ox "$filename" \
    && ld -macosx_version_min 10.7 "${base}.o" -o "$base"
fi
如果您有一个名为
foo.s
的文件,此脚本将首先运行

nasm -f macho64 -Ox foo.s
这将创建
foo.o
-Ox
标志使NASM对跳跃进行一些额外的优化(即使跳跃变短、变近或变远),这样您就不必自己进行优化。我使用的是x86-64,所以我的代码是64位的,但看起来您正在尝试组装32位。在这种情况下,您将使用
-f macho32
。有关有效输出格式的列表,请参见
nasm-hf

现在,将链接对象文件:

ld -macosx_version_min 10.7 foo.o -o foo
我已将
-macosx\u version\u min
选项设置为使NASM安静下来并防止出现警告。您不必将其设置为Lion(10.7)。这将创建一个名为
foo
的可执行文件。如果运气好的话,键入
/foo
并点击return应该可以运行您的程序


关于
ld:warning:symbol dyld\u stub\u活页夹找不到,通常在libSystem.dylib
警告中,我也每次都会看到,我不知道为什么,但当我运行可执行文件时,一切似乎都很好。

mac gcc编译器不会链接elf对象。你需要一个交叉编译器

然后你可以继续做类似的事情

/usr/local/gcc-4.8.1-for-linux32/bin/i586-pc-linux-ld -m elf_i386 -T link.ld -o kernel kasm.o kc.o

您是否尝试过使用gcc而不是ld进行链接?这通常要简单得多,并且您可以使用C运行时和标准库(例如,将
main
作为入口点)。另一种尝试是使用
mach
对象文件格式,而不是
elf