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