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