1 /* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*-
3 * Copyright (c) 2004-2005 Apple Computer, Inc. All rights reserved.
5 * @APPLE_LICENSE_HEADER_START@
7 * This file contains Original Code and/or Modifications of Original Code
8 * as defined in and that are subject to the Apple Public Source License
9 * Version 2.0 (the 'License'). You may not use this file except in
10 * compliance with the License. Please obtain a copy of the License at
11 * http://www.opensource.apple.com/apsl/ and read it before using this
14 * The Original Code and all software distributed under the License are
15 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
19 * Please see the License for the specific language governing rights and
20 * limitations under the License.
22 * @APPLE_LICENSE_HEADER_END@
25 #define __STDC_LIMIT_MACROS
30 #include <mach/mach.h>
31 #include <mach-o/loader.h>
32 #include <mach-o/ldsyms.h>
33 #include <mach-o/reloc.h>
34 #if __ppc__ || __ppc64__
35 #include <mach-o/ppc/reloc.h>
38 #include <mach-o/x86_64/reloc.h>
43 #define MH_PIE 0x200000
48 #define macho_header mach_header_64
49 #define LC_SEGMENT_COMMAND LC_SEGMENT_64
50 #define macho_segment_command segment_command_64
51 #define macho_section section_64
54 #define macho_header mach_header
55 #define LC_SEGMENT_COMMAND LC_SEGMENT
56 #define macho_segment_command segment_command
57 #define macho_section section
62 #define POINTER_RELOC X86_64_RELOC_UNSIGNED
64 #define POINTER_RELOC GENERIC_RELOC_VANILLA
68 namespace dyld
{ extern bool isRosetta(); };
72 // Code to bootstrap dyld into a runnable state
76 namespace dyldbootstrap
{
79 typedef void (*Initializer
)(int argc
, const char* argv
[], const char* envp
[], const char* apple
[]);
82 // For a regular executable, the crt code calls dyld to run the executables initializers.
83 // For a static executable, crt directly runs the initializers.
84 // dyld (should be static) but is a dynamic executable and needs this hack to run its own initializers.
85 // We pass argc, argv, etc in case libc.a uses those arguments
87 static void runDyldInitializers(const struct macho_header
* mh
, intptr_t slide
, int argc
, const char* argv
[], const char* envp
[], const char* apple
[])
89 const uint32_t cmd_count
= mh
->ncmds
;
90 const struct load_command
* const cmds
= (struct load_command
*)(((char*)mh
)+sizeof(macho_header
));
91 const struct load_command
* cmd
= cmds
;
92 for (uint32_t i
= 0; i
< cmd_count
; ++i
) {
94 case LC_SEGMENT_COMMAND
:
96 const struct macho_segment_command
* seg
= (struct macho_segment_command
*)cmd
;
97 const struct macho_section
* const sectionsStart
= (struct macho_section
*)((char*)seg
+ sizeof(struct macho_segment_command
));
98 const struct macho_section
* const sectionsEnd
= §ionsStart
[seg
->nsects
];
99 for (const struct macho_section
* sect
=sectionsStart
; sect
< sectionsEnd
; ++sect
) {
100 const uint8_t type
= sect
->flags
& SECTION_TYPE
;
101 if ( type
== S_MOD_INIT_FUNC_POINTERS
){
102 Initializer
* inits
= (Initializer
*)(sect
->addr
+ slide
);
103 const uint32_t count
= sect
->size
/ sizeof(uintptr_t);
104 for (uint32_t i
=0; i
< count
; ++i
) {
105 Initializer func
= inits
[i
];
106 func(argc
, argv
, envp
, apple
);
113 cmd
= (const struct load_command
*)(((char*)cmd
)+cmd
->cmdsize
);
118 // If the kernel does not load dyld at its preferred address, we need to apply
119 // fixups to various initialized parts of the __DATA segment
121 static void rebaseDyld(const struct macho_header
* mh
, intptr_t slide
)
123 // rebase non-lazy pointers (which all point internal to dyld, since dyld uses no shared libraries)
124 // and get interesting pointers into dyld
125 const uint32_t cmd_count
= mh
->ncmds
;
126 const struct load_command
* const cmds
= (struct load_command
*)(((char*)mh
)+sizeof(macho_header
));
127 const struct load_command
* cmd
= cmds
;
128 const struct macho_segment_command
* linkEditSeg
= NULL
;
130 const struct macho_segment_command
* firstWritableSeg
= NULL
;
132 const struct dysymtab_command
* dynamicSymbolTable
= NULL
;
133 for (uint32_t i
= 0; i
< cmd_count
; ++i
) {
135 case LC_SEGMENT_COMMAND
:
137 const struct macho_segment_command
* seg
= (struct macho_segment_command
*)cmd
;
138 if ( strcmp(seg
->segname
, "__LINKEDIT") == 0 )
140 const struct macho_section
* const sectionsStart
= (struct macho_section
*)((char*)seg
+ sizeof(struct macho_segment_command
));
141 const struct macho_section
* const sectionsEnd
= §ionsStart
[seg
->nsects
];
142 for (const struct macho_section
* sect
=sectionsStart
; sect
< sectionsEnd
; ++sect
) {
143 const uint8_t type
= sect
->flags
& SECTION_TYPE
;
144 if ( type
== S_NON_LAZY_SYMBOL_POINTERS
) {
145 // rebase non-lazy pointers (which all point internal to dyld, since dyld uses no shared libraries)
146 const uint32_t pointerCount
= sect
->size
/ sizeof(uintptr_t);
147 uintptr_t* const symbolPointers
= (uintptr_t*)(sect
->addr
+ slide
);
148 for (uint32_t j
=0; j
< pointerCount
; ++j
) {
149 symbolPointers
[j
] += slide
;
154 if ( (firstWritableSeg
== NULL
) && (seg
->initprot
& VM_PROT_WRITE
) )
155 firstWritableSeg
= seg
;
160 dynamicSymbolTable
= (struct dysymtab_command
*)cmd
;
163 cmd
= (const struct load_command
*)(((char*)cmd
)+cmd
->cmdsize
);
166 // use reloc's to rebase all random data pointers
168 const uintptr_t relocBase
= firstWritableSeg
->vmaddr
+ slide
;
170 const uintptr_t relocBase
= (uintptr_t)mh
;
172 const relocation_info
* const relocsStart
= (struct relocation_info
*)(linkEditSeg
->vmaddr
+ slide
+ dynamicSymbolTable
->locreloff
- linkEditSeg
->fileoff
);
173 const relocation_info
* const relocsEnd
= &relocsStart
[dynamicSymbolTable
->nlocrel
];
174 for (const relocation_info
* reloc
=relocsStart
; reloc
< relocsEnd
; ++reloc
) {
175 #if __ppc__ || __ppc64__ || __i36__
176 if ( (reloc
->r_address
& R_SCATTERED
) != 0 )
177 throw "scattered relocation in dyld";
179 if ( reloc
->r_length
!= RELOC_SIZE
)
180 throw "relocation in dyld has wrong size";
182 if ( reloc
->r_type
!= POINTER_RELOC
)
183 throw "relocation in dyld has wrong type";
185 // update pointer by amount dyld slid
186 *((uintptr_t*)(reloc
->r_address
+ relocBase
)) += slide
;
192 // For some reason the kernel loads dyld with __TEXT and __LINKEDIT writable
193 // rdar://problem/3702311
195 static void segmentProtectDyld(const struct macho_header
* mh
, intptr_t slide
)
197 const uint32_t cmd_count
= mh
->ncmds
;
198 const struct load_command
* const cmds
= (struct load_command
*)(((char*)mh
)+sizeof(macho_header
));
199 const struct load_command
* cmd
= cmds
;
200 for (uint32_t i
= 0; i
< cmd_count
; ++i
) {
202 case LC_SEGMENT_COMMAND
:
204 const struct macho_segment_command
* seg
= (struct macho_segment_command
*)cmd
;
205 vm_address_t addr
= seg
->vmaddr
+ slide
;
206 vm_size_t size
= seg
->vmsize
;
207 const bool setCurrentPermissions
= false;
208 vm_protect(mach_task_self(), addr
, size
, setCurrentPermissions
, seg
->initprot
);
209 //dyld::log("dyld: segment %s, 0x%08X -> 0x%08X, set to %d\n", seg->segname, addr, addr+size-1, seg->initprot);
213 cmd
= (const struct load_command
*)(((char*)cmd
)+cmd
->cmdsize
);
220 // re-map the main executable to a new random address
222 static const struct mach_header
* randomizeExecutableLoadAddress(const struct mach_header
* orgMH
, uintptr_t* appsSlide
)
225 // don't slide PIE programs running under rosetta
226 if ( dyld::isRosetta() )
230 uint32_t segCount
= 0;
231 const uint32_t cmd_count
= orgMH
->ncmds
;
232 const struct load_command
* const cmds
= (struct load_command
*)(((char*)orgMH
)+sizeof(macho_header
));
233 const struct load_command
* cmd
= cmds
;
234 for (uint32_t i
= 0; i
< cmd_count
; ++i
) {
235 if ( cmd
->cmd
== LC_SEGMENT_COMMAND
) {
236 const struct macho_segment_command
* segCmd
= (struct macho_segment_command
*)cmd
;
237 // page-zero and custom stacks don't move
238 if ( (strcmp(segCmd
->segname
, "__PAGEZERO") != 0) && (strcmp(segCmd
->segname
, "__UNIXSTACK") != 0) )
241 cmd
= (const struct load_command
*)(((char*)cmd
)+cmd
->cmdsize
);
244 // make copy of segment info
245 macho_segment_command segs
[segCount
];
247 uintptr_t highestAddressUsed
= 0;
248 uintptr_t lowestAddressUsed
= UINTPTR_MAX
;
250 for (uint32_t i
= 0; i
< cmd_count
; ++i
) {
251 if ( cmd
->cmd
== LC_SEGMENT_COMMAND
) {
252 const struct macho_segment_command
* segCmd
= (struct macho_segment_command
*)cmd
;
253 if ( (strcmp(segCmd
->segname
, "__PAGEZERO") != 0) && (strcmp(segCmd
->segname
, "__UNIXSTACK") != 0) ) {
254 segs
[index
++] = *segCmd
;
255 if ( (segCmd
->vmaddr
+ segCmd
->vmsize
) > highestAddressUsed
)
256 highestAddressUsed
= ((segCmd
->vmaddr
+ segCmd
->vmsize
) + 4095) & -4096;
257 if ( segCmd
->vmaddr
< lowestAddressUsed
)
258 lowestAddressUsed
= segCmd
->vmaddr
;
259 // do nothing if kernel has already randomized load address
260 if ( (strcmp(segCmd
->segname
, "__TEXT") == 0) && (segCmd
->vmaddr
!= (uintptr_t)orgMH
) )
264 cmd
= (const struct load_command
*)(((char*)cmd
)+cmd
->cmdsize
);
267 // choose a random new base address
269 uintptr_t highestAddressPossible
= highestAddressUsed
+ 0x100000000ULL
;
271 uintptr_t highestAddressPossible
= 0x80000000;
273 uintptr_t sizeNeeded
= highestAddressUsed
-lowestAddressUsed
;
274 if ( (highestAddressPossible
-sizeNeeded
) < highestAddressUsed
) {
275 // new and old segments will overlap
276 // need better algorithm for remapping
277 // punt and don't re-map
280 uintptr_t possibleRange
= (highestAddressPossible
-sizeNeeded
) - highestAddressUsed
;
281 uintptr_t newBaseAddress
= highestAddressUsed
+ ((arc4random() % possibleRange
) & -4096);
283 vm_address_t addr
= newBaseAddress
;
284 // reserve new address range
285 if ( vm_allocate(mach_task_self(), &addr
, sizeNeeded
, VM_FLAGS_FIXED
) == KERN_SUCCESS
) {
286 // copy each segment to new address
287 for (uint32_t i
= 0; i
< segCount
; ++i
) {
288 uintptr_t newSegAddress
= segs
[i
].vmaddr
- lowestAddressUsed
+ newBaseAddress
;
289 if ( (vm_copy(mach_task_self(), segs
[i
].vmaddr
, segs
[i
].vmsize
, newSegAddress
) != KERN_SUCCESS
)
290 || (vm_protect(mach_task_self(), newSegAddress
, segs
[i
].vmsize
, true, segs
[i
].maxprot
) != KERN_SUCCESS
)
291 || (vm_protect(mach_task_self(), newSegAddress
, segs
[i
].vmsize
, false, segs
[i
].initprot
) != KERN_SUCCESS
) ) {
292 // can't copy so dealloc new region and run with original base address
293 vm_deallocate(mach_task_self(), newBaseAddress
, sizeNeeded
);
294 dyld::warn("could not relocate position independent exectable\n");
298 // unmap original segments
299 vm_deallocate(mach_task_self(), lowestAddressUsed
, highestAddressUsed
-lowestAddressUsed
);
301 // run with newly mapped executable
302 *appsSlide
= newBaseAddress
- lowestAddressUsed
;
303 return (const struct mach_header
*)newBaseAddress
;
306 // can't get new range, so don't slide to random address
311 extern "C" void dyld_exceptions_init(const struct macho_header
*, uintptr_t slide
); // in dyldExceptions.cpp
312 extern "C" void mach_init();
315 // _pthread_keys is partitioned in a lower part that dyld will use; libSystem
316 // will use the upper part. We set __pthread_tsd_first to 1 as the start of
317 // the lower part. Libc will take #1 and c++ exceptions will take #2. There
318 // is one free key=3 left.
321 extern int __pthread_tsd_first
;
322 extern void _pthread_keys_init();
327 // This is code to bootstrap dyld. This work in normally done for a program by dyld and crt.
328 // In dyld we have to do this manually.
330 uintptr_t start(const struct mach_header
* appsMachHeader
, int argc
, const char* argv
[], intptr_t slide
)
332 // _mh_dylinker_header is magic symbol defined by static linker (ld), see <mach-o/ldsyms.h>
333 const struct macho_header
* dyldsMachHeader
= (const struct macho_header
*)(((char*)&_mh_dylinker_header
)+slide
);
335 // if kernel had to slide dyld, we need to fix up load sensitive locations
336 // we have to do this before using any global variables
338 rebaseDyld(dyldsMachHeader
, slide
);
341 uintptr_t appsSlide
= 0;
343 // set pthread keys to dyld range
344 __pthread_tsd_first
= 1;
345 _pthread_keys_init();
347 // enable C++ exceptions to work inside dyld
348 dyld_exceptions_init(dyldsMachHeader
, slide
);
350 // allow dyld to use mach messaging
353 // set protection on segments (has to be done after mach_init)
354 segmentProtectDyld(dyldsMachHeader
, slide
);
356 // kernel sets up env pointer to be just past end of agv array
357 const char** envp
= &argv
[argc
+1];
359 // kernel sets up apple pointer to be just past end of envp array
360 const char** apple
= envp
;
361 while(*apple
!= NULL
) { ++apple
; }
364 // run all C++ initializers inside dyld
365 runDyldInitializers(dyldsMachHeader
, slide
, argc
, argv
, envp
, apple
);
367 // if main executable was linked -pie, then randomize its load address
368 if ( appsMachHeader
->flags
& MH_PIE
)
369 appsMachHeader
= randomizeExecutableLoadAddress(appsMachHeader
, &appsSlide
);
371 // now that we are done bootstrapping dyld, call dyld's main
372 return dyld::_main(appsMachHeader
, appsSlide
, argc
, argv
, envp
, apple
);
378 } // end of namespace