-
Notifications
You must be signed in to change notification settings - Fork 7
/
Symbol.m
67 lines (54 loc) · 1.9 KB
/
Symbol.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
//
// Symbol.m
// SymTabCreator
//
// Created by Karsten Kusche on 17.06.10.
// Copyright 2010 Briksoftware.com. All rights reserved.
//
#import "Symbol.h"
@implementation Symbol
- (NSComparisonResult)compareWithSymbol:(Symbol*)otherSymbol
{
return [self offset] - [otherSymbol offset];
}
+ (id)fromLine:(NSString*) string
{
if (string == nil || [string length] == 0) return nil;
NSScanner *scanner = [NSScanner scannerWithString:string];
uint64_t scannedOffset = 0;
NSString *scannedSymbolName = nil;
NSUInteger pointerSize = 0;
BOOL symbolNameOK = NO;
if ([scanner scanHexLongLong:&scannedOffset])
{
pointerSize = [scanner scanLocation] > 10 ? 8 : 4;
uint64_t imageBase = pointerSize == 8 ? 0x100000000 : 0x1000;
scannedOffset -= imageBase;
[scanner scanString:@" " intoString:NULL];
symbolNameOK = [scanner scanUpToString:@"" intoString:&scannedSymbolName];
}
if (!symbolNameOK)
{
fprintf(stderr,"error parsing line: %s\n",[string UTF8String]);
return nil;
}
Symbol *new = [[[self alloc] init] autorelease];
new.symbolName = scannedSymbolName;
new.offset = scannedOffset;
new.pointerSize = pointerSize;
return new;
}
- (NSInteger)writeToFile: (FILE*)file fromOffset:(NSInteger)startOffset
{
BOOL isMethodSymbol = ([self.symbolName hasPrefix:@"+["] || [self.symbolName hasPrefix:@"-["]);
const char *name = [self.symbolName UTF8String];
const char *nameWithUnderscore = isMethodSymbol ? name : [[@"_" stringByAppendingString:self.symbolName] UTF8String];
fprintf(file,".space %s,0x90\n",[[[NSNumber numberWithInteger:(self.offset - startOffset)] stringValue] UTF8String]);
fprintf(file,".globl \"%s\" \n \"%s\": \n .stabs \"%s:F(0,1)\",36,0,0,\"%s\" \n", nameWithUnderscore, nameWithUnderscore, name, nameWithUnderscore);
return self.offset;
}
- (NSString*) description
{
return [NSString stringWithFormat:@"%0*llx %@", (int)self.pointerSize * 2, self.offset, self.symbolName];
}
@end