blob: e186fc310ace465cd8102ba88d5ad520af79e3ef [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
Stephen Wilsonf325ba92010-07-13 23:07:23 +000023//------------------------------------------------------------------------------
24/// @class ObjectFileELF
25/// @brief Generic ELF object file reader.
26///
27/// This class provides a generic ELF (32/64 bit) reader plugin implementing the
28/// ObjectFile protocol.
Chris Lattner30fdc8d2010-06-08 16:52:24 +000029class ObjectFileELF :
30 public lldb_private::ObjectFile
31{
32public:
33 //------------------------------------------------------------------
34 // Static Functions
35 //------------------------------------------------------------------
36 static void
37 Initialize();
38
39 static void
40 Terminate();
41
Greg Clayton57abc5d2013-05-10 21:47:16 +000042 static lldb_private::ConstString
Chris Lattner30fdc8d2010-06-08 16:52:24 +000043 GetPluginNameStatic();
44
45 static const char *
46 GetPluginDescriptionStatic();
47
48 static lldb_private::ObjectFile *
Greg Claytone72dfb32012-02-24 01:59:29 +000049 CreateInstance(const lldb::ModuleSP &module_sp,
Greg Clayton5ce9c562013-02-06 17:22:03 +000050 lldb::DataBufferSP& data_sp,
51 lldb::offset_t data_offset,
Chris Lattner30fdc8d2010-06-08 16:52:24 +000052 const lldb_private::FileSpec* file,
Greg Clayton5ce9c562013-02-06 17:22:03 +000053 lldb::offset_t file_offset,
54 lldb::offset_t length);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000055
Greg Claytonc9660542012-02-05 02:38:54 +000056 static lldb_private::ObjectFile *
Greg Claytone72dfb32012-02-24 01:59:29 +000057 CreateMemoryInstance (const lldb::ModuleSP &module_sp,
Greg Claytonc9660542012-02-05 02:38:54 +000058 lldb::DataBufferSP& data_sp,
59 const lldb::ProcessSP &process_sp,
60 lldb::addr_t header_addr);
61
Greg Claytonf4d6de62013-04-24 22:29:28 +000062 static size_t
63 GetModuleSpecifications (const lldb_private::FileSpec& file,
64 lldb::DataBufferSP& data_sp,
65 lldb::offset_t data_offset,
66 lldb::offset_t file_offset,
67 lldb::offset_t length,
68 lldb_private::ModuleSpecList &specs);
Michael Sartain9f0013d2013-05-17 00:20:21 +000069
70 static bool
71 MagicBytesMatch (lldb::DataBufferSP& data_sp,
72 lldb::addr_t offset,
73 lldb::addr_t length);
74
Chris Lattner30fdc8d2010-06-08 16:52:24 +000075 //------------------------------------------------------------------
76 // PluginInterface protocol
77 //------------------------------------------------------------------
Greg Clayton57abc5d2013-05-10 21:47:16 +000078 virtual lldb_private::ConstString
Chris Lattner30fdc8d2010-06-08 16:52:24 +000079 GetPluginName();
80
Chris Lattner30fdc8d2010-06-08 16:52:24 +000081 virtual uint32_t
82 GetPluginVersion();
83
Stephen Wilsonf325ba92010-07-13 23:07:23 +000084 //------------------------------------------------------------------
85 // ObjectFile Protocol.
86 //------------------------------------------------------------------
87 virtual
88 ~ObjectFileELF();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000089
Stephen Wilsonf325ba92010-07-13 23:07:23 +000090 virtual bool
91 ParseHeader();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000092
Stephen Wilsonf325ba92010-07-13 23:07:23 +000093 virtual lldb::ByteOrder
94 GetByteOrder() const;
95
Jim Ingham5aee1622010-08-09 23:31:02 +000096 virtual bool
97 IsExecutable () const;
98
Greg Claytonc7bece562013-01-25 18:06:21 +000099 virtual uint32_t
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000100 GetAddressByteSize() const;
101
102 virtual lldb_private::Symtab *
Greg Clayton3046e662013-07-10 01:23:25 +0000103 GetSymtab();
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000104
Greg Clayton3046e662013-07-10 01:23:25 +0000105 virtual bool
106 IsStripped ();
107
108 virtual void
109 CreateSections (lldb_private::SectionList &unified_section_list);
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000110
111 virtual void
112 Dump(lldb_private::Stream *s);
113
114 virtual bool
Greg Clayton514487e2011-02-15 21:59:32 +0000115 GetArchitecture (lldb_private::ArchSpec &arch);
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000116
117 virtual bool
118 GetUUID(lldb_private::UUID* uuid);
119
Michael Sartaina7499c92013-07-01 19:45:50 +0000120 virtual lldb_private::FileSpecList
121 GetDebugSymbolFilePaths();
122
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000123 virtual uint32_t
124 GetDependentModules(lldb_private::FileSpecList& files);
125
Stephen Wilson2ab0a582011-01-15 00:08:44 +0000126 virtual lldb_private::Address
127 GetImageInfoAddress();
Jim Ingham672e6f52011-03-07 23:44:08 +0000128
129 virtual lldb_private::Address
130 GetEntryPointAddress ();
Greg Clayton9e00b6a652011-07-09 00:41:34 +0000131
132 virtual ObjectFile::Type
133 CalculateType();
134
135 virtual ObjectFile::Strata
136 CalculateStrata();
Stephen Wilson2ab0a582011-01-15 00:08:44 +0000137
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000138private:
Greg Claytone72dfb32012-02-24 01:59:29 +0000139 ObjectFileELF(const lldb::ModuleSP &module_sp,
Greg Clayton5ce9c562013-02-06 17:22:03 +0000140 lldb::DataBufferSP& data_sp,
141 lldb::offset_t data_offset,
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000142 const lldb_private::FileSpec* file,
Greg Clayton5ce9c562013-02-06 17:22:03 +0000143 lldb::offset_t offset,
144 lldb::offset_t length);
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000145
146 typedef std::vector<elf::ELFProgramHeader> ProgramHeaderColl;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000147 typedef ProgramHeaderColl::iterator ProgramHeaderCollIter;
148 typedef ProgramHeaderColl::const_iterator ProgramHeaderCollConstIter;
149
Michael Sartaina7499c92013-07-01 19:45:50 +0000150 struct ELFSectionHeaderInfo : public elf::ELFSectionHeader
151 {
152 lldb_private::ConstString section_name;
153 };
154 typedef std::vector<ELFSectionHeaderInfo> SectionHeaderColl;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000155 typedef SectionHeaderColl::iterator SectionHeaderCollIter;
156 typedef SectionHeaderColl::const_iterator SectionHeaderCollConstIter;
157
Stephen Wilson499b40e2011-03-30 16:07:05 +0000158 typedef std::vector<elf::ELFDynamic> DynamicSymbolColl;
159 typedef DynamicSymbolColl::iterator DynamicSymbolCollIter;
160 typedef DynamicSymbolColl::const_iterator DynamicSymbolCollConstIter;
161
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000162 /// Version of this reader common to all plugins based on this class.
163 static const uint32_t m_plugin_version = 1;
164
165 /// ELF file header.
166 elf::ELFHeader m_header;
167
Michael Sartaina7499c92013-07-01 19:45:50 +0000168 /// ELF build ID.
Michael Sartainc836ae72013-05-23 20:57:03 +0000169 lldb_private::UUID m_uuid;
170
Michael Sartaina7499c92013-07-01 19:45:50 +0000171 /// ELF .gnu_debuglink file and crc data if available.
172 std::string m_gnu_debuglink_file;
173 uint32_t m_gnu_debuglink_crc;
174
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000175 /// Collection of program headers.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000176 ProgramHeaderColl m_program_headers;
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000177
178 /// Collection of section headers.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000179 SectionHeaderColl m_section_headers;
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000180
Stephen Wilson499b40e2011-03-30 16:07:05 +0000181 /// Collection of symbols from the dynamic table.
182 DynamicSymbolColl m_dynamic_symbols;
183
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000184 /// List of file specifications corresponding to the modules (shared
185 /// libraries) on which this object file depends.
Greg Clayton7b0992d2013-04-18 22:45:39 +0000186 mutable std::unique_ptr<lldb_private::FileSpecList> m_filespec_ap;
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000187
Jim Ingham672e6f52011-03-07 23:44:08 +0000188 /// Cached value of the entry point for this module.
189 lldb_private::Address m_entry_point_address;
Stephen Wilson499b40e2011-03-30 16:07:05 +0000190
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000191 /// Returns a 1 based index of the given section header.
Greg Claytonc7bece562013-01-25 18:06:21 +0000192 size_t
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000193 SectionIndex(const SectionHeaderCollIter &I);
194
195 /// Returns a 1 based index of the given section header.
Greg Claytonc7bece562013-01-25 18:06:21 +0000196 size_t
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000197 SectionIndex(const SectionHeaderCollConstIter &I) const;
198
199 /// Parses all section headers present in this object file and populates
200 /// m_program_headers. This method will compute the header list only once.
201 /// Returns the number of headers parsed.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000202 size_t
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000203 ParseProgramHeaders();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000204
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000205 /// Parses all section headers present in this object file and populates
206 /// m_section_headers. This method will compute the header list only once.
207 /// Returns the number of headers parsed.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000208 size_t
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000209 ParseSectionHeaders();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000210
Michael Sartaina7499c92013-07-01 19:45:50 +0000211 /// Parses the elf section headers and returns the uuid, debug link name, crc.
212 static size_t
213 GetSectionHeaderInfo(SectionHeaderColl &section_headers,
214 lldb_private::DataExtractor &data,
215 const elf::ELFHeader &header,
216 lldb_private::UUID &uuid,
217 std::string &gnu_debuglink_file,
218 uint32_t &gnu_debuglink_crc);
219
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000220 /// Scans the dynamic section and locates all dependent modules (shared
Greg Clayton710dd5a2011-01-08 20:28:42 +0000221 /// libraries) populating m_filespec_ap. This method will compute the
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000222 /// dependent module list only once. Returns the number of dependent
223 /// modules parsed.
224 size_t
225 ParseDependentModules();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000226
Stephen Wilson499b40e2011-03-30 16:07:05 +0000227 /// Parses the dynamic symbol table and populates m_dynamic_symbols. The
228 /// vector retains the order as found in the object file. Returns the
229 /// number of dynamic symbols parsed.
230 size_t
231 ParseDynamicSymbols();
232
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000233 /// Populates m_symtab_ap will all non-dynamic linker symbols. This method
234 /// will parse the symbols only once. Returns the number of symbols parsed.
Stephen Wilson499b40e2011-03-30 16:07:05 +0000235 unsigned
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000236 ParseSymbolTable(lldb_private::Symtab *symbol_table,
Stephen Wilson499b40e2011-03-30 16:07:05 +0000237 lldb::user_id_t start_id,
Greg Clayton3046e662013-07-10 01:23:25 +0000238 lldb_private::Section *symtab);
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000239
Michael Sartaina7499c92013-07-01 19:45:50 +0000240 /// Helper routine for ParseSymbolTable().
241 unsigned
242 ParseSymbols(lldb_private::Symtab *symbol_table,
243 lldb::user_id_t start_id,
244 lldb_private::SectionList *section_list,
Greg Clayton3046e662013-07-10 01:23:25 +0000245 const size_t num_symbols,
Michael Sartaina7499c92013-07-01 19:45:50 +0000246 const lldb_private::DataExtractor &symtab_data,
247 const lldb_private::DataExtractor &strtab_data);
248
Stephen Wilson499b40e2011-03-30 16:07:05 +0000249 /// Scans the relocation entries and adds a set of artificial symbols to the
250 /// given symbol table for each PLT slot. Returns the number of symbols
251 /// added.
252 unsigned
253 ParseTrampolineSymbols(lldb_private::Symtab *symbol_table,
254 lldb::user_id_t start_id,
Michael Sartaina7499c92013-07-01 19:45:50 +0000255 const ELFSectionHeaderInfo *rela_hdr,
Stephen Wilson499b40e2011-03-30 16:07:05 +0000256 lldb::user_id_t section_id);
257
Stephen Wilson499b40e2011-03-30 16:07:05 +0000258 /// Returns the section header with the given id or NULL.
Michael Sartaina7499c92013-07-01 19:45:50 +0000259 const ELFSectionHeaderInfo *
Stephen Wilson499b40e2011-03-30 16:07:05 +0000260 GetSectionHeaderByIndex(lldb::user_id_t id);
261
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000262 /// @name ELF header dump routines
263 //@{
264 static void
265 DumpELFHeader(lldb_private::Stream *s, const elf::ELFHeader& header);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000266
267 static void
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000268 DumpELFHeader_e_ident_EI_DATA(lldb_private::Stream *s,
269 unsigned char ei_data);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000270
271 static void
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000272 DumpELFHeader_e_type(lldb_private::Stream *s, elf::elf_half e_type);
273 //@}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000274
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000275 /// @name ELF program header dump routines
276 //@{
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000277 void
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000278 DumpELFProgramHeaders(lldb_private::Stream *s);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000279
280 static void
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000281 DumpELFProgramHeader(lldb_private::Stream *s,
282 const elf::ELFProgramHeader &ph);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000283
284 static void
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000285 DumpELFProgramHeader_p_type(lldb_private::Stream *s, elf::elf_word p_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000286
287 static void
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000288 DumpELFProgramHeader_p_flags(lldb_private::Stream *s,
289 elf::elf_word p_flags);
290 //@}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000291
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000292 /// @name ELF section header dump routines
293 //@{
294 void
295 DumpELFSectionHeaders(lldb_private::Stream *s);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000296
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000297 static void
298 DumpELFSectionHeader(lldb_private::Stream *s,
Michael Sartaina7499c92013-07-01 19:45:50 +0000299 const ELFSectionHeaderInfo& sh);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000300
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000301 static void
302 DumpELFSectionHeader_sh_type(lldb_private::Stream *s,
303 elf::elf_word sh_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000304
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000305 static void
306 DumpELFSectionHeader_sh_flags(lldb_private::Stream *s,
Greg Claytonc7bece562013-01-25 18:06:21 +0000307 elf::elf_xword sh_flags);
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000308 //@}
309
310 /// ELF dependent module dump routine.
311 void
312 DumpDependentModules(lldb_private::Stream *s);
313
Stephen Wilson499b40e2011-03-30 16:07:05 +0000314 const elf::ELFDynamic *
315 FindDynamicSymbol(unsigned tag);
316
Stephen Wilson499b40e2011-03-30 16:07:05 +0000317 unsigned
318 PLTRelocationType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000319};
320
321#endif // #ifndef liblldb_ObjectFileELF_h_