1 //===-- ObjectFileELF.h --------------------------------------- -*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef liblldb_ObjectFileELF_h_
11 #define liblldb_ObjectFileELF_h_
16 #include "lldb/lldb-private.h"
17 #include "lldb/Host/FileSpec.h"
18 #include "lldb/Symbol/ObjectFile.h"
19 #include "lldb/Core/UUID.h"
21 #include "ELFHeader.h"
25 elf::elf_word n_namesz;
26 elf::elf_word n_descsz;
31 ELFNote() : n_namesz(0), n_descsz(0), n_type(0)
35 /// Parse an ELFNote entry from the given DataExtractor starting at position
39 /// The DataExtractor to read from.
41 /// @param[in,out] offset
42 /// Pointer to an offset in the data. On return the offset will be
43 /// advanced by the number of bytes read.
46 /// True if the ELFRel entry was successfully read and false otherwise.
48 Parse(const lldb_private::DataExtractor &data, lldb::offset_t *offset);
51 //------------------------------------------------------------------------------
52 /// @class ObjectFileELF
53 /// @brief Generic ELF object file reader.
55 /// This class provides a generic ELF (32/64 bit) reader plugin implementing the
56 /// ObjectFile protocol.
58 public lldb_private::ObjectFile
61 //------------------------------------------------------------------
63 //------------------------------------------------------------------
70 static lldb_private::ConstString
71 GetPluginNameStatic();
74 GetPluginDescriptionStatic();
76 static lldb_private::ObjectFile *
77 CreateInstance(const lldb::ModuleSP &module_sp,
78 lldb::DataBufferSP& data_sp,
79 lldb::offset_t data_offset,
80 const lldb_private::FileSpec* file,
81 lldb::offset_t file_offset,
82 lldb::offset_t length);
84 static lldb_private::ObjectFile *
85 CreateMemoryInstance (const lldb::ModuleSP &module_sp,
86 lldb::DataBufferSP& data_sp,
87 const lldb::ProcessSP &process_sp,
88 lldb::addr_t header_addr);
91 GetModuleSpecifications (const lldb_private::FileSpec& file,
92 lldb::DataBufferSP& data_sp,
93 lldb::offset_t data_offset,
94 lldb::offset_t file_offset,
95 lldb::offset_t length,
96 lldb_private::ModuleSpecList &specs);
99 MagicBytesMatch (lldb::DataBufferSP& data_sp,
101 lldb::addr_t length);
103 //------------------------------------------------------------------
104 // PluginInterface protocol
105 //------------------------------------------------------------------
106 virtual lldb_private::ConstString
112 //------------------------------------------------------------------
113 // ObjectFile Protocol.
114 //------------------------------------------------------------------
122 SetLoadAddress (lldb_private::Target &target,
124 bool value_is_offset);
126 virtual lldb::ByteOrder
127 GetByteOrder() const;
130 IsExecutable () const;
133 GetAddressByteSize() const;
135 virtual lldb_private::Symtab *
138 virtual lldb_private::Symbol *
139 ResolveSymbolForAddress(const lldb_private::Address& so_addr, bool verify_unique);
145 CreateSections (lldb_private::SectionList &unified_section_list);
148 Dump(lldb_private::Stream *s);
151 GetArchitecture (lldb_private::ArchSpec &arch);
154 GetUUID(lldb_private::UUID* uuid);
156 virtual lldb_private::FileSpecList
157 GetDebugSymbolFilePaths();
160 GetDependentModules(lldb_private::FileSpecList& files);
162 virtual lldb_private::Address
163 GetImageInfoAddress(lldb_private::Target *target);
165 virtual lldb_private::Address
166 GetEntryPointAddress ();
168 virtual ObjectFile::Type
171 virtual ObjectFile::Strata
174 // Returns number of program headers found in the ELF file.
176 GetProgramHeaderCount();
178 // Returns the program header with the given index.
179 const elf::ELFProgramHeader *
180 GetProgramHeaderByIndex(lldb::user_id_t id);
182 // Returns segment data for the given index.
183 lldb_private::DataExtractor
184 GetSegmentDataByIndex(lldb::user_id_t id);
187 ObjectFileELF(const lldb::ModuleSP &module_sp,
188 lldb::DataBufferSP& data_sp,
189 lldb::offset_t data_offset,
190 const lldb_private::FileSpec* file,
191 lldb::offset_t offset,
192 lldb::offset_t length);
194 typedef std::vector<elf::ELFProgramHeader> ProgramHeaderColl;
195 typedef ProgramHeaderColl::iterator ProgramHeaderCollIter;
196 typedef ProgramHeaderColl::const_iterator ProgramHeaderCollConstIter;
198 struct ELFSectionHeaderInfo : public elf::ELFSectionHeader
200 lldb_private::ConstString section_name;
202 typedef std::vector<ELFSectionHeaderInfo> SectionHeaderColl;
203 typedef SectionHeaderColl::iterator SectionHeaderCollIter;
204 typedef SectionHeaderColl::const_iterator SectionHeaderCollConstIter;
206 typedef std::vector<elf::ELFDynamic> DynamicSymbolColl;
207 typedef DynamicSymbolColl::iterator DynamicSymbolCollIter;
208 typedef DynamicSymbolColl::const_iterator DynamicSymbolCollConstIter;
210 /// Version of this reader common to all plugins based on this class.
211 static const uint32_t m_plugin_version = 1;
214 elf::ELFHeader m_header;
217 lldb_private::UUID m_uuid;
219 /// ELF .gnu_debuglink file and crc data if available.
220 std::string m_gnu_debuglink_file;
221 uint32_t m_gnu_debuglink_crc;
223 /// Collection of program headers.
224 ProgramHeaderColl m_program_headers;
226 /// Collection of section headers.
227 SectionHeaderColl m_section_headers;
229 /// Collection of symbols from the dynamic table.
230 DynamicSymbolColl m_dynamic_symbols;
232 /// List of file specifications corresponding to the modules (shared
233 /// libraries) on which this object file depends.
234 mutable std::unique_ptr<lldb_private::FileSpecList> m_filespec_ap;
236 /// Cached value of the entry point for this module.
237 lldb_private::Address m_entry_point_address;
239 /// Returns a 1 based index of the given section header.
241 SectionIndex(const SectionHeaderCollIter &I);
243 /// Returns a 1 based index of the given section header.
245 SectionIndex(const SectionHeaderCollConstIter &I) const;
247 /// Parses all section headers present in this object file and populates
248 /// m_program_headers. This method will compute the header list only once.
249 /// Returns the number of headers parsed.
251 ParseProgramHeaders();
253 /// Parses all section headers present in this object file and populates
254 /// m_section_headers. This method will compute the header list only once.
255 /// Returns the number of headers parsed.
257 ParseSectionHeaders();
259 /// Parses the elf section headers and returns the uuid, debug link name, crc.
261 GetSectionHeaderInfo(SectionHeaderColl §ion_headers,
262 lldb_private::DataExtractor &data,
263 const elf::ELFHeader &header,
264 lldb_private::UUID &uuid,
265 std::string &gnu_debuglink_file,
266 uint32_t &gnu_debuglink_crc);
268 /// Scans the dynamic section and locates all dependent modules (shared
269 /// libraries) populating m_filespec_ap. This method will compute the
270 /// dependent module list only once. Returns the number of dependent
273 ParseDependentModules();
275 /// Parses the dynamic symbol table and populates m_dynamic_symbols. The
276 /// vector retains the order as found in the object file. Returns the
277 /// number of dynamic symbols parsed.
279 ParseDynamicSymbols();
281 /// Populates m_symtab_ap will all non-dynamic linker symbols. This method
282 /// will parse the symbols only once. Returns the number of symbols parsed.
284 ParseSymbolTable(lldb_private::Symtab *symbol_table,
285 lldb::user_id_t start_id,
286 lldb_private::Section *symtab);
288 /// Helper routine for ParseSymbolTable().
290 ParseSymbols(lldb_private::Symtab *symbol_table,
291 lldb::user_id_t start_id,
292 lldb_private::SectionList *section_list,
293 const size_t num_symbols,
294 const lldb_private::DataExtractor &symtab_data,
295 const lldb_private::DataExtractor &strtab_data);
297 /// Scans the relocation entries and adds a set of artificial symbols to the
298 /// given symbol table for each PLT slot. Returns the number of symbols
301 ParseTrampolineSymbols(lldb_private::Symtab *symbol_table,
302 lldb::user_id_t start_id,
303 const ELFSectionHeaderInfo *rela_hdr,
304 lldb::user_id_t section_id);
306 /// Returns the section header with the given id or NULL.
307 const ELFSectionHeaderInfo *
308 GetSectionHeaderByIndex(lldb::user_id_t id);
310 /// @name ELF header dump routines
313 DumpELFHeader(lldb_private::Stream *s, const elf::ELFHeader& header);
316 DumpELFHeader_e_ident_EI_DATA(lldb_private::Stream *s,
317 unsigned char ei_data);
320 DumpELFHeader_e_type(lldb_private::Stream *s, elf::elf_half e_type);
323 /// @name ELF program header dump routines
326 DumpELFProgramHeaders(lldb_private::Stream *s);
329 DumpELFProgramHeader(lldb_private::Stream *s,
330 const elf::ELFProgramHeader &ph);
333 DumpELFProgramHeader_p_type(lldb_private::Stream *s, elf::elf_word p_type);
336 DumpELFProgramHeader_p_flags(lldb_private::Stream *s,
337 elf::elf_word p_flags);
340 /// @name ELF section header dump routines
343 DumpELFSectionHeaders(lldb_private::Stream *s);
346 DumpELFSectionHeader(lldb_private::Stream *s,
347 const ELFSectionHeaderInfo& sh);
350 DumpELFSectionHeader_sh_type(lldb_private::Stream *s,
351 elf::elf_word sh_type);
354 DumpELFSectionHeader_sh_flags(lldb_private::Stream *s,
355 elf::elf_xword sh_flags);
358 /// ELF dependent module dump routine.
360 DumpDependentModules(lldb_private::Stream *s);
362 const elf::ELFDynamic *
363 FindDynamicSymbol(unsigned tag);
369 #endif // #ifndef liblldb_ObjectFileELF_h_