10b57cec5SDimitry Andric //===-- ELFDump.cpp - ELF-specific dumper -----------------------*- C++ -*-===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric /// 90b57cec5SDimitry Andric /// \file 100b57cec5SDimitry Andric /// This file implements the ELF-specific dumper for llvm-objdump. 110b57cec5SDimitry Andric /// 120b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 130b57cec5SDimitry Andric 145ffd83dbSDimitry Andric #include "ELFDump.h" 155ffd83dbSDimitry Andric 160b57cec5SDimitry Andric #include "llvm-objdump.h" 170b57cec5SDimitry Andric #include "llvm/Demangle/Demangle.h" 180b57cec5SDimitry Andric #include "llvm/Object/ELFObjectFile.h" 190b57cec5SDimitry Andric #include "llvm/Support/Format.h" 200b57cec5SDimitry Andric #include "llvm/Support/MathExtras.h" 210b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h" 220b57cec5SDimitry Andric 235ffd83dbSDimitry Andric using namespace llvm; 240b57cec5SDimitry Andric using namespace llvm::object; 255ffd83dbSDimitry Andric using namespace llvm::objdump; 260b57cec5SDimitry Andric 270b57cec5SDimitry Andric template <class ELFT> 28e8d8bef9SDimitry Andric static Expected<StringRef> getDynamicStrTab(const ELFFile<ELFT> &Elf) { 29e8d8bef9SDimitry Andric auto DynamicEntriesOrError = Elf.dynamicEntries(); 300b57cec5SDimitry Andric if (!DynamicEntriesOrError) 310b57cec5SDimitry Andric return DynamicEntriesOrError.takeError(); 320b57cec5SDimitry Andric 330b57cec5SDimitry Andric for (const typename ELFT::Dyn &Dyn : *DynamicEntriesOrError) { 340b57cec5SDimitry Andric if (Dyn.d_tag == ELF::DT_STRTAB) { 35e8d8bef9SDimitry Andric auto MappedAddrOrError = Elf.toMappedAddr(Dyn.getPtr()); 360b57cec5SDimitry Andric if (!MappedAddrOrError) 370b57cec5SDimitry Andric consumeError(MappedAddrOrError.takeError()); 380b57cec5SDimitry Andric return StringRef(reinterpret_cast<const char *>(*MappedAddrOrError)); 390b57cec5SDimitry Andric } 400b57cec5SDimitry Andric } 410b57cec5SDimitry Andric 420b57cec5SDimitry Andric // If the dynamic segment is not present, we fall back on the sections. 43e8d8bef9SDimitry Andric auto SectionsOrError = Elf.sections(); 440b57cec5SDimitry Andric if (!SectionsOrError) 450b57cec5SDimitry Andric return SectionsOrError.takeError(); 460b57cec5SDimitry Andric 470b57cec5SDimitry Andric for (const typename ELFT::Shdr &Sec : *SectionsOrError) { 480b57cec5SDimitry Andric if (Sec.sh_type == ELF::SHT_DYNSYM) 49e8d8bef9SDimitry Andric return Elf.getStringTableForSymtab(Sec); 500b57cec5SDimitry Andric } 510b57cec5SDimitry Andric 520b57cec5SDimitry Andric return createError("dynamic string table not found"); 530b57cec5SDimitry Andric } 540b57cec5SDimitry Andric 550b57cec5SDimitry Andric template <class ELFT> 560b57cec5SDimitry Andric static Error getRelocationValueString(const ELFObjectFile<ELFT> *Obj, 570b57cec5SDimitry Andric const RelocationRef &RelRef, 580b57cec5SDimitry Andric SmallVectorImpl<char> &Result) { 59e8d8bef9SDimitry Andric const ELFFile<ELFT> &EF = Obj->getELFFile(); 600b57cec5SDimitry Andric DataRefImpl Rel = RelRef.getRawDataRefImpl(); 610b57cec5SDimitry Andric auto SecOrErr = EF.getSection(Rel.d.a); 620b57cec5SDimitry Andric if (!SecOrErr) 630b57cec5SDimitry Andric return SecOrErr.takeError(); 640b57cec5SDimitry Andric 650b57cec5SDimitry Andric int64_t Addend = 0; 660b57cec5SDimitry Andric // If there is no Symbol associated with the relocation, we set the undef 670b57cec5SDimitry Andric // boolean value to 'true'. This will prevent us from calling functions that 680b57cec5SDimitry Andric // requires the relocation to be associated with a symbol. 690b57cec5SDimitry Andric // 700b57cec5SDimitry Andric // In SHT_REL case we would need to read the addend from section data. 710b57cec5SDimitry Andric // GNU objdump does not do that and we just follow for simplicity atm. 720b57cec5SDimitry Andric bool Undef = false; 730b57cec5SDimitry Andric if ((*SecOrErr)->sh_type == ELF::SHT_RELA) { 740b57cec5SDimitry Andric const typename ELFT::Rela *ERela = Obj->getRela(Rel); 750b57cec5SDimitry Andric Addend = ERela->r_addend; 760b57cec5SDimitry Andric Undef = ERela->getSymbol(false) == 0; 77fe6060f1SDimitry Andric } else if ((*SecOrErr)->sh_type == ELF::SHT_REL) { 78fe6060f1SDimitry Andric const typename ELFT::Rel *ERel = Obj->getRel(Rel); 79fe6060f1SDimitry Andric Undef = ERel->getSymbol(false) == 0; 80fe6060f1SDimitry Andric } else { 810b57cec5SDimitry Andric return make_error<BinaryError>(); 820b57cec5SDimitry Andric } 830b57cec5SDimitry Andric 840b57cec5SDimitry Andric // Default scheme is to print Target, as well as "+ <addend>" for nonzero 850b57cec5SDimitry Andric // addend. Should be acceptable for all normal purposes. 860b57cec5SDimitry Andric std::string FmtBuf; 870b57cec5SDimitry Andric raw_string_ostream Fmt(FmtBuf); 880b57cec5SDimitry Andric 890b57cec5SDimitry Andric if (!Undef) { 900b57cec5SDimitry Andric symbol_iterator SI = RelRef.getSymbol(); 91e8d8bef9SDimitry Andric Expected<const typename ELFT::Sym *> SymOrErr = 92e8d8bef9SDimitry Andric Obj->getSymbol(SI->getRawDataRefImpl()); 93e8d8bef9SDimitry Andric // TODO: test this error. 94e8d8bef9SDimitry Andric if (!SymOrErr) 95e8d8bef9SDimitry Andric return SymOrErr.takeError(); 96e8d8bef9SDimitry Andric 97e8d8bef9SDimitry Andric if ((*SymOrErr)->getType() == ELF::STT_SECTION) { 980b57cec5SDimitry Andric Expected<section_iterator> SymSI = SI->getSection(); 990b57cec5SDimitry Andric if (!SymSI) 1000b57cec5SDimitry Andric return SymSI.takeError(); 1010b57cec5SDimitry Andric const typename ELFT::Shdr *SymSec = 1020b57cec5SDimitry Andric Obj->getSection((*SymSI)->getRawDataRefImpl()); 103e8d8bef9SDimitry Andric auto SecName = EF.getSectionName(*SymSec); 1040b57cec5SDimitry Andric if (!SecName) 1050b57cec5SDimitry Andric return SecName.takeError(); 1060b57cec5SDimitry Andric Fmt << *SecName; 1070b57cec5SDimitry Andric } else { 1080b57cec5SDimitry Andric Expected<StringRef> SymName = SI->getName(); 1090b57cec5SDimitry Andric if (!SymName) 1100b57cec5SDimitry Andric return SymName.takeError(); 1110b57cec5SDimitry Andric if (Demangle) 1125ffd83dbSDimitry Andric Fmt << demangle(std::string(*SymName)); 1130b57cec5SDimitry Andric else 1140b57cec5SDimitry Andric Fmt << *SymName; 1150b57cec5SDimitry Andric } 1160b57cec5SDimitry Andric } else { 1170b57cec5SDimitry Andric Fmt << "*ABS*"; 1180b57cec5SDimitry Andric } 119480093f4SDimitry Andric if (Addend != 0) { 120480093f4SDimitry Andric Fmt << (Addend < 0 121480093f4SDimitry Andric ? "-" 122480093f4SDimitry Andric : "+") << format("0x%" PRIx64, 123480093f4SDimitry Andric (Addend < 0 ? -(uint64_t)Addend : (uint64_t)Addend)); 124480093f4SDimitry Andric } 1250b57cec5SDimitry Andric Fmt.flush(); 1260b57cec5SDimitry Andric Result.append(FmtBuf.begin(), FmtBuf.end()); 1270b57cec5SDimitry Andric return Error::success(); 1280b57cec5SDimitry Andric } 1290b57cec5SDimitry Andric 1305ffd83dbSDimitry Andric Error objdump::getELFRelocationValueString(const ELFObjectFileBase *Obj, 1310b57cec5SDimitry Andric const RelocationRef &Rel, 1320b57cec5SDimitry Andric SmallVectorImpl<char> &Result) { 1330b57cec5SDimitry Andric if (auto *ELF32LE = dyn_cast<ELF32LEObjectFile>(Obj)) 1340b57cec5SDimitry Andric return getRelocationValueString(ELF32LE, Rel, Result); 1350b57cec5SDimitry Andric if (auto *ELF64LE = dyn_cast<ELF64LEObjectFile>(Obj)) 1360b57cec5SDimitry Andric return getRelocationValueString(ELF64LE, Rel, Result); 1370b57cec5SDimitry Andric if (auto *ELF32BE = dyn_cast<ELF32BEObjectFile>(Obj)) 1380b57cec5SDimitry Andric return getRelocationValueString(ELF32BE, Rel, Result); 1390b57cec5SDimitry Andric auto *ELF64BE = cast<ELF64BEObjectFile>(Obj); 1400b57cec5SDimitry Andric return getRelocationValueString(ELF64BE, Rel, Result); 1410b57cec5SDimitry Andric } 1420b57cec5SDimitry Andric 1430b57cec5SDimitry Andric template <class ELFT> 144e8d8bef9SDimitry Andric static uint64_t getSectionLMA(const ELFFile<ELFT> &Obj, 1450b57cec5SDimitry Andric const object::ELFSectionRef &Sec) { 146e8d8bef9SDimitry Andric auto PhdrRangeOrErr = Obj.program_headers(); 1470b57cec5SDimitry Andric if (!PhdrRangeOrErr) 148349cc55cSDimitry Andric report_fatal_error(Twine(toString(PhdrRangeOrErr.takeError()))); 1490b57cec5SDimitry Andric 1500b57cec5SDimitry Andric // Search for a PT_LOAD segment containing the requested section. Use this 1510b57cec5SDimitry Andric // segment's p_addr to calculate the section's LMA. 1520b57cec5SDimitry Andric for (const typename ELFT::Phdr &Phdr : *PhdrRangeOrErr) 1530b57cec5SDimitry Andric if ((Phdr.p_type == ELF::PT_LOAD) && (Phdr.p_vaddr <= Sec.getAddress()) && 1540b57cec5SDimitry Andric (Phdr.p_vaddr + Phdr.p_memsz > Sec.getAddress())) 1550b57cec5SDimitry Andric return Sec.getAddress() - Phdr.p_vaddr + Phdr.p_paddr; 1560b57cec5SDimitry Andric 1570b57cec5SDimitry Andric // Return section's VMA if it isn't in a PT_LOAD segment. 1580b57cec5SDimitry Andric return Sec.getAddress(); 1590b57cec5SDimitry Andric } 1600b57cec5SDimitry Andric 1615ffd83dbSDimitry Andric uint64_t objdump::getELFSectionLMA(const object::ELFSectionRef &Sec) { 1620b57cec5SDimitry Andric if (const auto *ELFObj = dyn_cast<ELF32LEObjectFile>(Sec.getObject())) 1630b57cec5SDimitry Andric return getSectionLMA(ELFObj->getELFFile(), Sec); 1640b57cec5SDimitry Andric else if (const auto *ELFObj = dyn_cast<ELF32BEObjectFile>(Sec.getObject())) 1650b57cec5SDimitry Andric return getSectionLMA(ELFObj->getELFFile(), Sec); 1660b57cec5SDimitry Andric else if (const auto *ELFObj = dyn_cast<ELF64LEObjectFile>(Sec.getObject())) 1670b57cec5SDimitry Andric return getSectionLMA(ELFObj->getELFFile(), Sec); 1680b57cec5SDimitry Andric const auto *ELFObj = cast<ELF64BEObjectFile>(Sec.getObject()); 1690b57cec5SDimitry Andric return getSectionLMA(ELFObj->getELFFile(), Sec); 1700b57cec5SDimitry Andric } 1710b57cec5SDimitry Andric 1720b57cec5SDimitry Andric template <class ELFT> 173e8d8bef9SDimitry Andric static void printDynamicSection(const ELFFile<ELFT> &Elf, StringRef Filename) { 174*3a9a9c0cSDimitry Andric auto DynamicEntriesOrErr = Elf.dynamicEntries(); 175*3a9a9c0cSDimitry Andric if (!DynamicEntriesOrErr) { 176*3a9a9c0cSDimitry Andric reportWarning(toString(DynamicEntriesOrErr.takeError()), Filename); 177*3a9a9c0cSDimitry Andric return; 178*3a9a9c0cSDimitry Andric } 179*3a9a9c0cSDimitry Andric ArrayRef<typename ELFT::Dyn> DynamicEntries = *DynamicEntriesOrErr; 1805ffd83dbSDimitry Andric 1815ffd83dbSDimitry Andric // Find the maximum tag name length to format the value column properly. 1825ffd83dbSDimitry Andric size_t MaxLen = 0; 1835ffd83dbSDimitry Andric for (const typename ELFT::Dyn &Dyn : DynamicEntries) 184e8d8bef9SDimitry Andric MaxLen = std::max(MaxLen, Elf.getDynamicTagAsString(Dyn.d_tag).size()); 1855ffd83dbSDimitry Andric std::string TagFmt = " %-" + std::to_string(MaxLen) + "s "; 1865ffd83dbSDimitry Andric 187fe6060f1SDimitry Andric outs() << "\nDynamic Section:\n"; 1880b57cec5SDimitry Andric for (const typename ELFT::Dyn &Dyn : DynamicEntries) { 1890b57cec5SDimitry Andric if (Dyn.d_tag == ELF::DT_NULL) 1900b57cec5SDimitry Andric continue; 1910b57cec5SDimitry Andric 192e8d8bef9SDimitry Andric std::string Str = Elf.getDynamicTagAsString(Dyn.d_tag); 1935ffd83dbSDimitry Andric outs() << format(TagFmt.c_str(), Str.c_str()); 1940b57cec5SDimitry Andric 1950b57cec5SDimitry Andric const char *Fmt = 1960b57cec5SDimitry Andric ELFT::Is64Bits ? "0x%016" PRIx64 "\n" : "0x%08" PRIx64 "\n"; 1970b57cec5SDimitry Andric if (Dyn.d_tag == ELF::DT_NEEDED || Dyn.d_tag == ELF::DT_RPATH || 1980b57cec5SDimitry Andric Dyn.d_tag == ELF::DT_RUNPATH || Dyn.d_tag == ELF::DT_SONAME || 1990b57cec5SDimitry Andric Dyn.d_tag == ELF::DT_AUXILIARY || Dyn.d_tag == ELF::DT_FILTER) { 2000b57cec5SDimitry Andric Expected<StringRef> StrTabOrErr = getDynamicStrTab(Elf); 2010b57cec5SDimitry Andric if (StrTabOrErr) { 2020b57cec5SDimitry Andric const char *Data = StrTabOrErr.get().data(); 2030b57cec5SDimitry Andric outs() << (Data + Dyn.d_un.d_val) << "\n"; 2040b57cec5SDimitry Andric continue; 2050b57cec5SDimitry Andric } 2068bcb0991SDimitry Andric reportWarning(toString(StrTabOrErr.takeError()), Filename); 2070b57cec5SDimitry Andric consumeError(StrTabOrErr.takeError()); 2080b57cec5SDimitry Andric } 2090b57cec5SDimitry Andric outs() << format(Fmt, (uint64_t)Dyn.d_un.d_val); 2100b57cec5SDimitry Andric } 2110b57cec5SDimitry Andric } 2120b57cec5SDimitry Andric 2135ffd83dbSDimitry Andric template <class ELFT> 214e8d8bef9SDimitry Andric static void printProgramHeaders(const ELFFile<ELFT> &Obj, StringRef FileName) { 215fe6060f1SDimitry Andric outs() << "\nProgram Header:\n"; 216e8d8bef9SDimitry Andric auto ProgramHeaderOrError = Obj.program_headers(); 2175ffd83dbSDimitry Andric if (!ProgramHeaderOrError) { 2185ffd83dbSDimitry Andric reportWarning("unable to read program headers: " + 2195ffd83dbSDimitry Andric toString(ProgramHeaderOrError.takeError()), 2205ffd83dbSDimitry Andric FileName); 2215ffd83dbSDimitry Andric return; 2225ffd83dbSDimitry Andric } 2235ffd83dbSDimitry Andric 2240b57cec5SDimitry Andric for (const typename ELFT::Phdr &Phdr : *ProgramHeaderOrError) { 2250b57cec5SDimitry Andric switch (Phdr.p_type) { 2260b57cec5SDimitry Andric case ELF::PT_DYNAMIC: 2270b57cec5SDimitry Andric outs() << " DYNAMIC "; 2280b57cec5SDimitry Andric break; 2290b57cec5SDimitry Andric case ELF::PT_GNU_EH_FRAME: 2300b57cec5SDimitry Andric outs() << "EH_FRAME "; 2310b57cec5SDimitry Andric break; 2320b57cec5SDimitry Andric case ELF::PT_GNU_RELRO: 2330b57cec5SDimitry Andric outs() << " RELRO "; 2340b57cec5SDimitry Andric break; 235480093f4SDimitry Andric case ELF::PT_GNU_PROPERTY: 236480093f4SDimitry Andric outs() << " PROPERTY "; 237480093f4SDimitry Andric break; 2380b57cec5SDimitry Andric case ELF::PT_GNU_STACK: 2390b57cec5SDimitry Andric outs() << " STACK "; 2400b57cec5SDimitry Andric break; 2410b57cec5SDimitry Andric case ELF::PT_INTERP: 2420b57cec5SDimitry Andric outs() << " INTERP "; 2430b57cec5SDimitry Andric break; 2440b57cec5SDimitry Andric case ELF::PT_LOAD: 2450b57cec5SDimitry Andric outs() << " LOAD "; 2460b57cec5SDimitry Andric break; 2470b57cec5SDimitry Andric case ELF::PT_NOTE: 2480b57cec5SDimitry Andric outs() << " NOTE "; 2490b57cec5SDimitry Andric break; 2500b57cec5SDimitry Andric case ELF::PT_OPENBSD_BOOTDATA: 2510b57cec5SDimitry Andric outs() << " OPENBSD_BOOTDATA "; 2520b57cec5SDimitry Andric break; 2530b57cec5SDimitry Andric case ELF::PT_OPENBSD_RANDOMIZE: 2540b57cec5SDimitry Andric outs() << " OPENBSD_RANDOMIZE "; 2550b57cec5SDimitry Andric break; 2560b57cec5SDimitry Andric case ELF::PT_OPENBSD_WXNEEDED: 2570b57cec5SDimitry Andric outs() << " OPENBSD_WXNEEDED "; 2580b57cec5SDimitry Andric break; 2590b57cec5SDimitry Andric case ELF::PT_PHDR: 2600b57cec5SDimitry Andric outs() << " PHDR "; 2610b57cec5SDimitry Andric break; 2620b57cec5SDimitry Andric case ELF::PT_TLS: 2630b57cec5SDimitry Andric outs() << " TLS "; 2640b57cec5SDimitry Andric break; 2650b57cec5SDimitry Andric default: 2660b57cec5SDimitry Andric outs() << " UNKNOWN "; 2670b57cec5SDimitry Andric } 2680b57cec5SDimitry Andric 2690b57cec5SDimitry Andric const char *Fmt = ELFT::Is64Bits ? "0x%016" PRIx64 " " : "0x%08" PRIx64 " "; 2700b57cec5SDimitry Andric 2710b57cec5SDimitry Andric outs() << "off " << format(Fmt, (uint64_t)Phdr.p_offset) << "vaddr " 2720b57cec5SDimitry Andric << format(Fmt, (uint64_t)Phdr.p_vaddr) << "paddr " 2730b57cec5SDimitry Andric << format(Fmt, (uint64_t)Phdr.p_paddr) 2740b57cec5SDimitry Andric << format("align 2**%u\n", 2750b57cec5SDimitry Andric countTrailingZeros<uint64_t>(Phdr.p_align)) 2760b57cec5SDimitry Andric << " filesz " << format(Fmt, (uint64_t)Phdr.p_filesz) 2770b57cec5SDimitry Andric << "memsz " << format(Fmt, (uint64_t)Phdr.p_memsz) << "flags " 2780b57cec5SDimitry Andric << ((Phdr.p_flags & ELF::PF_R) ? "r" : "-") 2790b57cec5SDimitry Andric << ((Phdr.p_flags & ELF::PF_W) ? "w" : "-") 2800b57cec5SDimitry Andric << ((Phdr.p_flags & ELF::PF_X) ? "x" : "-") << "\n"; 2810b57cec5SDimitry Andric } 2820b57cec5SDimitry Andric } 2830b57cec5SDimitry Andric 2840b57cec5SDimitry Andric template <class ELFT> 2855ffd83dbSDimitry Andric static void printSymbolVersionDependency(ArrayRef<uint8_t> Contents, 2860b57cec5SDimitry Andric StringRef StrTab) { 287fe6060f1SDimitry Andric outs() << "\nVersion References:\n"; 2880b57cec5SDimitry Andric 2890b57cec5SDimitry Andric const uint8_t *Buf = Contents.data(); 2900b57cec5SDimitry Andric while (Buf) { 2910b57cec5SDimitry Andric auto *Verneed = reinterpret_cast<const typename ELFT::Verneed *>(Buf); 2920b57cec5SDimitry Andric outs() << " required from " 2930b57cec5SDimitry Andric << StringRef(StrTab.drop_front(Verneed->vn_file).data()) << ":\n"; 2940b57cec5SDimitry Andric 2950b57cec5SDimitry Andric const uint8_t *BufAux = Buf + Verneed->vn_aux; 2960b57cec5SDimitry Andric while (BufAux) { 2970b57cec5SDimitry Andric auto *Vernaux = reinterpret_cast<const typename ELFT::Vernaux *>(BufAux); 2980b57cec5SDimitry Andric outs() << " " 2990b57cec5SDimitry Andric << format("0x%08" PRIx32 " ", (uint32_t)Vernaux->vna_hash) 3000b57cec5SDimitry Andric << format("0x%02" PRIx16 " ", (uint16_t)Vernaux->vna_flags) 3010b57cec5SDimitry Andric << format("%02" PRIu16 " ", (uint16_t)Vernaux->vna_other) 3020b57cec5SDimitry Andric << StringRef(StrTab.drop_front(Vernaux->vna_name).data()) << '\n'; 3030b57cec5SDimitry Andric BufAux = Vernaux->vna_next ? BufAux + Vernaux->vna_next : nullptr; 3040b57cec5SDimitry Andric } 3050b57cec5SDimitry Andric Buf = Verneed->vn_next ? Buf + Verneed->vn_next : nullptr; 3060b57cec5SDimitry Andric } 3070b57cec5SDimitry Andric } 3080b57cec5SDimitry Andric 3090b57cec5SDimitry Andric template <class ELFT> 3105ffd83dbSDimitry Andric static void printSymbolVersionDefinition(const typename ELFT::Shdr &Shdr, 3110b57cec5SDimitry Andric ArrayRef<uint8_t> Contents, 3120b57cec5SDimitry Andric StringRef StrTab) { 313fe6060f1SDimitry Andric outs() << "\nVersion definitions:\n"; 3140b57cec5SDimitry Andric 3150b57cec5SDimitry Andric const uint8_t *Buf = Contents.data(); 3160b57cec5SDimitry Andric uint32_t VerdefIndex = 1; 3170b57cec5SDimitry Andric // sh_info contains the number of entries in the SHT_GNU_verdef section. To 3180b57cec5SDimitry Andric // make the index column have consistent width, we should insert blank spaces 3190b57cec5SDimitry Andric // according to sh_info. 3200b57cec5SDimitry Andric uint16_t VerdefIndexWidth = std::to_string(Shdr.sh_info).size(); 3210b57cec5SDimitry Andric while (Buf) { 3220b57cec5SDimitry Andric auto *Verdef = reinterpret_cast<const typename ELFT::Verdef *>(Buf); 3230b57cec5SDimitry Andric outs() << format_decimal(VerdefIndex++, VerdefIndexWidth) << " " 3240b57cec5SDimitry Andric << format("0x%02" PRIx16 " ", (uint16_t)Verdef->vd_flags) 3250b57cec5SDimitry Andric << format("0x%08" PRIx32 " ", (uint32_t)Verdef->vd_hash); 3260b57cec5SDimitry Andric 3270b57cec5SDimitry Andric const uint8_t *BufAux = Buf + Verdef->vd_aux; 3280b57cec5SDimitry Andric uint16_t VerdauxIndex = 0; 3290b57cec5SDimitry Andric while (BufAux) { 3300b57cec5SDimitry Andric auto *Verdaux = reinterpret_cast<const typename ELFT::Verdaux *>(BufAux); 3310b57cec5SDimitry Andric if (VerdauxIndex) 3320b57cec5SDimitry Andric outs() << std::string(VerdefIndexWidth + 17, ' '); 3330b57cec5SDimitry Andric outs() << StringRef(StrTab.drop_front(Verdaux->vda_name).data()) << '\n'; 3340b57cec5SDimitry Andric BufAux = Verdaux->vda_next ? BufAux + Verdaux->vda_next : nullptr; 3350b57cec5SDimitry Andric ++VerdauxIndex; 3360b57cec5SDimitry Andric } 3370b57cec5SDimitry Andric Buf = Verdef->vd_next ? Buf + Verdef->vd_next : nullptr; 3380b57cec5SDimitry Andric } 3390b57cec5SDimitry Andric } 3400b57cec5SDimitry Andric 3410b57cec5SDimitry Andric template <class ELFT> 342e8d8bef9SDimitry Andric static void printSymbolVersionInfo(const ELFFile<ELFT> &Elf, 3435ffd83dbSDimitry Andric StringRef FileName) { 3440b57cec5SDimitry Andric ArrayRef<typename ELFT::Shdr> Sections = 345e8d8bef9SDimitry Andric unwrapOrError(Elf.sections(), FileName); 3460b57cec5SDimitry Andric for (const typename ELFT::Shdr &Shdr : Sections) { 3470b57cec5SDimitry Andric if (Shdr.sh_type != ELF::SHT_GNU_verneed && 3480b57cec5SDimitry Andric Shdr.sh_type != ELF::SHT_GNU_verdef) 3490b57cec5SDimitry Andric continue; 3500b57cec5SDimitry Andric 3510b57cec5SDimitry Andric ArrayRef<uint8_t> Contents = 352e8d8bef9SDimitry Andric unwrapOrError(Elf.getSectionContents(Shdr), FileName); 3530b57cec5SDimitry Andric const typename ELFT::Shdr *StrTabSec = 354e8d8bef9SDimitry Andric unwrapOrError(Elf.getSection(Shdr.sh_link), FileName); 355e8d8bef9SDimitry Andric StringRef StrTab = unwrapOrError(Elf.getStringTable(*StrTabSec), FileName); 3560b57cec5SDimitry Andric 3570b57cec5SDimitry Andric if (Shdr.sh_type == ELF::SHT_GNU_verneed) 3580b57cec5SDimitry Andric printSymbolVersionDependency<ELFT>(Contents, StrTab); 3590b57cec5SDimitry Andric else 3600b57cec5SDimitry Andric printSymbolVersionDefinition<ELFT>(Shdr, Contents, StrTab); 3610b57cec5SDimitry Andric } 3620b57cec5SDimitry Andric } 3630b57cec5SDimitry Andric 3645ffd83dbSDimitry Andric void objdump::printELFFileHeader(const object::ObjectFile *Obj) { 3650b57cec5SDimitry Andric if (const auto *ELFObj = dyn_cast<ELF32LEObjectFile>(Obj)) 3665ffd83dbSDimitry Andric printProgramHeaders(ELFObj->getELFFile(), Obj->getFileName()); 3670b57cec5SDimitry Andric else if (const auto *ELFObj = dyn_cast<ELF32BEObjectFile>(Obj)) 3685ffd83dbSDimitry Andric printProgramHeaders(ELFObj->getELFFile(), Obj->getFileName()); 3690b57cec5SDimitry Andric else if (const auto *ELFObj = dyn_cast<ELF64LEObjectFile>(Obj)) 3705ffd83dbSDimitry Andric printProgramHeaders(ELFObj->getELFFile(), Obj->getFileName()); 3710b57cec5SDimitry Andric else if (const auto *ELFObj = dyn_cast<ELF64BEObjectFile>(Obj)) 3725ffd83dbSDimitry Andric printProgramHeaders(ELFObj->getELFFile(), Obj->getFileName()); 3730b57cec5SDimitry Andric } 3740b57cec5SDimitry Andric 3755ffd83dbSDimitry Andric void objdump::printELFDynamicSection(const object::ObjectFile *Obj) { 3760b57cec5SDimitry Andric if (const auto *ELFObj = dyn_cast<ELF32LEObjectFile>(Obj)) 3770b57cec5SDimitry Andric printDynamicSection(ELFObj->getELFFile(), Obj->getFileName()); 3780b57cec5SDimitry Andric else if (const auto *ELFObj = dyn_cast<ELF32BEObjectFile>(Obj)) 3790b57cec5SDimitry Andric printDynamicSection(ELFObj->getELFFile(), Obj->getFileName()); 3800b57cec5SDimitry Andric else if (const auto *ELFObj = dyn_cast<ELF64LEObjectFile>(Obj)) 3810b57cec5SDimitry Andric printDynamicSection(ELFObj->getELFFile(), Obj->getFileName()); 3820b57cec5SDimitry Andric else if (const auto *ELFObj = dyn_cast<ELF64BEObjectFile>(Obj)) 3830b57cec5SDimitry Andric printDynamicSection(ELFObj->getELFFile(), Obj->getFileName()); 3840b57cec5SDimitry Andric } 3850b57cec5SDimitry Andric 3865ffd83dbSDimitry Andric void objdump::printELFSymbolVersionInfo(const object::ObjectFile *Obj) { 3870b57cec5SDimitry Andric if (const auto *ELFObj = dyn_cast<ELF32LEObjectFile>(Obj)) 3880b57cec5SDimitry Andric printSymbolVersionInfo(ELFObj->getELFFile(), Obj->getFileName()); 3890b57cec5SDimitry Andric else if (const auto *ELFObj = dyn_cast<ELF32BEObjectFile>(Obj)) 3900b57cec5SDimitry Andric printSymbolVersionInfo(ELFObj->getELFFile(), Obj->getFileName()); 3910b57cec5SDimitry Andric else if (const auto *ELFObj = dyn_cast<ELF64LEObjectFile>(Obj)) 3920b57cec5SDimitry Andric printSymbolVersionInfo(ELFObj->getELFFile(), Obj->getFileName()); 3930b57cec5SDimitry Andric else if (const auto *ELFObj = dyn_cast<ELF64BEObjectFile>(Obj)) 3940b57cec5SDimitry Andric printSymbolVersionInfo(ELFObj->getELFFile(), Obj->getFileName()); 3950b57cec5SDimitry Andric } 396