dyld-732.8.tar.gz
[apple/dyld.git] / dyld3 / Loading.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
26 #ifndef __DYLD_LOADING_H__
27 #define __DYLD_LOADING_H__
28
29 #include <string.h>
30 #include <stdint.h>
31 #include <mach/mach.h>
32 #include <_simple.h>
33
34 #include "Closure.h"
35 #include "MachOLoaded.h"
36
37 namespace objc_opt {
38 struct objc_clsopt_t;
39 struct objc_selopt_t;
40 }
41
42 namespace dyld3 {
43
44
45 //
46 // Tuple of info about a loaded image. Contains the loaded address, Image*, and state.
47 //
48 class VIS_HIDDEN LoadedImage {
49 public:
50 enum class State { unmapped=0, mapped=1, fixedUp=2, beingInited=3, inited=4 };
51
52 static LoadedImage make(const closure::Image* img) { LoadedImage result; result._image = img; return result; }
53 static LoadedImage make(const closure::Image* img, const MachOLoaded* mh)
54 { LoadedImage result; result._image = img; result.setLoadedAddress(mh); return result; }
55
56 const closure::Image* image() const { return _image; }
57 const MachOLoaded* loadedAddress() const { return (MachOLoaded*)(_loadAddr & (-4096)); }
58 void setLoadedAddress(const MachOLoaded* a) { _loadAddr |= ((uintptr_t)a & (-4096)); }
59 State state() const { return (State)(asBits().state); }
60 void setState(State s) { asBits().state = (int)s; }
61 bool hideFromFlatSearch() const { return asBits().hide; }
62 void setHideFromFlatSearch(bool h) { asBits().hide = h; }
63 bool leaveMapped() const { return asBits().leaveMapped; }
64 void markLeaveMapped() { asBits().leaveMapped = true; }
65
66 private:
67 // since loaded MachO files are always page aligned, that means at least low 12-bits are always zero
68 // so we don't need to record the low 12 bits, instead those bits hold various flags in the _loadeAddr field
69 struct AddrBits {
70 uintptr_t state : 3,
71 hide : 1,
72 leaveMapped : 1,
73 extra : 7,
74 #if __LP64__
75 addr : 52;
76 #else
77 addr : 20;
78 #endif
79 };
80 AddrBits& asBits() { return *((AddrBits*)&_loadAddr); }
81 const AddrBits& asBits() const { return *((AddrBits*)&_loadAddr); }
82
83 // Note: this must be statically initializable so as to not cause static initializers
84 const closure::Image* _image = nullptr;
85 uintptr_t _loadAddr = 0; // really AddrBits
86 };
87
88
89 //
90 // Utility class to recursively load dependents
91 //
92 class VIS_HIDDEN Loader {
93 public:
94 typedef bool (*LogFunc)(const char*, ...) __attribute__((format(printf, 1, 2)));
95
96 Loader(const Array<LoadedImage>& existingImages, Array<LoadedImage>& newImagesStorage,
97 const void* cacheAddress, const Array<const dyld3::closure::ImageArray*>& imagesArrays,
98 const closure::ObjCSelectorOpt* selOpt, const Array<closure::Image::ObjCSelectorImage>& selImages,
99 LogFunc log_loads, LogFunc log_segments, LogFunc log_fixups, LogFunc log_dofs);
100
101 void addImage(const LoadedImage&);
102 void completeAllDependents(Diagnostics& diag, bool& someCacheImageOverridden);
103 void mapAndFixupAllImages(Diagnostics& diag, bool processDOFs, bool fromOFI=false);
104 uintptr_t resolveTarget(closure::Image::ResolvedSymbolTarget target);
105 LoadedImage* findImage(closure::ImageNum targetImageNum);
106
107 static void unmapImage(LoadedImage& info);
108 static bool dtraceUserProbesEnabled();
109 static void vmAccountingSetSuspended(bool suspend, LogFunc);
110
111 private:
112
113 struct ImageOverride
114 {
115 closure::ImageNum inCache;
116 closure::ImageNum replacement;
117 };
118
119 struct DOFInfo {
120 const void* dof;
121 const mach_header* imageHeader;
122 const char* imageShortName;
123 };
124
125 #if BUILDING_DYLD
126 struct LaunchImagesCache {
127 LoadedImage* findImage(closure::ImageNum targetImageNum,
128 Array<LoadedImage>& images) const;
129 void tryAddImage(closure::ImageNum targetImageNum, uint64_t allImagesIndex);
130
131 static const uint64_t _cacheSize = 128;
132 static const closure::ImageNum _firstImageNum = closure::kFirstLaunchClosureImageNum;
133 static const closure::ImageNum _lastImageNum = closure::kFirstLaunchClosureImageNum + _cacheSize;
134
135 // Note, the cache stores "indices + 1" into the _allImages array.
136 // 0 means we haven't cached an entry yet
137 uint32_t _cacheStorage[_cacheSize] = { 0 };
138 Array<uint32_t> _imageIndices = { &_cacheStorage[0], _cacheSize, _cacheSize };
139 };
140 #endif
141
142 void mapImage(Diagnostics& diag, LoadedImage& info, bool fromOFI);
143 void applyFixupsToImage(Diagnostics& diag, LoadedImage& info);
144 void registerDOFs(const Array<DOFInfo>& dofs);
145 void setSegmentProtects(const LoadedImage& info, bool write);
146 bool sandboxBlockedMmap(const char* path);
147 bool sandboxBlockedOpen(const char* path);
148 bool sandboxBlockedStat(const char* path);
149 bool sandboxBlocked(const char* path, const char* kind);
150
151 const Array<LoadedImage>& _existingImages;
152 Array<LoadedImage>& _newImages;
153 const Array<const closure::ImageArray*>& _imagesArrays;
154 const void* _dyldCacheAddress;
155 const objc_opt::objc_selopt_t* _dyldCacheSelectorOpt;
156 const closure::ObjCSelectorOpt* _closureSelectorOpt;
157 const Array<closure::Image::ObjCSelectorImage>& _closureSelectorImages;
158 #if BUILDING_DYLD
159 LaunchImagesCache _launchImagesCache;
160 #endif
161 LogFunc _logLoads;
162 LogFunc _logSegments;
163 LogFunc _logFixups;
164 LogFunc _logDofs;
165 };
166
167
168
169 #if (BUILDING_LIBDYLD || BUILDING_DYLD)
170 bool internalInstall();
171 #endif
172 #if BUILDING_DYLD
173 void forEachLineInFile(const char* path, void (^lineHandler)(const char* line, bool& stop));
174 void forEachLineInFile(const char* buffer, size_t bufferLen, void (^lineHandler)(const char* line, bool& stop));
175 #endif
176
177 } // namespace dyld3
178
179 #endif // __DYLD_LOADING_H__
180
181