如何在没有库的情况下在 macOS 上编译或构建汇编程序?

问题描述 投票:0回答:1

问题是我在文档/程序集中有一个文件系统,如下所示:

test.s(来自 Linux 教程,使用 Linux 系统调用号):

    .global _start

    .text

_start:

    mov $60, %rax # exit
    mov $0, %rdi # return code 0
    syscall

编译.bash:

as -o test.o test.s && ld -e _start -o test.s test.o && chmod 777 test.o && (./test ; echo $?) 

但是当我尝试运行compile.bash时我得到:

ld: dynamic executables or dylibs must link with libSystem.dylib for architecture x86_64

当我尝试运行 test.o 时:

bash: ./test.o: 无法执行二进制文件

我正在使用 gnu 汇编器,但在教程中我说的是 as,而不是 gcc。

我尝试更改命令,但发生的情况是它给出了很多错误。我使用的是 macOS 蒙特雷版本 12.5.1 (21G83)。我期望它在运行命令时打印 0,但它没有。

(编者注:x86-64 macOS 使用与 x86-64 Linux 不同的系统调用号,因此将这些指令构建到可执行文件中可以运行,但会因段错误或总线错误而退出,但这是一个单独的问题。 对于未来寻找教程的读者,请确保它与您的操作系统以及 CPU 架构和模式(32 位与 64 位)相匹配。)

macos assembly x86-64 gnu-assembler
1个回答
0
投票

ld:动态可执行文件或 dylib 必须与 libSystem.dylib 链接 架构 x86_64

尝试以下几行:

as test.s -o test.o 
ld test.o -o test -macosx_version_min 13.0 -L /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib -lSystem
© www.soinside.com 2019 - 2024. All rights reserved.