首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >macOS上不正确的NASM间接寻址组装

macOS上不正确的NASM间接寻址组装
EN

Stack Overflow用户
提问于 2017-07-12 12:10:38
回答 1查看 162关注 0票数 2

在macOS上组装以下代码:

代码语言:javascript
复制
global start

default rel

section .text

start:
    lea rdx, [buffer + 0]
    lea rdx, [buffer + 1]
    lea rdx, [buffer + 2]
    lea rdx, [buffer + 3]
    lea rdx, [buffer + 4]
    lea rdx, [buffer + 5]
    lea rdx, [buffer + 6]
    lea rdx, [buffer + 7]
    lea rdx, [buffer + 8]


section .data

buffer: db 0,0,0

使用nasm -fmacho64 -w+all test.asm -o test.o命令,将生成:(使用gobjdump -d test.o)

代码语言:javascript
复制
0000000000000000 <start>:
   0:   48 8d 15 38 00 00 00    lea    0x38(%rip),%rdx        # 3f <buffer>
   7:   48 8d 15 32 00 00 00    lea    0x32(%rip),%rdx        # 40 <buffer+0x1>
   e:   48 8d 15 2c 00 00 00    lea    0x2c(%rip),%rdx        # 41 <buffer+0x2>
  15:   48 8d 15 26 00 00 00    lea    0x26(%rip),%rdx        # 42 <buffer+0x3>
  1c:   48 8d 15 20 00 00 00    lea    0x20(%rip),%rdx        # 43 <buffer+0x4>
  23:   48 8d 15 1a 00 00 00    lea    0x1a(%rip),%rdx        # 44 <buffer+0x5>
  2a:   48 8d 15 14 00 00 00    lea    0x14(%rip),%rdx        # 45 <buffer+0x6>
  31:   48 8d 15 0e 00 00 00    lea    0xe(%rip),%rdx        # 46 <buffer+0x7>
  38:   48 8d 15 08 00 00 00    lea    0x8(%rip),%rdx        # 47 <buffer+0x8>

这看起来是对的。当使用ld test.o -o test将其链接到可执行文件时,我们得到:(使用gobjdump -d test)

代码语言:javascript
复制
0000000000001fc1 <start>:
    1fc1:   48 8d 15 38 00 00 00    lea    0x38(%rip),%rdx        # 2000 <buffer>
    1fc8:   48 8d 15 32 00 00 00    lea    0x32(%rip),%rdx        # 2001 <buffer+0x1>
    1fcf:   48 8d 15 2c 00 00 00    lea    0x2c(%rip),%rdx        # 2002 <buffer+0x2>
    1fd6:   48 8d 15 23 00 00 00    lea    0x23(%rip),%rdx        # 2000 <buffer>
    1fdd:   48 8d 15 1d 00 00 00    lea    0x1d(%rip),%rdx        # 2001 <buffer+0x1>
    1fe4:   48 8d 15 17 00 00 00    lea    0x17(%rip),%rdx        # 2002 <buffer+0x2>
    1feb:   48 8d 15 11 00 00 00    lea    0x11(%rip),%rdx        # 2003 <buffer+0x3>
    1ff2:   48 8d 15 0b 00 00 00    lea    0xb(%rip),%rdx        # 2004 <buffer+0x4>
    1ff9:   48 8d 15 05 00 00 00    lea    0x5(%rip),%rdx        # 2005 <buffer+0x5>

突然间,如果n >= 3,缓冲器+n被转换为缓冲区+ (n - 3)。我正在使用的nasm版本是2.13.01,ld是macOS using的系统链接器,ld -v给出:

代码语言:javascript
复制
@(#)PROGRAM:ld  PROJECT:ld64-274.2
configured to support archs: armv6 armv7 armv7s arm64 i386 x86_64 x86_64h armv6m armv7k armv7m armv7em (tvOS)
LTO support using: LLVM version 8.0.0, (clang-800.0.42.1)
TAPI support using: Apple TAPI version 1.30

为什么会发生这种情况?请注意,在linux上以及使用雅玛斯时,相同的代码似乎可以很好地组装和链接。在其他地方,我读到nasm在Mach-O 64格式上有一些奇怪的问题,所以这可能是nasm在这方面的一个缺陷。

EN

回答 1

Stack Overflow用户

发布于 2018-04-20 13:26:51

这似乎是修正在nasm版本2.13.03,如今天的测试。(同样在2.13.02中,但该版本的macho64目标还有其他问题,re @MichaelPetch;.02的输出在本例中等于.03的输出。)对于问题中给出的test.asm和命令,输出如下。

nasm -fmacho64 -w+all test.asm -o test.ogobjdump -d test.o

代码语言:javascript
复制
test.o:     file format mach-o-x86-64


Disassembly of section .text:

0000000000000000 <start>:
   0:   48 8d 15 00 00 00 00    lea    0x0(%rip),%rdx        # 7 <start+0x7>
   7:   48 8d 15 01 00 00 00    lea    0x1(%rip),%rdx        # f <start+0xf>
   e:   48 8d 15 02 00 00 00    lea    0x2(%rip),%rdx        # 17 <start+0x17>
  15:   48 8d 15 03 00 00 00    lea    0x3(%rip),%rdx        # 1f <start+0x1f>
  1c:   48 8d 15 04 00 00 00    lea    0x4(%rip),%rdx        # 27 <start+0x27>
  23:   48 8d 15 05 00 00 00    lea    0x5(%rip),%rdx        # 2f <start+0x2f>
  2a:   48 8d 15 06 00 00 00    lea    0x6(%rip),%rdx        # 37 <start+0x37>
  31:   48 8d 15 07 00 00 00    lea    0x7(%rip),%rdx        # 3f <buffer>
  38:   48 8d 15 08 00 00 00    lea    0x8(%rip),%rdx        # 47 <buffer+0x8>

ld test.o -o testgobjdump -d test

代码语言:javascript
复制
test:     file format mach-o-x86-64


Disassembly of section .text:

0000000000001fc1 <start>:
    1fc1:       48 8d 15 38 00 00 00    lea    0x38(%rip),%rdx        # 2000 <buffer>
    1fc8:       48 8d 15 32 00 00 00    lea    0x32(%rip),%rdx        # 2001 <buffer+0x1>
    1fcf:       48 8d 15 2c 00 00 00    lea    0x2c(%rip),%rdx        # 2002 <buffer+0x2>
    1fd6:       48 8d 15 26 00 00 00    lea    0x26(%rip),%rdx        # 2003 <buffer+0x3>
    1fdd:       48 8d 15 20 00 00 00    lea    0x20(%rip),%rdx        # 2004 <buffer+0x4>
    1fe4:       48 8d 15 1a 00 00 00    lea    0x1a(%rip),%rdx        # 2005 <buffer+0x5>
    1feb:       48 8d 15 14 00 00 00    lea    0x14(%rip),%rdx        # 2006 <buffer+0x6>
    1ff2:       48 8d 15 0e 00 00 00    lea    0xe(%rip),%rdx        # 2007 <buffer+0x7>
    1ff9:       48 8d 15 08 00 00 00    lea    0x8(%rip),%rdx        # 2008 <buffer+0x8>

值得注意的是,我的ld已经更新,ld -v现在提供:

代码语言:javascript
复制
@(#)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)
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45057358

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档