dyld-655.1.tar.gz
[apple/dyld.git] / dyld3 / libdyldEntryVector.cpp
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 #include <stdarg.h>
25
26 #include "dyld_priv.h"
27 #include "libdyldEntryVector.h"
28 #include "AllImages.h"
29 #include "Array.h"
30 #include "Loading.h"
31 #include "Logging.h"
32 #include "PathOverrides.h"
33 #include "StartGlue.h"
34 #include "dyld_process_info_internal.h"
35
36 extern "C" char start;
37
38 VIS_HIDDEN const char** appleParams;
39
40 extern bool gUseDyld3;
41
42 namespace dyld3 {
43
44
45 AllImages::ProgramVars sVars;
46 static void (*sChildForkFunction)();
47
48 static const char* leafName(const char* argv0)
49 {
50 if ( argv0 == nullptr )
51 return "";
52
53 if ( const char* lastSlash = strrchr(argv0, '/') )
54 return lastSlash+1;
55 else
56 return argv0;
57 }
58
59 static void entry_setVars(const mach_header* mainMH, int argc, const char* argv[], const char* envp[], const char* apple[])
60 {
61 NXArgc = argc;
62 NXArgv = argv;
63 environ = (char**)envp;
64 appleParams = apple;
65 __progname = leafName(argv[0]);
66
67 sVars.mh = mainMH;
68 sVars.NXArgcPtr = &NXArgc;
69 sVars.NXArgvPtr = &NXArgv;
70 sVars.environPtr = (const char***)&environ;
71 sVars.__prognamePtr = &__progname;
72 gAllImages.setProgramVars(&sVars);
73
74 gUseDyld3 = true;
75
76 setLoggingFromEnvs(envp);
77 }
78
79 static void entry_setHaltFunction(void (*func)(const char* message) __attribute__((noreturn)) )
80 {
81 setHaltFunction(func);
82 }
83
84 static void entry_setLogFunction(void (*logFunction)(const char* format, va_list list))
85 {
86 setLoggingFunction(logFunction);
87 }
88
89 static void entry_setOldAllImageInfo(dyld_all_image_infos* old)
90 {
91 gAllImages.setOldAllImageInfo(old);
92 }
93
94 static void entry_setNotifyMonitoringDyldMain(void (*notifyMonitoringDyldMain)()) {
95 setNotifyMonitoringDyldMain(notifyMonitoringDyldMain);
96 }
97
98 static void entry_setNotifyMonitoringDyld(void (*notifyMonitoringDyld)(bool unloading,unsigned imageCount,
99 const struct mach_header* loadAddresses[],
100 const char* imagePaths[])) {
101 setNotifyMonitoringDyld(notifyMonitoringDyld);
102 }
103
104 static void entry_setInitialImageList(const closure::LaunchClosure* closure,
105 const DyldSharedCache* dyldCacheLoadAddress, const char* dyldCachePath,
106 const Array<LoadedImage>& initialImages, const LoadedImage& libSystem)
107 {
108 gAllImages.init(closure, dyldCacheLoadAddress, dyldCachePath, initialImages);
109 gAllImages.applyInterposingToDyldCache(closure);
110
111 const char* mainPath = _simple_getenv(appleParams, "executable_path");
112 if ( (mainPath != nullptr) && (mainPath[0] == '/') )
113 gAllImages.setMainPath(mainPath);
114
115 // run initializer for libSytem.B.dylib
116 // this calls back into _dyld_initializer which calls gAllIimages.addImages()
117 gAllImages.runLibSystemInitializer(libSystem);
118
119 // now that malloc is available, parse DYLD_ env vars
120 closure::gPathOverrides.setEnvVars((const char**)environ, gAllImages.mainExecutable(), gAllImages.mainExecutableImage()->path());
121 }
122
123 static void entry_runInitialzersBottomUp(const mach_header* mainExecutableImageLoadAddress)
124 {
125 gAllImages.runStartupInitialzers();
126 gAllImages.notifyMonitorMain();
127 }
128
129 static void entry_setChildForkFunction(void (*func)() )
130 {
131 sChildForkFunction = func;
132 }
133
134 static void entry_setRestrictions(bool allowAtPaths, bool allowEnvPaths)
135 {
136 gAllImages.setRestrictions(allowAtPaths, allowEnvPaths);
137 }
138
139 const LibDyldEntryVector entryVectorForDyld = {
140 LibDyldEntryVector::kCurrentVectorVersion,
141 closure::kFormatVersion,
142 &entry_setVars,
143 &entry_setHaltFunction,
144 &entry_setOldAllImageInfo,
145 &entry_setInitialImageList,
146 &entry_runInitialzersBottomUp,
147 (__typeof(LibDyldEntryVector::startFunc))address_of_start,
148 &entry_setChildForkFunction,
149 &entry_setLogFunction,
150 &entry_setRestrictions,
151 &entry_setNotifyMonitoringDyldMain,
152 &entry_setNotifyMonitoringDyld
153 };
154
155 VIS_HIDDEN void _dyld_fork_child()
156 {
157 (*sChildForkFunction)();
158 }
159
160
161 } // namespace dyld3
162