blob: cdf2731b254172c0626ec7c2db59082138ad6947 [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);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000068 //------------------------------------------------------------------
69 // PluginInterface protocol
70 //------------------------------------------------------------------
Greg Clayton57abc5d2013-05-10 21:47:16 +000071 virtual lldb_private::ConstString
Chris Lattner30fdc8d2010-06-08 16:52:24 +000072 GetPluginName();
73
Chris Lattner30fdc8d2010-06-08 16:52:24 +000074 virtual uint32_t
75 GetPluginVersion();
76
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
Jim Ingham5aee1622010-08-09 23:31:02 +000089 virtual bool
90 IsExecutable () const;
91
Greg Claytonc7bece562013-01-25 18:06:21 +000092 virtual uint32_t
Stephen Wilsonf325ba92010-07-13 23:07:23 +000093 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
Greg Clayton514487e2011-02-15 21:59:32 +0000105 GetArchitecture (lldb_private::ArchSpec &arch);
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000106
107 virtual bool
108 GetUUID(lldb_private::UUID* uuid);
109
110 virtual uint32_t
111 GetDependentModules(lldb_private::FileSpecList& files);
112
Stephen Wilson2ab0a582011-01-15 00:08:44 +0000113 virtual lldb_private::Address
114 GetImageInfoAddress();
Jim Ingham672e6f52011-03-07 23:44:08 +0000115
116 virtual lldb_private::Address
117 GetEntryPointAddress ();
Greg Clayton9e00b6a652011-07-09 00:41:34 +0000118
119 virtual ObjectFile::Type
120 CalculateType();
121
122 virtual ObjectFile::Strata
123 CalculateStrata();
Stephen Wilson2ab0a582011-01-15 00:08:44 +0000124
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000125private:
Greg Claytone72dfb32012-02-24 01:59:29 +0000126 ObjectFileELF(const lldb::ModuleSP &module_sp,
Greg Clayton5ce9c562013-02-06 17:22:03 +0000127 lldb::DataBufferSP& data_sp,
128 lldb::offset_t data_offset,
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000129 const lldb_private::FileSpec* file,
Greg Clayton5ce9c562013-02-06 17:22:03 +0000130 lldb::offset_t offset,
131 lldb::offset_t length);
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000132
133 typedef std::vector<elf::ELFProgramHeader> ProgramHeaderColl;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000134 typedef ProgramHeaderColl::iterator ProgramHeaderCollIter;
135 typedef ProgramHeaderColl::const_iterator ProgramHeaderCollConstIter;
136
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000137 typedef std::vector<elf::ELFSectionHeader> SectionHeaderColl;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000138 typedef SectionHeaderColl::iterator SectionHeaderCollIter;
139 typedef SectionHeaderColl::const_iterator SectionHeaderCollConstIter;
140
Stephen Wilson499b40e2011-03-30 16:07:05 +0000141 typedef std::vector<elf::ELFDynamic> DynamicSymbolColl;
142 typedef DynamicSymbolColl::iterator DynamicSymbolCollIter;
143 typedef DynamicSymbolColl::const_iterator DynamicSymbolCollConstIter;
144
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000145 /// Version of this reader common to all plugins based on this class.
146 static const uint32_t m_plugin_version = 1;
147
148 /// ELF file header.
149 elf::ELFHeader m_header;
150
151 /// Collection of program headers.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000152 ProgramHeaderColl m_program_headers;
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000153
154 /// Collection of section headers.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000155 SectionHeaderColl m_section_headers;
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000156
Stephen Wilson499b40e2011-03-30 16:07:05 +0000157 /// Collection of symbols from the dynamic table.
158 DynamicSymbolColl m_dynamic_symbols;
159
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000160 /// List of file specifications corresponding to the modules (shared
161 /// libraries) on which this object file depends.
Greg Clayton7b0992d2013-04-18 22:45:39 +0000162 mutable std::unique_ptr<lldb_private::FileSpecList> m_filespec_ap;
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000163
164 /// Data extractor holding the string table used to resolve section names.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000165 lldb_private::DataExtractor m_shstr_data;
166
Jim Ingham672e6f52011-03-07 23:44:08 +0000167 /// Cached value of the entry point for this module.
168 lldb_private::Address m_entry_point_address;
Stephen Wilson499b40e2011-03-30 16:07:05 +0000169
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000170 /// 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 SectionHeaderCollIter &I);
173
174 /// Returns a 1 based index of the given section header.
Greg Claytonc7bece562013-01-25 18:06:21 +0000175 size_t
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000176 SectionIndex(const SectionHeaderCollConstIter &I) const;
177
178 /// Parses all section headers present in this object file and populates
179 /// m_program_headers. This method will compute the header list only once.
180 /// Returns the number of headers parsed.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000181 size_t
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000182 ParseProgramHeaders();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000183
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000184 /// Parses all section headers present in this object file and populates
185 /// m_section_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 ParseSectionHeaders();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000189
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000190 /// Scans the dynamic section and locates all dependent modules (shared
Greg Clayton710dd5a2011-01-08 20:28:42 +0000191 /// libraries) populating m_filespec_ap. This method will compute the
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000192 /// dependent module list only once. Returns the number of dependent
193 /// modules parsed.
194 size_t
195 ParseDependentModules();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000196
Stephen Wilson499b40e2011-03-30 16:07:05 +0000197 /// Parses the dynamic symbol table and populates m_dynamic_symbols. The
198 /// vector retains the order as found in the object file. Returns the
199 /// number of dynamic symbols parsed.
200 size_t
201 ParseDynamicSymbols();
202
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000203 /// Populates m_symtab_ap will all non-dynamic linker symbols. This method
204 /// will parse the symbols only once. Returns the number of symbols parsed.
Stephen Wilson499b40e2011-03-30 16:07:05 +0000205 unsigned
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000206 ParseSymbolTable(lldb_private::Symtab *symbol_table,
Stephen Wilson499b40e2011-03-30 16:07:05 +0000207 lldb::user_id_t start_id,
208 const elf::ELFSectionHeader *symtab_section,
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000209 lldb::user_id_t symtab_id);
210
Stephen Wilson499b40e2011-03-30 16:07:05 +0000211 /// Scans the relocation entries and adds a set of artificial symbols to the
212 /// given symbol table for each PLT slot. Returns the number of symbols
213 /// added.
214 unsigned
215 ParseTrampolineSymbols(lldb_private::Symtab *symbol_table,
216 lldb::user_id_t start_id,
217 const elf::ELFSectionHeader *rela_hdr,
218 lldb::user_id_t section_id);
219
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000220 /// Loads the section name string table into m_shstr_data. Returns the
221 /// number of bytes constituting the table.
222 size_t
223 GetSectionHeaderStringTable();
224
225 /// Utility method for looking up a section given its name. Returns the
226 /// index of the corresponding section or zero if no section with the given
227 /// name can be found (note that section indices are always 1 based, and so
228 /// section index 0 is never valid).
229 lldb::user_id_t
230 GetSectionIndexByName(const char *name);
231
Stephen Wilson499b40e2011-03-30 16:07:05 +0000232 // Returns the ID of the first section that has the given type.
233 lldb::user_id_t
234 GetSectionIndexByType(unsigned type);
235
236 /// Returns the section header with the given id or NULL.
237 const elf::ELFSectionHeader *
238 GetSectionHeaderByIndex(lldb::user_id_t id);
239
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000240 /// @name ELF header dump routines
241 //@{
242 static void
243 DumpELFHeader(lldb_private::Stream *s, const elf::ELFHeader& header);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000244
245 static void
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000246 DumpELFHeader_e_ident_EI_DATA(lldb_private::Stream *s,
247 unsigned char ei_data);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000248
249 static void
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000250 DumpELFHeader_e_type(lldb_private::Stream *s, elf::elf_half e_type);
251 //@}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000252
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000253 /// @name ELF program header dump routines
254 //@{
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000255 void
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000256 DumpELFProgramHeaders(lldb_private::Stream *s);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000257
258 static void
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000259 DumpELFProgramHeader(lldb_private::Stream *s,
260 const elf::ELFProgramHeader &ph);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000261
262 static void
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000263 DumpELFProgramHeader_p_type(lldb_private::Stream *s, elf::elf_word p_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000264
265 static void
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000266 DumpELFProgramHeader_p_flags(lldb_private::Stream *s,
267 elf::elf_word p_flags);
268 //@}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000269
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000270 /// @name ELF section header dump routines
271 //@{
272 void
273 DumpELFSectionHeaders(lldb_private::Stream *s);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000274
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000275 static void
276 DumpELFSectionHeader(lldb_private::Stream *s,
277 const elf::ELFSectionHeader& sh);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000278
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000279 static void
280 DumpELFSectionHeader_sh_type(lldb_private::Stream *s,
281 elf::elf_word sh_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000282
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000283 static void
284 DumpELFSectionHeader_sh_flags(lldb_private::Stream *s,
Greg Claytonc7bece562013-01-25 18:06:21 +0000285 elf::elf_xword sh_flags);
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000286 //@}
287
288 /// ELF dependent module dump routine.
289 void
290 DumpDependentModules(lldb_private::Stream *s);
291
Stephen Wilson499b40e2011-03-30 16:07:05 +0000292 const elf::ELFDynamic *
293 FindDynamicSymbol(unsigned tag);
294
295 lldb_private::Section *
296 PLTSection();
297
298 unsigned
299 PLTRelocationType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000300};
301
302#endif // #ifndef liblldb_ObjectFileELF_h_