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