【问题标题】:how can I get the `__NSAutoreleaseNoPool` address?如何获取 `__NSAutoreleaseNoPool` 地址?
【发布时间】:2014-02-18 09:25:16
【问题描述】:

我试过了

extern void __NSAutoreleaseNoPool(void* obj);

但这会导致链接时出现未解析的符号(但不确定它需要什么框架)。

我也试过

dlsym(RTLD_DEFAULT, "__NSAutoreleaseNoPool")

但这只是NULL

我尝试使用_dyld_lookup_and_bindNSLookupSymbolInImage,但它们也不起作用。

dsymutilnm 都找到了符号,但是:

$ dsymutil -s --arch=x86_64 
----------------------------------------------------------------------
/System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation
----------------------------------------------------------------------
Symbol table for: '/System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation' (x86_64)
----------------------------------------------------------------------
Index    n_strx   n_type             n_sect n_desc n_value
======== -------- ------------------ ------ ------ ----------------
[     0] 00010795 1e (PEXT SECT    ) 01     0000   0000000000000000 '__mh_dylib_header'
[     1] 000107a7 0e (     SECT    ) 01     0000   0000000000001c20 '+[NSObject(NSObject) load]'
[     2] 000107c2 0e (     SECT    ) 01     0000   0000000000002630 '___exceptionInit'
[     3] 000107d3 0e (     SECT    ) 01     0000   00000000000029e0 '___CFgetenv'
[     4] 000107df 0e (     SECT    ) 01     0000   0000000000002a50 '___CFBaseInitialize'
...
[  1923] 0001e820 0e (     SECT    ) 01     0000   000000000010ad30 '___NSAutoreleaseNoPool'
...

$ nm -arch x86_64 /System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation
...
000000000010ad30 t ___NSAutoreleaseNoPool
...

(这是在 MacOSX 10.6 上。在更高版本的 MacOSX 上,该符号似乎确实不存在,至少我无法通过 grep/usr/lib/System/Library/Frameworks 中找到任何参考,而且 LLDB 也找不到它. 可能它被 ARC 以某种方式删除了。)

那么,如何在我的代码中获取该地址?

