1 // Copyright (c) 2011 Google Inc.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
14 // * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 // Restructured in 2009 by: Jim Blandy <jimb@mozilla.com> <jimb@red-bean.com>
32 // dump_symbols.cc: implement google_breakpad::WriteSymbolFile:
33 // Find all the debugging info in a file and dump it as a Breakpad symbol file.
35 #include "common/linux/dump_symbols.h"
55 #include "common/dwarf/bytereader-inl.h"
56 #include "common/dwarf/dwarf2diehandler.h"
57 #include "common/dwarf_cfi_to_module.h"
58 #include "common/dwarf_cu_to_module.h"
59 #include "common/dwarf_line_to_module.h"
60 #include "common/linux/crc32.h"
61 #include "common/linux/eintr_wrapper.h"
62 #include "common/linux/elfutils.h"
63 #include "common/linux/elfutils-inl.h"
64 #include "common/linux/elf_symbols_to_module.h"
65 #include "common/linux/file_id.h"
66 #include "common/module.h"
67 #include "common/scoped_ptr.h"
68 #ifndef NO_STABS_SUPPORT
69 #include "common/stabs_reader.h"
70 #include "common/stabs_to_module.h"
72 #include "common/using_std_string.h"
74 // This namespace contains helper functions.
77 using google_breakpad::DumpOptions;
78 using google_breakpad::DwarfCFIToModule;
79 using google_breakpad::DwarfCUToModule;
80 using google_breakpad::DwarfLineToModule;
81 using google_breakpad::ElfClass;
82 using google_breakpad::ElfClass32;
83 using google_breakpad::ElfClass64;
84 using google_breakpad::FindElfSectionByName;
85 using google_breakpad::GetOffset;
86 using google_breakpad::IsValidElf;
87 using google_breakpad::Module;
88 #ifndef NO_STABS_SUPPORT
89 using google_breakpad::StabsToModule;
91 using google_breakpad::scoped_ptr;
93 // Define AARCH64 ELF architecture if host machine does not include this define.
95 #define EM_AARCH64 183
101 // Wrapper class to make sure opened file is closed.
105 explicit FDWrapper(int fd) :
126 // Wrapper class to make sure mapped regions are unmapped.
130 MmapWrapper() : is_set_(false) {}
132 if (is_set_ && base_ != NULL) {
134 munmap(base_, size_);
137 void set(void *mapped_address, size_t mapped_size) {
139 base_ = mapped_address;
155 // Find the preferred loading address of the binary.
156 template<typename ElfClass>
157 typename ElfClass::Addr GetLoadingAddress(
158 const typename ElfClass::Phdr* program_headers,
160 typedef typename ElfClass::Phdr Phdr;
162 // For non-PIC executables (e_type == ET_EXEC), the load address is
163 // the start address of the first PT_LOAD segment. (ELF requires
164 // the segments to be sorted by load address.) For PIC executables
165 // and dynamic libraries (e_type == ET_DYN), this address will
167 for (int i = 0; i < nheader; ++i) {
168 const Phdr& header = program_headers[i];
169 if (header.p_type == PT_LOAD)
170 return header.p_vaddr;
175 #ifndef NO_STABS_SUPPORT
176 template<typename ElfClass>
177 bool LoadStabs(const typename ElfClass::Ehdr* elf_header,
178 const typename ElfClass::Shdr* stab_section,
179 const typename ElfClass::Shdr* stabstr_section,
180 const bool big_endian,
182 // A callback object to handle data from the STABS reader.
183 StabsToModule handler(module);
184 // Find the addresses of the STABS data, and create a STABS reader object.
185 // On Linux, STABS entries always have 32-bit values, regardless of the
186 // address size of the architecture whose code they're describing, and
187 // the strings are always "unitized".
188 const uint8_t* stabs =
189 GetOffset<ElfClass, uint8_t>(elf_header, stab_section->sh_offset);
190 const uint8_t* stabstr =
191 GetOffset<ElfClass, uint8_t>(elf_header, stabstr_section->sh_offset);
192 google_breakpad::StabsReader reader(stabs, stab_section->sh_size,
193 stabstr, stabstr_section->sh_size,
194 big_endian, 4, true, &handler);
195 // Read the STABS data, and do post-processing.
196 if (!reader.Process())
201 #endif // NO_STABS_SUPPORT
203 // A line-to-module loader that accepts line number info parsed by
204 // dwarf2reader::LineInfo and populates a Module and a line vector
206 class DumperLineToModule: public DwarfCUToModule::LineToModuleHandler {
208 // Create a line-to-module converter using BYTE_READER.
209 explicit DumperLineToModule(dwarf2reader::ByteReader *byte_reader)
210 : byte_reader_(byte_reader) { }
211 void StartCompilationUnit(const string& compilation_dir) {
212 compilation_dir_ = compilation_dir;
214 void ReadProgram(const char* program, uint64 length,
215 Module* module, std::vector<Module::Line>* lines) {
216 DwarfLineToModule handler(module, compilation_dir_, lines);
217 dwarf2reader::LineInfo parser(program, length, byte_reader_, &handler);
221 string compilation_dir_;
222 dwarf2reader::ByteReader *byte_reader_;
225 template<typename ElfClass>
226 bool LoadDwarf(const string& dwarf_filename,
227 const typename ElfClass::Ehdr* elf_header,
228 const bool big_endian,
229 bool handle_inter_cu_refs,
231 typedef typename ElfClass::Shdr Shdr;
233 const dwarf2reader::Endianness endianness = big_endian ?
234 dwarf2reader::ENDIANNESS_BIG : dwarf2reader::ENDIANNESS_LITTLE;
235 dwarf2reader::ByteReader byte_reader(endianness);
237 // Construct a context for this file.
238 DwarfCUToModule::FileContext file_context(dwarf_filename,
240 handle_inter_cu_refs);
242 // Build a map of the ELF file's sections.
243 const Shdr* sections =
244 GetOffset<ElfClass, Shdr>(elf_header, elf_header->e_shoff);
245 int num_sections = elf_header->e_shnum;
246 const Shdr* section_names = sections + elf_header->e_shstrndx;
247 for (int i = 0; i < num_sections; i++) {
248 const Shdr* section = §ions[i];
249 string name = GetOffset<ElfClass, char>(elf_header,
250 section_names->sh_offset) +
252 const char* contents = GetOffset<ElfClass, char>(elf_header,
254 file_context.AddSectionToSectionMap(name, contents, section->sh_size);
257 // Parse all the compilation units in the .debug_info section.
258 DumperLineToModule line_to_module(&byte_reader);
259 dwarf2reader::SectionMap::const_iterator debug_info_entry =
260 file_context.section_map().find(".debug_info");
261 assert(debug_info_entry != file_context.section_map().end());
262 const std::pair<const char*, uint64>& debug_info_section =
263 debug_info_entry->second;
264 // This should never have been called if the file doesn't have a
265 // .debug_info section.
266 assert(debug_info_section.first);
267 uint64 debug_info_length = debug_info_section.second;
268 for (uint64 offset = 0; offset < debug_info_length;) {
269 // Make a handler for the root DIE that populates MODULE with the
270 // data that was found.
271 DwarfCUToModule::WarningReporter reporter(dwarf_filename, offset);
272 DwarfCUToModule root_handler(&file_context, &line_to_module, &reporter);
273 // Make a Dwarf2Handler that drives the DIEHandler.
274 dwarf2reader::DIEDispatcher die_dispatcher(&root_handler);
275 // Make a DWARF parser for the compilation unit at OFFSET.
276 dwarf2reader::CompilationUnit reader(file_context.section_map(),
280 // Process the entire compilation unit; get the offset of the next.
281 offset += reader.Start();
286 // Fill REGISTER_NAMES with the register names appropriate to the
287 // machine architecture given in HEADER, indexed by the register
288 // numbers used in DWARF call frame information. Return true on
289 // success, or false if HEADER's machine architecture is not
291 template<typename ElfClass>
292 bool DwarfCFIRegisterNames(const typename ElfClass::Ehdr* elf_header,
293 std::vector<string>* register_names) {
294 switch (elf_header->e_machine) {
296 *register_names = DwarfCFIToModule::RegisterNames::I386();
299 *register_names = DwarfCFIToModule::RegisterNames::ARM();
302 *register_names = DwarfCFIToModule::RegisterNames::ARM64();
305 *register_names = DwarfCFIToModule::RegisterNames::MIPS();
308 *register_names = DwarfCFIToModule::RegisterNames::X86_64();
315 template<typename ElfClass>
316 bool LoadDwarfCFI(const string& dwarf_filename,
317 const typename ElfClass::Ehdr* elf_header,
318 const char* section_name,
319 const typename ElfClass::Shdr* section,
321 const typename ElfClass::Shdr* got_section,
322 const typename ElfClass::Shdr* text_section,
323 const bool big_endian,
325 // Find the appropriate set of register names for this file's
327 std::vector<string> register_names;
328 if (!DwarfCFIRegisterNames<ElfClass>(elf_header, ®ister_names)) {
329 fprintf(stderr, "%s: unrecognized ELF machine architecture '%d';"
330 " cannot convert DWARF call frame information\n",
331 dwarf_filename.c_str(), elf_header->e_machine);
335 const dwarf2reader::Endianness endianness = big_endian ?
336 dwarf2reader::ENDIANNESS_BIG : dwarf2reader::ENDIANNESS_LITTLE;
338 // Find the call frame information and its size.
340 GetOffset<ElfClass, char>(elf_header, section->sh_offset);
341 size_t cfi_size = section->sh_size;
343 // Plug together the parser, handler, and their entourages.
344 DwarfCFIToModule::Reporter module_reporter(dwarf_filename, section_name);
345 DwarfCFIToModule handler(module, register_names, &module_reporter);
346 dwarf2reader::ByteReader byte_reader(endianness);
348 byte_reader.SetAddressSize(ElfClass::kAddrSize);
350 // Provide the base addresses for .eh_frame encoded pointers, if
352 byte_reader.SetCFIDataBase(section->sh_addr, cfi);
354 byte_reader.SetDataBase(got_section->sh_addr);
356 byte_reader.SetTextBase(text_section->sh_addr);
358 dwarf2reader::CallFrameInfo::Reporter dwarf_reporter(dwarf_filename,
360 dwarf2reader::CallFrameInfo parser(cfi, cfi_size,
361 &byte_reader, &handler, &dwarf_reporter,
367 bool LoadELF(const string& obj_file, MmapWrapper* map_wrapper,
369 int obj_fd = open(obj_file.c_str(), O_RDONLY);
371 fprintf(stderr, "Failed to open ELF file '%s': %s\n",
372 obj_file.c_str(), strerror(errno));
375 FDWrapper obj_fd_wrapper(obj_fd);
377 if (fstat(obj_fd, &st) != 0 && st.st_size <= 0) {
378 fprintf(stderr, "Unable to fstat ELF file '%s': %s\n",
379 obj_file.c_str(), strerror(errno));
382 void* obj_base = mmap(NULL, st.st_size,
383 PROT_READ | PROT_WRITE, MAP_PRIVATE, obj_fd, 0);
384 if (obj_base == MAP_FAILED) {
385 fprintf(stderr, "Failed to mmap ELF file '%s': %s\n",
386 obj_file.c_str(), strerror(errno));
389 map_wrapper->set(obj_base, st.st_size);
390 *elf_header = obj_base;
391 if (!IsValidElf(*elf_header)) {
392 fprintf(stderr, "Not a valid ELF file: %s\n", obj_file.c_str());
398 // Get the endianness of ELF_HEADER. If it's invalid, return false.
399 template<typename ElfClass>
400 bool ElfEndianness(const typename ElfClass::Ehdr* elf_header,
402 if (elf_header->e_ident[EI_DATA] == ELFDATA2LSB) {
406 if (elf_header->e_ident[EI_DATA] == ELFDATA2MSB) {
411 fprintf(stderr, "bad data encoding in ELF header: %d\n",
412 elf_header->e_ident[EI_DATA]);
416 // Read the .gnu_debuglink and get the debug file name. If anything goes
417 // wrong, return an empty string.
418 string ReadDebugLink(const char* debuglink,
419 const size_t debuglink_size,
420 const bool big_endian,
421 const string& obj_file,
422 const std::vector<string>& debug_dirs) {
423 size_t debuglink_len = strlen(debuglink) + 5; // Include '\0' + CRC32.
424 debuglink_len = 4 * ((debuglink_len + 3) / 4); // Round up to 4 bytes.
427 if (debuglink_len != debuglink_size) {
428 fprintf(stderr, "Mismatched .gnu_debuglink string / section size: "
429 "%zx %zx\n", debuglink_len, debuglink_size);
434 int debuglink_fd = -1;
435 string debuglink_path;
436 std::vector<string>::const_iterator it;
437 for (it = debug_dirs.begin(); it < debug_dirs.end(); ++it) {
438 const string& debug_dir = *it;
439 debuglink_path = debug_dir + "/" + debuglink;
440 debuglink_fd = open(debuglink_path.c_str(), O_RDONLY);
441 if (debuglink_fd < 0)
444 FDWrapper debuglink_fd_wrapper(debuglink_fd);
446 // The CRC is the last 4 bytes in |debuglink|.
447 const dwarf2reader::Endianness endianness = big_endian ?
448 dwarf2reader::ENDIANNESS_BIG : dwarf2reader::ENDIANNESS_LITTLE;
449 dwarf2reader::ByteReader byte_reader(endianness);
450 uint32_t expected_crc =
451 byte_reader.ReadFourBytes(&debuglink[debuglink_size - 4]);
453 uint32_t actual_crc = 0;
455 const size_t kReadSize = 4096;
457 ssize_t bytes_read = HANDLE_EINTR(read(debuglink_fd, &buf, kReadSize));
458 if (bytes_read < 0) {
459 fprintf(stderr, "Error reading debug ELF file %s.\n",
460 debuglink_path.c_str());
465 actual_crc = google_breakpad::UpdateCrc32(actual_crc, buf, bytes_read);
467 if (actual_crc != expected_crc) {
468 fprintf(stderr, "Error reading debug ELF file - CRC32 mismatch: %s\n",
469 debuglink_path.c_str());
477 fprintf(stderr, "Failed to find debug ELF file for '%s' after trying:\n",
479 for (it = debug_dirs.begin(); it < debug_dirs.end(); ++it) {
480 const string debug_dir = *it;
481 fprintf(stderr, " %s/%s\n", debug_dir.c_str(), debuglink);
486 return debuglink_path;
492 // Holds the state between the two calls to LoadSymbols() in case it's necessary
493 // to follow the .gnu_debuglink section and load debug information from a
496 template<typename ElfClass>
497 class LoadSymbolsInfo {
499 typedef typename ElfClass::Addr Addr;
501 explicit LoadSymbolsInfo(const std::vector<string>& dbg_dirs) :
502 debug_dirs_(dbg_dirs),
503 has_loading_addr_(false) {}
505 // Keeps track of which sections have been loaded so sections don't
506 // accidentally get loaded twice from two different files.
507 void LoadedSection(const string §ion) {
508 if (loaded_sections_.count(section) == 0) {
509 loaded_sections_.insert(section);
511 fprintf(stderr, "Section %s has already been loaded.\n",
516 // The ELF file and linked debug file are expected to have the same preferred
518 void set_loading_addr(Addr addr, const string &filename) {
519 if (!has_loading_addr_) {
520 loading_addr_ = addr;
521 loaded_file_ = filename;
525 if (addr != loading_addr_) {
527 "ELF file '%s' and debug ELF file '%s' "
528 "have different load addresses.\n",
529 loaded_file_.c_str(), filename.c_str());
534 // Setters and getters
535 const std::vector<string>& debug_dirs() const {
539 string debuglink_file() const {
540 return debuglink_file_;
542 void set_debuglink_file(string file) {
543 debuglink_file_ = file;
547 const std::vector<string>& debug_dirs_; // Directories in which to
548 // search for the debug ELF file.
550 string debuglink_file_; // Full path to the debug ELF file.
552 bool has_loading_addr_; // Indicate if LOADING_ADDR_ is valid.
554 Addr loading_addr_; // Saves the preferred loading address from the
555 // first call to LoadSymbols().
557 string loaded_file_; // Name of the file loaded from the first call to
560 std::set<string> loaded_sections_; // Tracks the Loaded ELF sections
561 // between calls to LoadSymbols().
564 template<typename ElfClass>
565 bool LoadSymbols(const string& obj_file,
566 const bool big_endian,
567 const typename ElfClass::Ehdr* elf_header,
568 const bool read_gnu_debug_link,
569 LoadSymbolsInfo<ElfClass>* info,
570 const DumpOptions& options,
572 typedef typename ElfClass::Addr Addr;
573 typedef typename ElfClass::Phdr Phdr;
574 typedef typename ElfClass::Shdr Shdr;
575 typedef typename ElfClass::Word Word;
577 Addr loading_addr = GetLoadingAddress<ElfClass>(
578 GetOffset<ElfClass, Phdr>(elf_header, elf_header->e_phoff),
579 elf_header->e_phnum);
580 module->SetLoadAddress(loading_addr);
581 info->set_loading_addr(loading_addr, obj_file);
583 Word debug_section_type =
584 elf_header->e_machine == EM_MIPS ? SHT_MIPS_DWARF : SHT_PROGBITS;
585 const Shdr* sections =
586 GetOffset<ElfClass, Shdr>(elf_header, elf_header->e_shoff);
587 const Shdr* section_names = sections + elf_header->e_shstrndx;
589 GetOffset<ElfClass, char>(elf_header, section_names->sh_offset);
590 const char *names_end = names + section_names->sh_size;
591 bool found_debug_info_section = false;
592 bool found_usable_info = false;
594 if (options.symbol_data != ONLY_CFI) {
595 #ifndef NO_STABS_SUPPORT
596 // Look for STABS debugging information, and load it if present.
597 const Shdr* stab_section =
598 FindElfSectionByName<ElfClass>(".stab", SHT_PROGBITS,
599 sections, names, names_end,
600 elf_header->e_shnum);
602 const Shdr* stabstr_section = stab_section->sh_link + sections;
603 if (stabstr_section) {
604 found_debug_info_section = true;
605 found_usable_info = true;
606 info->LoadedSection(".stab");
607 if (!LoadStabs<ElfClass>(elf_header, stab_section, stabstr_section,
608 big_endian, module)) {
609 fprintf(stderr, "%s: \".stab\" section found, but failed to load"
610 " STABS debugging information\n", obj_file.c_str());
614 #endif // NO_STABS_SUPPORT
616 // Look for DWARF debugging information, and load it if present.
617 const Shdr* dwarf_section =
618 FindElfSectionByName<ElfClass>(".debug_info", debug_section_type,
619 sections, names, names_end,
620 elf_header->e_shnum);
622 found_debug_info_section = true;
623 found_usable_info = true;
624 info->LoadedSection(".debug_info");
625 if (!LoadDwarf<ElfClass>(obj_file, elf_header, big_endian,
626 options.handle_inter_cu_refs, module)) {
627 fprintf(stderr, "%s: \".debug_info\" section found, but failed to load "
628 "DWARF debugging information\n", obj_file.c_str());
633 if (options.symbol_data != NO_CFI) {
634 // Dwarf Call Frame Information (CFI) is actually independent from
635 // the other DWARF debugging information, and can be used alone.
636 const Shdr* dwarf_cfi_section =
637 FindElfSectionByName<ElfClass>(".debug_frame", debug_section_type,
638 sections, names, names_end,
639 elf_header->e_shnum);
640 if (dwarf_cfi_section) {
641 // Ignore the return value of this function; even without call frame
642 // information, the other debugging information could be perfectly
644 info->LoadedSection(".debug_frame");
646 LoadDwarfCFI<ElfClass>(obj_file, elf_header, ".debug_frame",
647 dwarf_cfi_section, false, 0, 0, big_endian,
649 found_usable_info = found_usable_info || result;
652 // Linux C++ exception handling information can also provide
654 const Shdr* eh_frame_section =
655 FindElfSectionByName<ElfClass>(".eh_frame", SHT_PROGBITS,
656 sections, names, names_end,
657 elf_header->e_shnum);
658 if (eh_frame_section) {
659 // Pointers in .eh_frame data may be relative to the base addresses of
660 // certain sections. Provide those sections if present.
661 const Shdr* got_section =
662 FindElfSectionByName<ElfClass>(".got", SHT_PROGBITS,
663 sections, names, names_end,
664 elf_header->e_shnum);
665 const Shdr* text_section =
666 FindElfSectionByName<ElfClass>(".text", SHT_PROGBITS,
667 sections, names, names_end,
668 elf_header->e_shnum);
669 info->LoadedSection(".eh_frame");
670 // As above, ignore the return value of this function.
672 LoadDwarfCFI<ElfClass>(obj_file, elf_header, ".eh_frame",
673 eh_frame_section, true,
674 got_section, text_section, big_endian, module);
675 found_usable_info = found_usable_info || result;
679 if (!found_debug_info_section) {
680 fprintf(stderr, "%s: file contains no debugging information"
681 " (no \".stab\" or \".debug_info\" sections)\n",
684 // Failed, but maybe there's a .gnu_debuglink section?
685 if (read_gnu_debug_link) {
686 const Shdr* gnu_debuglink_section
687 = FindElfSectionByName<ElfClass>(".gnu_debuglink", SHT_PROGBITS,
689 names_end, elf_header->e_shnum);
690 if (gnu_debuglink_section) {
691 if (!info->debug_dirs().empty()) {
692 const char* debuglink_contents =
693 GetOffset<ElfClass, char>(elf_header,
694 gnu_debuglink_section->sh_offset);
695 string debuglink_file =
696 ReadDebugLink(debuglink_contents,
697 gnu_debuglink_section->sh_size,
701 info->set_debuglink_file(debuglink_file);
703 fprintf(stderr, ".gnu_debuglink section found in '%s', "
704 "but no debug path specified.\n", obj_file.c_str());
707 fprintf(stderr, "%s does not contain a .gnu_debuglink section.\n",
711 if (options.symbol_data != ONLY_CFI) {
712 // The caller doesn't want to consult .gnu_debuglink.
713 // See if there are export symbols available.
714 const Shdr* dynsym_section =
715 FindElfSectionByName<ElfClass>(".dynsym", SHT_DYNSYM,
716 sections, names, names_end,
717 elf_header->e_shnum);
718 const Shdr* dynstr_section =
719 FindElfSectionByName<ElfClass>(".dynstr", SHT_STRTAB,
720 sections, names, names_end,
721 elf_header->e_shnum);
722 if (dynsym_section && dynstr_section) {
723 info->LoadedSection(".dynsym");
725 const uint8_t* dynsyms =
726 GetOffset<ElfClass, uint8_t>(elf_header,
727 dynsym_section->sh_offset);
728 const uint8_t* dynstrs =
729 GetOffset<ElfClass, uint8_t>(elf_header,
730 dynstr_section->sh_offset);
732 ELFSymbolsToModule(dynsyms,
733 dynsym_section->sh_size,
735 dynstr_section->sh_size,
739 found_usable_info = found_usable_info || result;
743 // Return true if some usable information was found, since
744 // the caller doesn't want to use .gnu_debuglink.
745 return found_usable_info;
748 // No debug info was found, let the user try again with .gnu_debuglink
756 // Return the breakpad symbol file identifier for the architecture of
758 template<typename ElfClass>
759 const char* ElfArchitecture(const typename ElfClass::Ehdr* elf_header) {
760 typedef typename ElfClass::Half Half;
761 Half arch = elf_header->e_machine;
763 case EM_386: return "x86";
764 case EM_ARM: return "arm";
765 case EM_AARCH64: return "arm64";
766 case EM_MIPS: return "mips";
767 case EM_PPC64: return "ppc64";
768 case EM_PPC: return "ppc";
769 case EM_S390: return "s390";
770 case EM_SPARC: return "sparc";
771 case EM_SPARCV9: return "sparcv9";
772 case EM_X86_64: return "x86_64";
773 default: return NULL;
777 // Format the Elf file identifier in IDENTIFIER as a UUID with the
779 string FormatIdentifier(unsigned char identifier[16]) {
780 char identifier_str[40];
781 google_breakpad::FileID::ConvertIdentifierToString(
784 sizeof(identifier_str));
786 for (int i = 0; identifier_str[i] != '\0'; ++i)
787 if (identifier_str[i] != '-')
788 id_no_dash += identifier_str[i];
789 // Add an extra "0" by the end. PDB files on Windows have an 'age'
790 // number appended to the end of the file identifier; this isn't
791 // really used or necessary on other platforms, but be consistent.
796 // Return the non-directory portion of FILENAME: the portion after the
797 // last slash, or the whole filename if there are no slashes.
798 string BaseFileName(const string &filename) {
799 // Lots of copies! basename's behavior is less than ideal.
800 char* c_filename = strdup(filename.c_str());
801 string base = basename(c_filename);
806 template<typename ElfClass>
807 bool ReadSymbolDataElfClass(const typename ElfClass::Ehdr* elf_header,
808 const string& obj_filename,
809 const std::vector<string>& debug_dirs,
810 const DumpOptions& options,
811 Module** out_module) {
812 typedef typename ElfClass::Ehdr Ehdr;
813 typedef typename ElfClass::Shdr Shdr;
817 unsigned char identifier[16];
818 if (!google_breakpad::FileID::ElfFileIdentifierFromMappedFile(elf_header,
820 fprintf(stderr, "%s: unable to generate file identifier\n",
821 obj_filename.c_str());
825 const char *architecture = ElfArchitecture<ElfClass>(elf_header);
827 fprintf(stderr, "%s: unrecognized ELF machine architecture: %d\n",
828 obj_filename.c_str(), elf_header->e_machine);
832 // Figure out what endianness this file is.
834 if (!ElfEndianness<ElfClass>(elf_header, &big_endian))
837 string name = BaseFileName(obj_filename);
839 string id = FormatIdentifier(identifier);
841 LoadSymbolsInfo<ElfClass> info(debug_dirs);
842 scoped_ptr<Module> module(new Module(name, os, architecture, id));
843 if (!LoadSymbols<ElfClass>(obj_filename, big_endian, elf_header,
844 !debug_dirs.empty(), &info,
845 options, module.get())) {
846 const string debuglink_file = info.debuglink_file();
847 if (debuglink_file.empty())
850 // Load debuglink ELF file.
851 fprintf(stderr, "Found debugging info in %s\n", debuglink_file.c_str());
852 MmapWrapper debug_map_wrapper;
853 Ehdr* debug_elf_header = NULL;
854 if (!LoadELF(debuglink_file, &debug_map_wrapper,
855 reinterpret_cast<void**>(&debug_elf_header)))
857 // Sanity checks to make sure everything matches up.
858 const char *debug_architecture =
859 ElfArchitecture<ElfClass>(debug_elf_header);
860 if (!debug_architecture) {
861 fprintf(stderr, "%s: unrecognized ELF machine architecture: %d\n",
862 debuglink_file.c_str(), debug_elf_header->e_machine);
865 if (strcmp(architecture, debug_architecture)) {
866 fprintf(stderr, "%s with ELF machine architecture %s does not match "
867 "%s with ELF architecture %s\n",
868 debuglink_file.c_str(), debug_architecture,
869 obj_filename.c_str(), architecture);
873 bool debug_big_endian;
874 if (!ElfEndianness<ElfClass>(debug_elf_header, &debug_big_endian))
876 if (debug_big_endian != big_endian) {
877 fprintf(stderr, "%s and %s does not match in endianness\n",
878 obj_filename.c_str(), debuglink_file.c_str());
882 if (!LoadSymbols<ElfClass>(debuglink_file, debug_big_endian,
883 debug_elf_header, false, &info,
884 options, module.get())) {
889 *out_module = module.release();
895 namespace google_breakpad {
897 // Not explicitly exported, but not static so it can be used in unit tests.
898 bool ReadSymbolDataInternal(const uint8_t* obj_file,
899 const string& obj_filename,
900 const std::vector<string>& debug_dirs,
901 const DumpOptions& options,
903 if (!IsValidElf(obj_file)) {
904 fprintf(stderr, "Not a valid ELF file: %s\n", obj_filename.c_str());
908 int elfclass = ElfClass(obj_file);
909 if (elfclass == ELFCLASS32) {
910 return ReadSymbolDataElfClass<ElfClass32>(
911 reinterpret_cast<const Elf32_Ehdr*>(obj_file), obj_filename, debug_dirs,
914 if (elfclass == ELFCLASS64) {
915 return ReadSymbolDataElfClass<ElfClass64>(
916 reinterpret_cast<const Elf64_Ehdr*>(obj_file), obj_filename, debug_dirs,
923 bool WriteSymbolFile(const string &obj_file,
924 const std::vector<string>& debug_dirs,
925 const DumpOptions& options,
926 std::ostream &sym_stream) {
928 if (!ReadSymbolData(obj_file, debug_dirs, options, &module))
931 bool result = module->Write(sym_stream, options.symbol_data);
936 bool ReadSymbolData(const string& obj_file,
937 const std::vector<string>& debug_dirs,
938 const DumpOptions& options,
940 MmapWrapper map_wrapper;
941 void* elf_header = NULL;
942 if (!LoadELF(obj_file, &map_wrapper, &elf_header))
945 return ReadSymbolDataInternal(reinterpret_cast<uint8_t*>(elf_header),
946 obj_file, debug_dirs, options, module);
949 } // namespace google_breakpad