blob: 66b6f7110c2257b99cf744d515ebeef8ec134e3d [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"
Greg Clayton53239f02011-02-08 05:05:52 +000017#include "lldb/Host/FileSpec.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000018#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 *
Greg Claytone72dfb32012-02-24 01:59:29 +000048 CreateInstance(const lldb::ModuleSP &module_sp,
Greg Clayton5ce9c562013-02-06 17:22:03 +000049 lldb::DataBufferSP& data_sp,
50 lldb::offset_t data_offset,
Chris Lattner30fdc8d2010-06-08 16:52:24 +000051 const lldb_private::FileSpec* file,
Greg Clayton5ce9c562013-02-06 17:22:03 +000052 lldb::offset_t file_offset,
53 lldb::offset_t length);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000054
Greg Claytonc9660542012-02-05 02:38:54 +000055 static lldb_private::ObjectFile *
Greg Claytone72dfb32012-02-24 01:59:29 +000056 CreateMemoryInstance (const lldb::ModuleSP &module_sp,
Greg Claytonc9660542012-02-05 02:38:54 +000057 lldb::DataBufferSP& data_sp,
58 const lldb::ProcessSP &process_sp,
59 lldb::addr_t header_addr);
60
Chris Lattner30fdc8d2010-06-08 16:52:24 +000061 //------------------------------------------------------------------
62 // PluginInterface protocol
63 //------------------------------------------------------------------
64 virtual const char *
65 GetPluginName();
66
67 virtual const char *
68 GetShortPluginName();
69
70 virtual uint32_t
71 GetPluginVersion();
72
Stephen Wilsonf325ba92010-07-13 23:07:23 +000073 //------------------------------------------------------------------
74 // ObjectFile Protocol.
75 //------------------------------------------------------------------
76 virtual
77 ~ObjectFileELF();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000078
Stephen Wilsonf325ba92010-07-13 23:07:23 +000079 virtual bool
80 ParseHeader();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000081
Stephen Wilsonf325ba92010-07-13 23:07:23 +000082 virtual lldb::ByteOrder
83 GetByteOrder() const;
84
Jim Ingham5aee1622010-08-09 23:31:02 +000085 virtual bool
86 IsExecutable () const;
87
Greg Claytonc7bece562013-01-25 18:06:21 +000088 virtual uint32_t
Stephen Wilsonf325ba92010-07-13 23:07:23 +000089 GetAddressByteSize() const;
90
91 virtual lldb_private::Symtab *
92 GetSymtab();
93
94 virtual lldb_private::SectionList *
95 GetSectionList();
96
97 virtual void
98 Dump(lldb_private::Stream *s);
99
100 virtual bool
Greg Clayton514487e2011-02-15 21:59:32 +0000101 GetArchitecture (lldb_private::ArchSpec &arch);
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000102
103 virtual bool
104 GetUUID(lldb_private::UUID* uuid);
105
106 virtual uint32_t
107 GetDependentModules(lldb_private::FileSpecList& files);
108
Stephen Wilson2ab0a582011-01-15 00:08:44 +0000109 virtual lldb_private::Address
110 GetImageInfoAddress();
Jim Ingham672e6f52011-03-07 23:44:08 +0000111
112 virtual lldb_private::Address
113 GetEntryPointAddress ();
Greg Clayton9e00b6a652011-07-09 00:41:34 +0000114
115 virtual ObjectFile::Type
116 CalculateType();
117
118 virtual ObjectFile::Strata
119 CalculateStrata();
Stephen Wilson2ab0a582011-01-15 00:08:44 +0000120
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000121private:
Greg Claytone72dfb32012-02-24 01:59:29 +0000122 ObjectFileELF(const lldb::ModuleSP &module_sp,
Greg Clayton5ce9c562013-02-06 17:22:03 +0000123 lldb::DataBufferSP& data_sp,
124 lldb::offset_t data_offset,
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000125 const lldb_private::FileSpec* file,
Greg Clayton5ce9c562013-02-06 17:22:03 +0000126 lldb::offset_t offset,
127 lldb::offset_t length);
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000128
129 typedef std::vector<elf::ELFProgramHeader> ProgramHeaderColl;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000130 typedef ProgramHeaderColl::iterator ProgramHeaderCollIter;
131 typedef ProgramHeaderColl::const_iterator ProgramHeaderCollConstIter;
132
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000133 typedef std::vector<elf::ELFSectionHeader> SectionHeaderColl;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000134 typedef SectionHeaderColl::iterator SectionHeaderCollIter;
135 typedef SectionHeaderColl::const_iterator SectionHeaderCollConstIter;
136
Stephen Wilson499b40e2011-03-30 16:07:05 +0000137 typedef std::vector<elf::ELFDynamic> DynamicSymbolColl;
138 typedef DynamicSymbolColl::iterator DynamicSymbolCollIter;
139 typedef DynamicSymbolColl::const_iterator DynamicSymbolCollConstIter;
140
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000141 /// Version of this reader common to all plugins based on this class.
142 static const uint32_t m_plugin_version = 1;
143
144 /// ELF file header.
145 elf::ELFHeader m_header;
146
147 /// Collection of program headers.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000148 ProgramHeaderColl m_program_headers;
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000149
150 /// Collection of section headers.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000151 SectionHeaderColl m_section_headers;
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000152
Stephen Wilson499b40e2011-03-30 16:07:05 +0000153 /// Collection of symbols from the dynamic table.
154 DynamicSymbolColl m_dynamic_symbols;
155
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000156 /// List of file specifications corresponding to the modules (shared
157 /// libraries) on which this object file depends.
Greg Clayton7b0992d2013-04-18 22:45:39 +0000158 mutable std::unique_ptr<lldb_private::FileSpecList> m_filespec_ap;
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000159
160 /// Data extractor holding the string table used to resolve section names.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000161 lldb_private::DataExtractor m_shstr_data;
162
Jim Ingham672e6f52011-03-07 23:44:08 +0000163 /// Cached value of the entry point for this module.
164 lldb_private::Address m_entry_point_address;
Stephen Wilson499b40e2011-03-30 16:07:05 +0000165
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000166 /// Returns a 1 based index of the given section header.
Greg Claytonc7bece562013-01-25 18:06:21 +0000167 size_t
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000168 SectionIndex(const SectionHeaderCollIter &I);
169
170 /// Returns a 1 based index of the given section header.
Greg Claytonc7bece562013-01-25 18:06:21 +0000171 size_t
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000172 SectionIndex(const SectionHeaderCollConstIter &I) const;
173
174 /// Parses all section headers present in this object file and populates
175 /// m_program_headers. This method will compute the header list only once.
176 /// Returns the number of headers parsed.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000177 size_t
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000178 ParseProgramHeaders();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000179
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000180 /// Parses all section headers present in this object file and populates
181 /// m_section_headers. This method will compute the header list only once.
182 /// Returns the number of headers parsed.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000183 size_t
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000184 ParseSectionHeaders();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000185
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000186 /// Scans the dynamic section and locates all dependent modules (shared
Greg Clayton710dd5a2011-01-08 20:28:42 +0000187 /// libraries) populating m_filespec_ap. This method will compute the
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000188 /// dependent module list only once. Returns the number of dependent
189 /// modules parsed.
190 size_t
191 ParseDependentModules();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000192
Stephen Wilson499b40e2011-03-30 16:07:05 +0000193 /// Parses the dynamic symbol table and populates m_dynamic_symbols. The
194 /// vector retains the order as found in the object file. Returns the
195 /// number of dynamic symbols parsed.
196 size_t
197 ParseDynamicSymbols();
198
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000199 /// Populates m_symtab_ap will all non-dynamic linker symbols. This method
200 /// will parse the symbols only once. Returns the number of symbols parsed.
Stephen Wilson499b40e2011-03-30 16:07:05 +0000201 unsigned
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000202 ParseSymbolTable(lldb_private::Symtab *symbol_table,
Stephen Wilson499b40e2011-03-30 16:07:05 +0000203 lldb::user_id_t start_id,
204 const elf::ELFSectionHeader *symtab_section,
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000205 lldb::user_id_t symtab_id);
206
Stephen Wilson499b40e2011-03-30 16:07:05 +0000207 /// Scans the relocation entries and adds a set of artificial symbols to the
208 /// given symbol table for each PLT slot. Returns the number of symbols
209 /// added.
210 unsigned
211 ParseTrampolineSymbols(lldb_private::Symtab *symbol_table,
212 lldb::user_id_t start_id,
213 const elf::ELFSectionHeader *rela_hdr,
214 lldb::user_id_t section_id);
215
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000216 /// Loads the section name string table into m_shstr_data. Returns the
217 /// number of bytes constituting the table.
218 size_t
219 GetSectionHeaderStringTable();
220
221 /// Utility method for looking up a section given its name. Returns the
222 /// index of the corresponding section or zero if no section with the given
223 /// name can be found (note that section indices are always 1 based, and so
224 /// section index 0 is never valid).
225 lldb::user_id_t
226 GetSectionIndexByName(const char *name);
227
Stephen Wilson499b40e2011-03-30 16:07:05 +0000228 // Returns the ID of the first section that has the given type.
229 lldb::user_id_t
230 GetSectionIndexByType(unsigned type);
231
232 /// Returns the section header with the given id or NULL.
233 const elf::ELFSectionHeader *
234 GetSectionHeaderByIndex(lldb::user_id_t id);
235
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000236 /// @name ELF header dump routines
237 //@{
238 static void
239 DumpELFHeader(lldb_private::Stream *s, const elf::ELFHeader& header);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000240
241 static void
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000242 DumpELFHeader_e_ident_EI_DATA(lldb_private::Stream *s,
243 unsigned char ei_data);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000244
245 static void
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000246 DumpELFHeader_e_type(lldb_private::Stream *s, elf::elf_half e_type);
247 //@}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000248
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000249 /// @name ELF program header dump routines
250 //@{
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000251 void
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000252 DumpELFProgramHeaders(lldb_private::Stream *s);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000253
254 static void
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000255 DumpELFProgramHeader(lldb_private::Stream *s,
256 const elf::ELFProgramHeader &ph);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000257
258 static void
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000259 DumpELFProgramHeader_p_type(lldb_private::Stream *s, elf::elf_word p_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000260
261 static void
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000262 DumpELFProgramHeader_p_flags(lldb_private::Stream *s,
263 elf::elf_word p_flags);
264 //@}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000265
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000266 /// @name ELF section header dump routines
267 //@{
268 void
269 DumpELFSectionHeaders(lldb_private::Stream *s);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000270
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000271 static void
272 DumpELFSectionHeader(lldb_private::Stream *s,
273 const elf::ELFSectionHeader& sh);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000274
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000275 static void
276 DumpELFSectionHeader_sh_type(lldb_private::Stream *s,
277 elf::elf_word sh_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000278
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000279 static void
280 DumpELFSectionHeader_sh_flags(lldb_private::Stream *s,
Greg Claytonc7bece562013-01-25 18:06:21 +0000281 elf::elf_xword sh_flags);
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000282 //@}
283
284 /// ELF dependent module dump routine.
285 void
286 DumpDependentModules(lldb_private::Stream *s);
287
Stephen Wilson499b40e2011-03-30 16:07:05 +0000288 const elf::ELFDynamic *
289 FindDynamicSymbol(unsigned tag);
290
291 lldb_private::Section *
292 PLTSection();
293
294 unsigned
295 PLTRelocationType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000296};
297
298#endif // #ifndef liblldb_ObjectFileELF_h_