dyld-551.3.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 "Logging.h"
30 #include "PathOverrides.h"
31 #include "LaunchCacheFormat.h"
32 #include "start_glue.h"
33
34 extern "C" void start();
35
36
37 VIS_HIDDEN const char** appleParams;
38
39 extern bool gUseDyld3;
40
41 namespace dyld3 {
42
43
44 AllImages::ProgramVars sVars;
45 static void (*sChildForkFunction)();
46
47 static const char* leafName(const char* argv0)
48 {
49 if ( argv0 == nullptr )
50 return "";
51
52 if ( const char* lastSlash = strrchr(argv0, '/') )
53 return lastSlash+1;
54 else
55 return argv0;
56 }
57
58 static void entry_setVars(const mach_header* mainMH, int argc, const char* argv[], const char* envp[], const char* apple[])
59 {
60 NXArgc = argc;
61 NXArgv = argv;
62 environ = (char**)envp;
63 appleParams = apple;
64 __progname = leafName(argv[0]);
65
66 sVars.mh = mainMH;
67 sVars.NXArgcPtr = &NXArgc;
68 sVars.NXArgvPtr = &NXArgv;
69 sVars.environPtr = (const char***)&environ;
70 sVars.__prognamePtr = &__progname;
71 gAllImages.setProgramVars(&sVars);
72
73 gUseDyld3 = true;
74
75 setLoggingFromEnvs(envp);
76 }
77
78 static void entry_setHaltFunction(void (*func)(const char* message) __attribute__((noreturn)) )
79 {
80 setHaltFunction(func);
81 }
82
83 static void entry_setLogFunction(void (*logFunction)(const char* format, va_list list))
84 {
85 setLoggingFunction(logFunction);
86 }
87
88 static void entry_setOldAllImageInfo(dyld_all_image_infos* old)
89 {
90 gAllImages.setOldAllImageInfo(old);
91 }
92
93 static void entry_setInitialImageList(const launch_cache::binary_format::Closure* closure,
94 const void* dyldCacheLoadAddress, const char* dyldCachePath,
95 const dyld3::launch_cache::DynArray<loader::ImageInfo>& initialImages,
96 const mach_header* libSystemMH, const launch_cache::binary_format::Image* libSystemImage)
97 {
98 gAllImages.init(closure, dyldCacheLoadAddress, dyldCachePath, initialImages);
99 gAllImages.applyInterposingToDyldCache(closure, initialImages);
100
101 const char* mainPath = _simple_getenv(appleParams, "executable_path");
102 if ( (mainPath != nullptr) && (mainPath[0] == '/') )
103 gAllImages.setMainPath(mainPath);
104
105 // ok to call before malloc is ready because 4 slots are reserved.
106 gAllImages.setInitialGroups();
107
108 // run initializer for libSytem.B.dylib
109 // this calls back into _dyld_initializer which calls gAllIimages.addImages()
110 gAllImages.runLibSystemInitializer(libSystemMH, libSystemImage);
111
112 // now that malloc is available, parse DYLD_ env vars
113 gPathOverrides.setEnvVars((const char**)environ);
114 }
115
116 static void entry_runInitialzersBottomUp(const mach_header* mainExecutableImageLoadAddress)
117 {
118 gAllImages.runInitialzersBottomUp(mainExecutableImageLoadAddress);
119 gAllImages.notifyMonitorMain();
120 }
121
122 static void entry_setChildForkFunction(void (*func)() )
123 {
124 sChildForkFunction = func;
125 }
126
127 typedef void (*StartFunc)();
128
129 const LibDyldEntryVector entryVectorForDyld = {
130 LibDyldEntryVector::kCurrentVectorVersion,
131 launch_cache::binary_format::kFormatVersion,
132 &entry_setVars,
133 &entry_setHaltFunction,
134 &entry_setOldAllImageInfo,
135 &entry_setInitialImageList,
136 &entry_runInitialzersBottomUp,
137 (StartFunc)address_of_start,
138 &entry_setChildForkFunction,
139 &entry_setLogFunction,
140 };
141
142 VIS_HIDDEN void _dyld_fork_child()
143 {
144 (*sChildForkFunction)();
145 }
146
147
148 } // namespace dyld3
149