]> git.saurik.com Git - apple/dyld.git/blob - src/ImageLoaderMachO.cpp
b068b7a03779efb0bfba4f7c656c5076d002f071
[apple/dyld.git] / src / ImageLoaderMachO.cpp
1 /* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*-
2 *
3 * Copyright (c) 2004-2010 Apple 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 // work around until conformance work is complete rdar://problem/4508801
26 #define __srr0 srr0
27 #define __eip eip
28 #define __rip rip
29
30 #define __STDC_LIMIT_MACROS
31 #include <string.h>
32 #include <fcntl.h>
33 #include <errno.h>
34 #include <sys/types.h>
35 #include <sys/fcntl.h>
36 #include <sys/stat.h>
37 #include <sys/mman.h>
38 #include <mach/mach.h>
39 #include <mach/thread_status.h>
40 #include <mach-o/loader.h>
41 #include <mach-o/nlist.h>
42 #include <sys/sysctl.h>
43 #include <libkern/OSAtomic.h>
44 #include <libkern/OSCacheControl.h>
45 #include <stdint.h>
46
47 #include "ImageLoaderMachO.h"
48 #include "ImageLoaderMachOCompressed.h"
49 #include "ImageLoaderMachOClassic.h"
50 #include "mach-o/dyld_images.h"
51
52 // <rdar://problem/8718137> use stack guard random value to add padding between dylibs
53 extern "C" long __stack_chk_guard;
54
55 #ifndef LC_LOAD_UPWARD_DYLIB
56 #define LC_LOAD_UPWARD_DYLIB (0x23|LC_REQ_DYLD) /* load of dylib whose initializers run later */
57 #endif
58
59 // relocation_info.r_length field has value 3 for 64-bit executables and value 2 for 32-bit executables
60 #if __LP64__
61 #define LC_SEGMENT_COMMAND LC_SEGMENT_64
62 #define LC_ROUTINES_COMMAND LC_ROUTINES_64
63 struct macho_segment_command : public segment_command_64 {};
64 struct macho_section : public section_64 {};
65 struct macho_routines_command : public routines_command_64 {};
66 #else
67 #define LC_SEGMENT_COMMAND LC_SEGMENT
68 #define LC_ROUTINES_COMMAND LC_ROUTINES
69 struct macho_segment_command : public segment_command {};
70 struct macho_section : public section {};
71 struct macho_routines_command : public routines_command {};
72 #endif
73
74 uint32_t ImageLoaderMachO::fgSymbolTableBinarySearchs = 0;
75 uint32_t ImageLoaderMachO::fgSymbolTrieSearchs = 0;
76
77
78 ImageLoaderMachO::ImageLoaderMachO(const macho_header* mh, const char* path, unsigned int segCount,
79 uint32_t segOffsets[], unsigned int libCount)
80 : ImageLoader(path, libCount), fMachOData((uint8_t*)mh), fLinkEditBase(NULL), fSlide(0),
81 fEHFrameSectionOffset(0), fUnwindInfoSectionOffset(0), fDylibIDOffset(0),
82 fSegmentsCount(segCount), fIsSplitSeg(false), fInSharedCache(false),
83 #if TEXT_RELOC_SUPPORT
84 fTextSegmentRebases(false),
85 fTextSegmentBinds(false),
86 #endif
87 #if __i386__
88 fReadOnlyImportSegment(false),
89 #endif
90 fHasSubLibraries(false), fHasSubUmbrella(false), fInUmbrella(false), fHasDOFSections(false), fHasDashInit(false),
91 fHasInitializers(false), fHasTerminators(false), fGoodFirstSegment(false), fRegisteredAsRequiresCoalescing(false)
92 {
93 fIsSplitSeg = ((mh->flags & MH_SPLIT_SEGS) != 0);
94
95 // construct SegmentMachO object for each LC_SEGMENT cmd using "placement new" to put
96 // each SegmentMachO object in array at end of ImageLoaderMachO object
97 const uint32_t cmd_count = mh->ncmds;
98 const struct load_command* const cmds = (struct load_command*)&fMachOData[sizeof(macho_header)];
99 const struct load_command* cmd = cmds;
100 for (uint32_t i = 0, segIndex=0; i < cmd_count; ++i) {
101 if ( cmd->cmd == LC_SEGMENT_COMMAND ) {
102 const struct macho_segment_command* segCmd = (struct macho_segment_command*)cmd;
103 // ignore zero-sized segments
104 if ( segCmd->vmsize != 0 ) {
105 // record offset of load command
106 segOffsets[segIndex++] = (uint8_t*)segCmd - fMachOData;
107 }
108 }
109 cmd = (const struct load_command*)(((char*)cmd)+cmd->cmdsize);
110 }
111
112 }
113
114
115 // determine if this mach-o file has classic or compressed LINKEDIT and number of segments it has
116 void ImageLoaderMachO::sniffLoadCommands(const macho_header* mh, const char* path, bool* compressed,
117 unsigned int* segCount, unsigned int* libCount,
118 const linkedit_data_command** codeSigCmd)
119 {
120 *compressed = false;
121 *segCount = 0;
122 *libCount = 0;
123 *codeSigCmd = NULL;
124 const uint32_t cmd_count = mh->ncmds;
125 const struct load_command* const cmds = (struct load_command*)(((uint8_t*)mh) + sizeof(macho_header));
126 const struct load_command* const endCmds = (struct load_command*)(((uint8_t*)mh) + sizeof(macho_header) + mh->sizeofcmds);
127 const struct load_command* cmd = cmds;
128 for (uint32_t i = 0; i < cmd_count; ++i) {
129 switch (cmd->cmd) {
130 case LC_DYLD_INFO:
131 case LC_DYLD_INFO_ONLY:
132 *compressed = true;
133 break;
134 case LC_SEGMENT_COMMAND:
135 // ignore zero-sized segments
136 if ( ((struct macho_segment_command*)cmd)->vmsize != 0 )
137 *segCount += 1;
138 break;
139 case LC_LOAD_DYLIB:
140 case LC_LOAD_WEAK_DYLIB:
141 case LC_REEXPORT_DYLIB:
142 case LC_LOAD_UPWARD_DYLIB:
143 *libCount += 1;
144 break;
145 case LC_CODE_SIGNATURE:
146 *codeSigCmd = (struct linkedit_data_command*)cmd; // only support one LC_CODE_SIGNATURE per image
147 break;
148 }
149 uint32_t cmdLength = cmd->cmdsize;
150 cmd = (const struct load_command*)(((char*)cmd)+cmdLength);
151 if ( cmd > endCmds ) {
152 dyld::throwf("malformed mach-o image: load command #%d length (%u) would exceed sizeofcmds (%u) in %s",
153 i, cmdLength, mh->sizeofcmds, path);
154 }
155 }
156 // fSegmentsArrayCount is only 8-bits
157 if ( *segCount > 255 )
158 dyld::throwf("malformed mach-o image: more than 255 segments in %s", path);
159
160 // fSegmentsArrayCount is only 8-bits
161 if ( *libCount > 4095 )
162 dyld::throwf("malformed mach-o image: more than 4095 dependent libraries in %s", path);
163
164 if ( needsAddedLibSystemDepency(*libCount, mh) )
165 *libCount = 1;
166 }
167
168
169
170 // create image for main executable
171 ImageLoader* ImageLoaderMachO::instantiateMainExecutable(const macho_header* mh, uintptr_t slide, const char* path, const LinkContext& context)
172 {
173 //dyld::log("ImageLoader=%ld, ImageLoaderMachO=%ld, ImageLoaderMachOClassic=%ld, ImageLoaderMachOCompressed=%ld\n",
174 // sizeof(ImageLoader), sizeof(ImageLoaderMachO), sizeof(ImageLoaderMachOClassic), sizeof(ImageLoaderMachOCompressed));
175 bool compressed;
176 unsigned int segCount;
177 unsigned int libCount;
178 const linkedit_data_command* codeSigCmd;
179 sniffLoadCommands(mh, path, &compressed, &segCount, &libCount, &codeSigCmd);
180 // instantiate concrete class based on content of load commands
181 if ( compressed )
182 return ImageLoaderMachOCompressed::instantiateMainExecutable(mh, slide, path, segCount, libCount, context);
183 else
184 return ImageLoaderMachOClassic::instantiateMainExecutable(mh, slide, path, segCount, libCount, context);
185 }
186
187
188 // create image by mapping in a mach-o file
189 ImageLoader* ImageLoaderMachO::instantiateFromFile(const char* path, int fd, const uint8_t firstPage[4096], uint64_t offsetInFat,
190 uint64_t lenInFat, const struct stat& info, const LinkContext& context)
191 {
192 // get load commands
193 const unsigned int dataSize = sizeof(macho_header) + ((macho_header*)firstPage)->sizeofcmds;
194 uint8_t buffer[dataSize];
195 const uint8_t* fileData = firstPage;
196 if ( dataSize > 4096 ) {
197 // only read more if cmds take up more space than first page
198 fileData = buffer;
199 memcpy(buffer, firstPage, 4096);
200 pread(fd, &buffer[4096], dataSize-4096, offsetInFat+4096);
201 }
202
203 bool compressed;
204 unsigned int segCount;
205 unsigned int libCount;
206 const linkedit_data_command* codeSigCmd;
207 sniffLoadCommands((const macho_header*)fileData, path, &compressed, &segCount, &libCount, &codeSigCmd);
208 // instantiate concrete class based on content of load commands
209 if ( compressed )
210 return ImageLoaderMachOCompressed::instantiateFromFile(path, fd, fileData, offsetInFat, lenInFat, info, segCount, libCount, codeSigCmd, context);
211 else
212 return ImageLoaderMachOClassic::instantiateFromFile(path, fd, fileData, offsetInFat, lenInFat, info, segCount, libCount, codeSigCmd, context);
213 }
214
215 // create image by using cached mach-o file
216 ImageLoader* ImageLoaderMachO::instantiateFromCache(const macho_header* mh, const char* path, long slide, const struct stat& info, const LinkContext& context)
217 {
218 // instantiate right concrete class
219 bool compressed;
220 unsigned int segCount;
221 unsigned int libCount;
222 const linkedit_data_command* codeSigCmd;
223 sniffLoadCommands(mh, path, &compressed, &segCount, &libCount, &codeSigCmd);
224 // instantiate concrete class based on content of load commands
225 if ( compressed )
226 return ImageLoaderMachOCompressed::instantiateFromCache(mh, path, slide, info, segCount, libCount, context);
227 else
228 return ImageLoaderMachOClassic::instantiateFromCache(mh, path, slide, info, segCount, libCount, context);
229 }
230
231 // create image by copying an in-memory mach-o file
232 ImageLoader* ImageLoaderMachO::instantiateFromMemory(const char* moduleName, const macho_header* mh, uint64_t len, const LinkContext& context)
233 {
234 bool compressed;
235 unsigned int segCount;
236 unsigned int libCount;
237 const linkedit_data_command* sigcmd;
238 sniffLoadCommands(mh, moduleName, &compressed, &segCount, &libCount, &sigcmd);
239 // instantiate concrete class based on content of load commands
240 if ( compressed )
241 return ImageLoaderMachOCompressed::instantiateFromMemory(moduleName, mh, len, segCount, libCount, context);
242 else
243 return ImageLoaderMachOClassic::instantiateFromMemory(moduleName, mh, len, segCount, libCount, context);
244 }
245
246
247
248 void ImageLoaderMachO::parseLoadCmds()
249 {
250 // now that segments are mapped in, get real fMachOData, fLinkEditBase, and fSlide
251 for(unsigned int i=0; i < fSegmentsCount; ++i) {
252 // set up pointer to __LINKEDIT segment
253 if ( strcmp(segName(i),"__LINKEDIT") == 0 )
254 fLinkEditBase = (uint8_t*)(segActualLoadAddress(i) - segFileOffset(i));
255 #if TEXT_RELOC_SUPPORT
256 // __TEXT segment always starts at beginning of file and contains mach_header and load commands
257 if ( strcmp(segName(i),"__TEXT") == 0 ) {
258 if ( segHasRebaseFixUps(i) && (fSlide != 0) )
259 fTextSegmentRebases = true;
260 if ( segHasBindFixUps(i) )
261 fTextSegmentBinds = true;
262 }
263 #endif
264 #if __i386__
265 if ( segIsReadOnlyImport(i) )
266 fReadOnlyImportSegment = true;
267 #endif
268 // some segment always starts at beginning of file and contains mach_header and load commands
269 if ( (segFileOffset(i) == 0) && (segFileSize(i) != 0) ) {
270 fMachOData = (uint8_t*)(segActualLoadAddress(i));
271 }
272 }
273
274 // keep count of prebound images with weak exports
275 if ( this->participatesInCoalescing() ) {
276 ++fgImagesRequiringCoalescing;
277 fRegisteredAsRequiresCoalescing = true;
278 if ( this->hasCoalescedExports() )
279 ++fgImagesHasWeakDefinitions;
280 }
281
282 // keep count of images used in shared cache
283 if ( fInSharedCache )
284 ++fgImagesUsedFromSharedCache;
285
286 // walk load commands (mapped in at start of __TEXT segment)
287 const dyld_info_command* dyldInfo = NULL;
288 const macho_nlist* symbolTable = NULL;
289 const char* symbolTableStrings = NULL;
290 const dysymtab_command* dynSymbolTable = NULL;
291 const uint32_t cmd_count = ((macho_header*)fMachOData)->ncmds;
292 const struct load_command* const cmds = (struct load_command*)&fMachOData[sizeof(macho_header)];
293 const struct load_command* cmd = cmds;
294 for (uint32_t i = 0; i < cmd_count; ++i) {
295 switch (cmd->cmd) {
296 case LC_SYMTAB:
297 {
298 const struct symtab_command* symtab = (struct symtab_command*)cmd;
299 symbolTableStrings = (const char*)&fLinkEditBase[symtab->stroff];
300 symbolTable = (macho_nlist*)(&fLinkEditBase[symtab->symoff]);
301 }
302 break;
303 case LC_DYSYMTAB:
304 dynSymbolTable = (struct dysymtab_command*)cmd;
305 break;
306 case LC_SUB_UMBRELLA:
307 fHasSubUmbrella = true;
308 break;
309 case LC_SUB_FRAMEWORK:
310 fInUmbrella = true;
311 break;
312 case LC_SUB_LIBRARY:
313 fHasSubLibraries = true;
314 break;
315 case LC_ROUTINES_COMMAND:
316 fHasDashInit = true;
317 break;
318 case LC_DYLD_INFO:
319 case LC_DYLD_INFO_ONLY:
320 dyldInfo = (struct dyld_info_command*)cmd;
321 break;
322 case LC_SEGMENT_COMMAND:
323 {
324 const struct macho_segment_command* seg = (struct macho_segment_command*)cmd;
325 const bool isTextSeg = (strcmp(seg->segname, "__TEXT") == 0);
326 const struct macho_section* const sectionsStart = (struct macho_section*)((char*)seg + sizeof(struct macho_segment_command));
327 const struct macho_section* const sectionsEnd = &sectionsStart[seg->nsects];
328 for (const struct macho_section* sect=sectionsStart; sect < sectionsEnd; ++sect) {
329 const uint8_t type = sect->flags & SECTION_TYPE;
330 if ( type == S_MOD_INIT_FUNC_POINTERS )
331 fHasInitializers = true;
332 else if ( type == S_MOD_TERM_FUNC_POINTERS )
333 fHasTerminators = true;
334 else if ( type == S_DTRACE_DOF )
335 fHasDOFSections = true;
336 else if ( isTextSeg && (strcmp(sect->sectname, "__eh_frame") == 0) )
337 fEHFrameSectionOffset = (uint8_t*)sect - fMachOData;
338 else if ( isTextSeg && (strcmp(sect->sectname, "__unwind_info") == 0) )
339 fUnwindInfoSectionOffset = (uint8_t*)sect - fMachOData;;
340 }
341 }
342 break;
343 case LC_TWOLEVEL_HINTS:
344 // no longer supported
345 break;
346 case LC_ID_DYLIB:
347 {
348 fDylibIDOffset = (uint8_t*)cmd - fMachOData;
349 }
350 break;
351 case LC_RPATH:
352 case LC_LOAD_WEAK_DYLIB:
353 case LC_REEXPORT_DYLIB:
354 case LC_LOAD_UPWARD_DYLIB:
355 // do nothing, just prevent LC_REQ_DYLD exception from occuring
356 break;
357 default:
358 if ( (cmd->cmd & LC_REQ_DYLD) != 0 )
359 dyld::throwf("unknown required load command 0x%08X", cmd->cmd);
360 }
361 cmd = (const struct load_command*)(((char*)cmd)+cmd->cmdsize);
362 }
363
364 if ( dyldInfo != NULL )
365 this->setDyldInfo(dyldInfo);
366 if ( symbolTable != NULL)
367 this->setSymbolTableInfo(symbolTable, symbolTableStrings, dynSymbolTable);
368
369 }
370
371 // don't do this work in destructor because we need object to be full subclass
372 // for UnmapSegments() to work
373 void ImageLoaderMachO::destroy()
374 {
375 // update count of images with weak exports
376 if ( fRegisteredAsRequiresCoalescing ) {
377 --fgImagesRequiringCoalescing;
378 if ( this->hasCoalescedExports() )
379 --fgImagesHasWeakDefinitions;
380 }
381
382 // keep count of images used in shared cache
383 if ( fInSharedCache )
384 --fgImagesUsedFromSharedCache;
385
386 // unmap image when done
387 UnmapSegments();
388 }
389
390
391 unsigned int ImageLoaderMachO::segmentCount() const
392 {
393 return fSegmentsCount;
394 }
395
396
397 const macho_segment_command* ImageLoaderMachO::segLoadCommand(unsigned int segIndex) const
398 {
399 uint32_t* lcOffsets = this->segmentCommandOffsets();
400 uint32_t lcOffset = lcOffsets[segIndex];
401 return (macho_segment_command*)(&fMachOData[lcOffset]);
402 }
403
404 const char* ImageLoaderMachO::segName(unsigned int segIndex) const
405 {
406 return segLoadCommand(segIndex)->segname;
407 }
408
409
410 uintptr_t ImageLoaderMachO::segSize(unsigned int segIndex) const
411 {
412 return segLoadCommand(segIndex)->vmsize;
413 }
414
415
416 uintptr_t ImageLoaderMachO::segFileSize(unsigned int segIndex) const
417 {
418 return segLoadCommand(segIndex)->filesize;
419 }
420
421
422 bool ImageLoaderMachO::segHasTrailingZeroFill(unsigned int segIndex)
423 {
424 return ( segWriteable(segIndex) && (segSize(segIndex) > segFileSize(segIndex)) );
425 }
426
427
428 uintptr_t ImageLoaderMachO::segFileOffset(unsigned int segIndex) const
429 {
430 return segLoadCommand(segIndex)->fileoff;
431 }
432
433
434 bool ImageLoaderMachO::segReadable(unsigned int segIndex) const
435 {
436 return ( (segLoadCommand(segIndex)->initprot & VM_PROT_READ) != 0);
437 }
438
439
440 bool ImageLoaderMachO::segWriteable(unsigned int segIndex) const
441 {
442 return ( (segLoadCommand(segIndex)->initprot & VM_PROT_WRITE) != 0);
443 }
444
445
446 bool ImageLoaderMachO::segExecutable(unsigned int segIndex) const
447 {
448 return ( (segLoadCommand(segIndex)->initprot & VM_PROT_EXECUTE) != 0);
449 }
450
451
452 bool ImageLoaderMachO::segUnaccessible(unsigned int segIndex) const
453 {
454 return (segLoadCommand(segIndex)->initprot == 0);
455 }
456
457 bool ImageLoaderMachO::segHasPreferredLoadAddress(unsigned int segIndex) const
458 {
459 return (segLoadCommand(segIndex)->vmaddr != 0);
460 }
461
462 uintptr_t ImageLoaderMachO::segPreferredLoadAddress(unsigned int segIndex) const
463 {
464 return segLoadCommand(segIndex)->vmaddr;
465 }
466
467 uintptr_t ImageLoaderMachO::segActualLoadAddress(unsigned int segIndex) const
468 {
469 return segLoadCommand(segIndex)->vmaddr + fSlide;
470 }
471
472
473 uintptr_t ImageLoaderMachO::segActualEndAddress(unsigned int segIndex) const
474 {
475 return segActualLoadAddress(segIndex) + segSize(segIndex);
476 }
477
478 bool ImageLoaderMachO::segHasRebaseFixUps(unsigned int segIndex) const
479 {
480 // scan sections for fix-up bit
481 const macho_segment_command* segCmd = segLoadCommand(segIndex);
482 const struct macho_section* const sectionsStart = (struct macho_section*)((char*)segCmd + sizeof(struct macho_segment_command));
483 const struct macho_section* const sectionsEnd = &sectionsStart[segCmd->nsects];
484 for (const struct macho_section* sect=sectionsStart; sect < sectionsEnd; ++sect) {
485 if ( (sect->flags & S_ATTR_LOC_RELOC) != 0 )
486 return true;
487 }
488 return false;
489 }
490
491 bool ImageLoaderMachO::segHasBindFixUps(unsigned int segIndex) const
492 {
493 // scan sections for fix-up bit
494 const macho_segment_command* segCmd = segLoadCommand(segIndex);
495 const struct macho_section* const sectionsStart = (struct macho_section*)((char*)segCmd + sizeof(struct macho_segment_command));
496 const struct macho_section* const sectionsEnd = &sectionsStart[segCmd->nsects];
497 for (const struct macho_section* sect=sectionsStart; sect < sectionsEnd; ++sect) {
498 if ( (sect->flags & S_ATTR_EXT_RELOC) != 0 )
499 return true;
500 }
501 return false;
502 }
503
504 #if __i386__
505 bool ImageLoaderMachO::segIsReadOnlyImport(unsigned int segIndex) const
506 {
507 const macho_segment_command* segCmd = segLoadCommand(segIndex);
508 return ( (segCmd->initprot & VM_PROT_EXECUTE)
509 && ((segCmd->initprot & VM_PROT_WRITE) == 0)
510 && (strcmp(segCmd->segname, "__IMPORT") == 0) );
511 }
512 #endif
513
514
515 void ImageLoaderMachO::UnmapSegments()
516 {
517 // usually unmap image when done
518 if ( ! this->leaveMapped() && (this->getState() >= dyld_image_state_mapped) ) {
519 // unmap TEXT segment last because it contains load command being inspected
520 unsigned int textSegmentIndex = 0;
521 for(unsigned int i=0; i < fSegmentsCount; ++i) {
522 //dyld::log("unmap %s at 0x%08lX\n", seg->getName(), seg->getActualLoadAddress(this));
523 if ( strcmp(segName(i), "__TEXT") == 0 ) {
524 textSegmentIndex = i;
525 }
526 else {
527 // update stats
528 --ImageLoader::fgTotalSegmentsMapped;
529 ImageLoader::fgTotalBytesMapped -= segSize(i);
530 munmap((void*)segActualLoadAddress(i), segSize(i));
531 }
532 }
533 // now unmap TEXT
534 --ImageLoader::fgTotalSegmentsMapped;
535 ImageLoader::fgTotalBytesMapped -= segSize(textSegmentIndex);
536 munmap((void*)segActualLoadAddress(textSegmentIndex), segSize(textSegmentIndex));
537 }
538 }
539
540
541 // prefetch __DATA/__OBJC pages during launch, but not for dynamically loaded code
542 void ImageLoaderMachO::preFetchDATA(int fd, uint64_t offsetInFat, const LinkContext& context)
543 {
544 if ( context.linkingMainExecutable ) {
545 for(unsigned int i=0, e=segmentCount(); i < e; ++i) {
546 if ( segWriteable(i) && (segFileSize(i) > 0) ) {
547 // prefetch writable segment that have mmap'ed regions
548 radvisory advice;
549 advice.ra_offset = offsetInFat + segFileOffset(i);
550 advice.ra_count = segFileSize(i);
551 // limit prefetch to 1MB (256 pages)
552 if ( advice.ra_count > 1024*1024 )
553 advice.ra_count = 1024*1024;
554 // don't prefetch single pages, let them fault in
555 fgTotalBytesPreFetched += advice.ra_count;
556 fcntl(fd, F_RDADVISE, &advice);
557 if ( context.verboseMapping ) {
558 dyld::log("%18s prefetching 0x%0lX -> 0x%0lX\n",
559 segName(i), segActualLoadAddress(i), segActualLoadAddress(i)+advice.ra_count-1);
560 }
561 }
562 }
563 }
564 }
565
566
567 bool ImageLoaderMachO::segmentsMustSlideTogether() const
568 {
569 return true;
570 }
571
572 bool ImageLoaderMachO::segmentsCanSlide() const
573 {
574 return (this->isDylib() || this->isBundle() || this->isPositionIndependentExecutable());
575 }
576
577 bool ImageLoaderMachO::isBundle() const
578 {
579 const macho_header* mh = (macho_header*)fMachOData;
580 return ( mh->filetype == MH_BUNDLE );
581 }
582
583 bool ImageLoaderMachO::isDylib() const
584 {
585 const macho_header* mh = (macho_header*)fMachOData;
586 return ( mh->filetype == MH_DYLIB );
587 }
588
589 bool ImageLoaderMachO::isExecutable() const
590 {
591 const macho_header* mh = (macho_header*)fMachOData;
592 return ( mh->filetype == MH_EXECUTE );
593 }
594
595 bool ImageLoaderMachO::isPositionIndependentExecutable() const
596 {
597 const macho_header* mh = (macho_header*)fMachOData;
598 return ( (mh->filetype == MH_EXECUTE) && ((mh->flags & MH_PIE) != 0) );
599 }
600
601
602 bool ImageLoaderMachO::forceFlat() const
603 {
604 const macho_header* mh = (macho_header*)fMachOData;
605 return ( (mh->flags & MH_FORCE_FLAT) != 0 );
606 }
607
608 bool ImageLoaderMachO::usesTwoLevelNameSpace() const
609 {
610 const macho_header* mh = (macho_header*)fMachOData;
611 return ( (mh->flags & MH_TWOLEVEL) != 0 );
612 }
613
614 bool ImageLoaderMachO::isPrebindable() const
615 {
616 const macho_header* mh = (macho_header*)fMachOData;
617 return ( (mh->flags & MH_PREBOUND) != 0 );
618 }
619
620 bool ImageLoaderMachO::hasCoalescedExports() const
621 {
622 const macho_header* mh = (macho_header*)fMachOData;
623 return ( (mh->flags & MH_WEAK_DEFINES) != 0 );
624 }
625
626 bool ImageLoaderMachO::hasReferencesToWeakSymbols() const
627 {
628 const macho_header* mh = (macho_header*)fMachOData;
629 return ( (mh->flags & MH_BINDS_TO_WEAK) != 0 );
630 }
631
632 bool ImageLoaderMachO::participatesInCoalescing() const
633 {
634 const macho_header* mh = (macho_header*)fMachOData;
635 // if image is loaded with RTLD_LOCAL, then its symbols' visibility
636 // is reduced and it can't coalesce with other images
637 if ( this->hasHiddenExports() )
638 return false;
639 return ( (mh->flags & (MH_WEAK_DEFINES|MH_BINDS_TO_WEAK)) != 0 );
640 }
641
642
643
644 void ImageLoaderMachO::setSlide(intptr_t slide)
645 {
646 fSlide = slide;
647 }
648
649 #if CODESIGNING_SUPPORT
650 void ImageLoaderMachO::loadCodeSignature(const struct linkedit_data_command* codeSigCmd, int fd, uint64_t offsetInFatFile)
651 {
652 fsignatures_t siginfo;
653 siginfo.fs_file_start=offsetInFatFile; // start of mach-o slice in fat file
654 siginfo.fs_blob_start=(void*)(codeSigCmd->dataoff); // start of CD in mach-o file
655 siginfo.fs_blob_size=codeSigCmd->datasize; // size of CD
656 int result = fcntl(fd, F_ADDFILESIGS, &siginfo);
657 if ( result == -1 )
658 dyld::log("dyld: F_ADDFILESIGS failed for %s with errno=%d\n", this->getPath(), errno);
659 //dyld::log("dyld: registered code signature for %s\n", this->getPath());
660 }
661 #endif
662
663
664 const char* ImageLoaderMachO::getInstallPath() const
665 {
666 if ( fDylibIDOffset != 0 ) {
667 const dylib_command* dylibID = (dylib_command*)(&fMachOData[fDylibIDOffset]);
668 return (char*)dylibID + dylibID->dylib.name.offset;
669 }
670 return NULL;
671 }
672
673 void ImageLoaderMachO::registerInterposing()
674 {
675 // mach-o files advertise interposing by having a __DATA __interpose section
676 uintptr_t textStart = this->segActualLoadAddress(0);
677 uintptr_t textEnd = this->segActualEndAddress(0);
678 // <rdar://problem/8268602> verify that the first segment load command is for a read-only segment
679 if ( ! fGoodFirstSegment )
680 return;
681 struct InterposeData { uintptr_t replacement; uintptr_t replacee; };
682 const uint32_t cmd_count = ((macho_header*)fMachOData)->ncmds;
683 const struct load_command* const cmds = (struct load_command*)&fMachOData[sizeof(macho_header)];
684 const struct load_command* cmd = cmds;
685 for (uint32_t i = 0; i < cmd_count; ++i) {
686 switch (cmd->cmd) {
687 case LC_SEGMENT_COMMAND:
688 {
689 const struct macho_segment_command* seg = (struct macho_segment_command*)cmd;
690 const struct macho_section* const sectionsStart = (struct macho_section*)((char*)seg + sizeof(struct macho_segment_command));
691 const struct macho_section* const sectionsEnd = &sectionsStart[seg->nsects];
692 for (const struct macho_section* sect=sectionsStart; sect < sectionsEnd; ++sect) {
693 if ( ((sect->flags & SECTION_TYPE) == S_INTERPOSING) || ((strcmp(sect->sectname, "__interpose") == 0) && (strcmp(seg->segname, "__DATA") == 0)) ) {
694 const InterposeData* interposeArray = (InterposeData*)(sect->addr + fSlide);
695 const unsigned int count = sect->size / sizeof(InterposeData);
696 for (uint32_t i=0; i < count; ++i) {
697 ImageLoader::InterposeTuple tuple;
698 tuple.replacement = interposeArray[i].replacement;
699 tuple.replacementImage = this;
700 tuple.replacee = interposeArray[i].replacee;
701 // <rdar://problem/7937695> verify that replacement is in this image
702 if ( (tuple.replacement >= textStart) && (tuple.replacement < textEnd) ) {
703 for (std::vector<InterposeTuple>::iterator it=fgInterposingTuples.begin(); it != fgInterposingTuples.end(); it++) {
704 if ( it->replacee == tuple.replacee ) {
705 tuple.replacee = it->replacement;
706 }
707 }
708 ImageLoader::fgInterposingTuples.push_back(tuple);
709 }
710 }
711 }
712 }
713 }
714 break;
715 }
716 cmd = (const struct load_command*)(((char*)cmd)+cmd->cmdsize);
717 }
718 }
719
720
721 void* ImageLoaderMachO::getMain() const
722 {
723 const uint32_t cmd_count = ((macho_header*)fMachOData)->ncmds;
724 const struct load_command* const cmds = (struct load_command*)&fMachOData[sizeof(macho_header)];
725 const struct load_command* cmd = cmds;
726 for (uint32_t i = 0; i < cmd_count; ++i) {
727 switch (cmd->cmd) {
728 case LC_UNIXTHREAD:
729 {
730 #if __ppc__
731 const ppc_thread_state_t* registers = (ppc_thread_state_t*)(((char*)cmd) + 16);
732 return (void*)(registers->srr0 + fSlide);
733 #elif __ppc64__
734 const ppc_thread_state64_t* registers = (ppc_thread_state64_t*)(((char*)cmd) + 16);
735 return (void*)(registers->srr0 + fSlide);
736 #elif __i386__
737 const i386_thread_state_t* registers = (i386_thread_state_t*)(((char*)cmd) + 16);
738 return (void*)(registers->eip + fSlide);
739 #elif __x86_64__
740 const x86_thread_state64_t* registers = (x86_thread_state64_t*)(((char*)cmd) + 16);
741 return (void*)(registers->rip + fSlide);
742 #elif __arm__
743 const arm_thread_state_t* registers = (arm_thread_state_t*)(((char*)cmd) + 16);
744 return (void*)(registers->__pc + fSlide);
745 #else
746 #warning need processor specific code
747 #endif
748 }
749 break;
750 }
751 cmd = (const struct load_command*)(((char*)cmd)+cmd->cmdsize);
752 }
753 return NULL;
754 }
755
756 bool ImageLoaderMachO::needsAddedLibSystemDepency(unsigned int libCount, const macho_header* mh)
757 {
758 // <rdar://problem/6357561> ensure that every image depends on something which depends on libSystem
759 if ( libCount > 1 )
760 return false;
761
762 // <rdar://problem/6409800> dyld implicit-libSystem breaks valgrind
763 if ( mh->filetype == MH_EXECUTE )
764 return false;
765
766 bool isNonOSdylib = false;
767 const uint32_t cmd_count = mh->ncmds;
768 const struct load_command* const cmds = (struct load_command*)((uint8_t*)mh+sizeof(macho_header));
769 const struct load_command* cmd = cmds;
770 for (uint32_t i = 0; i < cmd_count; ++i) {
771 switch (cmd->cmd) {
772 case LC_LOAD_DYLIB:
773 case LC_LOAD_WEAK_DYLIB:
774 case LC_REEXPORT_DYLIB:
775 case LC_LOAD_UPWARD_DYLIB:
776 return false;
777 case LC_ID_DYLIB:
778 {
779 const dylib_command* dylibID = (dylib_command*)cmd;
780 const char* installPath = (char*)cmd + dylibID->dylib.name.offset;
781 // It is OK for OS dylibs (libSystem or libmath or Rosetta shims) to have no dependents
782 // but all other dylibs must depend on libSystem for initialization to initialize libSystem first
783 // <rdar://problem/6497528> rosetta circular dependency spew
784 isNonOSdylib = ( (strncmp(installPath, "/usr/lib/", 9) != 0) && (strncmp(installPath, "/usr/libexec/oah/Shims", 9) != 0) );
785 }
786 break;
787 }
788 cmd = (const struct load_command*)(((char*)cmd)+cmd->cmdsize);
789 }
790 return isNonOSdylib;
791 }
792
793
794 void ImageLoaderMachO::doGetDependentLibraries(DependentLibraryInfo libs[])
795 {
796 if ( needsAddedLibSystemDepency(libraryCount(), (macho_header*)fMachOData) ) {
797 DependentLibraryInfo* lib = &libs[0];
798 lib->name = "/usr/lib/libSystem.B.dylib";
799 lib->info.checksum = 0;
800 lib->info.minVersion = 0;
801 lib->info.maxVersion = 0;
802 lib->required = false;
803 lib->reExported = false;
804 lib->upward = false;
805 }
806 else {
807 uint32_t index = 0;
808 const uint32_t cmd_count = ((macho_header*)fMachOData)->ncmds;
809 const struct load_command* const cmds = (struct load_command*)&fMachOData[sizeof(macho_header)];
810 const struct load_command* cmd = cmds;
811 for (uint32_t i = 0; i < cmd_count; ++i) {
812 switch (cmd->cmd) {
813 case LC_LOAD_DYLIB:
814 case LC_LOAD_WEAK_DYLIB:
815 case LC_REEXPORT_DYLIB:
816 case LC_LOAD_UPWARD_DYLIB:
817 {
818 const struct dylib_command* dylib = (struct dylib_command*)cmd;
819 DependentLibraryInfo* lib = &libs[index++];
820 lib->name = (char*)cmd + dylib->dylib.name.offset;
821 //lib->name = strdup((char*)cmd + dylib->dylib.name.offset);
822 lib->info.checksum = dylib->dylib.timestamp;
823 lib->info.minVersion = dylib->dylib.compatibility_version;
824 lib->info.maxVersion = dylib->dylib.current_version;
825 lib->required = (cmd->cmd != LC_LOAD_WEAK_DYLIB);
826 lib->reExported = (cmd->cmd == LC_REEXPORT_DYLIB);
827 lib->upward = (cmd->cmd == LC_LOAD_UPWARD_DYLIB);
828 }
829 break;
830 }
831 cmd = (const struct load_command*)(((char*)cmd)+cmd->cmdsize);
832 }
833 }
834 }
835
836 ImageLoader::LibraryInfo ImageLoaderMachO::doGetLibraryInfo()
837 {
838 LibraryInfo info;
839 if ( fDylibIDOffset != 0 ) {
840 const dylib_command* dylibID = (dylib_command*)(&fMachOData[fDylibIDOffset]);
841 info.minVersion = dylibID->dylib.compatibility_version;
842 info.maxVersion = dylibID->dylib.current_version;
843 info.checksum = dylibID->dylib.timestamp;
844 }
845 else {
846 info.minVersion = 0;
847 info.maxVersion = 0;
848 info.checksum = 0;
849 }
850 return info;
851 }
852
853 void ImageLoaderMachO::getRPaths(const LinkContext& context, std::vector<const char*>& paths) const
854 {
855 const uint32_t cmd_count = ((macho_header*)fMachOData)->ncmds;
856 const struct load_command* const cmds = (struct load_command*)&fMachOData[sizeof(macho_header)];
857 const struct load_command* cmd = cmds;
858 for (uint32_t i = 0; i < cmd_count; ++i) {
859 switch (cmd->cmd) {
860 case LC_RPATH:
861 const char* pathToAdd = NULL;
862 const char* path = (char*)cmd + ((struct rpath_command*)cmd)->path.offset;
863 if ( strncmp(path, "@loader_path/", 13) == 0 ) {
864 if ( context.processIsRestricted && (context.mainExecutable == this) ) {
865 dyld::warn("LC_RPATH %s in %s being ignored in restricted program because of @loader_path\n", path, this->getPath());
866 break;
867 }
868 char resolvedPath[PATH_MAX];
869 if ( realpath(this->getPath(), resolvedPath) != NULL ) {
870 char newRealPath[strlen(resolvedPath) + strlen(path)];
871 strcpy(newRealPath, resolvedPath);
872 char* addPoint = strrchr(newRealPath,'/');
873 if ( addPoint != NULL )
874 strcpy(&addPoint[1], &path[13]);
875 else
876 strcpy(newRealPath, &path[13]);
877 pathToAdd = strdup(newRealPath);
878 }
879 }
880 else if ( strncmp(path, "@executable_path/", 17) == 0 ) {
881 if ( context.processIsRestricted ) {
882 dyld::warn("LC_RPATH %s in %s being ignored in restricted program because of @executable_path\n", path, this->getPath());
883 break;
884 }
885 char resolvedPath[PATH_MAX];
886 if ( realpath(context.mainExecutable->getPath(), resolvedPath) != NULL ) {
887 char newRealPath[strlen(resolvedPath) + strlen(path)];
888 strcpy(newRealPath, resolvedPath);
889 char* addPoint = strrchr(newRealPath,'/');
890 if ( addPoint != NULL )
891 strcpy(&addPoint[1], &path[17]);
892 else
893 strcpy(newRealPath, &path[17]);
894 pathToAdd = strdup(newRealPath);
895 }
896 }
897 else if ( (path[0] != '/') && context.processIsRestricted ) {
898 dyld::warn("LC_RPATH %s in %s being ignored in restricted program because it is a relative path\n", path, this->getPath());
899 break;
900 }
901 else if ( (path[0] == '/') && (context.rootPaths != NULL) ) {
902 // <rdar://problem/5869973> DYLD_ROOT_PATH should apply to LC_RPATH rpaths
903 // DYLD_ROOT_PATH can be a list of paths, but at this point we can only support one, so use first combination that exists
904 bool found = false;
905 for(const char** rp = context.rootPaths; *rp != NULL; ++rp) {
906 char newPath[PATH_MAX];
907 strcpy(newPath, *rp);
908 strcat(newPath, path);
909 struct stat stat_buf;
910 if ( stat(newPath, &stat_buf) != -1 ) {
911 //dyld::log("combined DYLD_ROOT_PATH and LC_RPATH: %s\n", newPath);
912 pathToAdd = strdup(newPath);
913 found = true;
914 break;
915 }
916 }
917 if ( ! found ) {
918 // make copy so that all elements of 'paths' can be freed
919 pathToAdd = strdup(path);
920 }
921 }
922 else {
923 // make copy so that all elements of 'paths' can be freed
924 pathToAdd = strdup(path);
925 }
926 if ( pathToAdd != NULL )
927 paths.push_back(pathToAdd);
928 break;
929 }
930 cmd = (const struct load_command*)(((char*)cmd)+cmd->cmdsize);
931 }
932 }
933
934 bool ImageLoaderMachO::getUUID(uuid_t uuid) const
935 {
936 const uint32_t cmd_count = ((macho_header*)fMachOData)->ncmds;
937 const struct load_command* const cmds = (struct load_command*)&fMachOData[sizeof(macho_header)];
938 const struct load_command* cmd = cmds;
939 for (uint32_t i = 0; i < cmd_count; ++i) {
940 switch (cmd->cmd) {
941 case LC_UUID:
942 uuid_command* uc = (uuid_command*)cmd;
943 memcpy(uuid, uc->uuid, 16);
944 return true;
945 }
946 cmd = (const struct load_command*)(((char*)cmd)+cmd->cmdsize);
947 }
948 bzero(uuid, 16);
949 return false;
950 }
951
952 void ImageLoaderMachO::doRebase(const LinkContext& context)
953 {
954 // if prebound and loaded at prebound address, then no need to rebase
955 if ( this->usablePrebinding(context) ) {
956 // skip rebasing because prebinding is valid
957 ++fgImagesWithUsedPrebinding; // bump totals for statistics
958 return;
959 }
960
961 // print why prebinding was not used
962 if ( context.verbosePrebinding ) {
963 if ( !this->isPrebindable() ) {
964 dyld::log("dyld: image not prebound, so could not use prebinding in %s\n", this->getPath());
965 }
966 else if ( fSlide != 0 ) {
967 dyld::log("dyld: image slid, so could not use prebinding in %s\n", this->getPath());
968 }
969 else if ( !this->allDependentLibrariesAsWhenPreBound() ) {
970 dyld::log("dyld: dependent libraries changed, so could not use prebinding in %s\n", this->getPath());
971 }
972 else if ( !this->usesTwoLevelNameSpace() ){
973 dyld::log("dyld: image uses flat-namespace so, parts of prebinding ignored %s\n", this->getPath());
974 }
975 else {
976 dyld::log("dyld: environment variable disabled use of prebinding in %s\n", this->getPath());
977 }
978 }
979
980 //dyld::log("slide=0x%08lX for %s\n", slide, this->getPath());
981
982 #if PREBOUND_IMAGE_SUPPORT
983 // if prebound and we got here, then prebinding is not valid, so reset all lazy pointers
984 // if this image is in the shared cache, do not reset, they will be bound in doBind()
985 if ( this->isPrebindable() && !fInSharedCache )
986 this->resetPreboundLazyPointers(context);
987 #endif
988
989 // if loaded at preferred address, no rebasing necessary
990 if ( this->fSlide == 0 )
991 return;
992
993 #if TEXT_RELOC_SUPPORT
994 // if there are __TEXT fixups, temporarily make __TEXT writable
995 if ( fTextSegmentRebases )
996 this->makeTextSegmentWritable(context, true);
997 #endif
998
999 // do actual rebasing
1000 this->rebase(context);
1001
1002 #if TEXT_RELOC_SUPPORT
1003 // if there were __TEXT fixups, restore write protection
1004 if ( fTextSegmentRebases )
1005 this->makeTextSegmentWritable(context, false);
1006
1007 #endif
1008 }
1009
1010 #if TEXT_RELOC_SUPPORT
1011 void ImageLoaderMachO::makeTextSegmentWritable(const LinkContext& context, bool writeable)
1012 {
1013 int textSegmentIndex = 0;
1014 for(unsigned int i=0; i < fSegmentsCount; ++i) {
1015 if ( strcmp(segName(i), "__TEXT") == 0 ) {
1016 textSegmentIndex = i;
1017 break;
1018 }
1019 }
1020
1021 if ( writeable ) {
1022 segMakeWritable(textSegmentIndex, context);
1023 }
1024 else {
1025 // iPhoneOS requires range to be invalidated before it is made executable
1026 sys_icache_invalidate((void*)segActualLoadAddress(textSegmentIndex), segSize(textSegmentIndex));
1027 segProtect(textSegmentIndex, context);
1028 }
1029 }
1030 #endif
1031
1032 const ImageLoader::Symbol* ImageLoaderMachO::findExportedSymbol(const char* name, bool searchReExports, const ImageLoader** foundIn) const
1033 {
1034 // look in this image first
1035 const ImageLoader::Symbol* result = this->findExportedSymbol(name, foundIn);
1036 if ( result != NULL )
1037 return result;
1038
1039 if ( searchReExports ) {
1040 for(unsigned int i=0; i < libraryCount(); ++i){
1041 if ( libReExported(i) ) {
1042 ImageLoader* image = libImage(i);
1043 if ( image != NULL ) {
1044 const Symbol* result = image->findExportedSymbol(name, searchReExports, foundIn);
1045 if ( result != NULL )
1046 return result;
1047 }
1048 }
1049 }
1050 }
1051
1052
1053 return NULL;
1054 }
1055
1056
1057
1058 uintptr_t ImageLoaderMachO::getExportedSymbolAddress(const Symbol* sym, const LinkContext& context,
1059 const ImageLoader* requestor, bool runResolver) const
1060 {
1061 return this->getSymbolAddress(sym, requestor, context, runResolver);
1062 }
1063
1064 uintptr_t ImageLoaderMachO::getSymbolAddress(const Symbol* sym, const ImageLoader* requestor,
1065 const LinkContext& context, bool runResolver) const
1066 {
1067 uintptr_t result = exportedSymbolAddress(context, sym, runResolver);
1068 // check for interposing overrides
1069 for (std::vector<InterposeTuple>::iterator it=fgInterposingTuples.begin(); it != fgInterposingTuples.end(); it++) {
1070 // replace all references to 'replacee' with 'replacement'
1071 if ( (result == it->replacee) && (requestor != it->replacementImage) ) {
1072 if ( context.verboseInterposing ) {
1073 dyld::log("dyld interposing: replace 0x%lX with 0x%lX in %s\n",
1074 it->replacee, it->replacement, this->getPath());
1075 }
1076 result = it->replacement;
1077 }
1078 }
1079 return result;
1080 }
1081
1082 ImageLoader::DefinitionFlags ImageLoaderMachO::getExportedSymbolInfo(const Symbol* sym) const
1083 {
1084 if ( exportedSymbolIsWeakDefintion(sym) )
1085 return kWeakDefinition;
1086 else
1087 return kNoDefinitionOptions;
1088 }
1089
1090 const char* ImageLoaderMachO::getExportedSymbolName(const Symbol* sym) const
1091 {
1092 return exportedSymbolName(sym);
1093 }
1094
1095 uint32_t ImageLoaderMachO::getExportedSymbolCount() const
1096 {
1097 return exportedSymbolCount();
1098 }
1099
1100
1101 const ImageLoader::Symbol* ImageLoaderMachO::getIndexedExportedSymbol(uint32_t index) const
1102 {
1103 return exportedSymbolIndexed(index);
1104 }
1105
1106
1107 uint32_t ImageLoaderMachO::getImportedSymbolCount() const
1108 {
1109 return importedSymbolCount();
1110 }
1111
1112
1113 const ImageLoader::Symbol* ImageLoaderMachO::getIndexedImportedSymbol(uint32_t index) const
1114 {
1115 return importedSymbolIndexed(index);
1116 }
1117
1118
1119 ImageLoader::ReferenceFlags ImageLoaderMachO::getImportedSymbolInfo(const ImageLoader::Symbol* sym) const
1120 {
1121 ImageLoader::ReferenceFlags flags = kNoReferenceOptions;
1122 return flags;
1123 }
1124
1125
1126 const char* ImageLoaderMachO::getImportedSymbolName(const ImageLoader::Symbol* sym) const
1127 {
1128 return importedSymbolName(sym);
1129 }
1130
1131
1132 bool ImageLoaderMachO::getSectionContent(const char* segmentName, const char* sectionName, void** start, size_t* length)
1133 {
1134 const uint32_t cmd_count = ((macho_header*)fMachOData)->ncmds;
1135 const struct load_command* const cmds = (struct load_command*)&fMachOData[sizeof(macho_header)];
1136 const struct load_command* cmd = cmds;
1137 for (uint32_t i = 0; i < cmd_count; ++i) {
1138 switch (cmd->cmd) {
1139 case LC_SEGMENT_COMMAND:
1140 {
1141 const struct macho_segment_command* seg = (struct macho_segment_command*)cmd;
1142 const struct macho_section* const sectionsStart = (struct macho_section*)((char*)seg + sizeof(struct macho_segment_command));
1143 const struct macho_section* const sectionsEnd = &sectionsStart[seg->nsects];
1144 for (const struct macho_section* sect=sectionsStart; sect < sectionsEnd; ++sect) {
1145 if ( (strcmp(sect->segname, segmentName) == 0) && (strcmp(sect->sectname, sectionName) == 0) ) {
1146 *start = (uintptr_t*)(sect->addr + fSlide);
1147 *length = sect->size;
1148 return true;
1149 }
1150 }
1151 }
1152 break;
1153 }
1154 cmd = (const struct load_command*)(((char*)cmd)+cmd->cmdsize);
1155 }
1156 *start = NULL;
1157 *length = 0;
1158 return false;
1159 }
1160
1161 void ImageLoaderMachO::getUnwindInfo(dyld_unwind_sections* info)
1162 {
1163 info->mh = this->machHeader();
1164 info->dwarf_section = 0;
1165 info->dwarf_section_length = 0;
1166 info->compact_unwind_section = 0;
1167 info->compact_unwind_section_length = 0;
1168 if ( fEHFrameSectionOffset != 0 ) {
1169 const macho_section* sect = (macho_section*)&fMachOData[fEHFrameSectionOffset];
1170 info->dwarf_section = (void*)(sect->addr + fSlide);
1171 info->dwarf_section_length = sect->size;
1172 }
1173 if ( fUnwindInfoSectionOffset != 0 ) {
1174 const macho_section* sect = (macho_section*)&fMachOData[fUnwindInfoSectionOffset];
1175 info->compact_unwind_section = (void*)(sect->addr + fSlide);
1176 info->compact_unwind_section_length = sect->size;
1177 }
1178 }
1179
1180
1181 bool ImageLoaderMachO::findSection(const void* imageInterior, const char** segmentName, const char** sectionName, size_t* sectionOffset)
1182 {
1183 const uint32_t cmd_count = ((macho_header*)fMachOData)->ncmds;
1184 const struct load_command* const cmds = (struct load_command*)&fMachOData[sizeof(macho_header)];
1185 const struct load_command* cmd = cmds;
1186 const uintptr_t unslidInteriorAddress = (uintptr_t)imageInterior - this->getSlide();
1187 for (uint32_t i = 0; i < cmd_count; ++i) {
1188 switch (cmd->cmd) {
1189 case LC_SEGMENT_COMMAND:
1190 {
1191 const struct macho_segment_command* seg = (struct macho_segment_command*)cmd;
1192 if ( (unslidInteriorAddress >= seg->vmaddr) && (unslidInteriorAddress < (seg->vmaddr+seg->vmsize)) ) {
1193 const struct macho_section* const sectionsStart = (struct macho_section*)((char*)seg + sizeof(struct macho_segment_command));
1194 const struct macho_section* const sectionsEnd = &sectionsStart[seg->nsects];
1195 for (const struct macho_section* sect=sectionsStart; sect < sectionsEnd; ++sect) {
1196 if ((sect->addr <= unslidInteriorAddress) && (unslidInteriorAddress < (sect->addr+sect->size))) {
1197 if ( segmentName != NULL )
1198 *segmentName = sect->segname;
1199 if ( sectionName != NULL )
1200 *sectionName = sect->sectname;
1201 if ( sectionOffset != NULL )
1202 *sectionOffset = unslidInteriorAddress - sect->addr;
1203 return true;
1204 }
1205 }
1206 }
1207 }
1208 break;
1209 }
1210 cmd = (const struct load_command*)(((char*)cmd)+cmd->cmdsize);
1211 }
1212 return false;
1213 }
1214
1215
1216 void __attribute__((noreturn)) ImageLoaderMachO::throwSymbolNotFound(const LinkContext& context, const char* symbol,
1217 const char* referencedFrom, const char* expectedIn)
1218 {
1219 // record values for possible use by CrashReporter or Finder
1220 (*context.setErrorStrings)(dyld_error_kind_symbol_missing, referencedFrom, expectedIn, symbol);
1221 dyld::throwf("Symbol not found: %s\n Referenced from: %s\n Expected in: %s\n", symbol, referencedFrom, expectedIn);
1222 }
1223
1224 const mach_header* ImageLoaderMachO::machHeader() const
1225 {
1226 return (mach_header*)fMachOData;
1227 }
1228
1229 uintptr_t ImageLoaderMachO::getSlide() const
1230 {
1231 return fSlide;
1232 }
1233
1234 // hmm. maybe this should be up in ImageLoader??
1235 const void* ImageLoaderMachO::getEnd() const
1236 {
1237 uintptr_t lastAddress = 0;
1238 for(unsigned int i=0; i < fSegmentsCount; ++i) {
1239 uintptr_t segEnd = segActualEndAddress(i);
1240 if ( strcmp(segName(i), "__UNIXSTACK") != 0 ) {
1241 if ( segEnd > lastAddress )
1242 lastAddress = segEnd;
1243 }
1244 }
1245 return (const void*)lastAddress;
1246 }
1247
1248
1249 uintptr_t ImageLoaderMachO::bindLocation(const LinkContext& context, uintptr_t location, uintptr_t value,
1250 const ImageLoader* targetImage, uint8_t type, const char* symbolName,
1251 intptr_t addend, const char* msg)
1252 {
1253 // log
1254 if ( context.verboseBind ) {
1255 if ( addend != 0 )
1256 dyld::log("dyld: %sbind: %s:0x%08lX = %s:%s, *0x%08lX = 0x%08lX + %ld\n",
1257 msg, this->getShortName(), (uintptr_t)location,
1258 ((targetImage != NULL) ? targetImage->getShortName() : "<weak_import-missing>"),
1259 symbolName, (uintptr_t)location, value, addend);
1260 else
1261 dyld::log("dyld: %sbind: %s:0x%08lX = %s:%s, *0x%08lX = 0x%08lX\n",
1262 msg, this->getShortName(), (uintptr_t)location,
1263 ((targetImage != NULL) ? targetImage->getShortName() : "<weak>import-missing>"),
1264 symbolName, (uintptr_t)location, value);
1265 }
1266 #if LOG_BINDINGS
1267 // dyld::logBindings("%s: %s\n", targetImage->getShortName(), symbolName);
1268 #endif
1269
1270 // do actual update
1271 uintptr_t* locationToFix = (uintptr_t*)location;
1272 uint32_t* loc32;
1273 uintptr_t newValue = value+addend;
1274 uint32_t value32;
1275 switch (type) {
1276 case BIND_TYPE_POINTER:
1277 // test first so we don't needless dirty pages
1278 if ( *locationToFix != newValue )
1279 *locationToFix = newValue;
1280 break;
1281 case BIND_TYPE_TEXT_ABSOLUTE32:
1282 loc32 = (uint32_t*)locationToFix;
1283 value32 = (uint32_t)newValue;
1284 if ( *loc32 != value32 )
1285 *loc32 = value32;
1286 break;
1287 case BIND_TYPE_TEXT_PCREL32:
1288 loc32 = (uint32_t*)locationToFix;
1289 value32 = (uint32_t)newValue - (((uintptr_t)locationToFix) + 4);
1290 if ( *loc32 != value32 )
1291 *loc32 = value32;
1292 break;
1293 default:
1294 dyld::throwf("bad bind type %d", type);
1295 }
1296
1297 // update statistics
1298 ++fgTotalBindFixups;
1299
1300 return newValue;
1301 }
1302
1303
1304
1305
1306
1307 #if SUPPORT_OLD_CRT_INITIALIZATION
1308 // first 16 bytes of "start" in crt1.o
1309 #if __ppc__
1310 static uint32_t sStandardEntryPointInstructions[4] = { 0x7c3a0b78, 0x3821fffc, 0x54210034, 0x38000000 };
1311 #elif __i386__
1312 static uint8_t sStandardEntryPointInstructions[16] = { 0x6a, 0x00, 0x89, 0xe5, 0x83, 0xe4, 0xf0, 0x83, 0xec, 0x10, 0x8b, 0x5d, 0x04, 0x89, 0x5c, 0x24 };
1313 #endif
1314 #endif
1315
1316 struct DATAdyld {
1317 void* dyldLazyBinder; // filled in at launch by dyld to point into dyld to &stub_binding_helper_interface
1318 void* dyldFuncLookup; // filled in at launch by dyld to point into dyld to &_dyld_func_lookup
1319 // the following only exist in main executables built for 10.5 or later
1320 ProgramVars vars;
1321 };
1322
1323 // These are defined in dyldStartup.s
1324 extern "C" void stub_binding_helper();
1325 extern "C" bool dyld_func_lookup(const char* name, uintptr_t* address);
1326
1327
1328 void ImageLoaderMachO::setupLazyPointerHandler(const LinkContext& context)
1329 {
1330 const macho_header* mh = (macho_header*)fMachOData;
1331 const uint32_t cmd_count = mh->ncmds;
1332 const struct load_command* const cmds = (struct load_command*)&fMachOData[sizeof(macho_header)];
1333 const struct load_command* cmd;
1334 // set up __dyld section
1335 // optimizations:
1336 // 1) do nothing if image is in dyld shared cache and dyld loaded at same address as when cache built
1337 // 2) first read __dyld value, if already correct don't write, this prevents dirtying a page
1338 if ( !fInSharedCache || !context.dyldLoadedAtSameAddressNeededBySharedCache ) {
1339 cmd = cmds;
1340 for (uint32_t i = 0; i < cmd_count; ++i) {
1341 if ( cmd->cmd == LC_SEGMENT_COMMAND ) {
1342 const struct macho_segment_command* seg = (struct macho_segment_command*)cmd;
1343 if ( strcmp(seg->segname, "__DATA") == 0 ) {
1344 const struct macho_section* const sectionsStart = (struct macho_section*)((char*)seg + sizeof(struct macho_segment_command));
1345 const struct macho_section* const sectionsEnd = &sectionsStart[seg->nsects];
1346 for (const struct macho_section* sect=sectionsStart; sect < sectionsEnd; ++sect) {
1347 if ( strcmp(sect->sectname, "__dyld" ) == 0 ) {
1348 struct DATAdyld* dd = (struct DATAdyld*)(sect->addr + fSlide);
1349 if ( sect->size > offsetof(DATAdyld, dyldLazyBinder) ) {
1350 if ( dd->dyldLazyBinder != (void*)&stub_binding_helper )
1351 dd->dyldLazyBinder = (void*)&stub_binding_helper;
1352 }
1353 if ( sect->size > offsetof(DATAdyld, dyldFuncLookup) ) {
1354 if ( dd->dyldFuncLookup != (void*)&dyld_func_lookup )
1355 dd->dyldFuncLookup = (void*)&dyld_func_lookup;
1356 }
1357 if ( mh->filetype == MH_EXECUTE ) {
1358 // there are two ways to get the program variables
1359 if ( (sect->size > offsetof(DATAdyld, vars)) && (dd->vars.mh == mh) ) {
1360 // some really old binaries have space for vars, but it is zero filled
1361 // main executable has 10.5 style __dyld section that has program variable pointers
1362 context.setNewProgramVars(dd->vars);
1363 }
1364 else {
1365 // main executable is pre-10.5 and requires the symbols names to be looked up
1366 this->lookupProgramVars(context);
1367 #if SUPPORT_OLD_CRT_INITIALIZATION
1368 // If the first 16 bytes of the entry point's instructions do not
1369 // match what crt1.o supplies, then the program has a custom entry point.
1370 // This means it might be doing something that needs to be executed before
1371 // initializers are run.
1372 if ( memcmp(this->getMain(), sStandardEntryPointInstructions, 16) != 0 ) {
1373 if ( context.verboseInit )
1374 dyld::log("dyld: program uses non-standard entry point so delaying running of initializers\n");
1375 context.setRunInitialzersOldWay();
1376 }
1377 #endif
1378 }
1379 }
1380 }
1381 else if ( (strcmp(sect->sectname, "__program_vars" ) == 0) && (mh->filetype == MH_EXECUTE) ) {
1382 // this is a Mac OS X 10.6 or later main executable
1383 struct ProgramVars* pv = (struct ProgramVars*)(sect->addr + fSlide);
1384 context.setNewProgramVars(*pv);
1385 }
1386 }
1387 }
1388 }
1389 cmd = (const struct load_command*)(((char*)cmd)+cmd->cmdsize);
1390 }
1391 }
1392 }
1393
1394
1395 void ImageLoaderMachO::lookupProgramVars(const LinkContext& context) const
1396 {
1397 ProgramVars vars = context.programVars;
1398 const ImageLoader::Symbol* sym;
1399
1400 // get mach header directly
1401 vars.mh = (macho_header*)fMachOData;
1402
1403 // lookup _NXArgc
1404 sym = this->findExportedSymbol("_NXArgc", false, NULL);
1405 if ( sym != NULL )
1406 vars.NXArgcPtr = (int*)this->getExportedSymbolAddress(sym, context, this, false);
1407
1408 // lookup _NXArgv
1409 sym = this->findExportedSymbol("_NXArgv", false, NULL);
1410 if ( sym != NULL )
1411 vars.NXArgvPtr = (const char***)this->getExportedSymbolAddress(sym, context, this, false);
1412
1413 // lookup _environ
1414 sym = this->findExportedSymbol("_environ", false, NULL);
1415 if ( sym != NULL )
1416 vars.environPtr = (const char***)this->getExportedSymbolAddress(sym, context, this, false);
1417
1418 // lookup __progname
1419 sym = this->findExportedSymbol("___progname", false, NULL);
1420 if ( sym != NULL )
1421 vars.__prognamePtr = (const char**)this->getExportedSymbolAddress(sym, context, this, false);
1422
1423 context.setNewProgramVars(vars);
1424 }
1425
1426
1427 bool ImageLoaderMachO::usablePrebinding(const LinkContext& context) const
1428 {
1429 // if prebound and loaded at prebound address, and all libraries are same as when this was prebound, then no need to bind
1430 if ( ((this->isPrebindable() && (this->getSlide() == 0)) || fInSharedCache)
1431 && this->usesTwoLevelNameSpace()
1432 && this->allDependentLibrariesAsWhenPreBound() ) {
1433 // allow environment variables to disable prebinding
1434 if ( context.bindFlat )
1435 return false;
1436 switch ( context.prebindUsage ) {
1437 case kUseAllPrebinding:
1438 return true;
1439 case kUseSplitSegPrebinding:
1440 return this->fIsSplitSeg;
1441 case kUseAllButAppPredbinding:
1442 return (this != context.mainExecutable);
1443 case kUseNoPrebinding:
1444 return false;
1445 }
1446 }
1447 return false;
1448 }
1449
1450
1451 void ImageLoaderMachO::doImageInit(const LinkContext& context)
1452 {
1453 if ( fHasDashInit ) {
1454 #if __IPHONE_OS_VERSION_MIN_REQUIRED
1455 // <rdar://problem/8543820> verify initializers are in first segment for dylibs
1456 if ( this->isDylib() && !fGoodFirstSegment ) {
1457 if ( context.verboseInit )
1458 dyld::log("dyld: ignoring -init in %s\n", this->getPath());
1459 return;
1460 }
1461 uintptr_t textStart = this->segActualLoadAddress(0);
1462 uintptr_t textEnd = this->segActualEndAddress(0);
1463 #endif
1464 const uint32_t cmd_count = ((macho_header*)fMachOData)->ncmds;
1465 const struct load_command* const cmds = (struct load_command*)&fMachOData[sizeof(macho_header)];
1466 const struct load_command* cmd = cmds;
1467 for (uint32_t i = 0; i < cmd_count; ++i) {
1468 switch (cmd->cmd) {
1469 case LC_ROUTINES_COMMAND:
1470 Initializer func = (Initializer)(((struct macho_routines_command*)cmd)->init_address + fSlide);
1471 #if __IPHONE_OS_VERSION_MIN_REQUIRED
1472 // <rdar://problem/8543820> verify initializers are in first segment for dylibs
1473 if ( this->isDylib() && (((uintptr_t)func >= textEnd) || ((uintptr_t)func < textStart)) ) {
1474 if ( context.verboseInit )
1475 dyld::log("dyld: ignoring out of bounds initializer function %p in %s\n", func, this->getPath());
1476 }
1477 else {
1478 #endif
1479 if ( context.verboseInit )
1480 dyld::log("dyld: calling -init function 0x%p in %s\n", func, this->getPath());
1481 func(context.argc, context.argv, context.envp, context.apple, &context.programVars);
1482 #if __IPHONE_OS_VERSION_MIN_REQUIRED
1483 }
1484 #endif
1485 break;
1486 }
1487 cmd = (const struct load_command*)(((char*)cmd)+cmd->cmdsize);
1488 }
1489 }
1490 }
1491
1492 void ImageLoaderMachO::doModInitFunctions(const LinkContext& context)
1493 {
1494 if ( fHasInitializers ) {
1495 #if __IPHONE_OS_VERSION_MIN_REQUIRED
1496 // <rdar://problem/8543820> verify initializers are in first segment for dylibs
1497 if ( this->isDylib() && !fGoodFirstSegment ) {
1498 if ( context.verboseInit )
1499 dyld::log("dyld: ignoring all initializers in %s\n", this->getPath());
1500 return;
1501 }
1502 uintptr_t textStart = this->segActualLoadAddress(0);
1503 uintptr_t textEnd = this->segActualEndAddress(0);
1504 #endif
1505 const uint32_t cmd_count = ((macho_header*)fMachOData)->ncmds;
1506 const struct load_command* const cmds = (struct load_command*)&fMachOData[sizeof(macho_header)];
1507 const struct load_command* cmd = cmds;
1508 for (uint32_t i = 0; i < cmd_count; ++i) {
1509 if ( cmd->cmd == LC_SEGMENT_COMMAND ) {
1510 const struct macho_segment_command* seg = (struct macho_segment_command*)cmd;
1511 const struct macho_section* const sectionsStart = (struct macho_section*)((char*)seg + sizeof(struct macho_segment_command));
1512 const struct macho_section* const sectionsEnd = &sectionsStart[seg->nsects];
1513 for (const struct macho_section* sect=sectionsStart; sect < sectionsEnd; ++sect) {
1514 const uint8_t type = sect->flags & SECTION_TYPE;
1515 if ( type == S_MOD_INIT_FUNC_POINTERS ) {
1516 Initializer* inits = (Initializer*)(sect->addr + fSlide);
1517 const uint32_t count = sect->size / sizeof(uintptr_t);
1518 for (uint32_t i=0; i < count; ++i) {
1519 Initializer func = inits[i];
1520 #if __IPHONE_OS_VERSION_MIN_REQUIRED
1521 // <rdar://problem/8543820> verify initializers are in first segment for dylibs
1522 if ( this->isDylib() && (((uintptr_t)func >= textEnd) || ((uintptr_t)func < textStart)) ) {
1523 if ( context.verboseInit )
1524 dyld::log("dyld: ignoring out of bounds initializer function %p in %s\n", func, this->getPath());
1525 }
1526 else {
1527 #endif
1528 if ( context.verboseInit )
1529 dyld::log("dyld: calling initializer function %p in %s\n", func, this->getPath());
1530 func(context.argc, context.argv, context.envp, context.apple, &context.programVars);
1531 #if __IPHONE_OS_VERSION_MIN_REQUIRED
1532 }
1533 #endif
1534 }
1535 }
1536 }
1537 }
1538 cmd = (const struct load_command*)(((char*)cmd)+cmd->cmdsize);
1539 }
1540 }
1541 }
1542
1543
1544
1545
1546
1547
1548 void ImageLoaderMachO::doGetDOFSections(const LinkContext& context, std::vector<ImageLoader::DOFInfo>& dofs)
1549 {
1550 if ( fHasDOFSections ) {
1551 // walk load commands (mapped in at start of __TEXT segment)
1552 const uint32_t cmd_count = ((macho_header*)fMachOData)->ncmds;
1553 const struct load_command* const cmds = (struct load_command*)&fMachOData[sizeof(macho_header)];
1554 const struct load_command* cmd = cmds;
1555 for (uint32_t i = 0; i < cmd_count; ++i) {
1556 switch (cmd->cmd) {
1557 case LC_SEGMENT_COMMAND:
1558 {
1559 const struct macho_segment_command* seg = (struct macho_segment_command*)cmd;
1560 const struct macho_section* const sectionsStart = (struct macho_section*)((char*)seg + sizeof(struct macho_segment_command));
1561 const struct macho_section* const sectionsEnd = &sectionsStart[seg->nsects];
1562 for (const struct macho_section* sect=sectionsStart; sect < sectionsEnd; ++sect) {
1563 if ( (sect->flags & SECTION_TYPE) == S_DTRACE_DOF ) {
1564 ImageLoader::DOFInfo info;
1565 info.dof = (void*)(sect->addr + fSlide);
1566 info.imageHeader = this->machHeader();
1567 info.imageShortName = this->getShortName();
1568 dofs.push_back(info);
1569 }
1570 }
1571 }
1572 break;
1573 }
1574 cmd = (const struct load_command*)(((char*)cmd)+cmd->cmdsize);
1575 }
1576 }
1577 }
1578
1579
1580 bool ImageLoaderMachO::doInitialization(const LinkContext& context)
1581 {
1582 CRSetCrashLogMessage2(this->getPath());
1583
1584 // mach-o has -init and static initializers
1585 doImageInit(context);
1586 doModInitFunctions(context);
1587
1588 CRSetCrashLogMessage2(NULL);
1589
1590 return (fHasDashInit || fHasInitializers);
1591 }
1592
1593 bool ImageLoaderMachO::needsInitialization()
1594 {
1595 return ( fHasDashInit || fHasInitializers );
1596 }
1597
1598
1599 bool ImageLoaderMachO::needsTermination()
1600 {
1601 return fHasTerminators;
1602 }
1603
1604
1605 void ImageLoaderMachO::doTermination(const LinkContext& context)
1606 {
1607 if ( fHasTerminators ) {
1608 const uint32_t cmd_count = ((macho_header*)fMachOData)->ncmds;
1609 const struct load_command* const cmds = (struct load_command*)&fMachOData[sizeof(macho_header)];
1610 const struct load_command* cmd = cmds;
1611 for (uint32_t i = 0; i < cmd_count; ++i) {
1612 if ( cmd->cmd == LC_SEGMENT_COMMAND ) {
1613 const struct macho_segment_command* seg = (struct macho_segment_command*)cmd;
1614 const struct macho_section* const sectionsStart = (struct macho_section*)((char*)seg + sizeof(struct macho_segment_command));
1615 const struct macho_section* const sectionsEnd = &sectionsStart[seg->nsects];
1616 for (const struct macho_section* sect=sectionsStart; sect < sectionsEnd; ++sect) {
1617 const uint8_t type = sect->flags & SECTION_TYPE;
1618 if ( type == S_MOD_TERM_FUNC_POINTERS ) {
1619 Terminator* terms = (Terminator*)(sect->addr + fSlide);
1620 const uint32_t count = sect->size / sizeof(uintptr_t);
1621 for (uint32_t i=count; i > 0; --i) {
1622 Terminator func = terms[i-1];
1623 if ( context.verboseInit )
1624 dyld::log("dyld: calling termination function %p in %s\n", func, this->getPath());
1625 func();
1626 }
1627 }
1628 }
1629 }
1630 cmd = (const struct load_command*)(((char*)cmd)+cmd->cmdsize);
1631 }
1632 }
1633 }
1634
1635
1636 void ImageLoaderMachO::printStatistics(unsigned int imageCount, const InitializerTimingList& timingInfo)
1637 {
1638 ImageLoader::printStatistics(imageCount, timingInfo);
1639 dyld::log("total symbol trie searches: %d\n", fgSymbolTrieSearchs);
1640 dyld::log("total symbol table binary searches: %d\n", fgSymbolTableBinarySearchs);
1641 dyld::log("total images defining weak symbols: %u\n", fgImagesHasWeakDefinitions);
1642 dyld::log("total images using weak symbols: %u\n", fgImagesRequiringCoalescing);
1643 }
1644
1645
1646 intptr_t ImageLoaderMachO::assignSegmentAddresses(const LinkContext& context)
1647 {
1648 // preflight and calculate slide if needed
1649 const bool inPIE = (fgNextPIEDylibAddress != 0);
1650 intptr_t slide = 0;
1651 if ( this->segmentsCanSlide() && this->segmentsMustSlideTogether() ) {
1652 bool needsToSlide = false;
1653 bool imageHasPreferredLoadAddress = segHasPreferredLoadAddress(0);
1654 uintptr_t lowAddr = (unsigned long)(-1);
1655 uintptr_t highAddr = 0;
1656 for(unsigned int i=0, e=segmentCount(); i < e; ++i) {
1657 const uintptr_t segLow = segPreferredLoadAddress(i);
1658 const uintptr_t segHigh = (segLow + segSize(i) + 4095) & -4096;
1659 if ( segLow < lowAddr )
1660 lowAddr = segLow;
1661 if ( segHigh > highAddr )
1662 highAddr = segHigh;
1663
1664 if ( needsToSlide || !imageHasPreferredLoadAddress || inPIE || !reserveAddressRange(segPreferredLoadAddress(i), segSize(i)) )
1665 needsToSlide = true;
1666 }
1667 if ( needsToSlide ) {
1668 // find a chunk of address space to hold all segments
1669 uintptr_t addr = reserveAnAddressRange(highAddr-lowAddr, context);
1670 slide = addr - lowAddr;
1671 }
1672 }
1673 else if ( ! this->segmentsCanSlide() ) {
1674 for(unsigned int i=0, e=segmentCount(); i < e; ++i) {
1675 if ( strcmp(segName(i), "__PAGEZERO") == 0 )
1676 continue;
1677 if ( !reserveAddressRange(segPreferredLoadAddress(i), segSize(i)) )
1678 dyld::throwf("can't map unslidable segment %s to 0x%lX with size 0x%lX", segName(i), segPreferredLoadAddress(i), segSize(i));
1679 }
1680 }
1681 else {
1682 throw "mach-o does not support independently sliding segments";
1683 }
1684 return slide;
1685 }
1686
1687
1688 uintptr_t ImageLoaderMachO::reserveAnAddressRange(size_t length, const ImageLoader::LinkContext& context)
1689 {
1690 vm_address_t addr = 0;
1691 vm_size_t size = length;
1692 // in PIE programs, load initial dylibs after main executable so they don't have fixed addresses either
1693 if ( fgNextPIEDylibAddress != 0 ) {
1694 // add small (0-3 pages) random padding between dylibs
1695 addr = fgNextPIEDylibAddress + (__stack_chk_guard/fgNextPIEDylibAddress & (sizeof(long)-1))*4096;
1696 //dyld::log("padding 0x%08llX, guard=0x%08llX\n", (long long)(addr - fgNextPIEDylibAddress), (long long)(__stack_chk_guard));
1697 kern_return_t r = vm_allocate(mach_task_self(), &addr, size, VM_FLAGS_FIXED);
1698 if ( r == KERN_SUCCESS ) {
1699 fgNextPIEDylibAddress = addr + size;
1700 return addr;
1701 }
1702 fgNextPIEDylibAddress = 0;
1703 }
1704 kern_return_t r = vm_allocate(mach_task_self(), &addr, size, VM_FLAGS_ANYWHERE);
1705 if ( r != KERN_SUCCESS )
1706 throw "out of address space";
1707
1708 return addr;
1709 }
1710
1711 bool ImageLoaderMachO::reserveAddressRange(uintptr_t start, size_t length)
1712 {
1713 vm_address_t addr = start;
1714 vm_size_t size = length;
1715 kern_return_t r = vm_allocate(mach_task_self(), &addr, size, false /*only this range*/);
1716 if ( r != KERN_SUCCESS )
1717 return false;
1718 return true;
1719 }
1720
1721
1722
1723 void ImageLoaderMachO::mapSegments(int fd, uint64_t offsetInFat, uint64_t lenInFat, uint64_t fileLen, const LinkContext& context)
1724 {
1725 // find address range for image
1726 intptr_t slide = this->assignSegmentAddresses(context);
1727 if ( context.verboseMapping )
1728 dyld::log("dyld: Mapping %s\n", this->getPath());
1729 // <rdar://problem/8268602> verify that the first segment load command is for a r-x segment
1730 // that starts at begining of file and is larger than all load commands
1731 uintptr_t firstSegMappedStart = segPreferredLoadAddress(0) + slide;
1732 uintptr_t firstSegMappedEnd = firstSegMappedStart + this->segSize(0);
1733 if ( (this->segLoadCommand(0)->initprot == (VM_PROT_EXECUTE|VM_PROT_READ))
1734 && (this->segFileOffset(0) == 0)
1735 && (this->segFileSize(0) != 0)
1736 && (this->segSize(0) > ((macho_header*)fMachOData)->sizeofcmds) ) {
1737 fGoodFirstSegment = true;
1738 }
1739 // map in all segments
1740 for(unsigned int i=0, e=segmentCount(); i < e; ++i) {
1741 vm_offset_t fileOffset = segFileOffset(i) + offsetInFat;
1742 vm_size_t size = segFileSize(i);
1743 uintptr_t requestedLoadAddress = segPreferredLoadAddress(i) + slide;
1744 // <rdar://problem/8268602> verify other segments map after first
1745 if ( (i != 0) && (requestedLoadAddress < firstSegMappedEnd) )
1746 fGoodFirstSegment = false;
1747 int protection = 0;
1748 if ( !segUnaccessible(i) ) {
1749 // If has text-relocs, don't set x-bit initially.
1750 // Instead set it later after text-relocs have been done.
1751 // The iPhone OS does not like it when you make executable code writable.
1752 if ( segExecutable(i) && !(segHasRebaseFixUps(i) && (slide != 0)) )
1753 protection |= PROT_EXEC;
1754 if ( segReadable(i) )
1755 protection |= PROT_READ;
1756 if ( segWriteable(i) )
1757 protection |= PROT_WRITE;
1758 }
1759 #if __i386__
1760 // initially map __IMPORT segments R/W so dyld can update them
1761 if ( segIsReadOnlyImport(i) )
1762 protection |= PROT_WRITE;
1763 #endif
1764 // wholly zero-fill segments have nothing to mmap() in
1765 if ( size > 0 ) {
1766 if ( (fileOffset+size) > fileLen ) {
1767 dyld::throwf("truncated mach-o error: segment %s extends to %llu which is past end of file %llu",
1768 segName(i), (uint64_t)(fileOffset+size), fileLen);
1769 }
1770 void* loadAddress = mmap((void*)requestedLoadAddress, size, protection, MAP_FIXED | MAP_PRIVATE, fd, fileOffset);
1771 if ( loadAddress == ((void*)(-1)) ) {
1772 dyld::throwf("mmap() error %d at address=0x%08lX, size=0x%08lX segment=%s in Segment::map() mapping %s",
1773 errno, requestedLoadAddress, (uintptr_t)size, segName(i), getPath());
1774 }
1775 }
1776 // update stats
1777 ++ImageLoader::fgTotalSegmentsMapped;
1778 ImageLoader::fgTotalBytesMapped += size;
1779 if ( context.verboseMapping )
1780 dyld::log("%18s at 0x%08lX->0x%08lX with permissions %c%c%c\n", segName(i), requestedLoadAddress, requestedLoadAddress+size-1,
1781 (protection & PROT_READ) ? 'r' : '.', (protection & PROT_WRITE) ? 'w' : '.', (protection & PROT_EXEC) ? 'x' : '.' );
1782 }
1783
1784 // update slide to reflect load location
1785 this->setSlide(slide);
1786 }
1787
1788 void ImageLoaderMachO::mapSegments(const void* memoryImage, uint64_t imageLen, const LinkContext& context)
1789 {
1790 // find address range for image
1791 intptr_t slide = this->assignSegmentAddresses(context);
1792 if ( context.verboseMapping )
1793 dyld::log("dyld: Mapping memory %p\n", memoryImage);
1794 // map in all segments
1795 for(unsigned int i=0, e=segmentCount(); i < e; ++i) {
1796 vm_address_t loadAddress = segPreferredLoadAddress(i) + slide;
1797 vm_address_t srcAddr = (uintptr_t)memoryImage + segFileOffset(i);
1798 vm_size_t size = segFileSize(i);
1799 kern_return_t r = vm_copy(mach_task_self(), srcAddr, size, loadAddress);
1800 if ( r != KERN_SUCCESS )
1801 throw "can't map segment";
1802 if ( context.verboseMapping )
1803 dyld::log("%18s at 0x%08lX->0x%08lX\n", segName(i), (uintptr_t)loadAddress, (uintptr_t)loadAddress+size-1);
1804 }
1805 // update slide to reflect load location
1806 this->setSlide(slide);
1807 // set R/W permissions on all segments at slide location
1808 for(unsigned int i=0, e=segmentCount(); i < e; ++i) {
1809 segProtect(i, context);
1810 }
1811 }
1812
1813
1814 void ImageLoaderMachO::segProtect(unsigned int segIndex, const ImageLoader::LinkContext& context)
1815 {
1816 vm_prot_t protection = 0;
1817 if ( !segUnaccessible(segIndex) ) {
1818 if ( segExecutable(segIndex) )
1819 protection |= PROT_EXEC;
1820 if ( segReadable(segIndex) )
1821 protection |= PROT_READ;
1822 if ( segWriteable(segIndex) )
1823 protection |= PROT_WRITE;
1824 }
1825 vm_address_t addr = segActualLoadAddress(segIndex);
1826 vm_size_t size = segSize(segIndex);
1827 const bool setCurrentPermissions = false;
1828 kern_return_t r = vm_protect(mach_task_self(), addr, size, setCurrentPermissions, protection);
1829 if ( r != KERN_SUCCESS ) {
1830 dyld::throwf("vm_protect(0x%08llX, 0x%08llX, false, 0x%02X) failed, result=%d for segment %s in %s",
1831 (long long)addr, (long long)size, protection, r, segName(segIndex), this->getPath());
1832 }
1833 if ( context.verboseMapping ) {
1834 dyld::log("%18s at 0x%08lX->0x%08lX altered permissions to %c%c%c\n", segName(segIndex), (uintptr_t)addr, (uintptr_t)addr+size-1,
1835 (protection & PROT_READ) ? 'r' : '.', (protection & PROT_WRITE) ? 'w' : '.', (protection & PROT_EXEC) ? 'x' : '.' );
1836 }
1837 }
1838
1839 void ImageLoaderMachO::segMakeWritable(unsigned int segIndex, const ImageLoader::LinkContext& context)
1840 {
1841 vm_address_t addr = segActualLoadAddress(segIndex);
1842 vm_size_t size = segSize(segIndex);
1843 const bool setCurrentPermissions = false;
1844 vm_prot_t protection = VM_PROT_WRITE | VM_PROT_READ;
1845 if ( segExecutable(segIndex) && !segHasRebaseFixUps(segIndex) )
1846 protection |= VM_PROT_EXECUTE;
1847 kern_return_t r = vm_protect(mach_task_self(), addr, size, setCurrentPermissions, protection);
1848 if ( r != KERN_SUCCESS ) {
1849 dyld::throwf("vm_protect(0x%08llX, 0x%08llX, false, 0x%02X) failed, result=%d for segment %s in %s",
1850 (long long)addr, (long long)size, protection, r, segName(segIndex), this->getPath());
1851 }
1852 if ( context.verboseMapping ) {
1853 dyld::log("%18s at 0x%08lX->0x%08lX altered permissions to %c%c%c\n", segName(segIndex), (uintptr_t)addr, (uintptr_t)addr+size-1,
1854 (protection & PROT_READ) ? 'r' : '.', (protection & PROT_WRITE) ? 'w' : '.', (protection & PROT_EXEC) ? 'x' : '.' );
1855 }
1856 }
1857
1858