1 /* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*-
3 * Copyright (c) 2004-2005 Apple Computer, 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@
29 #include <sys/param.h>
30 #include <mach/mach_time.h> // mach_absolute_time()
31 #include <sys/types.h>
33 #include <mach-o/fat.h>
34 #include <mach-o/loader.h>
35 #include <libkern/OSByteOrder.h>
36 #include <mach/mach.h>
37 #include <sys/sysctl.h>
41 #include "mach-o/dyld_gdb.h"
44 #include "ImageLoader.h"
45 #include "ImageLoaderMachO.h"
46 #include "dyldLibSystemThreadHelpers.h"
49 #define CPU_TYPE_MASK 0x00FFFFFF /* complement of CPU_ARCH_MASK */
52 /* implemented in dyld_gdb.cpp */
53 void addImagesToAllImages(uint32_t infoCount
, const dyld_image_info info
[]);
54 void removeImageFromAllImages(const mach_header
* mh
);
55 #if OLD_GDB_DYLD_INTERFACE
56 void addImageForgdb(const mach_header
* mh
, uintptr_t slide
, const char* physicalPath
, const char* logicalPath
);
57 void removeImageForgdb(const struct mach_header
* mh
);
60 // magic so CrashReporter logs message
62 char error_string
[1024];
67 // The file contains the core of dyld used to get a process to main().
68 // The API's that dyld supports are implemented in dyldAPIs.cpp.
80 // state of all environment variables dyld uses
82 struct EnvironmentVariables
{
83 const char* const * DYLD_FRAMEWORK_PATH
;
84 const char* const * DYLD_FALLBACK_FRAMEWORK_PATH
;
85 const char* const * DYLD_LIBRARY_PATH
;
86 const char* const * DYLD_FALLBACK_LIBRARY_PATH
;
87 const char* const * DYLD_ROOT_PATH
;
88 const char* const * DYLD_INSERT_LIBRARIES
;
89 const char* const * LD_LIBRARY_PATH
; // for unix conformance
90 bool DYLD_PRINT_LIBRARIES
;
91 bool DYLD_PRINT_LIBRARIES_POST_LAUNCH
;
92 bool DYLD_BIND_AT_LAUNCH
;
93 bool DYLD_PRINT_STATISTICS
;
96 // DYLD_IMAGE_SUFFIX ==> gLinkContext.imageSuffix
97 // DYLD_PRINT_OPTS ==> gLinkContext.verboseOpts
98 // DYLD_PRINT_ENV ==> gLinkContext.verboseEnv
99 // DYLD_FORCE_FLAT_NAMESPACE ==> gLinkContext.bindFlat
100 // DYLD_PRINT_INITIALIZERS ==> gLinkContext.verboseInit
101 // DYLD_PRINT_SEGMENTS ==> gLinkContext.verboseMapping
102 // DYLD_PRINT_BINDINGS ==> gLinkContext.verboseBind
103 // DYLD_PRINT_REBASINGS ==> gLinkContext.verboseRebase
104 // DYLD_PRINT_APIS ==> gLogAPIs
105 // DYLD_IGNORE_PREBINDING ==> gLinkContext.prebindUsage
106 // DYLD_PREBIND_DEBUG ==> gLinkContext.verbosePrebinding
107 // DYLD_NEW_LOCAL_SHARED_REGIONS ==> gLinkContext.sharedRegionMode
108 // DYLD_SHARED_REGION ==> gLinkContext.sharedRegionMode
109 // DYLD_SLIDE_AND_PACK_DYLIBS ==> gLinkContext.slideAndPackDylibs
110 // DYLD_PRINT_WARNINGS ==> gLinkContext.verboseWarnings
114 static const char* sExecPath
= NULL
;
115 static const struct mach_header
* sMainExecutableMachHeader
= NULL
;
116 static cpu_type_t sHostCPU
;
117 static cpu_subtype_t sHostCPUsubtype
;
118 static ImageLoader
* sMainExecutable
= NULL
;
119 static std::vector
<ImageLoader
*> sAllImages
;
120 static std::vector
<ImageLoader
*> sImageRoots
;
121 static std::vector
<ImageLoader
*> sImageFilesNeedingTermination
;
122 static std::vector
<ImageLoader
*> sImagesToNotifyAboutOtherImages
;
123 static std::vector
<ImageCallback
> sAddImageCallbacks
;
124 static std::vector
<ImageCallback
> sRemoveImageCallbacks
;
125 static ImageLoader
* sLastImageByAddressCache
;
126 static EnvironmentVariables sEnv
;
127 static const char* sFrameworkFallbackPaths
[] = { "$HOME/Library/Frameworks", "/Library/Frameworks", "/Network/Library/Frameworks", "/System/Library/Frameworks", NULL
};
128 static const char* sLibraryFallbackPaths
[] = { "$HOME/lib", "/usr/local/lib", "/usr/lib", NULL
};
129 static BundleNotificationCallBack sBundleNotifier
= NULL
;
130 static BundleLocatorCallBack sBundleLocation
= NULL
;
131 static UndefinedHandler sUndefinedHandler
= NULL
;
132 ImageLoader::LinkContext gLinkContext
;
133 bool gLogAPIs
= false;
134 const struct ThreadingHelpers
* gThreadHelpers
= NULL
;
138 // utility class to assure files are closed when an exception is thrown
141 FileOpener(const char* path
);
143 int getFileDescriptor() { return fd
; }
148 FileOpener::FileOpener(const char* path
)
150 fd
= open(path
, O_RDONLY
, 0);
153 FileOpener::~FileOpener()
160 // Objective-C installs an addImage hook to dyld to get notified about new images
161 // The callback needs to be run after the image is rebased and bound, but before its initializers are called
162 static uint32_t imageNotification(ImageLoader
* image
, uint32_t startIndex
)
164 // tell all register add image handlers about this
165 const uint32_t callbackCount
= sAddImageCallbacks
.size();
166 for (uint32_t i
=startIndex
; i
< callbackCount
; ++i
) {
167 ImageCallback cb
= sAddImageCallbacks
[i
];
168 //fprintf(stderr, "dyld: calling add-image-callback[%d]=%p for %s\n", i, cb, image->getPath());
169 (cb
)(image
->machHeader(), image
->getSlide());
171 return callbackCount
;
176 // notify gdb et al about these new images
177 static void notifyAdding(std::vector
<ImageLoader
*>& images
)
180 unsigned int len
= images
.size();
182 dyld_image_info infos
[len
];
183 for (unsigned int i
=0; i
< len
; ++i
) {
184 dyld_image_info
* p
= &infos
[i
];
185 ImageLoader
* image
= images
[i
];
186 p
->imageLoadAddress
= image
->machHeader();
187 p
->imageFilePath
= image
->getPath();
188 p
->imageFileModDate
= image
->lastModified();
189 //fprintf(stderr, "notifying objc about %s\n", image->getPath());
193 addImagesToAllImages(len
, infos
);
195 // tell all interested images (after gdb, so you can debug anything the notification does)
196 for (std::vector
<ImageLoader
*>::iterator it
=sImagesToNotifyAboutOtherImages
.begin(); it
!= sImagesToNotifyAboutOtherImages
.end(); it
++) {
197 (*it
)->doNotification(dyld_image_adding
, len
, infos
);
204 // In order for register_func_for_add_image() callbacks to to be called bottom up,
205 // we need to maintain a list of root images. The main executable is usally the
206 // first root. Any images dynamically added are also roots (unless already loaded).
207 // If DYLD_INSERT_LIBRARIES is used, those libraries are first.
208 static void addRootImage(ImageLoader
* image
)
210 //fprintf(stderr, "addRootImage(%p, %s)\n", image, image->getPath());
211 // add to list of roots
212 sImageRoots
.push_back(image
);
215 // Objective-C will contain a __DATA/__image_notify section which contains pointers to a function to call
216 // whenever any new image is loaded.
217 static void addImageNeedingNotification(ImageLoader
* image
)
219 sImagesToNotifyAboutOtherImages
.push_back(image
);
222 static void addImage(ImageLoader
* image
)
224 // add to master list
225 sAllImages
.push_back(image
);
227 if ( sEnv
.DYLD_PRINT_LIBRARIES
|| (sEnv
.DYLD_PRINT_LIBRARIES_POST_LAUNCH
&& (sMainExecutable
!=NULL
) && sMainExecutable
->isLinked()) ) {
228 uint64_t offset
= image
->getOffsetInFatFile();
230 fprintf(stderr
, "dyld: loaded: %s\n", image
->getPath());
232 fprintf(stderr
, "dyld: loaded: %s, cpu-sub-type: %d\n", image
->getPath(), image
->machHeader()->cpusubtype
);
235 #if OLD_GDB_DYLD_INTERFACE
236 // let gdb find out about this
237 addImageForgdb(image
->machHeader(), image
->getSlide(), image
->getPath(), image
->getLogicalPath());
241 void removeImage(ImageLoader
* image
)
243 // flush find-by-address cache
244 if ( sLastImageByAddressCache
== image
)
245 sLastImageByAddressCache
= NULL
;
247 // if in termination list, pull it out and run terminator
248 for (std::vector
<ImageLoader
*>::iterator it
=sImageFilesNeedingTermination
.begin(); it
!= sImageFilesNeedingTermination
.end(); it
++) {
249 if ( *it
== image
) {
250 sImageFilesNeedingTermination
.erase(it
);
251 image
->doTermination(gLinkContext
);
256 // tell all register add image handlers about this
257 // do this before removing image from internal data structures so that the callback can querey dyld about the image
258 for (std::vector
<ImageCallback
>::iterator it
=sRemoveImageCallbacks
.begin(); it
!= sRemoveImageCallbacks
.end(); it
++) {
259 (*it
)(image
->machHeader(), image
->getSlide());
262 // remove from master list
263 for (std::vector
<ImageLoader
*>::iterator it
=sAllImages
.begin(); it
!= sAllImages
.end(); it
++) {
264 if ( *it
== image
) {
265 sAllImages
.erase(it
);
270 // if in announcement list, pull it out
271 for (std::vector
<ImageLoader
*>::iterator it
=sImagesToNotifyAboutOtherImages
.begin(); it
!= sImagesToNotifyAboutOtherImages
.end(); it
++) {
272 if ( *it
== image
) {
273 sImagesToNotifyAboutOtherImages
.erase(it
);
278 // if in root list, pull it out
279 for (std::vector
<ImageLoader
*>::iterator it
=sImageRoots
.begin(); it
!= sImageRoots
.end(); it
++) {
280 if ( *it
== image
) {
281 sImageRoots
.erase(it
);
287 removeImageFromAllImages(image
->machHeader());
289 #if OLD_GDB_DYLD_INTERFACE
291 removeImageForgdb(image
->machHeader());
292 gdb_dyld_state_changed();
297 static void terminationRecorder(ImageLoader
* image
)
299 sImageFilesNeedingTermination
.push_back(image
);
302 const char* getExecutablePath()
308 void initializeMainExecutable()
310 const int rootCount
= sImageRoots
.size();
311 for(int i
=0; i
< rootCount
; ++i
) {
312 ImageLoader
* image
= sImageRoots
[i
];
313 //fprintf(stderr, "initializeMainExecutable: image = %p\n", image);
314 image
->runInitializers(gLinkContext
);
317 // this does not work???
318 for (std::vector<ImageLoader*>::iterator it=sImageRoots.begin(); it != sImageRoots.end(); it++) {
319 ImageLoader* image = *it;
320 fprintf(stderr, "initializeMainExecutable: image = %p\n", image);
321 // don't know why vector sometimes starts with NULL element???
323 image->runInitializers(gLinkContext);
326 if ( sEnv
.DYLD_PRINT_STATISTICS
)
327 ImageLoaderMachO::printStatistics(sAllImages
.size());
330 bool mainExecutablePrebound()
332 return sMainExecutable
->usablePrebinding(gLinkContext
);
335 ImageLoader
* mainExecutable()
337 return sMainExecutable
;
341 void runTerminators()
343 const unsigned int imageCount
= sImageFilesNeedingTermination
.size();
344 for(unsigned int i
=imageCount
; i
> 0; --i
){
345 ImageLoader
* image
= sImageFilesNeedingTermination
[i
-1];
346 image
->doTermination(gLinkContext
);
348 sImageFilesNeedingTermination
.clear();
353 // Turns a colon separated list of strings
354 // into a NULL terminated array of string
357 static const char** parseColonList(const char* list
)
359 if ( list
[0] == '\0' )
363 for(const char* s
=list
; *s
!= '\0'; ++s
) {
369 const char* start
= list
;
370 char** result
= new char*[colonCount
+2];
371 for(const char* s
=list
; *s
!= '\0'; ++s
) {
374 char* str
= new char[len
+1];
375 strncpy(str
, start
, len
);
378 result
[index
++] = str
;
381 int len
= strlen(start
);
382 char* str
= new char[len
+1];
384 result
[index
++] = str
;
385 result
[index
] = NULL
;
387 return (const char**)result
;
391 * Library path searching is not done for setuid programs
392 * which are not run by the real user. Futher the
393 * evironment varaible for the library path is cleared so
394 * that if this program executes a non-set uid program this
395 * part of the evironment will not be passed along so that
396 * that program also will not have it's libraries searched
399 static bool riskyUser()
401 static bool checked
= false;
402 static bool risky
= false;
404 risky
= ( getuid() != 0 && (getuid() != geteuid() || getgid() != getegid()) );
411 static bool disableIfBadUser(char* rhs
)
413 bool didDisable
= false;
421 static void paths_expand_roots(const char **paths
, const char *key
, const char *val
)
423 // assert(val != NULL);
424 // assert(paths != NULL);
426 size_t keyLen
= strlen(key
);
427 for(int i
=0; paths
[i
] != NULL
; ++i
) {
428 if ( strncmp(paths
[i
], key
, keyLen
) == 0 ) {
429 char* newPath
= new char[strlen(val
) + (strlen(paths
[i
]) - keyLen
) + 1];
430 strcpy(newPath
, val
);
431 strcat(newPath
, &paths
[i
][keyLen
]);
439 static void removePathWithPrefix(const char* paths
[], const char* prefix
)
441 size_t prefixLen
= strlen(prefix
);
442 for(int s
=0,d
=0; (paths
[d
] != NULL
) && (paths
[s
] != NULL
); ++s
, ++d
) {
443 if ( strncmp(paths
[s
], prefix
, prefixLen
) == 0 )
450 static void paths_dump(const char **paths
)
452 // assert(paths != NULL);
453 const char **strs
= paths
;
456 fprintf(stderr
, "\"%s\"\n", *strs
);
463 static void printOptions(const char* argv
[])
466 while ( NULL
!= argv
[i
] ) {
467 fprintf(stderr
, "opt[%i] = \"%s\"\n", i
, argv
[i
]);
472 static void printEnvironmentVariables(const char* envp
[])
474 while ( NULL
!= *envp
) {
475 fprintf(stderr
, "%s\n", *envp
);
482 void processDyldEnvironmentVarible(const char* key
, const char* value
)
484 if ( strcmp(key
, "DYLD_FRAMEWORK_PATH") == 0 ) {
485 if ( !disableIfBadUser((char*)value
) )
486 sEnv
.DYLD_FRAMEWORK_PATH
= parseColonList(value
);
488 else if ( strcmp(key
, "DYLD_FALLBACK_FRAMEWORK_PATH") == 0 ) {
489 if ( !disableIfBadUser((char*)value
) )
490 sEnv
.DYLD_FALLBACK_FRAMEWORK_PATH
= parseColonList(value
);
492 else if ( strcmp(key
, "DYLD_LIBRARY_PATH") == 0 ) {
493 if ( !disableIfBadUser((char*)value
) )
494 sEnv
.DYLD_LIBRARY_PATH
= parseColonList(value
);
496 else if ( strcmp(key
, "DYLD_FALLBACK_LIBRARY_PATH") == 0 ) {
497 if ( !disableIfBadUser((char*)value
) )
498 sEnv
.DYLD_FALLBACK_LIBRARY_PATH
= parseColonList(value
);
500 else if ( (strcmp(key
, "DYLD_ROOT_PATH") == 0) || (strcmp(key
, "DYLD_PATHS_ROOT") == 0) ) {
501 if ( !disableIfBadUser((char*)value
) ) {
502 if ( strcmp(value
, "/") != 0 ) {
503 sEnv
.DYLD_ROOT_PATH
= parseColonList(value
);
504 for (int i
=0; sEnv
.DYLD_ROOT_PATH
[i
] != NULL
; ++i
) {
505 if ( sEnv
.DYLD_ROOT_PATH
[i
][0] != '/' ) {
506 fprintf(stderr
, "dyld: warning DYLD_ROOT_PATH not used because it contains a non-absolute path");
507 sEnv
.DYLD_ROOT_PATH
= NULL
;
513 else if ( strcmp(key
, "DYLD_IMAGE_SUFFIX") == 0 ) {
514 if ( !disableIfBadUser((char*)value
) )
515 gLinkContext
.imageSuffix
= value
;
517 else if ( strcmp(key
, "DYLD_INSERT_LIBRARIES") == 0 ) {
518 if ( !disableIfBadUser((char*)value
) )
519 sEnv
.DYLD_INSERT_LIBRARIES
= parseColonList(value
);
521 else if ( strcmp(key
, "DYLD_DEBUG_TRACE") == 0 ) {
522 fprintf(stderr
, "dyld: warning DYLD_DEBUG_TRACE not supported\n");
524 else if ( strcmp(key
, "DYLD_ERROR_PRINT") == 0 ) {
525 fprintf(stderr
, "dyld: warning DYLD_ERROR_PRINT not supported\n");
527 else if ( strcmp(key
, "DYLD_PRINT_OPTS") == 0 ) {
528 sEnv
.DYLD_PRINT_OPTS
= true;
530 else if ( strcmp(key
, "DYLD_PRINT_ENV") == 0 ) {
531 sEnv
.DYLD_PRINT_ENV
= true;
533 else if ( strcmp(key
, "DYLD_PRINT_LIBRARIES") == 0 ) {
534 sEnv
.DYLD_PRINT_LIBRARIES
= true;
536 else if ( strcmp(key
, "DYLD_PRINT_LIBRARIES_POST_LAUNCH") == 0 ) {
537 sEnv
.DYLD_PRINT_LIBRARIES_POST_LAUNCH
= true;
539 else if ( strcmp(key
, "DYLD_TRACE") == 0 ) {
540 fprintf(stderr
, "dyld: warning DYLD_TRACE not supported\n");
542 else if ( strcmp(key
, "DYLD_EBADEXEC_ONLY") == 0 ) {
543 fprintf(stderr
, "dyld: warning DYLD_EBADEXEC_ONLY not supported\n");
545 else if ( strcmp(key
, "DYLD_BIND_AT_LAUNCH") == 0 ) {
546 sEnv
.DYLD_BIND_AT_LAUNCH
= true;
548 else if ( strcmp(key
, "DYLD_FORCE_FLAT_NAMESPACE") == 0 ) {
549 gLinkContext
.bindFlat
= true;
551 else if ( strcmp(key
, "DYLD_DEAD_LOCK_HANG") == 0 ) {
552 fprintf(stderr
, "dyld: warning DYLD_DEAD_LOCK_HANG not supported\n");
554 else if ( strcmp(key
, "DYLD_ABORT_MULTIPLE_INITS") == 0 ) {
555 fprintf(stderr
, "dyld: warning DYLD_ABORT_MULTIPLE_INITS not supported\n");
557 else if ( strcmp(key
, "DYLD_NEW_LOCAL_SHARED_REGIONS") == 0 ) {
558 gLinkContext
.sharedRegionMode
= ImageLoader::kUsePrivateSharedRegion
;
560 else if ( strcmp(key
, "DYLD_SLIDE_AND_PACK_DYLIBS") == 0 ) {
561 gLinkContext
.slideAndPackDylibs
= true;
563 else if ( strcmp(key
, "DYLD_NO_FIX_PREBINDING") == 0 ) {
564 // since the new dyld never runs fix_prebinding, no need to warn if someone does not want it run
565 //fprintf(stderr, "dyld: warning DYLD_NO_FIX_PREBINDING not supported\n");
567 else if ( strcmp(key
, "DYLD_PREBIND_DEBUG") == 0 ) {
568 gLinkContext
.verbosePrebinding
= true;
570 else if ( strcmp(key
, "DYLD_HINTS_DEBUG") == 0 ) {
571 fprintf(stderr
, "dyld: warning DYLD_HINTS_DEBUG not supported\n");
573 else if ( strcmp(key
, "DYLD_SAMPLE_DEBUG") == 0 ) {
574 fprintf(stderr
, "dyld: warning DYLD_SAMPLE_DEBUG not supported\n");
576 else if ( strcmp(key
, "DYLD_EXECUTABLE_PATH_DEBUG") == 0 ) {
577 fprintf(stderr
, "dyld: warning DYLD_EXECUTABLE_PATH_DEBUG not supported\n");
579 else if ( strcmp(key
, "DYLD_TWO_LEVEL_DEBUG") == 0 ) {
580 fprintf(stderr
, "dyld: warning DYLD_TWO_LEVEL_DEBUG not supported\n");
582 else if ( strcmp(key
, "DYLD_LAZY_INITIALIZERS") == 0 ) {
583 fprintf(stderr
, "dyld: warning DYLD_LAZY_INITIALIZERS not supported\n");
585 else if ( strcmp(key
, "DYLD_PRINT_INITIALIZERS") == 0 ) {
586 gLinkContext
.verboseInit
= true;
588 else if ( strcmp(key
, "DYLD_PRINT_STATISTICS") == 0 ) {
589 sEnv
.DYLD_PRINT_STATISTICS
= true;
591 else if ( strcmp(key
, "DYLD_PRINT_SEGMENTS") == 0 ) {
592 gLinkContext
.verboseMapping
= true;
594 else if ( strcmp(key
, "DYLD_PRINT_BINDINGS") == 0 ) {
595 gLinkContext
.verboseBind
= true;
597 else if ( strcmp(key
, "DYLD_PRINT_REBASINGS") == 0 ) {
598 gLinkContext
.verboseRebase
= true;
600 else if ( strcmp(key
, "DYLD_PRINT_APIS") == 0 ) {
603 else if ( strcmp(key
, "DYLD_PRINT_WARNINGS") == 0 ) {
604 gLinkContext
.verboseWarnings
= true;
606 else if ( strcmp(key
, "DYLD_SHARED_REGION") == 0 ) {
607 if ( strcmp(value
, "private") == 0 ) {
608 gLinkContext
.sharedRegionMode
= ImageLoader::kUsePrivateSharedRegion
;
610 else if ( strcmp(value
, "avoid") == 0 ) {
611 gLinkContext
.sharedRegionMode
= ImageLoader::kDontUseSharedRegion
;
613 else if ( strcmp(value
, "use") == 0 ) {
614 gLinkContext
.sharedRegionMode
= ImageLoader::kUseSharedRegion
;
616 else if ( value
[0] == '\0' ) {
617 gLinkContext
.sharedRegionMode
= ImageLoader::kUseSharedRegion
;
620 fprintf(stderr
, "dyld: warning unknown option to DYLD_SHARED_REGION. Valid options are: use, private, avoid\n");
623 else if ( strcmp(key
, "DYLD_IGNORE_PREBINDING") == 0 ) {
624 if ( strcmp(value
, "all") == 0 ) {
625 gLinkContext
.prebindUsage
= ImageLoader::kUseNoPrebinding
;
627 else if ( strcmp(value
, "app") == 0 ) {
628 gLinkContext
.prebindUsage
= ImageLoader::kUseAllButAppPredbinding
;
630 else if ( strcmp(value
, "nonsplit") == 0 ) {
631 gLinkContext
.prebindUsage
= ImageLoader::kUseSplitSegPrebinding
;
633 else if ( value
[0] == '\0' ) {
634 gLinkContext
.prebindUsage
= ImageLoader::kUseSplitSegPrebinding
;
637 fprintf(stderr
, "dyld: warning unknown option to DYLD_IGNORE_PREBINDING. Valid options are: all, app, nonsplit\n");
641 fprintf(stderr
, "dyld: warning, unknown environment variable: %s\n", key
);
645 static void checkEnvironmentVariables(const char* envp
[])
647 const char* home
= NULL
;
649 for(p
= envp
; *p
!= NULL
; p
++) {
650 const char* keyEqualsValue
= *p
;
651 if ( strncmp(keyEqualsValue
, "DYLD_", 5) == 0 ) {
652 const char* equals
= strchr(keyEqualsValue
, '=');
653 if ( equals
!= NULL
) {
654 const char* value
= &equals
[1];
655 const int keyLen
= equals
-keyEqualsValue
;
657 strncpy(key
, keyEqualsValue
, keyLen
);
659 processDyldEnvironmentVarible(key
, value
);
662 else if ( strncmp(keyEqualsValue
, "HOME=", 5) == 0 ) {
663 home
= &keyEqualsValue
[5];
665 else if ( strncmp(keyEqualsValue
, "LD_LIBRARY_PATH=", 16) == 0 ) {
666 const char* path
= &keyEqualsValue
[16];
667 if ( !disableIfBadUser((char*)path
) )
668 sEnv
.LD_LIBRARY_PATH
= parseColonList(path
);
672 // default value for DYLD_FALLBACK_FRAMEWORK_PATH, if not set in environment
673 if ( sEnv
.DYLD_FALLBACK_FRAMEWORK_PATH
== NULL
) {
674 const char** paths
= sFrameworkFallbackPaths
;
675 if ( home
!= NULL
) {
677 removePathWithPrefix(paths
, "$HOME");
679 paths_expand_roots(paths
, "$HOME", home
);
681 sEnv
.DYLD_FALLBACK_FRAMEWORK_PATH
= paths
;
684 // default value for DYLD_FALLBACK_LIBRARY_PATH, if not set in environment
685 if ( sEnv
.DYLD_FALLBACK_LIBRARY_PATH
== NULL
) {
686 const char** paths
= sLibraryFallbackPaths
;
687 if ( home
!= NULL
) {
689 removePathWithPrefix(paths
, "$HOME");
691 paths_expand_roots(paths
, "$HOME", home
);
693 sEnv
.DYLD_FALLBACK_LIBRARY_PATH
= paths
;
698 static void getHostInfo()
701 struct host_basic_info info
;
702 mach_msg_type_number_t count
= HOST_BASIC_INFO_COUNT
;
703 mach_port_t hostPort
= mach_host_self();
704 kern_return_t result
= host_info(hostPort
, HOST_BASIC_INFO
, (host_info_t
)&info
, &count
);
705 mach_port_deallocate(mach_task_self(), hostPort
);
706 if ( result
!= KERN_SUCCESS
)
707 throw "host_info() failed";
709 sHostCPU
= info
.cpu_type
;
710 sHostCPUsubtype
= info
.cpu_subtype
;
713 size_t valSize
= sizeof(sHostCPU
);
714 if (sysctlbyname ("hw.cputype", &sHostCPU
, &valSize
, NULL
, 0) != 0)
715 throw "sysctlbyname(hw.cputype) failed";
716 valSize
= sizeof(sHostCPUsubtype
);
717 if (sysctlbyname ("hw.cpusubtype", &sHostCPUsubtype
, &valSize
, NULL
, 0) != 0)
718 throw "sysctlbyname(hw.cpusubtype) failed";
721 bool validImage(ImageLoader
* possibleImage
)
723 const unsigned int imageCount
= sAllImages
.size();
724 for(unsigned int i
=0; i
< imageCount
; ++i
) {
725 if ( possibleImage
== sAllImages
[i
] ) {
732 uint32_t getImageCount()
734 return sAllImages
.size();
737 ImageLoader
* getIndexedImage(unsigned int index
)
739 if ( index
< sAllImages
.size() )
740 return sAllImages
[index
];
745 ImageLoader
* findImageByMachHeader(const struct mach_header
* target
)
747 const unsigned int imageCount
= sAllImages
.size();
748 for(unsigned int i
=0; i
< imageCount
; ++i
) {
749 ImageLoader
* anImage
= sAllImages
[i
];
750 if ( anImage
->machHeader() == target
)
757 ImageLoader
* findImageContainingAddress(const void* addr
)
760 static int cacheHit
= 0;
761 static int cacheMiss
= 0;
762 static int cacheNotMacho
= 0;
763 if ( ((cacheHit
+cacheMiss
+cacheNotMacho
) % 100) == 0 )
764 fprintf(stderr
, "findImageContainingAddress(): cache hit = %d, miss = %d, unknown = %d\n", cacheHit
, cacheMiss
, cacheNotMacho
);
766 // first look in image where last address was found rdar://problem/3685517
767 if ( (sLastImageByAddressCache
!= NULL
) && sLastImageByAddressCache
->containsAddress(addr
) ) {
771 return sLastImageByAddressCache
;
773 // do exhastive search
774 // todo: consider maintaining a list sorted by address ranges and do a binary search on that
775 const unsigned int imageCount
= sAllImages
.size();
776 for(unsigned int i
=0; i
< imageCount
; ++i
) {
777 ImageLoader
* anImage
= sAllImages
[i
];
778 if ( anImage
->containsAddress(addr
) ) {
779 sLastImageByAddressCache
= anImage
;
793 void forEachImageDo( void (*callback
)(ImageLoader
*, void* userData
), void* userData
)
795 const unsigned int imageCount
= sAllImages
.size();
796 for(unsigned int i
=0; i
< imageCount
; ++i
) {
797 ImageLoader
* anImage
= sAllImages
[i
];
798 (*callback
)(anImage
, userData
);
802 ImageLoader
* findLoadedImage(const struct stat
& stat_buf
)
804 const unsigned int imageCount
= sAllImages
.size();
805 for(unsigned int i
=0; i
< imageCount
; ++i
){
806 ImageLoader
* anImage
= sAllImages
[i
];
807 if ( anImage
->statMatch(stat_buf
) )
813 // based on ANSI-C strstr()
814 static const char* strrstr(const char* str
, const char* sub
)
816 const int sublen
= strlen(sub
);
817 for(const char* p
= &str
[strlen(str
)]; p
!= str
; --p
) {
818 if ( strncmp(p
, sub
, sublen
) == 0 )
826 // Find framework path
828 // /path/foo.framework/foo => foo.framework/foo
829 // /path/foo.framework/Versions/A/foo => foo.framework/Versions/A/foo
830 // /path/foo.framework/Frameworks/bar.framework/bar => bar.framework/bar
831 // /path/foo.framework/Libraries/bar.dylb => NULL
832 // /path/foo.framework/bar => NULL
834 // Returns NULL if not a framework path
836 static const char* getFrameworkPartialPath(const char* path
)
838 const char* dirDot
= strrstr(path
, ".framework/");
839 if ( dirDot
!= NULL
) {
840 const char* dirStart
= dirDot
;
841 for ( ; dirStart
>= path
; --dirStart
) {
842 if ( (*dirStart
== '/') || (dirStart
== path
) ) {
843 const char* frameworkStart
= &dirStart
[1];
844 if ( dirStart
== path
)
846 int len
= dirDot
- frameworkStart
;
847 char framework
[len
+1];
848 strncpy(framework
, frameworkStart
, len
);
849 framework
[len
] = '\0';
850 const char* leaf
= strrchr(path
, '/');
851 if ( leaf
!= NULL
) {
852 if ( strcmp(framework
, &leaf
[1]) == 0 ) {
853 return frameworkStart
;
855 if ( gLinkContext
.imageSuffix
!= NULL
) {
856 // some debug frameworks have install names that end in _debug
857 if ( strncmp(framework
, &leaf
[1], len
) == 0 ) {
858 if ( strcmp( gLinkContext
.imageSuffix
, &leaf
[len
+1]) == 0 )
859 return frameworkStart
;
870 static const char* getLibraryLeafName(const char* path
)
872 const char* start
= strrchr(path
, '/');
881 const cpu_subtype_t CPU_SUBTYPE_END_OF_LIST
= -1;
885 // A fat file may contain multiple sub-images for the same CPU type.
886 // In that case, dyld picks which sub-image to use by scanning a table
887 // of preferred cpu-sub-types for the running cpu.
889 // There is one row in the table for each cpu-sub-type on which dyld might run.
890 // The first entry in a row is that cpu-sub-type. It is followed by all
891 // cpu-sub-types that can run on that cpu, if preferred order. Each row ends with
892 // a "SUBTYPE_ALL" (to denote that images written to run on any cpu-sub-type are usable),
893 // followed by one or more CPU_SUBTYPE_END_OF_LIST to pad out this row.
898 // 32-bit PowerPC sub-type lists
900 const int kPPC_RowCount
= 4;
901 static const cpu_subtype_t kPPC32
[kPPC_RowCount
][6] = {
902 // G5 can run any code
903 { CPU_SUBTYPE_POWERPC_970
, CPU_SUBTYPE_POWERPC_7450
, CPU_SUBTYPE_POWERPC_7400
, CPU_SUBTYPE_POWERPC_750
, CPU_SUBTYPE_POWERPC_ALL
, CPU_SUBTYPE_END_OF_LIST
},
905 // G4 can run all but G5 code
906 { CPU_SUBTYPE_POWERPC_7450
, CPU_SUBTYPE_POWERPC_7400
, CPU_SUBTYPE_POWERPC_750
, CPU_SUBTYPE_POWERPC_ALL
, CPU_SUBTYPE_END_OF_LIST
, CPU_SUBTYPE_END_OF_LIST
},
907 { CPU_SUBTYPE_POWERPC_7400
, CPU_SUBTYPE_POWERPC_7450
, CPU_SUBTYPE_POWERPC_750
, CPU_SUBTYPE_POWERPC_ALL
, CPU_SUBTYPE_END_OF_LIST
, CPU_SUBTYPE_END_OF_LIST
},
909 // G3 cannot run G4 or G5 code
910 { CPU_SUBTYPE_POWERPC_750
, CPU_SUBTYPE_POWERPC_ALL
, CPU_SUBTYPE_END_OF_LIST
, CPU_SUBTYPE_END_OF_LIST
, CPU_SUBTYPE_END_OF_LIST
, CPU_SUBTYPE_END_OF_LIST
}
915 // 64-bit PowerPC sub-type lists
917 const int kPPC64_RowCount
= 1;
918 static const cpu_subtype_t kPPC64
[kPPC64_RowCount
][3] = {
919 // G5 can run any 64-bit code
920 { CPU_SUBTYPE_POWERPC_970
, CPU_SUBTYPE_POWERPC_ALL
, CPU_SUBTYPE_END_OF_LIST
},
926 // 32-bit x86 sub-type lists
932 // scan the tables above to find the cpu-sub-type-list for this machine
933 static const cpu_subtype_t
* findCPUSubtypeList(cpu_type_t cpu
, cpu_subtype_t subtype
)
936 case CPU_TYPE_POWERPC
:
937 for (int i
=0; i
< kPPC_RowCount
; ++i
) {
938 if ( kPPC32
[i
][0] == subtype
)
942 case CPU_TYPE_POWERPC64
:
943 for (int i
=0; i
< kPPC64_RowCount
; ++i
) {
944 if ( kPPC64
[i
][0] == subtype
)
958 // scan fat table-of-contents for best most preferred subtype
959 static bool fatFindBestFromOrderedList(cpu_type_t cpu
, const cpu_subtype_t list
[], const fat_header
* fh
, uint64_t* offset
, uint64_t* len
)
961 const fat_arch
* const archs
= (fat_arch
*)(((char*)fh
)+sizeof(fat_header
));
962 for (uint32_t subTypeIndex
=0; list
[subTypeIndex
] != CPU_SUBTYPE_END_OF_LIST
; ++subTypeIndex
) {
963 for(uint32_t fatIndex
=0; fatIndex
< OSSwapBigToHostInt32(fh
->nfat_arch
); ++fatIndex
) {
964 if ( ((cpu_type_t
)OSSwapBigToHostInt32(archs
[fatIndex
].cputype
) == cpu
)
965 && (list
[subTypeIndex
] == archs
[fatIndex
].cpusubtype
) ) {
966 *offset
= OSSwapBigToHostInt32(archs
[fatIndex
].offset
);
967 *len
= OSSwapBigToHostInt32(archs
[fatIndex
].size
);
975 // scan fat table-of-contents for exact match of cpu and cpu-sub-type
976 static bool fatFindExactMatch(cpu_type_t cpu
, cpu_subtype_t subtype
, const fat_header
* fh
, uint64_t* offset
, uint64_t* len
)
978 const fat_arch
* archs
= (fat_arch
*)(((char*)fh
)+sizeof(fat_header
));
979 for(uint32_t i
=0; i
< OSSwapBigToHostInt32(fh
->nfat_arch
); ++i
) {
980 if ( ((cpu_type_t
)OSSwapBigToHostInt32(archs
[i
].cputype
) == cpu
)
981 && ((cpu_subtype_t
)OSSwapBigToHostInt32(archs
[i
].cpusubtype
) == subtype
) ) {
982 *offset
= OSSwapBigToHostInt32(archs
[i
].offset
);
983 *len
= OSSwapBigToHostInt32(archs
[i
].size
);
990 // scan fat table-of-contents for image with matching cpu-type and runs-on-all-sub-types
991 static bool fatFindRunsOnAllCPUs(cpu_type_t cpu
, const fat_header
* fh
, uint64_t* offset
, uint64_t* len
)
993 const fat_arch
* archs
= (fat_arch
*)(((char*)fh
)+sizeof(fat_header
));
994 for(uint32_t i
=0; i
< OSSwapBigToHostInt32(fh
->nfat_arch
); ++i
) {
995 if ( (cpu_type_t
)OSSwapBigToHostInt32(archs
[i
].cputype
) == cpu
) {
997 case CPU_TYPE_POWERPC
:
998 case CPU_TYPE_POWERPC64
:
999 if ( (cpu_subtype_t
)OSSwapBigToHostInt32(archs
[i
].cpusubtype
) == CPU_SUBTYPE_POWERPC_ALL
) {
1000 *offset
= OSSwapBigToHostInt32(archs
[i
].offset
);
1001 *len
= OSSwapBigToHostInt32(archs
[i
].size
);
1006 if ( (cpu_subtype_t
)OSSwapBigToHostInt32(archs
[i
].cpusubtype
) == CPU_SUBTYPE_I386_ALL
) {
1007 *offset
= OSSwapBigToHostInt32(archs
[i
].offset
);
1008 *len
= OSSwapBigToHostInt32(archs
[i
].size
);
1020 // A fat file may contain multiple sub-images for the same cpu-type,
1021 // each optimized for a different cpu-sub-type (e.g G3 or G5).
1022 // This routine picks the optimal sub-image.
1024 static bool fatFindBest(const fat_header
* fh
, uint64_t* offset
, uint64_t* len
)
1026 // assume all dylibs loaded must have same cpu type as main executable
1027 const cpu_type_t cpu
= sMainExecutableMachHeader
->cputype
;
1029 // We only know the subtype to use if the main executable cpu type matches the host
1030 if ( (cpu
& CPU_TYPE_MASK
) == sHostCPU
) {
1031 // get preference ordered list of subtypes
1032 const cpu_subtype_t
* subTypePreferenceList
= findCPUSubtypeList(cpu
, sHostCPUsubtype
);
1034 // use ordered list to find best sub-image in fat file
1035 if ( subTypePreferenceList
!= NULL
)
1036 return fatFindBestFromOrderedList(cpu
, subTypePreferenceList
, fh
, offset
, len
);
1038 // if running cpu is not in list, try for an exact match
1039 if ( fatFindExactMatch(cpu
, sHostCPUsubtype
, fh
, offset
, len
) )
1043 // running on an uknown cpu, can only load generic code
1044 return fatFindRunsOnAllCPUs(cpu
, fh
, offset
, len
);
1050 // This is used to validate if a non-fat (aka thin or raw) mach-o file can be used
1051 // on the current processor. It is deemed compatible if any of the following are true:
1052 // 1) mach_header subtype is in list of compatible subtypes for running processor
1053 // 2) mach_header subtype is same as running processor subtype
1054 // 3) mach_header subtype runs on all processor variants
1057 bool isCompatibleMachO(const uint8_t* firstPage
)
1059 const mach_header
* mh
= (mach_header
*)firstPage
;
1060 if ( mh
->magic
== sMainExecutableMachHeader
->magic
) {
1061 if ( mh
->cputype
== sMainExecutableMachHeader
->cputype
) {
1062 if ( (mh
->cputype
& CPU_TYPE_MASK
) == sHostCPU
) {
1063 // get preference ordered list of subtypes that this machine can use
1064 const cpu_subtype_t
* subTypePreferenceList
= findCPUSubtypeList(mh
->cputype
, sHostCPUsubtype
);
1065 if ( subTypePreferenceList
!= NULL
) {
1066 // if image's subtype is in the list, it is compatible
1067 for (const cpu_subtype_t
* p
= subTypePreferenceList
; *p
!= CPU_SUBTYPE_END_OF_LIST
; ++p
) {
1068 if ( *p
== mh
->cpusubtype
)
1071 // have list and not in list, so not compatible
1072 throw "incompatible cpu-subtype";
1074 // unknown cpu sub-type, but if exact match for current subtype then ok to use
1075 if ( mh
->cpusubtype
== sHostCPUsubtype
)
1079 // cpu unknown, so don't know if subtype is compatible
1080 // only load _ALL variant
1081 switch (mh
->cputype
) {
1082 case CPU_TYPE_POWERPC
:
1083 case CPU_TYPE_POWERPC64
:
1084 if ( mh
->cpusubtype
== CPU_SUBTYPE_POWERPC_ALL
)
1088 if ( mh
->cpusubtype
== CPU_SUBTYPE_I386_ALL
)
1098 // The kernel maps in main executable before dyld gets control. We need to
1099 // make an ImageLoader* for the already mapped in main executable.
1100 static ImageLoader
* instantiateFromLoadedImage(const struct mach_header
* mh
, const char* path
)
1102 // try mach-o loader
1103 if ( isCompatibleMachO((const uint8_t*)mh
) ) {
1104 ImageLoader
* image
= new ImageLoaderMachO(path
, mh
, 0, gLinkContext
);
1115 // map in file and instantiate an ImageLoader
1116 static ImageLoader
* loadPhase6(int fd
, struct stat
& stat_buf
, const char* path
, const LoadContext
& context
)
1118 //fprintf(stderr, "%s(%s)\n", __func__ , path);
1119 uint64_t fileOffset
= 0;
1120 uint64_t fileLength
= stat_buf
.st_size
;
1122 if ( *((uint32_t*)((char*)(&stat_buf
)+0x60)) == 0xFEFEFEFE )
1123 fileLength
= *((uint64_t*)((char*)(&stat_buf
)+0x30)); // HACK work around for kernel stat bug rdar://problem/3845883
1126 // validate it is a file (not directory)
1127 if ( (stat_buf
.st_mode
& S_IFMT
) != S_IFREG
)
1131 if ( fileLength
< 4096 ) {
1132 throw "file to short";
1135 uint8_t firstPage
[4096];
1136 pread(fd
, firstPage
, 4096,0);
1138 // if fat wrapper, find usable sub-file
1139 const fat_header
* fileStartAsFat
= (fat_header
*)firstPage
;
1140 if ( fileStartAsFat
->magic
== OSSwapBigToHostInt32(FAT_MAGIC
) ) {
1141 if ( fatFindBest(fileStartAsFat
, &fileOffset
, &fileLength
) ) {
1142 pread(fd
, firstPage
, 4096, fileOffset
);
1145 throw "no matching architecture in fat wrapper";
1149 // try mach-o loader
1150 if ( isCompatibleMachO(firstPage
) ) {
1151 char realFilePath
[PATH_MAX
];
1152 if ( gLinkContext
.slideAndPackDylibs
) {
1153 // when prebinding, we always want to track the real path of images
1154 if ( realpath(path
, realFilePath
) != NULL
)
1155 path
= realFilePath
;
1158 // instantiate an image
1159 ImageLoader
* image
= new ImageLoaderMachO(path
, fd
, firstPage
, fileOffset
, fileLength
, stat_buf
, gLinkContext
);
1161 // now sanity check that this loaded image does not have the same install path as any existing image
1162 const char* loadedImageInstallPath
= image
->getInstallPath();
1163 if ( image
->isDylib() && (loadedImageInstallPath
!= NULL
) && (loadedImageInstallPath
[0] == '/') ) {
1164 for (std::vector
<ImageLoader
*>::iterator it
=sAllImages
.begin(); it
!= sAllImages
.end(); it
++) {
1165 ImageLoader
* anImage
= *it
;
1166 const char* installPath
= anImage
->getInstallPath();
1167 if ( installPath
!= NULL
) {
1168 if ( strcmp(loadedImageInstallPath
, installPath
) == 0 ) {
1169 //fprintf(stderr, "duplicate(%s) => %p\n", installPath, anImage);
1177 // some API's restrict what they can load
1178 if ( context
.mustBeBundle
&& !image
->isBundle() )
1179 throw "not a bundle";
1180 if ( context
.mustBeDylib
&& !image
->isDylib() )
1181 throw "not a dylib";
1183 // don't add bundles to global list, they can be loaded but not linked. When linked it will be added to list
1184 if ( ! image
->isBundle() )
1190 // try other file formats...
1192 throwf("unknown file type, first eight bytes: 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X",
1193 firstPage
[0], firstPage
[1], firstPage
[2], firstPage
[3], firstPage
[4], firstPage
[5], firstPage
[6],firstPage
[7]);
1198 static ImageLoader
* loadPhase5open(const char* path
, const LoadContext
& context
, std::vector
<const char*>* exceptions
)
1200 //fprintf(stdout, "%s(%s)\n", __func__, path);
1201 ImageLoader
* image
= NULL
;
1203 // open file (automagically closed when this function exits)
1204 FileOpener
file(path
);
1206 //fprintf(stderr, "open(%s) => %d\n", path, file.getFileDescriptor() );
1208 if ( file
.getFileDescriptor() == -1 )
1211 struct stat stat_buf
;
1213 memset(&stat_buf
, 254, sizeof(struct stat
)); // hack until rdar://problem/3845883 is fixed
1215 if ( fstat(file
.getFileDescriptor(), &stat_buf
) == -1)
1218 // in case image was renamed or found via symlinks, check for inode match
1219 image
= findLoadedImage(stat_buf
);
1220 if ( image
!= NULL
)
1223 // needed to implement NSADDIMAGE_OPTION_RETURN_ONLY_IF_LOADED
1224 if ( context
.dontLoad
)
1228 return loadPhase6(file
.getFileDescriptor(), stat_buf
, path
, context
);
1230 catch (const char* msg
) {
1231 char* newMsg
= new char[strlen(msg
) + strlen(path
) + 8];
1232 sprintf(newMsg
, "%s: %s", path
, msg
);
1233 exceptions
->push_back(newMsg
);
1238 // look for path match with existing loaded images
1239 static ImageLoader
* loadPhase5check(const char* path
, const LoadContext
& context
)
1241 //fprintf(stderr, "%s(%s)\n", __func__ , path);
1242 // search path against load-path and install-path of all already loaded images
1243 uint32_t hash
= ImageLoader::hash(path
);
1244 for (std::vector
<ImageLoader
*>::iterator it
=sAllImages
.begin(); it
!= sAllImages
.end(); it
++) {
1245 ImageLoader
* anImage
= *it
;
1246 // check has first to cut down on strcmp calls
1247 if ( anImage
->getPathHash() == hash
)
1248 if ( strcmp(path
, anImage
->getPath()) == 0 ) {
1249 // if we are looking for a dylib don't return something else
1250 if ( !context
.mustBeDylib
|| anImage
->isDylib() )
1253 if ( context
.matchByInstallName
|| anImage
->matchInstallPath() ) {
1254 const char* installPath
= anImage
->getInstallPath();
1255 if ( installPath
!= NULL
) {
1256 if ( strcmp(path
, installPath
) == 0 ) {
1257 // if we are looking for a dylib don't return something else
1258 if ( !context
.mustBeDylib
|| anImage
->isDylib() )
1265 //fprintf(stderr, "check(%s) => NULL\n", path);
1270 // open or check existing
1271 static ImageLoader
* loadPhase5(const char* path
, const LoadContext
& context
, std::vector
<const char*>* exceptions
)
1273 //fprintf(stderr, "%s(%s)\n", __func__ , path);
1274 if ( exceptions
!= NULL
)
1275 return loadPhase5open(path
, context
, exceptions
);
1277 return loadPhase5check(path
, context
);
1280 // try with and without image suffix
1281 static ImageLoader
* loadPhase4(const char* path
, const LoadContext
& context
, std::vector
<const char*>* exceptions
)
1283 //fprintf(stderr, "%s(%s)\n", __func__ , path);
1284 ImageLoader
* image
= NULL
;
1285 if ( gLinkContext
.imageSuffix
!= NULL
) {
1286 char pathWithSuffix
[strlen(path
)+strlen( gLinkContext
.imageSuffix
)+2];
1287 ImageLoader::addSuffix(path
, gLinkContext
.imageSuffix
, pathWithSuffix
);
1288 image
= loadPhase5(pathWithSuffix
, context
, exceptions
);
1290 if ( image
== NULL
)
1291 image
= loadPhase5(path
, context
, exceptions
);
1296 // expand @ variables
1297 static ImageLoader
* loadPhase3(const char* path
, const LoadContext
& context
, std::vector
<const char*>* exceptions
)
1299 //fprintf(stderr, "%s(%s)\n", __func__ , path);
1300 ImageLoader
* image
= NULL
;
1301 if ( strncmp(path
, "@executable_path/", 17) == 0 ) {
1302 // handle @executable_path path prefix
1303 const char* executablePath
= sExecPath
;
1304 char newPath
[strlen(executablePath
) + strlen(path
)];
1305 strcpy(newPath
, executablePath
);
1306 char* addPoint
= strrchr(newPath
,'/');
1307 if ( addPoint
!= NULL
)
1308 strcpy(&addPoint
[1], &path
[17]);
1310 strcpy(newPath
, &path
[17]);
1311 image
= loadPhase4(newPath
, context
, exceptions
);
1312 if ( image
!= NULL
)
1315 // perhaps main executable path is a sym link, find realpath and retry
1316 char resolvedPath
[PATH_MAX
];
1317 if ( realpath(sExecPath
, resolvedPath
) != NULL
) {
1318 char newRealPath
[strlen(resolvedPath
) + strlen(path
)];
1319 strcpy(newRealPath
, resolvedPath
);
1320 char* addPoint
= strrchr(newRealPath
,'/');
1321 if ( addPoint
!= NULL
)
1322 strcpy(&addPoint
[1], &path
[17]);
1324 strcpy(newRealPath
, &path
[17]);
1325 image
= loadPhase4(newRealPath
, context
, exceptions
);
1326 if ( image
!= NULL
)
1330 else if ( (strncmp(path
, "@loader_path/", 13) == 0) && (context
.origin
!= NULL
) ) {
1331 // handle @loader_path path prefix
1332 char newPath
[strlen(context
.origin
) + strlen(path
)];
1333 strcpy(newPath
, context
.origin
);
1334 char* addPoint
= strrchr(newPath
,'/');
1335 if ( addPoint
!= NULL
)
1336 strcpy(&addPoint
[1], &path
[13]);
1338 strcpy(newPath
, &path
[13]);
1339 image
= loadPhase4(newPath
, context
, exceptions
);
1340 if ( image
!= NULL
)
1343 // perhaps loader path is a sym link, find realpath and retry
1344 char resolvedPath
[PATH_MAX
];
1345 if ( realpath(context
.origin
, resolvedPath
) != NULL
) {
1346 char newRealPath
[strlen(resolvedPath
) + strlen(path
)];
1347 strcpy(newRealPath
, resolvedPath
);
1348 char* addPoint
= strrchr(newRealPath
,'/');
1349 if ( addPoint
!= NULL
)
1350 strcpy(&addPoint
[1], &path
[13]);
1352 strcpy(newRealPath
, &path
[13]);
1353 image
= loadPhase4(newRealPath
, context
, exceptions
);
1354 if ( image
!= NULL
)
1359 return loadPhase4(path
, context
, exceptions
);
1364 static ImageLoader
* loadPhase2(const char* path
, const LoadContext
& context
,
1365 const char* const frameworkPaths
[], const char* const libraryPaths
[],
1366 std::vector
<const char*>* exceptions
)
1368 //fprintf(stderr, "%s(%s)\n", __func__ , path);
1369 ImageLoader
* image
= NULL
;
1370 const char* frameworkPartialPath
= getFrameworkPartialPath(path
);
1371 if ( frameworkPaths
!= NULL
) {
1372 if ( frameworkPartialPath
!= NULL
) {
1373 const int frameworkPartialPathLen
= strlen(frameworkPartialPath
);
1374 for(const char* const* fp
= frameworkPaths
; *fp
!= NULL
; ++fp
) {
1375 char npath
[strlen(*fp
)+frameworkPartialPathLen
+8];
1378 strcat(npath
, frameworkPartialPath
);
1379 //fprintf(stderr, "dyld: fallback framework path used: %s() -> loadPhase4(\"%s\", ...)\n", __func__, npath);
1380 image
= loadPhase4(npath
, context
, exceptions
);
1381 if ( image
!= NULL
)
1386 if ( libraryPaths
!= NULL
) {
1387 const char* libraryLeafName
= getLibraryLeafName(path
);
1388 const int libraryLeafNameLen
= strlen(libraryLeafName
);
1389 for(const char* const* lp
= libraryPaths
; *lp
!= NULL
; ++lp
) {
1390 char libpath
[strlen(*lp
)+libraryLeafNameLen
+8];
1391 strcpy(libpath
, *lp
);
1392 strcat(libpath
, "/");
1393 strcat(libpath
, libraryLeafName
);
1394 //fprintf(stderr, "dyld: fallback library path used: %s() -> loadPhase4(\"%s\", ...)\n", __func__, libpath);
1395 image
= loadPhase4(libpath
, context
, exceptions
);
1396 if ( image
!= NULL
)
1403 // try search overrides and fallbacks
1404 static ImageLoader
* loadPhase1(const char* path
, const LoadContext
& context
, std::vector
<const char*>* exceptions
)
1406 //fprintf(stderr, "%s(%s)\n", __func__ , path);
1407 ImageLoader
* image
= NULL
;
1409 // handle LD_LIBRARY_PATH environment variables that force searching
1410 if ( context
.useLdLibraryPath
&& (sEnv
.LD_LIBRARY_PATH
!= NULL
) ) {
1411 image
= loadPhase2(path
, context
, NULL
, sEnv
.LD_LIBRARY_PATH
, exceptions
);
1412 if ( image
!= NULL
)
1416 // handle DYLD_ environment variables that force searching
1417 if ( context
.useSearchPaths
&& ((sEnv
.DYLD_FRAMEWORK_PATH
!= NULL
) || (sEnv
.DYLD_LIBRARY_PATH
!= NULL
)) ) {
1418 image
= loadPhase2(path
, context
, sEnv
.DYLD_FRAMEWORK_PATH
, sEnv
.DYLD_LIBRARY_PATH
, exceptions
);
1419 if ( image
!= NULL
)
1424 image
= loadPhase3(path
, context
, exceptions
);
1425 if ( image
!= NULL
)
1428 // try fallback paths
1429 if ( (sEnv
.DYLD_FALLBACK_FRAMEWORK_PATH
!= NULL
) || (sEnv
.DYLD_FALLBACK_LIBRARY_PATH
!= NULL
) ) {
1430 image
= loadPhase2(path
, context
, sEnv
.DYLD_FALLBACK_FRAMEWORK_PATH
, sEnv
.DYLD_FALLBACK_LIBRARY_PATH
, exceptions
);
1431 if ( image
!= NULL
)
1438 // try root substitutions
1439 static ImageLoader
* loadPhase0(const char* path
, const LoadContext
& context
, std::vector
<const char*>* exceptions
)
1441 //fprintf(stderr, "%s(%s)\n", __func__ , path);
1443 // handle DYLD_ROOT_PATH which forces absolute paths to use a new root
1444 if ( (sEnv
.DYLD_ROOT_PATH
!= NULL
) && (path
[0] == '/') ) {
1445 for(const char* const* rootPath
= sEnv
.DYLD_ROOT_PATH
; *rootPath
!= NULL
; ++rootPath
) {
1446 char newPath
[strlen(*rootPath
) + strlen(path
)+2];
1447 strcpy(newPath
, *rootPath
);
1448 strcat(newPath
, path
);
1449 ImageLoader
* image
= loadPhase1(newPath
, context
, exceptions
);
1450 if ( image
!= NULL
)
1456 return loadPhase1(path
, context
, exceptions
);
1460 // Given all the DYLD_ environment variables, the general case for loading libraries
1461 // is that any given path expands into a list of possible locations to load. We
1462 // also must take care to ensure two copies of the "same" library are never loaded.
1464 // The algorithm used here is that there is a separate function for each "phase" of the
1465 // path expansion. Each phase function calls the next phase with each possible expansion
1466 // of that phase. The result is the last phase is called with all possible paths.
1468 // To catch duplicates the algorithm is run twice. The first time, the last phase checks
1469 // the path against all loaded images. The second time, the last phase calls open() on
1470 // the path. Either time, if an image is found, the phases all unwind without checking
1473 ImageLoader
* load(const char* path
, const LoadContext
& context
)
1475 //fprintf(stderr, "%s(%s)\n", __func__ , path);
1476 char realPath
[PATH_MAX
];
1477 // when DYLD_IMAGE_SUFFIX is in used, do a realpath(), otherwise a load of "Foo.framework/Foo" will not match
1478 if ( context
.useSearchPaths
&& ( gLinkContext
.imageSuffix
!= NULL
) ) {
1479 if ( realpath(path
, realPath
) != NULL
)
1483 // try all path permutations and check against existing loaded images
1484 ImageLoader
* image
= loadPhase0(path
, context
, NULL
);
1485 if ( image
!= NULL
)
1488 // try all path permutations and try open() until first sucesss
1489 std::vector
<const char*> exceptions
;
1490 image
= loadPhase0(path
, context
, &exceptions
);
1491 if ( image
!= NULL
)
1493 else if ( context
.dontLoad
)
1495 else if ( exceptions
.size() == 0 )
1496 throw "image not found";
1498 const char* msgStart
= "no suitable image found. Did find:";
1499 const char* delim
= "\n\t";
1500 size_t allsizes
= strlen(msgStart
)+8;
1501 for (unsigned int i
=0; i
< exceptions
.size(); ++i
)
1502 allsizes
+= (strlen(exceptions
[i
]) + strlen(delim
));
1503 char* fullMsg
= new char[allsizes
];
1504 strcpy(fullMsg
, msgStart
);
1505 for (unsigned int i
=0; i
< exceptions
.size(); ++i
) {
1506 strcat(fullMsg
, delim
);
1507 strcat(fullMsg
, exceptions
[i
]);
1509 throw (const char*)fullMsg
;
1516 // create when NSLinkModule is called for a second time on a bundle
1517 ImageLoader
* cloneImage(ImageLoader
* image
)
1519 const uint64_t offsetInFat
= image
->getOffsetInFatFile();
1521 // open file (automagically closed when this function exits)
1522 FileOpener
file(image
->getPath());
1524 struct stat stat_buf
;
1526 memset(&stat_buf
, 254, sizeof(struct stat
)); // hack until rdar://problem/3845883 is fixed
1528 if ( fstat(file
.getFileDescriptor(), &stat_buf
) == -1)
1531 // read first page of file
1532 uint8_t firstPage
[4096];
1533 pread(file
.getFileDescriptor(), firstPage
, 4096, offsetInFat
);
1535 // fat length is only used for sanity checking, since this image was already loaded once, just use upper bound
1536 uint64_t lenInFat
= stat_buf
.st_size
- offsetInFat
;
1538 // try mach-o loader
1539 if ( isCompatibleMachO(firstPage
) ) {
1540 ImageLoader
* clone
= new ImageLoaderMachO(image
->getPath(), file
.getFileDescriptor(), firstPage
, offsetInFat
, lenInFat
, stat_buf
, gLinkContext
);
1541 // don't add bundles to global list, they can be loaded but not linked. When linked it will be added to list
1542 if ( ! image
->isBundle() )
1547 // try other file formats...
1548 throw "can't clone image";
1552 ImageLoader
* loadFromMemory(const uint8_t* mem
, uint64_t len
, const char* moduleName
)
1554 // try mach-o each loader
1555 if ( isCompatibleMachO(mem
) ) {
1556 ImageLoader
* image
= new ImageLoaderMachO(moduleName
, (mach_header
*)mem
, len
, gLinkContext
);
1557 // don't add bundles to global list, they can be loaded but not linked. When linked it will be added to list
1558 if ( ! image
->isBundle() )
1563 // try other file formats...
1565 throwf("unknown file type, first eight bytes: 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X",
1566 mem
[0], mem
[1], mem
[2], mem
[3], mem
[4], mem
[5], mem
[6],mem
[7]);
1570 void registerAddCallback(ImageCallback func
)
1572 // now add to list to get notified when any more images are added
1573 sAddImageCallbacks
.push_back(func
);
1575 // call callback with all existing images, starting at roots
1576 const int rootCount
= sImageRoots
.size();
1577 for(int i
=0; i
< rootCount
; ++i
) {
1578 ImageLoader
* image
= sImageRoots
[i
];
1579 image
->runNotification(gLinkContext
, sAddImageCallbacks
.size());
1582 // for (std::vector<ImageLoader*>::iterator it=sImageRoots.begin(); it != sImageRoots.end(); it++) {
1583 // ImageLoader* image = *it;
1584 // image->runNotification(gLinkContext, sAddImageCallbacks.size());
1588 void registerRemoveCallback(ImageCallback func
)
1590 sRemoveImageCallbacks
.push_back(func
);
1593 void clearErrorMessage()
1595 error_string
[0] = '\0';
1598 void setErrorMessage(const char* message
)
1600 // save off error message in global buffer for CrashReporter to find
1601 strncpy(error_string
, message
, sizeof(error_string
)-1);
1602 error_string
[sizeof(error_string
)-1] = '\0';
1605 const char* getErrorMessage()
1607 return error_string
;
1610 void halt(const char* message
)
1612 fprintf(stderr
, "dyld: %s\n", message
);
1613 setErrorMessage(message
);
1614 strncpy(error_string
, message
, sizeof(error_string
)-1);
1615 error_string
[sizeof(error_string
)-1] = '\0';
1617 #if __ppc__ || __ppc64__
1622 #error unknown architecture
1624 abort(); // needed to suppress warning that noreturn function returns
1628 uintptr_t bindLazySymbol(const mach_header
* mh
, uintptr_t* lazyPointer
)
1630 uintptr_t result
= 0;
1631 // acquire read-lock on dyld's data structures
1632 #if 0 // rdar://problem/3811777 turn off locking until deadlock is resolved
1633 if ( gThreadHelpers
!= NULL
)
1634 (*gThreadHelpers
->lockForReading
)();
1636 // lookup and bind lazy pointer and get target address
1638 // note, target should always be mach-o, because only mach-o lazy handler wired up to this
1639 ImageLoader
* target
= dyld::findImageByMachHeader(mh
);
1640 if ( target
== NULL
)
1641 throw "image not found for lazy pointer";
1642 result
= target
->doBindLazySymbol(lazyPointer
, gLinkContext
);
1644 catch (const char* message
) {
1645 fprintf(stderr
, "dyld: lazy symbol binding failed: %s\n", message
);
1648 // release read-lock on dyld's data structures
1650 if ( gThreadHelpers
!= NULL
)
1651 (*gThreadHelpers
->unlockForReading
)();
1653 // return target address to glue which jumps to it with real parameters restored
1658 // SPI used by ZeroLink to lazy load bundles
1659 void registerZeroLinkHandlers(BundleNotificationCallBack notify
, BundleLocatorCallBack locate
)
1661 sBundleNotifier
= notify
;
1662 sBundleLocation
= locate
;
1665 void registerUndefinedHandler(UndefinedHandler handler
)
1667 sUndefinedHandler
= handler
;
1670 static void undefinedHandler(const char* symboName
)
1672 if ( sUndefinedHandler
!= NULL
) {
1673 (*sUndefinedHandler
)(symboName
);
1677 static bool findExportedSymbol(const char* name
, bool onlyInCoalesced
, const ImageLoader::Symbol
** sym
, ImageLoader
** image
)
1679 // try ZeroLink short cut to finding bundle which exports this symbol
1680 if ( sBundleLocation
!= NULL
) {
1681 ImageLoader
* zlImage
= (*sBundleLocation
)(name
);
1682 if ( zlImage
== ((ImageLoader
*)(-1)) ) {
1683 // -1 is magic value that request symbol is in a bundle not yet linked into process
1684 // try calling handler to link in that symbol
1685 undefinedHandler(name
);
1686 // call locator again
1687 zlImage
= (*sBundleLocation
)(name
);
1689 // if still not found, then ZeroLink has no idea where to find it
1690 if ( zlImage
== ((ImageLoader
*)(-1)) )
1692 if ( zlImage
!= NULL
) {
1693 // ZeroLink cache knows where the symbol is
1694 *sym
= zlImage
->findExportedSymbol(name
, NULL
, false, image
);
1695 if ( *sym
!= NULL
) {
1701 // ZeroLink says it is in some bundle already loaded, but not linked, walk them all
1702 const unsigned int imageCount
= sAllImages
.size();
1703 for(unsigned int i
=0; i
< imageCount
; ++i
){
1704 ImageLoader
* anImage
= sAllImages
[i
];
1705 if ( anImage
->isBundle() && !anImage
->hasHiddenExports() ) {
1706 //fprintf(stderr, "dyld: search for %s in %s\n", name, anImage->getPath());
1707 *sym
= anImage
->findExportedSymbol(name
, NULL
, false, image
);
1708 if ( *sym
!= NULL
) {
1716 // search all images in order
1717 ImageLoader
* firstWeakImage
= NULL
;
1718 const ImageLoader::Symbol
* firstWeakSym
= NULL
;
1719 const unsigned int imageCount
= sAllImages
.size();
1720 for(unsigned int i
=0; i
< imageCount
; ++i
){
1721 ImageLoader
* anImage
= sAllImages
[i
];
1722 if ( ! anImage
->hasHiddenExports() && (!onlyInCoalesced
|| anImage
->hasCoalescedExports()) ) {
1723 *sym
= anImage
->findExportedSymbol(name
, NULL
, false, image
);
1724 if ( *sym
!= NULL
) {
1725 // if weak definition found, record first one found
1726 if ( ((*image
)->getExportedSymbolInfo(*sym
) & ImageLoader::kWeakDefinition
) != 0 ) {
1727 if ( firstWeakImage
== NULL
) {
1728 firstWeakImage
= *image
;
1729 firstWeakSym
= *sym
;
1733 // found non-weak, so immediately return with it
1739 if ( firstWeakSym
!= NULL
) {
1740 // found a weak definition, but no non-weak, so return first weak found
1741 *sym
= firstWeakSym
;
1742 *image
= firstWeakImage
;
1749 bool flatFindExportedSymbol(const char* name
, const ImageLoader::Symbol
** sym
, ImageLoader
** image
)
1751 return findExportedSymbol(name
, false, sym
, image
);
1754 bool findCoalescedExportedSymbol(const char* name
, const ImageLoader::Symbol
** sym
, ImageLoader
** image
)
1756 return findExportedSymbol(name
, true, sym
, image
);
1760 bool flatFindExportedSymbolWithHint(const char* name
, const char* librarySubstring
, const ImageLoader::Symbol
** sym
, ImageLoader
** image
)
1762 // search all images in order
1763 const unsigned int imageCount
= sAllImages
.size();
1764 for(unsigned int i
=0; i
< imageCount
; ++i
){
1765 ImageLoader
* anImage
= sAllImages
[i
];
1766 // only look at images whose paths contain the hint string (NULL hint string is wildcard)
1767 if ( ! anImage
->isBundle() && ((librarySubstring
==NULL
) || (strstr(anImage
->getPath(), librarySubstring
) != NULL
)) ) {
1768 *sym
= anImage
->findExportedSymbol(name
, NULL
, false, image
);
1769 if ( *sym
!= NULL
) {
1777 static void getMappedRegions(ImageLoader::RegionsVector
& regions
)
1779 const unsigned int imageCount
= sAllImages
.size();
1780 for(unsigned int i
=0; i
< imageCount
; ++i
){
1781 ImageLoader
* anImage
= sAllImages
[i
];
1782 anImage
->addMappedRegions(regions
);
1787 static ImageLoader
* libraryLocator(const char* libraryName
, bool search
, const char* origin
, const char* rpath
[])
1789 dyld::LoadContext context
;
1790 context
.useSearchPaths
= search
;
1791 context
.useLdLibraryPath
= false;
1792 context
.dontLoad
= false;
1793 context
.mustBeBundle
= false;
1794 context
.mustBeDylib
= true;
1795 context
.matchByInstallName
= false;
1796 context
.origin
= origin
;
1797 context
.rpath
= rpath
;
1798 return load(libraryName
, context
);
1802 static void setContext(int argc
, const char* argv
[], const char* envp
[], const char* apple
[])
1804 gLinkContext
.loadLibrary
= &libraryLocator
;
1805 gLinkContext
.imageNotification
= &imageNotification
;
1806 gLinkContext
.terminationRecorder
= &terminationRecorder
;
1807 gLinkContext
.flatExportFinder
= &flatFindExportedSymbol
;
1808 gLinkContext
.coalescedExportFinder
= &findCoalescedExportedSymbol
;
1809 gLinkContext
.undefinedHandler
= &undefinedHandler
;
1810 gLinkContext
.addImageNeedingNotification
= &addImageNeedingNotification
;
1811 gLinkContext
.notifyAdding
= ¬ifyAdding
;
1812 gLinkContext
.getAllMappedRegions
= &getMappedRegions
;
1813 gLinkContext
.bindingHandler
= NULL
;
1814 gLinkContext
.bindingOptions
= ImageLoader::kBindingNone
;
1815 gLinkContext
.mainExecutable
= sMainExecutable
;
1816 gLinkContext
.argc
= argc
;
1817 gLinkContext
.argv
= argv
;
1818 gLinkContext
.envp
= envp
;
1819 gLinkContext
.apple
= apple
;
1824 void link(ImageLoader
* image
, ImageLoader::BindingLaziness bindness
, ImageLoader::InitializerRunning runInitializers
)
1826 // add to list of known images. This did not happen at creation time for bundles
1827 if ( image
->isBundle() )
1830 // we detect root images as those not linked in yet
1831 if ( !image
->isLinked() )
1832 addRootImage(image
);
1834 // notify ZeroLink of new image with concat of logical and physical name
1835 if ( sBundleNotifier
!= NULL
&& image
->isBundle() ) {
1836 const int logicalLen
= strlen(image
->getLogicalPath());
1837 char logAndPhys
[strlen(image
->getPath())+logicalLen
+2];
1838 strcpy(logAndPhys
, image
->getLogicalPath());
1839 strcpy(&logAndPhys
[logicalLen
+1], image
->getPath());
1840 (*sBundleNotifier
)(logAndPhys
, image
);
1844 image
->link(gLinkContext
, bindness
, runInitializers
, sAddImageCallbacks
.size());
1846 #if OLD_GDB_DYLD_INTERFACE
1847 // notify gdb that loaded libraries have changed
1848 gdb_dyld_state_changed();
1854 // Entry point for dyld. The kernel loads dyld and jumps to __dyld_start which
1855 // sets up some registers and call this function.
1857 // Returns address of main() in target program which __dyld_start jumps to
1860 _main(const struct mach_header
* mainExecutableMH
, int argc
, const char* argv
[], const char* envp
[], const char* apple
[])
1862 // Pickup the pointer to the exec path.
1863 const char* executable
= apple
[0];
1864 if ( executable
[0] == '/' ) {
1865 // have full path, use it
1866 sExecPath
= executable
;
1869 // have relative path, use cwd to make absolute
1870 char cwdbuff
[MAXPATHLEN
];
1871 if ( getcwd(cwdbuff
, MAXPATHLEN
) != NULL
) {
1872 // maybe use static buffer to avoid calling malloc so early...
1873 char* s
= new char[strlen(cwdbuff
) + strlen(executable
) + 2];
1876 strcat(s
, executable
);
1880 uintptr_t result
= 0;
1881 sMainExecutableMachHeader
= mainExecutableMH
;
1882 checkEnvironmentVariables(envp
);
1883 if ( sEnv
.DYLD_PRINT_OPTS
)
1885 if ( sEnv
.DYLD_PRINT_ENV
)
1886 printEnvironmentVariables(envp
);
1888 setContext(argc
, argv
, envp
, apple
);
1889 ImageLoader::BindingLaziness bindness
= sEnv
.DYLD_BIND_AT_LAUNCH
? ImageLoader::kLazyAndNonLazy
: ImageLoader::kNonLazyOnly
;
1891 // load any inserted libraries before loading the main executable so that they are first in flat namespace
1892 int insertLibrariesCount
= 0;
1893 if ( sEnv
.DYLD_INSERT_LIBRARIES
!= NULL
) {
1894 for (const char* const* lib
= sEnv
.DYLD_INSERT_LIBRARIES
; *lib
!= NULL
; ++lib
) {
1895 insertLibrariesCount
++;
1898 ImageLoader
* insertedImages
[insertLibrariesCount
];
1899 if ( insertLibrariesCount
> 0 ) {
1900 for (int i
=0; i
< insertLibrariesCount
; ++i
) {
1902 LoadContext context
;
1903 context
.useSearchPaths
= false;
1904 context
.useLdLibraryPath
= false;
1905 context
.dontLoad
= false;
1906 context
.mustBeBundle
= false;
1907 context
.mustBeDylib
= true;
1908 context
.matchByInstallName
= false;
1909 context
.origin
= NULL
; // can't use @loader_path with DYLD_INSERT_LIBRARIES
1910 context
.rpath
= NULL
;
1911 insertedImages
[i
] = load(sEnv
.DYLD_INSERT_LIBRARIES
[i
], context
);
1914 char buf
[strlen(sEnv
.DYLD_INSERT_LIBRARIES
[i
])+50];
1915 sprintf(buf
, "could not load inserted library: %s\n", sEnv
.DYLD_INSERT_LIBRARIES
[i
]);
1916 insertedImages
[i
] = NULL
;
1922 // load and link main executable
1924 sMainExecutable
= instantiateFromLoadedImage(mainExecutableMH
, sExecPath
);
1925 gLinkContext
.mainExecutable
= sMainExecutable
;
1926 if ( sMainExecutable
->forceFlat() ) {
1927 gLinkContext
.bindFlat
= true;
1928 gLinkContext
.prebindUsage
= ImageLoader::kUseNoPrebinding
;
1930 link(sMainExecutable
, bindness
, ImageLoader::kDontRunInitializers
);
1931 result
= (uintptr_t)sMainExecutable
->getMain();
1933 catch(const char* message
) {
1937 fprintf(stderr
, "dyld: launch failed\n");
1940 // Link in any inserted libraries.
1941 // Do this after link main executable so any extra libraries pulled in by inserted libraries are at end of flat namespace
1942 if ( insertLibrariesCount
> 0 ) {
1943 for (int i
=0; i
< insertLibrariesCount
; ++i
) {
1945 if ( insertedImages
[i
] != NULL
)
1946 link(insertedImages
[i
], bindness
, ImageLoader::kDontRunInitializers
);
1948 catch (const char* message
) {
1949 char buf
[strlen(sEnv
.DYLD_INSERT_LIBRARIES
[i
])+50+strlen(message
)];
1950 sprintf(buf
, "could not link inserted library: %s\n%s\n", sEnv
.DYLD_INSERT_LIBRARIES
[i
], message
);