1 // Copyright 2010 Google LLC
2 //
3 // Redistribution and use in source and binary forms, with or without
4 // modification, are permitted provided that the following conditions are
5 // met:
6 //
7 // * Redistributions of source code must retain the above copyright
8 // notice, this list of conditions and the following disclaimer.
9 // * Redistributions in binary form must reproduce the above
10 // copyright notice, this list of conditions and the following disclaimer
11 // in the documentation and/or other materials provided with the
12 // distribution.
13 // * Neither the name of Google LLC nor the names of its
14 // contributors may be used to endorse or promote products derived from
15 // this software without specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29 // Author: Alfred Peng
30
31 #ifdef HAVE_CONFIG_H
32 #include <config.h> // Must come first
33 #endif
34
35 #include <demangle.h>
36 #include <fcntl.h>
37 #include <gelf.h>
38 #include <link.h>
39 #include <sys/mman.h>
40 #include <stab.h>
41 #include <sys/stat.h>
42 #include <sys/types.h>
43 #include <unistd.h>
44
45 #include <functional>
46 #include <map>
47 #include <vector>
48
49 #include "common/scoped_ptr.h"
50 #include "common/solaris/dump_symbols.h"
51 #include "common/solaris/file_id.h"
52 #include "common/solaris/guid_creator.h"
53
54 // This namespace contains helper functions.
55 namespace {
56
57 using std::make_pair;
58
59 #if defined(_LP64)
60 typedef Elf64_Sym Elf_Sym;
61 #else
62 typedef Elf32_Sym Elf_Sym;
63 #endif
64
65 // Symbol table entry from stabs. Sun CC specific.
66 struct slist {
67 // String table index.
68 unsigned int n_strx;
69 // Stab type.
70 unsigned char n_type;
71 char n_other;
72 short n_desc;
73 unsigned long n_value;
74 };
75
76 // Symbol table entry
77 struct SymbolEntry {
78 // Offset from the start of the file.
79 GElf_Addr offset;
80 // Function size.
81 GElf_Word size;
82 };
83
84 // Infomation of a line.
85 struct LineInfo {
86 // Offset from start of the function.
87 // Load from stab symbol.
88 GElf_Off rva_to_func;
89 // Offset from base of the loading binary.
90 GElf_Off rva_to_base;
91 // Size of the line.
92 // The first line: equals to rva_to_func.
93 // The other lines: the difference of rva_to_func of the line and
94 // rva_to_func of the previous N_SLINE.
95 uint32_t size;
96 // Line number.
97 uint32_t line_num;
98 };
99
100 // Information of a function.
101 struct FuncInfo {
102 // Name of the function.
103 const char* name;
104 // Offset from the base of the loading address.
105 GElf_Off rva_to_base;
106 // Virtual address of the function.
107 // Load from stab symbol.
108 GElf_Addr addr;
109 // Size of the function.
110 // Equal to rva_to_func of the last function line.
111 uint32_t size;
112 // Total size of stack parameters.
113 uint32_t stack_param_size;
114 // Line information array.
115 std::vector<struct LineInfo> line_info;
116 };
117
118 // Information of a source file.
119 struct SourceFileInfo {
120 // Name of the source file.
121 const char* name;
122 // Starting address of the source file.
123 GElf_Addr addr;
124 // Id of the source file.
125 int source_id;
126 // Functions information.
127 std::vector<struct FuncInfo> func_info;
128 };
129
130 struct CompareString {
operator ()__anon494f73470111::CompareString131 bool operator()(const char* s1, const char* s2) const {
132 return strcmp(s1, s2) < 0;
133 }
134 };
135
136 typedef std::map<const char*, struct SymbolEntry*, CompareString> SymbolMap;
137
138 // Information of a symbol table.
139 // This is the root of all types of symbol.
140 struct SymbolInfo {
141 std::vector<struct SourceFileInfo> source_file_info;
142 // Symbols information.
143 SymbolMap symbol_entries;
144 };
145
146 // Stab section name.
147 const char* kStabName = ".stab";
148
149 // Stab str section name.
150 const char* kStabStrName = ".stabstr";
151
152 // Symtab section name.
153 const char* kSymtabName = ".symtab";
154
155 // Strtab section name.
156 const char* kStrtabName = ".strtab";
157
158 // Default buffer lenght for demangle.
159 const int demangleLen = 20000;
160
161 // Offset to the string table.
162 uint64_t stringOffset = 0;
163
164 // Update the offset to the start of the string index of the next
165 // object module for every N_ENDM stabs.
RecalculateOffset(struct slist * cur_list,char * stabstr)166 inline void RecalculateOffset(struct slist* cur_list, char* stabstr) {
167 while ((--cur_list)->n_strx == 0) ;
168 stringOffset += cur_list->n_strx;
169
170 char* temp = stabstr + stringOffset;
171 while (*temp != '\0') {
172 ++stringOffset;
173 ++temp;
174 }
175 // Skip the extra '\0'
176 ++stringOffset;
177 }
178
179 // Demangle using demangle library on Solaris.
Demangle(const char * mangled)180 std::string Demangle(const char* mangled) {
181 int status = 0;
182 std::string str(mangled);
183 char* demangled = (char*)malloc(demangleLen);
184
185 if (!demangled) {
186 fprintf(stderr, "no enough memory.\n");
187 goto out;
188 }
189
190 if ((status = cplus_demangle(mangled, demangled, demangleLen)) ==
191 DEMANGLE_ESPACE) {
192 fprintf(stderr, "incorrect demangle.\n");
193 goto out;
194 }
195
196 str = demangled;
197 free(demangled);
198
199 out:
200 return str;
201 }
202
WriteFormat(int fd,const char * fmt,...)203 bool WriteFormat(int fd, const char* fmt, ...) {
204 va_list list;
205 char buffer[4096];
206 ssize_t expected, written;
207 va_start(list, fmt);
208 vsnprintf(buffer, sizeof(buffer), fmt, list);
209 expected = strlen(buffer);
210 written = write(fd, buffer, strlen(buffer));
211 va_end(list);
212 return expected == written;
213 }
214
IsValidElf(const GElf_Ehdr * elf_header)215 bool IsValidElf(const GElf_Ehdr* elf_header) {
216 return memcmp(elf_header, ELFMAG, SELFMAG) == 0;
217 }
218
FindSectionByName(Elf * elf,const char * name,int shstrndx,GElf_Shdr * shdr)219 static bool FindSectionByName(Elf* elf, const char* name,
220 int shstrndx,
221 GElf_Shdr* shdr) {
222 assert(name != NULL);
223
224 if (strlen(name) == 0)
225 return false;
226
227 Elf_Scn* scn = NULL;
228
229 while ((scn = elf_nextscn(elf, scn)) != NULL) {
230 if (gelf_getshdr(scn, shdr) == (GElf_Shdr*)0) {
231 fprintf(stderr, "failed to read section header: %s\n", elf_errmsg(0));
232 return false;
233 }
234
235 const char* section_name = elf_strptr(elf, shstrndx, shdr->sh_name);
236 if (!section_name) {
237 fprintf(stderr, "Section name error: %s\n", elf_errmsg(-1));
238 continue;
239 }
240
241 if (strcmp(section_name, name) == 0)
242 return true;
243 }
244
245 return false;
246 }
247
248 // The parameter size is used for FPO-optimized code, and
249 // this is all tied up with the debugging data for Windows x86.
250 // Set it to 0 on Solaris.
LoadStackParamSize(struct slist * list,struct slist * list_end,struct FuncInfo * func_info)251 int LoadStackParamSize(struct slist* list,
252 struct slist* list_end,
253 struct FuncInfo* func_info) {
254 struct slist* cur_list = list;
255 int step = 1;
256 while (cur_list < list_end && cur_list->n_type == N_PSYM) {
257 ++cur_list;
258 ++step;
259 }
260
261 func_info->stack_param_size = 0;
262 return step;
263 }
264
LoadLineInfo(struct slist * list,struct slist * list_end,struct FuncInfo * func_info)265 int LoadLineInfo(struct slist* list,
266 struct slist* list_end,
267 struct FuncInfo* func_info) {
268 struct slist* cur_list = list;
269 do {
270 // Skip non line information.
271 while (cur_list < list_end && cur_list->n_type != N_SLINE) {
272 // Only exit when got another function, or source file, or end stab.
273 if (cur_list->n_type == N_FUN || cur_list->n_type == N_SO ||
274 cur_list->n_type == N_ENDM) {
275 return cur_list - list;
276 }
277 ++cur_list;
278 }
279 struct LineInfo line;
280 while (cur_list < list_end && cur_list->n_type == N_SLINE) {
281 line.rva_to_func = cur_list->n_value;
282 // n_desc is a signed short
283 line.line_num = (unsigned short)cur_list->n_desc;
284 func_info->line_info.push_back(line);
285 ++cur_list;
286 }
287 if (cur_list == list_end && cur_list->n_type == N_ENDM)
288 break;
289 } while (list < list_end);
290
291 return cur_list - list;
292 }
293
LoadFuncSymbols(struct slist * list,struct slist * list_end,char * stabstr,GElf_Word base,struct SourceFileInfo * source_file_info)294 int LoadFuncSymbols(struct slist* list,
295 struct slist* list_end,
296 char* stabstr,
297 GElf_Word base,
298 struct SourceFileInfo* source_file_info) {
299 struct slist* cur_list = list;
300 assert(cur_list->n_type == N_SO);
301 ++cur_list;
302
303 source_file_info->func_info.clear();
304 while (cur_list < list_end) {
305 // Go until the function symbol.
306 while (cur_list < list_end && cur_list->n_type != N_FUN) {
307 if (cur_list->n_type == N_SO) {
308 return cur_list - list;
309 }
310 ++cur_list;
311 if (cur_list->n_type == N_ENDM)
312 RecalculateOffset(cur_list, stabstr);
313 continue;
314 }
315 while (cur_list->n_type == N_FUN) {
316 struct FuncInfo func_info;
317 memset(&func_info, 0, sizeof(func_info));
318 func_info.name = stabstr + cur_list->n_strx + stringOffset;
319 // The n_value field is always 0 from stab generated by Sun CC.
320 // TODO(Alfred): Find the correct value.
321 func_info.addr = cur_list->n_value;
322 ++cur_list;
323 if (cur_list->n_type == N_ENDM)
324 RecalculateOffset(cur_list, stabstr);
325 if (cur_list->n_type != N_ESYM && cur_list->n_type != N_ISYM &&
326 cur_list->n_type != N_FUN) {
327 // Stack parameter size.
328 cur_list += LoadStackParamSize(cur_list, list_end, &func_info);
329 // Line info.
330 cur_list += LoadLineInfo(cur_list, list_end, &func_info);
331 }
332 if (cur_list < list_end && cur_list->n_type == N_ENDM)
333 RecalculateOffset(cur_list, stabstr);
334 // Functions in this module should have address bigger than the module
335 // starting address.
336 //
337 // These two values are always 0 with Sun CC.
338 // TODO(Alfred): Get the correct value or remove the condition statement.
339 if (func_info.addr >= source_file_info->addr) {
340 source_file_info->func_info.push_back(func_info);
341 }
342 }
343 }
344 return cur_list - list;
345 }
346
347 // Compute size and rva information based on symbols loaded from stab section.
ComputeSizeAndRVA(struct SymbolInfo * symbols)348 bool ComputeSizeAndRVA(struct SymbolInfo* symbols) {
349 std::vector<struct SourceFileInfo>* sorted_files =
350 &(symbols->source_file_info);
351 SymbolMap* symbol_entries = &(symbols->symbol_entries);
352 for (size_t i = 0; i < sorted_files->size(); ++i) {
353 struct SourceFileInfo& source_file = (*sorted_files)[i];
354 std::vector<struct FuncInfo>* sorted_functions = &(source_file.func_info);
355 int func_size = sorted_functions->size();
356
357 for (size_t j = 0; j < func_size; ++j) {
358 struct FuncInfo& func_info = (*sorted_functions)[j];
359 int line_count = func_info.line_info.size();
360
361 // Discard the ending part of the name.
362 std::string func_name(func_info.name);
363 std::string::size_type last_colon = func_name.find_first_of(':');
364 if (last_colon != std::string::npos)
365 func_name = func_name.substr(0, last_colon);
366
367 // Fine the symbol offset from the loading address and size by name.
368 SymbolMap::const_iterator it = symbol_entries->find(func_name.c_str());
369 if (it->second) {
370 func_info.rva_to_base = it->second->offset;
371 func_info.size = (line_count == 0) ? 0 : it->second->size;
372 } else {
373 func_info.rva_to_base = 0;
374 func_info.size = 0;
375 }
376
377 // Compute function and line size.
378 for (size_t k = 0; k < line_count; ++k) {
379 struct LineInfo& line_info = func_info.line_info[k];
380
381 line_info.rva_to_base = line_info.rva_to_func + func_info.rva_to_base;
382 if (k == line_count - 1) {
383 line_info.size = func_info.size - line_info.rva_to_func;
384 } else {
385 struct LineInfo& next_line = func_info.line_info[k + 1];
386 line_info.size = next_line.rva_to_func - line_info.rva_to_func;
387 }
388 } // for each line.
389 } // for each function.
390 } // for each source file.
391 for (SymbolMap::iterator it = symbol_entries->begin();
392 it != symbol_entries->end(); ++it) {
393 free(it->second);
394 }
395 return true;
396 }
397
LoadAllSymbols(const GElf_Shdr * stab_section,const GElf_Shdr * stabstr_section,GElf_Word base,struct SymbolInfo * symbols)398 bool LoadAllSymbols(const GElf_Shdr* stab_section,
399 const GElf_Shdr* stabstr_section,
400 GElf_Word base,
401 struct SymbolInfo* symbols) {
402 if (stab_section == NULL || stabstr_section == NULL)
403 return false;
404
405 char* stabstr = reinterpret_cast<char*>(stabstr_section->sh_offset + base);
406 struct slist* lists =
407 reinterpret_cast<struct slist*>(stab_section->sh_offset + base);
408 int nstab = stab_section->sh_size / sizeof(struct slist);
409 int source_id = 0;
410
411 // First pass, load all symbols from the object file.
412 for (int i = 0; i < nstab; ) {
413 int step = 1;
414 struct slist* cur_list = lists + i;
415 if (cur_list->n_type == N_SO) {
416 // FUNC <address> <size> <param_stack_size> <function>
417 struct SourceFileInfo source_file_info;
418 source_file_info.name = stabstr + cur_list->n_strx + stringOffset;
419 // The n_value field is always 0 from stab generated by Sun CC.
420 // TODO(Alfred): Find the correct value.
421 source_file_info.addr = cur_list->n_value;
422 if (strchr(source_file_info.name, '.'))
423 source_file_info.source_id = source_id++;
424 else
425 source_file_info.source_id = -1;
426 step = LoadFuncSymbols(cur_list, lists + nstab - 1, stabstr,
427 base, &source_file_info);
428 symbols->source_file_info.push_back(source_file_info);
429 }
430 i += step;
431 }
432 // Second pass, compute the size of functions and lines.
433 return ComputeSizeAndRVA(symbols);
434 }
435
LoadSymbols(Elf * elf,GElf_Ehdr * elf_header,struct SymbolInfo * symbols,void * obj_base)436 bool LoadSymbols(Elf* elf, GElf_Ehdr* elf_header, struct SymbolInfo* symbols,
437 void* obj_base) {
438 GElf_Word base = reinterpret_cast<GElf_Word>(obj_base);
439
440 const GElf_Shdr* sections =
441 reinterpret_cast<GElf_Shdr*>(elf_header->e_shoff + base);
442 GElf_Shdr stab_section;
443 if (!FindSectionByName(elf, kStabName, elf_header->e_shstrndx,
444 &stab_section)) {
445 fprintf(stderr, "Stab section not found.\n");
446 return false;
447 }
448 GElf_Shdr stabstr_section;
449 if (!FindSectionByName(elf, kStabStrName, elf_header->e_shstrndx,
450 &stabstr_section)) {
451 fprintf(stderr, "Stabstr section not found.\n");
452 return false;
453 }
454 GElf_Shdr symtab_section;
455 if (!FindSectionByName(elf, kSymtabName, elf_header->e_shstrndx,
456 &symtab_section)) {
457 fprintf(stderr, "Symtab section not found.\n");
458 return false;
459 }
460 GElf_Shdr strtab_section;
461 if (!FindSectionByName(elf, kStrtabName, elf_header->e_shstrndx,
462 &strtab_section)) {
463 fprintf(stderr, "Strtab section not found.\n");
464 return false;
465 }
466
467 Elf_Sym* symbol = (Elf_Sym*)((char*)base + symtab_section.sh_offset);
468 for (int i = 0; i < symtab_section.sh_size/symtab_section.sh_entsize; ++i) {
469 struct SymbolEntry* symbol_entry =
470 (struct SymbolEntry*)malloc(sizeof(struct SymbolEntry));
471 const char* name = reinterpret_cast<char*>(
472 strtab_section.sh_offset + (GElf_Word)base + symbol->st_name);
473 symbol_entry->offset = symbol->st_value;
474 symbol_entry->size = symbol->st_size;
475 symbols->symbol_entries.insert(make_pair(name, symbol_entry));
476 ++symbol;
477 }
478
479
480 // Load symbols.
481 return LoadAllSymbols(&stab_section, &stabstr_section, base, symbols);
482 }
483
WriteModuleInfo(int fd,GElf_Half arch,const std::string & obj_file)484 bool WriteModuleInfo(int fd, GElf_Half arch, const std::string& obj_file) {
485 const char* arch_name = NULL;
486 if (arch == EM_386)
487 arch_name = "x86";
488 else if (arch == EM_X86_64)
489 arch_name = "x86_64";
490 else if (arch == EM_SPARC32PLUS)
491 arch_name = "SPARC_32+";
492 else {
493 printf("Please add more ARCH support\n");
494 return false;
495 }
496
497 unsigned char identifier[16];
498 google_breakpad::elf::FileID file_id(obj_file.c_str());
499 if (file_id.ElfFileIdentifier(identifier)) {
500 char identifier_str[40];
501 file_id.ConvertIdentifierToString(identifier,
502 identifier_str, sizeof(identifier_str));
503 std::string filename = obj_file;
504 size_t slash_pos = obj_file.find_last_of("/");
505 if (slash_pos != std::string::npos)
506 filename = obj_file.substr(slash_pos + 1);
507 return WriteFormat(fd, "MODULE solaris %s %s %s\n", arch_name,
508 identifier_str, filename.c_str());
509 }
510 return false;
511 }
512
WriteSourceFileInfo(int fd,const struct SymbolInfo & symbols)513 bool WriteSourceFileInfo(int fd, const struct SymbolInfo& symbols) {
514 for (size_t i = 0; i < symbols.source_file_info.size(); ++i) {
515 if (symbols.source_file_info[i].source_id != -1) {
516 const char* name = symbols.source_file_info[i].name;
517 if (!WriteFormat(fd, "FILE %d %s\n",
518 symbols.source_file_info[i].source_id, name))
519 return false;
520 }
521 }
522 return true;
523 }
524
WriteOneFunction(int fd,int source_id,const struct FuncInfo & func_info)525 bool WriteOneFunction(int fd, int source_id,
526 const struct FuncInfo& func_info){
527 // Discard the ending part of the name.
528 std::string func_name(func_info.name);
529 std::string::size_type last_colon = func_name.find_last_of(':');
530 if (last_colon != std::string::npos)
531 func_name = func_name.substr(0, last_colon);
532 func_name = Demangle(func_name.c_str());
533
534 if (func_info.size <= 0)
535 return true;
536
537 // rva_to_base could be unsigned long(32 bit) or unsigned long long(64 bit).
538 if (WriteFormat(fd, "FUNC %llx %x %d %s\n",
539 (long long)func_info.rva_to_base,
540 func_info.size,
541 func_info.stack_param_size,
542 func_name.c_str())) {
543 for (size_t i = 0; i < func_info.line_info.size(); ++i) {
544 const struct LineInfo& line_info = func_info.line_info[i];
545 if (line_info.line_num == 0)
546 return true;
547 if (!WriteFormat(fd, "%llx %x %d %d\n",
548 (long long)line_info.rva_to_base,
549 line_info.size,
550 line_info.line_num,
551 source_id))
552 return false;
553 }
554 return true;
555 }
556 return false;
557 }
558
WriteFunctionInfo(int fd,const struct SymbolInfo & symbols)559 bool WriteFunctionInfo(int fd, const struct SymbolInfo& symbols) {
560 for (size_t i = 0; i < symbols.source_file_info.size(); ++i) {
561 const struct SourceFileInfo& file_info = symbols.source_file_info[i];
562 for (size_t j = 0; j < file_info.func_info.size(); ++j) {
563 const struct FuncInfo& func_info = file_info.func_info[j];
564 if (!WriteOneFunction(fd, file_info.source_id, func_info))
565 return false;
566 }
567 }
568 return true;
569 }
570
DumpStabSymbols(int fd,const struct SymbolInfo & symbols)571 bool DumpStabSymbols(int fd, const struct SymbolInfo& symbols) {
572 return WriteSourceFileInfo(fd, symbols) &&
573 WriteFunctionInfo(fd, symbols);
574 }
575
576 //
577 // FDWrapper
578 //
579 // Wrapper class to make sure opened file is closed.
580 //
581 class FDWrapper {
582 public:
FDWrapper(int fd)583 explicit FDWrapper(int fd) :
584 fd_(fd) {
585 }
~FDWrapper()586 ~FDWrapper() {
587 if (fd_ != -1)
588 close(fd_);
589 }
get()590 int get() {
591 return fd_;
592 }
release()593 int release() {
594 int fd = fd_;
595 fd_ = -1;
596 return fd;
597 }
598 private:
599 int fd_;
600 };
601
602 //
603 // MmapWrapper
604 //
605 // Wrapper class to make sure mapped regions are unmapped.
606 //
607 class MmapWrapper {
608 public:
MmapWrapper(void * mapped_address,size_t mapped_size)609 MmapWrapper(void* mapped_address, size_t mapped_size) :
610 base_(mapped_address), size_(mapped_size) {
611 }
~MmapWrapper()612 ~MmapWrapper() {
613 if (base_ != NULL) {
614 assert(size_ > 0);
615 munmap((char*)base_, size_);
616 }
617 }
release()618 void release() {
619 base_ = NULL;
620 size_ = 0;
621 }
622
623 private:
624 void* base_;
625 size_t size_;
626 };
627
628 } // namespace
629
630 namespace google_breakpad {
631
632 class AutoElfEnder {
633 public:
AutoElfEnder(Elf * elf)634 AutoElfEnder(Elf* elf) : elf_(elf) {}
~AutoElfEnder()635 ~AutoElfEnder() { if (elf_) elf_end(elf_); }
636 private:
637 Elf* elf_;
638 };
639
640
WriteSymbolFile(const std::string & obj_file,int sym_fd)641 bool DumpSymbols::WriteSymbolFile(const std::string& obj_file, int sym_fd) {
642 if (elf_version(EV_CURRENT) == EV_NONE) {
643 fprintf(stderr, "elf_version() failed: %s\n", elf_errmsg(0));
644 return false;
645 }
646
647 int obj_fd = open(obj_file.c_str(), O_RDONLY);
648 if (obj_fd < 0)
649 return false;
650 FDWrapper obj_fd_wrapper(obj_fd);
651 struct stat st;
652 if (fstat(obj_fd, &st) != 0 && st.st_size <= 0)
653 return false;
654 void* obj_base = mmap(NULL, st.st_size,
655 PROT_READ, MAP_PRIVATE, obj_fd, 0);
656 if (obj_base == MAP_FAILED)
657 return false;
658 MmapWrapper map_wrapper(obj_base, st.st_size);
659 GElf_Ehdr elf_header;
660 Elf* elf = elf_begin(obj_fd, ELF_C_READ, NULL);
661 AutoElfEnder elfEnder(elf);
662
663 if (gelf_getehdr(elf, &elf_header) == (GElf_Ehdr*)NULL) {
664 fprintf(stderr, "failed to read elf header: %s\n", elf_errmsg(-1));
665 return false;
666 }
667
668 if (!IsValidElf(&elf_header)) {
669 fprintf(stderr, "header magic doesn't match\n");
670 return false;
671 }
672 struct SymbolInfo symbols;
673 if (!LoadSymbols(elf, &elf_header, &symbols, obj_base))
674 return false;
675 // Write to symbol file.
676 if (WriteModuleInfo(sym_fd, elf_header.e_machine, obj_file) &&
677 DumpStabSymbols(sym_fd, symbols))
678 return true;
679
680 return false;
681 }
682
683 } // namespace google_breakpad
684