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