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