ld 链接器错误 - 体系结构的未定义符号 x86_64

ld linker error - Undefined symbols for architecture x86_64

我正在尝试 link 用 yasm 汇编的 single-module 汇编语言程序,我从 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

我实际上在 semi-regular 的基础上得到这个错误,所以我想这是一个相当普遍的问题,但不知何故似乎没有人有满意的答案。在有人说这是上一个问题的重复之前,是的,我知道。正如您可以查看 text-wall 的 similarly-titled 个问题并发现这是重复的一样,我也可以。

Compiler Error: Undefined symbols for architecture x86_64

不适用于我的问题。我不是用 C++ 编写代码,并且该问题中给出的解决方案是该语言所特有的。

也没有解决我的问题,因为我没有尝试 link 多个 object 文件在一起。

解决方案必须使用 high-level 语言的特定框架。

Compiler Error: Undefined symbols for architecture x86_64

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

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

请帮我解决这个问题。我厌倦了一次又一次地遇到这个错误,却又无能为力,因为它的记录太少了。恕我直言,世界迫切需要一个相当于 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

object 文件的 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 将参数传递给内核。 OS/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

Assemble 和 link 以及:

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. push 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 使用与 32 位 OS/X 相同的 System Call numbers,但是所有数字都必须添加 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 位寄存器可以在代码中节省一个字节。

Assemble 和 link 以及:

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 相似,并修复了一些更正和编码错误。