blob: 6efd98123ad6c9125d5de91cc068c4d113a9321b [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
Greg Clayton57abc5d2013-05-10 21:47:16 +000041 static lldb_private::ConstString
Chris Lattner30fdc8d2010-06-08 16:52:24 +000042 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
Greg Claytonf4d6de62013-04-24 22:29:28 +000061 static size_t
62 GetModuleSpecifications (const lldb_private::FileSpec& file,
63 lldb::DataBufferSP& data_sp,
64 lldb::offset_t data_offset,
65 lldb::offset_t file_offset,
66 lldb::offset_t length,
67 lldb_private::ModuleSpecList &specs);
Michael Sartain9f0013d2013-05-17 00:20:21 +000068
69 static bool
70 MagicBytesMatch (lldb::DataBufferSP& data_sp,
71 lldb::addr_t offset,
72 lldb::addr_t length);
73
Chris Lattner30fdc8d2010-06-08 16:52:24 +000074 //------------------------------------------------------------------
75 // PluginInterface protocol
76 //------------------------------------------------------------------
Greg Clayton57abc5d2013-05-10 21:47:16 +000077 virtual lldb_private::ConstString
Chris Lattner30fdc8d2010-06-08 16:52:24 +000078 GetPluginName();
79
Chris Lattner30fdc8d2010-06-08 16:52:24 +000080 virtual uint32_t
81 GetPluginVersion();
82
Stephen Wilsonf325ba92010-07-13 23:07:23 +000083 //------------------------------------------------------------------
84 // ObjectFile Protocol.
85 //------------------------------------------------------------------
86 virtual
87 ~ObjectFileELF();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000088
Stephen Wilsonf325ba92010-07-13 23:07:23 +000089 virtual bool
90 ParseHeader();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000091
Stephen Wilsonf325ba92010-07-13 23:07:23 +000092 virtual lldb::ByteOrder
93 GetByteOrder() const;
94
Jim Ingham5aee1622010-08-09 23:31:02 +000095 virtual bool
96 IsExecutable () const;
97
Greg Claytonc7bece562013-01-25 18:06:21 +000098 virtual uint32_t
Stephen Wilsonf325ba92010-07-13 23:07:23 +000099 GetAddressByteSize() const;
100
101 virtual lldb_private::Symtab *
102 GetSymtab();
103
104 virtual lldb_private::SectionList *
105 GetSectionList();
106
107 virtual void
108 Dump(lldb_private::Stream *s);
109
110 virtual bool
Greg Clayton514487e2011-02-15 21:59:32 +0000111 GetArchitecture (lldb_private::ArchSpec &arch);
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000112
113 virtual bool
114 GetUUID(lldb_private::UUID* uuid);
115
116 virtual uint32_t
117 GetDependentModules(lldb_private::FileSpecList& files);
118
Stephen Wilson2ab0a582011-01-15 00:08:44 +0000119 virtual lldb_private::Address
120 GetImageInfoAddress();
Jim Ingham672e6f52011-03-07 23:44:08 +0000121
122 virtual lldb_private::Address
123 GetEntryPointAddress ();
Greg Clayton9e00b6a652011-07-09 00:41:34 +0000124
125 virtual ObjectFile::Type
126 CalculateType();
127
128 virtual ObjectFile::Strata
129 CalculateStrata();
Stephen Wilson2ab0a582011-01-15 00:08:44 +0000130
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000131private:
Greg Claytone72dfb32012-02-24 01:59:29 +0000132 ObjectFileELF(const lldb::ModuleSP &module_sp,
Greg Clayton5ce9c562013-02-06 17:22:03 +0000133 lldb::DataBufferSP& data_sp,
134 lldb::offset_t data_offset,
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000135 const lldb_private::FileSpec* file,
Greg Clayton5ce9c562013-02-06 17:22:03 +0000136 lldb::offset_t offset,
137 lldb::offset_t length);
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000138
139 typedef std::vector<elf::ELFProgramHeader> ProgramHeaderColl;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000140 typedef ProgramHeaderColl::iterator ProgramHeaderCollIter;
141 typedef ProgramHeaderColl::const_iterator ProgramHeaderCollConstIter;
142
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000143 typedef std::vector<elf::ELFSectionHeader> SectionHeaderColl;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000144 typedef SectionHeaderColl::iterator SectionHeaderCollIter;
145 typedef SectionHeaderColl::const_iterator SectionHeaderCollConstIter;
146
Stephen Wilson499b40e2011-03-30 16:07:05 +0000147 typedef std::vector<elf::ELFDynamic> DynamicSymbolColl;
148 typedef DynamicSymbolColl::iterator DynamicSymbolCollIter;
149 typedef DynamicSymbolColl::const_iterator DynamicSymbolCollConstIter;
150
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000151 /// Version of this reader common to all plugins based on this class.
152 static const uint32_t m_plugin_version = 1;
153
154 /// ELF file header.
155 elf::ELFHeader m_header;
156
157 /// Collection of program headers.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000158 ProgramHeaderColl m_program_headers;
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000159
160 /// Collection of section headers.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000161 SectionHeaderColl m_section_headers;
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000162
Stephen Wilson499b40e2011-03-30 16:07:05 +0000163 /// Collection of symbols from the dynamic table.
164 DynamicSymbolColl m_dynamic_symbols;
165
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000166 /// List of file specifications corresponding to the modules (shared
167 /// libraries) on which this object file depends.
Greg Clayton7b0992d2013-04-18 22:45:39 +0000168 mutable std::unique_ptr<lldb_private::FileSpecList> m_filespec_ap;
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000169
170 /// Data extractor holding the string table used to resolve section names.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000171 lldb_private::DataExtractor m_shstr_data;
172
Jim Ingham672e6f52011-03-07 23:44:08 +0000173 /// Cached value of the entry point for this module.
174 lldb_private::Address m_entry_point_address;
Stephen Wilson499b40e2011-03-30 16:07:05 +0000175
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000176 /// Returns a 1 based index of the given section header.
Greg Claytonc7bece562013-01-25 18:06:21 +0000177 size_t
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000178 SectionIndex(const SectionHeaderCollIter &I);
179
180 /// Returns a 1 based index of the given section header.
Greg Claytonc7bece562013-01-25 18:06:21 +0000181 size_t
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000182 SectionIndex(const SectionHeaderCollConstIter &I) const;
183
184 /// Parses all section headers present in this object file and populates
185 /// m_program_headers. This method will compute the header list only once.
186 /// Returns the number of headers parsed.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000187 size_t
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000188 ParseProgramHeaders();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000189
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000190 /// Parses all section headers present in this object file and populates
191 /// m_section_headers. This method will compute the header list only once.
192 /// Returns the number of headers parsed.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000193 size_t
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000194 ParseSectionHeaders();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000195
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000196 /// Scans the dynamic section and locates all dependent modules (shared
Greg Clayton710dd5a2011-01-08 20:28:42 +0000197 /// libraries) populating m_filespec_ap. This method will compute the
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000198 /// dependent module list only once. Returns the number of dependent
199 /// modules parsed.
200 size_t
201 ParseDependentModules();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000202
Stephen Wilson499b40e2011-03-30 16:07:05 +0000203 /// Parses the dynamic symbol table and populates m_dynamic_symbols. The
204 /// vector retains the order as found in the object file. Returns the
205 /// number of dynamic symbols parsed.
206 size_t
207 ParseDynamicSymbols();
208
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000209 /// Populates m_symtab_ap will all non-dynamic linker symbols. This method
210 /// will parse the symbols only once. Returns the number of symbols parsed.
Stephen Wilson499b40e2011-03-30 16:07:05 +0000211 unsigned
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000212 ParseSymbolTable(lldb_private::Symtab *symbol_table,
Stephen Wilson499b40e2011-03-30 16:07:05 +0000213 lldb::user_id_t start_id,
214 const elf::ELFSectionHeader *symtab_section,
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000215 lldb::user_id_t symtab_id);
216
Stephen Wilson499b40e2011-03-30 16:07:05 +0000217 /// Scans the relocation entries and adds a set of artificial symbols to the
218 /// given symbol table for each PLT slot. Returns the number of symbols
219 /// added.
220 unsigned
221 ParseTrampolineSymbols(lldb_private::Symtab *symbol_table,
222 lldb::user_id_t start_id,
223 const elf::ELFSectionHeader *rela_hdr,
224 lldb::user_id_t section_id);
225
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000226 /// Loads the section name string table into m_shstr_data. Returns the
227 /// number of bytes constituting the table.
228 size_t
229 GetSectionHeaderStringTable();
230
231 /// Utility method for looking up a section given its name. Returns the
232 /// index of the corresponding section or zero if no section with the given
233 /// name can be found (note that section indices are always 1 based, and so
234 /// section index 0 is never valid).
235 lldb::user_id_t
236 GetSectionIndexByName(const char *name);
237
Stephen Wilson499b40e2011-03-30 16:07:05 +0000238 // Returns the ID of the first section that has the given type.
239 lldb::user_id_t
240 GetSectionIndexByType(unsigned type);
241
242 /// Returns the section header with the given id or NULL.
243 const elf::ELFSectionHeader *
244 GetSectionHeaderByIndex(lldb::user_id_t id);
245
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000246 /// @name ELF header dump routines
247 //@{
248 static void
249 DumpELFHeader(lldb_private::Stream *s, const elf::ELFHeader& header);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000250
251 static void
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000252 DumpELFHeader_e_ident_EI_DATA(lldb_private::Stream *s,
253 unsigned char ei_data);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000254
255 static void
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000256 DumpELFHeader_e_type(lldb_private::Stream *s, elf::elf_half e_type);
257 //@}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000258
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000259 /// @name ELF program header dump routines
260 //@{
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000261 void
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000262 DumpELFProgramHeaders(lldb_private::Stream *s);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000263
264 static void
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000265 DumpELFProgramHeader(lldb_private::Stream *s,
266 const elf::ELFProgramHeader &ph);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000267
268 static void
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000269 DumpELFProgramHeader_p_type(lldb_private::Stream *s, elf::elf_word p_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000270
271 static void
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000272 DumpELFProgramHeader_p_flags(lldb_private::Stream *s,
273 elf::elf_word p_flags);
274 //@}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000275
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000276 /// @name ELF section header dump routines
277 //@{
278 void
279 DumpELFSectionHeaders(lldb_private::Stream *s);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000280
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000281 static void
282 DumpELFSectionHeader(lldb_private::Stream *s,
283 const elf::ELFSectionHeader& sh);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000284
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000285 static void
286 DumpELFSectionHeader_sh_type(lldb_private::Stream *s,
287 elf::elf_word sh_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000288
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000289 static void
290 DumpELFSectionHeader_sh_flags(lldb_private::Stream *s,
Greg Claytonc7bece562013-01-25 18:06:21 +0000291 elf::elf_xword sh_flags);
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000292 //@}
293
294 /// ELF dependent module dump routine.
295 void
296 DumpDependentModules(lldb_private::Stream *s);
297
Stephen Wilson499b40e2011-03-30 16:07:05 +0000298 const elf::ELFDynamic *
299 FindDynamicSymbol(unsigned tag);
300
301 lldb_private::Section *
302 PLTSection();
303
304 unsigned
305 PLTRelocationType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000306};
307
308#endif // #ifndef liblldb_ObjectFileELF_h_