Loading...
src/dyldInitialization.cpp dyld-519.2.2 dyld-43
--- dyld/dyld-519.2.2/src/dyldInitialization.cpp
+++ dyld/dyld-43/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,56 +22,31 @@
  * @APPLE_LICENSE_HEADER_END@
  */
 
-#define __STDC_LIMIT_MACROS
-#include <stdint.h>
 #include <stddef.h>
 #include <string.h>
-#include <stdlib.h>
-#include <Availability.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"
-#include "dyldSyscallInterface.h"
-
-// from dyld_gdb.cpp 
-extern void addImagesToAllImages(uint32_t infoCount, const dyld_image_info info[]);
-extern void syncProcessInfo();
-
-#ifndef MH_PIE
-	#define MH_PIE 0x200000 
-#endif
-
-// currently dyld has no initializers, but if some come back, set this to non-zero
-#define DYLD_INITIALIZER_SUPPORT  0
 
 #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
-
-
-#if TARGET_IPHONE_SIMULATOR
-const dyld::SyscallHelpers* gSyscallHelpers = NULL;
-#endif
-
 
 //
 //  Code to bootstrap dyld into a runnable state
@@ -81,13 +56,7 @@
 namespace dyldbootstrap {
 
 
-
-#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.
@@ -97,31 +66,32 @@
 //
 static void runDyldInitializers(const struct macho_header* mh, intptr_t slide, 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
-
-
-//
-//  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 ( (segCmd->fileoff == 0) && (segCmd->filesize != 0)) {
-				return (uintptr_t)mh - segCmd->vmaddr;
-			}
+		switch (cmd->cmd) {
+			case LC_SEGMENT_COMMAND:
+				{
+					const struct macho_segment_command* seg = (struct macho_segment_command*)cmd;
+					const struct macho_section* const sectionsStart = (struct macho_section*)((char*)seg + sizeof(struct macho_segment_command));
+					const struct macho_section* const sectionsEnd = &sectionsStart[seg->nsects];
+					for (const struct macho_section* sect=sectionsStart; sect < sectionsEnd; ++sect) {
+						const uint8_t type = sect->flags & SECTION_TYPE;
+						if ( type == S_MOD_INIT_FUNC_POINTERS ){
+							Initializer* inits = (Initializer*)(sect->addr + slide);
+							const uint32_t count = sect->size / sizeof(uintptr_t);
+							for (uint32_t i=0; i < count; ++i) {
+								Initializer func = inits[i];
+								func(argc, argv, envp, apple);
+							}
+						}
+					}
+				}
+				break;
 		}
 		cmd = (const struct load_command*)(((char*)cmd)+cmd->cmdsize);
 	}
-	return 0;
 }
 
 
