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_
19 #include "lldb/Symbol/ObjectFile.h"
20 #include "lldb/Utility/ArchSpec.h"
21 #include "lldb/Utility/FileSpec.h"
22 #include "lldb/Utility/UUID.h"
23 #include "lldb/lldb-private.h"
25 #include "ELFHeader.h"
28 elf::elf_word n_namesz;
29 elf::elf_word n_descsz;
34 ELFNote() : n_namesz(0), n_descsz(0), n_type(0) {}
36 /// Parse an ELFNote entry from the given DataExtractor starting at position
40 /// The DataExtractor to read from.
42 /// @param[in,out] offset
43 /// Pointer to an offset in the data. On return the offset will be
44 /// advanced by the number of bytes read.
47 /// True if the ELFRel entry was successfully read and false otherwise.
48 bool Parse(const lldb_private::DataExtractor &data, lldb::offset_t *offset);
50 size_t GetByteSize() const {
51 return 12 + llvm::alignTo(n_namesz, 4) + llvm::alignTo(n_descsz, 4);
55 //------------------------------------------------------------------------------
56 /// @class ObjectFileELF
57 /// Generic ELF object file reader.
59 /// This class provides a generic ELF (32/64 bit) reader plugin implementing
60 /// the ObjectFile protocol.
61 class ObjectFileELF : public lldb_private::ObjectFile {
63 ~ObjectFileELF() override;
65 //------------------------------------------------------------------
67 //------------------------------------------------------------------
68 static void Initialize();
70 static void Terminate();
72 static lldb_private::ConstString GetPluginNameStatic();
74 static const char *GetPluginDescriptionStatic();
76 static lldb_private::ObjectFile *
77 CreateInstance(const lldb::ModuleSP &module_sp, lldb::DataBufferSP &data_sp,
78 lldb::offset_t data_offset, const lldb_private::FileSpec *file,
79 lldb::offset_t file_offset, lldb::offset_t length);
81 static lldb_private::ObjectFile *CreateMemoryInstance(
82 const lldb::ModuleSP &module_sp, lldb::DataBufferSP &data_sp,
83 const lldb::ProcessSP &process_sp, lldb::addr_t header_addr);
85 static size_t GetModuleSpecifications(const lldb_private::FileSpec &file,
86 lldb::DataBufferSP &data_sp,
87 lldb::offset_t data_offset,
88 lldb::offset_t file_offset,
89 lldb::offset_t length,
90 lldb_private::ModuleSpecList &specs);
92 static bool MagicBytesMatch(lldb::DataBufferSP &data_sp, lldb::addr_t offset,
95 //------------------------------------------------------------------
96 // PluginInterface protocol
97 //------------------------------------------------------------------
98 lldb_private::ConstString GetPluginName() override;
100 uint32_t GetPluginVersion() override;
102 //------------------------------------------------------------------
103 // ObjectFile Protocol.
104 //------------------------------------------------------------------
105 bool ParseHeader() override;
107 bool SetLoadAddress(lldb_private::Target &target, lldb::addr_t value,
108 bool value_is_offset) override;
110 lldb::ByteOrder GetByteOrder() const override;
112 bool IsExecutable() const override;
114 uint32_t GetAddressByteSize() const override;
116 lldb_private::AddressClass GetAddressClass(lldb::addr_t file_addr) override;
118 lldb_private::Symtab *GetSymtab() override;
120 bool IsStripped() override;
122 void CreateSections(lldb_private::SectionList &unified_section_list) override;
124 void Dump(lldb_private::Stream *s) override;
126 bool GetArchitecture(lldb_private::ArchSpec &arch) override;
128 bool GetUUID(lldb_private::UUID *uuid) override;
130 lldb_private::FileSpecList GetDebugSymbolFilePaths() override;
132 uint32_t GetDependentModules(lldb_private::FileSpecList &files) override;
134 lldb_private::Address
135 GetImageInfoAddress(lldb_private::Target *target) override;
137 lldb_private::Address GetEntryPointAddress() override;
139 ObjectFile::Type CalculateType() override;
141 ObjectFile::Strata CalculateStrata() override;
143 size_t ReadSectionData(lldb_private::Section *section,
144 lldb::offset_t section_offset, void *dst,
145 size_t dst_len) override;
147 size_t ReadSectionData(lldb_private::Section *section,
148 lldb_private::DataExtractor §ion_data) override;
150 // Returns number of program headers found in the ELF file.
151 size_t GetProgramHeaderCount();
153 // Returns the program header with the given index.
154 const elf::ELFProgramHeader *GetProgramHeaderByIndex(lldb::user_id_t id);
156 // Returns segment data for the given index.
157 lldb_private::DataExtractor GetSegmentDataByIndex(lldb::user_id_t id);
160 StripLinkerSymbolAnnotations(llvm::StringRef symbol_name) const override;
162 void RelocateSection(lldb_private::Section *section) override;
166 std::vector<LoadableData>
167 GetLoadableData(lldb_private::Target &target) override;
170 ObjectFileELF(const lldb::ModuleSP &module_sp, lldb::DataBufferSP &data_sp,
171 lldb::offset_t data_offset, const lldb_private::FileSpec *file,
172 lldb::offset_t offset, lldb::offset_t length);
174 ObjectFileELF(const lldb::ModuleSP &module_sp,
175 lldb::DataBufferSP &header_data_sp,
176 const lldb::ProcessSP &process_sp, lldb::addr_t header_addr);
178 typedef std::vector<elf::ELFProgramHeader> ProgramHeaderColl;
179 typedef ProgramHeaderColl::iterator ProgramHeaderCollIter;
180 typedef ProgramHeaderColl::const_iterator ProgramHeaderCollConstIter;
182 struct ELFSectionHeaderInfo : public elf::ELFSectionHeader {
183 lldb_private::ConstString section_name;
186 typedef std::vector<ELFSectionHeaderInfo> SectionHeaderColl;
187 typedef SectionHeaderColl::iterator SectionHeaderCollIter;
188 typedef SectionHeaderColl::const_iterator SectionHeaderCollConstIter;
190 typedef std::vector<elf::ELFDynamic> DynamicSymbolColl;
191 typedef DynamicSymbolColl::iterator DynamicSymbolCollIter;
192 typedef DynamicSymbolColl::const_iterator DynamicSymbolCollConstIter;
194 typedef std::map<lldb::addr_t, lldb_private::AddressClass>
195 FileAddressToAddressClassMap;
197 /// Version of this reader common to all plugins based on this class.
198 static const uint32_t m_plugin_version = 1;
199 static const uint32_t g_core_uuid_magic;
202 elf::ELFHeader m_header;
205 lldb_private::UUID m_uuid;
207 /// ELF .gnu_debuglink file and crc data if available.
208 std::string m_gnu_debuglink_file;
209 uint32_t m_gnu_debuglink_crc;
211 /// Collection of program headers.
212 ProgramHeaderColl m_program_headers;
214 /// Collection of section headers.
215 SectionHeaderColl m_section_headers;
217 /// Collection of symbols from the dynamic table.
218 DynamicSymbolColl m_dynamic_symbols;
220 /// List of file specifications corresponding to the modules (shared
221 /// libraries) on which this object file depends.
222 mutable std::unique_ptr<lldb_private::FileSpecList> m_filespec_ap;
224 /// Cached value of the entry point for this module.
225 lldb_private::Address m_entry_point_address;
227 /// The architecture detected from parsing elf file contents.
228 lldb_private::ArchSpec m_arch_spec;
230 /// The address class for each symbol in the elf file
231 FileAddressToAddressClassMap m_address_class_map;
233 /// Returns a 1 based index of the given section header.
234 size_t SectionIndex(const SectionHeaderCollIter &I);
236 /// Returns a 1 based index of the given section header.
237 size_t SectionIndex(const SectionHeaderCollConstIter &I) const;
239 // Parses the ELF program headers.
240 static size_t GetProgramHeaderInfo(ProgramHeaderColl &program_headers,
241 lldb_private::DataExtractor &object_data,
242 const elf::ELFHeader &header);
244 // Finds PT_NOTE segments and calculates their crc sum.
246 CalculateELFNotesSegmentsCRC32(const ProgramHeaderColl &program_headers,
247 lldb_private::DataExtractor &data);
249 /// Parses all section headers present in this object file and populates
250 /// m_program_headers. This method will compute the header list only once.
251 /// Returns the number of headers parsed.
252 size_t ParseProgramHeaders();
254 /// Parses all section headers present in this object file and populates
255 /// m_section_headers. This method will compute the header list only once.
256 /// Returns the number of headers parsed.
257 size_t ParseSectionHeaders();
259 static void ParseARMAttributes(lldb_private::DataExtractor &data,
261 lldb_private::ArchSpec &arch_spec);
263 /// Parses the elf section headers and returns the uuid, debug link name,
265 static size_t GetSectionHeaderInfo(SectionHeaderColl §ion_headers,
266 lldb_private::DataExtractor &object_data,
267 const elf::ELFHeader &header,
268 lldb_private::UUID &uuid,
269 std::string &gnu_debuglink_file,
270 uint32_t &gnu_debuglink_crc,
271 lldb_private::ArchSpec &arch_spec);
273 /// Scans the dynamic section and locates all dependent modules (shared
274 /// libraries) populating m_filespec_ap. This method will compute the
275 /// dependent module list only once. Returns the number of dependent
277 size_t ParseDependentModules();
279 /// Parses the dynamic symbol table and populates m_dynamic_symbols. The
280 /// vector retains the order as found in the object file. Returns the
281 /// number of dynamic symbols parsed.
282 size_t ParseDynamicSymbols();
284 /// Populates m_symtab_ap will all non-dynamic linker symbols. This method
285 /// will parse the symbols only once. Returns the number of symbols parsed.
286 unsigned ParseSymbolTable(lldb_private::Symtab *symbol_table,
287 lldb::user_id_t start_id,
288 lldb_private::Section *symtab);
290 /// Helper routine for ParseSymbolTable().
291 unsigned ParseSymbols(lldb_private::Symtab *symbol_table,
292 lldb::user_id_t start_id,
293 lldb_private::SectionList *section_list,
294 const size_t num_symbols,
295 const lldb_private::DataExtractor &symtab_data,
296 const lldb_private::DataExtractor &strtab_data);
298 /// Scans the relocation entries and adds a set of artificial symbols to the
299 /// given symbol table for each PLT slot. Returns the number of symbols
301 unsigned 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 void ParseUnwindSymbols(lldb_private::Symtab *symbol_table,
307 lldb_private::DWARFCallFrameInfo *eh_frame);
309 /// Relocates debug sections
310 unsigned RelocateDebugSections(const elf::ELFSectionHeader *rel_hdr,
311 lldb::user_id_t rel_id,
312 lldb_private::Symtab *thetab);
314 unsigned ApplyRelocations(lldb_private::Symtab *symtab,
315 const elf::ELFHeader *hdr,
316 const elf::ELFSectionHeader *rel_hdr,
317 const elf::ELFSectionHeader *symtab_hdr,
318 const elf::ELFSectionHeader *debug_hdr,
319 lldb_private::DataExtractor &rel_data,
320 lldb_private::DataExtractor &symtab_data,
321 lldb_private::DataExtractor &debug_data,
322 lldb_private::Section *rel_section);
324 /// Loads the section name string table into m_shstr_data. Returns the
325 /// number of bytes constituting the table.
326 size_t GetSectionHeaderStringTable();
328 /// Utility method for looking up a section given its name. Returns the
329 /// index of the corresponding section or zero if no section with the given
330 /// name can be found (note that section indices are always 1 based, and so
331 /// section index 0 is never valid).
332 lldb::user_id_t GetSectionIndexByName(const char *name);
334 // Returns the ID of the first section that has the given type.
335 lldb::user_id_t GetSectionIndexByType(unsigned type);
337 /// Returns the section header with the given id or NULL.
338 const ELFSectionHeaderInfo *GetSectionHeaderByIndex(lldb::user_id_t id);
340 /// @name ELF header dump routines
342 static void DumpELFHeader(lldb_private::Stream *s,
343 const elf::ELFHeader &header);
345 static void DumpELFHeader_e_ident_EI_DATA(lldb_private::Stream *s,
346 unsigned char ei_data);
348 static void DumpELFHeader_e_type(lldb_private::Stream *s,
349 elf::elf_half e_type);
352 /// @name ELF program header dump routines
354 void DumpELFProgramHeaders(lldb_private::Stream *s);
356 static void DumpELFProgramHeader(lldb_private::Stream *s,
357 const elf::ELFProgramHeader &ph);
359 static void DumpELFProgramHeader_p_type(lldb_private::Stream *s,
360 elf::elf_word p_type);
362 static void DumpELFProgramHeader_p_flags(lldb_private::Stream *s,
363 elf::elf_word p_flags);
366 /// @name ELF section header dump routines
368 void DumpELFSectionHeaders(lldb_private::Stream *s);
370 static void DumpELFSectionHeader(lldb_private::Stream *s,
371 const ELFSectionHeaderInfo &sh);
373 static void DumpELFSectionHeader_sh_type(lldb_private::Stream *s,
374 elf::elf_word sh_type);
376 static void DumpELFSectionHeader_sh_flags(lldb_private::Stream *s,
377 elf::elf_xword sh_flags);
380 /// ELF dependent module dump routine.
381 void DumpDependentModules(lldb_private::Stream *s);
383 const elf::ELFDynamic *FindDynamicSymbol(unsigned tag);
385 unsigned PLTRelocationType();
387 static lldb_private::Status
388 RefineModuleDetailsFromNote(lldb_private::DataExtractor &data,
389 lldb_private::ArchSpec &arch_spec,
390 lldb_private::UUID &uuid);
392 bool AnySegmentHasPhysicalAddress();
395 #endif // liblldb_ObjectFileELF_h_