blob: dcee1ad5d09dd84512407a018632aaac2097e490 [file] [log] [blame]
Stephen Wilsonddd29622010-07-13 23:07:23 +00001//===-- ObjectFileELF.h --------------------------------------- -*- C++ -*-===//
Chris Lattner24943d22010-06-08 16:52:24 +00002//
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
Stephen Wilsonddd29622010-07-13 23:07:23 +000020#include "ELFHeader.h"
Chris Lattner24943d22010-06-08 16:52:24 +000021
Stephen Wilsonddd29622010-07-13 23:07:23 +000022//------------------------------------------------------------------------------
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.
Chris Lattner24943d22010-06-08 16:52:24 +000028class ObjectFileELF :
29 public lldb_private::ObjectFile
30{
31public:
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 *
Stephen Wilsonddd29622010-07-13 23:07:23 +000048 CreateInstance(lldb_private::Module* module,
Chris Lattner24943d22010-06-08 16:52:24 +000049 lldb::DataBufferSP& dataSP,
50 const lldb_private::FileSpec* file,
51 lldb::addr_t offset,
52 lldb::addr_t length);
53
Chris Lattner24943d22010-06-08 16:52:24 +000054 //------------------------------------------------------------------
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
Stephen Wilsonddd29622010-07-13 23:07:23 +000067 GetPluginCommandHelp(const char *command, lldb_private::Stream *strm);
Chris Lattner24943d22010-06-08 16:52:24 +000068
69 virtual lldb_private::Error
Stephen Wilsonddd29622010-07-13 23:07:23 +000070 ExecutePluginCommand(lldb_private::Args &command,
71 lldb_private::Stream *strm);
Chris Lattner24943d22010-06-08 16:52:24 +000072
73 virtual lldb_private::Log *
Stephen Wilsonddd29622010-07-13 23:07:23 +000074 EnablePluginLogging(lldb_private::Stream *strm,
75 lldb_private::Args &command);
Chris Lattner24943d22010-06-08 16:52:24 +000076
Stephen Wilsonddd29622010-07-13 23:07:23 +000077 //------------------------------------------------------------------
78 // ObjectFile Protocol.
79 //------------------------------------------------------------------
80 virtual
81 ~ObjectFileELF();
Chris Lattner24943d22010-06-08 16:52:24 +000082
Stephen Wilsonddd29622010-07-13 23:07:23 +000083 virtual bool
84 ParseHeader();
Chris Lattner24943d22010-06-08 16:52:24 +000085
Stephen Wilsonddd29622010-07-13 23:07:23 +000086 virtual lldb::ByteOrder
87 GetByteOrder() const;
88
Jim Ingham7508e732010-08-09 23:31:02 +000089 virtual bool
90 IsExecutable () const;
91
Stephen Wilsonddd29622010-07-13 23:07:23 +000092 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
113private:
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;
Chris Lattner24943d22010-06-08 16:52:24 +0000121 typedef ProgramHeaderColl::iterator ProgramHeaderCollIter;
122 typedef ProgramHeaderColl::const_iterator ProgramHeaderCollConstIter;
123
Stephen Wilsonddd29622010-07-13 23:07:23 +0000124 typedef std::vector<elf::ELFSectionHeader> SectionHeaderColl;
Chris Lattner24943d22010-06-08 16:52:24 +0000125 typedef SectionHeaderColl::iterator SectionHeaderCollIter;
126 typedef SectionHeaderColl::const_iterator SectionHeaderCollConstIter;
127
Stephen Wilsonddd29622010-07-13 23:07:23 +0000128 /// 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.
Chris Lattner24943d22010-06-08 16:52:24 +0000135 ProgramHeaderColl m_program_headers;
Stephen Wilsonddd29622010-07-13 23:07:23 +0000136
137 /// Collection of section headers.
Chris Lattner24943d22010-06-08 16:52:24 +0000138 SectionHeaderColl m_section_headers;
Stephen Wilsonddd29622010-07-13 23:07:23 +0000139
140 /// List of sections present in this ELF object file.
Chris Lattner24943d22010-06-08 16:52:24 +0000141 mutable std::auto_ptr<lldb_private::SectionList> m_sections_ap;
Stephen Wilsonddd29622010-07-13 23:07:23 +0000142
143 /// Table of all non-dynamic symbols present in this object file.
Chris Lattner24943d22010-06-08 16:52:24 +0000144 mutable std::auto_ptr<lldb_private::Symtab> m_symtab_ap;
Stephen Wilsonddd29622010-07-13 23:07:23 +0000145
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.
Chris Lattner24943d22010-06-08 16:52:24 +0000151 lldb_private::DataExtractor m_shstr_data;
152
Stephen Wilsonddd29622010-07-13 23:07:23 +0000153 /// 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.
Chris Lattner24943d22010-06-08 16:52:24 +0000164 size_t
Stephen Wilsonddd29622010-07-13 23:07:23 +0000165 ParseProgramHeaders();
Chris Lattner24943d22010-06-08 16:52:24 +0000166
Stephen Wilsonddd29622010-07-13 23:07:23 +0000167 /// 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.
Chris Lattner24943d22010-06-08 16:52:24 +0000170 size_t
Stephen Wilsonddd29622010-07-13 23:07:23 +0000171 ParseSectionHeaders();
Chris Lattner24943d22010-06-08 16:52:24 +0000172
Stephen Wilsonddd29622010-07-13 23:07:23 +0000173 /// 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();
Chris Lattner24943d22010-06-08 16:52:24 +0000179
Stephen Wilsonddd29622010-07-13 23:07:23 +0000180 /// 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.
Chris Lattner24943d22010-06-08 16:52:24 +0000182 void
Stephen Wilsonddd29622010-07-13 23:07:23 +0000183 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);
Chris Lattner24943d22010-06-08 16:52:24 +0000203
204 static void
Stephen Wilsonddd29622010-07-13 23:07:23 +0000205 DumpELFHeader_e_ident_EI_DATA(lldb_private::Stream *s,
206 unsigned char ei_data);
Chris Lattner24943d22010-06-08 16:52:24 +0000207
208 static void
Stephen Wilsonddd29622010-07-13 23:07:23 +0000209 DumpELFHeader_e_type(lldb_private::Stream *s, elf::elf_half e_type);
210 //@}
Chris Lattner24943d22010-06-08 16:52:24 +0000211
Stephen Wilsonddd29622010-07-13 23:07:23 +0000212 /// @name ELF program header dump routines
213 //@{
Chris Lattner24943d22010-06-08 16:52:24 +0000214 void
Stephen Wilsonddd29622010-07-13 23:07:23 +0000215 DumpELFProgramHeaders(lldb_private::Stream *s);
Chris Lattner24943d22010-06-08 16:52:24 +0000216
217 static void
Stephen Wilsonddd29622010-07-13 23:07:23 +0000218 DumpELFProgramHeader(lldb_private::Stream *s,
219 const elf::ELFProgramHeader &ph);
Chris Lattner24943d22010-06-08 16:52:24 +0000220
221 static void
Stephen Wilsonddd29622010-07-13 23:07:23 +0000222 DumpELFProgramHeader_p_type(lldb_private::Stream *s, elf::elf_word p_type);
Chris Lattner24943d22010-06-08 16:52:24 +0000223
224 static void
Stephen Wilsonddd29622010-07-13 23:07:23 +0000225 DumpELFProgramHeader_p_flags(lldb_private::Stream *s,
226 elf::elf_word p_flags);
227 //@}
Chris Lattner24943d22010-06-08 16:52:24 +0000228
Stephen Wilsonddd29622010-07-13 23:07:23 +0000229 /// @name ELF section header dump routines
230 //@{
231 void
232 DumpELFSectionHeaders(lldb_private::Stream *s);
Chris Lattner24943d22010-06-08 16:52:24 +0000233
Stephen Wilsonddd29622010-07-13 23:07:23 +0000234 static void
235 DumpELFSectionHeader(lldb_private::Stream *s,
236 const elf::ELFSectionHeader& sh);
Chris Lattner24943d22010-06-08 16:52:24 +0000237
Stephen Wilsonddd29622010-07-13 23:07:23 +0000238 static void
239 DumpELFSectionHeader_sh_type(lldb_private::Stream *s,
240 elf::elf_word sh_type);
Chris Lattner24943d22010-06-08 16:52:24 +0000241
Stephen Wilsonddd29622010-07-13 23:07:23 +0000242 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
Chris Lattner24943d22010-06-08 16:52:24 +0000251};
252
253#endif // #ifndef liblldb_ObjectFileELF_h_