Loading...
launch-cache/dsc_extractor.cpp dyld-239.3 dyld-195.5
--- dyld/dyld-239.3/launch-cache/dsc_extractor.cpp
+++ dyld/dyld-195.5/launch-cache/dsc_extractor.cpp
@@ -1,6 +1,6 @@
 /* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*- 
  *
- * Copyright (c) 2011 Apple Inc. All rights reserved.
+ * Copyright (c) 2010 Apple Inc. All rights reserved.
  *
  * @APPLE_LICENSE_HEADER_START@
  * 
@@ -41,14 +41,13 @@
 #define NO_ULEB 
 #include "Architectures.hpp"
 #include "MachOFileAbstraction.hpp"
-#include "CacheFileAbstraction.hpp"
 
 #include "dsc_iterator.h"
 #include "dsc_extractor.h"
 
 #include <vector>
 #include <map>
-#include <unordered_map>
+#include <ext/hash_map>
 #include <algorithm>
 #include <dispatch/dispatch.h>
 
@@ -61,24 +60,15 @@
 	uint64_t	sizem;
 };
 
-class CStringHash {
-public:
-	size_t operator()(const char* __s) const {
-		size_t __h = 0;
-		for ( ; *__s; ++__s)
-			__h = 5 * __h + *__s;
-		return __h;
-	};
-};
 class CStringEquals {
 public:
 	bool operator()(const char* left, const char* right) const { return (strcmp(left, right) == 0); }
 };
-typedef std::unordered_map<const char*, std::vector<seg_info>, CStringHash, CStringEquals> NameToSegments;
+typedef __gnu_cxx::hash_map<const char*, std::vector<seg_info>, __gnu_cxx::hash<const char*>, CStringEquals> NameToSegments;
 
 
 template <typename A>
-int optimize_linkedit(macho_header<typename A::P>* mh, uint32_t textOffsetInCache, const void* mapped_cache, uint64_t* newSize) 
+int optimize_linkedit(macho_header<typename A::P>* mh, const void* mapped_cache, uint64_t* newSize) 
 {
 	typedef typename A::P P;
 	typedef typename A::P::E E;
@@ -95,8 +85,6 @@
 	macho_segment_command<P>* linkEditSegCmd = NULL;
 	macho_symtab_command<P>* symtab = NULL;
 	macho_dysymtab_command<P>*	dynamicSymTab = NULL;
-	macho_linkedit_data_command<P>*	functionStarts = NULL;
-	macho_linkedit_data_command<P>*	dataInCode = NULL;
 	for (uint32_t i = 0; i < cmd_count; ++i) {
 		if ( cmd->cmd() == macho_segment_command<P>::CMD ) {
 			// update segment/section file offsets
@@ -133,12 +121,6 @@
 		else if ( cmd->cmd() == LC_DYSYMTAB ) {
 			dynamicSymTab = (macho_dysymtab_command<P>*)cmd;
 		}
-		else if ( cmd->cmd() == LC_FUNCTION_STARTS ) {
-			functionStarts = (macho_linkedit_data_command<P>*)cmd;
-		}
-		else if ( cmd->cmd() == LC_DATA_IN_CODE ) {
-			dataInCode = (macho_linkedit_data_command<P>*)cmd;
-		}
 		cmd = (const macho_load_command<P>*)(((uint8_t*)cmd)+cmd->cmdsize());
 	}
 	
@@ -155,118 +137,34 @@
 		fprintf(stderr, "LC_DYSYMTAB not found\n");
 		return -1;
 	}
-
-	const uint32_t newFunctionStartsOffset = linkEditSegCmd->fileoff();
-	uint32_t functionStartsSize = 0;
-	if ( functionStarts != NULL ) {
-		// copy function starts from original cache file to new mapped dylib file
-		functionStartsSize = functionStarts->datasize();
-		memcpy((char*)mh + newFunctionStartsOffset, (char*)mapped_cache + functionStarts->dataoff(), functionStartsSize);
-	}
-	const uint32_t newDataInCodeOffset = (newFunctionStartsOffset + functionStartsSize + sizeof(pint_t) - 1) & (-sizeof(pint_t)); // pointer align
-	uint32_t dataInCodeSize = 0;
-	if ( dataInCode != NULL ) {
-		// copy data-in-code info from original cache file to new mapped dylib file
-		dataInCodeSize = dataInCode->datasize();
-		memcpy((char*)mh + newDataInCodeOffset, (char*)mapped_cache + dataInCode->dataoff(), dataInCodeSize);
-	}
-	
-	// look for local symbol info in unmapped part of shared cache
-	dyldCacheHeader<E>* header = (dyldCacheHeader<E>*)mapped_cache;
-	macho_nlist<P>* localNlists = NULL;
-	uint32_t localNlistCount = 0;
-	const char* localStrings = NULL;
-	if ( header->mappingOffset() > offsetof(dyld_cache_header,localSymbolsSize) ) {
-		dyldCacheLocalSymbolsInfo<E>* localInfo = (dyldCacheLocalSymbolsInfo<E>*)(((uint8_t*)mapped_cache) + header->localSymbolsOffset());
-		dyldCacheLocalSymbolEntry<E>* entries = (dyldCacheLocalSymbolEntry<E>*)(((uint8_t*)mapped_cache) + header->localSymbolsOffset() + localInfo->entriesOffset());
-		macho_nlist<P>* allLocalNlists = (macho_nlist<P>*)(((uint8_t*)localInfo) + localInfo->nlistOffset());
-		const uint32_t entriesCount = localInfo->entriesCount();
-		for (uint32_t i=0; i < entriesCount; ++i) {
-			if ( entries[i].dylibOffset() == textOffsetInCache ) {
-				uint32_t localNlistStart = entries[i].nlistStartIndex();
-				localNlistCount = entries[i].nlistCount();
-				localNlists = &allLocalNlists[localNlistStart];
-				localStrings = ((char*)localInfo) + localInfo->stringsOffset();
-				break;
-			}
-		}
-	}
-	
-	// compute number of symbols in new symbol table
-	const macho_nlist<P>* const mergedSymTabStart = (macho_nlist<P>*)(((uint8_t*)mapped_cache) + symtab->symoff());
-	const macho_nlist<P>* const mergedSymTabend = &mergedSymTabStart[symtab->nsyms()];
-	uint32_t newSymCount = symtab->nsyms();
-	if ( localNlists != NULL ) {
-		newSymCount = localNlistCount;
-		for (const macho_nlist<P>* s = mergedSymTabStart; s != mergedSymTabend; ++s) {
-			// skip any locals in cache
-			if ( (s->n_type() & (N_TYPE|N_EXT)) == N_SECT ) 
-				continue;
-			++newSymCount;
-		}
-	}
-	
 	// copy symbol entries and strings from original cache file to new mapped dylib file
-	const uint32_t newSymTabOffset = (newDataInCodeOffset + dataInCodeSize + sizeof(pint_t) - 1) & (-sizeof(pint_t)); // pointer align
-	const uint32_t newIndSymTabOffset = newSymTabOffset + newSymCount*sizeof(macho_nlist<P>);
+	const uint32_t newSymTabOffset = linkEditSegCmd->fileoff();
+	const uint32_t newIndSymTabOffset = newSymTabOffset + symtab->nsyms()*sizeof(macho_nlist<P>);
 	const uint32_t newStringPoolOffset = newIndSymTabOffset + dynamicSymTab->nindirectsyms()*sizeof(uint32_t);
 	macho_nlist<P>* const newSymTabStart = (macho_nlist<P>*)(((uint8_t*)mh) + newSymTabOffset);
 	char* const newStringPoolStart = (char*)mh + newStringPoolOffset;
+	uint32_t* newIndSymTab = (uint32_t*)((char*)mh + newIndSymTabOffset);
 	const uint32_t* mergedIndSymTab = (uint32_t*)((char*)mapped_cache + dynamicSymTab->indirectsymoff());
+	const macho_nlist<P>* const mergedSymTabStart = (macho_nlist<P>*)(((uint8_t*)mapped_cache) + symtab->symoff());
+	const macho_nlist<P>* const mergedSymTabend = &mergedSymTabStart[symtab->nsyms()];
 	const char* mergedStringPoolStart = (char*)mapped_cache + symtab->stroff();
 	macho_nlist<P>* t = newSymTabStart;
 	int poolOffset = 0;
-	uint32_t symbolsCopied = 0;
 	newStringPoolStart[poolOffset++] = '\0'; // first pool entry is always empty string
 	for (const macho_nlist<P>* s = mergedSymTabStart; s != mergedSymTabend; ++s) {
-		// if we have better local symbol info, skip any locals here
-		if ( (localNlists != NULL) && ((s->n_type() & (N_TYPE|N_EXT)) == N_SECT) ) 
-			continue;
 		*t = *s;
 		t->set_n_strx(poolOffset);
 		strcpy(&newStringPoolStart[poolOffset], &mergedStringPoolStart[s->n_strx()]);
 		poolOffset += (strlen(&newStringPoolStart[poolOffset]) + 1);
 		++t;
-		++symbolsCopied;
-	}
-	if ( localNlists != NULL ) {
-		// update load command to reflect new count of locals
-		dynamicSymTab->set_ilocalsym(symbolsCopied);
-		dynamicSymTab->set_nlocalsym(localNlistCount);
-		// copy local symbols
-		for (uint32_t i=0; i < localNlistCount; ++i) {
-			const char* localName = &localStrings[localNlists[i].n_strx()];
-			*t = localNlists[i];
-			t->set_n_strx(poolOffset);
-			strcpy(&newStringPoolStart[poolOffset], localName);
-			poolOffset += (strlen(localName) + 1);
-			++t;
-			++symbolsCopied;
-		}
-	}
-	
-	if ( newSymCount != symbolsCopied ) {
-		fprintf(stderr, "symbol count miscalculation\n");
-		return -1;
-	}
-	
+	}
 	// pointer align string pool size
 	while ( (poolOffset % sizeof(pint_t)) != 0 )
 		++poolOffset; 
 	// copy indirect symbol table
-	uint32_t* newIndSymTab = (uint32_t*)((char*)mh + newIndSymTabOffset);
 	memcpy(newIndSymTab, mergedIndSymTab, dynamicSymTab->nindirectsyms()*sizeof(uint32_t));
 	
 	// update load commands
-	if ( functionStarts != NULL ) {
-		functionStarts->set_dataoff(newFunctionStartsOffset);
-		functionStarts->set_datasize(functionStartsSize);
-	}
-	if ( dataInCode != NULL ) {
-		dataInCode->set_dataoff(newDataInCodeOffset);
-		dataInCode->set_datasize(dataInCodeSize);
-	}
-	symtab->set_nsyms(symbolsCopied);
 	symtab->set_symoff(newSymTabOffset);
 	symtab->set_stroff(newStringPoolOffset);
 	symtab->set_strsize(poolOffset);
@@ -297,7 +195,7 @@
 	lastSlash[1] = '\0';
 	struct stat stat_buf;
 	if ( stat(dirs, &stat_buf) != 0 ) {
-		char* afterSlash = &dirs[1];
+		const char* afterSlash = &dirs[1];
 		char* slash;
 		while ( (slash = strchr(afterSlash, '/')) != NULL ) {
 			*slash = '\0';
@@ -325,7 +223,7 @@
     uint32_t                nfat_archs          = 0;
 	uint32_t                offsetInFatFile     = 4096;
     uint8_t                 *base_ptr           = &dylib_data.front();
-	    
+    
 #define FH reinterpret_cast<fat_header*>(base_ptr)
 #define FA reinterpret_cast<fat_arch*>(base_ptr + (8 + (nfat_archs - 1) * sizeof(fat_arch)))
     
@@ -349,12 +247,11 @@
     
 	// Write regular segments into the buffer
 	uint32_t                totalSize           = 0;
-    uint32_t				textOffsetInCache	= 0;
+    
 	for( std::vector<seg_info>::const_iterator it=segments.begin(); it != segments.end(); ++it) {
         
         if(strcmp(it->segName, "__TEXT") == 0 ) {
-			textOffsetInCache					= it->offset;
-            const macho_header<P>   *textMH     = reinterpret_cast<macho_header<P>*>((uint8_t*)mapped_cache+textOffsetInCache);
+            const macho_header<P>   *textMH     = reinterpret_cast<macho_header<P>*>((uint8_t*)mapped_cache+it->offset);
             FA->cputype                         = OSSwapHostToBigInt32(textMH->cputype()); 
             FA->cpusubtype                      = OSSwapHostToBigInt32(textMH->cpusubtype());
             
@@ -381,7 +278,7 @@
     
 	// optimize linkedit
 	uint64_t                newSize             = dylib_data.size();
-	optimize_linkedit<A>(((macho_header<P>*)(base_ptr+offsetInFatFile)), textOffsetInCache, mapped_cache, &newSize);
+	optimize_linkedit<A>(((macho_header<P>*)(base_ptr+offsetInFatFile)), mapped_cache, &newSize);
 	
 	// update fat header with new file size
     dylib_data.resize(offsetInFatFile+newSize);
@@ -393,7 +290,7 @@
 } 
 
 
-int dyld_shared_cache_extract_dylibs_progress(const char* shared_cache_file_path, const char* extraction_root_path,
+extern int dyld_shared_cache_extract_dylibs_progress(const char* shared_cache_file_path, const char* extraction_root_path,
 													void (^progress)(unsigned current, unsigned total))
 {
 	struct stat statbuf;
@@ -415,21 +312,21 @@
 	}
     
     close(cache_fd);
-
+    
 	// instantiate arch specific dylib maker
     size_t (*dylib_create_func)(const void*, std::vector<uint8_t>&, const std::vector<seg_info>&) = NULL;
 	     if ( strcmp((char*)mapped_cache, "dyld_v1    i386") == 0 ) 
 		dylib_create_func = dylib_maker<x86>;
 	else if ( strcmp((char*)mapped_cache, "dyld_v1  x86_64") == 0 ) 
 		dylib_create_func = dylib_maker<x86_64>;
+	else if ( strcmp((char*)mapped_cache, "dyld_v1     ppc") == 0 ) 
+		dylib_create_func = dylib_maker<ppc>;
 	else if ( strcmp((char*)mapped_cache, "dyld_v1   armv5") == 0 ) 
 		dylib_create_func = dylib_maker<arm>;
 	else if ( strcmp((char*)mapped_cache, "dyld_v1   armv6") == 0 ) 
 		dylib_create_func = dylib_maker<arm>;
 	else if ( strcmp((char*)mapped_cache, "dyld_v1   armv7") == 0 ) 
 		dylib_create_func = dylib_maker<arm>;
-	else if ( strncmp((char*)mapped_cache, "dyld_v1  armv7", 14) == 0 ) 
-		dylib_create_func = dylib_maker<arm>;
 	else {
 		fprintf(stderr, "Error: unrecognized dyld shared cache magic.\n");
         munmap(mapped_cache, statbuf.st_size);
@@ -437,20 +334,16 @@
 	}
 
 	// iterate through all images in cache and build map of dylibs and segments
-	__block NameToSegments  map;
-	__block int				result              = dyld_shared_cache_iterate(mapped_cache, statbuf.st_size, ^(const dyld_shared_cache_dylib_info* dylibInfo, const dyld_shared_cache_segment_info* segInfo) {
-        map[dylibInfo->path].push_back(seg_info(segInfo->name, segInfo->fileOffset, segInfo->fileSize));
-    });
-
-    if(result != 0) {
-		fprintf(stderr, "Error: dyld_shared_cache_iterate_segments_with_slide failed.\n");
-        munmap(mapped_cache, statbuf.st_size);
-		return result;
-    }
+	__block NameToSegments map;
+	dyld_shared_cache_iterate_segments_with_slide(mapped_cache, 
+		^(const char* dylib, const char* segName, uint64_t offset, uint64_t sizem, 
+							uint64_t mappedddress, uint64_t slide) {
+			map[dylib].push_back(seg_info(segName, offset, sizem));
+		});
 
 	// for each dylib instantiate a dylib file
     dispatch_group_t        group               = dispatch_group_create();
-    dispatch_semaphore_t    sema                = dispatch_semaphore_create(2);
+    dispatch_semaphore_t    sema                = dispatch_semaphore_create(4);
     dispatch_queue_t        process_queue       = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0);
     dispatch_queue_t        writer_queue        = dispatch_queue_create("dyld writer queue", 0);
     
@@ -458,15 +351,15 @@
 	__block unsigned        count               = 0;
     
 	for ( NameToSegments::iterator it = map.begin(); it != map.end(); ++it) {
-        dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
         dispatch_group_async(group, process_queue, ^{
+            dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
             
             char    dylib_path[PATH_MAX];
             strcpy(dylib_path, extraction_root_path);
             strcat(dylib_path, "/");
             strcat(dylib_path, it->first);
             
-            //printf("%s with %lu segments\n", dylib_path, it->second.size());
+            //printf("%s with %lu segments\n", dylib_path, segments.size());
             // make sure all directories in this path exist
             make_dirs(dylib_path);
             
@@ -533,43 +426,18 @@
 
 
 #if 0 
-// test program
-#include <stdio.h>
-#include <stddef.h>
-#include <dlfcn.h>
-
-
-typedef int (*extractor_proc)(const char* shared_cache_file_path, const char* extraction_root_path,
-													void (^progress)(unsigned current, unsigned total));
-
 int main(int argc, const char* argv[])
 {
 	if ( argc != 3 ) {
 		fprintf(stderr, "usage: dsc_extractor <path-to-cache-file> <path-to-device-dir>\n");
 		return 1;
 	}
-	
-	//void* handle = dlopen("/Volumes/my/src/dyld/build/Debug/dsc_extractor.bundle", RTLD_LAZY);
-	void* handle = dlopen("/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/usr/lib/dsc_extractor.bundle", RTLD_LAZY);
-	if ( handle == NULL ) {
-		fprintf(stderr, "dsc_extractor.bundle could not be loaded\n");
-		return 1;
-	}
-	
-	extractor_proc proc = (extractor_proc)dlsym(handle, "dyld_shared_cache_extract_dylibs_progress");
-	if ( proc == NULL ) {
-		fprintf(stderr, "dsc_extractor.bundle did not have dyld_shared_cache_extract_dylibs_progress symbol\n");
-		return 1;
-	}
-	
-	int result = (*proc)(argv[1], argv[2], ^(unsigned c, unsigned total) { printf("%d/%d\n", c, total); } );
+		
+	int result = dyld_shared_cache_extract_dylibs_progress(argv[1], argv[2], ^(unsigned c, unsigned total) { printf("%d/%d\n", c, total); } );
 	fprintf(stderr, "dyld_shared_cache_extract_dylibs_progress() => %d\n", result);
 	return 0;
 }
-
-
 #endif
 
- 
- 
-
+
+