]> git.saurik.com Git - apple/dyld.git/blob - src/dyldInitialization.cpp
dyld-195.5.tar.gz
[apple/dyld.git] / src / dyldInitialization.cpp
1 /* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*-
2 *
3 * Copyright (c) 2004-2008 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 #define __STDC_LIMIT_MACROS
26 #include <stdint.h>
27 #include <stddef.h>
28 #include <string.h>
29 #include <stdlib.h>
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>
36 #endif
37 #if __x86_64__
38 #include <mach-o/x86_64/reloc.h>
39 #endif
40 #include "dyld.h"
41
42 #ifndef MH_PIE
43 #define MH_PIE 0x200000
44 #endif
45
46
47 #if __LP64__
48 #define LC_SEGMENT_COMMAND LC_SEGMENT_64
49 #define macho_segment_command segment_command_64
50 #define macho_section section_64
51 #define RELOC_SIZE 3
52 #else
53 #define LC_SEGMENT_COMMAND LC_SEGMENT
54 #define macho_segment_command segment_command
55 #define macho_section section
56 #define RELOC_SIZE 2
57 #endif
58
59 #if __x86_64__
60 #define POINTER_RELOC X86_64_RELOC_UNSIGNED
61 #else
62 #define POINTER_RELOC GENERIC_RELOC_VANILLA
63 #endif
64
65 // from dyld.cpp
66 namespace dyld { extern bool isRosetta(); };
67
68
69 //
70 // Code to bootstrap dyld into a runnable state
71 //
72 //
73
74 namespace dyldbootstrap {
75
76
77 typedef void (*Initializer)(int argc, const char* argv[], const char* envp[], const char* apple[]);
78
79 //
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
84 //
85 static void runDyldInitializers(const struct macho_header* mh, intptr_t slide, int argc, const char* argv[], const char* envp[], const char* apple[])
86 {
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) {
91 switch (cmd->cmd) {
92 case LC_SEGMENT_COMMAND:
93 {
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 = &sectionsStart[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);
105 }
106 }
107 }
108 }
109 break;
110 }
111 cmd = (const struct load_command*)(((char*)cmd)+cmd->cmdsize);
112 }
113 }
114
115
116 //
117 // The kernel may have slid a Position Independent Executable
118 //
119 static uintptr_t slideOfMainExecutable(const struct macho_header* mh)
120 {
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;
129 }
130 }
131 cmd = (const struct load_command*)(((char*)cmd)+cmd->cmdsize);
132 }
133 return 0;
134 }
135
136
137 //
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
140 //
141 static void rebaseDyld(const struct macho_header* mh, intptr_t slide)
142 {
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;
149 #if __x86_64__
150 const struct macho_segment_command* firstWritableSeg = NULL;
151 #endif
152 const struct dysymtab_command* dynamicSymbolTable = NULL;
153 for (uint32_t i = 0; i < cmd_count; ++i) {
154 switch (cmd->cmd) {
155 case LC_SEGMENT_COMMAND:
156 {
157 const struct macho_segment_command* seg = (struct macho_segment_command*)cmd;
158 if ( strcmp(seg->segname, "__LINKEDIT") == 0 )
159 linkEditSeg = seg;
160 const struct macho_section* const sectionsStart = (struct macho_section*)((char*)seg + sizeof(struct macho_segment_command));
161 const struct macho_section* const sectionsEnd = &sectionsStart[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;
170 }
171 }
172 }
173 #if __x86_64__
174 if ( (firstWritableSeg == NULL) && (seg->initprot & VM_PROT_WRITE) )
175 firstWritableSeg = seg;
176 #endif
177 }
178 break;
179 case LC_DYSYMTAB:
180 dynamicSymbolTable = (struct dysymtab_command *)cmd;
181 break;
182 }
183 cmd = (const struct load_command*)(((char*)cmd)+cmd->cmdsize);
184 }
185
186 // use reloc's to rebase all random data pointers
187 #if __x86_64__
188 const uintptr_t relocBase = firstWritableSeg->vmaddr + slide;
189 #else
190 const uintptr_t relocBase = (uintptr_t)mh;
191 #endif
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";
198 #endif
199 if ( reloc->r_length != RELOC_SIZE )
200 throw "relocation in dyld has wrong size";
201
202 if ( reloc->r_type != POINTER_RELOC )
203 throw "relocation in dyld has wrong type";
204
205 // update pointer by amount dyld slid
206 *((uintptr_t*)(reloc->r_address + relocBase)) += slide;
207 }
208 }
209
210
211 extern "C" void dyld_exceptions_init(const struct macho_header*, uintptr_t slide); // in dyldExceptions.cpp
212 extern "C" void mach_init();
213
214 //
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.
219 //
220 extern "C" {
221 extern int __pthread_tsd_first;
222 extern void _pthread_keys_init();
223 }
224
225
226 //
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.
229 //
230 uintptr_t start(const struct macho_header* appsMachHeader, int argc, const char* argv[],
231 intptr_t slide, const struct macho_header* dyldsMachHeader)
232 {
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
235 if ( slide != 0 ) {
236 rebaseDyld(dyldsMachHeader, slide);
237 }
238
239 #if __IPHONE_OS_VERSION_MIN_REQUIRED
240 // set pthread keys to dyld range
241 __pthread_tsd_first = 1;
242 _pthread_keys_init();
243 #endif
244
245 // enable C++ exceptions to work inside dyld
246 dyld_exceptions_init(dyldsMachHeader, slide);
247
248 // allow dyld to use mach messaging
249 mach_init();
250
251 // kernel sets up env pointer to be just past end of agv array
252 const char** envp = &argv[argc+1];
253
254 // kernel sets up apple pointer to be just past end of envp array
255 const char** apple = envp;
256 while(*apple != NULL) { ++apple; }
257 ++apple;
258
259 // run all C++ initializers inside dyld
260 runDyldInitializers(dyldsMachHeader, slide, argc, argv, envp, apple);
261
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);
265 }
266
267
268
269
270 } // end of namespace
271
272
273
274