Tue, 06 Jan 2015 21:39:09 +0100
Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.
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, 2011, 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 | // Restructured in 2009 by: Jim Blandy <jimb@mozilla.com> <jimb@red-bean.com> |
michael@0 | 34 | |
michael@0 | 35 | // (derived from) |
michael@0 | 36 | // dump_symbols.cc: implement google_breakpad::WriteSymbolFile: |
michael@0 | 37 | // Find all the debugging info in a file and dump it as a Breakpad symbol file. |
michael@0 | 38 | // |
michael@0 | 39 | // dump_symbols.h: Read debugging information from an ELF file, and write |
michael@0 | 40 | // it out as a Breakpad symbol file. |
michael@0 | 41 | |
michael@0 | 42 | // This file is derived from the following files in |
michael@0 | 43 | // toolkit/crashreporter/google-breakpad: |
michael@0 | 44 | // src/common/linux/dump_symbols.cc |
michael@0 | 45 | // src/common/linux/elfutils.cc |
michael@0 | 46 | // src/common/linux/file_id.cc |
michael@0 | 47 | |
michael@0 | 48 | #include <errno.h> |
michael@0 | 49 | #include <fcntl.h> |
michael@0 | 50 | #include <stdio.h> |
michael@0 | 51 | #include <string.h> |
michael@0 | 52 | #include <sys/mman.h> |
michael@0 | 53 | #include <sys/stat.h> |
michael@0 | 54 | #include <unistd.h> |
michael@0 | 55 | #include <arpa/inet.h> |
michael@0 | 56 | |
michael@0 | 57 | #include <set> |
michael@0 | 58 | #include <string> |
michael@0 | 59 | #include <vector> |
michael@0 | 60 | |
michael@0 | 61 | #include "mozilla/Assertions.h" |
michael@0 | 62 | |
michael@0 | 63 | #include "LulPlatformMacros.h" |
michael@0 | 64 | #include "LulCommonExt.h" |
michael@0 | 65 | #include "LulDwarfExt.h" |
michael@0 | 66 | #if defined(LUL_PLAT_arm_android) |
michael@0 | 67 | # include "LulExidxExt.h" |
michael@0 | 68 | #endif |
michael@0 | 69 | #include "LulElfInt.h" |
michael@0 | 70 | #include "LulMainInt.h" |
michael@0 | 71 | |
michael@0 | 72 | |
michael@0 | 73 | #if defined(LUL_PLAT_arm_android) && !defined(SHT_ARM_EXIDX) |
michael@0 | 74 | // bionic and older glibsc don't define it |
michael@0 | 75 | # define SHT_ARM_EXIDX (SHT_LOPROC + 1) |
michael@0 | 76 | #endif |
michael@0 | 77 | |
michael@0 | 78 | |
michael@0 | 79 | // This namespace contains helper functions. |
michael@0 | 80 | namespace { |
michael@0 | 81 | |
michael@0 | 82 | using lul::DwarfCFIToModule; |
michael@0 | 83 | using lul::FindElfSectionByName; |
michael@0 | 84 | using lul::GetOffset; |
michael@0 | 85 | using lul::IsValidElf; |
michael@0 | 86 | using lul::Module; |
michael@0 | 87 | using lul::UniqueString; |
michael@0 | 88 | using lul::scoped_ptr; |
michael@0 | 89 | using lul::Summariser; |
michael@0 | 90 | using std::string; |
michael@0 | 91 | using std::vector; |
michael@0 | 92 | using std::set; |
michael@0 | 93 | |
michael@0 | 94 | // |
michael@0 | 95 | // FDWrapper |
michael@0 | 96 | // |
michael@0 | 97 | // Wrapper class to make sure opened file is closed. |
michael@0 | 98 | // |
michael@0 | 99 | class FDWrapper { |
michael@0 | 100 | public: |
michael@0 | 101 | explicit FDWrapper(int fd) : |
michael@0 | 102 | fd_(fd) {} |
michael@0 | 103 | ~FDWrapper() { |
michael@0 | 104 | if (fd_ != -1) |
michael@0 | 105 | close(fd_); |
michael@0 | 106 | } |
michael@0 | 107 | int get() { |
michael@0 | 108 | return fd_; |
michael@0 | 109 | } |
michael@0 | 110 | int release() { |
michael@0 | 111 | int fd = fd_; |
michael@0 | 112 | fd_ = -1; |
michael@0 | 113 | return fd; |
michael@0 | 114 | } |
michael@0 | 115 | private: |
michael@0 | 116 | int fd_; |
michael@0 | 117 | }; |
michael@0 | 118 | |
michael@0 | 119 | // |
michael@0 | 120 | // MmapWrapper |
michael@0 | 121 | // |
michael@0 | 122 | // Wrapper class to make sure mapped regions are unmapped. |
michael@0 | 123 | // |
michael@0 | 124 | class MmapWrapper { |
michael@0 | 125 | public: |
michael@0 | 126 | MmapWrapper() : is_set_(false) {} |
michael@0 | 127 | ~MmapWrapper() { |
michael@0 | 128 | if (is_set_ && base_ != NULL) { |
michael@0 | 129 | MOZ_ASSERT(size_ > 0); |
michael@0 | 130 | munmap(base_, size_); |
michael@0 | 131 | } |
michael@0 | 132 | } |
michael@0 | 133 | void set(void *mapped_address, size_t mapped_size) { |
michael@0 | 134 | is_set_ = true; |
michael@0 | 135 | base_ = mapped_address; |
michael@0 | 136 | size_ = mapped_size; |
michael@0 | 137 | } |
michael@0 | 138 | void release() { |
michael@0 | 139 | MOZ_ASSERT(is_set_); |
michael@0 | 140 | is_set_ = false; |
michael@0 | 141 | base_ = NULL; |
michael@0 | 142 | size_ = 0; |
michael@0 | 143 | } |
michael@0 | 144 | |
michael@0 | 145 | private: |
michael@0 | 146 | bool is_set_; |
michael@0 | 147 | void *base_; |
michael@0 | 148 | size_t size_; |
michael@0 | 149 | }; |
michael@0 | 150 | |
michael@0 | 151 | |
michael@0 | 152 | // Set NUM_DW_REGNAMES to be the number of Dwarf register names |
michael@0 | 153 | // appropriate to the machine architecture given in HEADER. Return |
michael@0 | 154 | // true on success, or false if HEADER's machine architecture is not |
michael@0 | 155 | // supported. |
michael@0 | 156 | template<typename ElfClass> |
michael@0 | 157 | bool DwarfCFIRegisterNames(const typename ElfClass::Ehdr* elf_header, |
michael@0 | 158 | unsigned int* num_dw_regnames) { |
michael@0 | 159 | switch (elf_header->e_machine) { |
michael@0 | 160 | case EM_386: |
michael@0 | 161 | *num_dw_regnames = DwarfCFIToModule::RegisterNames::I386(); |
michael@0 | 162 | return true; |
michael@0 | 163 | case EM_ARM: |
michael@0 | 164 | *num_dw_regnames = DwarfCFIToModule::RegisterNames::ARM(); |
michael@0 | 165 | return true; |
michael@0 | 166 | case EM_X86_64: |
michael@0 | 167 | *num_dw_regnames = DwarfCFIToModule::RegisterNames::X86_64(); |
michael@0 | 168 | return true; |
michael@0 | 169 | default: |
michael@0 | 170 | MOZ_ASSERT(0); |
michael@0 | 171 | return false; |
michael@0 | 172 | } |
michael@0 | 173 | } |
michael@0 | 174 | |
michael@0 | 175 | template<typename ElfClass> |
michael@0 | 176 | bool LoadDwarfCFI(const string& dwarf_filename, |
michael@0 | 177 | const typename ElfClass::Ehdr* elf_header, |
michael@0 | 178 | const char* section_name, |
michael@0 | 179 | const typename ElfClass::Shdr* section, |
michael@0 | 180 | const bool eh_frame, |
michael@0 | 181 | const typename ElfClass::Shdr* got_section, |
michael@0 | 182 | const typename ElfClass::Shdr* text_section, |
michael@0 | 183 | const bool big_endian, |
michael@0 | 184 | SecMap* smap, |
michael@0 | 185 | uintptr_t text_bias, |
michael@0 | 186 | void (*log)(const char*)) { |
michael@0 | 187 | // Find the appropriate set of register names for this file's |
michael@0 | 188 | // architecture. |
michael@0 | 189 | unsigned int num_dw_regs = 0; |
michael@0 | 190 | if (!DwarfCFIRegisterNames<ElfClass>(elf_header, &num_dw_regs)) { |
michael@0 | 191 | fprintf(stderr, "%s: unrecognized ELF machine architecture '%d';" |
michael@0 | 192 | " cannot convert DWARF call frame information\n", |
michael@0 | 193 | dwarf_filename.c_str(), elf_header->e_machine); |
michael@0 | 194 | return false; |
michael@0 | 195 | } |
michael@0 | 196 | |
michael@0 | 197 | const lul::Endianness endianness |
michael@0 | 198 | = big_endian ? lul::ENDIANNESS_BIG : lul::ENDIANNESS_LITTLE; |
michael@0 | 199 | |
michael@0 | 200 | // Find the call frame information and its size. |
michael@0 | 201 | const char* cfi = |
michael@0 | 202 | GetOffset<ElfClass, char>(elf_header, section->sh_offset); |
michael@0 | 203 | size_t cfi_size = section->sh_size; |
michael@0 | 204 | |
michael@0 | 205 | // Plug together the parser, handler, and their entourages. |
michael@0 | 206 | |
michael@0 | 207 | // Here's a summariser, which will receive the output of the |
michael@0 | 208 | // parser, create summaries, and add them to |smap|. |
michael@0 | 209 | Summariser* summ = new Summariser(smap, text_bias, log); |
michael@0 | 210 | |
michael@0 | 211 | DwarfCFIToModule::Reporter module_reporter(log, dwarf_filename, section_name); |
michael@0 | 212 | DwarfCFIToModule handler(num_dw_regs, &module_reporter, summ); |
michael@0 | 213 | lul::ByteReader byte_reader(endianness); |
michael@0 | 214 | |
michael@0 | 215 | byte_reader.SetAddressSize(ElfClass::kAddrSize); |
michael@0 | 216 | |
michael@0 | 217 | // Provide the base addresses for .eh_frame encoded pointers, if |
michael@0 | 218 | // possible. |
michael@0 | 219 | byte_reader.SetCFIDataBase(section->sh_addr, cfi); |
michael@0 | 220 | if (got_section) |
michael@0 | 221 | byte_reader.SetDataBase(got_section->sh_addr); |
michael@0 | 222 | if (text_section) |
michael@0 | 223 | byte_reader.SetTextBase(text_section->sh_addr); |
michael@0 | 224 | |
michael@0 | 225 | lul::CallFrameInfo::Reporter dwarf_reporter(log, dwarf_filename, |
michael@0 | 226 | section_name); |
michael@0 | 227 | lul::CallFrameInfo parser(cfi, cfi_size, |
michael@0 | 228 | &byte_reader, &handler, &dwarf_reporter, |
michael@0 | 229 | eh_frame); |
michael@0 | 230 | parser.Start(); |
michael@0 | 231 | |
michael@0 | 232 | delete summ; |
michael@0 | 233 | return true; |
michael@0 | 234 | } |
michael@0 | 235 | |
michael@0 | 236 | #if defined(LUL_PLAT_arm_android) |
michael@0 | 237 | template<typename ElfClass> |
michael@0 | 238 | bool LoadARMexidx(const typename ElfClass::Ehdr* elf_header, |
michael@0 | 239 | const typename ElfClass::Shdr* exidx_section, |
michael@0 | 240 | const typename ElfClass::Shdr* extab_section, |
michael@0 | 241 | uint32_t loading_addr, |
michael@0 | 242 | uintptr_t text_bias, |
michael@0 | 243 | SecMap* smap, |
michael@0 | 244 | void (*log)(const char*)) { |
michael@0 | 245 | // To do this properly we need to know: |
michael@0 | 246 | // * the bounds of the .ARM.exidx section in the mapped image |
michael@0 | 247 | // * the bounds of the .ARM.extab section in the mapped image |
michael@0 | 248 | // * the vma of the last byte in the text section associated with the .exidx |
michael@0 | 249 | // The first two are easy. The third is a bit tricky. If we can't |
michael@0 | 250 | // figure out what it is, just pass in zero. |
michael@0 | 251 | // Note that we are reading EXIDX directly out of the mapped in |
michael@0 | 252 | // executable image. Unlike with the CFI reader, there is no |
michael@0 | 253 | // auxiliary, temporary mapping used to read the unwind data. |
michael@0 | 254 | // |
michael@0 | 255 | // An .exidx section is always required, but the .extab section |
michael@0 | 256 | // can be optionally omitted, provided that .exidx does not refer |
michael@0 | 257 | // to it. If the .exidx is erroneous and does refer to .extab even |
michael@0 | 258 | // though .extab is missing, the range checks done by GET_EX_U32 in |
michael@0 | 259 | // ExceptionTableInfo::ExtabEntryExtract should prevent any invalid |
michael@0 | 260 | // memory accesses, and cause the .extab to be rejected as invalid. |
michael@0 | 261 | const char *exidx_img |
michael@0 | 262 | = GetOffset<ElfClass, char>(elf_header, exidx_section->sh_offset); |
michael@0 | 263 | size_t exidx_size = exidx_section->sh_size; |
michael@0 | 264 | const char *extab_img |
michael@0 | 265 | = extab_section |
michael@0 | 266 | ? GetOffset<ElfClass, char>(elf_header, extab_section->sh_offset) |
michael@0 | 267 | : nullptr; |
michael@0 | 268 | size_t extab_size = extab_section ? extab_section->sh_size : 0; |
michael@0 | 269 | |
michael@0 | 270 | // The sh_link field of the exidx section gives the section number |
michael@0 | 271 | // for the associated text section. |
michael@0 | 272 | uint32_t exidx_text_last_svma = 0; |
michael@0 | 273 | int exidx_text_sno = exidx_section->sh_link; |
michael@0 | 274 | typedef typename ElfClass::Shdr Shdr; |
michael@0 | 275 | // |sections| points to the section header table |
michael@0 | 276 | const Shdr* sections |
michael@0 | 277 | = GetOffset<ElfClass, Shdr>(elf_header, elf_header->e_shoff); |
michael@0 | 278 | const int num_sections = elf_header->e_shnum; |
michael@0 | 279 | if (exidx_text_sno >= 0 && exidx_text_sno < num_sections) { |
michael@0 | 280 | const Shdr* exidx_text_shdr = §ions[exidx_text_sno]; |
michael@0 | 281 | if (exidx_text_shdr->sh_size > 0) { |
michael@0 | 282 | exidx_text_last_svma |
michael@0 | 283 | = exidx_text_shdr->sh_addr + exidx_text_shdr->sh_size - 1; |
michael@0 | 284 | } |
michael@0 | 285 | } |
michael@0 | 286 | |
michael@0 | 287 | lul::ARMExToModule handler(smap, log); |
michael@0 | 288 | lul::ExceptionTableInfo |
michael@0 | 289 | parser(exidx_img, exidx_size, extab_img, extab_size, exidx_text_last_svma, |
michael@0 | 290 | &handler, |
michael@0 | 291 | reinterpret_cast<const char*>(elf_header), |
michael@0 | 292 | loading_addr, text_bias, log); |
michael@0 | 293 | parser.Start(); |
michael@0 | 294 | return true; |
michael@0 | 295 | } |
michael@0 | 296 | #endif /* defined(LUL_PLAT_arm_android) */ |
michael@0 | 297 | |
michael@0 | 298 | bool LoadELF(const string& obj_file, MmapWrapper* map_wrapper, |
michael@0 | 299 | void** elf_header) { |
michael@0 | 300 | int obj_fd = open(obj_file.c_str(), O_RDONLY); |
michael@0 | 301 | if (obj_fd < 0) { |
michael@0 | 302 | fprintf(stderr, "Failed to open ELF file '%s': %s\n", |
michael@0 | 303 | obj_file.c_str(), strerror(errno)); |
michael@0 | 304 | return false; |
michael@0 | 305 | } |
michael@0 | 306 | FDWrapper obj_fd_wrapper(obj_fd); |
michael@0 | 307 | struct stat st; |
michael@0 | 308 | if (fstat(obj_fd, &st) != 0 && st.st_size <= 0) { |
michael@0 | 309 | fprintf(stderr, "Unable to fstat ELF file '%s': %s\n", |
michael@0 | 310 | obj_file.c_str(), strerror(errno)); |
michael@0 | 311 | return false; |
michael@0 | 312 | } |
michael@0 | 313 | // Mapping it read-only is good enough. In any case, mapping it |
michael@0 | 314 | // read-write confuses Valgrind's debuginfo acquire/discard |
michael@0 | 315 | // heuristics, making it hard to profile the profiler. |
michael@0 | 316 | void *obj_base = mmap(nullptr, st.st_size, |
michael@0 | 317 | PROT_READ, MAP_PRIVATE, obj_fd, 0); |
michael@0 | 318 | if (obj_base == MAP_FAILED) { |
michael@0 | 319 | fprintf(stderr, "Failed to mmap ELF file '%s': %s\n", |
michael@0 | 320 | obj_file.c_str(), strerror(errno)); |
michael@0 | 321 | return false; |
michael@0 | 322 | } |
michael@0 | 323 | map_wrapper->set(obj_base, st.st_size); |
michael@0 | 324 | *elf_header = obj_base; |
michael@0 | 325 | if (!IsValidElf(*elf_header)) { |
michael@0 | 326 | fprintf(stderr, "Not a valid ELF file: %s\n", obj_file.c_str()); |
michael@0 | 327 | return false; |
michael@0 | 328 | } |
michael@0 | 329 | return true; |
michael@0 | 330 | } |
michael@0 | 331 | |
michael@0 | 332 | // Get the endianness of ELF_HEADER. If it's invalid, return false. |
michael@0 | 333 | template<typename ElfClass> |
michael@0 | 334 | bool ElfEndianness(const typename ElfClass::Ehdr* elf_header, |
michael@0 | 335 | bool* big_endian) { |
michael@0 | 336 | if (elf_header->e_ident[EI_DATA] == ELFDATA2LSB) { |
michael@0 | 337 | *big_endian = false; |
michael@0 | 338 | return true; |
michael@0 | 339 | } |
michael@0 | 340 | if (elf_header->e_ident[EI_DATA] == ELFDATA2MSB) { |
michael@0 | 341 | *big_endian = true; |
michael@0 | 342 | return true; |
michael@0 | 343 | } |
michael@0 | 344 | |
michael@0 | 345 | fprintf(stderr, "bad data encoding in ELF header: %d\n", |
michael@0 | 346 | elf_header->e_ident[EI_DATA]); |
michael@0 | 347 | return false; |
michael@0 | 348 | } |
michael@0 | 349 | |
michael@0 | 350 | // |
michael@0 | 351 | // LoadSymbolsInfo |
michael@0 | 352 | // |
michael@0 | 353 | // Holds the state between the two calls to LoadSymbols() in case it's necessary |
michael@0 | 354 | // to follow the .gnu_debuglink section and load debug information from a |
michael@0 | 355 | // different file. |
michael@0 | 356 | // |
michael@0 | 357 | template<typename ElfClass> |
michael@0 | 358 | class LoadSymbolsInfo { |
michael@0 | 359 | public: |
michael@0 | 360 | typedef typename ElfClass::Addr Addr; |
michael@0 | 361 | |
michael@0 | 362 | explicit LoadSymbolsInfo(const vector<string>& dbg_dirs) : |
michael@0 | 363 | debug_dirs_(dbg_dirs), |
michael@0 | 364 | has_loading_addr_(false) {} |
michael@0 | 365 | |
michael@0 | 366 | // Keeps track of which sections have been loaded so sections don't |
michael@0 | 367 | // accidentally get loaded twice from two different files. |
michael@0 | 368 | void LoadedSection(const string §ion) { |
michael@0 | 369 | if (loaded_sections_.count(section) == 0) { |
michael@0 | 370 | loaded_sections_.insert(section); |
michael@0 | 371 | } else { |
michael@0 | 372 | fprintf(stderr, "Section %s has already been loaded.\n", |
michael@0 | 373 | section.c_str()); |
michael@0 | 374 | } |
michael@0 | 375 | } |
michael@0 | 376 | |
michael@0 | 377 | string debuglink_file() const { |
michael@0 | 378 | return debuglink_file_; |
michael@0 | 379 | } |
michael@0 | 380 | |
michael@0 | 381 | private: |
michael@0 | 382 | const vector<string>& debug_dirs_; // Directories in which to |
michael@0 | 383 | // search for the debug ELF file. |
michael@0 | 384 | |
michael@0 | 385 | string debuglink_file_; // Full path to the debug ELF file. |
michael@0 | 386 | |
michael@0 | 387 | bool has_loading_addr_; // Indicate if LOADING_ADDR_ is valid. |
michael@0 | 388 | |
michael@0 | 389 | set<string> loaded_sections_; // Tracks the Loaded ELF sections |
michael@0 | 390 | // between calls to LoadSymbols(). |
michael@0 | 391 | }; |
michael@0 | 392 | |
michael@0 | 393 | // Find the preferred loading address of the binary. |
michael@0 | 394 | template<typename ElfClass> |
michael@0 | 395 | typename ElfClass::Addr GetLoadingAddress( |
michael@0 | 396 | const typename ElfClass::Phdr* program_headers, |
michael@0 | 397 | int nheader) { |
michael@0 | 398 | typedef typename ElfClass::Phdr Phdr; |
michael@0 | 399 | |
michael@0 | 400 | // For non-PIC executables (e_type == ET_EXEC), the load address is |
michael@0 | 401 | // the start address of the first PT_LOAD segment. (ELF requires |
michael@0 | 402 | // the segments to be sorted by load address.) For PIC executables |
michael@0 | 403 | // and dynamic libraries (e_type == ET_DYN), this address will |
michael@0 | 404 | // normally be zero. |
michael@0 | 405 | for (int i = 0; i < nheader; ++i) { |
michael@0 | 406 | const Phdr& header = program_headers[i]; |
michael@0 | 407 | if (header.p_type == PT_LOAD) |
michael@0 | 408 | return header.p_vaddr; |
michael@0 | 409 | } |
michael@0 | 410 | return 0; |
michael@0 | 411 | } |
michael@0 | 412 | |
michael@0 | 413 | template<typename ElfClass> |
michael@0 | 414 | bool LoadSymbols(const string& obj_file, |
michael@0 | 415 | const bool big_endian, |
michael@0 | 416 | const typename ElfClass::Ehdr* elf_header, |
michael@0 | 417 | const bool read_gnu_debug_link, |
michael@0 | 418 | LoadSymbolsInfo<ElfClass>* info, |
michael@0 | 419 | SecMap* smap, |
michael@0 | 420 | void* rx_avma, |
michael@0 | 421 | void (*log)(const char*)) { |
michael@0 | 422 | typedef typename ElfClass::Phdr Phdr; |
michael@0 | 423 | typedef typename ElfClass::Shdr Shdr; |
michael@0 | 424 | |
michael@0 | 425 | char buf[500]; |
michael@0 | 426 | snprintf(buf, sizeof(buf), "LoadSymbols: BEGIN %s\n", obj_file.c_str()); |
michael@0 | 427 | buf[sizeof(buf)-1] = 0; |
michael@0 | 428 | log(buf); |
michael@0 | 429 | |
michael@0 | 430 | // This is how the text bias is calculated. |
michael@0 | 431 | // BEGIN CALCULATE BIAS |
michael@0 | 432 | uintptr_t loading_addr = GetLoadingAddress<ElfClass>( |
michael@0 | 433 | GetOffset<ElfClass, Phdr>(elf_header, elf_header->e_phoff), |
michael@0 | 434 | elf_header->e_phnum); |
michael@0 | 435 | uintptr_t text_bias = ((uintptr_t)rx_avma) - loading_addr; |
michael@0 | 436 | snprintf(buf, sizeof(buf), |
michael@0 | 437 | "LoadSymbols: rx_avma=%llx, text_bias=%llx", |
michael@0 | 438 | (unsigned long long int)(uintptr_t)rx_avma, |
michael@0 | 439 | (unsigned long long int)text_bias); |
michael@0 | 440 | buf[sizeof(buf)-1] = 0; |
michael@0 | 441 | log(buf); |
michael@0 | 442 | // END CALCULATE BIAS |
michael@0 | 443 | |
michael@0 | 444 | const Shdr* sections = |
michael@0 | 445 | GetOffset<ElfClass, Shdr>(elf_header, elf_header->e_shoff); |
michael@0 | 446 | const Shdr* section_names = sections + elf_header->e_shstrndx; |
michael@0 | 447 | const char* names = |
michael@0 | 448 | GetOffset<ElfClass, char>(elf_header, section_names->sh_offset); |
michael@0 | 449 | const char *names_end = names + section_names->sh_size; |
michael@0 | 450 | bool found_usable_info = false; |
michael@0 | 451 | |
michael@0 | 452 | // Dwarf Call Frame Information (CFI) is actually independent from |
michael@0 | 453 | // the other DWARF debugging information, and can be used alone. |
michael@0 | 454 | const Shdr* dwarf_cfi_section = |
michael@0 | 455 | FindElfSectionByName<ElfClass>(".debug_frame", SHT_PROGBITS, |
michael@0 | 456 | sections, names, names_end, |
michael@0 | 457 | elf_header->e_shnum); |
michael@0 | 458 | if (dwarf_cfi_section) { |
michael@0 | 459 | // Ignore the return value of this function; even without call frame |
michael@0 | 460 | // information, the other debugging information could be perfectly |
michael@0 | 461 | // useful. |
michael@0 | 462 | info->LoadedSection(".debug_frame"); |
michael@0 | 463 | bool result = |
michael@0 | 464 | LoadDwarfCFI<ElfClass>(obj_file, elf_header, ".debug_frame", |
michael@0 | 465 | dwarf_cfi_section, false, 0, 0, big_endian, |
michael@0 | 466 | smap, text_bias, log); |
michael@0 | 467 | found_usable_info = found_usable_info || result; |
michael@0 | 468 | if (result) |
michael@0 | 469 | log("LoadSymbols: read CFI from .debug_frame"); |
michael@0 | 470 | } |
michael@0 | 471 | |
michael@0 | 472 | // Linux C++ exception handling information can also provide |
michael@0 | 473 | // unwinding data. |
michael@0 | 474 | const Shdr* eh_frame_section = |
michael@0 | 475 | FindElfSectionByName<ElfClass>(".eh_frame", SHT_PROGBITS, |
michael@0 | 476 | sections, names, names_end, |
michael@0 | 477 | elf_header->e_shnum); |
michael@0 | 478 | if (eh_frame_section) { |
michael@0 | 479 | // Pointers in .eh_frame data may be relative to the base addresses of |
michael@0 | 480 | // certain sections. Provide those sections if present. |
michael@0 | 481 | const Shdr* got_section = |
michael@0 | 482 | FindElfSectionByName<ElfClass>(".got", SHT_PROGBITS, |
michael@0 | 483 | sections, names, names_end, |
michael@0 | 484 | elf_header->e_shnum); |
michael@0 | 485 | const Shdr* text_section = |
michael@0 | 486 | FindElfSectionByName<ElfClass>(".text", SHT_PROGBITS, |
michael@0 | 487 | sections, names, names_end, |
michael@0 | 488 | elf_header->e_shnum); |
michael@0 | 489 | info->LoadedSection(".eh_frame"); |
michael@0 | 490 | // As above, ignore the return value of this function. |
michael@0 | 491 | bool result = |
michael@0 | 492 | LoadDwarfCFI<ElfClass>(obj_file, elf_header, ".eh_frame", |
michael@0 | 493 | eh_frame_section, true, |
michael@0 | 494 | got_section, text_section, big_endian, |
michael@0 | 495 | smap, text_bias, log); |
michael@0 | 496 | found_usable_info = found_usable_info || result; |
michael@0 | 497 | if (result) |
michael@0 | 498 | log("LoadSymbols: read CFI from .eh_frame"); |
michael@0 | 499 | } |
michael@0 | 500 | |
michael@0 | 501 | # if defined(LUL_PLAT_arm_android) |
michael@0 | 502 | // ARM has special unwind tables that can be used. .exidx is |
michael@0 | 503 | // always required, and .extab is normally required, but may |
michael@0 | 504 | // be omitted if it is empty. See comments on LoadARMexidx() |
michael@0 | 505 | // for more details. |
michael@0 | 506 | const Shdr* arm_exidx_section = |
michael@0 | 507 | FindElfSectionByName<ElfClass>(".ARM.exidx", SHT_ARM_EXIDX, |
michael@0 | 508 | sections, names, names_end, |
michael@0 | 509 | elf_header->e_shnum); |
michael@0 | 510 | const Shdr* arm_extab_section = |
michael@0 | 511 | FindElfSectionByName<ElfClass>(".ARM.extab", SHT_PROGBITS, |
michael@0 | 512 | sections, names, names_end, |
michael@0 | 513 | elf_header->e_shnum); |
michael@0 | 514 | const Shdr* debug_info_section = |
michael@0 | 515 | FindElfSectionByName<ElfClass>(".debug_info", SHT_PROGBITS, |
michael@0 | 516 | sections, names, names_end, |
michael@0 | 517 | elf_header->e_shnum); |
michael@0 | 518 | // Only load information from this section if there isn't a .debug_info |
michael@0 | 519 | // section. |
michael@0 | 520 | if (!debug_info_section && arm_exidx_section) { |
michael@0 | 521 | info->LoadedSection(".ARM.exidx"); |
michael@0 | 522 | if (arm_extab_section) |
michael@0 | 523 | info->LoadedSection(".ARM.extab"); |
michael@0 | 524 | bool result = LoadARMexidx<ElfClass>(elf_header, |
michael@0 | 525 | arm_exidx_section, arm_extab_section, |
michael@0 | 526 | loading_addr, text_bias, smap, log); |
michael@0 | 527 | found_usable_info = found_usable_info || result; |
michael@0 | 528 | if (result) |
michael@0 | 529 | log("LoadSymbols: read EXIDX from .ARM.{exidx,extab}"); |
michael@0 | 530 | } |
michael@0 | 531 | # endif /* defined(LUL_PLAT_arm_android) */ |
michael@0 | 532 | |
michael@0 | 533 | snprintf(buf, sizeof(buf), "LoadSymbols: END %s\n", obj_file.c_str()); |
michael@0 | 534 | buf[sizeof(buf)-1] = 0; |
michael@0 | 535 | log(buf); |
michael@0 | 536 | |
michael@0 | 537 | return found_usable_info; |
michael@0 | 538 | } |
michael@0 | 539 | |
michael@0 | 540 | // Return the breakpad symbol file identifier for the architecture of |
michael@0 | 541 | // ELF_HEADER. |
michael@0 | 542 | template<typename ElfClass> |
michael@0 | 543 | const char* ElfArchitecture(const typename ElfClass::Ehdr* elf_header) { |
michael@0 | 544 | typedef typename ElfClass::Half Half; |
michael@0 | 545 | Half arch = elf_header->e_machine; |
michael@0 | 546 | switch (arch) { |
michael@0 | 547 | case EM_386: return "x86"; |
michael@0 | 548 | case EM_ARM: return "arm"; |
michael@0 | 549 | case EM_MIPS: return "mips"; |
michael@0 | 550 | case EM_PPC64: return "ppc64"; |
michael@0 | 551 | case EM_PPC: return "ppc"; |
michael@0 | 552 | case EM_S390: return "s390"; |
michael@0 | 553 | case EM_SPARC: return "sparc"; |
michael@0 | 554 | case EM_SPARCV9: return "sparcv9"; |
michael@0 | 555 | case EM_X86_64: return "x86_64"; |
michael@0 | 556 | default: return NULL; |
michael@0 | 557 | } |
michael@0 | 558 | } |
michael@0 | 559 | |
michael@0 | 560 | // Format the Elf file identifier in IDENTIFIER as a UUID with the |
michael@0 | 561 | // dashes removed. |
michael@0 | 562 | string FormatIdentifier(unsigned char identifier[16]) { |
michael@0 | 563 | char identifier_str[40]; |
michael@0 | 564 | lul::FileID::ConvertIdentifierToString( |
michael@0 | 565 | identifier, |
michael@0 | 566 | identifier_str, |
michael@0 | 567 | sizeof(identifier_str)); |
michael@0 | 568 | string id_no_dash; |
michael@0 | 569 | for (int i = 0; identifier_str[i] != '\0'; ++i) |
michael@0 | 570 | if (identifier_str[i] != '-') |
michael@0 | 571 | id_no_dash += identifier_str[i]; |
michael@0 | 572 | // Add an extra "0" by the end. PDB files on Windows have an 'age' |
michael@0 | 573 | // number appended to the end of the file identifier; this isn't |
michael@0 | 574 | // really used or necessary on other platforms, but be consistent. |
michael@0 | 575 | id_no_dash += '0'; |
michael@0 | 576 | return id_no_dash; |
michael@0 | 577 | } |
michael@0 | 578 | |
michael@0 | 579 | // Return the non-directory portion of FILENAME: the portion after the |
michael@0 | 580 | // last slash, or the whole filename if there are no slashes. |
michael@0 | 581 | string BaseFileName(const string &filename) { |
michael@0 | 582 | // Lots of copies! basename's behavior is less than ideal. |
michael@0 | 583 | char *c_filename = strdup(filename.c_str()); |
michael@0 | 584 | string base = basename(c_filename); |
michael@0 | 585 | free(c_filename); |
michael@0 | 586 | return base; |
michael@0 | 587 | } |
michael@0 | 588 | |
michael@0 | 589 | template<typename ElfClass> |
michael@0 | 590 | bool ReadSymbolDataElfClass(const typename ElfClass::Ehdr* elf_header, |
michael@0 | 591 | const string& obj_filename, |
michael@0 | 592 | const vector<string>& debug_dirs, |
michael@0 | 593 | SecMap* smap, void* rx_avma, |
michael@0 | 594 | void (*log)(const char*)) { |
michael@0 | 595 | typedef typename ElfClass::Ehdr Ehdr; |
michael@0 | 596 | |
michael@0 | 597 | unsigned char identifier[16]; |
michael@0 | 598 | if (!lul |
michael@0 | 599 | ::FileID::ElfFileIdentifierFromMappedFile(elf_header, identifier)) { |
michael@0 | 600 | fprintf(stderr, "%s: unable to generate file identifier\n", |
michael@0 | 601 | obj_filename.c_str()); |
michael@0 | 602 | return false; |
michael@0 | 603 | } |
michael@0 | 604 | |
michael@0 | 605 | const char *architecture = ElfArchitecture<ElfClass>(elf_header); |
michael@0 | 606 | if (!architecture) { |
michael@0 | 607 | fprintf(stderr, "%s: unrecognized ELF machine architecture: %d\n", |
michael@0 | 608 | obj_filename.c_str(), elf_header->e_machine); |
michael@0 | 609 | return false; |
michael@0 | 610 | } |
michael@0 | 611 | |
michael@0 | 612 | // Figure out what endianness this file is. |
michael@0 | 613 | bool big_endian; |
michael@0 | 614 | if (!ElfEndianness<ElfClass>(elf_header, &big_endian)) |
michael@0 | 615 | return false; |
michael@0 | 616 | |
michael@0 | 617 | string name = BaseFileName(obj_filename); |
michael@0 | 618 | string os = "Linux"; |
michael@0 | 619 | string id = FormatIdentifier(identifier); |
michael@0 | 620 | |
michael@0 | 621 | LoadSymbolsInfo<ElfClass> info(debug_dirs); |
michael@0 | 622 | if (!LoadSymbols<ElfClass>(obj_filename, big_endian, elf_header, |
michael@0 | 623 | !debug_dirs.empty(), &info, |
michael@0 | 624 | smap, rx_avma, log)) { |
michael@0 | 625 | const string debuglink_file = info.debuglink_file(); |
michael@0 | 626 | if (debuglink_file.empty()) |
michael@0 | 627 | return false; |
michael@0 | 628 | |
michael@0 | 629 | // Load debuglink ELF file. |
michael@0 | 630 | fprintf(stderr, "Found debugging info in %s\n", debuglink_file.c_str()); |
michael@0 | 631 | MmapWrapper debug_map_wrapper; |
michael@0 | 632 | Ehdr* debug_elf_header = NULL; |
michael@0 | 633 | if (!LoadELF(debuglink_file, &debug_map_wrapper, |
michael@0 | 634 | reinterpret_cast<void**>(&debug_elf_header))) |
michael@0 | 635 | return false; |
michael@0 | 636 | // Sanity checks to make sure everything matches up. |
michael@0 | 637 | const char *debug_architecture = |
michael@0 | 638 | ElfArchitecture<ElfClass>(debug_elf_header); |
michael@0 | 639 | if (!debug_architecture) { |
michael@0 | 640 | fprintf(stderr, "%s: unrecognized ELF machine architecture: %d\n", |
michael@0 | 641 | debuglink_file.c_str(), debug_elf_header->e_machine); |
michael@0 | 642 | return false; |
michael@0 | 643 | } |
michael@0 | 644 | if (strcmp(architecture, debug_architecture)) { |
michael@0 | 645 | fprintf(stderr, "%s with ELF machine architecture %s does not match " |
michael@0 | 646 | "%s with ELF architecture %s\n", |
michael@0 | 647 | debuglink_file.c_str(), debug_architecture, |
michael@0 | 648 | obj_filename.c_str(), architecture); |
michael@0 | 649 | return false; |
michael@0 | 650 | } |
michael@0 | 651 | |
michael@0 | 652 | bool debug_big_endian; |
michael@0 | 653 | if (!ElfEndianness<ElfClass>(debug_elf_header, &debug_big_endian)) |
michael@0 | 654 | return false; |
michael@0 | 655 | if (debug_big_endian != big_endian) { |
michael@0 | 656 | fprintf(stderr, "%s and %s does not match in endianness\n", |
michael@0 | 657 | obj_filename.c_str(), debuglink_file.c_str()); |
michael@0 | 658 | return false; |
michael@0 | 659 | } |
michael@0 | 660 | |
michael@0 | 661 | if (!LoadSymbols<ElfClass>(debuglink_file, debug_big_endian, |
michael@0 | 662 | debug_elf_header, false, &info, |
michael@0 | 663 | smap, rx_avma, log)) { |
michael@0 | 664 | return false; |
michael@0 | 665 | } |
michael@0 | 666 | } |
michael@0 | 667 | |
michael@0 | 668 | return true; |
michael@0 | 669 | } |
michael@0 | 670 | |
michael@0 | 671 | } // namespace (anon) |
michael@0 | 672 | |
michael@0 | 673 | |
michael@0 | 674 | namespace lul { |
michael@0 | 675 | |
michael@0 | 676 | bool ReadSymbolDataInternal(const uint8_t* obj_file, |
michael@0 | 677 | const string& obj_filename, |
michael@0 | 678 | const vector<string>& debug_dirs, |
michael@0 | 679 | SecMap* smap, void* rx_avma, |
michael@0 | 680 | void (*log)(const char*)) { |
michael@0 | 681 | |
michael@0 | 682 | if (!IsValidElf(obj_file)) { |
michael@0 | 683 | fprintf(stderr, "Not a valid ELF file: %s\n", obj_filename.c_str()); |
michael@0 | 684 | return false; |
michael@0 | 685 | } |
michael@0 | 686 | |
michael@0 | 687 | int elfclass = ElfClass(obj_file); |
michael@0 | 688 | if (elfclass == ELFCLASS32) { |
michael@0 | 689 | return ReadSymbolDataElfClass<ElfClass32>( |
michael@0 | 690 | reinterpret_cast<const Elf32_Ehdr*>(obj_file), |
michael@0 | 691 | obj_filename, debug_dirs, smap, rx_avma, log); |
michael@0 | 692 | } |
michael@0 | 693 | if (elfclass == ELFCLASS64) { |
michael@0 | 694 | return ReadSymbolDataElfClass<ElfClass64>( |
michael@0 | 695 | reinterpret_cast<const Elf64_Ehdr*>(obj_file), |
michael@0 | 696 | obj_filename, debug_dirs, smap, rx_avma, log); |
michael@0 | 697 | } |
michael@0 | 698 | |
michael@0 | 699 | return false; |
michael@0 | 700 | } |
michael@0 | 701 | |
michael@0 | 702 | bool ReadSymbolData(const string& obj_file, |
michael@0 | 703 | const vector<string>& debug_dirs, |
michael@0 | 704 | SecMap* smap, void* rx_avma, |
michael@0 | 705 | void (*log)(const char*)) { |
michael@0 | 706 | MmapWrapper map_wrapper; |
michael@0 | 707 | void* elf_header = NULL; |
michael@0 | 708 | if (!LoadELF(obj_file, &map_wrapper, &elf_header)) |
michael@0 | 709 | return false; |
michael@0 | 710 | |
michael@0 | 711 | return ReadSymbolDataInternal(reinterpret_cast<uint8_t*>(elf_header), |
michael@0 | 712 | obj_file, debug_dirs, smap, rx_avma, log); |
michael@0 | 713 | } |
michael@0 | 714 | |
michael@0 | 715 | |
michael@0 | 716 | namespace { |
michael@0 | 717 | |
michael@0 | 718 | template<typename ElfClass> |
michael@0 | 719 | void FindElfClassSection(const char *elf_base, |
michael@0 | 720 | const char *section_name, |
michael@0 | 721 | typename ElfClass::Word section_type, |
michael@0 | 722 | const void **section_start, |
michael@0 | 723 | int *section_size) { |
michael@0 | 724 | typedef typename ElfClass::Ehdr Ehdr; |
michael@0 | 725 | typedef typename ElfClass::Shdr Shdr; |
michael@0 | 726 | |
michael@0 | 727 | MOZ_ASSERT(elf_base); |
michael@0 | 728 | MOZ_ASSERT(section_start); |
michael@0 | 729 | MOZ_ASSERT(section_size); |
michael@0 | 730 | |
michael@0 | 731 | MOZ_ASSERT(strncmp(elf_base, ELFMAG, SELFMAG) == 0); |
michael@0 | 732 | |
michael@0 | 733 | const Ehdr* elf_header = reinterpret_cast<const Ehdr*>(elf_base); |
michael@0 | 734 | MOZ_ASSERT(elf_header->e_ident[EI_CLASS] == ElfClass::kClass); |
michael@0 | 735 | |
michael@0 | 736 | const Shdr* sections = |
michael@0 | 737 | GetOffset<ElfClass,Shdr>(elf_header, elf_header->e_shoff); |
michael@0 | 738 | const Shdr* section_names = sections + elf_header->e_shstrndx; |
michael@0 | 739 | const char* names = |
michael@0 | 740 | GetOffset<ElfClass,char>(elf_header, section_names->sh_offset); |
michael@0 | 741 | const char *names_end = names + section_names->sh_size; |
michael@0 | 742 | |
michael@0 | 743 | const Shdr* section = |
michael@0 | 744 | FindElfSectionByName<ElfClass>(section_name, section_type, |
michael@0 | 745 | sections, names, names_end, |
michael@0 | 746 | elf_header->e_shnum); |
michael@0 | 747 | |
michael@0 | 748 | if (section != NULL && section->sh_size > 0) { |
michael@0 | 749 | *section_start = elf_base + section->sh_offset; |
michael@0 | 750 | *section_size = section->sh_size; |
michael@0 | 751 | } |
michael@0 | 752 | } |
michael@0 | 753 | |
michael@0 | 754 | template<typename ElfClass> |
michael@0 | 755 | void FindElfClassSegment(const char *elf_base, |
michael@0 | 756 | typename ElfClass::Word segment_type, |
michael@0 | 757 | const void **segment_start, |
michael@0 | 758 | int *segment_size) { |
michael@0 | 759 | typedef typename ElfClass::Ehdr Ehdr; |
michael@0 | 760 | typedef typename ElfClass::Phdr Phdr; |
michael@0 | 761 | |
michael@0 | 762 | MOZ_ASSERT(elf_base); |
michael@0 | 763 | MOZ_ASSERT(segment_start); |
michael@0 | 764 | MOZ_ASSERT(segment_size); |
michael@0 | 765 | |
michael@0 | 766 | MOZ_ASSERT(strncmp(elf_base, ELFMAG, SELFMAG) == 0); |
michael@0 | 767 | |
michael@0 | 768 | const Ehdr* elf_header = reinterpret_cast<const Ehdr*>(elf_base); |
michael@0 | 769 | MOZ_ASSERT(elf_header->e_ident[EI_CLASS] == ElfClass::kClass); |
michael@0 | 770 | |
michael@0 | 771 | const Phdr* phdrs = |
michael@0 | 772 | GetOffset<ElfClass,Phdr>(elf_header, elf_header->e_phoff); |
michael@0 | 773 | |
michael@0 | 774 | for (int i = 0; i < elf_header->e_phnum; ++i) { |
michael@0 | 775 | if (phdrs[i].p_type == segment_type) { |
michael@0 | 776 | *segment_start = elf_base + phdrs[i].p_offset; |
michael@0 | 777 | *segment_size = phdrs[i].p_filesz; |
michael@0 | 778 | return; |
michael@0 | 779 | } |
michael@0 | 780 | } |
michael@0 | 781 | } |
michael@0 | 782 | |
michael@0 | 783 | } // namespace (anon) |
michael@0 | 784 | |
michael@0 | 785 | bool IsValidElf(const void* elf_base) { |
michael@0 | 786 | return strncmp(reinterpret_cast<const char*>(elf_base), |
michael@0 | 787 | ELFMAG, SELFMAG) == 0; |
michael@0 | 788 | } |
michael@0 | 789 | |
michael@0 | 790 | int ElfClass(const void* elf_base) { |
michael@0 | 791 | const ElfW(Ehdr)* elf_header = |
michael@0 | 792 | reinterpret_cast<const ElfW(Ehdr)*>(elf_base); |
michael@0 | 793 | |
michael@0 | 794 | return elf_header->e_ident[EI_CLASS]; |
michael@0 | 795 | } |
michael@0 | 796 | |
michael@0 | 797 | bool FindElfSection(const void *elf_mapped_base, |
michael@0 | 798 | const char *section_name, |
michael@0 | 799 | uint32_t section_type, |
michael@0 | 800 | const void **section_start, |
michael@0 | 801 | int *section_size, |
michael@0 | 802 | int *elfclass) { |
michael@0 | 803 | MOZ_ASSERT(elf_mapped_base); |
michael@0 | 804 | MOZ_ASSERT(section_start); |
michael@0 | 805 | MOZ_ASSERT(section_size); |
michael@0 | 806 | |
michael@0 | 807 | *section_start = NULL; |
michael@0 | 808 | *section_size = 0; |
michael@0 | 809 | |
michael@0 | 810 | if (!IsValidElf(elf_mapped_base)) |
michael@0 | 811 | return false; |
michael@0 | 812 | |
michael@0 | 813 | int cls = ElfClass(elf_mapped_base); |
michael@0 | 814 | if (elfclass) { |
michael@0 | 815 | *elfclass = cls; |
michael@0 | 816 | } |
michael@0 | 817 | |
michael@0 | 818 | const char* elf_base = |
michael@0 | 819 | static_cast<const char*>(elf_mapped_base); |
michael@0 | 820 | |
michael@0 | 821 | if (cls == ELFCLASS32) { |
michael@0 | 822 | FindElfClassSection<ElfClass32>(elf_base, section_name, section_type, |
michael@0 | 823 | section_start, section_size); |
michael@0 | 824 | return *section_start != NULL; |
michael@0 | 825 | } else if (cls == ELFCLASS64) { |
michael@0 | 826 | FindElfClassSection<ElfClass64>(elf_base, section_name, section_type, |
michael@0 | 827 | section_start, section_size); |
michael@0 | 828 | return *section_start != NULL; |
michael@0 | 829 | } |
michael@0 | 830 | |
michael@0 | 831 | return false; |
michael@0 | 832 | } |
michael@0 | 833 | |
michael@0 | 834 | bool FindElfSegment(const void *elf_mapped_base, |
michael@0 | 835 | uint32_t segment_type, |
michael@0 | 836 | const void **segment_start, |
michael@0 | 837 | int *segment_size, |
michael@0 | 838 | int *elfclass) { |
michael@0 | 839 | MOZ_ASSERT(elf_mapped_base); |
michael@0 | 840 | MOZ_ASSERT(segment_start); |
michael@0 | 841 | MOZ_ASSERT(segment_size); |
michael@0 | 842 | |
michael@0 | 843 | *segment_start = NULL; |
michael@0 | 844 | *segment_size = 0; |
michael@0 | 845 | |
michael@0 | 846 | if (!IsValidElf(elf_mapped_base)) |
michael@0 | 847 | return false; |
michael@0 | 848 | |
michael@0 | 849 | int cls = ElfClass(elf_mapped_base); |
michael@0 | 850 | if (elfclass) { |
michael@0 | 851 | *elfclass = cls; |
michael@0 | 852 | } |
michael@0 | 853 | |
michael@0 | 854 | const char* elf_base = |
michael@0 | 855 | static_cast<const char*>(elf_mapped_base); |
michael@0 | 856 | |
michael@0 | 857 | if (cls == ELFCLASS32) { |
michael@0 | 858 | FindElfClassSegment<ElfClass32>(elf_base, segment_type, |
michael@0 | 859 | segment_start, segment_size); |
michael@0 | 860 | return *segment_start != NULL; |
michael@0 | 861 | } else if (cls == ELFCLASS64) { |
michael@0 | 862 | FindElfClassSegment<ElfClass64>(elf_base, segment_type, |
michael@0 | 863 | segment_start, segment_size); |
michael@0 | 864 | return *segment_start != NULL; |
michael@0 | 865 | } |
michael@0 | 866 | |
michael@0 | 867 | return false; |
michael@0 | 868 | } |
michael@0 | 869 | |
michael@0 | 870 | |
michael@0 | 871 | // (derived from) |
michael@0 | 872 | // file_id.cc: Return a unique identifier for a file |
michael@0 | 873 | // |
michael@0 | 874 | // See file_id.h for documentation |
michael@0 | 875 | // |
michael@0 | 876 | |
michael@0 | 877 | // ELF note name and desc are 32-bits word padded. |
michael@0 | 878 | #define NOTE_PADDING(a) ((a + 3) & ~3) |
michael@0 | 879 | |
michael@0 | 880 | // These functions are also used inside the crashed process, so be safe |
michael@0 | 881 | // and use the syscall/libc wrappers instead of direct syscalls or libc. |
michael@0 | 882 | |
michael@0 | 883 | template<typename ElfClass> |
michael@0 | 884 | static bool ElfClassBuildIDNoteIdentifier(const void *section, int length, |
michael@0 | 885 | uint8_t identifier[kMDGUIDSize]) { |
michael@0 | 886 | typedef typename ElfClass::Nhdr Nhdr; |
michael@0 | 887 | |
michael@0 | 888 | const void* section_end = reinterpret_cast<const char*>(section) + length; |
michael@0 | 889 | const Nhdr* note_header = reinterpret_cast<const Nhdr*>(section); |
michael@0 | 890 | while (reinterpret_cast<const void *>(note_header) < section_end) { |
michael@0 | 891 | if (note_header->n_type == NT_GNU_BUILD_ID) |
michael@0 | 892 | break; |
michael@0 | 893 | note_header = reinterpret_cast<const Nhdr*>( |
michael@0 | 894 | reinterpret_cast<const char*>(note_header) + sizeof(Nhdr) + |
michael@0 | 895 | NOTE_PADDING(note_header->n_namesz) + |
michael@0 | 896 | NOTE_PADDING(note_header->n_descsz)); |
michael@0 | 897 | } |
michael@0 | 898 | if (reinterpret_cast<const void *>(note_header) >= section_end || |
michael@0 | 899 | note_header->n_descsz == 0) { |
michael@0 | 900 | return false; |
michael@0 | 901 | } |
michael@0 | 902 | |
michael@0 | 903 | const char* build_id = reinterpret_cast<const char*>(note_header) + |
michael@0 | 904 | sizeof(Nhdr) + NOTE_PADDING(note_header->n_namesz); |
michael@0 | 905 | // Copy as many bits of the build ID as will fit |
michael@0 | 906 | // into the GUID space. |
michael@0 | 907 | memset(identifier, 0, kMDGUIDSize); |
michael@0 | 908 | memcpy(identifier, build_id, |
michael@0 | 909 | std::min(kMDGUIDSize, (size_t)note_header->n_descsz)); |
michael@0 | 910 | |
michael@0 | 911 | return true; |
michael@0 | 912 | } |
michael@0 | 913 | |
michael@0 | 914 | // Attempt to locate a .note.gnu.build-id section in an ELF binary |
michael@0 | 915 | // and copy as many bytes of it as will fit into |identifier|. |
michael@0 | 916 | static bool FindElfBuildIDNote(const void *elf_mapped_base, |
michael@0 | 917 | uint8_t identifier[kMDGUIDSize]) { |
michael@0 | 918 | void* note_section; |
michael@0 | 919 | int note_size, elfclass; |
michael@0 | 920 | if ((!FindElfSegment(elf_mapped_base, PT_NOTE, |
michael@0 | 921 | (const void**)¬e_section, ¬e_size, &elfclass) || |
michael@0 | 922 | note_size == 0) && |
michael@0 | 923 | (!FindElfSection(elf_mapped_base, ".note.gnu.build-id", SHT_NOTE, |
michael@0 | 924 | (const void**)¬e_section, ¬e_size, &elfclass) || |
michael@0 | 925 | note_size == 0)) { |
michael@0 | 926 | return false; |
michael@0 | 927 | } |
michael@0 | 928 | |
michael@0 | 929 | if (elfclass == ELFCLASS32) { |
michael@0 | 930 | return ElfClassBuildIDNoteIdentifier<ElfClass32>(note_section, note_size, |
michael@0 | 931 | identifier); |
michael@0 | 932 | } else if (elfclass == ELFCLASS64) { |
michael@0 | 933 | return ElfClassBuildIDNoteIdentifier<ElfClass64>(note_section, note_size, |
michael@0 | 934 | identifier); |
michael@0 | 935 | } |
michael@0 | 936 | |
michael@0 | 937 | return false; |
michael@0 | 938 | } |
michael@0 | 939 | |
michael@0 | 940 | // Attempt to locate the .text section of an ELF binary and generate |
michael@0 | 941 | // a simple hash by XORing the first page worth of bytes into |identifier|. |
michael@0 | 942 | static bool HashElfTextSection(const void *elf_mapped_base, |
michael@0 | 943 | uint8_t identifier[kMDGUIDSize]) { |
michael@0 | 944 | void* text_section; |
michael@0 | 945 | int text_size; |
michael@0 | 946 | if (!FindElfSection(elf_mapped_base, ".text", SHT_PROGBITS, |
michael@0 | 947 | (const void**)&text_section, &text_size, NULL) || |
michael@0 | 948 | text_size == 0) { |
michael@0 | 949 | return false; |
michael@0 | 950 | } |
michael@0 | 951 | |
michael@0 | 952 | memset(identifier, 0, kMDGUIDSize); |
michael@0 | 953 | const uint8_t* ptr = reinterpret_cast<const uint8_t*>(text_section); |
michael@0 | 954 | const uint8_t* ptr_end = ptr + std::min(text_size, 4096); |
michael@0 | 955 | while (ptr < ptr_end) { |
michael@0 | 956 | for (unsigned i = 0; i < kMDGUIDSize; i++) |
michael@0 | 957 | identifier[i] ^= ptr[i]; |
michael@0 | 958 | ptr += kMDGUIDSize; |
michael@0 | 959 | } |
michael@0 | 960 | return true; |
michael@0 | 961 | } |
michael@0 | 962 | |
michael@0 | 963 | // static |
michael@0 | 964 | bool FileID::ElfFileIdentifierFromMappedFile(const void* base, |
michael@0 | 965 | uint8_t identifier[kMDGUIDSize]) { |
michael@0 | 966 | // Look for a build id note first. |
michael@0 | 967 | if (FindElfBuildIDNote(base, identifier)) |
michael@0 | 968 | return true; |
michael@0 | 969 | |
michael@0 | 970 | // Fall back on hashing the first page of the text section. |
michael@0 | 971 | return HashElfTextSection(base, identifier); |
michael@0 | 972 | } |
michael@0 | 973 | |
michael@0 | 974 | // static |
michael@0 | 975 | void FileID::ConvertIdentifierToString(const uint8_t identifier[kMDGUIDSize], |
michael@0 | 976 | char* buffer, int buffer_length) { |
michael@0 | 977 | uint8_t identifier_swapped[kMDGUIDSize]; |
michael@0 | 978 | |
michael@0 | 979 | // Endian-ness swap to match dump processor expectation. |
michael@0 | 980 | memcpy(identifier_swapped, identifier, kMDGUIDSize); |
michael@0 | 981 | uint32_t* data1 = reinterpret_cast<uint32_t*>(identifier_swapped); |
michael@0 | 982 | *data1 = htonl(*data1); |
michael@0 | 983 | uint16_t* data2 = reinterpret_cast<uint16_t*>(identifier_swapped + 4); |
michael@0 | 984 | *data2 = htons(*data2); |
michael@0 | 985 | uint16_t* data3 = reinterpret_cast<uint16_t*>(identifier_swapped + 6); |
michael@0 | 986 | *data3 = htons(*data3); |
michael@0 | 987 | |
michael@0 | 988 | int buffer_idx = 0; |
michael@0 | 989 | for (unsigned int idx = 0; |
michael@0 | 990 | (buffer_idx < buffer_length) && (idx < kMDGUIDSize); |
michael@0 | 991 | ++idx) { |
michael@0 | 992 | int hi = (identifier_swapped[idx] >> 4) & 0x0F; |
michael@0 | 993 | int lo = (identifier_swapped[idx]) & 0x0F; |
michael@0 | 994 | |
michael@0 | 995 | if (idx == 4 || idx == 6 || idx == 8 || idx == 10) |
michael@0 | 996 | buffer[buffer_idx++] = '-'; |
michael@0 | 997 | |
michael@0 | 998 | buffer[buffer_idx++] = (hi >= 10) ? 'A' + hi - 10 : '0' + hi; |
michael@0 | 999 | buffer[buffer_idx++] = (lo >= 10) ? 'A' + lo - 10 : '0' + lo; |
michael@0 | 1000 | } |
michael@0 | 1001 | |
michael@0 | 1002 | // NULL terminate |
michael@0 | 1003 | buffer[(buffer_idx < buffer_length) ? buffer_idx : buffer_idx - 1] = 0; |
michael@0 | 1004 | } |
michael@0 | 1005 | |
michael@0 | 1006 | } // namespace lul |