OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2005 Google Inc. All Rights Reserved. | |
ivanpe
2016/04/27 01:11:45
Where does this file come from? Has it been revie
yunlian
2016/04/27 16:35:29
It comes from internal google tree and it has been
| |
2 // Author: chatham@google.com (Andrew Chatham) | |
3 // Author: satorux@google.com (Satoru Takabayashi) | |
4 // | |
5 // ElfReader handles reading in ELF. It can extract symbols from the | |
6 // current process, which may be used to symbolize stack traces | |
7 // without having to make a potentially dangerous call to fork(). | |
8 // | |
9 // ElfReader dynamically allocates memory, so it is not appropriate to | |
10 // use once the address space might be corrupted, such as during | |
11 // process death. | |
12 // | |
13 // ElfReader supports both 32-bit and 64-bit ELF binaries. | |
14 | |
15 #ifndef COMMON_DWARF_ELF_READER_H__ | |
16 #define COMMON_DWARF_ELF_READER_H__ | |
17 | |
18 #include <string> | |
19 #include <vector> | |
20 | |
21 #include "common/dwarf/types.h" | |
22 | |
23 using std::string; | |
24 using std::vector; | |
25 using std::pair; | |
26 | |
27 namespace dwarf2reader { | |
28 | |
29 class SymbolMap; | |
30 class Elf32; | |
31 class Elf64; | |
32 template<typename ElfArch> | |
33 class ElfReaderImpl; | |
34 | |
35 class ElfReader { | |
36 public: | |
37 explicit ElfReader(const string &path); | |
38 ~ElfReader(); | |
39 | |
40 // Parse the ELF prologue of this file and return whether it was | |
41 // successfully parsed and matches the word size and byte order of | |
42 // the current process. | |
43 bool IsNativeElfFile() const; | |
44 | |
45 // Similar to IsNativeElfFile but checks if it's a 32-bit ELF file. | |
46 bool IsElf32File() const; | |
47 | |
48 // Similar to IsNativeElfFile but checks if it's a 64-bit ELF file. | |
49 bool IsElf64File() const; | |
50 | |
51 // Checks if it's an ELF file of type ET_DYN (shared object file). | |
52 bool IsDynamicSharedObject(); | |
53 | |
54 // Add symbols in the given ELF file into the provided SymbolMap, | |
55 // assuming that the file has been loaded into the specified | |
56 // offset. | |
57 // | |
58 // The remaining arguments are typically taken from a | |
59 // ProcMapsIterator (base/sysinfo.h) and describe which portions of | |
60 // the ELF file are mapped into which parts of memory: | |
61 // | |
62 // mem_offset - position at which the segment is mapped into memory | |
63 // file_offset - offset in the file where the mapping begins | |
64 // length - length of the mapped segment | |
65 void AddSymbols(SymbolMap *symbols, | |
66 uint64 mem_offset, uint64 file_offset, | |
67 uint64 length); | |
68 | |
69 class SymbolSink { | |
70 public: | |
71 virtual ~SymbolSink() {} | |
72 virtual void AddSymbol(const char *name, uint64 address, uint64 size) = 0; | |
73 }; | |
74 | |
75 // Like AddSymbols above, but with no address correction. | |
76 // Processes any SHT_SYMTAB section, followed by any SHT_DYNSYM section. | |
77 void VisitSymbols(SymbolSink *sink); | |
78 | |
79 // Like VisitSymbols above, but for a specific symbol binding/type. | |
80 // A negative value for the binding and type parameters means any | |
81 // binding or type. | |
82 void VisitSymbols(SymbolSink *sink, int symbol_binding, int symbol_type); | |
83 | |
84 // Like VisitSymbols above but can optionally export raw symbol values instead | |
85 // of adjusted ones. | |
86 void VisitSymbols(SymbolSink *sink, int symbol_binding, int symbol_type, | |
87 bool get_raw_symbol_values); | |
88 | |
89 // p_vaddr of the first PT_LOAD segment (if any), or 0 if no PT_LOAD | |
90 // segments are present. This is the address an ELF image was linked | |
91 // (by static linker) to be loaded at. Usually (but not always) 0 for | |
92 // shared libraries and position-independent executables. | |
93 uint64 VaddrOfFirstLoadSegment(); | |
94 | |
95 // Return the name of section "shndx". Returns NULL if the section | |
96 // is not found. | |
97 const char *GetSectionName(int shndx); | |
98 | |
99 // Return the number of sections in the given ELF file. | |
100 uint64 GetNumSections(); | |
101 | |
102 // Get section "shndx" from the given ELF file. On success, return | |
103 // the pointer to the section and store the size in "size". | |
104 // On error, return NULL. The returned section data is only valid | |
105 // until the ElfReader gets destroyed. | |
106 const char *GetSectionByIndex(int shndx, size_t *size); | |
107 | |
108 // Get section with "section_name" (ex. ".text", ".symtab") in the | |
109 // given ELF file. On success, return the pointer to the section | |
110 // and store the size in "size". On error, return NULL. The | |
111 // returned section data is only valid until the ElfReader gets | |
112 // destroyed. | |
113 const char *GetSectionByName(const string §ion_name, size_t *size); | |
114 | |
115 // This is like GetSectionByName() but it returns a lot of extra information | |
116 // about the section. The SectionInfo structure is almost identical to | |
117 // the typedef struct Elf64_Shdr defined in <elf.h>, but is redefined | |
118 // here so that the many short macro names in <elf.h> don't have to be | |
119 // added to our already cluttered namespace. | |
120 struct SectionInfo { | |
121 uint32 type; // Section type (SHT_xxx constant from elf.h). | |
122 uint64 flags; // Section flags (SHF_xxx constants from elf.h). | |
123 uint64 addr; // Section virtual address at execution. | |
124 uint64 offset; // Section file offset. | |
125 uint64 size; // Section size in bytes. | |
126 uint32 link; // Link to another section. | |
127 uint32 info; // Additional section information. | |
128 uint64 addralign; // Section alignment. | |
129 uint64 entsize; // Entry size if section holds a table. | |
130 }; | |
131 const char *GetSectionInfoByName(const string §ion_name, | |
132 SectionInfo *info); | |
133 | |
134 // Check if "path" is an ELF binary that has not been stripped of symbol | |
135 // tables. This function supports both 32-bit and 64-bit ELF binaries. | |
136 static bool IsNonStrippedELFBinary(const string &path); | |
137 | |
138 // Check if "path" is an ELF binary that has not been stripped of debug | |
139 // info. Unlike IsNonStrippedELFBinary, this function will return | |
140 // false for binaries passed through "strip -S". | |
141 static bool IsNonDebugStrippedELFBinary(const string &path); | |
142 | |
143 // Match a requested section name with the section name as it | |
144 // appears in the elf-file, adjusting for compressed debug section | |
145 // names. For example, returns true if name == ".debug_abbrev" and | |
146 // sh_name == ".zdebug_abbrev" | |
147 static bool SectionNamesMatch(const string &name, const string &sh_name); | |
148 | |
149 private: | |
150 // Lazily initialize impl32_ and return it. | |
151 ElfReaderImpl<Elf32> *GetImpl32(); | |
152 // Ditto for impl64_. | |
153 ElfReaderImpl<Elf64> *GetImpl64(); | |
154 | |
155 // Path of the file we're reading. | |
156 const string path_; | |
157 // Read-only file descriptor for the file. May be -1 if there was an | |
158 // error during open. | |
159 int fd_; | |
160 ElfReaderImpl<Elf32> *impl32_; | |
161 ElfReaderImpl<Elf64> *impl64_; | |
162 }; | |
163 | |
164 } // namespace dwarf2reader | |
165 | |
166 #endif // COMMON_DWARF_ELF_READER_H__ | |
OLD | NEW |