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>
35 #include <mach-o/x86_64/reloc.h>
40 #define MH_PIE 0x200000
45 #define LC_SEGMENT_COMMAND LC_SEGMENT_64
46 #define macho_segment_command segment_command_64
47 #define macho_section section_64
50 #define LC_SEGMENT_COMMAND LC_SEGMENT
51 #define macho_segment_command segment_command
52 #define macho_section section
57 #define POINTER_RELOC X86_64_RELOC_UNSIGNED
59 #define POINTER_RELOC GENERIC_RELOC_VANILLA
63 namespace dyld
{ extern bool isRosetta(); };
67 // Code to bootstrap dyld into a runnable state
71 namespace dyldbootstrap
{
74 typedef void (*Initializer
)(int argc
, const char* argv
[], const char* envp
[], const char* apple
[]);
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
82 static void runDyldInitializers(const struct macho_header
* mh
, intptr_t slide
, int argc
, const char* argv
[], const char* envp
[], const char* apple
[])
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
) {
89 case LC_SEGMENT_COMMAND
:
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
= §ionsStart
[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
);
108 cmd
= (const struct load_command
*)(((char*)cmd
)+cmd
->cmdsize
);
114 // The kernel may have slid a Position Independent Executable
116 static uintptr_t slideOfMainExecutable(const struct macho_header
* mh
)
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
;
128 cmd
= (const struct load_command
*)(((char*)cmd
)+cmd
->cmdsize
);
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
138 static void rebaseDyld(const struct macho_header
* mh
, intptr_t slide
)
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
;
147 const struct macho_segment_command
* firstWritableSeg
= NULL
;
149 const struct dysymtab_command
* dynamicSymbolTable
= NULL
;
150 for (uint32_t i
= 0; i
< cmd_count
; ++i
) {
152 case LC_SEGMENT_COMMAND
:
154 const struct macho_segment_command
* seg
= (struct macho_segment_command
*)cmd
;
155 if ( strcmp(seg
->segname
, "__LINKEDIT") == 0 )
157 const struct macho_section
* const sectionsStart
= (struct macho_section
*)((char*)seg
+ sizeof(struct macho_segment_command
));
158 const struct macho_section
* const sectionsEnd
= §ionsStart
[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
;
171 if ( (firstWritableSeg
== NULL
) && (seg
->initprot
& VM_PROT_WRITE
) )
172 firstWritableSeg
= seg
;
177 dynamicSymbolTable
= (struct dysymtab_command
*)cmd
;
180 cmd
= (const struct load_command
*)(((char*)cmd
)+cmd
->cmdsize
);
183 // use reloc's to rebase all random data pointers
185 const uintptr_t relocBase
= firstWritableSeg
->vmaddr
+ slide
;
187 const uintptr_t relocBase
= (uintptr_t)mh
;
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";
195 if ( reloc
->r_type
!= POINTER_RELOC
)
196 throw "relocation in dyld has wrong type";
198 // update pointer by amount dyld slid
199 *((uintptr_t*)(reloc
->r_address
+ relocBase
)) += slide
;
204 extern "C" void mach_init();
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.
213 extern int __pthread_tsd_first
;
214 extern void _pthread_keys_init();
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.
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
)
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
229 rebaseDyld(dyldsMachHeader
, slide
);
232 #if __IPHONE_OS_VERSION_MIN_REQUIRED
233 // set pthread keys to dyld range
234 __pthread_tsd_first
= 1;
235 _pthread_keys_init();
238 // allow dyld to use mach messaging
241 // kernel sets up env pointer to be just past end of agv array
242 const char** envp
= &argv
[argc
+1];
244 // kernel sets up apple pointer to be just past end of envp array
245 const char** apple
= envp
;
246 while(*apple
!= NULL
) { ++apple
; }
249 // run all C++ initializers inside dyld
250 runDyldInitializers(dyldsMachHeader
, slide
, argc
, argv
, envp
, apple
);
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
);
260 } // end of namespace