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 "CDLCSymbolTable.h" |
| 7 |
| 8 #include <mach-o/nlist.h> |
| 9 #import "CDMachOFile.h" |
| 10 #import "CDMachO32File.h" |
| 11 #import "CDSymbol.h" |
| 12 #import "CDLCSegment.h" |
| 13 |
| 14 @implementation CDLCSymbolTable |
| 15 |
| 16 - (id)initWithDataCursor:(CDDataCursor *)cursor machOFile:(CDMachOFile *)aMachOF
ile; |
| 17 { |
| 18 if ([super initWithDataCursor:cursor machOFile:aMachOFile] == nil) |
| 19 return nil; |
| 20 |
| 21 symtabCommand.cmd = [cursor readInt32]; |
| 22 symtabCommand.cmdsize = [cursor readInt32]; |
| 23 |
| 24 symtabCommand.symoff = [cursor readInt32]; |
| 25 symtabCommand.nsyms = [cursor readInt32]; |
| 26 symtabCommand.stroff = [cursor readInt32]; |
| 27 symtabCommand.strsize = [cursor readInt32]; |
| 28 |
| 29 // symoff is at the start of the first section (__pointers) of the __IMPORT
segment |
| 30 // stroff falls within the __LINKEDIT segment |
| 31 #if 0 |
| 32 NSLog(@"symtab: %08x %08x %08x %08x %08x %08x", |
| 33 symtabCommand.cmd, symtabCommand.cmdsize, |
| 34 symtabCommand.symoff, symtabCommand.nsyms, symtabCommand.stroff, symta
bCommand.strsize); |
| 35 NSLog(@"data offset for stroff: %lu", [aMachOFile dataOffsetForAddress:symta
bCommand.stroff]); |
| 36 #endif |
| 37 |
| 38 symbols = [[NSMutableArray alloc] init]; |
| 39 baseAddress = 0; |
| 40 |
| 41 classSymbols = [[NSMutableDictionary alloc] init]; |
| 42 |
| 43 flags.didFindBaseAddress = NO; |
| 44 flags.didWarnAboutUnfoundBaseAddress = NO; |
| 45 |
| 46 return self; |
| 47 } |
| 48 |
| 49 - (void)dealloc; |
| 50 { |
| 51 [symbols release]; |
| 52 [classSymbols release]; |
| 53 |
| 54 [super dealloc]; |
| 55 } |
| 56 |
| 57 - (uint32_t)cmd; |
| 58 { |
| 59 return symtabCommand.cmd; |
| 60 } |
| 61 |
| 62 - (uint32_t)cmdsize; |
| 63 { |
| 64 return symtabCommand.cmdsize; |
| 65 } |
| 66 |
| 67 #define CD_VM_PROT_RW (VM_PROT_READ|VM_PROT_WRITE) |
| 68 |
| 69 - (void)loadSymbols; |
| 70 { |
| 71 CDDataCursor *cursor; |
| 72 uint32_t index; |
| 73 const char *strtab, *ptr; |
| 74 |
| 75 for (CDLoadCommand *loadCommand in [nonretained_machOFile loadCommands]) { |
| 76 if ([loadCommand isKindOfClass:[CDLCSegment class]]) { |
| 77 CDLCSegment *segment = (CDLCSegment *)loadCommand; |
| 78 |
| 79 if (([segment initprot] & CD_VM_PROT_RW) == CD_VM_PROT_RW) { |
| 80 //NSLog(@"segment... initprot = %08x, addr= %016lx *** r/w", [se
gment initprot], [segment vmaddr]); |
| 81 baseAddress = [segment vmaddr]; |
| 82 flags.didFindBaseAddress = YES; |
| 83 break; |
| 84 } |
| 85 } |
| 86 } |
| 87 |
| 88 |
| 89 cursor = [[CDDataCursor alloc] initWithData:[nonretained_machOFile data]]; |
| 90 [cursor setByteOrder:[nonretained_machOFile byteOrder]]; |
| 91 [cursor setOffset:[nonretained_machOFile offset] + symtabCommand.symoff]; |
| 92 //NSLog(@"offset= %lu", [cursor offset]); |
| 93 //NSLog(@"stroff= %lu", symtabCommand.stroff); |
| 94 //NSLog(@"strsize= %lu", symtabCommand.strsize); |
| 95 |
| 96 strtab = [nonretained_machOFile machODataBytes] + symtabCommand.stroff; |
| 97 |
| 98 if ([nonretained_machOFile isKindOfClass:[CDMachO32File class]]) { |
| 99 //NSLog(@"32 bit..."); |
| 100 //NSLog(@" str table index type sect desc value"); |
| 101 //NSLog(@" --------------- ---- ---- ---- --------"); |
| 102 for (index = 0; index < symtabCommand.nsyms; index++) { |
| 103 struct nlist nlist; |
| 104 CDSymbol *symbol; |
| 105 NSString *str; |
| 106 |
| 107 nlist.n_un.n_strx = [cursor readInt32]; |
| 108 nlist.n_type = [cursor readByte]; |
| 109 nlist.n_sect = [cursor readByte]; |
| 110 nlist.n_desc = [cursor readInt16]; |
| 111 nlist.n_value = [cursor readInt32]; |
| 112 #if 0 |
| 113 NSLog(@"%5u: %08x %02x %02x %04x %08x - %s", |
| 114 index, nlist.n_un.n_strx, nlist.n_type, nlist.n_sect, nlist.n_
desc, nlist.n_value, strtab + nlist.n_un.n_strx); |
| 115 #endif |
| 116 |
| 117 ptr = strtab + nlist.n_un.n_strx; |
| 118 str = [[NSString alloc] initWithBytes:ptr length:strlen(ptr) encodin
g:NSASCIIStringEncoding]; |
| 119 |
| 120 symbol = [[CDSymbol alloc] initWithName:str machOFile:nonretained_ma
chOFile nlist32:nlist]; |
| 121 [symbols addObject:symbol]; |
| 122 [symbol release]; |
| 123 |
| 124 [str release]; |
| 125 } |
| 126 |
| 127 //NSLog(@"Loaded %lu 32-bit symbols", [symbols count]); |
| 128 } else { |
| 129 //NSLog(@" str table index type sect desc value"); |
| 130 //NSLog(@" --------------- ---- ---- ---- ----------------"); |
| 131 for (index = 0; index < symtabCommand.nsyms; index++) { |
| 132 struct nlist_64 nlist; |
| 133 CDSymbol *symbol; |
| 134 NSString *str; |
| 135 |
| 136 nlist.n_un.n_strx = [cursor readInt32]; |
| 137 nlist.n_type = [cursor readByte]; |
| 138 nlist.n_sect = [cursor readByte]; |
| 139 nlist.n_desc = [cursor readInt16]; |
| 140 nlist.n_value = [cursor readInt64]; |
| 141 #if 0 |
| 142 NSLog(@"%5u: %08x %02x %02x %04x %016x - %s", |
| 143 index, nlist.n_un.n_strx, nlist.n_type, nlist.n_sect, nlist.n_
desc, nlist.n_value, strtab + nlist.n_un.n_strx); |
| 144 #endif |
| 145 ptr = strtab + nlist.n_un.n_strx; |
| 146 str = [[NSString alloc] initWithBytes:ptr length:strlen(ptr) encodin
g:NSASCIIStringEncoding]; |
| 147 |
| 148 symbol = [[CDSymbol alloc] initWithName:str machOFile:nonretained_ma
chOFile nlist64:nlist]; |
| 149 [symbols addObject:symbol]; |
| 150 |
| 151 if ([str hasPrefix:ObjCClassSymbolPrefix] && [symbol value] != 0) { |
| 152 NSString *className = [str substringFromIndex:[ObjCClassSymbolPr
efix length]]; |
| 153 [classSymbols setObject:symbol forKey:className]; |
| 154 } |
| 155 |
| 156 [symbol release]; |
| 157 |
| 158 [str release]; |
| 159 } |
| 160 |
| 161 //NSLog(@"Loaded %lu 64-bit symbols", [symbols count]); |
| 162 } |
| 163 |
| 164 [cursor release]; |
| 165 |
| 166 //NSLog(@"symbols: %@", symbols); |
| 167 } |
| 168 |
| 169 - (uint32_t)symoff; |
| 170 { |
| 171 return symtabCommand.symoff; |
| 172 } |
| 173 |
| 174 - (uint32_t)nsyms; |
| 175 { |
| 176 return symtabCommand.nsyms; |
| 177 } |
| 178 |
| 179 - (uint32_t)stroff; |
| 180 { |
| 181 return symtabCommand.stroff; |
| 182 } |
| 183 |
| 184 - (uint32_t)strsize; |
| 185 { |
| 186 return symtabCommand.strsize; |
| 187 } |
| 188 |
| 189 - (NSUInteger)baseAddress; |
| 190 { |
| 191 if (flags.didFindBaseAddress == NO && flags.didWarnAboutUnfoundBaseAddress =
= NO) { |
| 192 fprintf(stderr, "Warning: Couldn't find first read/write segment for bas
e address of relocation entries.\n"); |
| 193 flags.didWarnAboutUnfoundBaseAddress = YES; |
| 194 } |
| 195 |
| 196 return baseAddress; |
| 197 } |
| 198 |
| 199 - (NSArray *)symbols; |
| 200 { |
| 201 return symbols; |
| 202 } |
| 203 |
| 204 - (CDSymbol *)symbolForClass:(NSString *)className; |
| 205 { |
| 206 return [classSymbols objectForKey:className]; |
| 207 } |
| 208 |
| 209 - (NSString *)extraDescription; |
| 210 { |
| 211 return [NSString stringWithFormat:@"symoff: 0x%08x (%u), nsyms: 0x%08x (%u),
stroff: 0x%08x (%u), strsize: 0x%08x (%u)", |
| 212 symtabCommand.symoff, symtabCommand.symoff, symtabCommand.n
syms, symtabCommand.nsyms, |
| 213 symtabCommand.stroff, symtabCommand.stroff, symtabCommand.s
trsize, symtabCommand.strsize]; |
| 214 } |
| 215 |
| 216 @end |
OLD | NEW |