OLD | NEW |
(Empty) | |
| 1 // -*- mode: ObjC -*- |
| 2 |
| 3 // This file is part of class-dump, a utility for examining the Objective-C seg
ment of Mach-O files. |
| 4 // Copyright (C) 1997-1998, 2000-2001, 2004-2010 Steve Nygard. |
| 5 |
| 6 #import "CDFile.h" |
| 7 |
| 8 #import "CDFatFile.h" |
| 9 #import "CDMachO32File.h" |
| 10 #import "CDMachO64File.h" |
| 11 |
| 12 NSString *CDNameForCPUType(cpu_type_t cputype, cpu_subtype_t cpusubtype) |
| 13 { |
| 14 const NXArchInfo *archInfo; |
| 15 |
| 16 archInfo = NXGetArchInfoFromCpuType(cputype, cpusubtype); |
| 17 if (archInfo == NULL) |
| 18 return [NSString stringWithFormat:@"0x%x:0x%x", cputype, cpusubtype]; |
| 19 |
| 20 return [NSString stringWithUTF8String:archInfo->name]; |
| 21 } |
| 22 |
| 23 CDArch CDArchFromName(NSString *name) |
| 24 { |
| 25 const NXArchInfo *archInfo; |
| 26 CDArch arch; |
| 27 |
| 28 arch.cputype = CPU_TYPE_ANY; |
| 29 arch.cpusubtype = 0; |
| 30 |
| 31 if (name == nil) |
| 32 return arch; |
| 33 |
| 34 archInfo = NXGetArchInfoFromName([name UTF8String]); |
| 35 if (archInfo == NULL) { |
| 36 NSScanner *scanner; |
| 37 NSString *ignore; |
| 38 |
| 39 scanner = [[NSScanner alloc] initWithString:name]; |
| 40 if ([scanner scanHexInt:(uint32_t *)&arch.cputype] |
| 41 && [scanner scanString:@":" intoString:&ignore] |
| 42 && [scanner scanHexInt:(uint32_t *)&arch.cpusubtype]) { |
| 43 // Great! |
| 44 //NSLog(@"scanned 0x%08x : 0x%08x from '%@'", arch.cputype, arch.cpu
subtype, name); |
| 45 } else { |
| 46 arch.cputype = CPU_TYPE_ANY; |
| 47 arch.cpusubtype = 0; |
| 48 } |
| 49 |
| 50 [scanner release]; |
| 51 } else { |
| 52 arch.cputype = archInfo->cputype; |
| 53 arch.cpusubtype = archInfo->cpusubtype; |
| 54 } |
| 55 |
| 56 return arch; |
| 57 } |
| 58 |
| 59 BOOL CDArchUses64BitABI(CDArch arch) |
| 60 { |
| 61 return (arch.cputype & CPU_ARCH_MASK) == CPU_ARCH_ABI64; |
| 62 } |
| 63 |
| 64 @implementation CDFile |
| 65 |
| 66 + (id)fileWithData:(NSData *)someData filename:(NSString *)aFilename searchPathS
tate:(CDSearchPathState *)aSearchPathState; |
| 67 { |
| 68 return [self fileWithData:someData offset:0 filename:aFilename searchPathSta
te:aSearchPathState]; |
| 69 } |
| 70 |
| 71 + (id)fileWithData:(NSData *)someData offset:(NSUInteger)anOffset filename:(NSSt
ring *)aFilename searchPathState:(CDSearchPathState *)aSearchPathState; |
| 72 { |
| 73 CDFatFile *aFatFile = nil; |
| 74 |
| 75 if (anOffset == 0) |
| 76 aFatFile = [[[CDFatFile alloc] initWithData:someData offset:anOffset fil
ename:aFilename searchPathState:aSearchPathState] autorelease]; |
| 77 |
| 78 if (aFatFile == nil) { |
| 79 CDMachOFile *machOFile; |
| 80 |
| 81 machOFile = [[[CDMachO32File alloc] initWithData:someData offset:anOffse
t filename:aFilename searchPathState:aSearchPathState] autorelease]; |
| 82 if (machOFile == nil) |
| 83 machOFile = [[[CDMachO64File alloc] initWithData:someData offset:anO
ffset filename:aFilename searchPathState:aSearchPathState] autorelease]; |
| 84 return machOFile; |
| 85 } |
| 86 |
| 87 return aFatFile; |
| 88 } |
| 89 |
| 90 - (id)init; |
| 91 { |
| 92 [NSException raise:@"RejectUnusedImplementation" format:@"-initWithData: is
the designated initializer"]; |
| 93 return nil; |
| 94 } |
| 95 |
| 96 - (id)initWithData:(NSData *)someData offset:(NSUInteger)anOffset filename:(NSSt
ring *)aFilename searchPathState:(CDSearchPathState *)aSearchPathState; |
| 97 { |
| 98 if ([super init] == nil) |
| 99 return nil; |
| 100 |
| 101 // Otherwise reading the magic number fails. |
| 102 if ([someData length] < 4) { |
| 103 [self release]; |
| 104 return nil; |
| 105 } |
| 106 |
| 107 filename = [aFilename retain]; |
| 108 data = [someData retain]; |
| 109 offset = anOffset; |
| 110 searchPathState = [aSearchPathState retain]; |
| 111 |
| 112 return self; |
| 113 } |
| 114 |
| 115 - (void)dealloc; |
| 116 { |
| 117 [filename release]; |
| 118 [data release]; |
| 119 [searchPathState release]; |
| 120 |
| 121 [super dealloc]; |
| 122 } |
| 123 |
| 124 - (NSString *)filename; |
| 125 { |
| 126 return filename; |
| 127 } |
| 128 |
| 129 - (NSData *)data; |
| 130 { |
| 131 return data; |
| 132 } |
| 133 |
| 134 - (NSUInteger)offset; |
| 135 { |
| 136 return offset; |
| 137 } |
| 138 |
| 139 - (void)setOffset:(NSUInteger)newOffset; |
| 140 { |
| 141 offset = newOffset; |
| 142 } |
| 143 |
| 144 - (CDSearchPathState *)searchPathState; |
| 145 { |
| 146 return searchPathState; |
| 147 } |
| 148 |
| 149 - (BOOL)bestMatchForLocalArch:(CDArch *)archPtr; |
| 150 { |
| 151 if (archPtr != NULL) { |
| 152 archPtr->cputype = CPU_TYPE_ANY; |
| 153 archPtr->cpusubtype = 0; |
| 154 } |
| 155 |
| 156 return YES; |
| 157 } |
| 158 |
| 159 - (CDMachOFile *)machOFileWithArch:(CDArch)arch; |
| 160 { |
| 161 return nil; |
| 162 } |
| 163 |
| 164 @end |
OLD | NEW |