1 //===-- ObjectFileELF.h --------------------------------------- -*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #ifndef liblldb_ObjectFileELF_h_
11 #define liblldb_ObjectFileELF_h_
12 
13 #include <stdint.h>
14 #include <vector>
15 
16 #include "lldb/lldb-private.h"
17 #include "lldb/Host/FileSpec.h"
18 #include "lldb/Symbol/ObjectFile.h"
19 #include "lldb/Core/UUID.h"
20 
21 #include "ELFHeader.h"
22 
23 //------------------------------------------------------------------------------
24 /// @class ObjectFileELF
25 /// @brief Generic ELF object file reader.
26 ///
27 /// This class provides a generic ELF (32/64 bit) reader plugin implementing the
28 /// ObjectFile protocol.
29 class ObjectFileELF :
30     public lldb_private::ObjectFile
31 {
32 public:
33     //------------------------------------------------------------------
34     // Static Functions
35     //------------------------------------------------------------------
36     static void
37     Initialize();
38 
39     static void
40     Terminate();
41 
42     static lldb_private::ConstString
43     GetPluginNameStatic();
44 
45     static const char *
46     GetPluginDescriptionStatic();
47 
48     static lldb_private::ObjectFile *
49     CreateInstance(const lldb::ModuleSP &module_sp,
50                    lldb::DataBufferSP& data_sp,
51                    lldb::offset_t data_offset,
52                    const lldb_private::FileSpec* file,
53                    lldb::offset_t file_offset,
54                    lldb::offset_t length);
55 
56     static lldb_private::ObjectFile *
57     CreateMemoryInstance (const lldb::ModuleSP &module_sp,
58                           lldb::DataBufferSP& data_sp,
59                           const lldb::ProcessSP &process_sp,
60                           lldb::addr_t header_addr);
61 
62     static size_t
63     GetModuleSpecifications (const lldb_private::FileSpec& file,
64                              lldb::DataBufferSP& data_sp,
65                              lldb::offset_t data_offset,
66                              lldb::offset_t file_offset,
67                              lldb::offset_t length,
68                              lldb_private::ModuleSpecList &specs);
69 
70     static bool
71     MagicBytesMatch (lldb::DataBufferSP& data_sp,
72                      lldb::addr_t offset,
73                      lldb::addr_t length);
74 
75     //------------------------------------------------------------------
76     // PluginInterface protocol
77     //------------------------------------------------------------------
78     virtual lldb_private::ConstString
79     GetPluginName();
80 
81     virtual uint32_t
82     GetPluginVersion();
83 
84     //------------------------------------------------------------------
85     // ObjectFile Protocol.
86     //------------------------------------------------------------------
87     virtual
88     ~ObjectFileELF();
89 
90     virtual bool
91     ParseHeader();
92 
93     virtual lldb::ByteOrder
94     GetByteOrder() const;
95 
96     virtual bool
97     IsExecutable () const;
98 
99     virtual uint32_t
100     GetAddressByteSize() const;
101 
102     virtual lldb_private::Symtab *
103     GetSymtab(uint32_t flags = 0);
104 
105     virtual lldb_private::SectionList *
106     GetSectionList();
107 
108     virtual void
109     Dump(lldb_private::Stream *s);
110 
111     virtual bool
112     GetArchitecture (lldb_private::ArchSpec &arch);
113 
114     virtual bool
115     GetUUID(lldb_private::UUID* uuid);
116 
117     virtual lldb_private::FileSpecList
118     GetDebugSymbolFilePaths();
119 
120     virtual uint32_t
121     GetDependentModules(lldb_private::FileSpecList& files);
122 
123     virtual lldb_private::Address
124     GetImageInfoAddress();
125 
126     virtual lldb_private::Address
127     GetEntryPointAddress ();
128 
129     virtual ObjectFile::Type
130     CalculateType();
131 
132     virtual ObjectFile::Strata
133     CalculateStrata();
134 
135 private:
136     ObjectFileELF(const lldb::ModuleSP &module_sp,
137                   lldb::DataBufferSP& data_sp,
138                   lldb::offset_t data_offset,
139                   const lldb_private::FileSpec* file,
140                   lldb::offset_t offset,
141                   lldb::offset_t length);
142 
143     typedef std::vector<elf::ELFProgramHeader>  ProgramHeaderColl;
144     typedef ProgramHeaderColl::iterator         ProgramHeaderCollIter;
145     typedef ProgramHeaderColl::const_iterator   ProgramHeaderCollConstIter;
146 
147     struct ELFSectionHeaderInfo : public elf::ELFSectionHeader
148     {
149         lldb_private::ConstString section_name;
150     };
151     typedef std::vector<ELFSectionHeaderInfo>   SectionHeaderColl;
152     typedef SectionHeaderColl::iterator         SectionHeaderCollIter;
153     typedef SectionHeaderColl::const_iterator   SectionHeaderCollConstIter;
154 
155     typedef std::vector<elf::ELFDynamic>        DynamicSymbolColl;
156     typedef DynamicSymbolColl::iterator         DynamicSymbolCollIter;
157     typedef DynamicSymbolColl::const_iterator   DynamicSymbolCollConstIter;
158 
159     /// Version of this reader common to all plugins based on this class.
160     static const uint32_t m_plugin_version = 1;
161 
162     /// ELF file header.
163     elf::ELFHeader m_header;
164 
165     /// ELF build ID.
166     lldb_private::UUID m_uuid;
167 
168     /// ELF .gnu_debuglink file and crc data if available.
169     std::string m_gnu_debuglink_file;
170     uint32_t m_gnu_debuglink_crc;
171 
172     /// Collection of program headers.
173     ProgramHeaderColl m_program_headers;
174 
175     /// Collection of section headers.
176     SectionHeaderColl m_section_headers;
177 
178     /// Collection of symbols from the dynamic table.
179     DynamicSymbolColl m_dynamic_symbols;
180 
181     /// List of file specifications corresponding to the modules (shared
182     /// libraries) on which this object file depends.
183     mutable std::unique_ptr<lldb_private::FileSpecList> m_filespec_ap;
184 
185     /// Cached value of the entry point for this module.
186     lldb_private::Address  m_entry_point_address;
187 
188     /// Returns a 1 based index of the given section header.
189     size_t
190     SectionIndex(const SectionHeaderCollIter &I);
191 
192     /// Returns a 1 based index of the given section header.
193     size_t
194     SectionIndex(const SectionHeaderCollConstIter &I) const;
195 
196     /// Parses all section headers present in this object file and populates
197     /// m_program_headers.  This method will compute the header list only once.
198     /// Returns the number of headers parsed.
199     size_t
200     ParseProgramHeaders();
201 
202     /// Parses all section headers present in this object file and populates
203     /// m_section_headers.  This method will compute the header list only once.
204     /// Returns the number of headers parsed.
205     size_t
206     ParseSectionHeaders();
207 
208     /// Parses the elf section headers and returns the uuid, debug link name, crc.
209     static size_t
210     GetSectionHeaderInfo(SectionHeaderColl &section_headers,
211                          lldb_private::DataExtractor &data,
212                          const elf::ELFHeader &header,
213                          lldb_private::UUID &uuid,
214                          std::string &gnu_debuglink_file,
215                          uint32_t &gnu_debuglink_crc);
216 
217     /// Scans the dynamic section and locates all dependent modules (shared
218     /// libraries) populating m_filespec_ap.  This method will compute the
219     /// dependent module list only once.  Returns the number of dependent
220     /// modules parsed.
221     size_t
222     ParseDependentModules();
223 
224     /// Parses the dynamic symbol table and populates m_dynamic_symbols.  The
225     /// vector retains the order as found in the object file.  Returns the
226     /// number of dynamic symbols parsed.
227     size_t
228     ParseDynamicSymbols();
229 
230     /// Populates m_symtab_ap will all non-dynamic linker symbols.  This method
231     /// will parse the symbols only once.  Returns the number of symbols parsed.
232     unsigned
233     ParseSymbolTable(lldb_private::Symtab *symbol_table,
234                      lldb::user_id_t start_id,
235                      lldb::user_id_t symtab_id);
236 
237     /// Helper routine for ParseSymbolTable().
238     unsigned
239     ParseSymbols(lldb_private::Symtab *symbol_table,
240                  lldb::user_id_t start_id,
241                  lldb_private::SectionList *section_list,
242                  const ELFSectionHeaderInfo *symtab_shdr,
243                  const lldb_private::DataExtractor &symtab_data,
244                  const lldb_private::DataExtractor &strtab_data);
245 
246     /// Scans the relocation entries and adds a set of artificial symbols to the
247     /// given symbol table for each PLT slot.  Returns the number of symbols
248     /// added.
249     unsigned
250     ParseTrampolineSymbols(lldb_private::Symtab *symbol_table,
251                            lldb::user_id_t start_id,
252                            const ELFSectionHeaderInfo *rela_hdr,
253                            lldb::user_id_t section_id);
254 
255     /// Utility method for looking up a section given its name.  Returns the
256     /// index of the corresponding section or zero if no section with the given
257     /// name can be found (note that section indices are always 1 based, and so
258     /// section index 0 is never valid).
259     lldb::user_id_t
260     GetSectionIndexByName(const char *name);
261 
262     // Returns the ID of the first section that has the given type.
263     lldb::user_id_t
264     GetSectionIndexByType(unsigned type);
265 
266     /// Returns the section header with the given id or NULL.
267     const ELFSectionHeaderInfo *
268     GetSectionHeaderByIndex(lldb::user_id_t id);
269 
270     /// @name  ELF header dump routines
271     //@{
272     static void
273     DumpELFHeader(lldb_private::Stream *s, const elf::ELFHeader& header);
274 
275     static void
276     DumpELFHeader_e_ident_EI_DATA(lldb_private::Stream *s,
277                                   unsigned char ei_data);
278 
279     static void
280     DumpELFHeader_e_type(lldb_private::Stream *s, elf::elf_half e_type);
281     //@}
282 
283     /// @name ELF program header dump routines
284     //@{
285     void
286     DumpELFProgramHeaders(lldb_private::Stream *s);
287 
288     static void
289     DumpELFProgramHeader(lldb_private::Stream *s,
290                          const elf::ELFProgramHeader &ph);
291 
292     static void
293     DumpELFProgramHeader_p_type(lldb_private::Stream *s, elf::elf_word p_type);
294 
295     static void
296     DumpELFProgramHeader_p_flags(lldb_private::Stream *s,
297                                  elf::elf_word p_flags);
298     //@}
299 
300     /// @name ELF section header dump routines
301     //@{
302     void
303     DumpELFSectionHeaders(lldb_private::Stream *s);
304 
305     static void
306     DumpELFSectionHeader(lldb_private::Stream *s,
307                          const ELFSectionHeaderInfo& sh);
308 
309     static void
310     DumpELFSectionHeader_sh_type(lldb_private::Stream *s,
311                                  elf::elf_word sh_type);
312 
313     static void
314     DumpELFSectionHeader_sh_flags(lldb_private::Stream *s,
315                                   elf::elf_xword sh_flags);
316     //@}
317 
318     /// ELF dependent module dump routine.
319     void
320     DumpDependentModules(lldb_private::Stream *s);
321 
322     const elf::ELFDynamic *
323     FindDynamicSymbol(unsigned tag);
324 
325     unsigned
326     PLTRelocationType();
327 };
328 
329 #endif // #ifndef liblldb_ObjectFileELF_h_
330