forked from nygard/class-dump
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CDLCSymbolTable.m
221 lines (175 loc) · 7.15 KB
/
CDLCSymbolTable.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
// -*- mode: ObjC -*-
// This file is part of class-dump, a utility for examining the Objective-C segment of Mach-O files.
// Copyright (C) 1997-1998, 2000-2001, 2004-2015 Steve Nygard.
#import "CDLCSymbolTable.h"
#include <mach-o/nlist.h>
#import "CDMachOFile.h"
#import "CDSymbol.h"
#import "CDLCSegment.h"
#import "CDLCDylib.h"
@implementation CDLCSymbolTable
{
struct symtab_command _symtabCommand;
NSArray *_symbols;
NSUInteger _baseAddress;
NSDictionary *_classSymbols;
NSDictionary *_externalClassSymbols;
struct {
unsigned int didFindBaseAddress:1;
unsigned int didWarnAboutUnfoundBaseAddress:1;
unsigned int _unused:30;
} _flags;
}
- (id)initWithDataCursor:(CDMachOFileDataCursor *)cursor;
{
if ((self = [super initWithDataCursor:cursor])) {
_symtabCommand.cmd = [cursor readInt32];
_symtabCommand.cmdsize = [cursor readInt32];
_symtabCommand.symoff = [cursor readInt32];
_symtabCommand.nsyms = [cursor readInt32];
_symtabCommand.stroff = [cursor readInt32];
_symtabCommand.strsize = [cursor readInt32];
// symoff is at the start of the first section (__pointers) of the __IMPORT segment
// stroff falls within the __LINKEDIT segment
#if 0
NSLog(@"symtab: %08x %08x %08x %08x %08x %08x",
symtabCommand.cmd, symtabCommand.cmdsize,
symtabCommand.symoff, symtabCommand.nsyms, symtabCommand.stroff, symtabCommand.strsize);
NSLog(@"data offset for stroff: %lu", [cursor.machOFile dataOffsetForAddress:symtabCommand.stroff]);
#endif
_symbols = nil;
_baseAddress = 0;
_classSymbols = nil;
_flags.didFindBaseAddress = NO;
_flags.didWarnAboutUnfoundBaseAddress = NO;
}
return self;
}
#pragma mark - Debugging
- (NSString *)extraDescription;
{
return [NSString stringWithFormat:@"symoff: 0x%08x (%u), nsyms: 0x%08x (%u), stroff: 0x%08x (%u), strsize: 0x%08x (%u)",
_symtabCommand.symoff, _symtabCommand.symoff, _symtabCommand.nsyms, _symtabCommand.nsyms,
_symtabCommand.stroff, _symtabCommand.stroff, _symtabCommand.strsize, _symtabCommand.strsize];
}
#pragma mark -
- (uint32_t)cmd;
{
return _symtabCommand.cmd;
}
- (uint32_t)cmdsize;
{
return _symtabCommand.cmdsize;
}
#define CD_VM_PROT_RW (VM_PROT_READ|VM_PROT_WRITE)
- (void)loadSymbols;
{
for (CDLoadCommand *loadCommand in [self.machOFile loadCommands]) {
if ([loadCommand isKindOfClass:[CDLCSegment class]]) {
CDLCSegment *segment = (CDLCSegment *)loadCommand;
if (([segment initprot] & CD_VM_PROT_RW) == CD_VM_PROT_RW) {
//NSLog(@"segment... initprot = %08x, addr= %016lx *** r/w", [segment initprot], [segment vmaddr]);
_baseAddress = [segment vmaddr];
_flags.didFindBaseAddress = YES;
break;
}
}
}
NSMutableArray *symbols = [[NSMutableArray alloc] init];
NSMutableDictionary *classSymbols = [[NSMutableDictionary alloc] init];
NSMutableDictionary *externalClassSymbols = [[NSMutableDictionary alloc] init];
CDMachOFileDataCursor *cursor = [[CDMachOFileDataCursor alloc] initWithFile:self.machOFile offset:_symtabCommand.symoff];
//NSLog(@"offset= %lu", [cursor offset]);
//NSLog(@"stroff= %lu", symtabCommand.stroff);
//NSLog(@"strsize= %lu", symtabCommand.strsize);
const char *strtab = (char *)[self.machOFile.data bytes] + _symtabCommand.stroff;
void (^addSymbol)(NSString *, CDSymbol *) = ^(NSString *name, CDSymbol *symbol) {
[symbols addObject:symbol];
NSString *className = [CDSymbol classNameFromSymbolName:symbol.name];
if (className != nil) {
if (symbol.value != 0)
classSymbols[className] = symbol;
else
externalClassSymbols[className] = symbol;
}
};
if (![self.machOFile uses64BitABI]) {
//NSLog(@"32 bit...");
//NSLog(@" str table index type sect desc value");
//NSLog(@" --------------- ---- ---- ---- --------");
for (uint32_t index = 0; index < _symtabCommand.nsyms; index++) {
struct nlist nlist;
nlist.n_un.n_strx = [cursor readInt32];
nlist.n_type = [cursor readByte];
nlist.n_sect = [cursor readByte];
nlist.n_desc = [cursor readInt16];
nlist.n_value = [cursor readInt32];
#if 0
NSLog(@"%5u: %08x %02x %02x %04x %08x - %s",
index, nlist.n_un.n_strx, nlist.n_type, nlist.n_sect, nlist.n_desc, nlist.n_value, strtab + nlist.n_un.n_strx);
#endif
const char *ptr = strtab + nlist.n_un.n_strx;
NSString *str = [[NSString alloc] initWithBytes:ptr length:strlen(ptr) encoding:NSASCIIStringEncoding];
CDSymbol *symbol = [[CDSymbol alloc] initWithName:str machOFile:self.machOFile nlist32:nlist];
addSymbol(str, symbol);
}
//NSLog(@"Loaded %lu 32-bit symbols", [symbols count]);
} else {
//NSLog(@" str table index type sect desc value");
//NSLog(@" --------------- ---- ---- ---- ----------------");
for (uint32_t index = 0; index < _symtabCommand.nsyms; index++) {
struct nlist_64 nlist;
nlist.n_un.n_strx = [cursor readInt32];
nlist.n_type = [cursor readByte];
nlist.n_sect = [cursor readByte];
nlist.n_desc = [cursor readInt16];
nlist.n_value = [cursor readInt64];
#if 0
NSLog(@"%5u: %08x %02x %02x %04x %016x - %s",
index, nlist.n_un.n_strx, nlist.n_type, nlist.n_sect, nlist.n_desc, nlist.n_value, strtab + nlist.n_un.n_strx);
#endif
const char *ptr = strtab + nlist.n_un.n_strx;
NSString *str = [[NSString alloc] initWithBytes:ptr length:strlen(ptr) encoding:NSASCIIStringEncoding];
CDSymbol *symbol = [[CDSymbol alloc] initWithName:str machOFile:self.machOFile nlist64:nlist];
addSymbol(str, symbol);
}
//NSLog(@"Loaded %lu 64-bit symbols", [symbols count]);
}
_symbols = [symbols copy];
_classSymbols = [classSymbols copy];
_externalClassSymbols = [externalClassSymbols copy];
//NSLog(@"symbols: %@", _symbols);
}
- (uint32_t)symoff;
{
return _symtabCommand.symoff;
}
- (uint32_t)nsyms;
{
return _symtabCommand.nsyms;
}
- (uint32_t)stroff;
{
return _symtabCommand.stroff;
}
- (uint32_t)strsize;
{
return _symtabCommand.strsize;
}
- (NSUInteger)baseAddress;
{
if (_flags.didFindBaseAddress == NO && _flags.didWarnAboutUnfoundBaseAddress == NO) {
fprintf(stderr, "Warning: Couldn't find first read/write segment for base address of relocation entries.\n");
_flags.didWarnAboutUnfoundBaseAddress = YES;
}
return _baseAddress;
}
- (CDSymbol *)symbolForClassName:(NSString *)className;
{
return _classSymbols[className];
}
- (CDSymbol *)symbolForExternalClassName:(NSString *)className
{
return _externalClassSymbols[className];
}
@end