blob: 4fb0250108a4da6d45fa29b81223d2efcb91087d [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"
Michael Sartainc836ae72013-05-23 20:57:03 +000019#include "lldb/Core/UUID.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000020
Stephen Wilsonf325ba92010-07-13 23:07:23 +000021#include "ELFHeader.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000022
Ed Mastec113ff82013-12-02 17:49:13 +000023struct ELFNote
24{
25 elf::elf_word n_namesz;
26 elf::elf_word n_descsz;
27 elf::elf_word n_type;
28
29 std::string n_name;
30
31 ELFNote() : n_namesz(0), n_descsz(0), n_type(0)
32 {
33 }
34
35 /// Parse an ELFNote entry from the given DataExtractor starting at position
36 /// \p offset.
37 ///
38 /// @param[in] data
39 /// The DataExtractor to read from.
40 ///
41 /// @param[in,out] offset
42 /// Pointer to an offset in the data. On return the offset will be
43 /// advanced by the number of bytes read.
44 ///
45 /// @return
46 /// True if the ELFRel entry was successfully read and false otherwise.
47 bool
48 Parse(const lldb_private::DataExtractor &data, lldb::offset_t *offset);
49};
50
Stephen Wilsonf325ba92010-07-13 23:07:23 +000051//------------------------------------------------------------------------------
52/// @class ObjectFileELF
53/// @brief Generic ELF object file reader.
54///
55/// This class provides a generic ELF (32/64 bit) reader plugin implementing the
56/// ObjectFile protocol.
Chris Lattner30fdc8d2010-06-08 16:52:24 +000057class ObjectFileELF :
58 public lldb_private::ObjectFile
59{
60public:
61 //------------------------------------------------------------------
62 // Static Functions
63 //------------------------------------------------------------------
64 static void
65 Initialize();
66
67 static void
68 Terminate();
69
Greg Clayton57abc5d2013-05-10 21:47:16 +000070 static lldb_private::ConstString
Chris Lattner30fdc8d2010-06-08 16:52:24 +000071 GetPluginNameStatic();
72
73 static const char *
74 GetPluginDescriptionStatic();
75
76 static lldb_private::ObjectFile *
Greg Claytone72dfb32012-02-24 01:59:29 +000077 CreateInstance(const lldb::ModuleSP &module_sp,
Greg Clayton5ce9c562013-02-06 17:22:03 +000078 lldb::DataBufferSP& data_sp,
79 lldb::offset_t data_offset,
Chris Lattner30fdc8d2010-06-08 16:52:24 +000080 const lldb_private::FileSpec* file,
Greg Clayton5ce9c562013-02-06 17:22:03 +000081 lldb::offset_t file_offset,
82 lldb::offset_t length);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000083
Greg Claytonc9660542012-02-05 02:38:54 +000084 static lldb_private::ObjectFile *
Greg Claytone72dfb32012-02-24 01:59:29 +000085 CreateMemoryInstance (const lldb::ModuleSP &module_sp,
Greg Claytonc9660542012-02-05 02:38:54 +000086 lldb::DataBufferSP& data_sp,
87 const lldb::ProcessSP &process_sp,
88 lldb::addr_t header_addr);
89
Greg Claytonf4d6de62013-04-24 22:29:28 +000090 static size_t
91 GetModuleSpecifications (const lldb_private::FileSpec& file,
92 lldb::DataBufferSP& data_sp,
93 lldb::offset_t data_offset,
94 lldb::offset_t file_offset,
95 lldb::offset_t length,
96 lldb_private::ModuleSpecList &specs);
Michael Sartain9f0013d2013-05-17 00:20:21 +000097
98 static bool
99 MagicBytesMatch (lldb::DataBufferSP& data_sp,
100 lldb::addr_t offset,
101 lldb::addr_t length);
102
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000103 //------------------------------------------------------------------
104 // PluginInterface protocol
105 //------------------------------------------------------------------
Greg Clayton57abc5d2013-05-10 21:47:16 +0000106 virtual lldb_private::ConstString
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000107 GetPluginName();
108
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000109 virtual uint32_t
110 GetPluginVersion();
111
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000112 //------------------------------------------------------------------
113 // ObjectFile Protocol.
114 //------------------------------------------------------------------
115 virtual
116 ~ObjectFileELF();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000117
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000118 virtual bool
119 ParseHeader();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000120
Steve Pucci9e02dac2014-02-06 19:02:19 +0000121 virtual bool
122 SetLoadAddress(lldb_private::Target &target, lldb::addr_t base_addr);
123
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000124 virtual lldb::ByteOrder
125 GetByteOrder() const;
126
Jim Ingham5aee1622010-08-09 23:31:02 +0000127 virtual bool
128 IsExecutable () const;
129
Greg Claytonc7bece562013-01-25 18:06:21 +0000130 virtual uint32_t
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000131 GetAddressByteSize() const;
132
133 virtual lldb_private::Symtab *
Greg Clayton3046e662013-07-10 01:23:25 +0000134 GetSymtab();
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000135
Ashok Thirumurthi35729bb2013-09-24 15:34:13 +0000136 virtual lldb_private::Symbol *
137 ResolveSymbolForAddress(const lldb_private::Address& so_addr, bool verify_unique);
138
Greg Clayton3046e662013-07-10 01:23:25 +0000139 virtual bool
140 IsStripped ();
141
142 virtual void
143 CreateSections (lldb_private::SectionList &unified_section_list);
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000144
145 virtual void
146 Dump(lldb_private::Stream *s);
147
148 virtual bool
Greg Clayton514487e2011-02-15 21:59:32 +0000149 GetArchitecture (lldb_private::ArchSpec &arch);
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000150
151 virtual bool
152 GetUUID(lldb_private::UUID* uuid);
153
Michael Sartaina7499c92013-07-01 19:45:50 +0000154 virtual lldb_private::FileSpecList
155 GetDebugSymbolFilePaths();
156
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000157 virtual uint32_t
158 GetDependentModules(lldb_private::FileSpecList& files);
159
Stephen Wilson2ab0a582011-01-15 00:08:44 +0000160 virtual lldb_private::Address
Ed Maste54803652013-10-11 17:39:07 +0000161 GetImageInfoAddress(lldb_private::Target *target);
Jim Ingham672e6f52011-03-07 23:44:08 +0000162
163 virtual lldb_private::Address
164 GetEntryPointAddress ();
Greg Clayton9e00b6a652011-07-09 00:41:34 +0000165
166 virtual ObjectFile::Type
167 CalculateType();
168
169 virtual ObjectFile::Strata
170 CalculateStrata();
Stephen Wilson2ab0a582011-01-15 00:08:44 +0000171
Ashok Thirumurthi4822d922013-07-11 20:39:00 +0000172 // Returns number of program headers found in the ELF file.
173 size_t
174 GetProgramHeaderCount();
175
176 // Returns the program header with the given index.
177 const elf::ELFProgramHeader *
178 GetProgramHeaderByIndex(lldb::user_id_t id);
179
180 // Returns segment data for the given index.
181 lldb_private::DataExtractor
182 GetSegmentDataByIndex(lldb::user_id_t id);
183
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000184private:
Greg Claytone72dfb32012-02-24 01:59:29 +0000185 ObjectFileELF(const lldb::ModuleSP &module_sp,
Greg Clayton5ce9c562013-02-06 17:22:03 +0000186 lldb::DataBufferSP& data_sp,
187 lldb::offset_t data_offset,
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000188 const lldb_private::FileSpec* file,
Greg Clayton5ce9c562013-02-06 17:22:03 +0000189 lldb::offset_t offset,
190 lldb::offset_t length);
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000191
192 typedef std::vector<elf::ELFProgramHeader> ProgramHeaderColl;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000193 typedef ProgramHeaderColl::iterator ProgramHeaderCollIter;
194 typedef ProgramHeaderColl::const_iterator ProgramHeaderCollConstIter;
195
Michael Sartaina7499c92013-07-01 19:45:50 +0000196 struct ELFSectionHeaderInfo : public elf::ELFSectionHeader
197 {
198 lldb_private::ConstString section_name;
199 };
200 typedef std::vector<ELFSectionHeaderInfo> SectionHeaderColl;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000201 typedef SectionHeaderColl::iterator SectionHeaderCollIter;
202 typedef SectionHeaderColl::const_iterator SectionHeaderCollConstIter;
203
Stephen Wilson499b40e2011-03-30 16:07:05 +0000204 typedef std::vector<elf::ELFDynamic> DynamicSymbolColl;
205 typedef DynamicSymbolColl::iterator DynamicSymbolCollIter;
206 typedef DynamicSymbolColl::const_iterator DynamicSymbolCollConstIter;
207
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000208 /// Version of this reader common to all plugins based on this class.
209 static const uint32_t m_plugin_version = 1;
210
211 /// ELF file header.
212 elf::ELFHeader m_header;
213
Michael Sartaina7499c92013-07-01 19:45:50 +0000214 /// ELF build ID.
Michael Sartainc836ae72013-05-23 20:57:03 +0000215 lldb_private::UUID m_uuid;
216
Michael Sartaina7499c92013-07-01 19:45:50 +0000217 /// ELF .gnu_debuglink file and crc data if available.
218 std::string m_gnu_debuglink_file;
219 uint32_t m_gnu_debuglink_crc;
220
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000221 /// Collection of program headers.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000222 ProgramHeaderColl m_program_headers;
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000223
224 /// Collection of section headers.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000225 SectionHeaderColl m_section_headers;
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000226
Stephen Wilson499b40e2011-03-30 16:07:05 +0000227 /// Collection of symbols from the dynamic table.
228 DynamicSymbolColl m_dynamic_symbols;
229
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000230 /// List of file specifications corresponding to the modules (shared
231 /// libraries) on which this object file depends.
Greg Clayton7b0992d2013-04-18 22:45:39 +0000232 mutable std::unique_ptr<lldb_private::FileSpecList> m_filespec_ap;
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000233
Jim Ingham672e6f52011-03-07 23:44:08 +0000234 /// Cached value of the entry point for this module.
235 lldb_private::Address m_entry_point_address;
Stephen Wilson499b40e2011-03-30 16:07:05 +0000236
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000237 /// Returns a 1 based index of the given section header.
Greg Claytonc7bece562013-01-25 18:06:21 +0000238 size_t
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000239 SectionIndex(const SectionHeaderCollIter &I);
240
241 /// Returns a 1 based index of the given section header.
Greg Claytonc7bece562013-01-25 18:06:21 +0000242 size_t
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000243 SectionIndex(const SectionHeaderCollConstIter &I) const;
244
245 /// Parses all section headers present in this object file and populates
246 /// m_program_headers. This method will compute the header list only once.
247 /// Returns the number of headers parsed.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000248 size_t
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000249 ParseProgramHeaders();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000250
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000251 /// Parses all section headers present in this object file and populates
252 /// m_section_headers. This method will compute the header list only once.
253 /// Returns the number of headers parsed.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000254 size_t
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000255 ParseSectionHeaders();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000256
Michael Sartaina7499c92013-07-01 19:45:50 +0000257 /// Parses the elf section headers and returns the uuid, debug link name, crc.
258 static size_t
259 GetSectionHeaderInfo(SectionHeaderColl &section_headers,
260 lldb_private::DataExtractor &data,
261 const elf::ELFHeader &header,
262 lldb_private::UUID &uuid,
263 std::string &gnu_debuglink_file,
264 uint32_t &gnu_debuglink_crc);
265
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000266 /// Scans the dynamic section and locates all dependent modules (shared
Greg Clayton710dd5a2011-01-08 20:28:42 +0000267 /// libraries) populating m_filespec_ap. This method will compute the
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000268 /// dependent module list only once. Returns the number of dependent
269 /// modules parsed.
270 size_t
271 ParseDependentModules();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000272
Stephen Wilson499b40e2011-03-30 16:07:05 +0000273 /// Parses the dynamic symbol table and populates m_dynamic_symbols. The
274 /// vector retains the order as found in the object file. Returns the
275 /// number of dynamic symbols parsed.
276 size_t
277 ParseDynamicSymbols();
278
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000279 /// Populates m_symtab_ap will all non-dynamic linker symbols. This method
280 /// will parse the symbols only once. Returns the number of symbols parsed.
Stephen Wilson499b40e2011-03-30 16:07:05 +0000281 unsigned
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000282 ParseSymbolTable(lldb_private::Symtab *symbol_table,
Stephen Wilson499b40e2011-03-30 16:07:05 +0000283 lldb::user_id_t start_id,
Greg Clayton3046e662013-07-10 01:23:25 +0000284 lldb_private::Section *symtab);
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000285
Michael Sartaina7499c92013-07-01 19:45:50 +0000286 /// Helper routine for ParseSymbolTable().
287 unsigned
288 ParseSymbols(lldb_private::Symtab *symbol_table,
289 lldb::user_id_t start_id,
290 lldb_private::SectionList *section_list,
Greg Clayton3046e662013-07-10 01:23:25 +0000291 const size_t num_symbols,
Michael Sartaina7499c92013-07-01 19:45:50 +0000292 const lldb_private::DataExtractor &symtab_data,
293 const lldb_private::DataExtractor &strtab_data);
294
Stephen Wilson499b40e2011-03-30 16:07:05 +0000295 /// Scans the relocation entries and adds a set of artificial symbols to the
296 /// given symbol table for each PLT slot. Returns the number of symbols
297 /// added.
298 unsigned
299 ParseTrampolineSymbols(lldb_private::Symtab *symbol_table,
300 lldb::user_id_t start_id,
Michael Sartaina7499c92013-07-01 19:45:50 +0000301 const ELFSectionHeaderInfo *rela_hdr,
Stephen Wilson499b40e2011-03-30 16:07:05 +0000302 lldb::user_id_t section_id);
303
Stephen Wilson499b40e2011-03-30 16:07:05 +0000304 /// Returns the section header with the given id or NULL.
Michael Sartaina7499c92013-07-01 19:45:50 +0000305 const ELFSectionHeaderInfo *
Stephen Wilson499b40e2011-03-30 16:07:05 +0000306 GetSectionHeaderByIndex(lldb::user_id_t id);
307
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000308 /// @name ELF header dump routines
309 //@{
310 static void
311 DumpELFHeader(lldb_private::Stream *s, const elf::ELFHeader& header);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000312
313 static void
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000314 DumpELFHeader_e_ident_EI_DATA(lldb_private::Stream *s,
315 unsigned char ei_data);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000316
317 static void
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000318 DumpELFHeader_e_type(lldb_private::Stream *s, elf::elf_half e_type);
319 //@}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000320
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000321 /// @name ELF program header dump routines
322 //@{
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000323 void
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000324 DumpELFProgramHeaders(lldb_private::Stream *s);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000325
326 static void
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000327 DumpELFProgramHeader(lldb_private::Stream *s,
328 const elf::ELFProgramHeader &ph);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000329
330 static void
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000331 DumpELFProgramHeader_p_type(lldb_private::Stream *s, elf::elf_word p_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000332
333 static void
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000334 DumpELFProgramHeader_p_flags(lldb_private::Stream *s,
335 elf::elf_word p_flags);
336 //@}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000337
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000338 /// @name ELF section header dump routines
339 //@{
340 void
341 DumpELFSectionHeaders(lldb_private::Stream *s);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000342
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000343 static void
344 DumpELFSectionHeader(lldb_private::Stream *s,
Michael Sartaina7499c92013-07-01 19:45:50 +0000345 const ELFSectionHeaderInfo& sh);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000346
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000347 static void
348 DumpELFSectionHeader_sh_type(lldb_private::Stream *s,
349 elf::elf_word sh_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000350
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000351 static void
352 DumpELFSectionHeader_sh_flags(lldb_private::Stream *s,
Greg Claytonc7bece562013-01-25 18:06:21 +0000353 elf::elf_xword sh_flags);
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000354 //@}
355
356 /// ELF dependent module dump routine.
357 void
358 DumpDependentModules(lldb_private::Stream *s);
359
Stephen Wilson499b40e2011-03-30 16:07:05 +0000360 const elf::ELFDynamic *
361 FindDynamicSymbol(unsigned tag);
362
Stephen Wilson499b40e2011-03-30 16:07:05 +0000363 unsigned
364 PLTRelocationType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000365};
366
367#endif // #ifndef liblldb_ObjectFileELF_h_