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