Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 | /* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*- * * Copyright (c) 2004-2008 Apple Inc. All rights reserved. * * @APPLE_LICENSE_HEADER_START@ * * This file contains Original Code and/or Modifications of Original Code * as defined in and that are subject to the Apple Public Source License * Version 2.0 (the 'License'). You may not use this file except in * compliance with the License. Please obtain a copy of the License at * http://www.opensource.apple.com/apsl/ and read it before using this * file. * * The Original Code and all software distributed under the License are * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. * Please see the License for the specific language governing rights and * limitations under the License. * * @APPLE_LICENSE_HEADER_END@ */ #define __STDC_LIMIT_MACROS #include <stdint.h> #include <stddef.h> #include <string.h> #include <stdlib.h> #include <sys/mman.h> #include <mach/mach.h> #include "dyld2.h" #include "dyldSyscallInterface.h" #include "MachOAnalyzer.h" #include "Tracing.h" // from libc.a extern "C" void mach_init(); extern "C" void __guard_setup(const char* apple[]); extern "C" void _subsystem_init(const char* apple[]); // from dyld_debugger.cpp extern void syncProcessInfo(); const dyld::SyscallHelpers* gSyscallHelpers = NULL; // // Code to bootstrap dyld into a runnable state // // namespace dyldbootstrap { // currently dyld has no initializers, but if some come back, set this to non-zero #define DYLD_INITIALIZER_SUPPORT 0 #if DYLD_INITIALIZER_SUPPORT typedef void (*Initializer)(int argc, const char* argv[], const char* envp[], const char* apple[]); extern const Initializer inits_start __asm("section$start$__DATA$__mod_init_func"); extern const Initializer inits_end __asm("section$end$__DATA$__mod_init_func"); // // For a regular executable, the crt code calls dyld to run the executables initializers. // For a static executable, crt directly runs the initializers. // dyld (should be static) but is a dynamic executable and needs this hack to run its own initializers. // We pass argc, argv, etc in case libc.a uses those arguments // static void runDyldInitializers(int argc, const char* argv[], const char* envp[], const char* apple[]) { for (const Initializer* p = &inits_start; p < &inits_end; ++p) { (*p)(argc, argv, envp, apple); } } #endif // DYLD_INITIALIZER_SUPPORT // // On disk, all pointers in dyld's DATA segment are chained together. // They need to be fixed up to be real pointers to run. // static void rebaseDyld(const dyld3::MachOLoaded* dyldMH) { // walk all fixups chains and rebase dyld const dyld3::MachOAnalyzer* ma = (dyld3::MachOAnalyzer*)dyldMH; assert(ma->hasChainedFixups()); uintptr_t slide = (long)ma; // all fixup chain based images have a base address of zero, so slide == load address __block Diagnostics diag; ma->withChainStarts(diag, 0, ^(const dyld_chained_starts_in_image* starts) { ma->fixupAllChainedFixups(diag, starts, slide, dyld3::Array<const void*>(), nullptr); }); diag.assertNoError(); // now that rebasing done, initialize mach/syscall layer mach_init(); // <rdar://47805386> mark __DATA_CONST segment in dyld as read-only (once fixups are done) ma->forEachSegment(^(const dyld3::MachOFile::SegmentInfo& info, bool& stop) { if ( info.readOnlyData ) { ::mprotect(((uint8_t*)(dyldMH))+info.vmAddr, (size_t)info.vmSize, VM_PROT_READ); } }); } // // This is code to bootstrap dyld. This work in normally done for a program by dyld and crt. // In dyld we have to do this manually. // uintptr_t start(const dyld3::MachOLoaded* appsMachHeader, int argc, const char* argv[], const dyld3::MachOLoaded* dyldsMachHeader, uintptr_t* startGlue) { // Emit kdebug tracepoint to indicate dyld bootstrap has started <rdar://46878536> dyld3::kdebug_trace_dyld_marker(DBG_DYLD_TIMING_BOOTSTRAP_START, 0, 0, 0, 0); // if kernel had to slide dyld, we need to fix up load sensitive locations // we have to do this before using any global variables rebaseDyld(dyldsMachHeader); // kernel sets up env pointer to be just past end of agv array const char** envp = &argv[argc+1]; // kernel sets up apple pointer to be just past end of envp array const char** apple = envp; while(*apple != NULL) { ++apple; } ++apple; // set up random value for stack canary __guard_setup(apple); #if DYLD_INITIALIZER_SUPPORT // run all C++ initializers inside dyld runDyldInitializers(argc, argv, envp, apple); #endif _subsystem_init(apple); // now that we are done bootstrapping dyld, call dyld's main uintptr_t appsSlide = appsMachHeader->getSlide(); return dyld::_main((macho_header*)appsMachHeader, appsSlide, argc, argv, envp, apple, startGlue); } #if TARGET_OS_SIMULATOR extern "C" uintptr_t start_sim(int argc, const char* argv[], const char* envp[], const char* apple[], const dyld3::MachOLoaded* mainExecutableMH, const dyld3::MachOLoaded* dyldMH, uintptr_t dyldSlide, const dyld::SyscallHelpers*, uintptr_t* startGlue); uintptr_t start_sim(int argc, const char* argv[], const char* envp[], const char* apple[], const dyld3::MachOLoaded* mainExecutableMH, const dyld3::MachOLoaded* dyldSimMH, uintptr_t dyldSlide, const dyld::SyscallHelpers* sc, uintptr_t* startGlue) { // save table of syscall pointers gSyscallHelpers = sc; // dyld_sim uses chained rebases, so it always need to be fixed up rebaseDyld(dyldSimMH); // set up random value for stack canary __guard_setup(apple); // setup gProcessInfo to point to host dyld's struct dyld::gProcessInfo = (struct dyld_all_image_infos*)(sc->getProcessInfo()); syncProcessInfo(); // now that we are done bootstrapping dyld, call dyld's main uintptr_t appsSlide = mainExecutableMH->getSlide(); return dyld::_main((macho_header*)mainExecutableMH, appsSlide, argc, argv, envp, apple, startGlue); } #endif } // end of namespace |