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@
31 #include <sys/param.h>
32 #include <mach/mach_time.h> // mach_absolute_time()
33 #include <mach/mach_init.h>
34 #include <sys/types.h>
36 #include <sys/syscall.h>
37 #include <sys/socket.h>
39 #include <sys/syslog.h>
41 #include <mach-o/fat.h>
42 #include <mach-o/loader.h>
43 #include <mach-o/ldsyms.h>
44 #include <libkern/OSByteOrder.h>
45 #include <libkern/OSAtomic.h>
46 #include <mach/mach.h>
47 #include <sys/sysctl.h>
49 #include <sys/dtrace.h>
50 #include <libkern/OSAtomic.h>
51 #include <Availability.h>
52 #include <System/sys/codesign.h>
54 #include <os/lock_private.h>
57 #ifndef CPU_SUBTYPE_ARM_V5TEJ
58 #define CPU_SUBTYPE_ARM_V5TEJ ((cpu_subtype_t) 7)
60 #ifndef CPU_SUBTYPE_ARM_XSCALE
61 #define CPU_SUBTYPE_ARM_XSCALE ((cpu_subtype_t) 8)
63 #ifndef CPU_SUBTYPE_ARM_V7
64 #define CPU_SUBTYPE_ARM_V7 ((cpu_subtype_t) 9)
66 #ifndef CPU_SUBTYPE_ARM_V7F
67 #define CPU_SUBTYPE_ARM_V7F ((cpu_subtype_t) 10)
69 #ifndef CPU_SUBTYPE_ARM_V7S
70 #define CPU_SUBTYPE_ARM_V7S ((cpu_subtype_t) 11)
72 #ifndef CPU_SUBTYPE_ARM_V7K
73 #define CPU_SUBTYPE_ARM_V7K ((cpu_subtype_t) 12)
75 #ifndef LC_DYLD_ENVIRONMENT
76 #define LC_DYLD_ENVIRONMENT 0x27
79 #ifndef CPU_SUBTYPE_X86_64_H
80 #define CPU_SUBTYPE_X86_64_H ((cpu_subtype_t) 8)
84 #define VM_PROT_SLIDE 0x20
90 #include "mach-o/dyld_gdb.h"
93 #include "ImageLoader.h"
94 #include "ImageLoaderMachO.h"
95 #include "dyldLibSystemInterface.h"
96 #include "dyldSyscallInterface.h"
97 #if DYLD_SHARED_CACHE_SUPPORT
98 #include "dyld_cache_format.h"
100 #if TARGET_IPHONE_SIMULATOR
101 void coresymbolication_load_image(void*, const ImageLoader
*, uint64_t);
102 void coresymbolication_unload_image(void*, const ImageLoader
*);
104 #include "coreSymbolicationDyldSupport.hpp"
107 // not libc header for send() syscall interface
108 extern "C" ssize_t
__sendto(int, const void *, size_t, int, const struct sockaddr
*, socklen_t
);
111 // ARM and x86_64 are the only architecture that use cpu-sub-types
112 #define CPU_SUBTYPES_SUPPORTED ((__arm__ || __x86_64__) && !TARGET_IPHONE_SIMULATOR)
116 #define CPU_TYPE_MASK 0x00FFFFFF /* complement of CPU_ARCH_MASK */
119 /* implemented in dyld_gdb.cpp */
120 extern void addImagesToAllImages(uint32_t infoCount
, const dyld_image_info info
[]);
121 extern void removeImageFromAllImages(const mach_header
* mh
);
122 extern void setAlImageInfosHalt(const char* message
, uintptr_t flags
);
123 extern void addNonSharedCacheImageUUID(const dyld_uuid_info
& info
);
124 extern const char* notifyGDB(enum dyld_image_states state
, uint32_t infoCount
, const dyld_image_info info
[]);
126 // magic so CrashReporter logs message
128 char error_string
[1024];
130 // implemented in dyldStartup.s for CrashReporter
131 extern "C" void dyld_fatal_error(const char* errString
) __attribute__((noreturn
));
133 // magic linker symbol for start of dyld binary
134 extern "C" const macho_header __dso_handle
;
138 // The file contains the core of dyld used to get a process to main().
139 // The API's that dyld supports are implemented in dyldAPIs.cpp.
146 struct RegisteredDOF
{ const mach_header
* mh
; int registrationID
; };
147 struct DylibOverride
{ const char* installName
; const char* override
; };
151 VECTOR_NEVER_DESTRUCTED(ImageLoader
*);
152 VECTOR_NEVER_DESTRUCTED(dyld::RegisteredDOF
);
153 VECTOR_NEVER_DESTRUCTED(dyld::ImageCallback
);
154 VECTOR_NEVER_DESTRUCTED(dyld::DylibOverride
);
155 VECTOR_NEVER_DESTRUCTED(ImageLoader::DynamicReference
);
157 VECTOR_NEVER_DESTRUCTED(dyld_image_state_change_handler
);
163 // state of all environment variables dyld uses
165 struct EnvironmentVariables
{
166 const char* const * DYLD_FRAMEWORK_PATH
;
167 const char* const * DYLD_FALLBACK_FRAMEWORK_PATH
;
168 const char* const * DYLD_LIBRARY_PATH
;
169 const char* const * DYLD_FALLBACK_LIBRARY_PATH
;
170 const char* const * DYLD_INSERT_LIBRARIES
;
171 const char* const * LD_LIBRARY_PATH
; // for unix conformance
172 const char* const * DYLD_VERSIONED_LIBRARY_PATH
;
173 const char* const * DYLD_VERSIONED_FRAMEWORK_PATH
;
174 bool DYLD_PRINT_LIBRARIES
;
175 bool DYLD_PRINT_LIBRARIES_POST_LAUNCH
;
176 bool DYLD_BIND_AT_LAUNCH
;
177 bool DYLD_PRINT_STATISTICS
;
178 bool DYLD_PRINT_OPTS
;
180 bool DYLD_DISABLE_DOFS
;
181 bool DYLD_PRINT_CS_NOTIFICATIONS
;
182 // DYLD_SHARED_CACHE_DONT_VALIDATE ==> sSharedCacheIgnoreInodeAndTimeStamp
183 // DYLD_SHARED_CACHE_DIR ==> sSharedCacheDir
184 // DYLD_ROOT_PATH ==> gLinkContext.rootPaths
185 // DYLD_IMAGE_SUFFIX ==> gLinkContext.imageSuffix
186 // DYLD_PRINT_OPTS ==> gLinkContext.verboseOpts
187 // DYLD_PRINT_ENV ==> gLinkContext.verboseEnv
188 // DYLD_FORCE_FLAT_NAMESPACE ==> gLinkContext.bindFlat
189 // DYLD_PRINT_INITIALIZERS ==> gLinkContext.verboseInit
190 // DYLD_PRINT_SEGMENTS ==> gLinkContext.verboseMapping
191 // DYLD_PRINT_BINDINGS ==> gLinkContext.verboseBind
192 // DYLD_PRINT_WEAK_BINDINGS ==> gLinkContext.verboseWeakBind
193 // DYLD_PRINT_REBASINGS ==> gLinkContext.verboseRebase
194 // DYLD_PRINT_DOFS ==> gLinkContext.verboseDOF
195 // DYLD_PRINT_APIS ==> gLogAPIs
196 // DYLD_IGNORE_PREBINDING ==> gLinkContext.prebindUsage
197 // DYLD_PREBIND_DEBUG ==> gLinkContext.verbosePrebinding
198 // DYLD_NEW_LOCAL_SHARED_REGIONS ==> gLinkContext.sharedRegionMode
199 // DYLD_SHARED_REGION ==> gLinkContext.sharedRegionMode
200 // DYLD_PRINT_WARNINGS ==> gLinkContext.verboseWarnings
201 // DYLD_PRINT_RPATHS ==> gLinkContext.verboseRPaths
202 // DYLD_PRINT_INTERPOSING ==> gLinkContext.verboseInterposing
207 typedef std::vector
<dyld_image_state_change_handler
> StateHandlers
;
210 enum RestrictedReason
{ restrictedNot
, restrictedBySetGUid
, restrictedBySegment
, restrictedByEntitlements
};
213 static const char* sExecPath
= NULL
;
214 static const char* sExecShortName
= NULL
;
215 static const macho_header
* sMainExecutableMachHeader
= NULL
;
216 #if CPU_SUBTYPES_SUPPORTED
217 static cpu_type_t sHostCPU
;
218 static cpu_subtype_t sHostCPUsubtype
;
220 static ImageLoader
* sMainExecutable
= NULL
;
221 static bool sProcessIsRestricted
= false;
222 static RestrictedReason sRestrictedReason
= restrictedNot
;
223 static size_t sInsertedDylibCount
= 0;
224 static std::vector
<ImageLoader
*> sAllImages
;
225 static std::vector
<ImageLoader
*> sImageRoots
;
226 static std::vector
<ImageLoader
*> sImageFilesNeedingTermination
;
227 static std::vector
<RegisteredDOF
> sImageFilesNeedingDOFUnregistration
;
228 static std::vector
<ImageCallback
> sAddImageCallbacks
;
229 static std::vector
<ImageCallback
> sRemoveImageCallbacks
;
230 static bool sRemoveImageCallbacksInUse
= false;
231 static void* sSingleHandlers
[7][3];
232 static void* sBatchHandlers
[7][3];
233 static ImageLoader
* sLastImageByAddressCache
;
234 static EnvironmentVariables sEnv
;
235 static const char* sFrameworkFallbackPaths
[] = { "$HOME/Library/Frameworks", "/Library/Frameworks", "/Network/Library/Frameworks", "/System/Library/Frameworks", NULL
};
236 static const char* sLibraryFallbackPaths
[] = { "$HOME/lib", "/usr/local/lib", "/usr/lib", NULL
};
237 static UndefinedHandler sUndefinedHandler
= NULL
;
238 static ImageLoader
* sBundleBeingLoaded
= NULL
; // hack until OFI is reworked
239 #if DYLD_SHARED_CACHE_SUPPORT
240 static const dyld_cache_header
* sSharedCache
= NULL
;
241 static long sSharedCacheSlide
= 0;
242 static bool sSharedCacheIgnoreInodeAndTimeStamp
= false;
243 bool gSharedCacheOverridden
= false;
244 #if __IPHONE_OS_VERSION_MIN_REQUIRED
245 static const char* sSharedCacheDir
= IPHONE_DYLD_SHARED_CACHE_DIR
;
246 static bool sDylibsOverrideCache
= false;
247 #define ENABLE_DYLIBS_TO_OVERRIDE_CACHE_SIZE 1024
249 static const char* sSharedCacheDir
= MACOSX_DYLD_SHARED_CACHE_DIR
;
252 ImageLoader::LinkContext gLinkContext
;
253 bool gLogAPIs
= false;
254 const struct LibSystemHelpers
* gLibSystemHelpers
= NULL
;
255 #if SUPPORT_OLD_CRT_INITIALIZATION
256 bool gRunInitializersOldWay
= false;
258 static std::vector
<DylibOverride
> sDylibOverrides
;
259 #if !TARGET_IPHONE_SIMULATOR
260 static int sLogSocket
= -1;
262 static bool sFrameworksFoundAsDylibs
= false;
264 static bool sHaswell
= false;
266 static std::vector
<ImageLoader::DynamicReference
> sDynamicReferences
;
267 static bool sLogToFile
= false;
268 static char sLoadingCrashMessage
[1024] = "dyld: launch, loading dependent libraries";
271 // The MappedRanges structure is used for fast address->image lookups.
272 // The table is only updated when the dyld lock is held, so we don't
273 // need to worry about multiple writers. But readers may look at this
274 // data without holding the lock. Therefore, all updates must be done
275 // in an order that will never cause readers to see inconsistent data.
276 // The general rule is that if the image field is non-NULL then
277 // the other fields are valid.
290 static MappedRanges sMappedRangesStart
;
292 void addMappedRange(ImageLoader
* image
, uintptr_t start
, uintptr_t end
)
294 //dyld::log("addMappedRange(0x%lX->0x%lX) for %s\n", start, end, image->getShortName());
295 for (MappedRanges
* p
= &sMappedRangesStart
; p
!= NULL
; p
= p
->next
) {
296 for (int i
=0; i
< MappedRanges::count
; ++i
) {
297 if ( p
->array
[i
].image
== NULL
) {
298 p
->array
[i
].start
= start
;
299 p
->array
[i
].end
= end
;
300 // add image field last with a barrier so that any reader will see consistent records
302 p
->array
[i
].image
= image
;
307 // table must be full, chain another
308 MappedRanges
* newRanges
= (MappedRanges
*)malloc(sizeof(MappedRanges
));
309 bzero(newRanges
, sizeof(MappedRanges
));
310 newRanges
->array
[0].start
= start
;
311 newRanges
->array
[0].end
= end
;
312 newRanges
->array
[0].image
= image
;
313 for (MappedRanges
* p
= &sMappedRangesStart
; p
!= NULL
; p
= p
->next
) {
314 if ( p
->next
== NULL
) {
322 void removedMappedRanges(ImageLoader
* image
)
324 for (MappedRanges
* p
= &sMappedRangesStart
; p
!= NULL
; p
= p
->next
) {
325 for (int i
=0; i
< MappedRanges::count
; ++i
) {
326 if ( p
->array
[i
].image
== image
) {
327 // clear with a barrier so that any reader will see consistent records
329 p
->array
[i
].image
= NULL
;
335 ImageLoader
* findMappedRange(uintptr_t target
)
337 for (MappedRanges
* p
= &sMappedRangesStart
; p
!= NULL
; p
= p
->next
) {
338 for (int i
=0; i
< MappedRanges::count
; ++i
) {
339 if ( p
->array
[i
].image
!= NULL
) {
340 if ( (p
->array
[i
].start
<= target
) && (target
< p
->array
[i
].end
) )
341 return p
->array
[i
].image
;
350 const char* mkstringf(const char* format
, ...)
352 _SIMPLE_STRING buf
= _simple_salloc();
355 va_start(list
, format
);
356 _simple_vsprintf(buf
, format
, list
);
358 const char* t
= strdup(_simple_string(buf
));
363 return "mkstringf, out of memory error";
367 void throwf(const char* format
, ...)
369 _SIMPLE_STRING buf
= _simple_salloc();
372 va_start(list
, format
);
373 _simple_vsprintf(buf
, format
, list
);
375 const char* t
= strdup(_simple_string(buf
));
380 throw "throwf, out of memory error";
384 #if !TARGET_IPHONE_SIMULATOR
385 static int sLogfile
= STDERR_FILENO
;
389 static int sBindingsLogfile
= -1;
390 static void mysprintf(char* dst
, const char* format
, ...)
392 _SIMPLE_STRING buf
= _simple_salloc();
395 va_start(list
, format
);
396 _simple_vsprintf(buf
, format
, list
);
398 strcpy(dst
, _simple_string(buf
));
402 strcpy(dst
, "out of memory");
405 void logBindings(const char* format
, ...)
407 if ( sBindingsLogfile
!= -1 ) {
409 va_start(list
, format
);
410 _simple_vdprintf(sBindingsLogfile
, format
, list
);
416 #if !TARGET_IPHONE_SIMULATOR
417 // based on CFUtilities.c: also_do_stderr()
418 static bool useSyslog()
420 // Use syslog() for processes managed by launchd
421 if ( (gLibSystemHelpers
!= NULL
) && (gLibSystemHelpers
->version
>= 11) ) {
422 if ( (*gLibSystemHelpers
->isLaunchdOwned
)() ) {
427 // If stderr is not available, use syslog()
429 int result
= fstat(STDERR_FILENO
, &sb
);
431 return true; // file descriptor 2 is closed
437 static void socket_syslogv(int priority
, const char* format
, va_list list
)
439 // lazily create socket and connection to syslogd
440 if ( sLogSocket
== -1 ) {
441 sLogSocket
= ::socket(AF_UNIX
, SOCK_DGRAM
, 0);
442 if (sLogSocket
== -1)
443 return; // cannot log
444 ::fcntl(sLogSocket
, F_SETFD
, 1);
446 struct sockaddr_un addr
;
447 addr
.sun_family
= AF_UNIX
;
448 strncpy(addr
.sun_path
, _PATH_LOG
, sizeof(addr
.sun_path
));
449 if ( ::connect(sLogSocket
, (struct sockaddr
*)&addr
, sizeof(addr
)) == -1 ) {
456 // format message to syslogd like: "<priority>Process[pid]: message"
457 _SIMPLE_STRING buf
= _simple_salloc();
460 if ( _simple_sprintf(buf
, "<%d>%s[%d]: ", LOG_USER
|LOG_NOTICE
, sExecShortName
, getpid()) == 0 ) {
461 if ( _simple_vsprintf(buf
, format
, list
) == 0 ) {
462 const char* p
= _simple_string(buf
);
463 ::__sendto(sLogSocket
, p
, strlen(p
), 0, NULL
, 0);
469 void vlog(const char* format
, va_list list
)
471 if ( !sLogToFile
&& useSyslog() )
472 socket_syslogv(LOG_ERR
, format
, list
);
474 _simple_vdprintf(sLogfile
, format
, list
);
478 void log(const char* format
, ...)
481 va_start(list
, format
);
487 void vwarn(const char* format
, va_list list
)
489 _simple_dprintf(sLogfile
, "dyld: warning, ");
490 _simple_vdprintf(sLogfile
, format
, list
);
493 void warn(const char* format
, ...)
496 va_start(list
, format
);
502 #endif // !TARGET_IPHONE_SIMULATOR
505 // <rdar://problem/8867781> control access to sAllImages through a lock
506 // because global dyld lock is not held during initialization phase of dlopen()
507 // <rdar://problem/16145518> Use OSSpinLockLock to allow yielding
508 static OSSpinLock sAllImagesLock
= 0;
510 static void allImagesLock()
512 //dyld::log("allImagesLock()\n");
513 #if TARGET_IPHONE_SIMULATOR
514 // <rdar://problem/16154256> can't use OSSpinLockLock in simulator until thread_switch is provided by host dyld
515 while ( ! OSAtomicCompareAndSwapPtrBarrier((void*)0, (void*)1, (void**)&sAllImagesLock
) ) {
519 OSSpinLockLock(&sAllImagesLock
);
523 static void allImagesUnlock()
525 //dyld::log("allImagesUnlock()\n");
526 #if TARGET_IPHONE_SIMULATOR
527 while ( ! OSAtomicCompareAndSwapPtrBarrier((void*)1, (void*)0, (void**)&sAllImagesLock
) ) {
531 OSSpinLockUnlock(&sAllImagesLock
);
538 // utility class to assure files are closed when an exception is thrown
541 FileOpener(const char* path
);
543 int getFileDescriptor() { return fd
; }
548 FileOpener::FileOpener(const char* path
)
551 fd
= my_open(path
, O_RDONLY
, 0);
554 FileOpener::~FileOpener()
561 static void registerDOFs(const std::vector
<ImageLoader::DOFInfo
>& dofs
)
563 const size_t dofSectionCount
= dofs
.size();
564 if ( !sEnv
.DYLD_DISABLE_DOFS
&& (dofSectionCount
!= 0) ) {
565 int fd
= open("/dev/" DTRACEMNR_HELPER
, O_RDWR
);
567 //dyld::warn("can't open /dev/" DTRACEMNR_HELPER " to register dtrace DOF sections\n");
570 // allocate a buffer on the stack for the variable length dof_ioctl_data_t type
571 uint8_t buffer
[sizeof(dof_ioctl_data_t
) + dofSectionCount
*sizeof(dof_helper_t
)];
572 dof_ioctl_data_t
* ioctlData
= (dof_ioctl_data_t
*)buffer
;
574 // fill in buffer with one dof_helper_t per DOF section
575 ioctlData
->dofiod_count
= dofSectionCount
;
576 for (unsigned int i
=0; i
< dofSectionCount
; ++i
) {
577 strlcpy(ioctlData
->dofiod_helpers
[i
].dofhp_mod
, dofs
[i
].imageShortName
, DTRACE_MODNAMELEN
);
578 ioctlData
->dofiod_helpers
[i
].dofhp_dof
= (uintptr_t)(dofs
[i
].dof
);
579 ioctlData
->dofiod_helpers
[i
].dofhp_addr
= (uintptr_t)(dofs
[i
].dof
);
582 // tell kernel about all DOF sections en mas
583 // pass pointer to ioctlData because ioctl() only copies a fixed size amount of data into kernel
584 user_addr_t val
= (user_addr_t
)(unsigned long)ioctlData
;
585 if ( ioctl(fd
, DTRACEHIOC_ADDDOF
, &val
) != -1 ) {
586 // kernel returns a unique identifier for each section in the dofiod_helpers[].dofhp_dof field.
587 for (unsigned int i
=0; i
< dofSectionCount
; ++i
) {
589 info
.mh
= dofs
[i
].imageHeader
;
590 info
.registrationID
= (int)(ioctlData
->dofiod_helpers
[i
].dofhp_dof
);
591 sImageFilesNeedingDOFUnregistration
.push_back(info
);
592 if ( gLinkContext
.verboseDOF
) {
593 dyld::log("dyld: registering DOF section %p in %s with dtrace, ID=0x%08X\n",
594 dofs
[i
].dof
, dofs
[i
].imageShortName
, info
.registrationID
);
599 //dyld::log( "dyld: ioctl to register dtrace DOF section failed\n");
606 static void unregisterDOF(int registrationID
)
608 int fd
= open("/dev/" DTRACEMNR_HELPER
, O_RDWR
);
610 dyld::warn("can't open /dev/" DTRACEMNR_HELPER
" to unregister dtrace DOF section\n");
613 ioctl(fd
, DTRACEHIOC_REMOVE
, registrationID
);
615 if ( gLinkContext
.verboseInit
)
616 dyld::warn("unregistering DOF section ID=0x%08X with dtrace\n", registrationID
);
622 // _dyld_register_func_for_add_image() is implemented as part of the general image state change notification
624 static void notifyAddImageCallbacks(ImageLoader
* image
)
626 // use guard so that we cannot notify about the same image twice
627 if ( ! image
->addFuncNotified() ) {
628 for (std::vector
<ImageCallback
>::iterator it
=sAddImageCallbacks
.begin(); it
!= sAddImageCallbacks
.end(); it
++)
629 (*it
)(image
->machHeader(), image
->getSlide());
630 image
->setAddFuncNotified();
636 // notify gdb about these new images
637 static const char* updateAllImages(enum dyld_image_states state
, uint32_t infoCount
, const struct dyld_image_info info
[])
639 // <rdar://problem/8812589> don't add images without paths to all-image-info-list
640 if ( info
[0].imageFilePath
!= NULL
)
641 addImagesToAllImages(infoCount
, info
);
646 static StateHandlers
* stateToHandlers(dyld_image_states state
, void* handlersArray
[7][3])
649 case dyld_image_state_mapped
:
650 return reinterpret_cast<StateHandlers
*>(&handlersArray
[0]);
652 case dyld_image_state_dependents_mapped
:
653 return reinterpret_cast<StateHandlers
*>(&handlersArray
[1]);
655 case dyld_image_state_rebased
:
656 return reinterpret_cast<StateHandlers
*>(&handlersArray
[2]);
658 case dyld_image_state_bound
:
659 return reinterpret_cast<StateHandlers
*>(&handlersArray
[3]);
661 case dyld_image_state_dependents_initialized
:
662 return reinterpret_cast<StateHandlers
*>(&handlersArray
[4]);
664 case dyld_image_state_initialized
:
665 return reinterpret_cast<StateHandlers
*>(&handlersArray
[5]);
667 case dyld_image_state_terminated
:
668 return reinterpret_cast<StateHandlers
*>(&handlersArray
[6]);
673 static void notifySingle(dyld_image_states state
, const ImageLoader
* image
)
675 //dyld::log("notifySingle(state=%d, image=%s)\n", state, image->getPath());
676 std::vector
<dyld_image_state_change_handler
>* handlers
= stateToHandlers(state
, sSingleHandlers
);
677 if ( handlers
!= NULL
) {
678 dyld_image_info info
;
679 info
.imageLoadAddress
= image
->machHeader();
680 info
.imageFilePath
= image
->getRealPath();
681 info
.imageFileModDate
= image
->lastModified();
682 for (std::vector
<dyld_image_state_change_handler
>::iterator it
= handlers
->begin(); it
!= handlers
->end(); ++it
) {
683 const char* result
= (*it
)(state
, 1, &info
);
684 if ( (result
!= NULL
) && (state
== dyld_image_state_mapped
) ) {
685 //fprintf(stderr, " image rejected by handler=%p\n", *it);
686 // make copy of thrown string so that later catch clauses can free it
687 const char* str
= strdup(result
);
692 if ( state
== dyld_image_state_mapped
) {
693 // <rdar://problem/7008875> Save load addr + UUID for images from outside the shared cache
694 if ( !image
->inSharedCache() ) {
696 if ( image
->getUUID(info
.imageUUID
) ) {
697 info
.imageLoadAddress
= image
->machHeader();
698 addNonSharedCacheImageUUID(info
);
702 // mach message csdlc about dynamically loaded images
703 if ( image
->addFuncNotified() && (state
== dyld_image_state_terminated
) ) {
704 if ( sEnv
.DYLD_PRINT_CS_NOTIFICATIONS
) {
705 dyld::log("dyld core symbolication unload notification: %p %s\n", image
->machHeader(), image
->getPath());
707 if ( dyld::gProcessInfo
->coreSymbolicationShmPage
!= NULL
) {
708 #if TARGET_IPHONE_SIMULATOR
709 void* connection
= dyld::gProcessInfo
->coreSymbolicationShmPage
;
710 if ( *((uint32_t*)connection
) == 2 ) {
712 CSCppDyldSharedMemoryPage
* connection
= (CSCppDyldSharedMemoryPage
*)dyld::gProcessInfo
->coreSymbolicationShmPage
;
713 if ( connection
->is_valid_version() ) {
715 coresymbolication_unload_image(connection
, image
);
725 // Normally, dyld_all_image_infos is only updated in batches after an entire
726 // graph is loaded. But if there is an error loading the initial set of
727 // dylibs needed by the main executable, dyld_all_image_infos is not yet set
728 // up, leading to usually brief crash logs.
730 // This function manually adds the images loaded so far to dyld::gProcessInfo.
731 // It should only be called before terminating.
735 for (std::vector
<ImageLoader
*>::iterator it
=sAllImages
.begin(); it
!= sAllImages
.end(); ++it
) {
736 dyld_image_info info
;
737 ImageLoader
* image
= *it
;
738 info
.imageLoadAddress
= image
->machHeader();
739 info
.imageFilePath
= image
->getRealPath();
740 info
.imageFileModDate
= image
->lastModified();
741 // add to all_image_infos if not already there
743 int existingCount
= dyld::gProcessInfo
->infoArrayCount
;
744 const dyld_image_info
* existing
= dyld::gProcessInfo
->infoArray
;
745 if ( existing
!= NULL
) {
746 for (int i
=0; i
< existingCount
; ++i
) {
747 if ( existing
[i
].imageLoadAddress
== info
.imageLoadAddress
) {
748 //dyld::log("not adding %s\n", info.imageFilePath);
755 //dyld::log("adding %s\n", info.imageFilePath);
756 addImagesToAllImages(1, &info
);
762 static int imageSorter(const void* l
, const void* r
)
764 const ImageLoader
* left
= *((ImageLoader
**)l
);
765 const ImageLoader
* right
= *((ImageLoader
**)r
);
766 return left
->compare(right
);
769 static void notifyBatchPartial(dyld_image_states state
, bool orLater
, dyld_image_state_change_handler onlyHandler
)
771 std::vector
<dyld_image_state_change_handler
>* handlers
= stateToHandlers(state
, sBatchHandlers
);
772 if ( handlers
!= NULL
) {
773 // don't use a vector because it will use malloc/free and we want notifcation to be low cost
775 ImageLoader
* images
[sAllImages
.size()+1];
776 ImageLoader
** end
= images
;
777 for (std::vector
<ImageLoader
*>::iterator it
=sAllImages
.begin(); it
!= sAllImages
.end(); it
++) {
778 dyld_image_states imageState
= (*it
)->getState();
779 if ( (imageState
== state
) || (orLater
&& (imageState
> state
)) )
782 if ( sBundleBeingLoaded
!= NULL
) {
783 dyld_image_states imageState
= sBundleBeingLoaded
->getState();
784 if ( (imageState
== state
) || (orLater
&& (imageState
> state
)) )
785 *end
++ = sBundleBeingLoaded
;
787 const char* dontLoadReason
= NULL
;
788 uint32_t count
= (uint32_t)(end
-images
);
789 if ( end
!= images
) {
791 qsort(images
, count
, sizeof(ImageLoader
*), &imageSorter
);
793 dyld_image_info infos
[count
];
794 for (unsigned int i
=0; i
< count
; ++i
) {
795 dyld_image_info
* p
= &infos
[i
];
796 ImageLoader
* image
= images
[i
];
797 //dyld::log(" state=%d, name=%s\n", state, image->getPath());
798 p
->imageLoadAddress
= image
->machHeader();
799 p
->imageFilePath
= image
->getRealPath();
800 p
->imageFileModDate
= image
->lastModified();
801 // special case for add_image hook
802 if ( state
== dyld_image_state_bound
)
803 notifyAddImageCallbacks(image
);
806 if ( onlyHandler
!= NULL
) {
807 const char* result
= (*onlyHandler
)(state
, count
, infos
);
808 if ( (result
!= NULL
) && (state
== dyld_image_state_dependents_mapped
) ) {
809 //fprintf(stderr, " images rejected by handler=%p\n", onlyHandler);
810 // make copy of thrown string so that later catch clauses can free it
811 dontLoadReason
= strdup(result
);
815 // call each handler with whole array
816 for (std::vector
<dyld_image_state_change_handler
>::iterator it
= handlers
->begin(); it
!= handlers
->end(); ++it
) {
817 const char* result
= (*it
)(state
, count
, infos
);
818 if ( (result
!= NULL
) && (state
== dyld_image_state_dependents_mapped
) ) {
819 //fprintf(stderr, " images rejected by handler=%p\n", *it);
820 // make copy of thrown string so that later catch clauses can free it
821 dontLoadReason
= strdup(result
);
828 if ( dontLoadReason
!= NULL
)
829 throw dontLoadReason
;
831 if ( state
== dyld_image_state_rebased
) {
832 if ( sEnv
.DYLD_PRINT_CS_NOTIFICATIONS
) {
833 for (std::vector
<ImageLoader
*>::iterator it
=sAllImages
.begin(); it
!= sAllImages
.end(); it
++) {
834 dyld_image_states imageState
= (*it
)->getState();
835 if ( (imageState
== dyld_image_state_rebased
) || (orLater
&& (imageState
> dyld_image_state_rebased
)) )
836 dyld::log("dyld core symbolication load notification: %p %s\n", (*it
)->machHeader(), (*it
)->getPath());
839 if ( dyld::gProcessInfo
->coreSymbolicationShmPage
!= NULL
) {
840 #if TARGET_IPHONE_SIMULATOR
841 void* connection
= dyld::gProcessInfo
->coreSymbolicationShmPage
;
842 if ( *((uint32_t*)connection
) == 2 ) {
844 CSCppDyldSharedMemoryPage
* connection
= (CSCppDyldSharedMemoryPage
*)dyld::gProcessInfo
->coreSymbolicationShmPage
;
845 if ( connection
->is_valid_version() ) {
847 // This needs to be captured now
848 uint64_t load_timestamp
= mach_absolute_time();
849 for (std::vector
<ImageLoader
*>::iterator it
=sAllImages
.begin(); it
!= sAllImages
.end(); it
++) {
850 dyld_image_states imageState
= (*it
)->getState();
851 if ( (imageState
== state
) || (orLater
&& (imageState
> state
)) )
852 coresymbolication_load_image(connection
, *it
, load_timestamp
);
861 static void notifyBatch(dyld_image_states state
)
863 notifyBatchPartial(state
, false, NULL
);
866 // In order for register_func_for_add_image() callbacks to to be called bottom up,
867 // we need to maintain a list of root images. The main executable is usally the
868 // first root. Any images dynamically added are also roots (unless already loaded).
869 // If DYLD_INSERT_LIBRARIES is used, those libraries are first.
870 static void addRootImage(ImageLoader
* image
)
872 //dyld::log("addRootImage(%p, %s)\n", image, image->getPath());
873 // add to list of roots
874 sImageRoots
.push_back(image
);
878 static void clearAllDepths()
880 for (std::vector
<ImageLoader
*>::iterator it
=sAllImages
.begin(); it
!= sAllImages
.end(); it
++)
884 static void printAllDepths()
886 for (std::vector
<ImageLoader
*>::iterator it
=sAllImages
.begin(); it
!= sAllImages
.end(); it
++)
887 dyld::log("%03d %s\n", (*it
)->getDepth(), (*it
)->getShortName());
891 static unsigned int imageCount()
893 return (unsigned int)sAllImages
.size();
897 static void setNewProgramVars(const ProgramVars
& newVars
)
899 // make a copy of the pointers to program variables
900 gLinkContext
.programVars
= newVars
;
902 // now set each program global to their initial value
903 *gLinkContext
.programVars
.NXArgcPtr
= gLinkContext
.argc
;
904 *gLinkContext
.programVars
.NXArgvPtr
= gLinkContext
.argv
;
905 *gLinkContext
.programVars
.environPtr
= gLinkContext
.envp
;
906 *gLinkContext
.programVars
.__prognamePtr
= gLinkContext
.progname
;
909 #if SUPPORT_OLD_CRT_INITIALIZATION
910 static void setRunInitialzersOldWay()
912 gRunInitializersOldWay
= true;
916 static void addDynamicReference(ImageLoader
* from
, ImageLoader
* to
) {
917 // don't add dynamic reference if either are in the shared cache
918 if( from
->inSharedCache() )
920 if( to
->inSharedCache() )
923 // don't add dynamic reference if there already is a static one
924 if ( from
->dependsOn(to
) )
927 // don't add if this combination already exists
928 for (std::vector
<ImageLoader::DynamicReference
>::iterator it
=sDynamicReferences
.begin(); it
!= sDynamicReferences
.end(); ++it
) {
929 if ( (it
->from
== from
) && (it
->to
== to
) )
932 //dyld::log("addDynamicReference(%s, %s\n", from->getShortName(), to->getShortName());
933 ImageLoader::DynamicReference t
;
936 sDynamicReferences
.push_back(t
);
939 static void addImage(ImageLoader
* image
)
941 // add to master list
943 sAllImages
.push_back(image
);
946 // update mapped ranges
947 uintptr_t lastSegStart
= 0;
948 uintptr_t lastSegEnd
= 0;
949 for(unsigned int i
=0, e
=image
->segmentCount(); i
< e
; ++i
) {
950 if ( image
->segUnaccessible(i
) )
952 uintptr_t start
= image
->segActualLoadAddress(i
);
953 uintptr_t end
= image
->segActualEndAddress(i
);
954 if ( start
== lastSegEnd
) {
955 // two segments are contiguous, just record combined segments
959 // non-contiguous segments, record last (if any)
960 if ( lastSegEnd
!= 0 )
961 addMappedRange(image
, lastSegStart
, lastSegEnd
);
962 lastSegStart
= start
;
966 if ( lastSegEnd
!= 0 )
967 addMappedRange(image
, lastSegStart
, lastSegEnd
);
970 if ( sEnv
.DYLD_PRINT_LIBRARIES
|| (sEnv
.DYLD_PRINT_LIBRARIES_POST_LAUNCH
&& (sMainExecutable
!=NULL
) && sMainExecutable
->isLinked()) ) {
971 dyld::log("dyld: loaded: %s\n", image
->getPath());
977 // Helper for std::remove_if
981 RefUsesImage(ImageLoader
* image
) : _image(image
) {}
982 bool operator()(const ImageLoader::DynamicReference
& ref
) const {
983 return ( (ref
.from
== _image
) || (ref
.to
== _image
) );
991 void removeImage(ImageLoader
* image
)
993 // if has dtrace DOF section, tell dtrace it is going away, then remove from sImageFilesNeedingDOFUnregistration
994 for (std::vector
<RegisteredDOF
>::iterator it
=sImageFilesNeedingDOFUnregistration
.begin(); it
!= sImageFilesNeedingDOFUnregistration
.end(); ) {
995 if ( it
->mh
== image
->machHeader() ) {
996 unregisterDOF(it
->registrationID
);
997 sImageFilesNeedingDOFUnregistration
.erase(it
);
998 // don't increment iterator, the erase caused next element to be copied to where this iterator points
1005 // tell all registered remove image handlers about this
1006 // do this before removing image from internal data structures so that the callback can query dyld about the image
1007 if ( image
->getState() >= dyld_image_state_bound
) {
1008 sRemoveImageCallbacksInUse
= true; // This only runs inside dyld's global lock, so ok to use a global for the in-use flag.
1009 for (std::vector
<ImageCallback
>::iterator it
=sRemoveImageCallbacks
.begin(); it
!= sRemoveImageCallbacks
.end(); it
++) {
1010 (*it
)(image
->machHeader(), image
->getSlide());
1012 sRemoveImageCallbacksInUse
= false;
1016 notifySingle(dyld_image_state_terminated
, image
);
1018 // remove from mapped images table
1019 removedMappedRanges(image
);
1021 // remove from master list
1023 for (std::vector
<ImageLoader
*>::iterator it
=sAllImages
.begin(); it
!= sAllImages
.end(); it
++) {
1024 if ( *it
== image
) {
1025 sAllImages
.erase(it
);
1031 // remove from sDynamicReferences
1032 sDynamicReferences
.erase(std::remove_if(sDynamicReferences
.begin(), sDynamicReferences
.end(), RefUsesImage(image
)), sDynamicReferences
.end());
1034 // flush find-by-address cache (do this after removed from master list, so there is no chance it can come back)
1035 if ( sLastImageByAddressCache
== image
)
1036 sLastImageByAddressCache
= NULL
;
1038 // if in root list, pull it out
1039 for (std::vector
<ImageLoader
*>::iterator it
=sImageRoots
.begin(); it
!= sImageRoots
.end(); it
++) {
1040 if ( *it
== image
) {
1041 sImageRoots
.erase(it
);
1047 if ( sEnv
.DYLD_PRINT_LIBRARIES
|| (sEnv
.DYLD_PRINT_LIBRARIES_POST_LAUNCH
&& (sMainExecutable
!=NULL
) && sMainExecutable
->isLinked()) ) {
1048 dyld::log("dyld: unloaded: %s\n", image
->getPath());
1051 // tell gdb, new way
1052 removeImageFromAllImages(image
->machHeader());
1056 void runImageStaticTerminators(ImageLoader
* image
)
1058 // if in termination list, pull it out and run terminator
1061 mightBeMore
= false;
1062 for (std::vector
<ImageLoader
*>::iterator it
=sImageFilesNeedingTermination
.begin(); it
!= sImageFilesNeedingTermination
.end(); it
++) {
1063 if ( *it
== image
) {
1064 sImageFilesNeedingTermination
.erase(it
);
1065 if (gLogAPIs
) dyld::log("dlclose(), running static terminators for %p %s\n", image
, image
->getShortName());
1066 image
->doTermination(gLinkContext
);
1071 } while ( mightBeMore
);
1074 static void terminationRecorder(ImageLoader
* image
)
1076 sImageFilesNeedingTermination
.push_back(image
);
1079 const char* getExecutablePath()
1084 static void runAllStaticTerminators(void* extra
)
1087 const size_t imageCount
= sImageFilesNeedingTermination
.size();
1088 for(size_t i
=imageCount
; i
> 0; --i
){
1089 ImageLoader
* image
= sImageFilesNeedingTermination
[i
-1];
1090 image
->doTermination(gLinkContext
);
1092 sImageFilesNeedingTermination
.clear();
1093 notifyBatch(dyld_image_state_terminated
);
1095 catch (const char* msg
) {
1100 void initializeMainExecutable()
1102 // record that we've reached this step
1103 gLinkContext
.startedInitializingMainExecutable
= true;
1105 // run initialzers for any inserted dylibs
1106 ImageLoader::InitializerTimingList initializerTimes
[sAllImages
.size()];
1107 initializerTimes
[0].count
= 0;
1108 const size_t rootCount
= sImageRoots
.size();
1109 if ( rootCount
> 1 ) {
1110 for(size_t i
=1; i
< rootCount
; ++i
) {
1111 sImageRoots
[i
]->runInitializers(gLinkContext
, initializerTimes
[0]);
1115 // run initializers for main executable and everything it brings up
1116 sMainExecutable
->runInitializers(gLinkContext
, initializerTimes
[0]);
1118 // register cxa_atexit() handler to run static terminators in all loaded images when this process exits
1119 if ( gLibSystemHelpers
!= NULL
)
1120 (*gLibSystemHelpers
->cxa_atexit
)(&runAllStaticTerminators
, NULL
, NULL
);
1122 // dump info if requested
1123 if ( sEnv
.DYLD_PRINT_STATISTICS
)
1124 ImageLoaderMachO::printStatistics((unsigned int)sAllImages
.size(), initializerTimes
[0]);
1127 bool mainExecutablePrebound()
1129 return sMainExecutable
->usablePrebinding(gLinkContext
);
1132 ImageLoader
* mainExecutable()
1134 return sMainExecutable
;
1140 #if SUPPORT_VERSIONED_PATHS
1142 // forward reference
1143 static bool getDylibVersionAndInstallname(const char* dylibPath
, uint32_t* version
, char* installName
);
1147 // Examines a dylib file and if its current_version is newer than the installed
1148 // dylib at its install_name, then add the dylib file to sDylibOverrides.
1150 static void checkDylibOverride(const char* dylibFile
)
1152 //dyld::log("checkDylibOverride('%s')\n", dylibFile);
1153 uint32_t altVersion
;
1154 char sysInstallName
[PATH_MAX
];
1155 if ( getDylibVersionAndInstallname(dylibFile
, &altVersion
, sysInstallName
) ) {
1156 //dyld::log("%s has version 0x%08X and install name %s\n", dylibFile, altVersion, sysInstallName);
1157 uint32_t sysVersion
;
1158 if ( getDylibVersionAndInstallname(sysInstallName
, &sysVersion
, NULL
) ) {
1159 //dyld::log("%s has version 0x%08X\n", sysInstallName, sysVersion);
1160 if ( altVersion
> sysVersion
) {
1161 //dyld::log("override found: %s -> %s\n", sysInstallName, dylibFile);
1162 // see if there already is an override for this dylib
1163 bool entryExists
= false;
1164 for (std::vector
<DylibOverride
>::iterator it
= sDylibOverrides
.begin(); it
!= sDylibOverrides
.end(); ++it
) {
1165 if ( strcmp(it
->installName
, sysInstallName
) == 0 ) {
1167 uint32_t prevVersion
;
1168 if ( getDylibVersionAndInstallname(it
->override
, &prevVersion
, NULL
) ) {
1169 if ( altVersion
> prevVersion
) {
1170 // found an even newer override
1171 free((void*)(it
->override
));
1172 char resolvedPath
[PATH_MAX
];
1173 if ( realpath(dylibFile
, resolvedPath
) != NULL
)
1174 it
->override
= strdup(resolvedPath
);
1176 it
->override
= strdup(dylibFile
);
1182 if ( ! entryExists
) {
1183 DylibOverride entry
;
1184 entry
.installName
= strdup(sysInstallName
);
1185 char resolvedPath
[PATH_MAX
];
1186 if ( realpath(dylibFile
, resolvedPath
) != NULL
)
1187 entry
.override
= strdup(resolvedPath
);
1189 entry
.override
= strdup(dylibFile
);
1190 sDylibOverrides
.push_back(entry
);
1191 //dyld::log("added override: %s -> %s\n", entry.installName, entry.override);
1199 static void checkDylibOverridesInDir(const char* dirPath
)
1201 //dyld::log("checkDylibOverridesInDir('%s')\n", dirPath);
1202 char dylibPath
[PATH_MAX
];
1203 int dirPathLen
= strlen(dirPath
);
1204 strlcpy(dylibPath
, dirPath
, PATH_MAX
);
1205 DIR* dirp
= opendir(dirPath
);
1206 if ( dirp
!= NULL
) {
1208 dirent
* entp
= NULL
;
1209 while ( readdir_r(dirp
, &entry
, &entp
) == 0 ) {
1212 if ( entp
->d_type
!= DT_REG
)
1214 dylibPath
[dirPathLen
] = '/';
1215 dylibPath
[dirPathLen
+1] = '\0';
1216 if ( strlcat(dylibPath
, entp
->d_name
, PATH_MAX
) > PATH_MAX
)
1218 checkDylibOverride(dylibPath
);
1225 static void checkFrameworkOverridesInDir(const char* dirPath
)
1227 //dyld::log("checkFrameworkOverridesInDir('%s')\n", dirPath);
1228 char frameworkPath
[PATH_MAX
];
1229 int dirPathLen
= strlen(dirPath
);
1230 strlcpy(frameworkPath
, dirPath
, PATH_MAX
);
1231 DIR* dirp
= opendir(dirPath
);
1232 if ( dirp
!= NULL
) {
1234 dirent
* entp
= NULL
;
1235 while ( readdir_r(dirp
, &entry
, &entp
) == 0 ) {
1238 if ( entp
->d_type
!= DT_DIR
)
1240 frameworkPath
[dirPathLen
] = '/';
1241 frameworkPath
[dirPathLen
+1] = '\0';
1242 int dirNameLen
= strlen(entp
->d_name
);
1243 if ( dirNameLen
< 11 )
1245 if ( strcmp(&entp
->d_name
[dirNameLen
-10], ".framework") != 0 )
1247 if ( strlcat(frameworkPath
, entp
->d_name
, PATH_MAX
) > PATH_MAX
)
1249 if ( strlcat(frameworkPath
, "/", PATH_MAX
) > PATH_MAX
)
1251 if ( strlcat(frameworkPath
, entp
->d_name
, PATH_MAX
) > PATH_MAX
)
1253 frameworkPath
[strlen(frameworkPath
)-10] = '\0';
1254 checkDylibOverride(frameworkPath
);
1259 #endif // SUPPORT_VERSIONED_PATHS
1263 // Turns a colon separated list of strings into a NULL terminated array
1264 // of string pointers. If mainExecutableDir param is not NULL,
1265 // substitutes @loader_path with main executable's dir.
1267 static const char** parseColonList(const char* list
, const char* mainExecutableDir
)
1269 static const char* sEmptyList
[] = { NULL
};
1271 if ( list
[0] == '\0' )
1275 for(const char* s
=list
; *s
!= '\0'; ++s
) {
1281 const char* start
= list
;
1282 char** result
= new char*[colonCount
+2];
1283 for(const char* s
=list
; *s
!= '\0'; ++s
) {
1285 size_t len
= s
-start
;
1286 if ( (mainExecutableDir
!= NULL
) && (strncmp(start
, "@loader_path/", 13) == 0) ) {
1287 size_t mainExecDirLen
= strlen(mainExecutableDir
);
1288 char* str
= new char[mainExecDirLen
+len
+1];
1289 strcpy(str
, mainExecutableDir
);
1290 strlcat(str
, &start
[13], mainExecDirLen
+len
+1);
1291 str
[mainExecDirLen
+len
-13] = '\0';
1293 result
[index
++] = str
;
1295 else if ( (mainExecutableDir
!= NULL
) && (strncmp(start
, "@executable_path/", 17) == 0) ) {
1296 size_t mainExecDirLen
= strlen(mainExecutableDir
);
1297 char* str
= new char[mainExecDirLen
+len
+1];
1298 strcpy(str
, mainExecutableDir
);
1299 strlcat(str
, &start
[17], mainExecDirLen
+len
+1);
1300 str
[mainExecDirLen
+len
-17] = '\0';
1302 result
[index
++] = str
;
1305 char* str
= new char[len
+1];
1306 strncpy(str
, start
, len
);
1309 result
[index
++] = str
;
1313 size_t len
= strlen(start
);
1314 if ( (mainExecutableDir
!= NULL
) && (strncmp(start
, "@loader_path/", 13) == 0) ) {
1315 size_t mainExecDirLen
= strlen(mainExecutableDir
);
1316 char* str
= new char[mainExecDirLen
+len
+1];
1317 strcpy(str
, mainExecutableDir
);
1318 strlcat(str
, &start
[13], mainExecDirLen
+len
+1);
1319 str
[mainExecDirLen
+len
-13] = '\0';
1320 result
[index
++] = str
;
1322 else if ( (mainExecutableDir
!= NULL
) && (strncmp(start
, "@executable_path/", 17) == 0) ) {
1323 size_t mainExecDirLen
= strlen(mainExecutableDir
);
1324 char* str
= new char[mainExecDirLen
+len
+1];
1325 strcpy(str
, mainExecutableDir
);
1326 strlcat(str
, &start
[17], mainExecDirLen
+len
+1);
1327 str
[mainExecDirLen
+len
-17] = '\0';
1328 result
[index
++] = str
;
1331 char* str
= new char[len
+1];
1333 result
[index
++] = str
;
1335 result
[index
] = NULL
;
1337 //dyld::log("parseColonList(%s)\n", list);
1338 //for(int i=0; result[i] != NULL; ++i)
1339 // dyld::log(" %s\n", result[i]);
1340 return (const char**)result
;
1343 static void appendParsedColonList(const char* list
, const char* mainExecutableDir
, const char* const ** storage
)
1345 const char** newlist
= parseColonList(list
, mainExecutableDir
);
1346 if ( *storage
== NULL
) {
1347 // first time, just set
1351 // need to append to existing list
1352 const char* const* existing
= *storage
;
1354 for(int i
=0; existing
[i
] != NULL
; ++i
)
1356 for(int i
=0; newlist
[i
] != NULL
; ++i
)
1358 const char** combinedList
= new const char*[count
+2];
1360 for(int i
=0; existing
[i
] != NULL
; ++i
)
1361 combinedList
[index
++] = existing
[i
];
1362 for(int i
=0; newlist
[i
] != NULL
; ++i
)
1363 combinedList
[index
++] = newlist
[i
];
1364 combinedList
[index
] = NULL
;
1366 *storage
= combinedList
;
1371 static void paths_expand_roots(const char **paths
, const char *key
, const char *val
)
1373 // assert(val != NULL);
1374 // assert(paths != NULL);
1376 size_t keyLen
= strlen(key
);
1377 for(int i
=0; paths
[i
] != NULL
; ++i
) {
1378 if ( strncmp(paths
[i
], key
, keyLen
) == 0 ) {
1379 char* newPath
= new char[strlen(val
) + (strlen(paths
[i
]) - keyLen
) + 1];
1380 strcpy(newPath
, val
);
1381 strcat(newPath
, &paths
[i
][keyLen
]);
1389 static void removePathWithPrefix(const char* paths
[], const char* prefix
)
1391 size_t prefixLen
= strlen(prefix
);
1394 for(i
= 0; paths
[i
] != NULL
; ++i
) {
1395 if ( strncmp(paths
[i
], prefix
, prefixLen
) == 0 )
1398 paths
[i
-skip
] = paths
[i
];
1400 paths
[i
-skip
] = NULL
;
1405 static void paths_dump(const char **paths
)
1407 // assert(paths != NULL);
1408 const char **strs
= paths
;
1409 while(*strs
!= NULL
)
1411 dyld::log("\"%s\"\n", *strs
);
1418 static void printOptions(const char* argv
[])
1421 while ( NULL
!= argv
[i
] ) {
1422 dyld::log("opt[%i] = \"%s\"\n", i
, argv
[i
]);
1427 static void printEnvironmentVariables(const char* envp
[])
1429 while ( NULL
!= *envp
) {
1430 dyld::log("%s\n", *envp
);
1435 void processDyldEnvironmentVariable(const char* key
, const char* value
, const char* mainExecutableDir
)
1437 if ( strcmp(key
, "DYLD_FRAMEWORK_PATH") == 0 ) {
1438 appendParsedColonList(value
, mainExecutableDir
, &sEnv
.DYLD_FRAMEWORK_PATH
);
1440 else if ( strcmp(key
, "DYLD_FALLBACK_FRAMEWORK_PATH") == 0 ) {
1441 appendParsedColonList(value
, mainExecutableDir
, &sEnv
.DYLD_FALLBACK_FRAMEWORK_PATH
);
1443 else if ( strcmp(key
, "DYLD_LIBRARY_PATH") == 0 ) {
1444 appendParsedColonList(value
, mainExecutableDir
, &sEnv
.DYLD_LIBRARY_PATH
);
1446 else if ( strcmp(key
, "DYLD_FALLBACK_LIBRARY_PATH") == 0 ) {
1447 appendParsedColonList(value
, mainExecutableDir
, &sEnv
.DYLD_FALLBACK_LIBRARY_PATH
);
1449 else if ( (strcmp(key
, "DYLD_ROOT_PATH") == 0) || (strcmp(key
, "DYLD_PATHS_ROOT") == 0) ) {
1450 if ( strcmp(value
, "/") != 0 ) {
1451 gLinkContext
.rootPaths
= parseColonList(value
, mainExecutableDir
);
1452 for (int i
=0; gLinkContext
.rootPaths
[i
] != NULL
; ++i
) {
1453 if ( gLinkContext
.rootPaths
[i
][0] != '/' ) {
1454 dyld::warn("DYLD_ROOT_PATH not used because it contains a non-absolute path\n");
1455 gLinkContext
.rootPaths
= NULL
;
1461 else if ( strcmp(key
, "DYLD_IMAGE_SUFFIX") == 0 ) {
1462 gLinkContext
.imageSuffix
= value
;
1464 else if ( strcmp(key
, "DYLD_INSERT_LIBRARIES") == 0 ) {
1465 sEnv
.DYLD_INSERT_LIBRARIES
= parseColonList(value
, NULL
);
1467 else if ( strcmp(key
, "DYLD_PRINT_OPTS") == 0 ) {
1468 sEnv
.DYLD_PRINT_OPTS
= true;
1470 else if ( strcmp(key
, "DYLD_PRINT_ENV") == 0 ) {
1471 sEnv
.DYLD_PRINT_ENV
= true;
1473 else if ( strcmp(key
, "DYLD_DISABLE_DOFS") == 0 ) {
1474 sEnv
.DYLD_DISABLE_DOFS
= true;
1476 else if ( strcmp(key
, "DYLD_DISABLE_PREFETCH") == 0 ) {
1477 gLinkContext
.preFetchDisabled
= true;
1479 else if ( strcmp(key
, "DYLD_PRINT_LIBRARIES") == 0 ) {
1480 sEnv
.DYLD_PRINT_LIBRARIES
= true;
1482 else if ( strcmp(key
, "DYLD_PRINT_LIBRARIES_POST_LAUNCH") == 0 ) {
1483 sEnv
.DYLD_PRINT_LIBRARIES_POST_LAUNCH
= true;
1485 else if ( strcmp(key
, "DYLD_BIND_AT_LAUNCH") == 0 ) {
1486 sEnv
.DYLD_BIND_AT_LAUNCH
= true;
1488 else if ( strcmp(key
, "DYLD_FORCE_FLAT_NAMESPACE") == 0 ) {
1489 gLinkContext
.bindFlat
= true;
1491 else if ( strcmp(key
, "DYLD_NEW_LOCAL_SHARED_REGIONS") == 0 ) {
1492 // ignore, no longer relevant but some scripts still set it
1494 else if ( strcmp(key
, "DYLD_NO_FIX_PREBINDING") == 0 ) {
1496 else if ( strcmp(key
, "DYLD_PREBIND_DEBUG") == 0 ) {
1497 gLinkContext
.verbosePrebinding
= true;
1499 else if ( strcmp(key
, "DYLD_PRINT_INITIALIZERS") == 0 ) {
1500 gLinkContext
.verboseInit
= true;
1502 else if ( strcmp(key
, "DYLD_PRINT_DOFS") == 0 ) {
1503 gLinkContext
.verboseDOF
= true;
1505 else if ( strcmp(key
, "DYLD_PRINT_STATISTICS") == 0 ) {
1506 sEnv
.DYLD_PRINT_STATISTICS
= true;
1508 else if ( strcmp(key
, "DYLD_PRINT_SEGMENTS") == 0 ) {
1509 gLinkContext
.verboseMapping
= true;
1511 else if ( strcmp(key
, "DYLD_PRINT_BINDINGS") == 0 ) {
1512 gLinkContext
.verboseBind
= true;
1514 else if ( strcmp(key
, "DYLD_PRINT_WEAK_BINDINGS") == 0 ) {
1515 gLinkContext
.verboseWeakBind
= true;
1517 else if ( strcmp(key
, "DYLD_PRINT_REBASINGS") == 0 ) {
1518 gLinkContext
.verboseRebase
= true;
1520 else if ( strcmp(key
, "DYLD_PRINT_APIS") == 0 ) {
1523 else if ( strcmp(key
, "DYLD_PRINT_WARNINGS") == 0 ) {
1524 gLinkContext
.verboseWarnings
= true;
1526 else if ( strcmp(key
, "DYLD_PRINT_RPATHS") == 0 ) {
1527 gLinkContext
.verboseRPaths
= true;
1529 else if ( strcmp(key
, "DYLD_PRINT_CS_NOTIFICATIONS") == 0 ) {
1530 sEnv
.DYLD_PRINT_CS_NOTIFICATIONS
= true;
1532 else if ( strcmp(key
, "DYLD_PRINT_INTERPOSING") == 0 ) {
1533 gLinkContext
.verboseInterposing
= true;
1535 else if ( strcmp(key
, "DYLD_PRINT_CODE_SIGNATURES") == 0 ) {
1536 gLinkContext
.verboseCodeSignatures
= true;
1538 else if ( strcmp(key
, "DYLD_SHARED_REGION") == 0 ) {
1539 if ( strcmp(value
, "private") == 0 ) {
1540 gLinkContext
.sharedRegionMode
= ImageLoader::kUsePrivateSharedRegion
;
1542 else if ( strcmp(value
, "avoid") == 0 ) {
1543 gLinkContext
.sharedRegionMode
= ImageLoader::kDontUseSharedRegion
;
1545 else if ( strcmp(value
, "use") == 0 ) {
1546 gLinkContext
.sharedRegionMode
= ImageLoader::kUseSharedRegion
;
1548 else if ( value
[0] == '\0' ) {
1549 gLinkContext
.sharedRegionMode
= ImageLoader::kUseSharedRegion
;
1552 dyld::warn("unknown option to DYLD_SHARED_REGION. Valid options are: use, private, avoid\n");
1555 #if DYLD_SHARED_CACHE_SUPPORT
1556 else if ( strcmp(key
, "DYLD_SHARED_CACHE_DIR") == 0 ) {
1557 sSharedCacheDir
= value
;
1559 else if ( strcmp(key
, "DYLD_SHARED_CACHE_DONT_VALIDATE") == 0 ) {
1560 sSharedCacheIgnoreInodeAndTimeStamp
= true;
1563 else if ( strcmp(key
, "DYLD_IGNORE_PREBINDING") == 0 ) {
1564 if ( strcmp(value
, "all") == 0 ) {
1565 gLinkContext
.prebindUsage
= ImageLoader::kUseNoPrebinding
;
1567 else if ( strcmp(value
, "app") == 0 ) {
1568 gLinkContext
.prebindUsage
= ImageLoader::kUseAllButAppPredbinding
;
1570 else if ( strcmp(value
, "nonsplit") == 0 ) {
1571 gLinkContext
.prebindUsage
= ImageLoader::kUseSplitSegPrebinding
;
1573 else if ( value
[0] == '\0' ) {
1574 gLinkContext
.prebindUsage
= ImageLoader::kUseSplitSegPrebinding
;
1577 dyld::warn("unknown option to DYLD_IGNORE_PREBINDING. Valid options are: all, app, nonsplit\n");
1580 #if SUPPORT_VERSIONED_PATHS
1581 else if ( strcmp(key
, "DYLD_VERSIONED_LIBRARY_PATH") == 0 ) {
1582 appendParsedColonList(value
, mainExecutableDir
, &sEnv
.DYLD_VERSIONED_LIBRARY_PATH
);
1584 else if ( strcmp(key
, "DYLD_VERSIONED_FRAMEWORK_PATH") == 0 ) {
1585 appendParsedColonList(value
, mainExecutableDir
, &sEnv
.DYLD_VERSIONED_FRAMEWORK_PATH
);
1588 else if ( strcmp(key
, "DYLD_PRINT_TO_FILE") == 0 ) {
1589 // handled in _main()
1592 dyld::warn("unknown environment variable: %s\n", key
);
1597 #if SUPPORT_LC_DYLD_ENVIRONMENT
1598 static void checkLoadCommandEnvironmentVariables()
1600 // <rdar://problem/8440934> Support augmenting dyld environment variables in load commands
1601 const uint32_t cmd_count
= sMainExecutableMachHeader
->ncmds
;
1602 const struct load_command
* const cmds
= (struct load_command
*)(((char*)sMainExecutableMachHeader
)+sizeof(macho_header
));
1603 const struct load_command
* cmd
= cmds
;
1604 for (uint32_t i
= 0; i
< cmd_count
; ++i
) {
1606 case LC_DYLD_ENVIRONMENT
:
1608 const struct dylinker_command
* envcmd
= (struct dylinker_command
*)cmd
;
1609 const char* keyEqualsValue
= (char*)envcmd
+ envcmd
->name
.offset
;
1610 char mainExecutableDir
[strlen(sExecPath
)+2];
1611 strcpy(mainExecutableDir
, sExecPath
);
1612 char* lastSlash
= strrchr(mainExecutableDir
, '/');
1613 if ( lastSlash
!= NULL
)
1614 lastSlash
[1] = '\0';
1615 // only process variables that start with DYLD_ and end in _PATH
1616 if ( (strncmp(keyEqualsValue
, "DYLD_", 5) == 0) ) {
1617 const char* equals
= strchr(keyEqualsValue
, '=');
1618 if ( equals
!= NULL
) {
1619 if ( strncmp(&equals
[-5], "_PATH", 5) == 0 ) {
1620 const char* value
= &equals
[1];
1621 const size_t keyLen
= equals
-keyEqualsValue
;
1623 strncpy(key
, keyEqualsValue
, keyLen
);
1625 //dyld::log("processing: %s\n", keyEqualsValue);
1626 //dyld::log("mainExecutableDir: %s\n", mainExecutableDir);
1627 processDyldEnvironmentVariable(key
, value
, mainExecutableDir
);
1634 cmd
= (const struct load_command
*)(((char*)cmd
)+cmd
->cmdsize
);
1637 #endif // SUPPORT_LC_DYLD_ENVIRONMENT
1640 static bool hasCodeSignatureLoadCommand(const macho_header
* mh
)
1642 const uint32_t cmd_count
= mh
->ncmds
;
1643 const struct load_command
* const cmds
= (struct load_command
*)(((char*)mh
)+sizeof(macho_header
));
1644 const struct load_command
* cmd
= cmds
;
1645 for (uint32_t i
= 0; i
< cmd_count
; ++i
) {
1646 if (cmd
->cmd
== LC_CODE_SIGNATURE
)
1648 cmd
= (const struct load_command
*)(((char*)cmd
)+cmd
->cmdsize
);
1654 #if SUPPORT_VERSIONED_PATHS
1655 static void checkVersionedPaths()
1657 // search DYLD_VERSIONED_LIBRARY_PATH directories for dylibs and check if they are newer
1658 if ( sEnv
.DYLD_VERSIONED_LIBRARY_PATH
!= NULL
) {
1659 for(const char* const* lp
= sEnv
.DYLD_VERSIONED_LIBRARY_PATH
; *lp
!= NULL
; ++lp
) {
1660 checkDylibOverridesInDir(*lp
);
1664 // search DYLD_VERSIONED_FRAMEWORK_PATH directories for dylibs and check if they are newer
1665 if ( sEnv
.DYLD_VERSIONED_FRAMEWORK_PATH
!= NULL
) {
1666 for(const char* const* fp
= sEnv
.DYLD_VERSIONED_FRAMEWORK_PATH
; *fp
!= NULL
; ++fp
) {
1667 checkFrameworkOverridesInDir(*fp
);
1675 // For security, setuid programs ignore DYLD_* environment variables.
1676 // Additionally, the DYLD_* enviroment variables are removed
1677 // from the environment, so that any child processes don't see them.
1679 static void pruneEnvironmentVariables(const char* envp
[], const char*** applep
)
1681 // delete all DYLD_* and LD_LIBRARY_PATH environment variables
1682 int removedCount
= 0;
1683 const char** d
= envp
;
1684 for(const char** s
= envp
; *s
!= NULL
; s
++) {
1685 if ( (strncmp(*s
, "DYLD_", 5) != 0) && (strncmp(*s
, "LD_LIBRARY_PATH=", 16) != 0) ) {
1693 // <rdar://11894054> Disable warnings about DYLD_ env vars being ignored. The warnings are causing too much confusion.
1695 if ( removedCount
!= 0 ) {
1696 dyld::log("dyld: DYLD_ environment variables being ignored because ");
1697 switch (sRestrictedReason
) {
1700 case restrictedBySetGUid
:
1701 dyld::log("main executable (%s) is setuid or setgid\n", sExecPath
);
1703 case restrictedBySegment
:
1704 dyld::log("main executable (%s) has __RESTRICT/__restrict section\n", sExecPath
);
1706 case restrictedByEntitlements
:
1707 dyld::log("main executable (%s) is code signed with entitlements\n", sExecPath
);
1712 // slide apple parameters
1713 if ( removedCount
> 0 ) {
1716 *d
= d
[removedCount
];
1717 } while ( *d
++ != NULL
);
1718 for(int i
=0; i
< removedCount
; ++i
)
1722 // disable framework and library fallback paths for setuid binaries rdar://problem/4589305
1723 sEnv
.DYLD_FALLBACK_FRAMEWORK_PATH
= NULL
;
1724 sEnv
.DYLD_FALLBACK_LIBRARY_PATH
= NULL
;
1726 if ( removedCount
> 0 )
1727 strlcat(sLoadingCrashMessage
, ", ignoring DYLD_* env vars", sizeof(sLoadingCrashMessage
));
1731 static void checkEnvironmentVariables(const char* envp
[], bool ignoreEnviron
)
1733 const char* home
= NULL
;
1735 for(p
= envp
; *p
!= NULL
; p
++) {
1736 const char* keyEqualsValue
= *p
;
1737 if ( strncmp(keyEqualsValue
, "DYLD_", 5) == 0 ) {
1738 const char* equals
= strchr(keyEqualsValue
, '=');
1739 if ( (equals
!= NULL
) && !ignoreEnviron
) {
1740 strlcat(sLoadingCrashMessage
, "\n", sizeof(sLoadingCrashMessage
));
1741 strlcat(sLoadingCrashMessage
, keyEqualsValue
, sizeof(sLoadingCrashMessage
));
1742 const char* value
= &equals
[1];
1743 const size_t keyLen
= equals
-keyEqualsValue
;
1745 strncpy(key
, keyEqualsValue
, keyLen
);
1747 processDyldEnvironmentVariable(key
, value
, NULL
);
1750 else if ( strncmp(keyEqualsValue
, "HOME=", 5) == 0 ) {
1751 home
= &keyEqualsValue
[5];
1753 else if ( strncmp(keyEqualsValue
, "LD_LIBRARY_PATH=", 16) == 0 ) {
1754 const char* path
= &keyEqualsValue
[16];
1755 sEnv
.LD_LIBRARY_PATH
= parseColonList(path
, NULL
);
1759 #if SUPPORT_LC_DYLD_ENVIRONMENT
1760 checkLoadCommandEnvironmentVariables();
1761 #endif // SUPPORT_LC_DYLD_ENVIRONMENT
1763 // default value for DYLD_FALLBACK_FRAMEWORK_PATH, if not set in environment
1764 if ( sEnv
.DYLD_FALLBACK_FRAMEWORK_PATH
== NULL
) {
1765 const char** paths
= sFrameworkFallbackPaths
;
1767 removePathWithPrefix(paths
, "$HOME");
1769 paths_expand_roots(paths
, "$HOME", home
);
1770 sEnv
.DYLD_FALLBACK_FRAMEWORK_PATH
= paths
;
1773 // default value for DYLD_FALLBACK_LIBRARY_PATH, if not set in environment
1774 if ( sEnv
.DYLD_FALLBACK_LIBRARY_PATH
== NULL
) {
1775 const char** paths
= sLibraryFallbackPaths
;
1777 removePathWithPrefix(paths
, "$HOME");
1779 paths_expand_roots(paths
, "$HOME", home
);
1780 sEnv
.DYLD_FALLBACK_LIBRARY_PATH
= paths
;
1783 // <rdar://problem/11281064> DYLD_IMAGE_SUFFIX and DYLD_ROOT_PATH cannot be used together
1784 if ( (gLinkContext
.imageSuffix
!= NULL
) && (gLinkContext
.rootPaths
!= NULL
) ) {
1785 dyld::warn("Ignoring DYLD_IMAGE_SUFFIX because DYLD_ROOT_PATH is used.\n");
1786 gLinkContext
.imageSuffix
= NULL
;
1789 #if SUPPORT_VERSIONED_PATHS
1790 checkVersionedPaths();
1795 static void getHostInfo()
1797 #if CPU_SUBTYPES_SUPPORTED
1799 sHostCPU
= CPU_TYPE_ARM
;
1800 sHostCPUsubtype
= CPU_SUBTYPE_ARM_V7K
;
1801 #elif __ARM_ARCH_7A__
1802 sHostCPU
= CPU_TYPE_ARM
;
1803 sHostCPUsubtype
= CPU_SUBTYPE_ARM_V7
;
1804 #elif __ARM_ARCH_6K__
1805 sHostCPU
= CPU_TYPE_ARM
;
1806 sHostCPUsubtype
= CPU_SUBTYPE_ARM_V6
;
1807 #elif __ARM_ARCH_7F__
1808 sHostCPU
= CPU_TYPE_ARM
;
1809 sHostCPUsubtype
= CPU_SUBTYPE_ARM_V7F
;
1810 #elif __ARM_ARCH_7S__
1811 sHostCPU
= CPU_TYPE_ARM
;
1812 sHostCPUsubtype
= CPU_SUBTYPE_ARM_V7S
;
1814 struct host_basic_info info
;
1815 mach_msg_type_number_t count
= HOST_BASIC_INFO_COUNT
;
1816 mach_port_t hostPort
= mach_host_self();
1817 kern_return_t result
= host_info(hostPort
, HOST_BASIC_INFO
, (host_info_t
)&info
, &count
);
1818 if ( result
!= KERN_SUCCESS
)
1819 throw "host_info() failed";
1820 sHostCPU
= info
.cpu_type
;
1821 sHostCPUsubtype
= info
.cpu_subtype
;
1822 mach_port_deallocate(mach_task_self(), hostPort
);
1827 static void checkSharedRegionDisable()
1829 #if __MAC_OS_X_VERSION_MIN_REQUIRED
1830 // if main executable has segments that overlap the shared region,
1831 // then disable using the shared region
1832 if ( sMainExecutable
->overlapsWithAddressRange((void*)(uintptr_t)SHARED_REGION_BASE
, (void*)(uintptr_t)(SHARED_REGION_BASE
+ SHARED_REGION_SIZE
)) ) {
1833 gLinkContext
.sharedRegionMode
= ImageLoader::kDontUseSharedRegion
;
1834 if ( gLinkContext
.verboseMapping
)
1835 dyld::warn("disabling shared region because main executable overlaps\n");
1838 if ( sProcessIsRestricted
) {
1839 // <rdar://problem/15280847> use private or no shared region for suid processes
1840 gLinkContext
.sharedRegionMode
= ImageLoader::kUsePrivateSharedRegion
;
1844 // iPhoneOS cannot run without shared region
1847 bool validImage(const ImageLoader
* possibleImage
)
1849 const size_t imageCount
= sAllImages
.size();
1850 for(size_t i
=0; i
< imageCount
; ++i
) {
1851 if ( possibleImage
== sAllImages
[i
] ) {
1858 uint32_t getImageCount()
1860 return (uint32_t)sAllImages
.size();
1863 ImageLoader
* getIndexedImage(unsigned int index
)
1865 if ( index
< sAllImages
.size() )
1866 return sAllImages
[index
];
1870 ImageLoader
* findImageByMachHeader(const struct mach_header
* target
)
1872 return findMappedRange((uintptr_t)target
);
1876 ImageLoader
* findImageContainingAddress(const void* addr
)
1878 return findMappedRange((uintptr_t)addr
);
1882 ImageLoader
* findImageContainingSymbol(const void* symbol
)
1884 for (std::vector
<ImageLoader
*>::iterator it
=sAllImages
.begin(); it
!= sAllImages
.end(); it
++) {
1885 ImageLoader
* anImage
= *it
;
1886 if ( anImage
->containsSymbol(symbol
) )
1894 void forEachImageDo( void (*callback
)(ImageLoader
*, void* userData
), void* userData
)
1896 const size_t imageCount
= sAllImages
.size();
1897 for(size_t i
=0; i
< imageCount
; ++i
) {
1898 ImageLoader
* anImage
= sAllImages
[i
];
1899 (*callback
)(anImage
, userData
);
1903 ImageLoader
* findLoadedImage(const struct stat
& stat_buf
)
1905 const size_t imageCount
= sAllImages
.size();
1906 for(size_t i
=0; i
< imageCount
; ++i
){
1907 ImageLoader
* anImage
= sAllImages
[i
];
1908 if ( anImage
->statMatch(stat_buf
) )
1914 // based on ANSI-C strstr()
1915 static const char* strrstr(const char* str
, const char* sub
)
1917 const size_t sublen
= strlen(sub
);
1918 for(const char* p
= &str
[strlen(str
)]; p
!= str
; --p
) {
1919 if ( strncmp(p
, sub
, sublen
) == 0 )
1927 // Find framework path
1929 // /path/foo.framework/foo => foo.framework/foo
1930 // /path/foo.framework/Versions/A/foo => foo.framework/Versions/A/foo
1931 // /path/foo.framework/Frameworks/bar.framework/bar => bar.framework/bar
1932 // /path/foo.framework/Libraries/bar.dylb => NULL
1933 // /path/foo.framework/bar => NULL
1935 // Returns NULL if not a framework path
1937 static const char* getFrameworkPartialPath(const char* path
)
1939 const char* dirDot
= strrstr(path
, ".framework/");
1940 if ( dirDot
!= NULL
) {
1941 const char* dirStart
= dirDot
;
1942 for ( ; dirStart
>= path
; --dirStart
) {
1943 if ( (*dirStart
== '/') || (dirStart
== path
) ) {
1944 const char* frameworkStart
= &dirStart
[1];
1945 if ( dirStart
== path
)
1947 size_t len
= dirDot
- frameworkStart
;
1948 char framework
[len
+1];
1949 strncpy(framework
, frameworkStart
, len
);
1950 framework
[len
] = '\0';
1951 const char* leaf
= strrchr(path
, '/');
1952 if ( leaf
!= NULL
) {
1953 if ( strcmp(framework
, &leaf
[1]) == 0 ) {
1954 return frameworkStart
;
1956 if ( gLinkContext
.imageSuffix
!= NULL
) {
1957 // some debug frameworks have install names that end in _debug
1958 if ( strncmp(framework
, &leaf
[1], len
) == 0 ) {
1959 if ( strcmp( gLinkContext
.imageSuffix
, &leaf
[len
+1]) == 0 )
1960 return frameworkStart
;
1971 static const char* getLibraryLeafName(const char* path
)
1973 const char* start
= strrchr(path
, '/');
1974 if ( start
!= NULL
)
1981 // only for architectures that use cpu-sub-types
1982 #if CPU_SUBTYPES_SUPPORTED
1984 const cpu_subtype_t CPU_SUBTYPE_END_OF_LIST
= -1;
1988 // A fat file may contain multiple sub-images for the same CPU type.
1989 // In that case, dyld picks which sub-image to use by scanning a table
1990 // of preferred cpu-sub-types for the running cpu.
1992 // There is one row in the table for each cpu-sub-type on which dyld might run.
1993 // The first entry in a row is that cpu-sub-type. It is followed by all
1994 // cpu-sub-types that can run on that cpu, if preferred order. Each row ends with
1995 // a "SUBTYPE_ALL" (to denote that images written to run on any cpu-sub-type are usable),
1996 // followed by one or more CPU_SUBTYPE_END_OF_LIST to pad out this row.
2002 // ARM sub-type lists
2004 const int kARM_RowCount
= 8;
2005 static const cpu_subtype_t kARM
[kARM_RowCount
][9] = {
2007 // armv7f can run: v7f, v7, v6, v5, and v4
2008 { 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
},
2010 // armv7k can run: v7k
2011 { CPU_SUBTYPE_ARM_V7K
, CPU_SUBTYPE_END_OF_LIST
},
2013 // armv7s can run: v7s, v7, v7f, v7k, v6, v5, and v4
2014 { 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
},
2016 // armv7 can run: v7, v6, v5, and v4
2017 { 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
},
2019 // armv6 can run: v6, v5, and v4
2020 { 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
},
2022 // xscale can run: xscale, v5, and v4
2023 { 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
},
2025 // armv5 can run: v5 and v4
2026 { 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
},
2028 // armv4 can run: v4
2029 { 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
},
2035 // x86_64 sub-type lists
2037 const int kX86_64_RowCount
= 2;
2038 static const cpu_subtype_t kX86_64
[kX86_64_RowCount
][5] = {
2040 // x86_64h can run: x86_64h, x86_64h(lib), x86_64(lib), and x86_64
2041 { CPU_SUBTYPE_X86_64_H
, CPU_SUBTYPE_LIB64
|CPU_SUBTYPE_X86_64_H
, CPU_SUBTYPE_LIB64
|CPU_SUBTYPE_X86_64_ALL
, CPU_SUBTYPE_X86_64_ALL
, CPU_SUBTYPE_END_OF_LIST
},
2043 // x86_64 can run: x86_64(lib) and x86_64
2044 { CPU_SUBTYPE_X86_64_ALL
, CPU_SUBTYPE_LIB64
|CPU_SUBTYPE_X86_64_ALL
, CPU_SUBTYPE_END_OF_LIST
},
2050 // scan the tables above to find the cpu-sub-type-list for this machine
2051 static const cpu_subtype_t
* findCPUSubtypeList(cpu_type_t cpu
, cpu_subtype_t subtype
)
2056 for (int i
=0; i
< kARM_RowCount
; ++i
) {
2057 if ( kARM
[i
][0] == subtype
)
2063 case CPU_TYPE_X86_64
:
2064 for (int i
=0; i
< kX86_64_RowCount
; ++i
) {
2065 if ( kX86_64
[i
][0] == subtype
)
2077 // scan fat table-of-contents for best most preferred subtype
2078 static bool fatFindBestFromOrderedList(cpu_type_t cpu
, const cpu_subtype_t list
[], const fat_header
* fh
, uint64_t* offset
, uint64_t* len
)
2080 const fat_arch
* const archs
= (fat_arch
*)(((char*)fh
)+sizeof(fat_header
));
2081 for (uint32_t subTypeIndex
=0; list
[subTypeIndex
] != CPU_SUBTYPE_END_OF_LIST
; ++subTypeIndex
) {
2082 for(uint32_t fatIndex
=0; fatIndex
< OSSwapBigToHostInt32(fh
->nfat_arch
); ++fatIndex
) {
2083 if ( ((cpu_type_t
)OSSwapBigToHostInt32(archs
[fatIndex
].cputype
) == cpu
)
2084 && (list
[subTypeIndex
] == (cpu_subtype_t
)OSSwapBigToHostInt32(archs
[fatIndex
].cpusubtype
)) ) {
2085 *offset
= OSSwapBigToHostInt32(archs
[fatIndex
].offset
);
2086 *len
= OSSwapBigToHostInt32(archs
[fatIndex
].size
);
2094 // scan fat table-of-contents for exact match of cpu and cpu-sub-type
2095 static bool fatFindExactMatch(cpu_type_t cpu
, cpu_subtype_t subtype
, const fat_header
* fh
, uint64_t* offset
, uint64_t* len
)
2097 const fat_arch
* archs
= (fat_arch
*)(((char*)fh
)+sizeof(fat_header
));
2098 for(uint32_t i
=0; i
< OSSwapBigToHostInt32(fh
->nfat_arch
); ++i
) {
2099 if ( ((cpu_type_t
)OSSwapBigToHostInt32(archs
[i
].cputype
) == cpu
)
2100 && ((cpu_subtype_t
)OSSwapBigToHostInt32(archs
[i
].cpusubtype
) == subtype
) ) {
2101 *offset
= OSSwapBigToHostInt32(archs
[i
].offset
);
2102 *len
= OSSwapBigToHostInt32(archs
[i
].size
);
2109 // scan fat table-of-contents for image with matching cpu-type and runs-on-all-sub-types
2110 static bool fatFindRunsOnAllCPUs(cpu_type_t cpu
, const fat_header
* fh
, uint64_t* offset
, uint64_t* len
)
2112 const fat_arch
* archs
= (fat_arch
*)(((char*)fh
)+sizeof(fat_header
));
2113 for(uint32_t i
=0; i
< OSSwapBigToHostInt32(fh
->nfat_arch
); ++i
) {
2114 if ( (cpu_type_t
)OSSwapBigToHostInt32(archs
[i
].cputype
) == cpu
) {
2118 if ( (cpu_subtype_t
)OSSwapBigToHostInt32(archs
[i
].cpusubtype
) == CPU_SUBTYPE_ARM_ALL
) {
2119 *offset
= OSSwapBigToHostInt32(archs
[i
].offset
);
2120 *len
= OSSwapBigToHostInt32(archs
[i
].size
);
2126 case CPU_TYPE_X86_64
:
2127 if ( (cpu_subtype_t
)OSSwapBigToHostInt32(archs
[i
].cpusubtype
) == CPU_SUBTYPE_X86_64_ALL
) {
2128 *offset
= OSSwapBigToHostInt32(archs
[i
].offset
);
2129 *len
= OSSwapBigToHostInt32(archs
[i
].size
);
2140 #endif // CPU_SUBTYPES_SUPPORTED
2143 // A fat file may contain multiple sub-images for the same cpu-type,
2144 // each optimized for a different cpu-sub-type (e.g G3 or G5).
2145 // This routine picks the optimal sub-image.
2147 static bool fatFindBest(const fat_header
* fh
, uint64_t* offset
, uint64_t* len
)
2149 #if CPU_SUBTYPES_SUPPORTED
2150 // assume all dylibs loaded must have same cpu type as main executable
2151 const cpu_type_t cpu
= sMainExecutableMachHeader
->cputype
;
2153 // We only know the subtype to use if the main executable cpu type matches the host
2154 if ( (cpu
& CPU_TYPE_MASK
) == sHostCPU
) {
2155 // get preference ordered list of subtypes
2156 const cpu_subtype_t
* subTypePreferenceList
= findCPUSubtypeList(cpu
, sHostCPUsubtype
);
2158 // use ordered list to find best sub-image in fat file
2159 if ( subTypePreferenceList
!= NULL
)
2160 return fatFindBestFromOrderedList(cpu
, subTypePreferenceList
, fh
, offset
, len
);
2162 // if running cpu is not in list, try for an exact match
2163 if ( fatFindExactMatch(cpu
, sHostCPUsubtype
, fh
, offset
, len
) )
2167 // running on an uknown cpu, can only load generic code
2168 return fatFindRunsOnAllCPUs(cpu
, fh
, offset
, len
);
2170 // just find first slice with matching architecture
2171 const fat_arch
* archs
= (fat_arch
*)(((char*)fh
)+sizeof(fat_header
));
2172 for(uint32_t i
=0; i
< OSSwapBigToHostInt32(fh
->nfat_arch
); ++i
) {
2173 if ( (cpu_type_t
)OSSwapBigToHostInt32(archs
[i
].cputype
) == sMainExecutableMachHeader
->cputype
) {
2174 *offset
= OSSwapBigToHostInt32(archs
[i
].offset
);
2175 *len
= OSSwapBigToHostInt32(archs
[i
].size
);
2186 // This is used to validate if a non-fat (aka thin or raw) mach-o file can be used
2187 // on the current processor. //
2188 bool isCompatibleMachO(const uint8_t* firstPage
, const char* path
)
2190 #if CPU_SUBTYPES_SUPPORTED
2191 // It is deemed compatible if any of the following are true:
2192 // 1) mach_header subtype is in list of compatible subtypes for running processor
2193 // 2) mach_header subtype is same as running processor subtype
2194 // 3) mach_header subtype runs on all processor variants
2195 const mach_header
* mh
= (mach_header
*)firstPage
;
2196 if ( mh
->magic
== sMainExecutableMachHeader
->magic
) {
2197 if ( mh
->cputype
== sMainExecutableMachHeader
->cputype
) {
2198 if ( (mh
->cputype
& CPU_TYPE_MASK
) == sHostCPU
) {
2199 // get preference ordered list of subtypes that this machine can use
2200 const cpu_subtype_t
* subTypePreferenceList
= findCPUSubtypeList(mh
->cputype
, sHostCPUsubtype
);
2201 if ( subTypePreferenceList
!= NULL
) {
2202 // if image's subtype is in the list, it is compatible
2203 for (const cpu_subtype_t
* p
= subTypePreferenceList
; *p
!= CPU_SUBTYPE_END_OF_LIST
; ++p
) {
2204 if ( *p
== mh
->cpusubtype
)
2207 // have list and not in list, so not compatible
2208 throwf("incompatible cpu-subtype: 0x%08X in %s", mh
->cpusubtype
, path
);
2210 // unknown cpu sub-type, but if exact match for current subtype then ok to use
2211 if ( mh
->cpusubtype
== sHostCPUsubtype
)
2215 // cpu type has no ordered list of subtypes
2216 switch (mh
->cputype
) {
2218 case CPU_TYPE_X86_64
:
2219 // subtypes are not used or these architectures
2225 // For architectures that don't support cpu-sub-types
2226 // this just check the cpu type.
2227 const mach_header
* mh
= (mach_header
*)firstPage
;
2228 if ( mh
->magic
== sMainExecutableMachHeader
->magic
) {
2229 if ( mh
->cputype
== sMainExecutableMachHeader
->cputype
) {
2240 // The kernel maps in main executable before dyld gets control. We need to
2241 // make an ImageLoader* for the already mapped in main executable.
2242 static ImageLoader
* instantiateFromLoadedImage(const macho_header
* mh
, uintptr_t slide
, const char* path
)
2244 // try mach-o loader
2245 if ( isCompatibleMachO((const uint8_t*)mh
, path
) ) {
2246 ImageLoader
* image
= ImageLoaderMachO::instantiateMainExecutable(mh
, slide
, path
, gLinkContext
);
2251 throw "main executable not a known format";
2255 #if DYLD_SHARED_CACHE_SUPPORT
2256 static bool findInSharedCacheImage(const char* path
, bool searchByPath
, const struct stat
* stat_buf
, const macho_header
** mh
, const char** pathInCache
, long* slide
)
2258 if ( sSharedCache
!= NULL
) {
2259 #if __MAC_OS_X_VERSION_MIN_REQUIRED
2260 // Mac OS X always requires inode/mtime to valid cache
2261 // if stat() not done yet, do it now
2263 if ( stat_buf
== NULL
) {
2264 if ( my_stat(path
, &statb
) == -1 )
2269 #if __IPHONE_OS_VERSION_MIN_REQUIRED
2271 for (const char* s
=path
; *s
!= '\0'; ++s
)
2272 hash
+= hash
*4 + *s
;
2275 // walk shared cache to see if there is a cached image that matches the inode/mtime/path desired
2276 const dyld_cache_image_info
* const start
= (dyld_cache_image_info
*)((uint8_t*)sSharedCache
+ sSharedCache
->imagesOffset
);
2277 const dyld_cache_image_info
* const end
= &start
[sSharedCache
->imagesCount
];
2278 #if __IPHONE_OS_VERSION_MIN_REQUIRED
2279 const bool cacheHasHashInfo
= (start
->modTime
== 0);
2281 for( const dyld_cache_image_info
* p
= start
; p
!= end
; ++p
) {
2282 #if __IPHONE_OS_VERSION_MIN_REQUIRED
2284 const char* aPath
= (char*)sSharedCache
+ p
->pathFileOffset
;
2285 if ( cacheHasHashInfo
&& (p
->inode
!= hash
) )
2287 if ( strcmp(path
, aPath
) == 0 ) {
2288 // found image in cache
2289 *mh
= (macho_header
*)(p
->address
+sSharedCacheSlide
);
2290 *pathInCache
= aPath
;
2291 *slide
= sSharedCacheSlide
;
2294 #elif __MAC_OS_X_VERSION_MIN_REQUIRED
2295 // check mtime and inode first because it is fast
2296 bool inodeMatch
= ( ((time_t)p
->modTime
== stat_buf
->st_mtime
) && ((ino_t
)p
->inode
== stat_buf
->st_ino
) );
2297 if ( searchByPath
|| sSharedCacheIgnoreInodeAndTimeStamp
|| inodeMatch
) {
2298 // mod-time and inode match an image in the shared cache, now check path
2299 const char* aPath
= (char*)sSharedCache
+ p
->pathFileOffset
;
2300 bool cacheHit
= (strcmp(path
, aPath
) == 0);
2301 if ( inodeMatch
&& !cacheHit
) {
2302 // path does not match install name of dylib in cache, but inode and mtime does match
2303 // perhaps path is a symlink to the cached dylib
2304 struct stat pathInCacheStatBuf
;
2305 if ( my_stat(aPath
, &pathInCacheStatBuf
) != -1 )
2306 cacheHit
= ( (pathInCacheStatBuf
.st_dev
== stat_buf
->st_dev
) && (pathInCacheStatBuf
.st_ino
== stat_buf
->st_ino
) );
2309 // found image in cache, return info
2310 *mh
= (macho_header
*)(p
->address
+sSharedCacheSlide
);
2311 //dyld::log("findInSharedCacheImage(), mh=%p, p->address=0x%0llX, slid=0x%0lX, path=%p\n",
2312 // *mh, p->address, sSharedCacheSlide, aPath);
2313 *pathInCache
= aPath
;
2314 *slide
= sSharedCacheSlide
;
2324 bool inSharedCache(const char* path
)
2326 const macho_header
* mhInCache
;
2327 const char* pathInCache
;
2329 return findInSharedCacheImage(path
, true, NULL
, &mhInCache
, &pathInCache
, &slide
);
2334 static ImageLoader
* checkandAddImage(ImageLoader
* image
, const LoadContext
& context
)
2336 // now sanity check that this loaded image does not have the same install path as any existing image
2337 const char* loadedImageInstallPath
= image
->getInstallPath();
2338 if ( image
->isDylib() && (loadedImageInstallPath
!= NULL
) && (loadedImageInstallPath
[0] == '/') ) {
2339 for (std::vector
<ImageLoader
*>::iterator it
=sAllImages
.begin(); it
!= sAllImages
.end(); it
++) {
2340 ImageLoader
* anImage
= *it
;
2341 const char* installPath
= anImage
->getInstallPath();
2342 if ( installPath
!= NULL
) {
2343 if ( strcmp(loadedImageInstallPath
, installPath
) == 0 ) {
2344 //dyld::log("duplicate(%s) => %p\n", installPath, anImage);
2346 ImageLoader::deleteImage(image
);
2353 // some API's restrict what they can load
2354 if ( context
.mustBeBundle
&& !image
->isBundle() )
2355 throw "not a bundle";
2356 if ( context
.mustBeDylib
&& !image
->isDylib() )
2357 throw "not a dylib";
2359 // regular main executables cannot be loaded
2360 if ( image
->isExecutable() ) {
2361 if ( !context
.canBePIE
|| !image
->isPositionIndependentExecutable() )
2362 throw "can't load a main executable";
2365 // don't add bundles to global list, they can be loaded but not linked. When linked it will be added to list
2366 if ( ! image
->isBundle() )
2372 #if TARGET_IPHONE_SIMULATOR
2373 static bool isSimulatorBinary(const uint8_t* firstPage
, const char* path
)
2375 const macho_header
* mh
= (macho_header
*)firstPage
;
2376 const uint32_t cmd_count
= mh
->ncmds
;
2377 const struct load_command
* const cmds
= (struct load_command
*)(((char*)mh
)+sizeof(macho_header
));
2378 const struct load_command
* const cmdsReadEnd
= (struct load_command
*)(((char*)mh
)+4096);
2379 const struct load_command
* cmd
= cmds
;
2380 for (uint32_t i
= 0; i
< cmd_count
; ++i
) {
2382 case LC_VERSION_MIN_IPHONEOS
:
2384 case LC_VERSION_MIN_MACOSX
:
2385 // grandfather in a few libSystem dylibs
2386 if ( strncmp(path
, "/usr/lib/system/libsystem_", 26) == 0 )
2390 cmd
= (const struct load_command
*)(((char*)cmd
)+cmd
->cmdsize
);
2391 if ( cmd
> cmdsReadEnd
)
2398 // map in file and instantiate an ImageLoader
2399 static ImageLoader
* loadPhase6(int fd
, const struct stat
& stat_buf
, const char* path
, const LoadContext
& context
)
2401 //dyld::log("%s(%s)\n", __func__ , path);
2402 uint64_t fileOffset
= 0;
2403 uint64_t fileLength
= stat_buf
.st_size
;
2405 // validate it is a file (not directory)
2406 if ( (stat_buf
.st_mode
& S_IFMT
) != S_IFREG
)
2409 uint8_t firstPage
[4096];
2410 bool shortPage
= false;
2412 // min mach-o file is 4K
2413 if ( fileLength
< 4096 ) {
2414 if ( pread(fd
, firstPage
, fileLength
, 0) != (ssize_t
)fileLength
)
2415 throwf("pread of short file failed: %d", errno
);
2419 if ( pread(fd
, firstPage
, 4096,0) != 4096 )
2420 throwf("pread of first 4K failed: %d", errno
);
2423 // if fat wrapper, find usable sub-file
2424 const fat_header
* fileStartAsFat
= (fat_header
*)firstPage
;
2425 if ( fileStartAsFat
->magic
== OSSwapBigToHostInt32(FAT_MAGIC
) ) {
2426 if ( fatFindBest(fileStartAsFat
, &fileOffset
, &fileLength
) ) {
2427 if ( (fileOffset
+fileLength
) > (uint64_t)(stat_buf
.st_size
) )
2428 throwf("truncated fat file. file length=%llu, but needed slice goes to %llu", stat_buf
.st_size
, fileOffset
+fileLength
);
2429 if (pread(fd
, firstPage
, 4096, fileOffset
) != 4096)
2430 throwf("pread of fat file failed: %d", errno
);
2433 throw "no matching architecture in universal wrapper";
2437 // try mach-o loader
2439 throw "file too short";
2440 if ( isCompatibleMachO(firstPage
, path
) ) {
2442 // only MH_BUNDLE, MH_DYLIB, and some MH_EXECUTE can be dynamically loaded
2443 switch ( ((mach_header
*)firstPage
)->filetype
) {
2449 throw "mach-o, but wrong filetype";
2452 #if TARGET_IPHONE_SIMULATOR
2453 // <rdar://problem/14168872> dyld_sim should restrict loading osx binaries
2454 if ( !isSimulatorBinary(firstPage
, path
) ) {
2455 throw "mach-o, but not built for iOS simulator";
2459 // instantiate an image
2460 ImageLoader
* image
= ImageLoaderMachO::instantiateFromFile(path
, fd
, firstPage
, fileOffset
, fileLength
, stat_buf
, gLinkContext
);
2463 return checkandAddImage(image
, context
);
2466 // try other file formats here...
2469 // throw error about what was found
2470 switch (*(uint32_t*)firstPage
) {
2475 throw "mach-o, but wrong architecture";
2477 throwf("unknown file type, first eight bytes: 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X",
2478 firstPage
[0], firstPage
[1], firstPage
[2], firstPage
[3], firstPage
[4], firstPage
[5], firstPage
[6],firstPage
[7]);
2483 static ImageLoader
* loadPhase5open(const char* path
, const LoadContext
& context
, const struct stat
& stat_buf
, std::vector
<const char*>* exceptions
)
2485 //dyld::log("%s(%s, %p)\n", __func__ , path, exceptions);
2487 // open file (automagically closed when this function exits)
2488 FileOpener
file(path
);
2490 // just return NULL if file not found, but record any other errors
2491 if ( file
.getFileDescriptor() == -1 ) {
2493 if ( err
!= ENOENT
) {
2494 const char* newMsg
= dyld::mkstringf("%s: open() failed with errno=%d", path
, err
);
2495 exceptions
->push_back(newMsg
);
2501 return loadPhase6(file
.getFileDescriptor(), stat_buf
, path
, context
);
2503 catch (const char* msg
) {
2504 const char* newMsg
= dyld::mkstringf("%s: %s", path
, msg
);
2505 exceptions
->push_back(newMsg
);
2512 #if __MAC_OS_X_VERSION_MIN_REQUIRED
2513 static ImageLoader
* loadPhase5load(const char* path
, const char* orgPath
, const LoadContext
& context
, std::vector
<const char*>* exceptions
)
2515 //dyld::log("%s(%s, %p)\n", __func__ , path, exceptions);
2516 ImageLoader
* image
= NULL
;
2518 // just return NULL if file not found, but record any other errors
2519 struct stat stat_buf
;
2520 if ( my_stat(path
, &stat_buf
) == -1 ) {
2522 if ( err
!= ENOENT
) {
2523 exceptions
->push_back(dyld::mkstringf("%s: stat() failed with errno=%d", path
, err
));
2528 // in case image was renamed or found via symlinks, check for inode match
2529 image
= findLoadedImage(stat_buf
);
2530 if ( image
!= NULL
)
2533 // do nothing if not already loaded and if RTLD_NOLOAD or NSADDIMAGE_OPTION_RETURN_ONLY_IF_LOADED
2534 if ( context
.dontLoad
)
2537 #if DYLD_SHARED_CACHE_SUPPORT
2538 // see if this image is in shared cache
2539 const macho_header
* mhInCache
;
2540 const char* pathInCache
;
2542 if ( findInSharedCacheImage(path
, false, &stat_buf
, &mhInCache
, &pathInCache
, &slideInCache
) ) {
2543 image
= ImageLoaderMachO::instantiateFromCache(mhInCache
, pathInCache
, slideInCache
, stat_buf
, gLinkContext
);
2544 return checkandAddImage(image
, context
);
2547 // file exists and is not in dyld shared cache, so open it
2548 return loadPhase5open(path
, context
, stat_buf
, exceptions
);
2550 #endif // __MAC_OS_X_VERSION_MIN_REQUIRED
2554 #if __IPHONE_OS_VERSION_MIN_REQUIRED
2555 static ImageLoader
* loadPhase5stat(const char* path
, const LoadContext
& context
, struct stat
* stat_buf
,
2556 int* statErrNo
, bool* imageFound
, std::vector
<const char*>* exceptions
)
2558 ImageLoader
* image
= NULL
;
2559 *imageFound
= false;
2561 if ( my_stat(path
, stat_buf
) == 0 ) {
2562 // in case image was renamed or found via symlinks, check for inode match
2563 image
= findLoadedImage(*stat_buf
);
2564 if ( image
!= NULL
) {
2568 // do nothing if not already loaded and if RTLD_NOLOAD
2569 if ( context
.dontLoad
) {
2573 image
= loadPhase5open(path
, context
, *stat_buf
, exceptions
);
2574 if ( image
!= NULL
) {
2586 static ImageLoader
* loadPhase5load(const char* path
, const char* orgPath
, const LoadContext
& context
, std::vector
<const char*>* exceptions
)
2588 //dyld::log("%s(%s, %p)\n", __func__ , path, exceptions);
2589 struct stat stat_buf
;
2593 #if DYLD_SHARED_CACHE_SUPPORT
2594 if ( sDylibsOverrideCache
) {
2595 // flag is set that allows installed framework roots to override dyld shared cache
2596 image
= loadPhase5stat(path
, context
, &stat_buf
, &statErrNo
, &imageFound
, exceptions
);
2600 // see if this image is in shared cache
2601 const macho_header
* mhInCache
;
2602 const char* pathInCache
;
2604 if ( findInSharedCacheImage(path
, true, NULL
, &mhInCache
, &pathInCache
, &slideInCache
) ) {
2605 // see if this image in the cache was already loaded via a different path
2606 for (std::vector
<ImageLoader
*>::iterator it
=sAllImages
.begin(); it
!= sAllImages
.end(); ++it
) {
2607 ImageLoader
* anImage
= *it
;
2608 if ( (const macho_header
*)anImage
->machHeader() == mhInCache
)
2611 // do nothing if not already loaded and if RTLD_NOLOAD
2612 if ( context
.dontLoad
)
2614 // nope, so instantiate a new image from dyld shared cache
2615 // <rdar://problem/7014995> zero out stat buffer so mtime, etc are zero for items from the shared cache
2616 bzero(&stat_buf
, sizeof(stat_buf
));
2617 image
= ImageLoaderMachO::instantiateFromCache(mhInCache
, pathInCache
, slideInCache
, stat_buf
, gLinkContext
);
2618 return checkandAddImage(image
, context
);
2621 if ( !sDylibsOverrideCache
) {
2622 // flag is not set, and not in cache to try opening it
2623 image
= loadPhase5stat(path
, context
, &stat_buf
, &statErrNo
, &imageFound
, exceptions
);
2628 image
= loadPhase5stat(path
, context
, &stat_buf
, &statErrNo
, &imageFound
, exceptions
);
2632 // just return NULL if file not found, but record any other errors
2633 if ( (statErrNo
!= ENOENT
) && (statErrNo
!= 0) ) {
2634 exceptions
->push_back(dyld::mkstringf("%s: stat() failed with errno=%d", path
, statErrNo
));
2638 #endif // __IPHONE_OS_VERSION_MIN_REQUIRED
2641 // look for path match with existing loaded images
2642 static ImageLoader
* loadPhase5check(const char* path
, const char* orgPath
, const LoadContext
& context
)
2644 //dyld::log("%s(%s, %s)\n", __func__ , path, orgPath);
2645 // search path against load-path and install-path of all already loaded images
2646 uint32_t hash
= ImageLoader::hash(path
);
2647 //dyld::log("check() hash=%d, path=%s\n", hash, path);
2648 for (std::vector
<ImageLoader
*>::iterator it
=sAllImages
.begin(); it
!= sAllImages
.end(); it
++) {
2649 ImageLoader
* anImage
= *it
;
2650 // check hash first to cut down on strcmp calls
2651 //dyld::log(" check() hash=%d, path=%s\n", anImage->getPathHash(), anImage->getPath());
2652 if ( anImage
->getPathHash() == hash
) {
2653 if ( strcmp(path
, anImage
->getPath()) == 0 ) {
2654 // if we are looking for a dylib don't return something else
2655 if ( !context
.mustBeDylib
|| anImage
->isDylib() )
2659 if ( context
.matchByInstallName
|| anImage
->matchInstallPath() ) {
2660 const char* installPath
= anImage
->getInstallPath();
2661 if ( installPath
!= NULL
) {
2662 if ( strcmp(path
, installPath
) == 0 ) {
2663 // if we are looking for a dylib don't return something else
2664 if ( !context
.mustBeDylib
|| anImage
->isDylib() )
2669 // an install name starting with @rpath should match by install name, not just real path
2670 if ( (orgPath
[0] == '@') && (strncmp(orgPath
, "@rpath/", 7) == 0) ) {
2671 const char* installPath
= anImage
->getInstallPath();
2672 if ( installPath
!= NULL
) {
2673 if ( !context
.mustBeDylib
|| anImage
->isDylib() ) {
2674 if ( strcmp(orgPath
, installPath
) == 0 )
2681 //dyld::log("%s(%s) => NULL\n", __func__, path);
2686 // open or check existing
2687 static ImageLoader
* loadPhase5(const char* path
, const char* orgPath
, const LoadContext
& context
, std::vector
<const char*>* exceptions
)
2689 //dyld::log("%s(%s, %p)\n", __func__ , path, exceptions);
2691 // check for specific dylib overrides
2692 for (std::vector
<DylibOverride
>::iterator it
= sDylibOverrides
.begin(); it
!= sDylibOverrides
.end(); ++it
) {
2693 if ( strcmp(it
->installName
, path
) == 0 ) {
2694 path
= it
->override
;
2699 if ( exceptions
!= NULL
)
2700 return loadPhase5load(path
, orgPath
, context
, exceptions
);
2702 return loadPhase5check(path
, orgPath
, context
);
2705 // try with and without image suffix
2706 static ImageLoader
* loadPhase4(const char* path
, const char* orgPath
, const LoadContext
& context
, std::vector
<const char*>* exceptions
)
2708 //dyld::log("%s(%s, %p)\n", __func__ , path, exceptions);
2709 ImageLoader
* image
= NULL
;
2710 if ( gLinkContext
.imageSuffix
!= NULL
) {
2711 char pathWithSuffix
[strlen(path
)+strlen( gLinkContext
.imageSuffix
)+2];
2712 ImageLoader::addSuffix(path
, gLinkContext
.imageSuffix
, pathWithSuffix
);
2713 image
= loadPhase5(pathWithSuffix
, orgPath
, context
, exceptions
);
2715 if ( image
== NULL
)
2716 image
= loadPhase5(path
, orgPath
, context
, exceptions
);
2720 static ImageLoader
* loadPhase2(const char* path
, const char* orgPath
, const LoadContext
& context
,
2721 const char* const frameworkPaths
[], const char* const libraryPaths
[],
2722 std::vector
<const char*>* exceptions
); // forward reference
2725 // expand @ variables
2726 static ImageLoader
* loadPhase3(const char* path
, const char* orgPath
, const LoadContext
& context
, std::vector
<const char*>* exceptions
)
2728 //dyld::log("%s(%s, %p)\n", __func__ , path, exceptions);
2729 ImageLoader
* image
= NULL
;
2730 if ( strncmp(path
, "@executable_path/", 17) == 0 ) {
2731 // executable_path cannot be in used in any binary in a setuid process rdar://problem/4589305
2732 if ( sProcessIsRestricted
)
2733 throwf("unsafe use of @executable_path in %s with restricted binary", context
.origin
);
2734 // handle @executable_path path prefix
2735 const char* executablePath
= sExecPath
;
2736 char newPath
[strlen(executablePath
) + strlen(path
)];
2737 strcpy(newPath
, executablePath
);
2738 char* addPoint
= strrchr(newPath
,'/');
2739 if ( addPoint
!= NULL
)
2740 strcpy(&addPoint
[1], &path
[17]);
2742 strcpy(newPath
, &path
[17]);
2743 image
= loadPhase4(newPath
, orgPath
, context
, exceptions
);
2744 if ( image
!= NULL
)
2747 // perhaps main executable path is a sym link, find realpath and retry
2748 char resolvedPath
[PATH_MAX
];
2749 if ( realpath(sExecPath
, resolvedPath
) != NULL
) {
2750 char newRealPath
[strlen(resolvedPath
) + strlen(path
)];
2751 strcpy(newRealPath
, resolvedPath
);
2752 char* addPoint
= strrchr(newRealPath
,'/');
2753 if ( addPoint
!= NULL
)
2754 strcpy(&addPoint
[1], &path
[17]);
2756 strcpy(newRealPath
, &path
[17]);
2757 image
= loadPhase4(newRealPath
, orgPath
, context
, exceptions
);
2758 if ( image
!= NULL
)
2762 else if ( (strncmp(path
, "@loader_path/", 13) == 0) && (context
.origin
!= NULL
) ) {
2763 // @loader_path cannot be used from the main executable of a setuid process rdar://problem/4589305
2764 if ( sProcessIsRestricted
&& (strcmp(context
.origin
, sExecPath
) == 0) )
2765 throwf("unsafe use of @loader_path in %s with restricted binary", context
.origin
);
2766 // handle @loader_path path prefix
2767 char newPath
[strlen(context
.origin
) + strlen(path
)];
2768 strcpy(newPath
, context
.origin
);
2769 char* addPoint
= strrchr(newPath
,'/');
2770 if ( addPoint
!= NULL
)
2771 strcpy(&addPoint
[1], &path
[13]);
2773 strcpy(newPath
, &path
[13]);
2774 image
= loadPhase4(newPath
, orgPath
, context
, exceptions
);
2775 if ( image
!= NULL
)
2778 // perhaps loader path is a sym link, find realpath and retry
2779 char resolvedPath
[PATH_MAX
];
2780 if ( realpath(context
.origin
, resolvedPath
) != NULL
) {
2781 char newRealPath
[strlen(resolvedPath
) + strlen(path
)];
2782 strcpy(newRealPath
, resolvedPath
);
2783 char* addPoint
= strrchr(newRealPath
,'/');
2784 if ( addPoint
!= NULL
)
2785 strcpy(&addPoint
[1], &path
[13]);
2787 strcpy(newRealPath
, &path
[13]);
2788 image
= loadPhase4(newRealPath
, orgPath
, context
, exceptions
);
2789 if ( image
!= NULL
)
2793 else if ( context
.implicitRPath
|| (strncmp(path
, "@rpath/", 7) == 0) ) {
2794 const char* trailingPath
= (strncmp(path
, "@rpath/", 7) == 0) ? &path
[7] : path
;
2795 // substitute @rpath with all -rpath paths up the load chain
2796 for(const ImageLoader::RPathChain
* rp
=context
.rpath
; rp
!= NULL
; rp
=rp
->next
) {
2797 if (rp
->paths
!= NULL
) {
2798 for(std::vector
<const char*>::iterator it
=rp
->paths
->begin(); it
!= rp
->paths
->end(); ++it
) {
2799 const char* anRPath
= *it
;
2800 char newPath
[strlen(anRPath
) + strlen(trailingPath
)+2];
2801 strcpy(newPath
, anRPath
);
2802 strcat(newPath
, "/");
2803 strcat(newPath
, trailingPath
);
2804 image
= loadPhase4(newPath
, orgPath
, context
, exceptions
);
2805 if ( gLinkContext
.verboseRPaths
&& (exceptions
!= NULL
) ) {
2806 if ( image
!= NULL
)
2807 dyld::log("RPATH successful expansion of %s to: %s\n", orgPath
, newPath
);
2809 dyld::log("RPATH failed to expanding %s to: %s\n", orgPath
, newPath
);
2811 if ( image
!= NULL
)
2817 // substitute @rpath with LD_LIBRARY_PATH
2818 if ( sEnv
.LD_LIBRARY_PATH
!= NULL
) {
2819 image
= loadPhase2(trailingPath
, orgPath
, context
, NULL
, sEnv
.LD_LIBRARY_PATH
, exceptions
);
2820 if ( image
!= NULL
)
2824 // if this is the "open" pass, don't try to open @rpath/... as a relative path
2825 if ( (exceptions
!= NULL
) && (trailingPath
!= path
) )
2828 else if (sProcessIsRestricted
&& (path
[0] != '/' )) {
2829 throwf("unsafe use of relative rpath %s in %s with restricted binary", path
, context
.origin
);
2832 return loadPhase4(path
, orgPath
, context
, exceptions
);
2837 static ImageLoader
* loadPhase2(const char* path
, const char* orgPath
, const LoadContext
& context
,
2838 const char* const frameworkPaths
[], const char* const libraryPaths
[],
2839 std::vector
<const char*>* exceptions
)
2841 //dyld::log("%s(%s, %p)\n", __func__ , path, exceptions);
2842 ImageLoader
* image
= NULL
;
2843 const char* frameworkPartialPath
= getFrameworkPartialPath(path
);
2844 if ( frameworkPaths
!= NULL
) {
2845 if ( frameworkPartialPath
!= NULL
) {
2846 const size_t frameworkPartialPathLen
= strlen(frameworkPartialPath
);
2847 for(const char* const* fp
= frameworkPaths
; *fp
!= NULL
; ++fp
) {
2848 char npath
[strlen(*fp
)+frameworkPartialPathLen
+8];
2851 strcat(npath
, frameworkPartialPath
);
2852 //dyld::log("dyld: fallback framework path used: %s() -> loadPhase4(\"%s\", ...)\n", __func__, npath);
2853 image
= loadPhase4(npath
, orgPath
, context
, exceptions
);
2854 if ( image
!= NULL
)
2859 // <rdar://problem/12649639> An executable with the same name as a framework & DYLD_LIBRARY_PATH pointing to it gets loaded twice
2860 // <rdar://problem/14160846> Some apps depend on frameworks being found via library paths
2861 if ( (libraryPaths
!= NULL
) && ((frameworkPartialPath
== NULL
) || sFrameworksFoundAsDylibs
) ) {
2862 const char* libraryLeafName
= getLibraryLeafName(path
);
2863 const size_t libraryLeafNameLen
= strlen(libraryLeafName
);
2864 for(const char* const* lp
= libraryPaths
; *lp
!= NULL
; ++lp
) {
2865 char libpath
[strlen(*lp
)+libraryLeafNameLen
+8];
2866 strcpy(libpath
, *lp
);
2867 strcat(libpath
, "/");
2868 strcat(libpath
, libraryLeafName
);
2869 //dyld::log("dyld: fallback library path used: %s() -> loadPhase4(\"%s\", ...)\n", __func__, libpath);
2870 image
= loadPhase4(libpath
, orgPath
, context
, exceptions
);
2871 if ( image
!= NULL
)
2878 // try search overrides and fallbacks
2879 static ImageLoader
* loadPhase1(const char* path
, const char* orgPath
, const LoadContext
& context
, std::vector
<const char*>* exceptions
)
2881 //dyld::log("%s(%s, %p)\n", __func__ , path, exceptions);
2882 ImageLoader
* image
= NULL
;
2884 // handle LD_LIBRARY_PATH environment variables that force searching
2885 if ( context
.useLdLibraryPath
&& (sEnv
.LD_LIBRARY_PATH
!= NULL
) ) {
2886 image
= loadPhase2(path
, orgPath
, context
, NULL
, sEnv
.LD_LIBRARY_PATH
, exceptions
);
2887 if ( image
!= NULL
)
2891 // handle DYLD_ environment variables that force searching
2892 if ( context
.useSearchPaths
&& ((sEnv
.DYLD_FRAMEWORK_PATH
!= NULL
) || (sEnv
.DYLD_LIBRARY_PATH
!= NULL
)) ) {
2893 image
= loadPhase2(path
, orgPath
, context
, sEnv
.DYLD_FRAMEWORK_PATH
, sEnv
.DYLD_LIBRARY_PATH
, exceptions
);
2894 if ( image
!= NULL
)
2899 image
= loadPhase3(path
, orgPath
, context
, exceptions
);
2900 if ( image
!= NULL
)
2903 // try fallback paths during second time (will open file)
2904 const char* const* fallbackLibraryPaths
= sEnv
.DYLD_FALLBACK_LIBRARY_PATH
;
2905 if ( (fallbackLibraryPaths
!= NULL
) && !context
.useFallbackPaths
)
2906 fallbackLibraryPaths
= NULL
;
2907 if ( !context
.dontLoad
&& (exceptions
!= NULL
) && ((sEnv
.DYLD_FALLBACK_FRAMEWORK_PATH
!= NULL
) || (fallbackLibraryPaths
!= NULL
)) ) {
2908 image
= loadPhase2(path
, orgPath
, context
, sEnv
.DYLD_FALLBACK_FRAMEWORK_PATH
, fallbackLibraryPaths
, exceptions
);
2909 if ( image
!= NULL
)
2916 // try root substitutions
2917 static ImageLoader
* loadPhase0(const char* path
, const char* orgPath
, const LoadContext
& context
, std::vector
<const char*>* exceptions
)
2919 //dyld::log("%s(%s, %p)\n", __func__ , path, exceptions);
2921 // handle DYLD_ROOT_PATH which forces absolute paths to use a new root
2922 if ( (gLinkContext
.rootPaths
!= NULL
) && (path
[0] == '/') ) {
2923 for(const char* const* rootPath
= gLinkContext
.rootPaths
; *rootPath
!= NULL
; ++rootPath
) {
2924 char newPath
[strlen(*rootPath
) + strlen(path
)+2];
2925 strcpy(newPath
, *rootPath
);
2926 strcat(newPath
, path
);
2927 ImageLoader
* image
= loadPhase1(newPath
, orgPath
, context
, exceptions
);
2928 if ( image
!= NULL
)
2934 return loadPhase1(path
, orgPath
, context
, exceptions
);
2937 #if DYLD_SHARED_CACHE_SUPPORT
2938 static bool cacheablePath(const char* path
) {
2939 if (strncmp(path
, "/usr/lib/", 9) == 0)
2941 if (strncmp(path
, "/System/Library/", 16) == 0)
2948 // Given all the DYLD_ environment variables, the general case for loading libraries
2949 // is that any given path expands into a list of possible locations to load. We
2950 // also must take care to ensure two copies of the "same" library are never loaded.
2952 // The algorithm used here is that there is a separate function for each "phase" of the
2953 // path expansion. Each phase function calls the next phase with each possible expansion
2954 // of that phase. The result is the last phase is called with all possible paths.
2956 // To catch duplicates the algorithm is run twice. The first time, the last phase checks
2957 // the path against all loaded images. The second time, the last phase calls open() on
2958 // the path. Either time, if an image is found, the phases all unwind without checking
2961 ImageLoader
* load(const char* path
, const LoadContext
& context
)
2963 CRSetCrashLogMessage2(path
);
2964 const char* orgPath
= path
;
2966 //dyld::log("%s(%s)\n", __func__ , path);
2967 char realPath
[PATH_MAX
];
2968 // when DYLD_IMAGE_SUFFIX is in used, do a realpath(), otherwise a load of "Foo.framework/Foo" will not match
2969 if ( context
.useSearchPaths
&& ( gLinkContext
.imageSuffix
!= NULL
) ) {
2970 if ( realpath(path
, realPath
) != NULL
)
2974 // try all path permutations and check against existing loaded images
2975 ImageLoader
* image
= loadPhase0(path
, orgPath
, context
, NULL
);
2976 if ( image
!= NULL
) {
2977 CRSetCrashLogMessage2(NULL
);
2981 // try all path permutations and try open() until first success
2982 std::vector
<const char*> exceptions
;
2983 image
= loadPhase0(path
, orgPath
, context
, &exceptions
);
2984 #if __IPHONE_OS_VERSION_MIN_REQUIRED && DYLD_SHARED_CACHE_SUPPORT && !TARGET_IPHONE_SIMULATOR
2985 // <rdar://problem/16704628> support symlinks on disk to a path in dyld shared cache
2986 if ( (image
== NULL
) && cacheablePath(path
) && !context
.dontLoad
) {
2987 char resolvedPath
[PATH_MAX
];
2988 realpath(path
, resolvedPath
);
2990 // If realpath() resolves to a path which does not exist on disk, errno is set to ENOENT
2991 if ( (myerr
== ENOENT
) || (myerr
== 0) )
2993 // see if this image is in shared cache
2994 const macho_header
* mhInCache
;
2995 const char* pathInCache
;
2997 if ( findInSharedCacheImage(resolvedPath
, false, NULL
, &mhInCache
, &pathInCache
, &slideInCache
) ) {
2998 struct stat stat_buf
;
2999 bzero(&stat_buf
, sizeof(stat_buf
));
3001 image
= ImageLoaderMachO::instantiateFromCache(mhInCache
, pathInCache
, slideInCache
, stat_buf
, gLinkContext
);
3002 image
= checkandAddImage(image
, context
);
3011 CRSetCrashLogMessage2(NULL
);
3012 if ( image
!= NULL
) {
3013 // <rdar://problem/6916014> leak in dyld during dlopen when using DYLD_ variables
3014 for (std::vector
<const char*>::iterator it
= exceptions
.begin(); it
!= exceptions
.end(); ++it
) {
3017 #if DYLD_SHARED_CACHE_SUPPORT
3018 // if loaded image is not from cache, but original path is in cache
3019 // set gSharedCacheOverridden flag to disable some ObjC optimizations
3020 if ( !gSharedCacheOverridden
&& !image
->inSharedCache() && image
->isDylib() && cacheablePath(path
) && inSharedCache(path
) ) {
3021 gSharedCacheOverridden
= true;
3026 else if ( exceptions
.size() == 0 ) {
3027 if ( context
.dontLoad
) {
3031 throw "image not found";
3034 const char* msgStart
= "no suitable image found. Did find:";
3035 const char* delim
= "\n\t";
3036 size_t allsizes
= strlen(msgStart
)+8;
3037 for (size_t i
=0; i
< exceptions
.size(); ++i
)
3038 allsizes
+= (strlen(exceptions
[i
]) + strlen(delim
));
3039 char* fullMsg
= new char[allsizes
];
3040 strcpy(fullMsg
, msgStart
);
3041 for (size_t i
=0; i
< exceptions
.size(); ++i
) {
3042 strcat(fullMsg
, delim
);
3043 strcat(fullMsg
, exceptions
[i
]);
3044 free((void*)exceptions
[i
]);
3046 throw (const char*)fullMsg
;
3052 #if DYLD_SHARED_CACHE_SUPPORT
3057 #define ARCH_NAME "i386"
3058 #define ARCH_CACHE_MAGIC "dyld_v1 i386"
3060 #define ARCH_NAME "x86_64"
3061 #define ARCH_CACHE_MAGIC "dyld_v1 x86_64"
3062 #define ARCH_NAME_H "x86_64h"
3063 #define ARCH_CACHE_MAGIC_H "dyld_v1 x86_64h"
3064 #elif __ARM_ARCH_5TEJ__
3065 #define ARCH_NAME "armv5"
3066 #define ARCH_CACHE_MAGIC "dyld_v1 armv5"
3067 #elif __ARM_ARCH_6K__
3068 #define ARCH_NAME "armv6"
3069 #define ARCH_CACHE_MAGIC "dyld_v1 armv6"
3070 #elif __ARM_ARCH_7F__
3071 #define ARCH_NAME "armv7f"
3072 #define ARCH_CACHE_MAGIC "dyld_v1 armv7f"
3073 #elif __ARM_ARCH_7K__
3074 #define ARCH_NAME "armv7k"
3075 #define ARCH_CACHE_MAGIC "dyld_v1 armv7k"
3076 #elif __ARM_ARCH_7A__
3077 #define ARCH_NAME "armv7"
3078 #define ARCH_CACHE_MAGIC "dyld_v1 armv7"
3079 #elif __ARM_ARCH_7S__
3080 #define ARCH_NAME "armv7s"
3081 #define ARCH_CACHE_MAGIC "dyld_v1 armv7s"
3083 #define ARCH_NAME "arm64"
3084 #define ARCH_CACHE_MAGIC "dyld_v1 arm64"
3088 static int __attribute__((noinline
)) _shared_region_check_np(uint64_t* start_address
)
3090 if ( gLinkContext
.sharedRegionMode
== ImageLoader::kUseSharedRegion
)
3091 return syscall(294, start_address
);
3096 static int __attribute__((noinline
)) _shared_region_map_and_slide_np(int fd
, uint32_t count
, const shared_file_mapping_np mappings
[],
3097 int codeSignatureMappingIndex
, long slide
, void* slideInfo
, unsigned long slideInfoSize
)
3099 // register code signature blob for whole dyld cache
3100 if ( codeSignatureMappingIndex
!= -1 ) {
3101 fsignatures_t siginfo
;
3102 siginfo
.fs_file_start
= 0; // cache always starts at beginning of file
3103 siginfo
.fs_blob_start
= (void*)mappings
[codeSignatureMappingIndex
].sfm_file_offset
;
3104 siginfo
.fs_blob_size
= mappings
[codeSignatureMappingIndex
].sfm_size
;
3105 int result
= fcntl(fd
, F_ADDFILESIGS
, &siginfo
);
3106 // <rdar://problem/12891874> don't warn in chrooted case because mapping syscall is about to fail too
3107 if ( (result
== -1) && gLinkContext
.verboseMapping
)
3108 dyld::log("dyld: code signature registration for shared cache failed with errno=%d\n", errno
);
3111 if ( gLinkContext
.sharedRegionMode
== ImageLoader::kUseSharedRegion
) {
3112 return syscall(438, fd
, count
, mappings
, slide
, slideInfo
, slideInfoSize
);
3115 // remove the shared region sub-map
3116 vm_deallocate(mach_task_self(), (vm_address_t
)SHARED_REGION_BASE
, SHARED_REGION_SIZE
);
3118 // notify gdb or other lurkers that this process is no longer using the shared region
3119 dyld::gProcessInfo
->processDetachedFromSharedRegion
= true;
3121 // map cache just for this process with mmap()
3122 const shared_file_mapping_np
* const start
= mappings
;
3123 const shared_file_mapping_np
* const end
= &mappings
[count
];
3124 for (const shared_file_mapping_np
* p
= start
; p
< end
; ++p
) {
3125 void* mmapAddress
= (void*)(uintptr_t)(p
->sfm_address
);
3126 size_t size
= p
->sfm_size
;
3127 //dyld::log("dyld: mapping address %p with size 0x%08lX\n", mmapAddress, size);
3129 if ( p
->sfm_init_prot
& VM_PROT_EXECUTE
)
3130 protection
|= PROT_EXEC
;
3131 if ( p
->sfm_init_prot
& VM_PROT_READ
)
3132 protection
|= PROT_READ
;
3133 if ( p
->sfm_init_prot
& VM_PROT_WRITE
)
3134 protection
|= PROT_WRITE
;
3135 off_t offset
= p
->sfm_file_offset
;
3136 if ( mmap(mmapAddress
, size
, protection
, MAP_FIXED
| MAP_PRIVATE
, fd
, offset
) != mmapAddress
) {
3137 // failed to map some chunk of this shared cache file
3138 // clear shared region
3139 vm_deallocate(mach_task_self(), (vm_address_t
)SHARED_REGION_BASE
, SHARED_REGION_SIZE
);
3140 // go back to not using shared region at all
3141 gLinkContext
.sharedRegionMode
= ImageLoader::kDontUseSharedRegion
;
3142 if ( gLinkContext
.verboseMapping
) {
3143 dyld::log("dyld: shared cached region cannot be mapped at address %p with size 0x%08lX\n",
3151 // update all __DATA pages with slide info
3153 const uintptr_t dataPagesStart
= mappings
[1].sfm_address
;
3154 const dyld_cache_slide_info
* slideInfoHeader
= (dyld_cache_slide_info
*)slideInfo
;
3155 const uint16_t* toc
= (uint16_t*)((long)(slideInfoHeader
) + slideInfoHeader
->toc_offset
);
3156 const uint8_t* entries
= (uint8_t*)((long)(slideInfoHeader
) + slideInfoHeader
->entries_offset
);
3157 for(uint32_t i
=0; i
< slideInfoHeader
->toc_count
; ++i
) {
3158 const uint8_t* entry
= &entries
[toc
[i
]*slideInfoHeader
->entries_size
];
3159 const uint8_t* page
= (uint8_t*)(long)(dataPagesStart
+ (4096*i
));
3160 //dyld::log("page=%p toc[%d]=%d entries=%p\n", page, i, toc[i], entry);
3161 for(int j
=0; j
< 128; ++j
) {
3162 uint8_t b
= entry
[j
];
3163 //dyld::log(" entry[%d] = 0x%02X\n", j, b);
3165 for(int k
=0; k
< 8; ++k
) {
3167 uintptr_t* p
= (uintptr_t*)(page
+ j
*8*4 + k
*4);
3168 uintptr_t value
= *p
;
3169 //dyld::log(" *%p was 0x%lX will be 0x%lX\n", p, value, value+sSharedCacheSlide);
3178 // succesfully mapped shared cache for just this process
3179 gLinkContext
.sharedRegionMode
= ImageLoader::kUsePrivateSharedRegion
;
3185 const void* imMemorySharedCacheHeader()
3187 return sSharedCache
;
3190 int openSharedCacheFile()
3192 char path
[MAXPATHLEN
];
3193 strlcpy(path
, sSharedCacheDir
, MAXPATHLEN
);
3194 strlcat(path
, "/", MAXPATHLEN
);
3197 strlcat(path
, DYLD_SHARED_CACHE_BASE_NAME ARCH_NAME_H
, MAXPATHLEN
);
3198 int fd
= my_open(path
, O_RDONLY
, 0);
3200 if ( gLinkContext
.verboseMapping
)
3201 dyld::log("dyld: Mapping%s shared cache from %s\n", (gLinkContext
.sharedRegionMode
== ImageLoader::kUsePrivateSharedRegion
) ? " private": "", path
);
3204 strlcpy(path
, sSharedCacheDir
, MAXPATHLEN
);
3207 strlcat(path
, DYLD_SHARED_CACHE_BASE_NAME ARCH_NAME
, MAXPATHLEN
);
3208 if ( gLinkContext
.verboseMapping
)
3209 dyld::log("dyld: Mapping%s shared cache from %s\n", (gLinkContext
.sharedRegionMode
== ImageLoader::kUsePrivateSharedRegion
) ? " private": "", path
);
3210 return my_open(path
, O_RDONLY
, 0);
3214 static void getCacheBounds(uint32_t mappingsCount
, const shared_file_mapping_np mappings
[], uint64_t& lowAddress
, uint64_t& highAddress
)
3218 for(uint32_t i
=0; i
< mappingsCount
; ++i
) {
3219 if ( lowAddress
== 0 ) {
3220 lowAddress
= mappings
[i
].sfm_address
;
3221 highAddress
= mappings
[i
].sfm_address
+ mappings
[i
].sfm_size
;
3224 if ( mappings
[i
].sfm_address
< lowAddress
)
3225 lowAddress
= mappings
[i
].sfm_address
;
3226 if ( (mappings
[i
].sfm_address
+ mappings
[i
].sfm_size
) > highAddress
)
3227 highAddress
= mappings
[i
].sfm_address
+ mappings
[i
].sfm_size
;
3232 static long pickCacheSlide(uint32_t mappingsCount
, shared_file_mapping_np mappings
[])
3235 // x86_64 has a two memory regions:
3236 // 256MB at 0x00007FFF70000000
3237 // 1024MB at 0x00007FFF80000000
3238 // Some old shared caches have r/w region after rx region, so all regions slide within 1GB range
3239 // Newer shared caches have r/w region based at 0x7FFF70000000 and r/o regions at 0x7FFF80000000, so each part has max slide
3240 if ( (mappingsCount
>= 3) && (mappings
[1].sfm_init_prot
== (VM_PROT_READ
|VM_PROT_WRITE
)) && (mappings
[1].sfm_address
== 0x00007FFF70000000) ) {
3241 const uint64_t rwSize
= mappings
[1].sfm_size
;
3242 const uint64_t rwSlop
= 0x10000000ULL
- rwSize
;
3243 const uint64_t roSize
= (mappings
[2].sfm_address
+ mappings
[2].sfm_size
) - mappings
[0].sfm_address
;
3244 const uint64_t roSlop
= 0x40000000ULL
- roSize
;
3245 const uint64_t space
= (rwSlop
< roSlop
) ? rwSlop
: roSlop
;
3247 // choose new random slide
3248 long slide
= (arc4random() % space
) & (-4096);
3249 //dyld::log("rwSlop=0x%0llX, roSlop=0x%0llX\n", rwSlop, roSlop);
3250 //dyld::log("space=0x%0llX, slide=0x%0lX\n", space, slide);
3253 for(uint32_t i
=0; i
< mappingsCount
; ++i
) {
3254 mappings
[i
].sfm_address
+= slide
;
3259 // else fall through to handle old style cache
3261 // get bounds of cache
3262 uint64_t lowAddress
;
3263 uint64_t highAddress
;
3264 getCacheBounds(mappingsCount
, mappings
, lowAddress
, highAddress
);
3267 const uint64_t space
= (SHARED_REGION_BASE
+ SHARED_REGION_SIZE
) - highAddress
;
3269 // choose new random slide
3270 long slide
= dyld_page_trunc(arc4random() % space
);
3271 //dyld::log("slideSpace=0x%0llX\n", space);
3272 //dyld::log("slide=0x%0lX\n", slide);
3275 for(uint32_t i
=0; i
< mappingsCount
; ++i
) {
3276 mappings
[i
].sfm_address
+= slide
;
3282 static void mapSharedCache()
3284 uint64_t cacheBaseAddress
= 0;
3285 // quick check if a cache is already mapped into shared region
3286 if ( _shared_region_check_np(&cacheBaseAddress
) == 0 ) {
3287 sSharedCache
= (dyld_cache_header
*)cacheBaseAddress
;
3288 // if we don't understand the currently mapped shared cache, then ignore
3290 const char* magic
= (sHaswell
? ARCH_CACHE_MAGIC_H
: ARCH_CACHE_MAGIC
);
3292 const char* magic
= ARCH_CACHE_MAGIC
;
3294 if ( strcmp(sSharedCache
->magic
, magic
) != 0 ) {
3295 sSharedCache
= NULL
;
3296 if ( gLinkContext
.verboseMapping
) {
3297 dyld::log("dyld: existing shared cached in memory is not compatible\n");
3301 // check if cache file is slidable
3302 const dyld_cache_header
* header
= sSharedCache
;
3303 if ( (header
->mappingOffset
>= 0x48) && (header
->slideInfoSize
!= 0) ) {
3304 // solve for slide by comparing loaded address to address of first region
3305 const uint8_t* loadedAddress
= (uint8_t*)sSharedCache
;
3306 const dyld_cache_mapping_info
* const mappings
= (dyld_cache_mapping_info
*)(loadedAddress
+header
->mappingOffset
);
3307 const uint8_t* preferedLoadAddress
= (uint8_t*)(long)(mappings
[0].address
);
3308 sSharedCacheSlide
= loadedAddress
- preferedLoadAddress
;
3309 dyld::gProcessInfo
->sharedCacheSlide
= sSharedCacheSlide
;
3310 //dyld::log("sSharedCacheSlide=0x%08lX, loadedAddress=%p, preferedLoadAddress=%p\n", sSharedCacheSlide, loadedAddress, preferedLoadAddress);
3312 // if cache has a uuid, copy it
3313 if ( header
->mappingOffset
>= 0x68 ) {
3314 memcpy(dyld::gProcessInfo
->sharedCacheUUID
, header
->uuid
, 16);
3317 if ( gLinkContext
.verboseMapping
) {
3318 dyld::log("dyld: re-using existing shared cache mapping\n");
3322 #if __i386__ || __x86_64__
3323 // <rdar://problem/5925940> Safe Boot should disable dyld shared cache
3324 // if we are in safe-boot mode and the cache was not made during this boot cycle,
3325 // delete the cache file
3326 uint32_t safeBootValue
= 0;
3327 size_t safeBootValueSize
= sizeof(safeBootValue
);
3328 if ( (sysctlbyname("kern.safeboot", &safeBootValue
, &safeBootValueSize
, NULL
, 0) == 0) && (safeBootValue
!= 0) ) {
3329 // user booted machine in safe-boot mode
3330 struct stat dyldCacheStatInfo
;
3331 // Don't use custom DYLD_SHARED_CACHE_DIR if provided, use standard path
3332 if ( my_stat(MACOSX_DYLD_SHARED_CACHE_DIR DYLD_SHARED_CACHE_BASE_NAME ARCH_NAME
, &dyldCacheStatInfo
) == 0 ) {
3333 struct timeval bootTimeValue
;
3334 size_t bootTimeValueSize
= sizeof(bootTimeValue
);
3335 if ( (sysctlbyname("kern.boottime", &bootTimeValue
, &bootTimeValueSize
, NULL
, 0) == 0) && (bootTimeValue
.tv_sec
!= 0) ) {
3336 // if the cache file was created before this boot, then throw it away and let it rebuild itself
3337 if ( dyldCacheStatInfo
.st_mtime
< bootTimeValue
.tv_sec
) {
3338 ::unlink(MACOSX_DYLD_SHARED_CACHE_DIR DYLD_SHARED_CACHE_BASE_NAME ARCH_NAME
);
3339 gLinkContext
.sharedRegionMode
= ImageLoader::kDontUseSharedRegion
;
3346 // map in shared cache to shared region
3347 int fd
= openSharedCacheFile();
3349 uint8_t firstPages
[8192];
3350 if ( ::read(fd
, firstPages
, 8192) == 8192 ) {
3351 dyld_cache_header
* header
= (dyld_cache_header
*)firstPages
;
3353 const char* magic
= (sHaswell
? ARCH_CACHE_MAGIC_H
: ARCH_CACHE_MAGIC
);
3355 const char* magic
= ARCH_CACHE_MAGIC
;
3357 if ( strcmp(header
->magic
, magic
) == 0 ) {
3358 const dyld_cache_mapping_info
* const fileMappingsStart
= (dyld_cache_mapping_info
*)&firstPages
[header
->mappingOffset
];
3359 const dyld_cache_mapping_info
* const fileMappingsEnd
= &fileMappingsStart
[header
->mappingCount
];
3360 shared_file_mapping_np mappings
[header
->mappingCount
+1]; // add room for code-sig
3361 unsigned int mappingCount
= header
->mappingCount
;
3362 int codeSignatureMappingIndex
= -1;
3363 int readWriteMappingIndex
= -1;
3364 int readOnlyMappingIndex
= -1;
3365 // validate that the cache file has not been truncated
3366 bool goodCache
= false;
3367 struct stat stat_buf
;
3368 if ( fstat(fd
, &stat_buf
) == 0 ) {
3371 for (const dyld_cache_mapping_info
* p
= fileMappingsStart
; p
< fileMappingsEnd
; ++p
, ++i
) {
3372 mappings
[i
].sfm_address
= p
->address
;
3373 mappings
[i
].sfm_size
= p
->size
;
3374 mappings
[i
].sfm_file_offset
= p
->fileOffset
;
3375 mappings
[i
].sfm_max_prot
= p
->maxProt
;
3376 mappings
[i
].sfm_init_prot
= p
->initProt
;
3377 // rdar://problem/5694507 old update_dyld_shared_cache tool could make a cache file
3378 // that is not page aligned, but otherwise ok.
3379 if ( p
->fileOffset
+p
->size
> (uint64_t)(stat_buf
.st_size
+4095 & (-4096)) ) {
3380 dyld::log("dyld: shared cached file is corrupt: %s" DYLD_SHARED_CACHE_BASE_NAME ARCH_NAME
"\n", sSharedCacheDir
);
3383 if ( (mappings
[i
].sfm_init_prot
& (VM_PROT_READ
|VM_PROT_WRITE
)) == (VM_PROT_READ
|VM_PROT_WRITE
) ) {
3384 readWriteMappingIndex
= i
;
3386 if ( mappings
[i
].sfm_init_prot
== VM_PROT_READ
) {
3387 readOnlyMappingIndex
= i
;
3390 // if shared cache is code signed, add a mapping for the code signature
3391 uint64_t signatureSize
= header
->codeSignatureSize
;
3392 // zero size in header means signature runs to end-of-file
3393 if ( signatureSize
== 0 )
3394 signatureSize
= stat_buf
.st_size
- header
->codeSignatureOffset
;
3395 if ( signatureSize
!= 0 ) {
3396 int linkeditMapping
= mappingCount
-1;
3397 codeSignatureMappingIndex
= mappingCount
++;
3398 mappings
[codeSignatureMappingIndex
].sfm_address
= mappings
[linkeditMapping
].sfm_address
+ mappings
[linkeditMapping
].sfm_size
;
3399 #if __arm__ || __arm64__
3400 mappings
[codeSignatureMappingIndex
].sfm_size
= (signatureSize
+16383) & (-16384);
3402 mappings
[codeSignatureMappingIndex
].sfm_size
= (signatureSize
+4095) & (-4096);
3404 mappings
[codeSignatureMappingIndex
].sfm_file_offset
= header
->codeSignatureOffset
;
3405 mappings
[codeSignatureMappingIndex
].sfm_max_prot
= VM_PROT_READ
;
3406 mappings
[codeSignatureMappingIndex
].sfm_init_prot
= VM_PROT_READ
;
3409 #if __MAC_OS_X_VERSION_MIN_REQUIRED
3410 // sanity check that /usr/lib/libSystem.B.dylib stat() info matches cache
3411 if ( header
->imagesCount
* sizeof(dyld_cache_image_info
) + header
->imagesOffset
< 8192 ) {
3412 bool foundLibSystem
= false;
3413 if ( my_stat("/usr/lib/libSystem.B.dylib", &stat_buf
) == 0 ) {
3414 const dyld_cache_image_info
* images
= (dyld_cache_image_info
*)&firstPages
[header
->imagesOffset
];
3415 const dyld_cache_image_info
* const imagesEnd
= &images
[header
->imagesCount
];
3416 for (const dyld_cache_image_info
* p
= images
; p
< imagesEnd
; ++p
) {
3417 if ( ((time_t)p
->modTime
== stat_buf
.st_mtime
) && ((ino_t
)p
->inode
== stat_buf
.st_ino
) ) {
3418 foundLibSystem
= true;
3423 if ( !sSharedCacheIgnoreInodeAndTimeStamp
&& !foundLibSystem
) {
3424 dyld::log("dyld: shared cached file was built against a different libSystem.dylib, ignoring cache.\n"
3425 "to update dyld shared cache run: 'sudo update_dyld_shared_cache' then reboot.\n");
3430 #if __IPHONE_OS_VERSION_MIN_REQUIRED
3432 uint64_t lowAddress
;
3433 uint64_t highAddress
;
3434 getCacheBounds(mappingCount
, mappings
, lowAddress
, highAddress
);
3435 if ( (highAddress
-lowAddress
) > SHARED_REGION_SIZE
)
3436 throw "dyld shared cache is too big to fit in shared region";
3440 if ( goodCache
&& (readWriteMappingIndex
== -1) ) {
3441 dyld::log("dyld: shared cached file is missing read/write mapping: %s" DYLD_SHARED_CACHE_BASE_NAME ARCH_NAME
"\n", sSharedCacheDir
);
3444 if ( goodCache
&& (readOnlyMappingIndex
== -1) ) {
3445 dyld::log("dyld: shared cached file is missing read-only mapping: %s" DYLD_SHARED_CACHE_BASE_NAME ARCH_NAME
"\n", sSharedCacheDir
);
3449 long cacheSlide
= 0;
3450 void* slideInfo
= NULL
;
3451 uint64_t slideInfoSize
= 0;
3452 // check if shared cache contains slid info
3453 if ( header
->slideInfoSize
!= 0 ) {
3454 // <rdar://problem/8611968> don't slide shared cache if ASLR disabled (main executable didn't slide)
3455 if ( sMainExecutable
->isPositionIndependentExecutable() && (sMainExecutable
->getSlide() == 0) )
3458 // generate random slide amount
3459 cacheSlide
= pickCacheSlide(mappingCount
, mappings
);
3460 slideInfo
= (void*)(long)(mappings
[readOnlyMappingIndex
].sfm_address
+ (header
->slideInfoOffset
- mappings
[readOnlyMappingIndex
].sfm_file_offset
));
3461 slideInfoSize
= header
->slideInfoSize
;
3462 // add VM_PROT_SLIDE bit to __DATA area of cache
3463 mappings
[readWriteMappingIndex
].sfm_max_prot
|= VM_PROT_SLIDE
;
3464 mappings
[readWriteMappingIndex
].sfm_init_prot
|= VM_PROT_SLIDE
;
3467 if ( gLinkContext
.verboseMapping
) {
3468 dyld::log("dyld: calling _shared_region_map_and_slide_np() with regions:\n");
3469 for (int i
=0; i
< mappingCount
; ++i
) {
3470 dyld::log(" address=0x%08llX, size=0x%08llX, fileOffset=0x%08llX\n", mappings
[i
].sfm_address
, mappings
[i
].sfm_size
, mappings
[i
].sfm_file_offset
);
3473 if (_shared_region_map_and_slide_np(fd
, mappingCount
, mappings
, codeSignatureMappingIndex
, cacheSlide
, slideInfo
, slideInfoSize
) == 0) {
3474 // successfully mapped cache into shared region
3475 sSharedCache
= (dyld_cache_header
*)mappings
[0].sfm_address
;
3476 sSharedCacheSlide
= cacheSlide
;
3477 dyld::gProcessInfo
->sharedCacheSlide
= cacheSlide
;
3478 //dyld::log("sSharedCache=%p sSharedCacheSlide=0x%08lX\n", sSharedCache, sSharedCacheSlide);
3479 // if cache has a uuid, copy it
3480 if ( header
->mappingOffset
>= 0x68 ) {
3481 memcpy(dyld::gProcessInfo
->sharedCacheUUID
, header
->uuid
, 16);
3485 #if __IPHONE_OS_VERSION_MIN_REQUIRED
3486 throw "dyld shared cache could not be mapped";
3488 if ( gLinkContext
.verboseMapping
)
3489 dyld::log("dyld: shared cached file could not be mapped\n");
3494 if ( gLinkContext
.verboseMapping
)
3495 dyld::log("dyld: shared cached file is invalid\n");
3499 if ( gLinkContext
.verboseMapping
)
3500 dyld::log("dyld: shared cached file cannot be read\n");
3505 if ( gLinkContext
.verboseMapping
)
3506 dyld::log("dyld: shared cached file cannot be opened\n");
3510 // remember if dyld loaded at same address as when cache built
3511 if ( sSharedCache
!= NULL
) {
3512 gLinkContext
.dyldLoadedAtSameAddressNeededBySharedCache
= ((uintptr_t)(sSharedCache
->dyldBaseAddress
) == (uintptr_t)&_mh_dylinker_header
);
3515 // tell gdb where the shared cache is
3516 if ( sSharedCache
!= NULL
) {
3517 const dyld_cache_mapping_info
* const start
= (dyld_cache_mapping_info
*)((uint8_t*)sSharedCache
+ sSharedCache
->mappingOffset
);
3518 dyld_shared_cache_ranges
.sharedRegionsCount
= sSharedCache
->mappingCount
;
3519 // only room to tell gdb about first four regions
3520 if ( dyld_shared_cache_ranges
.sharedRegionsCount
> 4 )
3521 dyld_shared_cache_ranges
.sharedRegionsCount
= 4;
3522 const dyld_cache_mapping_info
* const end
= &start
[dyld_shared_cache_ranges
.sharedRegionsCount
];
3524 for (const dyld_cache_mapping_info
* p
= start
; p
< end
; ++p
, ++index
) {
3525 dyld_shared_cache_ranges
.ranges
[index
].start
= p
->address
+sSharedCacheSlide
;
3526 dyld_shared_cache_ranges
.ranges
[index
].length
= p
->size
;
3527 if ( gLinkContext
.verboseMapping
) {
3528 dyld::log(" 0x%08llX->0x%08llX %s%s%s init=%x, max=%x\n",
3529 p
->address
+sSharedCacheSlide
, p
->address
+sSharedCacheSlide
+p
->size
-1,
3530 ((p
->initProt
& VM_PROT_READ
) ? "read " : ""),
3531 ((p
->initProt
& VM_PROT_WRITE
) ? "write " : ""),
3532 ((p
->initProt
& VM_PROT_EXECUTE
) ? "execute " : ""), p
->initProt
, p
->maxProt
);
3535 // If a non-writable and executable region is found in the R/W shared region, then this is __IMPORT segments
3536 // This is an old cache. Make writable. dyld no longer supports turn W on and off as it binds
3537 if ( (p
->initProt
== (VM_PROT_READ
|VM_PROT_EXECUTE
)) && ((p
->address
& 0xF0000000) == 0xA0000000) ) {
3538 if ( p
->size
!= 0 ) {
3539 vm_prot_t prot
= VM_PROT_EXECUTE
| PROT_READ
| VM_PROT_WRITE
;
3540 vm_protect(mach_task_self(), p
->address
, p
->size
, false, prot
);
3541 if ( gLinkContext
.verboseMapping
) {
3542 dyld::log("%18s at 0x%08llX->0x%08llX altered permissions to %c%c%c\n", "", p
->address
,
3543 p
->address
+p
->size
-1,
3544 (prot
& PROT_READ
) ? 'r' : '.', (prot
& PROT_WRITE
) ? 'w' : '.', (prot
& PROT_EXEC
) ? 'x' : '.' );
3550 if ( gLinkContext
.verboseMapping
) {
3551 // list the code blob
3552 dyld_cache_header
* header
= (dyld_cache_header
*)sSharedCache
;
3553 uint64_t signatureSize
= header
->codeSignatureSize
;
3554 // zero size in header means signature runs to end-of-file
3555 if ( signatureSize
== 0 ) {
3556 struct stat stat_buf
;
3557 if ( my_stat(IPHONE_DYLD_SHARED_CACHE_DIR DYLD_SHARED_CACHE_BASE_NAME ARCH_NAME
, &stat_buf
) == 0 )
3558 signatureSize
= stat_buf
.st_size
- header
->codeSignatureOffset
;
3560 if ( signatureSize
!= 0 ) {
3561 const dyld_cache_mapping_info
* const last
= &start
[dyld_shared_cache_ranges
.sharedRegionsCount
-1];
3562 uint64_t codeBlobStart
= last
->address
+ last
->size
;
3563 dyld::log(" 0x%08llX->0x%08llX (code signature)\n", codeBlobStart
, codeBlobStart
+signatureSize
);
3566 #if __IPHONE_OS_VERSION_MIN_REQUIRED
3567 // check for file that enables dyld shared cache dylibs to be overridden
3568 struct stat enableStatBuf
;
3569 // check file size to determine if correct file is in place.
3570 // See <rdar://problem/13591370> Need a way to disable roots without removing /S/L/C/com.apple.dyld/enable...
3571 sDylibsOverrideCache
= ( (my_stat(IPHONE_DYLD_SHARED_CACHE_DIR
"enable-dylibs-to-override-cache", &enableStatBuf
) == 0)
3572 && (enableStatBuf
.st_size
< ENABLE_DYLIBS_TO_OVERRIDE_CACHE_SIZE
) );
3576 #endif // #if DYLD_SHARED_CACHE_SUPPORT
3580 // create when NSLinkModule is called for a second time on a bundle
3581 ImageLoader
* cloneImage(ImageLoader
* image
)
3583 // open file (automagically closed when this function exits)
3584 FileOpener
file(image
->getPath());
3586 struct stat stat_buf
;
3587 if ( fstat(file
.getFileDescriptor(), &stat_buf
) == -1)
3590 dyld::LoadContext context
;
3591 context
.useSearchPaths
= false;
3592 context
.useFallbackPaths
= false;
3593 context
.useLdLibraryPath
= false;
3594 context
.implicitRPath
= false;
3595 context
.matchByInstallName
= false;
3596 context
.dontLoad
= false;
3597 context
.mustBeBundle
= true;
3598 context
.mustBeDylib
= false;
3599 context
.canBePIE
= false;
3600 context
.origin
= NULL
;
3601 context
.rpath
= NULL
;
3602 return loadPhase6(file
.getFileDescriptor(), stat_buf
, image
->getPath(), context
);
3606 ImageLoader
* loadFromMemory(const uint8_t* mem
, uint64_t len
, const char* moduleName
)
3608 // if fat wrapper, find usable sub-file
3609 const fat_header
* memStartAsFat
= (fat_header
*)mem
;
3610 uint64_t fileOffset
= 0;
3611 uint64_t fileLength
= len
;
3612 if ( memStartAsFat
->magic
== OSSwapBigToHostInt32(FAT_MAGIC
) ) {
3613 if ( fatFindBest(memStartAsFat
, &fileOffset
, &fileLength
) ) {
3614 mem
= &mem
[fileOffset
];
3618 throw "no matching architecture in universal wrapper";
3623 if ( isCompatibleMachO(mem
, moduleName
) ) {
3624 ImageLoader
* image
= ImageLoaderMachO::instantiateFromMemory(moduleName
, (macho_header
*)mem
, len
, gLinkContext
);
3625 // don't add bundles to global list, they can be loaded but not linked. When linked it will be added to list
3626 if ( ! image
->isBundle() )
3631 // try other file formats here...
3633 // throw error about what was found
3634 switch (*(uint32_t*)mem
) {
3639 throw "mach-o, but wrong architecture";
3641 throwf("unknown file type, first eight bytes: 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X",
3642 mem
[0], mem
[1], mem
[2], mem
[3], mem
[4], mem
[5], mem
[6],mem
[7]);
3647 void registerAddCallback(ImageCallback func
)
3649 // now add to list to get notified when any more images are added
3650 sAddImageCallbacks
.push_back(func
);
3652 // call callback with all existing images
3653 for (std::vector
<ImageLoader
*>::iterator it
=sAllImages
.begin(); it
!= sAllImages
.end(); it
++) {
3654 ImageLoader
* image
= *it
;
3655 if ( image
->getState() >= dyld_image_state_bound
&& image
->getState() < dyld_image_state_terminated
)
3656 (*func
)(image
->machHeader(), image
->getSlide());
3660 void registerRemoveCallback(ImageCallback func
)
3662 // <rdar://problem/15025198> ignore calls to register a notification during a notification
3663 if ( sRemoveImageCallbacksInUse
)
3665 sRemoveImageCallbacks
.push_back(func
);
3668 void clearErrorMessage()
3670 error_string
[0] = '\0';
3673 void setErrorMessage(const char* message
)
3675 // save off error message in global buffer for CrashReporter to find
3676 strlcpy(error_string
, message
, sizeof(error_string
));
3679 const char* getErrorMessage()
3681 return error_string
;
3685 void halt(const char* message
)
3687 dyld::log("dyld: %s\n", message
);
3688 setErrorMessage(message
);
3689 uintptr_t terminationFlags
= 0;
3690 if ( !gLinkContext
.startedInitializingMainExecutable
)
3691 terminationFlags
= 1;
3692 setAlImageInfosHalt(error_string
, terminationFlags
);
3693 dyld_fatal_error(error_string
);
3696 static void setErrorStrings(unsigned errorCode
, const char* errorClientOfDylibPath
,
3697 const char* errorTargetDylibPath
, const char* errorSymbol
)
3699 dyld::gProcessInfo
->errorKind
= errorCode
;
3700 dyld::gProcessInfo
->errorClientOfDylibPath
= errorClientOfDylibPath
;
3701 dyld::gProcessInfo
->errorTargetDylibPath
= errorTargetDylibPath
;
3702 dyld::gProcessInfo
->errorSymbol
= errorSymbol
;
3706 uintptr_t bindLazySymbol(const mach_header
* mh
, uintptr_t* lazyPointer
)
3708 uintptr_t result
= 0;
3709 // acquire read-lock on dyld's data structures
3710 #if 0 // rdar://problem/3811777 turn off locking until deadlock is resolved
3711 if ( gLibSystemHelpers
!= NULL
)
3712 (*gLibSystemHelpers
->lockForReading
)();
3714 // lookup and bind lazy pointer and get target address
3716 ImageLoader
* target
;
3718 // fast stubs pass NULL for mh and image is instead found via the location of stub (aka lazyPointer)
3720 target
= dyld::findImageContainingAddress(lazyPointer
);
3722 target
= dyld::findImageByMachHeader(mh
);
3724 // note, target should always be mach-o, because only mach-o lazy handler wired up to this
3725 target
= dyld::findImageByMachHeader(mh
);
3727 if ( target
== NULL
)
3728 throwf("image not found for lazy pointer at %p", lazyPointer
);
3729 result
= target
->doBindLazySymbol(lazyPointer
, gLinkContext
);
3731 catch (const char* message
) {
3732 dyld::log("dyld: lazy symbol binding failed: %s\n", message
);
3735 // release read-lock on dyld's data structures
3737 if ( gLibSystemHelpers
!= NULL
)
3738 (*gLibSystemHelpers
->unlockForReading
)();
3740 // return target address to glue which jumps to it with real parameters restored
3745 uintptr_t fastBindLazySymbol(ImageLoader
** imageLoaderCache
, uintptr_t lazyBindingInfoOffset
)
3747 uintptr_t result
= 0;
3749 if ( *imageLoaderCache
== NULL
) {
3751 *imageLoaderCache
= dyld::findMappedRange((uintptr_t)imageLoaderCache
);
3752 if ( *imageLoaderCache
== NULL
) {
3753 const char* message
= "fast lazy binding from unknown image";
3754 dyld::log("dyld: %s\n", message
);
3759 // bind lazy pointer and return it
3761 result
= (*imageLoaderCache
)->doBindFastLazySymbol((uint32_t)lazyBindingInfoOffset
, gLinkContext
,
3762 (dyld::gLibSystemHelpers
!= NULL
) ? dyld::gLibSystemHelpers
->acquireGlobalDyldLock
: NULL
,
3763 (dyld::gLibSystemHelpers
!= NULL
) ? dyld::gLibSystemHelpers
->releaseGlobalDyldLock
: NULL
);
3765 catch (const char* message
) {
3766 dyld::log("dyld: lazy symbol binding failed: %s\n", message
);
3770 // return target address to glue which jumps to it with real parameters restored
3776 void registerUndefinedHandler(UndefinedHandler handler
)
3778 sUndefinedHandler
= handler
;
3781 static void undefinedHandler(const char* symboName
)
3783 if ( sUndefinedHandler
!= NULL
) {
3784 (*sUndefinedHandler
)(symboName
);
3788 static bool findExportedSymbol(const char* name
, bool onlyInCoalesced
, const ImageLoader::Symbol
** sym
, const ImageLoader
** image
)
3790 // search all images in order
3791 const ImageLoader
* firstWeakImage
= NULL
;
3792 const ImageLoader::Symbol
* firstWeakSym
= NULL
;
3793 const size_t imageCount
= sAllImages
.size();
3794 for(size_t i
=0; i
< imageCount
; ++i
) {
3795 ImageLoader
* anImage
= sAllImages
[i
];
3796 // the use of inserted libraries alters search order
3797 // so that inserted libraries are found before the main executable
3798 if ( sInsertedDylibCount
> 0 ) {
3799 if ( i
< sInsertedDylibCount
)
3800 anImage
= sAllImages
[i
+1];
3801 else if ( i
== sInsertedDylibCount
)
3802 anImage
= sAllImages
[0];
3804 if ( ! anImage
->hasHiddenExports() && (!onlyInCoalesced
|| anImage
->hasCoalescedExports()) ) {
3805 *sym
= anImage
->findExportedSymbol(name
, false, image
);
3806 if ( *sym
!= NULL
) {
3807 // if weak definition found, record first one found
3808 if ( ((*image
)->getExportedSymbolInfo(*sym
) & ImageLoader::kWeakDefinition
) != 0 ) {
3809 if ( firstWeakImage
== NULL
) {
3810 firstWeakImage
= *image
;
3811 firstWeakSym
= *sym
;
3815 // found non-weak, so immediately return with it
3821 if ( firstWeakSym
!= NULL
) {
3822 // found a weak definition, but no non-weak, so return first weak found
3823 *sym
= firstWeakSym
;
3824 *image
= firstWeakImage
;
3831 bool flatFindExportedSymbol(const char* name
, const ImageLoader::Symbol
** sym
, const ImageLoader
** image
)
3833 return findExportedSymbol(name
, false, sym
, image
);
3836 bool findCoalescedExportedSymbol(const char* name
, const ImageLoader::Symbol
** sym
, const ImageLoader
** image
)
3838 return findExportedSymbol(name
, true, sym
, image
);
3842 bool flatFindExportedSymbolWithHint(const char* name
, const char* librarySubstring
, const ImageLoader::Symbol
** sym
, const ImageLoader
** image
)
3844 // search all images in order
3845 const size_t imageCount
= sAllImages
.size();
3846 for(size_t i
=0; i
< imageCount
; ++i
){
3847 ImageLoader
* anImage
= sAllImages
[i
];
3848 // only look at images whose paths contain the hint string (NULL hint string is wildcard)
3849 if ( ! anImage
->isBundle() && ((librarySubstring
==NULL
) || (strstr(anImage
->getPath(), librarySubstring
) != NULL
)) ) {
3850 *sym
= anImage
->findExportedSymbol(name
, false, image
);
3851 if ( *sym
!= NULL
) {
3859 unsigned int getCoalescedImages(ImageLoader
* images
[])
3861 unsigned int count
= 0;
3862 for (std::vector
<ImageLoader
*>::iterator it
=sAllImages
.begin(); it
!= sAllImages
.end(); it
++) {
3863 ImageLoader
* image
= *it
;
3864 if ( image
->participatesInCoalescing() ) {
3873 static ImageLoader::MappedRegion
* getMappedRegions(ImageLoader::MappedRegion
* regions
)
3875 ImageLoader::MappedRegion
* end
= regions
;
3876 for (std::vector
<ImageLoader
*>::iterator it
=sAllImages
.begin(); it
!= sAllImages
.end(); it
++) {
3877 (*it
)->getMappedRegions(end
);
3882 void registerImageStateSingleChangeHandler(dyld_image_states state
, dyld_image_state_change_handler handler
)
3884 // mark the image that the handler is in as never-unload because dyld has a reference into it
3885 ImageLoader
* handlerImage
= findImageContainingAddress((void*)handler
);
3886 if ( handlerImage
!= NULL
)
3887 handlerImage
->setNeverUnload();
3889 // add to list of handlers
3890 std::vector
<dyld_image_state_change_handler
>* handlers
= stateToHandlers(state
, sSingleHandlers
);
3891 if ( handlers
!= NULL
) {
3892 // <rdar://problem/10332417> need updateAllImages() to be last in dyld_image_state_mapped list
3893 // so that if ObjC adds a handler that prevents a load, it happens before the gdb list is updated
3894 if ( state
== dyld_image_state_mapped
)
3895 handlers
->insert(handlers
->begin(), handler
);
3897 handlers
->push_back(handler
);
3899 // call callback with all existing images
3900 for (std::vector
<ImageLoader
*>::iterator it
=sAllImages
.begin(); it
!= sAllImages
.end(); it
++) {
3901 ImageLoader
* image
= *it
;
3902 dyld_image_info info
;
3903 info
.imageLoadAddress
= image
->machHeader();
3904 info
.imageFilePath
= image
->getRealPath();
3905 info
.imageFileModDate
= image
->lastModified();
3906 // should only call handler if state == image->state
3907 if ( image
->getState() == state
)
3908 (*handler
)(state
, 1, &info
);
3909 // ignore returned string, too late to do anything
3914 void registerImageStateBatchChangeHandler(dyld_image_states state
, dyld_image_state_change_handler handler
)
3916 // mark the image that the handler is in as never-unload because dyld has a reference into it
3917 ImageLoader
* handlerImage
= findImageContainingAddress((void*)handler
);
3918 if ( handlerImage
!= NULL
)
3919 handlerImage
->setNeverUnload();
3921 // add to list of handlers
3922 std::vector
<dyld_image_state_change_handler
>* handlers
= stateToHandlers(state
, sBatchHandlers
);
3923 if ( handlers
!= NULL
) {
3924 // insert at front, so that gdb handler is always last
3925 handlers
->insert(handlers
->begin(), handler
);
3927 // call callback with all existing images
3929 notifyBatchPartial(state
, true, handler
);
3931 catch (const char* msg
) {
3932 // ignore request to abort during registration
3937 static ImageLoader
* libraryLocator(const char* libraryName
, bool search
, const char* origin
, const ImageLoader::RPathChain
* rpaths
)
3939 dyld::LoadContext context
;
3940 context
.useSearchPaths
= search
;
3941 context
.useFallbackPaths
= search
;
3942 context
.useLdLibraryPath
= false;
3943 context
.implicitRPath
= false;
3944 context
.matchByInstallName
= false;
3945 context
.dontLoad
= false;
3946 context
.mustBeBundle
= false;
3947 context
.mustBeDylib
= true;
3948 context
.canBePIE
= false;
3949 context
.origin
= origin
;
3950 context
.rpath
= rpaths
;
3951 return load(libraryName
, context
);
3954 static const char* basename(const char* path
)
3956 const char* last
= path
;
3957 for (const char* s
= path
; *s
!= '\0'; s
++) {
3964 static void setContext(const macho_header
* mainExecutableMH
, int argc
, const char* argv
[], const char* envp
[], const char* apple
[])
3966 gLinkContext
.loadLibrary
= &libraryLocator
;
3967 gLinkContext
.terminationRecorder
= &terminationRecorder
;
3968 gLinkContext
.flatExportFinder
= &flatFindExportedSymbol
;
3969 gLinkContext
.coalescedExportFinder
= &findCoalescedExportedSymbol
;
3970 gLinkContext
.getCoalescedImages
= &getCoalescedImages
;
3971 gLinkContext
.undefinedHandler
= &undefinedHandler
;
3972 gLinkContext
.getAllMappedRegions
= &getMappedRegions
;
3973 gLinkContext
.bindingHandler
= NULL
;
3974 gLinkContext
.notifySingle
= ¬ifySingle
;
3975 gLinkContext
.notifyBatch
= ¬ifyBatch
;
3976 gLinkContext
.removeImage
= &removeImage
;
3977 gLinkContext
.registerDOFs
= ®isterDOFs
;
3978 gLinkContext
.clearAllDepths
= &clearAllDepths
;
3979 gLinkContext
.printAllDepths
= &printAllDepths
;
3980 gLinkContext
.imageCount
= &imageCount
;
3981 gLinkContext
.setNewProgramVars
= &setNewProgramVars
;
3982 #if DYLD_SHARED_CACHE_SUPPORT
3983 gLinkContext
.inSharedCache
= &inSharedCache
;
3985 gLinkContext
.setErrorStrings
= &setErrorStrings
;
3986 #if SUPPORT_OLD_CRT_INITIALIZATION
3987 gLinkContext
.setRunInitialzersOldWay
= &setRunInitialzersOldWay
;
3989 gLinkContext
.findImageContainingAddress
= &findImageContainingAddress
;
3990 gLinkContext
.addDynamicReference
= &addDynamicReference
;
3991 gLinkContext
.bindingOptions
= ImageLoader::kBindingNone
;
3992 gLinkContext
.argc
= argc
;
3993 gLinkContext
.argv
= argv
;
3994 gLinkContext
.envp
= envp
;
3995 gLinkContext
.apple
= apple
;
3996 gLinkContext
.progname
= (argv
[0] != NULL
) ? basename(argv
[0]) : "";
3997 gLinkContext
.programVars
.mh
= mainExecutableMH
;
3998 gLinkContext
.programVars
.NXArgcPtr
= &gLinkContext
.argc
;
3999 gLinkContext
.programVars
.NXArgvPtr
= &gLinkContext
.argv
;
4000 gLinkContext
.programVars
.environPtr
= &gLinkContext
.envp
;
4001 gLinkContext
.programVars
.__prognamePtr
=&gLinkContext
.progname
;
4002 gLinkContext
.mainExecutable
= NULL
;
4003 gLinkContext
.imageSuffix
= NULL
;
4004 gLinkContext
.dynamicInterposeArray
= NULL
;
4005 gLinkContext
.dynamicInterposeCount
= 0;
4006 gLinkContext
.prebindUsage
= ImageLoader::kUseAllPrebinding
;
4007 #if TARGET_IPHONE_SIMULATOR
4008 gLinkContext
.sharedRegionMode
= ImageLoader::kDontUseSharedRegion
;
4010 gLinkContext
.sharedRegionMode
= ImageLoader::kUseSharedRegion
;
4016 #define LC_SEGMENT_COMMAND LC_SEGMENT_64
4017 #define macho_segment_command segment_command_64
4018 #define macho_section section_64
4020 #define LC_SEGMENT_COMMAND LC_SEGMENT
4021 #define macho_segment_command segment_command
4022 #define macho_section section
4027 // Look for a special segment in the mach header.
4028 // Its presences means that the binary wants to have DYLD ignore
4029 // DYLD_ environment variables.
4031 static bool hasRestrictedSegment(const macho_header
* mh
)
4033 const uint32_t cmd_count
= mh
->ncmds
;
4034 const struct load_command
* const cmds
= (struct load_command
*)(((char*)mh
)+sizeof(macho_header
));
4035 const struct load_command
* cmd
= cmds
;
4036 for (uint32_t i
= 0; i
< cmd_count
; ++i
) {
4038 case LC_SEGMENT_COMMAND
:
4040 const struct macho_segment_command
* seg
= (struct macho_segment_command
*)cmd
;
4042 //dyld::log("seg name: %s\n", seg->segname);
4043 if (strcmp(seg
->segname
, "__RESTRICT") == 0) {
4044 const struct macho_section
* const sectionsStart
= (struct macho_section
*)((char*)seg
+ sizeof(struct macho_segment_command
));
4045 const struct macho_section
* const sectionsEnd
= §ionsStart
[seg
->nsects
];
4046 for (const struct macho_section
* sect
=sectionsStart
; sect
< sectionsEnd
; ++sect
) {
4047 if (strcmp(sect
->sectname
, "__restrict") == 0)
4054 cmd
= (const struct load_command
*)(((char*)cmd
)+cmd
->cmdsize
);
4060 #if SUPPORT_VERSIONED_PATHS
4062 // Peeks at a dylib file and returns its current_version and install_name.
4063 // Returns false on error.
4065 static bool getDylibVersionAndInstallname(const char* dylibPath
, uint32_t* version
, char* installName
)
4067 // open file (automagically closed when this function exits)
4068 FileOpener
file(dylibPath
);
4070 if ( file
.getFileDescriptor() == -1 )
4073 uint8_t firstPage
[4096];
4074 if ( pread(file
.getFileDescriptor(), firstPage
, 4096, 0) != 4096 )
4077 // if fat wrapper, find usable sub-file
4078 const fat_header
* fileStartAsFat
= (fat_header
*)firstPage
;
4079 if ( fileStartAsFat
->magic
== OSSwapBigToHostInt32(FAT_MAGIC
) ) {
4080 uint64_t fileOffset
;
4081 uint64_t fileLength
;
4082 if ( fatFindBest(fileStartAsFat
, &fileOffset
, &fileLength
) ) {
4083 if ( pread(file
.getFileDescriptor(), firstPage
, 4096, fileOffset
) != 4096 )
4091 // check mach-o header
4092 const mach_header
* mh
= (mach_header
*)firstPage
;
4093 if ( mh
->magic
!= sMainExecutableMachHeader
->magic
)
4095 if ( mh
->cputype
!= sMainExecutableMachHeader
->cputype
)
4098 // scan load commands for LC_ID_DYLIB
4099 const uint32_t cmd_count
= mh
->ncmds
;
4100 const struct load_command
* const cmds
= (struct load_command
*)(((char*)mh
)+sizeof(macho_header
));
4101 const struct load_command
* const cmdsReadEnd
= (struct load_command
*)(((char*)mh
)+4096);
4102 const struct load_command
* cmd
= cmds
;
4103 for (uint32_t i
= 0; i
< cmd_count
; ++i
) {
4107 const struct dylib_command
* id
= (struct dylib_command
*)cmd
;
4108 *version
= id
->dylib
.current_version
;
4109 if ( installName
!= NULL
)
4110 strlcpy(installName
, (char *)id
+ id
->dylib
.name
.offset
, PATH_MAX
);
4115 cmd
= (const struct load_command
*)(((char*)cmd
)+cmd
->cmdsize
);
4116 if ( cmd
> cmdsReadEnd
)
4122 #endif // SUPPORT_VERSIONED_PATHS
4126 static void printAllImages()
4128 dyld::log("printAllImages()\n");
4129 for (std::vector
<ImageLoader
*>::iterator it
=sAllImages
.begin(); it
!= sAllImages
.end(); it
++) {
4130 ImageLoader
* image
= *it
;
4131 dyld_image_states imageState
= image
->getState();
4132 dyld::log(" state=%d, dlopen-count=%d, never-unload=%d, in-use=%d, name=%s\n",
4133 imageState
, image
->dlopenCount(), image
->neverUnload(), image
->isMarkedInUse(), image
->getShortName());
4138 void link(ImageLoader
* image
, bool forceLazysBound
, bool neverUnload
, const ImageLoader::RPathChain
& loaderRPaths
)
4140 // add to list of known images. This did not happen at creation time for bundles
4141 if ( image
->isBundle() && !image
->isLinked() )
4144 // we detect root images as those not linked in yet
4145 if ( !image
->isLinked() )
4146 addRootImage(image
);
4150 image
->link(gLinkContext
, forceLazysBound
, false, neverUnload
, loaderRPaths
);
4152 catch (const char* msg
) {
4153 garbageCollectImages();
4159 void runInitializers(ImageLoader
* image
)
4161 // do bottom up initialization
4162 ImageLoader::InitializerTimingList initializerTimes
[sAllImages
.size()];
4163 initializerTimes
[0].count
= 0;
4164 image
->runInitializers(gLinkContext
, initializerTimes
[0]);
4167 // This function is called at the end of dlclose() when the reference count goes to zero.
4168 // The dylib being unloaded may have brought in other dependent dylibs when it was loaded.
4169 // Those dependent dylibs need to be unloaded, but only if they are not referenced by
4170 // something else. We use a standard mark and sweep garbage collection.
4172 // The tricky part is that when a dylib is unloaded it may have a termination function that
4173 // can run and itself call dlclose() on yet another dylib. The problem is that this
4174 // sort of gabage collection is not re-entrant. Instead a terminator's call to dlclose()
4175 // which calls garbageCollectImages() will just set a flag to re-do the garbage collection
4176 // when the current pass is done.
4178 // Also note that this is done within the dyld global lock, so it is always single threaded.
4180 void garbageCollectImages()
4182 static bool sDoingGC
= false;
4183 static bool sRedo
= false;
4186 // GC is currently being run, just set a flag to have it run again.
4195 // mark phase: mark all images not-in-use
4196 for (std::vector
<ImageLoader
*>::iterator it
=sAllImages
.begin(); it
!= sAllImages
.end(); it
++) {
4197 ImageLoader
* image
= *it
;
4198 //dyld::log("gc: neverUnload=%d name=%s\n", image->neverUnload(), image->getShortName());
4199 image
->markNotUsed();
4202 // sweep phase: mark as in-use, images reachable from never-unload or in-use image
4203 for (std::vector
<ImageLoader
*>::iterator it
=sAllImages
.begin(); it
!= sAllImages
.end(); it
++) {
4204 ImageLoader
* image
= *it
;
4205 if ( (image
->dlopenCount() != 0) || image
->neverUnload() ) {
4206 image
->markedUsedRecursive(sDynamicReferences
);
4210 // collect phase: build array of images not marked in-use
4211 ImageLoader
* deadImages
[sAllImages
.size()];
4212 unsigned deadCount
= 0;
4214 for (std::vector
<ImageLoader
*>::iterator it
=sAllImages
.begin(); it
!= sAllImages
.end(); it
++) {
4215 ImageLoader
* image
= *it
;
4216 if ( ! image
->isMarkedInUse() ) {
4217 deadImages
[i
++] = image
;
4218 if (gLogAPIs
) dyld::log("dlclose(), found unused image %p %s\n", image
, image
->getShortName());
4223 // collect phase: run termination routines for images not marked in-use
4224 const int maxRangeCount
= deadCount
*2;
4225 __cxa_range_t ranges
[maxRangeCount
];
4227 for (unsigned i
=0; i
< deadCount
; ++i
) {
4228 ImageLoader
* image
= deadImages
[i
];
4229 for (unsigned int j
=0; j
< image
->segmentCount(); ++j
) {
4230 if ( !image
->segExecutable(j
) )
4232 if ( rangeCount
< maxRangeCount
) {
4233 ranges
[rangeCount
].addr
= (const void*)image
->segActualLoadAddress(j
);
4234 ranges
[rangeCount
].length
= image
->segSize(j
);
4239 runImageStaticTerminators(image
);
4241 catch (const char* msg
) {
4242 dyld::warn("problem running terminators for image: %s\n", msg
);
4246 // <rdar://problem/14718598> dyld should call __cxa_finalize_ranges()
4247 if ( (rangeCount
> 0) && (gLibSystemHelpers
!= NULL
) && (gLibSystemHelpers
->version
>= 13) )
4248 (*gLibSystemHelpers
->cxa_finalize_ranges
)(ranges
, rangeCount
);
4250 // collect phase: delete all images which are not marked in-use
4253 mightBeMore
= false;
4254 for (std::vector
<ImageLoader
*>::iterator it
=sAllImages
.begin(); it
!= sAllImages
.end(); it
++) {
4255 ImageLoader
* image
= *it
;
4256 if ( ! image
->isMarkedInUse() ) {
4258 if (gLogAPIs
) dyld::log("dlclose(), deleting %p %s\n", image
, image
->getShortName());
4260 ImageLoader::deleteImage(image
);
4262 break; // interator in invalidated by this removal
4264 catch (const char* msg
) {
4265 dyld::warn("problem deleting image: %s\n", msg
);
4269 } while ( mightBeMore
);
4278 static void preflight_finally(ImageLoader
* image
)
4280 if ( image
->isBundle() ) {
4281 removeImageFromAllImages(image
->machHeader());
4282 ImageLoader::deleteImage(image
);
4284 sBundleBeingLoaded
= NULL
;
4285 dyld::garbageCollectImages();
4289 void preflight(ImageLoader
* image
, const ImageLoader::RPathChain
& loaderRPaths
)
4292 if ( image
->isBundle() )
4293 sBundleBeingLoaded
= image
; // hack
4294 image
->link(gLinkContext
, false, true, false, loaderRPaths
);
4296 catch (const char* msg
) {
4297 preflight_finally(image
);
4300 preflight_finally(image
);
4304 static bool isHaswell()
4306 #if TARGET_IPHONE_SIMULATOR
4309 // check system is capable of running x86_64h code
4310 struct host_basic_info info
;
4311 mach_msg_type_number_t count
= HOST_BASIC_INFO_COUNT
;
4312 mach_port_t hostPort
= mach_host_self();
4313 kern_return_t result
= host_info(hostPort
, HOST_BASIC_INFO
, (host_info_t
)&info
, &count
);
4314 mach_port_deallocate(mach_task_self(), hostPort
);
4315 if ( result
!= KERN_SUCCESS
)
4317 return ( info
.cpu_subtype
== CPU_SUBTYPE_X86_64_H
);
4322 static void loadInsertedDylib(const char* path
)
4324 ImageLoader
* image
= NULL
;
4326 LoadContext context
;
4327 context
.useSearchPaths
= false;
4328 context
.useFallbackPaths
= false;
4329 context
.useLdLibraryPath
= false;
4330 context
.implicitRPath
= false;
4331 context
.matchByInstallName
= false;
4332 context
.dontLoad
= false;
4333 context
.mustBeBundle
= false;
4334 context
.mustBeDylib
= true;
4335 context
.canBePIE
= false;
4336 context
.origin
= NULL
; // can't use @loader_path with DYLD_INSERT_LIBRARIES
4337 context
.rpath
= NULL
;
4338 image
= load(path
, context
);
4340 catch (const char* msg
) {
4341 #if TARGET_IPHONE_SIMULATOR
4342 dyld::log("dyld: warning: could not load inserted library '%s' because %s\n", path
, msg
);
4344 halt(dyld::mkstringf("could not load inserted library '%s' because %s\n", path
, msg
));
4348 halt(dyld::mkstringf("could not load inserted library '%s'\n", path
));
4352 static bool processRestricted(const macho_header
* mainExecutableMH
)
4354 #if __MAC_OS_X_VERSION_MIN_REQUIRED
4355 // ask kernel if code signature of program makes it restricted
4357 if ( csops(0, CS_OPS_STATUS
, &flags
, sizeof(flags
)) != -1 ) {
4358 if ( flags
& CS_ENFORCEMENT
) {
4359 gLinkContext
.codeSigningEnforced
= true;
4362 if (flags
& CS_RESTRICT
) {
4363 sRestrictedReason
= restrictedByEntitlements
;
4367 gLinkContext
.codeSigningEnforced
= true;
4370 // all processes with setuid or setgid bit set are restricted
4371 if ( issetugid() ) {
4372 sRestrictedReason
= restrictedBySetGUid
;
4376 // <rdar://problem/13158444&13245742> Respect __RESTRICT,__restrict section for root processes
4377 if ( hasRestrictedSegment(mainExecutableMH
) ) {
4378 // existence of __RESTRICT/__restrict section make process restricted
4379 sRestrictedReason
= restrictedBySegment
;
4386 bool processIsRestricted()
4388 return sProcessIsRestricted
;
4392 // <rdar://problem/10583252> Add dyld to uuidArray to enable symbolication of stackshots
4393 static void addDyldImageToUUIDList()
4395 const struct macho_header
* mh
= (macho_header
*)&__dso_handle
;
4396 const uint32_t cmd_count
= mh
->ncmds
;
4397 const struct load_command
* const cmds
= (struct load_command
*)((char*)mh
+ sizeof(macho_header
));
4398 const struct load_command
* cmd
= cmds
;
4399 for (uint32_t i
= 0; i
< cmd_count
; ++i
) {
4402 uuid_command
* uc
= (uuid_command
*)cmd
;
4403 dyld_uuid_info info
;
4404 info
.imageLoadAddress
= (mach_header
*)mh
;
4405 memcpy(info
.imageUUID
, uc
->uuid
, 16);
4406 addNonSharedCacheImageUUID(info
);
4410 cmd
= (const struct load_command
*)(((char*)cmd
)+cmd
->cmdsize
);
4414 #if __MAC_OS_X_VERSION_MIN_REQUIRED
4415 typedef int (*open_proc_t
)(const char*, int, int);
4416 typedef int (*fcntl_proc_t
)(int, int, void*);
4417 typedef int (*ioctl_proc_t
)(int, unsigned long, void*);
4418 static void* getProcessInfo() { return dyld::gProcessInfo
; }
4419 static SyscallHelpers sSysCalls
= {
4421 // added in version 1
4430 (fcntl_proc_t
)&fcntl
,
4431 (ioctl_proc_t
)&ioctl
,
4440 &pthread_mutex_lock
,
4441 &pthread_mutex_unlock
,
4443 &mach_port_deallocate
,
4445 &mach_timebase_info
,
4446 &OSAtomicCompareAndSwapPtrBarrier
,
4450 &mach_absolute_time
,
4451 // added in version 2
4453 // added in version 3
4459 __attribute__((noinline
))
4460 static uintptr_t useSimulatorDyld(int fd
, const macho_header
* mainExecutableMH
, const char* dyldPath
,
4461 int argc
, const char* argv
[], const char* envp
[], const char* apple
[], uintptr_t* startGlue
)
4465 // verify simulator dyld file is owned by root
4467 if ( fstat(fd
, &sb
) == -1 )
4469 if ( sb
.st_uid
!= 0 )
4472 // read first page of dyld file
4473 uint8_t firstPage
[4096];
4474 if ( pread(fd
, firstPage
, 4096, 0) != 4096 )
4477 // if fat file, pick matching slice
4478 uint64_t fileOffset
= 0;
4479 uint64_t fileLength
= sb
.st_size
;
4480 const fat_header
* fileStartAsFat
= (fat_header
*)firstPage
;
4481 if ( fileStartAsFat
->magic
== OSSwapBigToHostInt32(FAT_MAGIC
) ) {
4482 if ( !fatFindBest(fileStartAsFat
, &fileOffset
, &fileLength
) )
4484 // re-read buffer from start of mach-o slice in fat file
4485 if ( pread(fd
, firstPage
, 4096, fileOffset
) != 4096 )
4488 else if ( !isCompatibleMachO(firstPage
, dyldPath
) ) {
4492 // calculate total size of dyld segments
4493 const macho_header
* mh
= (const macho_header
*)firstPage
;
4494 uintptr_t mappingSize
= 0;
4495 uintptr_t preferredLoadAddress
= 0;
4496 const uint32_t cmd_count
= mh
->ncmds
;
4497 const struct load_command
* const cmds
= (struct load_command
*)(((char*)mh
)+sizeof(macho_header
));
4498 const struct load_command
* cmd
= cmds
;
4499 for (uint32_t i
= 0; i
< cmd_count
; ++i
) {
4501 case LC_SEGMENT_COMMAND
:
4503 struct macho_segment_command
* seg
= (struct macho_segment_command
*)cmd
;
4504 mappingSize
+= seg
->vmsize
;
4505 if ( seg
->fileoff
== 0 )
4506 preferredLoadAddress
= seg
->vmaddr
;
4510 cmd
= (const struct load_command
*)(((char*)cmd
)+cmd
->cmdsize
);
4513 // reserve space, then mmap each segment
4514 vm_address_t loadAddress
= 0;
4515 uintptr_t entry
= 0;
4516 if ( ::vm_allocate(mach_task_self(), &loadAddress
, mappingSize
, VM_FLAGS_ANYWHERE
) != 0 )
4519 struct linkedit_data_command
* codeSigCmd
= NULL
;
4520 for (uint32_t i
= 0; i
< cmd_count
; ++i
) {
4522 case LC_SEGMENT_COMMAND
:
4524 struct macho_segment_command
* seg
= (struct macho_segment_command
*)cmd
;
4525 uintptr_t requestedLoadAddress
= seg
->vmaddr
- preferredLoadAddress
+ loadAddress
;
4526 void* segAddress
= ::mmap((void*)requestedLoadAddress
, seg
->filesize
, seg
->initprot
, MAP_FIXED
| MAP_PRIVATE
, fd
, fileOffset
+ seg
->fileoff
);
4527 //dyld::log("dyld_sim %s mapped at %p\n", seg->segname, segAddress);
4528 if ( segAddress
== (void*)(-1) )
4535 const i386_thread_state_t
* registers
= (i386_thread_state_t
*)(((char*)cmd
) + 16);
4536 entry
= (registers
->__eip
+ loadAddress
- preferredLoadAddress
);
4538 const x86_thread_state64_t
* registers
= (x86_thread_state64_t
*)(((char*)cmd
) + 16);
4539 entry
= (registers
->__rip
+ loadAddress
- preferredLoadAddress
);
4543 case LC_CODE_SIGNATURE
:
4544 codeSigCmd
= (struct linkedit_data_command
*)cmd
;
4547 cmd
= (const struct load_command
*)(((char*)cmd
)+cmd
->cmdsize
);
4550 if ( codeSigCmd
!= NULL
) {
4551 fsignatures_t siginfo
;
4552 siginfo
.fs_file_start
=fileOffset
; // start of mach-o slice in fat file
4553 siginfo
.fs_blob_start
=(void*)(long)(codeSigCmd
->dataoff
); // start of code-signature in mach-o file
4554 siginfo
.fs_blob_size
=codeSigCmd
->datasize
; // size of code-signature
4555 int result
= fcntl(fd
, F_ADDFILESIGS
, &siginfo
);
4556 if ( result
== -1 ) {
4557 if ( (errno
== EPERM
) || (errno
== EBADEXEC
) )
4563 // notify debugger that dyld_sim is loaded
4564 dyld_image_info info
;
4565 info
.imageLoadAddress
= (mach_header
*)loadAddress
;
4566 info
.imageFilePath
= strdup(dyldPath
);
4567 info
.imageFileModDate
= sb
.st_mtime
;
4568 addImagesToAllImages(1, &info
);
4569 dyld::gProcessInfo
->notification(dyld_image_adding
, 1, &info
);
4571 // jump into new simulator dyld
4572 typedef uintptr_t (*sim_entry_proc_t
)(int argc
, const char* argv
[], const char* envp
[], const char* apple
[],
4573 const macho_header
* mainExecutableMH
, const macho_header
* dyldMH
, uintptr_t dyldSlide
,
4574 const dyld::SyscallHelpers
* vtable
, uintptr_t* startGlue
);
4575 sim_entry_proc_t newDyld
= (sim_entry_proc_t
)entry
;
4576 return (*newDyld
)(argc
, argv
, envp
, apple
, mainExecutableMH
, (macho_header
*)loadAddress
,
4577 loadAddress
- preferredLoadAddress
,
4578 &sSysCalls
, startGlue
);
4584 // Entry point for dyld. The kernel loads dyld and jumps to __dyld_start which
4585 // sets up some registers and call this function.
4587 // Returns address of main() in target program which __dyld_start jumps to
4590 _main(const macho_header
* mainExecutableMH
, uintptr_t mainExecutableSlide
,
4591 int argc
, const char* argv
[], const char* envp
[], const char* apple
[],
4592 uintptr_t* startGlue
)
4594 uintptr_t result
= 0;
4595 sMainExecutableMachHeader
= mainExecutableMH
;
4596 #if !TARGET_IPHONE_SIMULATOR
4597 const char* loggingPath
= _simple_getenv(envp
, "DYLD_PRINT_TO_FILE");
4598 if ( loggingPath
!= NULL
) {
4599 int fd
= open(loggingPath
, O_WRONLY
| O_CREAT
| O_APPEND
, 0644);
4605 dyld::log("dyld: could not open DYLD_PRINT_TO_FILE='%s', errno=%d\n", loggingPath
, errno
);
4609 #if __MAC_OS_X_VERSION_MIN_REQUIRED
4610 // if this is host dyld, check to see if iOS simulator is being run
4611 const char* rootPath
= _simple_getenv(envp
, "DYLD_ROOT_PATH");
4612 if ( rootPath
!= NULL
) {
4613 // look to see if simulator has its own dyld
4614 char simDyldPath
[PATH_MAX
];
4615 strlcpy(simDyldPath
, rootPath
, PATH_MAX
);
4616 strlcat(simDyldPath
, "/usr/lib/dyld_sim", PATH_MAX
);
4617 int fd
= my_open(simDyldPath
, O_RDONLY
, 0);
4619 result
= useSimulatorDyld(fd
, mainExecutableMH
, simDyldPath
, argc
, argv
, envp
, apple
, startGlue
);
4620 if ( !result
&& (*startGlue
== 0) )
4621 halt("problem loading iOS simulator dyld");
4627 CRSetCrashLogMessage("dyld: launch started");
4630 char bindingsLogPath
[256];
4632 const char* shortProgName
= "unknown";
4634 shortProgName
= strrchr(argv
[0], '/');
4635 if ( shortProgName
== NULL
)
4636 shortProgName
= argv
[0];
4640 mysprintf(bindingsLogPath
, "/tmp/bindings/%d-%s", getpid(), shortProgName
);
4641 sBindingsLogfile
= open(bindingsLogPath
, O_WRONLY
| O_CREAT
, 0666);
4642 if ( sBindingsLogfile
== -1 ) {
4643 ::mkdir("/tmp/bindings", 0777);
4644 sBindingsLogfile
= open(bindingsLogPath
, O_WRONLY
| O_CREAT
, 0666);
4646 //dyld::log("open(%s) => %d, errno = %d\n", bindingsLogPath, sBindingsLogfile, errno);
4648 setContext(mainExecutableMH
, argc
, argv
, envp
, apple
);
4650 // Pickup the pointer to the exec path.
4651 sExecPath
= _simple_getenv(apple
, "executable_path");
4653 // <rdar://problem/13868260> Remove interim apple[0] transition code from dyld
4654 if (!sExecPath
) sExecPath
= apple
[0];
4656 sExecPath
= apple
[0];
4657 bool ignoreEnvironmentVariables
= false;
4658 if ( sExecPath
[0] != '/' ) {
4659 // have relative path, use cwd to make absolute
4660 char cwdbuff
[MAXPATHLEN
];
4661 if ( getcwd(cwdbuff
, MAXPATHLEN
) != NULL
) {
4662 // maybe use static buffer to avoid calling malloc so early...
4663 char* s
= new char[strlen(cwdbuff
) + strlen(sExecPath
) + 2];
4666 strcat(s
, sExecPath
);
4670 // Remember short name of process for later logging
4671 sExecShortName
= ::strrchr(sExecPath
, '/');
4672 if ( sExecShortName
!= NULL
)
4675 sExecShortName
= sExecPath
;
4676 sProcessIsRestricted
= processRestricted(mainExecutableMH
);
4677 if ( sProcessIsRestricted
) {
4678 #if SUPPORT_LC_DYLD_ENVIRONMENT
4679 checkLoadCommandEnvironmentVariables();
4680 #if SUPPORT_VERSIONED_PATHS
4681 checkVersionedPaths();
4684 pruneEnvironmentVariables(envp
, &apple
);
4685 // set again because envp and apple may have changed or moved
4686 setContext(mainExecutableMH
, argc
, argv
, envp
, apple
);
4689 checkEnvironmentVariables(envp
, ignoreEnvironmentVariables
);
4690 if ( sEnv
.DYLD_PRINT_OPTS
)
4692 if ( sEnv
.DYLD_PRINT_ENV
)
4693 printEnvironmentVariables(envp
);
4695 // install gdb notifier
4696 stateToHandlers(dyld_image_state_dependents_mapped
, sBatchHandlers
)->push_back(notifyGDB
);
4697 stateToHandlers(dyld_image_state_mapped
, sSingleHandlers
)->push_back(updateAllImages
);
4698 // make initial allocations large enough that it is unlikely to need to be re-alloced
4699 sAllImages
.reserve(INITIAL_IMAGE_COUNT
);
4700 sImageRoots
.reserve(16);
4701 sAddImageCallbacks
.reserve(4);
4702 sRemoveImageCallbacks
.reserve(4);
4703 sImageFilesNeedingTermination
.reserve(16);
4704 sImageFilesNeedingDOFUnregistration
.reserve(8);
4706 #ifdef WAIT_FOR_SYSTEM_ORDER_HANDSHAKE
4707 // <rdar://problem/6849505> Add gating mechanism to dyld support system order file generation process
4708 WAIT_FOR_SYSTEM_ORDER_HANDSHAKE(dyld::gProcessInfo
->systemOrderFlag
);
4713 // add dyld itself to UUID list
4714 addDyldImageToUUIDList();
4715 CRSetCrashLogMessage(sLoadingCrashMessage
);
4716 // instantiate ImageLoader for main executable
4717 sMainExecutable
= instantiateFromLoadedImage(mainExecutableMH
, mainExecutableSlide
, sExecPath
);
4718 gLinkContext
.mainExecutable
= sMainExecutable
;
4719 gLinkContext
.processIsRestricted
= sProcessIsRestricted
;
4720 gLinkContext
.mainExecutableCodeSigned
= hasCodeSignatureLoadCommand(mainExecutableMH
);
4722 #if TARGET_IPHONE_SIMULATOR
4723 // check main executable is not too new for this OS
4725 if ( ! isSimulatorBinary((uint8_t*)mainExecutableMH
, sExecPath
) ) {
4726 throwf("program was built for Mac OS X and cannot be run in simulator");
4728 uint32_t mainMinOS
= sMainExecutable
->minOSVersion();
4729 // dyld is always built for the current OS, so we can get the current OS version
4730 // from the load command in dyld itself.
4731 uint32_t dyldMinOS
= ImageLoaderMachO::minOSVersion((const mach_header
*)&__dso_handle
);
4732 if ( mainMinOS
> dyldMinOS
) {
4733 throwf("app was built for iOS %d.%d which is newer than this simulator %d.%d",
4734 mainMinOS
>> 16, ((mainMinOS
>> 8) & 0xFF),
4735 dyldMinOS
>> 16, ((dyldMinOS
>> 8) & 0xFF));
4740 // load shared cache
4742 sHaswell
= isHaswell();
4744 checkSharedRegionDisable();
4745 #if DYLD_SHARED_CACHE_SUPPORT
4746 if ( gLinkContext
.sharedRegionMode
!= ImageLoader::kDontUseSharedRegion
)
4749 // load any inserted libraries
4750 if ( sEnv
.DYLD_INSERT_LIBRARIES
!= NULL
) {
4751 for (const char* const* lib
= sEnv
.DYLD_INSERT_LIBRARIES
; *lib
!= NULL
; ++lib
)
4752 loadInsertedDylib(*lib
);
4754 // record count of inserted libraries so that a flat search will look at
4755 // inserted libraries, then main, then others.
4756 sInsertedDylibCount
= sAllImages
.size()-1;
4758 // link main executable
4759 gLinkContext
.linkingMainExecutable
= true;
4760 link(sMainExecutable
, sEnv
.DYLD_BIND_AT_LAUNCH
, true, ImageLoader::RPathChain(NULL
, NULL
));
4761 sMainExecutable
->setNeverUnloadRecursive();
4762 if ( sMainExecutable
->forceFlat() ) {
4763 gLinkContext
.bindFlat
= true;
4764 gLinkContext
.prebindUsage
= ImageLoader::kUseNoPrebinding
;
4767 // link any inserted libraries
4768 // do this after linking main executable so that any dylibs pulled in by inserted
4769 // dylibs (e.g. libSystem) will not be in front of dylibs the program uses
4770 if ( sInsertedDylibCount
> 0 ) {
4771 for(unsigned int i
=0; i
< sInsertedDylibCount
; ++i
) {
4772 ImageLoader
* image
= sAllImages
[i
+1];
4773 link(image
, sEnv
.DYLD_BIND_AT_LAUNCH
, true, ImageLoader::RPathChain(NULL
, NULL
));
4774 image
->setNeverUnloadRecursive();
4776 // only INSERTED libraries can interpose
4777 // register interposing info after all inserted libraries are bound so chaining works
4778 for(unsigned int i
=0; i
< sInsertedDylibCount
; ++i
) {
4779 ImageLoader
* image
= sAllImages
[i
+1];
4780 image
->registerInterposing();
4783 // apply interposing to initial set of images
4784 for(int i
=0; i
< sImageRoots
.size(); ++i
) {
4785 sImageRoots
[i
]->applyInterposing(gLinkContext
);
4787 gLinkContext
.linkingMainExecutable
= false;
4789 // <rdar://problem/12186933> do weak binding only after all inserted images linked
4790 sMainExecutable
->weakBind(gLinkContext
);
4792 CRSetCrashLogMessage("dyld: launch, running initializers");
4793 #if SUPPORT_OLD_CRT_INITIALIZATION
4794 // Old way is to run initializers via a callback from crt1.o
4795 if ( ! gRunInitializersOldWay
)
4796 initializeMainExecutable();
4798 // run all initializers
4799 initializeMainExecutable();
4801 // find entry point for main executable
4802 result
= (uintptr_t)sMainExecutable
->getThreadPC();
4803 if ( result
!= 0 ) {
4804 // main executable uses LC_MAIN, needs to return to glue in libdyld.dylib
4805 if ( (gLibSystemHelpers
!= NULL
) && (gLibSystemHelpers
->version
>= 9) )
4806 *startGlue
= (uintptr_t)gLibSystemHelpers
->startGlueToCallExit
;
4808 halt("libdyld.dylib support not present for LC_MAIN");
4811 // main executable uses LC_UNIXTHREAD, dyld needs to let "start" in program set up for main()
4812 result
= (uintptr_t)sMainExecutable
->getMain();
4816 catch(const char* message
) {
4821 dyld::log("dyld: launch failed\n");
4824 CRSetCrashLogMessage(NULL
);