Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / breakpad / src / common / linux / dump_symbols.cc
1 // Copyright (c) 2011 Google Inc.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
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
13 // distribution.
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.
17 //
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.
29
30 // Restructured in 2009 by: Jim Blandy <jimb@mozilla.com> <jimb@red-bean.com>
31
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.
34
35 #include "common/linux/dump_symbols.h"
36
37 #include <assert.h>
38 #include <elf.h>
39 #include <errno.h>
40 #include <fcntl.h>
41 #include <link.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <sys/mman.h>
46 #include <sys/stat.h>
47 #include <unistd.h>
48
49 #include <iostream>
50 #include <set>
51 #include <string>
52 #include <utility>
53 #include <vector>
54
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"
71 #endif
72 #include "common/using_std_string.h"
73
74 // This namespace contains helper functions.
75 namespace {
76
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;
90 #endif
91 using google_breakpad::scoped_ptr;
92
93 // Define AARCH64 ELF architecture if host machine does not include this define.
94 #ifndef EM_AARCH64
95 #define EM_AARCH64      183
96 #endif
97
98 //
99 // FDWrapper
100 //
101 // Wrapper class to make sure opened file is closed.
102 //
103 class FDWrapper {
104  public:
105   explicit FDWrapper(int fd) :
106     fd_(fd) {}
107   ~FDWrapper() {
108     if (fd_ != -1)
109       close(fd_);
110   }
111   int get() {
112     return fd_;
113   }
114   int release() {
115     int fd = fd_;
116     fd_ = -1;
117     return fd;
118   }
119  private:
120   int fd_;
121 };
122
123 //
124 // MmapWrapper
125 //
126 // Wrapper class to make sure mapped regions are unmapped.
127 //
128 class MmapWrapper {
129  public:
130   MmapWrapper() : is_set_(false) {}
131   ~MmapWrapper() {
132     if (is_set_ && base_ != NULL) {
133       assert(size_ > 0);
134       munmap(base_, size_);
135     }
136   }
137   void set(void *mapped_address, size_t mapped_size) {
138     is_set_ = true;
139     base_ = mapped_address;
140     size_ = mapped_size;
141   }
142   void release() {
143     assert(is_set_);
144     is_set_ = false;
145     base_ = NULL;
146     size_ = 0;
147   }
148
149  private:
150   bool is_set_;
151   void* base_;
152   size_t size_;
153 };
154
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,
159     int nheader) {
160   typedef typename ElfClass::Phdr Phdr;
161
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
166   // normally be zero.
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;
171   }
172   return 0;
173 }
174
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,
181                Module* module) {
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())
197     return false;
198   handler.Finalize();
199   return true;
200 }
201 #endif  // NO_STABS_SUPPORT
202
203 // A line-to-module loader that accepts line number info parsed by
204 // dwarf2reader::LineInfo and populates a Module and a line vector
205 // with the results.
206 class DumperLineToModule: public DwarfCUToModule::LineToModuleHandler {
207  public:
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;
213   }
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);
218     parser.Start();
219   }
220  private:
221   string compilation_dir_;
222   dwarf2reader::ByteReader *byte_reader_;
223 };
224
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,
230                Module* module) {
231   typedef typename ElfClass::Shdr Shdr;
232
233   const dwarf2reader::Endianness endianness = big_endian ?
234       dwarf2reader::ENDIANNESS_BIG : dwarf2reader::ENDIANNESS_LITTLE;
235   dwarf2reader::ByteReader byte_reader(endianness);
236
237   // Construct a context for this file.
238   DwarfCUToModule::FileContext file_context(dwarf_filename,
239                                             module,
240                                             handle_inter_cu_refs);
241
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 = &sections[i];
249     string name = GetOffset<ElfClass, char>(elf_header,
250                                             section_names->sh_offset) +
251                   section->sh_name;
252     const char* contents = GetOffset<ElfClass, char>(elf_header,
253                                                      section->sh_offset);
254     file_context.AddSectionToSectionMap(name, contents, section->sh_size);
255   }
256
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(),
277                                          offset,
278                                          &byte_reader,
279                                          &die_dispatcher);
280     // Process the entire compilation unit; get the offset of the next.
281     offset += reader.Start();
282   }
283   return true;
284 }
285
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
290 // supported.
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) {
295     case EM_386:
296       *register_names = DwarfCFIToModule::RegisterNames::I386();
297       return true;
298     case EM_ARM:
299       *register_names = DwarfCFIToModule::RegisterNames::ARM();
300       return true;
301     case EM_AARCH64:
302       *register_names = DwarfCFIToModule::RegisterNames::ARM64();
303       return true;
304     case EM_MIPS:
305       *register_names = DwarfCFIToModule::RegisterNames::MIPS();
306       return true;
307     case EM_X86_64:
308       *register_names = DwarfCFIToModule::RegisterNames::X86_64();
309       return true;
310     default:
311       return false;
312   }
313 }
314
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,
320                   const bool eh_frame,
321                   const typename ElfClass::Shdr* got_section,
322                   const typename ElfClass::Shdr* text_section,
323                   const bool big_endian,
324                   Module* module) {
325   // Find the appropriate set of register names for this file's
326   // architecture.
327   std::vector<string> register_names;
328   if (!DwarfCFIRegisterNames<ElfClass>(elf_header, &register_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);
332     return false;
333   }
334
335   const dwarf2reader::Endianness endianness = big_endian ?
336       dwarf2reader::ENDIANNESS_BIG : dwarf2reader::ENDIANNESS_LITTLE;
337
338   // Find the call frame information and its size.
339   const char* cfi =
340       GetOffset<ElfClass, char>(elf_header, section->sh_offset);
341   size_t cfi_size = section->sh_size;
342
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);
347
348   byte_reader.SetAddressSize(ElfClass::kAddrSize);
349
350   // Provide the base addresses for .eh_frame encoded pointers, if
351   // possible.
352   byte_reader.SetCFIDataBase(section->sh_addr, cfi);
353   if (got_section)
354     byte_reader.SetDataBase(got_section->sh_addr);
355   if (text_section)
356     byte_reader.SetTextBase(text_section->sh_addr);
357
358   dwarf2reader::CallFrameInfo::Reporter dwarf_reporter(dwarf_filename,
359                                                        section_name);
360   dwarf2reader::CallFrameInfo parser(cfi, cfi_size,
361                                      &byte_reader, &handler, &dwarf_reporter,
362                                      eh_frame);
363   parser.Start();
364   return true;
365 }
366
367 bool LoadELF(const string& obj_file, MmapWrapper* map_wrapper,
368              void** elf_header) {
369   int obj_fd = open(obj_file.c_str(), O_RDONLY);
370   if (obj_fd < 0) {
371     fprintf(stderr, "Failed to open ELF file '%s': %s\n",
372             obj_file.c_str(), strerror(errno));
373     return false;
374   }
375   FDWrapper obj_fd_wrapper(obj_fd);
376   struct stat st;
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));
380     return false;
381   }
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));
387     return false;
388   }
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());
393     return false;
394   }
395   return true;
396 }
397
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,
401                    bool* big_endian) {
402   if (elf_header->e_ident[EI_DATA] == ELFDATA2LSB) {
403     *big_endian = false;
404     return true;
405   }
406   if (elf_header->e_ident[EI_DATA] == ELFDATA2MSB) {
407     *big_endian = true;
408     return true;
409   }
410
411   fprintf(stderr, "bad data encoding in ELF header: %d\n",
412           elf_header->e_ident[EI_DATA]);
413   return false;
414 }
415
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.
425
426   // Sanity check.
427   if (debuglink_len != debuglink_size) {
428     fprintf(stderr, "Mismatched .gnu_debuglink string / section size: "
429             "%zx %zx\n", debuglink_len, debuglink_size);
430     return string();
431   }
432
433   bool found = false;
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)
442       continue;
443
444     FDWrapper debuglink_fd_wrapper(debuglink_fd);
445
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]);
452
453     uint32_t actual_crc = 0;
454     while (true) {
455       const size_t kReadSize = 4096;
456       char buf[kReadSize];
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());
461         return string();
462       }
463       if (bytes_read == 0)
464         break;
465       actual_crc = google_breakpad::UpdateCrc32(actual_crc, buf, bytes_read);
466     }
467     if (actual_crc != expected_crc) {
468       fprintf(stderr, "Error reading debug ELF file - CRC32 mismatch: %s\n",
469               debuglink_path.c_str());
470       continue;
471     }
472     found = true;
473     break;
474   }
475
476   if (!found) {
477     fprintf(stderr, "Failed to find debug ELF file for '%s' after trying:\n",
478             obj_file.c_str());
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);
482     }
483     return string();
484   }
485
486   return debuglink_path;
487 }
488
489 //
490 // LoadSymbolsInfo
491 //
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
494 // different file.
495 //
496 template<typename ElfClass>
497 class LoadSymbolsInfo {
498  public:
499   typedef typename ElfClass::Addr Addr;
500
501   explicit LoadSymbolsInfo(const std::vector<string>& dbg_dirs) :
502     debug_dirs_(dbg_dirs),
503     has_loading_addr_(false) {}
504
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 &section) {
508     if (loaded_sections_.count(section) == 0) {
509       loaded_sections_.insert(section);
510     } else {
511       fprintf(stderr, "Section %s has already been loaded.\n",
512               section.c_str());
513     }
514   }
515
516   // The ELF file and linked debug file are expected to have the same preferred
517   // loading address.
518   void set_loading_addr(Addr addr, const string &filename) {
519     if (!has_loading_addr_) {
520       loading_addr_ = addr;
521       loaded_file_ = filename;
522       return;
523     }
524
525     if (addr != loading_addr_) {
526       fprintf(stderr,
527               "ELF file '%s' and debug ELF file '%s' "
528               "have different load addresses.\n",
529               loaded_file_.c_str(), filename.c_str());
530       assert(false);
531     }
532   }
533
534   // Setters and getters
535   const std::vector<string>& debug_dirs() const {
536     return debug_dirs_;
537   }
538
539   string debuglink_file() const {
540     return debuglink_file_;
541   }
542   void set_debuglink_file(string file) {
543     debuglink_file_ = file;
544   }
545
546  private:
547   const std::vector<string>& debug_dirs_; // Directories in which to
548                                           // search for the debug ELF file.
549
550   string debuglink_file_;  // Full path to the debug ELF file.
551
552   bool has_loading_addr_;  // Indicate if LOADING_ADDR_ is valid.
553
554   Addr loading_addr_;  // Saves the preferred loading address from the
555                        // first call to LoadSymbols().
556
557   string loaded_file_;  // Name of the file loaded from the first call to
558                         // LoadSymbols().
559
560   std::set<string> loaded_sections_;  // Tracks the Loaded ELF sections
561                                       // between calls to LoadSymbols().
562 };
563
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,
571                  Module* module) {
572   typedef typename ElfClass::Addr Addr;
573   typedef typename ElfClass::Phdr Phdr;
574   typedef typename ElfClass::Shdr Shdr;
575   typedef typename ElfClass::Word Word;
576
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);
582
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;
588   const char* names =
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;
593
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);
601     if (stab_section) {
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());
611         }
612       }
613     }
614 #endif  // NO_STABS_SUPPORT
615
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);
621     if (dwarf_section) {
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());
629       }
630     }
631   }
632
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
643       // useful.
644       info->LoadedSection(".debug_frame");
645       bool result =
646           LoadDwarfCFI<ElfClass>(obj_file, elf_header, ".debug_frame",
647                                  dwarf_cfi_section, false, 0, 0, big_endian,
648                                  module);
649       found_usable_info = found_usable_info || result;
650     }
651
652     // Linux C++ exception handling information can also provide
653     // unwinding data.
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.
671       bool result =
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;
676     }
677   }
678
679   if (!found_debug_info_section) {
680     fprintf(stderr, "%s: file contains no debugging information"
681             " (no \".stab\" or \".debug_info\" sections)\n",
682             obj_file.c_str());
683
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,
688                                            sections, names,
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,
698                             big_endian,
699                             obj_file,
700                             info->debug_dirs());
701           info->set_debuglink_file(debuglink_file);
702         } else {
703           fprintf(stderr, ".gnu_debuglink section found in '%s', "
704                   "but no debug path specified.\n", obj_file.c_str());
705         }
706       } else {
707         fprintf(stderr, "%s does not contain a .gnu_debuglink section.\n",
708                 obj_file.c_str());
709       }
710     } else {
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");
724
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);
731           bool result =
732               ELFSymbolsToModule(dynsyms,
733                                  dynsym_section->sh_size,
734                                  dynstrs,
735                                  dynstr_section->sh_size,
736                                  big_endian,
737                                  ElfClass::kAddrSize,
738                                  module);
739           found_usable_info = found_usable_info || result;
740         }
741       }
742
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;
746     }
747
748     // No debug info was found, let the user try again with .gnu_debuglink
749     // if present.
750     return false;
751   }
752
753   return true;
754 }
755
756 // Return the breakpad symbol file identifier for the architecture of
757 // ELF_HEADER.
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;
762   switch (arch) {
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;
774   }
775 }
776
777 // Format the Elf file identifier in IDENTIFIER as a UUID with the
778 // dashes removed.
779 string FormatIdentifier(unsigned char identifier[16]) {
780   char identifier_str[40];
781   google_breakpad::FileID::ConvertIdentifierToString(
782       identifier,
783       identifier_str,
784       sizeof(identifier_str));
785   string id_no_dash;
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.
792   id_no_dash += '0';
793   return id_no_dash;
794 }
795
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);
802   free(c_filename);
803   return base;
804 }
805
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;
814
815   *out_module = NULL;
816
817   unsigned char identifier[16];
818   if (!google_breakpad::FileID::ElfFileIdentifierFromMappedFile(elf_header,
819                                                                 identifier)) {
820     fprintf(stderr, "%s: unable to generate file identifier\n",
821             obj_filename.c_str());
822     return false;
823   }
824
825   const char *architecture = ElfArchitecture<ElfClass>(elf_header);
826   if (!architecture) {
827     fprintf(stderr, "%s: unrecognized ELF machine architecture: %d\n",
828             obj_filename.c_str(), elf_header->e_machine);
829     return false;
830   }
831
832   // Figure out what endianness this file is.
833   bool big_endian;
834   if (!ElfEndianness<ElfClass>(elf_header, &big_endian))
835     return false;
836
837   string name = BaseFileName(obj_filename);
838   string os = "Linux";
839   string id = FormatIdentifier(identifier);
840
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())
848       return false;
849
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)))
856       return false;
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);
863       return false;
864     }
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);
870       return false;
871     }
872
873     bool debug_big_endian;
874     if (!ElfEndianness<ElfClass>(debug_elf_header, &debug_big_endian))
875       return false;
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());
879       return false;
880     }
881
882     if (!LoadSymbols<ElfClass>(debuglink_file, debug_big_endian,
883                                debug_elf_header, false, &info,
884                                options, module.get())) {
885       return false;
886     }
887   }
888
889   *out_module = module.release();
890   return true;
891 }
892
893 }  // namespace
894
895 namespace google_breakpad {
896
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,
902                             Module** module) {
903   if (!IsValidElf(obj_file)) {
904     fprintf(stderr, "Not a valid ELF file: %s\n", obj_filename.c_str());
905     return false;
906   }
907
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,
912         options, module);
913   }
914   if (elfclass == ELFCLASS64) {
915     return ReadSymbolDataElfClass<ElfClass64>(
916         reinterpret_cast<const Elf64_Ehdr*>(obj_file), obj_filename, debug_dirs,
917         options, module);
918   }
919
920   return false;
921 }
922
923 bool WriteSymbolFile(const string &obj_file,
924                      const std::vector<string>& debug_dirs,
925                      const DumpOptions& options,
926                      std::ostream &sym_stream) {
927   Module* module;
928   if (!ReadSymbolData(obj_file, debug_dirs, options, &module))
929     return false;
930
931   bool result = module->Write(sym_stream, options.symbol_data);
932   delete module;
933   return result;
934 }
935
936 bool ReadSymbolData(const string& obj_file,
937                     const std::vector<string>& debug_dirs,
938                     const DumpOptions& options,
939                     Module** module) {
940   MmapWrapper map_wrapper;
941   void* elf_header = NULL;
942   if (!LoadELF(obj_file, &map_wrapper, &elf_header))
943     return false;
944
945   return ReadSymbolDataInternal(reinterpret_cast<uint8_t*>(elf_header),
946                                 obj_file, debug_dirs, options, module);
947 }
948
949 }  // namespace google_breakpad