blob: 78d2b5d6be14992912b22eeeb9b8cf0d599630ae [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
Chris Lattner24943d22010-06-08 16:52:24 +000054 //------------------------------------------------------------------
55 // PluginInterface protocol
56 //------------------------------------------------------------------
57 virtual const char *
58 GetPluginName();
59
60 virtual const char *
61 GetShortPluginName();
62
63 virtual uint32_t
64 GetPluginVersion();
65
Stephen Wilsonddd29622010-07-13 23:07:23 +000066 //------------------------------------------------------------------
67 // ObjectFile Protocol.
68 //------------------------------------------------------------------
69 virtual
70 ~ObjectFileELF();
Chris Lattner24943d22010-06-08 16:52:24 +000071
Stephen Wilsonddd29622010-07-13 23:07:23 +000072 virtual bool
73 ParseHeader();
Chris Lattner24943d22010-06-08 16:52:24 +000074
Stephen Wilsonddd29622010-07-13 23:07:23 +000075 virtual lldb::ByteOrder
76 GetByteOrder() const;
77
Jim Ingham7508e732010-08-09 23:31:02 +000078 virtual bool
79 IsExecutable () const;
80
Stephen Wilsonddd29622010-07-13 23:07:23 +000081 virtual size_t
82 GetAddressByteSize() const;
83
84 virtual lldb_private::Symtab *
85 GetSymtab();
86
87 virtual lldb_private::SectionList *
88 GetSectionList();
89
90 virtual void
91 Dump(lldb_private::Stream *s);
92
93 virtual bool
Greg Clayton395fc332011-02-15 21:59:32 +000094 GetArchitecture (lldb_private::ArchSpec &arch);
Stephen Wilsonddd29622010-07-13 23:07:23 +000095
96 virtual bool
97 GetUUID(lldb_private::UUID* uuid);
98
99 virtual uint32_t
100 GetDependentModules(lldb_private::FileSpecList& files);
101
Stephen Wilsonf2ad3252011-01-15 00:08:44 +0000102 virtual lldb_private::Address
103 GetImageInfoAddress();
Jim Ingham28775942011-03-07 23:44:08 +0000104
105 virtual lldb_private::Address
106 GetEntryPointAddress ();
Greg Claytonca319972011-07-09 00:41:34 +0000107
108 virtual ObjectFile::Type
109 CalculateType();
110
111 virtual ObjectFile::Strata
112 CalculateStrata();
Stephen Wilsonf2ad3252011-01-15 00:08:44 +0000113
Stephen Wilsonddd29622010-07-13 23:07:23 +0000114private:
115 ObjectFileELF(lldb_private::Module* module,
116 lldb::DataBufferSP& dataSP,
117 const lldb_private::FileSpec* file,
118 lldb::addr_t offset,
119 lldb::addr_t length);
120
121 typedef std::vector<elf::ELFProgramHeader> ProgramHeaderColl;
Chris Lattner24943d22010-06-08 16:52:24 +0000122 typedef ProgramHeaderColl::iterator ProgramHeaderCollIter;
123 typedef ProgramHeaderColl::const_iterator ProgramHeaderCollConstIter;
124
Stephen Wilsonddd29622010-07-13 23:07:23 +0000125 typedef std::vector<elf::ELFSectionHeader> SectionHeaderColl;
Chris Lattner24943d22010-06-08 16:52:24 +0000126 typedef SectionHeaderColl::iterator SectionHeaderCollIter;
127 typedef SectionHeaderColl::const_iterator SectionHeaderCollConstIter;
128
Stephen Wilsonce477322011-03-30 16:07:05 +0000129 typedef std::vector<elf::ELFDynamic> DynamicSymbolColl;
130 typedef DynamicSymbolColl::iterator DynamicSymbolCollIter;
131 typedef DynamicSymbolColl::const_iterator DynamicSymbolCollConstIter;
132
Stephen Wilsonddd29622010-07-13 23:07:23 +0000133 /// Version of this reader common to all plugins based on this class.
134 static const uint32_t m_plugin_version = 1;
135
136 /// ELF file header.
137 elf::ELFHeader m_header;
138
139 /// Collection of program headers.
Chris Lattner24943d22010-06-08 16:52:24 +0000140 ProgramHeaderColl m_program_headers;
Stephen Wilsonddd29622010-07-13 23:07:23 +0000141
142 /// Collection of section headers.
Chris Lattner24943d22010-06-08 16:52:24 +0000143 SectionHeaderColl m_section_headers;
Stephen Wilsonddd29622010-07-13 23:07:23 +0000144
Stephen Wilsonce477322011-03-30 16:07:05 +0000145 /// Collection of symbols from the dynamic table.
146 DynamicSymbolColl m_dynamic_symbols;
147
Stephen Wilsonddd29622010-07-13 23:07:23 +0000148 /// List of sections present in this ELF object file.
Chris Lattner24943d22010-06-08 16:52:24 +0000149 mutable std::auto_ptr<lldb_private::SectionList> m_sections_ap;
Stephen Wilsonddd29622010-07-13 23:07:23 +0000150
151 /// Table of all non-dynamic symbols present in this object file.
Chris Lattner24943d22010-06-08 16:52:24 +0000152 mutable std::auto_ptr<lldb_private::Symtab> m_symtab_ap;
Stephen Wilsonddd29622010-07-13 23:07:23 +0000153
154 /// List of file specifications corresponding to the modules (shared
155 /// libraries) on which this object file depends.
156 mutable std::auto_ptr<lldb_private::FileSpecList> m_filespec_ap;
157
158 /// Data extractor holding the string table used to resolve section names.
Chris Lattner24943d22010-06-08 16:52:24 +0000159 lldb_private::DataExtractor m_shstr_data;
160
Jim Ingham28775942011-03-07 23:44:08 +0000161 /// Cached value of the entry point for this module.
162 lldb_private::Address m_entry_point_address;
Stephen Wilsonce477322011-03-30 16:07:05 +0000163
Stephen Wilsonddd29622010-07-13 23:07:23 +0000164 /// Returns a 1 based index of the given section header.
165 unsigned
166 SectionIndex(const SectionHeaderCollIter &I);
167
168 /// Returns a 1 based index of the given section header.
169 unsigned
170 SectionIndex(const SectionHeaderCollConstIter &I) const;
171
172 /// Parses all section headers present in this object file and populates
173 /// m_program_headers. This method will compute the header list only once.
174 /// Returns the number of headers parsed.
Chris Lattner24943d22010-06-08 16:52:24 +0000175 size_t
Stephen Wilsonddd29622010-07-13 23:07:23 +0000176 ParseProgramHeaders();
Chris Lattner24943d22010-06-08 16:52:24 +0000177
Stephen Wilsonddd29622010-07-13 23:07:23 +0000178 /// Parses all section headers present in this object file and populates
179 /// m_section_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 ParseSectionHeaders();
Chris Lattner24943d22010-06-08 16:52:24 +0000183
Stephen Wilsonddd29622010-07-13 23:07:23 +0000184 /// Scans the dynamic section and locates all dependent modules (shared
Greg Clayton5d187e52011-01-08 20:28:42 +0000185 /// libraries) populating m_filespec_ap. This method will compute the
Stephen Wilsonddd29622010-07-13 23:07:23 +0000186 /// dependent module list only once. Returns the number of dependent
187 /// modules parsed.
188 size_t
189 ParseDependentModules();
Chris Lattner24943d22010-06-08 16:52:24 +0000190
Stephen Wilsonce477322011-03-30 16:07:05 +0000191 /// Parses the dynamic symbol table and populates m_dynamic_symbols. The
192 /// vector retains the order as found in the object file. Returns the
193 /// number of dynamic symbols parsed.
194 size_t
195 ParseDynamicSymbols();
196
Stephen Wilsonddd29622010-07-13 23:07:23 +0000197 /// Populates m_symtab_ap will all non-dynamic linker symbols. This method
198 /// will parse the symbols only once. Returns the number of symbols parsed.
Stephen Wilsonce477322011-03-30 16:07:05 +0000199 unsigned
Stephen Wilsonddd29622010-07-13 23:07:23 +0000200 ParseSymbolTable(lldb_private::Symtab *symbol_table,
Stephen Wilsonce477322011-03-30 16:07:05 +0000201 lldb::user_id_t start_id,
202 const elf::ELFSectionHeader *symtab_section,
Stephen Wilsonddd29622010-07-13 23:07:23 +0000203 lldb::user_id_t symtab_id);
204
Stephen Wilsonce477322011-03-30 16:07:05 +0000205 /// Scans the relocation entries and adds a set of artificial symbols to the
206 /// given symbol table for each PLT slot. Returns the number of symbols
207 /// added.
208 unsigned
209 ParseTrampolineSymbols(lldb_private::Symtab *symbol_table,
210 lldb::user_id_t start_id,
211 const elf::ELFSectionHeader *rela_hdr,
212 lldb::user_id_t section_id);
213
Stephen Wilsonddd29622010-07-13 23:07:23 +0000214 /// Loads the section name string table into m_shstr_data. Returns the
215 /// number of bytes constituting the table.
216 size_t
217 GetSectionHeaderStringTable();
218
219 /// Utility method for looking up a section given its name. Returns the
220 /// index of the corresponding section or zero if no section with the given
221 /// name can be found (note that section indices are always 1 based, and so
222 /// section index 0 is never valid).
223 lldb::user_id_t
224 GetSectionIndexByName(const char *name);
225
Stephen Wilsonce477322011-03-30 16:07:05 +0000226 // Returns the ID of the first section that has the given type.
227 lldb::user_id_t
228 GetSectionIndexByType(unsigned type);
229
230 /// Returns the section header with the given id or NULL.
231 const elf::ELFSectionHeader *
232 GetSectionHeaderByIndex(lldb::user_id_t id);
233
Stephen Wilsonddd29622010-07-13 23:07:23 +0000234 /// @name ELF header dump routines
235 //@{
236 static void
237 DumpELFHeader(lldb_private::Stream *s, const elf::ELFHeader& header);
Chris Lattner24943d22010-06-08 16:52:24 +0000238
239 static void
Stephen Wilsonddd29622010-07-13 23:07:23 +0000240 DumpELFHeader_e_ident_EI_DATA(lldb_private::Stream *s,
241 unsigned char ei_data);
Chris Lattner24943d22010-06-08 16:52:24 +0000242
243 static void
Stephen Wilsonddd29622010-07-13 23:07:23 +0000244 DumpELFHeader_e_type(lldb_private::Stream *s, elf::elf_half e_type);
245 //@}
Chris Lattner24943d22010-06-08 16:52:24 +0000246
Stephen Wilsonddd29622010-07-13 23:07:23 +0000247 /// @name ELF program header dump routines
248 //@{
Chris Lattner24943d22010-06-08 16:52:24 +0000249 void
Stephen Wilsonddd29622010-07-13 23:07:23 +0000250 DumpELFProgramHeaders(lldb_private::Stream *s);
Chris Lattner24943d22010-06-08 16:52:24 +0000251
252 static void
Stephen Wilsonddd29622010-07-13 23:07:23 +0000253 DumpELFProgramHeader(lldb_private::Stream *s,
254 const elf::ELFProgramHeader &ph);
Chris Lattner24943d22010-06-08 16:52:24 +0000255
256 static void
Stephen Wilsonddd29622010-07-13 23:07:23 +0000257 DumpELFProgramHeader_p_type(lldb_private::Stream *s, elf::elf_word p_type);
Chris Lattner24943d22010-06-08 16:52:24 +0000258
259 static void
Stephen Wilsonddd29622010-07-13 23:07:23 +0000260 DumpELFProgramHeader_p_flags(lldb_private::Stream *s,
261 elf::elf_word p_flags);
262 //@}
Chris Lattner24943d22010-06-08 16:52:24 +0000263
Stephen Wilsonddd29622010-07-13 23:07:23 +0000264 /// @name ELF section header dump routines
265 //@{
266 void
267 DumpELFSectionHeaders(lldb_private::Stream *s);
Chris Lattner24943d22010-06-08 16:52:24 +0000268
Stephen Wilsonddd29622010-07-13 23:07:23 +0000269 static void
270 DumpELFSectionHeader(lldb_private::Stream *s,
271 const elf::ELFSectionHeader& sh);
Chris Lattner24943d22010-06-08 16:52:24 +0000272
Stephen Wilsonddd29622010-07-13 23:07:23 +0000273 static void
274 DumpELFSectionHeader_sh_type(lldb_private::Stream *s,
275 elf::elf_word sh_type);
Chris Lattner24943d22010-06-08 16:52:24 +0000276
Stephen Wilsonddd29622010-07-13 23:07:23 +0000277 static void
278 DumpELFSectionHeader_sh_flags(lldb_private::Stream *s,
279 elf::elf_word sh_flags);
280 //@}
281
282 /// ELF dependent module dump routine.
283 void
284 DumpDependentModules(lldb_private::Stream *s);
285
Stephen Wilsonce477322011-03-30 16:07:05 +0000286 const elf::ELFDynamic *
287 FindDynamicSymbol(unsigned tag);
288
289 lldb_private::Section *
290 PLTSection();
291
292 unsigned
293 PLTRelocationType();
Chris Lattner24943d22010-06-08 16:52:24 +0000294};
295
296#endif // #ifndef liblldb_ObjectFileELF_h_