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 "CDFatFile.h" |
| 7 |
| 8 #include <mach-o/arch.h> |
| 9 #include <mach-o/fat.h> |
| 10 |
| 11 #import "CDDataCursor.h" |
| 12 #import "CDFatArch.h" |
| 13 #import "CDMachOFile.h" |
| 14 |
| 15 @implementation CDFatFile |
| 16 |
| 17 - (id)initWithData:(NSData *)someData offset:(NSUInteger)anOffset filename:(NSSt
ring *)aFilename searchPathState:(CDSearchPathState *)aSearchPathState; |
| 18 { |
| 19 CDDataCursor *cursor; |
| 20 unsigned int index; |
| 21 struct fat_header header; |
| 22 |
| 23 if ([super initWithData:someData offset:anOffset filename:aFilename searchPa
thState:aSearchPathState] == nil) |
| 24 return nil; |
| 25 |
| 26 arches = [[NSMutableArray alloc] init]; |
| 27 |
| 28 cursor = [[CDDataCursor alloc] initWithData:someData]; |
| 29 [cursor setOffset:offset]; |
| 30 header.magic = [cursor readBigInt32]; |
| 31 |
| 32 //NSLog(@"(testing fat) magic: 0x%x", header.magic); |
| 33 if (header.magic != FAT_MAGIC) { |
| 34 [cursor release]; |
| 35 [self release]; |
| 36 return nil; |
| 37 } |
| 38 |
| 39 header.nfat_arch = [cursor readBigInt32]; |
| 40 //NSLog(@"nfat_arch: %u", header.nfat_arch); |
| 41 for (index = 0; index < header.nfat_arch; index++) { |
| 42 CDFatArch *arch; |
| 43 |
| 44 arch = [[CDFatArch alloc] initWithDataCursor:cursor]; |
| 45 [arch setFatFile:self]; |
| 46 [arches addObject:arch]; |
| 47 [arch release]; |
| 48 } |
| 49 |
| 50 [cursor release]; |
| 51 |
| 52 //NSLog(@"arches: %@", arches); |
| 53 |
| 54 return self; |
| 55 } |
| 56 |
| 57 - (void)dealloc; |
| 58 { |
| 59 [arches release]; |
| 60 |
| 61 [super dealloc]; |
| 62 } |
| 63 |
| 64 |
| 65 // Case 1: no arch specified |
| 66 // - check main file for these, then lock down on that arch: |
| 67 // - local arch, 64 bit |
| 68 // - local arch, 32 bit |
| 69 // - any arch, 64 bit |
| 70 // - any arch, 32 bit |
| 71 // |
| 72 // Case 2: you specified a specific arch (i386, x86_64, ppc, ppc7400, ppc64, etc
.) |
| 73 // - only that arch |
| 74 // |
| 75 // In either case, we can ignore the cpu subtype |
| 76 |
| 77 // Returns YES on success, NO on failure. |
| 78 - (BOOL)bestMatchForLocalArch:(CDArch *)archPtr; |
| 79 { |
| 80 const NXArchInfo *archInfo; |
| 81 cpu_type_t targetType; |
| 82 |
| 83 archInfo = NXGetLocalArchInfo(); |
| 84 if (archInfo == NULL) |
| 85 return NO; |
| 86 |
| 87 if (archPtr == NULL) |
| 88 return [arches count] > 0; |
| 89 |
| 90 targetType = archInfo->cputype & ~CPU_ARCH_MASK; |
| 91 |
| 92 #ifdef __LP64__ |
| 93 // This architecture, 64 bit |
| 94 for (CDFatArch *fatArch in arches) { |
| 95 if ([fatArch maskedCPUType] == targetType && [fatArch uses64BitABI]) { |
| 96 *archPtr = [fatArch arch]; |
| 97 return YES; |
| 98 } |
| 99 } |
| 100 #endif |
| 101 |
| 102 // This architecture, 32 bit |
| 103 for (CDFatArch *fatArch in arches) { |
| 104 if ([fatArch maskedCPUType] == targetType && [fatArch uses64BitABI] == N
O) { |
| 105 *archPtr = [fatArch arch]; |
| 106 return YES; |
| 107 } |
| 108 } |
| 109 |
| 110 #ifdef __LP64__ |
| 111 // Any architecture, 64 bit |
| 112 for (CDFatArch *fatArch in arches) { |
| 113 if ([fatArch uses64BitABI]) { |
| 114 *archPtr = [fatArch arch]; |
| 115 return YES; |
| 116 } |
| 117 } |
| 118 #endif |
| 119 |
| 120 // Any architecture, 32 bit |
| 121 for (CDFatArch *fatArch in arches) { |
| 122 if ([fatArch uses64BitABI] == NO) { |
| 123 *archPtr = [fatArch arch]; |
| 124 return YES; |
| 125 } |
| 126 } |
| 127 |
| 128 // Any architecture |
| 129 if ([arches count] > 0) { |
| 130 *archPtr = [[arches objectAtIndex:0] arch]; |
| 131 return YES; |
| 132 } |
| 133 |
| 134 return NO; |
| 135 } |
| 136 |
| 137 - (CDMachOFile *)machOFileWithArch:(CDArch)cdarch; |
| 138 { |
| 139 for (CDFatArch *arch in arches) { |
| 140 if ([arch cpuType] == cdarch.cputype) |
| 141 return [arch machOFile]; |
| 142 } |
| 143 |
| 144 return nil; |
| 145 } |
| 146 |
| 147 - (NSString *)description; |
| 148 { |
| 149 return [NSString stringWithFormat:@"[%p] CDFatFile with %u arches", self, [a
rches count]]; |
| 150 } |
| 151 |
| 152 - (NSArray *)arches; |
| 153 { |
| 154 return arches; |
| 155 } |
| 156 |
| 157 - (NSArray *)archNames; |
| 158 { |
| 159 NSMutableArray *archNames; |
| 160 |
| 161 archNames = [NSMutableArray array]; |
| 162 for (CDFatArch *arch in arches) |
| 163 [archNames addObject:[arch archName]]; |
| 164 |
| 165 return archNames; |
| 166 } |
| 167 |
| 168 @end |
OLD | NEW |