(相关问题:herehere

(我这样做的动机是here。)

【问题讨论】:

  • 你想达到什么目的?为什么需要地址?
  • @Volker:只是为了尝试一些使用 mach_override 的黑客技术。
  • 试试单下划线;第一个是 cdecl name mangling。
  • @hamstergene:不起作用。此外,__NSAutoreleaseNoPool 确实有两个下划线。
  • 也许用三个下划线调用 dlsym 呢?然而,它可能根本没有导出,至少按名称导出。作为最后的手段,您可以使用调试器将其定位在图像中,并在验证图像的 md5 后,只需在预定义的偏移量处调用即可。

标签: macos dlsym dsymutil


【解决方案1】:

这行得通:

#include <dlfcn.h>
#include <stdio.h>
#import <Foundation/Foundation.h>
#include <mach-o/dyld.h>
#include <mach-o/nlist.h>
#include <string.h>
#include <assert.h>

// Adapted from:
// https://github.com/0xced/iOS-Artwork-Extractor/blob/master/Classes/FindSymbol.c
// Adapted from MoreAddrToSym / GetFunctionName()
// http://www.opensource.apple.com/source/openmpi/openmpi-8/openmpi/opal/mca/backtrace/darwin/MoreBacktrace/MoreDebugging/MoreAddrToSym.c
void *FindSymbol(const struct mach_header *img, const char *symbol)
{
    if ((img == NULL) || (symbol == NULL))
        return NULL;

    // only 64bit supported
#if defined (__LP64__)

    if(img->magic != MH_MAGIC_64)
        // we currently only support Intel 64bit
        return NULL;

    struct mach_header_64 *image = (struct mach_header_64*) img;

    struct segment_command_64 *seg_linkedit = NULL;
    struct segment_command_64 *seg_text = NULL;
    struct symtab_command *symtab = NULL;
    unsigned int index;

    struct load_command *cmd = (struct load_command*)(image + 1);

    for (index = 0; index < image->ncmds; index += 1, cmd = (struct load_command*)((char*)cmd + cmd->cmdsize))
    {
        switch(cmd->cmd)
        {
            case LC_SEGMENT_64: {
                struct segment_command_64* segcmd = (struct segment_command_64*)cmd;
                if (!strcmp(segcmd->segname, SEG_TEXT))
                    seg_text = segcmd;
                else if (!strcmp(segcmd->segname, SEG_LINKEDIT))
                    seg_linkedit = segcmd;
                break;
            }

            case LC_SYMTAB:
                symtab = (struct symtab_command*)cmd;
                break;

            default:
                break;
        }
    }

    if ((seg_text == NULL) || (seg_linkedit == NULL) || (symtab == NULL))
        return NULL;

    unsigned long vm_slide = (unsigned long)image - (unsigned long)seg_text->vmaddr;
    unsigned long file_slide = ((unsigned long)seg_linkedit->vmaddr - (unsigned long)seg_text->vmaddr) - seg_linkedit->fileoff;
    struct nlist_64 *symbase = (struct nlist_64*)((unsigned long)image + (symtab->symoff + file_slide));
    char *strings = (char*)((unsigned long)image + (symtab->stroff + file_slide));
    struct nlist_64 *sym;

    for (index = 0, sym = symbase; index < symtab->nsyms; index += 1, sym += 1)
    {
        if (sym->n_un.n_strx != 0 && !strcmp(symbol, strings + sym->n_un.n_strx))
        {
            unsigned long address = vm_slide + sym->n_value;
            if (sym->n_desc & N_ARM_THUMB_DEF)
                return (void*)(address | 1);
            else
                return (void*)(address);
        }
    }   
#endif

    return NULL;
}

typedef void (*NSAutoreleaseNoPoolFunc) (void* obj);

void getNSAutoreleaseNoPool() { 
    const struct mach_header* img = NSAddImage("/System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation", NSADDIMAGE_OPTION_NONE);
    NSAutoreleaseNoPoolFunc f = (NSAutoreleaseNoPoolFunc) FindSymbol((struct mach_header*)img, "___NSAutoreleaseNoPool");

    printf("func: %p\n", f);

    if(f) {
        NSObject* foo = [[NSObject alloc] init];
        f(foo);
    }
}

它获得与 GDB 中相同的函数指针。

请注意,您不会看到常见的 NSAutoreleaseNoPool 日志:

2014-02-18 14:46:26.583 a.out[24989:a0b] *** __NSAutoreleaseNoPool(): Object 0x7fff71154190 of class NSCFString autoreleased with no pool in place - just leaking

发生这种情况时,标准回溯是这样的:

(gdb) bt
#0  0x00007fff8724bd34 in __NSAutoreleaseNoPool ()
#1  0x00007fff87196e79 in _CFAutoreleasePoolAddObject ()
#2  0x00007fff87196be6 in -[NSObject(NSObject) autorelease] ()

实际的NSLog 调用是在_CFAutoreleasePoolAddObject 中完成的。

关于__NSAutoreleaseNoPool的注释,来自Foundation/NSDebug.h

/****************   Autorelease pool debugging  ****************/

// Functions used as interesting breakpoints in a debugger
// void __NSAutoreleaseNoPool(void *object);
    // Called to log the "Object X of class Y autoreleased with no
    // pool in place - just leaking" message.  If an environment
    // variable named "NSAutoreleaseHaltOnNoPool" is set with string
    // value "YES", the function will automatically break in the
    // debugger (or terminate the process).

// void __NSAutoreleaseFreedObject(void *freedObject);
    // Called when a previously freed object would be released
    // by an autorelease pool.  If an environment variable named
    // "NSAutoreleaseHaltOnFreedObject" is set with string value
    // "YES", the function will automatically break in the debugger
    // (or terminate the process).

因此,如果您想调试此类情况,请启动 GDB 并发出 b __NSAutoreleaseNoPool 以在此函数上设置断点。或者在你的 shell 中做一个export NSAutoreleaseHaltOnNoPool=1

__NSAutoreleaseNoPool 很简单:

(gdb) disassemble 
Dump of assembler code for function __NSAutoreleaseNoPool:
0x00007fff8724bd30 <__NSAutoreleaseNoPool+0>:   push   %rbp
0x00007fff8724bd31 <__NSAutoreleaseNoPool+1>:   mov    %rsp,%rbp
0x00007fff8724bd34 <__NSAutoreleaseNoPool+4>:   nop    
0x00007fff8724bd35 <__NSAutoreleaseNoPool+5>:   nopl   0x0(%rax)
0x00007fff8724bd39 <__NSAutoreleaseNoPool+9>:   lea    0x2ced8(%rip),%rdi        # 0x7fff87278c18 <__PRETTY_FUNCTION__.27904+480>
0x00007fff8724bd40 <__NSAutoreleaseNoPool+16>:  callq  0x7fff871439e0 <__CFgetenv>
0x00007fff8724bd45 <__NSAutoreleaseNoPool+21>:  test   %rax,%rax
0x00007fff8724bd48 <__NSAutoreleaseNoPool+24>:  je     0x7fff8724bd55 <__NSAutoreleaseNoPool+37>
0x00007fff8724bd4a <__NSAutoreleaseNoPool+26>:  movzbl (%rax),%eax
0x00007fff8724bd4d <__NSAutoreleaseNoPool+29>:  cmp    $0x59,%al
0x00007fff8724bd4f <__NSAutoreleaseNoPool+31>:  je     0x7fff8724bd60 <__NSAutoreleaseNoPool+48>
0x00007fff8724bd51 <__NSAutoreleaseNoPool+33>:  cmp    $0x79,%al
0x00007fff8724bd53 <__NSAutoreleaseNoPool+35>:  je     0x7fff8724bd60 <__NSAutoreleaseNoPool+48>
0x00007fff8724bd55 <__NSAutoreleaseNoPool+37>:  leaveq 
0x00007fff8724bd56 <__NSAutoreleaseNoPool+38>:  retq   
0x00007fff8724bd57 <__NSAutoreleaseNoPool+39>:  nopw   0x0(%rax,%rax,1)
0x00007fff8724bd60 <__NSAutoreleaseNoPool+48>:  int3   
0x00007fff8724bd61 <__NSAutoreleaseNoPool+49>:  callq  0x7fff872609c2 <dyld_stub_getpid>
0x00007fff8724bd66 <__NSAutoreleaseNoPool+54>:  mov    %eax,%edi
0x00007fff8724bd68 <__NSAutoreleaseNoPool+56>:  mov    $0x9,%esi
0x00007fff8724bd6d <__NSAutoreleaseNoPool+61>:  leaveq 
0x00007fff8724bd6e <__NSAutoreleaseNoPool+62>:  jmpq   0x7fff87260a16 <dyld_stub_kill>
0x00007fff8724bd73 <__NSAutoreleaseNoPool+67>:  nopw   0x0(%rax,%rax,1)
0x00007fff8724bd79 <__NSAutoreleaseNoPool+73>:  nopl   0x0(%rax)
End of assembler dump.

有关实际示例,请参阅demo_NSAutoreleaseNoPool.mm

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-30
    相关资源
    最近更新 更多