blob: f20b0bdb975c095b261d5a7dd67ef65fdb4ba2ff [file] [log] [blame]
Stephen Wilsonddd29622010-07-13 23:07:23 +00001//===-- ObjectFileELF.h --------------------------------------- -*- C++ -*-===//
Chris Lattner24943d22010-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 Clayton5f54ac32011-02-08 05:05:52 +000017#include "lldb/Host/FileSpec.h"
Chris Lattner24943d22010-06-08 16:52:24 +000018#include "lldb/Symbol/ObjectFile.h"
19
Stephen Wilsonddd29622010-07-13 23:07:23 +000020#include "ELFHeader.h"
Chris Lattner24943d22010-06-08 16:52:24 +000021
Stephen Wilsonddd29622010-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 Lattner24943d22010-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 *
Stephen Wilsonddd29622010-07-13 23:07:23 +000048 CreateInstance(lldb_private::Module* module,
Chris Lattner24943d22010-06-08 16:52:24 +000049 lldb::DataBufferSP& dataSP,
50 const lldb_private::FileSpec* file,
51 lldb::addr_t offset,
52 lldb::addr_t length);
53
Greg Claytonb5a8f142012-02-05 02:38:54 +000054 static lldb_private::ObjectFile *
55 CreateMemoryInstance (lldb_private::Module* module,
56 lldb::DataBufferSP& data_sp,
57 const lldb::ProcessSP &process_sp,
58 lldb::addr_t header_addr);
59
Chris Lattner24943d22010-06-08 16:52:24 +000060 //------------------------------------------------------------------
61 // PluginInterface protocol
62 //------------------------------------------------------------------
63 virtual const char *
64 GetPluginName();
65
66 virtual const char *
67 GetShortPluginName();
68
69 virtual uint32_t
70 GetPluginVersion();
71
Stephen Wilsonddd29622010-07-13 23:07:23 +000072 //------------------------------------------------------------------
73 // ObjectFile Protocol.
74 //------------------------------------------------------------------
75 virtual
76 ~ObjectFileELF();
Chris Lattner24943d22010-06-08 16:52:24 +000077
Stephen Wilsonddd29622010-07-13 23:07:23 +000078 virtual bool
79 ParseHeader();
Chris Lattner24943d22010-06-08 16:52:24 +000080
Stephen Wilsonddd29622010-07-13 23:07:23 +000081 virtual lldb::ByteOrder
82 GetByteOrder() const;
83
Jim Ingham7508e732010-08-09 23:31:02 +000084 virtual bool
85 IsExecutable () const;
86
Stephen Wilsonddd29622010-07-13 23:07:23 +000087 virtual size_t
88 GetAddressByteSize() const;
89
90 virtual lldb_private::Symtab *
91 GetSymtab();
92
93 virtual lldb_private::SectionList *
94 GetSectionList();
95
96 virtual void
97 Dump(lldb_private::Stream *s);
98
99 virtual bool
Greg Clayton395fc332011-02-15 21:59:32 +0000100 GetArchitecture (lldb_private::ArchSpec &arch);
Stephen Wilsonddd29622010-07-13 23:07:23 +0000101
102 virtual bool
103 GetUUID(lldb_private::UUID* uuid);
104
105 virtual uint32_t
106 GetDependentModules(lldb_private::FileSpecList& files);
107
Stephen Wilsonf2ad3252011-01-15 00:08:44 +0000108 virtual lldb_private::Address
109 GetImageInfoAddress();
Jim Ingham28775942011-03-07 23:44:08 +0000110
111 virtual lldb_private::Address
112 GetEntryPointAddress ();
Greg Claytonca319972011-07-09 00:41:34 +0000113
114 virtual ObjectFile::Type
115 CalculateType();
116
117 virtual ObjectFile::Strata
118 CalculateStrata();
Stephen Wilsonf2ad3252011-01-15 00:08:44 +0000119
Stephen Wilsonddd29622010-07-13 23:07:23 +0000120private:
121 ObjectFileELF(lldb_private::Module* module,
122 lldb::DataBufferSP& dataSP,
123 const lldb_private::FileSpec* file,
124 lldb::addr_t offset,
125 lldb::addr_t length);
126
127 typedef std::vector<elf::ELFProgramHeader> ProgramHeaderColl;
Chris Lattner24943d22010-06-08 16:52:24 +0000128 typedef ProgramHeaderColl::iterator ProgramHeaderCollIter;
129 typedef ProgramHeaderColl::const_iterator ProgramHeaderCollConstIter;
130
Stephen Wilsonddd29622010-07-13 23:07:23 +0000131 typedef std::vector<elf::ELFSectionHeader> SectionHeaderColl;
Chris Lattner24943d22010-06-08 16:52:24 +0000132 typedef SectionHeaderColl::iterator SectionHeaderCollIter;
133 typedef SectionHeaderColl::const_iterator SectionHeaderCollConstIter;
134
Stephen Wilsonce477322011-03-30 16:07:05 +0000135 typedef std::vector<elf::ELFDynamic> DynamicSymbolColl;
136 typedef DynamicSymbolColl::iterator DynamicSymbolCollIter;
137 typedef DynamicSymbolColl::const_iterator DynamicSymbolCollConstIter;
138
Stephen Wilsonddd29622010-07-13 23:07:23 +0000139 /// Version of this reader common to all plugins based on this class.
140 static const uint32_t m_plugin_version = 1;
141
142 /// ELF file header.
143 elf::ELFHeader m_header;
144
145 /// Collection of program headers.
Chris Lattner24943d22010-06-08 16:52:24 +0000146 ProgramHeaderColl m_program_headers;
Stephen Wilsonddd29622010-07-13 23:07:23 +0000147
148 /// Collection of section headers.
Chris Lattner24943d22010-06-08 16:52:24 +0000149 SectionHeaderColl m_section_headers;
Stephen Wilsonddd29622010-07-13 23:07:23 +0000150
Stephen Wilsonce477322011-03-30 16:07:05 +0000151 /// Collection of symbols from the dynamic table.
152 DynamicSymbolColl m_dynamic_symbols;
153
Stephen Wilsonddd29622010-07-13 23:07:23 +0000154 /// List of sections present in this ELF object file.
Chris Lattner24943d22010-06-08 16:52:24 +0000155 mutable std::auto_ptr<lldb_private::SectionList> m_sections_ap;
Stephen Wilsonddd29622010-07-13 23:07:23 +0000156
157 /// Table of all non-dynamic symbols present in this object file.
Chris Lattner24943d22010-06-08 16:52:24 +0000158 mutable std::auto_ptr<lldb_private::Symtab> m_symtab_ap;
Stephen Wilsonddd29622010-07-13 23:07:23 +0000159
160 /// List of file specifications corresponding to the modules (shared
161 /// libraries) on which this object file depends.
162 mutable std::auto_ptr<lldb_private::FileSpecList> m_filespec_ap;
163
164 /// Data extractor holding the string table used to resolve section names.
Chris Lattner24943d22010-06-08 16:52:24 +0000165 lldb_private::DataExtractor m_shstr_data;
166
Jim Ingham28775942011-03-07 23:44:08 +0000167 /// Cached value of the entry point for this module.
168 lldb_private::Address m_entry_point_address;
Stephen Wilsonce477322011-03-30 16:07:05 +0000169
Stephen Wilsonddd29622010-07-13 23:07:23 +0000170 /// Returns a 1 based index of the given section header.
171 unsigned
172 SectionIndex(const SectionHeaderCollIter &I);
173
174 /// Returns a 1 based index of the given section header.
175 unsigned
176 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 Lattner24943d22010-06-08 16:52:24 +0000181 size_t
Stephen Wilsonddd29622010-07-13 23:07:23 +0000182 ParseProgramHeaders();
Chris Lattner24943d22010-06-08 16:52:24 +0000183
Stephen Wilsonddd29622010-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 Lattner24943d22010-06-08 16:52:24 +0000187 size_t
Stephen Wilsonddd29622010-07-13 23:07:23 +0000188 ParseSectionHeaders();
Chris Lattner24943d22010-06-08 16:52:24 +0000189
Stephen Wilsonddd29622010-07-13 23:07:23 +0000190 /// Scans the dynamic section and locates all dependent modules (shared
Greg Clayton5d187e52011-01-08 20:28:42 +0000191 /// libraries) populating m_filespec_ap. This method will compute the
Stephen Wilsonddd29622010-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 Lattner24943d22010-06-08 16:52:24 +0000196
Stephen Wilsonce477322011-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 Wilsonddd29622010-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 Wilsonce477322011-03-30 16:07:05 +0000205 unsigned
Stephen Wilsonddd29622010-07-13 23:07:23 +0000206 ParseSymbolTable(lldb_private::Symtab *symbol_table,
Stephen Wilsonce477322011-03-30 16:07:05 +0000207 lldb::user_id_t start_id,
208 const elf::ELFSectionHeader *symtab_section,
Stephen Wilsonddd29622010-07-13 23:07:23 +0000209 lldb::user_id_t symtab_id);
210
Stephen Wilsonce477322011-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 Wilsonddd29622010-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 Wilsonce477322011-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 Wilsonddd29622010-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 Lattner24943d22010-06-08 16:52:24 +0000244
245 static void
Stephen Wilsonddd29622010-07-13 23:07:23 +0000246 DumpELFHeader_e_ident_EI_DATA(lldb_private::Stream *s,
247 unsigned char ei_data);
Chris Lattner24943d22010-06-08 16:52:24 +0000248
249 static void
Stephen Wilsonddd29622010-07-13 23:07:23 +0000250 DumpELFHeader_e_type(lldb_private::Stream *s, elf::elf_half e_type);
251 //@}
Chris Lattner24943d22010-06-08 16:52:24 +0000252
Stephen Wilsonddd29622010-07-13 23:07:23 +0000253 /// @name ELF program header dump routines
254 //@{
Chris Lattner24943d22010-06-08 16:52:24 +0000255 void
Stephen Wilsonddd29622010-07-13 23:07:23 +0000256 DumpELFProgramHeaders(lldb_private::Stream *s);
Chris Lattner24943d22010-06-08 16:52:24 +0000257
258 static void
Stephen Wilsonddd29622010-07-13 23:07:23 +0000259 DumpELFProgramHeader(lldb_private::Stream *s,
260 const elf::ELFProgramHeader &ph);
Chris Lattner24943d22010-06-08 16:52:24 +0000261
262 static void
Stephen Wilsonddd29622010-07-13 23:07:23 +0000263 DumpELFProgramHeader_p_type(lldb_private::Stream *s, elf::elf_word p_type);
Chris Lattner24943d22010-06-08 16:52:24 +0000264
265 static void
Stephen Wilsonddd29622010-07-13 23:07:23 +0000266 DumpELFProgramHeader_p_flags(lldb_private::Stream *s,
267 elf::elf_word p_flags);
268 //@}
Chris Lattner24943d22010-06-08 16:52:24 +0000269
Stephen Wilsonddd29622010-07-13 23:07:23 +0000270 /// @name ELF section header dump routines
271 //@{
272 void
273 DumpELFSectionHeaders(lldb_private::Stream *s);
Chris Lattner24943d22010-06-08 16:52:24 +0000274
Stephen Wilsonddd29622010-07-13 23:07:23 +0000275 static void
276 DumpELFSectionHeader(lldb_private::Stream *s,
277 const elf::ELFSectionHeader& sh);
Chris Lattner24943d22010-06-08 16:52:24 +0000278
Stephen Wilsonddd29622010-07-13 23:07:23 +0000279 static void
280 DumpELFSectionHeader_sh_type(lldb_private::Stream *s,
281 elf::elf_word sh_type);
Chris Lattner24943d22010-06-08 16:52:24 +0000282
Stephen Wilsonddd29622010-07-13 23:07:23 +0000283 static void
284 DumpELFSectionHeader_sh_flags(lldb_private::Stream *s,
285 elf::elf_word sh_flags);
286 //@}
287
288 /// ELF dependent module dump routine.
289 void
290 DumpDependentModules(lldb_private::Stream *s);
291
Stephen Wilsonce477322011-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 Lattner24943d22010-06-08 16:52:24 +0000300};
301
302#endif // #ifndef liblldb_ObjectFileELF_h_