dyld-625.13.tar.gz
[apple/dyld.git] / dyld3 / MachOLoaded.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 MachOLoaded_h
25 #define MachOLoaded_h
26
27 #include <stdint.h>
28
29 #include "MachOFile.h"
30
31
32 class CacheBuilder;
33
34 namespace dyld3 {
35
36
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
40 {
41 typedef const MachOLoaded* (^DependentToMachOLoaded)(const MachOLoaded* image, uint32_t depIndex);
42
43 // for dlsym()
44 bool hasExportedSymbol(const char* symbolName, DependentToMachOLoaded finder, void** result,
45 bool* resultPointsToInstructions) const;
46
47 // for DYLD_PRINT_SEGMENTS
48 const char* segmentName(uint32_t segIndex) const;
49
50 // used to see if main executable overlaps shared region
51 bool intersectsRange(uintptr_t start, uintptr_t length) const;
52
53 // for _dyld_get_image_slide()
54 intptr_t getSlide() const;
55
56 // quick check if image has been incorporated into the dyld cache
57 bool inDyldCache() const { return (this->flags & 0x80000000); }
58
59 // for dladdr()
60 bool findClosestSymbol(uint64_t unSlidAddr, const char** symbolName, uint64_t* symbolUnslidAddr) const;
61
62 // for _dyld_find_unwind_sections()
63 const void* findSectionContent(const char* segName, const char* sectName, uint64_t& size) const;
64
65 // used at runtime to validate loaded image matches closure
66 bool cdHashOfCodeSignature(const void* codeSigStart, size_t codeSignLen, uint8_t cdHash[20]) const;
67
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);
70
71 // used by cache builder during error handling in chain bind processing
72 const char* dependentDylibLoadPath(uint32_t depIndex) const;
73
74 // used by closure builder to find the offset and size of the trie.
75 bool hasExportTrie(uint32_t& runtimeOffset, uint32_t& size) const;
76
77
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
81 {
82 struct PlainRebase
83 {
84 uint64_t target : 51,
85 next : 11,
86 bind : 1, // 0
87 auth : 1; // 0
88 uint64_t signExtendedTarget() const;
89 };
90 struct PlainBind
91 {
92 uint64_t ordinal : 16,
93 zero : 16,
94 addend : 19,
95 next : 11,
96 bind : 1, // 1
97 auth : 1; // 0
98 uint64_t signExtendedAddend() const;
99 };
100 struct AuthRebase
101 {
102 uint64_t target : 32,
103 diversity : 16,
104 addrDiv : 1,
105 key : 2,
106 next : 11,
107 bind : 1, // 0
108 auth : 1; // 1
109 const char* keyName() const;
110 };
111 struct AuthBind
112 {
113 uint64_t ordinal : 16,
114 zero : 16,
115 diversity : 16,
116 addrDiv : 1,
117 key : 2,
118 next : 11,
119 bind : 1, // 1
120 auth : 1; // 1
121 const char* keyName() const;
122 };
123
124 uint64_t raw;
125 AuthRebase authRebase;
126 AuthBind authBind;
127 PlainRebase plainRebase;
128 PlainBind plainBind;
129
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;
133 };
134
135 protected:
136 friend CacheBuilder;
137
138 struct FoundSymbol {
139 enum class Kind { headerOffset, absolute, resolverOffset };
140 Kind kind;
141 bool isThreadLocal;
142 bool isWeakDef;
143 const MachOLoaded* foundInDylib;
144 uint64_t value;
145 uint32_t resolverFuncOffset;
146 const char* foundSymbolName;
147 };
148
149 struct LayoutInfo {
150 uintptr_t slide;
151 uintptr_t textUnslidVMAddr;
152 uintptr_t linkeditUnslidVMAddr;
153 uint32_t linkeditFileOffset;
154 uint32_t linkeditFileSize;
155 uint32_t linkeditSegIndex;
156 };
157
158 struct LinkEditInfo
159 {
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;
167 LayoutInfo layout;
168 };
169
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;
179
180 const void* findCodeDirectoryBlob(const void* codeSigStart, size_t codeSignLen) const;
181
182 };
183
184 } // namespace dyld3
185
186 #endif /* MachOLoaded_h */