2 * Copyright (c) 2017 Apple Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
24 #ifndef MachOAnalyzer_h
25 #define MachOAnalyzer_h
28 #include "MachOLoaded.h"
30 #include "ClosureFileSystem.h"
35 // Extra functionality on loaded mach-o files only used during closure building
36 struct VIS_HIDDEN MachOAnalyzer
: public MachOLoaded
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
;
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
);
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;
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;
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;
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;
113 void validateDyldCacheDylib(Diagnostics
& diag
, const char* path
) const;
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;
120 const MachOAnalyzer
* remapIfZeroFill(Diagnostics
& diag
, const closure::FileSystem
& fileSystem
, closure::LoadedFileInfo
& info
) const;
123 uint32_t selRefCount
;
124 uint32_t classDefCount
;
125 uint32_t protocolDefCount
;
127 ObjCInfo
getObjCInfo() const;
129 // This optionally caches a list of sections for lookup
130 struct SectionCache
{
132 SectionInfo buffer
[2];
135 SectionCache(const MachOAnalyzer
* ma
) : ma(ma
) { }
137 bool findSectionForVMAddr(uint64_t vmAddr
, bool (^sectionHandler
)(const SectionInfo
& sectionInfo
));
139 const MachOAnalyzer
* ma
= nullptr;
140 dyld3::OverflowSafeArray
<SectionInfo
> sectionInfos
= { buffer
, sizeof(buffer
) / sizeof(buffer
[0]) };
143 // Caches data useful for converting from raw data to VM addresses
144 struct VMAddrConverter
{
145 uint64_t preferredLoadAddress
= 0;
147 uint16_t chainedPointerFormat
= 0;
148 bool contentRebased
= false;
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;
159 // This field is only present if this is a Swift object, ie, has the Swift
161 uint32_t swiftClassFlags
= 0;
163 // These are taken from the low bits of the dataVMAddr value
164 bool isSwiftLegacy
= false;
165 bool isSwiftStable
= false;
167 // Cache the data to convert vmAddr's
168 MachOAnalyzer::VMAddrConverter vmAddrConverter
;
170 // These are from the class_ro_t which data points to
171 enum class ReadOnlyDataField
{
176 uint64_t getReadOnlyDataField(ReadOnlyDataField field
, uint32_t pointerSize
) const;
177 uint64_t nameVMAddr(uint32_t pointerSize
) const {
178 return getReadOnlyDataField(ReadOnlyDataField::name
, pointerSize
);
180 uint64_t baseMethodsVMAddr(uint32_t pointerSize
) const {
181 return getReadOnlyDataField(ReadOnlyDataField::baseMethods
, pointerSize
);
184 // These are embedded in the Mach-O itself by the compiler
186 FAST_IS_SWIFT_LEGACY
= 0x1,
187 FAST_IS_SWIFT_STABLE
= 0x2
190 // These are embedded by the Swift compiler in the swiftClassFlags field
191 enum SwiftClassFlags
{
192 isSwiftPreStableABI
= 0x1
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;
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
;
208 struct ObjCImageInfo
{
212 // FIXME: Put this somewhere objc can see it.
214 dyldPreoptimized
= 1 << 7
219 uint64_t nameVMAddr
; // & SEL
220 uint64_t typesVMAddr
; // & const char *
221 uint64_t impVMAddr
; // & IMP
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
;
228 struct ObjCCategory
{
231 uint64_t instanceMethodsVMAddr
;
232 uint64_t classMethodsVMAddr
;
233 uint64_t protocolsVMAddr
;
234 uint64_t instancePropertiesVMAddr
;
237 struct ObjCProtocol
{
240 //uint64_t protocolsVMAddr;
241 uint64_t instanceMethodsVMAddr
;
242 uint64_t classMethodsVMAddr
;
243 uint64_t optionalInstanceMethodsVMAddr
;
244 uint64_t optionalClassMethodsVMAddr
;
245 //uint64_t instancePropertiesVMAddr;
248 // Fields below this point are not always present on disk.
249 //uint64_t extendedMethodTypesVMAddr;
250 //uint64_t demangledNameVMAddr;
251 //uint64_t classPropertiesVMAddr;
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
;
258 enum class PrintableStringResult
{
265 const char* getPrintableString(uint64_t stringVMAddr
, PrintableStringResult
& result
,
266 SectionCache
* sectionCache
= nullptr,
267 bool (^sectionHandler
)(const SectionInfo
& sectionInfo
) = nullptr) const;
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;
274 void forEachObjCCategory(Diagnostics
& diag
, bool contentRebased
,
275 void (^handler
)(Diagnostics
& diag
, uint64_t categoryVMAddr
,
276 const dyld3::MachOAnalyzer::ObjCCategory
& objcCategory
)) const;
278 void forEachObjCProtocol(Diagnostics
& diag
, bool contentRebased
,
279 void (^handler
)(Diagnostics
& diag
, uint64_t protocolVMAddr
,
280 const dyld3::MachOAnalyzer::ObjCProtocol
& objCProtocol
)) const;
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;
287 void forEachObjCSelectorReference(Diagnostics
& diag
, bool contentRebased
,
288 void (^handler
)(uint64_t selRefVMAddr
, uint64_t selRefTargetVMAddr
)) const;
290 void forEachObjCMethodName(void (^handler
)(const char* methodName
)) const;
292 bool hasObjCMessageReferences() const;
294 const ObjCImageInfo
* objcImageInfo() const;
296 void forEachWeakDef(Diagnostics
& diag
, void (^handler
)(const char* symbolName
, uintptr_t imageOffset
, bool isFromExportTrie
)) const;
304 uint64_t writable
: 1,
306 textRelocsAllowed
: 1, // segment supports text relocs (i386 only)
310 enum class Malformed
{ linkeditOrder
, linkeditAlignment
, linkeditPermissions
, dyldInfoAndlocalRelocs
, segmentOrder
, textPermissions
, executableData
, codeSigAlignment
};
311 bool enforceFormat(Malformed
) const;
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;
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;
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;
353 #endif /* MachOAnalyzer_h */