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