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 <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 LC_SEGMENT_COMMAND LC_SEGMENT_64
49 #define macho_segment_command segment_command_64
50 #define macho_section section_64
53 #define LC_SEGMENT_COMMAND LC_SEGMENT
54 #define macho_segment_command segment_command
55 #define macho_section section
60 #define POINTER_RELOC X86_64_RELOC_UNSIGNED
62 #define POINTER_RELOC GENERIC_RELOC_VANILLA
66 namespace dyld
{ extern bool isRosetta(); };
70 // Code to bootstrap dyld into a runnable state
74 namespace dyldbootstrap
{
77 typedef void (*Initializer
)(int argc
, const char* argv
[], const char* envp
[], const char* apple
[]);
80 // For a regular executable, the crt code calls dyld to run the executables initializers.
81 // For a static executable, crt directly runs the initializers.
82 // dyld (should be static) but is a dynamic executable and needs this hack to run its own initializers.
83 // We pass argc, argv, etc in case libc.a uses those arguments
85 static void runDyldInitializers(const struct macho_header
* mh
, intptr_t slide
, int argc
, const char* argv
[], const char* envp
[], const char* apple
[])
87 const uint32_t cmd_count
= mh
->ncmds
;
88 const struct load_command
* const cmds
= (struct load_command
*)(((char*)mh
)+sizeof(macho_header
));
89 const struct load_command
* cmd
= cmds
;
90 for (uint32_t i
= 0; i
< cmd_count
; ++i
) {
92 case LC_SEGMENT_COMMAND
:
94 const struct macho_segment_command
* seg
= (struct macho_segment_command
*)cmd
;
95 const struct macho_section
* const sectionsStart
= (struct macho_section
*)((char*)seg
+ sizeof(struct macho_segment_command
));
96 const struct macho_section
* const sectionsEnd
= §ionsStart
[seg
->nsects
];
97 for (const struct macho_section
* sect
=sectionsStart
; sect
< sectionsEnd
; ++sect
) {
98 const uint8_t type
= sect
->flags
& SECTION_TYPE
;
99 if ( type
== S_MOD_INIT_FUNC_POINTERS
){
100 Initializer
* inits
= (Initializer
*)(sect
->addr
+ slide
);
101 const uint32_t count
= sect
->size
/ sizeof(uintptr_t);
102 for (uint32_t i
=0; i
< count
; ++i
) {
103 Initializer func
= inits
[i
];
104 func(argc
, argv
, envp
, apple
);
111 cmd
= (const struct load_command
*)(((char*)cmd
)+cmd
->cmdsize
);
117 // The kernel may have slid a Position Independent Executable
119 static uintptr_t slideOfMainExecutable(const struct macho_header
* mh
)
121 const uint32_t cmd_count
= mh
->ncmds
;
122 const struct load_command
* const cmds
= (struct load_command
*)(((char*)mh
)+sizeof(macho_header
));
123 const struct load_command
* cmd
= cmds
;
124 for (uint32_t i
= 0; i
< cmd_count
; ++i
) {
125 if ( cmd
->cmd
== LC_SEGMENT_COMMAND
) {
126 const struct macho_segment_command
* segCmd
= (struct macho_segment_command
*)cmd
;
127 if ( strcmp(segCmd
->segname
, "__TEXT") == 0 ) {
128 return (uintptr_t)mh
- segCmd
->vmaddr
;
131 cmd
= (const struct load_command
*)(((char*)cmd
)+cmd
->cmdsize
);
138 // If the kernel does not load dyld at its preferred address, we need to apply
139 // fixups to various initialized parts of the __DATA segment
141 static void rebaseDyld(const struct macho_header
* mh
, intptr_t slide
)
143 // rebase non-lazy pointers (which all point internal to dyld, since dyld uses no shared libraries)
144 // and get interesting pointers into dyld
145 const uint32_t cmd_count
= mh
->ncmds
;
146 const struct load_command
* const cmds
= (struct load_command
*)(((char*)mh
)+sizeof(macho_header
));
147 const struct load_command
* cmd
= cmds
;
148 const struct macho_segment_command
* linkEditSeg
= NULL
;
150 const struct macho_segment_command
* firstWritableSeg
= NULL
;
152 const struct dysymtab_command
* dynamicSymbolTable
= NULL
;
153 for (uint32_t i
= 0; i
< cmd_count
; ++i
) {
155 case LC_SEGMENT_COMMAND
:
157 const struct macho_segment_command
* seg
= (struct macho_segment_command
*)cmd
;
158 if ( strcmp(seg
->segname
, "__LINKEDIT") == 0 )
160 const struct macho_section
* const sectionsStart
= (struct macho_section
*)((char*)seg
+ sizeof(struct macho_segment_command
));
161 const struct macho_section
* const sectionsEnd
= §ionsStart
[seg
->nsects
];
162 for (const struct macho_section
* sect
=sectionsStart
; sect
< sectionsEnd
; ++sect
) {
163 const uint8_t type
= sect
->flags
& SECTION_TYPE
;
164 if ( type
== S_NON_LAZY_SYMBOL_POINTERS
) {
165 // rebase non-lazy pointers (which all point internal to dyld, since dyld uses no shared libraries)
166 const uint32_t pointerCount
= sect
->size
/ sizeof(uintptr_t);
167 uintptr_t* const symbolPointers
= (uintptr_t*)(sect
->addr
+ slide
);
168 for (uint32_t j
=0; j
< pointerCount
; ++j
) {
169 symbolPointers
[j
] += slide
;
174 if ( (firstWritableSeg
== NULL
) && (seg
->initprot
& VM_PROT_WRITE
) )
175 firstWritableSeg
= seg
;
180 dynamicSymbolTable
= (struct dysymtab_command
*)cmd
;
183 cmd
= (const struct load_command
*)(((char*)cmd
)+cmd
->cmdsize
);
186 // use reloc's to rebase all random data pointers
188 const uintptr_t relocBase
= firstWritableSeg
->vmaddr
+ slide
;
190 const uintptr_t relocBase
= (uintptr_t)mh
;
192 const relocation_info
* const relocsStart
= (struct relocation_info
*)(linkEditSeg
->vmaddr
+ slide
+ dynamicSymbolTable
->locreloff
- linkEditSeg
->fileoff
);
193 const relocation_info
* const relocsEnd
= &relocsStart
[dynamicSymbolTable
->nlocrel
];
194 for (const relocation_info
* reloc
=relocsStart
; reloc
< relocsEnd
; ++reloc
) {
195 #if __ppc__ || __ppc64__ || __i36__
196 if ( (reloc
->r_address
& R_SCATTERED
) != 0 )
197 throw "scattered relocation in dyld";
199 if ( reloc
->r_length
!= RELOC_SIZE
)
200 throw "relocation in dyld has wrong size";
202 if ( reloc
->r_type
!= POINTER_RELOC
)
203 throw "relocation in dyld has wrong type";
205 // update pointer by amount dyld slid
206 *((uintptr_t*)(reloc
->r_address
+ relocBase
)) += slide
;
211 extern "C" void dyld_exceptions_init(const struct macho_header
*, uintptr_t slide
); // in dyldExceptions.cpp
212 extern "C" void mach_init();
215 // _pthread_keys is partitioned in a lower part that dyld will use; libSystem
216 // will use the upper part. We set __pthread_tsd_first to 1 as the start of
217 // the lower part. Libc will take #1 and c++ exceptions will take #2. There
218 // is one free key=3 left.
221 extern int __pthread_tsd_first
;
222 extern void _pthread_keys_init();
227 // This is code to bootstrap dyld. This work in normally done for a program by dyld and crt.
228 // In dyld we have to do this manually.
230 uintptr_t start(const struct macho_header
* appsMachHeader
, int argc
, const char* argv
[],
231 intptr_t slide
, const struct macho_header
* dyldsMachHeader
)
233 // if kernel had to slide dyld, we need to fix up load sensitive locations
234 // we have to do this before using any global variables
236 rebaseDyld(dyldsMachHeader
, slide
);
239 #if __IPHONE_OS_VERSION_MIN_REQUIRED
240 // set pthread keys to dyld range
241 __pthread_tsd_first
= 1;
242 _pthread_keys_init();
245 // enable C++ exceptions to work inside dyld
246 dyld_exceptions_init(dyldsMachHeader
, slide
);
248 // allow dyld to use mach messaging
251 // kernel sets up env pointer to be just past end of agv array
252 const char** envp
= &argv
[argc
+1];
254 // kernel sets up apple pointer to be just past end of envp array
255 const char** apple
= envp
;
256 while(*apple
!= NULL
) { ++apple
; }
259 // run all C++ initializers inside dyld
260 runDyldInitializers(dyldsMachHeader
, slide
, argc
, argv
, envp
, apple
);
262 // now that we are done bootstrapping dyld, call dyld's main
263 uintptr_t appsSlide
= slideOfMainExecutable(appsMachHeader
);
264 return dyld::_main(appsMachHeader
, appsSlide
, argc
, argv
, envp
, apple
);
270 } // end of namespace