blob: ef13ca46ad2da78f51a8034ae7158318ddf21483 [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"
19
Stephen Wilsonf325ba92010-07-13 23:07:23 +000020#include "ELFHeader.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000021
Stephen Wilsonf325ba92010-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 Lattner30fdc8d2010-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 Wilsonf325ba92010-07-13 23:07:23 +000048 CreateInstance(lldb_private::Module* module,
Chris Lattner30fdc8d2010-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 Lattner30fdc8d2010-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
66 virtual void
Stephen Wilsonf325ba92010-07-13 23:07:23 +000067 GetPluginCommandHelp(const char *command, lldb_private::Stream *strm);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000068
69 virtual lldb_private::Error
Stephen Wilsonf325ba92010-07-13 23:07:23 +000070 ExecutePluginCommand(lldb_private::Args &command,
71 lldb_private::Stream *strm);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000072
73 virtual lldb_private::Log *
Stephen Wilsonf325ba92010-07-13 23:07:23 +000074 EnablePluginLogging(lldb_private::Stream *strm,
75 lldb_private::Args &command);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000076
Stephen Wilsonf325ba92010-07-13 23:07:23 +000077 //------------------------------------------------------------------
78 // ObjectFile Protocol.
79 //------------------------------------------------------------------
80 virtual
81 ~ObjectFileELF();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000082
Stephen Wilsonf325ba92010-07-13 23:07:23 +000083 virtual bool
84 ParseHeader();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000085
Stephen Wilsonf325ba92010-07-13 23:07:23 +000086 virtual lldb::ByteOrder
87 GetByteOrder() const;
88
Jim Ingham5aee1622010-08-09 23:31:02 +000089 virtual bool
90 IsExecutable () const;
91
Stephen Wilson2ab0a582011-01-15 00:08:44 +000092 virtual lldb_private::Address
93 GetEntryPoint() const;
94
Stephen Wilsonf325ba92010-07-13 23:07:23 +000095 virtual size_t
96 GetAddressByteSize() const;
97
98 virtual lldb_private::Symtab *
99 GetSymtab();
100
101 virtual lldb_private::SectionList *
102 GetSectionList();
103
104 virtual void
105 Dump(lldb_private::Stream *s);
106
107 virtual bool
108 GetTargetTriple(lldb_private::ConstString &target_triple);
109
110 virtual bool
111 GetUUID(lldb_private::UUID* uuid);
112
113 virtual uint32_t
114 GetDependentModules(lldb_private::FileSpecList& files);
115
Stephen Wilson2ab0a582011-01-15 00:08:44 +0000116 virtual lldb_private::Address
117 GetImageInfoAddress();
118
119 lldb_private::ArchSpec
120 GetArchitecture();
121
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000122private:
123 ObjectFileELF(lldb_private::Module* module,
124 lldb::DataBufferSP& dataSP,
125 const lldb_private::FileSpec* file,
126 lldb::addr_t offset,
127 lldb::addr_t length);
128
129 typedef std::vector<elf::ELFProgramHeader> ProgramHeaderColl;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000130 typedef ProgramHeaderColl::iterator ProgramHeaderCollIter;
131 typedef ProgramHeaderColl::const_iterator ProgramHeaderCollConstIter;
132
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000133 typedef std::vector<elf::ELFSectionHeader> SectionHeaderColl;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000134 typedef SectionHeaderColl::iterator SectionHeaderCollIter;
135 typedef SectionHeaderColl::const_iterator SectionHeaderCollConstIter;
136
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000137 /// Version of this reader common to all plugins based on this class.
138 static const uint32_t m_plugin_version = 1;
139
140 /// ELF file header.
141 elf::ELFHeader m_header;
142
143 /// Collection of program headers.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000144 ProgramHeaderColl m_program_headers;
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000145
146 /// Collection of section headers.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000147 SectionHeaderColl m_section_headers;
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000148
149 /// List of sections present in this ELF object file.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000150 mutable std::auto_ptr<lldb_private::SectionList> m_sections_ap;
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000151
152 /// Table of all non-dynamic symbols present in this object file.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000153 mutable std::auto_ptr<lldb_private::Symtab> m_symtab_ap;
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000154
155 /// List of file specifications corresponding to the modules (shared
156 /// libraries) on which this object file depends.
157 mutable std::auto_ptr<lldb_private::FileSpecList> m_filespec_ap;
158
159 /// Data extractor holding the string table used to resolve section names.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000160 lldb_private::DataExtractor m_shstr_data;
161
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000162 /// Returns a 1 based index of the given section header.
163 unsigned
164 SectionIndex(const SectionHeaderCollIter &I);
165
166 /// Returns a 1 based index of the given section header.
167 unsigned
168 SectionIndex(const SectionHeaderCollConstIter &I) const;
169
170 /// Parses all section headers present in this object file and populates
171 /// m_program_headers. This method will compute the header list only once.
172 /// Returns the number of headers parsed.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000173 size_t
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000174 ParseProgramHeaders();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000175
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000176 /// Parses all section headers present in this object file and populates
177 /// m_section_headers. This method will compute the header list only once.
178 /// Returns the number of headers parsed.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000179 size_t
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000180 ParseSectionHeaders();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000181
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000182 /// Scans the dynamic section and locates all dependent modules (shared
Greg Clayton710dd5a2011-01-08 20:28:42 +0000183 /// libraries) populating m_filespec_ap. This method will compute the
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000184 /// dependent module list only once. Returns the number of dependent
185 /// modules parsed.
186 size_t
187 ParseDependentModules();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000188
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000189 /// Populates m_symtab_ap will all non-dynamic linker symbols. This method
190 /// will parse the symbols only once. Returns the number of symbols parsed.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000191 void
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000192 ParseSymbolTable(lldb_private::Symtab *symbol_table,
193 const elf::ELFSectionHeader &symtab_section,
194 lldb::user_id_t symtab_id);
195
196 /// Loads the section name string table into m_shstr_data. Returns the
197 /// number of bytes constituting the table.
198 size_t
199 GetSectionHeaderStringTable();
200
201 /// Utility method for looking up a section given its name. Returns the
202 /// index of the corresponding section or zero if no section with the given
203 /// name can be found (note that section indices are always 1 based, and so
204 /// section index 0 is never valid).
205 lldb::user_id_t
206 GetSectionIndexByName(const char *name);
207
208 /// @name ELF header dump routines
209 //@{
210 static void
211 DumpELFHeader(lldb_private::Stream *s, const elf::ELFHeader& header);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000212
213 static void
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000214 DumpELFHeader_e_ident_EI_DATA(lldb_private::Stream *s,
215 unsigned char ei_data);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000216
217 static void
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000218 DumpELFHeader_e_type(lldb_private::Stream *s, elf::elf_half e_type);
219 //@}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000220
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000221 /// @name ELF program header dump routines
222 //@{
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000223 void
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000224 DumpELFProgramHeaders(lldb_private::Stream *s);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000225
226 static void
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000227 DumpELFProgramHeader(lldb_private::Stream *s,
228 const elf::ELFProgramHeader &ph);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000229
230 static void
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000231 DumpELFProgramHeader_p_type(lldb_private::Stream *s, elf::elf_word p_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000232
233 static void
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000234 DumpELFProgramHeader_p_flags(lldb_private::Stream *s,
235 elf::elf_word p_flags);
236 //@}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000237
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000238 /// @name ELF section header dump routines
239 //@{
240 void
241 DumpELFSectionHeaders(lldb_private::Stream *s);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000242
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000243 static void
244 DumpELFSectionHeader(lldb_private::Stream *s,
245 const elf::ELFSectionHeader& sh);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000246
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000247 static void
248 DumpELFSectionHeader_sh_type(lldb_private::Stream *s,
249 elf::elf_word sh_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000250
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000251 static void
252 DumpELFSectionHeader_sh_flags(lldb_private::Stream *s,
253 elf::elf_word sh_flags);
254 //@}
255
256 /// ELF dependent module dump routine.
257 void
258 DumpDependentModules(lldb_private::Stream *s);
259
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000260};
261
262#endif // #ifndef liblldb_ObjectFileELF_h_