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