dyld-733.6.tar.gz
[apple/dyld.git] / dyld3 / MachOAnalyzer.h
1 /*
2 * Copyright (c) 2017 Apple Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24 #ifndef MachOAnalyzer_h
25 #define MachOAnalyzer_h
26
27
28 #include "MachOLoaded.h"
29 #include "Array.h"
30 #include "ClosureFileSystem.h"
31
32
33 namespace dyld3 {
34
35 // Extra functionality on loaded mach-o files only used during closure building
36 struct VIS_HIDDEN MachOAnalyzer : public MachOLoaded
37 {
38 // protected members of subclass promoted to public here
39 using MachOLoaded::SegmentInfo;
40 using MachOLoaded::SectionInfo;
41 using MachOLoaded::forEachSegment;
42 using MachOLoaded::forEachSection;
43 using MachOLoaded::forEachDependentDylib;
44 using MachOLoaded::getDylibInstallName;
45 using MachOLoaded::FoundSymbol;
46 using MachOLoaded::findExportedSymbol;
47
48 static closure::LoadedFileInfo load(Diagnostics& diag, const closure::FileSystem& fileSystem,
49 const char* logicalPath, const GradedArchs& archs, Platform platform, char realerPath[MAXPATHLEN]);
50 static const MachOAnalyzer* validMainExecutable(Diagnostics& diag, const mach_header* mh, const char* path, uint64_t sliceLength,
51 const GradedArchs& archs, Platform platform);
52
53 typedef void (^ExportsCallback)(const char* symbolName, uint64_t imageOffset, uint64_t flags,
54 uint64_t other, const char* importName, bool& stop);
55 bool validMachOForArchAndPlatform(Diagnostics& diag, size_t mappedSize, const char* path, const GradedArchs& archs, Platform platform) const;
56 uint64_t mappedSize() const;
57 bool hasObjC() const;
58 bool hasPlusLoadMethod(Diagnostics& diag) const;
59 uint64_t preferredLoadAddress() const;
60 void forEachLocalSymbol(Diagnostics& diag, void (^callback)(const char* symbolName, uint64_t n_value, uint8_t n_type, uint8_t n_sect, uint16_t n_desc, bool& stop)) const;
61 void forEachRPath(void (^callback)(const char* rPath, bool& stop)) const;
62
63 bool isEncrypted() const;
64 void forEachCDHash(void (^handler)(const uint8_t cdHash[20])) const;
65 bool hasCodeSignature(uint32_t& fileOffset, uint32_t& size) const;
66 bool usesLibraryValidation() const;
67 bool isRestricted() const;
68 bool getEntry(uint32_t& offset, bool& usesCRT) const;
69 bool isSlideable() const;
70 bool hasInitializer(Diagnostics& diag, bool contentRebased, const void* dyldCache=nullptr) const;
71 void forEachInitializerPointerSection(Diagnostics& diag, void (^callback)(uint32_t sectionOffset, uint32_t sectionSize, const uint8_t* content, bool& stop)) const;
72 void forEachInitializer(Diagnostics& diag, bool contentRebased, void (^callback)(uint32_t offset), const void* dyldCache=nullptr) const;
73 bool hasTerminators(Diagnostics& diag, bool contentRebased) const;
74 void forEachTerminator(Diagnostics& diag, bool contentRebased, void (^callback)(uint32_t offset)) const;
75 void forEachDOFSection(Diagnostics& diag, void (^callback)(uint32_t offset)) const;
76 uint32_t segmentCount() const;
77 void forEachExportedSymbol(Diagnostics& diag, ExportsCallback callback) const;
78 void forEachWeakDef(Diagnostics& diag, void (^callback)(bool strongDef, uint32_t dataSegIndex, uint64_t dataSegOffset,
79 uint64_t addend, const char* symbolName, bool& stop)) const;
80 void forEachIndirectPointer(Diagnostics& diag, void (^handler)(uint64_t pointerAddress, bool bind, int bindLibOrdinal,
81 const char* bindSymbolName, bool bindWeakImport, bool bindLazy, bool selfModifyingStub, bool& stop)) const;
82 void forEachInterposingSection(Diagnostics& diag, void (^handler)(uint64_t vmOffset, uint64_t vmSize, bool& stop)) const;
83 const void* content(uint64_t vmOffset);
84 void forEachLocalReloc(void (^handler)(uint64_t runtimeOffset, bool& stop)) const;
85 void forEachExternalReloc(void (^handler)(uint64_t runtimeOffset, int libOrdinal, const char* symbolName, bool& stop)) const;
86
87 const void* getRebaseOpcodes(uint32_t& size) const;
88 const void* getBindOpcodes(uint32_t& size) const;
89 const void* getLazyBindOpcodes(uint32_t& size) const;
90 const void* getSplitSeg(uint32_t& size) const;
91 uint64_t segAndOffsetToRuntimeOffset(uint8_t segIndex, uint64_t segOffset) const;
92 bool hasLazyPointers(uint32_t& runtimeOffset, uint32_t& size) const;
93 void forEachRebase(Diagnostics& diag, bool ignoreLazyPointer, void (^callback)(uint64_t runtimeOffset, bool& stop)) const;
94 void forEachTextRebase(Diagnostics& diag, void (^callback)(uint64_t runtimeOffset, bool& stop)) const;
95 void forEachBind(Diagnostics& diag, void (^callback)(uint64_t runtimeOffset, int libOrdinal, const char* symbolName,
96 bool weakImport, bool lazyBind, uint64_t addend, bool& stop),
97 void (^strongHandler)(const char* symbolName),
98 void (^missingLazyBindHandler)()) const;
99 void forEachChainedFixupTarget(Diagnostics& diag, void (^callback)(int libOrdinal, const char* symbolName, uint64_t addend, bool weakImport, bool& stop)) const;
100 bool canHavePrecomputedDlopenClosure(const char* path, void (^failureReason)(const char*)) const;
101 void forEachRebase(Diagnostics& diag, void (^handler)(const char* opcodeName, const LinkEditInfo& leInfo, const SegmentInfo segments[],
102 bool segIndexSet, uint32_t pointerSize, uint8_t segmentIndex, uint64_t segmentOffset, uint8_t type, bool& stop)) const;
103 void forEachBind(Diagnostics& diag, void (^handler)(const char* opcodeName, const LinkEditInfo& leInfo, const SegmentInfo segments[],
104 bool segIndexSet, bool libraryOrdinalSet, uint32_t dylibCount, int libOrdinal,
105 uint32_t pointerSize, uint8_t segmentIndex, uint64_t segmentOffset,
106 uint8_t type, const char* symbolName, bool weakImport, bool lazyBind, uint64_t addend, bool& stop),
107 void (^strongHandler)(const char* symbolName),
108 void (^missingLazyBindHandler)()) const;
109 bool canBePlacedInDyldCache(const char* path, void (^failureReason)(const char*)) const;
110 uint32_t loadCommandsFreeSpace() const;
111
112 #if DEBUG
113 void validateDyldCacheDylib(Diagnostics& diag, const char* path) const;
114 #endif
115 void withChainStarts(Diagnostics& diag, uint64_t startsStructOffsetHint, void (^callback)(const dyld_chained_starts_in_image*)) const;
116 uint64_t chainStartsOffset() const;
117 uint16_t chainedPointerFormat() const;
118 bool hasUnalignedPointerFixups() const;
119
120 const MachOAnalyzer* remapIfZeroFill(Diagnostics& diag, const closure::FileSystem& fileSystem, closure::LoadedFileInfo& info) const;
121
122 struct ObjCInfo {
123 uint32_t selRefCount;
124 uint32_t classDefCount;
125 uint32_t protocolDefCount;
126 };
127 ObjCInfo getObjCInfo() const;
128
129 // This optionally caches a list of sections for lookup
130 struct SectionCache {
131 private:
132 SectionInfo buffer[2];
133
134 public:
135 SectionCache(const MachOAnalyzer* ma) : ma(ma) { }
136
137 bool findSectionForVMAddr(uint64_t vmAddr, bool (^sectionHandler)(const SectionInfo& sectionInfo));
138
139 const MachOAnalyzer* ma = nullptr;
140 dyld3::OverflowSafeArray<SectionInfo> sectionInfos = { buffer, sizeof(buffer) / sizeof(buffer[0]) };
141 };
142
143 // Caches data useful for converting from raw data to VM addresses
144 struct VMAddrConverter {
145 uint64_t preferredLoadAddress = 0;
146 intptr_t slide = 0;
147 uint16_t chainedPointerFormat = 0;
148 bool contentRebased = false;
149 };
150
151 struct ObjCClassInfo {
152 // These fields are all present on the objc_class_t struct
153 uint64_t isaVMAddr = 0;
154 uint64_t superclassVMAddr = 0;
155 //uint64_t methodCacheBuckets;
156 //uint64_t methodCacheProperties;
157 uint64_t dataVMAddr = 0;
158
159 // This field is only present if this is a Swift object, ie, has the Swift
160 // fast bits set
161 uint32_t swiftClassFlags = 0;
162
163 // These are taken from the low bits of the dataVMAddr value
164 bool isSwiftLegacy = false;
165 bool isSwiftStable = false;
166
167 // Cache the data to convert vmAddr's
168 MachOAnalyzer::VMAddrConverter vmAddrConverter;
169
170 // These are from the class_ro_t which data points to
171 enum class ReadOnlyDataField {
172 name,
173 baseMethods
174 };
175
176 uint64_t getReadOnlyDataField(ReadOnlyDataField field, uint32_t pointerSize) const;
177 uint64_t nameVMAddr(uint32_t pointerSize) const {
178 return getReadOnlyDataField(ReadOnlyDataField::name, pointerSize);
179 }
180 uint64_t baseMethodsVMAddr(uint32_t pointerSize) const {
181 return getReadOnlyDataField(ReadOnlyDataField::baseMethods, pointerSize);
182 }
183
184 // These are embedded in the Mach-O itself by the compiler
185 enum FastDataBits {
186 FAST_IS_SWIFT_LEGACY = 0x1,
187 FAST_IS_SWIFT_STABLE = 0x2
188 };
189
190 // These are embedded by the Swift compiler in the swiftClassFlags field
191 enum SwiftClassFlags {
192 isSwiftPreStableABI = 0x1
193 };
194
195 // Note this is taken from the objc runtime
196 bool isUnfixedBackwardDeployingStableSwift() const {
197 // Only classes marked as Swift legacy need apply.
198 if (!isSwiftLegacy) return false;
199
200 // Check the true legacy vs stable distinguisher.
201 // The low bit of Swift's ClassFlags is SET for true legacy
202 // and UNSET for stable pretending to be legacy.
203 bool isActuallySwiftLegacy = (swiftClassFlags & isSwiftPreStableABI) != 0;
204 return !isActuallySwiftLegacy;
205 }
206 };
207
208 struct ObjCImageInfo {
209 uint32_t version;
210 uint32_t flags;
211
212 // FIXME: Put this somewhere objc can see it.
213 enum : uint32_t {
214 dyldPreoptimized = 1 << 7
215 };
216 };
217
218 struct ObjCMethod {
219 uint64_t nameVMAddr; // & SEL
220 uint64_t typesVMAddr; // & const char *
221 uint64_t impVMAddr; // & IMP
222
223 // We also need to know where the reference to the nameVMAddr was
224 // This is so that we know how to rebind that location
225 uint64_t nameLocationVMAddr;
226 };
227
228 struct ObjCCategory {
229 uint64_t nameVMAddr;
230 uint64_t clsVMAddr;
231 uint64_t instanceMethodsVMAddr;
232 uint64_t classMethodsVMAddr;
233 uint64_t protocolsVMAddr;
234 uint64_t instancePropertiesVMAddr;
235 };
236
237 struct ObjCProtocol {
238 uint64_t isaVMAddr;
239 uint64_t nameVMAddr;
240 //uint64_t protocolsVMAddr;
241 uint64_t instanceMethodsVMAddr;
242 uint64_t classMethodsVMAddr;
243 uint64_t optionalInstanceMethodsVMAddr;
244 uint64_t optionalClassMethodsVMAddr;
245 //uint64_t instancePropertiesVMAddr;
246 //uint32_t size;
247 //uint32_t flags;
248 // Fields below this point are not always present on disk.
249 //uint64_t extendedMethodTypesVMAddr;
250 //uint64_t demangledNameVMAddr;
251 //uint64_t classPropertiesVMAddr;
252
253 // Note this isn't in a protocol, but we use it in dyld to track if the protocol
254 // is large enough to avoid a reallocation in objc.
255 bool requiresObjCReallocation;
256 };
257
258 enum class PrintableStringResult {
259 CanPrint,
260 FairPlayEncrypted,
261 ProtectedSection,
262 UnknownSection
263 };
264
265 const char* getPrintableString(uint64_t stringVMAddr, PrintableStringResult& result,
266 SectionCache* sectionCache = nullptr,
267 bool (^sectionHandler)(const SectionInfo& sectionInfo) = nullptr) const;
268
269 void forEachObjCClass(Diagnostics& diag, bool contentRebased,
270 void (^handler)(Diagnostics& diag, uint64_t classVMAddr,
271 uint64_t classSuperclassVMAddr, uint64_t classDataVMAddr,
272 const ObjCClassInfo& objcClass, bool isMetaClass)) const;
273
274 void forEachObjCCategory(Diagnostics& diag, bool contentRebased,
275 void (^handler)(Diagnostics& diag, uint64_t categoryVMAddr,
276 const dyld3::MachOAnalyzer::ObjCCategory& objcCategory)) const;
277
278 void forEachObjCProtocol(Diagnostics& diag, bool contentRebased,
279 void (^handler)(Diagnostics& diag, uint64_t protocolVMAddr,
280 const dyld3::MachOAnalyzer::ObjCProtocol& objCProtocol)) const;
281
282 // Walk a method list starting from its vmAddr.
283 // Note, classes, categories, protocols, etc, all share the same method list struture so can all use this.
284 void forEachObjCMethod(uint64_t methodListVMAddr, bool contentRebased,
285 void (^handler)(uint64_t methodVMAddr, const ObjCMethod& method)) const;
286
287 void forEachObjCSelectorReference(Diagnostics& diag, bool contentRebased,
288 void (^handler)(uint64_t selRefVMAddr, uint64_t selRefTargetVMAddr)) const;
289
290 void forEachObjCMethodName(void (^handler)(const char* methodName)) const;
291
292 bool hasObjCMessageReferences() const;
293
294 const ObjCImageInfo* objcImageInfo() const;
295
296 void forEachWeakDef(Diagnostics& diag, void (^handler)(const char* symbolName, uintptr_t imageOffset, bool isFromExportTrie)) const;
297
298 private:
299
300 struct SegmentStuff
301 {
302 uint64_t fileOffset;
303 uint64_t fileSize;
304 uint64_t writable : 1,
305 executable : 1,
306 textRelocsAllowed : 1, // segment supports text relocs (i386 only)
307 segSize : 61;
308 };
309
310 enum class Malformed { linkeditOrder, linkeditAlignment, linkeditPermissions, dyldInfoAndlocalRelocs, segmentOrder, textPermissions, executableData, codeSigAlignment };
311 bool enforceFormat(Malformed) const;
312
313 const uint8_t* getContentForVMAddr(const LayoutInfo& info, uint64_t vmAddr) const;
314 bool validLoadCommands(Diagnostics& diag, const char* path, size_t fileLen) const;
315 bool validEmbeddedPaths(Diagnostics& diag, Platform platform, const char* path) const;
316 bool validSegments(Diagnostics& diag, const char* path, size_t fileLen) const;
317 bool validLinkedit(Diagnostics& diag, const char* path) const;
318 bool validLinkeditLayout(Diagnostics& diag, const char* path) const;
319 bool validRebaseInfo(Diagnostics& diag, const char* path) const;
320 bool validBindInfo(Diagnostics& diag, const char* path) const;
321 bool validMain(Diagnostics& diag, const char* path) const;
322 bool validChainedFixupsInfo(Diagnostics& diag, const char* path) const;
323
324 bool invalidRebaseState(Diagnostics& diag, const char* opcodeName, const char* path, const LinkEditInfo& leInfo, const SegmentInfo segments[],
325 bool segIndexSet, uint32_t pointerSize, uint8_t segmentIndex, uint64_t segmentOffset, uint8_t type) const;
326 bool invalidBindState(Diagnostics& diag, const char* opcodeName, const char* path, const LinkEditInfo& leInfo, const SegmentInfo segments[],
327 bool segIndexSet, bool libraryOrdinalSet, uint32_t dylibCount, int libOrdinal, uint32_t pointerSize,
328 uint8_t segmentIndex, uint64_t segmentOffset, uint8_t type, const char* symbolName) const;
329 bool doLocalReloc(Diagnostics& diag, uint32_t r_address, bool& stop, void (^callback)(uint32_t dataSegIndex, uint64_t dataSegOffset, uint8_t type, bool& stop)) const;
330 uint8_t relocPointerType() const;
331 int libOrdinalFromDesc(uint16_t n_desc) const;
332 bool doExternalReloc(Diagnostics& diag, uint32_t r_address, uint32_t r_symbolnum, LinkEditInfo& leInfo, bool& stop,
333 void (^callback)(uint32_t dataSegIndex, uint64_t dataSegOffset, uint8_t type, int libOrdinal,
334 uint64_t addend, const char* symbolName, bool weakImport, bool lazy, bool& stop)) const;
335
336 void getAllSegmentsInfos(Diagnostics& diag, SegmentInfo segments[]) const;
337 bool segmentHasTextRelocs(uint32_t segIndex) const;
338 uint64_t relocBaseAddress(const SegmentInfo segmentsInfos[], uint32_t segCount) const;
339 bool segIndexAndOffsetForAddress(uint64_t addr, const SegmentInfo segmentsInfos[], uint32_t segCount, uint32_t& segIndex, uint64_t& segOffset) const;
340 void parseOrgArm64eChainedFixups(Diagnostics& diag, void (^targetCount)(uint32_t totalTargets, bool& stop),
341 void (^addTarget)(const LinkEditInfo& leInfo, const SegmentInfo segments[], bool libraryOrdinalSet, uint32_t dylibCount, int libOrdinal, uint8_t type, const char* symbolName, uint64_t addend, bool weakImport, bool& stop),
342 void (^addChainStart)(const LinkEditInfo& leInfo, const SegmentInfo segments[], uint8_t segmentIndex, bool segIndexSet, uint64_t segmentOffset, uint16_t format, bool& stop)) const;
343 bool contentIsRegularStub(const uint8_t* helperContent) const;
344 uint64_t entryAddrFromThreadCmd(const thread_command* cmd) const;
345 void recurseTrie(Diagnostics& diag, const uint8_t* const start, const uint8_t* p, const uint8_t* const end,
346 OverflowSafeArray<char>& cummulativeString, int curStrOffset, bool& stop, MachOAnalyzer::ExportsCallback callback) const;
347 void analyzeSegmentsLayout(uint64_t& vmSpace, bool& hasZeroFill) const;
348
349 };
350
351 } // namespace dyld3
352
353 #endif /* MachOAnalyzer_h */