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 21 #include "ELFHeader.h" 22 23 //------------------------------------------------------------------------------ 24 /// @class ObjectFileELF 25 /// @brief Generic ELF object file reader. 26 /// 27 /// This class provides a generic ELF (32/64 bit) reader plugin implementing the 28 /// ObjectFile protocol. 29 class ObjectFileELF : 30 public lldb_private::ObjectFile 31 { 32 public: 33 //------------------------------------------------------------------ 34 // Static Functions 35 //------------------------------------------------------------------ 36 static void 37 Initialize(); 38 39 static void 40 Terminate(); 41 42 static lldb_private::ConstString 43 GetPluginNameStatic(); 44 45 static const char * 46 GetPluginDescriptionStatic(); 47 48 static lldb_private::ObjectFile * 49 CreateInstance(const lldb::ModuleSP &module_sp, 50 lldb::DataBufferSP& data_sp, 51 lldb::offset_t data_offset, 52 const lldb_private::FileSpec* file, 53 lldb::offset_t file_offset, 54 lldb::offset_t length); 55 56 static lldb_private::ObjectFile * 57 CreateMemoryInstance (const lldb::ModuleSP &module_sp, 58 lldb::DataBufferSP& data_sp, 59 const lldb::ProcessSP &process_sp, 60 lldb::addr_t header_addr); 61 62 static size_t 63 GetModuleSpecifications (const lldb_private::FileSpec& file, 64 lldb::DataBufferSP& data_sp, 65 lldb::offset_t data_offset, 66 lldb::offset_t file_offset, 67 lldb::offset_t length, 68 lldb_private::ModuleSpecList &specs); 69 70 static bool 71 MagicBytesMatch (lldb::DataBufferSP& data_sp, 72 lldb::addr_t offset, 73 lldb::addr_t length); 74 75 //------------------------------------------------------------------ 76 // PluginInterface protocol 77 //------------------------------------------------------------------ 78 virtual lldb_private::ConstString 79 GetPluginName(); 80 81 virtual uint32_t 82 GetPluginVersion(); 83 84 //------------------------------------------------------------------ 85 // ObjectFile Protocol. 86 //------------------------------------------------------------------ 87 virtual 88 ~ObjectFileELF(); 89 90 virtual bool 91 ParseHeader(); 92 93 virtual lldb::ByteOrder 94 GetByteOrder() const; 95 96 virtual bool 97 IsExecutable () const; 98 99 virtual uint32_t 100 GetAddressByteSize() const; 101 102 virtual lldb_private::Symtab * 103 GetSymtab(); 104 105 virtual lldb_private::SectionList * 106 GetSectionList(); 107 108 virtual void 109 Dump(lldb_private::Stream *s); 110 111 virtual bool 112 GetArchitecture (lldb_private::ArchSpec &arch); 113 114 virtual bool 115 GetUUID(lldb_private::UUID* uuid); 116 117 virtual uint32_t 118 GetDependentModules(lldb_private::FileSpecList& files); 119 120 virtual lldb_private::Address 121 GetImageInfoAddress(); 122 123 virtual lldb_private::Address 124 GetEntryPointAddress (); 125 126 virtual ObjectFile::Type 127 CalculateType(); 128 129 virtual ObjectFile::Strata 130 CalculateStrata(); 131 132 private: 133 ObjectFileELF(const lldb::ModuleSP &module_sp, 134 lldb::DataBufferSP& data_sp, 135 lldb::offset_t data_offset, 136 const lldb_private::FileSpec* file, 137 lldb::offset_t offset, 138 lldb::offset_t length); 139 140 typedef std::vector<elf::ELFProgramHeader> ProgramHeaderColl; 141 typedef ProgramHeaderColl::iterator ProgramHeaderCollIter; 142 typedef ProgramHeaderColl::const_iterator ProgramHeaderCollConstIter; 143 144 typedef std::vector<elf::ELFSectionHeader> SectionHeaderColl; 145 typedef SectionHeaderColl::iterator SectionHeaderCollIter; 146 typedef SectionHeaderColl::const_iterator SectionHeaderCollConstIter; 147 148 typedef std::vector<elf::ELFDynamic> DynamicSymbolColl; 149 typedef DynamicSymbolColl::iterator DynamicSymbolCollIter; 150 typedef DynamicSymbolColl::const_iterator DynamicSymbolCollConstIter; 151 152 /// Version of this reader common to all plugins based on this class. 153 static const uint32_t m_plugin_version = 1; 154 155 /// ELF file header. 156 elf::ELFHeader m_header; 157 158 /// ELF build ID 159 lldb_private::UUID m_uuid; 160 161 /// Collection of program headers. 162 ProgramHeaderColl m_program_headers; 163 164 /// Collection of section headers. 165 SectionHeaderColl m_section_headers; 166 167 /// Collection of symbols from the dynamic table. 168 DynamicSymbolColl m_dynamic_symbols; 169 170 /// List of file specifications corresponding to the modules (shared 171 /// libraries) on which this object file depends. 172 mutable std::unique_ptr<lldb_private::FileSpecList> m_filespec_ap; 173 174 /// Data extractor holding the string table used to resolve section names. 175 lldb_private::DataExtractor m_shstr_data; 176 177 /// Cached value of the entry point for this module. 178 lldb_private::Address m_entry_point_address; 179 180 /// Returns a 1 based index of the given section header. 181 size_t 182 SectionIndex(const SectionHeaderCollIter &I); 183 184 /// Returns a 1 based index of the given section header. 185 size_t 186 SectionIndex(const SectionHeaderCollConstIter &I) const; 187 188 /// Parses all section headers present in this object file and populates 189 /// m_program_headers. This method will compute the header list only once. 190 /// Returns the number of headers parsed. 191 size_t 192 ParseProgramHeaders(); 193 194 /// Parses all section headers present in this object file and populates 195 /// m_section_headers. This method will compute the header list only once. 196 /// Returns the number of headers parsed. 197 size_t 198 ParseSectionHeaders(); 199 200 /// Scans the dynamic section and locates all dependent modules (shared 201 /// libraries) populating m_filespec_ap. This method will compute the 202 /// dependent module list only once. Returns the number of dependent 203 /// modules parsed. 204 size_t 205 ParseDependentModules(); 206 207 /// Parses the dynamic symbol table and populates m_dynamic_symbols. The 208 /// vector retains the order as found in the object file. Returns the 209 /// number of dynamic symbols parsed. 210 size_t 211 ParseDynamicSymbols(); 212 213 /// Populates m_symtab_ap will all non-dynamic linker symbols. This method 214 /// will parse the symbols only once. Returns the number of symbols parsed. 215 unsigned 216 ParseSymbolTable(lldb_private::Symtab *symbol_table, 217 lldb::user_id_t start_id, 218 const elf::ELFSectionHeader *symtab_section, 219 lldb::user_id_t symtab_id); 220 221 /// Scans the relocation entries and adds a set of artificial symbols to the 222 /// given symbol table for each PLT slot. Returns the number of symbols 223 /// added. 224 unsigned 225 ParseTrampolineSymbols(lldb_private::Symtab *symbol_table, 226 lldb::user_id_t start_id, 227 const elf::ELFSectionHeader *rela_hdr, 228 lldb::user_id_t section_id); 229 230 /// Loads the section name string table into m_shstr_data. Returns the 231 /// number of bytes constituting the table. 232 size_t 233 GetSectionHeaderStringTable(); 234 235 /// Utility method for looking up a section given its name. Returns the 236 /// index of the corresponding section or zero if no section with the given 237 /// name can be found (note that section indices are always 1 based, and so 238 /// section index 0 is never valid). 239 lldb::user_id_t 240 GetSectionIndexByName(const char *name); 241 242 // Returns the ID of the first section that has the given type. 243 lldb::user_id_t 244 GetSectionIndexByType(unsigned type); 245 246 /// Returns the section header with the given id or NULL. 247 const elf::ELFSectionHeader * 248 GetSectionHeaderByIndex(lldb::user_id_t id); 249 250 /// @name ELF header dump routines 251 //@{ 252 static void 253 DumpELFHeader(lldb_private::Stream *s, const elf::ELFHeader& header); 254 255 static void 256 DumpELFHeader_e_ident_EI_DATA(lldb_private::Stream *s, 257 unsigned char ei_data); 258 259 static void 260 DumpELFHeader_e_type(lldb_private::Stream *s, elf::elf_half e_type); 261 //@} 262 263 /// @name ELF program header dump routines 264 //@{ 265 void 266 DumpELFProgramHeaders(lldb_private::Stream *s); 267 268 static void 269 DumpELFProgramHeader(lldb_private::Stream *s, 270 const elf::ELFProgramHeader &ph); 271 272 static void 273 DumpELFProgramHeader_p_type(lldb_private::Stream *s, elf::elf_word p_type); 274 275 static void 276 DumpELFProgramHeader_p_flags(lldb_private::Stream *s, 277 elf::elf_word p_flags); 278 //@} 279 280 /// @name ELF section header dump routines 281 //@{ 282 void 283 DumpELFSectionHeaders(lldb_private::Stream *s); 284 285 static void 286 DumpELFSectionHeader(lldb_private::Stream *s, 287 const elf::ELFSectionHeader& sh); 288 289 static void 290 DumpELFSectionHeader_sh_type(lldb_private::Stream *s, 291 elf::elf_word sh_type); 292 293 static void 294 DumpELFSectionHeader_sh_flags(lldb_private::Stream *s, 295 elf::elf_xword sh_flags); 296 //@} 297 298 /// ELF dependent module dump routine. 299 void 300 DumpDependentModules(lldb_private::Stream *s); 301 302 const elf::ELFDynamic * 303 FindDynamicSymbol(unsigned tag); 304 305 lldb_private::Section * 306 PLTSection(); 307 308 unsigned 309 PLTRelocationType(); 310 }; 311 312 #endif // #ifndef liblldb_ObjectFileELF_h_ 313