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