我对 Mac 上的 x64-assembly 还很陌生,所以我对在 64 位中移植一些 32 位代码感到困惑。程序应该通过C 标准库中
的函数简单地打印出一条消息。
我从这段代码开始:printf
section .data
msg db 'This is a test', 10, 0 ; something stupid here
section .text
global _main
extern _printf
_main:
push rbp
mov rbp, rsp
push msg
call _printf
mov rsp, rbp
pop rbp
ret
以这种方式用 nasm 编译它:
$ nasm -f macho64 main.s
返回以下错误:
main.s:12: error: Mach-O 64-bit format does not support 32-bit absolute addresses
我试图解决这个问题,将代码更改为:
section .data
msg db 'This is a test', 10, 0 ; something stupid here
section .text
global _main
extern _printf
_main:
push rbp
mov rbp, rsp
mov rax, msg ; shouldn't rax now contain the address of msg?
push rax ; push the address
call _printf
mov rsp, rbp
pop rbp
ret
它使用上面的命令编译得很好,nasm
但现在在将目标文件编译gcc
为实际程序时出现警告:
$ gcc main.o
ld: warning: PIE disabled. Absolute addressing (perhaps -mdynamic-no-pic) not
allowed in code signed PIE, but used in _main from main.o. To fix this warning,
don't compile with -mdynamic-no-pic or link with -Wl,-no_pie
由于这是警告而不是错误,因此我执行了该a.out
文件:
$ ./a.out
Segmentation fault: 11
希望有人知道我做错了什么。