From a5aa3f08133bdb592b20d95948a06350b52f5b3f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=AE=B8=20=E8=8B=A5=E6=A2=A6?= Date: Fri, 3 Apr 2015 22:38:09 +0800 Subject: [PATCH 1/7] Adding Swift support --- Source/CDDataCursor.m | 76 ++++++++++++++++++++++++++++++++++++------- 1 file changed, 64 insertions(+), 12 deletions(-) diff --git a/Source/CDDataCursor.m b/Source/CDDataCursor.m index e80834b8..2e8f1d55 100644 --- a/Source/CDDataCursor.m +++ b/Source/CDDataCursor.m @@ -30,15 +30,27 @@ - (const void *)bytes; - (void)setOffset:(NSUInteger)newOffset; { - if (newOffset <= [_data length]) { - _offset = newOffset; - } else { - [NSException raise:NSRangeException format:@"Trying to seek past end of data."]; + if (newOffset == -'S') { + NSLog(@"Warning: Maybe meet a Swift object at %s",__cmd); + _offset = -'S'; + } + else + { + if (newOffset <= [_data length]) { + _offset = newOffset; + } else { + [NSException raise:NSRangeException format:@"Trying to seek past end of data."]; + } } } - (void)advanceByLength:(NSUInteger)length; { + if (_offset == -'S') { + NSLog(@"Warning: Maybe meet a Swift object at %s",__cmd); + _offset += 10; + return; + } if (_offset + length <= [_data length]) { _offset += length; } else { @@ -56,7 +68,10 @@ - (NSUInteger)remaining; - (uint8_t)readByte; { uint8_t result; - + if (_offset == -'S') { + NSLog(@"Warning: Maybe meet a Swift object at %s",__cmd); + return 0; + } if (_offset + sizeof(result) <= [_data length]) { result = OSReadLittleInt16([_data bytes], _offset) & 0xFF; _offset += sizeof(result); @@ -71,7 +86,10 @@ - (uint8_t)readByte; - (uint16_t)readLittleInt16; { uint16_t result; - + if (_offset == -'S') { + NSLog(@"Warning: Maybe meet a Swift object at %s",__cmd); + return 0; + } if (_offset + sizeof(result) <= [_data length]) { result = OSReadLittleInt16([_data bytes], _offset); _offset += sizeof(result); @@ -86,7 +104,10 @@ - (uint16_t)readLittleInt16; - (uint32_t)readLittleInt32; { uint32_t result; - + if (_offset == -'S') { + NSLog(@"Warning: Maybe meet a Swift object at %s",__cmd); + return 0; + } if (_offset + sizeof(result) <= [_data length]) { result = OSReadLittleInt32([_data bytes], _offset); _offset += sizeof(result); @@ -101,7 +122,10 @@ - (uint32_t)readLittleInt32; - (uint64_t)readLittleInt64; { uint64_t result; - + if (_offset == -'S') { + NSLog(@"Warning: Maybe meet a Swift object at %s",__cmd); + return 0; + } if (_offset + sizeof(result) <= [_data length]) { result = OSReadLittleInt64([_data bytes], _offset); _offset += sizeof(result); @@ -116,7 +140,10 @@ - (uint64_t)readLittleInt64; - (uint16_t)readBigInt16; { uint16_t result; - + if (_offset == -'S') { + NSLog(@"Warning: Maybe meet a Swift object at %s",__cmd); + return 0; + } if (_offset + sizeof(result) <= [_data length]) { result = OSReadBigInt16([_data bytes], _offset); _offset += sizeof(result); @@ -131,7 +158,10 @@ - (uint16_t)readBigInt16; - (uint32_t)readBigInt32; { uint32_t result; - + if (_offset == -'S') { + NSLog(@"Warning: Maybe meet a Swift object at %s",__cmd); + return 0; + } if (_offset + sizeof(result) <= [_data length]) { result = OSReadBigInt32([_data bytes], _offset); _offset += sizeof(result); @@ -146,7 +176,10 @@ - (uint32_t)readBigInt32; - (uint64_t)readBigInt64; { uint64_t result; - + if (_offset == -'S') { + NSLog(@"Warning: Maybe meet a Swift object at %s",__cmd); + return 0; + } if (_offset + sizeof(result) <= [_data length]) { result = OSReadBigInt64([_data bytes], _offset); _offset += sizeof(result); @@ -190,6 +223,10 @@ - (double)readLittleFloat64; - (void)appendBytesOfLength:(NSUInteger)length intoData:(NSMutableData *)data; { + if (_offset == -'S') { + NSLog(@"Warning: Maybe meet a Swift object at %s",__cmd); + return; + } if (_offset + length <= [_data length]) { [data appendBytes:(uint8_t *)[_data bytes] + _offset length:length]; _offset += length; @@ -200,6 +237,10 @@ - (void)appendBytesOfLength:(NSUInteger)length intoData:(NSMutableData *)data; - (void)readBytesOfLength:(NSUInteger)length intoBuffer:(void *)buf; { + if (_offset == -'S') { + NSLog(@"Warning: Maybe meet a Swift object at %s",__cmd); + return; + } if (_offset + length <= [_data length]) { memcpy(buf, (uint8_t *)[_data bytes] + _offset, length); _offset += length; @@ -215,6 +256,10 @@ - (BOOL)isAtEnd; - (NSString *)readCString; { + if (_offset == -'S') { + NSLog(@"Warning: Maybe meet a Swift object at %s",__cmd); + return @"Swift"; + } return [self readStringOfLength:strlen((const char *)[_data bytes] + _offset) encoding:NSASCIIStringEncoding]; } @@ -232,7 +277,10 @@ - (NSString *)readStringOfLength:(NSUInteger)length encoding:(NSStringEncoding)e NSLog(@"Error: malloc() failed."); return nil; } - + if (_offset == -'S') { + NSLog(@"Warning: Maybe meet a Swift object at 1 of %s",__cmd); + return @"Swift"; + } strncpy(buf, (const char *)[_data bytes] + _offset, length); buf[length] = 0; @@ -246,6 +294,10 @@ - (NSString *)readStringOfLength:(NSUInteger)length encoding:(NSStringEncoding)e return str; } } else { + if (_offset == -'S') { + NSLog(@"Warning: Maybe meet a Swift object at 2 of %s",__cmd); + return @"Swift"; + } [NSException raise:NSRangeException format:@"Trying to read past end in %s", __cmd]; } From 45a0dc3f87f4fd49425a498e2ec732104d8884fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=AE=B8=20=E8=8B=A5=E6=A2=A6?= Date: Fri, 3 Apr 2015 22:39:19 +0800 Subject: [PATCH 2/7] Adding Swift support --- Source/CDObjectiveC2Processor.m | 33 ++++++++++++++++++++------------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/Source/CDObjectiveC2Processor.m b/Source/CDObjectiveC2Processor.m index 4bc07cf9..86b95b7b 100644 --- a/Source/CDObjectiveC2Processor.m +++ b/Source/CDObjectiveC2Processor.m @@ -63,15 +63,17 @@ - (CDOCProtocol *)protocolAtAddress:(uint64_t)address; { if (address == 0) return nil; - CDOCProtocol *protocol = [self.protocolUniquer protocolWithAddress:address]; if (protocol == nil) { protocol = [[CDOCProtocol alloc] init]; [self.protocolUniquer setProtocol:protocol withAddress:address]; CDMachOFileDataCursor *cursor = [[CDMachOFileDataCursor alloc] initWithFile:self.machOFile address:address]; + if ([cursor offset] == -'S') { + NSLog(@"Warning: Meet Swift object at %s",__cmd); + return nil; + } NSParameterAssert([cursor offset] != 0); - struct cd_objc2_protocol objc2Protocol; objc2Protocol.isa = [cursor readPtr]; objc2Protocol.name = [cursor readPtr]; @@ -316,18 +318,23 @@ - (NSArray *)loadPropertiesAtAddress:(uint64_t)address; listHeader.entsize = [cursor readInt32]; listHeader.count = [cursor readInt32]; - NSParameterAssert(listHeader.entsize == 2 * [self.machOFile ptrSize]); - for (uint32_t index = 0; index < listHeader.count; index++) { - struct cd_objc2_property objc2Property; - - objc2Property.name = [cursor readPtr]; - objc2Property.attributes = [cursor readPtr]; - NSString *name = [self.machOFile stringAtAddress:objc2Property.name]; - NSString *attributes = [self.machOFile stringAtAddress:objc2Property.attributes]; - - CDOCProperty *property = [[CDOCProperty alloc] initWithName:name attributes:attributes]; - [properties addObject:property]; + if (listHeader.entsize == 2 * [self.machOFile ptrSize]) { + for (uint32_t index = 0; index < listHeader.count; index++) { + struct cd_objc2_property objc2Property; + + objc2Property.name = [cursor readPtr]; + objc2Property.attributes = [cursor readPtr]; + NSString *name = [self.machOFile stringAtAddress:objc2Property.name]; + NSString *attributes = [self.machOFile stringAtAddress:objc2Property.attributes]; + + CDOCProperty *property = [[CDOCProperty alloc] initWithName:name attributes:attributes]; + [properties addObject:property]; + } + } + else + { + return nil; } } From ae660e850bdc081ed43f249c3193c79b7a92492f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=AE=B8=20=E8=8B=A5=E6=A2=A6?= Date: Fri, 3 Apr 2015 22:39:24 +0800 Subject: [PATCH 3/7] Adding Swift support --- Source/CDLCDyldInfo.m | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Source/CDLCDyldInfo.m b/Source/CDLCDyldInfo.m index 4a70d217..a191422e 100644 --- a/Source/CDLCDyldInfo.m +++ b/Source/CDLCDyldInfo.m @@ -69,6 +69,11 @@ - (id)initWithDataCursor:(CDMachOFileDataCursor *)cursor; _dyldInfoCommand.export_off = [cursor readInt32]; _dyldInfoCommand.export_size = [cursor readInt32]; + if (_dyldInfoCommand.cmd == -'S' || _dyldInfoCommand.cmdsize == -'S' || _dyldInfoCommand.rebase_off == -'S' || _dyldInfoCommand.rebase_size == -'S' || _dyldInfoCommand.bind_off == -'S' || _dyldInfoCommand.bind_size == -'S' || _dyldInfoCommand.weak_bind_off == -'S' || _dyldInfoCommand.weak_bind_size == -'S' || _dyldInfoCommand.lazy_bind_off == -'S' || _dyldInfoCommand.lazy_bind_size == -'S' || _dyldInfoCommand.export_off == -'S' || _dyldInfoCommand.export_size == -'S') { + NSLog(@"Warning: Meet Swift object at %s",__cmd); + return nil; + } + #if 0 NSLog(@" cmdsize: %08x", _dyldInfoCommand.cmdsize); NSLog(@" rebase_off: %08x", _dyldInfoCommand.rebase_off); From ad45736d1ea0913b1e5801e0596a23d86e75e80c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=AE=B8=20=E8=8B=A5=E6=A2=A6?= Date: Fri, 3 Apr 2015 22:42:33 +0800 Subject: [PATCH 4/7] Adding Swift support --- Source/CDMachOFile.m | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/Source/CDMachOFile.m b/Source/CDMachOFile.m index 3d6656a1..33576783 100644 --- a/Source/CDMachOFile.m +++ b/Source/CDMachOFile.m @@ -315,9 +315,9 @@ - (NSString *)stringAtAddress:(NSUInteger)address; CDLCSegment *segment = [self segmentContainingAddress:address]; if (segment == nil) { - NSLog(@"Error: Cannot find offset for address 0x%08lx in stringAtAddress:", address); - exit(5); - return nil; + NSLog(@"Warning: Cannot find offset for address 0x%08lx in stringAtAddress:", address); +// exit(5); + return @"Swift"; } if ([segment isProtected]) { @@ -333,7 +333,10 @@ - (NSString *)stringAtAddress:(NSUInteger)address; NSUInteger offset = [self dataOffsetForAddress:address]; if (offset == 0) return nil; - + if (offset == -'S') { + NSLog(@"Warning: Meet Swift object at %s",__cmd); + return @"Swift"; + } ptr = (uint8_t *)[self.data bytes] + offset; return [[NSString alloc] initWithBytes:ptr length:strlen(ptr) encoding:NSASCIIStringEncoding]; @@ -346,8 +349,10 @@ - (NSUInteger)dataOffsetForAddress:(NSUInteger)address; CDLCSegment *segment = [self segmentContainingAddress:address]; if (segment == nil) { - NSLog(@"Error: Cannot find offset for address 0x%08lx in dataOffsetForAddress:", address); - exit(5); + NSLog(@"Warning: Cannot find offset for address 0x%08lx in dataOffsetForAddress:", address); + NSLog(@"Warning: Maybe meet a Swift object at %s",__cmd); +// exit(5); + return -'S'; } if ([segment isProtected]) { From 38464a3fa8be3e0039b1b4e839841179ec6d1539 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=AE=B8=20=E8=8B=A5=E6=A2=A6?= Date: Fri, 3 Apr 2015 22:51:47 +0800 Subject: [PATCH 5/7] Adding Swift support --- README.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/README.md b/README.md index 93a65a66..be7441de 100644 --- a/README.md +++ b/README.md @@ -13,6 +13,22 @@ The latest version and information is available at: The source code is also available from my Github repository at: https://github.com/nygard/class-dump + +"Swift support" +========== + +I added "Swift support" for class-dump. + +Now, the tool can dump Objective-C headers even the executable file uses Swift and ObjC at the same time. +Notice, only ObjC headers can be dumped! + +LAST, THIS IS AN EXPERIMENTAL VERSION. + +我为class-dump添加了"Swift支持"。 + +现在,这个工具可以dump出可执行文件的Objective-C头文件,即使那个可执行文件同时使用了Swift和ObjC。请注意只有ObjC类的头文件可以被dump出来! + +最后,这只是一个试验版本。 Usage ----- From 6d70b4c38454e3c3033e706ac8595b2b90cd6131 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=AE=B8=20=E8=8B=A5=E6=A2=A6?= Date: Fri, 3 Apr 2015 22:54:15 +0800 Subject: [PATCH 6/7] Adding Swift support --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index be7441de..5ed7e0db 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,7 @@ The source code is also available from my Github repository at: I added "Swift support" for class-dump. -Now, the tool can dump Objective-C headers even the executable file uses Swift and ObjC at the same time. +Now, this tool can dump Objective-C headers even the executable file uses Swift and ObjC at the same time. Notice, only ObjC headers can be dumped! LAST, THIS IS AN EXPERIMENTAL VERSION. From 09a09df5fa5904c64331580c4371d35c5a966b82 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=AE=B8=20=E8=8B=A5=E6=A2=A6?= Date: Fri, 3 Apr 2015 22:58:18 +0800 Subject: [PATCH 7/7] Adding Swift support --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 5ed7e0db..5bc5834c 100644 --- a/README.md +++ b/README.md @@ -19,14 +19,14 @@ The source code is also available from my Github repository at: I added "Swift support" for class-dump. -Now, this tool can dump Objective-C headers even the executable file uses Swift and ObjC at the same time. +Now, this tool can dump Objective-C headers even the MachO file uses Swift and ObjC at the same time. Notice, only ObjC headers can be dumped! LAST, THIS IS AN EXPERIMENTAL VERSION. 我为class-dump添加了"Swift支持"。 -现在,这个工具可以dump出可执行文件的Objective-C头文件,即使那个可执行文件同时使用了Swift和ObjC。请注意只有ObjC类的头文件可以被dump出来! +现在,这个工具可以dump出可执行文件的Objective-C头文件,即使那个MachO文件同时使用了Swift和ObjC。请注意只有ObjC类的头文件可以被dump出来! 最后,这只是一个试验版本。