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 liblldb_ObjectFileELF_h_ 10 #define liblldb_ObjectFileELF_h_ 11 12 #include <stdint.h> 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; 26 elf::elf_word n_descsz; 27 elf::elf_word n_type; 28 29 std::string n_name; 30 31 ELFNote() : n_namesz(0), n_descsz(0), n_type(0) {} 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 uint32_t GetPluginVersion() override; 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 lldb_private::Symtab *GetSymtab() 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 lldb_private::FileSpecList GetDebugSymbolFilePaths() override; 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 /// List of file specifications corresponding to the modules (shared 210 /// libraries) on which this object file depends. 211 mutable std::unique_ptr<lldb_private::FileSpecList> m_filespec_up; 212 213 /// Cached value of the entry point for this module. 214 lldb_private::Address m_entry_point_address; 215 216 /// The architecture detected from parsing elf file contents. 217 lldb_private::ArchSpec m_arch_spec; 218 219 /// The address class for each symbol in the elf file 220 FileAddressToAddressClassMap m_address_class_map; 221 222 /// Returns the index of the given section header. 223 size_t SectionIndex(const SectionHeaderCollIter &I); 224 225 /// Returns the index of the given section header. 226 size_t SectionIndex(const SectionHeaderCollConstIter &I) const; 227 228 // Parses the ELF program headers. 229 static size_t GetProgramHeaderInfo(ProgramHeaderColl &program_headers, 230 lldb_private::DataExtractor &object_data, 231 const elf::ELFHeader &header); 232 233 // Finds PT_NOTE segments and calculates their crc sum. 234 static uint32_t 235 CalculateELFNotesSegmentsCRC32(const ProgramHeaderColl &program_headers, 236 lldb_private::DataExtractor &data); 237 238 /// Parses all section headers present in this object file and populates 239 /// m_program_headers. This method will compute the header list only once. 240 /// Returns true iff the headers have been successfully parsed. 241 bool ParseProgramHeaders(); 242 243 /// Parses all section headers present in this object file and populates 244 /// m_section_headers. This method will compute the header list only once. 245 /// Returns the number of headers parsed. 246 size_t ParseSectionHeaders(); 247 248 lldb::SectionType GetSectionType(const ELFSectionHeaderInfo &H) const; 249 250 static void ParseARMAttributes(lldb_private::DataExtractor &data, 251 uint64_t length, 252 lldb_private::ArchSpec &arch_spec); 253 254 /// Parses the elf section headers and returns the uuid, debug link name, 255 /// crc, archspec. 256 static size_t GetSectionHeaderInfo(SectionHeaderColl §ion_headers, 257 lldb_private::DataExtractor &object_data, 258 const elf::ELFHeader &header, 259 lldb_private::UUID &uuid, 260 std::string &gnu_debuglink_file, 261 uint32_t &gnu_debuglink_crc, 262 lldb_private::ArchSpec &arch_spec); 263 264 /// Scans the dynamic section and locates all dependent modules (shared 265 /// libraries) populating m_filespec_up. This method will compute the 266 /// dependent module list only once. Returns the number of dependent 267 /// modules parsed. 268 size_t ParseDependentModules(); 269 270 /// Parses the dynamic symbol table and populates m_dynamic_symbols. The 271 /// vector retains the order as found in the object file. Returns the 272 /// number of dynamic symbols parsed. 273 size_t ParseDynamicSymbols(); 274 275 /// Populates m_symtab_up will all non-dynamic linker symbols. This method 276 /// will parse the symbols only once. Returns the number of symbols parsed. 277 unsigned ParseSymbolTable(lldb_private::Symtab *symbol_table, 278 lldb::user_id_t start_id, 279 lldb_private::Section *symtab); 280 281 /// Helper routine for ParseSymbolTable(). 282 unsigned ParseSymbols(lldb_private::Symtab *symbol_table, 283 lldb::user_id_t start_id, 284 lldb_private::SectionList *section_list, 285 const size_t num_symbols, 286 const lldb_private::DataExtractor &symtab_data, 287 const lldb_private::DataExtractor &strtab_data); 288 289 /// Scans the relocation entries and adds a set of artificial symbols to the 290 /// given symbol table for each PLT slot. Returns the number of symbols 291 /// added. 292 unsigned ParseTrampolineSymbols(lldb_private::Symtab *symbol_table, 293 lldb::user_id_t start_id, 294 const ELFSectionHeaderInfo *rela_hdr, 295 lldb::user_id_t section_id); 296 297 void ParseUnwindSymbols(lldb_private::Symtab *symbol_table, 298 lldb_private::DWARFCallFrameInfo *eh_frame); 299 300 /// Relocates debug sections 301 unsigned RelocateDebugSections(const elf::ELFSectionHeader *rel_hdr, 302 lldb::user_id_t rel_id, 303 lldb_private::Symtab *thetab); 304 305 unsigned ApplyRelocations(lldb_private::Symtab *symtab, 306 const elf::ELFHeader *hdr, 307 const elf::ELFSectionHeader *rel_hdr, 308 const elf::ELFSectionHeader *symtab_hdr, 309 const elf::ELFSectionHeader *debug_hdr, 310 lldb_private::DataExtractor &rel_data, 311 lldb_private::DataExtractor &symtab_data, 312 lldb_private::DataExtractor &debug_data, 313 lldb_private::Section *rel_section); 314 315 /// Loads the section name string table into m_shstr_data. Returns the 316 /// number of bytes constituting the table. 317 size_t GetSectionHeaderStringTable(); 318 319 /// Utility method for looking up a section given its name. Returns the 320 /// index of the corresponding section or zero if no section with the given 321 /// name can be found (note that section indices are always 1 based, and so 322 /// section index 0 is never valid). 323 lldb::user_id_t GetSectionIndexByName(const char *name); 324 325 // Returns the ID of the first section that has the given type. 326 lldb::user_id_t GetSectionIndexByType(unsigned type); 327 328 /// Returns the section header with the given id or NULL. 329 const ELFSectionHeaderInfo *GetSectionHeaderByIndex(lldb::user_id_t id); 330 331 /// \name ELF header dump routines 332 //@{ 333 static void DumpELFHeader(lldb_private::Stream *s, 334 const elf::ELFHeader &header); 335 336 static void DumpELFHeader_e_ident_EI_DATA(lldb_private::Stream *s, 337 unsigned char ei_data); 338 339 static void DumpELFHeader_e_type(lldb_private::Stream *s, 340 elf::elf_half e_type); 341 //@} 342 343 /// \name ELF program header dump routines 344 //@{ 345 void DumpELFProgramHeaders(lldb_private::Stream *s); 346 347 static void DumpELFProgramHeader(lldb_private::Stream *s, 348 const elf::ELFProgramHeader &ph); 349 350 static void DumpELFProgramHeader_p_type(lldb_private::Stream *s, 351 elf::elf_word p_type); 352 353 static void DumpELFProgramHeader_p_flags(lldb_private::Stream *s, 354 elf::elf_word p_flags); 355 //@} 356 357 /// \name ELF section header dump routines 358 //@{ 359 void DumpELFSectionHeaders(lldb_private::Stream *s); 360 361 static void DumpELFSectionHeader(lldb_private::Stream *s, 362 const ELFSectionHeaderInfo &sh); 363 364 static void DumpELFSectionHeader_sh_type(lldb_private::Stream *s, 365 elf::elf_word sh_type); 366 367 static void DumpELFSectionHeader_sh_flags(lldb_private::Stream *s, 368 elf::elf_xword sh_flags); 369 //@} 370 371 /// ELF dependent module dump routine. 372 void DumpDependentModules(lldb_private::Stream *s); 373 374 const elf::ELFDynamic *FindDynamicSymbol(unsigned tag); 375 376 unsigned PLTRelocationType(); 377 378 static lldb_private::Status 379 RefineModuleDetailsFromNote(lldb_private::DataExtractor &data, 380 lldb_private::ArchSpec &arch_spec, 381 lldb_private::UUID &uuid); 382 383 bool AnySegmentHasPhysicalAddress(); 384 }; 385 386 #endif // liblldb_ObjectFileELF_h_ 387