1 /* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*-
3 * Copyright (c) 2004-2013 Apple Inc. All rights reserved.
5 * @APPLE_LICENSE_HEADER_START@
7 * This file contains Original Code and/or Modifications of Original Code
8 * as defined in and that are subject to the Apple Public Source License
9 * Version 2.0 (the 'License'). You may not use this file except in
10 * compliance with the License. Please obtain a copy of the License at
11 * http://www.opensource.apple.com/apsl/ and read it before using this
14 * The Original Code and all software distributed under the License are
15 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
19 * Please see the License for the specific language governing rights and
20 * limitations under the License.
22 * @APPLE_LICENSE_HEADER_END@
33 #include <sys/param.h>
34 #include <mach/mach_time.h> // mach_absolute_time()
35 #include <mach/mach_init.h>
36 #include <sys/types.h>
38 #include <sys/syscall.h>
39 #include <sys/socket.h>
41 #include <sys/syslog.h>
43 #include <mach/mach.h>
44 #include <mach-o/fat.h>
45 #include <mach-o/loader.h>
46 #include <mach-o/ldsyms.h>
47 #include <libkern/OSByteOrder.h>
48 #include <libkern/OSAtomic.h>
49 #include <mach/mach.h>
50 #include <sys/sysctl.h>
52 #include <sys/dtrace.h>
53 #include <libkern/OSAtomic.h>
54 #include <Availability.h>
55 #include <System/sys/codesign.h>
56 #include <System/sys/csr.h>
58 #include <os/lock_private.h>
59 #include <System/machine/cpu_capabilities.h>
60 #include <System/sys/reason.h>
61 #include <kern/kcdata.h>
62 #if TARGET_IPHONE_SIMULATOR
64 AMFI_DYLD_INPUT_PROC_IN_SIMULATOR
= (1 << 0),
66 enum amfi_dyld_policy_output_flag_set
{
67 AMFI_DYLD_OUTPUT_ALLOW_AT_PATH
= (1 << 0),
68 AMFI_DYLD_OUTPUT_ALLOW_PATH_VARS
= (1 << 1),
69 AMFI_DYLD_OUTPUT_ALLOW_CUSTOM_SHARED_CACHE
= (1 << 2),
70 AMFI_DYLD_OUTPUT_ALLOW_FALLBACK_PATHS
= (1 << 3),
71 AMFI_DYLD_OUTPUT_ALLOW_PRINT_VARS
= (1 << 4),
72 AMFI_DYLD_OUTPUT_ALLOW_FAILED_LIBRARY_INSERTION
= (1 << 5),
74 extern "C" int amfi_check_dyld_policy_self(uint64_t input_flags
, uint64_t* output_flags
);
79 #include <corecrypto/ccdigest.h>
80 #include <corecrypto/ccsha2.h>
83 #include <sandbox/private.h>
84 #if __has_feature(ptrauth_calls)
88 extern "C" int __fork();
94 #ifndef CPU_SUBTYPE_ARM_V5TEJ
95 #define CPU_SUBTYPE_ARM_V5TEJ ((cpu_subtype_t) 7)
97 #ifndef CPU_SUBTYPE_ARM_XSCALE
98 #define CPU_SUBTYPE_ARM_XSCALE ((cpu_subtype_t) 8)
100 #ifndef CPU_SUBTYPE_ARM_V7
101 #define CPU_SUBTYPE_ARM_V7 ((cpu_subtype_t) 9)
103 #ifndef CPU_SUBTYPE_ARM_V7F
104 #define CPU_SUBTYPE_ARM_V7F ((cpu_subtype_t) 10)
106 #ifndef CPU_SUBTYPE_ARM_V7S
107 #define CPU_SUBTYPE_ARM_V7S ((cpu_subtype_t) 11)
109 #ifndef CPU_SUBTYPE_ARM_V7K
110 #define CPU_SUBTYPE_ARM_V7K ((cpu_subtype_t) 12)
112 #ifndef LC_DYLD_ENVIRONMENT
113 #define LC_DYLD_ENVIRONMENT 0x27
116 #ifndef CPU_SUBTYPE_X86_64_H
117 #define CPU_SUBTYPE_X86_64_H ((cpu_subtype_t) 8)
120 #ifndef CPU_SUBTYPE_ARM64_E
121 #define CPU_SUBTYPE_ARM64_E 2
124 #ifndef CPU_ARCH_ABI64_32
125 #define CPU_ARCH_ABI64_32 ((cpu_type_t) 0x02000000)
127 #ifndef CPU_TYPE_ARM64_32
128 #define CPU_TYPE_ARM64_32 ((cpu_type_t) (CPU_TYPE_ARM | CPU_ARCH_ABI64_32))
130 #ifndef CPU_SUBTYPE_ARM64_32_V8
131 #define CPU_SUBTYPE_ARM64_32_V8 ((cpu_subtype_t) 1)
134 #ifndef VM_PROT_SLIDE
135 #define VM_PROT_SLIDE 0x20
138 #include "mach-o/dyld_gdb.h"
141 #include "ImageLoader.h"
142 #include "ImageLoaderMachO.h"
143 #include "dyldLibSystemInterface.h"
144 #include "dyld_cache_format.h"
145 #include "dyld_process_info_internal.h"
147 #if SUPPORT_ACCELERATE_TABLES
148 #include "ImageLoaderMegaDylib.h"
151 #if TARGET_IPHONE_SIMULATOR
152 extern "C" void* gSyscallHelpers
;
154 #include "dyldSyscallInterface.h"
158 #include "libdyldEntryVector.h"
159 #include "MachOLoaded.h"
161 #include "DyldSharedCache.h"
162 #include "SharedCacheRuntime.h"
163 #include "StringUtils.h"
165 #include "ClosureBuilder.h"
166 #include "ClosureFileSystemPhysical.h"
167 #include "FileUtils.h"
170 // not libc header for send() syscall interface
171 extern "C" ssize_t
__sendto(int, const void *, size_t, int, const struct sockaddr
*, socklen_t
);
174 // ARM and x86_64 are the only architecture that use cpu-sub-types
175 #define CPU_SUBTYPES_SUPPORTED ((__arm__ || __arm64__ || __x86_64__) && !TARGET_IPHONE_SIMULATOR)
178 #define LC_SEGMENT_COMMAND LC_SEGMENT_64
179 #define LC_SEGMENT_COMMAND_WRONG LC_SEGMENT
180 #define LC_ENCRYPT_COMMAND LC_ENCRYPTION_INFO
181 #define macho_segment_command segment_command_64
182 #define macho_section section_64
184 #define LC_SEGMENT_COMMAND LC_SEGMENT
185 #define LC_SEGMENT_COMMAND_WRONG LC_SEGMENT_64
186 #define LC_ENCRYPT_COMMAND LC_ENCRYPTION_INFO_64
187 #define macho_segment_command segment_command
188 #define macho_section section
193 #define CPU_TYPE_MASK 0x00FFFFFF /* complement of CPU_ARCH_MASK */
196 /* implemented in dyld_gdb.cpp */
197 extern void resetAllImages();
198 extern void addImagesToAllImages(uint32_t infoCount
, const dyld_image_info info
[]);
199 extern void removeImageFromAllImages(const mach_header
* mh
);
200 extern void addNonSharedCacheImageUUID(const dyld_uuid_info
& info
);
201 extern const char* notifyGDB(enum dyld_image_states state
, uint32_t infoCount
, const dyld_image_info info
[]);
202 extern size_t allImagesCount();
204 // magic so CrashReporter logs message
206 char error_string
[1024];
209 // magic linker symbol for start of dyld binary
210 extern "C" const macho_header __dso_handle
;
214 // The file contains the core of dyld used to get a process to main().
215 // The API's that dyld supports are implemented in dyldAPIs.cpp.
222 struct RegisteredDOF
{ const mach_header
* mh
; int registrationID
; };
223 struct DylibOverride
{ const char* installName
; const char* override
; };
227 VECTOR_NEVER_DESTRUCTED(ImageLoader
*);
228 VECTOR_NEVER_DESTRUCTED(dyld::RegisteredDOF
);
229 VECTOR_NEVER_DESTRUCTED(dyld::ImageCallback
);
230 VECTOR_NEVER_DESTRUCTED(dyld::DylibOverride
);
231 VECTOR_NEVER_DESTRUCTED(ImageLoader::DynamicReference
);
233 VECTOR_NEVER_DESTRUCTED(dyld_image_state_change_handler
);
239 // state of all environment variables dyld uses
241 struct EnvironmentVariables
{
242 const char* const * DYLD_FRAMEWORK_PATH
;
243 const char* const * DYLD_FALLBACK_FRAMEWORK_PATH
;
244 const char* const * DYLD_LIBRARY_PATH
;
245 const char* const * DYLD_FALLBACK_LIBRARY_PATH
;
246 const char* const * DYLD_INSERT_LIBRARIES
;
247 const char* const * LD_LIBRARY_PATH
; // for unix conformance
248 const char* const * DYLD_VERSIONED_LIBRARY_PATH
;
249 const char* const * DYLD_VERSIONED_FRAMEWORK_PATH
;
250 bool DYLD_PRINT_LIBRARIES_POST_LAUNCH
;
251 bool DYLD_BIND_AT_LAUNCH
;
252 bool DYLD_PRINT_STATISTICS
;
253 bool DYLD_PRINT_STATISTICS_DETAILS
;
254 bool DYLD_PRINT_OPTS
;
256 bool DYLD_DISABLE_DOFS
;
257 // DYLD_SHARED_CACHE_DIR ==> sSharedCacheOverrideDir
258 // DYLD_ROOT_PATH ==> gLinkContext.rootPaths
259 // DYLD_IMAGE_SUFFIX ==> gLinkContext.imageSuffix
260 // DYLD_PRINT_OPTS ==> gLinkContext.verboseOpts
261 // DYLD_PRINT_ENV ==> gLinkContext.verboseEnv
262 // DYLD_FORCE_FLAT_NAMESPACE ==> gLinkContext.bindFlat
263 // DYLD_PRINT_INITIALIZERS ==> gLinkContext.verboseInit
264 // DYLD_PRINT_SEGMENTS ==> gLinkContext.verboseMapping
265 // DYLD_PRINT_BINDINGS ==> gLinkContext.verboseBind
266 // DYLD_PRINT_WEAK_BINDINGS ==> gLinkContext.verboseWeakBind
267 // DYLD_PRINT_REBASINGS ==> gLinkContext.verboseRebase
268 // DYLD_PRINT_DOFS ==> gLinkContext.verboseDOF
269 // DYLD_PRINT_APIS ==> gLogAPIs
270 // DYLD_IGNORE_PREBINDING ==> gLinkContext.prebindUsage
271 // DYLD_PREBIND_DEBUG ==> gLinkContext.verbosePrebinding
272 // DYLD_NEW_LOCAL_SHARED_REGIONS ==> gLinkContext.sharedRegionMode
273 // DYLD_SHARED_REGION ==> gLinkContext.sharedRegionMode
274 // DYLD_PRINT_WARNINGS ==> gLinkContext.verboseWarnings
275 // DYLD_PRINT_RPATHS ==> gLinkContext.verboseRPaths
276 // DYLD_PRINT_INTERPOSING ==> gLinkContext.verboseInterposing
277 // DYLD_PRINT_LIBRARIES ==> gLinkContext.verboseLoading
282 typedef std::vector
<dyld_image_state_change_handler
> StateHandlers
;
285 enum EnvVarMode
{ envNone
, envPrintOnly
, envAll
};
288 static const char* sExecPath
= NULL
;
289 static const char* sExecShortName
= NULL
;
290 static const macho_header
* sMainExecutableMachHeader
= NULL
;
291 static uintptr_t sMainExecutableSlide
= 0;
292 #if CPU_SUBTYPES_SUPPORTED
293 static cpu_type_t sHostCPU
;
294 static cpu_subtype_t sHostCPUsubtype
;
296 static ImageLoaderMachO
* sMainExecutable
= NULL
;
297 static size_t sInsertedDylibCount
= 0;
298 static std::vector
<ImageLoader
*> sAllImages
;
299 static std::vector
<ImageLoader
*> sImageRoots
;
300 static std::vector
<ImageLoader
*> sImageFilesNeedingTermination
;
301 static std::vector
<RegisteredDOF
> sImageFilesNeedingDOFUnregistration
;
302 static std::vector
<ImageCallback
> sAddImageCallbacks
;
303 static std::vector
<ImageCallback
> sRemoveImageCallbacks
;
304 static std::vector
<LoadImageCallback
> sAddLoadImageCallbacks
;
305 static bool sRemoveImageCallbacksInUse
= false;
306 static void* sSingleHandlers
[7][3];
307 static void* sBatchHandlers
[7][3];
308 static ImageLoader
* sLastImageByAddressCache
;
309 static EnvironmentVariables sEnv
;
310 #if __MAC_OS_X_VERSION_MIN_REQUIRED
311 static const char* sFrameworkFallbackPaths
[] = { "$HOME/Library/Frameworks", "/Library/Frameworks", "/Network/Library/Frameworks", "/System/Library/Frameworks", NULL
};
312 static const char* sLibraryFallbackPaths
[] = { "$HOME/lib", "/usr/local/lib", "/usr/lib", NULL
};
313 static const char* sRestrictedFrameworkFallbackPaths
[] = { "/System/Library/Frameworks", NULL
};
314 static const char* sRestrictedLibraryFallbackPaths
[] = { "/usr/lib", NULL
};
316 static const char* sFrameworkFallbackPaths
[] = { "/System/Library/Frameworks", NULL
};
317 static const char* sLibraryFallbackPaths
[] = { "/usr/local/lib", "/usr/lib", NULL
};
319 static UndefinedHandler sUndefinedHandler
= NULL
;
320 static ImageLoader
* sBundleBeingLoaded
= NULL
; // hack until OFI is reworked
321 static dyld3::SharedCacheLoadInfo sSharedCacheLoadInfo
;
322 static const char* sSharedCacheOverrideDir
;
323 bool gSharedCacheOverridden
= false;
324 ImageLoader::LinkContext gLinkContext
;
325 bool gLogAPIs
= false;
326 #if SUPPORT_ACCELERATE_TABLES
327 bool gLogAppAPIs
= false;
329 const struct LibSystemHelpers
* gLibSystemHelpers
= NULL
;
330 #if SUPPORT_OLD_CRT_INITIALIZATION
331 bool gRunInitializersOldWay
= false;
333 static std::vector
<DylibOverride
> sDylibOverrides
;
334 #if !TARGET_IPHONE_SIMULATOR
335 static int sLogSocket
= -1;
337 static bool sFrameworksFoundAsDylibs
= false;
338 #if __x86_64__ && !TARGET_IPHONE_SIMULATOR
339 static bool sHaswell
= false;
341 static std::vector
<ImageLoader::DynamicReference
> sDynamicReferences
;
342 static OSSpinLock sDynamicReferencesLock
= 0;
343 #if !TARGET_IPHONE_SIMULATOR
344 static bool sLogToFile
= false;
346 static char sLoadingCrashMessage
[1024] = "dyld: launch, loading dependent libraries";
347 static _dyld_objc_notify_mapped sNotifyObjCMapped
;
348 static _dyld_objc_notify_init sNotifyObjCInit
;
349 static _dyld_objc_notify_unmapped sNotifyObjCUnmapped
;
351 #if __IPHONE_OS_VERSION_MIN_REQUIRED && !TARGET_IPHONE_SIMULATOR
352 static bool sForceStderr
= false;
356 #if SUPPORT_ACCELERATE_TABLES
357 static ImageLoaderMegaDylib
* sAllCacheImagesProxy
= NULL
;
358 static bool sDisableAcceleratorTables
= false;
361 bool gUseDyld3
= false;
362 static bool sSkipMain
= false;
363 static bool sEnableClosures
= false;
364 static uint64_t launchTraceID
= 0;
367 // The MappedRanges structure is used for fast address->image lookups.
368 // The table is only updated when the dyld lock is held, so we don't
369 // need to worry about multiple writers. But readers may look at this
370 // data without holding the lock. Therefore, all updates must be done
371 // in an order that will never cause readers to see inconsistent data.
372 // The general rule is that if the image field is non-NULL then
373 // the other fields are valid.
386 static MappedRanges
* sMappedRangesStart
;
388 void addMappedRange(ImageLoader
* image
, uintptr_t start
, uintptr_t end
)
390 //dyld::log("addMappedRange(0x%lX->0x%lX) for %s\n", start, end, image->getShortName());
391 for (MappedRanges
* p
= sMappedRangesStart
; p
!= NULL
; p
= p
->next
) {
392 for (unsigned long i
=0; i
< p
->count
; ++i
) {
393 if ( p
->array
[i
].image
== NULL
) {
394 p
->array
[i
].start
= start
;
395 p
->array
[i
].end
= end
;
396 // add image field last with a barrier so that any reader will see consistent records
398 p
->array
[i
].image
= image
;
403 // table must be full, chain another
404 #if SUPPORT_ACCELERATE_TABLES
405 unsigned count
= (sAllCacheImagesProxy
!= NULL
) ? 16 : 400;
407 unsigned count
= 400;
409 size_t allocationSize
= sizeof(MappedRanges
) + (count
-1)*3*sizeof(void*);
410 MappedRanges
* newRanges
= (MappedRanges
*)malloc(allocationSize
);
411 bzero(newRanges
, allocationSize
);
412 newRanges
->count
= count
;
413 newRanges
->array
[0].start
= start
;
414 newRanges
->array
[0].end
= end
;
415 newRanges
->array
[0].image
= image
;
417 if ( sMappedRangesStart
== NULL
) {
418 sMappedRangesStart
= newRanges
;
421 for (MappedRanges
* p
= sMappedRangesStart
; p
!= NULL
; p
= p
->next
) {
422 if ( p
->next
== NULL
) {
431 void removedMappedRanges(ImageLoader
* image
)
433 for (MappedRanges
* p
= sMappedRangesStart
; p
!= NULL
; p
= p
->next
) {
434 for (unsigned long i
=0; i
< p
->count
; ++i
) {
435 if ( p
->array
[i
].image
== image
) {
436 // clear with a barrier so that any reader will see consistent records
438 p
->array
[i
].image
= NULL
;
444 ImageLoader
* findMappedRange(uintptr_t target
)
446 for (MappedRanges
* p
= sMappedRangesStart
; p
!= NULL
; p
= p
->next
) {
447 for (unsigned long i
=0; i
< p
->count
; ++i
) {
448 if ( p
->array
[i
].image
!= NULL
) {
449 if ( (p
->array
[i
].start
<= target
) && (target
< p
->array
[i
].end
) )
450 return p
->array
[i
].image
;
459 const char* mkstringf(const char* format
, ...)
461 _SIMPLE_STRING buf
= _simple_salloc();
464 va_start(list
, format
);
465 _simple_vsprintf(buf
, format
, list
);
467 const char* t
= strdup(_simple_string(buf
));
472 return "mkstringf, out of memory error";
476 void throwf(const char* format
, ...)
478 _SIMPLE_STRING buf
= _simple_salloc();
481 va_start(list
, format
);
482 _simple_vsprintf(buf
, format
, list
);
484 const char* t
= strdup(_simple_string(buf
));
489 throw "throwf, out of memory error";
493 #if !TARGET_IPHONE_SIMULATOR
494 static int sLogfile
= STDERR_FILENO
;
497 #if !TARGET_IPHONE_SIMULATOR
498 // based on CFUtilities.c: also_do_stderr()
499 static bool useSyslog()
501 // Use syslog() for processes managed by launchd
502 static bool launchdChecked
= false;
503 static bool launchdOwned
= false;
504 if ( !launchdChecked
&& gProcessInfo
->libSystemInitialized
) {
505 if ( (gLibSystemHelpers
!= NULL
) && (gLibSystemHelpers
->version
>= 11) ) {
506 // <rdar://problem/23520449> only call isLaunchdOwned() after libSystem is initialized
507 launchdOwned
= (*gLibSystemHelpers
->isLaunchdOwned
)();
508 launchdChecked
= true;
511 if ( launchdChecked
&& launchdOwned
)
514 // If stderr is not available, use syslog()
516 int result
= fstat(STDERR_FILENO
, &sb
);
518 return true; // file descriptor 2 is closed
524 static void socket_syslogv(int priority
, const char* format
, va_list list
)
526 // lazily create socket and connection to syslogd
527 if ( sLogSocket
== -1 ) {
528 sLogSocket
= ::socket(AF_UNIX
, SOCK_DGRAM
, 0);
529 if (sLogSocket
== -1)
530 return; // cannot log
531 ::fcntl(sLogSocket
, F_SETFD
, 1);
533 struct sockaddr_un addr
;
534 addr
.sun_family
= AF_UNIX
;
535 strncpy(addr
.sun_path
, _PATH_LOG
, sizeof(addr
.sun_path
));
536 if ( ::connect(sLogSocket
, (struct sockaddr
*)&addr
, sizeof(addr
)) == -1 ) {
543 // format message to syslogd like: "<priority>Process[pid]: message"
544 _SIMPLE_STRING buf
= _simple_salloc();
547 if ( _simple_sprintf(buf
, "<%d>%s[%d]: ", LOG_USER
|LOG_NOTICE
, sExecShortName
, getpid()) == 0 ) {
548 if ( _simple_vsprintf(buf
, format
, list
) == 0 ) {
549 const char* p
= _simple_string(buf
);
550 ::__sendto(sLogSocket
, p
, strlen(p
), 0, NULL
, 0);
558 void vlog(const char* format
, va_list list
)
560 #if __IPHONE_OS_VERSION_MIN_REQUIRED && !TARGET_IPHONE_SIMULATOR
561 // <rdar://problem/25965832> log to console when running iOS app from Xcode
562 if ( !sLogToFile
&& !sForceStderr
&& useSyslog() )
564 if ( !sLogToFile
&& useSyslog() )
566 socket_syslogv(LOG_ERR
, format
, list
);
568 _simple_vdprintf(sLogfile
, format
, list
);
572 void log(const char* format
, ...)
575 va_start(list
, format
);
581 void vwarn(const char* format
, va_list list
)
583 _simple_dprintf(sLogfile
, "dyld: warning, ");
584 _simple_vdprintf(sLogfile
, format
, list
);
587 void warn(const char* format
, ...)
590 va_start(list
, format
);
596 extern void vlog(const char* format
, va_list list
);
597 #endif // !TARGET_IPHONE_SIMULATOR
600 // <rdar://problem/8867781> control access to sAllImages through a lock
601 // because global dyld lock is not held during initialization phase of dlopen()
602 // <rdar://problem/16145518> Use OSSpinLockLock to allow yielding
603 static OSSpinLock sAllImagesLock
= 0;
605 static void allImagesLock()
607 OSSpinLockLock(&sAllImagesLock
);
610 static void allImagesUnlock()
612 OSSpinLockUnlock(&sAllImagesLock
);
616 // utility class to assure files are closed when an exception is thrown
619 FileOpener(const char* path
);
621 int getFileDescriptor() { return fd
; }
626 FileOpener::FileOpener(const char* path
)
629 fd
= my_open(path
, O_RDONLY
, 0);
632 FileOpener::~FileOpener()
639 static void registerDOFs(const std::vector
<ImageLoader::DOFInfo
>& dofs
)
641 const size_t dofSectionCount
= dofs
.size();
642 if ( !sEnv
.DYLD_DISABLE_DOFS
&& (dofSectionCount
!= 0) ) {
643 int fd
= open("/dev/" DTRACEMNR_HELPER
, O_RDWR
);
645 //dyld::warn("can't open /dev/" DTRACEMNR_HELPER " to register dtrace DOF sections\n");
648 // allocate a buffer on the stack for the variable length dof_ioctl_data_t type
649 uint8_t buffer
[sizeof(dof_ioctl_data_t
) + dofSectionCount
*sizeof(dof_helper_t
)];
650 dof_ioctl_data_t
* ioctlData
= (dof_ioctl_data_t
*)buffer
;
652 // fill in buffer with one dof_helper_t per DOF section
653 ioctlData
->dofiod_count
= dofSectionCount
;
654 for (unsigned int i
=0; i
< dofSectionCount
; ++i
) {
655 strlcpy(ioctlData
->dofiod_helpers
[i
].dofhp_mod
, dofs
[i
].imageShortName
, DTRACE_MODNAMELEN
);
656 ioctlData
->dofiod_helpers
[i
].dofhp_dof
= (uintptr_t)(dofs
[i
].dof
);
657 ioctlData
->dofiod_helpers
[i
].dofhp_addr
= (uintptr_t)(dofs
[i
].dof
);
660 // tell kernel about all DOF sections en mas
661 // pass pointer to ioctlData because ioctl() only copies a fixed size amount of data into kernel
662 user_addr_t val
= (user_addr_t
)(unsigned long)ioctlData
;
663 if ( ioctl(fd
, DTRACEHIOC_ADDDOF
, &val
) != -1 ) {
664 // kernel returns a unique identifier for each section in the dofiod_helpers[].dofhp_dof field.
665 for (unsigned int i
=0; i
< dofSectionCount
; ++i
) {
667 info
.mh
= dofs
[i
].imageHeader
;
668 info
.registrationID
= (int)(ioctlData
->dofiod_helpers
[i
].dofhp_dof
);
669 sImageFilesNeedingDOFUnregistration
.push_back(info
);
670 if ( gLinkContext
.verboseDOF
) {
671 dyld::log("dyld: registering DOF section %p in %s with dtrace, ID=0x%08X\n",
672 dofs
[i
].dof
, dofs
[i
].imageShortName
, info
.registrationID
);
677 //dyld::log( "dyld: ioctl to register dtrace DOF section failed\n");
684 static void unregisterDOF(int registrationID
)
686 int fd
= open("/dev/" DTRACEMNR_HELPER
, O_RDWR
);
688 dyld::warn("can't open /dev/" DTRACEMNR_HELPER
" to unregister dtrace DOF section\n");
691 ioctl(fd
, DTRACEHIOC_REMOVE
, registrationID
);
693 if ( gLinkContext
.verboseInit
)
694 dyld::warn("unregistering DOF section ID=0x%08X with dtrace\n", registrationID
);
700 // _dyld_register_func_for_add_image() is implemented as part of the general image state change notification
702 static void notifyAddImageCallbacks(ImageLoader
* image
)
704 // use guard so that we cannot notify about the same image twice
705 if ( ! image
->addFuncNotified() ) {
706 for (std::vector
<ImageCallback
>::iterator it
=sAddImageCallbacks
.begin(); it
!= sAddImageCallbacks
.end(); it
++) {
707 dyld3::ScopedTimer
timer(DBG_DYLD_TIMING_FUNC_FOR_ADD_IMAGE
, (uint64_t)image
->machHeader(), (uint64_t)(*it
), 0);
708 (*it
)(image
->machHeader(), image
->getSlide());
710 for (LoadImageCallback func
: sAddLoadImageCallbacks
) {
711 dyld3::ScopedTimer
timer(DBG_DYLD_TIMING_FUNC_FOR_ADD_IMAGE
, (uint64_t)image
->machHeader(), (uint64_t)(*func
), 0);
712 (*func
)(image
->machHeader(), image
->getPath(), !image
->neverUnload());
714 image
->setAddFuncNotified();
720 // notify gdb about these new images
721 static const char* updateAllImages(enum dyld_image_states state
, uint32_t infoCount
, const struct dyld_image_info info
[])
723 // <rdar://problem/8812589> don't add images without paths to all-image-info-list
724 if ( info
[0].imageFilePath
!= NULL
)
725 addImagesToAllImages(infoCount
, info
);
730 static StateHandlers
* stateToHandlers(dyld_image_states state
, void* handlersArray
[7][3])
733 case dyld_image_state_mapped
:
734 return reinterpret_cast<StateHandlers
*>(&handlersArray
[0]);
736 case dyld_image_state_dependents_mapped
:
737 return reinterpret_cast<StateHandlers
*>(&handlersArray
[1]);
739 case dyld_image_state_rebased
:
740 return reinterpret_cast<StateHandlers
*>(&handlersArray
[2]);
742 case dyld_image_state_bound
:
743 return reinterpret_cast<StateHandlers
*>(&handlersArray
[3]);
745 case dyld_image_state_dependents_initialized
:
746 return reinterpret_cast<StateHandlers
*>(&handlersArray
[4]);
748 case dyld_image_state_initialized
:
749 return reinterpret_cast<StateHandlers
*>(&handlersArray
[5]);
751 case dyld_image_state_terminated
:
752 return reinterpret_cast<StateHandlers
*>(&handlersArray
[6]);
757 #if SUPPORT_ACCELERATE_TABLES
758 static dyld_image_state_change_handler
getPreInitNotifyHandler(unsigned index
)
760 std::vector
<dyld_image_state_change_handler
>* handlers
= stateToHandlers(dyld_image_state_dependents_initialized
, sSingleHandlers
);
761 if ( index
>= handlers
->size() )
763 return (*handlers
)[index
];
766 static dyld_image_state_change_handler
getBoundBatchHandler(unsigned index
)
768 std::vector
<dyld_image_state_change_handler
>* handlers
= stateToHandlers(dyld_image_state_bound
, sBatchHandlers
);
769 if ( index
>= handlers
->size() )
771 return (*handlers
)[index
];
774 static void notifySingleFromCache(dyld_image_states state
, const mach_header
* mh
, const char* path
)
776 //dyld::log("notifySingle(state=%d, image=%s)\n", state, image->getPath());
777 std::vector
<dyld_image_state_change_handler
>* handlers
= stateToHandlers(state
, sSingleHandlers
);
778 if ( handlers
!= NULL
) {
779 dyld_image_info info
;
780 info
.imageLoadAddress
= mh
;
781 info
.imageFilePath
= path
;
782 info
.imageFileModDate
= 0;
783 for (dyld_image_state_change_handler handler
: *handlers
) {
784 const char* result
= (*handler
)(state
, 1, &info
);
785 if ( (result
!= NULL
) && (state
== dyld_image_state_mapped
) ) {
786 //fprintf(stderr, " image rejected by handler=%p\n", *it);
787 // make copy of thrown string so that later catch clauses can free it
788 const char* str
= strdup(result
);
793 if ( (state
== dyld_image_state_dependents_initialized
) && (sNotifyObjCInit
!= NULL
) && (mh
->flags
& MH_HAS_OBJC
) ) {
794 dyld3::ScopedTimer
timer(DBG_DYLD_TIMING_OBJC_INIT
, (uint64_t)mh
, 0, 0);
795 (*sNotifyObjCInit
)(path
, mh
);
800 #if !TARGET_OS_SIMULATOR
801 static void sendMessage(unsigned portSlot
, mach_msg_id_t msgId
, mach_msg_size_t sendSize
, mach_msg_header_t
* buffer
, mach_msg_size_t bufferSize
) {
802 // Allocate a port to listen on in this monitoring task
803 mach_port_t sendPort
= dyld::gProcessInfo
->notifyPorts
[portSlot
];
804 if (sendPort
== MACH_PORT_NULL
) {
807 mach_port_t replyPort
= MACH_PORT_NULL
;
808 mach_port_options_t options
= { .flags
= MPO_CONTEXT_AS_GUARD
| MPO_STRICT
,
810 kern_return_t kr
= mach_port_construct(mach_task_self(), &options
, (mach_port_context_t
)&replyPort
, &replyPort
);
811 if (kr
!= KERN_SUCCESS
) {
814 // Assemble a message
815 mach_msg_header_t
* h
= buffer
;
816 h
->msgh_bits
= MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND
,MACH_MSG_TYPE_MAKE_SEND_ONCE
);
818 h
->msgh_local_port
= replyPort
;
819 h
->msgh_remote_port
= sendPort
;
820 h
->msgh_reserved
= 0;
821 h
->msgh_size
= sendSize
;
822 kr
= mach_msg(h
, MACH_SEND_MSG
| MACH_RCV_MSG
, h
->msgh_size
, bufferSize
, replyPort
, 0, MACH_PORT_NULL
);
824 if ( kr
== MACH_SEND_INVALID_DEST
) {
825 if (OSAtomicCompareAndSwap32(sendPort
, 0, (volatile int32_t*)&dyld::gProcessInfo
->notifyPorts
[portSlot
])) {
826 mach_port_deallocate(mach_task_self(), sendPort
);
829 mach_port_destruct(mach_task_self(), replyPort
, 0, (mach_port_context_t
)&replyPort
);
832 static void notifyMonitoringDyld(bool unloading
, unsigned imageCount
, const struct mach_header
* loadAddresses
[],
833 const char* imagePaths
[])
835 dyld3::ScopedTimer(DBG_DYLD_REMOTE_IMAGE_NOTIFIER
, 0, 0, 0);
836 for (int slot
=0; slot
< DYLD_MAX_PROCESS_INFO_NOTIFY_COUNT
; ++slot
) {
837 if ( dyld::gProcessInfo
->notifyPorts
[slot
] == 0) continue;
838 unsigned entriesSize
= imageCount
*sizeof(dyld_process_info_image_entry
);
839 unsigned pathsSize
= 0;
840 for (unsigned j
=0; j
< imageCount
; ++j
) {
841 pathsSize
+= (strlen(imagePaths
[j
]) + 1);
843 unsigned totalSize
= (sizeof(dyld_process_info_notify_header
) + entriesSize
+ pathsSize
+ 127) & -128; // align
844 if ( totalSize
> DYLD_PROCESS_INFO_NOTIFY_MAX_BUFFER_SIZE
) {
845 // Putting all image paths into one message would make buffer too big.
846 // Instead split into two messages. Recurse as needed until paths fit in buffer.
847 unsigned imageHalfCount
= imageCount
/2;
848 notifyMonitoringDyld(unloading
, imageHalfCount
, loadAddresses
, imagePaths
);
849 notifyMonitoringDyld(unloading
, imageCount
- imageHalfCount
, &loadAddresses
[imageHalfCount
], &imagePaths
[imageHalfCount
]);
852 uint8_t buffer
[totalSize
+ MAX_TRAILER_SIZE
];
853 dyld_process_info_notify_header
* header
= (dyld_process_info_notify_header
*)buffer
;
855 header
->imageCount
= imageCount
;
856 header
->imagesOffset
= sizeof(dyld_process_info_notify_header
);
857 header
->stringsOffset
= sizeof(dyld_process_info_notify_header
) + entriesSize
;
858 header
->timestamp
= dyld::gProcessInfo
->infoArrayChangeTimestamp
;
859 dyld_process_info_image_entry
* entries
= (dyld_process_info_image_entry
*)&buffer
[header
->imagesOffset
];
860 char* const pathPoolStart
= (char*)&buffer
[header
->stringsOffset
];
861 char* pathPool
= pathPoolStart
;
862 for (unsigned j
=0; j
< imageCount
; ++j
) {
863 strcpy(pathPool
, imagePaths
[j
]);
864 uint32_t len
= (uint32_t)strlen(pathPool
);
865 bzero(entries
->uuid
, 16);
866 dyld3::MachOFile
* mf
= (dyld3::MachOFile
*)loadAddresses
[j
];
867 mf
->getUuid(entries
->uuid
);
868 entries
->loadAddress
= (uint64_t)loadAddresses
[j
];
869 entries
->pathStringOffset
= (uint32_t)(pathPool
- pathPoolStart
);
870 entries
->pathLength
= len
;
871 pathPool
+= (len
+1);
875 sendMessage(slot
, DYLD_PROCESS_INFO_NOTIFY_UNLOAD_ID
, totalSize
, (mach_msg_header_t
*)buffer
, totalSize
+MAX_TRAILER_SIZE
);
877 sendMessage(slot
, DYLD_PROCESS_INFO_NOTIFY_LOAD_ID
, totalSize
, (mach_msg_header_t
*)buffer
, totalSize
+MAX_TRAILER_SIZE
);
882 static void notifyMonitoringDyldMain()
884 dyld3::ScopedTimer(DBG_DYLD_REMOTE_IMAGE_NOTIFIER
, 0, 0, 0);
885 for (int slot
=0; slot
< DYLD_MAX_PROCESS_INFO_NOTIFY_COUNT
; ++slot
) {
886 if ( dyld::gProcessInfo
->notifyPorts
[slot
] == 0) continue;
887 uint8_t buffer
[sizeof(mach_msg_header_t
) + MAX_TRAILER_SIZE
];
888 sendMessage(slot
, DYLD_PROCESS_INFO_NOTIFY_MAIN_ID
, sizeof(mach_msg_header_t
), (mach_msg_header_t
*)buffer
, sizeof(mach_msg_header_t
) + MAX_TRAILER_SIZE
);
892 extern void notifyMonitoringDyldMain() VIS_HIDDEN
;
893 extern void notifyMonitoringDyld(bool unloading
, unsigned imageCount
, const struct mach_header
* loadAddresses
[],
894 const char* imagePaths
[]) VIS_HIDDEN
;
897 void notifyKernel(const ImageLoader
& image
, bool loading
) {
898 if ( !image
.inSharedCache() ) {
899 uint32_t baseCode
= loading
? DBG_DYLD_UUID_MAP_A
: DBG_DYLD_UUID_UNMAP_A
;
901 ino_t inode
= image
.getInode();
903 dyld3::kdebug_trace_dyld_image(baseCode
, (const uuid_t
*)&uuid
, *(fsobj_id_t
*)&inode
, {{ image
.getDevice(), 0 }}, image
.machHeader());
907 static void notifySingle(dyld_image_states state
, const ImageLoader
* image
, ImageLoader::InitializerTimingList
* timingInfo
)
909 //dyld::log("notifySingle(state=%d, image=%s)\n", state, image->getPath());
910 std::vector
<dyld_image_state_change_handler
>* handlers
= stateToHandlers(state
, sSingleHandlers
);
911 if ( handlers
!= NULL
) {
912 dyld_image_info info
;
913 info
.imageLoadAddress
= image
->machHeader();
914 info
.imageFilePath
= image
->getRealPath();
915 info
.imageFileModDate
= image
->lastModified();
916 for (std::vector
<dyld_image_state_change_handler
>::iterator it
= handlers
->begin(); it
!= handlers
->end(); ++it
) {
917 const char* result
= (*it
)(state
, 1, &info
);
918 if ( (result
!= NULL
) && (state
== dyld_image_state_mapped
) ) {
919 //fprintf(stderr, " image rejected by handler=%p\n", *it);
920 // make copy of thrown string so that later catch clauses can free it
921 const char* str
= strdup(result
);
926 if ( state
== dyld_image_state_mapped
) {
927 // <rdar://problem/7008875> Save load addr + UUID for images from outside the shared cache
928 if ( !image
->inSharedCache() ) {
930 if ( image
->getUUID(info
.imageUUID
) ) {
931 info
.imageLoadAddress
= image
->machHeader();
932 addNonSharedCacheImageUUID(info
);
936 if ( (state
== dyld_image_state_dependents_initialized
) && (sNotifyObjCInit
!= NULL
) && image
->notifyObjC() ) {
937 uint64_t t0
= mach_absolute_time();
938 dyld3::ScopedTimer
timer(DBG_DYLD_TIMING_OBJC_INIT
, (uint64_t)image
->machHeader(), 0, 0);
939 (*sNotifyObjCInit
)(image
->getRealPath(), image
->machHeader());
940 uint64_t t1
= mach_absolute_time();
941 uint64_t t2
= mach_absolute_time();
942 uint64_t timeInObjC
= t1
-t0
;
943 uint64_t emptyTime
= (t2
-t1
)*100;
944 if ( (timeInObjC
> emptyTime
) && (timingInfo
!= NULL
) ) {
945 timingInfo
->addTime(image
->getShortName(), timeInObjC
);
948 // mach message csdlc about dynamically unloaded images
949 if ( image
->addFuncNotified() && (state
== dyld_image_state_terminated
) ) {
950 notifyKernel(*image
, false);
951 const struct mach_header
* loadAddress
[] = { image
->machHeader() };
952 const char* loadPath
[] = { image
->getPath() };
953 notifyMonitoringDyld(true, 1, loadAddress
, loadPath
);
959 // Normally, dyld_all_image_infos is only updated in batches after an entire
960 // graph is loaded. But if there is an error loading the initial set of
961 // dylibs needed by the main executable, dyld_all_image_infos is not yet set
962 // up, leading to usually brief crash logs.
964 // This function manually adds the images loaded so far to dyld::gProcessInfo.
965 // It should only be called before terminating.
969 for (std::vector
<ImageLoader
*>::iterator it
=sAllImages
.begin(); it
!= sAllImages
.end(); ++it
) {
970 dyld_image_info info
;
971 ImageLoader
* image
= *it
;
972 info
.imageLoadAddress
= image
->machHeader();
973 info
.imageFilePath
= image
->getRealPath();
974 info
.imageFileModDate
= image
->lastModified();
975 // add to all_image_infos if not already there
977 int existingCount
= dyld::gProcessInfo
->infoArrayCount
;
978 const dyld_image_info
* existing
= dyld::gProcessInfo
->infoArray
;
979 if ( existing
!= NULL
) {
980 for (int i
=0; i
< existingCount
; ++i
) {
981 if ( existing
[i
].imageLoadAddress
== info
.imageLoadAddress
) {
982 //dyld::log("not adding %s\n", info.imageFilePath);
989 //dyld::log("adding %s\n", info.imageFilePath);
990 addImagesToAllImages(1, &info
);
996 static int imageSorter(const void* l
, const void* r
)
998 const ImageLoader
* left
= *((ImageLoader
**)l
);
999 const ImageLoader
* right
= *((ImageLoader
**)r
);
1000 return left
->compare(right
);
1003 static void notifyBatchPartial(dyld_image_states state
, bool orLater
, dyld_image_state_change_handler onlyHandler
, bool preflightOnly
, bool onlyObjCMappedNotification
)
1005 std::vector
<dyld_image_state_change_handler
>* handlers
= stateToHandlers(state
, sBatchHandlers
);
1006 if ( (handlers
!= NULL
) || ((state
== dyld_image_state_bound
) && (sNotifyObjCMapped
!= NULL
)) ) {
1007 // don't use a vector because it will use malloc/free and we want notifcation to be low cost
1009 dyld_image_info infos
[allImagesCount()+1];
1010 ImageLoader
* images
[allImagesCount()+1];
1011 ImageLoader
** end
= images
;
1012 for (std::vector
<ImageLoader
*>::iterator it
=sAllImages
.begin(); it
!= sAllImages
.end(); it
++) {
1013 dyld_image_states imageState
= (*it
)->getState();
1014 if ( (imageState
== state
) || (orLater
&& (imageState
> state
)) )
1017 if ( sBundleBeingLoaded
!= NULL
) {
1018 dyld_image_states imageState
= sBundleBeingLoaded
->getState();
1019 if ( (imageState
== state
) || (orLater
&& (imageState
> state
)) )
1020 *end
++ = sBundleBeingLoaded
;
1022 const char* dontLoadReason
= NULL
;
1023 uint32_t imageCount
= (uint32_t)(end
-images
);
1024 if ( imageCount
!= 0 ) {
1026 qsort(images
, imageCount
, sizeof(ImageLoader
*), &imageSorter
);
1028 for (unsigned int i
=0; i
< imageCount
; ++i
) {
1029 dyld_image_info
* p
= &infos
[i
];
1030 ImageLoader
* image
= images
[i
];
1031 //dyld::log(" state=%d, name=%s\n", state, image->getPath());
1032 p
->imageLoadAddress
= image
->machHeader();
1033 p
->imageFilePath
= image
->getRealPath();
1034 p
->imageFileModDate
= image
->lastModified();
1035 // get these registered with the kernel as early as possible
1036 if ( state
== dyld_image_state_dependents_mapped
)
1037 notifyKernel(*image
, true);
1038 // special case for add_image hook
1039 if ( state
== dyld_image_state_bound
)
1040 notifyAddImageCallbacks(image
);
1043 #if SUPPORT_ACCELERATE_TABLES
1044 if ( sAllCacheImagesProxy
!= NULL
) {
1045 unsigned cacheCount
= sAllCacheImagesProxy
->appendImagesToNotify(state
, orLater
, &infos
[imageCount
]);
1046 // support _dyld_register_func_for_add_image()
1047 if ( state
== dyld_image_state_bound
) {
1048 for (ImageCallback callback
: sAddImageCallbacks
) {
1049 for (unsigned i
=0; i
< cacheCount
; ++i
) {
1050 dyld3::ScopedTimer
timer(DBG_DYLD_TIMING_FUNC_FOR_ADD_IMAGE
, (uint64_t)infos
[imageCount
+i
].imageLoadAddress
, (uint64_t)(*callback
), 0);
1051 (*callback
)(infos
[imageCount
+i
].imageLoadAddress
, sSharedCacheLoadInfo
.slide
);
1054 for (LoadImageCallback func
: sAddLoadImageCallbacks
) {
1055 for (unsigned i
=0; i
< cacheCount
; ++i
) {
1056 dyld3::ScopedTimer
timer(DBG_DYLD_TIMING_FUNC_FOR_ADD_IMAGE
, (uint64_t)infos
[imageCount
+i
].imageLoadAddress
, (uint64_t)(*func
), 0);
1057 (*func
)(infos
[imageCount
+i
].imageLoadAddress
, infos
[imageCount
+i
].imageFilePath
, false);
1061 imageCount
+= cacheCount
;
1064 if ( imageCount
!= 0 ) {
1065 if ( !onlyObjCMappedNotification
) {
1066 if ( onlyHandler
!= NULL
) {
1067 const char* result
= NULL
;
1068 if ( result
== NULL
) {
1069 result
= (*onlyHandler
)(state
, imageCount
, infos
);
1071 if ( (result
!= NULL
) && (state
== dyld_image_state_dependents_mapped
) ) {
1072 //fprintf(stderr, " images rejected by handler=%p\n", onlyHandler);
1073 // make copy of thrown string so that later catch clauses can free it
1074 dontLoadReason
= strdup(result
);
1078 // call each handler with whole array
1079 if ( handlers
!= NULL
) {
1080 for (std::vector
<dyld_image_state_change_handler
>::iterator it
= handlers
->begin(); it
!= handlers
->end(); ++it
) {
1081 const char* result
= (*it
)(state
, imageCount
, infos
);
1082 if ( (result
!= NULL
) && (state
== dyld_image_state_dependents_mapped
) ) {
1083 //fprintf(stderr, " images rejected by handler=%p\n", *it);
1084 // make copy of thrown string so that later catch clauses can free it
1085 dontLoadReason
= strdup(result
);
1092 // tell objc about new images
1093 if ( (onlyHandler
== NULL
) && ((state
== dyld_image_state_bound
) || (orLater
&& (dyld_image_state_bound
> state
))) && (sNotifyObjCMapped
!= NULL
) ) {
1094 const char* paths
[imageCount
];
1095 const mach_header
* mhs
[imageCount
];
1096 unsigned objcImageCount
= 0;
1097 for (int i
=0; i
< imageCount
; ++i
) {
1098 const ImageLoader
* image
= findImageByMachHeader(infos
[i
].imageLoadAddress
);
1099 bool hasObjC
= false;
1100 if ( image
!= NULL
) {
1101 hasObjC
= image
->notifyObjC();
1103 #if SUPPORT_ACCELERATE_TABLES
1104 else if ( sAllCacheImagesProxy
!= NULL
) {
1105 const mach_header
* mh
;
1108 if ( sAllCacheImagesProxy
->addressInCache(infos
[i
].imageLoadAddress
, &mh
, &path
, &index
) ) {
1109 hasObjC
= (mh
->flags
& MH_HAS_OBJC
);
1114 paths
[objcImageCount
] = infos
[i
].imageFilePath
;
1115 mhs
[objcImageCount
] = infos
[i
].imageLoadAddress
;
1119 if ( objcImageCount
!= 0 ) {
1120 dyld3::ScopedTimer
timer(DBG_DYLD_TIMING_OBJC_MAP
, 0, 0, 0);
1121 uint64_t t0
= mach_absolute_time();
1122 (*sNotifyObjCMapped
)(objcImageCount
, paths
, mhs
);
1123 uint64_t t1
= mach_absolute_time();
1124 ImageLoader::fgTotalObjCSetupTime
+= (t1
-t0
);
1129 if ( dontLoadReason
!= NULL
)
1130 throw dontLoadReason
;
1131 if ( !preflightOnly
&& (state
== dyld_image_state_dependents_mapped
) ) {
1132 const struct mach_header
* loadAddresses
[imageCount
];
1133 const char* loadPaths
[imageCount
];
1134 for(uint32_t i
= 0; i
<imageCount
; ++i
) {
1135 loadAddresses
[i
] = infos
[i
].imageLoadAddress
;
1136 loadPaths
[i
] = infos
[i
].imageFilePath
;
1138 notifyMonitoringDyld(false, imageCount
, loadAddresses
, loadPaths
);
1143 static void notifyBatch(dyld_image_states state
, bool preflightOnly
)
1145 notifyBatchPartial(state
, false, NULL
, preflightOnly
, false);
1149 void coresymbolication_load_notifier(void* connection
, uint64_t timestamp
, const char* path
, const struct mach_header
* mh
)
1151 const struct mach_header
* loadAddress
[] = { mh
};
1152 const char* loadPath
[] = { path
};
1153 notifyMonitoringDyld(false, 1, loadAddress
, loadPath
);
1157 void coresymbolication_unload_notifier(void* connection
, uint64_t timestamp
, const char* path
, const struct mach_header
* mh
)
1159 const struct mach_header
* loadAddress
= { mh
};
1160 const char* loadPath
= { path
};
1161 notifyMonitoringDyld(true, 1, &loadAddress
, &loadPath
);
1164 // In order for register_func_for_add_image() callbacks to to be called bottom up,
1165 // we need to maintain a list of root images. The main executable is usally the
1166 // first root. Any images dynamically added are also roots (unless already loaded).
1167 // If DYLD_INSERT_LIBRARIES is used, those libraries are first.
1168 static void addRootImage(ImageLoader
* image
)
1170 //dyld::log("addRootImage(%p, %s)\n", image, image->getPath());
1171 // add to list of roots
1172 sImageRoots
.push_back(image
);
1176 static void clearAllDepths()
1178 for (std::vector
<ImageLoader
*>::iterator it
=sAllImages
.begin(); it
!= sAllImages
.end(); it
++)
1179 (*it
)->clearDepth();
1182 static void printAllDepths()
1184 for (std::vector
<ImageLoader
*>::iterator it
=sAllImages
.begin(); it
!= sAllImages
.end(); it
++)
1185 dyld::log("%03d %s\n", (*it
)->getDepth(), (*it
)->getShortName());
1189 static unsigned int imageCount()
1192 unsigned int result
= (unsigned int)sAllImages
.size();
1198 static void setNewProgramVars(const ProgramVars
& newVars
)
1200 // make a copy of the pointers to program variables
1201 gLinkContext
.programVars
= newVars
;
1203 // now set each program global to their initial value
1204 *gLinkContext
.programVars
.NXArgcPtr
= gLinkContext
.argc
;
1205 *gLinkContext
.programVars
.NXArgvPtr
= gLinkContext
.argv
;
1206 *gLinkContext
.programVars
.environPtr
= gLinkContext
.envp
;
1207 *gLinkContext
.programVars
.__prognamePtr
= gLinkContext
.progname
;
1210 #if SUPPORT_OLD_CRT_INITIALIZATION
1211 static void setRunInitialzersOldWay()
1213 gRunInitializersOldWay
= true;
1217 static bool sandboxBlocked(const char* path
, const char* kind
)
1219 #if TARGET_IPHONE_SIMULATOR
1220 // sandbox calls not yet supported in simulator runtime
1223 sandbox_filter_type filter
= (sandbox_filter_type
)(SANDBOX_FILTER_PATH
| SANDBOX_CHECK_NO_REPORT
);
1224 return ( sandbox_check(getpid(), kind
, filter
, path
) > 0 );
1228 bool sandboxBlockedMmap(const char* path
)
1230 return sandboxBlocked(path
, "file-map-executable");
1233 bool sandboxBlockedOpen(const char* path
)
1235 return sandboxBlocked(path
, "file-read-data");
1238 bool sandboxBlockedStat(const char* path
)
1240 return sandboxBlocked(path
, "file-read-metadata");
1244 static void addDynamicReference(ImageLoader
* from
, ImageLoader
* to
) {
1245 // don't add dynamic reference if target is in the shared cache (since it can't be unloaded)
1246 if ( to
->inSharedCache() )
1249 // don't add dynamic reference if there already is a static one
1250 if ( from
->dependsOn(to
) )
1253 // don't add if this combination already exists
1254 OSSpinLockLock(&sDynamicReferencesLock
);
1255 for (std::vector
<ImageLoader::DynamicReference
>::iterator it
=sDynamicReferences
.begin(); it
!= sDynamicReferences
.end(); ++it
) {
1256 if ( (it
->from
== from
) && (it
->to
== to
) ) {
1257 OSSpinLockUnlock(&sDynamicReferencesLock
);
1262 //dyld::log("addDynamicReference(%s, %s\n", from->getShortName(), to->getShortName());
1263 ImageLoader::DynamicReference t
;
1266 sDynamicReferences
.push_back(t
);
1267 OSSpinLockUnlock(&sDynamicReferencesLock
);
1270 static void addImage(ImageLoader
* image
)
1272 // add to master list
1274 sAllImages
.push_back(image
);
1277 // update mapped ranges
1278 uintptr_t lastSegStart
= 0;
1279 uintptr_t lastSegEnd
= 0;
1280 for(unsigned int i
=0, e
=image
->segmentCount(); i
< e
; ++i
) {
1281 if ( image
->segUnaccessible(i
) )
1283 uintptr_t start
= image
->segActualLoadAddress(i
);
1284 uintptr_t end
= image
->segActualEndAddress(i
);
1285 if ( start
== lastSegEnd
) {
1286 // two segments are contiguous, just record combined segments
1290 // non-contiguous segments, record last (if any)
1291 if ( lastSegEnd
!= 0 )
1292 addMappedRange(image
, lastSegStart
, lastSegEnd
);
1293 lastSegStart
= start
;
1297 if ( lastSegEnd
!= 0 )
1298 addMappedRange(image
, lastSegStart
, lastSegEnd
);
1301 if ( gLinkContext
.verboseLoading
|| (sEnv
.DYLD_PRINT_LIBRARIES_POST_LAUNCH
&& (sMainExecutable
!=NULL
) && sMainExecutable
->isLinked()) ) {
1302 dyld::log("dyld: loaded: %s\n", image
->getPath());
1308 // Helper for std::remove_if
1310 class RefUsesImage
{
1312 RefUsesImage(ImageLoader
* image
) : _image(image
) {}
1313 bool operator()(const ImageLoader::DynamicReference
& ref
) const {
1314 return ( (ref
.from
== _image
) || (ref
.to
== _image
) );
1317 ImageLoader
* _image
;
1322 void removeImage(ImageLoader
* image
)
1324 // if has dtrace DOF section, tell dtrace it is going away, then remove from sImageFilesNeedingDOFUnregistration
1325 for (std::vector
<RegisteredDOF
>::iterator it
=sImageFilesNeedingDOFUnregistration
.begin(); it
!= sImageFilesNeedingDOFUnregistration
.end(); ) {
1326 if ( it
->mh
== image
->machHeader() ) {
1327 unregisterDOF(it
->registrationID
);
1328 sImageFilesNeedingDOFUnregistration
.erase(it
);
1329 // don't increment iterator, the erase caused next element to be copied to where this iterator points
1336 // tell all registered remove image handlers about this
1337 // do this before removing image from internal data structures so that the callback can query dyld about the image
1338 if ( image
->getState() >= dyld_image_state_bound
) {
1339 sRemoveImageCallbacksInUse
= true; // This only runs inside dyld's global lock, so ok to use a global for the in-use flag.
1340 for (std::vector
<ImageCallback
>::iterator it
=sRemoveImageCallbacks
.begin(); it
!= sRemoveImageCallbacks
.end(); it
++) {
1341 dyld3::ScopedTimer
timer(DBG_DYLD_TIMING_FUNC_FOR_REMOVE_IMAGE
, (uint64_t)image
->machHeader(), (uint64_t)(*it
), 0);
1342 (*it
)(image
->machHeader(), image
->getSlide());
1344 sRemoveImageCallbacksInUse
= false;
1346 if ( sNotifyObjCUnmapped
!= NULL
&& image
->notifyObjC() )
1347 (*sNotifyObjCUnmapped
)(image
->getRealPath(), image
->machHeader());
1351 notifySingle(dyld_image_state_terminated
, image
, NULL
);
1353 // remove from mapped images table
1354 removedMappedRanges(image
);
1356 // remove from master list
1358 for (std::vector
<ImageLoader
*>::iterator it
=sAllImages
.begin(); it
!= sAllImages
.end(); it
++) {
1359 if ( *it
== image
) {
1360 sAllImages
.erase(it
);
1366 // remove from sDynamicReferences
1367 OSSpinLockLock(&sDynamicReferencesLock
);
1368 sDynamicReferences
.erase(std::remove_if(sDynamicReferences
.begin(), sDynamicReferences
.end(), RefUsesImage(image
)), sDynamicReferences
.end());
1369 OSSpinLockUnlock(&sDynamicReferencesLock
);
1371 // flush find-by-address cache (do this after removed from master list, so there is no chance it can come back)
1372 if ( sLastImageByAddressCache
== image
)
1373 sLastImageByAddressCache
= NULL
;
1375 // if in root list, pull it out
1376 for (std::vector
<ImageLoader
*>::iterator it
=sImageRoots
.begin(); it
!= sImageRoots
.end(); it
++) {
1377 if ( *it
== image
) {
1378 sImageRoots
.erase(it
);
1384 if ( gLinkContext
.verboseLoading
|| (sEnv
.DYLD_PRINT_LIBRARIES_POST_LAUNCH
&& (sMainExecutable
!=NULL
) && sMainExecutable
->isLinked()) ) {
1385 dyld::log("dyld: unloaded: %s\n", image
->getPath());
1388 // tell gdb, new way
1389 removeImageFromAllImages(image
->machHeader());
1393 void runImageStaticTerminators(ImageLoader
* image
)
1395 // if in termination list, pull it out and run terminator
1398 mightBeMore
= false;
1399 for (std::vector
<ImageLoader
*>::iterator it
=sImageFilesNeedingTermination
.begin(); it
!= sImageFilesNeedingTermination
.end(); it
++) {
1400 if ( *it
== image
) {
1401 sImageFilesNeedingTermination
.erase(it
);
1402 if (gLogAPIs
) dyld::log("dlclose(), running static terminators for %p %s\n", image
, image
->getShortName());
1403 image
->doTermination(gLinkContext
);
1408 } while ( mightBeMore
);
1411 static void terminationRecorder(ImageLoader
* image
)
1413 sImageFilesNeedingTermination
.push_back(image
);
1416 const char* getExecutablePath()
1421 static void runAllStaticTerminators(void* extra
)
1424 const size_t imageCount
= sImageFilesNeedingTermination
.size();
1425 for(size_t i
=imageCount
; i
> 0; --i
){
1426 ImageLoader
* image
= sImageFilesNeedingTermination
[i
-1];
1427 image
->doTermination(gLinkContext
);
1429 sImageFilesNeedingTermination
.clear();
1430 notifyBatch(dyld_image_state_terminated
, false);
1432 catch (const char* msg
) {
1437 void initializeMainExecutable()
1439 // record that we've reached this step
1440 gLinkContext
.startedInitializingMainExecutable
= true;
1442 // run initialzers for any inserted dylibs
1443 ImageLoader::InitializerTimingList initializerTimes
[allImagesCount()];
1444 initializerTimes
[0].count
= 0;
1445 const size_t rootCount
= sImageRoots
.size();
1446 if ( rootCount
> 1 ) {
1447 for(size_t i
=1; i
< rootCount
; ++i
) {
1448 sImageRoots
[i
]->runInitializers(gLinkContext
, initializerTimes
[0]);
1452 // run initializers for main executable and everything it brings up
1453 sMainExecutable
->runInitializers(gLinkContext
, initializerTimes
[0]);
1455 // register cxa_atexit() handler to run static terminators in all loaded images when this process exits
1456 if ( gLibSystemHelpers
!= NULL
)
1457 (*gLibSystemHelpers
->cxa_atexit
)(&runAllStaticTerminators
, NULL
, NULL
);
1459 // dump info if requested
1460 if ( sEnv
.DYLD_PRINT_STATISTICS
)
1461 ImageLoader::printStatistics((unsigned int)allImagesCount(), initializerTimes
[0]);
1462 if ( sEnv
.DYLD_PRINT_STATISTICS_DETAILS
)
1463 ImageLoaderMachO::printStatisticsDetails((unsigned int)allImagesCount(), initializerTimes
[0]);
1466 bool mainExecutablePrebound()
1468 return sMainExecutable
->usablePrebinding(gLinkContext
);
1471 ImageLoader
* mainExecutable()
1473 return sMainExecutable
;
1479 #if SUPPORT_VERSIONED_PATHS
1481 // forward reference
1482 static bool getDylibVersionAndInstallname(const char* dylibPath
, uint32_t* version
, char* installName
);
1486 // Examines a dylib file and if its current_version is newer than the installed
1487 // dylib at its install_name, then add the dylib file to sDylibOverrides.
1489 static void checkDylibOverride(const char* dylibFile
)
1491 //dyld::log("checkDylibOverride('%s')\n", dylibFile);
1492 uint32_t altVersion
;
1493 char sysInstallName
[PATH_MAX
];
1494 if ( getDylibVersionAndInstallname(dylibFile
, &altVersion
, sysInstallName
) && (sysInstallName
[0] =='/') ) {
1495 //dyld::log("%s has version 0x%08X and install name %s\n", dylibFile, altVersion, sysInstallName);
1496 uint32_t sysVersion
;
1497 if ( getDylibVersionAndInstallname(sysInstallName
, &sysVersion
, NULL
) ) {
1498 //dyld::log("%s has version 0x%08X\n", sysInstallName, sysVersion);
1499 if ( altVersion
> sysVersion
) {
1500 //dyld::log("override found: %s -> %s\n", sysInstallName, dylibFile);
1501 // see if there already is an override for this dylib
1502 bool entryExists
= false;
1503 for (std::vector
<DylibOverride
>::iterator it
= sDylibOverrides
.begin(); it
!= sDylibOverrides
.end(); ++it
) {
1504 if ( strcmp(it
->installName
, sysInstallName
) == 0 ) {
1506 uint32_t prevVersion
;
1507 if ( getDylibVersionAndInstallname(it
->override
, &prevVersion
, NULL
) ) {
1508 if ( altVersion
> prevVersion
) {
1509 // found an even newer override
1510 free((void*)(it
->override
));
1511 char resolvedPath
[PATH_MAX
];
1512 if ( realpath(dylibFile
, resolvedPath
) != NULL
)
1513 it
->override
= strdup(resolvedPath
);
1515 it
->override
= strdup(dylibFile
);
1521 if ( ! entryExists
) {
1522 DylibOverride entry
;
1523 entry
.installName
= strdup(sysInstallName
);
1524 char resolvedPath
[PATH_MAX
];
1525 if ( realpath(dylibFile
, resolvedPath
) != NULL
)
1526 entry
.override
= strdup(resolvedPath
);
1528 entry
.override
= strdup(dylibFile
);
1529 sDylibOverrides
.push_back(entry
);
1530 //dyld::log("added override: %s -> %s\n", entry.installName, entry.override);
1538 static void checkDylibOverridesInDir(const char* dirPath
)
1540 //dyld::log("checkDylibOverridesInDir('%s')\n", dirPath);
1541 char dylibPath
[PATH_MAX
];
1542 long dirPathLen
= strlcpy(dylibPath
, dirPath
, PATH_MAX
-1);
1543 if ( dirPathLen
>= PATH_MAX
)
1545 DIR* dirp
= opendir(dirPath
);
1546 if ( dirp
!= NULL
) {
1548 dirent
* entp
= NULL
;
1549 while ( readdir_r(dirp
, &entry
, &entp
) == 0 ) {
1552 if ( entp
->d_type
!= DT_REG
)
1554 dylibPath
[dirPathLen
] = '/';
1555 dylibPath
[dirPathLen
+1] = '\0';
1556 if ( strlcat(dylibPath
, entp
->d_name
, PATH_MAX
) >= PATH_MAX
)
1558 checkDylibOverride(dylibPath
);
1565 static void checkFrameworkOverridesInDir(const char* dirPath
)
1567 //dyld::log("checkFrameworkOverridesInDir('%s')\n", dirPath);
1568 char frameworkPath
[PATH_MAX
];
1569 long dirPathLen
= strlcpy(frameworkPath
, dirPath
, PATH_MAX
-1);
1570 if ( dirPathLen
>= PATH_MAX
)
1572 DIR* dirp
= opendir(dirPath
);
1573 if ( dirp
!= NULL
) {
1575 dirent
* entp
= NULL
;
1576 while ( readdir_r(dirp
, &entry
, &entp
) == 0 ) {
1579 if ( entp
->d_type
!= DT_DIR
)
1581 frameworkPath
[dirPathLen
] = '/';
1582 frameworkPath
[dirPathLen
+1] = '\0';
1583 int dirNameLen
= (int)strlen(entp
->d_name
);
1584 if ( dirNameLen
< 11 )
1586 if ( strcmp(&entp
->d_name
[dirNameLen
-10], ".framework") != 0 )
1588 if ( strlcat(frameworkPath
, entp
->d_name
, PATH_MAX
) >= PATH_MAX
)
1590 if ( strlcat(frameworkPath
, "/", PATH_MAX
) >= PATH_MAX
)
1592 if ( strlcat(frameworkPath
, entp
->d_name
, PATH_MAX
) >= PATH_MAX
)
1594 frameworkPath
[strlen(frameworkPath
)-10] = '\0';
1595 checkDylibOverride(frameworkPath
);
1600 #endif // SUPPORT_VERSIONED_PATHS
1604 // Turns a colon separated list of strings into a NULL terminated array
1605 // of string pointers. If mainExecutableDir param is not NULL,
1606 // substitutes @loader_path with main executable's dir.
1608 static const char** parseColonList(const char* list
, const char* mainExecutableDir
)
1610 static const char* sEmptyList
[] = { NULL
};
1612 if ( list
[0] == '\0' )
1616 for(const char* s
=list
; *s
!= '\0'; ++s
) {
1622 const char* start
= list
;
1623 char** result
= new char*[colonCount
+2];
1624 for(const char* s
=list
; *s
!= '\0'; ++s
) {
1626 size_t len
= s
-start
;
1627 if ( (mainExecutableDir
!= NULL
) && (strncmp(start
, "@loader_path/", 13) == 0) ) {
1628 if ( !gLinkContext
.allowAtPaths
) {
1629 dyld::log("dyld: warning: @loader_path/ ignored because of amfi policy\n");
1632 size_t mainExecDirLen
= strlen(mainExecutableDir
);
1633 char* str
= new char[mainExecDirLen
+len
+1];
1634 strcpy(str
, mainExecutableDir
);
1635 strlcat(str
, &start
[13], mainExecDirLen
+len
+1);
1636 str
[mainExecDirLen
+len
-13] = '\0';
1638 result
[index
++] = str
;
1640 else if ( (mainExecutableDir
!= NULL
) && (strncmp(start
, "@executable_path/", 17) == 0) ) {
1641 if ( !gLinkContext
.allowAtPaths
) {
1642 dyld::log("dyld: warning: @executable_path/ ignored because of amfi policy\n");
1645 size_t mainExecDirLen
= strlen(mainExecutableDir
);
1646 char* str
= new char[mainExecDirLen
+len
+1];
1647 strcpy(str
, mainExecutableDir
);
1648 strlcat(str
, &start
[17], mainExecDirLen
+len
+1);
1649 str
[mainExecDirLen
+len
-17] = '\0';
1651 result
[index
++] = str
;
1654 char* str
= new char[len
+1];
1655 strncpy(str
, start
, len
);
1658 result
[index
++] = str
;
1662 size_t len
= strlen(start
);
1663 if ( (mainExecutableDir
!= NULL
) && (strncmp(start
, "@loader_path/", 13) == 0) ) {
1664 if ( !gLinkContext
.allowAtPaths
) {
1665 dyld::log("dyld: warning: @loader_path/ ignored because of amfi policy\n");
1669 size_t mainExecDirLen
= strlen(mainExecutableDir
);
1670 char* str
= new char[mainExecDirLen
+len
+1];
1671 strcpy(str
, mainExecutableDir
);
1672 strlcat(str
, &start
[13], mainExecDirLen
+len
+1);
1673 str
[mainExecDirLen
+len
-13] = '\0';
1674 result
[index
++] = str
;
1677 else if ( (mainExecutableDir
!= NULL
) && (strncmp(start
, "@executable_path/", 17) == 0) ) {
1678 if ( !gLinkContext
.allowAtPaths
) {
1679 dyld::log("dyld: warning: @executable_path/ ignored because of amfi policy\n");
1683 size_t mainExecDirLen
= strlen(mainExecutableDir
);
1684 char* str
= new char[mainExecDirLen
+len
+1];
1685 strcpy(str
, mainExecutableDir
);
1686 strlcat(str
, &start
[17], mainExecDirLen
+len
+1);
1687 str
[mainExecDirLen
+len
-17] = '\0';
1688 result
[index
++] = str
;
1692 char* str
= new char[len
+1];
1694 result
[index
++] = str
;
1696 result
[index
] = NULL
;
1698 //dyld::log("parseColonList(%s)\n", list);
1699 //for(int i=0; result[i] != NULL; ++i)
1700 // dyld::log(" %s\n", result[i]);
1701 return (const char**)result
;
1704 static void appendParsedColonList(const char* list
, const char* mainExecutableDir
, const char* const ** storage
)
1706 const char** newlist
= parseColonList(list
, mainExecutableDir
);
1707 if ( *storage
== NULL
) {
1708 // first time, just set
1712 // need to append to existing list
1713 const char* const* existing
= *storage
;
1715 for(int i
=0; existing
[i
] != NULL
; ++i
)
1717 for(int i
=0; newlist
[i
] != NULL
; ++i
)
1719 const char** combinedList
= new const char*[count
+2];
1721 for(int i
=0; existing
[i
] != NULL
; ++i
)
1722 combinedList
[index
++] = existing
[i
];
1723 for(int i
=0; newlist
[i
] != NULL
; ++i
)
1724 combinedList
[index
++] = newlist
[i
];
1725 combinedList
[index
] = NULL
;
1727 *storage
= combinedList
;
1731 #if __MAC_OS_X_VERSION_MIN_REQUIRED
1732 static void paths_expand_roots(const char **paths
, const char *key
, const char *val
)
1734 // assert(val != NULL);
1735 // assert(paths != NULL);
1737 size_t keyLen
= strlen(key
);
1738 for(int i
=0; paths
[i
] != NULL
; ++i
) {
1739 if ( strncmp(paths
[i
], key
, keyLen
) == 0 ) {
1740 char* newPath
= new char[strlen(val
) + (strlen(paths
[i
]) - keyLen
) + 1];
1741 strcpy(newPath
, val
);
1742 strcat(newPath
, &paths
[i
][keyLen
]);
1750 static void removePathWithPrefix(const char* paths
[], const char* prefix
)
1752 size_t prefixLen
= strlen(prefix
);
1755 for(i
= 0; paths
[i
] != NULL
; ++i
) {
1756 if ( strncmp(paths
[i
], prefix
, prefixLen
) == 0 )
1759 paths
[i
-skip
] = paths
[i
];
1761 paths
[i
-skip
] = NULL
;
1767 static void paths_dump(const char **paths
)
1769 // assert(paths != NULL);
1770 const char **strs
= paths
;
1771 while(*strs
!= NULL
)
1773 dyld::log("\"%s\"\n", *strs
);
1782 static void printOptions(const char* argv
[])
1785 while ( NULL
!= argv
[i
] ) {
1786 dyld::log("opt[%i] = \"%s\"\n", i
, argv
[i
]);
1791 static void printEnvironmentVariables(const char* envp
[])
1793 while ( NULL
!= *envp
) {
1794 dyld::log("%s\n", *envp
);
1799 void processDyldEnvironmentVariable(const char* key
, const char* value
, const char* mainExecutableDir
)
1801 if ( strcmp(key
, "DYLD_FRAMEWORK_PATH") == 0 ) {
1802 appendParsedColonList(value
, mainExecutableDir
, &sEnv
.DYLD_FRAMEWORK_PATH
);
1804 else if ( strcmp(key
, "DYLD_FALLBACK_FRAMEWORK_PATH") == 0 ) {
1805 appendParsedColonList(value
, mainExecutableDir
, &sEnv
.DYLD_FALLBACK_FRAMEWORK_PATH
);
1807 else if ( strcmp(key
, "DYLD_LIBRARY_PATH") == 0 ) {
1808 appendParsedColonList(value
, mainExecutableDir
, &sEnv
.DYLD_LIBRARY_PATH
);
1810 else if ( strcmp(key
, "DYLD_FALLBACK_LIBRARY_PATH") == 0 ) {
1811 appendParsedColonList(value
, mainExecutableDir
, &sEnv
.DYLD_FALLBACK_LIBRARY_PATH
);
1813 #if SUPPORT_ROOT_PATH
1814 else if ( (strcmp(key
, "DYLD_ROOT_PATH") == 0) || (strcmp(key
, "DYLD_PATHS_ROOT") == 0) ) {
1815 if ( strcmp(value
, "/") != 0 ) {
1816 gLinkContext
.rootPaths
= parseColonList(value
, mainExecutableDir
);
1817 for (int i
=0; gLinkContext
.rootPaths
[i
] != NULL
; ++i
) {
1818 if ( gLinkContext
.rootPaths
[i
][0] != '/' ) {
1819 dyld::warn("DYLD_ROOT_PATH not used because it contains a non-absolute path\n");
1820 gLinkContext
.rootPaths
= NULL
;
1827 else if ( strcmp(key
, "DYLD_IMAGE_SUFFIX") == 0 ) {
1828 gLinkContext
.imageSuffix
= parseColonList(value
, NULL
);
1830 else if ( strcmp(key
, "DYLD_INSERT_LIBRARIES") == 0 ) {
1831 sEnv
.DYLD_INSERT_LIBRARIES
= parseColonList(value
, NULL
);
1832 #if SUPPORT_ACCELERATE_TABLES
1833 sDisableAcceleratorTables
= true;
1836 else if ( strcmp(key
, "DYLD_PRINT_OPTS") == 0 ) {
1837 sEnv
.DYLD_PRINT_OPTS
= true;
1839 else if ( strcmp(key
, "DYLD_PRINT_ENV") == 0 ) {
1840 sEnv
.DYLD_PRINT_ENV
= true;
1842 else if ( strcmp(key
, "DYLD_DISABLE_DOFS") == 0 ) {
1843 sEnv
.DYLD_DISABLE_DOFS
= true;
1845 else if ( strcmp(key
, "DYLD_DISABLE_PREFETCH") == 0 ) {
1846 gLinkContext
.preFetchDisabled
= true;
1848 else if ( strcmp(key
, "DYLD_PRINT_LIBRARIES") == 0 ) {
1849 gLinkContext
.verboseLoading
= true;
1851 else if ( strcmp(key
, "DYLD_PRINT_LIBRARIES_POST_LAUNCH") == 0 ) {
1852 sEnv
.DYLD_PRINT_LIBRARIES_POST_LAUNCH
= true;
1854 else if ( strcmp(key
, "DYLD_BIND_AT_LAUNCH") == 0 ) {
1855 sEnv
.DYLD_BIND_AT_LAUNCH
= true;
1857 else if ( strcmp(key
, "DYLD_FORCE_FLAT_NAMESPACE") == 0 ) {
1858 gLinkContext
.bindFlat
= true;
1860 else if ( strcmp(key
, "DYLD_NEW_LOCAL_SHARED_REGIONS") == 0 ) {
1861 // ignore, no longer relevant but some scripts still set it
1863 else if ( strcmp(key
, "DYLD_NO_FIX_PREBINDING") == 0 ) {
1865 else if ( strcmp(key
, "DYLD_PREBIND_DEBUG") == 0 ) {
1866 gLinkContext
.verbosePrebinding
= true;
1868 else if ( strcmp(key
, "DYLD_PRINT_INITIALIZERS") == 0 ) {
1869 gLinkContext
.verboseInit
= true;
1871 else if ( strcmp(key
, "DYLD_PRINT_DOFS") == 0 ) {
1872 gLinkContext
.verboseDOF
= true;
1874 else if ( strcmp(key
, "DYLD_PRINT_STATISTICS") == 0 ) {
1875 sEnv
.DYLD_PRINT_STATISTICS
= true;
1876 #if __IPHONE_OS_VERSION_MIN_REQUIRED && !TARGET_IPHONE_SIMULATOR
1877 // <rdar://problem/26614838> DYLD_PRINT_STATISTICS no longer logs to xcode console for device apps
1878 sForceStderr
= true;
1881 else if ( strcmp(key
, "DYLD_PRINT_TO_STDERR") == 0 ) {
1882 #if __IPHONE_OS_VERSION_MIN_REQUIRED && !TARGET_IPHONE_SIMULATOR
1883 // <rdar://problem/26633440> DYLD_PRINT_STATISTICS no longer logs to xcode console for device apps
1884 sForceStderr
= true;
1887 else if ( strcmp(key
, "DYLD_PRINT_STATISTICS_DETAILS") == 0 ) {
1888 sEnv
.DYLD_PRINT_STATISTICS_DETAILS
= true;
1890 else if ( strcmp(key
, "DYLD_PRINT_SEGMENTS") == 0 ) {
1891 gLinkContext
.verboseMapping
= true;
1893 else if ( strcmp(key
, "DYLD_PRINT_BINDINGS") == 0 ) {
1894 gLinkContext
.verboseBind
= true;
1896 else if ( strcmp(key
, "DYLD_PRINT_WEAK_BINDINGS") == 0 ) {
1897 gLinkContext
.verboseWeakBind
= true;
1899 else if ( strcmp(key
, "DYLD_PRINT_REBASINGS") == 0 ) {
1900 gLinkContext
.verboseRebase
= true;
1902 else if ( strcmp(key
, "DYLD_PRINT_APIS") == 0 ) {
1905 #if SUPPORT_ACCELERATE_TABLES
1906 else if ( strcmp(key
, "DYLD_PRINT_APIS_APP") == 0 ) {
1910 else if ( strcmp(key
, "DYLD_PRINT_WARNINGS") == 0 ) {
1911 gLinkContext
.verboseWarnings
= true;
1913 else if ( strcmp(key
, "DYLD_PRINT_RPATHS") == 0 ) {
1914 gLinkContext
.verboseRPaths
= true;
1916 else if ( strcmp(key
, "DYLD_PRINT_INTERPOSING") == 0 ) {
1917 gLinkContext
.verboseInterposing
= true;
1919 else if ( strcmp(key
, "DYLD_PRINT_CODE_SIGNATURES") == 0 ) {
1920 gLinkContext
.verboseCodeSignatures
= true;
1922 else if ( (strcmp(key
, "DYLD_SHARED_REGION") == 0) && gLinkContext
.allowEnvVarsSharedCache
) {
1923 if ( strcmp(value
, "private") == 0 ) {
1924 gLinkContext
.sharedRegionMode
= ImageLoader::kUsePrivateSharedRegion
;
1926 else if ( strcmp(value
, "avoid") == 0 ) {
1927 gLinkContext
.sharedRegionMode
= ImageLoader::kDontUseSharedRegion
;
1929 else if ( strcmp(value
, "use") == 0 ) {
1930 gLinkContext
.sharedRegionMode
= ImageLoader::kUseSharedRegion
;
1932 else if ( value
[0] == '\0' ) {
1933 gLinkContext
.sharedRegionMode
= ImageLoader::kUseSharedRegion
;
1936 dyld::warn("unknown option to DYLD_SHARED_REGION. Valid options are: use, private, avoid\n");
1939 else if ( (strcmp(key
, "DYLD_SHARED_CACHE_DIR") == 0) && gLinkContext
.allowEnvVarsSharedCache
) {
1940 sSharedCacheOverrideDir
= value
;
1942 else if ( strcmp(key
, "DYLD_USE_CLOSURES") == 0 ) {
1943 if ( dyld3::internalInstall() ) {
1944 #if __MAC_OS_X_VERSION_MIN_REQUIRED && __i386__
1945 // don't support dyld3 for 32-bit macOS
1947 sEnableClosures
= true;
1951 else if ( strcmp(key
, "DYLD_IGNORE_PREBINDING") == 0 ) {
1952 if ( strcmp(value
, "all") == 0 ) {
1953 gLinkContext
.prebindUsage
= ImageLoader::kUseNoPrebinding
;
1955 else if ( strcmp(value
, "app") == 0 ) {
1956 gLinkContext
.prebindUsage
= ImageLoader::kUseAllButAppPredbinding
;
1958 else if ( strcmp(value
, "nonsplit") == 0 ) {
1959 gLinkContext
.prebindUsage
= ImageLoader::kUseSplitSegPrebinding
;
1961 else if ( value
[0] == '\0' ) {
1962 gLinkContext
.prebindUsage
= ImageLoader::kUseSplitSegPrebinding
;
1965 dyld::warn("unknown option to DYLD_IGNORE_PREBINDING. Valid options are: all, app, nonsplit\n");
1968 #if SUPPORT_VERSIONED_PATHS
1969 else if ( strcmp(key
, "DYLD_VERSIONED_LIBRARY_PATH") == 0 ) {
1970 appendParsedColonList(value
, mainExecutableDir
, &sEnv
.DYLD_VERSIONED_LIBRARY_PATH
);
1971 #if SUPPORT_ACCELERATE_TABLES
1972 sDisableAcceleratorTables
= true;
1975 else if ( strcmp(key
, "DYLD_VERSIONED_FRAMEWORK_PATH") == 0 ) {
1976 appendParsedColonList(value
, mainExecutableDir
, &sEnv
.DYLD_VERSIONED_FRAMEWORK_PATH
);
1977 #if SUPPORT_ACCELERATE_TABLES
1978 sDisableAcceleratorTables
= true;
1982 #if !TARGET_IPHONE_SIMULATOR
1983 else if ( (strcmp(key
, "DYLD_PRINT_TO_FILE") == 0) && (mainExecutableDir
== NULL
) && gLinkContext
.allowEnvVarsSharedCache
) {
1984 int fd
= open(value
, O_WRONLY
| O_CREAT
| O_APPEND
, 0644);
1990 dyld::log("dyld: could not open DYLD_PRINT_TO_FILE='%s', errno=%d\n", value
, errno
);
1993 else if ( (strcmp(key
, "DYLD_SKIP_MAIN") == 0)) {
1994 if ( dyld3::internalInstall() )
1999 dyld::warn("unknown environment variable: %s\n", key
);
2004 #if SUPPORT_LC_DYLD_ENVIRONMENT
2005 static void checkLoadCommandEnvironmentVariables()
2007 // <rdar://problem/8440934> Support augmenting dyld environment variables in load commands
2008 const uint32_t cmd_count
= sMainExecutableMachHeader
->ncmds
;
2009 const struct load_command
* const cmds
= (struct load_command
*)(((char*)sMainExecutableMachHeader
)+sizeof(macho_header
));
2010 const struct load_command
* cmd
= cmds
;
2011 for (uint32_t i
= 0; i
< cmd_count
; ++i
) {
2013 case LC_DYLD_ENVIRONMENT
:
2015 const struct dylinker_command
* envcmd
= (struct dylinker_command
*)cmd
;
2016 const char* keyEqualsValue
= (char*)envcmd
+ envcmd
->name
.offset
;
2017 char mainExecutableDir
[strlen(sExecPath
)+2];
2018 strcpy(mainExecutableDir
, sExecPath
);
2019 char* lastSlash
= strrchr(mainExecutableDir
, '/');
2020 if ( lastSlash
!= NULL
)
2021 lastSlash
[1] = '\0';
2022 // only process variables that start with DYLD_ and end in _PATH
2023 if ( (strncmp(keyEqualsValue
, "DYLD_", 5) == 0) ) {
2024 const char* equals
= strchr(keyEqualsValue
, '=');
2025 if ( equals
!= NULL
) {
2026 if ( strncmp(&equals
[-5], "_PATH", 5) == 0 ) {
2027 const char* value
= &equals
[1];
2028 const size_t keyLen
= equals
-keyEqualsValue
;
2029 // <rdar://problem/22799635> don't let malformed load command overflow stack
2030 if ( keyLen
< 40 ) {
2032 strncpy(key
, keyEqualsValue
, keyLen
);
2034 //dyld::log("processing: %s\n", keyEqualsValue);
2035 //dyld::log("mainExecutableDir: %s\n", mainExecutableDir);
2036 processDyldEnvironmentVariable(key
, value
, mainExecutableDir
);
2044 cmd
= (const struct load_command
*)(((char*)cmd
)+cmd
->cmdsize
);
2047 #endif // SUPPORT_LC_DYLD_ENVIRONMENT
2050 static bool hasCodeSignatureLoadCommand(const macho_header
* mh
)
2052 const uint32_t cmd_count
= mh
->ncmds
;
2053 const struct load_command
* const cmds
= (struct load_command
*)(((char*)mh
)+sizeof(macho_header
));
2054 const struct load_command
* cmd
= cmds
;
2055 for (uint32_t i
= 0; i
< cmd_count
; ++i
) {
2056 if (cmd
->cmd
== LC_CODE_SIGNATURE
)
2058 cmd
= (const struct load_command
*)(((char*)cmd
)+cmd
->cmdsize
);
2064 #if SUPPORT_VERSIONED_PATHS
2065 static void checkVersionedPaths()
2067 // search DYLD_VERSIONED_LIBRARY_PATH directories for dylibs and check if they are newer
2068 if ( sEnv
.DYLD_VERSIONED_LIBRARY_PATH
!= NULL
) {
2069 for(const char* const* lp
= sEnv
.DYLD_VERSIONED_LIBRARY_PATH
; *lp
!= NULL
; ++lp
) {
2070 checkDylibOverridesInDir(*lp
);
2074 // search DYLD_VERSIONED_FRAMEWORK_PATH directories for dylibs and check if they are newer
2075 if ( sEnv
.DYLD_VERSIONED_FRAMEWORK_PATH
!= NULL
) {
2076 for(const char* const* fp
= sEnv
.DYLD_VERSIONED_FRAMEWORK_PATH
; *fp
!= NULL
; ++fp
) {
2077 checkFrameworkOverridesInDir(*fp
);
2084 #if __MAC_OS_X_VERSION_MIN_REQUIRED
2086 // For security, setuid programs ignore DYLD_* environment variables.
2087 // Additionally, the DYLD_* enviroment variables are removed
2088 // from the environment, so that any child processes don't see them.
2090 static void pruneEnvironmentVariables(const char* envp
[], const char*** applep
)
2092 #if SUPPORT_LC_DYLD_ENVIRONMENT
2093 checkLoadCommandEnvironmentVariables();
2096 // Are we testing dyld on an internal config?
2097 if ( _simple_getenv(envp
, "DYLD_SKIP_MAIN") != NULL
) {
2098 if ( dyld3::internalInstall() )
2102 // delete all DYLD_* and LD_LIBRARY_PATH environment variables
2103 int removedCount
= 0;
2104 const char** d
= envp
;
2105 for(const char** s
= envp
; *s
!= NULL
; s
++) {
2107 if ( (strncmp(*s
, "DYLD_", 5) != 0) && (strncmp(*s
, "LD_LIBRARY_PATH=", 16) != 0) ) {
2115 // slide apple parameters
2116 if ( removedCount
> 0 ) {
2119 *d
= d
[removedCount
];
2120 } while ( *d
++ != NULL
);
2121 for(int i
=0; i
< removedCount
; ++i
)
2125 // disable framework and library fallback paths for setuid binaries rdar://problem/4589305
2126 sEnv
.DYLD_FALLBACK_FRAMEWORK_PATH
= NULL
;
2127 sEnv
.DYLD_FALLBACK_LIBRARY_PATH
= NULL
;
2129 if ( removedCount
> 0 )
2130 strlcat(sLoadingCrashMessage
, ", ignoring DYLD_* env vars", sizeof(sLoadingCrashMessage
));
2134 static void defaultUninitializedFallbackPaths(const char* envp
[])
2136 #if __MAC_OS_X_VERSION_MIN_REQUIRED
2137 if ( !gLinkContext
.allowClassicFallbackPaths
) {
2138 sEnv
.DYLD_FALLBACK_FRAMEWORK_PATH
= sRestrictedFrameworkFallbackPaths
;
2139 sEnv
.DYLD_FALLBACK_LIBRARY_PATH
= sRestrictedLibraryFallbackPaths
;
2143 // default value for DYLD_FALLBACK_FRAMEWORK_PATH, if not set in environment
2144 const char* home
= _simple_getenv(envp
, "HOME");;
2145 if ( sEnv
.DYLD_FALLBACK_FRAMEWORK_PATH
== NULL
) {
2146 const char** fpaths
= sFrameworkFallbackPaths
;
2148 removePathWithPrefix(fpaths
, "$HOME");
2150 paths_expand_roots(fpaths
, "$HOME", home
);
2151 sEnv
.DYLD_FALLBACK_FRAMEWORK_PATH
= fpaths
;
2154 // default value for DYLD_FALLBACK_LIBRARY_PATH, if not set in environment
2155 if ( sEnv
.DYLD_FALLBACK_LIBRARY_PATH
== NULL
) {
2156 const char** lpaths
= sLibraryFallbackPaths
;
2158 removePathWithPrefix(lpaths
, "$HOME");
2160 paths_expand_roots(lpaths
, "$HOME", home
);
2161 sEnv
.DYLD_FALLBACK_LIBRARY_PATH
= lpaths
;
2164 if ( sEnv
.DYLD_FALLBACK_FRAMEWORK_PATH
== NULL
)
2165 sEnv
.DYLD_FALLBACK_FRAMEWORK_PATH
= sFrameworkFallbackPaths
;
2167 if ( sEnv
.DYLD_FALLBACK_LIBRARY_PATH
== NULL
)
2168 sEnv
.DYLD_FALLBACK_LIBRARY_PATH
= sLibraryFallbackPaths
;
2173 static void checkEnvironmentVariables(const char* envp
[])
2175 if ( !gLinkContext
.allowEnvVarsPath
&& !gLinkContext
.allowEnvVarsPrint
)
2178 for(p
= envp
; *p
!= NULL
; p
++) {
2179 const char* keyEqualsValue
= *p
;
2180 if ( strncmp(keyEqualsValue
, "DYLD_", 5) == 0 ) {
2181 const char* equals
= strchr(keyEqualsValue
, '=');
2182 if ( equals
!= NULL
) {
2183 strlcat(sLoadingCrashMessage
, "\n", sizeof(sLoadingCrashMessage
));
2184 strlcat(sLoadingCrashMessage
, keyEqualsValue
, sizeof(sLoadingCrashMessage
));
2185 const char* value
= &equals
[1];
2186 const size_t keyLen
= equals
-keyEqualsValue
;
2188 strncpy(key
, keyEqualsValue
, keyLen
);
2190 if ( (strncmp(key
, "DYLD_PRINT_", 11) == 0) && !gLinkContext
.allowEnvVarsPrint
)
2192 processDyldEnvironmentVariable(key
, value
, NULL
);
2195 else if ( strncmp(keyEqualsValue
, "LD_LIBRARY_PATH=", 16) == 0 ) {
2196 const char* path
= &keyEqualsValue
[16];
2197 sEnv
.LD_LIBRARY_PATH
= parseColonList(path
, NULL
);
2201 #if SUPPORT_LC_DYLD_ENVIRONMENT
2202 checkLoadCommandEnvironmentVariables();
2203 #endif // SUPPORT_LC_DYLD_ENVIRONMENT
2205 #if SUPPORT_ROOT_PATH
2206 // <rdar://problem/11281064> DYLD_IMAGE_SUFFIX and DYLD_ROOT_PATH cannot be used together
2207 if ( (gLinkContext
.imageSuffix
!= NULL
&& *gLinkContext
.imageSuffix
!= NULL
) && (gLinkContext
.rootPaths
!= NULL
) ) {
2208 dyld::warn("Ignoring DYLD_IMAGE_SUFFIX because DYLD_ROOT_PATH is used.\n");
2209 gLinkContext
.imageSuffix
= NULL
; // this leaks allocations from parseColonList
2214 #if __x86_64__ && !TARGET_IPHONE_SIMULATOR
2215 static bool isGCProgram(const macho_header
* mh
, uintptr_t slide
)
2217 const uint32_t cmd_count
= mh
->ncmds
;
2218 const struct load_command
* const cmds
= (struct load_command
*)(((char*)mh
)+sizeof(macho_header
));
2219 const struct load_command
* cmd
= cmds
;
2220 for (uint32_t i
= 0; i
< cmd_count
; ++i
) {
2222 case LC_SEGMENT_COMMAND
:
2224 const struct macho_segment_command
* seg
= (struct macho_segment_command
*)cmd
;
2225 if (strcmp(seg
->segname
, "__DATA") == 0) {
2226 const struct macho_section
* const sectionsStart
= (struct macho_section
*)((char*)seg
+ sizeof(struct macho_segment_command
));
2227 const struct macho_section
* const sectionsEnd
= §ionsStart
[seg
->nsects
];
2228 for (const struct macho_section
* sect
=sectionsStart
; sect
< sectionsEnd
; ++sect
) {
2229 if (strncmp(sect
->sectname
, "__objc_imageinfo", 16) == 0) {
2230 const uint32_t* objcInfo
= (uint32_t*)(sect
->addr
+ slide
);
2231 return (objcInfo
[1] & 6); // 6 = (OBJC_IMAGE_SUPPORTS_GC | OBJC_IMAGE_REQUIRES_GC)
2238 cmd
= (const struct load_command
*)(((char*)cmd
)+cmd
->cmdsize
);
2244 static void getHostInfo(const macho_header
* mainExecutableMH
, uintptr_t mainExecutableSlide
)
2246 #if CPU_SUBTYPES_SUPPORTED
2248 sHostCPU
= CPU_TYPE_ARM
;
2249 sHostCPUsubtype
= CPU_SUBTYPE_ARM_V7K
;
2250 #elif __ARM_ARCH_7A__
2251 sHostCPU
= CPU_TYPE_ARM
;
2252 sHostCPUsubtype
= CPU_SUBTYPE_ARM_V7
;
2253 #elif __ARM_ARCH_6K__
2254 sHostCPU
= CPU_TYPE_ARM
;
2255 sHostCPUsubtype
= CPU_SUBTYPE_ARM_V6
;
2256 #elif __ARM_ARCH_7F__
2257 sHostCPU
= CPU_TYPE_ARM
;
2258 sHostCPUsubtype
= CPU_SUBTYPE_ARM_V7F
;
2259 #elif __ARM_ARCH_7S__
2260 sHostCPU
= CPU_TYPE_ARM
;
2261 sHostCPUsubtype
= CPU_SUBTYPE_ARM_V7S
;
2262 #elif __ARM64_ARCH_8_32__
2263 sHostCPU
= CPU_TYPE_ARM64_32
;
2264 sHostCPUsubtype
= CPU_SUBTYPE_ARM64_32_V8
;
2266 sHostCPU
= CPU_TYPE_ARM64
;
2267 sHostCPUsubtype
= CPU_SUBTYPE_ARM64_E
;
2269 sHostCPU
= CPU_TYPE_ARM64
;
2270 sHostCPUsubtype
= CPU_SUBTYPE_ARM64_V8
;
2272 struct host_basic_info info
;
2273 mach_msg_type_number_t count
= HOST_BASIC_INFO_COUNT
;
2274 mach_port_t hostPort
= mach_host_self();
2275 kern_return_t result
= host_info(hostPort
, HOST_BASIC_INFO
, (host_info_t
)&info
, &count
);
2276 if ( result
!= KERN_SUCCESS
)
2277 throw "host_info() failed";
2278 sHostCPU
= info
.cpu_type
;
2279 sHostCPUsubtype
= info
.cpu_subtype
;
2280 mach_port_deallocate(mach_task_self(), hostPort
);
2282 // host_info returns CPU_TYPE_I386 even for x86_64. Override that here so that
2283 // we don't need to mask the cpu type later.
2284 sHostCPU
= CPU_TYPE_X86_64
;
2285 #if !TARGET_IPHONE_SIMULATOR
2286 sHaswell
= (sHostCPUsubtype
== CPU_SUBTYPE_X86_64_H
);
2287 // <rdar://problem/18528074> x86_64h: Fall back to the x86_64 slice if an app requires GC.
2289 if ( isGCProgram(mainExecutableMH
, mainExecutableSlide
) ) {
2290 // When running a GC program on a haswell machine, don't use and 'h slices
2291 sHostCPUsubtype
= CPU_SUBTYPE_X86_64_ALL
;
2293 gLinkContext
.sharedRegionMode
= ImageLoader::kDontUseSharedRegion
;
2302 static void checkSharedRegionDisable(const dyld3::MachOLoaded
* mainExecutableMH
, uintptr_t mainExecutableSlide
)
2304 #if __MAC_OS_X_VERSION_MIN_REQUIRED
2305 // if main executable has segments that overlap the shared region,
2306 // then disable using the shared region
2307 if ( mainExecutableMH
->intersectsRange(SHARED_REGION_BASE
, SHARED_REGION_SIZE
) ) {
2308 gLinkContext
.sharedRegionMode
= ImageLoader::kDontUseSharedRegion
;
2309 if ( gLinkContext
.verboseMapping
)
2310 dyld::warn("disabling shared region because main executable overlaps\n");
2313 if ( !gLinkContext
.allowEnvVarsPath
) {
2314 // <rdar://problem/15280847> use private or no shared region for suid processes
2315 gLinkContext
.sharedRegionMode
= ImageLoader::kUsePrivateSharedRegion
;
2319 // iOS cannot run without shared region
2322 bool validImage(const ImageLoader
* possibleImage
)
2324 const size_t imageCount
= sAllImages
.size();
2325 for(size_t i
=0; i
< imageCount
; ++i
) {
2326 if ( possibleImage
== sAllImages
[i
] ) {
2333 uint32_t getImageCount()
2335 return (uint32_t)sAllImages
.size();
2338 ImageLoader
* getIndexedImage(unsigned int index
)
2340 if ( index
< sAllImages
.size() )
2341 return sAllImages
[index
];
2345 ImageLoader
* findImageByMachHeader(const struct mach_header
* target
)
2347 return findMappedRange((uintptr_t)target
);
2351 ImageLoader
* findImageContainingAddress(const void* addr
)
2353 #if SUPPORT_ACCELERATE_TABLES
2354 if ( sAllCacheImagesProxy
!= NULL
) {
2355 const mach_header
* mh
;
2358 if ( sAllCacheImagesProxy
->addressInCache(addr
, &mh
, &path
, &index
) )
2359 return sAllCacheImagesProxy
;
2362 return findMappedRange((uintptr_t)addr
);
2366 ImageLoader
* findImageContainingSymbol(const void* symbol
)
2368 for (std::vector
<ImageLoader
*>::iterator it
=sAllImages
.begin(); it
!= sAllImages
.end(); it
++) {
2369 ImageLoader
* anImage
= *it
;
2370 if ( anImage
->containsSymbol(symbol
) )
2378 void forEachImageDo( void (*callback
)(ImageLoader
*, void* userData
), void* userData
)
2380 const size_t imageCount
= sAllImages
.size();
2381 for(size_t i
=0; i
< imageCount
; ++i
) {
2382 ImageLoader
* anImage
= sAllImages
[i
];
2383 (*callback
)(anImage
, userData
);
2387 ImageLoader
* findLoadedImage(const struct stat
& stat_buf
)
2389 const size_t imageCount
= sAllImages
.size();
2390 for(size_t i
=0; i
< imageCount
; ++i
){
2391 ImageLoader
* anImage
= sAllImages
[i
];
2392 if ( anImage
->statMatch(stat_buf
) )
2398 // based on ANSI-C strstr()
2399 static const char* strrstr(const char* str
, const char* sub
)
2401 const size_t sublen
= strlen(sub
);
2402 for(const char* p
= &str
[strlen(str
)]; p
!= str
; --p
) {
2403 if ( strncmp(p
, sub
, sublen
) == 0 )
2411 // Find framework path
2413 // /path/foo.framework/foo => foo.framework/foo
2414 // /path/foo.framework/Versions/A/foo => foo.framework/Versions/A/foo
2415 // /path/foo.framework/Frameworks/bar.framework/bar => bar.framework/bar
2416 // /path/foo.framework/Libraries/bar.dylb => NULL
2417 // /path/foo.framework/bar => NULL
2419 // Returns NULL if not a framework path
2421 static const char* getFrameworkPartialPath(const char* path
)
2423 const char* dirDot
= strrstr(path
, ".framework/");
2424 if ( dirDot
!= NULL
) {
2425 const char* dirStart
= dirDot
;
2426 for ( ; dirStart
>= path
; --dirStart
) {
2427 if ( (*dirStart
== '/') || (dirStart
== path
) ) {
2428 const char* frameworkStart
= &dirStart
[1];
2429 if ( dirStart
== path
)
2431 size_t len
= dirDot
- frameworkStart
;
2432 char framework
[len
+1];
2433 strncpy(framework
, frameworkStart
, len
);
2434 framework
[len
] = '\0';
2435 const char* leaf
= strrchr(path
, '/');
2436 if ( leaf
!= NULL
) {
2437 if ( strcmp(framework
, &leaf
[1]) == 0 ) {
2438 return frameworkStart
;
2440 if ( gLinkContext
.imageSuffix
!= NULL
) {
2441 // some debug frameworks have install names that end in _debug
2442 if ( strncmp(framework
, &leaf
[1], len
) == 0 ) {
2443 for (const char* const* suffix
=gLinkContext
.imageSuffix
; *suffix
!= NULL
; ++suffix
) {
2444 if ( strcmp(*suffix
, &leaf
[len
+1]) == 0 )
2445 return frameworkStart
;
2457 static const char* getLibraryLeafName(const char* path
)
2459 const char* start
= strrchr(path
, '/');
2460 if ( start
!= NULL
)
2467 // only for architectures that use cpu-sub-types
2468 #if CPU_SUBTYPES_SUPPORTED
2470 const cpu_subtype_t CPU_SUBTYPE_END_OF_LIST
= -1;
2474 // A fat file may contain multiple sub-images for the same CPU type.
2475 // In that case, dyld picks which sub-image to use by scanning a table
2476 // of preferred cpu-sub-types for the running cpu.
2478 // There is one row in the table for each cpu-sub-type on which dyld might run.
2479 // The first entry in a row is that cpu-sub-type. It is followed by all
2480 // cpu-sub-types that can run on that cpu, if preferred order. Each row ends with
2481 // a "SUBTYPE_ALL" (to denote that images written to run on any cpu-sub-type are usable),
2482 // followed by one or more CPU_SUBTYPE_END_OF_LIST to pad out this row.
2488 // ARM sub-type lists
2490 const int kARM_RowCount
= 8;
2491 static const cpu_subtype_t kARM
[kARM_RowCount
][9] = {
2493 // armv7f can run: v7f, v7, v6, v5, and v4
2494 { CPU_SUBTYPE_ARM_V7F
, CPU_SUBTYPE_ARM_V7
, CPU_SUBTYPE_ARM_V6
, CPU_SUBTYPE_ARM_V5TEJ
, CPU_SUBTYPE_ARM_V4T
, CPU_SUBTYPE_ARM_ALL
, CPU_SUBTYPE_END_OF_LIST
},
2496 // armv7k can run: v7k
2497 { CPU_SUBTYPE_ARM_V7K
, CPU_SUBTYPE_END_OF_LIST
},
2499 // armv7s can run: v7s, v7, v7f, v7k, v6, v5, and v4
2500 { CPU_SUBTYPE_ARM_V7S
, CPU_SUBTYPE_ARM_V7
, CPU_SUBTYPE_ARM_V7F
, CPU_SUBTYPE_ARM_V6
, CPU_SUBTYPE_ARM_V5TEJ
, CPU_SUBTYPE_ARM_V4T
, CPU_SUBTYPE_ARM_ALL
, CPU_SUBTYPE_END_OF_LIST
},
2502 // armv7 can run: v7, v6, v5, and v4
2503 { CPU_SUBTYPE_ARM_V7
, CPU_SUBTYPE_ARM_V6
, CPU_SUBTYPE_ARM_V5TEJ
, CPU_SUBTYPE_ARM_V4T
, CPU_SUBTYPE_ARM_ALL
, CPU_SUBTYPE_END_OF_LIST
},
2505 // armv6 can run: v6, v5, and v4
2506 { CPU_SUBTYPE_ARM_V6
, CPU_SUBTYPE_ARM_V5TEJ
, CPU_SUBTYPE_ARM_V4T
, CPU_SUBTYPE_ARM_ALL
, CPU_SUBTYPE_END_OF_LIST
, CPU_SUBTYPE_END_OF_LIST
},
2508 // xscale can run: xscale, v5, and v4
2509 { CPU_SUBTYPE_ARM_XSCALE
, CPU_SUBTYPE_ARM_V5TEJ
, CPU_SUBTYPE_ARM_V4T
, CPU_SUBTYPE_ARM_ALL
, CPU_SUBTYPE_END_OF_LIST
, CPU_SUBTYPE_END_OF_LIST
},
2511 // armv5 can run: v5 and v4
2512 { CPU_SUBTYPE_ARM_V5TEJ
, CPU_SUBTYPE_ARM_V4T
, CPU_SUBTYPE_ARM_ALL
, CPU_SUBTYPE_END_OF_LIST
, CPU_SUBTYPE_END_OF_LIST
, CPU_SUBTYPE_END_OF_LIST
},
2514 // armv4 can run: v4
2515 { CPU_SUBTYPE_ARM_V4T
, CPU_SUBTYPE_ARM_ALL
, CPU_SUBTYPE_END_OF_LIST
, CPU_SUBTYPE_END_OF_LIST
, CPU_SUBTYPE_END_OF_LIST
, CPU_SUBTYPE_END_OF_LIST
},
2521 // ARM64 sub-type lists
2523 const int kARM64_RowCount
= 2;
2524 static const cpu_subtype_t kARM64
[kARM64_RowCount
][4] = {
2526 // armv64e can run: 64e, 64
2527 { CPU_SUBTYPE_ARM64_E
, CPU_SUBTYPE_ARM64_V8
, CPU_SUBTYPE_ARM64_ALL
, CPU_SUBTYPE_END_OF_LIST
},
2529 // armv64 can run: 64
2530 { CPU_SUBTYPE_ARM64_V8
, CPU_SUBTYPE_ARM64_ALL
, CPU_SUBTYPE_END_OF_LIST
},
2533 #if __ARM64_ARCH_8_32__
2534 const int kARM64_32_RowCount
= 2;
2535 static const cpu_subtype_t kARM64_32
[kARM64_32_RowCount
][4] = {
2537 // armv64_32 can run: v8
2538 { CPU_SUBTYPE_ARM64_32_V8
, CPU_SUBTYPE_END_OF_LIST
},
2540 // armv64 can run: 64
2541 { CPU_SUBTYPE_ARM64_V8
, CPU_SUBTYPE_ARM64_ALL
, CPU_SUBTYPE_END_OF_LIST
},
2549 // x86_64 sub-type lists
2551 const int kX86_64_RowCount
= 2;
2552 static const cpu_subtype_t kX86_64
[kX86_64_RowCount
][5] = {
2554 // x86_64h can run: x86_64h, x86_64h(lib), x86_64(lib), and x86_64
2555 { CPU_SUBTYPE_X86_64_H
, (cpu_subtype_t
)(CPU_SUBTYPE_LIB64
|CPU_SUBTYPE_X86_64_H
), (cpu_subtype_t
)(CPU_SUBTYPE_LIB64
|CPU_SUBTYPE_X86_64_ALL
), CPU_SUBTYPE_X86_64_ALL
, CPU_SUBTYPE_END_OF_LIST
},
2557 // x86_64 can run: x86_64(lib) and x86_64
2558 { CPU_SUBTYPE_X86_64_ALL
, (cpu_subtype_t
)(CPU_SUBTYPE_LIB64
|CPU_SUBTYPE_X86_64_ALL
), CPU_SUBTYPE_END_OF_LIST
},
2564 // scan the tables above to find the cpu-sub-type-list for this machine
2565 static const cpu_subtype_t
* findCPUSubtypeList(cpu_type_t cpu
, cpu_subtype_t subtype
)
2570 for (int i
=0; i
< kARM_RowCount
; ++i
) {
2571 if ( kARM
[i
][0] == subtype
)
2577 case CPU_TYPE_ARM64
:
2578 for (int i
=0; i
< kARM64_RowCount
; ++i
) {
2579 if ( kARM64
[i
][0] == subtype
)
2584 #if __ARM64_ARCH_8_32__
2585 case CPU_TYPE_ARM64_32
:
2586 for (int i
=0; i
< kARM64_32_RowCount
; ++i
) {
2587 if ( kARM64_32
[i
][0] == subtype
)
2588 return kARM64_32
[i
];
2595 case CPU_TYPE_X86_64
:
2596 for (int i
=0; i
< kX86_64_RowCount
; ++i
) {
2597 if ( kX86_64
[i
][0] == subtype
)
2609 // scan fat table-of-contents for best most preferred subtype
2610 static bool fatFindBestFromOrderedList(cpu_type_t cpu
, const cpu_subtype_t list
[], const fat_header
* fh
, uint64_t* offset
, uint64_t* len
)
2612 const fat_arch
* const archs
= (fat_arch
*)(((char*)fh
)+sizeof(fat_header
));
2613 for (uint32_t subTypeIndex
=0; list
[subTypeIndex
] != CPU_SUBTYPE_END_OF_LIST
; ++subTypeIndex
) {
2614 for(uint32_t fatIndex
=0; fatIndex
< OSSwapBigToHostInt32(fh
->nfat_arch
); ++fatIndex
) {
2615 if ( ((cpu_type_t
)OSSwapBigToHostInt32(archs
[fatIndex
].cputype
) == cpu
)
2616 && (list
[subTypeIndex
] == (cpu_subtype_t
)OSSwapBigToHostInt32(archs
[fatIndex
].cpusubtype
)) ) {
2617 *offset
= OSSwapBigToHostInt32(archs
[fatIndex
].offset
);
2618 *len
= OSSwapBigToHostInt32(archs
[fatIndex
].size
);
2626 // scan fat table-of-contents for exact match of cpu and cpu-sub-type
2627 static bool fatFindExactMatch(cpu_type_t cpu
, cpu_subtype_t subtype
, const fat_header
* fh
, uint64_t* offset
, uint64_t* len
)
2629 const fat_arch
* archs
= (fat_arch
*)(((char*)fh
)+sizeof(fat_header
));
2630 for(uint32_t i
=0; i
< OSSwapBigToHostInt32(fh
->nfat_arch
); ++i
) {
2631 if ( ((cpu_type_t
)OSSwapBigToHostInt32(archs
[i
].cputype
) == cpu
)
2632 && ((cpu_subtype_t
)OSSwapBigToHostInt32(archs
[i
].cpusubtype
) == subtype
) ) {
2633 *offset
= OSSwapBigToHostInt32(archs
[i
].offset
);
2634 *len
= OSSwapBigToHostInt32(archs
[i
].size
);
2641 // scan fat table-of-contents for image with matching cpu-type and runs-on-all-sub-types
2642 static bool fatFindRunsOnAllCPUs(cpu_type_t cpu
, const fat_header
* fh
, uint64_t* offset
, uint64_t* len
)
2644 const fat_arch
* archs
= (fat_arch
*)(((char*)fh
)+sizeof(fat_header
));
2645 for(uint32_t i
=0; i
< OSSwapBigToHostInt32(fh
->nfat_arch
); ++i
) {
2646 if ( (cpu_type_t
)OSSwapBigToHostInt32(archs
[i
].cputype
) == cpu
) {
2650 if ( (cpu_subtype_t
)OSSwapBigToHostInt32(archs
[i
].cpusubtype
) == CPU_SUBTYPE_ARM_ALL
) {
2651 *offset
= OSSwapBigToHostInt32(archs
[i
].offset
);
2652 *len
= OSSwapBigToHostInt32(archs
[i
].size
);
2658 case CPU_TYPE_ARM64
:
2659 if ( (cpu_subtype_t
)OSSwapBigToHostInt32(archs
[i
].cpusubtype
) == CPU_SUBTYPE_ARM64_ALL
) {
2660 *offset
= OSSwapBigToHostInt32(archs
[i
].offset
);
2661 *len
= OSSwapBigToHostInt32(archs
[i
].size
);
2667 case CPU_TYPE_X86_64
:
2668 if ( (cpu_subtype_t
)OSSwapBigToHostInt32(archs
[i
].cpusubtype
) == CPU_SUBTYPE_X86_64_ALL
) {
2669 *offset
= OSSwapBigToHostInt32(archs
[i
].offset
);
2670 *len
= OSSwapBigToHostInt32(archs
[i
].size
);
2681 #endif // CPU_SUBTYPES_SUPPORTED
2685 // Validate the fat_header and fat_arch array:
2687 // 1) arch count would not cause array to extend past 4096 byte read buffer
2688 // 2) no slice overlaps the fat_header and arch array
2689 // 3) arch list does not contain duplicate cputype/cpusubtype tuples
2690 // 4) arch list does not have two overlapping slices.
2692 static bool fatValidate(const fat_header
* fh
)
2694 if ( fh
->magic
!= OSSwapBigToHostInt32(FAT_MAGIC
) )
2697 // since only first 4096 bytes of file read, we can only handle up to 204 slices.
2698 const uint32_t sliceCount
= OSSwapBigToHostInt32(fh
->nfat_arch
);
2699 if ( sliceCount
> 204 )
2702 // compare all slices looking for conflicts
2703 const fat_arch
* archs
= (fat_arch
*)(((char*)fh
)+sizeof(fat_header
));
2704 for (uint32_t i
=0; i
< sliceCount
; ++i
) {
2705 uint32_t i_offset
= OSSwapBigToHostInt32(archs
[i
].offset
);
2706 uint32_t i_size
= OSSwapBigToHostInt32(archs
[i
].size
);
2707 uint32_t i_cputype
= OSSwapBigToHostInt32(archs
[i
].cputype
);
2708 uint32_t i_cpusubtype
= OSSwapBigToHostInt32(archs
[i
].cpusubtype
);
2709 uint32_t i_end
= i_offset
+ i_size
;
2710 // slice cannot overlap with header
2711 if ( i_offset
< 4096 )
2713 // slice size cannot overflow
2714 if ( i_end
< i_offset
)
2716 for (uint32_t j
=i
+1; j
< sliceCount
; ++j
) {
2717 uint32_t j_offset
= OSSwapBigToHostInt32(archs
[j
].offset
);
2718 uint32_t j_size
= OSSwapBigToHostInt32(archs
[j
].size
);
2719 uint32_t j_cputype
= OSSwapBigToHostInt32(archs
[j
].cputype
);
2720 uint32_t j_cpusubtype
= OSSwapBigToHostInt32(archs
[j
].cpusubtype
);
2721 uint32_t j_end
= j_offset
+ j_size
;
2722 // duplicate slices types not allowed
2723 if ( (i_cputype
== j_cputype
) && (i_cpusubtype
== j_cpusubtype
) )
2725 // slice size cannot overflow
2726 if ( j_end
< j_offset
)
2728 // check for overlap of slices
2729 if ( i_offset
<= j_offset
) {
2730 if ( j_offset
< i_end
)
2731 return false; // j overlaps end of i
2734 // j overlaps end of i
2735 if ( i_offset
< j_end
)
2736 return false; // i overlaps end of j
2744 // A fat file may contain multiple sub-images for the same cpu-type,
2745 // each optimized for a different cpu-sub-type (e.g G3 or G5).
2746 // This routine picks the optimal sub-image.
2748 static bool fatFindBest(const fat_header
* fh
, uint64_t* offset
, uint64_t* len
)
2750 if ( !fatValidate(fh
) )
2753 #if CPU_SUBTYPES_SUPPORTED
2754 // assume all dylibs loaded must have same cpu type as main executable
2755 const cpu_type_t cpu
= sMainExecutableMachHeader
->cputype
;
2757 // We only know the subtype to use if the main executable cpu type matches the host
2758 if ( cpu
== sHostCPU
) {
2759 // get preference ordered list of subtypes
2760 const cpu_subtype_t
* subTypePreferenceList
= findCPUSubtypeList(cpu
, sHostCPUsubtype
);
2762 // use ordered list to find best sub-image in fat file
2763 if ( subTypePreferenceList
!= NULL
) {
2764 if ( fatFindBestFromOrderedList(cpu
, subTypePreferenceList
, fh
, offset
, len
) )
2768 // if running cpu is not in list, try for an exact match
2769 if ( fatFindExactMatch(cpu
, sHostCPUsubtype
, fh
, offset
, len
) )
2773 // running on an uknown cpu, can only load generic code
2774 return fatFindRunsOnAllCPUs(cpu
, fh
, offset
, len
);
2776 // just find first slice with matching architecture
2777 const fat_arch
* archs
= (fat_arch
*)(((char*)fh
)+sizeof(fat_header
));
2778 for(uint32_t i
=0; i
< OSSwapBigToHostInt32(fh
->nfat_arch
); ++i
) {
2779 if ( (cpu_type_t
)OSSwapBigToHostInt32(archs
[i
].cputype
) == sMainExecutableMachHeader
->cputype
) {
2780 *offset
= OSSwapBigToHostInt32(archs
[i
].offset
);
2781 *len
= OSSwapBigToHostInt32(archs
[i
].size
);
2792 // This is used to validate if a non-fat (aka thin or raw) mach-o file can be used
2793 // on the current processor. //
2794 bool isCompatibleMachO(const uint8_t* firstPage
, const char* path
)
2796 #if CPU_SUBTYPES_SUPPORTED
2797 // It is deemed compatible if any of the following are true:
2798 // 1) mach_header subtype is in list of compatible subtypes for running processor
2799 // 2) mach_header subtype is same as running processor subtype
2800 // 3) mach_header subtype runs on all processor variants
2801 const mach_header
* mh
= (mach_header
*)firstPage
;
2802 if ( mh
->magic
== sMainExecutableMachHeader
->magic
) {
2803 if ( mh
->cputype
== sMainExecutableMachHeader
->cputype
) {
2804 if ( mh
->cputype
== sHostCPU
) {
2805 // get preference ordered list of subtypes that this machine can use
2806 const cpu_subtype_t
* subTypePreferenceList
= findCPUSubtypeList(mh
->cputype
, sHostCPUsubtype
);
2807 if ( subTypePreferenceList
!= NULL
) {
2808 // if image's subtype is in the list, it is compatible
2809 for (const cpu_subtype_t
* p
= subTypePreferenceList
; *p
!= CPU_SUBTYPE_END_OF_LIST
; ++p
) {
2810 if ( *p
== mh
->cpusubtype
)
2813 // have list and not in list, so not compatible
2814 throwf("incompatible cpu-subtype: 0x%08X in %s", mh
->cpusubtype
, path
);
2816 // unknown cpu sub-type, but if exact match for current subtype then ok to use
2817 if ( mh
->cpusubtype
== sHostCPUsubtype
)
2821 // cpu type has no ordered list of subtypes
2822 switch (mh
->cputype
) {
2824 case CPU_TYPE_X86_64
:
2825 // subtypes are not used or these architectures
2831 // For architectures that don't support cpu-sub-types
2832 // this just check the cpu type.
2833 const mach_header
* mh
= (mach_header
*)firstPage
;
2834 if ( mh
->magic
== sMainExecutableMachHeader
->magic
) {
2835 if ( mh
->cputype
== sMainExecutableMachHeader
->cputype
) {
2846 // The kernel maps in main executable before dyld gets control. We need to
2847 // make an ImageLoader* for the already mapped in main executable.
2848 static ImageLoaderMachO
* instantiateFromLoadedImage(const macho_header
* mh
, uintptr_t slide
, const char* path
)
2850 // try mach-o loader
2851 if ( isCompatibleMachO((const uint8_t*)mh
, path
) ) {
2852 ImageLoader
* image
= ImageLoaderMachO::instantiateMainExecutable(mh
, slide
, path
, gLinkContext
);
2854 return (ImageLoaderMachO
*)image
;
2857 throw "main executable not a known format";
2860 #if SUPPORT_ACCELERATE_TABLES
2861 static bool dylibsCanOverrideCache()
2863 if ( !dyld3::internalInstall() )
2865 return ( (sSharedCacheLoadInfo
.loadAddress
!= nullptr) && (sSharedCacheLoadInfo
.loadAddress
->header
.cacheType
== kDyldSharedCacheTypeDevelopment
) );
2869 const void* imMemorySharedCacheHeader()
2871 return sSharedCacheLoadInfo
.loadAddress
;
2875 const char* getStandardSharedCacheFilePath()
2877 if ( sSharedCacheLoadInfo
.loadAddress
!= nullptr )
2878 return sSharedCacheLoadInfo
.path
;
2884 static bool findInSharedCacheImage(const char* path
, bool searchByPath
, const struct stat
* stat_buf
, const macho_header
** mh
, const char** pathInCache
, long* slide
)
2886 dyld3::SharedCacheFindDylibResults results
;
2887 if ( dyld3::findInSharedCacheImage(sSharedCacheLoadInfo
, path
, &results
) ) {
2888 *mh
= (macho_header
*)results
.mhInCache
;
2889 *pathInCache
= results
.pathInCache
;
2890 *slide
= results
.slideInCache
;
2896 bool inSharedCache(const char* path
)
2898 return dyld3::pathIsInSharedCacheImage(sSharedCacheLoadInfo
, path
);
2902 static ImageLoader
* checkandAddImage(ImageLoader
* image
, const LoadContext
& context
)
2904 // now sanity check that this loaded image does not have the same install path as any existing image
2905 const char* loadedImageInstallPath
= image
->getInstallPath();
2906 if ( image
->isDylib() && (loadedImageInstallPath
!= NULL
) && (loadedImageInstallPath
[0] == '/') ) {
2907 for (std::vector
<ImageLoader
*>::iterator it
=sAllImages
.begin(); it
!= sAllImages
.end(); it
++) {
2908 ImageLoader
* anImage
= *it
;
2909 const char* installPath
= anImage
->getInstallPath();
2910 if ( installPath
!= NULL
) {
2911 if ( strcmp(loadedImageInstallPath
, installPath
) == 0 ) {
2912 //dyld::log("duplicate(%s) => %p\n", installPath, anImage);
2914 ImageLoader::deleteImage(image
);
2921 // some API's restrict what they can load
2922 if ( context
.mustBeBundle
&& !image
->isBundle() )
2923 throw "not a bundle";
2924 if ( context
.mustBeDylib
&& !image
->isDylib() )
2925 throw "not a dylib";
2927 // regular main executables cannot be loaded
2928 if ( image
->isExecutable() ) {
2929 if ( !context
.canBePIE
|| !image
->isPositionIndependentExecutable() )
2930 throw "can't load a main executable";
2933 // don't add bundles to global list, they can be loaded but not linked. When linked it will be added to list
2934 if ( ! image
->isBundle() )
2940 #if TARGET_IPHONE_SIMULATOR
2941 static bool isSimulatorBinary(const uint8_t* firstPages
, const char* path
)
2943 const macho_header
* mh
= (macho_header
*)firstPages
;
2944 const uint32_t cmd_count
= mh
->ncmds
;
2945 const load_command
* const cmds
= (struct load_command
*)(((char*)mh
)+sizeof(macho_header
));
2946 const load_command
* const cmdsEnd
= (load_command
*)((char*)cmds
+ mh
->sizeofcmds
);
2947 const struct load_command
* cmd
= cmds
;
2948 for (uint32_t i
= 0; i
< cmd_count
; ++i
) {
2951 case LC_VERSION_MIN_WATCHOS
:
2954 case LC_VERSION_MIN_TVOS
:
2957 case LC_VERSION_MIN_IPHONEOS
:
2960 case LC_VERSION_MIN_MACOSX
:
2961 // grandfather in a few libSystem dylibs
2962 if ((strcmp(path
, "/usr/lib/system/libsystem_kernel.dylib") == 0) ||
2963 (strcmp(path
, "/usr/lib/system/libsystem_platform.dylib") == 0) ||
2964 (strcmp(path
, "/usr/lib/system/libsystem_pthread.dylib") == 0) ||
2965 (strcmp(path
, "/usr/lib/system/libsystem_platform_debug.dylib") == 0) ||
2966 (strcmp(path
, "/usr/lib/system/libsystem_pthread_debug.dylib") == 0))
2969 case LC_BUILD_VERSION
:
2971 // Same logic as above, but for LC_BUILD_VERSION instead of legacy load commands
2972 const struct build_version_command
* buildVersionCmd
= (build_version_command
*)cmd
;
2973 switch(buildVersionCmd
->platform
) {
2974 case PLATFORM_IOSSIMULATOR
:
2975 case PLATFORM_TVOSSIMULATOR
:
2976 case PLATFORM_WATCHOSSIMULATOR
:
2977 case PLATFORM_WATCHOS
:
2979 #if TARGET_OS_IOSMAC
2983 case PLATFORM_MACOS
:
2984 if ((strcmp(path
, "/usr/lib/system/libsystem_kernel.dylib") == 0) ||
2985 (strcmp(path
, "/usr/lib/system/libsystem_platform.dylib") == 0) ||
2986 (strcmp(path
, "/usr/lib/system/libsystem_pthread.dylib") == 0) ||
2987 (strcmp(path
, "/usr/lib/system/libsystem_platform_debug.dylib") == 0) ||
2988 (strcmp(path
, "/usr/lib/system/libsystem_pthread_debug.dylib") == 0))
2993 cmd
= (const struct load_command
*)(((char*)cmd
)+cmd
->cmdsize
);
2994 if ( cmd
> cmdsEnd
)
3001 #if __MAC_OS_X_VERSION_MIN_REQUIRED
3002 static bool iOSMacWhiteListed(const char* path
)
3004 static char* whiteListBuffer
= nullptr;
3005 static size_t whiteListSize
= 0;
3006 static bool tried
= false;
3008 // only try to map file once
3009 whiteListBuffer
= (char*)mapFileReadOnly("/System/iOSSupport/dyld/macOS-whitelist.txt", whiteListSize
);
3012 __block
bool result
= false;
3013 if ( whiteListBuffer
!= nullptr ) {
3014 dyld3::forEachLineInFile(whiteListBuffer
, whiteListSize
, ^(const char* line
, bool& stop
) {
3015 // lines in the file are prefixes. Any path that starts with one of these lines is allowed to be unzippered
3016 size_t lineLen
= strlen(line
);
3017 if ( (*line
== '/') && strncmp(line
, path
, lineLen
) == 0 ) {
3026 static bool iOSMacBlackListed(const char* path
)
3028 static char* blackListBuffer
= nullptr;
3029 static size_t blackListSize
= 0;
3030 static bool tried
= false;
3032 // only try to map file once
3033 blackListBuffer
= (char*)mapFileReadOnly("/System/iOSSupport/dyld/macOS-blacklist.txt", blackListSize
);
3036 __block
bool result
= false;
3037 if ( blackListBuffer
!= nullptr ) {
3038 dyld3::forEachLineInFile(blackListBuffer
, blackListSize
, ^(const char* line
, bool& stop
) {
3039 // lines in the file are prefixes. Any path that starts with one of these lines is allowed to be unzippered
3040 size_t lineLen
= strlen(line
);
3041 if ( (*line
== '/') && strncmp(line
, path
, lineLen
) == 0 ) {
3053 // map in file and instantiate an ImageLoader
3054 static ImageLoader
* loadPhase6(int fd
, const struct stat
& stat_buf
, const char* path
, const LoadContext
& context
)
3056 //dyld::log("%s(%s)\n", __func__ , path);
3057 uint64_t fileOffset
= 0;
3058 uint64_t fileLength
= stat_buf
.st_size
;
3060 // validate it is a file (not directory)
3061 if ( (stat_buf
.st_mode
& S_IFMT
) != S_IFREG
)
3064 uint8_t firstPages
[MAX_MACH_O_HEADER_AND_LOAD_COMMANDS_SIZE
];
3065 uint8_t *firstPagesPtr
= firstPages
;
3066 bool shortPage
= false;
3068 // min mach-o file is 4K
3069 if ( fileLength
< 4096 ) {
3070 if ( pread(fd
, firstPages
, (size_t)fileLength
, 0) != (ssize_t
)fileLength
)
3071 throwf("pread of short file failed: %d", errno
);
3075 // optimistically read only first 4KB
3076 if ( pread(fd
, firstPages
, 4096, 0) != 4096 )
3077 throwf("pread of first 4K failed: %d", errno
);
3080 // if fat wrapper, find usable sub-file
3081 const fat_header
* fileStartAsFat
= (fat_header
*)firstPages
;
3082 if ( fileStartAsFat
->magic
== OSSwapBigToHostInt32(FAT_MAGIC
) ) {
3083 if ( OSSwapBigToHostInt32(fileStartAsFat
->nfat_arch
) > ((4096 - sizeof(fat_header
)) / sizeof(fat_arch
)) )
3084 throwf("fat header too large: %u entries", OSSwapBigToHostInt32(fileStartAsFat
->nfat_arch
));
3085 if ( fatFindBest(fileStartAsFat
, &fileOffset
, &fileLength
) ) {
3086 if ( (fileOffset
+fileLength
) > (uint64_t)(stat_buf
.st_size
) )
3087 throwf("truncated fat file. file length=%llu, but needed slice goes to %llu", stat_buf
.st_size
, fileOffset
+fileLength
);
3088 if (pread(fd
, firstPages
, 4096, fileOffset
) != 4096)
3089 throwf("pread of fat file failed: %d", errno
);
3092 throw "no matching architecture in universal wrapper";
3096 // try mach-o loader
3098 throw "file too short";
3100 if ( isCompatibleMachO(firstPages
, path
) ) {
3102 // only MH_BUNDLE, MH_DYLIB, and some MH_EXECUTE can be dynamically loaded
3103 const mach_header
* mh
= (mach_header
*)firstPages
;
3104 switch ( mh
->filetype
) {
3110 throw "mach-o, but wrong filetype";
3113 uint32_t headerAndLoadCommandsSize
= sizeof(macho_header
) + mh
->sizeofcmds
;
3114 if ( headerAndLoadCommandsSize
> MAX_MACH_O_HEADER_AND_LOAD_COMMANDS_SIZE
)
3115 throwf("malformed mach-o: load commands size (%u) > %u", headerAndLoadCommandsSize
, MAX_MACH_O_HEADER_AND_LOAD_COMMANDS_SIZE
);
3117 if ( headerAndLoadCommandsSize
> fileLength
)
3118 dyld::throwf("malformed mach-o: load commands size (%u) > mach-o file size (%llu)", headerAndLoadCommandsSize
, fileLength
);
3120 if ( headerAndLoadCommandsSize
> 4096 ) {
3122 unsigned readAmount
= headerAndLoadCommandsSize
- 4096;
3123 if ( pread(fd
, &firstPages
[4096], readAmount
, fileOffset
+4096) != readAmount
)
3124 throwf("pread of extra load commands past 4KB failed: %d", errno
);
3127 #if TARGET_IPHONE_SIMULATOR
3128 // <rdar://problem/14168872> dyld_sim should restrict loading osx binaries
3129 if ( !isSimulatorBinary(firstPages
, path
) ) {
3131 throw "mach-o, but not built for watchOS simulator";
3133 throw "mach-o, but not built for tvOS simulator";
3135 throw "mach-o, but not built for iOS simulator";
3140 #if __MAC_OS_X_VERSION_MIN_REQUIRED
3141 if ( gLinkContext
.marzipan
) {
3142 const dyld3::MachOFile
* mf
= (dyld3::MachOFile
*)firstPages
;
3143 bool isiOSMacBinary
= mf
->supportsPlatform(dyld3::Platform::iOSMac
) || iOSMacWhiteListed(path
);
3144 bool isProhibitedMacOSBinary
= !isiOSMacBinary
&& iOSMacBlackListed(path
);
3145 if ( (context
.enforceIOSMac
&& !isiOSMacBinary
) || isProhibitedMacOSBinary
) {
3146 throw "mach-o, but not built for iOSMac";
3152 if ( (sMainExecutableMachHeader
->cpusubtype
== CPU_SUBTYPE_ARM64_E
) && (mh
->cpusubtype
!= CPU_SUBTYPE_ARM64_E
) )
3153 throw "arm64 dylibs cannot be loaded into arm64e processes";
3155 ImageLoader
* image
= nullptr;
3157 dyld3::ScopedTimer
timer(DBG_DYLD_TIMING_MAP_IMAGE
, path
, 0, 0);
3158 image
= ImageLoaderMachO::instantiateFromFile(path
, fd
, firstPagesPtr
, headerAndLoadCommandsSize
, fileOffset
, fileLength
, stat_buf
, gLinkContext
);
3159 timer
.setData4((uint64_t)image
->machHeader());
3163 return checkandAddImage(image
, context
);
3166 // try other file formats here...
3169 // throw error about what was found
3170 switch (*(uint32_t*)firstPages
) {
3175 throw "mach-o, but wrong architecture";
3177 throwf("unknown file type, first eight bytes: 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X",
3178 firstPages
[0], firstPages
[1], firstPages
[2], firstPages
[3], firstPages
[4], firstPages
[5], firstPages
[6],firstPages
[7]);
3183 static ImageLoader
* loadPhase5open(const char* path
, const LoadContext
& context
, const struct stat
& stat_buf
, std::vector
<const char*>* exceptions
)
3185 //dyld::log("%s(%s, %p)\n", __func__ , path, exceptions);
3187 // open file (automagically closed when this function exits)
3188 FileOpener
file(path
);
3190 // just return NULL if file not found, but record any other errors
3191 if ( file
.getFileDescriptor() == -1 ) {
3193 if ( err
!= ENOENT
) {
3195 if ( (err
== EPERM
) && sandboxBlockedOpen(path
) )
3196 newMsg
= dyld::mkstringf("file system sandbox blocked open() of '%s'", path
);
3198 newMsg
= dyld::mkstringf("%s: open() failed with errno=%d", path
, err
);
3199 exceptions
->push_back(newMsg
);
3205 return loadPhase6(file
.getFileDescriptor(), stat_buf
, path
, context
);
3207 catch (const char* msg
) {
3208 const char* newMsg
= dyld::mkstringf("%s: %s", path
, msg
);
3209 exceptions
->push_back(newMsg
);
3218 static ImageLoader
* loadPhase5load(const char* path
, const char* orgPath
, const LoadContext
& context
, unsigned& cacheIndex
, std::vector
<const char*>* exceptions
)
3220 //dyld::log("%s(%s, %p)\n", __func__ , path, exceptions);
3221 #if SUPPORT_ACCELERATE_TABLES
3222 if ( sAllCacheImagesProxy
!= NULL
) {
3223 if ( sAllCacheImagesProxy
->hasDylib(path
, &cacheIndex
) )
3224 return sAllCacheImagesProxy
;
3227 #if TARGET_IPHONE_SIMULATOR
3228 // in simulators, 'path' has DYLD_ROOT_PATH prepended, but cache index does not have the prefix, so use orgPath
3229 const char* pathToFindInCache
= orgPath
;
3231 const char* pathToFindInCache
= path
;
3234 struct stat statBuf
;
3235 bool didStat
= false;
3237 dyld3::SharedCacheFindDylibResults shareCacheResults
;
3238 shareCacheResults
.image
= nullptr;
3239 if ( dyld3::findInSharedCacheImage(sSharedCacheLoadInfo
, pathToFindInCache
, &shareCacheResults
) ) {
3240 // see if this image in the cache was already loaded via a different path
3241 for (std::vector
<ImageLoader
*>::iterator it
=sAllImages
.begin(); it
!= sAllImages
.end(); ++it
) {
3242 ImageLoader
* anImage
= *it
;
3243 if ( (const mach_header
*)anImage
->machHeader() == shareCacheResults
.mhInCache
)
3246 // if RTLD_NOLOAD, do nothing if not already loaded
3247 if ( context
.dontLoad
) {
3248 // <rdar://33412890> possible that there is an override of cache
3249 if ( my_stat(path
, &statBuf
) == 0 ) {
3250 ImageLoader
* imageLoader
= findLoadedImage(statBuf
);
3251 if ( imageLoader
!= NULL
)
3256 bool useCache
= false;
3257 if ( shareCacheResults
.image
== nullptr ) {
3258 // HACK to support old caches
3259 existsOnDisk
= ( my_stat(path
, &statBuf
) == 0 );
3262 useCache
= !existsOnDisk
;
3265 // <rdar://problem/7014995> zero out stat buffer so mtime, etc are zero for items from the shared cache
3266 bzero(&statBuf
, sizeof(statBuf
));
3267 if ( shareCacheResults
.image
->overridableDylib() ) {
3268 existsOnDisk
= ( my_stat(path
, &statBuf
) == 0 );
3271 if ( sSharedCacheLoadInfo
.loadAddress
->header
.dylibsExpectedOnDisk
) {
3272 uint64_t expectedINode
;
3273 uint64_t expectedMtime
;
3274 if ( shareCacheResults
.image
->hasFileModTimeAndInode(expectedINode
, expectedMtime
) ) {
3275 if ( (expectedMtime
== statBuf
.st_mtime
) && (expectedINode
== statBuf
.st_ino
) )
3280 if ( !existsOnDisk
)
3289 #if __MAC_OS_X_VERSION_MIN_REQUIRED
3290 if ( gLinkContext
.marzipan
) {
3291 const dyld3::MachOFile
* mf
= (dyld3::MachOFile
*)shareCacheResults
.mhInCache
;
3292 bool isiOSMacBinary
= mf
->supportsPlatform(dyld3::Platform::iOSMac
) || iOSMacWhiteListed(path
);
3293 bool isProhibitedMacOSBinary
= !isiOSMacBinary
&& iOSMacBlackListed(path
);
3294 if ( (context
.enforceIOSMac
&& !isiOSMacBinary
) || isProhibitedMacOSBinary
) {
3295 throw "mach-o, but not built for iOSMac";
3299 ImageLoader
* imageLoader
= ImageLoaderMachO::instantiateFromCache((macho_header
*)shareCacheResults
.mhInCache
, shareCacheResults
.pathInCache
, shareCacheResults
.slideInCache
, statBuf
, gLinkContext
);
3300 return checkandAddImage(imageLoader
, context
);
3304 // not in cache or cache not usable
3306 existsOnDisk
= ( my_stat(path
, &statBuf
) == 0 );
3309 if ( existsOnDisk
) {
3310 // in case image was renamed or found via symlinks, check for inode match
3311 ImageLoader
* imageLoader
= findLoadedImage(statBuf
);
3312 if ( imageLoader
!= NULL
)
3314 // do nothing if not already loaded and if RTLD_NOLOAD
3315 if ( context
.dontLoad
)
3318 imageLoader
= loadPhase5open(path
, context
, statBuf
, exceptions
);
3319 if ( imageLoader
!= NULL
) {
3320 if ( shareCacheResults
.image
!= nullptr ) {
3321 // if image was found in cache, but is overridden by a newer file on disk, record what the image overrides
3322 imageLoader
->setOverridesCachedDylib(shareCacheResults
.image
->imageNum());
3328 // just return NULL if file not found, but record any other errors
3329 if ( (statErrNo
!= ENOENT
) && (statErrNo
!= 0) ) {
3330 if ( (statErrNo
== EPERM
) && sandboxBlockedStat(path
) )
3331 exceptions
->push_back(dyld::mkstringf("%s: file system sandbox blocked stat()", path
));
3333 exceptions
->push_back(dyld::mkstringf("%s: stat() failed with errno=%d", path
, statErrNo
));
3338 // look for path match with existing loaded images
3339 static ImageLoader
* loadPhase5check(const char* path
, const char* orgPath
, const LoadContext
& context
)
3341 //dyld::log("%s(%s, %s)\n", __func__ , path, orgPath);
3342 // search path against load-path and install-path of all already loaded images
3343 uint32_t hash
= ImageLoader::hash(path
);
3344 //dyld::log("check() hash=%d, path=%s\n", hash, path);
3345 for (std::vector
<ImageLoader
*>::iterator it
=sAllImages
.begin(); it
!= sAllImages
.end(); it
++) {
3346 ImageLoader
* anImage
= *it
;
3347 // check hash first to cut down on strcmp calls
3348 //dyld::log(" check() hash=%d, path=%s\n", anImage->getPathHash(), anImage->getPath());
3349 if ( anImage
->getPathHash() == hash
) {
3350 if ( strcmp(path
, anImage
->getPath()) == 0 ) {
3351 // if we are looking for a dylib don't return something else
3352 if ( !context
.mustBeDylib
|| anImage
->isDylib() )
3356 if ( context
.matchByInstallName
|| anImage
->matchInstallPath() ) {
3357 const char* installPath
= anImage
->getInstallPath();
3358 if ( installPath
!= NULL
) {
3359 if ( strcmp(path
, installPath
) == 0 ) {
3360 // if we are looking for a dylib don't return something else
3361 if ( !context
.mustBeDylib
|| anImage
->isDylib() )
3366 // an install name starting with @rpath should match by install name, not just real path
3367 if ( (orgPath
[0] == '@') && (strncmp(orgPath
, "@rpath/", 7) == 0) ) {
3368 const char* installPath
= anImage
->getInstallPath();
3369 if ( installPath
!= NULL
) {
3370 if ( !context
.mustBeDylib
|| anImage
->isDylib() ) {
3371 if ( strcmp(orgPath
, installPath
) == 0 )
3378 //dyld::log("%s(%s) => NULL\n", __func__, path);
3383 // open or check existing
3384 static ImageLoader
* loadPhase5(const char* path
, const char* orgPath
, const LoadContext
& context
, unsigned& cacheIndex
, std::vector
<const char*>* exceptions
)
3386 //dyld::log("%s(%s, %p)\n", __func__ , path, exceptions);
3388 // check for specific dylib overrides
3389 for (std::vector
<DylibOverride
>::iterator it
= sDylibOverrides
.begin(); it
!= sDylibOverrides
.end(); ++it
) {
3390 if ( strcmp(it
->installName
, path
) == 0 ) {
3391 path
= it
->override
;
3396 if ( exceptions
!= NULL
)
3397 return loadPhase5load(path
, orgPath
, context
, cacheIndex
, exceptions
);
3399 return loadPhase5check(path
, orgPath
, context
);
3402 // try with and without image suffix
3403 static ImageLoader
* loadPhase4(const char* path
, const char* orgPath
, const LoadContext
& context
, unsigned& cacheIndex
, std::vector
<const char*>* exceptions
)
3405 //dyld::log("%s(%s, %p)\n", __func__ , path, exceptions);
3406 ImageLoader
* image
= NULL
;
3407 if ( gLinkContext
.imageSuffix
!= NULL
) {
3408 for (const char* const* suffix
=gLinkContext
.imageSuffix
; *suffix
!= NULL
; ++suffix
) {
3409 char pathWithSuffix
[strlen(path
)+strlen(*suffix
)+2];
3410 ImageLoader::addSuffix(path
, *suffix
, pathWithSuffix
);
3411 image
= loadPhase5(pathWithSuffix
, orgPath
, context
, cacheIndex
, exceptions
);
3412 if ( image
!= NULL
)
3415 if ( image
!= NULL
) {
3416 // if original path is in the dyld cache, then mark this one found as an override
3417 dyld3::SharedCacheFindDylibResults shareCacheResults
;
3418 if ( dyld3::findInSharedCacheImage(sSharedCacheLoadInfo
, path
, &shareCacheResults
) && (shareCacheResults
.image
!= nullptr) )
3419 image
->setOverridesCachedDylib(shareCacheResults
.image
->imageNum());
3422 if ( image
== NULL
)
3423 image
= loadPhase5(path
, orgPath
, context
, cacheIndex
, exceptions
);
3427 static ImageLoader
* loadPhase2(const char* path
, const char* orgPath
, const LoadContext
& context
,
3428 const char* const frameworkPaths
[], const char* const libraryPaths
[],
3429 unsigned& cacheIndex
, std::vector
<const char*>* exceptions
); // forward reference
3432 // expand @ variables
3433 static ImageLoader
* loadPhase3(const char* path
, const char* orgPath
, const LoadContext
& context
, unsigned& cacheIndex
, std::vector
<const char*>* exceptions
)
3435 //dyld::log("%s(%s, %p)\n", __func__ , path, exceptions);
3436 ImageLoader
* image
= NULL
;
3437 if ( strncmp(path
, "@executable_path/", 17) == 0 ) {
3438 // executable_path cannot be in used in any binary in a setuid process rdar://problem/4589305
3439 if ( !gLinkContext
.allowAtPaths
)
3440 throwf("unsafe use of @executable_path in %s with restricted binary", context
.origin
);
3441 // handle @executable_path path prefix
3442 const char* executablePath
= sExecPath
;
3443 char newPath
[strlen(executablePath
) + strlen(path
)];
3444 strcpy(newPath
, executablePath
);
3445 char* addPoint
= strrchr(newPath
,'/');
3446 if ( addPoint
!= NULL
)
3447 strcpy(&addPoint
[1], &path
[17]);
3449 strcpy(newPath
, &path
[17]);
3450 image
= loadPhase4(newPath
, orgPath
, context
, cacheIndex
, exceptions
);
3451 if ( image
!= NULL
)
3454 // perhaps main executable path is a sym link, find realpath and retry
3455 char resolvedPath
[PATH_MAX
];
3456 if ( realpath(sExecPath
, resolvedPath
) != NULL
) {
3457 char newRealPath
[strlen(resolvedPath
) + strlen(path
)];
3458 strcpy(newRealPath
, resolvedPath
);
3459 addPoint
= strrchr(newRealPath
,'/');
3460 if ( addPoint
!= NULL
)
3461 strcpy(&addPoint
[1], &path
[17]);
3463 strcpy(newRealPath
, &path
[17]);
3464 image
= loadPhase4(newRealPath
, orgPath
, context
, cacheIndex
, exceptions
);
3465 if ( image
!= NULL
)
3469 else if ( (strncmp(path
, "@loader_path/", 13) == 0) && (context
.origin
!= NULL
) ) {
3470 // @loader_path cannot be used from the main executable of a setuid process rdar://problem/4589305
3471 if ( !gLinkContext
.allowAtPaths
&& (strcmp(context
.origin
, sExecPath
) == 0) )
3472 throwf("unsafe use of @loader_path in %s with restricted binary", context
.origin
);
3473 // handle @loader_path path prefix
3474 char newPath
[strlen(context
.origin
) + strlen(path
)];
3475 strcpy(newPath
, context
.origin
);
3476 char* addPoint
= strrchr(newPath
,'/');
3477 if ( addPoint
!= NULL
)
3478 strcpy(&addPoint
[1], &path
[13]);
3480 strcpy(newPath
, &path
[13]);
3481 image
= loadPhase4(newPath
, orgPath
, context
, cacheIndex
, exceptions
);
3482 if ( image
!= NULL
)
3485 // perhaps loader path is a sym link, find realpath and retry
3486 char resolvedPath
[PATH_MAX
];
3487 if ( realpath(context
.origin
, resolvedPath
) != NULL
) {
3488 char newRealPath
[strlen(resolvedPath
) + strlen(path
)];
3489 strcpy(newRealPath
, resolvedPath
);
3490 addPoint
= strrchr(newRealPath
,'/');
3491 if ( addPoint
!= NULL
)
3492 strcpy(&addPoint
[1], &path
[13]);
3494 strcpy(newRealPath
, &path
[13]);
3495 image
= loadPhase4(newRealPath
, orgPath
, context
, cacheIndex
, exceptions
);
3496 if ( image
!= NULL
)
3500 else if ( context
.implicitRPath
|| (strncmp(path
, "@rpath/", 7) == 0) ) {
3501 const char* trailingPath
= (strncmp(path
, "@rpath/", 7) == 0) ? &path
[7] : path
;
3502 // substitute @rpath with all -rpath paths up the load chain
3503 for(const ImageLoader::RPathChain
* rp
=context
.rpath
; rp
!= NULL
; rp
=rp
->next
) {
3504 if (rp
->paths
!= NULL
) {
3505 for(std::vector
<const char*>::iterator it
=rp
->paths
->begin(); it
!= rp
->paths
->end(); ++it
) {
3506 const char* anRPath
= *it
;
3507 char newPath
[strlen(anRPath
) + strlen(trailingPath
)+2];
3508 strcpy(newPath
, anRPath
);
3509 if ( newPath
[strlen(newPath
)-1] != '/' )
3510 strcat(newPath
, "/");
3511 strcat(newPath
, trailingPath
);
3512 image
= loadPhase4(newPath
, orgPath
, context
, cacheIndex
, exceptions
);
3513 if ( gLinkContext
.verboseRPaths
&& (exceptions
!= NULL
) ) {
3514 if ( image
!= NULL
)
3515 dyld::log("RPATH successful expansion of %s to: %s\n", orgPath
, newPath
);
3517 dyld::log("RPATH failed to expanding %s to: %s\n", orgPath
, newPath
);
3519 if ( image
!= NULL
)
3525 // substitute @rpath with LD_LIBRARY_PATH
3526 if ( sEnv
.LD_LIBRARY_PATH
!= NULL
) {
3527 image
= loadPhase2(trailingPath
, orgPath
, context
, NULL
, sEnv
.LD_LIBRARY_PATH
, cacheIndex
, exceptions
);
3528 if ( image
!= NULL
)
3532 // if this is the "open" pass, don't try to open @rpath/... as a relative path
3533 if ( (exceptions
!= NULL
) && (trailingPath
!= path
) )
3536 else if ( !gLinkContext
.allowEnvVarsPath
&& (path
[0] != '/' ) ) {
3537 throwf("unsafe use of relative rpath %s in %s with restricted binary", path
, context
.origin
);
3540 return loadPhase4(path
, orgPath
, context
, cacheIndex
, exceptions
);
3543 static ImageLoader
* loadPhase2cache(const char* path
, const char *orgPath
, const LoadContext
& context
, unsigned& cacheIndex
, std::vector
<const char*>* exceptions
) {
3544 ImageLoader
* image
= NULL
;
3545 #if !TARGET_IPHONE_SIMULATOR
3546 if ( exceptions
!= NULL
) {
3547 char resolvedPath
[PATH_MAX
];
3548 realpath(path
, resolvedPath
);
3550 // If realpath() resolves to a path which does not exist on disk, errno is set to ENOENT
3551 if ( (myerr
== ENOENT
) || (myerr
== 0) )
3553 image
= loadPhase4(resolvedPath
, orgPath
, context
, cacheIndex
, exceptions
);
3562 static ImageLoader
* loadPhase2(const char* path
, const char* orgPath
, const LoadContext
& context
,
3563 const char* const frameworkPaths
[], const char* const libraryPaths
[],
3564 unsigned& cacheIndex
, std::vector
<const char*>* exceptions
)
3566 //dyld::log("%s(%s, %p)\n", __func__ , path, exceptions);
3567 ImageLoader
* image
= NULL
;
3568 const char* frameworkPartialPath
= getFrameworkPartialPath(path
);
3569 if ( frameworkPaths
!= NULL
) {
3570 if ( frameworkPartialPath
!= NULL
) {
3571 const size_t frameworkPartialPathLen
= strlen(frameworkPartialPath
);
3572 for(const char* const* fp
= frameworkPaths
; *fp
!= NULL
; ++fp
) {
3573 char npath
[strlen(*fp
)+frameworkPartialPathLen
+8];
3576 strcat(npath
, frameworkPartialPath
);
3577 //dyld::log("dyld: fallback framework path used: %s() -> loadPhase4(\"%s\", ...)\n", __func__, npath);
3578 image
= loadPhase4(npath
, orgPath
, context
, cacheIndex
, exceptions
);
3579 // Look in the cache if appropriate
3581 image
= loadPhase2cache(npath
, orgPath
, context
, cacheIndex
, exceptions
);
3582 if ( image
!= NULL
) {
3583 // if original path is in the dyld cache, then mark this one found as an override
3584 dyld3::SharedCacheFindDylibResults shareCacheResults
;
3585 if ( dyld3::findInSharedCacheImage(sSharedCacheLoadInfo
, path
, &shareCacheResults
) && (shareCacheResults
.image
!= nullptr) )
3586 image
->setOverridesCachedDylib(shareCacheResults
.image
->imageNum());
3592 // <rdar://problem/12649639> An executable with the same name as a framework & DYLD_LIBRARY_PATH pointing to it gets loaded twice
3593 // <rdar://problem/14160846> Some apps depend on frameworks being found via library paths
3594 if ( (libraryPaths
!= NULL
) && ((frameworkPartialPath
== NULL
) || sFrameworksFoundAsDylibs
) ) {
3595 const char* libraryLeafName
= getLibraryLeafName(path
);
3596 const size_t libraryLeafNameLen
= strlen(libraryLeafName
);
3597 for(const char* const* lp
= libraryPaths
; *lp
!= NULL
; ++lp
) {
3598 char libpath
[strlen(*lp
)+libraryLeafNameLen
+8];
3599 strcpy(libpath
, *lp
);
3600 strcat(libpath
, "/");
3601 strcat(libpath
, libraryLeafName
);
3602 //dyld::log("dyld: fallback library path used: %s() -> loadPhase4(\"%s\", ...)\n", __func__, libpath);
3603 image
= loadPhase4(libpath
, orgPath
, context
, cacheIndex
, exceptions
);
3604 // Look in the cache if appropriate
3606 image
= loadPhase2cache(libpath
, orgPath
, context
, cacheIndex
, exceptions
);
3607 if ( image
!= NULL
) {
3608 // if original path is in the dyld cache, then mark this one found as an override
3609 dyld3::SharedCacheFindDylibResults shareCacheResults
;
3610 if ( dyld3::findInSharedCacheImage(sSharedCacheLoadInfo
, path
, &shareCacheResults
) && (shareCacheResults
.image
!= nullptr) )
3611 image
->setOverridesCachedDylib(shareCacheResults
.image
->imageNum());
3619 // try search overrides and fallbacks
3620 static ImageLoader
* loadPhase1(const char* path
, const char* orgPath
, const LoadContext
& context
, unsigned& cacheIndex
, std::vector
<const char*>* exceptions
)
3622 //dyld::log("%s(%s, %p)\n", __func__ , path, exceptions);
3623 ImageLoader
* image
= NULL
;
3625 // handle LD_LIBRARY_PATH environment variables that force searching
3626 if ( context
.useLdLibraryPath
&& (sEnv
.LD_LIBRARY_PATH
!= NULL
) ) {
3627 image
= loadPhase2(path
, orgPath
, context
, NULL
, sEnv
.LD_LIBRARY_PATH
, cacheIndex
,exceptions
);
3628 if ( image
!= NULL
)
3632 // handle DYLD_ environment variables that force searching
3633 if ( context
.useSearchPaths
&& ((sEnv
.DYLD_FRAMEWORK_PATH
!= NULL
) || (sEnv
.DYLD_LIBRARY_PATH
!= NULL
)) ) {
3634 image
= loadPhase2(path
, orgPath
, context
, sEnv
.DYLD_FRAMEWORK_PATH
, sEnv
.DYLD_LIBRARY_PATH
, cacheIndex
, exceptions
);
3635 if ( image
!= NULL
)
3640 image
= loadPhase3(path
, orgPath
, context
, cacheIndex
, exceptions
);
3641 if ( image
!= NULL
)
3644 // try fallback paths during second time (will open file)
3645 const char* const* fallbackLibraryPaths
= sEnv
.DYLD_FALLBACK_LIBRARY_PATH
;
3646 if ( (fallbackLibraryPaths
!= NULL
) && !context
.useFallbackPaths
)
3647 fallbackLibraryPaths
= NULL
;
3648 if ( !context
.dontLoad
&& (exceptions
!= NULL
) && ((sEnv
.DYLD_FALLBACK_FRAMEWORK_PATH
!= NULL
) || (fallbackLibraryPaths
!= NULL
)) ) {
3649 image
= loadPhase2(path
, orgPath
, context
, sEnv
.DYLD_FALLBACK_FRAMEWORK_PATH
, fallbackLibraryPaths
, cacheIndex
, exceptions
);
3650 if ( image
!= NULL
)
3657 // try root substitutions
3658 static ImageLoader
* loadPhase0(const char* path
, const char* orgPath
, const LoadContext
& context
, unsigned& cacheIndex
, std::vector
<const char*>* exceptions
)
3660 //dyld::log("%s(%s, %p)\n", __func__ , path, exceptions);
3662 #if __MAC_OS_X_VERSION_MIN_REQUIRED
3663 // handle macOS dylibs dlopen()ing versioned path which needs to map to flat path in mazipan simulator
3664 if ( gLinkContext
.marzipan
&& strstr(path
, ".framework/Versions/")) {
3665 uintptr_t sourceOffset
= 0;
3666 uintptr_t destOffset
= 0;
3667 size_t sourceLangth
= strlen(path
);
3668 char flatPath
[sourceLangth
];
3670 const char* frameworkBase
= NULL
;
3671 while ((frameworkBase
= strstr(&path
[sourceOffset
], ".framework/Versions/"))) {
3672 uintptr_t foundLength
= (frameworkBase
- &path
[sourceOffset
]) + strlen(".framework/") ;
3673 strlcat(&flatPath
[destOffset
], &path
[sourceOffset
], foundLength
);
3674 sourceOffset
+= foundLength
+ strlen("Versions/") + 1;
3675 destOffset
+= foundLength
- 1;
3677 strlcat(&flatPath
[destOffset
], &path
[sourceOffset
], sourceLangth
);
3678 ImageLoader
* image
= loadPhase0(flatPath
, orgPath
, context
, cacheIndex
, exceptions
);
3679 if ( image
!= NULL
)
3684 #if SUPPORT_ROOT_PATH
3685 // handle DYLD_ROOT_PATH which forces absolute paths to use a new root
3686 if ( (gLinkContext
.rootPaths
!= NULL
) && (path
[0] == '/') ) {
3687 for(const char* const* rootPath
= gLinkContext
.rootPaths
; *rootPath
!= NULL
; ++rootPath
) {
3688 size_t rootLen
= strlen(*rootPath
);
3689 if ( strncmp(path
, *rootPath
, rootLen
) != 0 ) {
3690 char newPath
[rootLen
+ strlen(path
)+2];
3691 strcpy(newPath
, *rootPath
);
3692 strcat(newPath
, path
);
3693 ImageLoader
* image
= loadPhase1(newPath
, orgPath
, context
, cacheIndex
, exceptions
);
3694 if ( image
!= NULL
)
3702 return loadPhase1(path
, orgPath
, context
, cacheIndex
, exceptions
);
3705 static bool cacheablePath(const char* path
) {
3706 if (strncmp(path
, "/usr/lib/", 9) == 0)
3708 if (strncmp(path
, "/System/Library/", 16) == 0)
3714 // Given all the DYLD_ environment variables, the general case for loading libraries
3715 // is that any given path expands into a list of possible locations to load. We
3716 // also must take care to ensure two copies of the "same" library are never loaded.
3718 // The algorithm used here is that there is a separate function for each "phase" of the
3719 // path expansion. Each phase function calls the next phase with each possible expansion
3720 // of that phase. The result is the last phase is called with all possible paths.
3722 // To catch duplicates the algorithm is run twice. The first time, the last phase checks
3723 // the path against all loaded images. The second time, the last phase calls open() on
3724 // the path. Either time, if an image is found, the phases all unwind without checking
3727 ImageLoader
* load(const char* path
, const LoadContext
& context
, unsigned& cacheIndex
)
3729 CRSetCrashLogMessage2(path
);
3730 const char* orgPath
= path
;
3731 cacheIndex
= UINT32_MAX
;
3733 //dyld::log("%s(%s)\n", __func__ , path);
3734 char realPath
[PATH_MAX
];
3735 // when DYLD_IMAGE_SUFFIX is in used, do a realpath(), otherwise a load of "Foo.framework/Foo" will not match
3736 if ( context
.useSearchPaths
&& ( gLinkContext
.imageSuffix
!= NULL
&& *gLinkContext
.imageSuffix
!= NULL
) ) {
3737 if ( realpath(path
, realPath
) != NULL
)
3741 // try all path permutations and check against existing loaded images
3743 ImageLoader
* image
= loadPhase0(path
, orgPath
, context
, cacheIndex
, NULL
);
3744 if ( image
!= NULL
) {
3745 CRSetCrashLogMessage2(NULL
);
3749 // try all path permutations and try open() until first success
3750 std::vector
<const char*> exceptions
;
3751 image
= loadPhase0(path
, orgPath
, context
, cacheIndex
, &exceptions
);
3752 #if !TARGET_IPHONE_SIMULATOR
3753 // <rdar://problem/16704628> support symlinks on disk to a path in dyld shared cache
3755 image
= loadPhase2cache(path
, orgPath
, context
, cacheIndex
, &exceptions
);
3757 CRSetCrashLogMessage2(NULL
);
3758 if ( image
!= NULL
) {
3759 // <rdar://problem/6916014> leak in dyld during dlopen when using DYLD_ variables
3760 for (std::vector
<const char*>::iterator it
= exceptions
.begin(); it
!= exceptions
.end(); ++it
) {
3763 // if loaded image is not from cache, but original path is in cache
3764 // set gSharedCacheOverridden flag to disable some ObjC optimizations
3765 if ( !gSharedCacheOverridden
&& !image
->inSharedCache() && image
->isDylib() && cacheablePath(path
) && inSharedCache(path
) ) {
3766 gSharedCacheOverridden
= true;
3770 else if ( exceptions
.size() == 0 ) {
3771 if ( context
.dontLoad
) {
3775 throw "image not found";
3778 const char* msgStart
= "no suitable image found. Did find:";
3779 const char* delim
= "\n\t";
3780 size_t allsizes
= strlen(msgStart
)+8;
3781 for (size_t i
=0; i
< exceptions
.size(); ++i
)
3782 allsizes
+= (strlen(exceptions
[i
]) + strlen(delim
));
3783 char* fullMsg
= new char[allsizes
];
3784 strcpy(fullMsg
, msgStart
);
3785 for (size_t i
=0; i
< exceptions
.size(); ++i
) {
3786 strcat(fullMsg
, delim
);
3787 strcat(fullMsg
, exceptions
[i
]);
3788 free((void*)exceptions
[i
]);
3790 throw (const char*)fullMsg
;
3798 static void mapSharedCache()
3800 dyld3::SharedCacheOptions opts
;
3801 opts
.cacheDirOverride
= sSharedCacheOverrideDir
;
3802 opts
.forcePrivate
= (gLinkContext
.sharedRegionMode
== ImageLoader::kUsePrivateSharedRegion
);
3805 #if __x86_64__ && !TARGET_IPHONE_SIMULATOR
3806 opts
.useHaswell
= sHaswell
;
3808 opts
.useHaswell
= false;
3810 opts
.verbose
= gLinkContext
.verboseMapping
;
3811 loadDyldCache(opts
, &sSharedCacheLoadInfo
);
3813 // update global state
3814 if ( sSharedCacheLoadInfo
.loadAddress
!= nullptr ) {
3815 gLinkContext
.dyldCache
= sSharedCacheLoadInfo
.loadAddress
;
3816 dyld::gProcessInfo
->processDetachedFromSharedRegion
= opts
.forcePrivate
;
3817 dyld::gProcessInfo
->sharedCacheSlide
= sSharedCacheLoadInfo
.slide
;
3818 dyld::gProcessInfo
->sharedCacheBaseAddress
= (unsigned long)sSharedCacheLoadInfo
.loadAddress
;
3819 sSharedCacheLoadInfo
.loadAddress
->getUUID(dyld::gProcessInfo
->sharedCacheUUID
);
3820 dyld3::kdebug_trace_dyld_image(DBG_DYLD_UUID_SHARED_CACHE_A
, (const uuid_t
*)&dyld::gProcessInfo
->sharedCacheUUID
[0], {0,0}, {{ 0, 0 }}, (const mach_header
*)sSharedCacheLoadInfo
.loadAddress
);
3823 //#if __IPHONE_OS_VERSION_MIN_REQUIRED && !TARGET_IPHONE_SIMULATOR
3824 // RAM disk booting does not have shared cache yet
3825 // Don't make lack of a shared cache fatal in that case
3826 // if ( sSharedCacheLoadInfo.loadAddress == nullptr ) {
3827 // if ( sSharedCacheLoadInfo.errorMessage != nullptr )
3828 // halt(sSharedCacheLoadInfo.errorMessage);
3830 // halt("error loading dyld shared cache");
3837 // create when NSLinkModule is called for a second time on a bundle
3838 ImageLoader
* cloneImage(ImageLoader
* image
)
3840 // open file (automagically closed when this function exits)
3841 FileOpener
file(image
->getPath());
3843 struct stat stat_buf
;
3844 if ( fstat(file
.getFileDescriptor(), &stat_buf
) == -1)
3847 dyld::LoadContext context
;
3848 context
.useSearchPaths
= false;
3849 context
.useFallbackPaths
= false;
3850 context
.useLdLibraryPath
= false;
3851 context
.implicitRPath
= false;
3852 context
.matchByInstallName
= false;
3853 context
.dontLoad
= false;
3854 context
.mustBeBundle
= true;
3855 context
.mustBeDylib
= false;
3856 context
.canBePIE
= false;
3857 context
.enforceIOSMac
= false;
3858 context
.origin
= NULL
;
3859 context
.rpath
= NULL
;
3860 return loadPhase6(file
.getFileDescriptor(), stat_buf
, image
->getPath(), context
);
3864 ImageLoader
* loadFromMemory(const uint8_t* mem
, uint64_t len
, const char* moduleName
)
3866 // if fat wrapper, find usable sub-file
3867 const fat_header
* memStartAsFat
= (fat_header
*)mem
;
3868 uint64_t fileOffset
= 0;
3869 uint64_t fileLength
= len
;
3870 if ( memStartAsFat
->magic
== OSSwapBigToHostInt32(FAT_MAGIC
) ) {
3871 if ( fatFindBest(memStartAsFat
, &fileOffset
, &fileLength
) ) {
3872 mem
= &mem
[fileOffset
];
3876 throw "no matching architecture in universal wrapper";
3881 if ( isCompatibleMachO(mem
, moduleName
) ) {
3882 ImageLoader
* image
= ImageLoaderMachO::instantiateFromMemory(moduleName
, (macho_header
*)mem
, len
, gLinkContext
);
3883 // don't add bundles to global list, they can be loaded but not linked. When linked it will be added to list
3884 if ( ! image
->isBundle() )
3889 // try other file formats here...
3891 // throw error about what was found
3892 switch (*(uint32_t*)mem
) {
3897 throw "mach-o, but wrong architecture";
3899 throwf("unknown file type, first eight bytes: 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X",
3900 mem
[0], mem
[1], mem
[2], mem
[3], mem
[4], mem
[5], mem
[6],mem
[7]);
3905 void registerAddCallback(ImageCallback func
)
3907 // now add to list to get notified when any more images are added
3908 sAddImageCallbacks
.push_back(func
);
3910 // call callback with all existing images
3911 for (std::vector
<ImageLoader
*>::iterator it
=sAllImages
.begin(); it
!= sAllImages
.end(); it
++) {
3912 ImageLoader
* image
= *it
;
3913 if ( image
->getState() >= dyld_image_state_bound
&& image
->getState() < dyld_image_state_terminated
) {
3914 dyld3::ScopedTimer
timer(DBG_DYLD_TIMING_FUNC_FOR_ADD_IMAGE
, (uint64_t)image
->machHeader(), (uint64_t)(*func
), 0);
3915 (*func
)(image
->machHeader(), image
->getSlide());
3918 #if SUPPORT_ACCELERATE_TABLES
3919 if ( sAllCacheImagesProxy
!= NULL
) {
3920 dyld_image_info infos
[allImagesCount()+1];
3921 unsigned cacheCount
= sAllCacheImagesProxy
->appendImagesToNotify(dyld_image_state_bound
, true, infos
);
3922 for (unsigned i
=0; i
< cacheCount
; ++i
) {
3923 dyld3::ScopedTimer
timer(DBG_DYLD_TIMING_FUNC_FOR_ADD_IMAGE
, (uint64_t)infos
[i
].imageLoadAddress
, (uint64_t)(*func
), 0);
3924 (*func
)(infos
[i
].imageLoadAddress
, sSharedCacheLoadInfo
.slide
);
3930 void registerLoadCallback(LoadImageCallback func
)
3932 // now add to list to get notified when any more images are added
3933 sAddLoadImageCallbacks
.push_back(func
);
3935 // call callback with all existing images
3936 for (ImageLoader
* image
: sAllImages
) {
3937 if ( image
->getState() >= dyld_image_state_bound
&& image
->getState() < dyld_image_state_terminated
) {
3938 dyld3::ScopedTimer
timer(DBG_DYLD_TIMING_FUNC_FOR_ADD_IMAGE
, (uint64_t)image
->machHeader(), (uint64_t)(*func
), 0);
3939 (*func
)(image
->machHeader(), image
->getPath(), !image
->neverUnload());
3942 #if SUPPORT_ACCELERATE_TABLES
3943 if ( sAllCacheImagesProxy
!= NULL
) {
3944 dyld_image_info infos
[allImagesCount()+1];
3945 unsigned cacheCount
= sAllCacheImagesProxy
->appendImagesToNotify(dyld_image_state_bound
, true, infos
);
3946 for (unsigned i
=0; i
< cacheCount
; ++i
) {
3947 dyld3::ScopedTimer
timer(DBG_DYLD_TIMING_FUNC_FOR_ADD_IMAGE
, (uint64_t)infos
[i
].imageLoadAddress
, (uint64_t)(*func
), 0);
3948 (*func
)(infos
[i
].imageLoadAddress
, infos
[i
].imageFilePath
, false);
3954 void registerRemoveCallback(ImageCallback func
)
3956 // <rdar://problem/15025198> ignore calls to register a notification during a notification
3957 if ( sRemoveImageCallbacksInUse
)
3959 sRemoveImageCallbacks
.push_back(func
);
3962 void clearErrorMessage()
3964 error_string
[0] = '\0';
3967 void setErrorMessage(const char* message
)
3969 // save off error message in global buffer for CrashReporter to find
3970 strlcpy(error_string
, message
, sizeof(error_string
));
3973 const char* getErrorMessage()
3975 return error_string
;
3978 void halt(const char* message
)
3980 dyld::log("dyld: %s\n", message
);
3981 setErrorMessage(message
);
3982 dyld::gProcessInfo
->errorMessage
= error_string
;
3983 if ( !gLinkContext
.startedInitializingMainExecutable
)
3984 dyld::gProcessInfo
->terminationFlags
= 1;
3986 dyld::gProcessInfo
->terminationFlags
= 0;
3988 char payloadBuffer
[EXIT_REASON_PAYLOAD_MAX_LEN
];
3989 dyld_abort_payload
* payload
= (dyld_abort_payload
*)payloadBuffer
;
3990 payload
->version
= 1;
3991 payload
->flags
= gLinkContext
.startedInitializingMainExecutable
? 0 : 1;
3992 payload
->targetDylibPathOffset
= 0;
3993 payload
->clientPathOffset
= 0;
3994 payload
->symbolOffset
= 0;
3995 int payloadSize
= sizeof(dyld_abort_payload
);
3997 if ( dyld::gProcessInfo
->errorTargetDylibPath
!= NULL
) {
3998 payload
->targetDylibPathOffset
= payloadSize
;
3999 payloadSize
+= strlcpy(&payloadBuffer
[payloadSize
], dyld::gProcessInfo
->errorTargetDylibPath
, sizeof(payloadBuffer
)-payloadSize
) + 1;
4001 if ( dyld::gProcessInfo
->errorClientOfDylibPath
!= NULL
) {
4002 payload
->clientPathOffset
= payloadSize
;
4003 payloadSize
+= strlcpy(&payloadBuffer
[payloadSize
], dyld::gProcessInfo
->errorClientOfDylibPath
, sizeof(payloadBuffer
)-payloadSize
) + 1;
4005 if ( dyld::gProcessInfo
->errorSymbol
!= NULL
) {
4006 payload
->symbolOffset
= payloadSize
;
4007 payloadSize
+= strlcpy(&payloadBuffer
[payloadSize
], dyld::gProcessInfo
->errorSymbol
, sizeof(payloadBuffer
)-payloadSize
) + 1;
4009 char truncMessage
[EXIT_REASON_USER_DESC_MAX_LEN
];
4010 strlcpy(truncMessage
, message
, EXIT_REASON_USER_DESC_MAX_LEN
);
4011 abort_with_payload(OS_REASON_DYLD
, dyld::gProcessInfo
->errorKind
? dyld::gProcessInfo
->errorKind
: DYLD_EXIT_REASON_OTHER
, payloadBuffer
, payloadSize
, truncMessage
, 0);
4014 static void setErrorStrings(unsigned errorCode
, const char* errorClientOfDylibPath
,
4015 const char* errorTargetDylibPath
, const char* errorSymbol
)
4017 dyld::gProcessInfo
->errorKind
= errorCode
;
4018 dyld::gProcessInfo
->errorClientOfDylibPath
= errorClientOfDylibPath
;
4019 dyld::gProcessInfo
->errorTargetDylibPath
= errorTargetDylibPath
;
4020 dyld::gProcessInfo
->errorSymbol
= errorSymbol
;
4024 uintptr_t bindLazySymbol(const mach_header
* mh
, uintptr_t* lazyPointer
)
4026 uintptr_t result
= 0;
4027 // acquire read-lock on dyld's data structures
4028 #if 0 // rdar://problem/3811777 turn off locking until deadlock is resolved
4029 if ( gLibSystemHelpers
!= NULL
)
4030 (*gLibSystemHelpers
->lockForReading
)();
4032 // lookup and bind lazy pointer and get target address
4034 ImageLoader
* target
;
4036 // fast stubs pass NULL for mh and image is instead found via the location of stub (aka lazyPointer)
4038 target
= dyld::findImageContainingAddress(lazyPointer
);
4040 target
= dyld::findImageByMachHeader(mh
);
4042 // note, target should always be mach-o, because only mach-o lazy handler wired up to this
4043 target
= dyld::findImageByMachHeader(mh
);
4045 if ( target
== NULL
)
4046 throwf("image not found for lazy pointer at %p", lazyPointer
);
4047 result
= target
->doBindLazySymbol(lazyPointer
, gLinkContext
);
4049 catch (const char* message
) {
4050 dyld::log("dyld: lazy symbol binding failed: %s\n", message
);
4053 // release read-lock on dyld's data structures
4055 if ( gLibSystemHelpers
!= NULL
)
4056 (*gLibSystemHelpers
->unlockForReading
)();
4058 // return target address to glue which jumps to it with real parameters restored
4063 uintptr_t fastBindLazySymbol(ImageLoader
** imageLoaderCache
, uintptr_t lazyBindingInfoOffset
)
4065 uintptr_t result
= 0;
4067 if ( *imageLoaderCache
== NULL
) {
4069 *imageLoaderCache
= dyld::findMappedRange((uintptr_t)imageLoaderCache
);
4070 if ( *imageLoaderCache
== NULL
) {
4071 #if SUPPORT_ACCELERATE_TABLES
4072 if ( sAllCacheImagesProxy
!= NULL
) {
4073 const mach_header
* mh
;
4076 if ( sAllCacheImagesProxy
->addressInCache(imageLoaderCache
, &mh
, &path
, &index
) ) {
4077 result
= sAllCacheImagesProxy
->bindLazy(lazyBindingInfoOffset
, gLinkContext
, mh
, index
);
4078 if ( result
== 0 ) {
4079 halt("dyld: lazy symbol binding failed for image in dyld shared\n");
4085 const char* message
= "fast lazy binding from unknown image";
4086 dyld::log("dyld: %s\n", message
);
4091 // bind lazy pointer and return it
4093 result
= (*imageLoaderCache
)->doBindFastLazySymbol((uint32_t)lazyBindingInfoOffset
, gLinkContext
,
4094 (dyld::gLibSystemHelpers
!= NULL
) ? dyld::gLibSystemHelpers
->acquireGlobalDyldLock
: NULL
,
4095 (dyld::gLibSystemHelpers
!= NULL
) ? dyld::gLibSystemHelpers
->releaseGlobalDyldLock
: NULL
);
4097 catch (const char* message
) {
4098 dyld::log("dyld: lazy symbol binding failed: %s\n", message
);
4102 // return target address to glue which jumps to it with real parameters restored
4108 void registerUndefinedHandler(UndefinedHandler handler
)
4110 sUndefinedHandler
= handler
;
4113 static void undefinedHandler(const char* symboName
)
4115 if ( sUndefinedHandler
!= NULL
) {
4116 (*sUndefinedHandler
)(symboName
);
4120 static bool findExportedSymbol(const char* name
, bool onlyInCoalesced
, const ImageLoader::Symbol
** sym
, const ImageLoader
** image
, ImageLoader::CoalesceNotifier notifier
=NULL
)
4122 // search all images in order
4123 const ImageLoader
* firstWeakImage
= NULL
;
4124 const ImageLoader::Symbol
* firstWeakSym
= NULL
;
4125 const ImageLoader
* firstNonWeakImage
= NULL
;
4126 const ImageLoader::Symbol
* firstNonWeakSym
= NULL
;
4127 const size_t imageCount
= sAllImages
.size();
4128 for(size_t i
=0; i
< imageCount
; ++i
) {
4129 ImageLoader
* anImage
= sAllImages
[i
];
4130 // the use of inserted libraries alters search order
4131 // so that inserted libraries are found before the main executable
4132 if ( sInsertedDylibCount
> 0 ) {
4133 if ( i
< sInsertedDylibCount
)
4134 anImage
= sAllImages
[i
+1];
4135 else if ( i
== sInsertedDylibCount
)
4136 anImage
= sAllImages
[0];
4138 //dyld::log("findExportedSymbol(%s) looking at %s\n", name, anImage->getPath());
4139 if ( ! anImage
->hasHiddenExports() && (!onlyInCoalesced
|| anImage
->hasCoalescedExports()) ) {
4140 const ImageLoader
* foundInImage
;
4141 *sym
= anImage
->findExportedSymbol(name
, false, &foundInImage
);
4142 //dyld::log("findExportedSymbol(%s) found: sym=%p, anImage=%p, foundInImage=%p\n", name, *sym, anImage, foundInImage /*, (foundInImage ? foundInImage->getPath() : "")*/);
4143 if ( *sym
!= NULL
) {
4144 if ( notifier
&& (foundInImage
== anImage
) )
4145 notifier(*sym
, foundInImage
, foundInImage
->machHeader());
4146 // if weak definition found, record first one found
4147 if ( (foundInImage
->getExportedSymbolInfo(*sym
) & ImageLoader::kWeakDefinition
) != 0 ) {
4148 if ( firstWeakImage
== NULL
) {
4149 firstWeakImage
= foundInImage
;
4150 firstWeakSym
= *sym
;
4155 if ( !onlyInCoalesced
) {
4156 // for flat lookups, return first found
4157 *image
= foundInImage
;
4160 if ( firstNonWeakImage
== NULL
) {
4161 firstNonWeakImage
= foundInImage
;
4162 firstNonWeakSym
= *sym
;
4168 if ( firstNonWeakImage
!= NULL
) {
4169 // found a weak definition, but no non-weak, so return first weak found
4170 *sym
= firstNonWeakSym
;
4171 *image
= firstNonWeakImage
;
4174 if ( firstWeakSym
!= NULL
) {
4175 // found a weak definition, but no non-weak, so return first weak found
4176 *sym
= firstWeakSym
;
4177 *image
= firstWeakImage
;
4180 #if SUPPORT_ACCELERATE_TABLES
4181 if ( sAllCacheImagesProxy
!= NULL
) {
4182 if ( sAllCacheImagesProxy
->flatFindSymbol(name
, onlyInCoalesced
, sym
, image
, notifier
) )
4190 bool flatFindExportedSymbol(const char* name
, const ImageLoader::Symbol
** sym
, const ImageLoader
** image
)
4192 return findExportedSymbol(name
, false, sym
, image
);
4195 bool findCoalescedExportedSymbol(const char* name
, const ImageLoader::Symbol
** sym
, const ImageLoader
** image
, ImageLoader::CoalesceNotifier notifier
)
4197 return findExportedSymbol(name
, true, sym
, image
, notifier
);
4201 bool flatFindExportedSymbolWithHint(const char* name
, const char* librarySubstring
, const ImageLoader::Symbol
** sym
, const ImageLoader
** image
)
4203 // search all images in order
4204 const size_t imageCount
= sAllImages
.size();
4205 for(size_t i
=0; i
< imageCount
; ++i
){
4206 ImageLoader
* anImage
= sAllImages
[i
];
4207 // only look at images whose paths contain the hint string (NULL hint string is wildcard)
4208 if ( ! anImage
->isBundle() && ((librarySubstring
==NULL
) || (strstr(anImage
->getPath(), librarySubstring
) != NULL
)) ) {
4209 *sym
= anImage
->findExportedSymbol(name
, false, image
);
4210 if ( *sym
!= NULL
) {
4219 unsigned int getCoalescedImages(ImageLoader
* images
[], unsigned imageIndex
[])
4221 unsigned int count
= 0;
4222 for (std::vector
<ImageLoader
*>::iterator it
=sAllImages
.begin(); it
!= sAllImages
.end(); it
++) {
4223 ImageLoader
* image
= *it
;
4224 if ( image
->participatesInCoalescing() ) {
4225 images
[count
] = *it
;
4226 imageIndex
[count
] = 0;
4230 #if SUPPORT_ACCELERATE_TABLES
4231 if ( sAllCacheImagesProxy
!= NULL
) {
4232 sAllCacheImagesProxy
->appendImagesNeedingCoalescing(images
, imageIndex
, count
);
4239 static ImageLoader::MappedRegion
* getMappedRegions(ImageLoader::MappedRegion
* regions
)
4241 ImageLoader::MappedRegion
* end
= regions
;
4242 for (std::vector
<ImageLoader
*>::iterator it
=sAllImages
.begin(); it
!= sAllImages
.end(); it
++) {
4243 (*it
)->getMappedRegions(end
);
4248 void registerImageStateSingleChangeHandler(dyld_image_states state
, dyld_image_state_change_handler handler
)
4250 // mark the image that the handler is in as never-unload because dyld has a reference into it
4251 ImageLoader
* handlerImage
= findImageContainingAddress((void*)handler
);
4252 if ( handlerImage
!= NULL
)
4253 handlerImage
->setNeverUnload();
4255 // add to list of handlers
4256 std::vector
<dyld_image_state_change_handler
>* handlers
= stateToHandlers(state
, sSingleHandlers
);
4257 if ( handlers
!= NULL
) {
4258 // <rdar://problem/10332417> need updateAllImages() to be last in dyld_image_state_mapped list
4259 // so that if ObjC adds a handler that prevents a load, it happens before the gdb list is updated
4260 if ( state
== dyld_image_state_mapped
)
4261 handlers
->insert(handlers
->begin(), handler
);
4263 handlers
->push_back(handler
);
4265 // call callback with all existing images
4266 for (std::vector
<ImageLoader
*>::iterator it
=sAllImages
.begin(); it
!= sAllImages
.end(); it
++) {
4267 ImageLoader
* image
= *it
;
4268 dyld_image_info info
;
4269 info
.imageLoadAddress
= image
->machHeader();
4270 info
.imageFilePath
= image
->getRealPath();
4271 info
.imageFileModDate
= image
->lastModified();
4272 // should only call handler if state == image->state
4273 if ( image
->getState() == state
)
4274 (*handler
)(state
, 1, &info
);
4275 // ignore returned string, too late to do anything
4280 void registerImageStateBatchChangeHandler(dyld_image_states state
, dyld_image_state_change_handler handler
)
4282 // mark the image that the handler is in as never-unload because dyld has a reference into it
4283 ImageLoader
* handlerImage
= findImageContainingAddress((void*)handler
);
4284 if ( handlerImage
!= NULL
)
4285 handlerImage
->setNeverUnload();
4287 // add to list of handlers
4288 std::vector
<dyld_image_state_change_handler
>* handlers
= stateToHandlers(state
, sBatchHandlers
);
4289 if ( handlers
!= NULL
) {
4290 // insert at front, so that gdb handler is always last
4291 handlers
->insert(handlers
->begin(), handler
);
4293 // call callback with all existing images
4295 notifyBatchPartial(state
, true, handler
, false, false);
4297 catch (const char* msg
) {
4298 // ignore request to abort during registration
4304 void registerObjCNotifiers(_dyld_objc_notify_mapped mapped
, _dyld_objc_notify_init init
, _dyld_objc_notify_unmapped unmapped
)
4306 // record functions to call
4307 sNotifyObjCMapped
= mapped
;
4308 sNotifyObjCInit
= init
;
4309 sNotifyObjCUnmapped
= unmapped
;
4311 // call 'mapped' function with all images mapped so far
4313 notifyBatchPartial(dyld_image_state_bound
, true, NULL
, false, true);
4315 catch (const char* msg
) {
4316 // ignore request to abort during registration
4319 // <rdar://problem/32209809> call 'init' function on all images already init'ed (below libSystem)
4320 for (std::vector
<ImageLoader
*>::iterator it
=sAllImages
.begin(); it
!= sAllImages
.end(); it
++) {
4321 ImageLoader
* image
= *it
;
4322 if ( (image
->getState() == dyld_image_state_initialized
) && image
->notifyObjC() ) {
4323 dyld3::ScopedTimer
timer(DBG_DYLD_TIMING_OBJC_INIT
, (uint64_t)image
->machHeader(), 0, 0);
4324 (*sNotifyObjCInit
)(image
->getRealPath(), image
->machHeader());
4329 bool sharedCacheUUID(uuid_t uuid
)
4331 if ( sSharedCacheLoadInfo
.loadAddress
== nullptr )
4334 sSharedCacheLoadInfo
.loadAddress
->getUUID(uuid
);
4338 #if SUPPORT_ACCELERATE_TABLES
4340 bool dlopenFromCache(const char* path
, int mode
, void** handle
)
4342 if ( sAllCacheImagesProxy
== NULL
)
4344 char fallbackPath
[PATH_MAX
];
4345 bool result
= sAllCacheImagesProxy
->dlopenFromCache(gLinkContext
, path
, mode
, handle
);
4346 if ( !result
&& (strchr(path
, '/') == NULL
) ) {
4347 // POSIX says you can call dlopen() with a leaf name (e.g. dlopen("libz.dylb"))
4348 strcpy(fallbackPath
, "/usr/lib/");
4349 strlcat(fallbackPath
, path
, PATH_MAX
);
4350 result
= sAllCacheImagesProxy
->dlopenFromCache(gLinkContext
, fallbackPath
, mode
, handle
);
4352 path
= fallbackPath
;
4355 // leaf name could be a symlink
4356 char resolvedPath
[PATH_MAX
];
4357 realpath(path
, resolvedPath
);
4358 int realpathErrno
= errno
;
4359 // If realpath() resolves to a path which does not exist on disk, errno is set to ENOENT
4360 if ( (realpathErrno
== ENOENT
) || (realpathErrno
== 0) ) {
4361 result
= sAllCacheImagesProxy
->dlopenFromCache(gLinkContext
, resolvedPath
, mode
, handle
);
4368 bool makeCacheHandle(ImageLoader
* image
, unsigned cacheIndex
, int mode
, void** result
)
4370 if ( sAllCacheImagesProxy
== NULL
)
4372 return sAllCacheImagesProxy
->makeCacheHandle(gLinkContext
, cacheIndex
, mode
, result
);
4375 bool isCacheHandle(void* handle
)
4377 if ( sAllCacheImagesProxy
== NULL
)
4379 return sAllCacheImagesProxy
->isCacheHandle(handle
, NULL
, NULL
);
4382 bool isPathInCache(const char* path
)
4384 if ( sAllCacheImagesProxy
== NULL
)
4387 return sAllCacheImagesProxy
->hasDylib(path
, &index
);
4390 const char* getPathFromIndex(unsigned cacheIndex
)
4392 if ( sAllCacheImagesProxy
== NULL
)
4394 return sAllCacheImagesProxy
->getIndexedPath(cacheIndex
);
4397 void* dlsymFromCache(void* handle
, const char* symName
, unsigned index
)
4399 if ( sAllCacheImagesProxy
== NULL
)
4401 return sAllCacheImagesProxy
->dlsymFromCache(gLinkContext
, handle
, symName
, index
);
4404 bool addressInCache(const void* address
, const mach_header
** mh
, const char** path
, unsigned* index
)
4406 if ( sAllCacheImagesProxy
== NULL
)
4409 return sAllCacheImagesProxy
->addressInCache(address
, mh
, path
, index
? index
: &ignore
);
4412 bool findUnwindSections(const void* addr
, dyld_unwind_sections
* info
)
4414 if ( sAllCacheImagesProxy
== NULL
)
4416 return sAllCacheImagesProxy
->findUnwindSections(addr
, info
);
4419 bool dladdrFromCache(const void* address
, Dl_info
* info
)
4421 if ( sAllCacheImagesProxy
== NULL
)
4423 return sAllCacheImagesProxy
->dladdrFromCache(address
, info
);
4427 static ImageLoader
* libraryLocator(const char* libraryName
, bool search
, const char* origin
, const ImageLoader::RPathChain
* rpaths
, bool enforceIOSMac
, unsigned& cacheIndex
)
4429 dyld::LoadContext context
;
4430 context
.useSearchPaths
= search
;
4431 context
.useFallbackPaths
= search
;
4432 context
.useLdLibraryPath
= false;
4433 context
.implicitRPath
= false;
4434 context
.matchByInstallName
= false;
4435 context
.dontLoad
= false;
4436 context
.mustBeBundle
= false;
4437 context
.mustBeDylib
= true;
4438 context
.canBePIE
= false;
4439 context
.enforceIOSMac
= enforceIOSMac
;
4440 context
.origin
= origin
;
4441 context
.rpath
= rpaths
;
4442 return load(libraryName
, context
, cacheIndex
);
4445 static const char* basename(const char* path
)
4447 const char* last
= path
;
4448 for (const char* s
= path
; *s
!= '\0'; s
++) {
4455 static void setContext(const macho_header
* mainExecutableMH
, int argc
, const char* argv
[], const char* envp
[], const char* apple
[])
4457 gLinkContext
.loadLibrary
= &libraryLocator
;
4458 gLinkContext
.terminationRecorder
= &terminationRecorder
;
4459 gLinkContext
.flatExportFinder
= &flatFindExportedSymbol
;
4460 gLinkContext
.coalescedExportFinder
= &findCoalescedExportedSymbol
;
4461 gLinkContext
.getCoalescedImages
= &getCoalescedImages
;
4462 gLinkContext
.undefinedHandler
= &undefinedHandler
;
4463 gLinkContext
.getAllMappedRegions
= &getMappedRegions
;
4464 gLinkContext
.bindingHandler
= NULL
;
4465 gLinkContext
.notifySingle
= ¬ifySingle
;
4466 gLinkContext
.notifyBatch
= ¬ifyBatch
;
4467 gLinkContext
.removeImage
= &removeImage
;
4468 gLinkContext
.registerDOFs
= ®isterDOFs
;
4469 gLinkContext
.clearAllDepths
= &clearAllDepths
;
4470 gLinkContext
.printAllDepths
= &printAllDepths
;
4471 gLinkContext
.imageCount
= &imageCount
;
4472 gLinkContext
.setNewProgramVars
= &setNewProgramVars
;
4473 gLinkContext
.inSharedCache
= &inSharedCache
;
4474 gLinkContext
.setErrorStrings
= &setErrorStrings
;
4475 #if SUPPORT_OLD_CRT_INITIALIZATION
4476 gLinkContext
.setRunInitialzersOldWay
= &setRunInitialzersOldWay
;
4478 gLinkContext
.findImageContainingAddress
= &findImageContainingAddress
;
4479 gLinkContext
.addDynamicReference
= &addDynamicReference
;
4480 #if SUPPORT_ACCELERATE_TABLES
4481 gLinkContext
.notifySingleFromCache
= ¬ifySingleFromCache
;
4482 gLinkContext
.getPreInitNotifyHandler
= &getPreInitNotifyHandler
;
4483 gLinkContext
.getBoundBatchHandler
= &getBoundBatchHandler
;
4485 gLinkContext
.bindingOptions
= ImageLoader::kBindingNone
;
4486 gLinkContext
.argc
= argc
;
4487 gLinkContext
.argv
= argv
;
4488 gLinkContext
.envp
= envp
;
4489 gLinkContext
.apple
= apple
;
4490 gLinkContext
.progname
= (argv
[0] != NULL
) ? basename(argv
[0]) : "";
4491 gLinkContext
.programVars
.mh
= mainExecutableMH
;
4492 gLinkContext
.programVars
.NXArgcPtr
= &gLinkContext
.argc
;
4493 gLinkContext
.programVars
.NXArgvPtr
= &gLinkContext
.argv
;
4494 gLinkContext
.programVars
.environPtr
= &gLinkContext
.envp
;
4495 gLinkContext
.programVars
.__prognamePtr
=&gLinkContext
.progname
;
4496 gLinkContext
.mainExecutable
= NULL
;
4497 gLinkContext
.imageSuffix
= NULL
;
4498 gLinkContext
.dynamicInterposeArray
= NULL
;
4499 gLinkContext
.dynamicInterposeCount
= 0;
4500 gLinkContext
.prebindUsage
= ImageLoader::kUseAllPrebinding
;
4501 #if TARGET_IPHONE_SIMULATOR
4502 gLinkContext
.sharedRegionMode
= ImageLoader::kUsePrivateSharedRegion
;
4504 gLinkContext
.sharedRegionMode
= ImageLoader::kUseSharedRegion
;
4511 // Look for a special segment in the mach header.
4512 // Its presences means that the binary wants to have DYLD ignore
4513 // DYLD_ environment variables.
4515 #if __MAC_OS_X_VERSION_MIN_REQUIRED
4516 static bool hasRestrictedSegment(const macho_header
* mh
)
4518 const uint32_t cmd_count
= mh
->ncmds
;
4519 const struct load_command
* const cmds
= (struct load_command
*)(((char*)mh
)+sizeof(macho_header
));
4520 const struct load_command
* cmd
= cmds
;
4521 for (uint32_t i
= 0; i
< cmd_count
; ++i
) {
4523 case LC_SEGMENT_COMMAND
:
4525 const struct macho_segment_command
* seg
= (struct macho_segment_command
*)cmd
;
4527 //dyld::log("seg name: %s\n", seg->segname);
4528 if (strcmp(seg
->segname
, "__RESTRICT") == 0) {
4529 const struct macho_section
* const sectionsStart
= (struct macho_section
*)((char*)seg
+ sizeof(struct macho_segment_command
));
4530 const struct macho_section
* const sectionsEnd
= §ionsStart
[seg
->nsects
];
4531 for (const struct macho_section
* sect
=sectionsStart
; sect
< sectionsEnd
; ++sect
) {
4532 if (strcmp(sect
->sectname
, "__restrict") == 0)
4539 cmd
= (const struct load_command
*)(((char*)cmd
)+cmd
->cmdsize
);
4546 #if __IPHONE_OS_VERSION_MIN_REQUIRED && !TARGET_IPHONE_SIMULATOR
4547 static bool isFairPlayEncrypted(const macho_header
* mh
)
4549 const uint32_t cmd_count
= mh
->ncmds
;
4550 const struct load_command
* const cmds
= (struct load_command
*)(((char*)mh
)+sizeof(macho_header
));
4551 const struct load_command
* cmd
= cmds
;
4552 for (uint32_t i
= 0; i
< cmd_count
; ++i
) {
4553 if ( cmd
->cmd
== LC_ENCRYPT_COMMAND
) {
4554 const encryption_info_command
* enc
= (encryption_info_command
*)cmd
;
4555 return (enc
->cryptid
!= 0);
4557 cmd
= (const struct load_command
*)(((char*)cmd
)+cmd
->cmdsize
);
4564 #if SUPPORT_VERSIONED_PATHS
4566 static bool readFirstPage(const char* dylibPath
, uint8_t firstPage
[4096])
4569 // open file (automagically closed when this function exits)
4570 FileOpener
file(dylibPath
);
4572 if ( file
.getFileDescriptor() == -1 )
4575 if ( pread(file
.getFileDescriptor(), firstPage
, 4096, 0) != 4096 )
4578 // if fat wrapper, find usable sub-file
4579 const fat_header
* fileStartAsFat
= (fat_header
*)firstPage
;
4580 if ( fileStartAsFat
->magic
== OSSwapBigToHostInt32(FAT_MAGIC
) ) {
4581 uint64_t fileOffset
;
4582 uint64_t fileLength
;
4583 if ( fatFindBest(fileStartAsFat
, &fileOffset
, &fileLength
) ) {
4584 if ( pread(file
.getFileDescriptor(), firstPage
, 4096, fileOffset
) != 4096 )
4596 // Peeks at a dylib file and returns its current_version and install_name.
4597 // Returns false on error.
4599 static bool getDylibVersionAndInstallname(const char* dylibPath
, uint32_t* version
, char* installName
)
4601 uint8_t firstPage
[4096];
4602 const macho_header
* mh
= (macho_header
*)firstPage
;
4603 if ( !readFirstPage(dylibPath
, firstPage
) ) {
4604 // If file cannot be read, check to see if path is in shared cache
4605 const macho_header
* mhInCache
;
4606 const char* pathInCache
;
4608 if ( !findInSharedCacheImage(dylibPath
, true, NULL
, &mhInCache
, &pathInCache
, &slideInCache
) )
4613 // check mach-o header
4614 if ( mh
->magic
!= sMainExecutableMachHeader
->magic
)
4616 if ( mh
->cputype
!= sMainExecutableMachHeader
->cputype
)
4619 // scan load commands for LC_ID_DYLIB
4620 const uint32_t cmd_count
= mh
->ncmds
;
4621 const struct load_command
* const cmds
= (struct load_command
*)(((char*)mh
)+sizeof(macho_header
));
4622 const struct load_command
* const cmdsReadEnd
= (struct load_command
*)(((char*)mh
)+4096);
4623 const struct load_command
* cmd
= cmds
;
4624 for (uint32_t i
= 0; i
< cmd_count
; ++i
) {
4628 const struct dylib_command
* id
= (struct dylib_command
*)cmd
;
4629 *version
= id
->dylib
.current_version
;
4630 if ( installName
!= NULL
)
4631 strlcpy(installName
, (char *)id
+ id
->dylib
.name
.offset
, PATH_MAX
);
4636 cmd
= (const struct load_command
*)(((char*)cmd
)+cmd
->cmdsize
);
4637 if ( cmd
> cmdsReadEnd
)
4643 #endif // SUPPORT_VERSIONED_PATHS
4647 static void printAllImages()
4649 dyld::log("printAllImages()\n");
4650 for (std::vector
<ImageLoader
*>::iterator it
=sAllImages
.begin(); it
!= sAllImages
.end(); it
++) {
4651 ImageLoader
* image
= *it
;
4652 dyld_image_states imageState
= image
->getState();
4653 dyld::log(" state=%d, dlopen-count=%d, never-unload=%d, in-use=%d, name=%s\n",
4654 imageState
, image
->dlopenCount(), image
->neverUnload(), image
->isMarkedInUse(), image
->getShortName());
4659 void link(ImageLoader
* image
, bool forceLazysBound
, bool neverUnload
, const ImageLoader::RPathChain
& loaderRPaths
, unsigned cacheIndex
)
4661 // add to list of known images. This did not happen at creation time for bundles
4662 if ( image
->isBundle() && !image
->isLinked() )
4665 // we detect root images as those not linked in yet
4666 if ( !image
->isLinked() )
4667 addRootImage(image
);
4671 const char* path
= image
->getPath();
4672 #if SUPPORT_ACCELERATE_TABLES
4673 if ( image
== sAllCacheImagesProxy
)
4674 path
= sAllCacheImagesProxy
->getIndexedPath(cacheIndex
);
4676 image
->link(gLinkContext
, forceLazysBound
, false, neverUnload
, loaderRPaths
, path
);
4678 catch (const char* msg
) {
4679 garbageCollectImages();
4685 void runInitializers(ImageLoader
* image
)
4687 // do bottom up initialization
4688 ImageLoader::InitializerTimingList initializerTimes
[allImagesCount()];
4689 initializerTimes
[0].count
= 0;
4690 image
->runInitializers(gLinkContext
, initializerTimes
[0]);
4693 // This function is called at the end of dlclose() when the reference count goes to zero.
4694 // The dylib being unloaded may have brought in other dependent dylibs when it was loaded.
4695 // Those dependent dylibs need to be unloaded, but only if they are not referenced by
4696 // something else. We use a standard mark and sweep garbage collection.
4698 // The tricky part is that when a dylib is unloaded it may have a termination function that
4699 // can run and itself call dlclose() on yet another dylib. The problem is that this
4700 // sort of gabage collection is not re-entrant. Instead a terminator's call to dlclose()
4701 // which calls garbageCollectImages() will just set a flag to re-do the garbage collection
4702 // when the current pass is done.
4704 // Also note that this is done within the dyld global lock, so it is always single threaded.
4706 void garbageCollectImages()
4708 static bool sDoingGC
= false;
4709 static bool sRedo
= false;
4712 // GC is currently being run, just set a flag to have it run again.
4721 // mark phase: mark all images not-in-use
4722 for (std::vector
<ImageLoader
*>::iterator it
=sAllImages
.begin(); it
!= sAllImages
.end(); it
++) {
4723 ImageLoader
* image
= *it
;
4724 //dyld::log("gc: neverUnload=%d name=%s\n", image->neverUnload(), image->getShortName());
4725 image
->markNotUsed();
4728 // sweep phase: mark as in-use, images reachable from never-unload or in-use image
4729 for (std::vector
<ImageLoader
*>::iterator it
=sAllImages
.begin(); it
!= sAllImages
.end(); it
++) {
4730 ImageLoader
* image
= *it
;
4731 if ( (image
->dlopenCount() != 0) || image
->neverUnload() || (image
== sMainExecutable
) ) {
4732 OSSpinLockLock(&sDynamicReferencesLock
);
4733 image
->markedUsedRecursive(sDynamicReferences
);
4734 OSSpinLockUnlock(&sDynamicReferencesLock
);
4738 // collect phase: build array of images not marked in-use
4739 ImageLoader
* deadImages
[sAllImages
.size()];
4740 unsigned deadCount
= 0;
4741 int maxRangeCount
= 0;
4742 for (std::vector
<ImageLoader
*>::iterator it
=sAllImages
.begin(); it
!= sAllImages
.end(); it
++) {
4743 ImageLoader
* image
= *it
;
4744 if ( ! image
->isMarkedInUse() ) {
4745 deadImages
[deadCount
++] = image
;
4746 if (gLogAPIs
) dyld::log("dlclose(), found unused image %p %s\n", image
, image
->getShortName());
4747 maxRangeCount
+= image
->segmentCount();
4751 // collect phase: run termination routines for images not marked in-use
4752 if ( maxRangeCount
!= 0 ) {
4753 __cxa_range_t ranges
[maxRangeCount
];
4755 for (unsigned i
=0; i
< deadCount
; ++i
) {
4756 ImageLoader
* image
= deadImages
[i
];
4757 for (unsigned int j
=0; j
< image
->segmentCount(); ++j
) {
4758 if ( !image
->segExecutable(j
) )
4760 if ( rangeCount
< maxRangeCount
) {
4761 ranges
[rangeCount
].addr
= (const void*)image
->segActualLoadAddress(j
);
4762 ranges
[rangeCount
].length
= image
->segSize(j
);
4767 runImageStaticTerminators(image
);
4769 catch (const char* msg
) {
4770 dyld::warn("problem running terminators for image: %s\n", msg
);
4774 // <rdar://problem/14718598> dyld should call __cxa_finalize_ranges()
4775 if ( (rangeCount
> 0) && (gLibSystemHelpers
!= NULL
) && (gLibSystemHelpers
->version
>= 13) )
4776 (*gLibSystemHelpers
->cxa_finalize_ranges
)(ranges
, rangeCount
);
4779 // collect phase: delete all images which are not marked in-use
4782 mightBeMore
= false;
4783 for (std::vector
<ImageLoader
*>::iterator it
=sAllImages
.begin(); it
!= sAllImages
.end(); it
++) {
4784 ImageLoader
* image
= *it
;
4785 if ( ! image
->isMarkedInUse() ) {
4787 if (gLogAPIs
) dyld::log("dlclose(), deleting %p %s\n", image
, image
->getShortName());
4789 ImageLoader::deleteImage(image
);
4791 break; // interator in invalidated by this removal
4793 catch (const char* msg
) {
4794 dyld::warn("problem deleting image: %s\n", msg
);
4798 } while ( mightBeMore
);
4807 static void preflight_finally(ImageLoader
* image
)
4809 if ( image
->isBundle() ) {
4810 removeImageFromAllImages(image
->machHeader());
4811 ImageLoader::deleteImage(image
);
4813 sBundleBeingLoaded
= NULL
;
4814 dyld::garbageCollectImages();
4818 void preflight(ImageLoader
* image
, const ImageLoader::RPathChain
& loaderRPaths
, unsigned cacheIndex
)
4821 if ( image
->isBundle() )
4822 sBundleBeingLoaded
= image
; // hack
4823 const char* path
= image
->getPath();
4824 #if SUPPORT_ACCELERATE_TABLES
4825 if ( image
== sAllCacheImagesProxy
)
4826 path
= sAllCacheImagesProxy
->getIndexedPath(cacheIndex
);
4828 image
->link(gLinkContext
, false, true, false, loaderRPaths
, path
);
4830 catch (const char* msg
) {
4831 preflight_finally(image
);
4834 preflight_finally(image
);
4837 static void loadInsertedDylib(const char* path
)
4839 ImageLoader
* image
= NULL
;
4840 unsigned cacheIndex
;
4842 LoadContext context
;
4843 context
.useSearchPaths
= false;
4844 context
.useFallbackPaths
= false;
4845 context
.useLdLibraryPath
= false;
4846 context
.implicitRPath
= false;
4847 context
.matchByInstallName
= false;
4848 context
.dontLoad
= false;
4849 context
.mustBeBundle
= false;
4850 context
.mustBeDylib
= true;
4851 context
.canBePIE
= false;
4852 context
.enforceIOSMac
= true;
4853 context
.origin
= NULL
; // can't use @loader_path with DYLD_INSERT_LIBRARIES
4854 context
.rpath
= NULL
;
4855 image
= load(path
, context
, cacheIndex
);
4857 catch (const char* msg
) {
4858 if ( gLinkContext
.allowInsertFailures
)
4859 dyld::log("dyld: warning: could not load inserted library '%s' into hardened process because %s\n", path
, msg
);
4861 halt(dyld::mkstringf("could not load inserted library '%s' because %s\n", path
, msg
));
4864 halt(dyld::mkstringf("could not load inserted library '%s'\n", path
));
4869 static void configureProcessRestrictions(const macho_header
* mainExecutableMH
)
4871 uint64_t amfiInputFlags
= 0;
4872 #if TARGET_IPHONE_SIMULATOR
4873 amfiInputFlags
|= AMFI_DYLD_INPUT_PROC_IN_SIMULATOR
;
4874 #elif __MAC_OS_X_VERSION_MIN_REQUIRED
4875 if ( hasRestrictedSegment(mainExecutableMH
) )
4876 amfiInputFlags
|= AMFI_DYLD_INPUT_PROC_HAS_RESTRICT_SEG
;
4877 #elif __IPHONE_OS_VERSION_MIN_REQUIRED
4878 if ( isFairPlayEncrypted(mainExecutableMH
) )
4879 amfiInputFlags
|= AMFI_DYLD_INPUT_PROC_IS_ENCRYPTED
;
4881 uint64_t amfiOutputFlags
= 0;
4882 if ( amfi_check_dyld_policy_self(amfiInputFlags
, &amfiOutputFlags
) == 0 ) {
4883 gLinkContext
.allowAtPaths
= (amfiOutputFlags
& AMFI_DYLD_OUTPUT_ALLOW_AT_PATH
);
4884 gLinkContext
.allowEnvVarsPrint
= (amfiOutputFlags
& AMFI_DYLD_OUTPUT_ALLOW_PRINT_VARS
);
4885 gLinkContext
.allowEnvVarsPath
= (amfiOutputFlags
& AMFI_DYLD_OUTPUT_ALLOW_PATH_VARS
);
4886 gLinkContext
.allowEnvVarsSharedCache
= (amfiOutputFlags
& AMFI_DYLD_OUTPUT_ALLOW_CUSTOM_SHARED_CACHE
);
4887 gLinkContext
.allowClassicFallbackPaths
= (amfiOutputFlags
& AMFI_DYLD_OUTPUT_ALLOW_FALLBACK_PATHS
);
4888 gLinkContext
.allowInsertFailures
= (amfiOutputFlags
& AMFI_DYLD_OUTPUT_ALLOW_FAILED_LIBRARY_INSERTION
);
4891 #if __MAC_OS_X_VERSION_MIN_REQUIRED
4892 // support chrooting from old kernel
4893 bool isRestricted
= false;
4894 bool libraryValidation
= false;
4895 // any processes with setuid or setgid bit set or with __RESTRICT segment is restricted
4896 if ( issetugid() || hasRestrictedSegment(mainExecutableMH
) ) {
4897 isRestricted
= true;
4899 bool usingSIP
= (csr_check(CSR_ALLOW_TASK_FOR_PID
) != 0);
4901 if ( csops(0, CS_OPS_STATUS
, &flags
, sizeof(flags
)) != -1 ) {
4902 // On OS X CS_RESTRICT means the program was signed with entitlements
4903 if ( ((flags
& CS_RESTRICT
) == CS_RESTRICT
) && usingSIP
) {
4904 isRestricted
= true;
4906 // Library Validation loosens searching but requires everything to be code signed
4907 if ( flags
& CS_REQUIRE_LV
) {
4908 isRestricted
= false;
4909 libraryValidation
= true;
4912 gLinkContext
.allowAtPaths
= !isRestricted
;
4913 gLinkContext
.allowEnvVarsPrint
= !isRestricted
;
4914 gLinkContext
.allowEnvVarsPath
= !isRestricted
;
4915 gLinkContext
.allowEnvVarsSharedCache
= !libraryValidation
|| !usingSIP
;
4916 gLinkContext
.allowClassicFallbackPaths
= !isRestricted
;
4917 gLinkContext
.allowInsertFailures
= false;
4919 halt("amfi_check_dyld_policy_self() failed\n");
4925 bool processIsRestricted()
4927 #if __MAC_OS_X_VERSION_MIN_REQUIRED
4928 return !gLinkContext
.allowEnvVarsPath
;
4935 // <rdar://problem/10583252> Add dyld to uuidArray to enable symbolication of stackshots
4936 static void addDyldImageToUUIDList()
4938 const struct macho_header
* mh
= (macho_header
*)&__dso_handle
;
4939 const uint32_t cmd_count
= mh
->ncmds
;
4940 const struct load_command
* const cmds
= (struct load_command
*)((char*)mh
+ sizeof(macho_header
));
4941 const struct load_command
* cmd
= cmds
;
4942 for (uint32_t i
= 0; i
< cmd_count
; ++i
) {
4945 uuid_command
* uc
= (uuid_command
*)cmd
;
4946 dyld_uuid_info info
;
4947 info
.imageLoadAddress
= (mach_header
*)mh
;
4948 memcpy(info
.imageUUID
, uc
->uuid
, 16);
4949 addNonSharedCacheImageUUID(info
);
4953 cmd
= (const struct load_command
*)(((char*)cmd
)+cmd
->cmdsize
);
4957 void notifyKernelAboutImage(const struct macho_header
* mh
, const char* fileInfo
)
4959 const char *endptr
= nullptr;
4960 uint64_t fsid_scalar
= hexToUInt64(fileInfo
, &endptr
);
4961 uint64_t fsobj_id_scalar
= 0;
4962 if (endptr
!= nullptr) {
4963 fsobj_id_scalar
= hexToUInt64(endptr
+1, &endptr
);
4965 const uint32_t cmd_count
= mh
->ncmds
;
4966 const struct load_command
* const cmds
= (struct load_command
*)((char*)mh
+ sizeof(macho_header
));
4967 const struct load_command
* cmd
= cmds
;
4968 for (uint32_t i
= 0; i
< cmd_count
; ++i
) {
4971 // Add dyld to the kernel image info
4972 uuid_command
* uc
= (uuid_command
*)cmd
;
4973 dyld3::kdebug_trace_dyld_image(DBG_DYLD_UUID_MAP_A
, (const uuid_t
*)&uc
->uuid
[0], *reinterpret_cast<fsobj_id_t
*>(&fsobj_id_scalar
), *reinterpret_cast<fsid_t
*>(&fsid_scalar
), (const mach_header
*)mh
);
4977 cmd
= (const struct load_command
*)(((char*)cmd
)+cmd
->cmdsize
);
4981 #if __MAC_OS_X_VERSION_MIN_REQUIRED
4982 typedef int (*open_proc_t
)(const char*, int, int);
4983 typedef int (*fcntl_proc_t
)(int, int, void*);
4984 typedef int (*ioctl_proc_t
)(int, unsigned long, void*);
4985 static void* getProcessInfo() { return dyld::gProcessInfo
; }
4986 static SyscallHelpers sSysCalls
= {
4988 // added in version 1
4997 (fcntl_proc_t
)&fcntl
,
4998 (ioctl_proc_t
)&ioctl
,
5007 &pthread_mutex_lock
,
5008 &pthread_mutex_unlock
,
5010 &mach_port_deallocate
,
5012 &mach_timebase_info
,
5013 &OSAtomicCompareAndSwapPtrBarrier
,
5017 &mach_absolute_time
,
5018 // added in version 2
5020 // added in version 3
5024 // added in version 4
5025 &coresymbolication_load_notifier
,
5026 &coresymbolication_unload_notifier
,
5027 // Added in version 5
5028 &proc_regionfilename
,
5030 &mach_port_insert_right
,
5031 &mach_port_allocate
,
5033 // Added in version 6
5034 &abort_with_payload
,
5035 // Added in version 7
5036 &task_register_dyld_image_infos
,
5037 &task_unregister_dyld_image_infos
,
5038 &task_get_dyld_image_infos
,
5039 &task_register_dyld_shared_cache_image_info
,
5040 &task_register_dyld_set_dyld_state
,
5041 &task_register_dyld_get_process_state
,
5042 // Added in version 8
5047 // Added in version 9
5048 &kdebug_trace_string
,
5049 // Added in version 10
5050 &amfi_check_dyld_policy_self
,
5051 // Added in version 11
5052 ¬ifyMonitoringDyldMain
,
5053 ¬ifyMonitoringDyld
,
5054 // Add in version 12
5056 &mach_port_construct
,
5060 __attribute__((noinline
))
5061 static const char* useSimulatorDyld(int fd
, const macho_header
* mainExecutableMH
, const char* dyldPath
,
5062 int argc
, const char* argv
[], const char* envp
[], const char* apple
[],
5063 uintptr_t* startGlue
, uintptr_t* mainAddr
)
5068 // <rdar://35873436> HACK to allow marzipan dyld_sim to run entitled processes
5069 if ( strncmp(dyldPath
, "/System/", 8) != 0 ) {
5071 if ( csops(0, CS_OPS_STATUS
, &flags
, sizeof(flags
)) == -1 )
5072 return "csops() failed";
5073 if ( (flags
& CS_RESTRICT
) == CS_RESTRICT
)
5074 return "dyld_sim cannot be loaded in a restricted process";
5077 // <rdar://problem/25311921> simulator does not support restricted processes
5079 return "dyld_sim cannot be loaded in a setuid process";
5080 if ( hasRestrictedSegment(mainExecutableMH
) )
5081 return "dyld_sim cannot be loaded in a restricted process";
5083 // get file size of dyld_sim
5085 if ( fstat(fd
, &sb
) == -1 )
5086 return "stat(dyld_sim) failed";
5088 // read first page of dyld_sim file
5089 uint8_t firstPage
[4096];
5090 if ( pread(fd
, firstPage
, 4096, 0) != 4096 )
5091 return "pread(dyld_sim) failed";
5093 // if fat file, pick matching slice
5094 uint64_t fileOffset
= 0;
5095 uint64_t fileLength
= sb
.st_size
;
5096 const fat_header
* fileStartAsFat
= (fat_header
*)firstPage
;
5097 if ( fileStartAsFat
->magic
== OSSwapBigToHostInt32(FAT_MAGIC
) ) {
5098 if ( !fatFindBest(fileStartAsFat
, &fileOffset
, &fileLength
) )
5099 return "no matching arch in dyld_sim";
5100 // re-read buffer from start of mach-o slice in fat file
5101 if ( pread(fd
, firstPage
, 4096, fileOffset
) != 4096 )
5102 return "pread(dyld_sim) failed";
5104 else if ( !isCompatibleMachO(firstPage
, dyldPath
) ) {
5105 return "dyld_sim is not compatible with the loaded process, likely due to architecture mismatch";
5108 // calculate total size of dyld segments
5109 const macho_header
* mh
= (const macho_header
*)firstPage
;
5110 struct macho_segment_command
* lastSeg
= NULL
;
5111 struct macho_segment_command
* firstSeg
= NULL
;
5112 uintptr_t mappingSize
= 0;
5113 uintptr_t preferredLoadAddress
= 0;
5114 const uint32_t cmd_count
= mh
->ncmds
;
5115 if ( mh
->sizeofcmds
> 4096 )
5116 return "dyld_sim load commands to large";
5117 if ( (sizeof(macho_header
) + mh
->sizeofcmds
) > 4096 )
5118 return "dyld_sim load commands to large";
5119 struct linkedit_data_command
* codeSigCmd
= NULL
;
5120 const struct load_command
* const cmds
= (struct load_command
*)(((char*)mh
)+sizeof(macho_header
));
5121 const struct load_command
* const endCmds
= (struct load_command
*)(((char*)mh
) + sizeof(macho_header
) + mh
->sizeofcmds
);
5122 const struct load_command
* cmd
= cmds
;
5123 for (uint32_t i
= 0; i
< cmd_count
; ++i
) {
5124 uint32_t cmdLength
= cmd
->cmdsize
;
5125 if ( cmdLength
< 8 )
5126 return "dyld_sim load command too small";
5127 const struct load_command
* const nextCmd
= (const struct load_command
*)(((char*)cmd
)+cmdLength
);
5128 if ( (nextCmd
> endCmds
) || (nextCmd
< cmd
) )
5129 return "dyld_sim load command too large";
5131 case LC_SEGMENT_COMMAND
:
5133 struct macho_segment_command
* seg
= (struct macho_segment_command
*)cmd
;
5134 if ( seg
->vmaddr
+ seg
->vmsize
< seg
->vmaddr
)
5135 return "dyld_sim seg wraps address space";
5136 if ( seg
->vmsize
< seg
->filesize
)
5137 return "dyld_sim seg vmsize too small";
5138 if ( (seg
->fileoff
+ seg
->filesize
) < seg
->fileoff
)
5139 return "dyld_sim seg size wraps address space";
5140 if ( lastSeg
== NULL
) {
5141 // first segment must be __TEXT and start at beginning of file/slice
5143 if ( strcmp(seg
->segname
, "__TEXT") != 0 )
5144 return "dyld_sim first segment not __TEXT";
5145 if ( seg
->fileoff
!= 0 )
5146 return "dyld_sim first segment not at file offset zero";
5147 if ( seg
->filesize
< (sizeof(macho_header
) + mh
->sizeofcmds
) )
5148 return "dyld_sim first segment smaller than load commands";
5149 preferredLoadAddress
= seg
->vmaddr
;
5152 // other sements must be continguous with previous segment and not executable
5153 if ( lastSeg
->fileoff
+ lastSeg
->filesize
!= seg
->fileoff
)
5154 return "dyld_sim segments not contiguous";
5155 if ( lastSeg
->vmaddr
+ lastSeg
->vmsize
!= seg
->vmaddr
)
5156 return "dyld_sim segments not address contiguous";
5157 if ( (seg
->initprot
& VM_PROT_EXECUTE
) != 0 )
5158 return "dyld_sim non-first segment is executable";
5160 mappingSize
+= seg
->vmsize
;
5164 case LC_SEGMENT_COMMAND_WRONG
:
5165 return "dyld_sim wrong load segment load command";
5166 case LC_CODE_SIGNATURE
:
5167 codeSigCmd
= (struct linkedit_data_command
*)cmd
;
5172 // last segment must be named __LINKEDIT and not writable
5173 if ( lastSeg
== NULL
)
5174 return "dyld_sim has no segments";
5175 if ( strcmp(lastSeg
->segname
, "__LINKEDIT") != 0 )
5176 return "dyld_sim last segment not __LINKEDIT";
5177 if ( lastSeg
->initprot
& VM_PROT_WRITE
)
5178 return "dyld_sim __LINKEDIT segment writable";
5180 // must have code signature which is contained within LINKEDIT segment
5181 if ( codeSigCmd
== NULL
)
5182 return "dyld_sim not code signed";
5183 if ( codeSigCmd
->dataoff
< lastSeg
->fileoff
)
5184 return "dyld_sim code signature not in __LINKEDIT";
5185 if ( (codeSigCmd
->dataoff
+ codeSigCmd
->datasize
) < codeSigCmd
->dataoff
)
5186 return "dyld_sim code signature size wraps";
5187 if ( (codeSigCmd
->dataoff
+ codeSigCmd
->datasize
) > (lastSeg
->fileoff
+ lastSeg
->filesize
) )
5188 return "dyld_sim code signature extends beyond __LINKEDIT";
5190 // register code signature with kernel before mmap()ing segments
5191 fsignatures_t siginfo
;
5192 siginfo
.fs_file_start
=fileOffset
; // start of mach-o slice in fat file
5193 siginfo
.fs_blob_start
=(void*)(long)(codeSigCmd
->dataoff
); // start of code-signature in mach-o file
5194 siginfo
.fs_blob_size
=codeSigCmd
->datasize
; // size of code-signature
5195 int result
= fcntl(fd
, F_ADDFILESIGS_FOR_DYLD_SIM
, &siginfo
);
5196 if ( result
== -1 ) {
5197 return mkstringf("dyld_sim fcntl(F_ADDFILESIGS_FOR_DYLD_SIM) failed with errno=%d", errno
);
5199 // file range covered by code signature must extend up to code signature itself
5200 if ( siginfo
.fs_file_start
< codeSigCmd
->dataoff
)
5201 return mkstringf("dyld_sim code signature does not cover all of dyld_sim. Signature covers up to 0x%08lX. Signature starts at 0x%08X", (unsigned long)siginfo
.fs_file_start
, codeSigCmd
->dataoff
);
5203 // reserve space, then mmap each segment
5204 vm_address_t loadAddress
= 0;
5205 if ( ::vm_allocate(mach_task_self(), &loadAddress
, mappingSize
, VM_FLAGS_ANYWHERE
) != 0 )
5206 return "dyld_sim cannot allocate space";
5208 struct source_version_command
* dyldVersionCmd
= NULL
;
5209 for (uint32_t i
= 0; i
< cmd_count
; ++i
) {
5211 case LC_SEGMENT_COMMAND
:
5213 struct macho_segment_command
* seg
= (struct macho_segment_command
*)cmd
;
5214 uintptr_t requestedLoadAddress
= seg
->vmaddr
- preferredLoadAddress
+ loadAddress
;
5215 void* segAddress
= ::mmap((void*)requestedLoadAddress
, seg
->filesize
, seg
->initprot
, MAP_FIXED
| MAP_PRIVATE
, fd
, fileOffset
+ seg
->fileoff
);
5216 //dyld::log("dyld_sim %s mapped at %p\n", seg->segname, segAddress);
5217 if ( segAddress
== (void*)(-1) )
5218 return "dyld_sim mmap() of segment failed";
5219 if ( ((uintptr_t)segAddress
< loadAddress
) || ((uintptr_t)segAddress
+seg
->filesize
> loadAddress
+mappingSize
) )
5220 return "dyld_sim mmap() to wrong location";
5223 case LC_SOURCE_VERSION
:
5224 dyldVersionCmd
= (struct source_version_command
*)cmd
;
5227 cmd
= (const struct load_command
*)(((char*)cmd
)+cmd
->cmdsize
);
5231 // walk newly mapped dyld_sim __TEXT load commands to find entry point
5232 uintptr_t entry
= 0;
5233 cmd
= (struct load_command
*)(((char*)loadAddress
)+sizeof(macho_header
));
5234 const uint32_t count
= ((macho_header
*)(loadAddress
))->ncmds
;
5235 for (uint32_t i
= 0; i
< count
; ++i
) {
5236 if (cmd
->cmd
== LC_UNIXTHREAD
) {
5238 const i386_thread_state_t
* registers
= (i386_thread_state_t
*)(((char*)cmd
) + 16);
5239 // entry point must be in first segment
5240 if ( registers
->__eip
< firstSeg
->vmaddr
)
5241 return "dyld_sim entry point not in __TEXT segment";
5242 if ( registers
->__eip
> (firstSeg
->vmaddr
+ firstSeg
->vmsize
) )
5243 return "dyld_sim entry point not in __TEXT segment";
5244 entry
= (registers
->__eip
+ loadAddress
- preferredLoadAddress
);
5246 const x86_thread_state64_t
* registers
= (x86_thread_state64_t
*)(((char*)cmd
) + 16);
5247 // entry point must be in first segment
5248 if ( registers
->__rip
< firstSeg
->vmaddr
)
5249 return "dyld_sim entry point not in __TEXT segment";
5250 if ( registers
->__rip
> (firstSeg
->vmaddr
+ firstSeg
->vmsize
) )
5251 return "dyld_sim entry point not in __TEXT segment";
5252 entry
= (registers
->__rip
+ loadAddress
- preferredLoadAddress
);
5255 cmd
= (const struct load_command
*)(((char*)cmd
)+cmd
->cmdsize
);
5258 return "dyld_sim entry not found";
5260 // notify debugger that dyld_sim is loaded
5261 dyld_image_info info
;
5262 info
.imageLoadAddress
= (mach_header
*)loadAddress
;
5263 info
.imageFilePath
= strdup(dyldPath
);
5264 info
.imageFileModDate
= sb
.st_mtime
;
5265 addImagesToAllImages(1, &info
);
5266 dyld::gProcessInfo
->notification(dyld_image_adding
, 1, &info
);
5268 const char** appleParams
= apple
;
5269 // jump into new simulator dyld
5270 typedef uintptr_t (*sim_entry_proc_t
)(int argc
, const char* argv
[], const char* envp
[], const char* apple
[],
5271 const macho_header
* mainExecutableMH
, const macho_header
* dyldMH
, uintptr_t dyldSlide
,
5272 const dyld::SyscallHelpers
* vtable
, uintptr_t* startGlue
);
5273 sim_entry_proc_t newDyld
= (sim_entry_proc_t
)entry
;
5274 *mainAddr
= (*newDyld
)(argc
, argv
, envp
, appleParams
, mainExecutableMH
, (macho_header
*)loadAddress
,
5275 loadAddress
- preferredLoadAddress
,
5276 &sSysCalls
, startGlue
);
5282 // If the DYLD_SKIP_MAIN environment is set to 1, dyld will return the
5283 // address of this function instead of main() in the target program which
5284 // __dyld_start jumps to. Useful for qualifying dyld itself.
5295 static bool envVarMatches(const dyld3::closure::LaunchClosure
* mainClosure
, const char* envp
[], const char* varName
)
5297 __block
const char* valueFromClosure
= nullptr;
5298 mainClosure
->forEachEnvVar(^(const char* keyEqualValue
, bool& stop
) {
5299 size_t keyLen
= strlen(varName
);
5300 if ( (strncmp(varName
, keyEqualValue
, keyLen
) == 0) && (keyEqualValue
[keyLen
] == '=') ) {
5301 valueFromClosure
= &keyEqualValue
[keyLen
+1];
5306 const char* valueFromEnv
= _simple_getenv(envp
, varName
);
5308 bool inClosure
= (valueFromClosure
!= nullptr);
5309 bool inEnv
= (valueFromEnv
!= nullptr);
5310 if ( inClosure
!= inEnv
)
5312 if ( !inClosure
&& !inEnv
)
5314 return ( strcmp(valueFromClosure
, valueFromEnv
) == 0 );
5317 static const char* const sEnvVarsToCheck
[] = {
5318 "DYLD_LIBRARY_PATH",
5319 "DYLD_FRAMEWORK_PATH",
5320 "DYLD_FALLBACK_LIBRARY_PATH",
5321 "DYLD_FALLBACK_FRAMEWORK_PATH",
5322 "DYLD_INSERT_LIBRARIES",
5323 "DYLD_IMAGE_SUFFIX",
5324 "DYLD_VERSIONED_FRAMEWORK_PATH",
5325 "DYLD_VERSIONED_LIBRARY_PATH",
5329 static bool envVarsMatch(const dyld3::closure::LaunchClosure
* mainClosure
, const char* envp
[])
5331 for (const char* envVar
: sEnvVarsToCheck
) {
5332 if ( !envVarMatches(mainClosure
, envp
, envVar
) ) {
5333 if ( gLinkContext
.verboseWarnings
)
5334 dyld::log("dyld: closure %p not used because %s changed\n", mainClosure
, envVar
);
5339 // FIXME: dyld3 doesn't support versioned paths so we need to fall back to dyld2 if we have them.
5340 // <rdar://problem/37004660> dyld3: support DYLD_VERSIONED_*_PATHs ?
5341 if ( sEnv
.DYLD_VERSIONED_LIBRARY_PATH
!= nullptr ) {
5342 if ( gLinkContext
.verboseWarnings
)
5343 dyld::log("dyld: closure %p not used because DYLD_VERSIONED_LIBRARY_PATH used\n", mainClosure
);
5346 if ( sEnv
.DYLD_VERSIONED_FRAMEWORK_PATH
!= nullptr ) {
5347 if ( gLinkContext
.verboseWarnings
)
5348 dyld::log("dyld: closure %p not used because DYLD_VERSIONED_FRAMEWORK_PATH used\n", mainClosure
);
5355 static bool closureValid(const dyld3::closure::LaunchClosure
* mainClosure
, const dyld3::closure::LoadedFileInfo
& mainFileInfo
,
5356 const uint8_t* mainExecutableCDHash
, bool closureInCache
, const char* envp
[])
5358 if ( !closureInCache
) {
5359 // verify current dyld cache is same as expected
5360 uuid_t expectedCacheUUID
;
5361 if ( mainClosure
->builtAgainstDyldCache(expectedCacheUUID
) ) {
5362 if ( sSharedCacheLoadInfo
.loadAddress
== nullptr ) {
5363 if ( gLinkContext
.verboseWarnings
)
5364 dyld::log("dyld: closure %p dyld cache not loaded\n", mainClosure
);
5368 uuid_t actualCacheUUID
;
5369 sSharedCacheLoadInfo
.loadAddress
->getUUID(actualCacheUUID
);
5370 if ( memcmp(expectedCacheUUID
, actualCacheUUID
, sizeof(uuid_t
)) != 0 ) {
5371 if ( gLinkContext
.verboseWarnings
)
5372 dyld::log("dyld: closure %p not used because built against different dyld cache\n", mainClosure
);
5378 // closure built assume there is no dyld cache
5379 if ( sSharedCacheLoadInfo
.loadAddress
!= nullptr ) {
5380 if ( gLinkContext
.verboseWarnings
)
5381 dyld::log("dyld: closure %p built expecting no dyld cache\n", mainClosure
);
5385 #if __IPHONE_OS_VERSION_MIN_REQUIRED
5386 // verify this closure is not from a previous reboot
5387 const char* expectedBootUUID
= mainClosure
->bootUUID();
5388 char actualBootSessionUUID
[256] = { 0 };
5389 size_t bootSize
= sizeof(actualBootSessionUUID
);
5390 bool gotActualBootUUID
= (sysctlbyname("kern.bootsessionuuid", actualBootSessionUUID
, &bootSize
, NULL
, 0) == 0);
5391 if ( !gotActualBootUUID
|| (expectedBootUUID
== nullptr) || (strcmp(expectedBootUUID
, actualBootSessionUUID
) != 0) ) {
5392 if ( gLinkContext
.verboseWarnings
)
5393 dyld::log("dyld: closure %p built in different boot context\n", mainClosure
);
5399 // verify all mach-o files have not changed since closure was built
5400 __block
bool foundFileThatInvalidatesClosure
= false;
5401 mainClosure
->images()->forEachImage(^(const dyld3::closure::Image
* image
, bool& stop
) {
5402 __block
uint64_t expectedInode
;
5403 __block
uint64_t expectedMtime
;
5404 if ( image
->hasFileModTimeAndInode(expectedInode
, expectedMtime
) ) {
5405 struct stat statBuf
;
5406 if ( ::stat(image
->path(), &statBuf
) == 0 ) {
5407 if ( (statBuf
.st_mtime
!= expectedMtime
) || (statBuf
.st_ino
!= expectedInode
) ) {
5408 if ( gLinkContext
.verboseWarnings
)
5409 dyld::log("dyld: closure %p not used because mtime/inode for '%s' has changed since closure was built\n", mainClosure
, image
->path());
5410 foundFileThatInvalidatesClosure
= true;
5415 if ( gLinkContext
.verboseWarnings
)
5416 dyld::log("dyld: closure %p not used because '%s' is needed by closure but is missing\n", mainClosure
, image
->path());
5417 foundFileThatInvalidatesClosure
= true;
5422 if ( foundFileThatInvalidatesClosure
)
5425 // verify cdHash of main executable is same as recorded in closure
5426 uint8_t expectedHash
[20];
5427 const dyld3::closure::Image
* mainImage
= mainClosure
->images()->imageForNum(mainClosure
->topImage());
5428 if ( mainImage
->hasCdHash(expectedHash
) ) {
5429 if ( mainExecutableCDHash
== nullptr ) {
5430 if ( gLinkContext
.verboseWarnings
)
5431 dyld::log("dyld: closure %p not used because main executable is not code signed but was expected to be\n", mainClosure
);
5434 if ( memcmp(mainExecutableCDHash
, expectedHash
, 20) != 0 ) {
5435 if ( gLinkContext
.verboseWarnings
)
5436 dyld::log("dyld: closure %p not used because main executable cd-hash changed since closure was built\n", mainClosure
);
5441 // verify UUID of main executable is same as recorded in closure
5442 uuid_t expectedUUID
;
5443 bool hasExpect
= mainImage
->getUuid(expectedUUID
);
5445 const dyld3::MachOLoaded
* mainExecutableMH
= (const dyld3::MachOLoaded
*)mainFileInfo
.fileContent
;
5446 bool hasActual
= mainExecutableMH
->getUuid(actualUUID
);
5447 if ( hasExpect
!= hasActual
) {
5448 if ( gLinkContext
.verboseWarnings
)
5449 dyld::log("dyld: closure %p not used because UUID of executable changed since closure was built\n", mainClosure
);
5452 if ( hasExpect
&& hasActual
&& memcmp(actualUUID
, expectedUUID
, sizeof(uuid_t
)) != 0 ) {
5453 if ( gLinkContext
.verboseWarnings
)
5454 dyld::log("dyld: closure %p not used because UUID of executable changed since closure was built\n", mainClosure
);
5458 // verify DYLD_* env vars are same as when closure was built
5459 if ( !envVarsMatch(mainClosure
, envp
) ) {
5463 // verify files that are supposed to be missing actually are missing
5464 mainClosure
->forEachMustBeMissingFile(^(const char* path
, bool& stop
) {
5465 struct stat statBuf
;
5466 if ( ::stat(path
, &statBuf
) == 0 ) {
5468 foundFileThatInvalidatesClosure
= true;
5469 if ( gLinkContext
.verboseWarnings
)
5470 dyld::log("dyld: closure %p not used because found unexpected file '%s'\n", mainClosure
, path
);
5474 // verify closure did not require anything unavailable
5475 if ( mainClosure
->usedAtPaths() && !gLinkContext
.allowAtPaths
) {
5476 if ( gLinkContext
.verboseWarnings
)
5477 dyld::log("dyld: closure %p not used because is used @paths, but process does not allow that\n", mainClosure
);
5480 if ( mainClosure
->usedFallbackPaths() && !gLinkContext
.allowClassicFallbackPaths
) {
5481 if ( gLinkContext
.verboseWarnings
)
5482 dyld::log("dyld: closure %p not used because is used default fallback paths, but process does not allow that\n", mainClosure
);
5486 return !foundFileThatInvalidatesClosure
;
5489 static bool nolog(const char* format
, ...)
5494 static bool dolog(const char* format
, ...)
5497 va_start(list
, format
);
5503 static bool launchWithClosure(const dyld3::closure::LaunchClosure
* mainClosure
,
5504 const DyldSharedCache
* dyldCache
,
5505 const dyld3::MachOLoaded
* mainExecutableMH
, uintptr_t mainExecutableSlide
,
5506 int argc
, const char* argv
[], const char* envp
[], const char* apple
[],
5507 uintptr_t* entry
, uintptr_t* startGlue
)
5509 // build list of all known ImageArrays (at most three: cached dylibs, other OS dylibs, and main prog)
5510 STACK_ALLOC_ARRAY(const dyld3::closure::ImageArray
*, imagesArrays
, 3);
5511 const dyld3::closure::ImageArray
* mainClosureImages
= mainClosure
->images();
5512 if ( dyldCache
!= nullptr ) {
5513 imagesArrays
.push_back(dyldCache
->cachedDylibsImageArray());
5514 if ( auto others
= dyldCache
->otherOSImageArray() )
5515 imagesArrays
.push_back(others
);
5517 imagesArrays
.push_back(mainClosureImages
);
5519 // allocate space for Array<LoadedImage>
5520 STACK_ALLOC_ARRAY(dyld3::LoadedImage
, allImages
, mainClosure
->initialLoadCount());
5522 __block
dyld3::Loader
loader(allImages
, dyldCache
, imagesArrays
, (gLinkContext
.verboseLoading
? &dolog
: &nolog
),
5523 (gLinkContext
.verboseMapping
? &dolog
: &nolog
),
5524 (gLinkContext
.verboseBind
? &dolog
: &nolog
),
5525 (gLinkContext
.verboseDOF
? &dolog
: &nolog
));
5526 dyld3::closure::ImageNum mainImageNum
= mainClosure
->topImage();
5527 mainClosureImages
->forEachImage(^(const dyld3::closure::Image
* image
, bool& stop
) {
5528 if ( image
->imageNum() == mainImageNum
) {
5529 // add main executable (which is already mapped by kernel) to list
5530 dyld3::LoadedImage mainLoadedImage
= dyld3::LoadedImage::make(image
, mainExecutableMH
);
5531 mainLoadedImage
.setState(dyld3::LoadedImage::State::mapped
);
5532 mainLoadedImage
.markLeaveMapped();
5533 loader
.addImage(mainLoadedImage
);
5537 // add inserted library to initial list
5538 loader
.addImage(dyld3::LoadedImage::make(image
));
5542 // recursively load all dependents and fill in allImages array
5544 loader
.completeAllDependents(diag
);
5545 if ( diag
.noError() )
5546 loader
.mapAndFixupAllImages(diag
, dyld3::Loader::dtraceUserProbesEnabled());
5547 if ( diag
.hasError() ) {
5548 if ( gLinkContext
.verboseWarnings
)
5549 dyld::log("dyld: %s\n", diag
.errorMessage());
5553 //dyld::log("loaded image list:\n");
5554 //for (const dyld3::LoadedImage& info : allImages) {
5555 // dyld::log("mh=%p, path=%s\n", info.loadedAddress(), info.image()->path());
5558 // find libdyld entry
5559 dyld3::closure::Image::ResolvedSymbolTarget dyldEntry
;
5560 mainClosure
->libDyldEntry(dyldEntry
);
5561 const dyld3::LibDyldEntryVector
* libDyldEntry
= (dyld3::LibDyldEntryVector
*)loader
.resolveTarget(dyldEntry
);
5563 // send info on all images to libdyld.dylb
5564 libDyldEntry
->setVars(mainExecutableMH
, argc
, argv
, envp
, apple
);
5565 if ( libDyldEntry
->vectorVersion
> 4 )
5566 libDyldEntry
->setRestrictions(gLinkContext
.allowAtPaths
, gLinkContext
.allowEnvVarsPath
);
5567 libDyldEntry
->setHaltFunction(&halt
);
5568 if ( libDyldEntry
->vectorVersion
> 5 ) {
5569 libDyldEntry
->setNotifyMonitoringDyldMain(¬ifyMonitoringDyldMain
);
5570 libDyldEntry
->setNotifyMonitoringDyld(¬ifyMonitoringDyld
);
5572 if ( libDyldEntry
->vectorVersion
> 2 )
5573 libDyldEntry
->setChildForkFunction(&_dyld_fork_child
);
5574 #if !TARGET_IPHONE_SIMULATOR
5575 if ( libDyldEntry
->vectorVersion
> 3 )
5576 libDyldEntry
->setLogFunction(&dyld::vlog
);
5578 libDyldEntry
->setOldAllImageInfo(gProcessInfo
);
5579 const dyld3::LoadedImage
* libSys
= loader
.findImage(mainClosure
->libSystemImageNum());
5580 libDyldEntry
->setInitialImageList(mainClosure
, dyldCache
, sSharedCacheLoadInfo
.path
, allImages
, *libSys
);
5582 CRSetCrashLogMessage("dyld3: launch, running initializers");
5583 libDyldEntry
->runInitialzersBottomUp((mach_header
*)mainExecutableMH
);
5584 //dyld::log("returned from runInitialzersBottomUp()\n");
5586 if (dyld3::kdebug_trace_dyld_enabled(DBG_DYLD_TIMING_LAUNCH_EXECUTABLE
)) {
5587 dyld3::kdebug_trace_dyld_duration_end(launchTraceID
, DBG_DYLD_TIMING_LAUNCH_EXECUTABLE
, 0, 0, 3);
5589 dyld3::closure::Image::ResolvedSymbolTarget progEntry
;
5590 if ( mainClosure
->mainEntry(progEntry
) ) {
5591 // modern app with LC_MAIN
5592 // set startGlue to "start" function in libdyld.dylib
5593 // set entry to "main" function in program
5594 *startGlue
= (uintptr_t)(libDyldEntry
->startFunc
);
5595 *entry
= loader
.resolveTarget(progEntry
);
5597 else if ( mainClosure
->startEntry(progEntry
) ) {
5598 // old style app linked with crt1.o
5599 // entry is "start" function in program
5601 *entry
= loader
.resolveTarget(progEntry
);
5607 CRSetCrashLogMessage("dyld3 mode");
5612 #if !TARGET_IPHONE_SIMULATOR
5614 static void putHexNibble(uint8_t value
, char*& p
)
5619 *p
++ = 'A' + value
- 10;
5622 static void putHexByte(uint8_t value
, char*& p
)
5625 putHexNibble(value
>> 4, p
);
5626 putHexNibble(value
& 0x0F, p
);
5629 #if __MAC_OS_X_VERSION_MIN_REQUIRED
5630 static void makeHashOfProgramAndEnv(const char* mainExecutablePath
, const uint8_t* mainExecutableCDHash
, const char* envp
[], uint8_t hash32
[32])
5632 // create hash of main path, main cd hash, cache UUID, DYLD_* env vars
5633 const struct ccdigest_info
* di
= ccsha256_di();
5634 ccdigest_di_decl(di
, hashTemp
); // defines hashTemp array in stack
5635 ccdigest_init(di
, hashTemp
);
5636 // hash in main executable path
5637 ccdigest_update(di
, hashTemp
, strlen(mainExecutablePath
), mainExecutablePath
);
5638 // hash in cdHash of main executable
5639 if ( mainExecutableCDHash
!= nullptr )
5640 ccdigest_update(di
, hashTemp
, 20, mainExecutableCDHash
);
5641 // hash in shared cache UUID
5642 if ( sSharedCacheLoadInfo
.loadAddress
!= nullptr ) {
5644 sSharedCacheLoadInfo
.loadAddress
->getUUID(cacheUUID
);
5645 ccdigest_update(di
, hashTemp
, sizeof(uuid_t
), cacheUUID
);
5647 #if __MAC_OS_X_VERSION_MIN_REQUIRED
5648 // hash in if process is restricted
5649 ccdigest_update(di
, hashTemp
, sizeof(gLinkContext
.allowEnvVarsPath
), &gLinkContext
.allowEnvVarsPath
);
5651 // include dyld's UUID so changing dyld invalidates closures
5653 if ( ((const dyld3::MachOLoaded
*)&__dso_handle
)->getUuid(dyldUUID
) )
5654 ccdigest_update(di
, hashTemp
, sizeof(uuid_t
), dyldUUID
);
5656 // hash in DYLD_* env vars
5657 for (const char* envVar
: sEnvVarsToCheck
) {
5658 if ( const char* keyValue
= _simple_getenv(envp
, envVar
) )
5659 ccdigest_update(di
, hashTemp
, strlen(keyValue
), keyValue
);
5661 // finish SHA256 into 32-byte value
5662 ccdigest_final(di
, hashTemp
, hash32
);
5663 ccdigest_di_clear(di
, hashTemp
);
5667 static void buildClosureCachePath(const char* mainExecutablePath
,const dyld3::MachOLoaded
* mainExecutableMH
,
5668 const uint8_t* mainExecutableCDHash
, const char* envp
[], char closurePath
[])
5670 // build base path of $TMPDIR/dyld/<prog-name>-
5671 const char* tempDir
= _simple_getenv(envp
, "TMPDIR");
5672 if ( tempDir
== nullptr )
5673 #if __MAC_OS_X_VERSION_MIN_REQUIRED
5674 tempDir
= "/private/tmp/";
5676 tempDir
= "/private/var/tmp/";
5678 strlcpy(closurePath
, tempDir
, PATH_MAX
);
5679 strlcat(closurePath
, "/com.apple.dyld/", PATH_MAX
);
5681 // make sure dyld sub-dir exists
5682 struct stat statbuf
;
5683 if ( ::stat(closurePath
, &statbuf
) != 0 ) {
5684 ::mkdir(closurePath
, S_IRWXU
);
5687 const char* leafName
= strrchr(mainExecutablePath
, '/');
5688 if ( leafName
== nullptr )
5689 leafName
= mainExecutablePath
;
5692 strlcat(closurePath
, leafName
, PATH_MAX
);
5694 #if __MAC_OS_X_VERSION_MIN_REQUIRED
5695 // on macOS we allow multiple closures by hashing the env vars into the cache filename
5696 strlcat(closurePath
, "-", PATH_MAX
);
5698 makeHashOfProgramAndEnv(mainExecutablePath
, mainExecutableCDHash
, envp
, hash32
);
5699 char hashString
[72];
5700 char* s
= hashString
;
5701 for (int i
=0; i
< 32; ++i
)
5702 putHexByte(hash32
[i
], s
);
5704 strlcat(closurePath
, hashString
, PATH_MAX
);
5706 // on iOS, the file name is the leaf name and UUID
5708 if ( mainExecutableMH
->getUuid(mainExeUUID
) ) {
5709 char mainUuidStr
[40];
5710 bytesToHex(mainExeUUID
, sizeof(uuid_t
), mainUuidStr
);
5711 strlcat(closurePath
, "-", PATH_MAX
);
5712 strlcat(closurePath
, mainUuidStr
, PATH_MAX
);
5715 strlcat(closurePath
, ".closure", PATH_MAX
);
5718 static const dyld3::closure::LaunchClosure
* mapClosureFile(const char* closurePath
)
5720 struct stat statbuf
;
5721 if ( ::stat(closurePath
, &statbuf
) == -1 )
5724 int fd
= ::open(closurePath
, O_RDONLY
);
5728 const dyld3::closure::LaunchClosure
* closure
= (dyld3::closure::LaunchClosure
*)::mmap(NULL
, (size_t)statbuf
.st_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
5731 if ( closure
== MAP_FAILED
)
5737 static const dyld3::closure::LaunchClosure
* buildLaunchClosure(const uint8_t* mainExecutableCDHash
,
5738 const dyld3::closure::LoadedFileInfo
& mainFileInfo
, const char* envp
[])
5740 const dyld3::MachOLoaded
* mainExecutableMH
= (const dyld3::MachOLoaded
*)mainFileInfo
.fileContent
;
5741 dyld3::closure::PathOverrides pathOverrides
;
5742 pathOverrides
.setFallbackPathHandling(gLinkContext
.allowClassicFallbackPaths
? dyld3::closure::PathOverrides::FallbackPathMode::classic
: dyld3::closure::PathOverrides::FallbackPathMode::restricted
);
5743 pathOverrides
.setEnvVars(envp
, mainExecutableMH
, mainFileInfo
.path
);
5744 STACK_ALLOC_ARRAY(const dyld3::closure::ImageArray
*, imagesArrays
, 3);
5745 if ( sSharedCacheLoadInfo
.loadAddress
!= nullptr ) {
5746 imagesArrays
.push_back(sSharedCacheLoadInfo
.loadAddress
->cachedDylibsImageArray());
5747 if ( auto others
= sSharedCacheLoadInfo
.loadAddress
->otherOSImageArray() )
5748 imagesArrays
.push_back(others
);
5751 dyld3::closure::ClosureBuilder::LaunchErrorInfo
* errorInfo
= (dyld3::closure::ClosureBuilder::LaunchErrorInfo
*)&gProcessInfo
->errorKind
;
5752 dyld3::closure::FileSystemPhysical fileSystem
;
5753 dyld3::closure::ClosureBuilder::AtPath atPathHanding
= (gLinkContext
.allowAtPaths
? dyld3::closure::ClosureBuilder::AtPath::all
: dyld3::closure::ClosureBuilder::AtPath::none
);
5754 dyld3::closure::ClosureBuilder
builder(dyld3::closure::kFirstLaunchClosureImageNum
, fileSystem
, sSharedCacheLoadInfo
.loadAddress
, true, pathOverrides
, atPathHanding
, errorInfo
, mainExecutableMH
->archName());
5755 const dyld3::closure::LaunchClosure
* result
= builder
.makeLaunchClosure(mainFileInfo
, gLinkContext
.allowInsertFailures
);
5756 if ( builder
.diagnostics().hasError() )
5757 halt(builder
.diagnostics().errorMessage());
5759 if ( result
== nullptr )
5762 if ( !closureValid(result
, mainFileInfo
, mainExecutableCDHash
, false, envp
) ) {
5763 // some how the freshly generated closure is invalid...
5764 if ( gLinkContext
.verboseWarnings
)
5765 dyld::log("dyld: somehow just built closure is invalid\n");
5768 // try to save closure to disk for next launch (atomically)
5769 char closurePath
[PATH_MAX
];
5770 buildClosureCachePath(mainFileInfo
.path
, mainExecutableMH
, mainExecutableCDHash
, envp
, closurePath
);
5771 char closurePathTemp
[PATH_MAX
];
5772 strlcpy(closurePathTemp
, closurePath
, PATH_MAX
);
5773 int mypid
= getpid();
5777 putHexByte(mypid
>> 24, s
);
5778 putHexByte(mypid
>> 16, s
);
5779 putHexByte(mypid
>> 8, s
);
5780 putHexByte(mypid
, s
);
5782 strlcat(closurePathTemp
, pidBuf
, PATH_MAX
);
5783 int fd
= ::open(closurePathTemp
, O_WRONLY
|O_CREAT
, S_IRUSR
|S_IWUSR
);
5785 ::ftruncate(fd
, result
->size());
5786 ::write(fd
, result
, result
->size());
5787 ::fchmod(fd
, S_IRUSR
);
5789 ::rename(closurePathTemp
, closurePath
);
5790 // free built closure and mmap file() to reduce dirty memory
5791 result
->deallocate();
5792 result
= mapClosureFile(closurePath
);
5794 else if ( gLinkContext
.verboseWarnings
) {
5795 dyld::log("could not save closure (errno=%d) to: %s\n", errno
, closurePathTemp
);
5798 if ( gLinkContext
.verboseWarnings
)
5799 dyld::log("dyld: just built closure %p (size=%lu) for %s\n", result
, result
->size(), sExecPath
);
5804 static const dyld3::closure::LaunchClosure
* findCachedLaunchClosure(const uint8_t* mainExecutableCDHash
,
5805 const dyld3::closure::LoadedFileInfo
& mainFileInfo
,
5808 char closurePath
[PATH_MAX
];
5809 buildClosureCachePath(mainFileInfo
.path
, (const dyld3::MachOLoaded
*)mainFileInfo
.fileContent
, mainExecutableCDHash
, envp
, closurePath
);
5810 const dyld3::closure::LaunchClosure
* closure
= mapClosureFile(closurePath
);
5811 if ( closure
== nullptr )
5814 if ( !closureValid(closure
, mainFileInfo
, mainExecutableCDHash
, false, envp
) ) {
5815 ::munmap((void*)closure
, closure
->size());
5819 if ( gLinkContext
.verboseWarnings
)
5820 dyld::log("dyld: used cached closure %p (size=%lu) for %s\n", closure
, closure
->size(), sExecPath
);
5825 #endif // !TARGET_IPHONE_SIMULATOR
5829 #if !__MAC_OS_X_VERSION_MIN_REQUIRED
5830 static const char* sWhiteListDirs
[] = {
5837 static bool inWhiteList(const char* execPath
)
5839 // First test to see if we forced in dyld2 via a kernel boot-arg
5840 if ( dyld3::bootArgsContains("force_dyld2=1") )
5843 #if __MAC_OS_X_VERSION_MIN_REQUIRED
5845 // rdar://problem/32701418: Don't use dyld3 for i386 for now.
5852 #endif // #if __i386__
5857 // <rdar://problem/33171968> enable dyld3 mode for all OS programs when using customer dyld cache (no roots)
5858 if ( (sSharedCacheLoadInfo
.loadAddress
!= nullptr) && (sSharedCacheLoadInfo
.loadAddress
->header
.cacheType
== kDyldSharedCacheTypeProduction
) )
5861 return dyld3::bootArgsContains("force_dyld3=1");
5865 #if !TARGET_IPHONE_SIMULATOR
5866 static bool isStagedApp(const dyld3::MachOFile
* mainExecutableMH
, const char* mainExecutablePath
)
5868 #if !__MAC_OS_X_VERSION_MIN_REQUIRED
5869 if ( (strncmp(mainExecutablePath
, "/var/containers/Bundle/Application/", 35) == 0)
5870 || (strncmp(mainExecutablePath
, "/private/var/containers/Bundle/Application/", 43) == 0) ) {
5871 // staged apps are built without LC_ENCRYPTION_INFO
5872 if ( !mainExecutableMH
->canBeFairPlayEncrypted() )
5881 // Entry point for dyld. The kernel loads dyld and jumps to __dyld_start which
5882 // sets up some registers and call this function.
5884 // Returns address of main() in target program which __dyld_start jumps to
5887 _main(const macho_header
* mainExecutableMH
, uintptr_t mainExecutableSlide
,
5888 int argc
, const char* argv
[], const char* envp
[], const char* apple
[],
5889 uintptr_t* startGlue
)
5891 if (dyld3::kdebug_trace_dyld_enabled(DBG_DYLD_TIMING_LAUNCH_EXECUTABLE
)) {
5892 launchTraceID
= dyld3::kdebug_trace_dyld_duration_start(DBG_DYLD_TIMING_LAUNCH_EXECUTABLE
, (uint64_t)mainExecutableMH
, 0, 0);
5895 // Grab the cdHash of the main executable from the environment
5896 uint8_t mainExecutableCDHashBuffer
[20];
5897 const uint8_t* mainExecutableCDHash
= nullptr;
5898 if ( hexToBytes(_simple_getenv(apple
, "executable_cdhash"), 40, mainExecutableCDHashBuffer
) )
5899 mainExecutableCDHash
= mainExecutableCDHashBuffer
;
5901 // Trace dyld's load
5902 notifyKernelAboutImage((macho_header
*)&__dso_handle
, _simple_getenv(apple
, "dyld_file"));
5903 #if !TARGET_IPHONE_SIMULATOR
5904 // Trace the main executable's load
5905 notifyKernelAboutImage(mainExecutableMH
, _simple_getenv(apple
, "executable_file"));
5908 uintptr_t result
= 0;
5909 sMainExecutableMachHeader
= mainExecutableMH
;
5910 sMainExecutableSlide
= mainExecutableSlide
;
5911 #if __MAC_OS_X_VERSION_MIN_REQUIRED
5912 // if this is host dyld, check to see if iOS simulator is being run
5913 const char* rootPath
= _simple_getenv(envp
, "DYLD_ROOT_PATH");
5914 if ( (rootPath
!= NULL
) ) {
5915 // look to see if simulator has its own dyld
5916 char simDyldPath
[PATH_MAX
];
5917 strlcpy(simDyldPath
, rootPath
, PATH_MAX
);
5918 strlcat(simDyldPath
, "/usr/lib/dyld_sim", PATH_MAX
);
5919 int fd
= my_open(simDyldPath
, O_RDONLY
, 0);
5921 const char* errMessage
= useSimulatorDyld(fd
, mainExecutableMH
, simDyldPath
, argc
, argv
, envp
, apple
, startGlue
, &result
);
5922 if ( errMessage
!= NULL
)
5929 CRSetCrashLogMessage("dyld: launch started");
5931 setContext(mainExecutableMH
, argc
, argv
, envp
, apple
);
5933 // Pickup the pointer to the exec path.
5934 sExecPath
= _simple_getenv(apple
, "executable_path");
5936 // <rdar://problem/13868260> Remove interim apple[0] transition code from dyld
5937 if (!sExecPath
) sExecPath
= apple
[0];
5939 if ( sExecPath
[0] != '/' ) {
5940 // have relative path, use cwd to make absolute
5941 char cwdbuff
[MAXPATHLEN
];
5942 if ( getcwd(cwdbuff
, MAXPATHLEN
) != NULL
) {
5943 // maybe use static buffer to avoid calling malloc so early...
5944 char* s
= new char[strlen(cwdbuff
) + strlen(sExecPath
) + 2];
5947 strcat(s
, sExecPath
);
5952 // Remember short name of process for later logging
5953 sExecShortName
= ::strrchr(sExecPath
, '/');
5954 if ( sExecShortName
!= NULL
)
5957 sExecShortName
= sExecPath
;
5959 configureProcessRestrictions(mainExecutableMH
);
5961 #if __MAC_OS_X_VERSION_MIN_REQUIRED
5962 if ( !gLinkContext
.allowEnvVarsPrint
&& !gLinkContext
.allowEnvVarsPath
&& !gLinkContext
.allowEnvVarsSharedCache
) {
5963 pruneEnvironmentVariables(envp
, &apple
);
5964 // set again because envp and apple may have changed or moved
5965 setContext(mainExecutableMH
, argc
, argv
, envp
, apple
);
5970 checkEnvironmentVariables(envp
);
5971 defaultUninitializedFallbackPaths(envp
);
5973 #if __MAC_OS_X_VERSION_MIN_REQUIRED
5974 if ( ((dyld3::MachOFile
*)mainExecutableMH
)->supportsPlatform(dyld3::Platform::iOSMac
)
5975 && !((dyld3::MachOFile
*)mainExecutableMH
)->supportsPlatform(dyld3::Platform::macOS
)) {
5976 gLinkContext
.rootPaths
= parseColonList("/System/iOSSupport", NULL
);
5977 gLinkContext
.marzipan
= true;
5978 if ( sEnv
.DYLD_FALLBACK_LIBRARY_PATH
== sLibraryFallbackPaths
)
5979 sEnv
.DYLD_FALLBACK_LIBRARY_PATH
= sRestrictedLibraryFallbackPaths
;
5980 if ( sEnv
.DYLD_FALLBACK_FRAMEWORK_PATH
== sFrameworkFallbackPaths
)
5981 sEnv
.DYLD_FALLBACK_FRAMEWORK_PATH
= sRestrictedFrameworkFallbackPaths
;
5984 if ( sEnv
.DYLD_PRINT_OPTS
)
5986 if ( sEnv
.DYLD_PRINT_ENV
)
5987 printEnvironmentVariables(envp
);
5988 getHostInfo(mainExecutableMH
, mainExecutableSlide
);
5990 // load shared cache
5991 checkSharedRegionDisable((dyld3::MachOLoaded
*)mainExecutableMH
, mainExecutableSlide
);
5992 #if TARGET_IPHONE_SIMULATOR
5993 // <HACK> until <rdar://30773711> is fixed
5994 gLinkContext
.sharedRegionMode
= ImageLoader::kUsePrivateSharedRegion
;
5997 if ( gLinkContext
.sharedRegionMode
!= ImageLoader::kDontUseSharedRegion
) {
6000 bool cacheCompatible
= (sSharedCacheLoadInfo
.loadAddress
== nullptr) || (sSharedCacheLoadInfo
.loadAddress
->header
.formatVersion
== dyld3::closure::kFormatVersion
);
6001 if ( cacheCompatible
&& (sEnableClosures
|| inWhiteList(sExecPath
)) ) {
6002 const dyld3::closure::LaunchClosure
* mainClosure
= nullptr;
6003 dyld3::closure::LoadedFileInfo mainFileInfo
;
6004 mainFileInfo
.fileContent
= mainExecutableMH
;
6005 mainFileInfo
.path
= sExecPath
;
6006 // FIXME: If we are saving this closure, this slice offset/length is probably wrong in the case of FAT files.
6007 mainFileInfo
.sliceOffset
= 0;
6008 mainFileInfo
.sliceLen
= std::numeric_limits
<__typeof(mainFileInfo
.sliceLen
)>::max();
6009 struct stat mainExeStatBuf
;
6010 if ( ::stat(sExecPath
, &mainExeStatBuf
) == 0 ) {
6011 mainFileInfo
.inode
= mainExeStatBuf
.st_ino
;
6012 mainFileInfo
.mtime
= mainExeStatBuf
.st_mtime
;
6014 // check for closure in cache first
6015 if ( sSharedCacheLoadInfo
.loadAddress
!= nullptr ) {
6016 mainClosure
= sSharedCacheLoadInfo
.loadAddress
->findClosure(sExecPath
);
6017 if ( gLinkContext
.verboseWarnings
&& (mainClosure
!= nullptr) )
6018 dyld::log("dyld: found closure %p (size=%lu) in dyld shared cache\n", mainClosure
, mainClosure
->size());
6020 #if !TARGET_IPHONE_SIMULATOR
6021 if ( (mainClosure
== nullptr) || !closureValid(mainClosure
, mainFileInfo
, mainExecutableCDHash
, true, envp
) ) {
6022 mainClosure
= nullptr;
6023 if ( sEnableClosures
|| isStagedApp((dyld3::MachOFile
*)mainExecutableMH
, sExecPath
) ) {
6024 // if forcing closures, and no closure in cache, or it is invalid, check for cached closure
6025 mainClosure
= findCachedLaunchClosure(mainExecutableCDHash
, mainFileInfo
, envp
);
6026 if ( mainClosure
== nullptr ) {
6027 // if no cached closure found, build new one
6028 mainClosure
= buildLaunchClosure(mainExecutableCDHash
, mainFileInfo
, envp
);
6033 // try using launch closure
6034 if ( mainClosure
!= nullptr ) {
6035 CRSetCrashLogMessage("dyld3: launch started");
6036 bool launched
= launchWithClosure(mainClosure
, sSharedCacheLoadInfo
.loadAddress
, (dyld3::MachOLoaded
*)mainExecutableMH
,
6037 mainExecutableSlide
, argc
, argv
, envp
, apple
, &result
, startGlue
);
6038 #if !TARGET_IPHONE_SIMULATOR
6040 // closure is out of date, build new one
6041 mainClosure
= buildLaunchClosure(mainExecutableCDHash
, mainFileInfo
, envp
);
6042 if ( mainClosure
!= nullptr ) {
6043 launched
= launchWithClosure(mainClosure
, sSharedCacheLoadInfo
.loadAddress
, (dyld3::MachOLoaded
*)mainExecutableMH
,
6044 mainExecutableSlide
, argc
, argv
, envp
, apple
, &result
, startGlue
);
6049 #if __has_feature(ptrauth_calls)
6050 // start() calls the result pointer as a function pointer so we need to sign it.
6051 result
= (uintptr_t)__builtin_ptrauth_sign_unauthenticated((void*)result
, 0, 0);
6054 result
= (uintptr_t)&fake_main
;
6058 if ( gLinkContext
.verboseWarnings
)
6059 dyld::log("dyld: unable to use closure %p\n", mainClosure
);
6064 if ( gLinkContext
.verboseWarnings
)
6065 dyld::log("dyld: not using closure because shared cache format version does not match dyld's\n");
6067 // could not use closure info, launch old way
6071 // install gdb notifier
6072 stateToHandlers(dyld_image_state_dependents_mapped
, sBatchHandlers
)->push_back(notifyGDB
);
6073 stateToHandlers(dyld_image_state_mapped
, sSingleHandlers
)->push_back(updateAllImages
);
6074 // make initial allocations large enough that it is unlikely to need to be re-alloced
6075 sImageRoots
.reserve(16);
6076 sAddImageCallbacks
.reserve(4);
6077 sRemoveImageCallbacks
.reserve(4);
6078 sAddLoadImageCallbacks
.reserve(4);
6079 sImageFilesNeedingTermination
.reserve(16);
6080 sImageFilesNeedingDOFUnregistration
.reserve(8);
6082 #if !TARGET_IPHONE_SIMULATOR
6083 #ifdef WAIT_FOR_SYSTEM_ORDER_HANDSHAKE
6084 // <rdar://problem/6849505> Add gating mechanism to dyld support system order file generation process
6085 WAIT_FOR_SYSTEM_ORDER_HANDSHAKE(dyld::gProcessInfo
->systemOrderFlag
);
6091 // add dyld itself to UUID list
6092 addDyldImageToUUIDList();
6094 #if SUPPORT_ACCELERATE_TABLES
6096 // Disable accelerator tables when we have threaded rebase/bind, which is arm64e executables only for now.
6097 if (sMainExecutableMachHeader
->cpusubtype
== CPU_SUBTYPE_ARM64_E
)
6098 sDisableAcceleratorTables
= true;
6100 bool mainExcutableAlreadyRebased
= false;
6101 if ( (sSharedCacheLoadInfo
.loadAddress
!= nullptr) && !dylibsCanOverrideCache() && !sDisableAcceleratorTables
&& (sSharedCacheLoadInfo
.loadAddress
->header
.accelerateInfoAddr
!= 0) ) {
6102 struct stat statBuf
;
6103 if ( ::stat(IPHONE_DYLD_SHARED_CACHE_DIR
"no-dyld2-accelerator-tables", &statBuf
) != 0 )
6104 sAllCacheImagesProxy
= ImageLoaderMegaDylib::makeImageLoaderMegaDylib(&sSharedCacheLoadInfo
.loadAddress
->header
, sSharedCacheLoadInfo
.slide
, mainExecutableMH
, gLinkContext
);
6110 CRSetCrashLogMessage(sLoadingCrashMessage
);
6111 // instantiate ImageLoader for main executable
6112 sMainExecutable
= instantiateFromLoadedImage(mainExecutableMH
, mainExecutableSlide
, sExecPath
);
6113 gLinkContext
.mainExecutable
= sMainExecutable
;
6114 gLinkContext
.mainExecutableCodeSigned
= hasCodeSignatureLoadCommand(mainExecutableMH
);
6116 #if TARGET_IPHONE_SIMULATOR
6117 // check main executable is not too new for this OS
6119 if ( ! isSimulatorBinary((uint8_t*)mainExecutableMH
, sExecPath
) ) {
6120 throwf("program was built for a platform that is not supported by this runtime");
6122 uint32_t mainMinOS
= sMainExecutable
->minOSVersion();
6124 // dyld is always built for the current OS, so we can get the current OS version
6125 // from the load command in dyld itself.
6126 uint32_t dyldMinOS
= ImageLoaderMachO::minOSVersion((const mach_header
*)&__dso_handle
);
6127 if ( mainMinOS
> dyldMinOS
) {
6129 throwf("app was built for watchOS %d.%d which is newer than this simulator %d.%d",
6130 mainMinOS
>> 16, ((mainMinOS
>> 8) & 0xFF),
6131 dyldMinOS
>> 16, ((dyldMinOS
>> 8) & 0xFF));
6133 throwf("app was built for tvOS %d.%d which is newer than this simulator %d.%d",
6134 mainMinOS
>> 16, ((mainMinOS
>> 8) & 0xFF),
6135 dyldMinOS
>> 16, ((dyldMinOS
>> 8) & 0xFF));
6137 throwf("app was built for iOS %d.%d which is newer than this simulator %d.%d",
6138 mainMinOS
>> 16, ((mainMinOS
>> 8) & 0xFF),
6139 dyldMinOS
>> 16, ((dyldMinOS
>> 8) & 0xFF));
6146 #if __MAC_OS_X_VERSION_MIN_REQUIRED
6147 // <rdar://problem/22805519> be less strict about old mach-o binaries
6148 uint32_t mainSDK
= sMainExecutable
->sdkVersion();
6149 gLinkContext
.strictMachORequired
= (mainSDK
>= DYLD_MACOSX_VERSION_10_12
) || gLinkContext
.allowInsertFailures
;
6151 // simulators, iOS, tvOS, and watchOS are always strict
6152 gLinkContext
.strictMachORequired
= true;
6155 #if SUPPORT_ACCELERATE_TABLES
6156 sAllImages
.reserve((sAllCacheImagesProxy
!= NULL
) ? 16 : INITIAL_IMAGE_COUNT
);
6158 sAllImages
.reserve(INITIAL_IMAGE_COUNT
);
6161 // Now that shared cache is loaded, setup an versioned dylib overrides
6162 #if SUPPORT_VERSIONED_PATHS
6163 checkVersionedPaths();
6167 // dyld_all_image_infos image list does not contain dyld
6168 // add it as dyldPath field in dyld_all_image_infos
6169 // for simulator, dyld_sim is in image list, need host dyld added
6170 #if TARGET_IPHONE_SIMULATOR
6171 // get path of host dyld from table of syscall vectors in host dyld
6172 void* addressInDyld
= gSyscallHelpers
;
6174 // get path of dyld itself
6175 void* addressInDyld
= (void*)&__dso_handle
;
6177 char dyldPathBuffer
[MAXPATHLEN
+1];
6178 int len
= proc_regionfilename(getpid(), (uint64_t)(long)addressInDyld
, dyldPathBuffer
, MAXPATHLEN
);
6180 dyldPathBuffer
[len
] = '\0'; // proc_regionfilename() does not zero terminate returned string
6181 if ( strcmp(dyldPathBuffer
, gProcessInfo
->dyldPath
) != 0 )
6182 gProcessInfo
->dyldPath
= strdup(dyldPathBuffer
);
6185 // load any inserted libraries
6186 if ( sEnv
.DYLD_INSERT_LIBRARIES
!= NULL
) {
6187 for (const char* const* lib
= sEnv
.DYLD_INSERT_LIBRARIES
; *lib
!= NULL
; ++lib
)
6188 loadInsertedDylib(*lib
);
6190 // record count of inserted libraries so that a flat search will look at
6191 // inserted libraries, then main, then others.
6192 sInsertedDylibCount
= sAllImages
.size()-1;
6194 // link main executable
6195 gLinkContext
.linkingMainExecutable
= true;
6196 #if SUPPORT_ACCELERATE_TABLES
6197 if ( mainExcutableAlreadyRebased
) {
6198 // previous link() on main executable has already adjusted its internal pointers for ASLR
6199 // work around that by rebasing by inverse amount
6200 sMainExecutable
->rebase(gLinkContext
, -mainExecutableSlide
);
6203 link(sMainExecutable
, sEnv
.DYLD_BIND_AT_LAUNCH
, true, ImageLoader::RPathChain(NULL
, NULL
), -1);
6204 sMainExecutable
->setNeverUnloadRecursive();
6205 if ( sMainExecutable
->forceFlat() ) {
6206 gLinkContext
.bindFlat
= true;
6207 gLinkContext
.prebindUsage
= ImageLoader::kUseNoPrebinding
;
6210 // link any inserted libraries
6211 // do this after linking main executable so that any dylibs pulled in by inserted
6212 // dylibs (e.g. libSystem) will not be in front of dylibs the program uses
6213 if ( sInsertedDylibCount
> 0 ) {
6214 for(unsigned int i
=0; i
< sInsertedDylibCount
; ++i
) {
6215 ImageLoader
* image
= sAllImages
[i
+1];
6216 link(image
, sEnv
.DYLD_BIND_AT_LAUNCH
, true, ImageLoader::RPathChain(NULL
, NULL
), -1);
6217 image
->setNeverUnloadRecursive();
6219 // only INSERTED libraries can interpose
6220 // register interposing info after all inserted libraries are bound so chaining works
6221 for(unsigned int i
=0; i
< sInsertedDylibCount
; ++i
) {
6222 ImageLoader
* image
= sAllImages
[i
+1];
6223 image
->registerInterposing(gLinkContext
);
6227 // <rdar://problem/19315404> dyld should support interposition even without DYLD_INSERT_LIBRARIES
6228 for (long i
=sInsertedDylibCount
+1; i
< sAllImages
.size(); ++i
) {
6229 ImageLoader
* image
= sAllImages
[i
];
6230 if ( image
->inSharedCache() )
6232 image
->registerInterposing(gLinkContext
);
6234 #if SUPPORT_ACCELERATE_TABLES
6235 if ( (sAllCacheImagesProxy
!= NULL
) && ImageLoader::haveInterposingTuples() ) {
6236 // Accelerator tables cannot be used with implicit interposing, so relaunch with accelerator tables disabled
6237 ImageLoader::clearInterposingTuples();
6238 // unmap all loaded dylibs (but not main executable)
6239 for (long i
=1; i
< sAllImages
.size(); ++i
) {
6240 ImageLoader
* image
= sAllImages
[i
];
6241 if ( image
== sMainExecutable
)
6243 if ( image
== sAllCacheImagesProxy
)
6245 image
->setCanUnload();
6246 ImageLoader::deleteImage(image
);
6248 // note: we don't need to worry about inserted images because if DYLD_INSERT_LIBRARIES was set we would not be using the accelerator table
6250 sImageRoots
.clear();
6251 sImageFilesNeedingTermination
.clear();
6252 sImageFilesNeedingDOFUnregistration
.clear();
6253 sAddImageCallbacks
.clear();
6254 sRemoveImageCallbacks
.clear();
6255 sAddLoadImageCallbacks
.clear();
6256 sDisableAcceleratorTables
= true;
6257 sAllCacheImagesProxy
= NULL
;
6258 sMappedRangesStart
= NULL
;
6259 mainExcutableAlreadyRebased
= true;
6260 gLinkContext
.linkingMainExecutable
= false;
6262 goto reloadAllImages
;
6266 // apply interposing to initial set of images
6267 for(int i
=0; i
< sImageRoots
.size(); ++i
) {
6268 sImageRoots
[i
]->applyInterposing(gLinkContext
);
6270 ImageLoader::applyInterposingToDyldCache(gLinkContext
);
6271 gLinkContext
.linkingMainExecutable
= false;
6273 // Bind and notify for the main executable now that interposing has been registered
6274 uint64_t bindMainExecutableStartTime
= mach_absolute_time();
6275 sMainExecutable
->recursiveBindWithAccounting(gLinkContext
, sEnv
.DYLD_BIND_AT_LAUNCH
, true);
6276 uint64_t bindMainExecutableEndTime
= mach_absolute_time();
6277 ImageLoaderMachO::fgTotalBindTime
+= bindMainExecutableEndTime
- bindMainExecutableStartTime
;
6278 gLinkContext
.notifyBatch(dyld_image_state_bound
, false);
6280 // Bind and notify for the inserted images now interposing has been registered
6281 if ( sInsertedDylibCount
> 0 ) {
6282 for(unsigned int i
=0; i
< sInsertedDylibCount
; ++i
) {
6283 ImageLoader
* image
= sAllImages
[i
+1];
6284 image
->recursiveBind(gLinkContext
, sEnv
.DYLD_BIND_AT_LAUNCH
, true);
6288 // <rdar://problem/12186933> do weak binding only after all inserted images linked
6289 sMainExecutable
->weakBind(gLinkContext
);
6291 // If cache has branch island dylibs, tell debugger about them
6292 if ( (sSharedCacheLoadInfo
.loadAddress
!= NULL
) && (sSharedCacheLoadInfo
.loadAddress
->header
.mappingOffset
>= 0x78) && (sSharedCacheLoadInfo
.loadAddress
->header
.branchPoolsOffset
!= 0) ) {
6293 uint32_t count
= sSharedCacheLoadInfo
.loadAddress
->header
.branchPoolsCount
;
6294 dyld_image_info info
[count
];
6295 const uint64_t* poolAddress
= (uint64_t*)((char*)sSharedCacheLoadInfo
.loadAddress
+ sSharedCacheLoadInfo
.loadAddress
->header
.branchPoolsOffset
);
6296 // <rdar://problem/20799203> empty branch pools can be in development cache
6297 if ( ((mach_header
*)poolAddress
)->magic
== sMainExecutableMachHeader
->magic
) {
6298 for (int poolIndex
=0; poolIndex
< count
; ++poolIndex
) {
6299 uint64_t poolAddr
= poolAddress
[poolIndex
] + sSharedCacheLoadInfo
.slide
;
6300 info
[poolIndex
].imageLoadAddress
= (mach_header
*)(long)poolAddr
;
6301 info
[poolIndex
].imageFilePath
= "dyld_shared_cache_branch_islands";
6302 info
[poolIndex
].imageFileModDate
= 0;
6304 // add to all_images list
6305 addImagesToAllImages(count
, info
);
6306 // tell gdb about new branch island images
6307 gProcessInfo
->notification(dyld_image_adding
, count
, info
);
6311 CRSetCrashLogMessage("dyld: launch, running initializers");
6312 #if SUPPORT_OLD_CRT_INITIALIZATION
6313 // Old way is to run initializers via a callback from crt1.o
6314 if ( ! gRunInitializersOldWay
)
6315 initializeMainExecutable();
6317 // run all initializers
6318 initializeMainExecutable();
6321 // notify any montoring proccesses that this process is about to enter main()
6322 if (dyld3::kdebug_trace_dyld_enabled(DBG_DYLD_TIMING_LAUNCH_EXECUTABLE
)) {
6323 dyld3::kdebug_trace_dyld_duration_end(launchTraceID
, DBG_DYLD_TIMING_LAUNCH_EXECUTABLE
, 0, 0, 2);
6325 notifyMonitoringDyldMain();
6327 // find entry point for main executable
6328 result
= (uintptr_t)sMainExecutable
->getEntryFromLC_MAIN();
6329 if ( result
!= 0 ) {
6330 // main executable uses LC_MAIN, we need to use helper in libdyld to call into main()
6331 if ( (gLibSystemHelpers
!= NULL
) && (gLibSystemHelpers
->version
>= 9) )
6332 *startGlue
= (uintptr_t)gLibSystemHelpers
->startGlueToCallExit
;
6334 halt("libdyld.dylib support not present for LC_MAIN");
6337 // main executable uses LC_UNIXTHREAD, dyld needs to let "start" in program set up for main()
6338 result
= (uintptr_t)sMainExecutable
->getEntryFromLC_UNIXTHREAD();
6341 #if __has_feature(ptrauth_calls)
6342 // start() calls the result pointer as a function pointer so we need to sign it.
6343 result
= (uintptr_t)__builtin_ptrauth_sign_unauthenticated((void*)result
, 0, 0);
6346 catch(const char* message
) {
6351 dyld::log("dyld: launch failed\n");
6354 CRSetCrashLogMessage("dyld2 mode");
6357 if (dyld3::kdebug_trace_dyld_enabled(DBG_DYLD_TIMING_LAUNCH_EXECUTABLE
)) {
6358 dyld3::kdebug_trace_dyld_duration_end(launchTraceID
, DBG_DYLD_TIMING_LAUNCH_EXECUTABLE
, 0, 0, 2);
6360 result
= (uintptr_t)&fake_main
;
6361 *startGlue
= (uintptr_t)gLibSystemHelpers
->startGlueToCallExit
;