]> git.saurik.com Git - apple/dyld.git/blob - src/ImageLoaderMachO.cpp
ed7c64569f7b762720077bd45f75422c7c89b71f
[apple/dyld.git] / src / ImageLoaderMachO.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 #include <string.h>
26 #include <fcntl.h>
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <sys/mman.h>
30 #include <mach/shared_memory_server.h>
31 #include <mach/mach.h>
32 #include <mach/thread_status.h>
33 #include <mach-o/loader.h>
34 #include <mach-o/reloc.h>
35 #include <mach-o/nlist.h>
36 #include <sys/sysctl.h>
37 #include <libkern/OSAtomic.h>
38 #if __ppc__ || __ppc64__
39 #include <mach-o/ppc/reloc.h>
40 #endif
41 #if __x86_64__
42 #include <mach-o/x86_64/reloc.h>
43 #endif
44
45 #ifndef S_ATTR_SELF_MODIFYING_CODE
46 #define S_ATTR_SELF_MODIFYING_CODE 0x04000000
47 #endif
48
49 #include "ImageLoaderMachO.h"
50 #include "mach-o/dyld_gdb.h"
51
52 // no header for this yet, rdar://problem/3850825
53 extern "C" void sys_icache_invalidate(void *, size_t);
54
55 // optimize strcmp for ppc
56 #if __ppc__
57 #include <ppc_intrinsics.h>
58 #else
59 #define astrcmp(a,b) strcmp(a,b)
60 #endif
61
62 // relocation_info.r_length field has value 3 for 64-bit executables and value 2 for 32-bit executables
63 #if __LP64__
64 #define RELOC_SIZE 3
65 #define LC_SEGMENT_COMMAND LC_SEGMENT_64
66 #define LC_ROUTINES_COMMAND LC_ROUTINES_64
67 struct macho_header : public mach_header_64 {};
68 struct macho_segment_command : public segment_command_64 {};
69 struct macho_section : public section_64 {};
70 struct macho_nlist : public nlist_64 {};
71 struct macho_routines_command : public routines_command_64 {};
72 #else
73 #define RELOC_SIZE 2
74 #define LC_SEGMENT_COMMAND LC_SEGMENT
75 #define LC_ROUTINES_COMMAND LC_ROUTINES
76 struct macho_header : public mach_header {};
77 struct macho_segment_command : public segment_command {};
78 struct macho_section : public section {};
79 struct macho_nlist : public nlist {};
80 struct macho_routines_command : public routines_command {};
81 #endif
82
83 #if __x86_64__
84 #define POINTER_RELOC X86_64_RELOC_UNSIGNED
85 #else
86 #define POINTER_RELOC GENERIC_RELOC_VANILLA
87 #endif
88
89 uint32_t ImageLoaderMachO::fgHintedBinaryTreeSearchs = 0;
90 uint32_t ImageLoaderMachO::fgUnhintedBinaryTreeSearchs = 0;
91 uint32_t ImageLoaderMachO::fgCountOfImagesWithWeakExports = 0;
92
93
94 //#define LINKEDIT_USAGE_DEBUG 1
95
96 #if LINKEDIT_USAGE_DEBUG
97 #include <set>
98 static std::set<uintptr_t> sLinkEditPageBuckets;
99
100 namespace dyld {
101 extern ImageLoader* findImageContainingAddress(const void* addr);
102 };
103
104 static void noteAccessedLinkEditAddress(const void* addr)
105 {
106 uintptr_t page = ((uintptr_t)addr) & (-4096);
107 sLinkEditPageBuckets.insert(page);
108 fprintf(stderr, "dyld: accessing page 0x%08lX in __LINKEDIT of %s\n", page, dyld::findImageContainingAddress(addr)->getPath());
109 }
110 #endif
111
112 // only way to share initialization in C++
113 void ImageLoaderMachO::init()
114 {
115 fMachOData = NULL;
116 fLinkEditBase = NULL;
117 fSymbolTable = NULL;
118 fStrings = NULL;
119 fDynamicInfo = NULL;
120 fSlide = 0;
121 fIsSplitSeg = false;
122 fHasSubLibraries= false;
123 fHasSubUmbrella = false;
124 fDashInit = NULL;
125 fModInitSection = NULL;
126 fModTermSection = NULL;
127 fDATAdyld = NULL;
128 fImageNotifySection = NULL;
129 fTwoLevelHints = NULL;
130 fDylibID = NULL;
131 fReExportThruFramework = NULL;
132 fTextSegmentWithFixups = NULL;
133 }
134
135 // create image by copying an in-memory mach-o file
136 ImageLoaderMachO::ImageLoaderMachO(const char* moduleName, const struct mach_header* mh, uint64_t len, const LinkContext& context)
137 : ImageLoader(moduleName)
138 {
139 // clean slate
140 this->init();
141
142 // temporary use this buffer until TEXT is mapped in
143 fMachOData = (const uint8_t*)mh;
144
145 // create segments
146 this->instantiateSegments((const uint8_t*)mh);
147
148 // map segments
149 if ( mh->filetype != MH_EXECUTE )
150 ImageLoader::mapSegments((const void*)mh, len, context);
151
152 // get pointers to interesting things
153 this->parseLoadCmds();
154 }
155
156
157 // create image by mapping in a mach-o file
158 ImageLoaderMachO::ImageLoaderMachO(const char* path, int fd, const uint8_t firstPage[4096], uint64_t offsetInFat,
159 uint64_t lenInFat, const struct stat& info, const LinkContext& context)
160 : ImageLoader(path, offsetInFat, info)
161 {
162 // clean slate
163 this->init();
164
165 // read load commands
166 const unsigned int dataSize = sizeof(macho_header) + ((macho_header*)firstPage)->sizeofcmds;
167 uint8_t buffer[dataSize];
168 const uint8_t* fileData = firstPage;
169 if ( dataSize > 4096 ) {
170 // only read more if cmds take up more space than first page
171 fileData = buffer;
172 memcpy(buffer, firstPage, 4096);
173 pread(fd, &buffer[4096], dataSize-4096, offsetInFat+4096);
174 }
175
176 // temporary use this buffer until TEXT is mapped in
177 fMachOData = fileData;
178
179 // the meaning of many fields changes in split seg mach-o files
180 fIsSplitSeg = ((((macho_header*)fileData)->flags & MH_SPLIT_SEGS) != 0) && (((macho_header*)fileData)->filetype == MH_DYLIB);
181
182 // create segments
183 this->instantiateSegments(fileData);
184
185 // map segments, except for main executable which is already mapped in by kernel
186 if ( ((macho_header*)fileData)->filetype != MH_EXECUTE )
187 this->mapSegments(fd, offsetInFat, lenInFat, info.st_size, context);
188
189 // get pointers to interesting things
190 this->parseLoadCmds();
191 }
192
193 ImageLoaderMachO::~ImageLoaderMachO()
194 {
195 // keep count of images with weak exports
196 if ( this->hasCoalescedExports() )
197 --fgCountOfImagesWithWeakExports;
198 }
199
200
201
202 void ImageLoaderMachO::instantiateSegments(const uint8_t* fileData)
203 {
204 const uint32_t cmd_count = ((macho_header*)fileData)->ncmds;
205 const struct load_command* const cmds = (struct load_command*)&fileData[sizeof(macho_header)];
206
207 // construct Segment object for each LC_SEGMENT cmd and add to list
208 const struct load_command* cmd = cmds;
209 for (unsigned long i = 0; i < cmd_count; ++i) {
210 if ( cmd->cmd == LC_SEGMENT_COMMAND ) {
211 if ( (((struct macho_segment_command*)cmd)->vmsize != 0) || !fIsSplitSeg )
212 fSegments.push_back(new SegmentMachO((struct macho_segment_command*)cmd, this, fileData));
213 }
214 cmd = (const struct load_command*)(((char*)cmd)+cmd->cmdsize);
215 }
216 }
217
218
219
220 bool ImageLoaderMachO::segmentsMustSlideTogether() const
221 {
222 return true;
223 }
224
225 bool ImageLoaderMachO::segmentsCanSlide() const
226 {
227 const macho_header* mh = (macho_header*)fMachOData;
228 return ( (mh->filetype == MH_DYLIB) || (mh->filetype == MH_BUNDLE) );
229 }
230
231 bool ImageLoaderMachO::isBundle() const
232 {
233 const macho_header* mh = (macho_header*)fMachOData;
234 return ( mh->filetype == MH_BUNDLE );
235 }
236
237 bool ImageLoaderMachO::isDylib() const
238 {
239 const macho_header* mh = (macho_header*)fMachOData;
240 return ( mh->filetype == MH_DYLIB );
241 }
242
243 bool ImageLoaderMachO::forceFlat() const
244 {
245 const macho_header* mh = (macho_header*)fMachOData;
246 return ( (mh->flags & MH_FORCE_FLAT) != 0 );
247 }
248
249 bool ImageLoaderMachO::usesTwoLevelNameSpace() const
250 {
251 const macho_header* mh = (macho_header*)fMachOData;
252 return ( (mh->flags & MH_TWOLEVEL) != 0 );
253 }
254
255 bool ImageLoaderMachO::isPrebindable() const
256 {
257 const macho_header* mh = (macho_header*)fMachOData;
258 return ( (mh->flags & MH_PREBOUND) != 0 );
259 }
260
261 bool ImageLoaderMachO::hasCoalescedExports() const
262 {
263 const macho_header* mh = (macho_header*)fMachOData;
264 return ( (mh->flags & MH_WEAK_DEFINES) != 0 );
265 }
266
267 bool ImageLoaderMachO::needsCoalescing() const
268 {
269 const macho_header* mh = (macho_header*)fMachOData;
270 return ( (mh->flags & MH_BINDS_TO_WEAK) != 0 );
271 }
272
273 #if !__LP64__ // split segs not supported for 64-bits
274
275 #if 1 // hack until kernel headers and glue are in system
276 struct _shared_region_mapping_np {
277 mach_vm_address_t address;
278 mach_vm_size_t size;
279 mach_vm_offset_t file_offset;
280 vm_prot_t max_prot; /* read/write/execute/COW/ZF */
281 vm_prot_t init_prot; /* read/write/execute/COW/ZF */
282 };
283 struct _shared_region_range_np {
284 mach_vm_address_t address;
285 mach_vm_size_t size;
286 };
287
288 // Called by dyld.
289 // Requests the kernel to map a number of regions from the fd into the
290 // shared sections address range (0x90000000-0xAFFFFFFF).
291 // If shared_region_make_private_np() has not been called by this process,
292 // the file mapped in is seen in the address space of all processes that
293 // participate in using the shared region.
294 // If shared_region_make_private_np() _has_ been called by this process,
295 // the file mapped in is only seen by this process.
296 // If the slide parameter is not NULL and then regions cannot be mapped
297 // as requested, the kernel will try to map the file in at a different
298 // address in the shared region and return the distance slid.
299 // If the mapping requesting cannot be fulfilled, returns non-zero.
300 static int
301 _shared_region_map_file_np(
302 int fd, // file descriptor to map into shared region
303 unsigned int regionCount, // number of entres in array of regions
304 const _shared_region_mapping_np regions[], // the array of regions to map
305 uint64_t* slide) // the amount all regions were slid, NULL means don't attempt to slide
306 {
307 //fprintf(stderr, "%s(%i, %u, %8p, %8p)\n", __func__, fd, regionCount, regions, slide);
308 //for ( unsigned int i=0; i < regionCount; ++i) {
309 // fprintf(stderr, "\taddress=0x%08llX, size=0x%08llX\n", regions[i].address, regions[i].size);
310 //}
311 int r = syscall(299, fd, regionCount, regions, slide);
312 // if(0 != r)
313 // fprintf(stderr, "%s(%i, %u, %8p, %8p) errno=%i (%s)\n", __func__, fd, regionCount, regions, slide, errno, strerror(errno));
314 return r;
315 }
316 // Called by dyld if shared_region_map_file() fails.
317 // Requests the kernel to take this process out of using the shared region.
318 // The specified ranges are created as private copies from the shared region for this process.
319 static int
320 _shared_region_make_private_np(
321 unsigned int rangeCount, // number of entres in array of msrp_range
322 const _shared_region_range_np ranges[]) // the array of shared regions to make private
323 {
324 //fprintf(stderr, "%s(%u, %8p)\n", __func__, rangeCount, ranges);
325 int r = syscall(300, rangeCount, ranges);
326 // if(0 != r)
327 // fprintf(stderr, "%s(%u, %8p) errno=%i (%s)\n", __func__, rangeCount, ranges, errno, strerror(errno));
328 return r;
329 }
330 #define KERN_SHREG_PRIVATIZABLE 54
331 #endif // hack until kernel headers and glue are in system
332
333 static uintptr_t sNextAltLoadAddress
334 #if __ppc_
335 = 0xC0000000;
336 #else
337 = 0;
338 #endif
339
340 static int
341 _shared_region_map_file_with_mmap(
342 int fd, // file descriptor to map into shared region
343 unsigned int regionCount, // number of entres in array of regions
344 const _shared_region_mapping_np regions[]) // the array of regions to map
345 {
346 // map in each region
347 for(unsigned int i=0; i < regionCount; ++i) {
348 void* mmapAddress = (void*)(uintptr_t)(regions[i].address);
349 size_t size = regions[i].size;
350 if ( (regions[i].init_prot & VM_PROT_ZF) != 0 ) {
351 // do nothing already vm_allocate() which zero fills
352 }
353 else {
354 int protection = 0;
355 if ( regions[i].init_prot & VM_PROT_EXECUTE )
356 protection |= PROT_EXEC;
357 if ( regions[i].init_prot & VM_PROT_READ )
358 protection |= PROT_READ;
359 if ( regions[i].init_prot & VM_PROT_WRITE )
360 protection |= PROT_WRITE;
361 off_t offset = regions[i].file_offset;
362 //fprintf(stderr, "mmap(%p, 0x%08lX, block=0x%08X, %s\n", mmapAddress, size, biggestDiff, fPath);
363 mmapAddress = mmap(mmapAddress, size, protection, MAP_FILE | MAP_FIXED | MAP_PRIVATE, fd, offset);
364 if ( mmapAddress == ((void*)(-1)) )
365 throw "mmap error";
366 }
367 }
368
369 return 0;
370 }
371
372
373 static
374 bool
375 hasSharedRegionMapFile(void)
376 {
377 int mib[CTL_MAXNAME];
378 int value = 0;
379 size_t size;
380
381 mib[0] = CTL_KERN;
382 mib[1] = KERN_SHREG_PRIVATIZABLE;
383 size = sizeof (int);
384 if (sysctl(mib, 2, &value, &size, NULL, 0) != 0) {
385 value = 0;
386 }
387
388 return 0 != value;
389 }
390
391 int
392 ImageLoaderMachO::sharedRegionMapFilePrivateOutside(int fd,
393 uint64_t offsetInFat,
394 uint64_t lenInFat,
395 uint64_t fileLen,
396 const LinkContext& context)
397 {
398 const unsigned int segmentCount = fSegments.size();
399 const unsigned int extraZeroFillEntries = getExtraZeroFillEntriesCount();
400 const unsigned int regionCount = segmentCount+extraZeroFillEntries;
401 _shared_region_mapping_np regions[regionCount];
402 initMappingTable(offsetInFat, regions);
403 int r = -1;
404 // find space somewhere to allocate split seg
405 bool foundRoom = false;
406 vm_size_t biggestDiff = 0;
407 while ( ! foundRoom ) {
408 foundRoom = true;
409 for(unsigned int i=0; i < regionCount; ++i) {
410 vm_address_t addr = sNextAltLoadAddress + regions[i].address - regions[0].address;
411 vm_size_t size = regions[i].size ;
412 r = vm_allocate(mach_task_self(), &addr, size, false /*only this range*/);
413 if ( 0 != r ) {
414 // no room here, deallocate what has succeeded so far
415 for(unsigned int j=0; j < i; ++j) {
416 vm_address_t addr = sNextAltLoadAddress + regions[j].address - regions[0].address;
417 vm_size_t size = regions[j].size ;
418 (void)vm_deallocate(mach_task_self(), addr, size);
419 }
420 sNextAltLoadAddress += 0x00100000; // skip ahead 1MB and try again
421 if ( (sNextAltLoadAddress & 0xF0000000) == 0x90000000 )
422 sNextAltLoadAddress = 0xB0000000;
423 if ( (sNextAltLoadAddress & 0xF0000000) == 0xF0000000 )
424 throw "can't map split seg anywhere";
425 foundRoom = false;
426 break;
427 }
428 vm_size_t high = (regions[i].address + size - regions[0].address) & 0x0FFFFFFF;
429 if ( high > biggestDiff )
430 biggestDiff = high;
431 }
432 }
433
434 // map in each region
435 uintptr_t slide = sNextAltLoadAddress - regions[0].address;
436 this->setSlide(slide);
437 for(unsigned int i=0; i < regionCount; ++i) {
438 if ( (regions[i].init_prot & VM_PROT_ZF) != 0 ) {
439 // do nothing vm_allocate() zero-fills by default
440 }
441 else {
442 void* mmapAddress = (void*)(uintptr_t)(regions[i].address + slide);
443 size_t size = regions[i].size;
444 int protection = 0;
445 if ( regions[i].init_prot & VM_PROT_EXECUTE )
446 protection |= PROT_EXEC;
447 if ( regions[i].init_prot & VM_PROT_READ )
448 protection |= PROT_READ;
449 if ( regions[i].init_prot & VM_PROT_WRITE )
450 protection |= PROT_WRITE;
451 off_t offset = regions[i].file_offset;
452 //fprintf(stderr, "mmap(%p, 0x%08lX, block=0x%08X, %s\n", mmapAddress, size, biggestDiff, fPath);
453 mmapAddress = mmap(mmapAddress, size, protection, MAP_FILE | MAP_FIXED | MAP_PRIVATE, fd, offset);
454 if ( mmapAddress == ((void*)(-1)) )
455 throw "mmap error";
456 }
457 }
458 // set so next maps right after this one
459 sNextAltLoadAddress += biggestDiff;
460 sNextAltLoadAddress = (sNextAltLoadAddress + 4095) & (-4096);
461
462 // logging
463 if ( context.verboseMapping ) {
464 fprintf(stderr, "dyld: Mapping split-seg outside shared region, slid by 0x%08lX %s\n", this->fSlide, this->getPath());
465 for(unsigned int segIndex=0,entryIndex=0; segIndex < segmentCount; ++segIndex, ++entryIndex){
466 Segment* seg = fSegments[segIndex];
467 const _shared_region_mapping_np* entry = &regions[entryIndex];
468 if ( (entry->init_prot & VM_PROT_ZF) == 0 )
469 fprintf(stderr, "%18s at 0x%08lX->0x%08lX\n",
470 seg->getName(), seg->getActualLoadAddress(), seg->getActualLoadAddress()+seg->getFileSize()-1);
471 if ( entryIndex < (regionCount-1) ) {
472 const _shared_region_mapping_np* nextEntry = &regions[entryIndex+1];
473 if ( (nextEntry->init_prot & VM_PROT_ZF) != 0 ) {
474 uint64_t segOffset = nextEntry->address - entry->address;
475 fprintf(stderr, "%18s at 0x%08lX->0x%08lX (zerofill)\n",
476 seg->getName(), (uintptr_t)(seg->getActualLoadAddress() + segOffset), (uintptr_t)(seg->getActualLoadAddress() + segOffset + nextEntry->size - 1));
477 ++entryIndex;
478 }
479 }
480 }
481 }
482
483 return r;
484 }
485
486
487 void ImageLoaderMachO::mapSegments(int fd, uint64_t offsetInFat, uint64_t lenInFat, uint64_t fileLen, const LinkContext& context)
488 {
489 enum SharedRegionState
490 {
491 kSharedRegionStartState = 0,
492 kSharedRegionLoadFileState,
493 kSharedRegionMapFileState,
494 kSharedRegionMapFilePrivateState,
495 kSharedRegionMapFilePrivateMMapState,
496 kSharedRegionMapFilePrivateOutsideState,
497 };
498 static SharedRegionState sSharedRegionState = kSharedRegionStartState;
499
500 // non-split segment libraries handled by super class
501 if ( !fIsSplitSeg )
502 return ImageLoader::mapSegments(fd, offsetInFat, lenInFat, fileLen, context);
503
504 if ( kSharedRegionStartState == sSharedRegionState ) {
505 if ( hasSharedRegionMapFile() ) {
506 if ( context.slideAndPackDylibs ) {
507 sharedRegionMakePrivate(context);
508 // remove underlying submap and block out 0x90000000 to 0xAFFFFFFF
509 vm_address_t addr = (vm_address_t)0x90000000;
510 vm_deallocate(mach_task_self(), addr, 0x20000000);
511 vm_allocate(mach_task_self(), &addr, 0x20000000, false);
512 sSharedRegionState = kSharedRegionMapFilePrivateMMapState;
513 }
514 else if ( context.sharedRegionMode == kUsePrivateSharedRegion ) {
515 sharedRegionMakePrivate(context);
516 sSharedRegionState = kSharedRegionMapFilePrivateState;
517 }
518 else if ( context.sharedRegionMode == kDontUseSharedRegion ) {
519 sSharedRegionState = kSharedRegionMapFilePrivateOutsideState;
520 }
521 else {
522 sSharedRegionState = kSharedRegionMapFileState;
523 }
524 }
525 else {
526 sSharedRegionState = kSharedRegionLoadFileState;
527 }
528 }
529
530 if ( kSharedRegionLoadFileState == sSharedRegionState ) {
531 if ( 0 != sharedRegionLoadFile(fd, offsetInFat, lenInFat, fileLen, context) ) {
532 sSharedRegionState = kSharedRegionMapFilePrivateOutsideState;
533 }
534 }
535 else
536 if ( kSharedRegionMapFileState == sSharedRegionState ) {
537 if ( 0 != sharedRegionMapFile(fd, offsetInFat, lenInFat, fileLen, context) ) {
538 sharedRegionMakePrivate(context);
539 sSharedRegionState = kSharedRegionMapFilePrivateState;
540 }
541 }
542
543 if ( (kSharedRegionMapFilePrivateState == sSharedRegionState) || (kSharedRegionMapFilePrivateMMapState == sSharedRegionState) ) {
544 if ( 0 != sharedRegionMapFilePrivate(fd, offsetInFat, lenInFat, fileLen, context, (kSharedRegionMapFilePrivateMMapState == sSharedRegionState)) ) {
545 sSharedRegionState = kSharedRegionMapFilePrivateOutsideState;
546 }
547 }
548
549 if ( kSharedRegionMapFilePrivateOutsideState == sSharedRegionState ) {
550 if ( 0 != sharedRegionMapFilePrivateOutside(fd, offsetInFat, lenInFat, fileLen, context) ) {
551 throw "mapping error";
552 }
553 }
554 }
555
556 unsigned int
557 ImageLoaderMachO::getExtraZeroFillEntriesCount()
558 {
559 // calculate mapping entries
560 const unsigned int segmentCount = fSegments.size();
561 unsigned int extraZeroFillEntries = 0;
562 for(unsigned int i=0; i < segmentCount; ++i){
563 Segment* seg = fSegments[i];
564 if ( seg->hasTrailingZeroFill() )
565 ++extraZeroFillEntries;
566 }
567
568 return extraZeroFillEntries;
569 }
570
571 void
572 ImageLoaderMachO::initMappingTable(uint64_t offsetInFat,
573 _shared_region_mapping_np *mappingTable)
574 {
575 unsigned int segmentCount = fSegments.size();
576 for(unsigned int segIndex=0,entryIndex=0; segIndex < segmentCount; ++segIndex, ++entryIndex){
577 Segment* seg = fSegments[segIndex];
578 _shared_region_mapping_np* entry = &mappingTable[entryIndex];
579 entry->address = seg->getActualLoadAddress();
580 entry->size = seg->getFileSize();
581 entry->file_offset = seg->getFileOffset() + offsetInFat;
582 entry->init_prot = VM_PROT_NONE;
583 if ( !seg->unaccessible() ) {
584 if ( seg->executable() )
585 entry->init_prot |= VM_PROT_EXECUTE;
586 if ( seg->readable() )
587 entry->init_prot |= VM_PROT_READ;
588 if ( seg->writeable() )
589 entry->init_prot |= VM_PROT_WRITE | VM_PROT_COW;
590 }
591 entry->max_prot = entry->init_prot;
592 if ( seg->hasTrailingZeroFill() ) {
593 _shared_region_mapping_np* zfentry = &mappingTable[++entryIndex];
594 zfentry->address = entry->address + seg->getFileSize();
595 zfentry->size = seg->getSize() - seg->getFileSize();
596 zfentry->file_offset = 0;
597 zfentry->init_prot = entry->init_prot | VM_PROT_COW | VM_PROT_ZF;
598 zfentry->max_prot = zfentry->init_prot;
599 }
600 }
601 }
602
603 int
604 ImageLoaderMachO::sharedRegionMakePrivate(const LinkContext& context)
605 {
606 if ( context.verboseMapping )
607 fprintf(stderr, "dyld: making shared regions private\n");
608
609 // shared mapping failed, so make private copy of shared region and try mapping private
610 RegionsVector allRegions;
611 context.getAllMappedRegions(allRegions);
612 std::vector<_shared_region_range_np> splitSegRegions;
613 const unsigned int allRegiontCount = allRegions.size();
614 for(unsigned int i=0; i < allRegiontCount; ++i){
615 MappedRegion region = allRegions[i];
616 uint8_t highByte = region.address >> 28;
617 if ( (highByte == 9) || (highByte == 0xA) ) {
618 _shared_region_range_np splitRegion;
619 splitRegion.address = region.address;
620 splitRegion.size = region.size;
621 splitSegRegions.push_back(splitRegion);
622 }
623 }
624 int result = _shared_region_make_private_np(splitSegRegions.size(), &splitSegRegions[0]);
625 // notify gdb or other lurkers that this process is no longer using the shared region
626 dyld_all_image_infos.processDetachedFromSharedRegion = true;
627 return result;
628 }
629
630 int
631 ImageLoaderMachO::sharedRegionMapFile(int fd,
632 uint64_t offsetInFat,
633 uint64_t lenInFat,
634 uint64_t fileLen,
635 const LinkContext& context)
636 {
637 // build table of segments to map
638 const unsigned int segmentCount = fSegments.size();
639 const unsigned int extraZeroFillEntries = getExtraZeroFillEntriesCount();
640 const unsigned int mappingTableCount = segmentCount+extraZeroFillEntries;
641 _shared_region_mapping_np mappingTable[mappingTableCount];
642 initMappingTable(offsetInFat, mappingTable);
643 // uint64_t slide;
644 uint64_t *slidep = NULL;
645
646 // try to map it in shared
647 int r = _shared_region_map_file_np(fd, mappingTableCount, mappingTable, slidep);
648 if ( 0 == r ) {
649 if(NULL != slidep && 0 != *slidep) {
650 // update with actual load addresses
651 }
652 if ( context.verboseMapping ) {
653 fprintf(stderr, "dyld: Mapping split-seg shared %s\n", this->getPath());
654 for(unsigned int segIndex=0,entryIndex=0; segIndex < segmentCount; ++segIndex, ++entryIndex){
655 Segment* seg = fSegments[segIndex];
656 const _shared_region_mapping_np* entry = &mappingTable[entryIndex];
657 if ( (entry->init_prot & VM_PROT_ZF) == 0 )
658 fprintf(stderr, "%18s at 0x%08lX->0x%08lX\n",
659 seg->getName(), seg->getActualLoadAddress(), seg->getActualLoadAddress()+seg->getFileSize()-1);
660 if ( entryIndex < (mappingTableCount-1) ) {
661 const _shared_region_mapping_np* nextEntry = &mappingTable[entryIndex+1];
662 if ( (nextEntry->init_prot & VM_PROT_ZF) != 0 ) {
663 uint64_t segOffset = nextEntry->address - entry->address;
664 fprintf(stderr, "%18s at 0x%08lX->0x%08lX\n",
665 seg->getName(), (uintptr_t)(seg->getActualLoadAddress() + segOffset), (uintptr_t)(seg->getActualLoadAddress() + segOffset + nextEntry->size - 1));
666 ++entryIndex;
667 }
668 }
669 }
670 }
671 }
672 return r;
673 }
674
675
676 int
677 ImageLoaderMachO::sharedRegionMapFilePrivate(int fd,
678 uint64_t offsetInFat,
679 uint64_t lenInFat,
680 uint64_t fileLen,
681 const LinkContext& context,
682 bool usemmap)
683 {
684 const unsigned int segmentCount = fSegments.size();
685
686 // adjust base address of segments to pack next to last dylib
687 if ( context.slideAndPackDylibs ) {
688 uintptr_t lowestReadOnly = (uintptr_t)(-1);
689 uintptr_t lowestWritable = (uintptr_t)(-1);
690 for(unsigned int segIndex=0; segIndex < segmentCount; ++segIndex){
691 Segment* seg = fSegments[segIndex];
692 uintptr_t segEnd = seg->getActualLoadAddress();
693 if ( seg->writeable() ) {
694 if ( segEnd < lowestWritable )
695 lowestWritable = segEnd;
696 }
697 else {
698 if ( segEnd < lowestReadOnly )
699 lowestReadOnly = segEnd;
700 }
701 }
702 uintptr_t baseAddress;
703 if ( lowestWritable - 256*1024*1024 < lowestReadOnly )
704 baseAddress = lowestWritable - 256*1024*1024;
705 else
706 baseAddress = lowestReadOnly;
707 // record that we want dylb slid to fgNextSplitSegAddress
708 this->setSlide(fgNextSplitSegAddress - baseAddress);
709 }
710
711 // build table of segments to map
712 const unsigned int extraZeroFillEntries = getExtraZeroFillEntriesCount();
713 const unsigned int mappingTableCount = segmentCount+extraZeroFillEntries;
714 _shared_region_mapping_np mappingTable[mappingTableCount];
715 initMappingTable(offsetInFat, mappingTable);
716 uint64_t slide = 0;
717
718 // try map it in privately (don't allow sliding if we pre-calculated the load address to pack dylibs)
719 int r;
720 if ( usemmap )
721 r = _shared_region_map_file_with_mmap(fd, mappingTableCount, mappingTable);
722 else
723 r = _shared_region_map_file_np(fd, mappingTableCount, mappingTable, context.slideAndPackDylibs ? NULL : &slide);
724 if ( 0 == r ) {
725 if ( 0 != slide ) {
726 slide = (slide) & (-4096); // round down to page boundary
727 this->setSlide(slide);
728 }
729 if ( context.verboseMapping ) {
730 if ( slide == 0 )
731 fprintf(stderr, "dyld: Mapping split-seg un-shared %s\n", this->getPath());
732 else
733 fprintf(stderr, "dyld: Mapping split-seg un-shared slid by 0x%08llX %s\n", slide, this->getPath());
734 for(unsigned int segIndex=0,entryIndex=0; segIndex < segmentCount; ++segIndex, ++entryIndex){
735 Segment* seg = fSegments[segIndex];
736 const _shared_region_mapping_np* entry = &mappingTable[entryIndex];
737 if ( (entry->init_prot & VM_PROT_ZF) == 0 )
738 fprintf(stderr, "%18s at 0x%08lX->0x%08lX\n",
739 seg->getName(), seg->getActualLoadAddress(), seg->getActualLoadAddress()+seg->getFileSize()-1);
740 if ( entryIndex < (mappingTableCount-1) ) {
741 const _shared_region_mapping_np* nextEntry = &mappingTable[entryIndex+1];
742 if ( (nextEntry->init_prot & VM_PROT_ZF) != 0 ) {
743 uint64_t segOffset = nextEntry->address - entry->address;
744 fprintf(stderr, "%18s at 0x%08lX->0x%08lX (zerofill)\n",
745 seg->getName(), (uintptr_t)(seg->getActualLoadAddress() + segOffset), (uintptr_t)(seg->getActualLoadAddress() + segOffset + nextEntry->size - 1));
746 ++entryIndex;
747 }
748 }
749 }
750 }
751 if ( context.slideAndPackDylibs ) {
752 // calculate where next split-seg dylib can load
753 uintptr_t largestReadOnly = 0;
754 uintptr_t largestWritable = 0;
755 for (unsigned int segIndex=0; segIndex < segmentCount; ++segIndex) {
756 Segment* seg = fSegments[segIndex];
757 uintptr_t segEnd = seg->getActualLoadAddress()+seg->getSize();
758 segEnd = (segEnd+4095) & (-4096); // page align
759 if ( seg->writeable() ) {
760 if ( segEnd > largestWritable )
761 largestWritable = segEnd;
762 }
763 else {
764 if ( segEnd > largestReadOnly )
765 largestReadOnly = segEnd;
766 }
767 }
768 if ( largestWritable - 256*1024*1024 > largestReadOnly )
769 fgNextSplitSegAddress = largestWritable - 256*1024*1024;
770 else
771 fgNextSplitSegAddress = largestReadOnly;
772 }
773 }
774 if ( context.slideAndPackDylibs && (r != 0) )
775 throwf("can't rebase split-seg dylib %s because shared_region_map_file_np() returned %d", this->getPath(), r);
776
777 return r;
778 }
779
780
781 int
782 ImageLoaderMachO::sharedRegionLoadFile(int fd, uint64_t offsetInFat, uint64_t lenInFat, uint64_t fileLen, const LinkContext& context)
783 {
784
785 // map in split segment file at random address, then tell kernel to share it
786 void* loadAddress = 0;
787 loadAddress = mmap(NULL, fileLen, PROT_READ, MAP_FILE, fd, 0);
788 if ( loadAddress == ((void*)(-1)) )
789 throw "mmap error";
790
791 // calculate mapping entries
792 const unsigned int segmentCount = fSegments.size();
793 unsigned int extraZeroFillEntries = getExtraZeroFillEntriesCount();
794
795 // build table of segments to map
796 const unsigned int mappingTableCount = segmentCount+extraZeroFillEntries;
797 const uintptr_t baseAddress = fSegments[0]->getPreferredLoadAddress();
798 sf_mapping mappingTable[mappingTableCount];
799 initMappingTable(offsetInFat, mappingTable, baseAddress);
800
801
802 // use load_shared_file() to map all segments at once
803 int flags = 0; // might need to set NEW_LOCAL_SHARED_REGIONS on first use
804 static bool firstTime = true;
805 if ( firstTime ) {
806 // when NEW_LOCAL_SHARED_REGIONS bit is set, this process will get is own shared region
807 // this is used by Xcode to prevent development libraries from polluting the global shared segment
808 if ( context.sharedRegionMode == kUsePrivateSharedRegion )
809 flags |= NEW_LOCAL_SHARED_REGIONS;
810 firstTime = false;
811 }
812
813 caddr_t base_address = (caddr_t)baseAddress;
814 kern_return_t r;
815 r = load_shared_file( (char*)fPath, // path of file to map shared
816 (char*)loadAddress, // beginning of local copy of sharable pages in file
817 fileLen, // end of shareable pages in file
818 &base_address, // beginning of address range to map
819 mappingTableCount, // number of entres in array of sf_mapping
820 mappingTable, // the array of sf_mapping
821 &flags); // in/out flags
822 if ( 0 != r ) {
823 // try again but tell kernel it is ok to slide
824 flags |= ALTERNATE_LOAD_SITE;
825 r = load_shared_file((char*)fPath,(char*)loadAddress, fileLen, &base_address,
826 mappingTableCount, mappingTable, &flags);
827 }
828
829 // unmap file from random address now that they are (hopefully) mapped into the shared region
830 munmap(loadAddress, fileLen);
831
832 if ( 0 == r ) {
833 if ( base_address != (caddr_t)baseAddress )
834 this->setSlide((uintptr_t)base_address - baseAddress);
835 if ( context.verboseMapping ) {
836 if ( base_address != (caddr_t)baseAddress )
837 fprintf(stderr, "dyld: Mapping split-seg load_shared_alt_region %s\n", this->getPath());
838 else
839 fprintf(stderr, "dyld: Mapping split-seg load_shared %s\n", this->getPath());
840 for(unsigned int segIndex=0,entryIndex=0; segIndex < segmentCount; ++segIndex, ++entryIndex){
841 Segment* seg = fSegments[segIndex];
842 const sf_mapping* entry = &mappingTable[entryIndex];
843 if ( (entry->protection & VM_PROT_ZF) == 0 )
844 fprintf(stderr, "%18s at 0x%08lX->0x%08lX\n",
845 seg->getName(), seg->getActualLoadAddress(), seg->getActualLoadAddress()+seg->getFileSize()-1);
846 if ( entryIndex < (mappingTableCount-1) ) {
847 const sf_mapping* nextEntry = &mappingTable[entryIndex+1];
848 if ( (nextEntry->protection & VM_PROT_ZF) != 0 ) {
849 fprintf(stderr, "%18s at 0x%08lX->0x%08lX\n",
850 seg->getName(), (uintptr_t)(nextEntry->mapping_offset + base_address), (uintptr_t)(nextEntry->mapping_offset + base_address + nextEntry->size - 1));
851 ++entryIndex;
852 }
853 }
854 }
855 }
856 }
857 return r;
858 }
859 void
860 ImageLoaderMachO::initMappingTable(uint64_t offsetInFat,
861 sf_mapping *mappingTable,
862 uintptr_t baseAddress)
863 {
864 unsigned int segmentCount = fSegments.size();
865 for(unsigned int segIndex=0,entryIndex=0; segIndex < segmentCount; ++segIndex, ++entryIndex){
866 Segment* seg = fSegments[segIndex];
867 sf_mapping* entry = &mappingTable[entryIndex];
868 entry->mapping_offset = seg->getPreferredLoadAddress() - baseAddress;
869 entry->size = seg->getFileSize();
870 entry->file_offset = seg->getFileOffset() + offsetInFat;
871 entry->protection = VM_PROT_NONE;
872 if ( !seg->unaccessible() ) {
873 if ( seg->executable() )
874 entry->protection |= VM_PROT_EXECUTE;
875 if ( seg->readable() )
876 entry->protection |= VM_PROT_READ;
877 if ( seg->writeable() )
878 entry->protection |= VM_PROT_WRITE | VM_PROT_COW;
879 }
880
881 entry->cksum = 0;
882 if ( seg->hasTrailingZeroFill() ) {
883 sf_mapping* zfentry = &mappingTable[++entryIndex];
884 zfentry->mapping_offset = entry->mapping_offset + seg->getFileSize();
885 zfentry->size = seg->getSize() - seg->getFileSize();
886 zfentry->file_offset = 0;
887 zfentry->protection = entry->protection | VM_PROT_COW | VM_PROT_ZF;
888 zfentry->cksum = 0;
889 }
890 }
891 }
892
893 #endif // !__LP64__ split segs not supported for 64-bits
894
895
896 void ImageLoaderMachO::setSlide(intptr_t slide)
897 {
898 fSlide = slide;
899 }
900
901 void ImageLoaderMachO::parseLoadCmds()
902 {
903 // now that segments are mapped in, get real fMachOData, fLinkEditBase, and fSlide
904 const unsigned int segmentCount = fSegments.size();
905 for(unsigned int i=0; i < segmentCount; ++i){
906 Segment* seg = fSegments[i];
907 // set up pointer to __LINKEDIT segment
908 if ( strcmp(seg->getName(),"__LINKEDIT") == 0 )
909 fLinkEditBase = (uint8_t*)(seg->getActualLoadAddress() - seg->getFileOffset());
910 // __TEXT segment always starts at beginning of file and contains mach_header and load commands
911 if ( strcmp(seg->getName(),"__TEXT") == 0 ) {
912 if ( seg->hasFixUps() )
913 fTextSegmentWithFixups = (SegmentMachO*)seg;
914 }
915 // some segment always starts at beginning of file and contains mach_header and load commands
916 if ( (seg->getFileOffset() == 0) && (seg->getFileSize() != 0) ) {
917 fMachOData = (uint8_t*)(seg->getActualLoadAddress());
918 }
919 }
920
921 // keep count of prebound images with weak exports
922 if ( this->hasCoalescedExports() )
923 ++fgCountOfImagesWithWeakExports;
924
925 // walk load commands (mapped in at start of __TEXT segment)
926 const uint32_t cmd_count = ((macho_header*)fMachOData)->ncmds;
927 const struct load_command* const cmds = (struct load_command*)&fMachOData[sizeof(macho_header)];
928 const struct load_command* cmd = cmds;
929 for (uint32_t i = 0; i < cmd_count; ++i) {
930 switch (cmd->cmd) {
931 case LC_SYMTAB:
932 {
933 const struct symtab_command* symtab = (struct symtab_command*)cmd;
934 fStrings = (const char*)&fLinkEditBase[symtab->stroff];
935 fSymbolTable = (struct macho_nlist*)(&fLinkEditBase[symtab->symoff]);
936 }
937 break;
938 case LC_DYSYMTAB:
939 fDynamicInfo = (struct dysymtab_command*)cmd;
940 break;
941 case LC_SUB_UMBRELLA:
942 fHasSubUmbrella = true;
943 break;
944 case LC_SUB_FRAMEWORK:
945 {
946 const struct sub_framework_command* subf = (struct sub_framework_command*)cmd;
947 fReExportThruFramework = (char*)cmd + subf->umbrella.offset;
948 }
949 break;
950 case LC_SUB_LIBRARY:
951 fHasSubLibraries = true;
952 break;
953 case LC_ROUTINES_COMMAND:
954 fDashInit = (struct macho_routines_command*)cmd;
955 break;
956 case LC_SEGMENT_COMMAND:
957 {
958 const struct macho_segment_command* seg = (struct macho_segment_command*)cmd;
959 const bool isDataSeg = (strcmp(seg->segname, "__DATA") == 0);
960 const struct macho_section* const sectionsStart = (struct macho_section*)((char*)seg + sizeof(struct macho_segment_command));
961 const struct macho_section* const sectionsEnd = &sectionsStart[seg->nsects];
962 for (const struct macho_section* sect=sectionsStart; sect < sectionsEnd; ++sect) {
963 const uint8_t type = sect->flags & SECTION_TYPE;
964 if ( type == S_MOD_INIT_FUNC_POINTERS )
965 fModInitSection = sect;
966 else if ( type == S_MOD_TERM_FUNC_POINTERS )
967 fModTermSection = sect;
968 else if ( isDataSeg && (strcmp(sect->sectname, "__dyld") == 0) ) {
969 fDATAdyld = sect;
970 }
971 else if ( isDataSeg && (strcmp(sect->sectname, "__image_notify") == 0) )
972 fImageNotifySection = sect;
973 }
974 }
975 break;
976 case LC_TWOLEVEL_HINTS:
977 fTwoLevelHints = (struct twolevel_hints_command*)cmd;
978 break;
979 case LC_ID_DYLIB:
980 {
981 fDylibID = (struct dylib_command*)cmd;
982 }
983 break;
984 case LC_LOAD_WEAK_DYLIB:
985 // do nothing, just prevent LC_REQ_DYLD exception from occuring
986 break;
987 default:
988 if ( (cmd->cmd & LC_REQ_DYLD) != 0 )
989 throwf("unknown required load command 0x%08X", cmd->cmd);
990 }
991 cmd = (const struct load_command*)(((char*)cmd)+cmd->cmdsize);
992 }
993 }
994
995
996
997
998 const char* ImageLoaderMachO::getInstallPath() const
999 {
1000 if ( fDylibID != NULL ) {
1001 return (char*)fDylibID + fDylibID->dylib.name.offset;
1002 }
1003 return NULL;
1004 }
1005
1006 // test if this image is re-exported through parent (the image that loaded this one)
1007 bool ImageLoaderMachO::isSubframeworkOf(const LinkContext& context, const ImageLoader* parent) const
1008 {
1009 if ( fReExportThruFramework != NULL ) {
1010 // need to match LC_SUB_FRAMEWORK string against the leaf name of the install location of parent...
1011 const char* parentInstallPath = parent->getInstallPath();
1012 if ( parentInstallPath != NULL ) {
1013 const char* lastSlash = strrchr(parentInstallPath, '/');
1014 if ( lastSlash != NULL ) {
1015 if ( strcmp(&lastSlash[1], fReExportThruFramework) == 0 )
1016 return true;
1017 if ( context.imageSuffix != NULL ) {
1018 // when DYLD_IMAGE_SUFFIX is used, lastSlash string needs imageSuffix removed from end
1019 char reexportAndSuffix[strlen(context.imageSuffix)+strlen(fReExportThruFramework)+1];
1020 strcpy(reexportAndSuffix, fReExportThruFramework);
1021 strcat(reexportAndSuffix, context.imageSuffix);
1022 if ( strcmp(&lastSlash[1], reexportAndSuffix) == 0 )
1023 return true;
1024 }
1025 }
1026 }
1027 }
1028 return false;
1029 }
1030
1031 // test if child is re-exported
1032 bool ImageLoaderMachO::hasSubLibrary(const LinkContext& context, const ImageLoader* child) const
1033 {
1034 if ( fHasSubLibraries ) {
1035 // need to match LC_SUB_LIBRARY string against the leaf name (without extension) of the install location of child...
1036 const char* childInstallPath = child->getInstallPath();
1037 if ( childInstallPath != NULL ) {
1038 const char* lastSlash = strrchr(childInstallPath, '/');
1039 if ( lastSlash != NULL ) {
1040 const char* firstDot = strchr(lastSlash, '.');
1041 int len;
1042 if ( firstDot == NULL )
1043 len = strlen(lastSlash);
1044 else
1045 len = firstDot-lastSlash-1;
1046 char childLeafName[len+1];
1047 strncpy(childLeafName, &lastSlash[1], len);
1048 childLeafName[len] = '\0';
1049 const uint32_t cmd_count = ((macho_header*)fMachOData)->ncmds;
1050 const struct load_command* const cmds = (struct load_command*)&fMachOData[sizeof(macho_header)];
1051 const struct load_command* cmd = cmds;
1052 for (uint32_t i = 0; i < cmd_count; ++i) {
1053 switch (cmd->cmd) {
1054 case LC_SUB_LIBRARY:
1055 {
1056 const struct sub_library_command* lib = (struct sub_library_command*)cmd;
1057 const char* aSubLibName = (char*)cmd + lib->sub_library.offset;
1058 if ( strcmp(aSubLibName, childLeafName) == 0 )
1059 return true;
1060 if ( context.imageSuffix != NULL ) {
1061 // when DYLD_IMAGE_SUFFIX is used, childLeafName string needs imageSuffix removed from end
1062 char aSubLibNameAndSuffix[strlen(context.imageSuffix)+strlen(aSubLibName)+1];
1063 strcpy(aSubLibNameAndSuffix, aSubLibName);
1064 strcat(aSubLibNameAndSuffix, context.imageSuffix);
1065 if ( strcmp(aSubLibNameAndSuffix, childLeafName) == 0 )
1066 return true;
1067 }
1068 }
1069 break;
1070 }
1071 cmd = (const struct load_command*)(((char*)cmd)+cmd->cmdsize);
1072 }
1073 }
1074 }
1075 }
1076 if ( fHasSubUmbrella ) {
1077 // need to match LC_SUB_UMBRELLA string against the leaf name of install location of child...
1078 const char* childInstallPath = child->getInstallPath();
1079 if ( childInstallPath != NULL ) {
1080 const char* lastSlash = strrchr(childInstallPath, '/');
1081 if ( lastSlash != NULL ) {
1082 const uint32_t cmd_count = ((macho_header*)fMachOData)->ncmds;
1083 const struct load_command* const cmds = (struct load_command*)&fMachOData[sizeof(macho_header)];
1084 const struct load_command* cmd = cmds;
1085 for (uint32_t i = 0; i < cmd_count; ++i) {
1086 switch (cmd->cmd) {
1087 case LC_SUB_UMBRELLA:
1088 {
1089 const struct sub_umbrella_command* um = (struct sub_umbrella_command*)cmd;
1090 const char* aSubUmbrellaName = (char*)cmd + um->sub_umbrella.offset;
1091 if ( strcmp(aSubUmbrellaName, &lastSlash[1]) == 0 )
1092 return true;
1093 if ( context.imageSuffix != NULL ) {
1094 // when DYLD_IMAGE_SUFFIX is used, lastSlash string needs imageSuffix removed from end
1095 char umbrellaAndSuffix[strlen(context.imageSuffix)+strlen(aSubUmbrellaName)+1];
1096 strcpy(umbrellaAndSuffix, aSubUmbrellaName);
1097 strcat(umbrellaAndSuffix, context.imageSuffix);
1098 if ( strcmp(umbrellaAndSuffix, &lastSlash[1]) == 0 )
1099 return true;
1100 }
1101 }
1102 break;
1103 }
1104 cmd = (const struct load_command*)(((char*)cmd)+cmd->cmdsize);
1105 }
1106 }
1107 }
1108 }
1109 return false;
1110 }
1111
1112
1113 void* ImageLoaderMachO::getMain() const
1114 {
1115 const uint32_t cmd_count = ((macho_header*)fMachOData)->ncmds;
1116 const struct load_command* const cmds = (struct load_command*)&fMachOData[sizeof(macho_header)];
1117 const struct load_command* cmd = cmds;
1118 for (unsigned long i = 0; i < cmd_count; ++i) {
1119 switch (cmd->cmd) {
1120 case LC_UNIXTHREAD:
1121 {
1122 #if __ppc__
1123 const ppc_thread_state_t* registers = (ppc_thread_state_t*)(((char*)cmd) + 16);
1124 return (void*)registers->srr0;
1125 #elif __ppc64__
1126 const ppc_thread_state64_t* registers = (ppc_thread_state64_t*)(((char*)cmd) + 16);
1127 return (void*)registers->srr0;
1128 #elif __i386__
1129 const i386_thread_state_t* registers = (i386_thread_state_t*)(((char*)cmd) + 16);
1130 return (void*)registers->eip;
1131 #elif __x86_64__
1132 const x86_thread_state64_t* registers = (x86_thread_state64_t*)(((char*)cmd) + 16);
1133 return (void*)registers->rip;
1134 #else
1135 #warning need processor specific code
1136 #endif
1137 }
1138 break;
1139 }
1140 cmd = (const struct load_command*)(((char*)cmd)+cmd->cmdsize);
1141 }
1142 return NULL;
1143 }
1144
1145
1146 uint32_t ImageLoaderMachO::doGetDependentLibraryCount()
1147 {
1148 const uint32_t cmd_count = ((macho_header*)fMachOData)->ncmds;
1149 const struct load_command* const cmds = (struct load_command*)&fMachOData[sizeof(macho_header)];
1150 uint32_t count = 0;
1151 const struct load_command* cmd = cmds;
1152 for (unsigned long i = 0; i < cmd_count; ++i) {
1153 switch (cmd->cmd) {
1154 case LC_LOAD_DYLIB:
1155 case LC_LOAD_WEAK_DYLIB:
1156 ++count;
1157 break;
1158 }
1159 cmd = (const struct load_command*)(((char*)cmd)+cmd->cmdsize);
1160 }
1161 return count;
1162 }
1163
1164 void ImageLoaderMachO::doGetDependentLibraries(DependentLibrary libs[])
1165 {
1166 uint32_t index = 0;
1167 const uint32_t cmd_count = ((macho_header*)fMachOData)->ncmds;
1168 const struct load_command* const cmds = (struct load_command*)&fMachOData[sizeof(macho_header)];
1169 const struct load_command* cmd = cmds;
1170 for (unsigned long i = 0; i < cmd_count; ++i) {
1171 switch (cmd->cmd) {
1172 case LC_LOAD_DYLIB:
1173 case LC_LOAD_WEAK_DYLIB:
1174 {
1175 const struct dylib_command* dylib = (struct dylib_command*)cmd;
1176 DependentLibrary* lib = &libs[index++];
1177 lib->name = (char*)cmd + dylib->dylib.name.offset;
1178 //lib->name = strdup((char*)cmd + dylib->dylib.name.offset);
1179 lib->image = NULL;
1180 lib->info.checksum = dylib->dylib.timestamp;
1181 lib->info.minVersion = dylib->dylib.compatibility_version;
1182 lib->info.maxVersion = dylib->dylib.current_version;
1183 lib->required = (cmd->cmd == LC_LOAD_DYLIB);
1184 lib->checksumMatches = false;
1185 lib->isReExported = false;
1186 }
1187 break;
1188 }
1189 cmd = (const struct load_command*)(((char*)cmd)+cmd->cmdsize);
1190 }
1191 }
1192
1193 ImageLoader::LibraryInfo ImageLoaderMachO::doGetLibraryInfo()
1194 {
1195 LibraryInfo info;
1196 if ( fDylibID != NULL ) {
1197 info.minVersion = fDylibID->dylib.compatibility_version;
1198 info.maxVersion = fDylibID->dylib.current_version;
1199 info.checksum = fDylibID->dylib.timestamp;
1200 }
1201 else {
1202 info.minVersion = 0;
1203 info.maxVersion = 0;
1204 info.checksum = 0;
1205 }
1206 return info;
1207 }
1208
1209 uintptr_t ImageLoaderMachO::getFirstWritableSegmentAddress()
1210 {
1211 // in split segment libraries r_address is offset from first writable segment
1212 for (std::vector<class Segment*>::iterator it=fSegments.begin(); it != fSegments.end(); ++it) {
1213 if ( (*it)->writeable() ) {
1214 return (*it)->getActualLoadAddress();
1215 }
1216 }
1217 throw "no writable segment";
1218 }
1219
1220 uintptr_t ImageLoaderMachO::getRelocBase()
1221 {
1222 #if __x86_64__
1223 // r_address is offset from first writable segment
1224 return getFirstWritableSegmentAddress();
1225 #endif
1226 #if __ppc__ || __i386__
1227 if ( fIsSplitSeg ) {
1228 // in split segment libraries r_address is offset from first writable segment
1229 return getFirstWritableSegmentAddress();
1230 }
1231 #endif
1232
1233 // in non-split segment libraries r_address is offset from first segment
1234 return fSegments[0]->getActualLoadAddress();
1235 }
1236
1237 #if __ppc__
1238 static inline void otherRelocsPPC(uintptr_t* locationToFix, uint8_t relocationType, uint16_t otherHalf, uintptr_t slide)
1239 {
1240 // low 16 bits of 32-bit ppc instructions need fixing
1241 struct ppcInstruction { uint16_t opcode; int16_t immediateValue; };
1242 ppcInstruction* instruction = (ppcInstruction*)locationToFix;
1243 //uint32_t before = *((uint32_t*)locationToFix);
1244 switch ( relocationType )
1245 {
1246 case PPC_RELOC_LO16:
1247 instruction->immediateValue = ((otherHalf << 16) | instruction->immediateValue) + slide;
1248 break;
1249 case PPC_RELOC_HI16:
1250 instruction->immediateValue = ((((instruction->immediateValue << 16) | otherHalf) + slide) >> 16);
1251 break;
1252 case PPC_RELOC_HA16:
1253 int16_t signedOtherHalf = (int16_t)(otherHalf & 0xffff);
1254 uint32_t temp = (instruction->immediateValue << 16) + signedOtherHalf + slide;
1255 if ( (temp & 0x00008000) != 0 )
1256 temp += 0x00008000;
1257 instruction->immediateValue = temp >> 16;
1258 }
1259 //uint32_t after = *((uint32_t*)locationToFix);
1260 //fprintf(stderr, "dyld: ppc fixup %0p type %d from 0x%08X to 0x%08X\n", locationToFix, relocationType, before, after);
1261 }
1262 #endif
1263
1264 #if __ppc__ || __i386__
1265 void ImageLoaderMachO::resetPreboundLazyPointers(const LinkContext& context, uintptr_t relocBase)
1266 {
1267 // loop through all local (internal) relocation records looking for pre-bound-lazy-pointer values
1268 register const uintptr_t slide = this->fSlide;
1269 const relocation_info* const relocsStart = (struct relocation_info*)(&fLinkEditBase[fDynamicInfo->locreloff]);
1270 const relocation_info* const relocsEnd = &relocsStart[fDynamicInfo->nlocrel];
1271 for (const relocation_info* reloc=relocsStart; reloc < relocsEnd; ++reloc) {
1272 if ( (reloc->r_address & R_SCATTERED) != 0 ) {
1273 const struct scattered_relocation_info* sreloc = (struct scattered_relocation_info*)reloc;
1274 if (sreloc->r_length == RELOC_SIZE) {
1275 uintptr_t* locationToFix = (uintptr_t*)(sreloc->r_address + relocBase);
1276 switch(sreloc->r_type) {
1277 #if __ppc__
1278 case PPC_RELOC_PB_LA_PTR:
1279 *locationToFix = sreloc->r_value + slide;
1280 break;
1281 #endif
1282 #if __i386__
1283 case GENERIC_RELOC_PB_LA_PTR:
1284 *locationToFix = sreloc->r_value + slide;
1285 break;
1286 #endif
1287 }
1288 }
1289 }
1290 }
1291 }
1292 #endif
1293
1294 void ImageLoaderMachO::doRebase(const LinkContext& context)
1295 {
1296 // if prebound and loaded at prebound address, then no need to rebase
1297 if ( this->usablePrebinding(context) ) {
1298 // skip rebasing cause prebound and prebinding not disabled
1299 ++fgImagesWithUsedPrebinding; // bump totals for statistics
1300 return;
1301 }
1302
1303 // print why prebinding was not used
1304 if ( context.verbosePrebinding ) {
1305 if ( !this->isPrebindable() ) {
1306 fprintf(stderr, "dyld: image not prebound, so could not use prebinding in %s\n", this->getPath());
1307 }
1308 else if ( fSlide != 0 ) {
1309 fprintf(stderr, "dyld: image slid, so could not use prebinding in %s\n", this->getPath());
1310 }
1311 else if ( !this->allDependentLibrariesAsWhenPreBound() ) {
1312 fprintf(stderr, "dyld: dependent libraries changed, so could not use prebinding in %s\n", this->getPath());
1313 }
1314 else if ( !this->usesTwoLevelNameSpace() ){
1315 fprintf(stderr, "dyld: image uses flat-namespace so, parts of prebinding ignored %s\n", this->getPath());
1316 }
1317 else {
1318 fprintf(stderr, "dyld: environment variable disabled use of prebinding in %s\n", this->getPath());
1319 }
1320 }
1321
1322 // cache values that are used in the following loop
1323 const uintptr_t relocBase = this->getRelocBase();
1324 register const uintptr_t slide = this->fSlide;
1325
1326 #if __ppc__ || __i386__
1327 // if prebound and we got here, then prebinding is not valid, so reset all lazy pointers
1328 if ( this->isPrebindable() )
1329 this->resetPreboundLazyPointers(context, relocBase);
1330 #endif
1331
1332 // if loaded at preferred address, no rebasing necessary
1333 if ( slide == 0 )
1334 return;
1335
1336 // if there are __TEXT fixups, temporarily make __TEXT writable
1337 if ( fTextSegmentWithFixups != NULL )
1338 fTextSegmentWithFixups->tempWritable();
1339
1340 // loop through all local (internal) relocation records
1341 const relocation_info* const relocsStart = (struct relocation_info*)(&fLinkEditBase[fDynamicInfo->locreloff]);
1342 const relocation_info* const relocsEnd = &relocsStart[fDynamicInfo->nlocrel];
1343 for (const relocation_info* reloc=relocsStart; reloc < relocsEnd; ++reloc) {
1344 #if __x86_64__
1345 // only one kind of local relocation supported for x86_64
1346 if ( reloc->r_length != 3 )
1347 throw "bad local relocation length";
1348 if ( reloc->r_type != X86_64_RELOC_UNSIGNED )
1349 throw "unknown local relocation type";
1350 if ( reloc->r_pcrel != 0 )
1351 throw "bad local relocation pc_rel";
1352 if ( reloc->r_extern != 0 )
1353 throw "extern relocation found with local relocations";
1354 *((uintptr_t*)(reloc->r_address + relocBase)) += slide;
1355 #endif
1356 #if __ppc__ || __ppc64__ || __i386__
1357 if ( (reloc->r_address & R_SCATTERED) == 0 ) {
1358 if ( reloc->r_symbolnum == R_ABS ) {
1359 // ignore absolute relocations
1360 }
1361 else if (reloc->r_length == RELOC_SIZE) {
1362 switch(reloc->r_type) {
1363 case GENERIC_RELOC_VANILLA:
1364 *((uintptr_t*)(reloc->r_address + relocBase)) += slide;
1365 break;
1366 #if __ppc__
1367 case PPC_RELOC_HI16:
1368 case PPC_RELOC_LO16:
1369 case PPC_RELOC_HA16:
1370 // some tools leave object file relocations in linked images
1371 otherRelocsPPC((uintptr_t*)(reloc->r_address + relocBase), reloc->r_type, reloc[1].r_address, slide);
1372 ++reloc; // these relocations come in pairs, skip next
1373 break;
1374 #endif
1375 default:
1376 throw "unknown local relocation type";
1377 }
1378 }
1379 else {
1380 throw "bad local relocation length";
1381 }
1382 }
1383 else {
1384 const struct scattered_relocation_info* sreloc = (struct scattered_relocation_info*)reloc;
1385 if (sreloc->r_length == RELOC_SIZE) {
1386 uintptr_t* locationToFix = (uintptr_t*)(sreloc->r_address + relocBase);
1387 switch(sreloc->r_type) {
1388 case GENERIC_RELOC_VANILLA:
1389 *locationToFix += slide;
1390 break;
1391 #if __ppc__
1392 case PPC_RELOC_HI16:
1393 case PPC_RELOC_LO16:
1394 case PPC_RELOC_HA16:
1395 // Metrowerks compiler sometimes leaves object file relocations in linked images???
1396 ++reloc; // these relocations come in pairs, get next one
1397 otherRelocsPPC(locationToFix, sreloc->r_type, reloc->r_address, slide);
1398 break;
1399 #endif
1400 #if __ppc__ || __ppc64__
1401 case PPC_RELOC_PB_LA_PTR:
1402 // do nothing
1403 break;
1404 #endif
1405 #if __i386__
1406 case GENERIC_RELOC_PB_LA_PTR:
1407 // do nothing
1408 break;
1409 #endif
1410 default:
1411 throw "unknown local scattered relocation type";
1412 }
1413 }
1414 else {
1415 throw "bad local scattered relocation length";
1416 }
1417 }
1418 #endif
1419 }
1420
1421 // if there were __TEXT fixups, restore write protection
1422 if ( fTextSegmentWithFixups != NULL ) {
1423 fTextSegmentWithFixups->setPermissions();
1424 sys_icache_invalidate((void*)fTextSegmentWithFixups->getActualLoadAddress(), fTextSegmentWithFixups->getSize());
1425 }
1426
1427 // update stats
1428 fgTotalRebaseFixups += fDynamicInfo->nlocrel;
1429 }
1430
1431
1432 const struct macho_nlist* ImageLoaderMachO::binarySearchWithToc(const char* key, const char stringPool[], const struct macho_nlist symbols[],
1433 const struct dylib_table_of_contents toc[], uint32_t symbolCount, uint32_t hintIndex)
1434 {
1435 int32_t high = symbolCount-1;
1436 int32_t mid = hintIndex;
1437
1438 // handle out of range hint
1439 if ( mid >= (int32_t)symbolCount ) {
1440 mid = symbolCount/2;
1441 ++ImageLoaderMachO::fgUnhintedBinaryTreeSearchs;
1442 }
1443 else {
1444 ++ImageLoaderMachO::fgHintedBinaryTreeSearchs;
1445 }
1446
1447 for (int32_t low = 0; low <= high; mid = (low+high)/2) {
1448 const uint32_t index = toc[mid].symbol_index;
1449 const struct macho_nlist* pivot = &symbols[index];
1450 const char* pivotStr = &stringPool[pivot->n_un.n_strx];
1451 #if LINKEDIT_USAGE_DEBUG
1452 noteAccessedLinkEditAddress(&toc[mid]);
1453 noteAccessedLinkEditAddress(pivot);
1454 noteAccessedLinkEditAddress(pivotStr);
1455 #endif
1456 int cmp = astrcmp(key, pivotStr);
1457 if ( cmp == 0 )
1458 return pivot;
1459 if ( cmp > 0 ) {
1460 // key > pivot
1461 low = mid + 1;
1462 }
1463 else {
1464 // key < pivot
1465 high = mid - 1;
1466 }
1467 }
1468 return NULL;
1469 }
1470
1471 const struct macho_nlist* ImageLoaderMachO::binarySearch(const char* key, const char stringPool[], const struct macho_nlist symbols[], uint32_t symbolCount)
1472 {
1473 ++ImageLoaderMachO::fgUnhintedBinaryTreeSearchs;
1474 const struct macho_nlist* base = symbols;
1475 for (uint32_t n = symbolCount; n > 0; n /= 2) {
1476 const struct macho_nlist* pivot = &base[n/2];
1477 const char* pivotStr = &stringPool[pivot->n_un.n_strx];
1478 #if LINKEDIT_USAGE_DEBUG
1479 noteAccessedLinkEditAddress(pivot);
1480 noteAccessedLinkEditAddress(pivotStr);
1481 #endif
1482 int cmp = astrcmp(key, pivotStr);
1483 if ( cmp == 0 )
1484 return pivot;
1485 if ( cmp > 0 ) {
1486 // key > pivot
1487 // move base to symbol after pivot
1488 base = &pivot[1];
1489 --n;
1490 }
1491 else {
1492 // key < pivot
1493 // keep same base
1494 }
1495 }
1496 return NULL;
1497 }
1498
1499 const ImageLoader::Symbol* ImageLoaderMachO::findExportedSymbol(const char* name, const void* hint, bool searchReExports, ImageLoader** foundIn) const
1500 {
1501 const struct macho_nlist* sym = NULL;
1502 const struct twolevel_hint* theHint = (struct twolevel_hint*)hint;
1503 if ( fDynamicInfo->tocoff == 0 )
1504 sym = binarySearch(name, fStrings, &fSymbolTable[fDynamicInfo->iextdefsym], fDynamicInfo->nextdefsym);
1505 else {
1506 uint32_t start = fDynamicInfo->nextdefsym;
1507 if ( theHint != NULL )
1508 start = theHint->itoc;
1509 if ( (theHint == NULL) || (theHint->isub_image == 0) ) {
1510 sym = binarySearchWithToc(name, fStrings, fSymbolTable, (dylib_table_of_contents*)&fLinkEditBase[fDynamicInfo->tocoff],
1511 fDynamicInfo->ntoc, start);
1512 }
1513 }
1514 if ( sym != NULL ) {
1515 if ( foundIn != NULL )
1516 *foundIn = (ImageLoader*)this;
1517
1518 return (const Symbol*)sym;
1519 }
1520
1521 if ( searchReExports ) {
1522 // hint might tell us to try a particular subimage
1523 if ( (theHint != NULL) && (theHint->isub_image > 0) && (theHint->isub_image <= fLibrariesCount) ) {
1524 // isub_image is an index into a list that is sorted non-rexported images first
1525 uint32_t index = 0;
1526 ImageLoader* target = NULL;
1527 // pass one, only look at sub-frameworks
1528 for (uint32_t i=0; i < fLibrariesCount; ++i) {
1529 DependentLibrary& libInfo = fLibraries[i];
1530 if ( libInfo.isSubFramework && (libInfo.image != NULL)) {
1531 if ( ++index == theHint->isub_image ) {
1532 target = libInfo.image;
1533 break;
1534 }
1535 }
1536 }
1537 if (target != NULL) {
1538 // pass two, only look at non-sub-framework-reexports
1539 for (uint32_t i=0; i < fLibrariesCount; ++i) {
1540 DependentLibrary& libInfo = fLibraries[i];
1541 if ( libInfo.isReExported && !libInfo.isSubFramework && (libInfo.image != NULL) ) {
1542 if ( ++index == theHint->isub_image ) {
1543 target = libInfo.image;
1544 break;
1545 }
1546 }
1547 }
1548 }
1549 if (target != NULL) {
1550 const Symbol* result = target->findExportedSymbol(name, NULL, searchReExports, foundIn);
1551 if ( result != NULL )
1552 return result;
1553 }
1554 }
1555
1556 // hint failed, try all sub images
1557 // pass one, only look at sub-frameworks
1558 for(unsigned int i=0; i < fLibrariesCount; ++i){
1559 DependentLibrary& libInfo = fLibraries[i];
1560 if ( (libInfo.image != NULL) && libInfo.isSubFramework ) {
1561 const Symbol* result = libInfo.image->findExportedSymbol(name, NULL, searchReExports, foundIn);
1562 if ( result != NULL )
1563 return result;
1564 }
1565 }
1566 // pass two, only look at non-sub-framework-reexports
1567 for(unsigned int i=0; i < fLibrariesCount; ++i){
1568 DependentLibrary& libInfo = fLibraries[i];
1569 if ( (libInfo.image != NULL) && libInfo.isReExported && !libInfo.isSubFramework ) {
1570 const Symbol* result = libInfo.image->findExportedSymbol(name, NULL, searchReExports, foundIn);
1571 if ( result != NULL )
1572 return result;
1573 }
1574 }
1575 }
1576
1577 // last change: the hint is wrong (non-zero but actually in this image)
1578 if ( (theHint != NULL) && (theHint->isub_image != 0) ) {
1579 sym = binarySearchWithToc(name, fStrings, fSymbolTable, (dylib_table_of_contents*)&fLinkEditBase[fDynamicInfo->tocoff],
1580 fDynamicInfo->ntoc, fDynamicInfo->nextdefsym);
1581 if ( sym != NULL ) {
1582 if ( foundIn != NULL )
1583 *foundIn = (ImageLoader*)this;
1584 return (const Symbol*)sym;
1585 }
1586 }
1587
1588
1589 return NULL;
1590 }
1591
1592
1593 uintptr_t ImageLoaderMachO::getExportedSymbolAddress(const Symbol* sym) const
1594 {
1595 const struct macho_nlist* nlistSym = (const struct macho_nlist*)sym;
1596 return nlistSym->n_value + fSlide;
1597 }
1598
1599 ImageLoader::DefinitionFlags ImageLoaderMachO::getExportedSymbolInfo(const Symbol* sym) const
1600 {
1601 const struct macho_nlist* nlistSym = (const struct macho_nlist*)sym;
1602 if ( (nlistSym->n_desc & N_WEAK_DEF) != 0 )
1603 return kWeakDefinition;
1604 return kNoDefinitionOptions;
1605 }
1606
1607 const char* ImageLoaderMachO::getExportedSymbolName(const Symbol* sym) const
1608 {
1609 const struct macho_nlist* nlistSym = (const struct macho_nlist*)sym;
1610 return &fStrings[nlistSym->n_un.n_strx];
1611 }
1612
1613 uint32_t ImageLoaderMachO::getExportedSymbolCount() const
1614 {
1615 return fDynamicInfo->nextdefsym;
1616 }
1617
1618
1619 const ImageLoader::Symbol* ImageLoaderMachO::getIndexedExportedSymbol(uint32_t index) const
1620 {
1621 if ( index < fDynamicInfo->nextdefsym ) {
1622 const struct macho_nlist* sym = &fSymbolTable[fDynamicInfo->iextdefsym + index];
1623 return (const ImageLoader::Symbol*)sym;
1624 }
1625 return NULL;
1626 }
1627
1628
1629 uint32_t ImageLoaderMachO::getImportedSymbolCount() const
1630 {
1631 return fDynamicInfo->nundefsym;
1632 }
1633
1634
1635 const ImageLoader::Symbol* ImageLoaderMachO::getIndexedImportedSymbol(uint32_t index) const
1636 {
1637 if ( index < fDynamicInfo->nundefsym ) {
1638 const struct macho_nlist* sym = &fSymbolTable[fDynamicInfo->iundefsym + index];
1639 return (const ImageLoader::Symbol*)sym;
1640 }
1641 return NULL;
1642 }
1643
1644
1645 ImageLoader::ReferenceFlags ImageLoaderMachO::geImportedSymbolInfo(const ImageLoader::Symbol* sym) const
1646 {
1647 const struct macho_nlist* nlistSym = (const struct macho_nlist*)sym;
1648 ImageLoader::ReferenceFlags flags = kNoReferenceOptions;
1649 if ( ((nlistSym->n_type & N_TYPE) == N_UNDF) && (nlistSym->n_value != 0) )
1650 flags |= ImageLoader::kTentativeDefinition;
1651 if ( (nlistSym->n_desc & N_WEAK_REF) != 0 )
1652 flags |= ImageLoader::kWeakReference;
1653 return flags;
1654 }
1655
1656
1657 const char* ImageLoaderMachO::getImportedSymbolName(const ImageLoader::Symbol* sym) const
1658 {
1659 const struct macho_nlist* nlistSym = (const struct macho_nlist*)sym;
1660 return &fStrings[nlistSym->n_un.n_strx];
1661 }
1662
1663
1664 bool ImageLoaderMachO::getSectionContent(const char* segmentName, const char* sectionName, void** start, size_t* length)
1665 {
1666 const uint32_t cmd_count = ((macho_header*)fMachOData)->ncmds;
1667 const struct load_command* const cmds = (struct load_command*)&fMachOData[sizeof(macho_header)];
1668 const struct load_command* cmd = cmds;
1669 for (uint32_t i = 0; i < cmd_count; ++i) {
1670 switch (cmd->cmd) {
1671 case LC_SEGMENT_COMMAND:
1672 {
1673 const struct macho_segment_command* seg = (struct macho_segment_command*)cmd;
1674 const struct macho_section* const sectionsStart = (struct macho_section*)((char*)seg + sizeof(struct macho_segment_command));
1675 const struct macho_section* const sectionsEnd = &sectionsStart[seg->nsects];
1676 for (const struct macho_section* sect=sectionsStart; sect < sectionsEnd; ++sect) {
1677 if ( (strcmp(sect->segname, segmentName) == 0) && (strcmp(sect->sectname, sectionName) == 0) ) {
1678 *start = (uintptr_t*)(sect->addr + fSlide);
1679 *length = sect->size;
1680 return true;
1681 }
1682 }
1683 }
1684 break;
1685 }
1686 cmd = (const struct load_command*)(((char*)cmd)+cmd->cmdsize);
1687 }
1688 return false;
1689 }
1690
1691
1692 bool ImageLoaderMachO::findSection(const void* imageInterior, const char** segmentName, const char** sectionName, size_t* sectionOffset)
1693 {
1694 const uint32_t cmd_count = ((macho_header*)fMachOData)->ncmds;
1695 const struct load_command* const cmds = (struct load_command*)&fMachOData[sizeof(macho_header)];
1696 const struct load_command* cmd = cmds;
1697 const uintptr_t unslidInteriorAddress = (uintptr_t)imageInterior - this->getSlide();
1698 for (uint32_t i = 0; i < cmd_count; ++i) {
1699 switch (cmd->cmd) {
1700 case LC_SEGMENT_COMMAND:
1701 {
1702 const struct macho_segment_command* seg = (struct macho_segment_command*)cmd;
1703 if ( (unslidInteriorAddress >= seg->vmaddr) && (unslidInteriorAddress < (seg->vmaddr+seg->vmsize)) ) {
1704 const struct macho_section* const sectionsStart = (struct macho_section*)((char*)seg + sizeof(struct macho_segment_command));
1705 const struct macho_section* const sectionsEnd = &sectionsStart[seg->nsects];
1706 for (const struct macho_section* sect=sectionsStart; sect < sectionsEnd; ++sect) {
1707 if ((sect->addr <= unslidInteriorAddress) && (unslidInteriorAddress < (sect->addr+sect->size))) {
1708 if ( segmentName != NULL )
1709 *segmentName = sect->segname;
1710 if ( sectionName != NULL )
1711 *sectionName = sect->sectname;
1712 if ( sectionOffset != NULL )
1713 *sectionOffset = unslidInteriorAddress - sect->addr;
1714 return true;
1715 }
1716 }
1717 }
1718 }
1719 break;
1720 }
1721 cmd = (const struct load_command*)(((char*)cmd)+cmd->cmdsize);
1722 }
1723 return false;
1724 }
1725
1726
1727 bool ImageLoaderMachO::symbolRequiresCoalescing(const struct macho_nlist* symbol)
1728 {
1729 // if a define and weak ==> coalesced
1730 if ( ((symbol->n_type & N_TYPE) == N_SECT) && ((symbol->n_desc & N_WEAK_DEF) != 0) )
1731 return true;
1732 // if an undefine and not referencing a weak symbol ==> coalesced
1733 if ( ((symbol->n_type & N_TYPE) != N_SECT) && ((symbol->n_desc & N_REF_TO_WEAK) != 0) )
1734 return true;
1735
1736 // regular symbol
1737 return false;
1738 }
1739
1740
1741 static void __attribute__((noreturn)) throwSymbolNotFound(const char* symbol, const char* referencedFrom, const char* expectedIn)
1742 {
1743 const char* formatString = "Symbol not found: %s\n Referenced from: %s\n Expected in: %s\n";
1744 char buf[strlen(symbol)+strlen(referencedFrom)+strlen(expectedIn)+strlen(formatString)];
1745 sprintf(buf, formatString, symbol, referencedFrom, expectedIn);
1746 throw strdup(buf); // this is a leak if exception doesn't halt program
1747 }
1748
1749 uintptr_t ImageLoaderMachO::resolveUndefined(const LinkContext& context, const struct macho_nlist* undefinedSymbol, bool twoLevel, ImageLoader** foundIn)
1750 {
1751 const char* symbolName = &fStrings[undefinedSymbol->n_un.n_strx];
1752
1753 if ( context.bindFlat || !twoLevel ) {
1754 // flat lookup
1755 const Symbol* sym;
1756 if ( context.flatExportFinder(symbolName, &sym, foundIn) )
1757 return (*foundIn)->getExportedSymbolAddress(sym);
1758 // if a bundle is loaded privately the above will not find its exports
1759 if ( this->isBundle() && this->hasHiddenExports() ) {
1760 // look in self for needed symbol
1761 sym = this->findExportedSymbol(symbolName, NULL, false, foundIn);
1762 if ( sym != NULL )
1763 return (*foundIn)->getExportedSymbolAddress(sym);
1764 }
1765 if ( ((undefinedSymbol->n_type & N_PEXT) != 0) || ((undefinedSymbol->n_type & N_TYPE) == N_SECT) ) {
1766 // could be a multi-module private_extern internal reference
1767 // the static linker squirrels away the target address in n_value
1768 uintptr_t addr = undefinedSymbol->n_value + this->fSlide;
1769 *foundIn = this;
1770 return addr;
1771 }
1772 if ( (undefinedSymbol->n_desc & N_WEAK_REF) != 0 ) {
1773 // definition can't be found anywhere
1774 // if reference is weak_import, then it is ok, just return 0
1775 return 0;
1776 }
1777 throwSymbolNotFound(symbolName, this->getPath(), "flat namespace");
1778 }
1779 else {
1780 // symbol requires searching images with coalesced symbols
1781 if ( this->needsCoalescing() && symbolRequiresCoalescing(undefinedSymbol) ) {
1782 const Symbol* sym;
1783 if ( context.coalescedExportFinder(symbolName, &sym, foundIn) )
1784 return (*foundIn)->getExportedSymbolAddress(sym);
1785 //throwSymbolNotFound(symbolName, this->getPath(), "coalesced namespace");
1786 //fprintf(stderr, "dyld: coalesced symbol %s not found in any coalesced image, falling back to two-level lookup", symbolName);
1787 }
1788
1789 // two level lookup
1790 void* hint = NULL;
1791 ImageLoader* target = NULL;
1792 uint8_t ord = GET_LIBRARY_ORDINAL(undefinedSymbol->n_desc);
1793 if ( ord == EXECUTABLE_ORDINAL ) {
1794 target = context.mainExecutable;
1795 }
1796 else if ( ord == SELF_LIBRARY_ORDINAL ) {
1797 target = this;
1798 }
1799 else if ( ord == DYNAMIC_LOOKUP_ORDINAL ) {
1800 // rnielsen: HACKHACK
1801 // flat lookup
1802 const Symbol* sym;
1803 if ( context.flatExportFinder(symbolName, &sym, foundIn) )
1804 return (*foundIn)->getExportedSymbolAddress(sym);
1805 // no image has exports this symbol
1806 // either report error or hope ZeroLink can just-in-time load an image
1807 context.undefinedHandler(symbolName);
1808 // try looking again
1809 if ( context.flatExportFinder(symbolName, &sym, foundIn) )
1810 return (*foundIn)->getExportedSymbolAddress(sym);
1811
1812 throwSymbolNotFound(symbolName, this->getPath(), "dynamic lookup");
1813 }
1814 else if ( ord <= fLibrariesCount ) {
1815 DependentLibrary& libInfo = fLibraries[ord-1];
1816 target = libInfo.image;
1817 if ( (target == NULL) && (((undefinedSymbol->n_desc & N_WEAK_REF) != 0) || !libInfo.required) ) {
1818 // if target library not loaded and reference is weak or library is weak return 0
1819 return 0;
1820 }
1821 }
1822 else {
1823 throw "corrupt binary, library ordinal too big";
1824 }
1825
1826 if ( target == NULL ) {
1827 fprintf(stderr, "resolveUndefined(%s) in %s\n", symbolName, this->getPath());
1828 throw "symbol not found";
1829 }
1830
1831 // interpret hint
1832 if ( fTwoLevelHints != NULL ) {
1833 uint32_t symIndex = undefinedSymbol - fSymbolTable;
1834 int32_t undefinedIndex = symIndex - fDynamicInfo->iundefsym;
1835 if ( (undefinedIndex >= 0) && ((uint32_t)undefinedIndex < fDynamicInfo->nundefsym) ) {
1836 const struct twolevel_hint* hints = (struct twolevel_hint*)(&fLinkEditBase[fTwoLevelHints->offset]);
1837 const struct twolevel_hint* theHint = &hints[undefinedIndex];
1838 hint = (void*)theHint;
1839 }
1840 }
1841
1842 const Symbol* sym = target->findExportedSymbol(symbolName, hint, true, foundIn);
1843 if ( sym!= NULL ) {
1844 return (*foundIn)->getExportedSymbolAddress(sym);
1845 }
1846 else if ( (undefinedSymbol->n_type & N_PEXT) != 0 ) {
1847 // don't know why the static linker did not eliminate the internal reference to a private extern definition
1848 *foundIn = this;
1849 return undefinedSymbol->n_value + fSlide;
1850 }
1851 else if ( (undefinedSymbol->n_desc & N_WEAK_REF) != 0 ) {
1852 // if definition not found and reference is weak return 0
1853 return 0;
1854 }
1855
1856 // nowhere to be found
1857 throwSymbolNotFound(symbolName, this->getPath(), target->getPath());
1858 }
1859 }
1860
1861 // returns if 'addr' is within the address range of section 'sectionIndex'
1862 // fSlide is not used. 'addr' is assumed to be a prebound address in this image
1863 bool ImageLoaderMachO::isAddrInSection(uintptr_t addr, uint8_t sectionIndex)
1864 {
1865 uint8_t currentSectionIndex = 1;
1866 const uint32_t cmd_count = ((macho_header*)fMachOData)->ncmds;
1867 const struct load_command* const cmds = (struct load_command*)&fMachOData[sizeof(macho_header)];
1868 const struct load_command* cmd = cmds;
1869 for (unsigned long i = 0; i < cmd_count; ++i) {
1870 if ( cmd->cmd == LC_SEGMENT_COMMAND ) {
1871 const struct macho_segment_command* seg = (struct macho_segment_command*)cmd;
1872 if ( (currentSectionIndex <= sectionIndex) && (sectionIndex < currentSectionIndex+seg->nsects) ) {
1873 // 'sectionIndex' is in this segment, get section info
1874 const struct macho_section* const sectionsStart = (struct macho_section*)((char*)seg + sizeof(struct macho_segment_command));
1875 const struct macho_section* const section = &sectionsStart[sectionIndex-currentSectionIndex];
1876 return ( (section->addr <= addr) && (addr < section->addr+section->size) );
1877 }
1878 else {
1879 // 'sectionIndex' not in this segment, skip to next segment
1880 currentSectionIndex += seg->nsects;
1881 }
1882 }
1883 cmd = (const struct load_command*)(((char*)cmd)+cmd->cmdsize);
1884 }
1885
1886 return false;
1887 }
1888
1889 void ImageLoaderMachO::doBindExternalRelocations(const LinkContext& context, bool onlyCoalescedSymbols)
1890 {
1891 const uintptr_t relocBase = this->getRelocBase();
1892 const bool twoLevel = this->usesTwoLevelNameSpace();
1893 const bool prebound = this->isPrebindable();
1894
1895 // if there are __TEXT fixups, temporarily make __TEXT writable
1896 if ( fTextSegmentWithFixups != NULL )
1897 fTextSegmentWithFixups->tempWritable();
1898
1899 // cache last lookup
1900 const struct macho_nlist* lastUndefinedSymbol = 0;
1901 uintptr_t symbolAddr = 0;
1902 ImageLoader* image = NULL;
1903
1904 // loop through all external relocation records and bind each
1905 const relocation_info* const relocsStart = (struct relocation_info*)(&fLinkEditBase[fDynamicInfo->extreloff]);
1906 const relocation_info* const relocsEnd = &relocsStart[fDynamicInfo->nextrel];
1907 for (const relocation_info* reloc=relocsStart; reloc < relocsEnd; ++reloc) {
1908 if (reloc->r_length == RELOC_SIZE) {
1909 switch(reloc->r_type) {
1910 case POINTER_RELOC:
1911 {
1912 const struct macho_nlist* undefinedSymbol = &fSymbolTable[reloc->r_symbolnum];
1913 // if only processing coalesced symbols and this one does not require coalesceing, skip to next
1914 if ( onlyCoalescedSymbols && !symbolRequiresCoalescing(undefinedSymbol) )
1915 continue;
1916 uintptr_t* location = ((uintptr_t*)(reloc->r_address + relocBase));
1917 uintptr_t value = *location;
1918 #if __i386__
1919 if ( reloc->r_pcrel ) {
1920 value += (uintptr_t)location + 4 - fSlide;
1921 }
1922 #endif
1923 if ( prebound ) {
1924 // we are doing relocations, so prebinding was not usable
1925 // in a prebound executable, the n_value field of an undefined symbol is set to the address where the symbol was found when prebound
1926 // so, subtracting that gives the initial displacement which we need to add to the newly found symbol address
1927 // if mach-o relocation structs had an "addend" field this complication would not be necessary.
1928 if ( ((undefinedSymbol->n_type & N_TYPE) == N_SECT) && ((undefinedSymbol->n_desc & N_WEAK_DEF) != 0) ) {
1929 // weak symbols need special casing, since *location may have been prebound to a definition in another image.
1930 // If *location is currently prebound to somewhere in the same section as the weak definition, we assume
1931 // that we can subtract off the weak symbol address to get the addend.
1932 // If prebound elsewhere, we've lost the addend and have to assume it is zero.
1933 // The prebinding to elsewhere only happens with 10.4+ update_prebinding which only operates on a small set of Apple dylibs
1934 if ( (value == undefinedSymbol->n_value) || this->isAddrInSection(value, undefinedSymbol->n_sect) )
1935 value -= undefinedSymbol->n_value;
1936 else
1937 value = 0;
1938 }
1939 else {
1940 // is undefined or non-weak symbol, so do subtraction to get addend
1941 value -= undefinedSymbol->n_value;
1942 }
1943 }
1944 // if undefinedSymbol is same as last time, then symbolAddr and image will resolve to the same too
1945 if ( undefinedSymbol != lastUndefinedSymbol ) {
1946 symbolAddr = this->resolveUndefined(context, undefinedSymbol, twoLevel, &image);
1947 lastUndefinedSymbol = undefinedSymbol;
1948 }
1949 if ( context.verboseBind ) {
1950 const char *path = NULL;
1951 if(NULL != image) {
1952 path = image->getShortName();
1953 }
1954 if(0 == value) {
1955 fprintf(stderr, "dyld: bind: %s:0x%08lx = %s:%s, *0x%08lx = 0x%08lx\n",
1956 this->getShortName(), (uintptr_t)location,
1957 path, &fStrings[undefinedSymbol->n_un.n_strx], (uintptr_t)location, symbolAddr);
1958 }
1959 else {
1960 fprintf(stderr, "dyld: bind: %s:0x%08lx = %s:%s, *0x%08lx = 0x%08lx + %ld\n",
1961 this->getShortName(), (uintptr_t)location,
1962 path, &fStrings[undefinedSymbol->n_un.n_strx], (uintptr_t)location, symbolAddr, value);
1963 }
1964 }
1965 value += symbolAddr;
1966 #if __i386__
1967 if ( reloc->r_pcrel ) {
1968 *location = value - ((uintptr_t)location + 4);
1969 }
1970 else {
1971 // don't dirty page if prebound value was correct
1972 if ( !prebound || (*location != value) )
1973 *location = value;
1974 }
1975 #else
1976 // don't dirty page if prebound value was correct
1977 if ( !prebound || (*location != value) )
1978 *location = value;
1979 #endif
1980 }
1981 break;
1982 default:
1983 throw "unknown external relocation type";
1984 }
1985 }
1986 else {
1987 throw "bad external relocation length";
1988 }
1989 }
1990
1991 // if there were __TEXT fixups, restore write protection
1992 if ( fTextSegmentWithFixups != NULL ) {
1993 fTextSegmentWithFixups->setPermissions();
1994 sys_icache_invalidate((void*)fTextSegmentWithFixups->getActualLoadAddress(), fTextSegmentWithFixups->getSize());
1995 }
1996
1997 // update stats
1998 fgTotalBindFixups += fDynamicInfo->nextrel;
1999 }
2000
2001 const mach_header* ImageLoaderMachO::machHeader() const
2002 {
2003 return (mach_header*)fMachOData;
2004 }
2005
2006 uintptr_t ImageLoaderMachO::getSlide() const
2007 {
2008 return fSlide;
2009 }
2010
2011 // hmm. maybe this should be up in ImageLoader??
2012 const void* ImageLoaderMachO::getBaseAddress() const
2013 {
2014 Segment* seg = fSegments[0];
2015 return (const void*)seg->getActualLoadAddress();
2016 }
2017
2018 uintptr_t ImageLoaderMachO::bindIndirectSymbol(uintptr_t* ptrToBind, const struct macho_section* sect, const char* symbolName, uintptr_t targetAddr, ImageLoader* targetImage, const LinkContext& context)
2019 {
2020 if ( context.verboseBind ) {
2021 const char* path = NULL;
2022 if ( targetImage != NULL )
2023 path = targetImage->getShortName();
2024 fprintf(stderr, "dyld: bind: %s:%s$%s = %s:%s, *0x%08lx = 0x%08lx\n",
2025 this->getShortName(), symbolName, (((sect->flags & SECTION_TYPE)==S_NON_LAZY_SYMBOL_POINTERS) ? "non_lazy_ptr" : "lazy_ptr"),
2026 path, symbolName, (uintptr_t)ptrToBind, targetAddr);
2027 }
2028 if ( context.bindingHandler != NULL ) {
2029 const char* path = NULL;
2030 if ( targetImage != NULL )
2031 path = targetImage->getShortName();
2032 targetAddr = (uintptr_t)context.bindingHandler(path, symbolName, (void *)targetAddr);
2033 }
2034 #if __i386__
2035 // i386 has special self-modifying stubs that change from "CALL rel32" to "JMP rel32"
2036 if ( ((sect->flags & SECTION_TYPE) == S_SYMBOL_STUBS) && ((sect->flags & S_ATTR_SELF_MODIFYING_CODE) != 0) && (sect->reserved2 == 5) ) {
2037 uint32_t rel32 = targetAddr - (((uint32_t)ptrToBind)+5);
2038 // re-write instruction in a thread-safe manner
2039 // use 8-byte compare-and-swap to alter 5-byte jump table entries
2040 // loop is required in case the extra three bytes that cover the next entry are altered by another thread
2041 bool done = false;
2042 while ( !done ) {
2043 volatile int64_t* jumpPtr = (int64_t*)ptrToBind;
2044 int pad = 0;
2045 // By default the three extra bytes swapped follow the 5-byte JMP.
2046 // But, if the 5-byte jump is up against the end of the __IMPORT segment
2047 // We don't want to access bytes off the end of the segment, so we shift
2048 // the extra bytes to precede the 5-byte JMP.
2049 if ( (((uint32_t)ptrToBind + 8) & 0x00000FFC) == 0x00000000 ) {
2050 jumpPtr = (int64_t*)((uint32_t)ptrToBind - 3);
2051 pad = 3;
2052 }
2053 int64_t oldEntry = *jumpPtr;
2054 union {
2055 int64_t int64;
2056 uint8_t bytes[8];
2057 } newEntry;
2058 newEntry.int64 = oldEntry;
2059 newEntry.bytes[pad+0] = 0xE9; // JMP rel32
2060 newEntry.bytes[pad+1] = rel32 & 0xFF;
2061 newEntry.bytes[pad+2] = (rel32 >> 8) & 0xFF;
2062 newEntry.bytes[pad+3] = (rel32 >> 16) & 0xFF;
2063 newEntry.bytes[pad+4] = (rel32 >> 24) & 0xFF;
2064 done = OSAtomicCompareAndSwap64Barrier(oldEntry, newEntry.int64, (int64_t*)jumpPtr);
2065 }
2066 }
2067 else
2068 #endif
2069 *ptrToBind = targetAddr;
2070 return targetAddr;
2071 }
2072
2073
2074 uintptr_t ImageLoaderMachO::doBindLazySymbol(uintptr_t* lazyPointer, const LinkContext& context)
2075 {
2076 // scan for all non-lazy-pointer sections
2077 const bool twoLevel = this->usesTwoLevelNameSpace();
2078 const uint32_t cmd_count = ((macho_header*)fMachOData)->ncmds;
2079 const struct load_command* const cmds = (struct load_command*)&fMachOData[sizeof(macho_header)];
2080 const struct load_command* cmd = cmds;
2081 const uint32_t* const indirectTable = (uint32_t*)&fLinkEditBase[fDynamicInfo->indirectsymoff];
2082 for (uint32_t i = 0; i < cmd_count; ++i) {
2083 switch (cmd->cmd) {
2084 case LC_SEGMENT_COMMAND:
2085 {
2086 const struct macho_segment_command* seg = (struct macho_segment_command*)cmd;
2087 const struct macho_section* const sectionsStart = (struct macho_section*)((char*)seg + sizeof(struct macho_segment_command));
2088 const struct macho_section* const sectionsEnd = &sectionsStart[seg->nsects];
2089 for (const struct macho_section* sect=sectionsStart; sect < sectionsEnd; ++sect) {
2090 const uint8_t type = sect->flags & SECTION_TYPE;
2091 uint32_t symbolIndex = INDIRECT_SYMBOL_LOCAL;
2092 if ( type == S_LAZY_SYMBOL_POINTERS ) {
2093 const uint32_t pointerCount = sect->size / sizeof(uintptr_t);
2094 uintptr_t* const symbolPointers = (uintptr_t*)(sect->addr + fSlide);
2095 if ( (lazyPointer >= symbolPointers) && (lazyPointer < &symbolPointers[pointerCount]) ) {
2096 const uint32_t indirectTableOffset = sect->reserved1;
2097 const uint32_t lazyIndex = lazyPointer - symbolPointers;
2098 symbolIndex = indirectTable[indirectTableOffset + lazyIndex];
2099 }
2100 }
2101 #if __i386__
2102 else if ( (type == S_SYMBOL_STUBS) && (sect->flags & S_ATTR_SELF_MODIFYING_CODE) && (sect->reserved2 == 5) ) {
2103 // 5 bytes stubs on i386 are new "fast stubs"
2104 uint8_t* const jmpTableBase = (uint8_t*)(sect->addr + fSlide);
2105 uint8_t* const jmpTableEnd = jmpTableBase + sect->size;
2106 // initial CALL instruction in jump table leaves pointer to next entry, so back up
2107 uint8_t* const jmpTableEntryToPatch = ((uint8_t*)lazyPointer) - 5;
2108 lazyPointer = (uintptr_t*)jmpTableEntryToPatch;
2109 if ( (jmpTableEntryToPatch >= jmpTableBase) && (jmpTableEntryToPatch < jmpTableEnd) ) {
2110 const uint32_t indirectTableOffset = sect->reserved1;
2111 const uint32_t entryIndex = (jmpTableEntryToPatch - jmpTableBase)/5;
2112 symbolIndex = indirectTable[indirectTableOffset + entryIndex];
2113 }
2114 }
2115 #endif
2116 if ( symbolIndex != INDIRECT_SYMBOL_ABS && symbolIndex != INDIRECT_SYMBOL_LOCAL ) {
2117 const char* symbolName = &fStrings[fSymbolTable[symbolIndex].n_un.n_strx];
2118 ImageLoader* image = NULL;
2119 uintptr_t symbolAddr = this->resolveUndefined(context, &fSymbolTable[symbolIndex], twoLevel, &image);
2120 symbolAddr = this->bindIndirectSymbol(lazyPointer, sect, symbolName, symbolAddr, image, context);
2121 ++fgTotalLazyBindFixups;
2122 return symbolAddr;
2123 }
2124 }
2125 }
2126 break;
2127 }
2128 cmd = (const struct load_command*)(((char*)cmd)+cmd->cmdsize);
2129 }
2130 throw "lazy pointer not found";
2131 }
2132
2133
2134
2135
2136 void ImageLoaderMachO::doBindIndirectSymbolPointers(const LinkContext& context, BindingLaziness bindness, bool onlyCoalescedSymbols)
2137 {
2138 // scan for all non-lazy-pointer sections
2139 const bool twoLevel = this->usesTwoLevelNameSpace();
2140 const uint32_t cmd_count = ((macho_header*)fMachOData)->ncmds;
2141 const struct load_command* const cmds = (struct load_command*)&fMachOData[sizeof(macho_header)];
2142 const struct load_command* cmd = cmds;
2143 const uint32_t* const indirectTable = (uint32_t*)&fLinkEditBase[fDynamicInfo->indirectsymoff];
2144 for (uint32_t i = 0; i < cmd_count; ++i) {
2145 switch (cmd->cmd) {
2146 case LC_SEGMENT_COMMAND:
2147 {
2148 const struct macho_segment_command* seg = (struct macho_segment_command*)cmd;
2149 const struct macho_section* const sectionsStart = (struct macho_section*)((char*)seg + sizeof(struct macho_segment_command));
2150 const struct macho_section* const sectionsEnd = &sectionsStart[seg->nsects];
2151 for (const struct macho_section* sect=sectionsStart; sect < sectionsEnd; ++sect) {
2152 const uint8_t type = sect->flags & SECTION_TYPE;
2153 uint32_t elementSize = sizeof(uintptr_t);
2154 uint32_t elementCount = sect->size / elementSize;
2155 if ( type == S_NON_LAZY_SYMBOL_POINTERS ) {
2156 if ( (bindness == kLazyOnly) || (bindness == kLazyOnlyNoDependents) )
2157 continue;
2158 }
2159 else if ( type == S_LAZY_SYMBOL_POINTERS ) {
2160 // process each symbol pointer in this section
2161 fgTotalPossibleLazyBindFixups += elementCount;
2162 if ( bindness == kNonLazyOnly )
2163 continue;
2164 }
2165 #if __i386__
2166 else if ( (type == S_SYMBOL_STUBS) && (sect->flags & S_ATTR_SELF_MODIFYING_CODE) && (sect->reserved2 == 5) ) {
2167 // process each jmp entry in this section
2168 elementCount = sect->size / 5;
2169 elementSize = 5;
2170 fgTotalPossibleLazyBindFixups += elementCount;
2171 if ( bindness == kNonLazyOnly )
2172 continue;
2173 }
2174 #endif
2175 else {
2176 continue;
2177 }
2178 const uint32_t indirectTableOffset = sect->reserved1;
2179 uint8_t* ptrToBind = (uint8_t*)(sect->addr + fSlide);
2180 for (uint32_t j=0; j < elementCount; ++j, ptrToBind += elementSize) {
2181 uint32_t symbolIndex = indirectTable[indirectTableOffset + j];
2182 if ( symbolIndex == INDIRECT_SYMBOL_LOCAL) {
2183 *((uintptr_t*)ptrToBind) += this->fSlide;
2184 }
2185 else if ( symbolIndex == INDIRECT_SYMBOL_ABS) {
2186 // do nothing since already has absolute address
2187 }
2188 else {
2189 const struct macho_nlist* sym = &fSymbolTable[symbolIndex];
2190 if ( symbolIndex == 0 ) {
2191 // This could be rdar://problem/3534709
2192 if ( ((const macho_header*)fMachOData)->filetype == MH_EXECUTE ) {
2193 static bool alreadyWarned = false;
2194 if ( (sym->n_type & N_TYPE) != N_UNDF ) {
2195 // The indirect table parallels the (non)lazy pointer sections. For
2196 // instance, to find info about the fifth lazy pointer you look at the
2197 // fifth entry in the indirect table. (try otool -Iv on a file).
2198 // The entry in the indirect table contains an index into the symbol table.
2199
2200 // The bug in ld caused the entry in the indirect table to be zero
2201 // (instead of a magic value that means a local symbol). So, if the
2202 // symbolIndex == 0, we may be encountering the bug, or 0 may be a valid
2203 // symbol table index. The check I put in place is to see if the zero'th
2204 // symbol table entry is an import entry (usually it is a local symbol
2205 // definition).
2206 if ( context.verboseWarnings && !alreadyWarned ) {
2207 fprintf(stderr, "dyld: malformed executable '%s', skipping indirect symbol to %s\n",
2208 this->getPath(), &fStrings[sym->n_un.n_strx]);
2209 alreadyWarned = true;
2210 }
2211 continue;
2212 }
2213 }
2214 }
2215 ImageLoader *image = NULL;
2216 // if only processing coalesced symbols and this one does not require coalesceing, skip to next
2217 if ( onlyCoalescedSymbols && !symbolRequiresCoalescing(sym) )
2218 continue;
2219 uintptr_t symbolAddr;
2220 symbolAddr = resolveUndefined(context, sym, twoLevel, &image);
2221
2222 // update pointer
2223 symbolAddr = this->bindIndirectSymbol((uintptr_t*)ptrToBind, sect, &fStrings[sym->n_un.n_strx], symbolAddr, image, context);
2224 }
2225 }
2226 // update stats
2227 fgTotalBindFixups += elementCount;
2228 }
2229 }
2230 break;
2231 }
2232 cmd = (const struct load_command*)(((char*)cmd)+cmd->cmdsize);
2233 }
2234 }
2235
2236 /*
2237 * The address of these symbols are written in to the (__DATA,__dyld) section
2238 * at the following offsets:
2239 * at offset 0 stub_binding_helper_interface
2240 * at offset 4 _dyld_func_lookup
2241 * at offset 8 start_debug_thread
2242 * The 'C' types (if any) for these symbols are ignored here and all are
2243 * declared as longs so the assignment of their address in to the section will
2244 * not require a cast. stub_binding_helper_interface is really a label in the
2245 * assembly code interface for the stub binding. It does not have a meaningful
2246 * 'C' type. _dyld_func_lookup is the routine in dyld_libfuncs.c.
2247 * start_debug_thread is the routine in debug.c.
2248 *
2249 * For ppc the image's stub_binding_binding_helper is read from:
2250 * at offset 20 the image's stub_binding_binding_helper address
2251 * and saved into to the image structure.
2252 */
2253 struct DATAdyld {
2254 void* dyldLazyBinder; // filled in at launch by dyld to point into dyld to &stub_binding_helper_interface
2255 void* dyldFuncLookup; // filled in at launch by dyld to point into dyld to &_dyld_func_lookup
2256 void* startDebugThread; // debugger interface ???
2257 void* debugPort; // debugger interface ???
2258 void* debugThread; // debugger interface ???
2259 void* stubBindHelper; // filled in at static link time to point to stub helper in image
2260 void* coreDebug; // ???
2261 };
2262
2263 // These are defined in dyldStartup.s
2264 extern "C" void stub_binding_helper();
2265 extern "C" bool dyld_func_lookup(const char* name, uintptr_t* address);
2266 extern "C" void fast_stub_binding_helper_interface();
2267
2268
2269 void ImageLoaderMachO::setupLazyPointerHandler(const LinkContext& context)
2270 {
2271 if ( fDATAdyld != NULL ) {
2272 struct DATAdyld* dd = (struct DATAdyld*)(fDATAdyld->addr + fSlide);
2273 if ( fDATAdyld->size > offsetof(DATAdyld, dyldLazyBinder) ) {
2274 if ( dd->dyldLazyBinder != (void*)&stub_binding_helper )
2275 dd->dyldLazyBinder = (void*)&stub_binding_helper;
2276 }
2277 if ( fDATAdyld->size > offsetof(DATAdyld, dyldFuncLookup) ) {
2278 if ( dd->dyldFuncLookup != (void*)&dyld_func_lookup )
2279 dd->dyldFuncLookup = (void*)&dyld_func_lookup;
2280 }
2281 //if ( fDATAdyld->size > offsetof(DATAdyld, startDebugThread) )
2282 // dd->startDebugThread = &start_debug_thread;
2283 #ifdef __ppc__
2284 //if ( fDATAdyld->size > offsetof(DATAdyld, stubBindHelper) )
2285 // save = dd->stubBindHelper;
2286 #endif
2287 }
2288 #if __i386__
2289 if ( ! this->usablePrebinding(context) || !this->usesTwoLevelNameSpace() ) {
2290 // reset all "fast" stubs
2291 const uint32_t cmd_count = ((macho_header*)fMachOData)->ncmds;
2292 const struct load_command* const cmds = (struct load_command*)&fMachOData[sizeof(macho_header)];
2293 const struct load_command* cmd = cmds;
2294 for (uint32_t i = 0; i < cmd_count; ++i) {
2295 switch (cmd->cmd) {
2296 case LC_SEGMENT_COMMAND:
2297 {
2298 const struct macho_segment_command* seg = (struct macho_segment_command*)cmd;
2299 const struct macho_section* const sectionsStart = (struct macho_section*)((char*)seg + sizeof(struct macho_segment_command));
2300 const struct macho_section* const sectionsEnd = &sectionsStart[seg->nsects];
2301 for (const struct macho_section* sect=sectionsStart; sect < sectionsEnd; ++sect) {
2302 const uint8_t type = sect->flags & SECTION_TYPE;
2303 if ( (type == S_SYMBOL_STUBS) && (sect->flags & S_ATTR_SELF_MODIFYING_CODE) && (sect->reserved2 == 5) ) {
2304 // reset each jmp entry in this section
2305 uint8_t* start = (uint8_t*)(sect->addr + this->fSlide);
2306 uint8_t* end = start + sect->size;
2307 uintptr_t dyldHandler = (uintptr_t)&fast_stub_binding_helper_interface;
2308 for (uint8_t* entry = start; entry < end; entry += 5) {
2309 uint32_t rel32 = dyldHandler - (((uint32_t)entry)+5);
2310 entry[0] = 0xE8; // CALL rel32
2311 entry[1] = rel32 & 0xFF;
2312 entry[2] = (rel32 >> 8) & 0xFF;
2313 entry[3] = (rel32 >> 16) & 0xFF;
2314 entry[4] = (rel32 >> 24) & 0xFF;
2315 }
2316 }
2317 }
2318 }
2319 }
2320 cmd = (const struct load_command*)(((char*)cmd)+cmd->cmdsize);
2321 }
2322 }
2323 #endif
2324 }
2325
2326 bool ImageLoaderMachO::usablePrebinding(const LinkContext& context) const
2327 {
2328 // if prebound and loaded at prebound address, and all libraries are same as when this was prebound, then no need to bind
2329 if ( this->isPrebindable() && this->allDependentLibrariesAsWhenPreBound() && (this->getSlide() == 0) ) {
2330 // allow environment variables to disable prebinding
2331 if ( context.bindFlat )
2332 return false;
2333 switch ( context.prebindUsage ) {
2334 case kUseAllPrebinding:
2335 return true;
2336 case kUseSplitSegPrebinding:
2337 return this->fIsSplitSeg;
2338 case kUseAllButAppPredbinding:
2339 return (this != context.mainExecutable);
2340 case kUseNoPrebinding:
2341 return false;
2342 }
2343 }
2344 return false;
2345 }
2346
2347 void ImageLoaderMachO::doBind(const LinkContext& context, BindingLaziness bindness)
2348 {
2349 // set dyld entry points in image
2350 this->setupLazyPointerHandler(context);
2351
2352 // if prebound and loaded at prebound address, and all libraries are same as when this was prebound, then no need to bind
2353 // note: flat-namespace binaries need to be imports rebound (even if correctly prebound)
2354 if ( this->usablePrebinding(context) && this->usesTwoLevelNameSpace() ) {
2355 // if image has coalesced symbols, then these need to be rebound, unless this is the only image with weak symbols
2356 if ( this->needsCoalescing() && (fgCountOfImagesWithWeakExports > 1) ) {
2357 this->doBindExternalRelocations(context, true);
2358 this->doBindIndirectSymbolPointers(context, kLazyAndNonLazy, true);
2359 }
2360 // skip binding because prebound and prebinding not disabled
2361 return;
2362 }
2363
2364 // values bound by name are stored two different ways in mach-o
2365 switch (bindness) {
2366 case kNonLazyOnly:
2367 case kLazyAndNonLazy:
2368 // external relocations are used for data initialized to external symbols
2369 this->doBindExternalRelocations(context, false);
2370 break;
2371 case kLazyOnly:
2372 case kLazyOnlyNoDependents:
2373 break;
2374 }
2375 // "indirect symbols" are used for code references to external symbols
2376 this->doBindIndirectSymbolPointers(context, bindness, false);
2377 }
2378
2379
2380
2381 void ImageLoaderMachO::doImageInit(const LinkContext& context)
2382 {
2383 if ( fDashInit != NULL ) {
2384 Initializer func = (Initializer)(fDashInit->init_address + fSlide);
2385 if ( context.verboseInit )
2386 fprintf(stderr, "dyld: calling -init function 0x%p in %s\n", func, this->getPath());
2387 func(context.argc, context.argv, context.envp, context.apple);
2388 }
2389 }
2390
2391 void ImageLoaderMachO::doModInitFunctions(const LinkContext& context)
2392 {
2393 if ( fModInitSection != NULL ) {
2394 Initializer* inits = (Initializer*)(fModInitSection->addr + fSlide);
2395 const uint32_t count = fModInitSection->size / sizeof(uintptr_t);
2396 for (uint32_t i=0; i < count; ++i) {
2397 Initializer func = inits[i];
2398 if ( context.verboseInit )
2399 fprintf(stderr, "dyld: calling initializer function %p in %s\n", func, this->getPath());
2400 func(context.argc, context.argv, context.envp, context.apple);
2401 }
2402 }
2403 }
2404
2405
2406 void ImageLoaderMachO::doInitialization(const LinkContext& context)
2407 {
2408 // mach-o has -init and static initializers
2409 doImageInit(context);
2410 doModInitFunctions(context);
2411 }
2412
2413 bool ImageLoaderMachO::needsInitialization()
2414 {
2415 return ( (fDashInit != NULL) || (fModInitSection != NULL) );
2416 }
2417
2418
2419 bool ImageLoaderMachO::needsTermination()
2420 {
2421 return ( fModTermSection != NULL );
2422 }
2423
2424 bool ImageLoaderMachO::hasImageNotification()
2425 {
2426 return ( fImageNotifySection != NULL );
2427 }
2428
2429
2430 void ImageLoaderMachO::doTermination(const LinkContext& context)
2431 {
2432 if ( fModTermSection != NULL ) {
2433 Terminator* terms = (Terminator*)(fModTermSection->addr + fSlide);
2434 const uint32_t count = fModTermSection->size / sizeof(uintptr_t);
2435 for (uint32_t i=count; i > 0; --i) {
2436 Terminator func = terms[i-1];
2437 if ( context.verboseInit )
2438 fprintf(stderr, "dyld: calling terminaton function %p in %s\n", func, this->getPath());
2439 func();
2440 }
2441 }
2442 }
2443
2444 void ImageLoaderMachO::doNotification(enum dyld_image_mode mode, uint32_t infoCount, const struct dyld_image_info info[])
2445 {
2446 if ( fImageNotifySection != NULL ) {
2447 dyld_image_notifier* notes = (dyld_image_notifier*)(fImageNotifySection->addr + fSlide);
2448 const uint32_t count = fImageNotifySection->size / sizeof(uintptr_t);
2449 for (uint32_t i=count; i > 0; --i) {
2450 dyld_image_notifier func = notes[i-1];
2451 func(mode, infoCount, info);
2452 }
2453 }
2454 }
2455
2456 void ImageLoaderMachO::printStatistics(unsigned int imageCount)
2457 {
2458 ImageLoader::printStatistics(imageCount);
2459 fprintf(stderr, "total hinted binary tree searches: %d\n", fgHintedBinaryTreeSearchs);
2460 fprintf(stderr, "total unhinted binary tree searches: %d\n", fgUnhintedBinaryTreeSearchs);
2461 fprintf(stderr, "total images with weak exports: %d\n", fgCountOfImagesWithWeakExports);
2462
2463 #if LINKEDIT_USAGE_DEBUG
2464 fprintf(stderr, "linkedit pages accessed (%lu):\n", sLinkEditPageBuckets.size());
2465 #endif
2466 }
2467
2468 void ImageLoaderMachO::doPrebinding(const LinkContext& context, time_t timestamp, uint8_t* fileToPrebind)
2469 {
2470 // update __DATA segment
2471 this->applyPrebindingToDATA(fileToPrebind);
2472
2473 // update load commands
2474 this->applyPrebindingToLoadCommands(context, fileToPrebind, timestamp);
2475
2476 // update symbol table
2477 this->applyPrebindingToLinkEdit(context, fileToPrebind);
2478 }
2479
2480 void ImageLoaderMachO::applyPrebindingToDATA(uint8_t* fileToPrebind)
2481 {
2482 const unsigned int segmentCount = fSegments.size();
2483 for(unsigned int i=0; i < segmentCount; ++i) {
2484 SegmentMachO* seg = (SegmentMachO*)fSegments[i];
2485 if ( seg->writeable() ) {
2486 memcpy(&fileToPrebind[seg->fFileOffset], (void*)seg->getActualLoadAddress(), seg->fFileSize);
2487 }
2488 }
2489 }
2490
2491 void ImageLoaderMachO::applyPrebindingToLoadCommands(const LinkContext& context, uint8_t* fileToPrebind, time_t timestamp)
2492 {
2493 macho_header* mh = (macho_header*)fileToPrebind;
2494 const uint32_t cmd_count = mh->ncmds;
2495 const struct load_command* const cmds = (struct load_command*)&fileToPrebind[sizeof(macho_header)];
2496 const struct load_command* cmd = cmds;
2497 for (uint32_t i = 0; i < cmd_count; ++i) {
2498 switch (cmd->cmd) {
2499 case LC_LOAD_DYLIB:
2500 case LC_LOAD_WEAK_DYLIB:
2501 {
2502 // update each dylib load command with the timestamp of the target dylib
2503 struct dylib_command* dylib = (struct dylib_command*)cmd;
2504 const char* name = (char*)cmd + dylib->dylib.name.offset;
2505 for (const DependentLibrary* dl=fLibraries; dl < &fLibraries[fLibrariesCount]; dl++) {
2506 if (strcmp(dl->name, name) == 0 ) {
2507 // found matching DependentLibrary for this load command
2508 ImageLoaderMachO* targetImage = (ImageLoaderMachO*)(dl->image); // !!! assume only mach-o images are prebound
2509 if ( ! targetImage->isPrebindable() )
2510 throw "dependent dylib is not prebound";
2511 // if the target is currently being re-prebound then its timestamp will be the same as this one
2512 if ( ! targetImage->usablePrebinding(context) ) {
2513 dylib->dylib.timestamp = timestamp;
2514 }
2515 else {
2516 // otherwise dependent library is already correctly prebound, so use its checksum
2517 dylib->dylib.timestamp = targetImage->doGetLibraryInfo().checksum;
2518 }
2519 break;
2520 }
2521 }
2522 }
2523 break;
2524 case LC_ID_DYLIB:
2525 {
2526 // update the ID of this library with the new timestamp
2527 struct dylib_command* dylib = (struct dylib_command*)cmd;
2528 dylib->dylib.timestamp = timestamp;
2529 }
2530 break;
2531 case LC_SEGMENT_COMMAND:
2532 // if dylib was rebased, update segment commands
2533 if ( fSlide != 0 ) {
2534 struct macho_segment_command* seg = (struct macho_segment_command*)cmd;
2535 seg->vmaddr += fSlide;
2536 struct macho_section* const sectionsStart = (struct macho_section*)((char*)seg + sizeof(struct macho_segment_command));
2537 struct macho_section* const sectionsEnd = &sectionsStart[seg->nsects];
2538 for (struct macho_section* sect=sectionsStart; sect < sectionsEnd; ++sect) {
2539 sect->addr += fSlide;
2540 }
2541 }
2542 break;
2543 case LC_ROUTINES_COMMAND:
2544 // if dylib was rebased, update -init command
2545 if ( fSlide != 0 ) {
2546 struct macho_routines_command* routines = (struct macho_routines_command*)cmd;
2547 routines->init_address += fSlide;
2548 }
2549 break;
2550 }
2551 cmd = (const struct load_command*)(((char*)cmd)+cmd->cmdsize);
2552 }
2553 }
2554
2555 void ImageLoaderMachO::applyPrebindingToLinkEdit(const LinkContext& context, uint8_t* fileToPrebind)
2556 {
2557 // In prebound images, the n_value of the symbol table entry for is the prebound address
2558 // This is needed when prebinding can't be used, to back solve for any possible addend in non-lazy pointers
2559 const char* stringPool = NULL;
2560 struct macho_nlist* symbolTable = NULL;
2561 const struct dysymtab_command* dysymtab = NULL;
2562
2563 // get symbol table info
2564 macho_header* mh = (macho_header*)fileToPrebind;
2565 const uint32_t cmd_count = mh->ncmds;
2566 const struct load_command* const cmds = (struct load_command*)&fileToPrebind[sizeof(macho_header)];
2567 const struct load_command* cmd = cmds;
2568 for (uint32_t i = 0; i < cmd_count; ++i) {
2569 switch (cmd->cmd) {
2570 case LC_SYMTAB:
2571 {
2572 const struct symtab_command* symtab = (struct symtab_command*)cmd;
2573 stringPool = (const char*)&fileToPrebind[symtab->stroff];
2574 symbolTable = (struct macho_nlist*)(&fileToPrebind[symtab->symoff]);
2575 }
2576 break;
2577 case LC_DYSYMTAB:
2578 dysymtab = (struct dysymtab_command*)cmd;
2579 break;
2580 }
2581 cmd = (const struct load_command*)(((char*)cmd)+cmd->cmdsize);
2582 }
2583
2584 // walk all imports and re-resolve their n_value (needed incase prebinding is invalid)
2585 struct macho_nlist* lastImport = &symbolTable[dysymtab->iundefsym+dysymtab->nundefsym];
2586 for (struct macho_nlist* entry = &symbolTable[dysymtab->iundefsym]; entry < lastImport; ++entry) {
2587 ImageLoader* dummy;
2588 entry->n_value = this->resolveUndefined(context, entry, this->usesTwoLevelNameSpace(), &dummy);
2589 }
2590
2591 // walk all exports and slide their n_value
2592 struct macho_nlist* lastExport = &symbolTable[dysymtab->iextdefsym+dysymtab->nextdefsym];
2593 for (struct macho_nlist* entry = &symbolTable[dysymtab->iextdefsym]; entry < lastExport; ++entry) {
2594 if ( (entry->n_type & N_TYPE) == N_SECT )
2595 entry->n_value += fSlide;
2596 }
2597
2598 // walk all local symbols and slide their n_value
2599 struct macho_nlist* lastLocal = &symbolTable[dysymtab->ilocalsym+dysymtab->nlocalsym];
2600 for (struct macho_nlist* entry = &symbolTable[dysymtab->ilocalsym]; entry < lastLocal; ++entry) {
2601 if ( entry->n_sect != NO_SECT )
2602 entry->n_value += fSlide;
2603 }
2604
2605 // walk all local relocations and reset every PPC_RELOC_PB_LA_PTR r_value
2606 relocation_info* const relocsStart = (struct relocation_info*)(&fileToPrebind[dysymtab->locreloff]);
2607 relocation_info* const relocsEnd = &relocsStart[dysymtab->nlocrel];
2608 for (relocation_info* reloc=relocsStart; reloc < relocsEnd; ++reloc) {
2609 if ( (reloc->r_address & R_SCATTERED) != 0 ) {
2610 struct scattered_relocation_info* sreloc = (struct scattered_relocation_info*)reloc;
2611 if (sreloc->r_length == RELOC_SIZE) {
2612 switch(sreloc->r_type) {
2613 #if __ppc__ || __ppc64__
2614 case PPC_RELOC_PB_LA_PTR:
2615 #elif __i386__ || __x86_64__
2616 case GENERIC_RELOC_PB_LA_PTR:
2617 #else
2618 #error unknown architecture
2619 #endif
2620 sreloc->r_value += fSlide;
2621 break;
2622 }
2623 }
2624 }
2625 }
2626
2627 // if multi-module, fix up objc_addr (10.4 and later runtime does not use this, but we want to keep file checksum consistent)
2628 if ( dysymtab->nmodtab != 0 ) {
2629 dylib_module* const modulesStart = (struct dylib_module*)(&fileToPrebind[dysymtab->modtaboff]);
2630 dylib_module* const modulesEnd = &modulesStart[dysymtab->nmodtab];
2631 for (dylib_module* module=modulesStart; module < modulesEnd; ++module) {
2632 if ( module->objc_module_info_size != 0 ) {
2633 module->objc_module_info_addr += fSlide;
2634 }
2635 }
2636 }
2637 }
2638
2639 // file on disk has been reprebound, but we are still mapped to old file
2640 void ImageLoaderMachO::prebindUnmap(const LinkContext& context)
2641 {
2642 // this removes all mappings to the old file, so the kernel will unlink (delete) it.
2643 // We need to leave the load commands and __LINKEDIT in place
2644 for (std::vector<class Segment*>::iterator it=fSegments.begin(); it != fSegments.end(); ++it) {
2645 void* segmentAddress = (void*)((*it)->getActualLoadAddress());
2646 uintptr_t segmentSize = (*it)->getSize();
2647 //fprintf(stderr, "unmapping segment %s at %p for %s\n", (*it)->getName(), segmentAddress, this->getPath());
2648 // save load commands at beginning of __TEXT segment
2649 if ( segmentAddress == fMachOData ) {
2650 // typically load commands are one or two pages in size, so ok to alloc on stack
2651 uint32_t loadCmdSize = sizeof(macho_header) + ((macho_header*)fMachOData)->sizeofcmds;
2652 uint32_t loadCmdPages = (loadCmdSize+4095) & (-4096);
2653 uint8_t loadcommands[loadCmdPages];
2654 memcpy(loadcommands, fMachOData, loadCmdPages);
2655 // unmap whole __TEXT segment
2656 munmap((void*)(fMachOData), segmentSize);
2657 // allocate and copy back mach_header and load commands
2658 vm_address_t addr = (vm_address_t)fMachOData;
2659 int r2 = vm_allocate(mach_task_self(), &addr, loadCmdPages, false /*at this address*/);
2660 if ( r2 != 0 )
2661 fprintf(stderr, "prebindUnmap() vm_allocate for __TEXT %d failed\n", loadCmdPages);
2662 memcpy((void*)fMachOData, loadcommands, loadCmdPages);
2663 //fprintf(stderr, "copying back load commands to %p size=%u for %s\n", segmentAddress, loadCmdPages, this->getPath());
2664 }
2665 else if ( strcmp((*it)->getName(), "__LINKEDIT") == 0 ) {
2666 uint32_t linkEditSize = segmentSize;
2667 uint32_t linkEditPages = (linkEditSize+4095) & (-4096);
2668 void* linkEditTmp = malloc(linkEditPages);
2669 memcpy(linkEditTmp, segmentAddress, linkEditPages);
2670 // unmap whole __LINKEDIT segment
2671 munmap(segmentAddress, segmentSize);
2672 vm_address_t addr = (vm_address_t)segmentAddress;
2673 int r2 = vm_allocate(mach_task_self(), &addr, linkEditPages, false /*at this address*/);
2674 if ( r2 != 0 )
2675 fprintf(stderr, "prebindUnmap() vm_allocate for __LINKEDIT %d failed\n", linkEditPages);
2676 memcpy(segmentAddress, linkEditTmp, linkEditPages);
2677 //fprintf(stderr, "copying back __LINKEDIT to %p size=%u for %s\n", segmentAddress, linkEditPages, this->getPath());
2678 free(linkEditTmp);
2679 }
2680 else {
2681 // unmap any other segment
2682 munmap((void*)(segmentAddress), (*it)->getSize());
2683 }
2684 }
2685 }
2686
2687
2688
2689 SegmentMachO::SegmentMachO(const struct macho_segment_command* cmd, ImageLoaderMachO* image, const uint8_t* fileData)
2690 : fImage(image), fSize(cmd->vmsize), fFileSize(cmd->filesize), fFileOffset(cmd->fileoff), fPreferredLoadAddress(cmd->vmaddr),
2691 fVMProtection(cmd->initprot), fHasFixUps(false), fUnMapOnDestruction(false)
2692 {
2693 strncpy(fName, cmd->segname, 16);
2694 fName[16] = '\0';
2695 // scan sections for fix-up bit
2696 const struct macho_section* const sectionsStart = (struct macho_section*)((char*)cmd + sizeof(struct macho_segment_command));
2697 const struct macho_section* const sectionsEnd = &sectionsStart[cmd->nsects];
2698 for (const struct macho_section* sect=sectionsStart; sect < sectionsEnd; ++sect) {
2699 if ( (sect->flags & (S_ATTR_EXT_RELOC | S_ATTR_LOC_RELOC)) != 0 )
2700 fHasFixUps = true;
2701 }
2702 }
2703
2704 SegmentMachO::~SegmentMachO()
2705 {
2706 if ( fUnMapOnDestruction ) {
2707 //fprintf(stderr, "unmapping segment %s at 0x%08lX\n", getName(), getActualLoadAddress());
2708 munmap((void*)(this->getActualLoadAddress()), this->getSize());
2709 }
2710 }
2711
2712 const ImageLoader* SegmentMachO::getImage()
2713 {
2714 return fImage;
2715 }
2716
2717 const char* SegmentMachO::getName()
2718 {
2719 return fName;
2720 }
2721
2722 uintptr_t SegmentMachO::getSize()
2723 {
2724 return fSize;
2725 }
2726
2727 uintptr_t SegmentMachO::getFileSize()
2728 {
2729 return fFileSize;
2730 }
2731
2732 uintptr_t SegmentMachO::getFileOffset()
2733 {
2734 return fFileOffset;
2735 }
2736
2737 bool SegmentMachO::readable()
2738 {
2739 return ( (fVMProtection & VM_PROT_READ) != 0);
2740 }
2741
2742 bool SegmentMachO::writeable()
2743 {
2744 return ((fVMProtection & VM_PROT_WRITE) != 0);
2745 }
2746
2747 bool SegmentMachO::executable()
2748 {
2749 return ((fVMProtection & VM_PROT_EXECUTE) != 0);
2750 }
2751
2752 bool SegmentMachO::unaccessible()
2753 {
2754 return (fVMProtection == 0);
2755 }
2756
2757 bool SegmentMachO::hasFixUps()
2758 {
2759 return fHasFixUps;
2760 }
2761
2762 uintptr_t SegmentMachO::getActualLoadAddress()
2763 {
2764 return fPreferredLoadAddress + fImage->fSlide;
2765 }
2766
2767 uintptr_t SegmentMachO::getPreferredLoadAddress()
2768 {
2769 return fPreferredLoadAddress;
2770 }
2771
2772 bool SegmentMachO::hasPreferredLoadAddress()
2773 {
2774 return (fPreferredLoadAddress != 0);
2775 }
2776
2777 void SegmentMachO::setUnMapWhenDestructed(bool unmap)
2778 {
2779 fUnMapOnDestruction = unmap;
2780 }
2781
2782 static uint32_t *buildCRCTable(void)
2783 {
2784 uint32_t *table = new uint32_t[256];
2785 uint32_t p = 0xedb88320UL; // standard CRC-32 polynomial
2786
2787 for (unsigned int i = 0; i < 256; i++) {
2788 uint32_t c = i;
2789 for (unsigned int j = 0; j < 8; j++) {
2790 if ( c & 1 ) c = p ^ (c >> 1);
2791 else c = c >> 1;
2792 }
2793 table[i] = c;
2794 }
2795
2796 return table;
2797 }
2798
2799 uint32_t SegmentMachO::crc32()
2800 {
2801 if ( !readable() ) return 0;
2802
2803 static uint32_t *crcTable = NULL;
2804 if ( !crcTable ) crcTable = buildCRCTable();
2805
2806 uint32_t crc = ~(uint32_t)0;
2807 uint8_t *p = (uint8_t *)getActualLoadAddress();
2808 uint8_t *end = p + getSize();
2809 while ( p < end ) {
2810 crc = crcTable[(crc & 0xff) ^ (*p++)] ^ (crc >> 8);
2811 }
2812 return crc ^ ~(uint32_t)0;
2813 }
2814
2815
2816
2817
2818