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