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