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/PluginManager.h" 20 #include "lldb/Core/Section.h" 21 #include "lldb/Core/Stream.h" 22 #include "lldb/Host/Host.h" 23 24 #define CASE_AND_STREAM(s, def, width) \ 25 case def: s->Printf("%-*s", width, #def); break; 26 27 using namespace lldb; 28 using namespace lldb_private; 29 using namespace elf; 30 using namespace llvm::ELF; 31 32 //------------------------------------------------------------------ 33 // Static methods. 34 //------------------------------------------------------------------ 35 void 36 ObjectFileELF::Initialize() 37 { 38 PluginManager::RegisterPlugin(GetPluginNameStatic(), 39 GetPluginDescriptionStatic(), 40 CreateInstance); 41 } 42 43 void 44 ObjectFileELF::Terminate() 45 { 46 PluginManager::UnregisterPlugin(CreateInstance); 47 } 48 49 const char * 50 ObjectFileELF::GetPluginNameStatic() 51 { 52 return "object-file.elf"; 53 } 54 55 const char * 56 ObjectFileELF::GetPluginDescriptionStatic() 57 { 58 return "ELF object file reader."; 59 } 60 61 ObjectFile * 62 ObjectFileELF::CreateInstance(Module *module, 63 DataBufferSP &data_sp, 64 const FileSpec *file, addr_t offset, 65 addr_t length) 66 { 67 if (data_sp && data_sp->GetByteSize() > (llvm::ELF::EI_NIDENT + offset)) 68 { 69 const uint8_t *magic = data_sp->GetBytes() + offset; 70 if (ELFHeader::MagicBytesMatch(magic)) 71 { 72 unsigned address_size = ELFHeader::AddressSizeInBytes(magic); 73 if (address_size == 4 || address_size == 8) 74 { 75 std::auto_ptr<ObjectFileELF> objfile_ap( 76 new ObjectFileELF(module, data_sp, file, offset, length)); 77 ArchSpec spec = objfile_ap->GetArchitecture(); 78 if (spec.IsValid() && objfile_ap->SetModulesArchitecture(spec)) 79 return objfile_ap.release(); 80 } 81 } 82 } 83 return NULL; 84 } 85 86 ArchSpec 87 ObjectFileELF::GetArchitecture() 88 { 89 if (!ParseHeader()) 90 return ArchSpec(); 91 92 return ArchSpec(eArchTypeELF, m_header.e_machine, m_header.e_flags); 93 } 94 95 //------------------------------------------------------------------ 96 // PluginInterface protocol 97 //------------------------------------------------------------------ 98 const char * 99 ObjectFileELF::GetPluginName() 100 { 101 return "ObjectFileELF"; 102 } 103 104 const char * 105 ObjectFileELF::GetShortPluginName() 106 { 107 return GetPluginNameStatic(); 108 } 109 110 uint32_t 111 ObjectFileELF::GetPluginVersion() 112 { 113 return m_plugin_version; 114 } 115 116 void 117 ObjectFileELF::GetPluginCommandHelp(const char *command, Stream *strm) 118 { 119 } 120 121 Error 122 ObjectFileELF::ExecutePluginCommand(Args &command, Stream *strm) 123 { 124 Error error; 125 error.SetErrorString("No plug-in commands are currently supported."); 126 return error; 127 } 128 129 Log * 130 ObjectFileELF::EnablePluginLogging(Stream *strm, Args &command) 131 { 132 return NULL; 133 } 134 135 //------------------------------------------------------------------ 136 // ObjectFile protocol 137 //------------------------------------------------------------------ 138 139 ObjectFileELF::ObjectFileELF(Module* module, DataBufferSP& dataSP, 140 const FileSpec* file, addr_t offset, 141 addr_t length) 142 : ObjectFile(module, file, offset, length, dataSP), 143 m_header(), 144 m_program_headers(), 145 m_section_headers(), 146 m_sections_ap(), 147 m_symtab_ap(), 148 m_filespec_ap(), 149 m_shstr_data() 150 { 151 if (file) 152 m_file = *file; 153 ::memset(&m_header, 0, sizeof(m_header)); 154 } 155 156 ObjectFileELF::~ObjectFileELF() 157 { 158 } 159 160 bool 161 ObjectFileELF::IsExecutable() const 162 { 163 return m_header.e_entry != 0; 164 } 165 166 Address 167 ObjectFileELF::GetEntryPoint() const 168 { 169 if (m_header.e_entry) 170 return Address(NULL, m_header.e_entry); 171 else 172 return Address(); 173 } 174 175 ByteOrder 176 ObjectFileELF::GetByteOrder() const 177 { 178 if (m_header.e_ident[EI_DATA] == ELFDATA2MSB) 179 return eByteOrderBig; 180 if (m_header.e_ident[EI_DATA] == ELFDATA2LSB) 181 return eByteOrderLittle; 182 return eByteOrderInvalid; 183 } 184 185 size_t 186 ObjectFileELF::GetAddressByteSize() const 187 { 188 return m_data.GetAddressByteSize(); 189 } 190 191 unsigned 192 ObjectFileELF::SectionIndex(const SectionHeaderCollIter &I) 193 { 194 return std::distance(m_section_headers.begin(), I) + 1; 195 } 196 197 unsigned 198 ObjectFileELF::SectionIndex(const SectionHeaderCollConstIter &I) const 199 { 200 return std::distance(m_section_headers.begin(), I) + 1; 201 } 202 203 bool 204 ObjectFileELF::ParseHeader() 205 { 206 uint32_t offset = GetOffset(); 207 return m_header.Parse(m_data, &offset); 208 } 209 210 bool 211 ObjectFileELF::GetUUID(lldb_private::UUID* uuid) 212 { 213 // FIXME: Return MD5 sum here. See comment in ObjectFile.h. 214 return false; 215 } 216 217 uint32_t 218 ObjectFileELF::GetDependentModules(FileSpecList &files) 219 { 220 size_t num_modules = ParseDependentModules(); 221 uint32_t num_specs = 0; 222 223 for (unsigned i = 0; i < num_modules; ++i) 224 { 225 if (files.AppendIfUnique(m_filespec_ap->GetFileSpecAtIndex(i))) 226 num_specs++; 227 } 228 229 return num_specs; 230 } 231 232 Address 233 ObjectFileELF::GetImageInfoAddress() 234 { 235 if (!ParseSectionHeaders()) 236 return Address(); 237 238 user_id_t dynsym_id = 0; 239 for (SectionHeaderCollIter sh_pos = m_section_headers.begin(); 240 sh_pos != m_section_headers.end(); ++sh_pos) 241 { 242 if (sh_pos->sh_type == SHT_DYNAMIC) 243 { 244 dynsym_id = SectionIndex(sh_pos); 245 break; 246 } 247 } 248 249 if (!dynsym_id) 250 return Address(); 251 252 SectionList *section_list = GetSectionList(); 253 if (!section_list) 254 return Address(); 255 256 // Resolve the dynamic table entries. 257 Section *dynsym = section_list->FindSectionByID(dynsym_id).get(); 258 if (!dynsym) 259 return Address(); 260 261 DataExtractor dynsym_data; 262 if (dynsym->ReadSectionDataFromObjectFile(this, dynsym_data)) 263 { 264 ELFDynamic symbol; 265 const unsigned section_size = dynsym_data.GetByteSize(); 266 unsigned offset = 0; 267 unsigned cursor = 0; 268 269 // Look for a DT_DEBUG entry. 270 while (cursor < section_size) 271 { 272 offset = cursor; 273 if (!symbol.Parse(dynsym_data, &cursor)) 274 break; 275 276 if (symbol.d_tag != DT_DEBUG) 277 continue; 278 279 return Address(dynsym, offset + sizeof(symbol.d_tag)); 280 } 281 } 282 283 return Address(); 284 } 285 286 //---------------------------------------------------------------------- 287 // ParseDependentModules 288 //---------------------------------------------------------------------- 289 size_t 290 ObjectFileELF::ParseDependentModules() 291 { 292 if (m_filespec_ap.get()) 293 return m_filespec_ap->GetSize(); 294 295 m_filespec_ap.reset(new FileSpecList()); 296 297 if (!(ParseSectionHeaders() && GetSectionHeaderStringTable())) 298 return 0; 299 300 // Locate the dynamic table. 301 user_id_t dynsym_id = 0; 302 user_id_t dynstr_id = 0; 303 for (SectionHeaderCollIter sh_pos = m_section_headers.begin(); 304 sh_pos != m_section_headers.end(); ++sh_pos) 305 { 306 if (sh_pos->sh_type == SHT_DYNAMIC) 307 { 308 dynsym_id = SectionIndex(sh_pos); 309 dynstr_id = sh_pos->sh_link + 1; // Section ID's are 1 based. 310 break; 311 } 312 } 313 314 if (!(dynsym_id && dynstr_id)) 315 return 0; 316 317 SectionList *section_list = GetSectionList(); 318 if (!section_list) 319 return 0; 320 321 // Resolve and load the dynamic table entries and corresponding string 322 // table. 323 Section *dynsym = section_list->FindSectionByID(dynsym_id).get(); 324 Section *dynstr = section_list->FindSectionByID(dynstr_id).get(); 325 if (!(dynsym && dynstr)) 326 return 0; 327 328 DataExtractor dynsym_data; 329 DataExtractor dynstr_data; 330 if (dynsym->ReadSectionDataFromObjectFile(this, dynsym_data) && 331 dynstr->ReadSectionDataFromObjectFile(this, dynstr_data)) 332 { 333 ELFDynamic symbol; 334 const unsigned section_size = dynsym_data.GetByteSize(); 335 unsigned offset = 0; 336 337 // The only type of entries we are concerned with are tagged DT_NEEDED, 338 // yielding the name of a required library. 339 while (offset < section_size) 340 { 341 if (!symbol.Parse(dynsym_data, &offset)) 342 break; 343 344 if (symbol.d_tag != DT_NEEDED) 345 continue; 346 347 uint32_t str_index = static_cast<uint32_t>(symbol.d_val); 348 const char *lib_name = dynstr_data.PeekCStr(str_index); 349 m_filespec_ap->Append(FileSpec(lib_name, true)); 350 } 351 } 352 353 return m_filespec_ap->GetSize(); 354 } 355 356 //---------------------------------------------------------------------- 357 // ParseProgramHeaders 358 //---------------------------------------------------------------------- 359 size_t 360 ObjectFileELF::ParseProgramHeaders() 361 { 362 // We have already parsed the program headers 363 if (!m_program_headers.empty()) 364 return m_program_headers.size(); 365 366 // If there are no program headers to read we are done. 367 if (m_header.e_phnum == 0) 368 return 0; 369 370 m_program_headers.resize(m_header.e_phnum); 371 if (m_program_headers.size() != m_header.e_phnum) 372 return 0; 373 374 const size_t ph_size = m_header.e_phnum * m_header.e_phentsize; 375 const elf_off ph_offset = m_offset + m_header.e_phoff; 376 DataBufferSP buffer_sp(m_file.ReadFileContents(ph_offset, ph_size)); 377 378 if (buffer_sp.get() == NULL || buffer_sp->GetByteSize() != ph_size) 379 return 0; 380 381 DataExtractor data(buffer_sp, m_data.GetByteOrder(), 382 m_data.GetAddressByteSize()); 383 384 uint32_t idx; 385 uint32_t offset; 386 for (idx = 0, offset = 0; idx < m_header.e_phnum; ++idx) 387 { 388 if (m_program_headers[idx].Parse(data, &offset) == false) 389 break; 390 } 391 392 if (idx < m_program_headers.size()) 393 m_program_headers.resize(idx); 394 395 return m_program_headers.size(); 396 } 397 398 //---------------------------------------------------------------------- 399 // ParseSectionHeaders 400 //---------------------------------------------------------------------- 401 size_t 402 ObjectFileELF::ParseSectionHeaders() 403 { 404 // We have already parsed the section headers 405 if (!m_section_headers.empty()) 406 return m_section_headers.size(); 407 408 // If there are no section headers we are done. 409 if (m_header.e_shnum == 0) 410 return 0; 411 412 m_section_headers.resize(m_header.e_shnum); 413 if (m_section_headers.size() != m_header.e_shnum) 414 return 0; 415 416 const size_t sh_size = m_header.e_shnum * m_header.e_shentsize; 417 const elf_off sh_offset = m_offset + m_header.e_shoff; 418 DataBufferSP buffer_sp(m_file.ReadFileContents(sh_offset, sh_size)); 419 420 if (buffer_sp.get() == NULL || buffer_sp->GetByteSize() != sh_size) 421 return 0; 422 423 DataExtractor data(buffer_sp, 424 m_data.GetByteOrder(), 425 m_data.GetAddressByteSize()); 426 427 uint32_t idx; 428 uint32_t offset; 429 for (idx = 0, offset = 0; idx < m_header.e_shnum; ++idx) 430 { 431 if (m_section_headers[idx].Parse(data, &offset) == false) 432 break; 433 } 434 if (idx < m_section_headers.size()) 435 m_section_headers.resize(idx); 436 437 return m_section_headers.size(); 438 } 439 440 size_t 441 ObjectFileELF::GetSectionHeaderStringTable() 442 { 443 if (m_shstr_data.GetByteSize() == 0) 444 { 445 const unsigned strtab_idx = m_header.e_shstrndx; 446 447 if (strtab_idx && strtab_idx < m_section_headers.size()) 448 { 449 const ELFSectionHeader &sheader = m_section_headers[strtab_idx]; 450 const size_t byte_size = sheader.sh_size; 451 const Elf64_Off offset = m_offset + sheader.sh_offset; 452 DataBufferSP buffer_sp(m_file.ReadFileContents(offset, byte_size)); 453 454 if (buffer_sp.get() == NULL || buffer_sp->GetByteSize() != byte_size) 455 return 0; 456 457 m_shstr_data.SetData(buffer_sp); 458 } 459 } 460 return m_shstr_data.GetByteSize(); 461 } 462 463 lldb::user_id_t 464 ObjectFileELF::GetSectionIndexByName(const char *name) 465 { 466 if (!(ParseSectionHeaders() && GetSectionHeaderStringTable())) 467 return 0; 468 469 // Search the collection of section headers for one with a matching name. 470 for (SectionHeaderCollIter I = m_section_headers.begin(); 471 I != m_section_headers.end(); ++I) 472 { 473 const char *sectionName = m_shstr_data.PeekCStr(I->sh_name); 474 475 if (!sectionName) 476 return 0; 477 478 if (strcmp(name, sectionName) != 0) 479 continue; 480 481 return SectionIndex(I); 482 } 483 484 return 0; 485 } 486 487 SectionList * 488 ObjectFileELF::GetSectionList() 489 { 490 if (m_sections_ap.get()) 491 return m_sections_ap.get(); 492 493 if (ParseSectionHeaders() && GetSectionHeaderStringTable()) 494 { 495 m_sections_ap.reset(new SectionList()); 496 497 for (SectionHeaderCollIter I = m_section_headers.begin(); 498 I != m_section_headers.end(); ++I) 499 { 500 const ELFSectionHeader &header = *I; 501 502 ConstString name(m_shstr_data.PeekCStr(header.sh_name)); 503 uint64_t size = header.sh_type == SHT_NOBITS ? 0 : header.sh_size; 504 505 static ConstString g_sect_name_text (".text"); 506 static ConstString g_sect_name_data (".data"); 507 static ConstString g_sect_name_bss (".bss"); 508 static ConstString g_sect_name_dwarf_debug_abbrev (".debug_abbrev"); 509 static ConstString g_sect_name_dwarf_debug_aranges (".debug_aranges"); 510 static ConstString g_sect_name_dwarf_debug_frame (".debug_frame"); 511 static ConstString g_sect_name_dwarf_debug_info (".debug_info"); 512 static ConstString g_sect_name_dwarf_debug_line (".debug_line"); 513 static ConstString g_sect_name_dwarf_debug_loc (".debug_loc"); 514 static ConstString g_sect_name_dwarf_debug_macinfo (".debug_macinfo"); 515 static ConstString g_sect_name_dwarf_debug_pubnames (".debug_pubnames"); 516 static ConstString g_sect_name_dwarf_debug_pubtypes (".debug_pubtypes"); 517 static ConstString g_sect_name_dwarf_debug_ranges (".debug_ranges"); 518 static ConstString g_sect_name_dwarf_debug_str (".debug_str"); 519 static ConstString g_sect_name_eh_frame (".eh_frame"); 520 521 SectionType sect_type = eSectionTypeOther; 522 523 if (name == g_sect_name_text) sect_type = eSectionTypeCode; 524 else if (name == g_sect_name_data) sect_type = eSectionTypeData; 525 else if (name == g_sect_name_bss) sect_type = eSectionTypeZeroFill; 526 else if (name == g_sect_name_dwarf_debug_abbrev) sect_type = eSectionTypeDWARFDebugAbbrev; 527 else if (name == g_sect_name_dwarf_debug_aranges) sect_type = eSectionTypeDWARFDebugAranges; 528 else if (name == g_sect_name_dwarf_debug_frame) sect_type = eSectionTypeDWARFDebugFrame; 529 else if (name == g_sect_name_dwarf_debug_info) sect_type = eSectionTypeDWARFDebugInfo; 530 else if (name == g_sect_name_dwarf_debug_line) sect_type = eSectionTypeDWARFDebugLine; 531 else if (name == g_sect_name_dwarf_debug_loc) sect_type = eSectionTypeDWARFDebugLoc; 532 else if (name == g_sect_name_dwarf_debug_macinfo) sect_type = eSectionTypeDWARFDebugMacInfo; 533 else if (name == g_sect_name_dwarf_debug_pubnames) sect_type = eSectionTypeDWARFDebugPubNames; 534 else if (name == g_sect_name_dwarf_debug_pubtypes) sect_type = eSectionTypeDWARFDebugPubTypes; 535 else if (name == g_sect_name_dwarf_debug_ranges) sect_type = eSectionTypeDWARFDebugRanges; 536 else if (name == g_sect_name_dwarf_debug_str) sect_type = eSectionTypeDWARFDebugStr; 537 else if (name == g_sect_name_eh_frame) sect_type = eSectionTypeEHFrame; 538 539 540 SectionSP section(new Section( 541 0, // Parent section. 542 GetModule(), // Module to which this section belongs. 543 SectionIndex(I), // Section ID. 544 name, // Section name. 545 sect_type, // Section type. 546 header.sh_addr, // VM address. 547 header.sh_size, // VM size in bytes of this section. 548 header.sh_offset, // Offset of this section in the file. 549 size, // Size of the section as found in the file. 550 header.sh_flags)); // Flags for this section. 551 552 m_sections_ap->AddSection(section); 553 } 554 } 555 556 return m_sections_ap.get(); 557 } 558 559 static void 560 ParseSymbols(Symtab *symtab, SectionList *section_list, 561 const ELFSectionHeader &symtab_shdr, 562 const DataExtractor &symtab_data, 563 const DataExtractor &strtab_data) 564 { 565 ELFSymbol symbol; 566 uint32_t offset = 0; 567 const unsigned numSymbols = 568 symtab_data.GetByteSize() / symtab_shdr.sh_entsize; 569 570 static ConstString text_section_name(".text"); 571 static ConstString init_section_name(".init"); 572 static ConstString fini_section_name(".fini"); 573 static ConstString ctors_section_name(".ctors"); 574 static ConstString dtors_section_name(".dtors"); 575 576 static ConstString data_section_name(".data"); 577 static ConstString rodata_section_name(".rodata"); 578 static ConstString rodata1_section_name(".rodata1"); 579 static ConstString data2_section_name(".data1"); 580 static ConstString bss_section_name(".bss"); 581 582 for (unsigned i = 0; i < numSymbols; ++i) 583 { 584 if (symbol.Parse(symtab_data, &offset) == false) 585 break; 586 587 Section *symbol_section = NULL; 588 SymbolType symbol_type = eSymbolTypeInvalid; 589 Elf64_Half symbol_idx = symbol.st_shndx; 590 591 switch (symbol_idx) 592 { 593 case SHN_ABS: 594 symbol_type = eSymbolTypeAbsolute; 595 break; 596 case SHN_UNDEF: 597 symbol_type = eSymbolTypeUndefined; 598 break; 599 default: 600 symbol_section = section_list->GetSectionAtIndex(symbol_idx).get(); 601 break; 602 } 603 604 switch (symbol.getType()) 605 { 606 default: 607 case STT_NOTYPE: 608 // The symbol's type is not specified. 609 break; 610 611 case STT_OBJECT: 612 // The symbol is associated with a data object, such as a variable, 613 // an array, etc. 614 symbol_type = eSymbolTypeData; 615 break; 616 617 case STT_FUNC: 618 // The symbol is associated with a function or other executable code. 619 symbol_type = eSymbolTypeCode; 620 break; 621 622 case STT_SECTION: 623 // The symbol is associated with a section. Symbol table entries of 624 // this type exist primarily for relocation and normally have 625 // STB_LOCAL binding. 626 break; 627 628 case STT_FILE: 629 // Conventionally, the symbol's name gives the name of the source 630 // file associated with the object file. A file symbol has STB_LOCAL 631 // binding, its section index is SHN_ABS, and it precedes the other 632 // STB_LOCAL symbols for the file, if it is present. 633 symbol_type = eSymbolTypeObjectFile; 634 break; 635 } 636 637 if (symbol_type == eSymbolTypeInvalid) 638 { 639 if (symbol_section) 640 { 641 const ConstString §_name = symbol_section->GetName(); 642 if (sect_name == text_section_name || 643 sect_name == init_section_name || 644 sect_name == fini_section_name || 645 sect_name == ctors_section_name || 646 sect_name == dtors_section_name) 647 { 648 symbol_type = eSymbolTypeCode; 649 } 650 else if (sect_name == data_section_name || 651 sect_name == data2_section_name || 652 sect_name == rodata_section_name || 653 sect_name == rodata1_section_name || 654 sect_name == bss_section_name) 655 { 656 symbol_type = eSymbolTypeData; 657 } 658 } 659 } 660 661 uint64_t symbol_value = symbol.st_value; 662 if (symbol_section != NULL) 663 symbol_value -= symbol_section->GetFileAddress(); 664 const char *symbol_name = strtab_data.PeekCStr(symbol.st_name); 665 bool is_global = symbol.getBinding() == STB_GLOBAL; 666 uint32_t flags = symbol.st_other << 8 | symbol.st_info; 667 668 Symbol dc_symbol( 669 i, // ID is the original symbol table index. 670 symbol_name, // Symbol name. 671 false, // Is the symbol name mangled? 672 symbol_type, // Type of this symbol 673 is_global, // Is this globally visible? 674 false, // Is this symbol debug info? 675 false, // Is this symbol a trampoline? 676 false, // Is this symbol artificial? 677 symbol_section, // Section in which this symbol is defined or null. 678 symbol_value, // Offset in section or symbol value. 679 symbol.st_size, // Size in bytes of this symbol. 680 flags); // Symbol flags. 681 symtab->AddSymbol(dc_symbol); 682 } 683 } 684 685 void 686 ObjectFileELF::ParseSymbolTable(Symtab *symbol_table, 687 const ELFSectionHeader &symtab_hdr, 688 user_id_t symtab_id) 689 { 690 assert(symtab_hdr.sh_type == SHT_SYMTAB || 691 symtab_hdr.sh_type == SHT_DYNSYM); 692 693 // Parse in the section list if needed. 694 SectionList *section_list = GetSectionList(); 695 if (!section_list) 696 return; 697 698 // Section ID's are ones based. 699 user_id_t strtab_id = symtab_hdr.sh_link + 1; 700 701 Section *symtab = section_list->FindSectionByID(symtab_id).get(); 702 Section *strtab = section_list->FindSectionByID(strtab_id).get(); 703 if (symtab && strtab) 704 { 705 DataExtractor symtab_data; 706 DataExtractor strtab_data; 707 if (symtab->ReadSectionDataFromObjectFile(this, symtab_data) && 708 strtab->ReadSectionDataFromObjectFile(this, strtab_data)) 709 { 710 ParseSymbols(symbol_table, section_list, symtab_hdr, 711 symtab_data, strtab_data); 712 } 713 } 714 } 715 716 Symtab * 717 ObjectFileELF::GetSymtab() 718 { 719 if (m_symtab_ap.get()) 720 return m_symtab_ap.get(); 721 722 Symtab *symbol_table = new Symtab(this); 723 m_symtab_ap.reset(symbol_table); 724 725 Mutex::Locker locker (symbol_table->GetMutex ()); 726 727 if (!(ParseSectionHeaders() && GetSectionHeaderStringTable())) 728 return symbol_table; 729 730 // Locate and parse all linker symbol tables. 731 for (SectionHeaderCollIter I = m_section_headers.begin(); 732 I != m_section_headers.end(); ++I) 733 { 734 if (I->sh_type == SHT_SYMTAB) 735 { 736 const ELFSectionHeader &symtab_section = *I; 737 user_id_t section_id = SectionIndex(I); 738 ParseSymbolTable (symbol_table, symtab_section, section_id); 739 } 740 } 741 742 return symbol_table; 743 } 744 745 //===----------------------------------------------------------------------===// 746 // Dump 747 // 748 // Dump the specifics of the runtime file container (such as any headers 749 // segments, sections, etc). 750 //---------------------------------------------------------------------- 751 void 752 ObjectFileELF::Dump(Stream *s) 753 { 754 DumpELFHeader(s, m_header); 755 s->EOL(); 756 DumpELFProgramHeaders(s); 757 s->EOL(); 758 DumpELFSectionHeaders(s); 759 s->EOL(); 760 SectionList *section_list = GetSectionList(); 761 if (section_list) 762 section_list->Dump(s, NULL, true, UINT32_MAX); 763 Symtab *symtab = GetSymtab(); 764 if (symtab) 765 symtab->Dump(s, NULL, lldb::eSortOrderNone); 766 s->EOL(); 767 DumpDependentModules(s); 768 s->EOL(); 769 } 770 771 //---------------------------------------------------------------------- 772 // DumpELFHeader 773 // 774 // Dump the ELF header to the specified output stream 775 //---------------------------------------------------------------------- 776 void 777 ObjectFileELF::DumpELFHeader(Stream *s, const ELFHeader &header) 778 { 779 s->PutCString("ELF Header\n"); 780 s->Printf("e_ident[EI_MAG0 ] = 0x%2.2x\n", header.e_ident[EI_MAG0]); 781 s->Printf("e_ident[EI_MAG1 ] = 0x%2.2x '%c'\n", 782 header.e_ident[EI_MAG1], header.e_ident[EI_MAG1]); 783 s->Printf("e_ident[EI_MAG2 ] = 0x%2.2x '%c'\n", 784 header.e_ident[EI_MAG2], header.e_ident[EI_MAG2]); 785 s->Printf("e_ident[EI_MAG3 ] = 0x%2.2x '%c'\n", 786 header.e_ident[EI_MAG3], header.e_ident[EI_MAG3]); 787 788 s->Printf("e_ident[EI_CLASS ] = 0x%2.2x\n", header.e_ident[EI_CLASS]); 789 s->Printf("e_ident[EI_DATA ] = 0x%2.2x ", header.e_ident[EI_DATA]); 790 DumpELFHeader_e_ident_EI_DATA(s, header.e_ident[EI_DATA]); 791 s->Printf ("\ne_ident[EI_VERSION] = 0x%2.2x\n", header.e_ident[EI_VERSION]); 792 s->Printf ("e_ident[EI_PAD ] = 0x%2.2x\n", header.e_ident[EI_PAD]); 793 794 s->Printf("e_type = 0x%4.4x ", header.e_type); 795 DumpELFHeader_e_type(s, header.e_type); 796 s->Printf("\ne_machine = 0x%4.4x\n", header.e_machine); 797 s->Printf("e_version = 0x%8.8x\n", header.e_version); 798 s->Printf("e_entry = 0x%8.8lx\n", header.e_entry); 799 s->Printf("e_phoff = 0x%8.8lx\n", header.e_phoff); 800 s->Printf("e_shoff = 0x%8.8lx\n", header.e_shoff); 801 s->Printf("e_flags = 0x%8.8x\n", header.e_flags); 802 s->Printf("e_ehsize = 0x%4.4x\n", header.e_ehsize); 803 s->Printf("e_phentsize = 0x%4.4x\n", header.e_phentsize); 804 s->Printf("e_phnum = 0x%4.4x\n", header.e_phnum); 805 s->Printf("e_shentsize = 0x%4.4x\n", header.e_shentsize); 806 s->Printf("e_shnum = 0x%4.4x\n", header.e_shnum); 807 s->Printf("e_shstrndx = 0x%4.4x\n", header.e_shstrndx); 808 } 809 810 //---------------------------------------------------------------------- 811 // DumpELFHeader_e_type 812 // 813 // Dump an token value for the ELF header member e_type 814 //---------------------------------------------------------------------- 815 void 816 ObjectFileELF::DumpELFHeader_e_type(Stream *s, elf_half e_type) 817 { 818 switch (e_type) 819 { 820 case ET_NONE: *s << "ET_NONE"; break; 821 case ET_REL: *s << "ET_REL"; break; 822 case ET_EXEC: *s << "ET_EXEC"; break; 823 case ET_DYN: *s << "ET_DYN"; break; 824 case ET_CORE: *s << "ET_CORE"; break; 825 default: 826 break; 827 } 828 } 829 830 //---------------------------------------------------------------------- 831 // DumpELFHeader_e_ident_EI_DATA 832 // 833 // Dump an token value for the ELF header member e_ident[EI_DATA] 834 //---------------------------------------------------------------------- 835 void 836 ObjectFileELF::DumpELFHeader_e_ident_EI_DATA(Stream *s, unsigned char ei_data) 837 { 838 switch (ei_data) 839 { 840 case ELFDATANONE: *s << "ELFDATANONE"; break; 841 case ELFDATA2LSB: *s << "ELFDATA2LSB - Little Endian"; break; 842 case ELFDATA2MSB: *s << "ELFDATA2MSB - Big Endian"; break; 843 default: 844 break; 845 } 846 } 847 848 849 //---------------------------------------------------------------------- 850 // DumpELFProgramHeader 851 // 852 // Dump a single ELF program header to the specified output stream 853 //---------------------------------------------------------------------- 854 void 855 ObjectFileELF::DumpELFProgramHeader(Stream *s, const ELFProgramHeader &ph) 856 { 857 DumpELFProgramHeader_p_type(s, ph.p_type); 858 s->Printf(" %8.8lx %8.8lx %8.8lx", ph.p_offset, ph.p_vaddr, ph.p_paddr); 859 s->Printf(" %8.8lx %8.8lx %8.8lx (", ph.p_filesz, ph.p_memsz, ph.p_flags); 860 861 DumpELFProgramHeader_p_flags(s, ph.p_flags); 862 s->Printf(") %8.8x", ph.p_align); 863 } 864 865 //---------------------------------------------------------------------- 866 // DumpELFProgramHeader_p_type 867 // 868 // Dump an token value for the ELF program header member p_type which 869 // describes the type of the program header 870 // ---------------------------------------------------------------------- 871 void 872 ObjectFileELF::DumpELFProgramHeader_p_type(Stream *s, elf_word p_type) 873 { 874 const int kStrWidth = 10; 875 switch (p_type) 876 { 877 CASE_AND_STREAM(s, PT_NULL , kStrWidth); 878 CASE_AND_STREAM(s, PT_LOAD , kStrWidth); 879 CASE_AND_STREAM(s, PT_DYNAMIC , kStrWidth); 880 CASE_AND_STREAM(s, PT_INTERP , kStrWidth); 881 CASE_AND_STREAM(s, PT_NOTE , kStrWidth); 882 CASE_AND_STREAM(s, PT_SHLIB , kStrWidth); 883 CASE_AND_STREAM(s, PT_PHDR , kStrWidth); 884 default: 885 s->Printf("0x%8.8x%*s", p_type, kStrWidth - 10, ""); 886 break; 887 } 888 } 889 890 891 //---------------------------------------------------------------------- 892 // DumpELFProgramHeader_p_flags 893 // 894 // Dump an token value for the ELF program header member p_flags 895 //---------------------------------------------------------------------- 896 void 897 ObjectFileELF::DumpELFProgramHeader_p_flags(Stream *s, elf_word p_flags) 898 { 899 *s << ((p_flags & PF_X) ? "PF_X" : " ") 900 << (((p_flags & PF_X) && (p_flags & PF_W)) ? '+' : ' ') 901 << ((p_flags & PF_W) ? "PF_W" : " ") 902 << (((p_flags & PF_W) && (p_flags & PF_R)) ? '+' : ' ') 903 << ((p_flags & PF_R) ? "PF_R" : " "); 904 } 905 906 //---------------------------------------------------------------------- 907 // DumpELFProgramHeaders 908 // 909 // Dump all of the ELF program header to the specified output stream 910 //---------------------------------------------------------------------- 911 void 912 ObjectFileELF::DumpELFProgramHeaders(Stream *s) 913 { 914 if (ParseProgramHeaders()) 915 { 916 s->PutCString("Program Headers\n"); 917 s->PutCString("IDX p_type p_offset p_vaddr p_paddr " 918 "p_filesz p_memsz p_flags p_align\n"); 919 s->PutCString("==== ---------- -------- -------- -------- " 920 "-------- -------- ------------------------- --------\n"); 921 922 uint32_t idx = 0; 923 for (ProgramHeaderCollConstIter I = m_program_headers.begin(); 924 I != m_program_headers.end(); ++I, ++idx) 925 { 926 s->Printf("[%2u] ", idx); 927 ObjectFileELF::DumpELFProgramHeader(s, *I); 928 s->EOL(); 929 } 930 } 931 } 932 933 //---------------------------------------------------------------------- 934 // DumpELFSectionHeader 935 // 936 // Dump a single ELF section header to the specified output stream 937 //---------------------------------------------------------------------- 938 void 939 ObjectFileELF::DumpELFSectionHeader(Stream *s, const ELFSectionHeader &sh) 940 { 941 s->Printf("%8.8x ", sh.sh_name); 942 DumpELFSectionHeader_sh_type(s, sh.sh_type); 943 s->Printf(" %8.8lx (", sh.sh_flags); 944 DumpELFSectionHeader_sh_flags(s, sh.sh_flags); 945 s->Printf(") %8.8lx %8.8lx %8.8lx", sh.sh_addr, sh.sh_offset, sh.sh_size); 946 s->Printf(" %8.8x %8.8x", sh.sh_link, sh.sh_info); 947 s->Printf(" %8.8lx %8.8lx", sh.sh_addralign, sh.sh_entsize); 948 } 949 950 //---------------------------------------------------------------------- 951 // DumpELFSectionHeader_sh_type 952 // 953 // Dump an token value for the ELF section header member sh_type which 954 // describes the type of the section 955 //---------------------------------------------------------------------- 956 void 957 ObjectFileELF::DumpELFSectionHeader_sh_type(Stream *s, elf_word sh_type) 958 { 959 const int kStrWidth = 12; 960 switch (sh_type) 961 { 962 CASE_AND_STREAM(s, SHT_NULL , kStrWidth); 963 CASE_AND_STREAM(s, SHT_PROGBITS , kStrWidth); 964 CASE_AND_STREAM(s, SHT_SYMTAB , kStrWidth); 965 CASE_AND_STREAM(s, SHT_STRTAB , kStrWidth); 966 CASE_AND_STREAM(s, SHT_RELA , kStrWidth); 967 CASE_AND_STREAM(s, SHT_HASH , kStrWidth); 968 CASE_AND_STREAM(s, SHT_DYNAMIC , kStrWidth); 969 CASE_AND_STREAM(s, SHT_NOTE , kStrWidth); 970 CASE_AND_STREAM(s, SHT_NOBITS , kStrWidth); 971 CASE_AND_STREAM(s, SHT_REL , kStrWidth); 972 CASE_AND_STREAM(s, SHT_SHLIB , kStrWidth); 973 CASE_AND_STREAM(s, SHT_DYNSYM , kStrWidth); 974 CASE_AND_STREAM(s, SHT_LOPROC , kStrWidth); 975 CASE_AND_STREAM(s, SHT_HIPROC , kStrWidth); 976 CASE_AND_STREAM(s, SHT_LOUSER , kStrWidth); 977 CASE_AND_STREAM(s, SHT_HIUSER , kStrWidth); 978 default: 979 s->Printf("0x%8.8x%*s", sh_type, kStrWidth - 10, ""); 980 break; 981 } 982 } 983 984 //---------------------------------------------------------------------- 985 // DumpELFSectionHeader_sh_flags 986 // 987 // Dump an token value for the ELF section header member sh_flags 988 //---------------------------------------------------------------------- 989 void 990 ObjectFileELF::DumpELFSectionHeader_sh_flags(Stream *s, elf_word sh_flags) 991 { 992 *s << ((sh_flags & SHF_WRITE) ? "WRITE" : " ") 993 << (((sh_flags & SHF_WRITE) && (sh_flags & SHF_ALLOC)) ? '+' : ' ') 994 << ((sh_flags & SHF_ALLOC) ? "ALLOC" : " ") 995 << (((sh_flags & SHF_ALLOC) && (sh_flags & SHF_EXECINSTR)) ? '+' : ' ') 996 << ((sh_flags & SHF_EXECINSTR) ? "EXECINSTR" : " "); 997 } 998 999 //---------------------------------------------------------------------- 1000 // DumpELFSectionHeaders 1001 // 1002 // Dump all of the ELF section header to the specified output stream 1003 //---------------------------------------------------------------------- 1004 void 1005 ObjectFileELF::DumpELFSectionHeaders(Stream *s) 1006 { 1007 if (!(ParseSectionHeaders() && GetSectionHeaderStringTable())) 1008 return; 1009 1010 s->PutCString("Section Headers\n"); 1011 s->PutCString("IDX name type flags " 1012 "addr offset size link info addralgn " 1013 "entsize Name\n"); 1014 s->PutCString("==== -------- ------------ -------------------------------- " 1015 "-------- -------- -------- -------- -------- -------- " 1016 "-------- ====================\n"); 1017 1018 uint32_t idx = 0; 1019 for (SectionHeaderCollConstIter I = m_section_headers.begin(); 1020 I != m_section_headers.end(); ++I, ++idx) 1021 { 1022 s->Printf("[%2u] ", idx); 1023 ObjectFileELF::DumpELFSectionHeader(s, *I); 1024 const char* section_name = m_shstr_data.PeekCStr(I->sh_name); 1025 if (section_name) 1026 *s << ' ' << section_name << "\n"; 1027 } 1028 } 1029 1030 void 1031 ObjectFileELF::DumpDependentModules(lldb_private::Stream *s) 1032 { 1033 size_t num_modules = ParseDependentModules(); 1034 1035 if (num_modules > 0) 1036 { 1037 s->PutCString("Dependent Modules:\n"); 1038 for (unsigned i = 0; i < num_modules; ++i) 1039 { 1040 const FileSpec &spec = m_filespec_ap->GetFileSpecAtIndex(i); 1041 s->Printf(" %s\n", spec.GetFilename().GetCString()); 1042 } 1043 } 1044 } 1045 1046 bool 1047 ObjectFileELF::GetArchitecture (ArchSpec &arch) 1048 { 1049 arch.SetArchitecture (lldb::eArchTypeELF, m_header.e_machine, m_header.e_flags); 1050 arch.GetTriple().setOSName (Host::GetOSString().GetCString()); 1051 arch.GetTriple().setVendorName(Host::GetVendorString().GetCString()); 1052 return true; 1053 } 1054 1055