dyld-551.3.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 <pthread.h>
29 #include <mach-o/loader.h>
30
31 #include "dyld_priv.h"
32
33 #include "LaunchCache.h"
34 #include "Loading.h"
35
36
37
38 #if __MAC_OS_X_VERSION_MIN_REQUIRED
39 // only in macOS and deprecated
40 struct VIS_HIDDEN __NSObjectFileImage
41 {
42 const char* path = nullptr;
43 const void* memSource = nullptr;
44 size_t memLength = 0;
45 const mach_header* loadAddress = nullptr;
46 const dyld3::launch_cache::binary_format::Image* binImage = nullptr;
47 };
48 #endif
49
50 namespace dyld3 {
51
52 class VIS_HIDDEN AllImages
53 {
54 public:
55 typedef launch_cache::binary_format::Closure BinaryClosure;
56 typedef launch_cache::binary_format::ImageGroup BinaryImageGroup;
57 typedef launch_cache::binary_format::Image BinaryImage;
58 typedef launch_cache::ImageGroupList ImageGroupList;
59 typedef void (*NotifyFunc)(const mach_header* mh, intptr_t slide);
60
61 void init(const BinaryClosure* closure, const void* dyldCacheLoadAddress, const char* dyldCachePath,
62 const dyld3::launch_cache::DynArray<loader::ImageInfo>& initialImages);
63 void setMainPath(const char* path);
64 void applyInitialImages();
65
66 void addImages(const dyld3::launch_cache::DynArray<loader::ImageInfo>& newImages);
67 void removeImages(const launch_cache::DynArray<loader::ImageInfo>& unloadImages);
68 void setNeverUnload(const loader::ImageInfo& existingImage);
69 void applyInterposingToDyldCache(const launch_cache::binary_format::Closure* closure, const dyld3::launch_cache::DynArray<loader::ImageInfo>& initialImages);
70 void runInitialzersBottomUp(const mach_header* imageLoadAddress);
71 void setInitialGroups();
72
73 uint32_t count() const;
74 const BinaryImageGroup* cachedDylibsGroup();
75 const BinaryImageGroup* otherDylibsGroup();
76 const BinaryImageGroup* mainClosureGroup();
77 const BinaryClosure* mainClosure() { return _mainClosure; }
78 uint32_t currentGroupsCount() const;
79 void copyCurrentGroups(ImageGroupList& groups) const;
80
81 const BinaryImage* messageClosured(const char* path, const char* apiName, const char* closuredErrorMessages[3], int& closuredErrorMessagesCount);
82
83 launch_cache::Image findByLoadOrder(uint32_t index, const mach_header** loadAddress) const;
84 launch_cache::Image findByLoadAddress(const mach_header* loadAddress) const;
85 launch_cache::Image findByOwnedAddress(const void* addr, const mach_header** loadAddress, uint8_t* permissions=nullptr) const;
86 const mach_header* findLoadAddressByImage(const BinaryImage*) const;
87 bool findIndexForLoadAddress(const mach_header* loadAddress, uint32_t& index);
88 void forEachImage(void (^handler)(uint32_t imageIndex, const mach_header* loadAddress, const launch_cache::Image image, bool& stop)) const;
89
90 const mach_header* mainExecutable() const;
91 launch_cache::Image mainExecutableImage() const;
92 const void* cacheLoadAddress() const { return _dyldCacheAddress; }
93 const char* dyldCachePath() const { return _dyldCachePath; }
94 const char* imagePath(const BinaryImage*) const;
95
96 const mach_header* alreadyLoaded(const char* path, bool bumpRefCount);
97 const mach_header* alreadyLoaded(const BinaryImage*, bool bumpRefCount);
98 const mach_header* alreadyLoaded(uint64_t inode, uint64_t mtime, bool bumpRefCount);
99 const BinaryImage* findImageInKnownGroups(const char* path);
100
101 bool imageUnloadable(const launch_cache::Image& image, const mach_header* loadAddress) const;
102 void incRefCount(const mach_header* loadAddress);
103 void decRefCount(const mach_header* loadAddress);
104
105 void addLoadNotifier(NotifyFunc);
106 void addUnloadNotifier(NotifyFunc);
107 void setObjCNotifiers(_dyld_objc_notify_mapped, _dyld_objc_notify_init, _dyld_objc_notify_unmapped);
108 void notifyObjCUnmap(const char* path, const struct mach_header* mh);
109
110 void runLibSystemInitializer(const mach_header* imageLoadAddress, const launch_cache::binary_format::Image* binImage);
111
112 void setOldAllImageInfo(dyld_all_image_infos* old) { _oldAllImageInfos = old; }
113 dyld_all_image_infos* oldAllImageInfo() const { return _oldAllImageInfos;}
114
115 void notifyMonitorMain();
116 void notifyMonitorLoads(const launch_cache::DynArray<loader::ImageInfo>& newImages);
117 void notifyMonitorUnloads(const launch_cache::DynArray<loader::ImageInfo>& unloadingImages);
118
119 #if __MAC_OS_X_VERSION_MIN_REQUIRED
120 __NSObjectFileImage* addNSObjectFileImage();
121 bool hasNSObjectFileImage(__NSObjectFileImage*);
122 void removeNSObjectFileImage(__NSObjectFileImage*);
123 #endif
124
125 struct ProgramVars
126 {
127 const void* mh;
128 int* NXArgcPtr;
129 const char*** NXArgvPtr;
130 const char*** environPtr;
131 const char** __prognamePtr;
132 };
133 void setProgramVars(ProgramVars* vars);
134
135 private:
136
137 typedef void (*Initializer)(int argc, const char* argv[], char* envp[], const char* apple[], const ProgramVars* vars);
138 typedef const launch_cache::DynArray<loader::ImageInfo> StartImageArray;
139
140 void runInitialzersInImage(const mach_header* imageLoadAddress, const launch_cache::binary_format::Image* binImage);
141 void mirrorToOldAllImageInfos();
142 void garbageCollectImages();
143 void vmAccountingSetSuspended(bool suspend);
144 void copyCurrentGroupsNoLock(ImageGroupList& groups) const;
145
146 const BinaryClosure* _mainClosure = nullptr;
147 const void* _dyldCacheAddress = nullptr;
148 const char* _dyldCachePath = nullptr;
149 uint64_t _dyldCacheSlide = 0;
150 StartImageArray* _initialImages = nullptr;
151 const char* _mainExeOverridePath = nullptr;
152 _dyld_objc_notify_mapped _objcNotifyMapped = nullptr;
153 _dyld_objc_notify_init _objcNotifyInit = nullptr;
154 _dyld_objc_notify_unmapped _objcNotifyUnmapped = nullptr;
155 ProgramVars* _programVars = nullptr;
156 dyld_all_image_infos* _oldAllImageInfos = nullptr;
157 dyld_image_info* _oldAllImageArray = nullptr;
158 uint32_t _oldArrayAllocCount = 0;
159 pthread_mutex_t _initializerLock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER;
160 pthread_cond_t _initializerCondition= PTHREAD_COND_INITIALIZER;
161 int32_t _gcCount = 0;
162 };
163
164 extern AllImages gAllImages;
165
166
167 } // dyld3
168
169
170 #endif // __ALL_IMAGES_H__