tito
tito

Reputation: 13271

How to get symbol address on iOS?

I'm building a library for accessing Objective-C from Python. I'm stuck on getting the address of variables on iOS.

Let's say i want to get the pointer address of CBCentralManagerScanOptionAllowDuplicatesKey:

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

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

I got:

Address is: 0x3a827fcc
Address2 is: 0x3a825514

Why do i get different values? I tried to look up with RTLD_NEXT, still get the same value. Is the objective-c variables are mangled somehow?

Upvotes: 1

Views: 481

Answers (1)

Martin R
Martin R

Reputation: 540075

dlsym() gives you the address of the CBCentralManagerScanOptionAllowDuplicatesKey variable, not its contents, which is a pointer to the Objective-C string.

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);

Output:

Address  is: 0x7fff78950388
Key      is: 0x7fff7894ec08
Address2 is: 0x7fff78950388
Key2     is: 0x7fff7894ec08

Upvotes: 5

Related Questions