blob: 7c941a70e0e54012c720f277fdd08eb590eda8fd [file] [log] [blame]
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001//===-- ObjectFileELF.h --------------------------------------- -*- C++ -*-===//
Chris Lattner30fdc8d2010-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 Wilsonf325ba92010-07-13 23:07:23 +000020#include "ELFHeader.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000021
Stephen Wilsonf325ba92010-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 Lattner30fdc8d2010-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 Wilsonf325ba92010-07-13 23:07:23 +000048 CreateInstance(lldb_private::Module* module,
Chris Lattner30fdc8d2010-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 Lattner30fdc8d2010-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 Wilsonf325ba92010-07-13 23:07:23 +000067 GetPluginCommandHelp(const char *command, lldb_private::Stream *strm);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000068
69 virtual lldb_private::Error
Stephen Wilsonf325ba92010-07-13 23:07:23 +000070 ExecutePluginCommand(lldb_private::Args &command,
71 lldb_private::Stream *strm);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000072
73 virtual lldb_private::Log *
Stephen Wilsonf325ba92010-07-13 23:07:23 +000074 EnablePluginLogging(lldb_private::Stream *strm,
75 lldb_private::Args &command);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000076
Stephen Wilsonf325ba92010-07-13 23:07:23 +000077 //------------------------------------------------------------------
78 // ObjectFile Protocol.
79 //------------------------------------------------------------------
80 virtual
81 ~ObjectFileELF();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000082
Stephen Wilsonf325ba92010-07-13 23:07:23 +000083 virtual bool
84 ParseHeader();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000085
Stephen Wilsonf325ba92010-07-13 23:07:23 +000086 virtual lldb::ByteOrder
87 GetByteOrder() const;
88
89 virtual size_t
90 GetAddressByteSize() const;
91
92 virtual lldb_private::Symtab *
93 GetSymtab();
94
95 virtual lldb_private::SectionList *
96 GetSectionList();
97
98 virtual void
99 Dump(lldb_private::Stream *s);
100
101 virtual bool
102 GetTargetTriple(lldb_private::ConstString &target_triple);
103
104 virtual bool
105 GetUUID(lldb_private::UUID* uuid);
106
107 virtual uint32_t
108 GetDependentModules(lldb_private::FileSpecList& files);
109
110private:
111 ObjectFileELF(lldb_private::Module* module,
112 lldb::DataBufferSP& dataSP,
113 const lldb_private::FileSpec* file,
114 lldb::addr_t offset,
115 lldb::addr_t length);
116
117 typedef std::vector<elf::ELFProgramHeader> ProgramHeaderColl;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000118 typedef ProgramHeaderColl::iterator ProgramHeaderCollIter;
119 typedef ProgramHeaderColl::const_iterator ProgramHeaderCollConstIter;
120
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000121 typedef std::vector<elf::ELFSectionHeader> SectionHeaderColl;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000122 typedef SectionHeaderColl::iterator SectionHeaderCollIter;
123 typedef SectionHeaderColl::const_iterator SectionHeaderCollConstIter;
124
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000125 /// Version of this reader common to all plugins based on this class.
126 static const uint32_t m_plugin_version = 1;
127
128 /// ELF file header.
129 elf::ELFHeader m_header;
130
131 /// Collection of program headers.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000132 ProgramHeaderColl m_program_headers;
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000133
134 /// Collection of section headers.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000135 SectionHeaderColl m_section_headers;
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000136
137 /// List of sections present in this ELF object file.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000138 mutable std::auto_ptr<lldb_private::SectionList> m_sections_ap;
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000139
140 /// Table of all non-dynamic symbols present in this object file.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000141 mutable std::auto_ptr<lldb_private::Symtab> m_symtab_ap;
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000142
143 /// List of file specifications corresponding to the modules (shared
144 /// libraries) on which this object file depends.
145 mutable std::auto_ptr<lldb_private::FileSpecList> m_filespec_ap;
146
147 /// Data extractor holding the string table used to resolve section names.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000148 lldb_private::DataExtractor m_shstr_data;
149
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000150 /// Returns a 1 based index of the given section header.
151 unsigned
152 SectionIndex(const SectionHeaderCollIter &I);
153
154 /// Returns a 1 based index of the given section header.
155 unsigned
156 SectionIndex(const SectionHeaderCollConstIter &I) const;
157
158 /// Parses all section headers present in this object file and populates
159 /// m_program_headers. This method will compute the header list only once.
160 /// Returns the number of headers parsed.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000161 size_t
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000162 ParseProgramHeaders();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000163
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000164 /// Parses all section headers present in this object file and populates
165 /// m_section_headers. This method will compute the header list only once.
166 /// Returns the number of headers parsed.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000167 size_t
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000168 ParseSectionHeaders();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000169
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000170 /// Scans the dynamic section and locates all dependent modules (shared
171 /// libaries) populating m_filespec_ap. This method will compute the
172 /// dependent module list only once. Returns the number of dependent
173 /// modules parsed.
174 size_t
175 ParseDependentModules();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000176
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000177 /// Populates m_symtab_ap will all non-dynamic linker symbols. This method
178 /// will parse the symbols only once. Returns the number of symbols parsed.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000179 void
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000180 ParseSymbolTable(lldb_private::Symtab *symbol_table,
181 const elf::ELFSectionHeader &symtab_section,
182 lldb::user_id_t symtab_id);
183
184 /// Loads the section name string table into m_shstr_data. Returns the
185 /// number of bytes constituting the table.
186 size_t
187 GetSectionHeaderStringTable();
188
189 /// Utility method for looking up a section given its name. Returns the
190 /// index of the corresponding section or zero if no section with the given
191 /// name can be found (note that section indices are always 1 based, and so
192 /// section index 0 is never valid).
193 lldb::user_id_t
194 GetSectionIndexByName(const char *name);
195
196 /// @name ELF header dump routines
197 //@{
198 static void
199 DumpELFHeader(lldb_private::Stream *s, const elf::ELFHeader& header);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000200
201 static void
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000202 DumpELFHeader_e_ident_EI_DATA(lldb_private::Stream *s,
203 unsigned char ei_data);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000204
205 static void
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000206 DumpELFHeader_e_type(lldb_private::Stream *s, elf::elf_half e_type);
207 //@}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000208
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000209 /// @name ELF program header dump routines
210 //@{
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000211 void
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000212 DumpELFProgramHeaders(lldb_private::Stream *s);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000213
214 static void
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000215 DumpELFProgramHeader(lldb_private::Stream *s,
216 const elf::ELFProgramHeader &ph);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000217
218 static void
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000219 DumpELFProgramHeader_p_type(lldb_private::Stream *s, elf::elf_word p_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000220
221 static void
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000222 DumpELFProgramHeader_p_flags(lldb_private::Stream *s,
223 elf::elf_word p_flags);
224 //@}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000225
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000226 /// @name ELF section header dump routines
227 //@{
228 void
229 DumpELFSectionHeaders(lldb_private::Stream *s);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000230
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000231 static void
232 DumpELFSectionHeader(lldb_private::Stream *s,
233 const elf::ELFSectionHeader& sh);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000234
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000235 static void
236 DumpELFSectionHeader_sh_type(lldb_private::Stream *s,
237 elf::elf_word sh_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000238
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000239 static void
240 DumpELFSectionHeader_sh_flags(lldb_private::Stream *s,
241 elf::elf_word sh_flags);
242 //@}
243
244 /// ELF dependent module dump routine.
245 void
246 DumpDependentModules(lldb_private::Stream *s);
247
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000248};
249
250#endif // #ifndef liblldb_ObjectFileELF_h_