1 //===-- ObjectFileELF.cpp ------------------------------------- -*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include "ObjectFileELF.h" 11 12 #include <cassert> 13 #include <algorithm> 14 15 #include "lldb/Core/ArchSpec.h" 16 #include "lldb/Core/DataBuffer.h" 17 #include "lldb/Core/Error.h" 18 #include "lldb/Core/FileSpecList.h" 19 #include "lldb/Core/Module.h" 20 #include "lldb/Core/ModuleSpec.h" 21 #include "lldb/Core/PluginManager.h" 22 #include "lldb/Core/Section.h" 23 #include "lldb/Core/Stream.h" 24 #include "lldb/Symbol/DWARFCallFrameInfo.h" 25 #include "lldb/Symbol/SymbolContext.h" 26 #include "lldb/Target/Target.h" 27 #include "lldb/Host/Host.h" 28 29 #include "llvm/ADT/PointerUnion.h" 30 31 #define CASE_AND_STREAM(s, def, width) \ 32 case def: s->Printf("%-*s", width, #def); break; 33 34 using namespace lldb; 35 using namespace lldb_private; 36 using namespace elf; 37 using namespace llvm::ELF; 38 39 namespace { 40 //===----------------------------------------------------------------------===// 41 /// @class ELFRelocation 42 /// @brief Generic wrapper for ELFRel and ELFRela. 43 /// 44 /// This helper class allows us to parse both ELFRel and ELFRela relocation 45 /// entries in a generic manner. 46 class ELFRelocation 47 { 48 public: 49 50 /// Constructs an ELFRelocation entry with a personality as given by @p 51 /// type. 52 /// 53 /// @param type Either DT_REL or DT_RELA. Any other value is invalid. 54 ELFRelocation(unsigned type); 55 56 ~ELFRelocation(); 57 58 bool 59 Parse(const lldb_private::DataExtractor &data, lldb::offset_t *offset); 60 61 static unsigned 62 RelocType32(const ELFRelocation &rel); 63 64 static unsigned 65 RelocType64(const ELFRelocation &rel); 66 67 static unsigned 68 RelocSymbol32(const ELFRelocation &rel); 69 70 static unsigned 71 RelocSymbol64(const ELFRelocation &rel); 72 73 private: 74 typedef llvm::PointerUnion<ELFRel*, ELFRela*> RelocUnion; 75 76 RelocUnion reloc; 77 }; 78 79 ELFRelocation::ELFRelocation(unsigned type) 80 { 81 if (type == DT_REL) 82 reloc = new ELFRel(); 83 else if (type == DT_RELA) 84 reloc = new ELFRela(); 85 else { 86 assert(false && "unexpected relocation type"); 87 reloc = static_cast<ELFRel*>(NULL); 88 } 89 } 90 91 ELFRelocation::~ELFRelocation() 92 { 93 if (reloc.is<ELFRel*>()) 94 delete reloc.get<ELFRel*>(); 95 else 96 delete reloc.get<ELFRela*>(); 97 } 98 99 bool 100 ELFRelocation::Parse(const lldb_private::DataExtractor &data, lldb::offset_t *offset) 101 { 102 if (reloc.is<ELFRel*>()) 103 return reloc.get<ELFRel*>()->Parse(data, offset); 104 else 105 return reloc.get<ELFRela*>()->Parse(data, offset); 106 } 107 108 unsigned 109 ELFRelocation::RelocType32(const ELFRelocation &rel) 110 { 111 if (rel.reloc.is<ELFRel*>()) 112 return ELFRel::RelocType32(*rel.reloc.get<ELFRel*>()); 113 else 114 return ELFRela::RelocType32(*rel.reloc.get<ELFRela*>()); 115 } 116 117 unsigned 118 ELFRelocation::RelocType64(const ELFRelocation &rel) 119 { 120 if (rel.reloc.is<ELFRel*>()) 121 return ELFRel::RelocType64(*rel.reloc.get<ELFRel*>()); 122 else 123 return ELFRela::RelocType64(*rel.reloc.get<ELFRela*>()); 124 } 125 126 unsigned 127 ELFRelocation::RelocSymbol32(const ELFRelocation &rel) 128 { 129 if (rel.reloc.is<ELFRel*>()) 130 return ELFRel::RelocSymbol32(*rel.reloc.get<ELFRel*>()); 131 else 132 return ELFRela::RelocSymbol32(*rel.reloc.get<ELFRela*>()); 133 } 134 135 unsigned 136 ELFRelocation::RelocSymbol64(const ELFRelocation &rel) 137 { 138 if (rel.reloc.is<ELFRel*>()) 139 return ELFRel::RelocSymbol64(*rel.reloc.get<ELFRel*>()); 140 else 141 return ELFRela::RelocSymbol64(*rel.reloc.get<ELFRela*>()); 142 } 143 144 } // end anonymous namespace 145 146 //------------------------------------------------------------------ 147 // Static methods. 148 //------------------------------------------------------------------ 149 void 150 ObjectFileELF::Initialize() 151 { 152 PluginManager::RegisterPlugin(GetPluginNameStatic(), 153 GetPluginDescriptionStatic(), 154 CreateInstance, 155 CreateMemoryInstance, 156 GetModuleSpecifications); 157 } 158 159 void 160 ObjectFileELF::Terminate() 161 { 162 PluginManager::UnregisterPlugin(CreateInstance); 163 } 164 165 lldb_private::ConstString 166 ObjectFileELF::GetPluginNameStatic() 167 { 168 static ConstString g_name("elf"); 169 return g_name; 170 } 171 172 const char * 173 ObjectFileELF::GetPluginDescriptionStatic() 174 { 175 return "ELF object file reader."; 176 } 177 178 ObjectFile * 179 ObjectFileELF::CreateInstance (const lldb::ModuleSP &module_sp, 180 DataBufferSP &data_sp, 181 lldb::offset_t data_offset, 182 const lldb_private::FileSpec* file, 183 lldb::offset_t file_offset, 184 lldb::offset_t length) 185 { 186 if (!data_sp) 187 { 188 data_sp = file->MemoryMapFileContents(file_offset, length); 189 data_offset = 0; 190 } 191 192 if (data_sp && data_sp->GetByteSize() > (llvm::ELF::EI_NIDENT + data_offset)) 193 { 194 const uint8_t *magic = data_sp->GetBytes() + data_offset; 195 if (ELFHeader::MagicBytesMatch(magic)) 196 { 197 // Update the data to contain the entire file if it doesn't already 198 if (data_sp->GetByteSize() < length) { 199 data_sp = file->MemoryMapFileContents(file_offset, length); 200 data_offset = 0; 201 magic = data_sp->GetBytes(); 202 } 203 unsigned address_size = ELFHeader::AddressSizeInBytes(magic); 204 if (address_size == 4 || address_size == 8) 205 { 206 std::unique_ptr<ObjectFileELF> objfile_ap(new ObjectFileELF(module_sp, data_sp, data_offset, file, file_offset, length)); 207 ArchSpec spec; 208 if (objfile_ap->GetArchitecture(spec) && 209 objfile_ap->SetModulesArchitecture(spec)) 210 return objfile_ap.release(); 211 } 212 } 213 } 214 return NULL; 215 } 216 217 218 ObjectFile* 219 ObjectFileELF::CreateMemoryInstance (const lldb::ModuleSP &module_sp, 220 DataBufferSP& data_sp, 221 const lldb::ProcessSP &process_sp, 222 lldb::addr_t header_addr) 223 { 224 return NULL; 225 } 226 227 bool 228 ObjectFileELF::MagicBytesMatch (DataBufferSP& data_sp, 229 lldb::addr_t data_offset, 230 lldb::addr_t data_length) 231 { 232 if (data_sp && data_sp->GetByteSize() > (llvm::ELF::EI_NIDENT + data_offset)) 233 { 234 const uint8_t *magic = data_sp->GetBytes() + data_offset; 235 return ELFHeader::MagicBytesMatch(magic); 236 } 237 return false; 238 } 239 240 /* 241 * crc function from http://svnweb.freebsd.org/base/head/sys/libkern/crc32.c 242 * 243 * COPYRIGHT (C) 1986 Gary S. Brown. You may use this program, or 244 * code or tables extracted from it, as desired without restriction. 245 */ 246 static uint32_t 247 calc_gnu_debuglink_crc32(const void *buf, size_t size) 248 { 249 static const uint32_t g_crc32_tab[] = 250 { 251 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f, 252 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988, 253 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2, 254 0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7, 255 0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9, 256 0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172, 257 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, 0x35b5a8fa, 0x42b2986c, 258 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59, 259 0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423, 260 0xcfba9599, 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924, 261 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190, 0x01db7106, 262 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433, 263 0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d, 264 0x91646c97, 0xe6635c01, 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e, 265 0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950, 266 0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65, 267 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7, 268 0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0, 269 0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa, 270 0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f, 271 0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81, 272 0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a, 273 0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683, 0xe3630b12, 0x94643b84, 274 0x0d6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1, 275 0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb, 276 0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc, 277 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, 0xd6d6a3e8, 0xa1d1937e, 278 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b, 279 0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55, 280 0x316e8eef, 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236, 281 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe, 0xb2bd0b28, 282 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d, 283 0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f, 284 0x72076785, 0x05005713, 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38, 285 0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242, 286 0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777, 287 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69, 288 0x616bffd3, 0x166ccf45, 0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2, 289 0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc, 290 0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9, 291 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693, 292 0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94, 293 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d 294 }; 295 const uint8_t *p = (const uint8_t *)buf; 296 uint32_t crc; 297 298 crc = ~0U; 299 while (size--) 300 crc = g_crc32_tab[(crc ^ *p++) & 0xFF] ^ (crc >> 8); 301 return crc ^ ~0U; 302 } 303 304 size_t 305 ObjectFileELF::GetModuleSpecifications (const lldb_private::FileSpec& file, 306 lldb::DataBufferSP& data_sp, 307 lldb::offset_t data_offset, 308 lldb::offset_t file_offset, 309 lldb::offset_t length, 310 lldb_private::ModuleSpecList &specs) 311 { 312 const size_t initial_count = specs.GetSize(); 313 314 if (ObjectFileELF::MagicBytesMatch(data_sp, 0, data_sp->GetByteSize())) 315 { 316 DataExtractor data; 317 data.SetData(data_sp); 318 elf::ELFHeader header; 319 if (header.Parse(data, &data_offset)) 320 { 321 if (data_sp) 322 { 323 ModuleSpec spec; 324 spec.GetFileSpec() = file; 325 spec.GetArchitecture().SetArchitecture(eArchTypeELF, 326 header.e_machine, 327 LLDB_INVALID_CPUTYPE); 328 if (spec.GetArchitecture().IsValid()) 329 { 330 // We could parse the ABI tag information (in .note, .notes, or .note.ABI-tag) to get the 331 // machine information. However, this info isn't guaranteed to exist or be correct. Details: 332 // http://refspecs.linuxfoundation.org/LSB_1.2.0/gLSB/noteabitag.html 333 // Instead of passing potentially incorrect information down the pipeline, grab 334 // the host information and use it. 335 spec.GetArchitecture().GetTriple().setOSName (Host::GetOSString().GetCString()); 336 spec.GetArchitecture().GetTriple().setVendorName(Host::GetVendorString().GetCString()); 337 338 // Try to get the UUID from the section list. Usually that's at the end, so 339 // map the file in if we don't have it already. 340 size_t section_header_end = header.e_shoff + header.e_shnum * header.e_shentsize; 341 if (section_header_end > data_sp->GetByteSize()) 342 { 343 data_sp = file.MemoryMapFileContents (file_offset, section_header_end); 344 data.SetData(data_sp); 345 } 346 347 uint32_t gnu_debuglink_crc = 0; 348 std::string gnu_debuglink_file; 349 SectionHeaderColl section_headers; 350 lldb_private::UUID &uuid = spec.GetUUID(); 351 GetSectionHeaderInfo(section_headers, data, header, uuid, gnu_debuglink_file, gnu_debuglink_crc); 352 353 if (!uuid.IsValid()) 354 { 355 if (!gnu_debuglink_crc) 356 { 357 // Need to map entire file into memory to calculate the crc. 358 data_sp = file.MemoryMapFileContents (file_offset, SIZE_MAX); 359 data.SetData(data_sp); 360 gnu_debuglink_crc = calc_gnu_debuglink_crc32 (data.GetDataStart(), data.GetByteSize()); 361 } 362 if (gnu_debuglink_crc) 363 { 364 // Use 4 bytes of crc from the .gnu_debuglink section. 365 uint32_t uuidt[4] = { gnu_debuglink_crc, 0, 0, 0 }; 366 uuid.SetBytes (uuidt, sizeof(uuidt)); 367 } 368 } 369 370 specs.Append(spec); 371 } 372 } 373 } 374 } 375 376 return specs.GetSize() - initial_count; 377 } 378 379 //------------------------------------------------------------------ 380 // PluginInterface protocol 381 //------------------------------------------------------------------ 382 lldb_private::ConstString 383 ObjectFileELF::GetPluginName() 384 { 385 return GetPluginNameStatic(); 386 } 387 388 uint32_t 389 ObjectFileELF::GetPluginVersion() 390 { 391 return m_plugin_version; 392 } 393 //------------------------------------------------------------------ 394 // ObjectFile protocol 395 //------------------------------------------------------------------ 396 397 ObjectFileELF::ObjectFileELF (const lldb::ModuleSP &module_sp, 398 DataBufferSP& data_sp, 399 lldb::offset_t data_offset, 400 const FileSpec* file, 401 lldb::offset_t file_offset, 402 lldb::offset_t length) : 403 ObjectFile(module_sp, file, file_offset, length, data_sp, data_offset), 404 m_header(), 405 m_program_headers(), 406 m_section_headers(), 407 m_filespec_ap() 408 { 409 if (file) 410 m_file = *file; 411 ::memset(&m_header, 0, sizeof(m_header)); 412 m_gnu_debuglink_crc = 0; 413 m_gnu_debuglink_file.clear(); 414 } 415 416 ObjectFileELF::~ObjectFileELF() 417 { 418 } 419 420 bool 421 ObjectFileELF::IsExecutable() const 422 { 423 return m_header.e_entry != 0; 424 } 425 426 ByteOrder 427 ObjectFileELF::GetByteOrder() const 428 { 429 if (m_header.e_ident[EI_DATA] == ELFDATA2MSB) 430 return eByteOrderBig; 431 if (m_header.e_ident[EI_DATA] == ELFDATA2LSB) 432 return eByteOrderLittle; 433 return eByteOrderInvalid; 434 } 435 436 uint32_t 437 ObjectFileELF::GetAddressByteSize() const 438 { 439 return m_data.GetAddressByteSize(); 440 } 441 442 size_t 443 ObjectFileELF::SectionIndex(const SectionHeaderCollIter &I) 444 { 445 return std::distance(m_section_headers.begin(), I) + 1u; 446 } 447 448 size_t 449 ObjectFileELF::SectionIndex(const SectionHeaderCollConstIter &I) const 450 { 451 return std::distance(m_section_headers.begin(), I) + 1u; 452 } 453 454 bool 455 ObjectFileELF::ParseHeader() 456 { 457 lldb::offset_t offset = 0; 458 return m_header.Parse(m_data, &offset); 459 } 460 461 bool 462 ObjectFileELF::GetUUID(lldb_private::UUID* uuid) 463 { 464 // Need to parse the section list to get the UUIDs, so make sure that's been done. 465 if (!ParseSectionHeaders()) 466 return false; 467 468 if (m_uuid.IsValid()) 469 { 470 // We have the full build id uuid. 471 *uuid = m_uuid; 472 return true; 473 } 474 else 475 { 476 if (!m_gnu_debuglink_crc) 477 m_gnu_debuglink_crc = calc_gnu_debuglink_crc32 (m_data.GetDataStart(), m_data.GetByteSize()); 478 if (m_gnu_debuglink_crc) 479 { 480 // Use 4 bytes of crc from the .gnu_debuglink section. 481 uint32_t uuidt[4] = { m_gnu_debuglink_crc, 0, 0, 0 }; 482 uuid->SetBytes (uuidt, sizeof(uuidt)); 483 return true; 484 } 485 } 486 487 return false; 488 } 489 490 lldb_private::FileSpecList 491 ObjectFileELF::GetDebugSymbolFilePaths() 492 { 493 FileSpecList file_spec_list; 494 495 if (!m_gnu_debuglink_file.empty()) 496 { 497 FileSpec file_spec (m_gnu_debuglink_file.c_str(), false); 498 file_spec_list.Append (file_spec); 499 } 500 return file_spec_list; 501 } 502 503 uint32_t 504 ObjectFileELF::GetDependentModules(FileSpecList &files) 505 { 506 size_t num_modules = ParseDependentModules(); 507 uint32_t num_specs = 0; 508 509 for (unsigned i = 0; i < num_modules; ++i) 510 { 511 if (files.AppendIfUnique(m_filespec_ap->GetFileSpecAtIndex(i))) 512 num_specs++; 513 } 514 515 return num_specs; 516 } 517 518 Address 519 ObjectFileELF::GetImageInfoAddress(Target *target) 520 { 521 if (!ParseDynamicSymbols()) 522 return Address(); 523 524 SectionList *section_list = GetSectionList(); 525 if (!section_list) 526 return Address(); 527 528 // Find the SHT_DYNAMIC (.dynamic) section. 529 SectionSP dynsym_section_sp (section_list->FindSectionByType (eSectionTypeELFDynamicLinkInfo, true)); 530 if (!dynsym_section_sp) 531 return Address(); 532 assert (dynsym_section_sp->GetObjectFile() == this); 533 534 user_id_t dynsym_id = dynsym_section_sp->GetID(); 535 const ELFSectionHeaderInfo *dynsym_hdr = GetSectionHeaderByIndex(dynsym_id); 536 if (!dynsym_hdr) 537 return Address(); 538 539 for (size_t i = 0; i < m_dynamic_symbols.size(); ++i) 540 { 541 ELFDynamic &symbol = m_dynamic_symbols[i]; 542 543 if (symbol.d_tag == DT_DEBUG) 544 { 545 // Compute the offset as the number of previous entries plus the 546 // size of d_tag. 547 addr_t offset = i * dynsym_hdr->sh_entsize + GetAddressByteSize(); 548 return Address(dynsym_section_sp, offset); 549 } 550 else if (symbol.d_tag == DT_MIPS_RLD_MAP && target) 551 { 552 addr_t offset = i * dynsym_hdr->sh_entsize + GetAddressByteSize(); 553 addr_t dyn_base = dynsym_section_sp->GetLoadBaseAddress(target); 554 if (dyn_base == LLDB_INVALID_ADDRESS) 555 return Address(); 556 Address addr; 557 Error error; 558 if (target->ReadPointerFromMemory(dyn_base + offset, false, error, addr)) 559 return addr; 560 } 561 } 562 563 return Address(); 564 } 565 566 lldb_private::Address 567 ObjectFileELF::GetEntryPointAddress () 568 { 569 if (m_entry_point_address.IsValid()) 570 return m_entry_point_address; 571 572 if (!ParseHeader() || !IsExecutable()) 573 return m_entry_point_address; 574 575 SectionList *section_list = GetSectionList(); 576 addr_t offset = m_header.e_entry; 577 578 if (!section_list) 579 m_entry_point_address.SetOffset(offset); 580 else 581 m_entry_point_address.ResolveAddressUsingFileSections(offset, section_list); 582 return m_entry_point_address; 583 } 584 585 //---------------------------------------------------------------------- 586 // ParseDependentModules 587 //---------------------------------------------------------------------- 588 size_t 589 ObjectFileELF::ParseDependentModules() 590 { 591 if (m_filespec_ap.get()) 592 return m_filespec_ap->GetSize(); 593 594 m_filespec_ap.reset(new FileSpecList()); 595 596 if (!ParseSectionHeaders()) 597 return 0; 598 599 SectionList *section_list = GetSectionList(); 600 if (!section_list) 601 return 0; 602 603 // Find the SHT_DYNAMIC section. 604 Section *dynsym = section_list->FindSectionByType (eSectionTypeELFDynamicLinkInfo, true).get(); 605 if (!dynsym) 606 return 0; 607 assert (dynsym->GetObjectFile() == this); 608 609 const ELFSectionHeaderInfo *header = GetSectionHeaderByIndex (dynsym->GetID()); 610 if (!header) 611 return 0; 612 // sh_link: section header index of string table used by entries in the section. 613 Section *dynstr = section_list->FindSectionByID (header->sh_link + 1).get(); 614 if (!dynstr) 615 return 0; 616 617 DataExtractor dynsym_data; 618 DataExtractor dynstr_data; 619 if (ReadSectionData(dynsym, dynsym_data) && 620 ReadSectionData(dynstr, dynstr_data)) 621 { 622 ELFDynamic symbol; 623 const lldb::offset_t section_size = dynsym_data.GetByteSize(); 624 lldb::offset_t offset = 0; 625 626 // The only type of entries we are concerned with are tagged DT_NEEDED, 627 // yielding the name of a required library. 628 while (offset < section_size) 629 { 630 if (!symbol.Parse(dynsym_data, &offset)) 631 break; 632 633 if (symbol.d_tag != DT_NEEDED) 634 continue; 635 636 uint32_t str_index = static_cast<uint32_t>(symbol.d_val); 637 const char *lib_name = dynstr_data.PeekCStr(str_index); 638 m_filespec_ap->Append(FileSpec(lib_name, true)); 639 } 640 } 641 642 return m_filespec_ap->GetSize(); 643 } 644 645 //---------------------------------------------------------------------- 646 // ParseProgramHeaders 647 //---------------------------------------------------------------------- 648 size_t 649 ObjectFileELF::ParseProgramHeaders() 650 { 651 // We have already parsed the program headers 652 if (!m_program_headers.empty()) 653 return m_program_headers.size(); 654 655 // If there are no program headers to read we are done. 656 if (m_header.e_phnum == 0) 657 return 0; 658 659 m_program_headers.resize(m_header.e_phnum); 660 if (m_program_headers.size() != m_header.e_phnum) 661 return 0; 662 663 const size_t ph_size = m_header.e_phnum * m_header.e_phentsize; 664 const elf_off ph_offset = m_header.e_phoff; 665 DataExtractor data; 666 if (GetData (ph_offset, ph_size, data) != ph_size) 667 return 0; 668 669 uint32_t idx; 670 lldb::offset_t offset; 671 for (idx = 0, offset = 0; idx < m_header.e_phnum; ++idx) 672 { 673 if (m_program_headers[idx].Parse(data, &offset) == false) 674 break; 675 } 676 677 if (idx < m_program_headers.size()) 678 m_program_headers.resize(idx); 679 680 return m_program_headers.size(); 681 } 682 683 static bool 684 ParseNoteGNUBuildID(DataExtractor &data, lldb_private::UUID &uuid) 685 { 686 // Try to parse the note section (ie .note.gnu.build-id|.notes|.note|...) and get the build id. 687 // BuildID documentation: https://fedoraproject.org/wiki/Releases/FeatureBuildId 688 struct 689 { 690 uint32_t name_len; // Length of note name 691 uint32_t desc_len; // Length of note descriptor 692 uint32_t type; // Type of note (1 is ABI_TAG, 3 is BUILD_ID) 693 } notehdr; 694 lldb::offset_t offset = 0; 695 static const uint32_t g_gnu_build_id = 3; // NT_GNU_BUILD_ID from elf.h 696 697 while (true) 698 { 699 if (data.GetU32 (&offset, ¬ehdr, 3) == NULL) 700 return false; 701 702 notehdr.name_len = llvm::RoundUpToAlignment (notehdr.name_len, 4); 703 notehdr.desc_len = llvm::RoundUpToAlignment (notehdr.desc_len, 4); 704 705 lldb::offset_t offset_next_note = offset + notehdr.name_len + notehdr.desc_len; 706 707 // 16 bytes is UUID|MD5, 20 bytes is SHA1 708 if ((notehdr.type == g_gnu_build_id) && (notehdr.name_len == 4) && 709 (notehdr.desc_len == 16 || notehdr.desc_len == 20)) 710 { 711 char name[4]; 712 if (data.GetU8 (&offset, name, 4) == NULL) 713 return false; 714 if (!strcmp(name, "GNU")) 715 { 716 uint8_t uuidbuf[20]; 717 if (data.GetU8 (&offset, &uuidbuf, notehdr.desc_len) == NULL) 718 return false; 719 uuid.SetBytes (uuidbuf, notehdr.desc_len); 720 return true; 721 } 722 } 723 offset = offset_next_note; 724 } 725 return false; 726 } 727 728 //---------------------------------------------------------------------- 729 // GetSectionHeaderInfo 730 //---------------------------------------------------------------------- 731 size_t 732 ObjectFileELF::GetSectionHeaderInfo(SectionHeaderColl §ion_headers, 733 lldb_private::DataExtractor &object_data, 734 const elf::ELFHeader &header, 735 lldb_private::UUID &uuid, 736 std::string &gnu_debuglink_file, 737 uint32_t &gnu_debuglink_crc) 738 { 739 // We have already parsed the section headers 740 if (!section_headers.empty()) 741 return section_headers.size(); 742 743 // If there are no section headers we are done. 744 if (header.e_shnum == 0) 745 return 0; 746 747 section_headers.resize(header.e_shnum); 748 if (section_headers.size() != header.e_shnum) 749 return 0; 750 751 const size_t sh_size = header.e_shnum * header.e_shentsize; 752 const elf_off sh_offset = header.e_shoff; 753 DataExtractor sh_data; 754 if (sh_data.SetData (object_data, sh_offset, sh_size) != sh_size) 755 return 0; 756 757 uint32_t idx; 758 lldb::offset_t offset; 759 for (idx = 0, offset = 0; idx < header.e_shnum; ++idx) 760 { 761 if (section_headers[idx].Parse(sh_data, &offset) == false) 762 break; 763 } 764 if (idx < section_headers.size()) 765 section_headers.resize(idx); 766 767 const unsigned strtab_idx = header.e_shstrndx; 768 if (strtab_idx && strtab_idx < section_headers.size()) 769 { 770 const ELFSectionHeaderInfo &sheader = section_headers[strtab_idx]; 771 const size_t byte_size = sheader.sh_size; 772 const Elf64_Off offset = sheader.sh_offset; 773 lldb_private::DataExtractor shstr_data; 774 775 if (shstr_data.SetData (object_data, offset, byte_size) == byte_size) 776 { 777 for (SectionHeaderCollIter I = section_headers.begin(); 778 I != section_headers.end(); ++I) 779 { 780 static ConstString g_sect_name_gnu_debuglink (".gnu_debuglink"); 781 const ELFSectionHeaderInfo &header = *I; 782 const uint64_t section_size = header.sh_type == SHT_NOBITS ? 0 : header.sh_size; 783 ConstString name(shstr_data.PeekCStr(I->sh_name)); 784 785 I->section_name = name; 786 787 if (name == g_sect_name_gnu_debuglink) 788 { 789 DataExtractor data; 790 if (section_size && (data.SetData (object_data, header.sh_offset, section_size) == section_size)) 791 { 792 lldb::offset_t gnu_debuglink_offset = 0; 793 gnu_debuglink_file = data.GetCStr (&gnu_debuglink_offset); 794 gnu_debuglink_offset = llvm::RoundUpToAlignment (gnu_debuglink_offset, 4); 795 data.GetU32 (&gnu_debuglink_offset, &gnu_debuglink_crc, 1); 796 } 797 } 798 799 if (header.sh_type == SHT_NOTE && !uuid.IsValid()) 800 { 801 DataExtractor data; 802 if (section_size && (data.SetData (object_data, header.sh_offset, section_size) == section_size)) 803 { 804 ParseNoteGNUBuildID (data, uuid); 805 } 806 } 807 } 808 809 return section_headers.size(); 810 } 811 } 812 813 section_headers.clear(); 814 return 0; 815 } 816 817 size_t 818 ObjectFileELF::GetProgramHeaderCount() 819 { 820 return ParseProgramHeaders(); 821 } 822 823 const elf::ELFProgramHeader * 824 ObjectFileELF::GetProgramHeaderByIndex(lldb::user_id_t id) 825 { 826 if (!id || !ParseProgramHeaders()) 827 return NULL; 828 829 if (--id < m_program_headers.size()) 830 return &m_program_headers[id]; 831 832 return NULL; 833 } 834 835 DataExtractor 836 ObjectFileELF::GetSegmentDataByIndex(lldb::user_id_t id) 837 { 838 const elf::ELFProgramHeader *segment_header = GetProgramHeaderByIndex(id); 839 if (segment_header == NULL) 840 return DataExtractor(); 841 return DataExtractor(m_data, segment_header->p_offset, segment_header->p_filesz); 842 } 843 844 //---------------------------------------------------------------------- 845 // ParseSectionHeaders 846 //---------------------------------------------------------------------- 847 size_t 848 ObjectFileELF::ParseSectionHeaders() 849 { 850 return GetSectionHeaderInfo(m_section_headers, m_data, m_header, m_uuid, m_gnu_debuglink_file, m_gnu_debuglink_crc); 851 } 852 853 const ObjectFileELF::ELFSectionHeaderInfo * 854 ObjectFileELF::GetSectionHeaderByIndex(lldb::user_id_t id) 855 { 856 if (!id || !ParseSectionHeaders()) 857 return NULL; 858 859 if (--id < m_section_headers.size()) 860 return &m_section_headers[id]; 861 862 return NULL; 863 } 864 865 void 866 ObjectFileELF::CreateSections(SectionList &unified_section_list) 867 { 868 if (!m_sections_ap.get() && ParseSectionHeaders()) 869 { 870 m_sections_ap.reset(new SectionList()); 871 872 for (SectionHeaderCollIter I = m_section_headers.begin(); 873 I != m_section_headers.end(); ++I) 874 { 875 const ELFSectionHeaderInfo &header = *I; 876 877 ConstString& name = I->section_name; 878 const uint64_t file_size = header.sh_type == SHT_NOBITS ? 0 : header.sh_size; 879 const uint64_t vm_size = header.sh_flags & SHF_ALLOC ? header.sh_size : 0; 880 881 static ConstString g_sect_name_text (".text"); 882 static ConstString g_sect_name_data (".data"); 883 static ConstString g_sect_name_bss (".bss"); 884 static ConstString g_sect_name_tdata (".tdata"); 885 static ConstString g_sect_name_tbss (".tbss"); 886 static ConstString g_sect_name_dwarf_debug_abbrev (".debug_abbrev"); 887 static ConstString g_sect_name_dwarf_debug_aranges (".debug_aranges"); 888 static ConstString g_sect_name_dwarf_debug_frame (".debug_frame"); 889 static ConstString g_sect_name_dwarf_debug_info (".debug_info"); 890 static ConstString g_sect_name_dwarf_debug_line (".debug_line"); 891 static ConstString g_sect_name_dwarf_debug_loc (".debug_loc"); 892 static ConstString g_sect_name_dwarf_debug_macinfo (".debug_macinfo"); 893 static ConstString g_sect_name_dwarf_debug_pubnames (".debug_pubnames"); 894 static ConstString g_sect_name_dwarf_debug_pubtypes (".debug_pubtypes"); 895 static ConstString g_sect_name_dwarf_debug_ranges (".debug_ranges"); 896 static ConstString g_sect_name_dwarf_debug_str (".debug_str"); 897 static ConstString g_sect_name_eh_frame (".eh_frame"); 898 899 SectionType sect_type = eSectionTypeOther; 900 901 bool is_thread_specific = false; 902 903 if (name == g_sect_name_text) sect_type = eSectionTypeCode; 904 else if (name == g_sect_name_data) sect_type = eSectionTypeData; 905 else if (name == g_sect_name_bss) sect_type = eSectionTypeZeroFill; 906 else if (name == g_sect_name_tdata) 907 { 908 sect_type = eSectionTypeData; 909 is_thread_specific = true; 910 } 911 else if (name == g_sect_name_tbss) 912 { 913 sect_type = eSectionTypeZeroFill; 914 is_thread_specific = true; 915 } 916 // .debug_abbrev – Abbreviations used in the .debug_info section 917 // .debug_aranges – Lookup table for mapping addresses to compilation units 918 // .debug_frame – Call frame information 919 // .debug_info – The core DWARF information section 920 // .debug_line – Line number information 921 // .debug_loc – Location lists used in DW_AT_location attributes 922 // .debug_macinfo – Macro information 923 // .debug_pubnames – Lookup table for mapping object and function names to compilation units 924 // .debug_pubtypes – Lookup table for mapping type names to compilation units 925 // .debug_ranges – Address ranges used in DW_AT_ranges attributes 926 // .debug_str – String table used in .debug_info 927 // MISSING? .gnu_debugdata - "mini debuginfo / MiniDebugInfo" section, http://sourceware.org/gdb/onlinedocs/gdb/MiniDebugInfo.html 928 // MISSING? .debug-index - http://src.chromium.org/viewvc/chrome/trunk/src/build/gdb-add-index?pathrev=144644 929 // MISSING? .debug_types - Type descriptions from DWARF 4? See http://gcc.gnu.org/wiki/DwarfSeparateTypeInfo 930 else if (name == g_sect_name_dwarf_debug_abbrev) sect_type = eSectionTypeDWARFDebugAbbrev; 931 else if (name == g_sect_name_dwarf_debug_aranges) sect_type = eSectionTypeDWARFDebugAranges; 932 else if (name == g_sect_name_dwarf_debug_frame) sect_type = eSectionTypeDWARFDebugFrame; 933 else if (name == g_sect_name_dwarf_debug_info) sect_type = eSectionTypeDWARFDebugInfo; 934 else if (name == g_sect_name_dwarf_debug_line) sect_type = eSectionTypeDWARFDebugLine; 935 else if (name == g_sect_name_dwarf_debug_loc) sect_type = eSectionTypeDWARFDebugLoc; 936 else if (name == g_sect_name_dwarf_debug_macinfo) sect_type = eSectionTypeDWARFDebugMacInfo; 937 else if (name == g_sect_name_dwarf_debug_pubnames) sect_type = eSectionTypeDWARFDebugPubNames; 938 else if (name == g_sect_name_dwarf_debug_pubtypes) sect_type = eSectionTypeDWARFDebugPubTypes; 939 else if (name == g_sect_name_dwarf_debug_ranges) sect_type = eSectionTypeDWARFDebugRanges; 940 else if (name == g_sect_name_dwarf_debug_str) sect_type = eSectionTypeDWARFDebugStr; 941 else if (name == g_sect_name_eh_frame) sect_type = eSectionTypeEHFrame; 942 943 switch (header.sh_type) 944 { 945 case SHT_SYMTAB: 946 assert (sect_type == eSectionTypeOther); 947 sect_type = eSectionTypeELFSymbolTable; 948 break; 949 case SHT_DYNSYM: 950 assert (sect_type == eSectionTypeOther); 951 sect_type = eSectionTypeELFDynamicSymbols; 952 break; 953 case SHT_RELA: 954 case SHT_REL: 955 assert (sect_type == eSectionTypeOther); 956 sect_type = eSectionTypeELFRelocationEntries; 957 break; 958 case SHT_DYNAMIC: 959 assert (sect_type == eSectionTypeOther); 960 sect_type = eSectionTypeELFDynamicLinkInfo; 961 break; 962 } 963 964 SectionSP section_sp (new Section(GetModule(), // Module to which this section belongs. 965 this, // ObjectFile to which this section belongs and should read section data from. 966 SectionIndex(I), // Section ID. 967 name, // Section name. 968 sect_type, // Section type. 969 header.sh_addr, // VM address. 970 vm_size, // VM size in bytes of this section. 971 header.sh_offset, // Offset of this section in the file. 972 file_size, // Size of the section as found in the file. 973 header.sh_flags)); // Flags for this section. 974 975 if (is_thread_specific) 976 section_sp->SetIsThreadSpecific (is_thread_specific); 977 m_sections_ap->AddSection(section_sp); 978 } 979 } 980 981 if (m_sections_ap.get()) 982 { 983 if (GetType() == eTypeDebugInfo) 984 { 985 static const SectionType g_sections[] = 986 { 987 eSectionTypeDWARFDebugAranges, 988 eSectionTypeDWARFDebugInfo, 989 eSectionTypeDWARFDebugAbbrev, 990 eSectionTypeDWARFDebugFrame, 991 eSectionTypeDWARFDebugLine, 992 eSectionTypeDWARFDebugStr, 993 eSectionTypeDWARFDebugLoc, 994 eSectionTypeDWARFDebugMacInfo, 995 eSectionTypeDWARFDebugPubNames, 996 eSectionTypeDWARFDebugPubTypes, 997 eSectionTypeDWARFDebugRanges, 998 eSectionTypeELFSymbolTable, 999 }; 1000 SectionList *elf_section_list = m_sections_ap.get(); 1001 for (size_t idx = 0; idx < sizeof(g_sections) / sizeof(g_sections[0]); ++idx) 1002 { 1003 SectionType section_type = g_sections[idx]; 1004 SectionSP section_sp (elf_section_list->FindSectionByType (section_type, true)); 1005 if (section_sp) 1006 { 1007 SectionSP module_section_sp (unified_section_list.FindSectionByType (section_type, true)); 1008 if (module_section_sp) 1009 unified_section_list.ReplaceSection (module_section_sp->GetID(), section_sp); 1010 else 1011 unified_section_list.AddSection (section_sp); 1012 } 1013 } 1014 } 1015 else 1016 { 1017 unified_section_list = *m_sections_ap; 1018 } 1019 } 1020 } 1021 1022 // private 1023 unsigned 1024 ObjectFileELF::ParseSymbols (Symtab *symtab, 1025 user_id_t start_id, 1026 SectionList *section_list, 1027 const size_t num_symbols, 1028 const DataExtractor &symtab_data, 1029 const DataExtractor &strtab_data) 1030 { 1031 ELFSymbol symbol; 1032 lldb::offset_t offset = 0; 1033 1034 static ConstString text_section_name(".text"); 1035 static ConstString init_section_name(".init"); 1036 static ConstString fini_section_name(".fini"); 1037 static ConstString ctors_section_name(".ctors"); 1038 static ConstString dtors_section_name(".dtors"); 1039 1040 static ConstString data_section_name(".data"); 1041 static ConstString rodata_section_name(".rodata"); 1042 static ConstString rodata1_section_name(".rodata1"); 1043 static ConstString data2_section_name(".data1"); 1044 static ConstString bss_section_name(".bss"); 1045 1046 //StreamFile strm(stdout, false); 1047 unsigned i; 1048 for (i = 0; i < num_symbols; ++i) 1049 { 1050 if (symbol.Parse(symtab_data, &offset) == false) 1051 break; 1052 1053 const char *symbol_name = strtab_data.PeekCStr(symbol.st_name); 1054 1055 // No need to add symbols that have no names 1056 if (symbol_name == NULL || symbol_name[0] == '\0') 1057 continue; 1058 1059 //symbol.Dump (&strm, i, &strtab_data, section_list); 1060 1061 SectionSP symbol_section_sp; 1062 SymbolType symbol_type = eSymbolTypeInvalid; 1063 Elf64_Half symbol_idx = symbol.st_shndx; 1064 1065 switch (symbol_idx) 1066 { 1067 case SHN_ABS: 1068 symbol_type = eSymbolTypeAbsolute; 1069 break; 1070 case SHN_UNDEF: 1071 symbol_type = eSymbolTypeUndefined; 1072 break; 1073 default: 1074 symbol_section_sp = section_list->GetSectionAtIndex(symbol_idx); 1075 break; 1076 } 1077 1078 // If a symbol is undefined do not process it further even if it has a STT type 1079 if (symbol_type != eSymbolTypeUndefined) 1080 { 1081 switch (symbol.getType()) 1082 { 1083 default: 1084 case STT_NOTYPE: 1085 // The symbol's type is not specified. 1086 break; 1087 1088 case STT_OBJECT: 1089 // The symbol is associated with a data object, such as a variable, 1090 // an array, etc. 1091 symbol_type = eSymbolTypeData; 1092 break; 1093 1094 case STT_FUNC: 1095 // The symbol is associated with a function or other executable code. 1096 symbol_type = eSymbolTypeCode; 1097 break; 1098 1099 case STT_SECTION: 1100 // The symbol is associated with a section. Symbol table entries of 1101 // this type exist primarily for relocation and normally have 1102 // STB_LOCAL binding. 1103 break; 1104 1105 case STT_FILE: 1106 // Conventionally, the symbol's name gives the name of the source 1107 // file associated with the object file. A file symbol has STB_LOCAL 1108 // binding, its section index is SHN_ABS, and it precedes the other 1109 // STB_LOCAL symbols for the file, if it is present. 1110 symbol_type = eSymbolTypeSourceFile; 1111 break; 1112 1113 case STT_GNU_IFUNC: 1114 // The symbol is associated with an indirect function. The actual 1115 // function will be resolved if it is referenced. 1116 symbol_type = eSymbolTypeResolver; 1117 break; 1118 } 1119 } 1120 1121 if (symbol_type == eSymbolTypeInvalid) 1122 { 1123 if (symbol_section_sp) 1124 { 1125 const ConstString §_name = symbol_section_sp->GetName(); 1126 if (sect_name == text_section_name || 1127 sect_name == init_section_name || 1128 sect_name == fini_section_name || 1129 sect_name == ctors_section_name || 1130 sect_name == dtors_section_name) 1131 { 1132 symbol_type = eSymbolTypeCode; 1133 } 1134 else if (sect_name == data_section_name || 1135 sect_name == data2_section_name || 1136 sect_name == rodata_section_name || 1137 sect_name == rodata1_section_name || 1138 sect_name == bss_section_name) 1139 { 1140 symbol_type = eSymbolTypeData; 1141 } 1142 } 1143 } 1144 1145 // If the symbol section we've found has no data (SHT_NOBITS), then check the module section 1146 // list. This can happen if we're parsing the debug file and it has no .text section, for example. 1147 if (symbol_section_sp && (symbol_section_sp->GetFileSize() == 0)) 1148 { 1149 ModuleSP module_sp(GetModule()); 1150 if (module_sp) 1151 { 1152 SectionList *module_section_list = module_sp->GetSectionList(); 1153 if (module_section_list && module_section_list != section_list) 1154 { 1155 const ConstString §_name = symbol_section_sp->GetName(); 1156 lldb::SectionSP section_sp (module_section_list->FindSectionByName (sect_name)); 1157 if (section_sp && section_sp->GetFileSize()) 1158 { 1159 symbol_section_sp = section_sp; 1160 } 1161 } 1162 } 1163 } 1164 1165 uint64_t symbol_value = symbol.st_value; 1166 if (symbol_section_sp) 1167 symbol_value -= symbol_section_sp->GetFileAddress(); 1168 bool is_global = symbol.getBinding() == STB_GLOBAL; 1169 uint32_t flags = symbol.st_other << 8 | symbol.st_info; 1170 bool is_mangled = symbol_name ? (symbol_name[0] == '_' && symbol_name[1] == 'Z') : false; 1171 Symbol dc_symbol( 1172 i + start_id, // ID is the original symbol table index. 1173 symbol_name, // Symbol name. 1174 is_mangled, // Is the symbol name mangled? 1175 symbol_type, // Type of this symbol 1176 is_global, // Is this globally visible? 1177 false, // Is this symbol debug info? 1178 false, // Is this symbol a trampoline? 1179 false, // Is this symbol artificial? 1180 symbol_section_sp, // Section in which this symbol is defined or null. 1181 symbol_value, // Offset in section or symbol value. 1182 symbol.st_size, // Size in bytes of this symbol. 1183 true, // Size is valid 1184 flags); // Symbol flags. 1185 symtab->AddSymbol(dc_symbol); 1186 } 1187 1188 return i; 1189 } 1190 1191 unsigned 1192 ObjectFileELF::ParseSymbolTable(Symtab *symbol_table, user_id_t start_id, lldb_private::Section *symtab) 1193 { 1194 if (symtab->GetObjectFile() != this) 1195 { 1196 // If the symbol table section is owned by a different object file, have it do the 1197 // parsing. 1198 ObjectFileELF *obj_file_elf = static_cast<ObjectFileELF *>(symtab->GetObjectFile()); 1199 return obj_file_elf->ParseSymbolTable (symbol_table, start_id, symtab); 1200 } 1201 1202 // Get section list for this object file. 1203 SectionList *section_list = m_sections_ap.get(); 1204 if (!section_list) 1205 return 0; 1206 1207 user_id_t symtab_id = symtab->GetID(); 1208 const ELFSectionHeaderInfo *symtab_hdr = GetSectionHeaderByIndex(symtab_id); 1209 assert(symtab_hdr->sh_type == SHT_SYMTAB || 1210 symtab_hdr->sh_type == SHT_DYNSYM); 1211 1212 // sh_link: section header index of associated string table. 1213 // Section ID's are ones based. 1214 user_id_t strtab_id = symtab_hdr->sh_link + 1; 1215 Section *strtab = section_list->FindSectionByID(strtab_id).get(); 1216 1217 unsigned num_symbols = 0; 1218 if (symtab && strtab) 1219 { 1220 assert (symtab->GetObjectFile() == this); 1221 assert (strtab->GetObjectFile() == this); 1222 1223 DataExtractor symtab_data; 1224 DataExtractor strtab_data; 1225 if (ReadSectionData(symtab, symtab_data) && 1226 ReadSectionData(strtab, strtab_data)) 1227 { 1228 size_t num_symbols = symtab_data.GetByteSize() / symtab_hdr->sh_entsize; 1229 1230 num_symbols = ParseSymbols(symbol_table, start_id, 1231 section_list, num_symbols, 1232 symtab_data, strtab_data); 1233 } 1234 } 1235 1236 return num_symbols; 1237 } 1238 1239 size_t 1240 ObjectFileELF::ParseDynamicSymbols() 1241 { 1242 if (m_dynamic_symbols.size()) 1243 return m_dynamic_symbols.size(); 1244 1245 SectionList *section_list = GetSectionList(); 1246 if (!section_list) 1247 return 0; 1248 1249 // Find the SHT_DYNAMIC section. 1250 Section *dynsym = section_list->FindSectionByType (eSectionTypeELFDynamicLinkInfo, true).get(); 1251 if (!dynsym) 1252 return 0; 1253 assert (dynsym->GetObjectFile() == this); 1254 1255 ELFDynamic symbol; 1256 DataExtractor dynsym_data; 1257 if (ReadSectionData(dynsym, dynsym_data)) 1258 { 1259 const lldb::offset_t section_size = dynsym_data.GetByteSize(); 1260 lldb::offset_t cursor = 0; 1261 1262 while (cursor < section_size) 1263 { 1264 if (!symbol.Parse(dynsym_data, &cursor)) 1265 break; 1266 1267 m_dynamic_symbols.push_back(symbol); 1268 } 1269 } 1270 1271 return m_dynamic_symbols.size(); 1272 } 1273 1274 const ELFDynamic * 1275 ObjectFileELF::FindDynamicSymbol(unsigned tag) 1276 { 1277 if (!ParseDynamicSymbols()) 1278 return NULL; 1279 1280 DynamicSymbolCollIter I = m_dynamic_symbols.begin(); 1281 DynamicSymbolCollIter E = m_dynamic_symbols.end(); 1282 for ( ; I != E; ++I) 1283 { 1284 ELFDynamic *symbol = &*I; 1285 1286 if (symbol->d_tag == tag) 1287 return symbol; 1288 } 1289 1290 return NULL; 1291 } 1292 1293 unsigned 1294 ObjectFileELF::PLTRelocationType() 1295 { 1296 // DT_PLTREL 1297 // This member specifies the type of relocation entry to which the 1298 // procedure linkage table refers. The d_val member holds DT_REL or 1299 // DT_RELA, as appropriate. All relocations in a procedure linkage table 1300 // must use the same relocation. 1301 const ELFDynamic *symbol = FindDynamicSymbol(DT_PLTREL); 1302 1303 if (symbol) 1304 return symbol->d_val; 1305 1306 return 0; 1307 } 1308 1309 static unsigned 1310 ParsePLTRelocations(Symtab *symbol_table, 1311 user_id_t start_id, 1312 unsigned rel_type, 1313 const ELFHeader *hdr, 1314 const ELFSectionHeader *rel_hdr, 1315 const ELFSectionHeader *plt_hdr, 1316 const ELFSectionHeader *sym_hdr, 1317 const lldb::SectionSP &plt_section_sp, 1318 DataExtractor &rel_data, 1319 DataExtractor &symtab_data, 1320 DataExtractor &strtab_data) 1321 { 1322 ELFRelocation rel(rel_type); 1323 ELFSymbol symbol; 1324 lldb::offset_t offset = 0; 1325 // Clang 3.3 sets entsize to 4 for 32-bit binaries, but the plt entries are 16 bytes. 1326 // So round the entsize up by the alignment if addralign is set. 1327 const elf_xword plt_entsize = plt_hdr->sh_addralign ? 1328 llvm::RoundUpToAlignment (plt_hdr->sh_entsize, plt_hdr->sh_addralign) : plt_hdr->sh_entsize; 1329 const elf_xword num_relocations = rel_hdr->sh_size / rel_hdr->sh_entsize; 1330 1331 typedef unsigned (*reloc_info_fn)(const ELFRelocation &rel); 1332 reloc_info_fn reloc_type; 1333 reloc_info_fn reloc_symbol; 1334 1335 if (hdr->Is32Bit()) 1336 { 1337 reloc_type = ELFRelocation::RelocType32; 1338 reloc_symbol = ELFRelocation::RelocSymbol32; 1339 } 1340 else 1341 { 1342 reloc_type = ELFRelocation::RelocType64; 1343 reloc_symbol = ELFRelocation::RelocSymbol64; 1344 } 1345 1346 unsigned slot_type = hdr->GetRelocationJumpSlotType(); 1347 unsigned i; 1348 for (i = 0; i < num_relocations; ++i) 1349 { 1350 if (rel.Parse(rel_data, &offset) == false) 1351 break; 1352 1353 if (reloc_type(rel) != slot_type) 1354 continue; 1355 1356 lldb::offset_t symbol_offset = reloc_symbol(rel) * sym_hdr->sh_entsize; 1357 uint64_t plt_index = (i + 1) * plt_entsize; 1358 1359 if (!symbol.Parse(symtab_data, &symbol_offset)) 1360 break; 1361 1362 const char *symbol_name = strtab_data.PeekCStr(symbol.st_name); 1363 bool is_mangled = symbol_name ? (symbol_name[0] == '_' && symbol_name[1] == 'Z') : false; 1364 1365 Symbol jump_symbol( 1366 i + start_id, // Symbol table index 1367 symbol_name, // symbol name. 1368 is_mangled, // is the symbol name mangled? 1369 eSymbolTypeTrampoline, // Type of this symbol 1370 false, // Is this globally visible? 1371 false, // Is this symbol debug info? 1372 true, // Is this symbol a trampoline? 1373 true, // Is this symbol artificial? 1374 plt_section_sp, // Section in which this symbol is defined or null. 1375 plt_index, // Offset in section or symbol value. 1376 plt_entsize, // Size in bytes of this symbol. 1377 true, // Size is valid 1378 0); // Symbol flags. 1379 1380 symbol_table->AddSymbol(jump_symbol); 1381 } 1382 1383 return i; 1384 } 1385 1386 unsigned 1387 ObjectFileELF::ParseTrampolineSymbols(Symtab *symbol_table, 1388 user_id_t start_id, 1389 const ELFSectionHeaderInfo *rel_hdr, 1390 user_id_t rel_id) 1391 { 1392 assert(rel_hdr->sh_type == SHT_RELA || rel_hdr->sh_type == SHT_REL); 1393 1394 // The link field points to the associated symbol table. The info field 1395 // points to the section holding the plt. 1396 user_id_t symtab_id = rel_hdr->sh_link; 1397 user_id_t plt_id = rel_hdr->sh_info; 1398 1399 if (!symtab_id || !plt_id) 1400 return 0; 1401 1402 // Section ID's are ones based; 1403 symtab_id++; 1404 plt_id++; 1405 1406 const ELFSectionHeaderInfo *plt_hdr = GetSectionHeaderByIndex(plt_id); 1407 if (!plt_hdr) 1408 return 0; 1409 1410 const ELFSectionHeaderInfo *sym_hdr = GetSectionHeaderByIndex(symtab_id); 1411 if (!sym_hdr) 1412 return 0; 1413 1414 SectionList *section_list = m_sections_ap.get(); 1415 if (!section_list) 1416 return 0; 1417 1418 Section *rel_section = section_list->FindSectionByID(rel_id).get(); 1419 if (!rel_section) 1420 return 0; 1421 1422 SectionSP plt_section_sp (section_list->FindSectionByID(plt_id)); 1423 if (!plt_section_sp) 1424 return 0; 1425 1426 Section *symtab = section_list->FindSectionByID(symtab_id).get(); 1427 if (!symtab) 1428 return 0; 1429 1430 // sh_link points to associated string table. 1431 Section *strtab = section_list->FindSectionByID(sym_hdr->sh_link + 1).get(); 1432 if (!strtab) 1433 return 0; 1434 1435 DataExtractor rel_data; 1436 if (!ReadSectionData(rel_section, rel_data)) 1437 return 0; 1438 1439 DataExtractor symtab_data; 1440 if (!ReadSectionData(symtab, symtab_data)) 1441 return 0; 1442 1443 DataExtractor strtab_data; 1444 if (!ReadSectionData(strtab, strtab_data)) 1445 return 0; 1446 1447 unsigned rel_type = PLTRelocationType(); 1448 if (!rel_type) 1449 return 0; 1450 1451 return ParsePLTRelocations (symbol_table, 1452 start_id, 1453 rel_type, 1454 &m_header, 1455 rel_hdr, 1456 plt_hdr, 1457 sym_hdr, 1458 plt_section_sp, 1459 rel_data, 1460 symtab_data, 1461 strtab_data); 1462 } 1463 1464 Symtab * 1465 ObjectFileELF::GetSymtab() 1466 { 1467 ModuleSP module_sp(GetModule()); 1468 if (!module_sp) 1469 return NULL; 1470 1471 // We always want to use the main object file so we (hopefully) only have one cached copy 1472 // of our symtab, dynamic sections, etc. 1473 ObjectFile *module_obj_file = module_sp->GetObjectFile(); 1474 if (module_obj_file && module_obj_file != this) 1475 return module_obj_file->GetSymtab(); 1476 1477 if (m_symtab_ap.get() == NULL) 1478 { 1479 SectionList *section_list = GetSectionList(); 1480 if (!section_list) 1481 return NULL; 1482 1483 uint64_t symbol_id = 0; 1484 lldb_private::Mutex::Locker locker(module_sp->GetMutex()); 1485 1486 m_symtab_ap.reset(new Symtab(this)); 1487 1488 // Sharable objects and dynamic executables usually have 2 distinct symbol 1489 // tables, one named ".symtab", and the other ".dynsym". The dynsym is a smaller 1490 // version of the symtab that only contains global symbols. The information found 1491 // in the dynsym is therefore also found in the symtab, while the reverse is not 1492 // necessarily true. 1493 Section *symtab = section_list->FindSectionByType (eSectionTypeELFSymbolTable, true).get(); 1494 if (!symtab) 1495 { 1496 // The symtab section is non-allocable and can be stripped, so if it doesn't exist 1497 // then use the dynsym section which should always be there. 1498 symtab = section_list->FindSectionByType (eSectionTypeELFDynamicSymbols, true).get(); 1499 } 1500 if (symtab) 1501 symbol_id += ParseSymbolTable (m_symtab_ap.get(), symbol_id, symtab); 1502 1503 // DT_JMPREL 1504 // If present, this entry's d_ptr member holds the address of relocation 1505 // entries associated solely with the procedure linkage table. Separating 1506 // these relocation entries lets the dynamic linker ignore them during 1507 // process initialization, if lazy binding is enabled. If this entry is 1508 // present, the related entries of types DT_PLTRELSZ and DT_PLTREL must 1509 // also be present. 1510 const ELFDynamic *symbol = FindDynamicSymbol(DT_JMPREL); 1511 if (symbol) 1512 { 1513 // Synthesize trampoline symbols to help navigate the PLT. 1514 addr_t addr = symbol->d_ptr; 1515 Section *reloc_section = section_list->FindSectionContainingFileAddress(addr).get(); 1516 if (reloc_section) 1517 { 1518 user_id_t reloc_id = reloc_section->GetID(); 1519 const ELFSectionHeaderInfo *reloc_header = GetSectionHeaderByIndex(reloc_id); 1520 assert(reloc_header); 1521 1522 ParseTrampolineSymbols (m_symtab_ap.get(), symbol_id, reloc_header, reloc_id); 1523 } 1524 } 1525 } 1526 return m_symtab_ap.get(); 1527 } 1528 1529 Symbol * 1530 ObjectFileELF::ResolveSymbolForAddress(const Address& so_addr, bool verify_unique) 1531 { 1532 if (!m_symtab_ap.get()) 1533 return nullptr; // GetSymtab() should be called first. 1534 1535 const SectionList *section_list = GetSectionList(); 1536 if (!section_list) 1537 return nullptr; 1538 1539 if (DWARFCallFrameInfo *eh_frame = GetUnwindTable().GetEHFrameInfo()) 1540 { 1541 AddressRange range; 1542 if (eh_frame->GetAddressRange (so_addr, range)) 1543 { 1544 const addr_t file_addr = range.GetBaseAddress().GetFileAddress(); 1545 Symbol * symbol = verify_unique ? m_symtab_ap->FindSymbolContainingFileAddress(file_addr) : nullptr; 1546 if (symbol) 1547 return symbol; 1548 1549 // Note that a (stripped) symbol won't be found by GetSymtab()... 1550 lldb::SectionSP eh_sym_section_sp = section_list->FindSectionContainingFileAddress(file_addr); 1551 if (eh_sym_section_sp.get()) 1552 { 1553 addr_t section_base = eh_sym_section_sp->GetFileAddress(); 1554 addr_t offset = file_addr - section_base; 1555 uint64_t symbol_id = m_symtab_ap->GetNumSymbols(); 1556 1557 Symbol eh_symbol( 1558 symbol_id, // Symbol table index. 1559 "???", // Symbol name. 1560 false, // Is the symbol name mangled? 1561 eSymbolTypeCode, // Type of this symbol. 1562 true, // Is this globally visible? 1563 false, // Is this symbol debug info? 1564 false, // Is this symbol a trampoline? 1565 true, // Is this symbol artificial? 1566 eh_sym_section_sp, // Section in which this symbol is defined or null. 1567 offset, // Offset in section or symbol value. 1568 range.GetByteSize(), // Size in bytes of this symbol. 1569 true, // Size is valid. 1570 0); // Symbol flags. 1571 if (symbol_id == m_symtab_ap->AddSymbol(eh_symbol)) 1572 return m_symtab_ap->SymbolAtIndex(symbol_id); 1573 } 1574 } 1575 } 1576 return nullptr; 1577 } 1578 1579 1580 bool 1581 ObjectFileELF::IsStripped () 1582 { 1583 // TODO: determine this for ELF 1584 return false; 1585 } 1586 1587 //===----------------------------------------------------------------------===// 1588 // Dump 1589 // 1590 // Dump the specifics of the runtime file container (such as any headers 1591 // segments, sections, etc). 1592 //---------------------------------------------------------------------- 1593 void 1594 ObjectFileELF::Dump(Stream *s) 1595 { 1596 DumpELFHeader(s, m_header); 1597 s->EOL(); 1598 DumpELFProgramHeaders(s); 1599 s->EOL(); 1600 DumpELFSectionHeaders(s); 1601 s->EOL(); 1602 SectionList *section_list = GetSectionList(); 1603 if (section_list) 1604 section_list->Dump(s, NULL, true, UINT32_MAX); 1605 Symtab *symtab = GetSymtab(); 1606 if (symtab) 1607 symtab->Dump(s, NULL, eSortOrderNone); 1608 s->EOL(); 1609 DumpDependentModules(s); 1610 s->EOL(); 1611 } 1612 1613 //---------------------------------------------------------------------- 1614 // DumpELFHeader 1615 // 1616 // Dump the ELF header to the specified output stream 1617 //---------------------------------------------------------------------- 1618 void 1619 ObjectFileELF::DumpELFHeader(Stream *s, const ELFHeader &header) 1620 { 1621 s->PutCString("ELF Header\n"); 1622 s->Printf("e_ident[EI_MAG0 ] = 0x%2.2x\n", header.e_ident[EI_MAG0]); 1623 s->Printf("e_ident[EI_MAG1 ] = 0x%2.2x '%c'\n", 1624 header.e_ident[EI_MAG1], header.e_ident[EI_MAG1]); 1625 s->Printf("e_ident[EI_MAG2 ] = 0x%2.2x '%c'\n", 1626 header.e_ident[EI_MAG2], header.e_ident[EI_MAG2]); 1627 s->Printf("e_ident[EI_MAG3 ] = 0x%2.2x '%c'\n", 1628 header.e_ident[EI_MAG3], header.e_ident[EI_MAG3]); 1629 1630 s->Printf("e_ident[EI_CLASS ] = 0x%2.2x\n", header.e_ident[EI_CLASS]); 1631 s->Printf("e_ident[EI_DATA ] = 0x%2.2x ", header.e_ident[EI_DATA]); 1632 DumpELFHeader_e_ident_EI_DATA(s, header.e_ident[EI_DATA]); 1633 s->Printf ("\ne_ident[EI_VERSION] = 0x%2.2x\n", header.e_ident[EI_VERSION]); 1634 s->Printf ("e_ident[EI_PAD ] = 0x%2.2x\n", header.e_ident[EI_PAD]); 1635 1636 s->Printf("e_type = 0x%4.4x ", header.e_type); 1637 DumpELFHeader_e_type(s, header.e_type); 1638 s->Printf("\ne_machine = 0x%4.4x\n", header.e_machine); 1639 s->Printf("e_version = 0x%8.8x\n", header.e_version); 1640 s->Printf("e_entry = 0x%8.8" PRIx64 "\n", header.e_entry); 1641 s->Printf("e_phoff = 0x%8.8" PRIx64 "\n", header.e_phoff); 1642 s->Printf("e_shoff = 0x%8.8" PRIx64 "\n", header.e_shoff); 1643 s->Printf("e_flags = 0x%8.8x\n", header.e_flags); 1644 s->Printf("e_ehsize = 0x%4.4x\n", header.e_ehsize); 1645 s->Printf("e_phentsize = 0x%4.4x\n", header.e_phentsize); 1646 s->Printf("e_phnum = 0x%4.4x\n", header.e_phnum); 1647 s->Printf("e_shentsize = 0x%4.4x\n", header.e_shentsize); 1648 s->Printf("e_shnum = 0x%4.4x\n", header.e_shnum); 1649 s->Printf("e_shstrndx = 0x%4.4x\n", header.e_shstrndx); 1650 } 1651 1652 //---------------------------------------------------------------------- 1653 // DumpELFHeader_e_type 1654 // 1655 // Dump an token value for the ELF header member e_type 1656 //---------------------------------------------------------------------- 1657 void 1658 ObjectFileELF::DumpELFHeader_e_type(Stream *s, elf_half e_type) 1659 { 1660 switch (e_type) 1661 { 1662 case ET_NONE: *s << "ET_NONE"; break; 1663 case ET_REL: *s << "ET_REL"; break; 1664 case ET_EXEC: *s << "ET_EXEC"; break; 1665 case ET_DYN: *s << "ET_DYN"; break; 1666 case ET_CORE: *s << "ET_CORE"; break; 1667 default: 1668 break; 1669 } 1670 } 1671 1672 //---------------------------------------------------------------------- 1673 // DumpELFHeader_e_ident_EI_DATA 1674 // 1675 // Dump an token value for the ELF header member e_ident[EI_DATA] 1676 //---------------------------------------------------------------------- 1677 void 1678 ObjectFileELF::DumpELFHeader_e_ident_EI_DATA(Stream *s, unsigned char ei_data) 1679 { 1680 switch (ei_data) 1681 { 1682 case ELFDATANONE: *s << "ELFDATANONE"; break; 1683 case ELFDATA2LSB: *s << "ELFDATA2LSB - Little Endian"; break; 1684 case ELFDATA2MSB: *s << "ELFDATA2MSB - Big Endian"; break; 1685 default: 1686 break; 1687 } 1688 } 1689 1690 1691 //---------------------------------------------------------------------- 1692 // DumpELFProgramHeader 1693 // 1694 // Dump a single ELF program header to the specified output stream 1695 //---------------------------------------------------------------------- 1696 void 1697 ObjectFileELF::DumpELFProgramHeader(Stream *s, const ELFProgramHeader &ph) 1698 { 1699 DumpELFProgramHeader_p_type(s, ph.p_type); 1700 s->Printf(" %8.8" PRIx64 " %8.8" PRIx64 " %8.8" PRIx64, ph.p_offset, ph.p_vaddr, ph.p_paddr); 1701 s->Printf(" %8.8" PRIx64 " %8.8" PRIx64 " %8.8x (", ph.p_filesz, ph.p_memsz, ph.p_flags); 1702 1703 DumpELFProgramHeader_p_flags(s, ph.p_flags); 1704 s->Printf(") %8.8" PRIx64, ph.p_align); 1705 } 1706 1707 //---------------------------------------------------------------------- 1708 // DumpELFProgramHeader_p_type 1709 // 1710 // Dump an token value for the ELF program header member p_type which 1711 // describes the type of the program header 1712 // ---------------------------------------------------------------------- 1713 void 1714 ObjectFileELF::DumpELFProgramHeader_p_type(Stream *s, elf_word p_type) 1715 { 1716 const int kStrWidth = 15; 1717 switch (p_type) 1718 { 1719 CASE_AND_STREAM(s, PT_NULL , kStrWidth); 1720 CASE_AND_STREAM(s, PT_LOAD , kStrWidth); 1721 CASE_AND_STREAM(s, PT_DYNAMIC , kStrWidth); 1722 CASE_AND_STREAM(s, PT_INTERP , kStrWidth); 1723 CASE_AND_STREAM(s, PT_NOTE , kStrWidth); 1724 CASE_AND_STREAM(s, PT_SHLIB , kStrWidth); 1725 CASE_AND_STREAM(s, PT_PHDR , kStrWidth); 1726 CASE_AND_STREAM(s, PT_TLS , kStrWidth); 1727 CASE_AND_STREAM(s, PT_GNU_EH_FRAME, kStrWidth); 1728 default: 1729 s->Printf("0x%8.8x%*s", p_type, kStrWidth - 10, ""); 1730 break; 1731 } 1732 } 1733 1734 1735 //---------------------------------------------------------------------- 1736 // DumpELFProgramHeader_p_flags 1737 // 1738 // Dump an token value for the ELF program header member p_flags 1739 //---------------------------------------------------------------------- 1740 void 1741 ObjectFileELF::DumpELFProgramHeader_p_flags(Stream *s, elf_word p_flags) 1742 { 1743 *s << ((p_flags & PF_X) ? "PF_X" : " ") 1744 << (((p_flags & PF_X) && (p_flags & PF_W)) ? '+' : ' ') 1745 << ((p_flags & PF_W) ? "PF_W" : " ") 1746 << (((p_flags & PF_W) && (p_flags & PF_R)) ? '+' : ' ') 1747 << ((p_flags & PF_R) ? "PF_R" : " "); 1748 } 1749 1750 //---------------------------------------------------------------------- 1751 // DumpELFProgramHeaders 1752 // 1753 // Dump all of the ELF program header to the specified output stream 1754 //---------------------------------------------------------------------- 1755 void 1756 ObjectFileELF::DumpELFProgramHeaders(Stream *s) 1757 { 1758 if (ParseProgramHeaders()) 1759 { 1760 s->PutCString("Program Headers\n"); 1761 s->PutCString("IDX p_type p_offset p_vaddr p_paddr " 1762 "p_filesz p_memsz p_flags p_align\n"); 1763 s->PutCString("==== --------------- -------- -------- -------- " 1764 "-------- -------- ------------------------- --------\n"); 1765 1766 uint32_t idx = 0; 1767 for (ProgramHeaderCollConstIter I = m_program_headers.begin(); 1768 I != m_program_headers.end(); ++I, ++idx) 1769 { 1770 s->Printf("[%2u] ", idx); 1771 ObjectFileELF::DumpELFProgramHeader(s, *I); 1772 s->EOL(); 1773 } 1774 } 1775 } 1776 1777 //---------------------------------------------------------------------- 1778 // DumpELFSectionHeader 1779 // 1780 // Dump a single ELF section header to the specified output stream 1781 //---------------------------------------------------------------------- 1782 void 1783 ObjectFileELF::DumpELFSectionHeader(Stream *s, const ELFSectionHeaderInfo &sh) 1784 { 1785 s->Printf("%8.8x ", sh.sh_name); 1786 DumpELFSectionHeader_sh_type(s, sh.sh_type); 1787 s->Printf(" %8.8" PRIx64 " (", sh.sh_flags); 1788 DumpELFSectionHeader_sh_flags(s, sh.sh_flags); 1789 s->Printf(") %8.8" PRIx64 " %8.8" PRIx64 " %8.8" PRIx64, sh.sh_addr, sh.sh_offset, sh.sh_size); 1790 s->Printf(" %8.8x %8.8x", sh.sh_link, sh.sh_info); 1791 s->Printf(" %8.8" PRIx64 " %8.8" PRIx64, sh.sh_addralign, sh.sh_entsize); 1792 } 1793 1794 //---------------------------------------------------------------------- 1795 // DumpELFSectionHeader_sh_type 1796 // 1797 // Dump an token value for the ELF section header member sh_type which 1798 // describes the type of the section 1799 //---------------------------------------------------------------------- 1800 void 1801 ObjectFileELF::DumpELFSectionHeader_sh_type(Stream *s, elf_word sh_type) 1802 { 1803 const int kStrWidth = 12; 1804 switch (sh_type) 1805 { 1806 CASE_AND_STREAM(s, SHT_NULL , kStrWidth); 1807 CASE_AND_STREAM(s, SHT_PROGBITS , kStrWidth); 1808 CASE_AND_STREAM(s, SHT_SYMTAB , kStrWidth); 1809 CASE_AND_STREAM(s, SHT_STRTAB , kStrWidth); 1810 CASE_AND_STREAM(s, SHT_RELA , kStrWidth); 1811 CASE_AND_STREAM(s, SHT_HASH , kStrWidth); 1812 CASE_AND_STREAM(s, SHT_DYNAMIC , kStrWidth); 1813 CASE_AND_STREAM(s, SHT_NOTE , kStrWidth); 1814 CASE_AND_STREAM(s, SHT_NOBITS , kStrWidth); 1815 CASE_AND_STREAM(s, SHT_REL , kStrWidth); 1816 CASE_AND_STREAM(s, SHT_SHLIB , kStrWidth); 1817 CASE_AND_STREAM(s, SHT_DYNSYM , kStrWidth); 1818 CASE_AND_STREAM(s, SHT_LOPROC , kStrWidth); 1819 CASE_AND_STREAM(s, SHT_HIPROC , kStrWidth); 1820 CASE_AND_STREAM(s, SHT_LOUSER , kStrWidth); 1821 CASE_AND_STREAM(s, SHT_HIUSER , kStrWidth); 1822 default: 1823 s->Printf("0x%8.8x%*s", sh_type, kStrWidth - 10, ""); 1824 break; 1825 } 1826 } 1827 1828 //---------------------------------------------------------------------- 1829 // DumpELFSectionHeader_sh_flags 1830 // 1831 // Dump an token value for the ELF section header member sh_flags 1832 //---------------------------------------------------------------------- 1833 void 1834 ObjectFileELF::DumpELFSectionHeader_sh_flags(Stream *s, elf_xword sh_flags) 1835 { 1836 *s << ((sh_flags & SHF_WRITE) ? "WRITE" : " ") 1837 << (((sh_flags & SHF_WRITE) && (sh_flags & SHF_ALLOC)) ? '+' : ' ') 1838 << ((sh_flags & SHF_ALLOC) ? "ALLOC" : " ") 1839 << (((sh_flags & SHF_ALLOC) && (sh_flags & SHF_EXECINSTR)) ? '+' : ' ') 1840 << ((sh_flags & SHF_EXECINSTR) ? "EXECINSTR" : " "); 1841 } 1842 1843 //---------------------------------------------------------------------- 1844 // DumpELFSectionHeaders 1845 // 1846 // Dump all of the ELF section header to the specified output stream 1847 //---------------------------------------------------------------------- 1848 void 1849 ObjectFileELF::DumpELFSectionHeaders(Stream *s) 1850 { 1851 if (!ParseSectionHeaders()) 1852 return; 1853 1854 s->PutCString("Section Headers\n"); 1855 s->PutCString("IDX name type flags " 1856 "addr offset size link info addralgn " 1857 "entsize Name\n"); 1858 s->PutCString("==== -------- ------------ -------------------------------- " 1859 "-------- -------- -------- -------- -------- -------- " 1860 "-------- ====================\n"); 1861 1862 uint32_t idx = 0; 1863 for (SectionHeaderCollConstIter I = m_section_headers.begin(); 1864 I != m_section_headers.end(); ++I, ++idx) 1865 { 1866 s->Printf("[%2u] ", idx); 1867 ObjectFileELF::DumpELFSectionHeader(s, *I); 1868 const char* section_name = I->section_name.AsCString(""); 1869 if (section_name) 1870 *s << ' ' << section_name << "\n"; 1871 } 1872 } 1873 1874 void 1875 ObjectFileELF::DumpDependentModules(lldb_private::Stream *s) 1876 { 1877 size_t num_modules = ParseDependentModules(); 1878 1879 if (num_modules > 0) 1880 { 1881 s->PutCString("Dependent Modules:\n"); 1882 for (unsigned i = 0; i < num_modules; ++i) 1883 { 1884 const FileSpec &spec = m_filespec_ap->GetFileSpecAtIndex(i); 1885 s->Printf(" %s\n", spec.GetFilename().GetCString()); 1886 } 1887 } 1888 } 1889 1890 bool 1891 ObjectFileELF::GetArchitecture (ArchSpec &arch) 1892 { 1893 if (!ParseHeader()) 1894 return false; 1895 1896 arch.SetArchitecture (eArchTypeELF, m_header.e_machine, LLDB_INVALID_CPUTYPE); 1897 arch.GetTriple().setOSName (Host::GetOSString().GetCString()); 1898 arch.GetTriple().setVendorName(Host::GetVendorString().GetCString()); 1899 return true; 1900 } 1901 1902 ObjectFile::Type 1903 ObjectFileELF::CalculateType() 1904 { 1905 switch (m_header.e_type) 1906 { 1907 case llvm::ELF::ET_NONE: 1908 // 0 - No file type 1909 return eTypeUnknown; 1910 1911 case llvm::ELF::ET_REL: 1912 // 1 - Relocatable file 1913 return eTypeObjectFile; 1914 1915 case llvm::ELF::ET_EXEC: 1916 // 2 - Executable file 1917 return eTypeExecutable; 1918 1919 case llvm::ELF::ET_DYN: 1920 // 3 - Shared object file 1921 return eTypeSharedLibrary; 1922 1923 case ET_CORE: 1924 // 4 - Core file 1925 return eTypeCoreFile; 1926 1927 default: 1928 break; 1929 } 1930 return eTypeUnknown; 1931 } 1932 1933 ObjectFile::Strata 1934 ObjectFileELF::CalculateStrata() 1935 { 1936 switch (m_header.e_type) 1937 { 1938 case llvm::ELF::ET_NONE: 1939 // 0 - No file type 1940 return eStrataUnknown; 1941 1942 case llvm::ELF::ET_REL: 1943 // 1 - Relocatable file 1944 return eStrataUnknown; 1945 1946 case llvm::ELF::ET_EXEC: 1947 // 2 - Executable file 1948 // TODO: is there any way to detect that an executable is a kernel 1949 // related executable by inspecting the program headers, section 1950 // headers, symbols, or any other flag bits??? 1951 return eStrataUser; 1952 1953 case llvm::ELF::ET_DYN: 1954 // 3 - Shared object file 1955 // TODO: is there any way to detect that an shared library is a kernel 1956 // related executable by inspecting the program headers, section 1957 // headers, symbols, or any other flag bits??? 1958 return eStrataUnknown; 1959 1960 case ET_CORE: 1961 // 4 - Core file 1962 // TODO: is there any way to detect that an core file is a kernel 1963 // related executable by inspecting the program headers, section 1964 // headers, symbols, or any other flag bits??? 1965 return eStrataUnknown; 1966 1967 default: 1968 break; 1969 } 1970 return eStrataUnknown; 1971 } 1972 1973