@@ -131,16 +101,13 @@
 //
 static void rebaseDyld(const struct macho_header* mh, intptr_t slide)
 {
-	// rebase non-lazy pointers (which all point internal to dyld, since dyld uses no shared libraries)
-	// and get interesting pointers into dyld
+	// get interesting pointers into dyld
 	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;
 	const struct macho_segment_command* linkEditSeg = NULL;
-#if __x86_64__
-	const struct macho_segment_command* firstWritableSeg = NULL;
-#endif
 	const struct dysymtab_command* dynamicSymbolTable = NULL;
+	const struct macho_section* nonLazySection = NULL;
 	for (uint32_t i = 0; i < cmd_count; ++i) {
 		switch (cmd->cmd) {
 			case LC_SEGMENT_COMMAND:
@@ -152,19 +119,9 @@
 					const struct macho_section* const sectionsEnd = &sectionsStart[seg->nsects];
 					for (const struct macho_section* sect=sectionsStart; sect < sectionsEnd; ++sect) {
 						const uint8_t type = sect->flags & SECTION_TYPE;
-						if ( type == S_NON_LAZY_SYMBOL_POINTERS ) {
-							// rebase non-lazy pointers (which all point internal to dyld, since dyld uses no shared libraries)
-							const uint32_t pointerCount = (uint32_t)(sect->size / sizeof(uintptr_t));
-							uintptr_t* const symbolPointers = (uintptr_t*)(sect->addr + slide);
-							for (uint32_t j=0; j < pointerCount; ++j) {
-								symbolPointers[j] += slide;
-							}
-						}
+						if ( type == S_NON_LAZY_SYMBOL_POINTERS ) 
+							nonLazySection = sect;
 					}
-#if __x86_64__
-					if ( (firstWritableSeg == NULL) && (seg->initprot & VM_PROT_WRITE) )
-						firstWritableSeg = seg;
-#endif
 				}
 				break;
 			case LC_DYSYMTAB:
@@ -175,47 +132,105 @@
 	}
 	
 	// 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 ( reloc->r_length != RELOC_SIZE ) 
-			throw "relocation in dyld has wrong size";
-
-		if ( reloc->r_type != POINTER_RELOC ) 
-			throw "relocation in dyld has wrong type";
-		
-		// update pointer by amount dyld slid
-		*((uintptr_t*)(reloc->r_address + relocBase)) += slide;
-	}
-}
-
-
+		if ( (reloc->r_address & R_SCATTERED) == 0 ) {
+			if (reloc->r_length == RELOC_SIZE) {
+				switch(reloc->r_type) {
+					case GENERIC_RELOC_VANILLA:
+						*((uintptr_t*)(reloc->r_address + relocBase)) += slide;
+						break;
+				}
+			}
+		}
+		else {
+			const struct scattered_relocation_info* sreloc = (struct scattered_relocation_info*)reloc;
+			if (sreloc->r_length == RELOC_SIZE) {
+				uintptr_t* locationToFix = (uintptr_t*)(sreloc->r_address + relocBase);
+				switch(sreloc->r_type) {
+					case GENERIC_RELOC_VANILLA:
+		#if __ppc__ || __ppc64__
+					case PPC_RELOC_PB_LA_PTR:
+		#elif __i386__
+					case GENERIC_RELOC_PB_LA_PTR:
+		#endif
+					// Note the use of PB_LA_PTR is unique here.  Seems like ld should strip out all lazy pointers
+					// but it does not.  But, since all lazy-pointers point within dyld, they can be slid too
+						*locationToFix += slide;
+						break;
+				}
+			}
+		}
+	}
+	
+	// rebase non-lazy pointers (which all point internal to dyld, since dyld uses no shared libraries)
+	if ( nonLazySection != NULL ) {
+		const uint32_t pointerCount = nonLazySection->size / sizeof(uintptr_t);
+		uintptr_t* const symbolPointers = (uintptr_t*)(nonLazySection->addr + slide);
+		for (uint32_t j=0; j < pointerCount; ++j) {
+			symbolPointers[j] += slide;
+		}
+	}
+	
+	
+}
+
+//
+// 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();
-extern "C" void __guard_setup(const char* apple[]);
-
 
 //
 //  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);
 	}
-
+	
+	// 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];
 	
@@ -224,55 +239,14 @@
 	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(dyldsMachHeader, slide, argc, argv, envp, apple);
-#endif
-
+	
 	// 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);
-}
-
-
-#if TARGET_IPHONE_SIMULATOR
-
-extern "C" uintptr_t start_sim(int argc, const char* argv[], const char* envp[], const char* apple[],
-							const macho_header* mainExecutableMH, const macho_header* 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 macho_header* mainExecutableMH, const macho_header* dyldMH, uintptr_t dyldSlide,
-					const dyld::SyscallHelpers* sc, uintptr_t* startGlue)
-{
-	// if simulator dyld loaded slid, it needs to rebase itself
-	// we have to do this before using any global variables
-	if ( dyldSlide != 0 ) {
-		rebaseDyld(dyldMH, dyldSlide);
-	}
-
-	// save table of syscall pointers
-	gSyscallHelpers = sc;
-	
-	// allow dyld to use mach messaging
-	mach_init();
-
-	// 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 = slideOfMainExecutable(mainExecutableMH);
-	return dyld::_main(mainExecutableMH, appsSlide, argc, argv, envp, apple, startGlue);
-}
-#endif
+	return dyld::_main(appsMachHeader, argc, argv, envp, apple);
+}
+
+
 
 
 } // end of namespace