blob: f442ea218abe5f70b9c15d12d538648598828a47 [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 ();
Stephen Wilsonf2ad3252011-01-15 00:08:44 +0000107
Stephen Wilsonddd29622010-07-13 23:07:23 +0000108private:
109 ObjectFileELF(lldb_private::Module* module,
110 lldb::DataBufferSP& dataSP,
111 const lldb_private::FileSpec* file,
112 lldb::addr_t offset,
113 lldb::addr_t length);
114
115 typedef std::vector<elf::ELFProgramHeader> ProgramHeaderColl;
Chris Lattner24943d22010-06-08 16:52:24 +0000116 typedef ProgramHeaderColl::iterator ProgramHeaderCollIter;
117 typedef ProgramHeaderColl::const_iterator ProgramHeaderCollConstIter;
118
Stephen Wilsonddd29622010-07-13 23:07:23 +0000119 typedef std::vector<elf::ELFSectionHeader> SectionHeaderColl;
Chris Lattner24943d22010-06-08 16:52:24 +0000120 typedef SectionHeaderColl::iterator SectionHeaderCollIter;
121 typedef SectionHeaderColl::const_iterator SectionHeaderCollConstIter;
122
Stephen Wilsonce477322011-03-30 16:07:05 +0000123 typedef std::vector<elf::ELFDynamic> DynamicSymbolColl;
124 typedef DynamicSymbolColl::iterator DynamicSymbolCollIter;
125 typedef DynamicSymbolColl::const_iterator DynamicSymbolCollConstIter;
126
Stephen Wilsonddd29622010-07-13 23:07:23 +0000127 /// Version of this reader common to all plugins based on this class.
128 static const uint32_t m_plugin_version = 1;
129
130 /// ELF file header.
131 elf::ELFHeader m_header;
132
133 /// Collection of program headers.
Chris Lattner24943d22010-06-08 16:52:24 +0000134 ProgramHeaderColl m_program_headers;
Stephen Wilsonddd29622010-07-13 23:07:23 +0000135
136 /// Collection of section headers.
Chris Lattner24943d22010-06-08 16:52:24 +0000137 SectionHeaderColl m_section_headers;
Stephen Wilsonddd29622010-07-13 23:07:23 +0000138
Stephen Wilsonce477322011-03-30 16:07:05 +0000139 /// Collection of symbols from the dynamic table.
140 DynamicSymbolColl m_dynamic_symbols;
141
Stephen Wilsonddd29622010-07-13 23:07:23 +0000142 /// List of sections present in this ELF object file.
Chris Lattner24943d22010-06-08 16:52:24 +0000143 mutable std::auto_ptr<lldb_private::SectionList> m_sections_ap;
Stephen Wilsonddd29622010-07-13 23:07:23 +0000144
145 /// Table of all non-dynamic symbols present in this object file.
Chris Lattner24943d22010-06-08 16:52:24 +0000146 mutable std::auto_ptr<lldb_private::Symtab> m_symtab_ap;
Stephen Wilsonddd29622010-07-13 23:07:23 +0000147
148 /// List of file specifications corresponding to the modules (shared
149 /// libraries) on which this object file depends.
150 mutable std::auto_ptr<lldb_private::FileSpecList> m_filespec_ap;
151
152 /// Data extractor holding the string table used to resolve section names.
Chris Lattner24943d22010-06-08 16:52:24 +0000153 lldb_private::DataExtractor m_shstr_data;
154
Jim Ingham28775942011-03-07 23:44:08 +0000155 /// Cached value of the entry point for this module.
156 lldb_private::Address m_entry_point_address;
Stephen Wilsonce477322011-03-30 16:07:05 +0000157
Stephen Wilsonddd29622010-07-13 23:07:23 +0000158 /// Returns a 1 based index of the given section header.
159 unsigned
160 SectionIndex(const SectionHeaderCollIter &I);
161
162 /// Returns a 1 based index of the given section header.
163 unsigned
164 SectionIndex(const SectionHeaderCollConstIter &I) const;
165
166 /// Parses all section headers present in this object file and populates
167 /// m_program_headers. This method will compute the header list only once.
168 /// Returns the number of headers parsed.
Chris Lattner24943d22010-06-08 16:52:24 +0000169 size_t
Stephen Wilsonddd29622010-07-13 23:07:23 +0000170 ParseProgramHeaders();
Chris Lattner24943d22010-06-08 16:52:24 +0000171
Stephen Wilsonddd29622010-07-13 23:07:23 +0000172 /// Parses all section headers present in this object file and populates
173 /// m_section_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 ParseSectionHeaders();
Chris Lattner24943d22010-06-08 16:52:24 +0000177
Stephen Wilsonddd29622010-07-13 23:07:23 +0000178 /// Scans the dynamic section and locates all dependent modules (shared
Greg Clayton5d187e52011-01-08 20:28:42 +0000179 /// libraries) populating m_filespec_ap. This method will compute the
Stephen Wilsonddd29622010-07-13 23:07:23 +0000180 /// dependent module list only once. Returns the number of dependent
181 /// modules parsed.
182 size_t
183 ParseDependentModules();
Chris Lattner24943d22010-06-08 16:52:24 +0000184
Stephen Wilsonce477322011-03-30 16:07:05 +0000185 /// Parses the dynamic symbol table and populates m_dynamic_symbols. The
186 /// vector retains the order as found in the object file. Returns the
187 /// number of dynamic symbols parsed.
188 size_t
189 ParseDynamicSymbols();
190
Stephen Wilsonddd29622010-07-13 23:07:23 +0000191 /// Populates m_symtab_ap will all non-dynamic linker symbols. This method
192 /// will parse the symbols only once. Returns the number of symbols parsed.
Stephen Wilsonce477322011-03-30 16:07:05 +0000193 unsigned
Stephen Wilsonddd29622010-07-13 23:07:23 +0000194 ParseSymbolTable(lldb_private::Symtab *symbol_table,
Stephen Wilsonce477322011-03-30 16:07:05 +0000195 lldb::user_id_t start_id,
196 const elf::ELFSectionHeader *symtab_section,
Stephen Wilsonddd29622010-07-13 23:07:23 +0000197 lldb::user_id_t symtab_id);
198
Stephen Wilsonce477322011-03-30 16:07:05 +0000199 /// Scans the relocation entries and adds a set of artificial symbols to the
200 /// given symbol table for each PLT slot. Returns the number of symbols
201 /// added.
202 unsigned
203 ParseTrampolineSymbols(lldb_private::Symtab *symbol_table,
204 lldb::user_id_t start_id,
205 const elf::ELFSectionHeader *rela_hdr,
206 lldb::user_id_t section_id);
207
Stephen Wilsonddd29622010-07-13 23:07:23 +0000208 /// Loads the section name string table into m_shstr_data. Returns the
209 /// number of bytes constituting the table.
210 size_t
211 GetSectionHeaderStringTable();
212
213 /// Utility method for looking up a section given its name. Returns the
214 /// index of the corresponding section or zero if no section with the given
215 /// name can be found (note that section indices are always 1 based, and so
216 /// section index 0 is never valid).
217 lldb::user_id_t
218 GetSectionIndexByName(const char *name);
219
Stephen Wilsonce477322011-03-30 16:07:05 +0000220 // Returns the ID of the first section that has the given type.
221 lldb::user_id_t
222 GetSectionIndexByType(unsigned type);
223
224 /// Returns the section header with the given id or NULL.
225 const elf::ELFSectionHeader *
226 GetSectionHeaderByIndex(lldb::user_id_t id);
227
Stephen Wilsonddd29622010-07-13 23:07:23 +0000228 /// @name ELF header dump routines
229 //@{
230 static void
231 DumpELFHeader(lldb_private::Stream *s, const elf::ELFHeader& header);
Chris Lattner24943d22010-06-08 16:52:24 +0000232
233 static void
Stephen Wilsonddd29622010-07-13 23:07:23 +0000234 DumpELFHeader_e_ident_EI_DATA(lldb_private::Stream *s,
235 unsigned char ei_data);
Chris Lattner24943d22010-06-08 16:52:24 +0000236
237 static void
Stephen Wilsonddd29622010-07-13 23:07:23 +0000238 DumpELFHeader_e_type(lldb_private::Stream *s, elf::elf_half e_type);
239 //@}
Chris Lattner24943d22010-06-08 16:52:24 +0000240
Stephen Wilsonddd29622010-07-13 23:07:23 +0000241 /// @name ELF program header dump routines
242 //@{
Chris Lattner24943d22010-06-08 16:52:24 +0000243 void
Stephen Wilsonddd29622010-07-13 23:07:23 +0000244 DumpELFProgramHeaders(lldb_private::Stream *s);
Chris Lattner24943d22010-06-08 16:52:24 +0000245
246 static void
Stephen Wilsonddd29622010-07-13 23:07:23 +0000247 DumpELFProgramHeader(lldb_private::Stream *s,
248 const elf::ELFProgramHeader &ph);
Chris Lattner24943d22010-06-08 16:52:24 +0000249
250 static void
Stephen Wilsonddd29622010-07-13 23:07:23 +0000251 DumpELFProgramHeader_p_type(lldb_private::Stream *s, elf::elf_word p_type);
Chris Lattner24943d22010-06-08 16:52:24 +0000252
253 static void
Stephen Wilsonddd29622010-07-13 23:07:23 +0000254 DumpELFProgramHeader_p_flags(lldb_private::Stream *s,
255 elf::elf_word p_flags);
256 //@}
Chris Lattner24943d22010-06-08 16:52:24 +0000257
Stephen Wilsonddd29622010-07-13 23:07:23 +0000258 /// @name ELF section header dump routines
259 //@{
260 void
261 DumpELFSectionHeaders(lldb_private::Stream *s);
Chris Lattner24943d22010-06-08 16:52:24 +0000262
Stephen Wilsonddd29622010-07-13 23:07:23 +0000263 static void
264 DumpELFSectionHeader(lldb_private::Stream *s,
265 const elf::ELFSectionHeader& sh);
Chris Lattner24943d22010-06-08 16:52:24 +0000266
Stephen Wilsonddd29622010-07-13 23:07:23 +0000267 static void
268 DumpELFSectionHeader_sh_type(lldb_private::Stream *s,
269 elf::elf_word sh_type);
Chris Lattner24943d22010-06-08 16:52:24 +0000270
Stephen Wilsonddd29622010-07-13 23:07:23 +0000271 static void
272 DumpELFSectionHeader_sh_flags(lldb_private::Stream *s,
273 elf::elf_word sh_flags);
274 //@}
275
276 /// ELF dependent module dump routine.
277 void
278 DumpDependentModules(lldb_private::Stream *s);
279
Stephen Wilsonce477322011-03-30 16:07:05 +0000280 const elf::ELFDynamic *
281 FindDynamicSymbol(unsigned tag);
282
283 lldb_private::Section *
284 PLTSection();
285
286 unsigned
287 PLTRelocationType();
Chris Lattner24943d22010-06-08 16:52:24 +0000288};
289
290#endif // #ifndef liblldb_ObjectFileELF_h_