]> git.saurik.com Git - apple/dyld.git/blob - src/ImageLoader.cpp
61f49ef3eb93bad5e8f63039040732947143511d
[apple/dyld.git] / src / ImageLoader.cpp
1 /* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*-
2 *
3 * Copyright (c) 2004-2005 Apple Computer, Inc. All rights reserved.
4 *
5 * @APPLE_LICENSE_HEADER_START@
6 *
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
12 * file.
13 *
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.
21 *
22 * @APPLE_LICENSE_HEADER_END@
23 */
24
25 #define __STDC_LIMIT_MACROS
26 #include <stdint.h>
27 #include <errno.h>
28 #include <fcntl.h>
29 #include <mach/mach.h>
30 #include <mach-o/fat.h>
31 #include <sys/types.h>
32 #include <sys/stat.h>
33 #include <sys/mman.h>
34 #include <sys/param.h>
35 #include <sys/mount.h>
36
37 #include "ImageLoader.h"
38
39
40 uint32_t ImageLoader::fgImagesWithUsedPrebinding = 0;
41 uint32_t ImageLoader::fgTotalRebaseFixups = 0;
42 uint32_t ImageLoader::fgTotalBindFixups = 0;
43 uint32_t ImageLoader::fgTotalLazyBindFixups = 0;
44 uint32_t ImageLoader::fgTotalPossibleLazyBindFixups = 0;
45 uint64_t ImageLoader::fgTotalLoadLibrariesTime;
46 uint64_t ImageLoader::fgTotalRebaseTime;
47 uint64_t ImageLoader::fgTotalBindTime;
48 uint64_t ImageLoader::fgTotalNotifyTime;
49 uint64_t ImageLoader::fgTotalInitTime;
50 uintptr_t ImageLoader::fgNextSplitSegAddress = 0x90000000;
51 uintptr_t Segment::fgNextNonSplitSegAddress = 0x8F000000;
52
53
54
55 __attribute__((noreturn))
56 void throwf(const char* format, ...)
57 {
58 va_list list;
59 char* p;
60 va_start(list, format);
61 vasprintf(&p, format, list);
62 va_end(list);
63
64 const char* t = p;
65 throw t;
66 }
67
68 void ImageLoader::init(const char* path, uint64_t offsetInFat, dev_t device, ino_t inode, time_t modDate)
69 {
70 fPathHash = 0;
71 fPath = NULL;
72 if ( path != NULL )
73 this->setPath(path);
74 fLogicalPath = NULL;
75 fDevice = device;
76 fInode = inode;
77 fLastModified = modDate;
78 fOffsetInFatFile = offsetInFat;
79 //fSegments = NULL;
80 fLibraries = NULL;
81 fLibrariesCount = 0;
82 fReferenceCount = 0;
83 fAllLibraryChecksumsAndLoadAddressesMatch = false;
84 fLeaveMapped = false;
85 fHideSymbols = false;
86 fMatchByInstallName = false;
87 fLibrariesLoaded = false;
88 fBased = false;
89 fBoundAllNonLazy = false;
90 fBoundAllLazy = false;
91 fAnnounced = false;
92 fInitialized = false;
93 fNextAddImageIndex = 0;
94 }
95
96
97 ImageLoader::ImageLoader(const char* path, uint64_t offsetInFat, const struct stat& info)
98 {
99 init(path, offsetInFat, info.st_dev, info.st_ino, info.st_mtime);
100 }
101
102 ImageLoader::ImageLoader(const char* moduleName)
103 {
104 init(moduleName, 0, 0, 0, 0);
105 }
106
107
108 ImageLoader::~ImageLoader()
109 {
110 // need to read up on STL and see if this is right way to destruct vector contents
111 const unsigned int segmentCount = fSegments.size();
112 for(unsigned int i=0; i < segmentCount; ++i){
113 Segment* seg = fSegments[i];
114 delete seg;
115 }
116 if ( fPath != NULL )
117 delete [] fPath;
118 if ( fLogicalPath != NULL )
119 delete [] fLogicalPath;
120 }
121
122
123 void ImageLoader::setPath(const char* path)
124 {
125 if ( fPath != NULL ) {
126 // if duplicate path, do nothing
127 if ( strcmp(path, fPath) == 0 )
128 return;
129 delete [] fPath;
130 }
131 fPath = new char[strlen(path)+1];
132 strcpy((char*)fPath, path);
133 fPathHash = hash(fPath);
134 }
135
136 void ImageLoader::setLogicalPath(const char* path)
137 {
138 if ( fPath == NULL ) {
139 // no physical path set yet, so use this path as physical
140 this->setPath(path);
141 }
142 else if ( strcmp(path, fPath) == 0 ) {
143 // do not set logical path because it is the same as the physical path
144 fLogicalPath = NULL;
145 }
146 else {
147 fLogicalPath = new char[strlen(path)+1];
148 strcpy((char*)fLogicalPath, path);
149 }
150 }
151
152 const char* ImageLoader::getLogicalPath() const
153 {
154 if ( fLogicalPath != NULL )
155 return fLogicalPath;
156 else
157 return fPath;
158 }
159
160 uint32_t ImageLoader::hash(const char* path)
161 {
162 // this does not need to be a great hash
163 // it is just used to reduce the number of strcmp() calls
164 // of existing images when loading a new image
165 uint32_t h = 0;
166 for (const char* s=path; *s != '\0'; ++s)
167 h = h*5 + *s;
168 return h;
169 }
170
171 bool ImageLoader::matchInstallPath() const
172 {
173 return fMatchByInstallName;
174 }
175
176 void ImageLoader::setMatchInstallPath(bool match)
177 {
178 fMatchByInstallName = match;
179 }
180
181 bool ImageLoader::statMatch(const struct stat& stat_buf) const
182 {
183 return ( (this->fDevice == stat_buf.st_dev) && (this->fInode == stat_buf.st_ino) );
184 }
185
186 const char* ImageLoader::getShortName() const
187 {
188 // try to return leaf name
189 if ( fPath != NULL ) {
190 const char* s = strrchr(fPath, '/');
191 if ( s != NULL )
192 return &s[1];
193 }
194 return fPath;
195 }
196
197 uint64_t ImageLoader::getOffsetInFatFile() const
198 {
199 return fOffsetInFatFile;
200 }
201
202 void ImageLoader::setLeaveMapped()
203 {
204 fLeaveMapped = true;
205 const unsigned int segmentCount = fSegments.size();
206 for(unsigned int i=0; i < segmentCount; ++i){
207 fSegments[i]->setUnMapWhenDestructed(false);
208 }
209 }
210
211 void ImageLoader::setHideExports(bool hide)
212 {
213 fHideSymbols = hide;
214 }
215
216 bool ImageLoader::hasHiddenExports() const
217 {
218 return fHideSymbols;
219 }
220
221 bool ImageLoader::isLinked() const
222 {
223 return fBoundAllNonLazy;
224 }
225
226 time_t ImageLoader::lastModified()
227 {
228 return fLastModified;
229 }
230
231 bool ImageLoader::containsAddress(const void* addr) const
232 {
233 const unsigned int segmentCount = fSegments.size();
234 for(unsigned int i=0; i < segmentCount; ++i){
235 Segment* seg = fSegments[i];
236 const uint8_t* start = (const uint8_t*)seg->getActualLoadAddress();
237 const uint8_t* end = start + seg->getSize();
238 if ( (start <= addr) && (addr < end) && !seg->unaccessible() )
239 return true;
240 }
241 return false;
242 }
243
244 void ImageLoader::addMappedRegions(RegionsVector& regions) const
245 {
246 const unsigned int segmentCount = fSegments.size();
247 for(unsigned int i=0; i < segmentCount; ++i){
248 Segment* seg = fSegments[i];
249 MappedRegion region;
250 region.address = seg->getActualLoadAddress();
251 region.size = seg->getSize();
252 regions.push_back(region);
253 }
254 }
255
256
257 void ImageLoader::incrementReferenceCount()
258 {
259 ++fReferenceCount;
260 }
261
262 bool ImageLoader::decrementReferenceCount()
263 {
264 return ( --fReferenceCount == 0 );
265 }
266
267 // private method that handles circular dependencies by only search any image once
268 const ImageLoader::Symbol* ImageLoader::findExportedSymbolInDependentImagesExcept(const char* name, std::set<const ImageLoader*>& dontSearchImages, ImageLoader** foundIn) const
269 {
270 const ImageLoader::Symbol* sym;
271 // search self
272 if ( dontSearchImages.count(this) == 0 ) {
273 sym = this->findExportedSymbol(name, NULL, false, foundIn);
274 if ( sym != NULL )
275 return sym;
276 dontSearchImages.insert(this);
277 }
278
279 // search directly dependent libraries
280 for (uint32_t i=0; i < fLibrariesCount; ++i) {
281 ImageLoader* dependentImage = fLibraries[i].image;
282 if ( (dependentImage != NULL) && (dontSearchImages.count(dependentImage) == 0) ) {
283 const ImageLoader::Symbol* sym = dependentImage->findExportedSymbol(name, NULL, false, foundIn);
284 if ( sym != NULL )
285 return sym;
286 }
287 }
288
289 // search indirectly dependent libraries
290 for (uint32_t i=0; i < fLibrariesCount; ++i) {
291 ImageLoader* dependentImage = fLibraries[i].image;
292 if ( (dependentImage != NULL) && (dontSearchImages.count(dependentImage) == 0) ) {
293 const ImageLoader::Symbol* sym = dependentImage->findExportedSymbolInDependentImagesExcept(name, dontSearchImages, foundIn);
294 if ( sym != NULL )
295 return sym;
296 dontSearchImages.insert(dependentImage);
297 }
298 }
299
300 return NULL;
301 }
302
303
304 const ImageLoader::Symbol* ImageLoader::findExportedSymbolInDependentImages(const char* name, ImageLoader** foundIn) const
305 {
306 std::set<const ImageLoader*> dontSearchImages;
307 dontSearchImages.insert(this); // don't search this image
308 return this->findExportedSymbolInDependentImagesExcept(name, dontSearchImages, foundIn);
309 }
310
311 const ImageLoader::Symbol* ImageLoader::findExportedSymbolInImageOrDependentImages(const char* name, ImageLoader** foundIn) const
312 {
313 std::set<const ImageLoader*> dontSearchImages;
314 return this->findExportedSymbolInDependentImagesExcept(name, dontSearchImages, foundIn);
315 }
316
317
318 void ImageLoader::link(const LinkContext& context, BindingLaziness bindness, InitializerRunning inits, uint32_t notifyCount)
319 {
320 uint64_t t1 = mach_absolute_time();
321 this->recursiveLoadLibraries(context);
322
323 uint64_t t2 = mach_absolute_time();
324 this->recursiveRebase(context);
325
326 uint64_t t3 = mach_absolute_time();
327 this->recursiveBind(context, bindness);
328
329 uint64_t t4 = mach_absolute_time();
330 this->recursiveImageNotification(context, notifyCount);
331
332 if ( (inits == kRunInitializers) || (inits == kDontRunInitializersButTellObjc) ) {
333 std::vector<ImageLoader*> newImages;
334 this->recursiveImageAnnouncement(context, newImages); // build bottom up list images being added
335 context.notifyAdding(newImages); // tell gdb or anyone who cares about these
336 }
337
338 uint64_t t5 = mach_absolute_time();
339 if ( inits == kRunInitializers ) {
340 this->recursiveInitialization(context);
341 uint64_t t6 = mach_absolute_time();
342 fgTotalInitTime += t6 - t5;
343 }
344 fgTotalLoadLibrariesTime += t2 - t1;
345 fgTotalRebaseTime += t3 - t2;
346 fgTotalBindTime += t4 - t3;
347 fgTotalNotifyTime += t5 - t4;
348 }
349
350
351 // only called pre-main on main executable
352 // if crt.c is ever cleaned up, this could go away
353 void ImageLoader::runInitializers(const LinkContext& context)
354 {
355 std::vector<ImageLoader*> newImages;
356 this->recursiveImageAnnouncement(context, newImages); // build bottom up list images being added
357 context.notifyAdding(newImages); // tell gdb or anyone who cares about these
358
359 this->recursiveInitialization(context);
360 }
361
362 // called inside _dyld_register_func_for_add_image()
363 void ImageLoader::runNotification(const LinkContext& context, uint32_t notifyCount)
364 {
365 this->recursiveImageNotification(context, notifyCount);
366 }
367
368
369 intptr_t ImageLoader::assignSegmentAddresses(const LinkContext& context)
370 {
371 // preflight and calculate slide if needed
372 const unsigned int segmentCount = fSegments.size();
373 intptr_t slide = 0;
374 if ( this->segmentsCanSlide() && this->segmentsMustSlideTogether() ) {
375 bool needsToSlide = false;
376 uintptr_t lowAddr = UINTPTR_MAX;
377 uintptr_t highAddr = 0;
378 for(unsigned int i=0; i < segmentCount; ++i){
379 Segment* seg = fSegments[i];
380 const uintptr_t segLow = seg->getPreferredLoadAddress();
381 const uintptr_t segHigh = segLow + seg->getSize();
382 if ( segLow < lowAddr )
383 lowAddr = segLow;
384 if ( segHigh > highAddr )
385 highAddr = segHigh;
386
387 if ( context.slideAndPackDylibs || !seg->hasPreferredLoadAddress() || !Segment::reserveAddressRange(seg->getPreferredLoadAddress(), seg->getSize()) )
388 needsToSlide = true;
389 }
390 if ( needsToSlide ) {
391 // find a chunk of address space to hold all segments
392 uintptr_t addr = Segment::reserveAnAddressRange(highAddr-lowAddr, context);
393 slide = addr - lowAddr;
394 }
395 }
396 else if ( ! this->segmentsCanSlide() ) {
397 for(unsigned int i=0; i < segmentCount; ++i){
398 Segment* seg = fSegments[i];
399 if ( strcmp(seg->getName(), "__PAGEZERO") == 0 )
400 continue;
401 if ( !Segment::reserveAddressRange(seg->getPreferredLoadAddress(), seg->getSize()) )
402 throw "can't map";
403 }
404 }
405 else {
406 // mach-o does not support independently sliding segments
407 }
408 return slide;
409 }
410
411
412 void ImageLoader::mapSegments(int fd, uint64_t offsetInFat, uint64_t lenInFat, uint64_t fileLen, const LinkContext& context)
413 {
414 if ( context.verboseMapping )
415 fprintf(stderr, "dyld: Mapping %s\n", this->getPath());
416 // find address range for image
417 intptr_t slide = this->assignSegmentAddresses(context);
418 // map in all segments
419 const unsigned int segmentCount = fSegments.size();
420 for(unsigned int i=0; i < segmentCount; ++i){
421 Segment* seg = fSegments[i];
422 seg->map(fd, offsetInFat, slide, context);
423 }
424 // update slide to reflect load location
425 this->setSlide(slide);
426
427 // now that it is mapped and slide is set, mark that we should unmap it when done
428 for(unsigned int i=0; i < segmentCount; ++i){
429 fSegments[i]->setUnMapWhenDestructed(true);
430 }
431 }
432
433 void ImageLoader::mapSegments(const void* memoryImage, uint64_t imageLen, const LinkContext& context)
434 {
435 if ( context.verboseMapping )
436 fprintf(stderr, "dyld: Mapping memory %p\n", memoryImage);
437 // find address range for image
438 intptr_t slide = this->assignSegmentAddresses(context);
439 // map in all segments
440 const unsigned int segmentCount = fSegments.size();
441 for(unsigned int i=0; i < segmentCount; ++i){
442 Segment* seg = fSegments[i];
443 seg->map(memoryImage, slide, context);
444 }
445 // update slide to reflect load location
446 this->setSlide(slide);
447 // set R/W permissions on all segments at slide location
448 for(unsigned int i=0; i < segmentCount; ++i){
449 Segment* seg = fSegments[i];
450 seg->setPermissions();
451 }
452 }
453
454 bool ImageLoader::allDependentLibrariesAsWhenPreBound() const
455 {
456 return fAllLibraryChecksumsAndLoadAddressesMatch;
457 }
458
459
460 void ImageLoader::recursiveLoadLibraries(const LinkContext& context)
461 {
462 if ( ! fLibrariesLoaded ) {
463 // break cycles
464 fLibrariesLoaded = true;
465
466 // get list of libraries this image needs
467 fLibrariesCount = this->doGetDependentLibraryCount();
468 fLibraries = new DependentLibrary[fLibrariesCount];
469 this->doGetDependentLibraries(fLibraries);
470
471 // try to load each
472 bool canUsePrelinkingInfo = true;
473 for(unsigned int i=0; i < fLibrariesCount; ++i){
474 DependentLibrary& requiredLib = fLibraries[i];
475 try {
476 requiredLib.image = context.loadLibrary(requiredLib.name, true, this->getPath(), NULL);
477 if ( requiredLib.image == this ) {
478 // found circular reference, perhaps DYLD_LIBARY_PATH is causing this rdar://problem/3684168
479 requiredLib.image = context.loadLibrary(requiredLib.name, false, NULL, NULL);
480 if ( requiredLib.image != this )
481 fprintf(stderr, "dyld: warning DYLD_ setting caused circular dependency in %s\n", this->getPath());
482 }
483 LibraryInfo actualInfo = requiredLib.image->doGetLibraryInfo();
484 requiredLib.checksumMatches = ( actualInfo.checksum == requiredLib.info.checksum );
485 // check found library version is compatible
486 if ( actualInfo.minVersion < requiredLib.info.minVersion ) {
487 throwf("Incompatible library version: %s requires version %d.%d.%d or later, but %s provides version %d.%d.%d",
488 this->getShortName(), requiredLib.info.minVersion >> 16, (requiredLib.info.minVersion >> 8) & 0xff, requiredLib.info.minVersion & 0xff,
489 requiredLib.image->getShortName(), actualInfo.minVersion >> 16, (actualInfo.minVersion >> 8) & 0xff, actualInfo.minVersion & 0xff);
490 }
491 // prebinding for this image disabled if any dependent library changed or slid
492 if ( !requiredLib.checksumMatches || (requiredLib.image->getSlide() != 0) )
493 canUsePrelinkingInfo = false;
494 //if ( context.verbosePrebinding ) {
495 // if ( !requiredLib.checksumMatches )
496 // fprintf(stderr, "dyld: checksum mismatch, (%lld v %lld) for %s referencing %s\n", requiredLib.info.checksum, actualInfo.checksum, this->getPath(), requiredLib.image->getPath());
497 // if ( requiredLib.image->getSlide() != 0 )
498 // fprintf(stderr, "dyld: dependent library slid for %s referencing %s\n", this->getPath(), requiredLib.image->getPath());
499 //}
500 }
501 catch (const char* msg) {
502 //if ( context.verbosePrebinding )
503 // fprintf(stderr, "dyld: exception during processing for %s referencing %s\n", this->getPath(), requiredLib.image->getPath());
504 if ( requiredLib.required ) {
505 const char* formatString = "Library not loaded: %s\n Referenced from: %s\n Reason: %s";
506 const char* referencedFrom = this->getPath();
507 char buf[strlen(requiredLib.name)+strlen(referencedFrom)+strlen(formatString)+strlen(msg)+2];
508 sprintf(buf, formatString, requiredLib.name, referencedFrom, msg);
509 fLibrariesLoaded = false;
510 throw strdup(buf); // this is a leak if exception doesn't halt program
511 }
512 // ok if weak library not found
513 requiredLib.image = NULL;
514 canUsePrelinkingInfo = false; // this disables all prebinding, we may want to just slam import vectors for this lib to zero
515 }
516 }
517 fAllLibraryChecksumsAndLoadAddressesMatch = canUsePrelinkingInfo;
518
519 // tell each to load its dependents
520 for(unsigned int i=0; i < fLibrariesCount; ++i){
521 DependentLibrary& libInfo = fLibraries[i];
522 if ( libInfo.image != NULL ) {
523 libInfo.isSubFramework = libInfo.image->isSubframeworkOf(context, this);
524 libInfo.isReExported = libInfo.isSubFramework || this->hasSubLibrary(context, libInfo.image);
525 //if ( libInfo.isReExported )
526 // fprintf(stderr, "%s re-exports %s\n", strrchr(this->getPath(), '/'), strrchr(libInfo.image->getPath(),'/'));
527 libInfo.image->recursiveLoadLibraries(context);
528 }
529 }
530
531 // do deep prebind check
532 if ( fAllLibraryChecksumsAndLoadAddressesMatch ) {
533 for(unsigned int i=0; i < fLibrariesCount; ++i){
534 const DependentLibrary& libInfo = fLibraries[i];
535 if ( libInfo.image != NULL ) {
536 if ( !libInfo.image->allDependentLibrariesAsWhenPreBound() )
537 fAllLibraryChecksumsAndLoadAddressesMatch = false;
538 }
539 }
540 }
541
542 }
543 }
544
545 void ImageLoader::recursiveRebase(const LinkContext& context)
546 {
547 if ( ! fBased ) {
548 // break cycles
549 fBased = true;
550
551 try {
552 // rebase lower level libraries first
553 for(unsigned int i=0; i < fLibrariesCount; ++i){
554 DependentLibrary& libInfo = fLibraries[i];
555 if ( libInfo.image != NULL )
556 libInfo.image->recursiveRebase(context);
557 }
558
559 // rebase this image
560 doRebase(context);
561 }
562 catch (const char* msg) {
563 // this image is not rebased
564 fBased = false;
565 throw msg;
566 }
567 }
568 }
569
570
571
572
573 void ImageLoader::recursiveBind(const LinkContext& context, BindingLaziness bindness)
574 {
575 // normally just non-lazy pointers are bound up front,
576 // but DYLD_BIND_AT_LAUNCH will cause lazy pointers to be bound up from
577 // and some dyld API's bind all lazys at runtime
578 bool nonLazy = false;
579 bool lazy = false;
580 switch( bindness ) {
581 case kNonLazyOnly:
582 nonLazy = true;
583 break;
584 case kLazyAndNonLazy:
585 nonLazy = true;
586 lazy = true;
587 break;
588 case kLazyOnly:
589 case kLazyOnlyNoDependents:
590 lazy = true;
591 break;
592 }
593 const bool doNonLazy = nonLazy && !fBoundAllNonLazy;
594 const bool doLazy = lazy && !fBoundAllLazy;
595 if ( doNonLazy || doLazy ) {
596 // break cycles
597 bool oldBoundAllNonLazy = fBoundAllNonLazy;
598 bool oldBoundAllLazy = fBoundAllLazy;
599 fBoundAllNonLazy = fBoundAllNonLazy || nonLazy;
600 fBoundAllLazy = fBoundAllLazy || lazy;
601
602 try {
603 // bind lower level libraries first
604 if ( bindness != kLazyOnlyNoDependents ) {
605 for(unsigned int i=0; i < fLibrariesCount; ++i){
606 DependentLibrary& libInfo = fLibraries[i];
607 if ( libInfo.image != NULL )
608 libInfo.image->recursiveBind(context, bindness);
609 }
610 }
611 // bind this image
612 if ( doLazy && !doNonLazy )
613 doBind(context, kLazyOnly);
614 else if ( !doLazy && doNonLazy )
615 doBind(context, kNonLazyOnly);
616 else
617 doBind(context, kLazyAndNonLazy);
618 }
619 catch (const char* msg) {
620 // restore state
621 fBoundAllNonLazy = oldBoundAllNonLazy;
622 fBoundAllLazy = oldBoundAllLazy;
623 throw msg;
624 }
625 }
626 }
627
628 //
629 // This is complex because _dyld_register_func_for_add_image() is defined to not only
630 // notify you of future image loads, but also of all currently loaded images. Therefore
631 // each image needs to track that all add-image-funcs have been notified about it.
632 // Since add-image-funcs cannot be removed, each has a unique index and each image
633 // records the thru which index notificiation has already been done.
634 //
635 void ImageLoader::recursiveImageNotification(const LinkContext& context, uint32_t addImageCount)
636 {
637 if ( fNextAddImageIndex < addImageCount ) {
638 // break cycles
639 const uint32_t initIndex = fNextAddImageIndex;
640 fNextAddImageIndex = addImageCount;
641
642 // notify all requestors about this image
643 context.imageNotification(this, initIndex);
644
645 // notify about lower level libraries first
646 for(unsigned int i=0; i < fLibrariesCount; ++i){
647 DependentLibrary& libInfo = fLibraries[i];
648 if ( libInfo.image != NULL )
649 libInfo.image->recursiveImageNotification(context, addImageCount);
650 }
651 }
652 }
653
654
655 void ImageLoader::recursiveImageAnnouncement(const LinkContext& context, std::vector<ImageLoader*>& newImages)
656 {
657 if ( ! fAnnounced ) {
658 // break cycles
659 fAnnounced = true;
660
661 // announce lower level libraries first
662 for(unsigned int i=0; i < fLibrariesCount; ++i){
663 DependentLibrary& libInfo = fLibraries[i];
664 if ( libInfo.image != NULL )
665 libInfo.image->recursiveImageAnnouncement(context, newImages);
666 }
667
668 // add to list of images to notify gdb about
669 newImages.push_back(this);
670 //fprintf(stderr, "next size = %d\n", newImages.size());
671
672 // remember that this image wants to be notified about other images
673 if ( this->hasImageNotification() )
674 context.addImageNeedingNotification(this);
675 }
676 }
677
678
679
680 void ImageLoader::recursiveInitialization(const LinkContext& context)
681 {
682 if ( ! fInitialized ) {
683 // break cycles
684 fInitialized = true;
685
686 try {
687 // initialize lower level libraries first
688 for(unsigned int i=0; i < fLibrariesCount; ++i){
689 DependentLibrary& libInfo = fLibraries[i];
690 if ( libInfo.image != NULL )
691 libInfo.image->recursiveInitialization(context);
692 }
693
694 // record termination order
695 if ( this->needsTermination() )
696 context.terminationRecorder(this);
697
698 // initialize this image
699 this->doInitialization(context);
700 }
701 catch (const char* msg) {
702 // this image is not initialized
703 fInitialized = false;
704 throw msg;
705 }
706 }
707 }
708
709 void ImageLoader::reprebindCommit(const LinkContext& context, bool commit, bool unmapOld)
710 {
711 // do nothing on unprebound images
712 if ( ! this->isPrebindable() )
713 return;
714
715 // do nothing if prebinding is up to date
716 if ( this->usablePrebinding(context) )
717 return;
718
719 // make sure we are not replacing a symlink with a file
720 char realFilePath[PATH_MAX];
721 if ( realpath(this->getPath(), realFilePath) == NULL ) {
722 throwf("realpath() failed on %s, errno=%d", this->getPath(), errno);
723 }
724 // recreate temp file name
725 char tempFilePath[PATH_MAX];
726 char suffix[64];
727 sprintf(suffix, "_redoprebinding%d", getpid());
728 ImageLoader::addSuffix(realFilePath, suffix, tempFilePath);
729
730 if ( commit ) {
731 // all files successfully reprebound, so do swap
732 int result = rename(tempFilePath, realFilePath);
733 if ( result != 0 ) {
734 // if there are two dylibs with the same install path, the second will fail to prebind
735 // because the _redoprebinding temp file is gone. In that case, log and go on.
736 if ( errno == ENOENT )
737 fprintf(stderr, "update_prebinding: temp file missing: %s\n", tempFilePath);
738 else
739 throwf("can't swap temporary re-prebound file: rename(%s,%s) returned errno=%d", tempFilePath, realFilePath, errno);
740 }
741 else if ( unmapOld ) {
742 this->prebindUnmap(context);
743 }
744 }
745 else {
746 // something went wrong during prebinding, delete the temp files
747 unlink(tempFilePath);
748 }
749 }
750
751 uint64_t ImageLoader::reprebind(const LinkContext& context, time_t timestamp)
752 {
753 // do nothing on unprebound images
754 if ( ! this->isPrebindable() )
755 return INT64_MAX;
756
757 // do nothing if prebinding is up to date
758 if ( this->usablePrebinding(context) ) {
759 if ( context.verbosePrebinding )
760 fprintf(stderr, "dyld: no need to re-prebind: %s\n", this->getPath());
761 return INT64_MAX;
762 }
763 // recreate temp file name
764 char realFilePath[PATH_MAX];
765 if ( realpath(this->getPath(), realFilePath) == NULL ) {
766 throwf("realpath() failed on %s, errno=%d", this->getPath(), errno);
767 }
768 char tempFilePath[PATH_MAX];
769 char suffix[64];
770 sprintf(suffix, "_redoprebinding%d", getpid());
771 ImageLoader::addSuffix(realFilePath, suffix, tempFilePath);
772
773 // make copy of file and map it in
774 uint8_t* fileToPrebind;
775 uint64_t fileToPrebindSize;
776 uint64_t freespace = this->copyAndMap(tempFilePath, &fileToPrebind, &fileToPrebindSize);
777
778 // do format specific prebinding
779 this->doPrebinding(context, timestamp, fileToPrebind);
780
781 // flush and swap files
782 int result = msync(fileToPrebind, fileToPrebindSize, MS_ASYNC);
783 if ( result != 0 )
784 throw "error syncing re-prebound file";
785 result = munmap(fileToPrebind, fileToPrebindSize);
786 if ( result != 0 )
787 throw "error unmapping re-prebound file";
788
789 // log
790 if ( context.verbosePrebinding )
791 fprintf(stderr, "dyld: re-prebound: %p %s\n", this->machHeader(), this->getPath());
792
793 return freespace;
794 }
795
796 uint64_t ImageLoader::copyAndMap(const char* tempFile, uint8_t** fileToPrebind, uint64_t* fileToPrebindSize)
797 {
798 // reopen dylib
799 int src = open(this->getPath(), O_RDONLY);
800 if ( src == -1 )
801 throw "can't open image";
802 struct stat stat_buf;
803 if ( fstat(src, &stat_buf) == -1)
804 throw "can't stat image";
805 if ( stat_buf.st_mtime != fLastModified )
806 throw "image file changed since it was loaded";
807
808 // create new file with all same permissions to hold copy of dylib
809 unlink(tempFile);
810 int dst = open(tempFile, O_CREAT | O_RDWR | O_TRUNC, stat_buf.st_mode);
811 if ( dst == -1 )
812 throw "can't create temp image";
813
814 // mark source as "don't cache"
815 (void)fcntl(src, F_NOCACHE, 1);
816 // we want to cache the dst because we are about to map it in and modify it
817
818 // copy permission bits
819 if ( chmod(tempFile, stat_buf.st_mode & 07777) == -1 )
820 throwf("can't chmod temp image. errno=%d for %s", errno, this->getPath());
821 if ( chown(tempFile, stat_buf.st_uid, stat_buf.st_gid) == -1)
822 throwf("can't chown temp image. errno=%d for %s", errno, this->getPath());
823
824 // copy contents
825 ssize_t len;
826 const uint32_t kBufferSize = 128*1024;
827 static uint8_t* buffer = NULL;
828 if ( buffer == NULL ) {
829 vm_address_t addr = 0;
830 if ( vm_allocate(mach_task_self(), &addr, kBufferSize, true /*find range*/) == KERN_SUCCESS )
831 buffer = (uint8_t*)addr;
832 else
833 throw "can't allcoate copy buffer";
834 }
835 while ( (len = read(src, buffer, kBufferSize)) > 0 ) {
836 if ( write(dst, buffer, len) == -1 )
837 throwf("write failure copying dylib errno=%d for %s", errno, this->getPath());
838 }
839
840 // map in dst file
841 *fileToPrebindSize = stat_buf.st_size - fOffsetInFatFile; // this may map in too much, but it does not matter
842 *fileToPrebind = (uint8_t*)mmap(NULL, *fileToPrebindSize, PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED, dst, fOffsetInFatFile);
843 if ( *fileToPrebind == (uint8_t*)(-1) )
844 throw "can't mmap temp image";
845
846 // get free space remaining on dst volume
847 struct statfs statfs_buf;
848 if ( fstatfs(dst, &statfs_buf) != 0 )
849 throwf("can't fstatfs(), errno=%d for %s", errno, tempFile);
850 uint64_t freespace = (uint64_t)statfs_buf.f_bavail * (uint64_t)statfs_buf.f_bsize;
851
852 // closing notes:
853 // ok to close file after mapped in
854 // ok to throw above without closing file because the throw will terminate update_prebinding
855 int result1 = close(dst);
856 int result2 = close(src);
857 if ( (result1 != 0) || (result2 != 0) )
858 throw "can't close file";
859
860 return freespace;
861 }
862
863
864 static void printTime(const char* msg, uint64_t partTime, uint64_t totalTime)
865 {
866 static uint64_t sUnitsPerSecond = 0;
867 if ( sUnitsPerSecond == 0 ) {
868 struct mach_timebase_info timeBaseInfo;
869 if ( mach_timebase_info(&timeBaseInfo) == KERN_SUCCESS ) {
870 sUnitsPerSecond = 1000000000ULL * timeBaseInfo.denom / timeBaseInfo.numer;
871 }
872 }
873 if ( partTime < sUnitsPerSecond ) {
874 uint32_t milliSecondsTimeTen = (partTime*10000)/sUnitsPerSecond;
875 uint32_t milliSeconds = milliSecondsTimeTen/10;
876 uint32_t percentTimesTen = (partTime*1000)/totalTime;
877 uint32_t percent = percentTimesTen/10;
878 fprintf(stderr, "%s: %u.%u milliseconds (%u.%u%%)\n", msg, milliSeconds, milliSecondsTimeTen-milliSeconds*10, percent, percentTimesTen-percent*10);
879 }
880 else {
881 uint32_t secondsTimeTen = (partTime*10)/sUnitsPerSecond;
882 uint32_t seconds = secondsTimeTen/10;
883 uint32_t percentTimesTen = (partTime*1000)/totalTime;
884 uint32_t percent = percentTimesTen/10;
885 fprintf(stderr, "%s: %u.%u seconds (%u.%u%%)\n", msg, seconds, secondsTimeTen-seconds*10, percent, percentTimesTen-percent*10);
886 }
887 }
888
889 static char* commatize(uint64_t in, char* out)
890 {
891 char* result = out;
892 char rawNum[30];
893 sprintf(rawNum, "%llu", in);
894 const int rawNumLen = strlen(rawNum);
895 for(int i=0; i < rawNumLen-1; ++i) {
896 *out++ = rawNum[i];
897 if ( ((rawNumLen-i) % 3) == 1 )
898 *out++ = ',';
899 }
900 *out++ = rawNum[rawNumLen-1];
901 *out = '\0';
902 return result;
903 }
904
905
906 void ImageLoader::printStatistics(unsigned int imageCount)
907 {
908 uint64_t totalTime = fgTotalLoadLibrariesTime + fgTotalRebaseTime + fgTotalBindTime + fgTotalNotifyTime + fgTotalInitTime;
909 char commaNum1[40];
910 char commaNum2[40];
911
912 printTime("total time", totalTime, totalTime);
913 fprintf(stderr, "total images loaded: %d (%d used prebinding)\n", imageCount, fgImagesWithUsedPrebinding);
914 printTime("total images loading time", fgTotalLoadLibrariesTime, totalTime);
915 fprintf(stderr, "total rebase fixups: %s\n", commatize(fgTotalRebaseFixups, commaNum1));
916 printTime("total rebase fixups time", fgTotalRebaseTime, totalTime);
917 fprintf(stderr, "total binding fixups: %s\n", commatize(fgTotalBindFixups, commaNum1));
918 printTime("total binding fixups time", fgTotalBindTime, totalTime);
919 fprintf(stderr, "total bindings lazily fixed up: %s of %s\n", commatize(fgTotalLazyBindFixups, commaNum1), commatize(fgTotalPossibleLazyBindFixups, commaNum2));
920 printTime("total notify time time", fgTotalNotifyTime, totalTime);
921 printTime("total init time time", fgTotalInitTime, totalTime);
922 }
923
924
925 //
926 // copy path and add suffix to result
927 //
928 // /path/foo.dylib _debug => /path/foo_debug.dylib
929 // foo.dylib _debug => foo_debug.dylib
930 // foo _debug => foo_debug
931 // /path/bar _debug => /path/bar_debug
932 // /path/bar.A.dylib _debug => /path/bar.A_debug.dylib
933 //
934 void ImageLoader::addSuffix(const char* path, const char* suffix, char* result)
935 {
936 strcpy(result, path);
937
938 char* start = strrchr(result, '/');
939 if ( start != NULL )
940 start++;
941 else
942 start = result;
943
944 char* dot = strrchr(start, '.');
945 if ( dot != NULL ) {
946 strcpy(dot, suffix);
947 strcat(&dot[strlen(suffix)], &path[dot-result]);
948 }
949 else {
950 strcat(result, suffix);
951 }
952 }
953
954
955 void Segment::map(int fd, uint64_t offsetInFatWrapper, intptr_t slide, const ImageLoader::LinkContext& context)
956 {
957 vm_offset_t fileOffset = this->getFileOffset() + offsetInFatWrapper;
958 vm_size_t size = this->getFileSize();
959 void* requestedLoadAddress = (void*)(this->getPreferredLoadAddress() + slide);
960 int protection = 0;
961 if ( !this->unaccessible() ) {
962 if ( this->executable() )
963 protection |= PROT_EXEC;
964 if ( this->readable() )
965 protection |= PROT_READ;
966 if ( this->writeable() )
967 protection |= PROT_WRITE;
968 }
969 void* loadAddress = mmap(requestedLoadAddress, size, protection, MAP_FILE | MAP_FIXED | MAP_PRIVATE, fd, fileOffset);
970 if ( loadAddress == ((void*)(-1)) )
971 throwf("mmap() error %d at address=0x%08lX, size=0x%08lX in Segment::map() mapping %s", errno, (uintptr_t)requestedLoadAddress, (uintptr_t)size, this->getImage()->getPath());
972
973 if ( context.verboseMapping )
974 fprintf(stderr, "%18s at %p->%p\n", this->getName(), loadAddress, (char*)loadAddress+this->getFileSize()-1);
975 }
976
977 void Segment::map(const void* memoryImage, intptr_t slide, const ImageLoader::LinkContext& context)
978 {
979 vm_address_t loadAddress = this->getPreferredLoadAddress() + slide;
980 vm_address_t srcAddr = (uintptr_t)memoryImage + this->getFileOffset();
981 vm_size_t size = this->getFileSize();
982 kern_return_t r = vm_copy(mach_task_self(), srcAddr, size, loadAddress);
983 if ( r != KERN_SUCCESS )
984 throw "can't map segment";
985
986 if ( context.verboseMapping )
987 fprintf(stderr, "%18s at %p->%p\n", this->getName(), (char*)loadAddress, (char*)loadAddress+this->getFileSize()-1);
988 }
989
990 void Segment::setPermissions()
991 {
992 vm_prot_t protection = 0;
993 if ( !this->unaccessible() ) {
994 if ( this->executable() )
995 protection |= VM_PROT_EXECUTE;
996 if ( this->readable() )
997 protection |= VM_PROT_READ;
998 if ( this->writeable() )
999 protection |= VM_PROT_WRITE;
1000 }
1001 vm_address_t addr = this->getActualLoadAddress();
1002 vm_size_t size = this->getSize();
1003 const bool setCurrentPermissions = false;
1004 kern_return_t r = vm_protect(mach_task_self(), addr, size, setCurrentPermissions, protection);
1005 if ( r != KERN_SUCCESS )
1006 throw "can't set vm permissions for mapped segment";
1007 }
1008
1009 void Segment::tempWritable()
1010 {
1011 vm_address_t addr = this->getActualLoadAddress();
1012 vm_size_t size = this->getSize();
1013 const bool setCurrentPermissions = false;
1014 kern_return_t r = vm_protect(mach_task_self(), addr, size, setCurrentPermissions, VM_PROT_WRITE | VM_PROT_READ);
1015 if ( r != KERN_SUCCESS )
1016 throw "can't set vm permissions for mapped segment";
1017 }
1018
1019
1020 bool Segment::hasTrailingZeroFill()
1021 {
1022 return ( this->writeable() && (this->getSize() > this->getFileSize()) );
1023 }
1024
1025
1026 uintptr_t Segment::reserveAnAddressRange(size_t length, const ImageLoader::LinkContext& context)
1027 {
1028 vm_address_t addr = 0;
1029 vm_size_t size = length;
1030 if ( context.slideAndPackDylibs ) {
1031 addr = (fgNextNonSplitSegAddress - length) & (-4096); // page align
1032 kern_return_t r = vm_allocate(mach_task_self(), &addr, size, false /*use this range*/);
1033 if ( r != KERN_SUCCESS )
1034 throw "out of address space";
1035 fgNextNonSplitSegAddress = addr;
1036 }
1037 else {
1038 kern_return_t r = vm_allocate(mach_task_self(), &addr, size, true /*find range*/);
1039 if ( r != KERN_SUCCESS )
1040 throw "out of address space";
1041 }
1042 return addr;
1043 }
1044
1045 bool Segment::reserveAddressRange(uintptr_t start, size_t length)
1046 {
1047 vm_address_t addr = start;
1048 vm_size_t size = length;
1049 kern_return_t r = vm_allocate(mach_task_self(), &addr, size, false /*only this range*/);
1050 if ( r != KERN_SUCCESS )
1051 return false;
1052 return true;
1053 }
1054
1055
1056