1/* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*- 2 * 3 * Copyright (c) 2004-2009 Apple Inc. All rights reserved. 4 * 5 * @APPLE_LICENSE_HEADER_START@ 6 * 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 12 * file. 13 * 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. 21 * 22 * @APPLE_LICENSE_HEADER_END@ 23 */ 24 25#include <stddef.h> 26#include <stdint.h> 27#include <stdlib.h> 28#include <string.h> 29#include <mach-o/loader.h> 30 31#include <vector> 32 33#include "mach-o/dyld_gdb.h" 34#include "mach-o/dyld_images.h" 35#include "ImageLoader.h" 36#include "dyld.h" 37 38#if __IPHONE_OS_VERSION_MIN_REQUIRED 39 #define INITIAL_UUID_IMAGE_COUNT 4 40#else 41 #define INITIAL_UUID_IMAGE_COUNT 32 42#endif 43 44VECTOR_NEVER_DESTRUCTED(dyld_image_info); 45VECTOR_NEVER_DESTRUCTED(dyld_uuid_info); 46 47static std::vector<dyld_image_info> sImageInfos; 48static std::vector<dyld_uuid_info> sImageUUIDs; 49 50 51void addImagesToAllImages(uint32_t infoCount, const dyld_image_info info[]) 52{ 53 // make initial size large enough that we probably won't need to re-alloc it 54 if ( sImageInfos.size() == 0 ) 55 sImageInfos.reserve(INITIAL_IMAGE_COUNT); 56 if ( sImageUUIDs.capacity() == 0 ) 57 sImageUUIDs.reserve(4); 58 // set infoArray to NULL to denote it is in-use 59 dyld::gProcessInfo->infoArray = NULL; 60 61 // append all new images 62 for (uint32_t i=0; i < infoCount; ++i) 63 sImageInfos.push_back(info[i]); 64 dyld::gProcessInfo->infoArrayCount = sImageInfos.size(); 65 66 // set infoArray back to base address of vector (other process can now read) 67 dyld::gProcessInfo->infoArray = &sImageInfos[0]; 68} 69 70#if TARGET_IPHONE_SIMULATOR 71// called once in dyld_sim start up to copy image list from host dyld to sImageInfos 72void syncProcessInfo() 73{ 74 // may want to set version field of gProcessInfo if it might be different than host 75 if ( sImageInfos.size() == 0 ) { 76 sImageInfos.reserve(INITIAL_IMAGE_COUNT); 77 if ( dyld::gProcessInfo->infoArray != NULL ) { 78 for (uint32_t i=0; i < dyld::gProcessInfo->infoArrayCount; ++i) { 79 sImageInfos.push_back(dyld::gProcessInfo->infoArray[i]); 80 } 81 dyld::gProcessInfo->infoArray = &sImageInfos[0]; 82 dyld::gProcessInfo->infoArrayCount = sImageInfos.size(); 83 } 84 } 85 dyld::gProcessInfo->notification(dyld_image_info_change, 0, NULL); 86} 87#endif 88 89const char* notifyGDB(enum dyld_image_states state, uint32_t infoCount, const dyld_image_info info[]) 90{ 91 // tell gdb that about the new images 92 dyld::gProcessInfo->notification(dyld_image_adding, infoCount, info); 93 // <rdar://problem/7739489> record initial count of images 94 // so CrashReporter can note which images were dynamically loaded 95 if ( dyld::gProcessInfo->initialImageCount == 0 ) 96 dyld::gProcessInfo->initialImageCount = infoCount; 97 return NULL; 98} 99 100 101 102void addNonSharedCacheImageUUID(const dyld_uuid_info& info) 103{ 104 // set uuidArray to NULL to denote it is in-use 105 dyld::gProcessInfo->uuidArray = NULL; 106 107 // append all new images 108 sImageUUIDs.push_back(info); 109 dyld::gProcessInfo->uuidArrayCount = sImageUUIDs.size(); 110 111 // set uuidArray back to base address of vector (other process can now read) 112 dyld::gProcessInfo->uuidArray = &sImageUUIDs[0]; 113} 114 115void removeImageFromAllImages(const struct mach_header* loadAddress) 116{ 117 dyld_image_info goingAway; 118 119 // set infoArray to NULL to denote it is in-use 120 dyld::gProcessInfo->infoArray = NULL; 121 122 // remove image from infoArray 123 for (std::vector<dyld_image_info>::iterator it=sImageInfos.begin(); it != sImageInfos.end(); it++) { 124 if ( it->imageLoadAddress == loadAddress ) { 125 goingAway = *it; 126 sImageInfos.erase(it); 127 break; 128 } 129 } 130 dyld::gProcessInfo->infoArrayCount = sImageInfos.size(); 131 132 // set infoArray back to base address of vector 133 dyld::gProcessInfo->infoArray = &sImageInfos[0]; 134 135 136 // set uuidArrayCount to NULL to denote it is in-use 137 dyld::gProcessInfo->uuidArray = NULL; 138 139 // remove image from infoArray 140 for (std::vector<dyld_uuid_info>::iterator it=sImageUUIDs.begin(); it != sImageUUIDs.end(); it++) { 141 if ( it->imageLoadAddress == loadAddress ) { 142 sImageUUIDs.erase(it); 143 break; 144 } 145 } 146 dyld::gProcessInfo->uuidArrayCount = sImageUUIDs.size(); 147 148 // set infoArray back to base address of vector 149 dyld::gProcessInfo->uuidArray = &sImageUUIDs[0]; 150 151 // tell gdb that about the new images 152 dyld::gProcessInfo->notification(dyld_image_removing, 1, &goingAway); 153} 154 155void setAlImageInfosHalt(const char* message, uintptr_t flags) 156{ 157 dyld::gProcessInfo->errorMessage = message; 158 dyld::gProcessInfo->terminationFlags = flags; 159} 160 161 162#if TARGET_IPHONE_SIMULATOR 163 namespace dyld { 164 struct dyld_all_image_infos* gProcessInfo = NULL; 165 } 166#else 167 168 #if __arm__ 169 // work around for: <rdar://problem/6530727> gdb-1109: notifier in dyld does not work if it is in thumb 170 extern "C" void gdb_image_notifier(enum dyld_image_mode mode, uint32_t infoCount, const dyld_image_info info[]); 171 #else 172 static void gdb_image_notifier(enum dyld_image_mode mode, uint32_t infoCount, const dyld_image_info info[]) 173 { 174 // do nothing 175 // gdb sets a break point here to catch notifications 176 //dyld::log("dyld: gdb_image_notifier(%s, %d, ...)\n", mode ? "dyld_image_removing" : "dyld_image_adding", infoCount); 177 //for (uint32_t i=0; i < infoCount; ++i) 178 // dyld::log("dyld: %d loading at %p %s\n", i, info[i].imageLoadAddress, info[i].imageFilePath); 179 //for (uint32_t i=0; i < dyld::gProcessInfo->infoArrayCount; ++i) 180 // dyld::log("dyld: %d loading at %p %s\n", i, dyld::gProcessInfo->infoArray[i].imageLoadAddress, dyld::gProcessInfo->infoArray[i].imageFilePath); 181 } 182 #endif 183 184 extern void* __dso_handle; 185 #define STR(s) # s 186 #define XSTR(s) STR(s) 187 188 struct dyld_all_image_infos dyld_all_image_infos __attribute__ ((section ("__DATA,__all_image_info"))) 189 = { 190 14, 0, NULL, &gdb_image_notifier, false, false, (const mach_header*)&__dso_handle, NULL, 191 XSTR(DYLD_VERSION), NULL, 0, NULL, 0, 0, NULL, &dyld_all_image_infos, 192 0, dyld_error_kind_none, NULL, NULL, NULL, 0, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,}, 0 193 }; 194 195 struct dyld_shared_cache_ranges dyld_shared_cache_ranges; 196 197 namespace dyld { 198 struct dyld_all_image_infos* gProcessInfo = &dyld_all_image_infos; 199 } 200#endif 201 202