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