1 /* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*-
3 * Copyright (c) 2004-2008 Apple 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 <Availability.h>
31 #include <mach/mach.h>
32 #include <mach-o/loader.h>
33 #include <mach-o/ldsyms.h>
34 #include <mach-o/reloc.h>
36 #include <mach-o/x86_64/reloc.h>
39 #include "dyldSyscallInterface.h"
42 extern void addImagesToAllImages(uint32_t infoCount
, const dyld_image_info info
[]);
43 extern void syncProcessInfo();
46 #define MH_PIE 0x200000
49 // currently dyld has no initializers, but if some come back, set this to non-zero
50 #define DYLD_INITIALIZER_SUPPORT 0
53 #define LC_SEGMENT_COMMAND LC_SEGMENT_64
54 #define macho_segment_command segment_command_64
55 #define macho_section section_64
58 #define LC_SEGMENT_COMMAND LC_SEGMENT
59 #define macho_segment_command segment_command
60 #define macho_section section
65 #define POINTER_RELOC X86_64_RELOC_UNSIGNED
67 #define POINTER_RELOC GENERIC_RELOC_VANILLA
71 #if TARGET_IPHONE_SIMULATOR
72 const dyld::SyscallHelpers
* gSyscallHelpers
= NULL
;
77 // Code to bootstrap dyld into a runnable state
81 namespace dyldbootstrap
{
85 #if DYLD_INITIALIZER_SUPPORT
87 typedef void (*Initializer
)(int argc
, const char* argv
[], const char* envp
[], const char* apple
[]);
89 extern const Initializer inits_start
__asm("section$start$__DATA$__mod_init_func");
90 extern const Initializer inits_end
__asm("section$end$__DATA$__mod_init_func");
93 // For a regular executable, the crt code calls dyld to run the executables initializers.
94 // For a static executable, crt directly runs the initializers.
95 // dyld (should be static) but is a dynamic executable and needs this hack to run its own initializers.
96 // We pass argc, argv, etc in case libc.a uses those arguments
98 static void runDyldInitializers(const struct macho_header
* mh
, intptr_t slide
, int argc
, const char* argv
[], const char* envp
[], const char* apple
[])
100 for (const Initializer
* p
= &inits_start
; p
< &inits_end
; ++p
) {
101 (*p
)(argc
, argv
, envp
, apple
);
104 #endif // DYLD_INITIALIZER_SUPPORT
108 // The kernel may have slid a Position Independent Executable
110 static uintptr_t slideOfMainExecutable(const struct macho_header
* mh
)
112 const uint32_t cmd_count
= mh
->ncmds
;
113 const struct load_command
* const cmds
= (struct load_command
*)(((char*)mh
)+sizeof(macho_header
));
114 const struct load_command
* cmd
= cmds
;
115 for (uint32_t i
= 0; i
< cmd_count
; ++i
) {
116 if ( cmd
->cmd
== LC_SEGMENT_COMMAND
) {
117 const struct macho_segment_command
* segCmd
= (struct macho_segment_command
*)cmd
;
118 if ( (segCmd
->fileoff
== 0) && (segCmd
->filesize
!= 0)) {
119 return (uintptr_t)mh
- segCmd
->vmaddr
;
122 cmd
= (const struct load_command
*)(((char*)cmd
)+cmd
->cmdsize
);
129 // If the kernel does not load dyld at its preferred address, we need to apply
130 // fixups to various initialized parts of the __DATA segment
132 static void rebaseDyld(const struct macho_header
* mh
, intptr_t slide
)
134 // rebase non-lazy pointers (which all point internal to dyld, since dyld uses no shared libraries)
135 // and get interesting pointers into dyld
136 const uint32_t cmd_count
= mh
->ncmds
;
137 const struct load_command
* const cmds
= (struct load_command
*)(((char*)mh
)+sizeof(macho_header
));
138 const struct load_command
* cmd
= cmds
;
139 const struct macho_segment_command
* linkEditSeg
= NULL
;
141 const struct macho_segment_command
* firstWritableSeg
= NULL
;
143 const struct dysymtab_command
* dynamicSymbolTable
= NULL
;
144 for (uint32_t i
= 0; i
< cmd_count
; ++i
) {
146 case LC_SEGMENT_COMMAND
:
148 const struct macho_segment_command
* seg
= (struct macho_segment_command
*)cmd
;
149 if ( strcmp(seg
->segname
, "__LINKEDIT") == 0 )
151 const struct macho_section
* const sectionsStart
= (struct macho_section
*)((char*)seg
+ sizeof(struct macho_segment_command
));
152 const struct macho_section
* const sectionsEnd
= §ionsStart
[seg
->nsects
];
153 for (const struct macho_section
* sect
=sectionsStart
; sect
< sectionsEnd
; ++sect
) {
154 const uint8_t type
= sect
->flags
& SECTION_TYPE
;
155 if ( type
== S_NON_LAZY_SYMBOL_POINTERS
) {
156 // rebase non-lazy pointers (which all point internal to dyld, since dyld uses no shared libraries)
157 const uint32_t pointerCount
= (uint32_t)(sect
->size
/ sizeof(uintptr_t));
158 uintptr_t* const symbolPointers
= (uintptr_t*)(sect
->addr
+ slide
);
159 for (uint32_t j
=0; j
< pointerCount
; ++j
) {
160 symbolPointers
[j
] += slide
;
165 if ( (firstWritableSeg
== NULL
) && (seg
->initprot
& VM_PROT_WRITE
) )
166 firstWritableSeg
= seg
;
171 dynamicSymbolTable
= (struct dysymtab_command
*)cmd
;
174 cmd
= (const struct load_command
*)(((char*)cmd
)+cmd
->cmdsize
);
177 // use reloc's to rebase all random data pointers
179 const uintptr_t relocBase
= firstWritableSeg
->vmaddr
+ slide
;
181 const uintptr_t relocBase
= (uintptr_t)mh
;
183 const relocation_info
* const relocsStart
= (struct relocation_info
*)(linkEditSeg
->vmaddr
+ slide
+ dynamicSymbolTable
->locreloff
- linkEditSeg
->fileoff
);
184 const relocation_info
* const relocsEnd
= &relocsStart
[dynamicSymbolTable
->nlocrel
];
185 for (const relocation_info
* reloc
=relocsStart
; reloc
< relocsEnd
; ++reloc
) {
186 if ( reloc
->r_length
!= RELOC_SIZE
)
187 throw "relocation in dyld has wrong size";
189 if ( reloc
->r_type
!= POINTER_RELOC
)
190 throw "relocation in dyld has wrong type";
192 // update pointer by amount dyld slid
193 *((uintptr_t*)(reloc
->r_address
+ relocBase
)) += slide
;
198 extern "C" void mach_init();
199 extern "C" void __guard_setup(const char* apple
[]);
203 // This is code to bootstrap dyld. This work in normally done for a program by dyld and crt.
204 // In dyld we have to do this manually.
206 uintptr_t start(const struct macho_header
* appsMachHeader
, int argc
, const char* argv
[],
207 intptr_t slide
, const struct macho_header
* dyldsMachHeader
,
208 uintptr_t* startGlue
)
210 // if kernel had to slide dyld, we need to fix up load sensitive locations
211 // we have to do this before using any global variables
213 rebaseDyld(dyldsMachHeader
, slide
);
216 // allow dyld to use mach messaging
219 // kernel sets up env pointer to be just past end of agv array
220 const char** envp
= &argv
[argc
+1];
222 // kernel sets up apple pointer to be just past end of envp array
223 const char** apple
= envp
;
224 while(*apple
!= NULL
) { ++apple
; }
227 // set up random value for stack canary
228 __guard_setup(apple
);
230 #if DYLD_INITIALIZER_SUPPORT
231 // run all C++ initializers inside dyld
232 runDyldInitializers(dyldsMachHeader
, slide
, argc
, argv
, envp
, apple
);
235 // now that we are done bootstrapping dyld, call dyld's main
236 uintptr_t appsSlide
= slideOfMainExecutable(appsMachHeader
);
237 return dyld::_main(appsMachHeader
, appsSlide
, argc
, argv
, envp
, apple
, startGlue
);
241 #if TARGET_IPHONE_SIMULATOR
243 extern "C" uintptr_t start_sim(int argc
, const char* argv
[], const char* envp
[], const char* apple
[],
244 const macho_header
* mainExecutableMH
, const macho_header
* dyldMH
, uintptr_t dyldSlide
,
245 const dyld::SyscallHelpers
*, uintptr_t* startGlue
);
248 uintptr_t start_sim(int argc
, const char* argv
[], const char* envp
[], const char* apple
[],
249 const macho_header
* mainExecutableMH
, const macho_header
* dyldMH
, uintptr_t dyldSlide
,
250 const dyld::SyscallHelpers
* sc
, uintptr_t* startGlue
)
252 // if simulator dyld loaded slid, it needs to rebase itself
253 // we have to do this before using any global variables
254 if ( dyldSlide
!= 0 ) {
255 rebaseDyld(dyldMH
, dyldSlide
);
258 // save table of syscall pointers
259 gSyscallHelpers
= sc
;
261 // allow dyld to use mach messaging
264 // set up random value for stack canary
265 __guard_setup(apple
);
267 // setup gProcessInfo to point to host dyld's struct
268 dyld::gProcessInfo
= (struct dyld_all_image_infos
*)(sc
->getProcessInfo());
271 // now that we are done bootstrapping dyld, call dyld's main
272 uintptr_t appsSlide
= slideOfMainExecutable(mainExecutableMH
);
273 return dyld::_main(mainExecutableMH
, appsSlide
, argc
, argv
, envp
, apple
, startGlue
);
278 } // end of namespace