Loading...
--- dyld/dyld-210.2.3/src/dyldInitialization.cpp
+++ dyld/dyld-45.3/src/dyldInitialization.cpp
@@ -1,6 +1,6 @@
/* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*-
*
- * Copyright (c) 2004-2008 Apple Inc. All rights reserved.
+ * Copyright (c) 2004-2005 Apple Computer, Inc. All rights reserved.
*
* @APPLE_LICENSE_HEADER_START@
*
@@ -22,46 +22,32 @@
* @APPLE_LICENSE_HEADER_END@
*/
-#define __STDC_LIMIT_MACROS
-#include <stdint.h>
#include <stddef.h>
#include <string.h>
-#include <stdlib.h>
#include <mach/mach.h>
#include <mach-o/loader.h>
#include <mach-o/ldsyms.h>
#include <mach-o/reloc.h>
-#if __x86_64__
- #include <mach-o/x86_64/reloc.h>
+#if __ppc__ || __ppc64__
+ #include <mach-o/ppc/reloc.h>
#endif
#include "dyld.h"
-#ifndef MH_PIE
- #define MH_PIE 0x200000
-#endif
-
-
#if __LP64__
+ #define macho_header mach_header_64
#define LC_SEGMENT_COMMAND LC_SEGMENT_64
#define macho_segment_command segment_command_64
#define macho_section section_64
#define RELOC_SIZE 3
#else
+ #define macho_header mach_header
#define LC_SEGMENT_COMMAND LC_SEGMENT
#define macho_segment_command segment_command
#define macho_section section
#define RELOC_SIZE 2
#endif
-#if __x86_64__
- #define POINTER_RELOC X86_64_RELOC_UNSIGNED
-#else
#define POINTER_RELOC GENERIC_RELOC_VANILLA
-#endif
-
-// from dyld.cpp
-namespace dyld { extern bool isRosetta(); };
-
//
// Code to bootstrap dyld into a runnable state
@@ -109,28 +95,6 @@
}
}
-
-//
-// The kernel may have slid a Position Independent Executable
-//
-static uintptr_t slideOfMainExecutable(const struct macho_header* mh)
-{
- const uint32_t cmd_count = mh->ncmds;
- const struct load_command* const cmds = (struct load_command*)(((char*)mh)+sizeof(macho_header));
- const struct load_command* cmd = cmds;
- for (uint32_t i = 0; i < cmd_count; ++i) {
- if ( cmd->cmd == LC_SEGMENT_COMMAND ) {
- const struct macho_segment_command* segCmd = (struct macho_segment_command*)cmd;
- if ( strcmp(segCmd->segname, "__TEXT") == 0 ) {
- return (uintptr_t)mh - segCmd->vmaddr;
- }
- }
- cmd = (const struct load_command*)(((char*)cmd)+cmd->cmdsize);
- }
- return 0;
-}
-
-
//
// If the kernel does not load dyld at its preferred address, we need to apply
// fixups to various initialized parts of the __DATA segment
@@ -143,9 +107,6 @@
const struct load_command* const cmds = (struct load_command*)(((char*)mh)+sizeof(macho_header));
const struct load_command* cmd = cmds;
const struct macho_segment_command* linkEditSeg = NULL;
-#if __x86_64__
- const struct macho_segment_command* firstWritableSeg = NULL;
-#endif
const struct dysymtab_command* dynamicSymbolTable = NULL;
for (uint32_t i = 0; i < cmd_count; ++i) {
switch (cmd->cmd) {
@@ -167,10 +128,6 @@
}
}
}
-#if __x86_64__
- if ( (firstWritableSeg == NULL) && (seg->initprot & VM_PROT_WRITE) )
- firstWritableSeg = seg;
-#endif
}
break;
case LC_DYSYMTAB:
@@ -181,14 +138,14 @@
}
// use reloc's to rebase all random data pointers
-#if __x86_64__
- const uintptr_t relocBase = firstWritableSeg->vmaddr + slide;
-#else
const uintptr_t relocBase = (uintptr_t)mh;
-#endif
const relocation_info* const relocsStart = (struct relocation_info*)(linkEditSeg->vmaddr + slide + dynamicSymbolTable->locreloff - linkEditSeg->fileoff);
const relocation_info* const relocsEnd = &relocsStart[dynamicSymbolTable->nlocrel];
for (const relocation_info* reloc=relocsStart; reloc < relocsEnd; ++reloc) {
+ #if __ppc__ || __ppc64__ || __i36__
+ if ( (reloc->r_address & R_SCATTERED) != 0 )
+ throw "scattered relocation in dyld";
+ #endif
if ( reloc->r_length != RELOC_SIZE )
throw "relocation in dyld has wrong size";
@@ -201,43 +158,60 @@
}
+//
+// For some reason the kernel loads dyld with __TEXT and __LINKEDIT writable
+// rdar://problem/3702311
+//
+static void segmentProtectDyld(const struct macho_header* mh, intptr_t slide)
+{
+ const uint32_t cmd_count = mh->ncmds;
+ const struct load_command* const cmds = (struct load_command*)(((char*)mh)+sizeof(macho_header));
+ const struct load_command* cmd = cmds;
+ for (uint32_t i = 0; i < cmd_count; ++i) {
+ switch (cmd->cmd) {
+ case LC_SEGMENT_COMMAND:
+ {
+ const struct macho_segment_command* seg = (struct macho_segment_command*)cmd;
+ vm_address_t addr = seg->vmaddr + slide;
+ vm_size_t size = seg->vmsize;
+ const bool setCurrentPermissions = false;
+ vm_protect(mach_task_self(), addr, size, setCurrentPermissions, seg->initprot);
+ //fprintf(stderr, "dyld: segment %s, 0x%08X -> 0x%08X, set to %d\n", seg->segname, addr, addr+size-1, seg->initprot);
+ }
+ break;
+ }
+ cmd = (const struct load_command*)(((char*)cmd)+cmd->cmdsize);
+ }
+
+}
+
+extern "C" void dyld_exceptions_init(const struct macho_header*, uintptr_t slide); // in dyldExceptions.cpp
extern "C" void mach_init();
-
-//
-// _pthread_keys is partitioned in a lower part that dyld will use; libSystem
-// will use the upper part. We set __pthread_tsd_first to 1 as the start of
-// the lower part. Libc will take #1 and c++ exceptions will take #2. There
-// is one free key=3 left.
-//
-extern "C" {
- extern int __pthread_tsd_first;
- extern void _pthread_keys_init();
-}
-
//
// 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 struct macho_header* appsMachHeader, int argc, const char* argv[],
- intptr_t slide, const struct macho_header* dyldsMachHeader,
- uintptr_t* startGlue)
-{
+uintptr_t start(const struct mach_header* appsMachHeader, int argc, const char* argv[], intptr_t slide)
+{
+ // _mh_dylinker_header is magic symbol defined by static linker (ld), see <mach-o/ldsyms.h>
+ const struct macho_header* dyldsMachHeader = (const struct macho_header*)(((char*)&_mh_dylinker_header)+slide);
+
// if kernel had to slide dyld, we need to fix up load sensitive locations
// we have to do this before using any global variables
if ( slide != 0 ) {
rebaseDyld(dyldsMachHeader, slide);
}
-
-#if __IPHONE_OS_VERSION_MIN_REQUIRED
- // set pthread keys to dyld range
- __pthread_tsd_first = 1;
- _pthread_keys_init();
-#endif
-
+
+ // enable C++ exceptions to work inside dyld
+ dyld_exceptions_init(dyldsMachHeader, slide);
+
// allow dyld to use mach messaging
mach_init();
+ // set protection on segments (has to be done after mach_init)
+ segmentProtectDyld(dyldsMachHeader, slide);
+
// kernel sets up env pointer to be just past end of agv array
const char** envp = &argv[argc+1];
@@ -248,10 +222,9 @@
// run all C++ initializers inside dyld
runDyldInitializers(dyldsMachHeader, slide, argc, argv, envp, apple);
-
+
// now that we are done bootstrapping dyld, call dyld's main
- uintptr_t appsSlide = slideOfMainExecutable(appsMachHeader);
- return dyld::_main(appsMachHeader, appsSlide, argc, argv, envp, apple, startGlue);
+ return dyld::_main(appsMachHeader, argc, argv, envp, apple);
}