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