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_
20 // Other libraries and framework includes
22 #include "lldb/lldb-private.h"
23 #include "lldb/Host/FileSpec.h"
24 #include "lldb/Symbol/ObjectFile.h"
25 #include "lldb/Core/UUID.h"
26 #include "lldb/Core/ArchSpec.h"
28 #include "ELFHeader.h"
32 elf::elf_word n_namesz;
33 elf::elf_word n_descsz;
38 ELFNote() : n_namesz(0), n_descsz(0), n_type(0)
42 /// Parse an ELFNote entry from the given DataExtractor starting at position
46 /// The DataExtractor to read from.
48 /// @param[in,out] offset
49 /// Pointer to an offset in the data. On return the offset will be
50 /// advanced by the number of bytes read.
53 /// True if the ELFRel entry was successfully read and false otherwise.
55 Parse(const lldb_private::DataExtractor &data, lldb::offset_t *offset);
60 return 12 + llvm::alignTo (n_namesz, 4) + llvm::alignTo (n_descsz, 4);
64 //------------------------------------------------------------------------------
65 /// @class ObjectFileELF
66 /// @brief Generic ELF object file reader.
68 /// This class provides a generic ELF (32/64 bit) reader plugin implementing the
69 /// ObjectFile protocol.
71 public lldb_private::ObjectFile
74 ~ObjectFileELF() override;
76 //------------------------------------------------------------------
78 //------------------------------------------------------------------
85 static lldb_private::ConstString
86 GetPluginNameStatic();
89 GetPluginDescriptionStatic();
91 static lldb_private::ObjectFile *
92 CreateInstance(const lldb::ModuleSP &module_sp,
93 lldb::DataBufferSP& data_sp,
94 lldb::offset_t data_offset,
95 const lldb_private::FileSpec* file,
96 lldb::offset_t file_offset,
97 lldb::offset_t length);
99 static lldb_private::ObjectFile *
100 CreateMemoryInstance (const lldb::ModuleSP &module_sp,
101 lldb::DataBufferSP& data_sp,
102 const lldb::ProcessSP &process_sp,
103 lldb::addr_t header_addr);
106 GetModuleSpecifications (const lldb_private::FileSpec& file,
107 lldb::DataBufferSP& data_sp,
108 lldb::offset_t data_offset,
109 lldb::offset_t file_offset,
110 lldb::offset_t length,
111 lldb_private::ModuleSpecList &specs);
114 MagicBytesMatch (lldb::DataBufferSP& data_sp,
116 lldb::addr_t length);
118 //------------------------------------------------------------------
119 // PluginInterface protocol
120 //------------------------------------------------------------------
121 lldb_private::ConstString
122 GetPluginName() override;
125 GetPluginVersion() override;
127 //------------------------------------------------------------------
128 // ObjectFile Protocol.
129 //------------------------------------------------------------------
131 ParseHeader() override;
134 SetLoadAddress (lldb_private::Target &target,
136 bool value_is_offset) override;
139 GetByteOrder() const override;
142 IsExecutable () const override;
145 GetAddressByteSize() const override;
148 GetAddressClass (lldb::addr_t file_addr) override;
150 lldb_private::Symtab *
151 GetSymtab() override;
154 IsStripped () override;
157 CreateSections (lldb_private::SectionList &unified_section_list) override;
160 Dump(lldb_private::Stream *s) override;
163 GetArchitecture (lldb_private::ArchSpec &arch) override;
166 GetUUID(lldb_private::UUID* uuid) override;
168 lldb_private::FileSpecList
169 GetDebugSymbolFilePaths() override;
172 GetDependentModules(lldb_private::FileSpecList& files) override;
174 lldb_private::Address
175 GetImageInfoAddress(lldb_private::Target *target) override;
177 lldb_private::Address
178 GetEntryPointAddress () override;
181 CalculateType() override;
184 CalculateStrata() override;
186 // Returns number of program headers found in the ELF file.
188 GetProgramHeaderCount();
190 // Returns the program header with the given index.
191 const elf::ELFProgramHeader *
192 GetProgramHeaderByIndex(lldb::user_id_t id);
194 // Returns segment data for the given index.
195 lldb_private::DataExtractor
196 GetSegmentDataByIndex(lldb::user_id_t id);
199 StripLinkerSymbolAnnotations(llvm::StringRef symbol_name) const override;
202 ObjectFileELF(const lldb::ModuleSP &module_sp,
203 lldb::DataBufferSP& data_sp,
204 lldb::offset_t data_offset,
205 const lldb_private::FileSpec* file,
206 lldb::offset_t offset,
207 lldb::offset_t length);
209 ObjectFileELF (const lldb::ModuleSP &module_sp,
210 lldb::DataBufferSP& header_data_sp,
211 const lldb::ProcessSP &process_sp,
212 lldb::addr_t header_addr);
214 typedef std::vector<elf::ELFProgramHeader> ProgramHeaderColl;
215 typedef ProgramHeaderColl::iterator ProgramHeaderCollIter;
216 typedef ProgramHeaderColl::const_iterator ProgramHeaderCollConstIter;
218 struct ELFSectionHeaderInfo : public elf::ELFSectionHeader
220 lldb_private::ConstString section_name;
223 typedef std::vector<ELFSectionHeaderInfo> SectionHeaderColl;
224 typedef SectionHeaderColl::iterator SectionHeaderCollIter;
225 typedef SectionHeaderColl::const_iterator SectionHeaderCollConstIter;
227 typedef std::vector<elf::ELFDynamic> DynamicSymbolColl;
228 typedef DynamicSymbolColl::iterator DynamicSymbolCollIter;
229 typedef DynamicSymbolColl::const_iterator DynamicSymbolCollConstIter;
231 typedef std::map<lldb::addr_t, lldb::AddressClass> FileAddressToAddressClassMap;
232 typedef std::function<lldb::offset_t (lldb_private::DataExtractor &, lldb::offset_t, lldb::offset_t)> SetDataFunction;
234 /// Version of this reader common to all plugins based on this class.
235 static const uint32_t m_plugin_version = 1;
236 static const uint32_t g_core_uuid_magic;
239 elf::ELFHeader m_header;
242 lldb_private::UUID m_uuid;
244 /// ELF .gnu_debuglink file and crc data if available.
245 std::string m_gnu_debuglink_file;
246 uint32_t m_gnu_debuglink_crc;
248 /// Collection of program headers.
249 ProgramHeaderColl m_program_headers;
251 /// Collection of section headers.
252 SectionHeaderColl m_section_headers;
254 /// Collection of symbols from the dynamic table.
255 DynamicSymbolColl m_dynamic_symbols;
257 /// List of file specifications corresponding to the modules (shared
258 /// libraries) on which this object file depends.
259 mutable std::unique_ptr<lldb_private::FileSpecList> m_filespec_ap;
261 /// Cached value of the entry point for this module.
262 lldb_private::Address m_entry_point_address;
264 /// The architecture detected from parsing elf file contents.
265 lldb_private::ArchSpec m_arch_spec;
267 /// The address class for each symbol in the elf file
268 FileAddressToAddressClassMap m_address_class_map;
270 /// Returns a 1 based index of the given section header.
272 SectionIndex(const SectionHeaderCollIter &I);
274 /// Returns a 1 based index of the given section header.
276 SectionIndex(const SectionHeaderCollConstIter &I) const;
278 // Parses the ELF program headers.
280 GetProgramHeaderInfo(ProgramHeaderColl &program_headers,
281 const SetDataFunction &set_data,
282 const elf::ELFHeader &header);
284 // Finds PT_NOTE segments and calculates their crc sum.
286 CalculateELFNotesSegmentsCRC32(const ProgramHeaderColl& program_headers,
287 lldb_private::DataExtractor &data);
289 /// Parses all section headers present in this object file and populates
290 /// m_program_headers. This method will compute the header list only once.
291 /// Returns the number of headers parsed.
293 ParseProgramHeaders();
295 /// Parses all section headers present in this object file and populates
296 /// m_section_headers. This method will compute the header list only once.
297 /// Returns the number of headers parsed.
299 ParseSectionHeaders();
302 ParseARMAttributes(lldb_private::DataExtractor &data, uint64_t length,
303 lldb_private::ArchSpec &arch_spec);
305 /// Parses the elf section headers and returns the uuid, debug link name, crc, archspec.
307 GetSectionHeaderInfo(SectionHeaderColl §ion_headers,
308 const SetDataFunction &set_data,
309 const elf::ELFHeader &header,
310 lldb_private::UUID &uuid,
311 std::string &gnu_debuglink_file,
312 uint32_t &gnu_debuglink_crc,
313 lldb_private::ArchSpec &arch_spec);
315 /// Scans the dynamic section and locates all dependent modules (shared
316 /// libraries) populating m_filespec_ap. This method will compute the
317 /// dependent module list only once. Returns the number of dependent
320 ParseDependentModules();
322 /// Parses the dynamic symbol table and populates m_dynamic_symbols. The
323 /// vector retains the order as found in the object file. Returns the
324 /// number of dynamic symbols parsed.
326 ParseDynamicSymbols();
328 /// Populates m_symtab_ap will all non-dynamic linker symbols. This method
329 /// will parse the symbols only once. Returns the number of symbols parsed.
331 ParseSymbolTable(lldb_private::Symtab *symbol_table,
332 lldb::user_id_t start_id,
333 lldb_private::Section *symtab);
335 /// Helper routine for ParseSymbolTable().
337 ParseSymbols(lldb_private::Symtab *symbol_table,
338 lldb::user_id_t start_id,
339 lldb_private::SectionList *section_list,
340 const size_t num_symbols,
341 const lldb_private::DataExtractor &symtab_data,
342 const lldb_private::DataExtractor &strtab_data);
344 /// Scans the relocation entries and adds a set of artificial symbols to the
345 /// given symbol table for each PLT slot. Returns the number of symbols
348 ParseTrampolineSymbols(lldb_private::Symtab *symbol_table,
349 lldb::user_id_t start_id,
350 const ELFSectionHeaderInfo *rela_hdr,
351 lldb::user_id_t section_id);
354 ParseUnwindSymbols(lldb_private::Symtab *symbol_table,
355 lldb_private::DWARFCallFrameInfo* eh_frame);
357 /// Relocates debug sections
359 RelocateDebugSections(const elf::ELFSectionHeader *rel_hdr, lldb::user_id_t rel_id);
362 RelocateSection(lldb_private::Symtab* symtab, const elf::ELFHeader *hdr, const elf::ELFSectionHeader *rel_hdr,
363 const elf::ELFSectionHeader *symtab_hdr, const elf::ELFSectionHeader *debug_hdr,
364 lldb_private::DataExtractor &rel_data, lldb_private::DataExtractor &symtab_data,
365 lldb_private::DataExtractor &debug_data, lldb_private::Section* rel_section);
367 /// Loads the section name string table into m_shstr_data. Returns the
368 /// number of bytes constituting the table.
370 GetSectionHeaderStringTable();
372 /// Utility method for looking up a section given its name. Returns the
373 /// index of the corresponding section or zero if no section with the given
374 /// name can be found (note that section indices are always 1 based, and so
375 /// section index 0 is never valid).
377 GetSectionIndexByName(const char *name);
379 // Returns the ID of the first section that has the given type.
381 GetSectionIndexByType(unsigned type);
383 /// Returns the section header with the given id or NULL.
384 const ELFSectionHeaderInfo *
385 GetSectionHeaderByIndex(lldb::user_id_t id);
387 /// @name ELF header dump routines
390 DumpELFHeader(lldb_private::Stream *s, const elf::ELFHeader& header);
393 DumpELFHeader_e_ident_EI_DATA(lldb_private::Stream *s,
394 unsigned char ei_data);
397 DumpELFHeader_e_type(lldb_private::Stream *s, elf::elf_half e_type);
400 /// @name ELF program header dump routines
403 DumpELFProgramHeaders(lldb_private::Stream *s);
406 DumpELFProgramHeader(lldb_private::Stream *s,
407 const elf::ELFProgramHeader &ph);
410 DumpELFProgramHeader_p_type(lldb_private::Stream *s, elf::elf_word p_type);
413 DumpELFProgramHeader_p_flags(lldb_private::Stream *s,
414 elf::elf_word p_flags);
417 /// @name ELF section header dump routines
420 DumpELFSectionHeaders(lldb_private::Stream *s);
423 DumpELFSectionHeader(lldb_private::Stream *s,
424 const ELFSectionHeaderInfo& sh);
427 DumpELFSectionHeader_sh_type(lldb_private::Stream *s,
428 elf::elf_word sh_type);
431 DumpELFSectionHeader_sh_flags(lldb_private::Stream *s,
432 elf::elf_xword sh_flags);
435 /// ELF dependent module dump routine.
437 DumpDependentModules(lldb_private::Stream *s);
439 const elf::ELFDynamic *
440 FindDynamicSymbol(unsigned tag);
445 static lldb_private::Error
446 RefineModuleDetailsFromNote (lldb_private::DataExtractor &data, lldb_private::ArchSpec &arch_spec, lldb_private::UUID &uuid);
449 static lldb::offset_t
450 SetData(const lldb_private::DataExtractor &src, lldb_private::DataExtractor &dst, lldb::offset_t offset, lldb::offset_t length);
453 SetDataWithReadMemoryFallback(lldb_private::DataExtractor &dst, lldb::offset_t offset, lldb::offset_t length);
456 #endif // liblldb_ObjectFileELF_h_