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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
|
#pragma once
#include <binaryninjaapi.h>
#include "../core/MetadataSerializable.hpp"
#include "view/macho/machoview.h"
#include "sharedcachecore.h"
using namespace BinaryNinja;
namespace SharedCacheAPI {
template<class T>
class SCRefCountObject {
void AddRefInternal() { m_refs.fetch_add(1); }
void ReleaseInternal() {
if (m_refs.fetch_sub(1) == 1)
delete this;
}
public:
std::atomic<int> m_refs;
T *m_object;
SCRefCountObject() : m_refs(0), m_object(nullptr) {}
virtual ~SCRefCountObject() {}
T *GetObject() const { return m_object; }
static T *GetObject(SCRefCountObject *obj) {
if (!obj)
return nullptr;
return obj->GetObject();
}
void AddRef() { AddRefInternal(); }
void Release() { ReleaseInternal(); }
void AddRefForRegistration() { AddRefInternal(); }
};
template<class T, T *(*AddObjectReference)(T *), void (*FreeObjectReference)(T *)>
class SCCoreRefCountObject {
void AddRefInternal() { m_refs.fetch_add(1); }
void ReleaseInternal() {
if (m_refs.fetch_sub(1) == 1) {
if (!m_registeredRef)
delete this;
}
}
public:
std::atomic<int> m_refs;
bool m_registeredRef = false;
T *m_object;
SCCoreRefCountObject() : m_refs(0), m_object(nullptr) {}
virtual ~SCCoreRefCountObject() {}
T *GetObject() const { return m_object; }
static T *GetObject(SCCoreRefCountObject *obj) {
if (!obj)
return nullptr;
return obj->GetObject();
}
void AddRef() {
if (m_object && (m_refs != 0))
AddObjectReference(m_object);
AddRefInternal();
}
void Release() {
if (m_object)
FreeObjectReference(m_object);
ReleaseInternal();
}
void AddRefForRegistration() { m_registeredRef = true; }
void ReleaseForRegistration() {
m_object = nullptr;
m_registeredRef = false;
if (m_refs == 0)
delete this;
}
};
struct DSCMemoryRegion {
uint64_t vmAddress;
uint64_t size;
std::string prettyName;
};
struct BackingCacheMapping {
uint64_t vmAddress;
uint64_t size;
uint64_t fileOffset;
};
struct BackingCache {
std::string path;
BNBackingCacheType cacheType;
std::vector<BackingCacheMapping> mappings;
};
struct DSCImageMemoryMapping {
std::string filePath;
std::string name;
uint64_t vmAddress;
uint64_t size;
bool loaded;
uint64_t rawViewOffset;
};
struct DSCImage {
std::string name;
uint64_t headerAddress;
std::vector<DSCImageMemoryMapping> mappings;
};
struct DSCSymbol {
uint64_t address;
BinaryNinja::StringRef name;
std::string image;
};
using namespace BinaryNinja;
struct SharedCacheMachOHeader : public SharedCacheCore::MetadataSerializable<SharedCacheMachOHeader> {
uint64_t textBase = 0;
uint64_t loadCommandOffset = 0;
mach_header_64 ident;
std::string identifierPrefix;
std::string installName;
std::vector<std::pair<uint64_t, bool>> entryPoints;
std::vector<uint64_t> m_entryPoints; // list of entrypoints
symtab_command symtab;
dysymtab_command dysymtab;
dyld_info_command dyldInfo;
routines_command_64 routines64;
function_starts_command functionStarts;
std::vector<section_64> moduleInitSections;
linkedit_data_command exportTrie;
linkedit_data_command chainedFixups {};
uint64_t relocationBase;
// Section and program headers, internally use 64-bit form as it is a superset of 32-bit
std::vector<segment_command_64> segments; // only three types of sections __TEXT, __DATA, __IMPORT
segment_command_64 linkeditSegment;
std::vector<section_64> sections;
std::vector<std::string> sectionNames;
std::vector<section_64> symbolStubSections;
std::vector<section_64> symbolPointerSections;
std::vector<std::string> dylibs;
build_version_command buildVersion;
std::vector<build_tool_version> buildToolVersions;
std::string exportTriePath;
bool linkeditPresent = false;
bool dysymPresent = false;
bool dyldInfoPresent = false;
bool exportTriePresent = false;
bool chainedFixupsPresent = false;
bool routinesPresent = false;
bool functionStartsPresent = false;
bool relocatable = false;
void Store(SharedCacheCore::SerializationContext& context) const {
MSS(textBase);
MSS(loadCommandOffset);
MSS_SUBCLASS(ident);
MSS(identifierPrefix);
MSS(installName);
MSS(entryPoints);
MSS(m_entryPoints);
MSS_SUBCLASS(symtab);
MSS_SUBCLASS(dysymtab);
MSS_SUBCLASS(dyldInfo);
MSS_SUBCLASS(routines64);
MSS_SUBCLASS(functionStarts);
MSS_SUBCLASS(moduleInitSections);
MSS_SUBCLASS(exportTrie);
MSS_SUBCLASS(chainedFixups);
MSS(relocationBase);
MSS_SUBCLASS(segments);
MSS_SUBCLASS(linkeditSegment);
MSS_SUBCLASS(sections);
MSS(sectionNames);
MSS_SUBCLASS(symbolStubSections);
MSS_SUBCLASS(symbolPointerSections);
MSS(dylibs);
MSS_SUBCLASS(buildVersion);
MSS_SUBCLASS(buildToolVersions);
MSS(exportTriePath);
MSS(linkeditPresent);
MSS(dysymPresent);
MSS(dyldInfoPresent);
MSS(exportTriePresent);
MSS(chainedFixupsPresent);
MSS(routinesPresent);
MSS(functionStartsPresent);
MSS(relocatable);
}
static SharedCacheMachOHeader Load(SharedCacheCore::DeserializationContext& context) {
SharedCacheMachOHeader header;
header.MSL(textBase);
header.MSL(loadCommandOffset);
header.MSL(ident);
header.MSL(identifierPrefix);
header.MSL(installName);
header.MSL(entryPoints);
header.MSL(m_entryPoints);
header.MSL(symtab);
header.MSL(dysymtab);
header.MSL(dyldInfo);
header.MSL(routines64);
header.MSL(functionStarts);
header.MSL(moduleInitSections);
header.MSL(exportTrie);
header.MSL(chainedFixups);
header.MSL(relocationBase);
header.MSL(segments);
header.MSL(linkeditSegment);
header.MSL(sections);
header.MSL(sectionNames);
header.MSL(symbolStubSections);
header.MSL(symbolPointerSections);
header.MSL(dylibs);
header.MSL(buildVersion);
header.MSL(buildToolVersions);
header.MSL(exportTriePath);
header.MSL(linkeditPresent);
header.MSL(dysymPresent);
header.MSL(dyldInfoPresent);
header.MSL(exportTriePresent);
header.MSL(chainedFixupsPresent);
header.MSL(routinesPresent);
header.MSL(functionStartsPresent);
header.MSL(relocatable);
return header;
}
};
class SharedCache : public SCCoreRefCountObject<BNSharedCache, BNNewSharedCacheReference, BNFreeSharedCacheReference> {
public:
SharedCache(Ref<BinaryView> view);
BNDSCViewState GetState();
static BNDSCViewLoadProgress GetLoadProgress(Ref<BinaryView> view);
static uint64_t FastGetBackingCacheCount(Ref<BinaryView> view);
bool LoadImageWithInstallName(std::string installName, bool skipObjC = false);
bool LoadSectionAtAddress(uint64_t addr);
bool LoadImageContainingAddress(uint64_t addr, bool skipObjC = false);
std::vector<std::string> GetAvailableImages();
void ProcessObjCSectionsForImageWithInstallName(std::string installName);
void ProcessAllObjCSections();
std::vector<DSCSymbol> LoadAllSymbolsAndWait();
std::string GetNameForAddress(uint64_t address);
std::string GetImageNameForAddress(uint64_t address);
std::vector<BackingCache> GetBackingCaches();
std::vector<DSCImage> GetImages();
std::optional<SharedCacheMachOHeader> GetMachOHeaderForImage(std::string name);
std::optional<SharedCacheMachOHeader> GetMachOHeaderForAddress(uint64_t address);
std::vector<DSCMemoryRegion> GetLoadedMemoryRegions();
void FindSymbolAtAddrAndApplyToAddr(uint64_t symbolLocation, uint64_t targetLocation, bool triggerReanalysis = true) const;
};
}
|