xref: /freebsd/contrib/llvm-project/llvm/tools/llvm-objdump/ELFDump.cpp (revision e8d8bef961a50d4dc22501cde4fb9fb0be1b2532)
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>
28*e8d8bef9SDimitry Andric static Expected<StringRef> getDynamicStrTab(const ELFFile<ELFT> &Elf) {
29*e8d8bef9SDimitry 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) {
35*e8d8bef9SDimitry 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.
43*e8d8bef9SDimitry 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)
49*e8d8bef9SDimitry 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) {
59*e8d8bef9SDimitry 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;
770b57cec5SDimitry Andric   } else if ((*SecOrErr)->sh_type != ELF::SHT_REL) {
780b57cec5SDimitry Andric     return make_error<BinaryError>();
790b57cec5SDimitry Andric   }
800b57cec5SDimitry Andric 
810b57cec5SDimitry Andric   // Default scheme is to print Target, as well as "+ <addend>" for nonzero
820b57cec5SDimitry Andric   // addend. Should be acceptable for all normal purposes.
830b57cec5SDimitry Andric   std::string FmtBuf;
840b57cec5SDimitry Andric   raw_string_ostream Fmt(FmtBuf);
850b57cec5SDimitry Andric 
860b57cec5SDimitry Andric   if (!Undef) {
870b57cec5SDimitry Andric     symbol_iterator SI = RelRef.getSymbol();
88*e8d8bef9SDimitry Andric     Expected<const typename ELFT::Sym *> SymOrErr =
89*e8d8bef9SDimitry Andric         Obj->getSymbol(SI->getRawDataRefImpl());
90*e8d8bef9SDimitry Andric     // TODO: test this error.
91*e8d8bef9SDimitry Andric     if (!SymOrErr)
92*e8d8bef9SDimitry Andric       return SymOrErr.takeError();
93*e8d8bef9SDimitry Andric 
94*e8d8bef9SDimitry Andric     if ((*SymOrErr)->getType() == ELF::STT_SECTION) {
950b57cec5SDimitry Andric       Expected<section_iterator> SymSI = SI->getSection();
960b57cec5SDimitry Andric       if (!SymSI)
970b57cec5SDimitry Andric         return SymSI.takeError();
980b57cec5SDimitry Andric       const typename ELFT::Shdr *SymSec =
990b57cec5SDimitry Andric           Obj->getSection((*SymSI)->getRawDataRefImpl());
100*e8d8bef9SDimitry Andric       auto SecName = EF.getSectionName(*SymSec);
1010b57cec5SDimitry Andric       if (!SecName)
1020b57cec5SDimitry Andric         return SecName.takeError();
1030b57cec5SDimitry Andric       Fmt << *SecName;
1040b57cec5SDimitry Andric     } else {
1050b57cec5SDimitry Andric       Expected<StringRef> SymName = SI->getName();
1060b57cec5SDimitry Andric       if (!SymName)
1070b57cec5SDimitry Andric         return SymName.takeError();
1080b57cec5SDimitry Andric       if (Demangle)
1095ffd83dbSDimitry Andric         Fmt << demangle(std::string(*SymName));
1100b57cec5SDimitry Andric       else
1110b57cec5SDimitry Andric         Fmt << *SymName;
1120b57cec5SDimitry Andric     }
1130b57cec5SDimitry Andric   } else {
1140b57cec5SDimitry Andric     Fmt << "*ABS*";
1150b57cec5SDimitry Andric   }
116480093f4SDimitry Andric   if (Addend != 0) {
117480093f4SDimitry Andric       Fmt << (Addend < 0
118480093f4SDimitry Andric           ? "-"
119480093f4SDimitry Andric           : "+") << format("0x%" PRIx64,
120480093f4SDimitry Andric                           (Addend < 0 ? -(uint64_t)Addend : (uint64_t)Addend));
121480093f4SDimitry Andric   }
1220b57cec5SDimitry Andric   Fmt.flush();
1230b57cec5SDimitry Andric   Result.append(FmtBuf.begin(), FmtBuf.end());
1240b57cec5SDimitry Andric   return Error::success();
1250b57cec5SDimitry Andric }
1260b57cec5SDimitry Andric 
1275ffd83dbSDimitry Andric Error objdump::getELFRelocationValueString(const ELFObjectFileBase *Obj,
1280b57cec5SDimitry Andric                                            const RelocationRef &Rel,
1290b57cec5SDimitry Andric                                            SmallVectorImpl<char> &Result) {
1300b57cec5SDimitry Andric   if (auto *ELF32LE = dyn_cast<ELF32LEObjectFile>(Obj))
1310b57cec5SDimitry Andric     return getRelocationValueString(ELF32LE, Rel, Result);
1320b57cec5SDimitry Andric   if (auto *ELF64LE = dyn_cast<ELF64LEObjectFile>(Obj))
1330b57cec5SDimitry Andric     return getRelocationValueString(ELF64LE, Rel, Result);
1340b57cec5SDimitry Andric   if (auto *ELF32BE = dyn_cast<ELF32BEObjectFile>(Obj))
1350b57cec5SDimitry Andric     return getRelocationValueString(ELF32BE, Rel, Result);
1360b57cec5SDimitry Andric   auto *ELF64BE = cast<ELF64BEObjectFile>(Obj);
1370b57cec5SDimitry Andric   return getRelocationValueString(ELF64BE, Rel, Result);
1380b57cec5SDimitry Andric }
1390b57cec5SDimitry Andric 
1400b57cec5SDimitry Andric template <class ELFT>
141*e8d8bef9SDimitry Andric static uint64_t getSectionLMA(const ELFFile<ELFT> &Obj,
1420b57cec5SDimitry Andric                               const object::ELFSectionRef &Sec) {
143*e8d8bef9SDimitry Andric   auto PhdrRangeOrErr = Obj.program_headers();
1440b57cec5SDimitry Andric   if (!PhdrRangeOrErr)
1450b57cec5SDimitry Andric     report_fatal_error(toString(PhdrRangeOrErr.takeError()));
1460b57cec5SDimitry Andric 
1470b57cec5SDimitry Andric   // Search for a PT_LOAD segment containing the requested section. Use this
1480b57cec5SDimitry Andric   // segment's p_addr to calculate the section's LMA.
1490b57cec5SDimitry Andric   for (const typename ELFT::Phdr &Phdr : *PhdrRangeOrErr)
1500b57cec5SDimitry Andric     if ((Phdr.p_type == ELF::PT_LOAD) && (Phdr.p_vaddr <= Sec.getAddress()) &&
1510b57cec5SDimitry Andric         (Phdr.p_vaddr + Phdr.p_memsz > Sec.getAddress()))
1520b57cec5SDimitry Andric       return Sec.getAddress() - Phdr.p_vaddr + Phdr.p_paddr;
1530b57cec5SDimitry Andric 
1540b57cec5SDimitry Andric   // Return section's VMA if it isn't in a PT_LOAD segment.
1550b57cec5SDimitry Andric   return Sec.getAddress();
1560b57cec5SDimitry Andric }
1570b57cec5SDimitry Andric 
1585ffd83dbSDimitry Andric uint64_t objdump::getELFSectionLMA(const object::ELFSectionRef &Sec) {
1590b57cec5SDimitry Andric   if (const auto *ELFObj = dyn_cast<ELF32LEObjectFile>(Sec.getObject()))
1600b57cec5SDimitry Andric     return getSectionLMA(ELFObj->getELFFile(), Sec);
1610b57cec5SDimitry Andric   else if (const auto *ELFObj = dyn_cast<ELF32BEObjectFile>(Sec.getObject()))
1620b57cec5SDimitry Andric     return getSectionLMA(ELFObj->getELFFile(), Sec);
1630b57cec5SDimitry Andric   else if (const auto *ELFObj = dyn_cast<ELF64LEObjectFile>(Sec.getObject()))
1640b57cec5SDimitry Andric     return getSectionLMA(ELFObj->getELFFile(), Sec);
1650b57cec5SDimitry Andric   const auto *ELFObj = cast<ELF64BEObjectFile>(Sec.getObject());
1660b57cec5SDimitry Andric   return getSectionLMA(ELFObj->getELFFile(), Sec);
1670b57cec5SDimitry Andric }
1680b57cec5SDimitry Andric 
1690b57cec5SDimitry Andric template <class ELFT>
170*e8d8bef9SDimitry Andric static void printDynamicSection(const ELFFile<ELFT> &Elf, StringRef Filename) {
1710b57cec5SDimitry Andric   ArrayRef<typename ELFT::Dyn> DynamicEntries =
172*e8d8bef9SDimitry Andric       unwrapOrError(Elf.dynamicEntries(), Filename);
1735ffd83dbSDimitry Andric 
1745ffd83dbSDimitry Andric   // Find the maximum tag name length to format the value column properly.
1755ffd83dbSDimitry Andric   size_t MaxLen = 0;
1765ffd83dbSDimitry Andric   for (const typename ELFT::Dyn &Dyn : DynamicEntries)
177*e8d8bef9SDimitry Andric     MaxLen = std::max(MaxLen, Elf.getDynamicTagAsString(Dyn.d_tag).size());
1785ffd83dbSDimitry Andric   std::string TagFmt = "  %-" + std::to_string(MaxLen) + "s ";
1795ffd83dbSDimitry Andric 
1800b57cec5SDimitry Andric   outs() << "Dynamic Section:\n";
1810b57cec5SDimitry Andric   for (const typename ELFT::Dyn &Dyn : DynamicEntries) {
1820b57cec5SDimitry Andric     if (Dyn.d_tag == ELF::DT_NULL)
1830b57cec5SDimitry Andric       continue;
1840b57cec5SDimitry Andric 
185*e8d8bef9SDimitry Andric     std::string Str = Elf.getDynamicTagAsString(Dyn.d_tag);
1865ffd83dbSDimitry Andric     outs() << format(TagFmt.c_str(), Str.c_str());
1870b57cec5SDimitry Andric 
1880b57cec5SDimitry Andric     const char *Fmt =
1890b57cec5SDimitry Andric         ELFT::Is64Bits ? "0x%016" PRIx64 "\n" : "0x%08" PRIx64 "\n";
1900b57cec5SDimitry Andric     if (Dyn.d_tag == ELF::DT_NEEDED || Dyn.d_tag == ELF::DT_RPATH ||
1910b57cec5SDimitry Andric         Dyn.d_tag == ELF::DT_RUNPATH || Dyn.d_tag == ELF::DT_SONAME ||
1920b57cec5SDimitry Andric         Dyn.d_tag == ELF::DT_AUXILIARY || Dyn.d_tag == ELF::DT_FILTER) {
1930b57cec5SDimitry Andric       Expected<StringRef> StrTabOrErr = getDynamicStrTab(Elf);
1940b57cec5SDimitry Andric       if (StrTabOrErr) {
1950b57cec5SDimitry Andric         const char *Data = StrTabOrErr.get().data();
1960b57cec5SDimitry Andric         outs() << (Data + Dyn.d_un.d_val) << "\n";
1970b57cec5SDimitry Andric         continue;
1980b57cec5SDimitry Andric       }
1998bcb0991SDimitry Andric       reportWarning(toString(StrTabOrErr.takeError()), Filename);
2000b57cec5SDimitry Andric       consumeError(StrTabOrErr.takeError());
2010b57cec5SDimitry Andric     }
2020b57cec5SDimitry Andric     outs() << format(Fmt, (uint64_t)Dyn.d_un.d_val);
2030b57cec5SDimitry Andric   }
2040b57cec5SDimitry Andric }
2050b57cec5SDimitry Andric 
2065ffd83dbSDimitry Andric template <class ELFT>
207*e8d8bef9SDimitry Andric static void printProgramHeaders(const ELFFile<ELFT> &Obj, StringRef FileName) {
2080b57cec5SDimitry Andric   outs() << "Program Header:\n";
209*e8d8bef9SDimitry Andric   auto ProgramHeaderOrError = Obj.program_headers();
2105ffd83dbSDimitry Andric   if (!ProgramHeaderOrError) {
2115ffd83dbSDimitry Andric     reportWarning("unable to read program headers: " +
2125ffd83dbSDimitry Andric                       toString(ProgramHeaderOrError.takeError()),
2135ffd83dbSDimitry Andric                   FileName);
2145ffd83dbSDimitry Andric     return;
2155ffd83dbSDimitry Andric   }
2165ffd83dbSDimitry Andric 
2170b57cec5SDimitry Andric   for (const typename ELFT::Phdr &Phdr : *ProgramHeaderOrError) {
2180b57cec5SDimitry Andric     switch (Phdr.p_type) {
2190b57cec5SDimitry Andric     case ELF::PT_DYNAMIC:
2200b57cec5SDimitry Andric       outs() << " DYNAMIC ";
2210b57cec5SDimitry Andric       break;
2220b57cec5SDimitry Andric     case ELF::PT_GNU_EH_FRAME:
2230b57cec5SDimitry Andric       outs() << "EH_FRAME ";
2240b57cec5SDimitry Andric       break;
2250b57cec5SDimitry Andric     case ELF::PT_GNU_RELRO:
2260b57cec5SDimitry Andric       outs() << "   RELRO ";
2270b57cec5SDimitry Andric       break;
228480093f4SDimitry Andric     case ELF::PT_GNU_PROPERTY:
229480093f4SDimitry Andric       outs() << "   PROPERTY ";
230480093f4SDimitry Andric       break;
2310b57cec5SDimitry Andric     case ELF::PT_GNU_STACK:
2320b57cec5SDimitry Andric       outs() << "   STACK ";
2330b57cec5SDimitry Andric       break;
2340b57cec5SDimitry Andric     case ELF::PT_INTERP:
2350b57cec5SDimitry Andric       outs() << "  INTERP ";
2360b57cec5SDimitry Andric       break;
2370b57cec5SDimitry Andric     case ELF::PT_LOAD:
2380b57cec5SDimitry Andric       outs() << "    LOAD ";
2390b57cec5SDimitry Andric       break;
2400b57cec5SDimitry Andric     case ELF::PT_NOTE:
2410b57cec5SDimitry Andric       outs() << "    NOTE ";
2420b57cec5SDimitry Andric       break;
2430b57cec5SDimitry Andric     case ELF::PT_OPENBSD_BOOTDATA:
2440b57cec5SDimitry Andric       outs() << "    OPENBSD_BOOTDATA ";
2450b57cec5SDimitry Andric       break;
2460b57cec5SDimitry Andric     case ELF::PT_OPENBSD_RANDOMIZE:
2470b57cec5SDimitry Andric       outs() << "    OPENBSD_RANDOMIZE ";
2480b57cec5SDimitry Andric       break;
2490b57cec5SDimitry Andric     case ELF::PT_OPENBSD_WXNEEDED:
2500b57cec5SDimitry Andric       outs() << "    OPENBSD_WXNEEDED ";
2510b57cec5SDimitry Andric       break;
2520b57cec5SDimitry Andric     case ELF::PT_PHDR:
2530b57cec5SDimitry Andric       outs() << "    PHDR ";
2540b57cec5SDimitry Andric       break;
2550b57cec5SDimitry Andric     case ELF::PT_TLS:
2560b57cec5SDimitry Andric       outs() << "    TLS ";
2570b57cec5SDimitry Andric       break;
2580b57cec5SDimitry Andric     default:
2590b57cec5SDimitry Andric       outs() << " UNKNOWN ";
2600b57cec5SDimitry Andric     }
2610b57cec5SDimitry Andric 
2620b57cec5SDimitry Andric     const char *Fmt = ELFT::Is64Bits ? "0x%016" PRIx64 " " : "0x%08" PRIx64 " ";
2630b57cec5SDimitry Andric 
2640b57cec5SDimitry Andric     outs() << "off    " << format(Fmt, (uint64_t)Phdr.p_offset) << "vaddr "
2650b57cec5SDimitry Andric            << format(Fmt, (uint64_t)Phdr.p_vaddr) << "paddr "
2660b57cec5SDimitry Andric            << format(Fmt, (uint64_t)Phdr.p_paddr)
2670b57cec5SDimitry Andric            << format("align 2**%u\n",
2680b57cec5SDimitry Andric                      countTrailingZeros<uint64_t>(Phdr.p_align))
2690b57cec5SDimitry Andric            << "         filesz " << format(Fmt, (uint64_t)Phdr.p_filesz)
2700b57cec5SDimitry Andric            << "memsz " << format(Fmt, (uint64_t)Phdr.p_memsz) << "flags "
2710b57cec5SDimitry Andric            << ((Phdr.p_flags & ELF::PF_R) ? "r" : "-")
2720b57cec5SDimitry Andric            << ((Phdr.p_flags & ELF::PF_W) ? "w" : "-")
2730b57cec5SDimitry Andric            << ((Phdr.p_flags & ELF::PF_X) ? "x" : "-") << "\n";
2740b57cec5SDimitry Andric   }
2750b57cec5SDimitry Andric   outs() << "\n";
2760b57cec5SDimitry Andric }
2770b57cec5SDimitry Andric 
2780b57cec5SDimitry Andric template <class ELFT>
2795ffd83dbSDimitry Andric static void printSymbolVersionDependency(ArrayRef<uint8_t> Contents,
2800b57cec5SDimitry Andric                                          StringRef StrTab) {
2810b57cec5SDimitry Andric   outs() << "Version References:\n";
2820b57cec5SDimitry Andric 
2830b57cec5SDimitry Andric   const uint8_t *Buf = Contents.data();
2840b57cec5SDimitry Andric   while (Buf) {
2850b57cec5SDimitry Andric     auto *Verneed = reinterpret_cast<const typename ELFT::Verneed *>(Buf);
2860b57cec5SDimitry Andric     outs() << "  required from "
2870b57cec5SDimitry Andric            << StringRef(StrTab.drop_front(Verneed->vn_file).data()) << ":\n";
2880b57cec5SDimitry Andric 
2890b57cec5SDimitry Andric     const uint8_t *BufAux = Buf + Verneed->vn_aux;
2900b57cec5SDimitry Andric     while (BufAux) {
2910b57cec5SDimitry Andric       auto *Vernaux = reinterpret_cast<const typename ELFT::Vernaux *>(BufAux);
2920b57cec5SDimitry Andric       outs() << "    "
2930b57cec5SDimitry Andric              << format("0x%08" PRIx32 " ", (uint32_t)Vernaux->vna_hash)
2940b57cec5SDimitry Andric              << format("0x%02" PRIx16 " ", (uint16_t)Vernaux->vna_flags)
2950b57cec5SDimitry Andric              << format("%02" PRIu16 " ", (uint16_t)Vernaux->vna_other)
2960b57cec5SDimitry Andric              << StringRef(StrTab.drop_front(Vernaux->vna_name).data()) << '\n';
2970b57cec5SDimitry Andric       BufAux = Vernaux->vna_next ? BufAux + Vernaux->vna_next : nullptr;
2980b57cec5SDimitry Andric     }
2990b57cec5SDimitry Andric     Buf = Verneed->vn_next ? Buf + Verneed->vn_next : nullptr;
3000b57cec5SDimitry Andric   }
3010b57cec5SDimitry Andric }
3020b57cec5SDimitry Andric 
3030b57cec5SDimitry Andric template <class ELFT>
3045ffd83dbSDimitry Andric static void printSymbolVersionDefinition(const typename ELFT::Shdr &Shdr,
3050b57cec5SDimitry Andric                                          ArrayRef<uint8_t> Contents,
3060b57cec5SDimitry Andric                                          StringRef StrTab) {
3070b57cec5SDimitry Andric   outs() << "Version definitions:\n";
3080b57cec5SDimitry Andric 
3090b57cec5SDimitry Andric   const uint8_t *Buf = Contents.data();
3100b57cec5SDimitry Andric   uint32_t VerdefIndex = 1;
3110b57cec5SDimitry Andric   // sh_info contains the number of entries in the SHT_GNU_verdef section. To
3120b57cec5SDimitry Andric   // make the index column have consistent width, we should insert blank spaces
3130b57cec5SDimitry Andric   // according to sh_info.
3140b57cec5SDimitry Andric   uint16_t VerdefIndexWidth = std::to_string(Shdr.sh_info).size();
3150b57cec5SDimitry Andric   while (Buf) {
3160b57cec5SDimitry Andric     auto *Verdef = reinterpret_cast<const typename ELFT::Verdef *>(Buf);
3170b57cec5SDimitry Andric     outs() << format_decimal(VerdefIndex++, VerdefIndexWidth) << " "
3180b57cec5SDimitry Andric            << format("0x%02" PRIx16 " ", (uint16_t)Verdef->vd_flags)
3190b57cec5SDimitry Andric            << format("0x%08" PRIx32 " ", (uint32_t)Verdef->vd_hash);
3200b57cec5SDimitry Andric 
3210b57cec5SDimitry Andric     const uint8_t *BufAux = Buf + Verdef->vd_aux;
3220b57cec5SDimitry Andric     uint16_t VerdauxIndex = 0;
3230b57cec5SDimitry Andric     while (BufAux) {
3240b57cec5SDimitry Andric       auto *Verdaux = reinterpret_cast<const typename ELFT::Verdaux *>(BufAux);
3250b57cec5SDimitry Andric       if (VerdauxIndex)
3260b57cec5SDimitry Andric         outs() << std::string(VerdefIndexWidth + 17, ' ');
3270b57cec5SDimitry Andric       outs() << StringRef(StrTab.drop_front(Verdaux->vda_name).data()) << '\n';
3280b57cec5SDimitry Andric       BufAux = Verdaux->vda_next ? BufAux + Verdaux->vda_next : nullptr;
3290b57cec5SDimitry Andric       ++VerdauxIndex;
3300b57cec5SDimitry Andric     }
3310b57cec5SDimitry Andric     Buf = Verdef->vd_next ? Buf + Verdef->vd_next : nullptr;
3320b57cec5SDimitry Andric   }
3330b57cec5SDimitry Andric }
3340b57cec5SDimitry Andric 
3350b57cec5SDimitry Andric template <class ELFT>
336*e8d8bef9SDimitry Andric static void printSymbolVersionInfo(const ELFFile<ELFT> &Elf,
3375ffd83dbSDimitry Andric                                    StringRef FileName) {
3380b57cec5SDimitry Andric   ArrayRef<typename ELFT::Shdr> Sections =
339*e8d8bef9SDimitry Andric       unwrapOrError(Elf.sections(), FileName);
3400b57cec5SDimitry Andric   for (const typename ELFT::Shdr &Shdr : Sections) {
3410b57cec5SDimitry Andric     if (Shdr.sh_type != ELF::SHT_GNU_verneed &&
3420b57cec5SDimitry Andric         Shdr.sh_type != ELF::SHT_GNU_verdef)
3430b57cec5SDimitry Andric       continue;
3440b57cec5SDimitry Andric 
3450b57cec5SDimitry Andric     ArrayRef<uint8_t> Contents =
346*e8d8bef9SDimitry Andric         unwrapOrError(Elf.getSectionContents(Shdr), FileName);
3470b57cec5SDimitry Andric     const typename ELFT::Shdr *StrTabSec =
348*e8d8bef9SDimitry Andric         unwrapOrError(Elf.getSection(Shdr.sh_link), FileName);
349*e8d8bef9SDimitry Andric     StringRef StrTab = unwrapOrError(Elf.getStringTable(*StrTabSec), FileName);
3500b57cec5SDimitry Andric 
3510b57cec5SDimitry Andric     if (Shdr.sh_type == ELF::SHT_GNU_verneed)
3520b57cec5SDimitry Andric       printSymbolVersionDependency<ELFT>(Contents, StrTab);
3530b57cec5SDimitry Andric     else
3540b57cec5SDimitry Andric       printSymbolVersionDefinition<ELFT>(Shdr, Contents, StrTab);
3550b57cec5SDimitry Andric   }
3560b57cec5SDimitry Andric }
3570b57cec5SDimitry Andric 
3585ffd83dbSDimitry Andric void objdump::printELFFileHeader(const object::ObjectFile *Obj) {
3590b57cec5SDimitry Andric   if (const auto *ELFObj = dyn_cast<ELF32LEObjectFile>(Obj))
3605ffd83dbSDimitry Andric     printProgramHeaders(ELFObj->getELFFile(), Obj->getFileName());
3610b57cec5SDimitry Andric   else if (const auto *ELFObj = dyn_cast<ELF32BEObjectFile>(Obj))
3625ffd83dbSDimitry Andric     printProgramHeaders(ELFObj->getELFFile(), Obj->getFileName());
3630b57cec5SDimitry Andric   else if (const auto *ELFObj = dyn_cast<ELF64LEObjectFile>(Obj))
3645ffd83dbSDimitry Andric     printProgramHeaders(ELFObj->getELFFile(), Obj->getFileName());
3650b57cec5SDimitry Andric   else if (const auto *ELFObj = dyn_cast<ELF64BEObjectFile>(Obj))
3665ffd83dbSDimitry Andric     printProgramHeaders(ELFObj->getELFFile(), Obj->getFileName());
3670b57cec5SDimitry Andric }
3680b57cec5SDimitry Andric 
3695ffd83dbSDimitry Andric void objdump::printELFDynamicSection(const object::ObjectFile *Obj) {
3700b57cec5SDimitry Andric   if (const auto *ELFObj = dyn_cast<ELF32LEObjectFile>(Obj))
3710b57cec5SDimitry Andric     printDynamicSection(ELFObj->getELFFile(), Obj->getFileName());
3720b57cec5SDimitry Andric   else if (const auto *ELFObj = dyn_cast<ELF32BEObjectFile>(Obj))
3730b57cec5SDimitry Andric     printDynamicSection(ELFObj->getELFFile(), Obj->getFileName());
3740b57cec5SDimitry Andric   else if (const auto *ELFObj = dyn_cast<ELF64LEObjectFile>(Obj))
3750b57cec5SDimitry Andric     printDynamicSection(ELFObj->getELFFile(), Obj->getFileName());
3760b57cec5SDimitry Andric   else if (const auto *ELFObj = dyn_cast<ELF64BEObjectFile>(Obj))
3770b57cec5SDimitry Andric     printDynamicSection(ELFObj->getELFFile(), Obj->getFileName());
3780b57cec5SDimitry Andric }
3790b57cec5SDimitry Andric 
3805ffd83dbSDimitry Andric void objdump::printELFSymbolVersionInfo(const object::ObjectFile *Obj) {
3810b57cec5SDimitry Andric   if (const auto *ELFObj = dyn_cast<ELF32LEObjectFile>(Obj))
3820b57cec5SDimitry Andric     printSymbolVersionInfo(ELFObj->getELFFile(), Obj->getFileName());
3830b57cec5SDimitry Andric   else if (const auto *ELFObj = dyn_cast<ELF32BEObjectFile>(Obj))
3840b57cec5SDimitry Andric     printSymbolVersionInfo(ELFObj->getELFFile(), Obj->getFileName());
3850b57cec5SDimitry Andric   else if (const auto *ELFObj = dyn_cast<ELF64LEObjectFile>(Obj))
3860b57cec5SDimitry Andric     printSymbolVersionInfo(ELFObj->getELFFile(), Obj->getFileName());
3870b57cec5SDimitry Andric   else if (const auto *ELFObj = dyn_cast<ELF64BEObjectFile>(Obj))
3880b57cec5SDimitry Andric     printSymbolVersionInfo(ELFObj->getELFFile(), Obj->getFileName());
3890b57cec5SDimitry Andric }
390