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"
32 #include "start_glue.h"
34 extern "C" void start();
37 VIS_HIDDEN
const char** appleParams
;
39 extern bool gUseDyld3
;
44 AllImages::ProgramVars sVars
;
45 static void (*sChildForkFunction
)();
47 static const char* leafName(const char* argv0
)
49 if ( argv0
== nullptr )
52 if ( const char* lastSlash
= strrchr(argv0
, '/') )
58 static void entry_setVars(const mach_header
* mainMH
, int argc
, const char* argv
[], const char* envp
[], const char* apple
[])
62 environ
= (char**)envp
;
64 __progname
= leafName(argv
[0]);
67 sVars
.NXArgcPtr
= &NXArgc
;
68 sVars
.NXArgvPtr
= &NXArgv
;
69 sVars
.environPtr
= (const char***)&environ
;
70 sVars
.__prognamePtr
= &__progname
;
71 gAllImages
.setProgramVars(&sVars
);
75 setLoggingFromEnvs(envp
);
78 static void entry_setHaltFunction(void (*func
)(const char* message
) __attribute__((noreturn
)) )
80 setHaltFunction(func
);
83 static void entry_setLogFunction(void (*logFunction
)(const char* format
, va_list list
))
85 setLoggingFunction(logFunction
);
88 static void entry_setOldAllImageInfo(dyld_all_image_infos
* old
)
90 gAllImages
.setOldAllImageInfo(old
);
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
)
98 gAllImages
.init(closure
, dyldCacheLoadAddress
, dyldCachePath
, initialImages
);
99 gAllImages
.applyInterposingToDyldCache(closure
, initialImages
);
101 const char* mainPath
= _simple_getenv(appleParams
, "executable_path");
102 if ( (mainPath
!= nullptr) && (mainPath
[0] == '/') )
103 gAllImages
.setMainPath(mainPath
);
105 // ok to call before malloc is ready because 4 slots are reserved.
106 gAllImages
.setInitialGroups();
108 // run initializer for libSytem.B.dylib
109 // this calls back into _dyld_initializer which calls gAllIimages.addImages()
110 gAllImages
.runLibSystemInitializer(libSystemMH
, libSystemImage
);
112 // now that malloc is available, parse DYLD_ env vars
113 gPathOverrides
.setEnvVars((const char**)environ
);
116 static void entry_runInitialzersBottomUp(const mach_header
* mainExecutableImageLoadAddress
)
118 gAllImages
.runInitialzersBottomUp(mainExecutableImageLoadAddress
);
119 gAllImages
.notifyMonitorMain();
122 static void entry_setChildForkFunction(void (*func
)() )
124 sChildForkFunction
= func
;
127 typedef void (*StartFunc
)();
129 const LibDyldEntryVector entryVectorForDyld
= {
130 LibDyldEntryVector::kCurrentVectorVersion
,
131 launch_cache::binary_format::kFormatVersion
,
133 &entry_setHaltFunction
,
134 &entry_setOldAllImageInfo
,
135 &entry_setInitialImageList
,
136 &entry_runInitialzersBottomUp
,
137 (StartFunc
)address_of_start
,
138 &entry_setChildForkFunction
,
139 &entry_setLogFunction
,
142 VIS_HIDDEN
void _dyld_fork_child()
144 (*sChildForkFunction
)();