// Copyright (c) 2006, 2011, 2012 Google Inc. // All rights reserved. // // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions are // met: // // * Redistributions of source code must retain the above copyright // notice, this list of conditions and the following disclaimer. // * Redistributions in binary form must reproduce the above // copyright notice, this list of conditions and the following disclaimer // in the documentation and/or other materials provided with the // distribution. // * Neither the name of Google Inc. nor the names of its // contributors may be used to endorse or promote products derived from // this software without specific prior written permission. // // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// Restructured in 2009 by: Jim Blandy <jimb@mozilla.com> <jimb@red-bean.com>
// (derived from) // dump_symbols.cc: implement google_breakpad::WriteSymbolFile: // Find all the debugging info in a file and dump it as a Breakpad symbol file. // // dump_symbols.h: Read debugging information from an ELF file, and write // it out as a Breakpad symbol file.
// This file is derived from the following files in // toolkit/crashreporter/google-breakpad: // src/common/linux/dump_symbols.cc // src/common/linux/elfutils.cc // src/common/linux/file_id.cc
#ifdefined(GP_PLAT_arm_android) && !defined(SHT_ARM_EXIDX) // bionic and older glibsc don't define it # define SHT_ARM_EXIDX (SHT_LOPROC + 1) #endif
#if (defined(GP_PLAT_amd64_linux) || defined(GP_PLAT_amd64_android)) && \
!defined(SHT_X86_64_UNWIND) // This is sometimes necessary on x86_64-android and x86_64-linux. # define SHT_X86_64_UNWIND 0x70000001 #endif
// Old Linux header doesn't define EM_AARCH64 #ifndef EM_AARCH64 # define EM_AARCH64 183 #endif
// This namespace contains helper functions. namespace {
using lul::DwarfCFIToModule; using lul::FindElfSectionByName; using lul::GetOffset; using lul::IsValidElf; using lul::Module; using lul::scoped_ptr; using lul::Summariser; using lul::UniqueStringUniverse; using std::set; using std::string; using std::vector;
// // FDWrapper // // Wrapper class to make sure opened file is closed. // class FDWrapper { public: explicit FDWrapper(int fd) : fd_(fd) {}
~FDWrapper() { if (fd_ != -1) close(fd_);
} int get() { return fd_; } int release() { int fd = fd_;
fd_ = -1; return fd;
}
// Set NUM_DW_REGNAMES to be the number of Dwarf register names // appropriate to the machine architecture given in HEADER. Return // true on success, or false if HEADER's machine architecture is not // supported. template <typename ElfClass> bool DwarfCFIRegisterNames(consttypename ElfClass::Ehdr* elf_header, unsignedint* num_dw_regnames) { switch (elf_header->e_machine) { case EM_386:
*num_dw_regnames = DwarfCFIToModule::RegisterNames::I386(); returntrue; case EM_ARM:
*num_dw_regnames = DwarfCFIToModule::RegisterNames::ARM(); returntrue; case EM_X86_64:
*num_dw_regnames = DwarfCFIToModule::RegisterNames::X86_64(); returntrue; case EM_MIPS:
*num_dw_regnames = DwarfCFIToModule::RegisterNames::MIPS(); returntrue; case EM_AARCH64:
*num_dw_regnames = DwarfCFIToModule::RegisterNames::ARM64(); returntrue; default:
MOZ_ASSERT(0); returnfalse;
}
}
// Find the call frame information and its size. constchar* cfi = GetOffset<ElfClass, char>(elf_header, section->sh_offset);
size_t cfi_size = section->sh_size;
// Plug together the parser, handler, and their entourages.
// Here's a summariser, which will receive the output of the // parser, create summaries, and add them to |smap|.
Summariser summ(smap, text_bias, log);
// Provide the base addresses for .eh_frame encoded pointers, if // possible.
reader.SetCFIDataBase(section->sh_addr, cfi); if (got_section) reader.SetDataBase(got_section->sh_addr); if (text_section) reader.SetTextBase(text_section->sh_addr);
bool LoadELF(const string& obj_file, MmapWrapper* map_wrapper, void** elf_header) { int obj_fd = open(obj_file.c_str(), O_RDONLY); if (obj_fd < 0) {
fprintf(stderr, "Failed to open ELF file '%s': %s\n", obj_file.c_str(),
strerror(errno)); returnfalse;
}
FDWrapper obj_fd_wrapper(obj_fd); struct stat st; if (fstat(obj_fd, &st) != 0 && st.st_size <= 0) {
fprintf(stderr, "Unable to fstat ELF file '%s': %s\n", obj_file.c_str(),
strerror(errno)); returnfalse;
} // Mapping it read-only is good enough. In any case, mapping it // read-write confuses Valgrind's debuginfo acquire/discard // heuristics, making it hard to profile the profiler. void* obj_base = mmap(nullptr, st.st_size, PROT_READ, MAP_PRIVATE, obj_fd, 0); if (obj_base == MAP_FAILED) {
fprintf(stderr, "Failed to mmap ELF file '%s': %s\n", obj_file.c_str(),
strerror(errno)); returnfalse;
}
map_wrapper->set(obj_base, st.st_size);
*elf_header = obj_base; if (!IsValidElf(*elf_header)) {
fprintf(stderr, "Not a valid ELF file: %s\n", obj_file.c_str()); returnfalse;
} returntrue;
}
// Get the endianness of ELF_HEADER. If it's invalid, return false. template <typename ElfClass> bool ElfEndianness(consttypename ElfClass::Ehdr* elf_header, bool* big_endian) { if (elf_header->e_ident[EI_DATA] == ELFDATA2LSB) {
*big_endian = false; returntrue;
} if (elf_header->e_ident[EI_DATA] == ELFDATA2MSB) {
*big_endian = true; returntrue;
}
fprintf(stderr, "bad data encoding in ELF header: %d\n",
elf_header->e_ident[EI_DATA]); returnfalse;
}
// // LoadSymbolsInfo // // Holds the state between the two calls to LoadSymbols() in case it's necessary // to follow the .gnu_debuglink section and load debug information from a // different file. // template <typename ElfClass> class LoadSymbolsInfo { public: typedeftypename ElfClass::Addr Addr;
// Keeps track of which sections have been loaded so sections don't // accidentally get loaded twice from two different files. void LoadedSection(const string& section) { if (loaded_sections_.count(section) == 0) {
loaded_sections_.insert(section);
} else {
fprintf(stderr, "Section %s has already been loaded.\n", section.c_str());
}
}
private: const vector<string>& debug_dirs_; // Directories in which to // search for the debug ELF file.
string debuglink_file_; // Full path to the debug ELF file.
bool has_loading_addr_; // Indicate if LOADING_ADDR_ is valid.
set<string> loaded_sections_; // Tracks the Loaded ELF sections // between calls to LoadSymbols().
};
// Find the preferred loading address of the binary. template <typename ElfClass> typename ElfClass::Addr GetLoadingAddress( consttypename ElfClass::Phdr* program_headers, int nheader) { typedeftypename ElfClass::Phdr Phdr;
// For non-PIC executables (e_type == ET_EXEC), the load address is // the start address of the first PT_LOAD segment. (ELF requires // the segments to be sorted by load address.) For PIC executables // and dynamic libraries (e_type == ET_DYN), this address will // normally be zero. for (int i = 0; i < nheader; ++i) { const Phdr& header = program_headers[i]; if (header.p_type == PT_LOAD) return header.p_vaddr;
} return 0;
}
// Dwarf Call Frame Information (CFI) is actually independent from // the other DWARF debugging information, and can be used alone. const Shdr* dwarf_cfi_section =
FindElfSectionByName<ElfClass>(".debug_frame", SHT_PROGBITS, sections,
names, names_end, elf_header->e_shnum); if (dwarf_cfi_section) { // Ignore the return value of this function; even without call frame // information, the other debugging information could be perfectly // useful.
info->LoadedSection(".debug_frame"); bool result = LoadDwarfCFI<ElfClass>(obj_file, elf_header, ".debug_frame",
dwarf_cfi_section, false, 0, 0,
big_endian, smap, text_bias, usu, log);
found_usable_info = found_usable_info || result; if (result) log("LoadSymbols: read CFI from .debug_frame");
}
// Linux C++ exception handling information can also provide // unwinding data. const Shdr* eh_frame_section =
FindElfSectionByName<ElfClass>(".eh_frame", SHT_PROGBITS, sections, names,
names_end, elf_header->e_shnum); #ifdefined(GP_PLAT_amd64_linux) || defined(GP_PLAT_amd64_android) if (!eh_frame_section) { // Possibly depending on which linker created libxul.so, on x86_64-linux // and -android, .eh_frame may instead have the SHT_X86_64_UNWIND type.
eh_frame_section =
FindElfSectionByName<ElfClass>(".eh_frame", SHT_X86_64_UNWIND, sections,
names, names_end, elf_header->e_shnum);
} #endif if (eh_frame_section) { // Pointers in .eh_frame data may be relative to the base addresses of // certain sections. Provide those sections if present. const Shdr* got_section = FindElfSectionByName<ElfClass>( ".got", SHT_PROGBITS, sections, names, names_end, elf_header->e_shnum); const Shdr* text_section = FindElfSectionByName<ElfClass>( ".text", SHT_PROGBITS, sections, names, names_end, elf_header->e_shnum);
info->LoadedSection(".eh_frame"); // As above, ignore the return value of this function. bool result = LoadDwarfCFI<ElfClass>(
obj_file, elf_header, ".eh_frame", eh_frame_section, true, got_section,
text_section, big_endian, smap, text_bias, usu, log);
found_usable_info = found_usable_info || result; if (result) log("LoadSymbols: read CFI from .eh_frame");
}
// Return the breakpad symbol file identifier for the architecture of // ELF_HEADER. template <typename ElfClass> constchar* ElfArchitecture(consttypename ElfClass::Ehdr* elf_header) { typedeftypename ElfClass::Half Half;
Half arch = elf_header->e_machine; switch (arch) { case EM_386: return"x86"; case EM_ARM: return"arm"; case EM_AARCH64: return"arm64"; case EM_MIPS: return"mips"; case EM_PPC64: return"ppc64"; case EM_PPC: return"ppc"; case EM_S390: return"s390"; case EM_SPARC: return"sparc"; case EM_SPARCV9: return"sparcv9"; case EM_X86_64: return"x86_64"; default: return NULL;
}
}
// Format the Elf file identifier in IDENTIFIER as a UUID with the // dashes removed.
string FormatIdentifier(unsignedchar identifier[16]) { char identifier_str[40];
lul::FileID::ConvertIdentifierToString(identifier, identifier_str, sizeof(identifier_str));
string id_no_dash; for (int i = 0; identifier_str[i] != '\0'; ++i) if (identifier_str[i] != '-') id_no_dash += identifier_str[i]; // Add an extra "0" by the end. PDB files on Windows have an 'age' // number appended to the end of the file identifier; this isn't // really used or necessary on other platforms, but be consistent.
id_no_dash += '0'; return id_no_dash;
}
// Return the non-directory portion of FILENAME: the portion after the // last slash, or the whole filename if there are no slashes.
string BaseFileName(const string& filename) { // Lots of copies! basename's behavior is less than ideal. char* c_filename = strdup(filename.c_str());
string base = basename(c_filename);
free(c_filename); return base;
}
// Load debuglink ELF file.
fprintf(stderr, "Found debugging info in %s\n", debuglink_file.c_str());
MmapWrapper debug_map_wrapper;
Ehdr* debug_elf_header = NULL; if (!LoadELF(debuglink_file, &debug_map_wrapper, reinterpret_cast<void**>(&debug_elf_header))) returnfalse; // Sanity checks to make sure everything matches up. constchar* debug_architecture =
ElfArchitecture<ElfClass>(debug_elf_header); if (!debug_architecture) {
fprintf(stderr, "%s: unrecognized ELF machine architecture: %d\n",
debuglink_file.c_str(), debug_elf_header->e_machine); returnfalse;
} if (strcmp(architecture, debug_architecture)) {
fprintf(stderr, "%s with ELF machine architecture %s does not match " "%s with ELF architecture %s\n",
debuglink_file.c_str(), debug_architecture, obj_filename.c_str(),
architecture); returnfalse;
}
bool debug_big_endian; if (!ElfEndianness<ElfClass>(debug_elf_header, &debug_big_endian)) returnfalse; if (debug_big_endian != big_endian) {
fprintf(stderr, "%s and %s does not match in endianness\n",
obj_filename.c_str(), debuglink_file.c_str()); returnfalse;
}
constchar* build_id = reinterpret_cast<constchar*>(note_header) + sizeof(Nhdr) + NOTE_PADDING(note_header->n_namesz); // Copy as many bits of the build ID as will fit // into the GUID space.
memset(identifier, 0, kMDGUIDSize);
memcpy(identifier, build_id,
std::min(kMDGUIDSize, (size_t)note_header->n_descsz));
returntrue;
}
// Attempt to locate a .note.gnu.build-id section in an ELF binary // and copy as many bytes of it as will fit into |identifier|. staticbool FindElfBuildIDNote(constvoid* elf_mapped_base,
uint8_t identifier[kMDGUIDSize]) { void* note_section; int note_size, elfclass; if ((!FindElfSegment(elf_mapped_base, PT_NOTE, (constvoid**)¬e_section,
¬e_size, &elfclass) ||
note_size == 0) &&
(!FindElfSection(elf_mapped_base, ".note.gnu.build-id", SHT_NOTE,
(constvoid**)¬e_section, ¬e_size, &elfclass) ||
note_size == 0)) { returnfalse;
}
// Attempt to locate the .text section of an ELF binary and generate // a simple hash by XORing the first page worth of bytes into |identifier|. staticbool HashElfTextSection(constvoid* elf_mapped_base,
uint8_t identifier[kMDGUIDSize]) { void* text_section; int text_size; if (!FindElfSection(elf_mapped_base, ".text", SHT_PROGBITS,
(constvoid**)&text_section, &text_size, NULL) ||
text_size == 0) { returnfalse;
}
memset(identifier, 0, kMDGUIDSize); const uint8_t* ptr = reinterpret_cast<const uint8_t*>(text_section); const uint8_t* ptr_end = ptr + std::min(text_size, 4096); while (ptr < ptr_end) { for (unsigned i = 0; i < kMDGUIDSize; i++) identifier[i] ^= ptr[i];
ptr += kMDGUIDSize;
} returntrue;
}
// static bool FileID::ElfFileIdentifierFromMappedFile(constvoid* base,
uint8_t identifier[kMDGUIDSize]) { // Look for a build id note first. if (FindElfBuildIDNote(base, identifier)) returntrue;
// Fall back on hashing the first page of the text section. return HashElfTextSection(base, identifier);
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.