Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support small methods and selector indirection #103

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Source/CDMachOFile.m
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,13 @@ - (NSString *)stringAtAddress:(NSUInteger)address;
if (offset == 0)
return nil;

// Support small methods referencing selector names in __objc_selrefs.
CDSection *section = [segment sectionContainingAddress:address];
if ([[section sectionName] isEqualToString:@"__objc_selrefs"]) {
const void * reference = [self.data bytes] + offset;
offset = ([self ptrSize] == 8) ? *((uint64_t *)reference) : *((uint32_t *)reference);
}

ptr = (uint8_t *)[self.data bytes] + offset;

return [[NSString alloc] initWithBytes:ptr length:strlen(ptr) encoding:NSASCIIStringEncoding];
Expand Down
1 change: 1 addition & 0 deletions Source/CDMachOFileDataCursor.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,6 @@

// Read using the current byteOrder and ptrSize (from the machOFile)
- (uint64_t)readPtr;
- (uint64_t)readPtr:(bool)small;

@end
11 changes: 11 additions & 0 deletions Source/CDMachOFileDataCursor.m
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,15 @@ - (uint64_t)readPtr;
return 0;
}

- (uint64_t)readPtr:(bool)small;
{
// "small" pointers are signed 32-bit values
if (small) {
// The pointers are relative to the location in the image, so get the offset before reading the offset:
return [self offset] + [self readInt32];
} else {
return [self readPtr];
}
}

@end
4 changes: 4 additions & 0 deletions Source/CDObjectiveC2Processor.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@

#import "CDObjectiveCProcessor.h"

#define METHOD_LIST_T_RESERVED_BITS 0x7FFF0003
#define METHOD_LIST_T_SMALL_METHOD_FLAG 0x80000000
#define METHOD_LIST_T_ENTSIZE_MASK (METHOD_LIST_T_RESERVED_BITS|METHOD_LIST_T_SMALL_METHOD_FLAG)

@interface CDObjectiveC2Processor : CDObjectiveCProcessor

@end
22 changes: 12 additions & 10 deletions Source/CDObjectiveC2Processor.m
Original file line number Diff line number Diff line change
Expand Up @@ -398,17 +398,19 @@ - (NSArray *)loadMethodsAtAddress:(uint64_t)address extendedMethodTypesCursor:(C

struct cd_objc2_list_header listHeader;

// See getEntsize() from http://www.opensource.apple.com/source/objc4/objc4-532.2/runtime/objc-runtime-new.h
listHeader.entsize = [cursor readInt32] & ~(uint32_t)3;
listHeader.count = [cursor readInt32];
NSParameterAssert(listHeader.entsize == 3 * [self.machOFile ptrSize]);

// See https://opensource.apple.com/source/objc4/objc4-787.1/runtime/objc-runtime-new.h
uint32_t value = [cursor readInt32];
listHeader.entsize = value & ~METHOD_LIST_T_ENTSIZE_MASK;
bool small = (value & METHOD_LIST_T_SMALL_METHOD_FLAG) != 0;
listHeader.count = [cursor readInt32];
NSParameterAssert(listHeader.entsize == 3 * (small ? sizeof(int32_t) : [self.machOFile ptrSize]));

for (uint32_t index = 0; index < listHeader.count; index++) {
struct cd_objc2_method objc2Method;
objc2Method.name = [cursor readPtr];
objc2Method.types = [cursor readPtr];
objc2Method.imp = [cursor readPtr];

objc2Method.name = [cursor readPtr:small];
objc2Method.types = [cursor readPtr:small];
objc2Method.imp = [cursor readPtr:small];
NSString *name = [self.machOFile stringAtAddress:objc2Method.name];
NSString *types = [self.machOFile stringAtAddress:objc2Method.types];

Expand All @@ -417,7 +419,7 @@ - (NSArray *)loadMethodsAtAddress:(uint64_t)address extendedMethodTypesCursor:(C
types = [self.machOFile stringAtAddress:extendedMethodTypes];
}

//NSLog(@"%3u: %016lx %016lx %016lx", index, objc2Method.name, objc2Method.types, objc2Method.imp);
//NSLog(@"%3u: %016llx %016llx %016llx", index, objc2Method.name, objc2Method.types, objc2Method.imp);
//NSLog(@"name: %@", name);
//NSLog(@"types: %@", types);

Expand Down