iOS 上如何获取符号地址?

tit*_*ito 0 objective-c ios

我正在构建一个用于从 Python 访问 Objective-C 的库。我一直在获取 iOS 上变量的地址。

假设我想获取以下指针地址CBCentralManagerScanOptionAllowDuplicatesKey

NSString *key = CBCentralManagerScanOptionAllowDuplicatesKey;
NSLog(@"Address is: %p\n", key);

NSString *key2 = dlsym(RTLD_SELF, "CBCentralManagerScanOptionAllowDuplicatesKey");
NSLog(@"Address2 is: %p\n", key2);
Run Code Online (Sandbox Code Playgroud)

我有:

Address is: 0x3a827fcc
Address2 is: 0x3a825514
Run Code Online (Sandbox Code Playgroud)

为什么我会得到不同的值?我尝试查找RTLD_NEXT,仍然得到相同的值。Objective-C 变量是否以某种方式被破坏了?

Mar*_*n R 5

dlsym()给你变量的地址CBCentralManagerScanOptionAllowDuplicatesKey ,而不是它的内容,它是一个指向 Objective-C 字符串的指针。

NSLog(@"Address  is: %p\n", & CBCentralManagerScanOptionAllowDuplicatesKey);
NSLog(@"Key      is: %p\n", CBCentralManagerScanOptionAllowDuplicatesKey);

void *addr2 = dlsym(RTLD_SELF, "CBCentralManagerScanOptionAllowDuplicatesKey");
NSLog(@"Address2 is: %p\n", addr2);
// Dereference pointer to get its contents:
NSString *key2 = *(NSString * __unsafe_unretained *)addr2;
NSLog(@"Key2     is: %p\n", key2);
Run Code Online (Sandbox Code Playgroud)

输出:

Address  is: 0x7fff78950388
Key      is: 0x7fff7894ec08
Address2 is: 0x7fff78950388
Key2     is: 0x7fff7894ec08
Run Code Online (Sandbox Code Playgroud)