dyld-851.27.tar.gz
[apple/dyld.git] / dyld3 / AllImages.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
25 #ifndef __ALL_IMAGES_H__
26 #define __ALL_IMAGES_H__
27
28 #include <mach-o/loader.h>
29 #include <pthread.h>
30 #include <os/lock_private.h>
31 #include <mach-o/dyld_priv.h>
32
33 #include <sys/types.h>
34
35 #include "Closure.h"
36 #include "Loading.h"
37 #include "MachOLoaded.h"
38 #include "DyldSharedCache.h"
39 #include "PointerAuth.h"
40
41
42 #if TARGET_OS_OSX
43 // only in macOS and deprecated
44 struct VIS_HIDDEN OFIInfo
45 {
46 const char* path; // = nullptr;
47 const void* memSource; // = nullptr;
48 size_t memLength; // = 0;
49 const dyld3::MachOLoaded* loadAddress; // = nullptr;
50 uint64_t imageNum; // = 0;
51 };
52 #endif
53
54 namespace dyld3 {
55
56 class VIS_HIDDEN AllImages
57 {
58 public:
59 typedef void (*NotifyFunc)(const mach_header* mh, intptr_t slide);
60 typedef void (*LoadNotifyFunc)(const mach_header* mh, const char* path, bool unloadable);
61 typedef void (*BulkLoadNotifier)(unsigned count, const mach_header* mhs[], const char* paths[]);
62 typedef void (*MainFunc)(void);
63
64 void init(const closure::LaunchClosure* closure, const DyldSharedCache* dyldCacheLoadAddress, const char* dyldCachePath,
65 const Array<LoadedImage>& initialImages);
66 void setRestrictions(bool allowAtPaths, bool allowEnvPaths);
67 void setHasCacheOverrides(bool someCacheImageOverriden);
68 bool hasCacheOverrides() const;
69 void setMainPath(const char* path);
70 void setLaunchMode(uint32_t flags);
71 void applyInitialImages();
72
73 void addImages(const Array<LoadedImage>& newImages);
74 void removeImages(const Array<LoadedImage>& unloadImages);
75 void runImageNotifiers(const Array<LoadedImage>& newImages);
76 void runImageCallbacks(const Array<LoadedImage>& newImages);
77 void applyInterposingToDyldCache(const closure::Closure* closure, mach_port_t mach_task_self);
78 void runStartupInitialzers();
79 void runInitialzersBottomUp(const closure::Image* topImage);
80 void runLibSystemInitializer(LoadedImage& libSystem);
81
82 uint32_t count() const;
83
84 void forEachImage(void (^handler)(const LoadedImage& loadedImage, bool& stop)) const;
85 const MachOLoaded* findDependent(const MachOLoaded* mh, uint32_t depIndex);
86 void visitDependentsTopDown(const LoadedImage& start, void (^handler)(const LoadedImage& aLoadedImage, bool& stop)) const;
87 void infoForImageMappedAt(const void* addr, void (^handler)(const LoadedImage& foundImage, uint8_t permissions)) const;
88 bool infoForImageMappedAt(const void* addr, const MachOLoaded** ml, uint64_t* textSize, const char** path) const;
89 void infoForNonCachedImageMappedAt(const void* addr, void (^handler)(const LoadedImage& foundImage, uint8_t permissions)) const;
90 void infoForImageWithLoadAddress(const MachOLoaded*, void (^handler)(const LoadedImage& foundImage)) const;
91 const char* pathForImageMappedAt(const void* addr) const;
92 const char* imagePathByIndex(uint32_t index) const;
93 const mach_header* imageLoadAddressByIndex(uint32_t index) const;
94 bool immutableMemory(const void* addr, size_t length) const;
95 void* interposeValue(void* value) const;
96
97 bool hasInsertedOrInterposingLibraries() const;
98 bool isRestricted() const;
99 const MachOLoaded* mainExecutable() const;
100 const closure::Image* mainExecutableImage() const;
101 const void* cacheLoadAddress() const { return _dyldCacheAddress; }
102 const char* dyldCachePath() const { return _dyldCachePath; }
103 bool dyldCacheHasPath(const char* path) const;
104 const char* imagePath(const closure::Image*) const;
105 dyld_platform_t platform() const;
106 const GradedArchs& archs() const;
107 uint32_t launchMode() const { return _launchMode; }
108
109 const Array<const closure::ImageArray*>& imagesArrays();
110
111 void incRefCount(const mach_header* loadAddress);
112 void decRefCount(const mach_header* loadAddress);
113
114 void addLoadNotifier(NotifyFunc);
115 void addUnloadNotifier(NotifyFunc);
116 void setObjCNotifiers(_dyld_objc_notify_mapped, _dyld_objc_notify_init, _dyld_objc_notify_unmapped);
117 void notifyObjCUnmap(const char* path, const struct mach_header* mh);
118 void addLoadNotifier(LoadNotifyFunc);
119 void addBulkLoadNotifier(BulkLoadNotifier);
120
121
122 void setOldAllImageInfo(dyld_all_image_infos* old) { _oldAllImageInfos = old; }
123 dyld_all_image_infos* oldAllImageInfo() const { return _oldAllImageInfos;}
124 void notifyMonitorMain();
125 void notifyMonitorLoads(const Array<LoadedImage>& newImages);
126 void notifyMonitorUnloads(const Array<LoadedImage>& unloadingImages);
127
128 #if TARGET_OS_OSX
129 NSObjectFileImage addNSObjectFileImage(const OFIInfo&);
130 void removeNSObjectFileImage(NSObjectFileImage);
131 bool forNSObjectFileImage(NSObjectFileImage imageHandle,
132 void (^handler)(OFIInfo& image));
133 #endif
134
135 const char* getObjCSelector(const char* selName) const;
136 void forEachObjCClass(const char* className,
137 void (^callback)(void* classPtr, bool isLoaded, bool* stop)) const;
138 void forEachObjCProtocol(const char* protocolName,
139 void (^callback)(void* protocolPtr, bool isLoaded, bool* stop)) const;
140
141 MainFunc getDriverkitMain();
142 void setDriverkitMain(MainFunc mainFunc);
143
144 const MachOLoaded* dlopen(Diagnostics& diag, const char* path, bool rtldNoLoad, bool rtldLocal, bool rtldNoDelete,
145 bool forceBindLazies, bool fromOFI, const void* callerAddress,
146 bool canUsePrebuiltSharedCacheClosure = true);
147
148 struct ProgramVars
149 {
150 const void* mh;
151 int* NXArgcPtr;
152 const char*** NXArgvPtr;
153 const char*** environPtr;
154 const char** __prognamePtr;
155 };
156 void setProgramVars(ProgramVars* vars, bool keysOff, bool platformBinariesOnly);
157
158 // Note these are to be used exclusively by forking
159 void takeLockBeforeFork();
160 void releaseLockInForkParent();
161 void resetLockInForkChild();
162
163 private:
164 friend class Reaper;
165
166 struct DlopenCount {
167 const mach_header* loadAddress;
168 uintptr_t refCount;
169 };
170
171 //
172 // The ImmutableRanges structure is used to make dyld_is_memory_immutable()
173 // fast and lock free. The table contains just ranges that are immutable,
174 // which means they are non-writable and will never be unloaded.
175 // This means the table is only every appended to. No entries are ever removed
176 // or changed. This makes it easier to be lock-less. The array fields
177 // all start as zero. Entries are only appended with the writer lock,
178 // so we don't need to worry about multiple writers colliding. And when
179 // appending, the end field is set before the start field. Readers
180 // of this structure just walk down the array and quit at the first
181 // start field that is zero.
182 //
183 struct ImmutableRanges {
184 std::atomic<ImmutableRanges*> next;
185 uintptr_t arraySize;
186 struct {
187 std::atomic<uintptr_t> start;
188 std::atomic<uintptr_t> end;
189 } array[2]; // programs with only main-exe and dyld cache fit in here
190 };
191
192 const MachOLoaded* loadImage(Diagnostics& diag, const char* path,
193 closure::ImageNum topImageNum, const closure::DlopenClosure* newClosure,
194 bool rtldLocal, bool rtldNoDelete, bool rtldNow, bool fromOFI,
195 const void* callerAddress);
196
197
198 typedef void (*Initializer)(int argc, const char* argv[], char* envp[], const char* apple[], const ProgramVars* vars);
199 typedef const Array<LoadedImage> StartImageArray;
200
201 void runInitialzersInImage(const mach_header* imageLoadAddress, const closure::Image* image);
202 void mirrorToOldAllImageInfos();
203 void garbageCollectImages();
204 void breadthFirstRecurseDependents(Array<closure::ImageNum>& visited, const LoadedImage& nodeLi, bool& stop, void (^handler)(const LoadedImage& aLoadedImage, bool& stop)) const;
205 void appendToImagesArray(const closure::ImageArray* newArray);
206 void withReadLock(void (^work)()) const;
207 void withWriteLock(void (^work)());
208 void withNotifiersLock(void (^work)()) const;
209 bool findImage(const mach_header* loadAddress, LoadedImage& foundImage) const;
210 bool findImageNum(closure::ImageNum imageNum, LoadedImage& foundImage) const;
211 LoadedImage findImageNum(closure::ImageNum num, uint32_t& indexHint);
212 bool swapImageState(closure::ImageNum num, uint32_t& indexHint, LoadedImage::State expectedCurrentState, LoadedImage::State newState);
213 void runAllInitializersInImage(const closure::Image* image, const MachOLoaded* ml);
214 void recomputeBounds();
215 void runAllStaticTerminators();
216 uintptr_t resolveTarget(closure::Image::ResolvedSymbolTarget target) const;
217 void addImmutableRange(uintptr_t start, uintptr_t end);
218
219 static void runAllStaticTerminatorsHelper(void*);
220
221 typedef closure::ImageArray ImageArray;
222
223 typedef const closure::LaunchClosure* __ptrauth_dyld_address_auth MainClosurePtrType;
224
225 MainClosurePtrType _mainClosure = nullptr;
226 const DyldSharedCache* _dyldCacheAddress = nullptr;
227 const char* _dyldCachePath = nullptr;
228 uint64_t _dyldCacheSlide = 0;
229 StartImageArray* _initialImages = nullptr;
230 const char* _mainExeOverridePath = nullptr;
231 _dyld_objc_notify_mapped _objcNotifyMapped = nullptr;
232 _dyld_objc_notify_init _objcNotifyInit = nullptr;
233 _dyld_objc_notify_unmapped _objcNotifyUnmapped = nullptr;
234 ProgramVars* _programVars = nullptr;
235 dyld_all_image_infos* _oldAllImageInfos = nullptr;
236 dyld_image_info* _oldAllImageArray = nullptr;
237 dyld_uuid_info* _oldUUIDArray = nullptr;
238 const GradedArchs* _archs = nullptr;
239 ImmutableRanges _immutableRanges = { nullptr, 2 };
240 uint32_t _oldArrayAllocCount = 0;
241 uint32_t _oldUUIDAllocCount = 0;
242 closure::ImageNum _nextImageNum = 0;
243 int32_t _gcCount = 0;
244 bool _processDOFs = false;
245 bool _allowAtPaths = false;
246 bool _allowEnvPaths = false;
247 bool _someImageOverridden = false;
248 uint32_t _launchMode = 0;
249 uintptr_t _lowestNonCached = 0;
250 uintptr_t _highestNonCached = UINTPTR_MAX;
251
252 MainFunc _driverkitMain = nullptr;
253 #ifdef OS_UNFAIR_RECURSIVE_LOCK_INIT
254 mutable os_unfair_recursive_lock _globalLock = OS_UNFAIR_RECURSIVE_LOCK_INIT;
255 #else
256 mutable pthread_mutex_t _globalLock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER;
257 #endif
258 GrowableArray<const ImageArray*, 4, 4> _imagesArrays;
259 GrowableArray<NotifyFunc, 4, 4> _loadNotifiers;
260 GrowableArray<NotifyFunc, 4, 4> _unloadNotifiers;
261 GrowableArray<LoadNotifyFunc, 4, 4> _loadNotifiers2;
262 GrowableArray<BulkLoadNotifier, 2, 2> _loadBulkNotifiers;
263 GrowableArray<DlopenCount, 4, 4> _dlopenRefCounts;
264 GrowableArray<LoadedImage, 16> _loadedImages;
265 #if TARGET_OS_OSX
266 uint64_t _nextObjectFileImageNum = 0;
267 GrowableArray<OFIInfo, 4, 1> _objectFileImages;
268 #endif
269
270 // ObjC selectors
271 // This is an array of the base addresses of sections containing selector strings
272 GrowableArray<uintptr_t, 4, 4> _objcSelectorHashTableImages;
273 const closure::ObjCSelectorOpt* _objcSelectorHashTable = nullptr;
274
275 // ObjC classes
276 // This is an array of the base addresses of (name vmaddr, data vmaddr) pairs of sections in each image
277 GrowableArray<std::pair<uintptr_t, uintptr_t>, 4, 4> _objcClassHashTableImages;
278 const closure::ObjCClassOpt* _objcClassHashTable = nullptr;
279 const closure::ObjCClassDuplicatesOpt* _objcClassDuplicatesHashTable = nullptr;
280 const closure::ObjCClassOpt* _objcProtocolHashTable = nullptr;
281 const objc_opt::objc_opt_t* _dyldCacheObjCOpt = nullptr;
282 };
283
284 extern AllImages gAllImages;
285
286
287 } // dyld3
288
289
290 #endif // __ALL_IMAGES_H__