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: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* vim: set ts=8 sts=2 et sw=2 tw=80: */ |
michael@0 | 3 | |
michael@0 | 4 | // Copyright (c) 2006, 2012, Google Inc. |
michael@0 | 5 | // All rights reserved. |
michael@0 | 6 | // |
michael@0 | 7 | // Redistribution and use in source and binary forms, with or without |
michael@0 | 8 | // modification, are permitted provided that the following conditions are |
michael@0 | 9 | // met: |
michael@0 | 10 | // |
michael@0 | 11 | // * Redistributions of source code must retain the above copyright |
michael@0 | 12 | // notice, this list of conditions and the following disclaimer. |
michael@0 | 13 | // * Redistributions in binary form must reproduce the above |
michael@0 | 14 | // copyright notice, this list of conditions and the following disclaimer |
michael@0 | 15 | // in the documentation and/or other materials provided with the |
michael@0 | 16 | // distribution. |
michael@0 | 17 | // * Neither the name of Google Inc. nor the names of its |
michael@0 | 18 | // contributors may be used to endorse or promote products derived from |
michael@0 | 19 | // this software without specific prior written permission. |
michael@0 | 20 | // |
michael@0 | 21 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
michael@0 | 22 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
michael@0 | 23 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
michael@0 | 24 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
michael@0 | 25 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
michael@0 | 26 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
michael@0 | 27 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
michael@0 | 28 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
michael@0 | 29 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
michael@0 | 30 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
michael@0 | 31 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
michael@0 | 32 | |
michael@0 | 33 | // This file is derived from the following files in |
michael@0 | 34 | // toolkit/crashreporter/google-breakpad: |
michael@0 | 35 | // src/common/android/include/elf.h |
michael@0 | 36 | // src/common/linux/elfutils.h |
michael@0 | 37 | // src/common/linux/file_id.h |
michael@0 | 38 | // src/common/linux/elfutils-inl.h |
michael@0 | 39 | |
michael@0 | 40 | #ifndef LulElfInt_h |
michael@0 | 41 | #define LulElfInt_h |
michael@0 | 42 | |
michael@0 | 43 | // This header defines functions etc internal to the ELF reader. It |
michael@0 | 44 | // should not be included outside of LulElf.cpp. |
michael@0 | 45 | |
michael@0 | 46 | #include <elf.h> |
michael@0 | 47 | #include <stdlib.h> |
michael@0 | 48 | |
michael@0 | 49 | #include "mozilla/Assertions.h" |
michael@0 | 50 | |
michael@0 | 51 | #include "LulPlatformMacros.h" |
michael@0 | 52 | |
michael@0 | 53 | |
michael@0 | 54 | // (derived from) |
michael@0 | 55 | // elfutils.h: Utilities for dealing with ELF files. |
michael@0 | 56 | // |
michael@0 | 57 | |
michael@0 | 58 | #if defined(LUL_OS_android) |
michael@0 | 59 | |
michael@0 | 60 | // From toolkit/crashreporter/google-breakpad/src/common/android/include/elf.h |
michael@0 | 61 | // The Android headers don't always define this constant. |
michael@0 | 62 | #ifndef EM_X86_64 |
michael@0 | 63 | #define EM_X86_64 62 |
michael@0 | 64 | #endif |
michael@0 | 65 | |
michael@0 | 66 | #ifndef EM_PPC64 |
michael@0 | 67 | #define EM_PPC64 21 |
michael@0 | 68 | #endif |
michael@0 | 69 | |
michael@0 | 70 | #ifndef EM_S390 |
michael@0 | 71 | #define EM_S390 22 |
michael@0 | 72 | #endif |
michael@0 | 73 | |
michael@0 | 74 | #ifndef NT_GNU_BUILD_ID |
michael@0 | 75 | #define NT_GNU_BUILD_ID 3 |
michael@0 | 76 | #endif |
michael@0 | 77 | |
michael@0 | 78 | #define ElfW(type) _ElfW (Elf, ELFSIZE, type) |
michael@0 | 79 | #define _ElfW(e,w,t) _ElfW_1 (e, w, _##t) |
michael@0 | 80 | #define _ElfW_1(e,w,t) e##w##t |
michael@0 | 81 | |
michael@0 | 82 | //FIXME |
michael@0 | 83 | extern "C" { |
michael@0 | 84 | extern char* basename(const char* path); |
michael@0 | 85 | }; |
michael@0 | 86 | #else |
michael@0 | 87 | |
michael@0 | 88 | # include <link.h> |
michael@0 | 89 | #endif |
michael@0 | 90 | |
michael@0 | 91 | |
michael@0 | 92 | namespace lul { |
michael@0 | 93 | |
michael@0 | 94 | // Traits classes so consumers can write templatized code to deal |
michael@0 | 95 | // with specific ELF bits. |
michael@0 | 96 | struct ElfClass32 { |
michael@0 | 97 | typedef Elf32_Addr Addr; |
michael@0 | 98 | typedef Elf32_Ehdr Ehdr; |
michael@0 | 99 | typedef Elf32_Nhdr Nhdr; |
michael@0 | 100 | typedef Elf32_Phdr Phdr; |
michael@0 | 101 | typedef Elf32_Shdr Shdr; |
michael@0 | 102 | typedef Elf32_Half Half; |
michael@0 | 103 | typedef Elf32_Off Off; |
michael@0 | 104 | typedef Elf32_Word Word; |
michael@0 | 105 | static const int kClass = ELFCLASS32; |
michael@0 | 106 | static const size_t kAddrSize = sizeof(Elf32_Addr); |
michael@0 | 107 | }; |
michael@0 | 108 | |
michael@0 | 109 | struct ElfClass64 { |
michael@0 | 110 | typedef Elf64_Addr Addr; |
michael@0 | 111 | typedef Elf64_Ehdr Ehdr; |
michael@0 | 112 | typedef Elf64_Nhdr Nhdr; |
michael@0 | 113 | typedef Elf64_Phdr Phdr; |
michael@0 | 114 | typedef Elf64_Shdr Shdr; |
michael@0 | 115 | typedef Elf64_Half Half; |
michael@0 | 116 | typedef Elf64_Off Off; |
michael@0 | 117 | typedef Elf64_Word Word; |
michael@0 | 118 | static const int kClass = ELFCLASS64; |
michael@0 | 119 | static const size_t kAddrSize = sizeof(Elf64_Addr); |
michael@0 | 120 | }; |
michael@0 | 121 | |
michael@0 | 122 | bool IsValidElf(const void* elf_header); |
michael@0 | 123 | int ElfClass(const void* elf_base); |
michael@0 | 124 | |
michael@0 | 125 | // Attempt to find a section named |section_name| of type |section_type| |
michael@0 | 126 | // in the ELF binary data at |elf_mapped_base|. On success, returns true |
michael@0 | 127 | // and sets |*section_start| to point to the start of the section data, |
michael@0 | 128 | // and |*section_size| to the size of the section's data. If |elfclass| |
michael@0 | 129 | // is not NULL, set |*elfclass| to the ELF file class. |
michael@0 | 130 | bool FindElfSection(const void *elf_mapped_base, |
michael@0 | 131 | const char *section_name, |
michael@0 | 132 | uint32_t section_type, |
michael@0 | 133 | const void **section_start, |
michael@0 | 134 | int *section_size, |
michael@0 | 135 | int *elfclass); |
michael@0 | 136 | |
michael@0 | 137 | // Internal helper method, exposed for convenience for callers |
michael@0 | 138 | // that already have more info. |
michael@0 | 139 | template<typename ElfClass> |
michael@0 | 140 | const typename ElfClass::Shdr* |
michael@0 | 141 | FindElfSectionByName(const char* name, |
michael@0 | 142 | typename ElfClass::Word section_type, |
michael@0 | 143 | const typename ElfClass::Shdr* sections, |
michael@0 | 144 | const char* section_names, |
michael@0 | 145 | const char* names_end, |
michael@0 | 146 | int nsection); |
michael@0 | 147 | |
michael@0 | 148 | // Attempt to find the first segment of type |segment_type| in the ELF |
michael@0 | 149 | // binary data at |elf_mapped_base|. On success, returns true and sets |
michael@0 | 150 | // |*segment_start| to point to the start of the segment data, and |
michael@0 | 151 | // and |*segment_size| to the size of the segment's data. If |elfclass| |
michael@0 | 152 | // is not NULL, set |*elfclass| to the ELF file class. |
michael@0 | 153 | bool FindElfSegment(const void *elf_mapped_base, |
michael@0 | 154 | uint32_t segment_type, |
michael@0 | 155 | const void **segment_start, |
michael@0 | 156 | int *segment_size, |
michael@0 | 157 | int *elfclass); |
michael@0 | 158 | |
michael@0 | 159 | // Convert an offset from an Elf header into a pointer to the mapped |
michael@0 | 160 | // address in the current process. Takes an extra template parameter |
michael@0 | 161 | // to specify the return type to avoid having to dynamic_cast the |
michael@0 | 162 | // result. |
michael@0 | 163 | template<typename ElfClass, typename T> |
michael@0 | 164 | const T* |
michael@0 | 165 | GetOffset(const typename ElfClass::Ehdr* elf_header, |
michael@0 | 166 | typename ElfClass::Off offset); |
michael@0 | 167 | |
michael@0 | 168 | |
michael@0 | 169 | // (derived from) |
michael@0 | 170 | // file_id.h: Return a unique identifier for a file |
michael@0 | 171 | // |
michael@0 | 172 | |
michael@0 | 173 | static const size_t kMDGUIDSize = sizeof(MDGUID); |
michael@0 | 174 | |
michael@0 | 175 | class FileID { |
michael@0 | 176 | public: |
michael@0 | 177 | |
michael@0 | 178 | // Load the identifier for the elf file mapped into memory at |base| into |
michael@0 | 179 | // |identifier|. Return false if the identifier could not be created for the |
michael@0 | 180 | // file. |
michael@0 | 181 | static bool ElfFileIdentifierFromMappedFile(const void* base, |
michael@0 | 182 | uint8_t identifier[kMDGUIDSize]); |
michael@0 | 183 | |
michael@0 | 184 | // Convert the |identifier| data to a NULL terminated string. The string will |
michael@0 | 185 | // be formatted as a UUID (e.g., 22F065BB-FC9C-49F7-80FE-26A7CEBD7BCE). |
michael@0 | 186 | // The |buffer| should be at least 37 bytes long to receive all of the data |
michael@0 | 187 | // and termination. Shorter buffers will contain truncated data. |
michael@0 | 188 | static void ConvertIdentifierToString(const uint8_t identifier[kMDGUIDSize], |
michael@0 | 189 | char* buffer, int buffer_length); |
michael@0 | 190 | }; |
michael@0 | 191 | |
michael@0 | 192 | |
michael@0 | 193 | |
michael@0 | 194 | template<typename ElfClass, typename T> |
michael@0 | 195 | const T* GetOffset(const typename ElfClass::Ehdr* elf_header, |
michael@0 | 196 | typename ElfClass::Off offset) { |
michael@0 | 197 | return reinterpret_cast<const T*>(reinterpret_cast<uintptr_t>(elf_header) + |
michael@0 | 198 | offset); |
michael@0 | 199 | } |
michael@0 | 200 | |
michael@0 | 201 | template<typename ElfClass> |
michael@0 | 202 | const typename ElfClass::Shdr* FindElfSectionByName( |
michael@0 | 203 | const char* name, |
michael@0 | 204 | typename ElfClass::Word section_type, |
michael@0 | 205 | const typename ElfClass::Shdr* sections, |
michael@0 | 206 | const char* section_names, |
michael@0 | 207 | const char* names_end, |
michael@0 | 208 | int nsection) { |
michael@0 | 209 | MOZ_ASSERT(name != NULL); |
michael@0 | 210 | MOZ_ASSERT(sections != NULL); |
michael@0 | 211 | MOZ_ASSERT(nsection > 0); |
michael@0 | 212 | |
michael@0 | 213 | int name_len = strlen(name); |
michael@0 | 214 | if (name_len == 0) |
michael@0 | 215 | return NULL; |
michael@0 | 216 | |
michael@0 | 217 | for (int i = 0; i < nsection; ++i) { |
michael@0 | 218 | const char* section_name = section_names + sections[i].sh_name; |
michael@0 | 219 | if (sections[i].sh_type == section_type && |
michael@0 | 220 | names_end - section_name >= name_len + 1 && |
michael@0 | 221 | strcmp(name, section_name) == 0) { |
michael@0 | 222 | return sections + i; |
michael@0 | 223 | } |
michael@0 | 224 | } |
michael@0 | 225 | return NULL; |
michael@0 | 226 | } |
michael@0 | 227 | |
michael@0 | 228 | } // namespace lul |
michael@0 | 229 | |
michael@0 | 230 | |
michael@0 | 231 | // And finally, the external interface, offered to LulMain.cpp |
michael@0 | 232 | #include "LulElfExt.h" |
michael@0 | 233 | |
michael@0 | 234 | #endif // LulElfInt_h |