[memprof] Move YAML traits to MemProf.h (NFC) (#118668)
[llvm-project.git] / lldb / source / Plugins / ObjectFile / ELF / ObjectFileELF.h
blob41b8ce189e41d2d7c838fa196f132244d98b3616
1 //===-- ObjectFileELF.h --------------------------------------- -*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
9 #ifndef LLDB_SOURCE_PLUGINS_OBJECTFILE_ELF_OBJECTFILEELF_H
10 #define LLDB_SOURCE_PLUGINS_OBJECTFILE_ELF_OBJECTFILEELF_H
12 #include <cstdint>
14 #include <optional>
15 #include <vector>
17 #include "lldb/Symbol/ObjectFile.h"
18 #include "lldb/Utility/ArchSpec.h"
19 #include "lldb/Utility/FileSpec.h"
20 #include "lldb/Utility/UUID.h"
21 #include "lldb/lldb-private.h"
23 #include "ELFHeader.h"
25 struct ELFNote {
26 elf::elf_word n_namesz = 0;
27 elf::elf_word n_descsz = 0;
28 elf::elf_word n_type = 0;
30 std::string n_name;
32 ELFNote() = default;
34 /// Parse an ELFNote entry from the given DataExtractor starting at position
35 /// \p offset.
36 ///
37 /// \param[in] data
38 /// The DataExtractor to read from.
39 ///
40 /// \param[in,out] offset
41 /// Pointer to an offset in the data. On return the offset will be
42 /// advanced by the number of bytes read.
43 ///
44 /// \return
45 /// True if the ELFRel entry was successfully read and false otherwise.
46 bool Parse(const lldb_private::DataExtractor &data, lldb::offset_t *offset);
48 size_t GetByteSize() const {
49 return 12 + llvm::alignTo(n_namesz, 4) + llvm::alignTo(n_descsz, 4);
53 /// \class ObjectFileELF
54 /// Generic ELF object file reader.
55 ///
56 /// This class provides a generic ELF (32/64 bit) reader plugin implementing
57 /// the ObjectFile protocol.
58 class ObjectFileELF : public lldb_private::ObjectFile {
59 public:
60 // Static Functions
61 static void Initialize();
63 static void Terminate();
65 static llvm::StringRef GetPluginNameStatic() { return "elf"; }
67 static llvm::StringRef GetPluginDescriptionStatic() {
68 return "ELF object file reader.";
71 static lldb_private::ObjectFile *
72 CreateInstance(const lldb::ModuleSP &module_sp, lldb::DataBufferSP data_sp,
73 lldb::offset_t data_offset, const lldb_private::FileSpec *file,
74 lldb::offset_t file_offset, lldb::offset_t length);
76 static lldb_private::ObjectFile *CreateMemoryInstance(
77 const lldb::ModuleSP &module_sp, lldb::WritableDataBufferSP data_sp,
78 const lldb::ProcessSP &process_sp, lldb::addr_t header_addr);
80 static size_t GetModuleSpecifications(const lldb_private::FileSpec &file,
81 lldb::DataBufferSP &data_sp,
82 lldb::offset_t data_offset,
83 lldb::offset_t file_offset,
84 lldb::offset_t length,
85 lldb_private::ModuleSpecList &specs);
87 static bool MagicBytesMatch(lldb::DataBufferSP &data_sp, lldb::addr_t offset,
88 lldb::addr_t length);
90 // PluginInterface protocol
91 llvm::StringRef GetPluginName() override { return GetPluginNameStatic(); }
93 // LLVM RTTI support
94 static char ID;
95 bool isA(const void *ClassID) const override {
96 return ClassID == &ID || ObjectFile::isA(ClassID);
98 static bool classof(const ObjectFile *obj) { return obj->isA(&ID); }
100 // ObjectFile Protocol.
101 bool ParseHeader() override;
103 bool SetLoadAddress(lldb_private::Target &target, lldb::addr_t value,
104 bool value_is_offset) override;
106 lldb::ByteOrder GetByteOrder() const override;
108 bool IsExecutable() const override;
110 uint32_t GetAddressByteSize() const override;
112 lldb_private::AddressClass GetAddressClass(lldb::addr_t file_addr) override;
114 void ParseSymtab(lldb_private::Symtab &symtab) override;
116 bool IsStripped() override;
118 void CreateSections(lldb_private::SectionList &unified_section_list) override;
120 void Dump(lldb_private::Stream *s) override;
122 lldb_private::ArchSpec GetArchitecture() override;
124 lldb_private::UUID GetUUID() override;
126 /// Return the contents of the .gnu_debuglink section, if the object file
127 /// contains it.
128 std::optional<lldb_private::FileSpec> GetDebugLink();
130 uint32_t GetDependentModules(lldb_private::FileSpecList &files) override;
132 lldb_private::Address
133 GetImageInfoAddress(lldb_private::Target *target) override;
135 lldb_private::Address GetEntryPointAddress() override;
137 lldb_private::Address GetBaseAddress() 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 &section_data) override;
150 llvm::ArrayRef<elf::ELFProgramHeader> ProgramHeaders();
151 lldb_private::DataExtractor GetSegmentData(const elf::ELFProgramHeader &H);
153 llvm::StringRef
154 StripLinkerSymbolAnnotations(llvm::StringRef symbol_name) const override;
156 void RelocateSection(lldb_private::Section *section) override;
158 protected:
160 std::vector<LoadableData>
161 GetLoadableData(lldb_private::Target &target) override;
163 static lldb::WritableDataBufferSP
164 MapFileDataWritable(const lldb_private::FileSpec &file, uint64_t Size,
165 uint64_t Offset);
167 private:
168 ObjectFileELF(const lldb::ModuleSP &module_sp, lldb::DataBufferSP data_sp,
169 lldb::offset_t data_offset, const lldb_private::FileSpec *file,
170 lldb::offset_t offset, lldb::offset_t length);
172 ObjectFileELF(const lldb::ModuleSP &module_sp,
173 lldb::DataBufferSP header_data_sp,
174 const lldb::ProcessSP &process_sp, lldb::addr_t header_addr);
176 typedef std::vector<elf::ELFProgramHeader> ProgramHeaderColl;
178 struct ELFSectionHeaderInfo : public elf::ELFSectionHeader {
179 lldb_private::ConstString section_name;
182 typedef std::vector<ELFSectionHeaderInfo> SectionHeaderColl;
183 typedef SectionHeaderColl::iterator SectionHeaderCollIter;
184 typedef SectionHeaderColl::const_iterator SectionHeaderCollConstIter;
186 struct ELFDynamicWithName {
187 elf::ELFDynamic symbol;
188 std::string name;
190 typedef std::vector<ELFDynamicWithName> DynamicSymbolColl;
191 typedef DynamicSymbolColl::iterator DynamicSymbolCollIter;
192 typedef DynamicSymbolColl::const_iterator DynamicSymbolCollConstIter;
194 /// An ordered map of file address to address class. Used on architectures
195 /// like Arm where there is an alternative ISA mode like Thumb. The container
196 /// is ordered so that it can be binary searched.
197 typedef std::map<lldb::addr_t, lldb_private::AddressClass>
198 FileAddressToAddressClassMap;
200 /// Version of this reader common to all plugins based on this class.
201 static const uint32_t m_plugin_version = 1;
202 static const uint32_t g_core_uuid_magic;
204 /// ELF file header.
205 elf::ELFHeader m_header;
207 /// ELF build ID.
208 lldb_private::UUID m_uuid;
210 /// ELF .gnu_debuglink file and crc data if available.
211 std::string m_gnu_debuglink_file;
212 uint32_t m_gnu_debuglink_crc = 0;
214 /// Collection of program headers.
215 ProgramHeaderColl m_program_headers;
217 /// Collection of section headers.
218 SectionHeaderColl m_section_headers;
220 /// The file address of the .dynamic section. This can be found in the p_vaddr
221 /// of the PT_DYNAMIC program header.
222 lldb::addr_t m_dynamic_base_addr = LLDB_INVALID_ADDRESS;
224 /// Collection of symbols from the dynamic table.
225 DynamicSymbolColl m_dynamic_symbols;
227 /// Object file parsed from .gnu_debugdata section (\sa
228 /// GetGnuDebugDataObjectFile())
229 std::shared_ptr<ObjectFileELF> m_gnu_debug_data_object_file;
231 /// List of file specifications corresponding to the modules (shared
232 /// libraries) on which this object file depends.
233 mutable std::unique_ptr<lldb_private::FileSpecList> m_filespec_up;
235 /// Cached value of the entry point for this module.
236 lldb_private::Address m_entry_point_address;
238 /// The architecture detected from parsing elf file contents.
239 lldb_private::ArchSpec m_arch_spec;
241 /// The address class for each symbol in the elf file
242 FileAddressToAddressClassMap m_address_class_map;
244 /// Returns the index of the given section header.
245 size_t SectionIndex(const SectionHeaderCollIter &I);
247 /// Returns the index of the given section header.
248 size_t SectionIndex(const SectionHeaderCollConstIter &I) const;
250 // Parses the ELF program headers.
251 static size_t GetProgramHeaderInfo(ProgramHeaderColl &program_headers,
252 lldb_private::DataExtractor &object_data,
253 const elf::ELFHeader &header);
255 // Finds PT_NOTE segments and calculates their crc sum.
256 static uint32_t
257 CalculateELFNotesSegmentsCRC32(const ProgramHeaderColl &program_headers,
258 lldb_private::DataExtractor &data);
260 /// Parses all section headers present in this object file and populates
261 /// m_program_headers. This method will compute the header list only once.
262 /// Returns true iff the headers have been successfully parsed.
263 bool ParseProgramHeaders();
265 /// Parses all section headers present in this object file and populates
266 /// m_section_headers. This method will compute the header list only once.
267 /// Returns the number of headers parsed.
268 size_t ParseSectionHeaders();
270 lldb::SectionType GetSectionType(const ELFSectionHeaderInfo &H) const;
272 static void ParseARMAttributes(lldb_private::DataExtractor &data,
273 uint64_t length,
274 lldb_private::ArchSpec &arch_spec);
276 /// Parses the elf section headers and returns the uuid, debug link name,
277 /// crc, archspec.
278 static size_t GetSectionHeaderInfo(SectionHeaderColl &section_headers,
279 lldb_private::DataExtractor &object_data,
280 const elf::ELFHeader &header,
281 lldb_private::UUID &uuid,
282 std::string &gnu_debuglink_file,
283 uint32_t &gnu_debuglink_crc,
284 lldb_private::ArchSpec &arch_spec);
286 /// Scans the dynamic section and locates all dependent modules (shared
287 /// libraries) populating m_filespec_up. This method will compute the
288 /// dependent module list only once. Returns the number of dependent
289 /// modules parsed.
290 size_t ParseDependentModules();
292 /// Parses the dynamic symbol table and populates m_dynamic_symbols. The
293 /// vector retains the order as found in the object file. Returns the
294 /// number of dynamic symbols parsed.
295 size_t ParseDynamicSymbols();
297 /// Populates the symbol table with all non-dynamic linker symbols. This
298 /// method will parse the symbols only once. Returns the number of symbols
299 /// parsed and a map of address types (used by targets like Arm that have
300 /// an alternative ISA mode like Thumb).
301 std::pair<unsigned, FileAddressToAddressClassMap>
302 ParseSymbolTable(lldb_private::Symtab *symbol_table, lldb::user_id_t start_id,
303 lldb_private::Section *symtab);
305 /// Helper routine for ParseSymbolTable().
306 std::pair<unsigned, FileAddressToAddressClassMap>
307 ParseSymbols(lldb_private::Symtab *symbol_table, lldb::user_id_t start_id,
308 lldb_private::SectionList *section_list,
309 const size_t num_symbols,
310 const lldb_private::DataExtractor &symtab_data,
311 const lldb_private::DataExtractor &strtab_data);
313 /// Scans the relocation entries and adds a set of artificial symbols to the
314 /// given symbol table for each PLT slot. Returns the number of symbols
315 /// added.
316 unsigned ParseTrampolineSymbols(lldb_private::Symtab *symbol_table,
317 lldb::user_id_t start_id,
318 const ELFSectionHeaderInfo *rela_hdr,
319 lldb::user_id_t section_id);
321 void ParseUnwindSymbols(lldb_private::Symtab *symbol_table,
322 lldb_private::DWARFCallFrameInfo *eh_frame);
324 /// Relocates debug sections
325 unsigned RelocateDebugSections(const elf::ELFSectionHeader *rel_hdr,
326 lldb::user_id_t rel_id,
327 lldb_private::Symtab *thetab);
329 unsigned ApplyRelocations(lldb_private::Symtab *symtab,
330 const elf::ELFHeader *hdr,
331 const elf::ELFSectionHeader *rel_hdr,
332 const elf::ELFSectionHeader *symtab_hdr,
333 const elf::ELFSectionHeader *debug_hdr,
334 lldb_private::DataExtractor &rel_data,
335 lldb_private::DataExtractor &symtab_data,
336 lldb_private::DataExtractor &debug_data,
337 lldb_private::Section *rel_section);
339 /// Loads the section name string table into m_shstr_data. Returns the
340 /// number of bytes constituting the table.
341 size_t GetSectionHeaderStringTable();
343 /// Utility method for looking up a section given its name. Returns the
344 /// index of the corresponding section or zero if no section with the given
345 /// name can be found (note that section indices are always 1 based, and so
346 /// section index 0 is never valid).
347 lldb::user_id_t GetSectionIndexByName(const char *name);
349 /// Returns the section header with the given id or NULL.
350 const ELFSectionHeaderInfo *GetSectionHeaderByIndex(lldb::user_id_t id);
352 /// \name ELF header dump routines
353 //@{
354 static void DumpELFHeader(lldb_private::Stream *s,
355 const elf::ELFHeader &header);
357 static void DumpELFHeader_e_ident_EI_DATA(lldb_private::Stream *s,
358 unsigned char ei_data);
360 static void DumpELFHeader_e_type(lldb_private::Stream *s,
361 elf::elf_half e_type);
362 //@}
364 /// \name ELF program header dump routines
365 //@{
366 void DumpELFProgramHeaders(lldb_private::Stream *s);
368 static void DumpELFProgramHeader(lldb_private::Stream *s,
369 const elf::ELFProgramHeader &ph);
371 static void DumpELFProgramHeader_p_type(lldb_private::Stream *s,
372 elf::elf_word p_type);
374 static void DumpELFProgramHeader_p_flags(lldb_private::Stream *s,
375 elf::elf_word p_flags);
376 //@}
378 /// \name ELF section header dump routines
379 //@{
380 void DumpELFSectionHeaders(lldb_private::Stream *s);
382 static void DumpELFSectionHeader(lldb_private::Stream *s,
383 const ELFSectionHeaderInfo &sh);
385 static void DumpELFSectionHeader_sh_type(lldb_private::Stream *s,
386 elf::elf_word sh_type);
388 static void DumpELFSectionHeader_sh_flags(lldb_private::Stream *s,
389 elf::elf_xword sh_flags);
390 //@}
392 /// ELF dependent module dump routine.
393 void DumpDependentModules(lldb_private::Stream *s);
395 /// ELF dump the .dynamic section
396 void DumpELFDynamic(lldb_private::Stream *s);
398 const elf::ELFDynamic *FindDynamicSymbol(unsigned tag);
400 unsigned PLTRelocationType();
402 static lldb_private::Status
403 RefineModuleDetailsFromNote(lldb_private::DataExtractor &data,
404 lldb_private::ArchSpec &arch_spec,
405 lldb_private::UUID &uuid);
407 bool AnySegmentHasPhysicalAddress();
409 /// Takes the .gnu_debugdata and returns the decompressed object file that is
410 /// stored within that section.
412 /// \returns either the decompressed object file stored within the
413 /// .gnu_debugdata section or \c nullptr if an error occured or if there's no
414 /// section with that name.
415 std::shared_ptr<ObjectFileELF> GetGnuDebugDataObjectFile();
417 /// Get the bytes that represent the .dynamic section.
419 /// This function will fetch the data for the .dynamic section in an ELF file.
420 /// The PT_DYNAMIC program header will be used to extract the data and this
421 /// function will fall back to using the section headers if PT_DYNAMIC isn't
422 /// found.
424 /// \return The bytes that represent the string table data or \c std::nullopt
425 /// if an error occured.
426 std::optional<lldb_private::DataExtractor> GetDynamicData();
428 /// Get the bytes that represent the dynamic string table data.
430 /// This function will fetch the data for the string table in an ELF file. If
431 /// the ELF file is loaded from a file on disk, it will use the section
432 /// headers to extract the data and fall back to using the DT_STRTAB and
433 /// DT_STRSZ .dynamic entries.
435 /// \return The bytes that represent the string table data or \c std::nullopt
436 /// if an error occured.
437 std::optional<lldb_private::DataExtractor> GetDynstrData();
439 /// Read the bytes pointed to by the \a dyn dynamic entry.
441 /// ELFDynamic::d_ptr values contain file addresses if we load the ELF file
442 /// form a file on disk, or they contain load addresses if they were read
443 /// from memory. This function will correctly extract the data in both cases
444 /// if it is available.
446 /// \param[in] dyn The dynamic entry to use to fetch the data from.
448 /// \param[in] length The number of bytes to read.
450 /// \param[in] offset The number of bytes to skip after the d_ptr value
451 /// before reading data.
453 /// \return The bytes that represent the dynanic entries data or
454 /// \c std::nullopt if an error occured or the data is not available.
455 std::optional<lldb_private::DataExtractor>
456 ReadDataFromDynamic(const elf::ELFDynamic *dyn, uint64_t length,
457 uint64_t offset = 0);
459 /// Get the bytes that represent the dynamic symbol table from the .dynamic
460 /// section from process memory.
462 /// This functon uses the DT_SYMTAB value from the .dynamic section to read
463 /// the symbols table data from process memory. The number of symbols in the
464 /// symbol table is calculated by looking at the DT_HASH or DT_GNU_HASH
465 /// values as the symbol count isn't stored in the .dynamic section.
467 /// \return The bytes that represent the symbol table data from the .dynamic
468 /// section or section headers or \c std::nullopt if an error
469 /// occured or if there is no dynamic symbol data available.
470 std::optional<lldb_private::DataExtractor>
471 GetDynsymDataFromDynamic(uint32_t &num_symbols);
473 /// Get the number of symbols from the DT_HASH dynamic entry.
474 std::optional<uint32_t> GetNumSymbolsFromDynamicHash();
476 /// Get the number of symbols from the DT_GNU_HASH dynamic entry.
477 std::optional<uint32_t> GetNumSymbolsFromDynamicGnuHash();
480 #endif // LLDB_SOURCE_PLUGINS_OBJECTFILE_ELF_OBJECTFILEELF_H