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