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