macos - ld 链接器错误 - 体系结构 x86_64 的 undefined symbol

标签 macos assembly linker x86 x86-64

我试图链接一个用 yasm 组装的单模块汇编语言程序,我从 ld 得到以下错误:

Undefined symbols for architecture x86_64:
  "start", referenced from:
     implicit entry/start for main executable
     (maybe you meant: _start)
ld: symbol(s) not found for inferred architecture x86_64

我实际上半定期地收到此错误,所以我想这是一个相当普遍的问题,但不知何故似乎没有人有满意的答案。在有人说这是上一个问题的重复之前,是的,我知道。正如您可以查看标题相似的问题的巨大文本墙并发现这是重复的一样,我也可以。

Compiler Error: Undefined symbols for architecture x86_64

不适用于我的问题。我没有使用 C++ 进行编码,并且该问题中给出的解决方案对于该语言来说是特殊的。

undefined symbol for architecture x86_64 in compiling C program

也没有解决我的问题,因为我没有尝试将多个目标文件链接在一起。

Error Undefined symbols for architecture x86_64:

解决方案必须使用高级语言的特定框架。

Compiler Error: Undefined symbols for architecture x86_64

解决方案涉及修复函数原型(prototype)。由于显而易见的原因,此处不适用。

...你明白了。我能找到的每一个过去的问题都是通过一些不适用于我的情况的特殊方法解决的。

请帮我解决这个问题。我厌倦了一次又一次地遇到这个错误,却又无能为力,因为它的记录太少了。恕我直言,世界迫切需要相当于 MS-DOS 错误代码引用手册的 GNU 开发工具。

附加信息:

操作系统:Mac OS X El Capitain

来源列表:

segment .text
global _start

_start:
    mov     eax,1   ; 1 is the syscall number for exit
    mov     ebx,5   ; 5 is the value to return
    int     0x80    ; execute a system call

目标文件的 Hexdump,显示符号确实是 _start 而不是 start:

00000000  cf fa ed fe 07 00 00 01  03 00 00 00 01 00 00 00  |................|
00000010  02 00 00 00 b0 00 00 00  00 00 00 00 00 00 00 00  |................|
00000020  19 00 00 00 98 00 00 00  00 00 00 00 00 00 00 00  |................|
00000030  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000040  0c 00 00 00 00 00 00 00  d0 00 00 00 00 00 00 00  |................|
00000050  0c 00 00 00 00 00 00 00  07 00 00 00 07 00 00 00  |................|
00000060  01 00 00 00 00 00 00 00  5f 5f 74 65 78 74 00 00  |........__text..|
00000070  00 00 00 00 00 00 00 00  5f 5f 54 45 58 54 00 00  |........__TEXT..|
00000080  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000090  0c 00 00 00 00 00 00 00  d0 00 00 00 00 00 00 00  |................|
000000a0  00 00 00 00 00 00 00 00  00 00 00 80 00 00 00 00  |................|
000000b0  00 00 00 00 00 00 00 00  02 00 00 00 18 00 00 00  |................|
000000c0  dc 00 00 00 01 00 00 00  ec 00 00 00 08 00 00 00  |................|
000000d0  b8 01 00 00 00 bb 05 00  00 00 cd 80 01 00 00 00  |................|
000000e0  0f 01 00 00 00 00 00 00  00 00 00 00 00 5f 73 74  |............._st|
000000f0  61 72 74 00                                       |art.|
000000f4

最佳答案

32 位 OS/X 代码通过 int 0x80 进行系统调用

代码:

segment .text
global _start

_start:
    mov     eax,1   ; 1 is the syscall number for exit
    mov     ebx,5   ; 5 is the value to return
    int     0x80    ; execute a system call

建议您使用的是 32 位 Linux 教程。我从 32-bit Linux ABI 开始就得出了这个结论。使用寄存器通过 int 0x80 将参数传递给内核。操作系统/X 是不同的。您在堆栈上传递参数(从右向左传递)。在 32 位 OS/X 中它看起来像:

global start

