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 <stdint.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <sys/mman.h>
47 #include <sys/stat.h>
48 #include <unistd.h>
49 
50 #include <iostream>
51 #include <set>
52 #include <string>
53 #include <utility>
54 #include <vector>
55 
56 #include "common/arm_ex_reader.h"
57 #include "common/dwarf/bytereader-inl.h"
58 #include "common/dwarf/dwarf2diehandler.h"
59 #include "common/dwarf_cfi_to_module.h"
60 #include "common/dwarf_cu_to_module.h"
61 #include "common/dwarf_line_to_module.h"
62 #include "common/linux/crc32.h"
63 #include "common/linux/eintr_wrapper.h"
64 #include "common/linux/elfutils.h"
65 #include "common/linux/elfutils-inl.h"
66 #include "common/linux/elf_symbols_to_module.h"
67 #include "common/linux/file_id.h"
68 #include "common/memory_allocator.h"
69 #include "common/module.h"
70 #include "common/scoped_ptr.h"
71 #ifndef NO_STABS_SUPPORT
72 #include "common/stabs_reader.h"
73 #include "common/stabs_to_module.h"
74 #endif
75 #include "common/using_std_string.h"
76 
77 #ifndef SHT_ARM_EXIDX
78 // bionic and older glibc don't define this
79 # define SHT_ARM_EXIDX (SHT_LOPROC + 1)
80 #endif
81 
82 // This namespace contains helper functions.
83 namespace {
84 
85 using google_breakpad::DumpOptions;
86 using google_breakpad::DwarfCFIToModule;
87 using google_breakpad::DwarfCUToModule;
88 using google_breakpad::DwarfLineToModule;
89 using google_breakpad::ElfClass;
90 using google_breakpad::ElfClass32;
91 using google_breakpad::ElfClass64;
92 using google_breakpad::FileID;
93 using google_breakpad::FindElfSectionByName;
94 using google_breakpad::GetOffset;
95 using google_breakpad::IsValidElf;
96 using google_breakpad::kDefaultBuildIdSize;
97 using google_breakpad::Module;
98 using google_breakpad::PageAllocator;
99 #ifndef NO_STABS_SUPPORT
100 using google_breakpad::StabsToModule;
101 #endif
102 using google_breakpad::scoped_ptr;
103 using google_breakpad::wasteful_vector;
104 
105 // Define AARCH64 ELF architecture if host machine does not include this define.
106 #ifndef EM_AARCH64
107 #define EM_AARCH64      183
108 #endif
109 
110 // Define SHT_ANDROID_REL and SHT_ANDROID_RELA if not defined by the host.
111 // Sections with this type contain Android packed relocations.
112 #ifndef SHT_ANDROID_REL
113 #define SHT_ANDROID_REL  (SHT_LOOS + 1)
114 #endif
115 #ifndef SHT_ANDROID_RELA
116 #define SHT_ANDROID_RELA (SHT_LOOS + 2)
117 #endif
118 
119 //
120 // FDWrapper
121 //
122 // Wrapper class to make sure opened file is closed.
123 //
124 class FDWrapper {
125  public:
FDWrapper(int fd)126   explicit FDWrapper(int fd) :
127     fd_(fd) {}
~FDWrapper()128   ~FDWrapper() {
129     if (fd_ != -1)
130       close(fd_);
131   }
get()132   int get() {
133     return fd_;
134   }
release()135   int release() {
136     int fd = fd_;
137     fd_ = -1;
138     return fd;
139   }
140  private:
141   int fd_;
142 };
143 
144 //
145 // MmapWrapper
146 //
147 // Wrapper class to make sure mapped regions are unmapped.
148 //
149 class MmapWrapper {
150  public:
MmapWrapper()151   MmapWrapper() : is_set_(false) {}
~MmapWrapper()152   ~MmapWrapper() {
153     if (is_set_ && base_ != NULL) {
154       assert(size_ > 0);
155       munmap(base_, size_);
156     }
157   }
set(void * mapped_address,size_t mapped_size)158   void set(void *mapped_address, size_t mapped_size) {
159     is_set_ = true;
160     base_ = mapped_address;
161     size_ = mapped_size;
162   }
release()163   void release() {
164     assert(is_set_);
165     is_set_ = false;
166     base_ = NULL;
167     size_ = 0;
168   }
169 
170  private:
171   bool is_set_;
172   void* base_;
173   size_t size_;
174 };
175 
176 // Find the preferred loading address of the binary.
177 template<typename ElfClass>
GetLoadingAddress(const typename ElfClass::Phdr * program_headers,int nheader)178 typename ElfClass::Addr GetLoadingAddress(
179     const typename ElfClass::Phdr* program_headers,
180     int nheader) {
181   typedef typename ElfClass::Phdr Phdr;
182 
183   // For non-PIC executables (e_type == ET_EXEC), the load address is
184   // the start address of the first PT_LOAD segment.  (ELF requires
185   // the segments to be sorted by load address.)  For PIC executables
186   // and dynamic libraries (e_type == ET_DYN), this address will
187   // normally be zero.
188   for (int i = 0; i < nheader; ++i) {
189     const Phdr& header = program_headers[i];
190     if (header.p_type == PT_LOAD)
191       return header.p_vaddr;
192   }
193   return 0;
194 }
195 
196 #ifndef NO_STABS_SUPPORT
197 template<typename ElfClass>
LoadStabs(const typename ElfClass::Ehdr * elf_header,const typename ElfClass::Shdr * stab_section,const typename ElfClass::Shdr * stabstr_section,const bool big_endian,Module * module)198 bool LoadStabs(const typename ElfClass::Ehdr* elf_header,
199                const typename ElfClass::Shdr* stab_section,
200                const typename ElfClass::Shdr* stabstr_section,
201                const bool big_endian,
202                Module* module) {
203   // A callback object to handle data from the STABS reader.
204   StabsToModule handler(module);
205   // Find the addresses of the STABS data, and create a STABS reader object.
206   // On Linux, STABS entries always have 32-bit values, regardless of the
207   // address size of the architecture whose code they're describing, and
208   // the strings are always "unitized".
209   const uint8_t* stabs =
210       GetOffset<ElfClass, uint8_t>(elf_header, stab_section->sh_offset);
211   const uint8_t* stabstr =
212       GetOffset<ElfClass, uint8_t>(elf_header, stabstr_section->sh_offset);
213   google_breakpad::StabsReader reader(stabs, stab_section->sh_size,
214                                       stabstr, stabstr_section->sh_size,
215                                       big_endian, 4, true, &handler);
216   // Read the STABS data, and do post-processing.
217   if (!reader.Process())
218     return false;
219   handler.Finalize();
220   return true;
221 }
222 #endif  // NO_STABS_SUPPORT
223 
224 // A line-to-module loader that accepts line number info parsed by
225 // dwarf2reader::LineInfo and populates a Module and a line vector
226 // with the results.
227 class DumperLineToModule: public DwarfCUToModule::LineToModuleHandler {
228  public:
229   // Create a line-to-module converter using BYTE_READER.
DumperLineToModule(dwarf2reader::ByteReader * byte_reader)230   explicit DumperLineToModule(dwarf2reader::ByteReader *byte_reader)
231       : byte_reader_(byte_reader) { }
StartCompilationUnit(const string & compilation_dir)232   void StartCompilationUnit(const string& compilation_dir) {
233     compilation_dir_ = compilation_dir;
234   }
ReadProgram(const uint8_t * program,uint64 length,Module * module,std::vector<Module::Line> * lines)235   void ReadProgram(const uint8_t *program, uint64 length,
236                    Module* module, std::vector<Module::Line>* lines) {
237     DwarfLineToModule handler(module, compilation_dir_, lines);
238     dwarf2reader::LineInfo parser(program, length, byte_reader_, &handler);
239     parser.Start();
240   }
241  private:
242   string compilation_dir_;
243   dwarf2reader::ByteReader *byte_reader_;
244 };
245 
246 template<typename ElfClass>
LoadDwarf(const string & dwarf_filename,const typename ElfClass::Ehdr * elf_header,const bool big_endian,bool handle_inter_cu_refs,Module * module)247 bool LoadDwarf(const string& dwarf_filename,
248                const typename ElfClass::Ehdr* elf_header,
249                const bool big_endian,
250                bool handle_inter_cu_refs,
251                Module* module) {
252   typedef typename ElfClass::Shdr Shdr;
253 
254   const dwarf2reader::Endianness endianness = big_endian ?
255       dwarf2reader::ENDIANNESS_BIG : dwarf2reader::ENDIANNESS_LITTLE;
256   dwarf2reader::ByteReader byte_reader(endianness);
257 
258   // Construct a context for this file.
259   DwarfCUToModule::FileContext file_context(dwarf_filename,
260                                             module,
261                                             handle_inter_cu_refs);
262 
263   // Build a map of the ELF file's sections.
264   const Shdr* sections =
265       GetOffset<ElfClass, Shdr>(elf_header, elf_header->e_shoff);
266   int num_sections = elf_header->e_shnum;
267   const Shdr* section_names = sections + elf_header->e_shstrndx;
268   for (int i = 0; i < num_sections; i++) {
269     const Shdr* section = &sections[i];
270     string name = GetOffset<ElfClass, char>(elf_header,
271                                             section_names->sh_offset) +
272                   section->sh_name;
273     const uint8_t *contents = GetOffset<ElfClass, uint8_t>(elf_header,
274                                                            section->sh_offset);
275     file_context.AddSectionToSectionMap(name, contents, section->sh_size);
276   }
277 
278   // Parse all the compilation units in the .debug_info section.
279   DumperLineToModule line_to_module(&byte_reader);
280   dwarf2reader::SectionMap::const_iterator debug_info_entry =
281       file_context.section_map().find(".debug_info");
282   assert(debug_info_entry != file_context.section_map().end());
283   const std::pair<const uint8_t *, uint64>& debug_info_section =
284       debug_info_entry->second;
285   // This should never have been called if the file doesn't have a
286   // .debug_info section.
287   assert(debug_info_section.first);
288   uint64 debug_info_length = debug_info_section.second;
289   for (uint64 offset = 0; offset < debug_info_length;) {
290     // Make a handler for the root DIE that populates MODULE with the
291     // data that was found.
292     DwarfCUToModule::WarningReporter reporter(dwarf_filename, offset);
293     DwarfCUToModule root_handler(&file_context, &line_to_module, &reporter);
294     // Make a Dwarf2Handler that drives the DIEHandler.
295     dwarf2reader::DIEDispatcher die_dispatcher(&root_handler);
296     // Make a DWARF parser for the compilation unit at OFFSET.
297     dwarf2reader::CompilationUnit reader(dwarf_filename,
298                                          file_context.section_map(),
299                                          offset,
300                                          &byte_reader,
301                                          &die_dispatcher);
302     // Process the entire compilation unit; get the offset of the next.
303     offset += reader.Start();
304   }
305   return true;
306 }
307 
308 // Fill REGISTER_NAMES with the register names appropriate to the
309 // machine architecture given in HEADER, indexed by the register
310 // numbers used in DWARF call frame information. Return true on
311 // success, or false if HEADER's machine architecture is not
312 // supported.
313 template<typename ElfClass>
DwarfCFIRegisterNames(const typename ElfClass::Ehdr * elf_header,std::vector<string> * register_names)314 bool DwarfCFIRegisterNames(const typename ElfClass::Ehdr* elf_header,
315                            std::vector<string>* register_names) {
316   switch (elf_header->e_machine) {
317     case EM_386:
318       *register_names = DwarfCFIToModule::RegisterNames::I386();
319       return true;
320     case EM_ARM:
321       *register_names = DwarfCFIToModule::RegisterNames::ARM();
322       return true;
323     case EM_AARCH64:
324       *register_names = DwarfCFIToModule::RegisterNames::ARM64();
325       return true;
326     case EM_MIPS:
327       *register_names = DwarfCFIToModule::RegisterNames::MIPS();
328       return true;
329     case EM_X86_64:
330       *register_names = DwarfCFIToModule::RegisterNames::X86_64();
331       return true;
332     default:
333       return false;
334   }
335 }
336 
337 template<typename ElfClass>
LoadDwarfCFI(const string & dwarf_filename,const typename ElfClass::Ehdr * elf_header,const char * section_name,const typename ElfClass::Shdr * section,const bool eh_frame,const typename ElfClass::Shdr * got_section,const typename ElfClass::Shdr * text_section,const bool big_endian,Module * module)338 bool LoadDwarfCFI(const string& dwarf_filename,
339                   const typename ElfClass::Ehdr* elf_header,
340                   const char* section_name,
341                   const typename ElfClass::Shdr* section,
342                   const bool eh_frame,
343                   const typename ElfClass::Shdr* got_section,
344                   const typename ElfClass::Shdr* text_section,
345                   const bool big_endian,
346                   Module* module) {
347   // Find the appropriate set of register names for this file's
348   // architecture.
349   std::vector<string> register_names;
350   if (!DwarfCFIRegisterNames<ElfClass>(elf_header, &register_names)) {
351     fprintf(stderr, "%s: unrecognized ELF machine architecture '%d';"
352             " cannot convert DWARF call frame information\n",
353             dwarf_filename.c_str(), elf_header->e_machine);
354     return false;
355   }
356 
357   const dwarf2reader::Endianness endianness = big_endian ?
358       dwarf2reader::ENDIANNESS_BIG : dwarf2reader::ENDIANNESS_LITTLE;
359 
360   // Find the call frame information and its size.
361   const uint8_t *cfi =
362       GetOffset<ElfClass, uint8_t>(elf_header, section->sh_offset);
363   size_t cfi_size = section->sh_size;
364 
365   // Plug together the parser, handler, and their entourages.
366   DwarfCFIToModule::Reporter module_reporter(dwarf_filename, section_name);
367   DwarfCFIToModule handler(module, register_names, &module_reporter);
368   dwarf2reader::ByteReader byte_reader(endianness);
369 
370   byte_reader.SetAddressSize(ElfClass::kAddrSize);
371 
372   // Provide the base addresses for .eh_frame encoded pointers, if
373   // possible.
374   byte_reader.SetCFIDataBase(section->sh_addr, cfi);
375   if (got_section)
376     byte_reader.SetDataBase(got_section->sh_addr);
377   if (text_section)
378     byte_reader.SetTextBase(text_section->sh_addr);
379 
380   dwarf2reader::CallFrameInfo::Reporter dwarf_reporter(dwarf_filename,
381                                                        section_name);
382   dwarf2reader::CallFrameInfo parser(cfi, cfi_size,
383                                      &byte_reader, &handler, &dwarf_reporter,
384                                      eh_frame);
385   parser.Start();
386   return true;
387 }
388 
389 template<typename ElfClass>
LoadARMexidx(const typename ElfClass::Ehdr * elf_header,const typename ElfClass::Shdr * exidx_section,const typename ElfClass::Shdr * extab_section,uint32_t loading_addr,Module * module)390 bool LoadARMexidx(const typename ElfClass::Ehdr* elf_header,
391                   const typename ElfClass::Shdr* exidx_section,
392                   const typename ElfClass::Shdr* extab_section,
393                   uint32_t loading_addr,
394                   Module* module) {
395   // To do this properly we need to know:
396   // * the bounds of the .ARM.exidx section in the mapped image
397   // * the bounds of the .ARM.extab section in the mapped image
398   // * the vma of the last byte in the text section associated with the .exidx
399   // The first two are easy.  The third is a bit tricky.  If we can't
400   // figure out what it is, just pass in zero.
401   const char *exidx_img
402     = GetOffset<ElfClass, char>(elf_header, exidx_section->sh_offset);
403   size_t exidx_size = exidx_section->sh_size;
404   const char *extab_img
405     = GetOffset<ElfClass, char>(elf_header, extab_section->sh_offset);
406   size_t extab_size = extab_section->sh_size;
407 
408   // The sh_link field of the exidx section gives the section number
409   // for the associated text section.
410   uint32_t exidx_text_last_svma = 0;
411   int exidx_text_sno = exidx_section->sh_link;
412   typedef typename ElfClass::Shdr Shdr;
413   // |sections| points to the section header table
414   const Shdr* sections
415     = GetOffset<ElfClass, Shdr>(elf_header, elf_header->e_shoff);
416   const int num_sections = elf_header->e_shnum;
417   if (exidx_text_sno >= 0 && exidx_text_sno < num_sections) {
418     const Shdr* exidx_text_shdr = &sections[exidx_text_sno];
419     if (exidx_text_shdr->sh_size > 0) {
420       exidx_text_last_svma
421         = exidx_text_shdr->sh_addr + exidx_text_shdr->sh_size - 1;
422     }
423   }
424 
425   arm_ex_to_module::ARMExToModule handler(module);
426   arm_ex_reader::ExceptionTableInfo
427     parser(exidx_img, exidx_size, extab_img, extab_size, exidx_text_last_svma,
428            &handler,
429            reinterpret_cast<const char*>(elf_header),
430            loading_addr);
431   parser.Start();
432   return true;
433 }
434 
LoadELF(const string & obj_file,MmapWrapper * map_wrapper,void ** elf_header)435 bool LoadELF(const string& obj_file, MmapWrapper* map_wrapper,
436              void** elf_header) {
437   int obj_fd = open(obj_file.c_str(), O_RDONLY);
438   if (obj_fd < 0) {
439     fprintf(stderr, "Failed to open ELF file '%s': %s\n",
440             obj_file.c_str(), strerror(errno));
441     return false;
442   }
443   FDWrapper obj_fd_wrapper(obj_fd);
444   struct stat st;
445   if (fstat(obj_fd, &st) != 0 && st.st_size <= 0) {
446     fprintf(stderr, "Unable to fstat ELF file '%s': %s\n",
447             obj_file.c_str(), strerror(errno));
448     return false;
449   }
450   void* obj_base = mmap(NULL, st.st_size,
451                         PROT_READ | PROT_WRITE, MAP_PRIVATE, obj_fd, 0);
452   if (obj_base == MAP_FAILED) {
453     fprintf(stderr, "Failed to mmap ELF file '%s': %s\n",
454             obj_file.c_str(), strerror(errno));
455     return false;
456   }
457   map_wrapper->set(obj_base, st.st_size);
458   *elf_header = obj_base;
459   if (!IsValidElf(*elf_header)) {
460     fprintf(stderr, "Not a valid ELF file: %s\n", obj_file.c_str());
461     return false;
462   }
463   return true;
464 }
465 
466 // Get the endianness of ELF_HEADER. If it's invalid, return false.
467 template<typename ElfClass>
ElfEndianness(const typename ElfClass::Ehdr * elf_header,bool * big_endian)468 bool ElfEndianness(const typename ElfClass::Ehdr* elf_header,
469                    bool* big_endian) {
470   if (elf_header->e_ident[EI_DATA] == ELFDATA2LSB) {
471     *big_endian = false;
472     return true;
473   }
474   if (elf_header->e_ident[EI_DATA] == ELFDATA2MSB) {
475     *big_endian = true;
476     return true;
477   }
478 
479   fprintf(stderr, "bad data encoding in ELF header: %d\n",
480           elf_header->e_ident[EI_DATA]);
481   return false;
482 }
483 
484 // Given |left_abspath|, find the absolute path for |right_path| and see if the
485 // two absolute paths are the same.
IsSameFile(const char * left_abspath,const string & right_path)486 bool IsSameFile(const char* left_abspath, const string& right_path) {
487   char right_abspath[PATH_MAX];
488   if (!realpath(right_path.c_str(), right_abspath))
489     return false;
490   return strcmp(left_abspath, right_abspath) == 0;
491 }
492 
493 // Read the .gnu_debuglink and get the debug file name. If anything goes
494 // wrong, return an empty string.
ReadDebugLink(const uint8_t * debuglink,const size_t debuglink_size,const bool big_endian,const string & obj_file,const std::vector<string> & debug_dirs)495 string ReadDebugLink(const uint8_t *debuglink,
496                      const size_t debuglink_size,
497                      const bool big_endian,
498                      const string& obj_file,
499                      const std::vector<string>& debug_dirs) {
500   // Include '\0' + CRC32 (4 bytes).
501   size_t debuglink_len = strlen(reinterpret_cast<const char *>(debuglink)) + 5;
502   debuglink_len = 4 * ((debuglink_len + 3) / 4);  // Round up to 4 bytes.
503 
504   // Sanity check.
505   if (debuglink_len != debuglink_size) {
506     fprintf(stderr, "Mismatched .gnu_debuglink string / section size: "
507             "%zx %zx\n", debuglink_len, debuglink_size);
508     return string();
509   }
510 
511   char obj_file_abspath[PATH_MAX];
512   if (!realpath(obj_file.c_str(), obj_file_abspath)) {
513     fprintf(stderr, "Cannot resolve absolute path for %s\n", obj_file.c_str());
514     return string();
515   }
516 
517   std::vector<string> searched_paths;
518   string debuglink_path;
519   std::vector<string>::const_iterator it;
520   for (it = debug_dirs.begin(); it < debug_dirs.end(); ++it) {
521     const string& debug_dir = *it;
522     debuglink_path = debug_dir + "/" +
523                      reinterpret_cast<const char *>(debuglink);
524 
525     // There is the annoying case of /path/to/foo.so having foo.so as the
526     // debug link file name. Thus this may end up opening /path/to/foo.so again,
527     // and there is a small chance of the two files having the same CRC.
528     if (IsSameFile(obj_file_abspath, debuglink_path))
529       continue;
530 
531     searched_paths.push_back(debug_dir);
532     int debuglink_fd = open(debuglink_path.c_str(), O_RDONLY);
533     if (debuglink_fd < 0)
534       continue;
535 
536     FDWrapper debuglink_fd_wrapper(debuglink_fd);
537 
538     // The CRC is the last 4 bytes in |debuglink|.
539     const dwarf2reader::Endianness endianness = big_endian ?
540         dwarf2reader::ENDIANNESS_BIG : dwarf2reader::ENDIANNESS_LITTLE;
541     dwarf2reader::ByteReader byte_reader(endianness);
542     uint32_t expected_crc =
543         byte_reader.ReadFourBytes(&debuglink[debuglink_size - 4]);
544 
545     uint32_t actual_crc = 0;
546     while (true) {
547       const size_t kReadSize = 4096;
548       char buf[kReadSize];
549       ssize_t bytes_read = HANDLE_EINTR(read(debuglink_fd, &buf, kReadSize));
550       if (bytes_read < 0) {
551         fprintf(stderr, "Error reading debug ELF file %s.\n",
552                 debuglink_path.c_str());
553         return string();
554       }
555       if (bytes_read == 0)
556         break;
557       actual_crc = google_breakpad::UpdateCrc32(actual_crc, buf, bytes_read);
558     }
559     if (actual_crc != expected_crc) {
560       fprintf(stderr, "Error reading debug ELF file - CRC32 mismatch: %s\n",
561               debuglink_path.c_str());
562       continue;
563     }
564 
565     // Found debug file.
566     return debuglink_path;
567   }
568 
569   // Not found case.
570   fprintf(stderr, "Failed to find debug ELF file for '%s' after trying:\n",
571           obj_file.c_str());
572   for (it = searched_paths.begin(); it < searched_paths.end(); ++it) {
573     const string& debug_dir = *it;
574     fprintf(stderr, "  %s/%s\n", debug_dir.c_str(), debuglink);
575   }
576   return string();
577 }
578 
579 //
580 // LoadSymbolsInfo
581 //
582 // Holds the state between the two calls to LoadSymbols() in case it's necessary
583 // to follow the .gnu_debuglink section and load debug information from a
584 // different file.
585 //
586 template<typename ElfClass>
587 class LoadSymbolsInfo {
588  public:
589   typedef typename ElfClass::Addr Addr;
590 
LoadSymbolsInfo(const std::vector<string> & dbg_dirs)591   explicit LoadSymbolsInfo(const std::vector<string>& dbg_dirs) :
592     debug_dirs_(dbg_dirs),
593     has_loading_addr_(false) {}
594 
595   // Keeps track of which sections have been loaded so sections don't
596   // accidentally get loaded twice from two different files.
LoadedSection(const string & section)597   void LoadedSection(const string &section) {
598     if (loaded_sections_.count(section) == 0) {
599       loaded_sections_.insert(section);
600     } else {
601       fprintf(stderr, "Section %s has already been loaded.\n",
602               section.c_str());
603     }
604   }
605 
606   // The ELF file and linked debug file are expected to have the same preferred
607   // loading address.
set_loading_addr(Addr addr,const string & filename)608   void set_loading_addr(Addr addr, const string &filename) {
609     if (!has_loading_addr_) {
610       loading_addr_ = addr;
611       loaded_file_ = filename;
612       return;
613     }
614 
615     if (addr != loading_addr_) {
616       fprintf(stderr,
617               "ELF file '%s' and debug ELF file '%s' "
618               "have different load addresses.\n",
619               loaded_file_.c_str(), filename.c_str());
620       assert(false);
621     }
622   }
623 
624   // Setters and getters
debug_dirs() const625   const std::vector<string>& debug_dirs() const {
626     return debug_dirs_;
627   }
628 
debuglink_file() const629   string debuglink_file() const {
630     return debuglink_file_;
631   }
set_debuglink_file(string file)632   void set_debuglink_file(string file) {
633     debuglink_file_ = file;
634   }
635 
636  private:
637   const std::vector<string>& debug_dirs_; // Directories in which to
638                                           // search for the debug ELF file.
639 
640   string debuglink_file_;  // Full path to the debug ELF file.
641 
642   bool has_loading_addr_;  // Indicate if LOADING_ADDR_ is valid.
643 
644   Addr loading_addr_;  // Saves the preferred loading address from the
645                        // first call to LoadSymbols().
646 
647   string loaded_file_;  // Name of the file loaded from the first call to
648                         // LoadSymbols().
649 
650   std::set<string> loaded_sections_;  // Tracks the Loaded ELF sections
651                                       // between calls to LoadSymbols().
652 };
653 
654 template<typename ElfClass>
LoadSymbols(const string & obj_file,const bool big_endian,const typename ElfClass::Ehdr * elf_header,const bool read_gnu_debug_link,LoadSymbolsInfo<ElfClass> * info,const DumpOptions & options,Module * module)655 bool LoadSymbols(const string& obj_file,
656                  const bool big_endian,
657                  const typename ElfClass::Ehdr* elf_header,
658                  const bool read_gnu_debug_link,
659                  LoadSymbolsInfo<ElfClass>* info,
660                  const DumpOptions& options,
661                  Module* module) {
662   typedef typename ElfClass::Addr Addr;
663   typedef typename ElfClass::Phdr Phdr;
664   typedef typename ElfClass::Shdr Shdr;
665   typedef typename ElfClass::Word Word;
666 
667   Addr loading_addr = GetLoadingAddress<ElfClass>(
668       GetOffset<ElfClass, Phdr>(elf_header, elf_header->e_phoff),
669       elf_header->e_phnum);
670   module->SetLoadAddress(loading_addr);
671   info->set_loading_addr(loading_addr, obj_file);
672 
673   Word debug_section_type =
674       elf_header->e_machine == EM_MIPS ? SHT_MIPS_DWARF : SHT_PROGBITS;
675   const Shdr* sections =
676       GetOffset<ElfClass, Shdr>(elf_header, elf_header->e_shoff);
677   const Shdr* section_names = sections + elf_header->e_shstrndx;
678   const char* names =
679       GetOffset<ElfClass, char>(elf_header, section_names->sh_offset);
680   const char *names_end = names + section_names->sh_size;
681   bool found_debug_info_section = false;
682   bool found_usable_info = false;
683 
684   // Reject files that contain Android packed relocations. The pre-packed
685   // version of the file should be symbolized; the packed version is only
686   // intended for use on the target system.
687   if (FindElfSectionByName<ElfClass>(".rel.dyn", SHT_ANDROID_REL,
688                                      sections, names,
689                                      names_end, elf_header->e_shnum)) {
690     fprintf(stderr, "%s: file contains a \".rel.dyn\" section "
691                     "with type SHT_ANDROID_REL\n", obj_file.c_str());
692     fprintf(stderr, "Files containing Android packed relocations "
693                     "may not be symbolized.\n");
694     return false;
695   }
696   if (FindElfSectionByName<ElfClass>(".rela.dyn", SHT_ANDROID_RELA,
697                                      sections, names,
698                                      names_end, elf_header->e_shnum)) {
699     fprintf(stderr, "%s: file contains a \".rela.dyn\" section "
700                     "with type SHT_ANDROID_RELA\n", obj_file.c_str());
701     fprintf(stderr, "Files containing Android packed relocations "
702                     "may not be symbolized.\n");
703     return false;
704   }
705 
706   if (options.symbol_data != ONLY_CFI) {
707 #ifndef NO_STABS_SUPPORT
708     // Look for STABS debugging information, and load it if present.
709     const Shdr* stab_section =
710       FindElfSectionByName<ElfClass>(".stab", SHT_PROGBITS,
711                                      sections, names, names_end,
712                                      elf_header->e_shnum);
713     if (stab_section) {
714       const Shdr* stabstr_section = stab_section->sh_link + sections;
715       if (stabstr_section) {
716         found_debug_info_section = true;
717         found_usable_info = true;
718         info->LoadedSection(".stab");
719         if (!LoadStabs<ElfClass>(elf_header, stab_section, stabstr_section,
720                                  big_endian, module)) {
721           fprintf(stderr, "%s: \".stab\" section found, but failed to load"
722                   " STABS debugging information\n", obj_file.c_str());
723         }
724       }
725     }
726 #endif  // NO_STABS_SUPPORT
727 
728     // Look for DWARF debugging information, and load it if present.
729     const Shdr* dwarf_section =
730       FindElfSectionByName<ElfClass>(".debug_info", debug_section_type,
731                                      sections, names, names_end,
732                                      elf_header->e_shnum);
733     if (dwarf_section) {
734       found_debug_info_section = true;
735       found_usable_info = true;
736       info->LoadedSection(".debug_info");
737       if (!LoadDwarf<ElfClass>(obj_file, elf_header, big_endian,
738                                options.handle_inter_cu_refs, module)) {
739         fprintf(stderr, "%s: \".debug_info\" section found, but failed to load "
740                 "DWARF debugging information\n", obj_file.c_str());
741       }
742     }
743 
744     // See if there are export symbols available.
745     const Shdr* symtab_section =
746         FindElfSectionByName<ElfClass>(".symtab", SHT_SYMTAB,
747                                        sections, names, names_end,
748                                        elf_header->e_shnum);
749     const Shdr* strtab_section =
750         FindElfSectionByName<ElfClass>(".strtab", SHT_STRTAB,
751                                        sections, names, names_end,
752                                        elf_header->e_shnum);
753     if (symtab_section && strtab_section) {
754       info->LoadedSection(".symtab");
755 
756       const uint8_t* symtab =
757           GetOffset<ElfClass, uint8_t>(elf_header,
758                                        symtab_section->sh_offset);
759       const uint8_t* strtab =
760           GetOffset<ElfClass, uint8_t>(elf_header,
761                                        strtab_section->sh_offset);
762       bool result =
763           ELFSymbolsToModule(symtab,
764                              symtab_section->sh_size,
765                              strtab,
766                              strtab_section->sh_size,
767                              big_endian,
768                              ElfClass::kAddrSize,
769                              module);
770       found_usable_info = found_usable_info || result;
771     } else {
772       // Look in dynsym only if full symbol table was not available.
773       const Shdr* dynsym_section =
774           FindElfSectionByName<ElfClass>(".dynsym", SHT_DYNSYM,
775                                          sections, names, names_end,
776                                          elf_header->e_shnum);
777       const Shdr* dynstr_section =
778           FindElfSectionByName<ElfClass>(".dynstr", SHT_STRTAB,
779                                          sections, names, names_end,
780                                          elf_header->e_shnum);
781       if (dynsym_section && dynstr_section) {
782         info->LoadedSection(".dynsym");
783 
784         const uint8_t* dynsyms =
785             GetOffset<ElfClass, uint8_t>(elf_header,
786                                          dynsym_section->sh_offset);
787         const uint8_t* dynstrs =
788             GetOffset<ElfClass, uint8_t>(elf_header,
789                                          dynstr_section->sh_offset);
790         bool result =
791             ELFSymbolsToModule(dynsyms,
792                                dynsym_section->sh_size,
793                                dynstrs,
794                                dynstr_section->sh_size,
795                                big_endian,
796                                ElfClass::kAddrSize,
797                                module);
798         found_usable_info = found_usable_info || result;
799       }
800     }
801   }
802 
803   if (options.symbol_data != NO_CFI) {
804     // Dwarf Call Frame Information (CFI) is actually independent from
805     // the other DWARF debugging information, and can be used alone.
806     const Shdr* dwarf_cfi_section =
807         FindElfSectionByName<ElfClass>(".debug_frame", debug_section_type,
808                                        sections, names, names_end,
809                                        elf_header->e_shnum);
810     if (dwarf_cfi_section) {
811       // Ignore the return value of this function; even without call frame
812       // information, the other debugging information could be perfectly
813       // useful.
814       info->LoadedSection(".debug_frame");
815       bool result =
816           LoadDwarfCFI<ElfClass>(obj_file, elf_header, ".debug_frame",
817                                  dwarf_cfi_section, false, 0, 0, big_endian,
818                                  module);
819       found_usable_info = found_usable_info || result;
820     }
821 
822     // Linux C++ exception handling information can also provide
823     // unwinding data.
824     const Shdr* eh_frame_section =
825         FindElfSectionByName<ElfClass>(".eh_frame", SHT_PROGBITS,
826                                        sections, names, names_end,
827                                        elf_header->e_shnum);
828     if (eh_frame_section) {
829       // Pointers in .eh_frame data may be relative to the base addresses of
830       // certain sections. Provide those sections if present.
831       const Shdr* got_section =
832           FindElfSectionByName<ElfClass>(".got", SHT_PROGBITS,
833                                          sections, names, names_end,
834                                          elf_header->e_shnum);
835       const Shdr* text_section =
836           FindElfSectionByName<ElfClass>(".text", SHT_PROGBITS,
837                                          sections, names, names_end,
838                                          elf_header->e_shnum);
839       info->LoadedSection(".eh_frame");
840       // As above, ignore the return value of this function.
841       bool result =
842           LoadDwarfCFI<ElfClass>(obj_file, elf_header, ".eh_frame",
843                                  eh_frame_section, true,
844                                  got_section, text_section, big_endian, module);
845       found_usable_info = found_usable_info || result;
846     }
847   }
848 
849   // ARM has special unwind tables that can be used.
850   const Shdr* arm_exidx_section =
851       FindElfSectionByName<ElfClass>(".ARM.exidx", SHT_ARM_EXIDX,
852                                      sections, names, names_end,
853                                      elf_header->e_shnum);
854   const Shdr* arm_extab_section =
855       FindElfSectionByName<ElfClass>(".ARM.extab", SHT_PROGBITS,
856                                      sections, names, names_end,
857                                      elf_header->e_shnum);
858   // Load information from these sections even if there is
859   // .debug_info, because some functions (e.g., hand-written or
860   // script-generated assembly) could have exidx entries but no DWARF.
861   // (For functions with both, the DWARF info that has already been
862   // parsed will take precedence.)
863   if (arm_exidx_section && arm_extab_section && options.symbol_data != NO_CFI) {
864     info->LoadedSection(".ARM.exidx");
865     info->LoadedSection(".ARM.extab");
866     bool result = LoadARMexidx<ElfClass>(elf_header,
867                                          arm_exidx_section, arm_extab_section,
868                                          loading_addr, module);
869     found_usable_info = found_usable_info || result;
870   }
871 
872   if (!found_debug_info_section) {
873     fprintf(stderr, "%s: file contains no debugging information"
874             " (no \".stab\" or \".debug_info\" sections)\n",
875             obj_file.c_str());
876 
877     // Failed, but maybe there's a .gnu_debuglink section?
878     if (read_gnu_debug_link) {
879       const Shdr* gnu_debuglink_section
880           = FindElfSectionByName<ElfClass>(".gnu_debuglink", SHT_PROGBITS,
881                                            sections, names,
882                                            names_end, elf_header->e_shnum);
883       if (gnu_debuglink_section) {
884         if (!info->debug_dirs().empty()) {
885           const uint8_t *debuglink_contents =
886               GetOffset<ElfClass, uint8_t>(elf_header,
887                                            gnu_debuglink_section->sh_offset);
888           string debuglink_file =
889               ReadDebugLink(debuglink_contents,
890                             gnu_debuglink_section->sh_size,
891                             big_endian,
892                             obj_file,
893                             info->debug_dirs());
894           info->set_debuglink_file(debuglink_file);
895         } else {
896           fprintf(stderr, ".gnu_debuglink section found in '%s', "
897                   "but no debug path specified.\n", obj_file.c_str());
898         }
899       } else {
900         fprintf(stderr, "%s does not contain a .gnu_debuglink section.\n",
901                 obj_file.c_str());
902       }
903     } else {
904       // Return true if some usable information was found, since the caller
905       // doesn't want to use .gnu_debuglink.
906       return found_usable_info;
907     }
908 
909     // No debug info was found, let the user try again with .gnu_debuglink
910     // if present.
911     return false;
912   }
913 
914   return true;
915 }
916 
917 // Return the breakpad symbol file identifier for the architecture of
918 // ELF_HEADER.
919 template<typename ElfClass>
ElfArchitecture(const typename ElfClass::Ehdr * elf_header)920 const char* ElfArchitecture(const typename ElfClass::Ehdr* elf_header) {
921   typedef typename ElfClass::Half Half;
922   Half arch = elf_header->e_machine;
923   switch (arch) {
924     case EM_386:        return "x86";
925     case EM_ARM:        return "arm";
926     case EM_AARCH64:    return "arm64";
927     case EM_MIPS:       return "mips";
928     case EM_PPC64:      return "ppc64";
929     case EM_PPC:        return "ppc";
930     case EM_S390:       return "s390";
931     case EM_SPARC:      return "sparc";
932     case EM_SPARCV9:    return "sparcv9";
933     case EM_X86_64:     return "x86_64";
934     default: return NULL;
935   }
936 }
937 
938 // Return the non-directory portion of FILENAME: the portion after the
939 // last slash, or the whole filename if there are no slashes.
BaseFileName(const string & filename)940 string BaseFileName(const string &filename) {
941   // Lots of copies!  basename's behavior is less than ideal.
942   char* c_filename = strdup(filename.c_str());
943   string base = basename(c_filename);
944   free(c_filename);
945   return base;
946 }
947 
948 template<typename ElfClass>
SanitizeDebugFile(const typename ElfClass::Ehdr * debug_elf_header,const string & debuglink_file,const string & obj_filename,const char * obj_file_architecture,const bool obj_file_is_big_endian)949 bool SanitizeDebugFile(const typename ElfClass::Ehdr* debug_elf_header,
950                        const string& debuglink_file,
951                        const string& obj_filename,
952                        const char* obj_file_architecture,
953                        const bool obj_file_is_big_endian) {
954   const char* debug_architecture =
955       ElfArchitecture<ElfClass>(debug_elf_header);
956   if (!debug_architecture) {
957     fprintf(stderr, "%s: unrecognized ELF machine architecture: %d\n",
958             debuglink_file.c_str(), debug_elf_header->e_machine);
959     return false;
960   }
961   if (strcmp(obj_file_architecture, debug_architecture)) {
962     fprintf(stderr, "%s with ELF machine architecture %s does not match "
963             "%s with ELF architecture %s\n",
964             debuglink_file.c_str(), debug_architecture,
965             obj_filename.c_str(), obj_file_architecture);
966     return false;
967   }
968   bool debug_big_endian;
969   if (!ElfEndianness<ElfClass>(debug_elf_header, &debug_big_endian))
970     return false;
971   if (debug_big_endian != obj_file_is_big_endian) {
972     fprintf(stderr, "%s and %s does not match in endianness\n",
973             obj_filename.c_str(), debuglink_file.c_str());
974     return false;
975   }
976   return true;
977 }
978 
979 template<typename ElfClass>
InitModuleForElfClass(const typename ElfClass::Ehdr * elf_header,const string & obj_filename,scoped_ptr<Module> & module)980 bool InitModuleForElfClass(const typename ElfClass::Ehdr* elf_header,
981                            const string& obj_filename,
982                            scoped_ptr<Module>& module) {
983   PageAllocator allocator;
984   wasteful_vector<uint8_t> identifier(&allocator, kDefaultBuildIdSize);
985   if (!FileID::ElfFileIdentifierFromMappedFile(elf_header, identifier)) {
986     fprintf(stderr, "%s: unable to generate file identifier\n",
987             obj_filename.c_str());
988     return false;
989   }
990 
991   const char *architecture = ElfArchitecture<ElfClass>(elf_header);
992   if (!architecture) {
993     fprintf(stderr, "%s: unrecognized ELF machine architecture: %d\n",
994             obj_filename.c_str(), elf_header->e_machine);
995     return false;
996   }
997 
998   string name = BaseFileName(obj_filename);
999   string os = "Linux";
1000   // Add an extra "0" at the end.  PDB files on Windows have an 'age'
1001   // number appended to the end of the file identifier; this isn't
1002   // really used or necessary on other platforms, but be consistent.
1003   string id = FileID::ConvertIdentifierToUUIDString(identifier) + "0";
1004   // This is just the raw Build ID in hex.
1005   string code_id = FileID::ConvertIdentifierToString(identifier);
1006 
1007   module.reset(new Module(name, os, architecture, id, code_id));
1008 
1009   return true;
1010 }
1011 
1012 template<typename ElfClass>
ReadSymbolDataElfClass(const typename ElfClass::Ehdr * elf_header,const string & obj_filename,const std::vector<string> & debug_dirs,const DumpOptions & options,Module ** out_module)1013 bool ReadSymbolDataElfClass(const typename ElfClass::Ehdr* elf_header,
1014                             const string& obj_filename,
1015                             const std::vector<string>& debug_dirs,
1016                             const DumpOptions& options,
1017                             Module** out_module) {
1018   typedef typename ElfClass::Ehdr Ehdr;
1019 
1020   *out_module = NULL;
1021 
1022   scoped_ptr<Module> module;
1023   if (!InitModuleForElfClass<ElfClass>(elf_header, obj_filename, module)) {
1024     return false;
1025   }
1026 
1027   // Figure out what endianness this file is.
1028   bool big_endian;
1029   if (!ElfEndianness<ElfClass>(elf_header, &big_endian))
1030     return false;
1031 
1032   LoadSymbolsInfo<ElfClass> info(debug_dirs);
1033   if (!LoadSymbols<ElfClass>(obj_filename, big_endian, elf_header,
1034                              !debug_dirs.empty(), &info,
1035                              options, module.get())) {
1036     const string debuglink_file = info.debuglink_file();
1037     if (debuglink_file.empty())
1038       return false;
1039 
1040     // Load debuglink ELF file.
1041     fprintf(stderr, "Found debugging info in %s\n", debuglink_file.c_str());
1042     MmapWrapper debug_map_wrapper;
1043     Ehdr* debug_elf_header = NULL;
1044     if (!LoadELF(debuglink_file, &debug_map_wrapper,
1045                  reinterpret_cast<void**>(&debug_elf_header)) ||
1046         !SanitizeDebugFile<ElfClass>(debug_elf_header, debuglink_file,
1047                                      obj_filename,
1048                                      module->architecture().c_str(),
1049                                      big_endian)) {
1050       return false;
1051     }
1052 
1053     if (!LoadSymbols<ElfClass>(debuglink_file, big_endian,
1054                                debug_elf_header, false, &info,
1055                                options, module.get())) {
1056       return false;
1057     }
1058   }
1059 
1060   *out_module = module.release();
1061   return true;
1062 }
1063 
1064 }  // namespace
1065 
1066 namespace google_breakpad {
1067 
1068 // Not explicitly exported, but not static so it can be used in unit tests.
ReadSymbolDataInternal(const uint8_t * obj_file,const string & obj_filename,const std::vector<string> & debug_dirs,const DumpOptions & options,Module ** module)1069 bool ReadSymbolDataInternal(const uint8_t* obj_file,
1070                             const string& obj_filename,
1071                             const std::vector<string>& debug_dirs,
1072                             const DumpOptions& options,
1073                             Module** module) {
1074   if (!IsValidElf(obj_file)) {
1075     fprintf(stderr, "Not a valid ELF file: %s\n", obj_filename.c_str());
1076     return false;
1077   }
1078 
1079   int elfclass = ElfClass(obj_file);
1080   if (elfclass == ELFCLASS32) {
1081     return ReadSymbolDataElfClass<ElfClass32>(
1082         reinterpret_cast<const Elf32_Ehdr*>(obj_file), obj_filename, debug_dirs,
1083         options, module);
1084   }
1085   if (elfclass == ELFCLASS64) {
1086     return ReadSymbolDataElfClass<ElfClass64>(
1087         reinterpret_cast<const Elf64_Ehdr*>(obj_file), obj_filename, debug_dirs,
1088         options, module);
1089   }
1090 
1091   return false;
1092 }
1093 
WriteSymbolFile(const string & obj_file,const std::vector<string> & debug_dirs,const DumpOptions & options,std::ostream & sym_stream)1094 bool WriteSymbolFile(const string &obj_file,
1095                      const std::vector<string>& debug_dirs,
1096                      const DumpOptions& options,
1097                      std::ostream &sym_stream) {
1098   Module* module;
1099   if (!ReadSymbolData(obj_file, debug_dirs, options, &module))
1100     return false;
1101 
1102   bool result = module->Write(sym_stream, options.symbol_data);
1103   delete module;
1104   return result;
1105 }
1106 
1107 // Read the selected object file's debugging information, and write out the
1108 // header only to |stream|. Return true on success; if an error occurs, report
1109 // it and return false.
WriteSymbolFileHeader(const string & obj_file,std::ostream & sym_stream)1110 bool WriteSymbolFileHeader(const string& obj_file,
1111                            std::ostream &sym_stream) {
1112   MmapWrapper map_wrapper;
1113   void* elf_header = NULL;
1114   if (!LoadELF(obj_file, &map_wrapper, &elf_header)) {
1115     fprintf(stderr, "Could not load ELF file: %s\n", obj_file.c_str());
1116     return false;
1117   }
1118 
1119   if (!IsValidElf(elf_header)) {
1120     fprintf(stderr, "Not a valid ELF file: %s\n", obj_file.c_str());
1121     return false;
1122   }
1123 
1124   int elfclass = ElfClass(elf_header);
1125   scoped_ptr<Module> module;
1126   if (elfclass == ELFCLASS32) {
1127     if (!InitModuleForElfClass<ElfClass32>(
1128         reinterpret_cast<const Elf32_Ehdr*>(elf_header), obj_file, module)) {
1129       fprintf(stderr, "Failed to load ELF module: %s\n", obj_file.c_str());
1130       return false;
1131     }
1132   } else if (elfclass == ELFCLASS64) {
1133     if (!InitModuleForElfClass<ElfClass64>(
1134         reinterpret_cast<const Elf64_Ehdr*>(elf_header), obj_file, module)) {
1135       fprintf(stderr, "Failed to load ELF module: %s\n", obj_file.c_str());
1136       return false;
1137     }
1138   } else {
1139     fprintf(stderr, "Unsupported module file: %s\n", obj_file.c_str());
1140     return false;
1141   }
1142 
1143   return module->Write(sym_stream, ALL_SYMBOL_DATA);
1144 }
1145 
ReadSymbolData(const string & obj_file,const std::vector<string> & debug_dirs,const DumpOptions & options,Module ** module)1146 bool ReadSymbolData(const string& obj_file,
1147                     const std::vector<string>& debug_dirs,
1148                     const DumpOptions& options,
1149                     Module** module) {
1150   MmapWrapper map_wrapper;
1151   void* elf_header = NULL;
1152   if (!LoadELF(obj_file, &map_wrapper, &elf_header))
1153     return false;
1154 
1155   return ReadSymbolDataInternal(reinterpret_cast<uint8_t*>(elf_header),
1156                                 obj_file, debug_dirs, options, module);
1157 }
1158 
1159 }  // namespace google_breakpad
1160