blob: 78cfb3f5157c7a827cd957c2ab48787cfbf968ed [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
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);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000068 //------------------------------------------------------------------
69 // PluginInterface protocol
70 //------------------------------------------------------------------
71 virtual const char *
72 GetPluginName();
73
74 virtual const char *
75 GetShortPluginName();
76
77 virtual uint32_t
78 GetPluginVersion();
79
Stephen Wilsonf325ba92010-07-13 23:07:23 +000080 //------------------------------------------------------------------
81 // ObjectFile Protocol.
82 //------------------------------------------------------------------
83 virtual
84 ~ObjectFileELF();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000085
Stephen Wilsonf325ba92010-07-13 23:07:23 +000086 virtual bool
87 ParseHeader();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000088
Stephen Wilsonf325ba92010-07-13 23:07:23 +000089 virtual lldb::ByteOrder
90 GetByteOrder() const;
91
Jim Ingham5aee1622010-08-09 23:31:02 +000092 virtual bool
93 IsExecutable () const;
94
Greg Claytonc7bece562013-01-25 18:06:21 +000095 virtual uint32_t
Stephen Wilsonf325ba92010-07-13 23:07:23 +000096 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
Greg Clayton514487e2011-02-15 21:59:32 +0000108 GetArchitecture (lldb_private::ArchSpec &arch);
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000109
110 virtual bool
111 GetUUID(lldb_private::UUID* uuid);
112
113 virtual uint32_t
114 GetDependentModules(lldb_private::FileSpecList& files);
115
Stephen Wilson2ab0a582011-01-15 00:08:44 +0000116 virtual lldb_private::Address
117 GetImageInfoAddress();
Jim Ingham672e6f52011-03-07 23:44:08 +0000118
119 virtual lldb_private::Address
120 GetEntryPointAddress ();
Greg Clayton9e00b6a652011-07-09 00:41:34 +0000121
122 virtual ObjectFile::Type
123 CalculateType();
124
125 virtual ObjectFile::Strata
126 CalculateStrata();
Stephen Wilson2ab0a582011-01-15 00:08:44 +0000127
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000128private:
Greg Claytone72dfb32012-02-24 01:59:29 +0000129 ObjectFileELF(const lldb::ModuleSP &module_sp,
Greg Clayton5ce9c562013-02-06 17:22:03 +0000130 lldb::DataBufferSP& data_sp,
131 lldb::offset_t data_offset,
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000132 const lldb_private::FileSpec* file,
Greg Clayton5ce9c562013-02-06 17:22:03 +0000133 lldb::offset_t offset,
134 lldb::offset_t length);
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000135
136 typedef std::vector<elf::ELFProgramHeader> ProgramHeaderColl;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000137 typedef ProgramHeaderColl::iterator ProgramHeaderCollIter;
138 typedef ProgramHeaderColl::const_iterator ProgramHeaderCollConstIter;
139
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000140 typedef std::vector<elf::ELFSectionHeader> SectionHeaderColl;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000141 typedef SectionHeaderColl::iterator SectionHeaderCollIter;
142 typedef SectionHeaderColl::const_iterator SectionHeaderCollConstIter;
143
Stephen Wilson499b40e2011-03-30 16:07:05 +0000144 typedef std::vector<elf::ELFDynamic> DynamicSymbolColl;
145 typedef DynamicSymbolColl::iterator DynamicSymbolCollIter;
146 typedef DynamicSymbolColl::const_iterator DynamicSymbolCollConstIter;
147
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000148 /// Version of this reader common to all plugins based on this class.
149 static const uint32_t m_plugin_version = 1;
150
151 /// ELF file header.
152 elf::ELFHeader m_header;
153
154 /// Collection of program headers.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000155 ProgramHeaderColl m_program_headers;
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000156
157 /// Collection of section headers.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000158 SectionHeaderColl m_section_headers;
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000159
Stephen Wilson499b40e2011-03-30 16:07:05 +0000160 /// Collection of symbols from the dynamic table.
161 DynamicSymbolColl m_dynamic_symbols;
162
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000163 /// List of file specifications corresponding to the modules (shared
164 /// libraries) on which this object file depends.
Greg Clayton7b0992d2013-04-18 22:45:39 +0000165 mutable std::unique_ptr<lldb_private::FileSpecList> m_filespec_ap;
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000166
167 /// Data extractor holding the string table used to resolve section names.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000168 lldb_private::DataExtractor m_shstr_data;
169
Jim Ingham672e6f52011-03-07 23:44:08 +0000170 /// Cached value of the entry point for this module.
171 lldb_private::Address m_entry_point_address;
Stephen Wilson499b40e2011-03-30 16:07:05 +0000172
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000173 /// Returns a 1 based index of the given section header.
Greg Claytonc7bece562013-01-25 18:06:21 +0000174 size_t
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000175 SectionIndex(const SectionHeaderCollIter &I);
176
177 /// Returns a 1 based index of the given section header.
Greg Claytonc7bece562013-01-25 18:06:21 +0000178 size_t
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000179 SectionIndex(const SectionHeaderCollConstIter &I) const;
180
181 /// Parses all section headers present in this object file and populates
182 /// m_program_headers. This method will compute the header list only once.
183 /// Returns the number of headers parsed.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000184 size_t
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000185 ParseProgramHeaders();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000186
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000187 /// Parses all section headers present in this object file and populates
188 /// m_section_headers. This method will compute the header list only once.
189 /// Returns the number of headers parsed.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000190 size_t
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000191 ParseSectionHeaders();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000192
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000193 /// Scans the dynamic section and locates all dependent modules (shared
Greg Clayton710dd5a2011-01-08 20:28:42 +0000194 /// libraries) populating m_filespec_ap. This method will compute the
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000195 /// dependent module list only once. Returns the number of dependent
196 /// modules parsed.
197 size_t
198 ParseDependentModules();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000199
Stephen Wilson499b40e2011-03-30 16:07:05 +0000200 /// Parses the dynamic symbol table and populates m_dynamic_symbols. The
201 /// vector retains the order as found in the object file. Returns the
202 /// number of dynamic symbols parsed.
203 size_t
204 ParseDynamicSymbols();
205
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000206 /// Populates m_symtab_ap will all non-dynamic linker symbols. This method
207 /// will parse the symbols only once. Returns the number of symbols parsed.
Stephen Wilson499b40e2011-03-30 16:07:05 +0000208 unsigned
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000209 ParseSymbolTable(lldb_private::Symtab *symbol_table,
Stephen Wilson499b40e2011-03-30 16:07:05 +0000210 lldb::user_id_t start_id,
211 const elf::ELFSectionHeader *symtab_section,
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000212 lldb::user_id_t symtab_id);
213
Stephen Wilson499b40e2011-03-30 16:07:05 +0000214 /// Scans the relocation entries and adds a set of artificial symbols to the
215 /// given symbol table for each PLT slot. Returns the number of symbols
216 /// added.
217 unsigned
218 ParseTrampolineSymbols(lldb_private::Symtab *symbol_table,
219 lldb::user_id_t start_id,
220 const elf::ELFSectionHeader *rela_hdr,
221 lldb::user_id_t section_id);
222
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000223 /// Loads the section name string table into m_shstr_data. Returns the
224 /// number of bytes constituting the table.
225 size_t
226 GetSectionHeaderStringTable();
227
228 /// Utility method for looking up a section given its name. Returns the
229 /// index of the corresponding section or zero if no section with the given
230 /// name can be found (note that section indices are always 1 based, and so
231 /// section index 0 is never valid).
232 lldb::user_id_t
233 GetSectionIndexByName(const char *name);
234
Stephen Wilson499b40e2011-03-30 16:07:05 +0000235 // Returns the ID of the first section that has the given type.
236 lldb::user_id_t
237 GetSectionIndexByType(unsigned type);
238
239 /// Returns the section header with the given id or NULL.
240 const elf::ELFSectionHeader *
241 GetSectionHeaderByIndex(lldb::user_id_t id);
242
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000243 /// @name ELF header dump routines
244 //@{
245 static void
246 DumpELFHeader(lldb_private::Stream *s, const elf::ELFHeader& header);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000247
248 static void
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000249 DumpELFHeader_e_ident_EI_DATA(lldb_private::Stream *s,
250 unsigned char ei_data);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000251
252 static void
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000253 DumpELFHeader_e_type(lldb_private::Stream *s, elf::elf_half e_type);
254 //@}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000255
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000256 /// @name ELF program header dump routines
257 //@{
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000258 void
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000259 DumpELFProgramHeaders(lldb_private::Stream *s);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000260
261 static void
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000262 DumpELFProgramHeader(lldb_private::Stream *s,
263 const elf::ELFProgramHeader &ph);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000264
265 static void
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000266 DumpELFProgramHeader_p_type(lldb_private::Stream *s, elf::elf_word p_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000267
268 static void
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000269 DumpELFProgramHeader_p_flags(lldb_private::Stream *s,
270 elf::elf_word p_flags);
271 //@}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000272
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000273 /// @name ELF section header dump routines
274 //@{
275 void
276 DumpELFSectionHeaders(lldb_private::Stream *s);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000277
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000278 static void
279 DumpELFSectionHeader(lldb_private::Stream *s,
280 const elf::ELFSectionHeader& sh);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000281
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000282 static void
283 DumpELFSectionHeader_sh_type(lldb_private::Stream *s,
284 elf::elf_word sh_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000285
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000286 static void
287 DumpELFSectionHeader_sh_flags(lldb_private::Stream *s,
Greg Claytonc7bece562013-01-25 18:06:21 +0000288 elf::elf_xword sh_flags);
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000289 //@}
290
291 /// ELF dependent module dump routine.
292 void
293 DumpDependentModules(lldb_private::Stream *s);
294
Stephen Wilson499b40e2011-03-30 16:07:05 +0000295 const elf::ELFDynamic *
296 FindDynamicSymbol(unsigned tag);
297
298 lldb_private::Section *
299 PLTSection();
300
301 unsigned
302 PLTRelocationType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000303};
304
305#endif // #ifndef liblldb_ObjectFileELF_h_