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