]> git.saurik.com Git - apple/dyld.git/blob - src/dyldInitialization.cpp
a1266d7f68cf221e276d40c86aa737911abcd23c
[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 __x86_64__
35 #include <mach-o/x86_64/reloc.h>
36 #endif
37 #include "dyld.h"
38
39 #ifndef MH_PIE
40 #define MH_PIE 0x200000
41 #endif
42
43
44 #if __LP64__
45 #define LC_SEGMENT_COMMAND LC_SEGMENT_64
46 #define macho_segment_command segment_command_64
47 #define macho_section section_64
48 #define RELOC_SIZE 3
49 #else
50 #define LC_SEGMENT_COMMAND LC_SEGMENT
51 #define macho_segment_command segment_command
52 #define macho_section section
53 #define RELOC_SIZE 2
54 #endif
55
56 #if __x86_64__
57 #define POINTER_RELOC X86_64_RELOC_UNSIGNED
58 #else
59 #define POINTER_RELOC GENERIC_RELOC_VANILLA
60 #endif
61
62 // from dyld.cpp
63 namespace dyld { extern bool isRosetta(); };
64
65
66 //
67 // Code to bootstrap dyld into a runnable state
68 //
69 //
70
71 namespace dyldbootstrap {
72
73
74 typedef void (*Initializer)(int argc, const char* argv[], const char* envp[], const char* apple[]);
75
76 //
77 // For a regular executable, the crt code calls dyld to run the executables initializers.
78 // For a static executable, crt directly runs the initializers.
79 // dyld (should be static) but is a dynamic executable and needs this hack to run its own initializers.
80 // We pass argc, argv, etc in case libc.a uses those arguments
81 //
82 static void runDyldInitializers(const struct macho_header* mh, intptr_t slide, int argc, const char* argv[], const char* envp[], const char* apple[])
83 {
84 const uint32_t cmd_count = mh->ncmds;
85 const struct load_command* const cmds = (struct load_command*)(((char*)mh)+sizeof(macho_header));
86 const struct load_command* cmd = cmds;
87 for (uint32_t i = 0; i < cmd_count; ++i) {
88 switch (cmd->cmd) {
89 case LC_SEGMENT_COMMAND:
90 {
91 const struct macho_segment_command* seg = (struct macho_segment_command*)cmd;
92 const struct macho_section* const sectionsStart = (struct macho_section*)((char*)seg + sizeof(struct macho_segment_command));
93 const struct macho_section* const sectionsEnd = &sectionsStart[seg->nsects];
94 for (const struct macho_section* sect=sectionsStart; sect < sectionsEnd; ++sect) {
95 const uint8_t type = sect->flags & SECTION_TYPE;
96 if ( type == S_MOD_INIT_FUNC_POINTERS ){
97 Initializer* inits = (Initializer*)(sect->addr + slide);
98 const uint32_t count = sect->size / sizeof(uintptr_t);
99 for (uint32_t i=0; i < count; ++i) {
100 Initializer func = inits[i];
101 func(argc, argv, envp, apple);
102 }
103 }
104 }
105 }
106 break;
107 }
108 cmd = (const struct load_command*)(((char*)cmd)+cmd->cmdsize);
109 }
110 }
111
112
113 //
114 // The kernel may have slid a Position Independent Executable
115 //
116 static uintptr_t slideOfMainExecutable(const struct macho_header* mh)
117 {
118 const uint32_t cmd_count = mh->ncmds;
119 const struct load_command* const cmds = (struct load_command*)(((char*)mh)+sizeof(macho_header));
120 const struct load_command* cmd = cmds;
121 for (uint32_t i = 0; i < cmd_count; ++i) {
122 if ( cmd->cmd == LC_SEGMENT_COMMAND ) {
123 const struct macho_segment_command* segCmd = (struct macho_segment_command*)cmd;
124 if ( strcmp(segCmd->segname, "__TEXT") == 0 ) {
125 return (uintptr_t)mh - segCmd->vmaddr;
126 }
127 }
128 cmd = (const struct load_command*)(((char*)cmd)+cmd->cmdsize);
129 }
130 return 0;
131 }
132
133
134 //
135 // If the kernel does not load dyld at its preferred address, we need to apply
136 // fixups to various initialized parts of the __DATA segment
137 //
138 static void rebaseDyld(const struct macho_header* mh, intptr_t slide)
139 {
140 // rebase non-lazy pointers (which all point internal to dyld, since dyld uses no shared libraries)
141 // and get interesting pointers into dyld
142 const uint32_t cmd_count = mh->ncmds;
143 const struct load_command* const cmds = (struct load_command*)(((char*)mh)+sizeof(macho_header));
144 const struct load_command* cmd = cmds;
145 const struct macho_segment_command* linkEditSeg = NULL;
146 #if __x86_64__
147 const struct macho_segment_command* firstWritableSeg = NULL;
148 #endif
149 const struct dysymtab_command* dynamicSymbolTable = NULL;
150 for (uint32_t i = 0; i < cmd_count; ++i) {
151 switch (cmd->cmd) {
152 case LC_SEGMENT_COMMAND:
153 {
154 const struct macho_segment_command* seg = (struct macho_segment_command*)cmd;
155 if ( strcmp(seg->segname, "__LINKEDIT") == 0 )
156 linkEditSeg = seg;
157 const struct macho_section* const sectionsStart = (struct macho_section*)((char*)seg + sizeof(struct macho_segment_command));
158 const struct macho_section* const sectionsEnd = &sectionsStart[seg->nsects];
159 for (const struct macho_section* sect=sectionsStart; sect < sectionsEnd; ++sect) {
160 const uint8_t type = sect->flags & SECTION_TYPE;
161 if ( type == S_NON_LAZY_SYMBOL_POINTERS ) {
162 // rebase non-lazy pointers (which all point internal to dyld, since dyld uses no shared libraries)
163 const uint32_t pointerCount = sect->size / sizeof(uintptr_t);
164 uintptr_t* const symbolPointers = (uintptr_t*)(sect->addr + slide);
165 for (uint32_t j=0; j < pointerCount; ++j) {
166 symbolPointers[j] += slide;
167 }
168 }
169 }
170 #if __x86_64__
171 if ( (firstWritableSeg == NULL) && (seg->initprot & VM_PROT_WRITE) )
172 firstWritableSeg = seg;
173 #endif
174 }
175 break;
176 case LC_DYSYMTAB:
177 dynamicSymbolTable = (struct dysymtab_command *)cmd;
178 break;
179 }
180 cmd = (const struct load_command*)(((char*)cmd)+cmd->cmdsize);
181 }
182
183 // use reloc's to rebase all random data pointers
184 #if __x86_64__
185 const uintptr_t relocBase = firstWritableSeg->vmaddr + slide;
186 #else
187 const uintptr_t relocBase = (uintptr_t)mh;
188 #endif
189 const relocation_info* const relocsStart = (struct relocation_info*)(linkEditSeg->vmaddr + slide + dynamicSymbolTable->locreloff - linkEditSeg->fileoff);
190 const relocation_info* const relocsEnd = &relocsStart[dynamicSymbolTable->nlocrel];
191 for (const relocation_info* reloc=relocsStart; reloc < relocsEnd; ++reloc) {
192 if ( reloc->r_length != RELOC_SIZE )
193 throw "relocation in dyld has wrong size";
194
195 if ( reloc->r_type != POINTER_RELOC )
196 throw "relocation in dyld has wrong type";
197
198 // update pointer by amount dyld slid
199 *((uintptr_t*)(reloc->r_address + relocBase)) += slide;
200 }
201 }
202
203
204 extern "C" void mach_init();
205
206 //
207 // _pthread_keys is partitioned in a lower part that dyld will use; libSystem
208 // will use the upper part. We set __pthread_tsd_first to 1 as the start of
209 // the lower part. Libc will take #1 and c++ exceptions will take #2. There
210 // is one free key=3 left.
211 //
212 extern "C" {
213 extern int __pthread_tsd_first;
214 extern void _pthread_keys_init();
215 }
216
217
218 //
219 // This is code to bootstrap dyld. This work in normally done for a program by dyld and crt.
220 // In dyld we have to do this manually.
221 //
222 uintptr_t start(const struct macho_header* appsMachHeader, int argc, const char* argv[],
223 intptr_t slide, const struct macho_header* dyldsMachHeader,
224 uintptr_t* startGlue)
225 {
226 // if kernel had to slide dyld, we need to fix up load sensitive locations
227 // we have to do this before using any global variables
228 if ( slide != 0 ) {
229 rebaseDyld(dyldsMachHeader, slide);
230 }
231
232 #if __IPHONE_OS_VERSION_MIN_REQUIRED
233 // set pthread keys to dyld range
234 __pthread_tsd_first = 1;
235 _pthread_keys_init();
236 #endif
237
238 // allow dyld to use mach messaging
239 mach_init();
240
241 // kernel sets up env pointer to be just past end of agv array
242 const char** envp = &argv[argc+1];
243
244 // kernel sets up apple pointer to be just past end of envp array
245 const char** apple = envp;
246 while(*apple != NULL) { ++apple; }
247 ++apple;
248
249 // run all C++ initializers inside dyld
250 runDyldInitializers(dyldsMachHeader, slide, argc, argv, envp, apple);
251
252 // now that we are done bootstrapping dyld, call dyld's main
253 uintptr_t appsSlide = slideOfMainExecutable(appsMachHeader);
254 return dyld::_main(appsMachHeader, appsSlide, argc, argv, envp, apple, startGlue);
255 }
256
257
258
259
260 } // end of namespace
261
262
263
264