首页 > 解决方案 > Mach-O - LINKEDIT 部分字节序

问题描述

我对这个__LINKEDIT部分有点困惑。

让我设置背景:

我所了解的__LINKEDIT

理论上(http://www.newosxbook.com/articles/DYLD.html)第一个“部分”将是LC_DYLD_INFO

如果我检查mach-o/loader.h我得到:

#define LC_DYLD_INFO    0x22
...
struct dyld_info_command {
   uint32_t   cmd;              /* LC_DYLD_INFO or LC_DYLD_INFO_ONLY */
   uint32_t   cmdsize;          /* sizeof(struct dyld_info_command) */
...

如果我检查一个 mach-o 文件,otool我会得到:

$ otool -l MyBinary  | grep -B3 -A8 LINKEDIT                                                              
Load command 3
      cmd LC_SEGMENT_64
  cmdsize 72
  segname __LINKEDIT
   vmaddr 0x0000000100038000
   vmsize 0x0000000000040000
  fileoff 229376
 filesize 254720
  maxprot 0x00000001
 initprot 0x00000001
   nsects 0
    flags 0x0

如果我使用检查十六进制xxd

$ xxd -s 229376 -l 4 MyBinary
00038000: 1122 9002                                ."..

我知道我的二进制文件的字节序是little

$ rabin2 -I MyBinary                                                                                        (03/14 10:21:51)
arch     arm
baddr    0x100000000
binsz    484096
bintype  mach0
bits     64
canary   false
class    MACH064
crypto   false
endian   little
havecode true
intrp    /usr/lib/dyld
laddr    0x0
lang     swift
linenum  false
lsyms    false
machine  all
maxopsz  16
minopsz  1
nx       false
os       darwin
pcalign  0
pic      true
relocs   false
sanitiz  false
static   false
stripped true
subsys   darwin
va       true

__LINKEDIT我可以LC_DYLD_INFO通过获取它的偏移形式来证实第一部分otool

$ otool -l MyBinary  | grep -B1 -A11 LC_DYLD_INFO                                                          (03/14 10:25:35)
Load command 4
            cmd LC_DYLD_INFO_ONLY
        cmdsize 48
     rebase_off 229376
    rebase_size 976
       bind_off 230352
      bind_size 3616
  weak_bind_off 0
 weak_bind_size 0
  lazy_bind_off 233968
 lazy_bind_size 6568
     export_off 240536
    export_size 9744

如果我们检查__LINKEDIT和 from的偏移量,LC_DYLD_INFO我们会得到相同的结果:229376

目前一切都很好,有点道理。

我的困惑

现在,当我在里面lldb并想要理解记忆时。

我可以在偏移处读取内存:

(lldb) image dump sections MyBinary
...
 0x00000400 container        [0x0000000100ca0000-0x0000000100ce0000)  r--  0x00038000 0x0003e300 0x00000000 MyBinary.__LINKEDIT

好的,让我们读一下这段记忆:

(lldb) x/x 0x00000100ca0000
0x100ca0000: 0x02902211

所以这是我的问题:

0x02902211

假设我不知道它是 Little Endian 还是 Big Endian。我应该0x22在字节的开头或结尾找到。但它在中间?(这让我很困惑)

我猜是0x11大小17(十进制),它可能对应于我从结构中看到的loader.h(12bytes + 5bytes of padding?):

struct dyld_info_command {
   uint32_t   cmd;              /* LC_DYLD_INFO or LC_DYLD_INFO_ONLY */
   uint32_t   cmdsize;          /* sizeof(struct dyld_info_command) */

    uint32_t   rebase_off;      /* file offset to rebase info  */
    uint32_t   rebase_size;     /* size of rebase info   */

    uint32_t   bind_off;        /* file offset to binding info   */
    uint32_t   bind_size;       /* size of binding info  */

    uint32_t   weak_bind_off;   /* file offset to weak binding info   */
    uint32_t   weak_bind_size;  /* size of weak binding info  */

    uint32_t   lazy_bind_off;   /* file offset to lazy binding info */
    uint32_t   lazy_bind_size;  /* size of lazy binding infs */

    uint32_t   export_off;      /* file offset to lazy binding info */
    uint32_t   export_size;     /* size of lazy binding infs */
};

我的问题

1.)为什么0x22不是最后(或开始)?还是我错误地读取了偏移量?2.)otool表示命令大小为480x30以十六进制表示),但我无法从0x22. 我从哪里得到尺寸?

感谢您花时间阅读这里,感谢您的帮助。

标签: macosbinaryreverse-engineeringdarwinmach-o

解决方案


推荐阅读