我有一个用 NASM(2.11.08) 编写的针对 macho64 架构的小示例程序。我正在运行 OSX 10.10.3:
bits 64
section .data
msg1 db 'Message One', 10, 0
msg1len equ $-msg1
msg2 db 'Message Two', 10, 0
msg2len equ $-msg2
section .text
global _main
extern _printf
_main:
sub rsp, 8 ; align
lea rdi, [rel msg1]
xor rax, rax
call _printf
lea rdi, [rel msg2]
xor rax, rax
call _printf
add rsp, 8
ret
我正在使用以下命令行进行编译和链接:
/usr/local/bin/nasm -f macho64 test2.s
ld -macosx_version_min 10.10.0 -lSystem -o test2 test2.o
当我对 test2 可执行文件进行对象转储时,这是相关的片段(如果我错了,我可以发布更多内容!):
0000000000001fb7 <_main>:
1fb7: 48 83 ec 08 sub $0x8,%rsp
1fbb: 48 8d 3d 56 01 00 00 lea 0x156(%rip),%rdi # 2118 <msg2+0xf3>
1fc2: 48 31 c0 xor %rax,%rax
1fc5: e8 14 00 00 00 callq 1fde <_printf$stub>
1fca: 48 8d 3d 54 00 00 00 lea 0x54(%rip),%rdi # 2025 <msg2>
1fd1: 48 31 c0 xor %rax,%rax
1fd4: e8 05 00 00 00 callq 1fde <_printf$stub>
1fd9: 48 83 c4 08 add $0x8,%rsp
1fdd: c3 retq
...
0000000000002018 <msg1>:
0000000000002025 <msg2>:
最后,输出:
$ ./test2
Message Two
$
我的问题是,msg1 发生了什么?
我假设 msg1 没有打印,因为0x14f(%rip)
不是正确的地址(只是空值)。
Why is lea edi, [rel msg2]
指向正确的地址,同时lea edi, [rel msg1]
是否指向 msg2 后面的 NULL?
它看起来像0x14f(%rip)
offset 正好超出 msg1 在内存中的位置 0x100(在该问题的许多测试中都是如此)。
我在这里缺少什么?
编辑:.data 部分最后出现的消息(msg1 或 msg2)是唯一打印的消息。