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@
29 #include "MachOFile.h"
37 // A mach-o mapped into memory with zero-fill expansion
38 // Can be used in dyld at runtime or during closure building
39 struct VIS_HIDDEN MachOLoaded
: public MachOFile
41 typedef const MachOLoaded
* (^DependentToMachOLoaded
)(const MachOLoaded
* image
, uint32_t depIndex
);
44 bool hasExportedSymbol(const char* symbolName
, DependentToMachOLoaded finder
, void** result
,
45 bool* resultPointsToInstructions
) const;
47 // for DYLD_PRINT_SEGMENTS
48 const char* segmentName(uint32_t segIndex
) const;
50 // used to see if main executable overlaps shared region
51 bool intersectsRange(uintptr_t start
, uintptr_t length
) const;
53 // for _dyld_get_image_slide()
54 intptr_t getSlide() const;
56 // quick check if image has been incorporated into the dyld cache
57 bool inDyldCache() const { return (this->flags
& 0x80000000); }
60 bool findClosestSymbol(uint64_t unSlidAddr
, const char** symbolName
, uint64_t* symbolUnslidAddr
) const;
62 // for _dyld_find_unwind_sections()
63 const void* findSectionContent(const char* segName
, const char* sectName
, uint64_t& size
) const;
65 // used at runtime to validate loaded image matches closure
66 bool cdHashOfCodeSignature(const void* codeSigStart
, size_t codeSignLen
, uint8_t cdHash
[20]) const;
68 // used by DyldSharedCache to find closure
69 static const uint8_t* trieWalk(Diagnostics
& diag
, const uint8_t* start
, const uint8_t* end
, const char* symbol
);
71 // used by cache builder during error handling in chain bind processing
72 const char* dependentDylibLoadPath(uint32_t depIndex
) const;
74 // used by closure builder to find the offset and size of the trie.
75 bool hasExportTrie(uint32_t& runtimeOffset
, uint32_t& size
) const;
78 // For use with new rebase/bind scheme were each fixup location on disk contains info on what
79 // fix up it needs plus the offset to the next fixup.
80 union ChainedFixupPointerOnDisk
88 uint64_t signExtendedTarget() const;
92 uint64_t ordinal
: 16,
98 uint64_t signExtendedAddend() const;
102 uint64_t target
: 32,
109 const char* keyName() const;
113 uint64_t ordinal
: 16,
121 const char* keyName() const;
125 AuthRebase authRebase
;
127 PlainRebase plainRebase
;
130 static const char* keyName(uint8_t keyBits
);
131 static uint64_t signExtend51(uint64_t);
132 uint64_t signPointer(void* loc
, uint64_t target
) const;
139 enum class Kind
{ headerOffset
, absolute
, resolverOffset
};
143 const MachOLoaded
* foundInDylib
;
145 uint32_t resolverFuncOffset
;
146 const char* foundSymbolName
;
151 uintptr_t textUnslidVMAddr
;
152 uintptr_t linkeditUnslidVMAddr
;
153 uint32_t linkeditFileOffset
;
154 uint32_t linkeditFileSize
;
155 uint32_t linkeditSegIndex
;
160 const dyld_info_command
* dyldInfo
;
161 const symtab_command
* symTab
;
162 const dysymtab_command
* dynSymTab
;
163 const linkedit_data_command
* splitSegInfo
;
164 const linkedit_data_command
* functionStarts
;
165 const linkedit_data_command
* dataInCode
;
166 const linkedit_data_command
* codeSig
;
170 bool findExportedSymbol(Diagnostics
& diag
, const char* symbolName
, FoundSymbol
& foundInfo
, DependentToMachOLoaded finder
) const;
171 void getLinkEditPointers(Diagnostics
& diag
, LinkEditInfo
&) const;
172 void getLinkEditLoadCommands(Diagnostics
& diag
, LinkEditInfo
& result
) const;
173 void getLayoutInfo(LayoutInfo
&) const;
174 const uint8_t* getLinkEditContent(const LayoutInfo
& info
, uint32_t fileOffset
) const;
175 void forEachGlobalSymbol(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;
176 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;
177 uint32_t dependentDylibCount() const;
178 bool findClosestFunctionStart(uint64_t address
, uint64_t* functionStartAddress
) const;
180 const void* findCodeDirectoryBlob(const void* codeSigStart
, size_t codeSignLen
) const;
186 #endif /* MachOLoaded_h */