2 * Copyright (c) 2017 Apple Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
26 #include "dyld_priv.h"
27 #include "libdyldEntryVector.h"
28 #include "AllImages.h"
30 #include "PathOverrides.h"
31 #include "LaunchCacheFormat.h"
33 extern "C" void start();
36 VIS_HIDDEN
const char** appleParams
;
38 extern bool gUseDyld3
;
43 AllImages::ProgramVars sVars
;
44 static void (*sChildForkFunction
)();
46 static const char* leafName(const char* argv0
)
48 if ( argv0
== nullptr )
51 if ( const char* lastSlash
= strrchr(argv0
, '/') )
57 static void entry_setVars(const mach_header
* mainMH
, int argc
, const char* argv
[], const char* envp
[], const char* apple
[])
61 environ
= (char**)envp
;
63 __progname
= leafName(argv
[0]);
66 sVars
.NXArgcPtr
= &NXArgc
;
67 sVars
.NXArgvPtr
= &NXArgv
;
68 sVars
.environPtr
= (const char***)&environ
;
69 sVars
.__prognamePtr
= &__progname
;
70 gAllImages
.setProgramVars(&sVars
);
74 setLoggingFromEnvs(envp
);
77 static void entry_setHaltFunction(void (*func
)(const char* message
) __attribute__((noreturn
)) )
79 setHaltFunction(func
);
82 static void entry_setLogFunction(void (*logFunction
)(const char* format
, va_list list
))
84 setLoggingFunction(logFunction
);
87 static void entry_setOldAllImageInfo(dyld_all_image_infos
* old
)
89 gAllImages
.setOldAllImageInfo(old
);
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
)
97 gAllImages
.init(closure
, dyldCacheLoadAddress
, dyldCachePath
, initialImages
);
98 gAllImages
.applyInterposingToDyldCache(closure
, initialImages
);
100 const char* mainPath
= _simple_getenv(appleParams
, "executable_path");
101 if ( (mainPath
!= nullptr) && (mainPath
[0] == '/') )
102 gAllImages
.setMainPath(mainPath
);
104 // ok to call before malloc is ready because 4 slots are reserved.
105 gAllImages
.setInitialGroups();
107 // run initializer for libSytem.B.dylib
108 // this calls back into _dyld_initializer which calls gAllIimages.addImages()
109 gAllImages
.runLibSystemInitializer(libSystemMH
, libSystemImage
);
111 // now that malloc is available, parse DYLD_ env vars
112 gPathOverrides
.setEnvVars((const char**)environ
);
115 static void entry_runInitialzersBottomUp(const mach_header
* mainExecutableImageLoadAddress
)
117 gAllImages
.runInitialzersBottomUp(mainExecutableImageLoadAddress
);
118 gAllImages
.notifyMonitorMain();
121 static void entry_setChildForkFunction(void (*func
)() )
123 sChildForkFunction
= func
;
126 const LibDyldEntryVector entryVectorForDyld
= {
127 LibDyldEntryVector::kCurrentVectorVersion
,
128 launch_cache::binary_format::kFormatVersion
,
130 &entry_setHaltFunction
,
131 &entry_setOldAllImageInfo
,
132 &entry_setInitialImageList
,
133 &entry_runInitialzersBottomUp
,
135 &entry_setChildForkFunction
,
136 &entry_setLogFunction
,
139 VIS_HIDDEN
void _dyld_fork_child()
141 (*sChildForkFunction
)();