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