forked from nygard/class-dump
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CDSegmentCommand.m
166 lines (126 loc) · 4.04 KB
/
CDSegmentCommand.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
// 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-2006 Steve Nygard
#import "CDSegmentCommand.h"
#include <mach-o/swap.h>
#import <Foundation/Foundation.h>
#import "CDFatFile.h"
#import "CDMachOFile.h"
#import "CDSection.h"
@implementation CDSegmentCommand
- (id)initWithPointer:(const void *)ptr machOFile:(CDMachOFile *)aMachOFile;
{
char buf[17];
if ([super initWithPointer:ptr machOFile:aMachOFile] == nil)
return nil;
segmentCommand = *(struct segment_command *)ptr;
if ([aMachOFile hasDifferentByteOrder] == YES)
swap_segment_command(&segmentCommand, CD_THIS_BYTE_ORDER);
memcpy(buf, segmentCommand.segname, 16);
buf[16] = 0;
name = [[NSString alloc] initWithBytes:buf length:strlen(buf) encoding:NSASCIIStringEncoding];
// Is segmentCommand->segname always going to be NULL terminated?
//name = [[NSString alloc] initWithBytes:segmentCommand->segname length:strlen(segmentCommand->segname) encoding:NSASCIIStringEncoding];
[self _processSectionsWithPointer:ptr + sizeof(struct segment_command)];
return self;
}
- (void)_processSectionsWithPointer:(const void *)ptr;
{
NSMutableArray *sects;
int count, index;
// PRECONDITION: sections == nil
// POSTCONDITION: sections != nil
sects = [[NSMutableArray alloc] init];
count = segmentCommand.nsects;
for (index = 0; index < count; index++) {
CDSection *section;
section = [[CDSection alloc] initWithPointer:ptr segment:self];
[sects addObject:section];
[section release];
ptr += sizeof(struct section);
}
sections = [[NSArray alloc] initWithArray:sects];
[sects release];
}
- (void)dealloc;
{
[name release];
[sections release];
[super dealloc];
}
- (NSString *)name;
{
return name;
}
- (unsigned long)vmaddr;
{
return segmentCommand.vmaddr;
}
- (unsigned long)fileoff;
{
return segmentCommand.fileoff;
}
- (unsigned long)flags;
{
return segmentCommand.flags;
}
- (NSArray *)sections;
{
return sections;
}
- (NSString *)flagDescription;
{
NSMutableArray *setFlags;
unsigned long flags;
setFlags = [NSMutableArray array];
flags = [self flags];
if (flags & SG_HIGHVM)
[setFlags addObject:@"HIGHVM"];
if (flags & SG_FVMLIB)
[setFlags addObject:@"FVMLIB"];
if (flags & SG_NORELOC)
[setFlags addObject:@"NORELOC"];
return [setFlags componentsJoinedByString:@" "];
}
- (NSString *)extraDescription;
{
return [NSString stringWithFormat:@"name: '%@', vmaddr: 0x%08x - 0x%08x [0x%08x], offset: %d, flags: 0x%x (%@), nsects: %d, sections: %@",
name, segmentCommand.vmaddr, segmentCommand.vmaddr + segmentCommand.vmsize - 1, segmentCommand.vmsize, segmentCommand.fileoff,
[self flags], [self flagDescription], segmentCommand.nsects, sections];
}
// TODO (2003-12-06): Might want to make this a range.
- (BOOL)containsAddress:(unsigned long)vmaddr;
{
return (vmaddr >= segmentCommand.vmaddr) && (vmaddr < segmentCommand.vmaddr + segmentCommand.vmsize);
}
- (CDSection *)sectionContainingVMAddr:(unsigned long)vmaddr;
{
int count, index;
count = [sections count];
for (index = 0; index < count; index++) {
CDSection *aSection;
aSection = [sections objectAtIndex:index];
if ([aSection containsAddress:vmaddr] == YES)
return aSection;
}
return nil;
}
- (unsigned long)segmentOffsetForVMAddr:(unsigned long)vmaddr;
{
CDSection *section;
section = [self sectionContainingVMAddr:vmaddr];
NSLog(@"section: %@", section);
return [section segmentOffsetForVMAddr:vmaddr];
}
- (CDSection *)sectionWithName:(NSString *)aName;
{
int count, index;
count = [sections count];
for (index = 0; index < count; index++) {
CDSection *aSection;
aSection = [sections objectAtIndex:index];
if ([[aSection sectionName] isEqual:aName] == YES)
return aSection;
}
return nil;
}
@end