Stephen Wilson | ddd2962 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 1 | //===-- ObjectFileELF.cpp ------------------------------------- -*- 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 | #include "ObjectFileELF.h" |
| 11 | |
Stephen Wilson | ddd2962 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 12 | #include <cassert> |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 13 | #include <algorithm> |
| 14 | |
Stephen Wilson | f2ad325 | 2011-01-15 00:08:44 +0000 | [diff] [blame] | 15 | #include "lldb/Core/ArchSpec.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 16 | #include "lldb/Core/DataBuffer.h" |
| 17 | #include "lldb/Core/Error.h" |
Stephen Wilson | ddd2962 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 18 | #include "lldb/Core/FileSpecList.h" |
Jim Ingham | 2877594 | 2011-03-07 23:44:08 +0000 | [diff] [blame] | 19 | #include "lldb/Core/Module.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 20 | #include "lldb/Core/PluginManager.h" |
| 21 | #include "lldb/Core/Section.h" |
| 22 | #include "lldb/Core/Stream.h" |
Jim Ingham | 2877594 | 2011-03-07 23:44:08 +0000 | [diff] [blame] | 23 | #include "lldb/Symbol/SymbolContext.h" |
Greg Clayton | 940b103 | 2011-02-23 00:35:02 +0000 | [diff] [blame] | 24 | #include "lldb/Host/Host.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 25 | |
Stephen Wilson | ce47732 | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/PointerUnion.h" |
| 27 | |
Stephen Wilson | ddd2962 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 28 | #define CASE_AND_STREAM(s, def, width) \ |
| 29 | case def: s->Printf("%-*s", width, #def); break; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 30 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 31 | using namespace lldb; |
| 32 | using namespace lldb_private; |
Stephen Wilson | ddd2962 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 33 | using namespace elf; |
| 34 | using namespace llvm::ELF; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 35 | |
Stephen Wilson | ce47732 | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 36 | namespace { |
| 37 | //===----------------------------------------------------------------------===// |
| 38 | /// @class ELFRelocation |
| 39 | /// @brief Generic wrapper for ELFRel and ELFRela. |
| 40 | /// |
| 41 | /// This helper class allows us to parse both ELFRel and ELFRela relocation |
| 42 | /// entries in a generic manner. |
| 43 | class ELFRelocation |
| 44 | { |
| 45 | public: |
| 46 | |
| 47 | /// Constructs an ELFRelocation entry with a personality as given by @p |
| 48 | /// type. |
| 49 | /// |
| 50 | /// @param type Either DT_REL or DT_RELA. Any other value is invalid. |
| 51 | ELFRelocation(unsigned type); |
| 52 | |
| 53 | ~ELFRelocation(); |
| 54 | |
| 55 | bool |
| 56 | Parse(const lldb_private::DataExtractor &data, uint32_t *offset); |
| 57 | |
| 58 | static unsigned |
| 59 | RelocType32(const ELFRelocation &rel); |
| 60 | |
| 61 | static unsigned |
| 62 | RelocType64(const ELFRelocation &rel); |
| 63 | |
| 64 | static unsigned |
| 65 | RelocSymbol32(const ELFRelocation &rel); |
| 66 | |
| 67 | static unsigned |
| 68 | RelocSymbol64(const ELFRelocation &rel); |
| 69 | |
| 70 | private: |
| 71 | typedef llvm::PointerUnion<ELFRel*, ELFRela*> RelocUnion; |
| 72 | |
| 73 | RelocUnion reloc; |
| 74 | }; |
| 75 | |
| 76 | ELFRelocation::ELFRelocation(unsigned type) |
| 77 | { |
| 78 | if (type == DT_REL) |
| 79 | reloc = new ELFRel(); |
| 80 | else if (type == DT_RELA) |
| 81 | reloc = new ELFRela(); |
| 82 | else { |
| 83 | assert(false && "unexpected relocation type"); |
| 84 | reloc = static_cast<ELFRel*>(NULL); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | ELFRelocation::~ELFRelocation() |
| 89 | { |
| 90 | if (reloc.is<ELFRel*>()) |
| 91 | delete reloc.get<ELFRel*>(); |
| 92 | else |
| 93 | delete reloc.get<ELFRela*>(); |
| 94 | } |
| 95 | |
| 96 | bool |
| 97 | ELFRelocation::Parse(const lldb_private::DataExtractor &data, uint32_t *offset) |
| 98 | { |
| 99 | if (reloc.is<ELFRel*>()) |
| 100 | return reloc.get<ELFRel*>()->Parse(data, offset); |
| 101 | else |
| 102 | return reloc.get<ELFRela*>()->Parse(data, offset); |
| 103 | } |
| 104 | |
| 105 | unsigned |
| 106 | ELFRelocation::RelocType32(const ELFRelocation &rel) |
| 107 | { |
| 108 | if (rel.reloc.is<ELFRel*>()) |
| 109 | return ELFRel::RelocType32(*rel.reloc.get<ELFRel*>()); |
| 110 | else |
| 111 | return ELFRela::RelocType32(*rel.reloc.get<ELFRela*>()); |
| 112 | } |
| 113 | |
| 114 | unsigned |
| 115 | ELFRelocation::RelocType64(const ELFRelocation &rel) |
| 116 | { |
| 117 | if (rel.reloc.is<ELFRel*>()) |
| 118 | return ELFRel::RelocType64(*rel.reloc.get<ELFRel*>()); |
| 119 | else |
| 120 | return ELFRela::RelocType64(*rel.reloc.get<ELFRela*>()); |
| 121 | } |
| 122 | |
| 123 | unsigned |
| 124 | ELFRelocation::RelocSymbol32(const ELFRelocation &rel) |
| 125 | { |
| 126 | if (rel.reloc.is<ELFRel*>()) |
| 127 | return ELFRel::RelocSymbol32(*rel.reloc.get<ELFRel*>()); |
| 128 | else |
| 129 | return ELFRela::RelocSymbol32(*rel.reloc.get<ELFRela*>()); |
| 130 | } |
| 131 | |
| 132 | unsigned |
| 133 | ELFRelocation::RelocSymbol64(const ELFRelocation &rel) |
| 134 | { |
| 135 | if (rel.reloc.is<ELFRel*>()) |
| 136 | return ELFRel::RelocSymbol64(*rel.reloc.get<ELFRel*>()); |
| 137 | else |
| 138 | return ELFRela::RelocSymbol64(*rel.reloc.get<ELFRela*>()); |
| 139 | } |
| 140 | |
| 141 | } // end anonymous namespace |
| 142 | |
Stephen Wilson | ddd2962 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 143 | //------------------------------------------------------------------ |
| 144 | // Static methods. |
| 145 | //------------------------------------------------------------------ |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 146 | void |
| 147 | ObjectFileELF::Initialize() |
| 148 | { |
Stephen Wilson | ddd2962 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 149 | PluginManager::RegisterPlugin(GetPluginNameStatic(), |
| 150 | GetPluginDescriptionStatic(), |
Greg Clayton | b5a8f14 | 2012-02-05 02:38:54 +0000 | [diff] [blame] | 151 | CreateInstance, |
| 152 | CreateMemoryInstance); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | void |
| 156 | ObjectFileELF::Terminate() |
| 157 | { |
Stephen Wilson | ddd2962 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 158 | PluginManager::UnregisterPlugin(CreateInstance); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 159 | } |
| 160 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 161 | const char * |
| 162 | ObjectFileELF::GetPluginNameStatic() |
| 163 | { |
Stephen Wilson | ddd2962 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 164 | return "object-file.elf"; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | const char * |
| 168 | ObjectFileELF::GetPluginDescriptionStatic() |
| 169 | { |
Stephen Wilson | ddd2962 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 170 | return "ELF object file reader."; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 171 | } |
| 172 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 173 | ObjectFile * |
Greg Clayton | 3508c38 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 174 | ObjectFileELF::CreateInstance (const lldb::ModuleSP &module_sp, |
| 175 | DataBufferSP &data_sp, |
| 176 | const FileSpec *file, |
| 177 | addr_t offset, |
| 178 | addr_t length) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 179 | { |
Stephen Wilson | ddd2962 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 180 | if (data_sp && data_sp->GetByteSize() > (llvm::ELF::EI_NIDENT + offset)) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 181 | { |
Stephen Wilson | ddd2962 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 182 | const uint8_t *magic = data_sp->GetBytes() + offset; |
| 183 | if (ELFHeader::MagicBytesMatch(magic)) |
| 184 | { |
| 185 | unsigned address_size = ELFHeader::AddressSizeInBytes(magic); |
| 186 | if (address_size == 4 || address_size == 8) |
| 187 | { |
Greg Clayton | 3508c38 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 188 | std::auto_ptr<ObjectFileELF> objfile_ap(new ObjectFileELF(module_sp, data_sp, file, offset, length)); |
Stephen Wilson | f7cc64d | 2011-02-24 19:16:15 +0000 | [diff] [blame] | 189 | ArchSpec spec; |
| 190 | if (objfile_ap->GetArchitecture(spec) && |
| 191 | objfile_ap->SetModulesArchitecture(spec)) |
Stephen Wilson | ddd2962 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 192 | return objfile_ap.release(); |
| 193 | } |
| 194 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 195 | } |
| 196 | return NULL; |
| 197 | } |
| 198 | |
Stephen Wilson | f2ad325 | 2011-01-15 00:08:44 +0000 | [diff] [blame] | 199 | |
Greg Clayton | b5a8f14 | 2012-02-05 02:38:54 +0000 | [diff] [blame] | 200 | ObjectFile* |
Greg Clayton | 3508c38 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 201 | ObjectFileELF::CreateMemoryInstance (const lldb::ModuleSP &module_sp, |
| 202 | DataBufferSP& data_sp, |
| 203 | const lldb::ProcessSP &process_sp, |
| 204 | lldb::addr_t header_addr) |
Greg Clayton | b5a8f14 | 2012-02-05 02:38:54 +0000 | [diff] [blame] | 205 | { |
| 206 | return NULL; |
| 207 | } |
| 208 | |
| 209 | |
Stephen Wilson | ddd2962 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 210 | //------------------------------------------------------------------ |
| 211 | // PluginInterface protocol |
| 212 | //------------------------------------------------------------------ |
| 213 | const char * |
| 214 | ObjectFileELF::GetPluginName() |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 215 | { |
Stephen Wilson | ddd2962 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 216 | return "ObjectFileELF"; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 217 | } |
| 218 | |
Stephen Wilson | ddd2962 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 219 | const char * |
| 220 | ObjectFileELF::GetShortPluginName() |
| 221 | { |
| 222 | return GetPluginNameStatic(); |
| 223 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 224 | |
Stephen Wilson | ddd2962 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 225 | uint32_t |
| 226 | ObjectFileELF::GetPluginVersion() |
| 227 | { |
| 228 | return m_plugin_version; |
| 229 | } |
Stephen Wilson | ddd2962 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 230 | //------------------------------------------------------------------ |
| 231 | // ObjectFile protocol |
| 232 | //------------------------------------------------------------------ |
| 233 | |
Greg Clayton | 3508c38 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 234 | ObjectFileELF::ObjectFileELF (const lldb::ModuleSP &module_sp, |
| 235 | DataBufferSP& dataSP, |
| 236 | const FileSpec* file, |
| 237 | addr_t offset, |
| 238 | addr_t length) : |
| 239 | ObjectFile(module_sp, file, offset, length, dataSP), |
| 240 | m_header(), |
| 241 | m_program_headers(), |
| 242 | m_section_headers(), |
| 243 | m_sections_ap(), |
| 244 | m_symtab_ap(), |
| 245 | m_filespec_ap(), |
| 246 | m_shstr_data() |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 247 | { |
| 248 | if (file) |
| 249 | m_file = *file; |
Stephen Wilson | ddd2962 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 250 | ::memset(&m_header, 0, sizeof(m_header)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 251 | } |
| 252 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 253 | ObjectFileELF::~ObjectFileELF() |
| 254 | { |
| 255 | } |
| 256 | |
Jim Ingham | 7508e73 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 257 | bool |
| 258 | ObjectFileELF::IsExecutable() const |
| 259 | { |
Stephen Wilson | 9098739 | 2011-01-15 00:09:50 +0000 | [diff] [blame] | 260 | return m_header.e_entry != 0; |
Jim Ingham | 7508e73 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 261 | } |
| 262 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 263 | ByteOrder |
Stephen Wilson | ddd2962 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 264 | ObjectFileELF::GetByteOrder() const |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 265 | { |
| 266 | if (m_header.e_ident[EI_DATA] == ELFDATA2MSB) |
| 267 | return eByteOrderBig; |
| 268 | if (m_header.e_ident[EI_DATA] == ELFDATA2LSB) |
| 269 | return eByteOrderLittle; |
| 270 | return eByteOrderInvalid; |
| 271 | } |
| 272 | |
| 273 | size_t |
Stephen Wilson | ddd2962 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 274 | ObjectFileELF::GetAddressByteSize() const |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 275 | { |
| 276 | return m_data.GetAddressByteSize(); |
| 277 | } |
| 278 | |
Stephen Wilson | ddd2962 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 279 | unsigned |
| 280 | ObjectFileELF::SectionIndex(const SectionHeaderCollIter &I) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 281 | { |
Stephen Wilson | ddd2962 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 282 | return std::distance(m_section_headers.begin(), I) + 1; |
| 283 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 284 | |
Stephen Wilson | ddd2962 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 285 | unsigned |
| 286 | ObjectFileELF::SectionIndex(const SectionHeaderCollConstIter &I) const |
| 287 | { |
| 288 | return std::distance(m_section_headers.begin(), I) + 1; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | bool |
Stephen Wilson | ddd2962 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 292 | ObjectFileELF::ParseHeader() |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 293 | { |
Stephen Wilson | ddd2962 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 294 | uint32_t offset = GetOffset(); |
| 295 | return m_header.Parse(m_data, &offset); |
| 296 | } |
| 297 | |
| 298 | bool |
Greg Clayton | 0467c78 | 2011-02-04 18:53:10 +0000 | [diff] [blame] | 299 | ObjectFileELF::GetUUID(lldb_private::UUID* uuid) |
Stephen Wilson | ddd2962 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 300 | { |
| 301 | // FIXME: Return MD5 sum here. See comment in ObjectFile.h. |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 302 | return false; |
| 303 | } |
| 304 | |
| 305 | uint32_t |
Stephen Wilson | ddd2962 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 306 | ObjectFileELF::GetDependentModules(FileSpecList &files) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 307 | { |
Stephen Wilson | ddd2962 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 308 | size_t num_modules = ParseDependentModules(); |
| 309 | uint32_t num_specs = 0; |
| 310 | |
| 311 | for (unsigned i = 0; i < num_modules; ++i) |
| 312 | { |
| 313 | if (files.AppendIfUnique(m_filespec_ap->GetFileSpecAtIndex(i))) |
| 314 | num_specs++; |
| 315 | } |
| 316 | |
| 317 | return num_specs; |
| 318 | } |
| 319 | |
Stephen Wilson | ce47732 | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 320 | user_id_t |
| 321 | ObjectFileELF::GetSectionIndexByType(unsigned type) |
Stephen Wilson | f2ad325 | 2011-01-15 00:08:44 +0000 | [diff] [blame] | 322 | { |
| 323 | if (!ParseSectionHeaders()) |
Stephen Wilson | ce47732 | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 324 | return 0; |
Stephen Wilson | f2ad325 | 2011-01-15 00:08:44 +0000 | [diff] [blame] | 325 | |
Stephen Wilson | f2ad325 | 2011-01-15 00:08:44 +0000 | [diff] [blame] | 326 | for (SectionHeaderCollIter sh_pos = m_section_headers.begin(); |
| 327 | sh_pos != m_section_headers.end(); ++sh_pos) |
| 328 | { |
Stephen Wilson | ce47732 | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 329 | if (sh_pos->sh_type == type) |
| 330 | return SectionIndex(sh_pos); |
Stephen Wilson | f2ad325 | 2011-01-15 00:08:44 +0000 | [diff] [blame] | 331 | } |
| 332 | |
Stephen Wilson | ce47732 | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 333 | return 0; |
| 334 | } |
| 335 | |
| 336 | Address |
| 337 | ObjectFileELF::GetImageInfoAddress() |
| 338 | { |
| 339 | if (!ParseDynamicSymbols()) |
Stephen Wilson | f2ad325 | 2011-01-15 00:08:44 +0000 | [diff] [blame] | 340 | return Address(); |
| 341 | |
| 342 | SectionList *section_list = GetSectionList(); |
| 343 | if (!section_list) |
| 344 | return Address(); |
| 345 | |
Stephen Wilson | ce47732 | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 346 | user_id_t dynsym_id = GetSectionIndexByType(SHT_DYNAMIC); |
| 347 | if (!dynsym_id) |
| 348 | return Address(); |
| 349 | |
| 350 | const ELFSectionHeader *dynsym_hdr = GetSectionHeaderByIndex(dynsym_id); |
| 351 | if (!dynsym_hdr) |
| 352 | return Address(); |
| 353 | |
Greg Clayton | 3508c38 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 354 | SectionSP dynsym_section_sp (section_list->FindSectionByID(dynsym_id)); |
| 355 | if (dynsym_section_sp) |
Stephen Wilson | f2ad325 | 2011-01-15 00:08:44 +0000 | [diff] [blame] | 356 | { |
Greg Clayton | 3508c38 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 357 | for (size_t i = 0; i < m_dynamic_symbols.size(); ++i) |
Stephen Wilson | f2ad325 | 2011-01-15 00:08:44 +0000 | [diff] [blame] | 358 | { |
Greg Clayton | 3508c38 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 359 | ELFDynamic &symbol = m_dynamic_symbols[i]; |
| 360 | |
| 361 | if (symbol.d_tag == DT_DEBUG) |
| 362 | { |
| 363 | // Compute the offset as the number of previous entries plus the |
| 364 | // size of d_tag. |
| 365 | addr_t offset = i * dynsym_hdr->sh_entsize + GetAddressByteSize(); |
| 366 | return Address(dynsym_section_sp, offset); |
| 367 | } |
Stephen Wilson | f2ad325 | 2011-01-15 00:08:44 +0000 | [diff] [blame] | 368 | } |
| 369 | } |
| 370 | |
| 371 | return Address(); |
| 372 | } |
| 373 | |
Jim Ingham | 2877594 | 2011-03-07 23:44:08 +0000 | [diff] [blame] | 374 | lldb_private::Address |
| 375 | ObjectFileELF::GetEntryPointAddress () |
| 376 | { |
Stephen Wilson | 791af3b | 2011-03-08 04:12:15 +0000 | [diff] [blame] | 377 | SectionList *sections; |
| 378 | addr_t offset; |
Jim Ingham | 2877594 | 2011-03-07 23:44:08 +0000 | [diff] [blame] | 379 | |
Stephen Wilson | 791af3b | 2011-03-08 04:12:15 +0000 | [diff] [blame] | 380 | if (m_entry_point_address.IsValid()) |
| 381 | return m_entry_point_address; |
| 382 | |
| 383 | if (!ParseHeader() || !IsExecutable()) |
| 384 | return m_entry_point_address; |
| 385 | |
| 386 | sections = GetSectionList(); |
| 387 | offset = m_header.e_entry; |
| 388 | |
| 389 | if (!sections) |
| 390 | { |
| 391 | m_entry_point_address.SetOffset(offset); |
| 392 | return m_entry_point_address; |
| 393 | } |
| 394 | |
| 395 | m_entry_point_address.ResolveAddressUsingFileSections(offset, sections); |
| 396 | |
| 397 | return m_entry_point_address; |
Jim Ingham | 2877594 | 2011-03-07 23:44:08 +0000 | [diff] [blame] | 398 | } |
| 399 | |
Stephen Wilson | ddd2962 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 400 | //---------------------------------------------------------------------- |
| 401 | // ParseDependentModules |
| 402 | //---------------------------------------------------------------------- |
| 403 | size_t |
| 404 | ObjectFileELF::ParseDependentModules() |
| 405 | { |
| 406 | if (m_filespec_ap.get()) |
| 407 | return m_filespec_ap->GetSize(); |
| 408 | |
| 409 | m_filespec_ap.reset(new FileSpecList()); |
| 410 | |
| 411 | if (!(ParseSectionHeaders() && GetSectionHeaderStringTable())) |
| 412 | return 0; |
| 413 | |
| 414 | // Locate the dynamic table. |
| 415 | user_id_t dynsym_id = 0; |
| 416 | user_id_t dynstr_id = 0; |
Greg Clayton | ad60bf4 | 2010-10-12 02:24:53 +0000 | [diff] [blame] | 417 | for (SectionHeaderCollIter sh_pos = m_section_headers.begin(); |
| 418 | sh_pos != m_section_headers.end(); ++sh_pos) |
Stephen Wilson | ddd2962 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 419 | { |
Greg Clayton | ad60bf4 | 2010-10-12 02:24:53 +0000 | [diff] [blame] | 420 | if (sh_pos->sh_type == SHT_DYNAMIC) |
Stephen Wilson | ddd2962 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 421 | { |
Greg Clayton | ad60bf4 | 2010-10-12 02:24:53 +0000 | [diff] [blame] | 422 | dynsym_id = SectionIndex(sh_pos); |
| 423 | dynstr_id = sh_pos->sh_link + 1; // Section ID's are 1 based. |
Stephen Wilson | ddd2962 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 424 | break; |
| 425 | } |
| 426 | } |
| 427 | |
| 428 | if (!(dynsym_id && dynstr_id)) |
| 429 | return 0; |
| 430 | |
| 431 | SectionList *section_list = GetSectionList(); |
| 432 | if (!section_list) |
| 433 | return 0; |
| 434 | |
| 435 | // Resolve and load the dynamic table entries and corresponding string |
| 436 | // table. |
| 437 | Section *dynsym = section_list->FindSectionByID(dynsym_id).get(); |
| 438 | Section *dynstr = section_list->FindSectionByID(dynstr_id).get(); |
| 439 | if (!(dynsym && dynstr)) |
| 440 | return 0; |
| 441 | |
| 442 | DataExtractor dynsym_data; |
| 443 | DataExtractor dynstr_data; |
Greg Clayton | b5a8f14 | 2012-02-05 02:38:54 +0000 | [diff] [blame] | 444 | if (ReadSectionData(dynsym, dynsym_data) && |
| 445 | ReadSectionData(dynstr, dynstr_data)) |
Stephen Wilson | ddd2962 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 446 | { |
| 447 | ELFDynamic symbol; |
| 448 | const unsigned section_size = dynsym_data.GetByteSize(); |
| 449 | unsigned offset = 0; |
| 450 | |
| 451 | // The only type of entries we are concerned with are tagged DT_NEEDED, |
| 452 | // yielding the name of a required library. |
| 453 | while (offset < section_size) |
| 454 | { |
| 455 | if (!symbol.Parse(dynsym_data, &offset)) |
| 456 | break; |
| 457 | |
| 458 | if (symbol.d_tag != DT_NEEDED) |
| 459 | continue; |
| 460 | |
| 461 | uint32_t str_index = static_cast<uint32_t>(symbol.d_val); |
| 462 | const char *lib_name = dynstr_data.PeekCStr(str_index); |
Greg Clayton | 537a7a8 | 2010-10-20 20:54:39 +0000 | [diff] [blame] | 463 | m_filespec_ap->Append(FileSpec(lib_name, true)); |
Stephen Wilson | ddd2962 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 464 | } |
| 465 | } |
| 466 | |
| 467 | return m_filespec_ap->GetSize(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 468 | } |
| 469 | |
| 470 | //---------------------------------------------------------------------- |
| 471 | // ParseProgramHeaders |
| 472 | //---------------------------------------------------------------------- |
| 473 | size_t |
| 474 | ObjectFileELF::ParseProgramHeaders() |
| 475 | { |
| 476 | // We have already parsed the program headers |
| 477 | if (!m_program_headers.empty()) |
| 478 | return m_program_headers.size(); |
| 479 | |
Stephen Wilson | ddd2962 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 480 | // If there are no program headers to read we are done. |
| 481 | if (m_header.e_phnum == 0) |
| 482 | return 0; |
| 483 | |
| 484 | m_program_headers.resize(m_header.e_phnum); |
| 485 | if (m_program_headers.size() != m_header.e_phnum) |
| 486 | return 0; |
| 487 | |
| 488 | const size_t ph_size = m_header.e_phnum * m_header.e_phentsize; |
Greg Clayton | db2dc2b | 2012-01-12 05:25:17 +0000 | [diff] [blame] | 489 | const elf_off ph_offset = m_header.e_phoff; |
| 490 | DataExtractor data; |
| 491 | if (GetData (ph_offset, ph_size, data) != ph_size) |
Stephen Wilson | ddd2962 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 492 | return 0; |
| 493 | |
Stephen Wilson | ddd2962 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 494 | uint32_t idx; |
| 495 | uint32_t offset; |
| 496 | for (idx = 0, offset = 0; idx < m_header.e_phnum; ++idx) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 497 | { |
Stephen Wilson | ddd2962 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 498 | if (m_program_headers[idx].Parse(data, &offset) == false) |
| 499 | break; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 500 | } |
| 501 | |
Stephen Wilson | ddd2962 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 502 | if (idx < m_program_headers.size()) |
| 503 | m_program_headers.resize(idx); |
| 504 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 505 | return m_program_headers.size(); |
| 506 | } |
| 507 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 508 | //---------------------------------------------------------------------- |
| 509 | // ParseSectionHeaders |
| 510 | //---------------------------------------------------------------------- |
| 511 | size_t |
| 512 | ObjectFileELF::ParseSectionHeaders() |
| 513 | { |
| 514 | // We have already parsed the section headers |
| 515 | if (!m_section_headers.empty()) |
| 516 | return m_section_headers.size(); |
| 517 | |
Stephen Wilson | ddd2962 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 518 | // If there are no section headers we are done. |
| 519 | if (m_header.e_shnum == 0) |
| 520 | return 0; |
| 521 | |
| 522 | m_section_headers.resize(m_header.e_shnum); |
| 523 | if (m_section_headers.size() != m_header.e_shnum) |
| 524 | return 0; |
| 525 | |
| 526 | const size_t sh_size = m_header.e_shnum * m_header.e_shentsize; |
Greg Clayton | db2dc2b | 2012-01-12 05:25:17 +0000 | [diff] [blame] | 527 | const elf_off sh_offset = m_header.e_shoff; |
| 528 | DataExtractor data; |
| 529 | if (GetData (sh_offset, sh_size, data) != sh_size) |
Stephen Wilson | ddd2962 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 530 | return 0; |
| 531 | |
Stephen Wilson | ddd2962 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 532 | uint32_t idx; |
| 533 | uint32_t offset; |
| 534 | for (idx = 0, offset = 0; idx < m_header.e_shnum; ++idx) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 535 | { |
Stephen Wilson | ddd2962 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 536 | if (m_section_headers[idx].Parse(data, &offset) == false) |
| 537 | break; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 538 | } |
Stephen Wilson | ddd2962 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 539 | if (idx < m_section_headers.size()) |
| 540 | m_section_headers.resize(idx); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 541 | |
| 542 | return m_section_headers.size(); |
| 543 | } |
| 544 | |
| 545 | size_t |
| 546 | ObjectFileELF::GetSectionHeaderStringTable() |
| 547 | { |
| 548 | if (m_shstr_data.GetByteSize() == 0) |
| 549 | { |
Stephen Wilson | ddd2962 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 550 | const unsigned strtab_idx = m_header.e_shstrndx; |
| 551 | |
| 552 | if (strtab_idx && strtab_idx < m_section_headers.size()) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 553 | { |
Stephen Wilson | ddd2962 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 554 | const ELFSectionHeader &sheader = m_section_headers[strtab_idx]; |
| 555 | const size_t byte_size = sheader.sh_size; |
Greg Clayton | db2dc2b | 2012-01-12 05:25:17 +0000 | [diff] [blame] | 556 | const Elf64_Off offset = sheader.sh_offset; |
| 557 | m_shstr_data.SetData (m_data, offset, byte_size); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 558 | |
Greg Clayton | db2dc2b | 2012-01-12 05:25:17 +0000 | [diff] [blame] | 559 | if (m_shstr_data.GetByteSize() != byte_size) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 560 | return 0; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 561 | } |
| 562 | } |
| 563 | return m_shstr_data.GetByteSize(); |
| 564 | } |
| 565 | |
Stephen Wilson | ddd2962 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 566 | lldb::user_id_t |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 567 | ObjectFileELF::GetSectionIndexByName(const char *name) |
| 568 | { |
Stephen Wilson | ddd2962 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 569 | if (!(ParseSectionHeaders() && GetSectionHeaderStringTable())) |
| 570 | return 0; |
| 571 | |
| 572 | // Search the collection of section headers for one with a matching name. |
| 573 | for (SectionHeaderCollIter I = m_section_headers.begin(); |
| 574 | I != m_section_headers.end(); ++I) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 575 | { |
Stephen Wilson | ddd2962 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 576 | const char *sectionName = m_shstr_data.PeekCStr(I->sh_name); |
| 577 | |
| 578 | if (!sectionName) |
| 579 | return 0; |
| 580 | |
| 581 | if (strcmp(name, sectionName) != 0) |
| 582 | continue; |
| 583 | |
| 584 | return SectionIndex(I); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 585 | } |
| 586 | |
Stephen Wilson | ddd2962 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 587 | return 0; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 588 | } |
| 589 | |
Stephen Wilson | ce47732 | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 590 | const elf::ELFSectionHeader * |
| 591 | ObjectFileELF::GetSectionHeaderByIndex(lldb::user_id_t id) |
| 592 | { |
| 593 | if (!ParseSectionHeaders() || !id) |
| 594 | return NULL; |
| 595 | |
| 596 | if (--id < m_section_headers.size()) |
| 597 | return &m_section_headers[id]; |
| 598 | |
| 599 | return NULL; |
| 600 | } |
| 601 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 602 | SectionList * |
| 603 | ObjectFileELF::GetSectionList() |
| 604 | { |
Stephen Wilson | ddd2962 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 605 | if (m_sections_ap.get()) |
| 606 | return m_sections_ap.get(); |
| 607 | |
| 608 | if (ParseSectionHeaders() && GetSectionHeaderStringTable()) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 609 | { |
| 610 | m_sections_ap.reset(new SectionList()); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 611 | |
Stephen Wilson | ddd2962 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 612 | for (SectionHeaderCollIter I = m_section_headers.begin(); |
| 613 | I != m_section_headers.end(); ++I) |
| 614 | { |
| 615 | const ELFSectionHeader &header = *I; |
| 616 | |
| 617 | ConstString name(m_shstr_data.PeekCStr(header.sh_name)); |
Greg Clayton | 23d90ae | 2012-03-27 02:40:46 +0000 | [diff] [blame] | 618 | const uint64_t file_size = header.sh_type == SHT_NOBITS ? 0 : header.sh_size; |
| 619 | const uint64_t vm_size = header.sh_flags & SHF_ALLOC ? header.sh_size : 0; |
Stephen Wilson | ddd2962 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 620 | |
Greg Clayton | 32a8c7e | 2010-07-21 22:54:26 +0000 | [diff] [blame] | 621 | static ConstString g_sect_name_text (".text"); |
| 622 | static ConstString g_sect_name_data (".data"); |
| 623 | static ConstString g_sect_name_bss (".bss"); |
Greg Clayton | 9ab696e | 2012-03-27 21:10:07 +0000 | [diff] [blame] | 624 | static ConstString g_sect_name_tdata (".tdata"); |
| 625 | static ConstString g_sect_name_tbss (".tbss"); |
Greg Clayton | 32a8c7e | 2010-07-21 22:54:26 +0000 | [diff] [blame] | 626 | static ConstString g_sect_name_dwarf_debug_abbrev (".debug_abbrev"); |
| 627 | static ConstString g_sect_name_dwarf_debug_aranges (".debug_aranges"); |
| 628 | static ConstString g_sect_name_dwarf_debug_frame (".debug_frame"); |
| 629 | static ConstString g_sect_name_dwarf_debug_info (".debug_info"); |
| 630 | static ConstString g_sect_name_dwarf_debug_line (".debug_line"); |
| 631 | static ConstString g_sect_name_dwarf_debug_loc (".debug_loc"); |
| 632 | static ConstString g_sect_name_dwarf_debug_macinfo (".debug_macinfo"); |
| 633 | static ConstString g_sect_name_dwarf_debug_pubnames (".debug_pubnames"); |
| 634 | static ConstString g_sect_name_dwarf_debug_pubtypes (".debug_pubtypes"); |
| 635 | static ConstString g_sect_name_dwarf_debug_ranges (".debug_ranges"); |
| 636 | static ConstString g_sect_name_dwarf_debug_str (".debug_str"); |
| 637 | static ConstString g_sect_name_eh_frame (".eh_frame"); |
| 638 | |
| 639 | SectionType sect_type = eSectionTypeOther; |
| 640 | |
Greg Clayton | 9ab696e | 2012-03-27 21:10:07 +0000 | [diff] [blame] | 641 | bool is_thread_specific = false; |
| 642 | |
Greg Clayton | 32a8c7e | 2010-07-21 22:54:26 +0000 | [diff] [blame] | 643 | if (name == g_sect_name_text) sect_type = eSectionTypeCode; |
| 644 | else if (name == g_sect_name_data) sect_type = eSectionTypeData; |
| 645 | else if (name == g_sect_name_bss) sect_type = eSectionTypeZeroFill; |
Greg Clayton | 9ab696e | 2012-03-27 21:10:07 +0000 | [diff] [blame] | 646 | else if (name == g_sect_name_tdata) |
| 647 | { |
| 648 | sect_type = eSectionTypeData; |
| 649 | is_thread_specific = true; |
| 650 | } |
| 651 | else if (name == g_sect_name_tbss) |
| 652 | { |
| 653 | sect_type = eSectionTypeZeroFill; |
| 654 | is_thread_specific = true; |
| 655 | } |
Greg Clayton | 32a8c7e | 2010-07-21 22:54:26 +0000 | [diff] [blame] | 656 | else if (name == g_sect_name_dwarf_debug_abbrev) sect_type = eSectionTypeDWARFDebugAbbrev; |
| 657 | else if (name == g_sect_name_dwarf_debug_aranges) sect_type = eSectionTypeDWARFDebugAranges; |
| 658 | else if (name == g_sect_name_dwarf_debug_frame) sect_type = eSectionTypeDWARFDebugFrame; |
| 659 | else if (name == g_sect_name_dwarf_debug_info) sect_type = eSectionTypeDWARFDebugInfo; |
| 660 | else if (name == g_sect_name_dwarf_debug_line) sect_type = eSectionTypeDWARFDebugLine; |
| 661 | else if (name == g_sect_name_dwarf_debug_loc) sect_type = eSectionTypeDWARFDebugLoc; |
| 662 | else if (name == g_sect_name_dwarf_debug_macinfo) sect_type = eSectionTypeDWARFDebugMacInfo; |
| 663 | else if (name == g_sect_name_dwarf_debug_pubnames) sect_type = eSectionTypeDWARFDebugPubNames; |
| 664 | else if (name == g_sect_name_dwarf_debug_pubtypes) sect_type = eSectionTypeDWARFDebugPubTypes; |
| 665 | else if (name == g_sect_name_dwarf_debug_ranges) sect_type = eSectionTypeDWARFDebugRanges; |
| 666 | else if (name == g_sect_name_dwarf_debug_str) sect_type = eSectionTypeDWARFDebugStr; |
| 667 | else if (name == g_sect_name_eh_frame) sect_type = eSectionTypeEHFrame; |
| 668 | |
| 669 | |
Greg Clayton | 9ab696e | 2012-03-27 21:10:07 +0000 | [diff] [blame] | 670 | SectionSP section_sp(new Section( |
Greg Clayton | 32a8c7e | 2010-07-21 22:54:26 +0000 | [diff] [blame] | 671 | GetModule(), // Module to which this section belongs. |
| 672 | SectionIndex(I), // Section ID. |
| 673 | name, // Section name. |
| 674 | sect_type, // Section type. |
| 675 | header.sh_addr, // VM address. |
Greg Clayton | 23d90ae | 2012-03-27 02:40:46 +0000 | [diff] [blame] | 676 | vm_size, // VM size in bytes of this section. |
Greg Clayton | 32a8c7e | 2010-07-21 22:54:26 +0000 | [diff] [blame] | 677 | header.sh_offset, // Offset of this section in the file. |
Greg Clayton | 23d90ae | 2012-03-27 02:40:46 +0000 | [diff] [blame] | 678 | file_size, // Size of the section as found in the file. |
Greg Clayton | 32a8c7e | 2010-07-21 22:54:26 +0000 | [diff] [blame] | 679 | header.sh_flags)); // Flags for this section. |
Stephen Wilson | ddd2962 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 680 | |
Greg Clayton | 9ab696e | 2012-03-27 21:10:07 +0000 | [diff] [blame] | 681 | if (is_thread_specific) |
| 682 | section_sp->SetIsThreadSpecific (is_thread_specific); |
| 683 | m_sections_ap->AddSection(section_sp); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 684 | } |
| 685 | } |
Stephen Wilson | ddd2962 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 686 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 687 | return m_sections_ap.get(); |
| 688 | } |
| 689 | |
Stephen Wilson | ce47732 | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 690 | static unsigned |
| 691 | ParseSymbols(Symtab *symtab, |
| 692 | user_id_t start_id, |
| 693 | SectionList *section_list, |
| 694 | const ELFSectionHeader *symtab_shdr, |
Stephen Wilson | ddd2962 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 695 | const DataExtractor &symtab_data, |
| 696 | const DataExtractor &strtab_data) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 697 | { |
Stephen Wilson | ddd2962 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 698 | ELFSymbol symbol; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 699 | uint32_t offset = 0; |
Stephen Wilson | ce47732 | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 700 | const unsigned num_symbols = |
| 701 | symtab_data.GetByteSize() / symtab_shdr->sh_entsize; |
Stephen Wilson | ddd2962 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 702 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 703 | static ConstString text_section_name(".text"); |
| 704 | static ConstString init_section_name(".init"); |
| 705 | static ConstString fini_section_name(".fini"); |
| 706 | static ConstString ctors_section_name(".ctors"); |
| 707 | static ConstString dtors_section_name(".dtors"); |
| 708 | |
| 709 | static ConstString data_section_name(".data"); |
| 710 | static ConstString rodata_section_name(".rodata"); |
| 711 | static ConstString rodata1_section_name(".rodata1"); |
| 712 | static ConstString data2_section_name(".data1"); |
| 713 | static ConstString bss_section_name(".bss"); |
| 714 | |
Stephen Wilson | ce47732 | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 715 | unsigned i; |
| 716 | for (i = 0; i < num_symbols; ++i) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 717 | { |
Stephen Wilson | ddd2962 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 718 | if (symbol.Parse(symtab_data, &offset) == false) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 719 | break; |
| 720 | |
Greg Clayton | 3508c38 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 721 | SectionSP symbol_section_sp; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 722 | SymbolType symbol_type = eSymbolTypeInvalid; |
Stephen Wilson | ddd2962 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 723 | Elf64_Half symbol_idx = symbol.st_shndx; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 724 | |
Stephen Wilson | ddd2962 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 725 | switch (symbol_idx) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 726 | { |
| 727 | case SHN_ABS: |
| 728 | symbol_type = eSymbolTypeAbsolute; |
| 729 | break; |
| 730 | case SHN_UNDEF: |
| 731 | symbol_type = eSymbolTypeUndefined; |
| 732 | break; |
| 733 | default: |
Greg Clayton | 3508c38 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 734 | symbol_section_sp = section_list->GetSectionAtIndex(symbol_idx); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 735 | break; |
| 736 | } |
| 737 | |
Stephen Wilson | ddd2962 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 738 | switch (symbol.getType()) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 739 | { |
| 740 | default: |
| 741 | case STT_NOTYPE: |
| 742 | // The symbol's type is not specified. |
| 743 | break; |
| 744 | |
| 745 | case STT_OBJECT: |
Stephen Wilson | ddd2962 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 746 | // The symbol is associated with a data object, such as a variable, |
| 747 | // an array, etc. |
| 748 | symbol_type = eSymbolTypeData; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 749 | break; |
| 750 | |
| 751 | case STT_FUNC: |
| 752 | // The symbol is associated with a function or other executable code. |
Stephen Wilson | ddd2962 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 753 | symbol_type = eSymbolTypeCode; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 754 | break; |
| 755 | |
| 756 | case STT_SECTION: |
| 757 | // The symbol is associated with a section. Symbol table entries of |
| 758 | // this type exist primarily for relocation and normally have |
| 759 | // STB_LOCAL binding. |
| 760 | break; |
| 761 | |
| 762 | case STT_FILE: |
| 763 | // Conventionally, the symbol's name gives the name of the source |
| 764 | // file associated with the object file. A file symbol has STB_LOCAL |
| 765 | // binding, its section index is SHN_ABS, and it precedes the other |
| 766 | // STB_LOCAL symbols for the file, if it is present. |
Stephen Wilson | ddd2962 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 767 | symbol_type = eSymbolTypeObjectFile; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 768 | break; |
| 769 | } |
| 770 | |
| 771 | if (symbol_type == eSymbolTypeInvalid) |
| 772 | { |
Greg Clayton | 3508c38 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 773 | if (symbol_section_sp) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 774 | { |
Greg Clayton | 3508c38 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 775 | const ConstString §_name = symbol_section_sp->GetName(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 776 | if (sect_name == text_section_name || |
| 777 | sect_name == init_section_name || |
| 778 | sect_name == fini_section_name || |
| 779 | sect_name == ctors_section_name || |
| 780 | sect_name == dtors_section_name) |
| 781 | { |
| 782 | symbol_type = eSymbolTypeCode; |
| 783 | } |
Stephen Wilson | ddd2962 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 784 | else if (sect_name == data_section_name || |
| 785 | sect_name == data2_section_name || |
| 786 | sect_name == rodata_section_name || |
| 787 | sect_name == rodata1_section_name || |
| 788 | sect_name == bss_section_name) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 789 | { |
| 790 | symbol_type = eSymbolTypeData; |
| 791 | } |
| 792 | } |
| 793 | } |
| 794 | |
| 795 | uint64_t symbol_value = symbol.st_value; |
Greg Clayton | 3508c38 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 796 | if (symbol_section_sp) |
| 797 | symbol_value -= symbol_section_sp->GetFileAddress(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 798 | const char *symbol_name = strtab_data.PeekCStr(symbol.st_name); |
Stephen Wilson | ddd2962 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 799 | bool is_global = symbol.getBinding() == STB_GLOBAL; |
| 800 | uint32_t flags = symbol.st_other << 8 | symbol.st_info; |
Greg Clayton | 23d90ae | 2012-03-27 02:40:46 +0000 | [diff] [blame] | 801 | bool is_mangled = symbol_name ? (symbol_name[0] == '_' && symbol_name[1] == 'Z') : false; |
Stephen Wilson | ddd2962 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 802 | Symbol dc_symbol( |
Greg Clayton | 3508c38 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 803 | i + start_id, // ID is the original symbol table index. |
| 804 | symbol_name, // Symbol name. |
Greg Clayton | 23d90ae | 2012-03-27 02:40:46 +0000 | [diff] [blame] | 805 | is_mangled, // Is the symbol name mangled? |
Greg Clayton | 3508c38 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 806 | symbol_type, // Type of this symbol |
| 807 | is_global, // Is this globally visible? |
| 808 | false, // Is this symbol debug info? |
| 809 | false, // Is this symbol a trampoline? |
| 810 | false, // Is this symbol artificial? |
| 811 | symbol_section_sp, // Section in which this symbol is defined or null. |
| 812 | symbol_value, // Offset in section or symbol value. |
| 813 | symbol.st_size, // Size in bytes of this symbol. |
| 814 | flags); // Symbol flags. |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 815 | symtab->AddSymbol(dc_symbol); |
| 816 | } |
Stephen Wilson | ce47732 | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 817 | |
| 818 | return i; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 819 | } |
| 820 | |
Stephen Wilson | ce47732 | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 821 | unsigned |
| 822 | ObjectFileELF::ParseSymbolTable(Symtab *symbol_table, user_id_t start_id, |
| 823 | const ELFSectionHeader *symtab_hdr, |
Stephen Wilson | ddd2962 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 824 | user_id_t symtab_id) |
| 825 | { |
Stephen Wilson | ce47732 | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 826 | assert(symtab_hdr->sh_type == SHT_SYMTAB || |
| 827 | symtab_hdr->sh_type == SHT_DYNSYM); |
Stephen Wilson | ddd2962 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 828 | |
| 829 | // Parse in the section list if needed. |
| 830 | SectionList *section_list = GetSectionList(); |
| 831 | if (!section_list) |
Stephen Wilson | ce47732 | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 832 | return 0; |
Stephen Wilson | ddd2962 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 833 | |
| 834 | // Section ID's are ones based. |
Stephen Wilson | ce47732 | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 835 | user_id_t strtab_id = symtab_hdr->sh_link + 1; |
Stephen Wilson | ddd2962 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 836 | |
| 837 | Section *symtab = section_list->FindSectionByID(symtab_id).get(); |
| 838 | Section *strtab = section_list->FindSectionByID(strtab_id).get(); |
Stephen Wilson | ce47732 | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 839 | unsigned num_symbols = 0; |
Stephen Wilson | ddd2962 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 840 | if (symtab && strtab) |
| 841 | { |
| 842 | DataExtractor symtab_data; |
| 843 | DataExtractor strtab_data; |
Greg Clayton | b5a8f14 | 2012-02-05 02:38:54 +0000 | [diff] [blame] | 844 | if (ReadSectionData(symtab, symtab_data) && |
| 845 | ReadSectionData(strtab, strtab_data)) |
Stephen Wilson | ddd2962 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 846 | { |
Stephen Wilson | ce47732 | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 847 | num_symbols = ParseSymbols(symbol_table, start_id, |
| 848 | section_list, symtab_hdr, |
| 849 | symtab_data, strtab_data); |
Stephen Wilson | ddd2962 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 850 | } |
| 851 | } |
Stephen Wilson | ce47732 | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 852 | |
| 853 | return num_symbols; |
| 854 | } |
| 855 | |
| 856 | size_t |
| 857 | ObjectFileELF::ParseDynamicSymbols() |
| 858 | { |
| 859 | if (m_dynamic_symbols.size()) |
| 860 | return m_dynamic_symbols.size(); |
| 861 | |
| 862 | user_id_t dyn_id = GetSectionIndexByType(SHT_DYNAMIC); |
| 863 | if (!dyn_id) |
Bill Wendling | 8c1b382 | 2012-04-03 07:50:11 +0000 | [diff] [blame] | 864 | return 0; |
Stephen Wilson | ce47732 | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 865 | |
| 866 | SectionList *section_list = GetSectionList(); |
| 867 | if (!section_list) |
Bill Wendling | 8c1b382 | 2012-04-03 07:50:11 +0000 | [diff] [blame] | 868 | return 0; |
Stephen Wilson | ce47732 | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 869 | |
| 870 | Section *dynsym = section_list->FindSectionByID(dyn_id).get(); |
| 871 | if (!dynsym) |
Bill Wendling | 8c1b382 | 2012-04-03 07:50:11 +0000 | [diff] [blame] | 872 | return 0; |
Stephen Wilson | ce47732 | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 873 | |
| 874 | ELFDynamic symbol; |
| 875 | DataExtractor dynsym_data; |
Greg Clayton | b5a8f14 | 2012-02-05 02:38:54 +0000 | [diff] [blame] | 876 | if (ReadSectionData(dynsym, dynsym_data)) |
Stephen Wilson | ce47732 | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 877 | { |
| 878 | |
| 879 | const unsigned section_size = dynsym_data.GetByteSize(); |
| 880 | unsigned offset = 0; |
| 881 | unsigned cursor = 0; |
| 882 | |
| 883 | while (cursor < section_size) |
| 884 | { |
| 885 | offset = cursor; |
| 886 | if (!symbol.Parse(dynsym_data, &cursor)) |
| 887 | break; |
| 888 | |
| 889 | m_dynamic_symbols.push_back(symbol); |
| 890 | } |
| 891 | } |
| 892 | |
| 893 | return m_dynamic_symbols.size(); |
| 894 | } |
| 895 | |
| 896 | const ELFDynamic * |
| 897 | ObjectFileELF::FindDynamicSymbol(unsigned tag) |
| 898 | { |
| 899 | if (!ParseDynamicSymbols()) |
| 900 | return NULL; |
| 901 | |
| 902 | SectionList *section_list = GetSectionList(); |
| 903 | if (!section_list) |
| 904 | return 0; |
| 905 | |
| 906 | DynamicSymbolCollIter I = m_dynamic_symbols.begin(); |
| 907 | DynamicSymbolCollIter E = m_dynamic_symbols.end(); |
| 908 | for ( ; I != E; ++I) |
| 909 | { |
| 910 | ELFDynamic *symbol = &*I; |
| 911 | |
| 912 | if (symbol->d_tag == tag) |
| 913 | return symbol; |
| 914 | } |
| 915 | |
| 916 | return NULL; |
| 917 | } |
| 918 | |
| 919 | Section * |
| 920 | ObjectFileELF::PLTSection() |
| 921 | { |
| 922 | const ELFDynamic *symbol = FindDynamicSymbol(DT_JMPREL); |
| 923 | SectionList *section_list = GetSectionList(); |
| 924 | |
| 925 | if (symbol && section_list) |
| 926 | { |
| 927 | addr_t addr = symbol->d_ptr; |
| 928 | return section_list->FindSectionContainingFileAddress(addr).get(); |
| 929 | } |
| 930 | |
| 931 | return NULL; |
| 932 | } |
| 933 | |
| 934 | unsigned |
| 935 | ObjectFileELF::PLTRelocationType() |
| 936 | { |
| 937 | const ELFDynamic *symbol = FindDynamicSymbol(DT_PLTREL); |
| 938 | |
| 939 | if (symbol) |
| 940 | return symbol->d_val; |
| 941 | |
| 942 | return 0; |
| 943 | } |
| 944 | |
| 945 | static unsigned |
| 946 | ParsePLTRelocations(Symtab *symbol_table, |
| 947 | user_id_t start_id, |
| 948 | unsigned rel_type, |
| 949 | const ELFHeader *hdr, |
| 950 | const ELFSectionHeader *rel_hdr, |
| 951 | const ELFSectionHeader *plt_hdr, |
| 952 | const ELFSectionHeader *sym_hdr, |
Greg Clayton | 3508c38 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 953 | const lldb::SectionSP &plt_section_sp, |
Stephen Wilson | ce47732 | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 954 | DataExtractor &rel_data, |
| 955 | DataExtractor &symtab_data, |
| 956 | DataExtractor &strtab_data) |
| 957 | { |
| 958 | ELFRelocation rel(rel_type); |
| 959 | ELFSymbol symbol; |
| 960 | uint32_t offset = 0; |
| 961 | const unsigned plt_entsize = plt_hdr->sh_entsize; |
| 962 | const unsigned num_relocations = rel_hdr->sh_size / rel_hdr->sh_entsize; |
| 963 | |
| 964 | typedef unsigned (*reloc_info_fn)(const ELFRelocation &rel); |
| 965 | reloc_info_fn reloc_type; |
| 966 | reloc_info_fn reloc_symbol; |
| 967 | |
| 968 | if (hdr->Is32Bit() == 4) |
| 969 | { |
| 970 | reloc_type = ELFRelocation::RelocType32; |
| 971 | reloc_symbol = ELFRelocation::RelocSymbol32; |
| 972 | } |
| 973 | else |
| 974 | { |
| 975 | reloc_type = ELFRelocation::RelocType64; |
| 976 | reloc_symbol = ELFRelocation::RelocSymbol64; |
| 977 | } |
| 978 | |
| 979 | unsigned slot_type = hdr->GetRelocationJumpSlotType(); |
| 980 | unsigned i; |
| 981 | for (i = 0; i < num_relocations; ++i) |
| 982 | { |
| 983 | if (rel.Parse(rel_data, &offset) == false) |
| 984 | break; |
| 985 | |
| 986 | if (reloc_type(rel) != slot_type) |
| 987 | continue; |
| 988 | |
| 989 | unsigned symbol_offset = reloc_symbol(rel) * sym_hdr->sh_entsize; |
| 990 | uint64_t plt_index = (i + 1) * plt_entsize; |
| 991 | |
| 992 | if (!symbol.Parse(symtab_data, &symbol_offset)) |
| 993 | break; |
| 994 | |
| 995 | const char *symbol_name = strtab_data.PeekCStr(symbol.st_name); |
Greg Clayton | 23d90ae | 2012-03-27 02:40:46 +0000 | [diff] [blame] | 996 | bool is_mangled = symbol_name ? (symbol_name[0] == '_' && symbol_name[1] == 'Z') : false; |
Stephen Wilson | ce47732 | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 997 | |
| 998 | Symbol jump_symbol( |
| 999 | i + start_id, // Symbol table index |
| 1000 | symbol_name, // symbol name. |
Greg Clayton | 23d90ae | 2012-03-27 02:40:46 +0000 | [diff] [blame] | 1001 | is_mangled, // is the symbol name mangled? |
Stephen Wilson | ce47732 | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 1002 | eSymbolTypeTrampoline, // Type of this symbol |
| 1003 | false, // Is this globally visible? |
| 1004 | false, // Is this symbol debug info? |
| 1005 | true, // Is this symbol a trampoline? |
| 1006 | true, // Is this symbol artificial? |
Greg Clayton | 3508c38 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 1007 | plt_section_sp, // Section in which this symbol is defined or null. |
Stephen Wilson | ce47732 | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 1008 | plt_index, // Offset in section or symbol value. |
| 1009 | plt_entsize, // Size in bytes of this symbol. |
| 1010 | 0); // Symbol flags. |
| 1011 | |
| 1012 | symbol_table->AddSymbol(jump_symbol); |
| 1013 | } |
| 1014 | |
| 1015 | return i; |
| 1016 | } |
| 1017 | |
| 1018 | unsigned |
| 1019 | ObjectFileELF::ParseTrampolineSymbols(Symtab *symbol_table, |
| 1020 | user_id_t start_id, |
| 1021 | const ELFSectionHeader *rel_hdr, |
| 1022 | user_id_t rel_id) |
| 1023 | { |
| 1024 | assert(rel_hdr->sh_type == SHT_RELA || rel_hdr->sh_type == SHT_REL); |
| 1025 | |
| 1026 | // The link field points to the asscoiated symbol table. The info field |
| 1027 | // points to the section holding the plt. |
| 1028 | user_id_t symtab_id = rel_hdr->sh_link; |
| 1029 | user_id_t plt_id = rel_hdr->sh_info; |
| 1030 | |
| 1031 | if (!symtab_id || !plt_id) |
| 1032 | return 0; |
| 1033 | |
| 1034 | // Section ID's are ones based; |
| 1035 | symtab_id++; |
| 1036 | plt_id++; |
| 1037 | |
| 1038 | const ELFSectionHeader *plt_hdr = GetSectionHeaderByIndex(plt_id); |
| 1039 | if (!plt_hdr) |
| 1040 | return 0; |
| 1041 | |
| 1042 | const ELFSectionHeader *sym_hdr = GetSectionHeaderByIndex(symtab_id); |
| 1043 | if (!sym_hdr) |
| 1044 | return 0; |
| 1045 | |
| 1046 | SectionList *section_list = GetSectionList(); |
| 1047 | if (!section_list) |
| 1048 | return 0; |
| 1049 | |
| 1050 | Section *rel_section = section_list->FindSectionByID(rel_id).get(); |
| 1051 | if (!rel_section) |
| 1052 | return 0; |
| 1053 | |
Greg Clayton | 3508c38 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 1054 | SectionSP plt_section_sp (section_list->FindSectionByID(plt_id)); |
| 1055 | if (!plt_section_sp) |
Stephen Wilson | ce47732 | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 1056 | return 0; |
| 1057 | |
| 1058 | Section *symtab = section_list->FindSectionByID(symtab_id).get(); |
| 1059 | if (!symtab) |
| 1060 | return 0; |
| 1061 | |
| 1062 | Section *strtab = section_list->FindSectionByID(sym_hdr->sh_link + 1).get(); |
| 1063 | if (!strtab) |
| 1064 | return 0; |
| 1065 | |
| 1066 | DataExtractor rel_data; |
Greg Clayton | b5a8f14 | 2012-02-05 02:38:54 +0000 | [diff] [blame] | 1067 | if (!ReadSectionData(rel_section, rel_data)) |
Stephen Wilson | ce47732 | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 1068 | return 0; |
| 1069 | |
| 1070 | DataExtractor symtab_data; |
Greg Clayton | b5a8f14 | 2012-02-05 02:38:54 +0000 | [diff] [blame] | 1071 | if (!ReadSectionData(symtab, symtab_data)) |
Stephen Wilson | ce47732 | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 1072 | return 0; |
| 1073 | |
| 1074 | DataExtractor strtab_data; |
Greg Clayton | b5a8f14 | 2012-02-05 02:38:54 +0000 | [diff] [blame] | 1075 | if (!ReadSectionData(strtab, strtab_data)) |
Stephen Wilson | ce47732 | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 1076 | return 0; |
| 1077 | |
| 1078 | unsigned rel_type = PLTRelocationType(); |
| 1079 | if (!rel_type) |
| 1080 | return 0; |
| 1081 | |
Greg Clayton | 3508c38 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 1082 | return ParsePLTRelocations (symbol_table, |
| 1083 | start_id, |
| 1084 | rel_type, |
| 1085 | &m_header, |
| 1086 | rel_hdr, |
| 1087 | plt_hdr, |
| 1088 | sym_hdr, |
| 1089 | plt_section_sp, |
| 1090 | rel_data, |
| 1091 | symtab_data, |
| 1092 | strtab_data); |
Stephen Wilson | ddd2962 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 1093 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1094 | |
| 1095 | Symtab * |
| 1096 | ObjectFileELF::GetSymtab() |
| 1097 | { |
Stephen Wilson | ddd2962 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 1098 | if (m_symtab_ap.get()) |
| 1099 | return m_symtab_ap.get(); |
| 1100 | |
| 1101 | Symtab *symbol_table = new Symtab(this); |
| 1102 | m_symtab_ap.reset(symbol_table); |
| 1103 | |
Stephen Wilson | ce47732 | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 1104 | Mutex::Locker locker(symbol_table->GetMutex()); |
Greg Clayton | 8d3802d | 2010-10-08 04:20:14 +0000 | [diff] [blame] | 1105 | |
Stephen Wilson | ddd2962 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 1106 | if (!(ParseSectionHeaders() && GetSectionHeaderStringTable())) |
| 1107 | return symbol_table; |
| 1108 | |
| 1109 | // Locate and parse all linker symbol tables. |
Stephen Wilson | ce47732 | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 1110 | uint64_t symbol_id = 0; |
Stephen Wilson | ddd2962 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 1111 | for (SectionHeaderCollIter I = m_section_headers.begin(); |
| 1112 | I != m_section_headers.end(); ++I) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1113 | { |
Peter Collingbourne | 182c286 | 2011-06-03 20:39:58 +0000 | [diff] [blame] | 1114 | if (I->sh_type == SHT_SYMTAB || I->sh_type == SHT_DYNSYM) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1115 | { |
Stephen Wilson | ce47732 | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 1116 | const ELFSectionHeader &symtab_header = *I; |
Stephen Wilson | ddd2962 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 1117 | user_id_t section_id = SectionIndex(I); |
Stephen Wilson | ce47732 | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 1118 | symbol_id += ParseSymbolTable(symbol_table, symbol_id, |
| 1119 | &symtab_header, section_id); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1120 | } |
| 1121 | } |
Stephen Wilson | ce47732 | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 1122 | |
| 1123 | // Synthesize trampoline symbols to help navigate the PLT. |
| 1124 | Section *reloc_section = PLTSection(); |
| 1125 | if (reloc_section) |
| 1126 | { |
| 1127 | user_id_t reloc_id = reloc_section->GetID(); |
| 1128 | const ELFSectionHeader *reloc_header = GetSectionHeaderByIndex(reloc_id); |
| 1129 | assert(reloc_header); |
| 1130 | |
| 1131 | ParseTrampolineSymbols(symbol_table, symbol_id, reloc_header, reloc_id); |
| 1132 | } |
Stephen Wilson | ddd2962 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 1133 | |
| 1134 | return symbol_table; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1135 | } |
| 1136 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1137 | //===----------------------------------------------------------------------===// |
| 1138 | // Dump |
| 1139 | // |
| 1140 | // Dump the specifics of the runtime file container (such as any headers |
| 1141 | // segments, sections, etc). |
| 1142 | //---------------------------------------------------------------------- |
| 1143 | void |
| 1144 | ObjectFileELF::Dump(Stream *s) |
| 1145 | { |
| 1146 | DumpELFHeader(s, m_header); |
| 1147 | s->EOL(); |
| 1148 | DumpELFProgramHeaders(s); |
| 1149 | s->EOL(); |
| 1150 | DumpELFSectionHeaders(s); |
| 1151 | s->EOL(); |
| 1152 | SectionList *section_list = GetSectionList(); |
| 1153 | if (section_list) |
Greg Clayton | 58e844b | 2010-12-08 05:08:21 +0000 | [diff] [blame] | 1154 | section_list->Dump(s, NULL, true, UINT32_MAX); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1155 | Symtab *symtab = GetSymtab(); |
| 1156 | if (symtab) |
Greg Clayton | b344843 | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 1157 | symtab->Dump(s, NULL, eSortOrderNone); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1158 | s->EOL(); |
Stephen Wilson | ddd2962 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 1159 | DumpDependentModules(s); |
| 1160 | s->EOL(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1161 | } |
| 1162 | |
| 1163 | //---------------------------------------------------------------------- |
| 1164 | // DumpELFHeader |
| 1165 | // |
| 1166 | // Dump the ELF header to the specified output stream |
| 1167 | //---------------------------------------------------------------------- |
| 1168 | void |
Stephen Wilson | ddd2962 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 1169 | ObjectFileELF::DumpELFHeader(Stream *s, const ELFHeader &header) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1170 | { |
Stephen Wilson | ddd2962 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 1171 | s->PutCString("ELF Header\n"); |
| 1172 | s->Printf("e_ident[EI_MAG0 ] = 0x%2.2x\n", header.e_ident[EI_MAG0]); |
| 1173 | s->Printf("e_ident[EI_MAG1 ] = 0x%2.2x '%c'\n", |
| 1174 | header.e_ident[EI_MAG1], header.e_ident[EI_MAG1]); |
| 1175 | s->Printf("e_ident[EI_MAG2 ] = 0x%2.2x '%c'\n", |
| 1176 | header.e_ident[EI_MAG2], header.e_ident[EI_MAG2]); |
| 1177 | s->Printf("e_ident[EI_MAG3 ] = 0x%2.2x '%c'\n", |
| 1178 | header.e_ident[EI_MAG3], header.e_ident[EI_MAG3]); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1179 | |
Stephen Wilson | ddd2962 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 1180 | s->Printf("e_ident[EI_CLASS ] = 0x%2.2x\n", header.e_ident[EI_CLASS]); |
| 1181 | s->Printf("e_ident[EI_DATA ] = 0x%2.2x ", header.e_ident[EI_DATA]); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1182 | DumpELFHeader_e_ident_EI_DATA(s, header.e_ident[EI_DATA]); |
| 1183 | s->Printf ("\ne_ident[EI_VERSION] = 0x%2.2x\n", header.e_ident[EI_VERSION]); |
| 1184 | s->Printf ("e_ident[EI_PAD ] = 0x%2.2x\n", header.e_ident[EI_PAD]); |
| 1185 | |
| 1186 | s->Printf("e_type = 0x%4.4x ", header.e_type); |
| 1187 | DumpELFHeader_e_type(s, header.e_type); |
| 1188 | s->Printf("\ne_machine = 0x%4.4x\n", header.e_machine); |
| 1189 | s->Printf("e_version = 0x%8.8x\n", header.e_version); |
Jason Molenda | 7e5fa7f | 2011-09-20 21:44:10 +0000 | [diff] [blame] | 1190 | s->Printf("e_entry = 0x%8.8llx\n", header.e_entry); |
| 1191 | s->Printf("e_phoff = 0x%8.8llx\n", header.e_phoff); |
| 1192 | s->Printf("e_shoff = 0x%8.8llx\n", header.e_shoff); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1193 | s->Printf("e_flags = 0x%8.8x\n", header.e_flags); |
| 1194 | s->Printf("e_ehsize = 0x%4.4x\n", header.e_ehsize); |
| 1195 | s->Printf("e_phentsize = 0x%4.4x\n", header.e_phentsize); |
| 1196 | s->Printf("e_phnum = 0x%4.4x\n", header.e_phnum); |
| 1197 | s->Printf("e_shentsize = 0x%4.4x\n", header.e_shentsize); |
| 1198 | s->Printf("e_shnum = 0x%4.4x\n", header.e_shnum); |
| 1199 | s->Printf("e_shstrndx = 0x%4.4x\n", header.e_shstrndx); |
| 1200 | } |
| 1201 | |
| 1202 | //---------------------------------------------------------------------- |
| 1203 | // DumpELFHeader_e_type |
| 1204 | // |
| 1205 | // Dump an token value for the ELF header member e_type |
| 1206 | //---------------------------------------------------------------------- |
| 1207 | void |
Stephen Wilson | ddd2962 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 1208 | ObjectFileELF::DumpELFHeader_e_type(Stream *s, elf_half e_type) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1209 | { |
| 1210 | switch (e_type) |
| 1211 | { |
| 1212 | case ET_NONE: *s << "ET_NONE"; break; |
| 1213 | case ET_REL: *s << "ET_REL"; break; |
| 1214 | case ET_EXEC: *s << "ET_EXEC"; break; |
| 1215 | case ET_DYN: *s << "ET_DYN"; break; |
| 1216 | case ET_CORE: *s << "ET_CORE"; break; |
| 1217 | default: |
| 1218 | break; |
| 1219 | } |
| 1220 | } |
| 1221 | |
| 1222 | //---------------------------------------------------------------------- |
| 1223 | // DumpELFHeader_e_ident_EI_DATA |
| 1224 | // |
| 1225 | // Dump an token value for the ELF header member e_ident[EI_DATA] |
| 1226 | //---------------------------------------------------------------------- |
| 1227 | void |
Stephen Wilson | ddd2962 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 1228 | ObjectFileELF::DumpELFHeader_e_ident_EI_DATA(Stream *s, unsigned char ei_data) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1229 | { |
| 1230 | switch (ei_data) |
| 1231 | { |
| 1232 | case ELFDATANONE: *s << "ELFDATANONE"; break; |
| 1233 | case ELFDATA2LSB: *s << "ELFDATA2LSB - Little Endian"; break; |
| 1234 | case ELFDATA2MSB: *s << "ELFDATA2MSB - Big Endian"; break; |
| 1235 | default: |
| 1236 | break; |
| 1237 | } |
| 1238 | } |
| 1239 | |
| 1240 | |
| 1241 | //---------------------------------------------------------------------- |
| 1242 | // DumpELFProgramHeader |
| 1243 | // |
| 1244 | // Dump a single ELF program header to the specified output stream |
| 1245 | //---------------------------------------------------------------------- |
| 1246 | void |
Stephen Wilson | ddd2962 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 1247 | ObjectFileELF::DumpELFProgramHeader(Stream *s, const ELFProgramHeader &ph) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1248 | { |
| 1249 | DumpELFProgramHeader_p_type(s, ph.p_type); |
Jason Molenda | 7e5fa7f | 2011-09-20 21:44:10 +0000 | [diff] [blame] | 1250 | s->Printf(" %8.8llx %8.8llx %8.8llx", ph.p_offset, ph.p_vaddr, ph.p_paddr); |
| 1251 | s->Printf(" %8.8llx %8.8llx %8.8x (", ph.p_filesz, ph.p_memsz, ph.p_flags); |
Stephen Wilson | ddd2962 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 1252 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1253 | DumpELFProgramHeader_p_flags(s, ph.p_flags); |
Jason Molenda | 7e5fa7f | 2011-09-20 21:44:10 +0000 | [diff] [blame] | 1254 | s->Printf(") %8.8llx", ph.p_align); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1255 | } |
| 1256 | |
| 1257 | //---------------------------------------------------------------------- |
| 1258 | // DumpELFProgramHeader_p_type |
| 1259 | // |
| 1260 | // Dump an token value for the ELF program header member p_type which |
| 1261 | // describes the type of the program header |
Stephen Wilson | ddd2962 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 1262 | // ---------------------------------------------------------------------- |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1263 | void |
Stephen Wilson | ddd2962 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 1264 | ObjectFileELF::DumpELFProgramHeader_p_type(Stream *s, elf_word p_type) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1265 | { |
| 1266 | const int kStrWidth = 10; |
| 1267 | switch (p_type) |
| 1268 | { |
| 1269 | CASE_AND_STREAM(s, PT_NULL , kStrWidth); |
| 1270 | CASE_AND_STREAM(s, PT_LOAD , kStrWidth); |
| 1271 | CASE_AND_STREAM(s, PT_DYNAMIC , kStrWidth); |
| 1272 | CASE_AND_STREAM(s, PT_INTERP , kStrWidth); |
| 1273 | CASE_AND_STREAM(s, PT_NOTE , kStrWidth); |
| 1274 | CASE_AND_STREAM(s, PT_SHLIB , kStrWidth); |
| 1275 | CASE_AND_STREAM(s, PT_PHDR , kStrWidth); |
| 1276 | default: |
| 1277 | s->Printf("0x%8.8x%*s", p_type, kStrWidth - 10, ""); |
| 1278 | break; |
| 1279 | } |
| 1280 | } |
| 1281 | |
| 1282 | |
| 1283 | //---------------------------------------------------------------------- |
| 1284 | // DumpELFProgramHeader_p_flags |
| 1285 | // |
| 1286 | // Dump an token value for the ELF program header member p_flags |
| 1287 | //---------------------------------------------------------------------- |
| 1288 | void |
Stephen Wilson | ddd2962 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 1289 | ObjectFileELF::DumpELFProgramHeader_p_flags(Stream *s, elf_word p_flags) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1290 | { |
| 1291 | *s << ((p_flags & PF_X) ? "PF_X" : " ") |
| 1292 | << (((p_flags & PF_X) && (p_flags & PF_W)) ? '+' : ' ') |
| 1293 | << ((p_flags & PF_W) ? "PF_W" : " ") |
| 1294 | << (((p_flags & PF_W) && (p_flags & PF_R)) ? '+' : ' ') |
| 1295 | << ((p_flags & PF_R) ? "PF_R" : " "); |
| 1296 | } |
| 1297 | |
| 1298 | //---------------------------------------------------------------------- |
| 1299 | // DumpELFProgramHeaders |
| 1300 | // |
| 1301 | // Dump all of the ELF program header to the specified output stream |
| 1302 | //---------------------------------------------------------------------- |
| 1303 | void |
| 1304 | ObjectFileELF::DumpELFProgramHeaders(Stream *s) |
| 1305 | { |
| 1306 | if (ParseProgramHeaders()) |
| 1307 | { |
| 1308 | s->PutCString("Program Headers\n"); |
Stephen Wilson | ddd2962 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 1309 | s->PutCString("IDX p_type p_offset p_vaddr p_paddr " |
| 1310 | "p_filesz p_memsz p_flags p_align\n"); |
| 1311 | s->PutCString("==== ---------- -------- -------- -------- " |
| 1312 | "-------- -------- ------------------------- --------\n"); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1313 | |
| 1314 | uint32_t idx = 0; |
Stephen Wilson | ddd2962 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 1315 | for (ProgramHeaderCollConstIter I = m_program_headers.begin(); |
| 1316 | I != m_program_headers.end(); ++I, ++idx) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1317 | { |
Stephen Wilson | ddd2962 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 1318 | s->Printf("[%2u] ", idx); |
| 1319 | ObjectFileELF::DumpELFProgramHeader(s, *I); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1320 | s->EOL(); |
| 1321 | } |
| 1322 | } |
| 1323 | } |
| 1324 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1325 | //---------------------------------------------------------------------- |
| 1326 | // DumpELFSectionHeader |
| 1327 | // |
| 1328 | // Dump a single ELF section header to the specified output stream |
| 1329 | //---------------------------------------------------------------------- |
| 1330 | void |
Stephen Wilson | ddd2962 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 1331 | ObjectFileELF::DumpELFSectionHeader(Stream *s, const ELFSectionHeader &sh) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1332 | { |
Stephen Wilson | ddd2962 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 1333 | s->Printf("%8.8x ", sh.sh_name); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1334 | DumpELFSectionHeader_sh_type(s, sh.sh_type); |
Jason Molenda | 7e5fa7f | 2011-09-20 21:44:10 +0000 | [diff] [blame] | 1335 | s->Printf(" %8.8llx (", sh.sh_flags); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1336 | DumpELFSectionHeader_sh_flags(s, sh.sh_flags); |
Jason Molenda | 7e5fa7f | 2011-09-20 21:44:10 +0000 | [diff] [blame] | 1337 | s->Printf(") %8.8llx %8.8llx %8.8llx", sh.sh_addr, sh.sh_offset, sh.sh_size); |
Stephen Wilson | ddd2962 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 1338 | s->Printf(" %8.8x %8.8x", sh.sh_link, sh.sh_info); |
Jason Molenda | 7e5fa7f | 2011-09-20 21:44:10 +0000 | [diff] [blame] | 1339 | s->Printf(" %8.8llx %8.8llx", sh.sh_addralign, sh.sh_entsize); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1340 | } |
| 1341 | |
| 1342 | //---------------------------------------------------------------------- |
| 1343 | // DumpELFSectionHeader_sh_type |
| 1344 | // |
| 1345 | // Dump an token value for the ELF section header member sh_type which |
| 1346 | // describes the type of the section |
| 1347 | //---------------------------------------------------------------------- |
| 1348 | void |
Stephen Wilson | ddd2962 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 1349 | ObjectFileELF::DumpELFSectionHeader_sh_type(Stream *s, elf_word sh_type) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1350 | { |
| 1351 | const int kStrWidth = 12; |
| 1352 | switch (sh_type) |
| 1353 | { |
| 1354 | CASE_AND_STREAM(s, SHT_NULL , kStrWidth); |
| 1355 | CASE_AND_STREAM(s, SHT_PROGBITS , kStrWidth); |
| 1356 | CASE_AND_STREAM(s, SHT_SYMTAB , kStrWidth); |
| 1357 | CASE_AND_STREAM(s, SHT_STRTAB , kStrWidth); |
| 1358 | CASE_AND_STREAM(s, SHT_RELA , kStrWidth); |
| 1359 | CASE_AND_STREAM(s, SHT_HASH , kStrWidth); |
| 1360 | CASE_AND_STREAM(s, SHT_DYNAMIC , kStrWidth); |
| 1361 | CASE_AND_STREAM(s, SHT_NOTE , kStrWidth); |
| 1362 | CASE_AND_STREAM(s, SHT_NOBITS , kStrWidth); |
| 1363 | CASE_AND_STREAM(s, SHT_REL , kStrWidth); |
| 1364 | CASE_AND_STREAM(s, SHT_SHLIB , kStrWidth); |
| 1365 | CASE_AND_STREAM(s, SHT_DYNSYM , kStrWidth); |
| 1366 | CASE_AND_STREAM(s, SHT_LOPROC , kStrWidth); |
| 1367 | CASE_AND_STREAM(s, SHT_HIPROC , kStrWidth); |
| 1368 | CASE_AND_STREAM(s, SHT_LOUSER , kStrWidth); |
| 1369 | CASE_AND_STREAM(s, SHT_HIUSER , kStrWidth); |
| 1370 | default: |
| 1371 | s->Printf("0x%8.8x%*s", sh_type, kStrWidth - 10, ""); |
| 1372 | break; |
| 1373 | } |
| 1374 | } |
| 1375 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1376 | //---------------------------------------------------------------------- |
| 1377 | // DumpELFSectionHeader_sh_flags |
| 1378 | // |
| 1379 | // Dump an token value for the ELF section header member sh_flags |
| 1380 | //---------------------------------------------------------------------- |
| 1381 | void |
Stephen Wilson | ddd2962 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 1382 | ObjectFileELF::DumpELFSectionHeader_sh_flags(Stream *s, elf_word sh_flags) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1383 | { |
| 1384 | *s << ((sh_flags & SHF_WRITE) ? "WRITE" : " ") |
| 1385 | << (((sh_flags & SHF_WRITE) && (sh_flags & SHF_ALLOC)) ? '+' : ' ') |
| 1386 | << ((sh_flags & SHF_ALLOC) ? "ALLOC" : " ") |
| 1387 | << (((sh_flags & SHF_ALLOC) && (sh_flags & SHF_EXECINSTR)) ? '+' : ' ') |
| 1388 | << ((sh_flags & SHF_EXECINSTR) ? "EXECINSTR" : " "); |
| 1389 | } |
Stephen Wilson | ddd2962 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 1390 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1391 | //---------------------------------------------------------------------- |
| 1392 | // DumpELFSectionHeaders |
| 1393 | // |
| 1394 | // Dump all of the ELF section header to the specified output stream |
| 1395 | //---------------------------------------------------------------------- |
| 1396 | void |
| 1397 | ObjectFileELF::DumpELFSectionHeaders(Stream *s) |
| 1398 | { |
Stephen Wilson | ddd2962 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 1399 | if (!(ParseSectionHeaders() && GetSectionHeaderStringTable())) |
| 1400 | return; |
| 1401 | |
| 1402 | s->PutCString("Section Headers\n"); |
| 1403 | s->PutCString("IDX name type flags " |
| 1404 | "addr offset size link info addralgn " |
| 1405 | "entsize Name\n"); |
| 1406 | s->PutCString("==== -------- ------------ -------------------------------- " |
| 1407 | "-------- -------- -------- -------- -------- -------- " |
| 1408 | "-------- ====================\n"); |
| 1409 | |
| 1410 | uint32_t idx = 0; |
| 1411 | for (SectionHeaderCollConstIter I = m_section_headers.begin(); |
| 1412 | I != m_section_headers.end(); ++I, ++idx) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1413 | { |
Stephen Wilson | ddd2962 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 1414 | s->Printf("[%2u] ", idx); |
| 1415 | ObjectFileELF::DumpELFSectionHeader(s, *I); |
| 1416 | const char* section_name = m_shstr_data.PeekCStr(I->sh_name); |
| 1417 | if (section_name) |
| 1418 | *s << ' ' << section_name << "\n"; |
| 1419 | } |
| 1420 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1421 | |
Stephen Wilson | ddd2962 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 1422 | void |
| 1423 | ObjectFileELF::DumpDependentModules(lldb_private::Stream *s) |
| 1424 | { |
| 1425 | size_t num_modules = ParseDependentModules(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1426 | |
Stephen Wilson | ddd2962 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 1427 | if (num_modules > 0) |
| 1428 | { |
| 1429 | s->PutCString("Dependent Modules:\n"); |
| 1430 | for (unsigned i = 0; i < num_modules; ++i) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1431 | { |
Stephen Wilson | ddd2962 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 1432 | const FileSpec &spec = m_filespec_ap->GetFileSpecAtIndex(i); |
| 1433 | s->Printf(" %s\n", spec.GetFilename().GetCString()); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1434 | } |
| 1435 | } |
| 1436 | } |
| 1437 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1438 | bool |
Greg Clayton | 395fc33 | 2011-02-15 21:59:32 +0000 | [diff] [blame] | 1439 | ObjectFileELF::GetArchitecture (ArchSpec &arch) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1440 | { |
Stephen Wilson | f7cc64d | 2011-02-24 19:16:15 +0000 | [diff] [blame] | 1441 | if (!ParseHeader()) |
| 1442 | return false; |
| 1443 | |
Greg Clayton | b344843 | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 1444 | arch.SetArchitecture (eArchTypeELF, m_header.e_machine, LLDB_INVALID_CPUTYPE); |
Greg Clayton | 940b103 | 2011-02-23 00:35:02 +0000 | [diff] [blame] | 1445 | arch.GetTriple().setOSName (Host::GetOSString().GetCString()); |
| 1446 | arch.GetTriple().setVendorName(Host::GetVendorString().GetCString()); |
Stephen Wilson | ddd2962 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 1447 | return true; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1448 | } |
| 1449 | |
Greg Clayton | ca31997 | 2011-07-09 00:41:34 +0000 | [diff] [blame] | 1450 | ObjectFile::Type |
| 1451 | ObjectFileELF::CalculateType() |
| 1452 | { |
| 1453 | switch (m_header.e_type) |
| 1454 | { |
| 1455 | case llvm::ELF::ET_NONE: |
| 1456 | // 0 - No file type |
| 1457 | return eTypeUnknown; |
| 1458 | |
| 1459 | case llvm::ELF::ET_REL: |
| 1460 | // 1 - Relocatable file |
| 1461 | return eTypeObjectFile; |
| 1462 | |
| 1463 | case llvm::ELF::ET_EXEC: |
| 1464 | // 2 - Executable file |
| 1465 | return eTypeExecutable; |
| 1466 | |
| 1467 | case llvm::ELF::ET_DYN: |
| 1468 | // 3 - Shared object file |
| 1469 | return eTypeSharedLibrary; |
| 1470 | |
| 1471 | case ET_CORE: |
| 1472 | // 4 - Core file |
| 1473 | return eTypeCoreFile; |
| 1474 | |
| 1475 | default: |
| 1476 | break; |
| 1477 | } |
| 1478 | return eTypeUnknown; |
| 1479 | } |
| 1480 | |
| 1481 | ObjectFile::Strata |
| 1482 | ObjectFileELF::CalculateStrata() |
| 1483 | { |
| 1484 | switch (m_header.e_type) |
| 1485 | { |
| 1486 | case llvm::ELF::ET_NONE: |
| 1487 | // 0 - No file type |
| 1488 | return eStrataUnknown; |
| 1489 | |
| 1490 | case llvm::ELF::ET_REL: |
| 1491 | // 1 - Relocatable file |
| 1492 | return eStrataUnknown; |
| 1493 | |
| 1494 | case llvm::ELF::ET_EXEC: |
| 1495 | // 2 - Executable file |
| 1496 | // TODO: is there any way to detect that an executable is a kernel |
| 1497 | // related executable by inspecting the program headers, section |
| 1498 | // headers, symbols, or any other flag bits??? |
| 1499 | return eStrataUser; |
| 1500 | |
| 1501 | case llvm::ELF::ET_DYN: |
| 1502 | // 3 - Shared object file |
| 1503 | // TODO: is there any way to detect that an shared library is a kernel |
| 1504 | // related executable by inspecting the program headers, section |
| 1505 | // headers, symbols, or any other flag bits??? |
| 1506 | return eStrataUnknown; |
| 1507 | |
| 1508 | case ET_CORE: |
| 1509 | // 4 - Core file |
| 1510 | // TODO: is there any way to detect that an core file is a kernel |
| 1511 | // related executable by inspecting the program headers, section |
| 1512 | // headers, symbols, or any other flag bits??? |
| 1513 | return eStrataUnknown; |
| 1514 | |
| 1515 | default: |
| 1516 | break; |
| 1517 | } |
| 1518 | return eStrataUnknown; |
| 1519 | } |
| 1520 | |