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