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