Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | #include <AvailabilityMacros.h> |
michael@0 | 7 | #include <mach-o/loader.h> |
michael@0 | 8 | #include <mach-o/dyld_images.h> |
michael@0 | 9 | #include <mach/task_info.h> |
michael@0 | 10 | #include <mach/task.h> |
michael@0 | 11 | #include <mach/mach_init.h> |
michael@0 | 12 | #include <mach/mach_traps.h> |
michael@0 | 13 | #include <string.h> |
michael@0 | 14 | #include <stdlib.h> |
michael@0 | 15 | #include <vector> |
michael@0 | 16 | #include <sstream> |
michael@0 | 17 | |
michael@0 | 18 | #include "shared-libraries.h" |
michael@0 | 19 | |
michael@0 | 20 | #ifndef MAC_OS_X_VERSION_10_6 |
michael@0 | 21 | #define MAC_OS_X_VERSION_10_6 1060 |
michael@0 | 22 | #endif |
michael@0 | 23 | |
michael@0 | 24 | #if MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_6 |
michael@0 | 25 | // borrowed from Breakpad |
michael@0 | 26 | // Fallback declarations for TASK_DYLD_INFO and friends, introduced in |
michael@0 | 27 | // <mach/task_info.h> in the Mac OS X 10.6 SDK. |
michael@0 | 28 | #define TASK_DYLD_INFO 17 |
michael@0 | 29 | struct task_dyld_info { |
michael@0 | 30 | mach_vm_address_t all_image_info_addr; |
michael@0 | 31 | mach_vm_size_t all_image_info_size; |
michael@0 | 32 | }; |
michael@0 | 33 | typedef struct task_dyld_info task_dyld_info_data_t; |
michael@0 | 34 | typedef struct task_dyld_info *task_dyld_info_t; |
michael@0 | 35 | #define TASK_DYLD_INFO_COUNT (sizeof(task_dyld_info_data_t) / sizeof(natural_t)) |
michael@0 | 36 | |
michael@0 | 37 | #endif |
michael@0 | 38 | |
michael@0 | 39 | // Architecture specific abstraction. |
michael@0 | 40 | #ifdef __i386__ |
michael@0 | 41 | typedef mach_header platform_mach_header; |
michael@0 | 42 | typedef segment_command mach_segment_command_type; |
michael@0 | 43 | #define MACHO_MAGIC_NUMBER MH_MAGIC |
michael@0 | 44 | #define CMD_SEGMENT LC_SEGMENT |
michael@0 | 45 | #define seg_size uint32_t |
michael@0 | 46 | #else |
michael@0 | 47 | typedef mach_header_64 platform_mach_header; |
michael@0 | 48 | typedef segment_command_64 mach_segment_command_type; |
michael@0 | 49 | #define MACHO_MAGIC_NUMBER MH_MAGIC_64 |
michael@0 | 50 | #define CMD_SEGMENT LC_SEGMENT_64 |
michael@0 | 51 | #define seg_size uint64_t |
michael@0 | 52 | #endif |
michael@0 | 53 | |
michael@0 | 54 | static |
michael@0 | 55 | void addSharedLibrary(const platform_mach_header* header, char *name, SharedLibraryInfo &info) { |
michael@0 | 56 | const struct load_command *cmd = |
michael@0 | 57 | reinterpret_cast<const struct load_command *>(header + 1); |
michael@0 | 58 | |
michael@0 | 59 | seg_size size = 0; |
michael@0 | 60 | unsigned long long start = reinterpret_cast<unsigned long long>(header); |
michael@0 | 61 | // Find the cmd segment in the macho image. It will contain the offset we care about. |
michael@0 | 62 | const uint8_t *uuid_bytes = nullptr; |
michael@0 | 63 | for (unsigned int i = 0; |
michael@0 | 64 | cmd && (i < header->ncmds) && (uuid_bytes == nullptr || size == 0); |
michael@0 | 65 | ++i) { |
michael@0 | 66 | if (cmd->cmd == CMD_SEGMENT) { |
michael@0 | 67 | const mach_segment_command_type *seg = |
michael@0 | 68 | reinterpret_cast<const mach_segment_command_type *>(cmd); |
michael@0 | 69 | |
michael@0 | 70 | if (!strcmp(seg->segname, "__TEXT")) { |
michael@0 | 71 | size = seg->vmsize; |
michael@0 | 72 | } |
michael@0 | 73 | } else if (cmd->cmd == LC_UUID) { |
michael@0 | 74 | const uuid_command *ucmd = reinterpret_cast<const uuid_command *>(cmd); |
michael@0 | 75 | uuid_bytes = ucmd->uuid; |
michael@0 | 76 | } |
michael@0 | 77 | |
michael@0 | 78 | cmd = reinterpret_cast<const struct load_command *> |
michael@0 | 79 | (reinterpret_cast<const char *>(cmd) + cmd->cmdsize); |
michael@0 | 80 | } |
michael@0 | 81 | |
michael@0 | 82 | std::stringstream uuid; |
michael@0 | 83 | uuid << std::hex << std::uppercase; |
michael@0 | 84 | if (uuid_bytes != nullptr) { |
michael@0 | 85 | for (int i = 0; i < 16; ++i) { |
michael@0 | 86 | uuid << ((uuid_bytes[i] & 0xf0) >> 4); |
michael@0 | 87 | uuid << (uuid_bytes[i] & 0xf); |
michael@0 | 88 | } |
michael@0 | 89 | uuid << '0'; |
michael@0 | 90 | } |
michael@0 | 91 | |
michael@0 | 92 | info.AddSharedLibrary(SharedLibrary(start, start + size, 0, uuid.str(), |
michael@0 | 93 | name)); |
michael@0 | 94 | } |
michael@0 | 95 | |
michael@0 | 96 | // Use dyld to inspect the macho image information. We can build the SharedLibraryEntry structure |
michael@0 | 97 | // giving us roughtly the same info as /proc/PID/maps in Linux. |
michael@0 | 98 | SharedLibraryInfo SharedLibraryInfo::GetInfoForSelf() |
michael@0 | 99 | { |
michael@0 | 100 | SharedLibraryInfo sharedLibraryInfo; |
michael@0 | 101 | |
michael@0 | 102 | task_dyld_info_data_t task_dyld_info; |
michael@0 | 103 | mach_msg_type_number_t count = TASK_DYLD_INFO_COUNT; |
michael@0 | 104 | if (task_info(mach_task_self (), TASK_DYLD_INFO, (task_info_t)&task_dyld_info, |
michael@0 | 105 | &count) != KERN_SUCCESS) { |
michael@0 | 106 | return sharedLibraryInfo; |
michael@0 | 107 | } |
michael@0 | 108 | |
michael@0 | 109 | struct dyld_all_image_infos* aii = (struct dyld_all_image_infos*)task_dyld_info.all_image_info_addr; |
michael@0 | 110 | size_t infoCount = aii->infoArrayCount; |
michael@0 | 111 | |
michael@0 | 112 | // Iterate through all dyld images (loaded libraries) to get their names |
michael@0 | 113 | // and offests. |
michael@0 | 114 | for (size_t i = 0; i < infoCount; ++i) { |
michael@0 | 115 | const dyld_image_info *info = &aii->infoArray[i]; |
michael@0 | 116 | |
michael@0 | 117 | // If the magic number doesn't match then go no further |
michael@0 | 118 | // since we're not pointing to where we think we are. |
michael@0 | 119 | if (info->imageLoadAddress->magic != MACHO_MAGIC_NUMBER) { |
michael@0 | 120 | continue; |
michael@0 | 121 | } |
michael@0 | 122 | |
michael@0 | 123 | const platform_mach_header* header = |
michael@0 | 124 | reinterpret_cast<const platform_mach_header*>(info->imageLoadAddress); |
michael@0 | 125 | |
michael@0 | 126 | // Add the entry for this image. |
michael@0 | 127 | addSharedLibrary(header, (char*)info->imageFilePath, sharedLibraryInfo); |
michael@0 | 128 | |
michael@0 | 129 | } |
michael@0 | 130 | return sharedLibraryInfo; |
michael@0 | 131 | } |
michael@0 | 132 |