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