尝试运行一个简单的程序,在我的Macbook上用nasm编写。下面的代码,我已经删除了所有不必要的部分并尽可能地简化了代码。
问题:
打印txt1时,输出正确打印到stdout
打印txt2时(如下面的代码所示),不会产生输出,退出代码为0.
代码:
%define SYSCALL_EXIT 0x2000001
%define SYSCALL_WRITE 0x2000004
%define STDOUT 0x1
section .data
txt1: db "first line of text", 10, 0
t1len: equ $-txt1
txt2 db "second line of text", 10, 0
t2len: equ $-txt2
section .text
global _main
_main:
call printText
mov rax, SYSCALL_EXIT
mov rdi, 0
syscall
printText:
mov rax, SYSCALL_WRITE
mov rdi, STDOUT
mov rsi, txt2
mov rdx, t2len
syscall
ret
编译:
nasm -f macho64 -o name2.o name2.asm
链接:
gcc -o name2 name2.o -Wl,-no_pie
还尝试使用 ld :
ld -e _main -o name2 name2.o
Nasm版本:
mac:asm-codes jabbson$ nasm -v
NASM version 2.13.02 compiled on Nov 30 2017
GCC版本:
mac:asm-codes jabbson$ gcc -v
Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 9.0.0 (clang-900.0.39.2)
Target: x86_64-apple-darwin17.3.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
-Wl,-no_pie的原因 - 没有它我收到错误:
ld: warning: PIE disabled. Absolute addressing (perhaps -mdynamic-no-pic) not allowed in code signed PIE, but used in printText from name2.o. To fix this warning, don't compile with -mdynamic-no-pic or link with -Wl,-no_pie
ld版本:
mac:asm-codes jabbson$ ld -v
@(#)PROGRAM:ld PROJECT:ld64-305
configured to support archs: armv6 armv7 armv7s arm64 i386 x86_64 x86_64h armv6m armv7k armv7m armv7em (tvOS)
LTO support using: LLVM version 9.0.0, (clang-900.0.39.2) (static support for 21, runtime is 21)
TAPI support using: Apple TAPI version 900.0.15 (tapi-900.0.15)