nasm - 无法在 macOS Mojave 上将目标文件与 ld 链接

Ver*_*ren 17 macos assembly nasm ld

我正在尝试组装一个简单的 Hello World,它在以前的 macOS 版本中运行良好:

        global   start
        section  .text
start:  mov      rax, 0x02000004
        mov      rdi, 1
        mov      rsi, msg
        mov      rdx, 13
        syscall
        mov      rax, 0x02000001
        xor      rdi, rdi
        syscall

        section  .data
msg:    db       "Hello world!", 10
Run Code Online (Sandbox Code Playgroud)

然后我像以前一样使用nasmand ld

$ nasm -f macho64 hello.asm
$ ld hello.o -o hello
Run Code Online (Sandbox Code Playgroud)

ld给了我以下错误:

ld: warning: No version-min specified on command line
Undefined symbols for architecture x86_64:
  "_main", referenced from:
     implicit entry/start for main executable
ld: symbol(s) not found for inferred architecture x86_64
Run Code Online (Sandbox Code Playgroud)

我尝试切换start_main,但得到以下信息:

ld: warning: No version-min specified on command line
ld: dynamic main executables must link with libSystem.dylib for inferred architecture x86_64
Run Code Online (Sandbox Code Playgroud)

甚至不知道这可能意味着什么。

Ver*_*ren 23

ld需要-lSystem标志来防止它抛出这个错误。它还需要-macosx_version_min删除警告。正确的使用ld方法是:ld hello.o -o hello -macosx_version_min 10.13 -lSystem

在 macOS 11 及更高版本上更新,您还需要通过-L/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib,以便-lSystem正确定位库。-L$(xcode-select -p)/SDKs/MacOSX.sdk/usr/lib如果需要,您可以使用动态评估正确的路径。

  • 那么运行这个 hello.asm 的完整命令是什么?我正在使用相同的教程,只是替换你的“ld”是行不通的。 (2认同)

Ols*_*ist 9

更简单的答案。ld默认为动态链接并尝试加载正在寻找main 的crt1。所以指定静态链接。

% ld -e start -static hello.o -o hello
% ./hello
Hello world!
Run Code Online (Sandbox Code Playgroud)


Sih*_*vic 5

除了上面的@Verloren 答案(/sf/answers/3698164081/

我在 macOS Big Sur (macOS 11.1) 上遇到了问题,其中 flag-lSystem无法定位libSystem.dylib,出现错误

ld: library not found for -lSystem
Run Code Online (Sandbox Code Playgroud)

我发现了 macOS Big Sur,引用自链接:https : //developer.apple.com/documentation/macos-release-notes/macos-big-sur-11_0_1-release-notes

macOS Big Sur 11.0.1 中的新功能,系统附带所有系统提供的库的内置动态链接器缓存。作为此更改的一部分,文件系统上不再存在动态库的副本。尝试通过在路径中查找文件或枚举目录来检查动态库是否存在的代码将失败...

动态库的所有副本都没有位于usr/lib/和相似的位置,因此默认情况下-lSystem找不到标志libSystem.dylib

该解决方案是更新/安装最新版本的命令行工具,如果没有准备好,并设置标志-L的的ld命令/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib

所以完整的命令看起来像这样:

ld hello.o -o hello -macosx_version_min 11.0 -L /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib -lSystem
Run Code Online (Sandbox Code Playgroud)