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/Core/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     virtual void
67     GetPluginCommandHelp(const char *command, lldb_private::Stream *strm);
68 
69     virtual lldb_private::Error
70     ExecutePluginCommand(lldb_private::Args &command,
71                          lldb_private::Stream *strm);
72 
73     virtual lldb_private::Log *
74     EnablePluginLogging(lldb_private::Stream *strm,
75                         lldb_private::Args &command);
76 
77     //------------------------------------------------------------------
78     // ObjectFile Protocol.
79     //------------------------------------------------------------------
80     virtual
81     ~ObjectFileELF();
82 
83     virtual bool
84     ParseHeader();
85 
86     virtual lldb::ByteOrder
87     GetByteOrder() const;
88 
89     virtual bool
90     IsExecutable () const;
91 
92     virtual size_t
93     GetAddressByteSize() const;
94 
95     virtual lldb_private::Symtab *
96     GetSymtab();
97 
98     virtual lldb_private::SectionList *
99     GetSectionList();
100 
101     virtual void
102     Dump(lldb_private::Stream *s);
103 
104     virtual bool
105     GetTargetTriple(lldb_private::ConstString &target_triple);
106 
107     virtual bool
108     GetUUID(lldb_private::UUID* uuid);
109 
110     virtual uint32_t
111     GetDependentModules(lldb_private::FileSpecList& files);
112 
113 private:
114     ObjectFileELF(lldb_private::Module* module,
115                   lldb::DataBufferSP& dataSP,
116                   const lldb_private::FileSpec* file,
117                   lldb::addr_t offset,
118                   lldb::addr_t length);
119 
120     typedef std::vector<elf::ELFProgramHeader>  ProgramHeaderColl;
121     typedef ProgramHeaderColl::iterator         ProgramHeaderCollIter;
122     typedef ProgramHeaderColl::const_iterator   ProgramHeaderCollConstIter;
123 
124     typedef std::vector<elf::ELFSectionHeader>  SectionHeaderColl;
125     typedef SectionHeaderColl::iterator         SectionHeaderCollIter;
126     typedef SectionHeaderColl::const_iterator   SectionHeaderCollConstIter;
127 
128     /// Version of this reader common to all plugins based on this class.
129     static const uint32_t m_plugin_version = 1;
130 
131     /// ELF file header.
132     elf::ELFHeader m_header;
133 
134     /// Collection of program headers.
135     ProgramHeaderColl m_program_headers;
136 
137     /// Collection of section headers.
138     SectionHeaderColl m_section_headers;
139 
140     /// List of sections present in this ELF object file.
141     mutable std::auto_ptr<lldb_private::SectionList> m_sections_ap;
142 
143     /// Table of all non-dynamic symbols present in this object file.
144     mutable std::auto_ptr<lldb_private::Symtab> m_symtab_ap;
145 
146     /// List of file specifications corresponding to the modules (shared
147     /// libraries) on which this object file depends.
148     mutable std::auto_ptr<lldb_private::FileSpecList> m_filespec_ap;
149 
150     /// Data extractor holding the string table used to resolve section names.
151     lldb_private::DataExtractor m_shstr_data;
152 
153     /// Returns a 1 based index of the given section header.
154     unsigned
155     SectionIndex(const SectionHeaderCollIter &I);
156 
157     /// Returns a 1 based index of the given section header.
158     unsigned
159     SectionIndex(const SectionHeaderCollConstIter &I) const;
160 
161     /// Parses all section headers present in this object file and populates
162     /// m_program_headers.  This method will compute the header list only once.
163     /// Returns the number of headers parsed.
164     size_t
165     ParseProgramHeaders();
166 
167     /// Parses all section headers present in this object file and populates
168     /// m_section_headers.  This method will compute the header list only once.
169     /// Returns the number of headers parsed.
170     size_t
171     ParseSectionHeaders();
172 
173     /// Scans the dynamic section and locates all dependent modules (shared
174     /// libaries) populating m_filespec_ap.  This method will compute the
175     /// dependent module list only once.  Returns the number of dependent
176     /// modules parsed.
177     size_t
178     ParseDependentModules();
179 
180     /// Populates m_symtab_ap will all non-dynamic linker symbols.  This method
181     /// will parse the symbols only once.  Returns the number of symbols parsed.
182     void
183     ParseSymbolTable(lldb_private::Symtab *symbol_table,
184                      const elf::ELFSectionHeader &symtab_section,
185                      lldb::user_id_t symtab_id);
186 
187     /// Loads the section name string table into m_shstr_data.  Returns the
188     /// number of bytes constituting the table.
189     size_t
190     GetSectionHeaderStringTable();
191 
192     /// Utility method for looking up a section given its name.  Returns the
193     /// index of the corresponding section or zero if no section with the given
194     /// name can be found (note that section indices are always 1 based, and so
195     /// section index 0 is never valid).
196     lldb::user_id_t
197     GetSectionIndexByName(const char *name);
198 
199     /// @name  ELF header dump routines
200     //@{
201     static void
202     DumpELFHeader(lldb_private::Stream *s, const elf::ELFHeader& header);
203 
204     static void
205     DumpELFHeader_e_ident_EI_DATA(lldb_private::Stream *s,
206                                   unsigned char ei_data);
207 
208     static void
209     DumpELFHeader_e_type(lldb_private::Stream *s, elf::elf_half e_type);
210     //@}
211 
212     /// @name ELF program header dump routines
213     //@{
214     void
215     DumpELFProgramHeaders(lldb_private::Stream *s);
216 
217     static void
218     DumpELFProgramHeader(lldb_private::Stream *s,
219                          const elf::ELFProgramHeader &ph);
220 
221     static void
222     DumpELFProgramHeader_p_type(lldb_private::Stream *s, elf::elf_word p_type);
223 
224     static void
225     DumpELFProgramHeader_p_flags(lldb_private::Stream *s,
226                                  elf::elf_word p_flags);
227     //@}
228 
229     /// @name ELF section header dump routines
230     //@{
231     void
232     DumpELFSectionHeaders(lldb_private::Stream *s);
233 
234     static void
235     DumpELFSectionHeader(lldb_private::Stream *s,
236                          const elf::ELFSectionHeader& sh);
237 
238     static void
239     DumpELFSectionHeader_sh_type(lldb_private::Stream *s,
240                                  elf::elf_word sh_type);
241 
242     static void
243     DumpELFSectionHeader_sh_flags(lldb_private::Stream *s,
244                                   elf::elf_word sh_flags);
245     //@}
246 
247     /// ELF dependent module dump routine.
248     void
249     DumpDependentModules(lldb_private::Stream *s);
250 
251 };
252 
253 #endif // #ifndef liblldb_ObjectFileELF_h_
254