section .text
start:
    ; sys_write syscall
    ; See: https://opensource.apple.com/source/xnu/xnu-1504.3.12/bsd/kern/syscalls.master
    ; 4 AUE_NULL ALL { user_ssize_t write(int fd, user_addr_t cbuf, user_size_t nbyte); }
    push    dword msg.len  ; Last argument is length
    push    dword msg      ; 2nd last is pointer to string
    push    dword 1        ; 1st argument is File descriptor (1=STDOUT)
    mov     eax, 4         ; eax = 4 is write system call
    sub     esp, 4         ; On OS/X 32-bit code always need to allocate 4 bytes on stack
    int     0x80

    ; sys_exit
    ; 1 AUE_EXIT ALL { void exit(int rval); }
    push    dword 42       ; Return value
    mov     eax, 1         ; eax=1 is exit system call
    sub     esp, 4         ; allocate 4 bytes on stack
    int     0x80

section .rodata

msg:    db      "Hello, world!", 10
.len:   equ     $ - msg

组装和链接:

nasm -f macho testexit.asm
ld -macosx_version_min 10.7.0 -o testexit testexit.o
./testexit
echo $?

YASM 参数应与 NASM 相同。它应该输出:

Hello, world!
42

32 位 OS/X 代码中系统调用的经验法则:

  • 参数在栈上从右向左传递
  • int 0x80 不需要具有 16 字节对齐的堆栈
  • 在压入参数之后和系统调用之前,需要在堆栈上分配额外的 4 个字节。示例:

    1. sub esp, 4
    2. 推送eax
  • EAX寄存器中的系统调用号

  • 通过int 0x80发起的系统调用

Apple 在其 website 上记录了 OS/X 系统调用。 .


64 位 OS/X 代码通过 SYSCALL 指令进行系统调用

64 位 OS/X 几乎使用与 64 位 Linux 相同的内核调用约定。 64-bit Linux System V ABI适用于系统调用。特别是A.2 AMD64 Linux 内核约定部分。该部分具有以下规则:

  1. User-level applications use as integer registers for passing the sequence %rdi, %rsi, %rdx, %rcx, %r8 and %r9. The kernel interface uses %rdi, %rsi, %rdx, %r10, %r8 and %r9.
  2. A system-call is done via the syscall instruction. The kernel destroys registers %rcx and %r11.
  3. The number of the syscall has to be passed in register %rax.
  4. System-calls are limited to six arguments, no argument is passed directly on the stack.
  5. Returning from the syscall, register %rax contains the result of the system-call. A value in the range between -4095 and -1 indicates an error, it is -errno.
  6. Only values of class INTEGER or class MEMORY are passed to the kernel.

64 位 OS/X 使用相同的 System Call numbers作为 32 位 OS/X,但是所有数字都必须添加 0x02000000。上面的代码可以修改为 64 位 OS/X 程序:

global start
section .text

start:
    mov     eax, 0x2000004 ; write system call
    mov     edi, 1         ; stdout = 1
    mov     rsi, msg       ; address of the message to print
    ;lea     rsi, [rel msg]; Alternative way using RIP relative addressing
    mov     edx, msg.len   ; length of message
    syscall                ; Use syscall, NOT int 0x80

    mov     eax, 0x2000001 ; exit system call
    mov     edi, 42        ; return 42 when exiting
    syscall

section .rodata

msg:    db      "Hello, world!", 10
.len:   equ     $ - msg

请注意,当写入 32 位寄存器时,CPU 会自动零扩展到 64 位寄存器。上面的代码通过写入 EAXEDI 而不是 RAXRDI 等寄存器来使用此功能。您本可以使用 64 位寄存器,但使用 32 位寄存器可以在代码中节省一个字节。

组装和链接:

nasm -f macho64 testexit64.asm
ld -macosx_version_min 10.7.0 -lSystem -o testexit64 testexit64.o
./testexit64 
echo $?

它应该输出:

Hello, world!
42

注意:其中一些信息在性质上与 OS/X tutorial 相似修复了一些更正和编码错误。

关于macos - ld 链接器错误 - 体系结构 x86_64 的 undefined symbol ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40814507/

相关文章:

c++ - 编译虚拟 C++ 程序时链接不成功

windows - 为什么 Windows 上的 clang/llvm 需要 Visual Studio 的 Link.exe?

c++ - 与 const char * [] 声明关联的重复符号错误

javascript - JS 中的 native OS X 应用程序?

assembly - 使用 .word 65535 会导致 QEMU 重新启动

ios - Swift 5 URLRequest 授权头 : reserved, 如何设置?

macos - Mac (68000) 汇编程序的名称是什么?

c - 带有 Cmake 的 arm-none-eabi-gcc 没有带有标志 -nostdlib 的入口点

matlab中parpool的Java异常错误

cocoa - Sketch 示例 AppKit 应用程序中的类如何分为模型/ View / Controller ?