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