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 //===----------------------------------------------------------------------===// 8 9 #ifndef LLDB_SOURCE_PLUGINS_OBJECTFILE_ELF_OBJECTFILEELF_H 10 #define LLDB_SOURCE_PLUGINS_OBJECTFILE_ELF_OBJECTFILEELF_H 11 12 #include <cstdint> 13 14 #include <vector> 15 16 #include "lldb/Symbol/ObjectFile.h" 17 #include "lldb/Utility/ArchSpec.h" 18 #include "lldb/Utility/FileSpec.h" 19 #include "lldb/Utility/UUID.h" 20 #include "lldb/lldb-private.h" 21 22 #include "ELFHeader.h" 23 24 struct ELFNote { 25 elf::elf_word n_namesz = 0; 26 elf::elf_word n_descsz = 0; 27 elf::elf_word n_type = 0; 28 29 std::string n_name; 30 31 ELFNote() = default; 32 33 /// Parse an ELFNote entry from the given DataExtractor starting at position 34 /// \p offset. 35 /// 36 /// \param[in] data 37 /// The DataExtractor to read from. 38 /// 39 /// \param[in,out] offset 40 /// Pointer to an offset in the data. On return the offset will be 41 /// advanced by the number of bytes read. 42 /// 43 /// \return 44 /// True if the ELFRel entry was successfully read and false otherwise. 45 bool Parse(const lldb_private::DataExtractor &data, lldb::offset_t *offset); 46 47 size_t GetByteSize() const { 48 return 12 + llvm::alignTo(n_namesz, 4) + llvm::alignTo(n_descsz, 4); 49 } 50 }; 51 52 /// \class ObjectFileELF 53 /// Generic ELF object file reader. 54 /// 55 /// This class provides a generic ELF (32/64 bit) reader plugin implementing 56 /// the ObjectFile protocol. 57 class ObjectFileELF : public lldb_private::ObjectFile { 58 public: 59 // Static Functions 60 static void Initialize(); 61 62 static void Terminate(); 63 64 static llvm::StringRef GetPluginNameStatic() { return "elf"; } 65 66 static llvm::StringRef GetPluginDescriptionStatic() { 67 return "ELF object file reader."; 68 } 69 70 static lldb_private::ObjectFile * 71 CreateInstance(const lldb::ModuleSP &module_sp, lldb::DataBufferSP data_sp, 72 lldb::offset_t data_offset, const lldb_private::FileSpec *file, 73 lldb::offset_t file_offset, lldb::offset_t length); 74 75 static lldb_private::ObjectFile *CreateMemoryInstance( 76 const lldb::ModuleSP &module_sp, lldb::WritableDataBufferSP data_sp, 77 const lldb::ProcessSP &process_sp, lldb::addr_t header_addr); 78 79 static size_t GetModuleSpecifications(const lldb_private::FileSpec &file, 80 lldb::DataBufferSP &data_sp, 81 lldb::offset_t data_offset, 82 lldb::offset_t file_offset, 83 lldb::offset_t length, 84 lldb_private::ModuleSpecList &specs); 85 86 static bool MagicBytesMatch(lldb::DataBufferSP &data_sp, lldb::addr_t offset, 87 lldb::addr_t length); 88 89 // PluginInterface protocol 90 llvm::StringRef GetPluginName() override { return GetPluginNameStatic(); } 91 92 // LLVM RTTI support 93 static char ID; 94 bool isA(const void *ClassID) const override { 95 return ClassID == &ID || ObjectFile::isA(ClassID); 96 } 97 static bool classof(const ObjectFile *obj) { return obj->isA(&ID); } 98 99 // ObjectFile Protocol. 100 bool ParseHeader() override; 101 102 bool SetLoadAddress(lldb_private::Target &target, lldb::addr_t value, 103 bool value_is_offset) override; 104 105 lldb::ByteOrder GetByteOrder() const override; 106 107 bool IsExecutable() const override; 108 109 uint32_t GetAddressByteSize() const override; 110 111 lldb_private::AddressClass GetAddressClass(lldb::addr_t file_addr) override; 112 113 void ParseSymtab(lldb_private::Symtab &symtab) override; 114 115 bool IsStripped() override; 116 117 void CreateSections(lldb_private::SectionList &unified_section_list) override; 118 119 void Dump(lldb_private::Stream *s) override; 120 121 lldb_private::ArchSpec GetArchitecture() override; 122 123 lldb_private::UUID GetUUID() override; 124 125 /// Return the contents of the .gnu_debuglink section, if the object file 126 /// contains it. 127 llvm::Optional<lldb_private::FileSpec> GetDebugLink(); 128 129 uint32_t GetDependentModules(lldb_private::FileSpecList &files) override; 130 131 lldb_private::Address 132 GetImageInfoAddress(lldb_private::Target *target) override; 133 134 lldb_private::Address GetEntryPointAddress() override; 135 136 lldb_private::Address GetBaseAddress() override; 137 138 ObjectFile::Type CalculateType() override; 139 140 ObjectFile::Strata CalculateStrata() override; 141 142 size_t ReadSectionData(lldb_private::Section *section, 143 lldb::offset_t section_offset, void *dst, 144 size_t dst_len) override; 145 146 size_t ReadSectionData(lldb_private::Section *section, 147 lldb_private::DataExtractor §ion_data) override; 148 149 llvm::ArrayRef<elf::ELFProgramHeader> ProgramHeaders(); 150 lldb_private::DataExtractor GetSegmentData(const elf::ELFProgramHeader &H); 151 152 llvm::StringRef 153 StripLinkerSymbolAnnotations(llvm::StringRef symbol_name) const override; 154 155 void RelocateSection(lldb_private::Section *section) override; 156 157 protected: 158 159 std::vector<LoadableData> 160 GetLoadableData(lldb_private::Target &target) override; 161 162 static lldb::WritableDataBufferSP 163 MapFileDataWritable(const lldb_private::FileSpec &file, uint64_t Size, 164 uint64_t Offset); 165 166 private: 167 ObjectFileELF(const lldb::ModuleSP &module_sp, lldb::DataBufferSP data_sp, 168 lldb::offset_t data_offset, const lldb_private::FileSpec *file, 169 lldb::offset_t offset, lldb::offset_t length); 170 171 ObjectFileELF(const lldb::ModuleSP &module_sp, 172 lldb::DataBufferSP header_data_sp, 173 const lldb::ProcessSP &process_sp, lldb::addr_t header_addr); 174 175 typedef std::vector<elf::ELFProgramHeader> ProgramHeaderColl; 176 177 struct ELFSectionHeaderInfo : public elf::ELFSectionHeader { 178 lldb_private::ConstString section_name; 179 }; 180 181 typedef std::vector<ELFSectionHeaderInfo> SectionHeaderColl; 182 typedef SectionHeaderColl::iterator SectionHeaderCollIter; 183 typedef SectionHeaderColl::const_iterator SectionHeaderCollConstIter; 184 185 typedef std::vector<elf::ELFDynamic> DynamicSymbolColl; 186 typedef DynamicSymbolColl::iterator DynamicSymbolCollIter; 187 typedef DynamicSymbolColl::const_iterator DynamicSymbolCollConstIter; 188 189 typedef std::map<lldb::addr_t, lldb_private::AddressClass> 190 FileAddressToAddressClassMap; 191 192 /// Version of this reader common to all plugins based on this class. 193 static const uint32_t m_plugin_version = 1; 194 static const uint32_t g_core_uuid_magic; 195 196 /// ELF file header. 197 elf::ELFHeader m_header; 198 199 /// ELF build ID. 200 lldb_private::UUID m_uuid; 201 202 /// ELF .gnu_debuglink file and crc data if available. 203 std::string m_gnu_debuglink_file; 204 uint32_t m_gnu_debuglink_crc = 0; 205 206 /// Collection of program headers. 207 ProgramHeaderColl m_program_headers; 208 209 /// Collection of section headers. 210 SectionHeaderColl m_section_headers; 211 212 /// Collection of symbols from the dynamic table. 213 DynamicSymbolColl m_dynamic_symbols; 214 215 /// Object file parsed from .gnu_debugdata section (\sa 216 /// GetGnuDebugDataObjectFile()) 217 std::shared_ptr<ObjectFileELF> m_gnu_debug_data_object_file; 218 219 /// List of file specifications corresponding to the modules (shared 220 /// libraries) on which this object file depends. 221 mutable std::unique_ptr<lldb_private::FileSpecList> m_filespec_up; 222 223 /// Cached value of the entry point for this module. 224 lldb_private::Address m_entry_point_address; 225 226 /// The architecture detected from parsing elf file contents. 227 lldb_private::ArchSpec m_arch_spec; 228 229 /// The address class for each symbol in the elf file 230 FileAddressToAddressClassMap m_address_class_map; 231 232 /// Returns the index of the given section header. 233 size_t SectionIndex(const SectionHeaderCollIter &I); 234 235 /// Returns the index of the given section header. 236 size_t SectionIndex(const SectionHeaderCollConstIter &I) const; 237 238 // Parses the ELF program headers. 239 static size_t GetProgramHeaderInfo(ProgramHeaderColl &program_headers, 240 lldb_private::DataExtractor &object_data, 241 const elf::ELFHeader &header); 242 243 // Finds PT_NOTE segments and calculates their crc sum. 244 static uint32_t 245 CalculateELFNotesSegmentsCRC32(const ProgramHeaderColl &program_headers, 246 lldb_private::DataExtractor &data); 247 248 /// Parses all section headers present in this object file and populates 249 /// m_program_headers. This method will compute the header list only once. 250 /// Returns true iff the headers have been successfully parsed. 251 bool ParseProgramHeaders(); 252 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. 256 size_t ParseSectionHeaders(); 257 258 lldb::SectionType GetSectionType(const ELFSectionHeaderInfo &H) const; 259 260 static void ParseARMAttributes(lldb_private::DataExtractor &data, 261 uint64_t length, 262 lldb_private::ArchSpec &arch_spec); 263 264 /// Parses the elf section headers and returns the uuid, debug link name, 265 /// crc, archspec. 266 static size_t GetSectionHeaderInfo(SectionHeaderColl §ion_headers, 267 lldb_private::DataExtractor &object_data, 268 const elf::ELFHeader &header, 269 lldb_private::UUID &uuid, 270 std::string &gnu_debuglink_file, 271 uint32_t &gnu_debuglink_crc, 272 lldb_private::ArchSpec &arch_spec); 273 274 /// Scans the dynamic section and locates all dependent modules (shared 275 /// libraries) populating m_filespec_up. This method will compute the 276 /// dependent module list only once. Returns the number of dependent 277 /// modules parsed. 278 size_t ParseDependentModules(); 279 280 /// Parses the dynamic symbol table and populates m_dynamic_symbols. The 281 /// vector retains the order as found in the object file. Returns the 282 /// number of dynamic symbols parsed. 283 size_t ParseDynamicSymbols(); 284 285 /// Populates the symbol table with all non-dynamic linker symbols. This 286 /// method will parse the symbols only once. Returns the number of symbols 287 /// parsed. 288 unsigned ParseSymbolTable(lldb_private::Symtab *symbol_table, 289 lldb::user_id_t start_id, 290 lldb_private::Section *symtab); 291 292 /// Helper routine for ParseSymbolTable(). 293 unsigned ParseSymbols(lldb_private::Symtab *symbol_table, 294 lldb::user_id_t start_id, 295 lldb_private::SectionList *section_list, 296 const size_t num_symbols, 297 const lldb_private::DataExtractor &symtab_data, 298 const lldb_private::DataExtractor &strtab_data); 299 300 /// Scans the relocation entries and adds a set of artificial symbols to the 301 /// given symbol table for each PLT slot. Returns the number of symbols 302 /// added. 303 unsigned ParseTrampolineSymbols(lldb_private::Symtab *symbol_table, 304 lldb::user_id_t start_id, 305 const ELFSectionHeaderInfo *rela_hdr, 306 lldb::user_id_t section_id); 307 308 void ParseUnwindSymbols(lldb_private::Symtab *symbol_table, 309 lldb_private::DWARFCallFrameInfo *eh_frame); 310 311 /// Relocates debug sections 312 unsigned RelocateDebugSections(const elf::ELFSectionHeader *rel_hdr, 313 lldb::user_id_t rel_id, 314 lldb_private::Symtab *thetab); 315 316 unsigned ApplyRelocations(lldb_private::Symtab *symtab, 317 const elf::ELFHeader *hdr, 318 const elf::ELFSectionHeader *rel_hdr, 319 const elf::ELFSectionHeader *symtab_hdr, 320 const elf::ELFSectionHeader *debug_hdr, 321 lldb_private::DataExtractor &rel_data, 322 lldb_private::DataExtractor &symtab_data, 323 lldb_private::DataExtractor &debug_data, 324 lldb_private::Section *rel_section); 325 326 /// Loads the section name string table into m_shstr_data. Returns the 327 /// number of bytes constituting the table. 328 size_t GetSectionHeaderStringTable(); 329 330 /// Utility method for looking up a section given its name. Returns the 331 /// index of the corresponding section or zero if no section with the given 332 /// name can be found (note that section indices are always 1 based, and so 333 /// section index 0 is never valid). 334 lldb::user_id_t GetSectionIndexByName(const char *name); 335 336 /// Returns the section header with the given id or NULL. 337 const ELFSectionHeaderInfo *GetSectionHeaderByIndex(lldb::user_id_t id); 338 339 /// \name ELF header dump routines 340 //@{ 341 static void DumpELFHeader(lldb_private::Stream *s, 342 const elf::ELFHeader &header); 343 344 static void DumpELFHeader_e_ident_EI_DATA(lldb_private::Stream *s, 345 unsigned char ei_data); 346 347 static void DumpELFHeader_e_type(lldb_private::Stream *s, 348 elf::elf_half e_type); 349 //@} 350 351 /// \name ELF program header dump routines 352 //@{ 353 void DumpELFProgramHeaders(lldb_private::Stream *s); 354 355 static void DumpELFProgramHeader(lldb_private::Stream *s, 356 const elf::ELFProgramHeader &ph); 357 358 static void DumpELFProgramHeader_p_type(lldb_private::Stream *s, 359 elf::elf_word p_type); 360 361 static void DumpELFProgramHeader_p_flags(lldb_private::Stream *s, 362 elf::elf_word p_flags); 363 //@} 364 365 /// \name ELF section header dump routines 366 //@{ 367 void DumpELFSectionHeaders(lldb_private::Stream *s); 368 369 static void DumpELFSectionHeader(lldb_private::Stream *s, 370 const ELFSectionHeaderInfo &sh); 371 372 static void DumpELFSectionHeader_sh_type(lldb_private::Stream *s, 373 elf::elf_word sh_type); 374 375 static void DumpELFSectionHeader_sh_flags(lldb_private::Stream *s, 376 elf::elf_xword sh_flags); 377 //@} 378 379 /// ELF dependent module dump routine. 380 void DumpDependentModules(lldb_private::Stream *s); 381 382 const elf::ELFDynamic *FindDynamicSymbol(unsigned tag); 383 384 unsigned PLTRelocationType(); 385 386 static lldb_private::Status 387 RefineModuleDetailsFromNote(lldb_private::DataExtractor &data, 388 lldb_private::ArchSpec &arch_spec, 389 lldb_private::UUID &uuid); 390 391 bool AnySegmentHasPhysicalAddress(); 392 393 /// Takes the .gnu_debugdata and returns the decompressed object file that is 394 /// stored within that section. 395 /// 396 /// \returns either the decompressed object file stored within the 397 /// .gnu_debugdata section or \c nullptr if an error occured or if there's no 398 /// section with that name. 399 std::shared_ptr<ObjectFileELF> GetGnuDebugDataObjectFile(); 400 }; 401 402 #endif // LLDB_SOURCE_PLUGINS_OBJECTFILE_ELF_OBJECTFILEELF_H 403