xref: /freebsd/contrib/llvm-project/llvm/tools/llvm-objdump/ELFDump.cpp (revision 5ffd83dbcc34f10e07f6d3e968ae6365869615f4)
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 
14*5ffd83dbSDimitry Andric #include "ELFDump.h"
15*5ffd83dbSDimitry 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 
23*5ffd83dbSDimitry Andric using namespace llvm;
240b57cec5SDimitry Andric using namespace llvm::object;
25*5ffd83dbSDimitry Andric using namespace llvm::objdump;
260b57cec5SDimitry Andric 
270b57cec5SDimitry Andric template <class ELFT>
280b57cec5SDimitry Andric static Expected<StringRef> getDynamicStrTab(const ELFFile<ELFT> *Elf) {
290b57cec5SDimitry 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) {
350b57cec5SDimitry 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.
430b57cec5SDimitry 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)
490b57cec5SDimitry 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) {
590b57cec5SDimitry 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();
880b57cec5SDimitry Andric     const typename ELFT::Sym *Sym = Obj->getSymbol(SI->getRawDataRefImpl());
890b57cec5SDimitry Andric     if (Sym->getType() == ELF::STT_SECTION) {
900b57cec5SDimitry Andric       Expected<section_iterator> SymSI = SI->getSection();
910b57cec5SDimitry Andric       if (!SymSI)
920b57cec5SDimitry Andric         return SymSI.takeError();
930b57cec5SDimitry Andric       const typename ELFT::Shdr *SymSec =
940b57cec5SDimitry Andric           Obj->getSection((*SymSI)->getRawDataRefImpl());
950b57cec5SDimitry Andric       auto SecName = EF.getSectionName(SymSec);
960b57cec5SDimitry Andric       if (!SecName)
970b57cec5SDimitry Andric         return SecName.takeError();
980b57cec5SDimitry Andric       Fmt << *SecName;
990b57cec5SDimitry Andric     } else {
1000b57cec5SDimitry Andric       Expected<StringRef> SymName = SI->getName();
1010b57cec5SDimitry Andric       if (!SymName)
1020b57cec5SDimitry Andric         return SymName.takeError();
1030b57cec5SDimitry Andric       if (Demangle)
104*5ffd83dbSDimitry Andric         Fmt << demangle(std::string(*SymName));
1050b57cec5SDimitry Andric       else
1060b57cec5SDimitry Andric         Fmt << *SymName;
1070b57cec5SDimitry Andric     }
1080b57cec5SDimitry Andric   } else {
1090b57cec5SDimitry Andric     Fmt << "*ABS*";
1100b57cec5SDimitry Andric   }
111480093f4SDimitry Andric   if (Addend != 0) {
112480093f4SDimitry Andric       Fmt << (Addend < 0
113480093f4SDimitry Andric           ? "-"
114480093f4SDimitry Andric           : "+") << format("0x%" PRIx64,
115480093f4SDimitry Andric                           (Addend < 0 ? -(uint64_t)Addend : (uint64_t)Addend));
116480093f4SDimitry Andric   }
1170b57cec5SDimitry Andric   Fmt.flush();
1180b57cec5SDimitry Andric   Result.append(FmtBuf.begin(), FmtBuf.end());
1190b57cec5SDimitry Andric   return Error::success();
1200b57cec5SDimitry Andric }
1210b57cec5SDimitry Andric 
122*5ffd83dbSDimitry Andric Error objdump::getELFRelocationValueString(const ELFObjectFileBase *Obj,
1230b57cec5SDimitry Andric                                            const RelocationRef &Rel,
1240b57cec5SDimitry Andric                                            SmallVectorImpl<char> &Result) {
1250b57cec5SDimitry Andric   if (auto *ELF32LE = dyn_cast<ELF32LEObjectFile>(Obj))
1260b57cec5SDimitry Andric     return getRelocationValueString(ELF32LE, Rel, Result);
1270b57cec5SDimitry Andric   if (auto *ELF64LE = dyn_cast<ELF64LEObjectFile>(Obj))
1280b57cec5SDimitry Andric     return getRelocationValueString(ELF64LE, Rel, Result);
1290b57cec5SDimitry Andric   if (auto *ELF32BE = dyn_cast<ELF32BEObjectFile>(Obj))
1300b57cec5SDimitry Andric     return getRelocationValueString(ELF32BE, Rel, Result);
1310b57cec5SDimitry Andric   auto *ELF64BE = cast<ELF64BEObjectFile>(Obj);
1320b57cec5SDimitry Andric   return getRelocationValueString(ELF64BE, Rel, Result);
1330b57cec5SDimitry Andric }
1340b57cec5SDimitry Andric 
1350b57cec5SDimitry Andric template <class ELFT>
1360b57cec5SDimitry Andric static uint64_t getSectionLMA(const ELFFile<ELFT> *Obj,
1370b57cec5SDimitry Andric                               const object::ELFSectionRef &Sec) {
1380b57cec5SDimitry Andric   auto PhdrRangeOrErr = Obj->program_headers();
1390b57cec5SDimitry Andric   if (!PhdrRangeOrErr)
1400b57cec5SDimitry Andric     report_fatal_error(toString(PhdrRangeOrErr.takeError()));
1410b57cec5SDimitry Andric 
1420b57cec5SDimitry Andric   // Search for a PT_LOAD segment containing the requested section. Use this
1430b57cec5SDimitry Andric   // segment's p_addr to calculate the section's LMA.
1440b57cec5SDimitry Andric   for (const typename ELFT::Phdr &Phdr : *PhdrRangeOrErr)
1450b57cec5SDimitry Andric     if ((Phdr.p_type == ELF::PT_LOAD) && (Phdr.p_vaddr <= Sec.getAddress()) &&
1460b57cec5SDimitry Andric         (Phdr.p_vaddr + Phdr.p_memsz > Sec.getAddress()))
1470b57cec5SDimitry Andric       return Sec.getAddress() - Phdr.p_vaddr + Phdr.p_paddr;
1480b57cec5SDimitry Andric 
1490b57cec5SDimitry Andric   // Return section's VMA if it isn't in a PT_LOAD segment.
1500b57cec5SDimitry Andric   return Sec.getAddress();
1510b57cec5SDimitry Andric }
1520b57cec5SDimitry Andric 
153*5ffd83dbSDimitry Andric uint64_t objdump::getELFSectionLMA(const object::ELFSectionRef &Sec) {
1540b57cec5SDimitry Andric   if (const auto *ELFObj = dyn_cast<ELF32LEObjectFile>(Sec.getObject()))
1550b57cec5SDimitry Andric     return getSectionLMA(ELFObj->getELFFile(), Sec);
1560b57cec5SDimitry Andric   else if (const auto *ELFObj = dyn_cast<ELF32BEObjectFile>(Sec.getObject()))
1570b57cec5SDimitry Andric     return getSectionLMA(ELFObj->getELFFile(), Sec);
1580b57cec5SDimitry Andric   else if (const auto *ELFObj = dyn_cast<ELF64LEObjectFile>(Sec.getObject()))
1590b57cec5SDimitry Andric     return getSectionLMA(ELFObj->getELFFile(), Sec);
1600b57cec5SDimitry Andric   const auto *ELFObj = cast<ELF64BEObjectFile>(Sec.getObject());
1610b57cec5SDimitry Andric   return getSectionLMA(ELFObj->getELFFile(), Sec);
1620b57cec5SDimitry Andric }
1630b57cec5SDimitry Andric 
1640b57cec5SDimitry Andric template <class ELFT>
165*5ffd83dbSDimitry Andric static void printDynamicSection(const ELFFile<ELFT> *Elf, StringRef Filename) {
1660b57cec5SDimitry Andric   ArrayRef<typename ELFT::Dyn> DynamicEntries =
1670b57cec5SDimitry Andric       unwrapOrError(Elf->dynamicEntries(), Filename);
168*5ffd83dbSDimitry Andric 
169*5ffd83dbSDimitry Andric   // Find the maximum tag name length to format the value column properly.
170*5ffd83dbSDimitry Andric   size_t MaxLen = 0;
171*5ffd83dbSDimitry Andric   for (const typename ELFT::Dyn &Dyn : DynamicEntries)
172*5ffd83dbSDimitry Andric     MaxLen = std::max(MaxLen, Elf->getDynamicTagAsString(Dyn.d_tag).size());
173*5ffd83dbSDimitry Andric   std::string TagFmt = "  %-" + std::to_string(MaxLen) + "s ";
174*5ffd83dbSDimitry Andric 
1750b57cec5SDimitry Andric   outs() << "Dynamic Section:\n";
1760b57cec5SDimitry Andric   for (const typename ELFT::Dyn &Dyn : DynamicEntries) {
1770b57cec5SDimitry Andric     if (Dyn.d_tag == ELF::DT_NULL)
1780b57cec5SDimitry Andric       continue;
1790b57cec5SDimitry Andric 
1800b57cec5SDimitry Andric     std::string Str = Elf->getDynamicTagAsString(Dyn.d_tag);
181*5ffd83dbSDimitry Andric     outs() << format(TagFmt.c_str(), Str.c_str());
1820b57cec5SDimitry Andric 
1830b57cec5SDimitry Andric     const char *Fmt =
1840b57cec5SDimitry Andric         ELFT::Is64Bits ? "0x%016" PRIx64 "\n" : "0x%08" PRIx64 "\n";
1850b57cec5SDimitry Andric     if (Dyn.d_tag == ELF::DT_NEEDED || Dyn.d_tag == ELF::DT_RPATH ||
1860b57cec5SDimitry Andric         Dyn.d_tag == ELF::DT_RUNPATH || Dyn.d_tag == ELF::DT_SONAME ||
1870b57cec5SDimitry Andric         Dyn.d_tag == ELF::DT_AUXILIARY || Dyn.d_tag == ELF::DT_FILTER) {
1880b57cec5SDimitry Andric       Expected<StringRef> StrTabOrErr = getDynamicStrTab(Elf);
1890b57cec5SDimitry Andric       if (StrTabOrErr) {
1900b57cec5SDimitry Andric         const char *Data = StrTabOrErr.get().data();
1910b57cec5SDimitry Andric         outs() << (Data + Dyn.d_un.d_val) << "\n";
1920b57cec5SDimitry Andric         continue;
1930b57cec5SDimitry Andric       }
1948bcb0991SDimitry Andric       reportWarning(toString(StrTabOrErr.takeError()), Filename);
1950b57cec5SDimitry Andric       consumeError(StrTabOrErr.takeError());
1960b57cec5SDimitry Andric     }
1970b57cec5SDimitry Andric     outs() << format(Fmt, (uint64_t)Dyn.d_un.d_val);
1980b57cec5SDimitry Andric   }
1990b57cec5SDimitry Andric }
2000b57cec5SDimitry Andric 
201*5ffd83dbSDimitry Andric template <class ELFT>
202*5ffd83dbSDimitry Andric static void printProgramHeaders(const ELFFile<ELFT> *Obj, StringRef FileName) {
2030b57cec5SDimitry Andric   outs() << "Program Header:\n";
204*5ffd83dbSDimitry Andric   auto ProgramHeaderOrError = Obj->program_headers();
205*5ffd83dbSDimitry Andric   if (!ProgramHeaderOrError) {
206*5ffd83dbSDimitry Andric     reportWarning("unable to read program headers: " +
207*5ffd83dbSDimitry Andric                       toString(ProgramHeaderOrError.takeError()),
208*5ffd83dbSDimitry Andric                   FileName);
209*5ffd83dbSDimitry Andric     return;
210*5ffd83dbSDimitry Andric   }
211*5ffd83dbSDimitry Andric 
2120b57cec5SDimitry Andric   for (const typename ELFT::Phdr &Phdr : *ProgramHeaderOrError) {
2130b57cec5SDimitry Andric     switch (Phdr.p_type) {
2140b57cec5SDimitry Andric     case ELF::PT_DYNAMIC:
2150b57cec5SDimitry Andric       outs() << " DYNAMIC ";
2160b57cec5SDimitry Andric       break;
2170b57cec5SDimitry Andric     case ELF::PT_GNU_EH_FRAME:
2180b57cec5SDimitry Andric       outs() << "EH_FRAME ";
2190b57cec5SDimitry Andric       break;
2200b57cec5SDimitry Andric     case ELF::PT_GNU_RELRO:
2210b57cec5SDimitry Andric       outs() << "   RELRO ";
2220b57cec5SDimitry Andric       break;
223480093f4SDimitry Andric     case ELF::PT_GNU_PROPERTY:
224480093f4SDimitry Andric       outs() << "   PROPERTY ";
225480093f4SDimitry Andric       break;
2260b57cec5SDimitry Andric     case ELF::PT_GNU_STACK:
2270b57cec5SDimitry Andric       outs() << "   STACK ";
2280b57cec5SDimitry Andric       break;
2290b57cec5SDimitry Andric     case ELF::PT_INTERP:
2300b57cec5SDimitry Andric       outs() << "  INTERP ";
2310b57cec5SDimitry Andric       break;
2320b57cec5SDimitry Andric     case ELF::PT_LOAD:
2330b57cec5SDimitry Andric       outs() << "    LOAD ";
2340b57cec5SDimitry Andric       break;
2350b57cec5SDimitry Andric     case ELF::PT_NOTE:
2360b57cec5SDimitry Andric       outs() << "    NOTE ";
2370b57cec5SDimitry Andric       break;
2380b57cec5SDimitry Andric     case ELF::PT_OPENBSD_BOOTDATA:
2390b57cec5SDimitry Andric       outs() << "    OPENBSD_BOOTDATA ";
2400b57cec5SDimitry Andric       break;
2410b57cec5SDimitry Andric     case ELF::PT_OPENBSD_RANDOMIZE:
2420b57cec5SDimitry Andric       outs() << "    OPENBSD_RANDOMIZE ";
2430b57cec5SDimitry Andric       break;
2440b57cec5SDimitry Andric     case ELF::PT_OPENBSD_WXNEEDED:
2450b57cec5SDimitry Andric       outs() << "    OPENBSD_WXNEEDED ";
2460b57cec5SDimitry Andric       break;
2470b57cec5SDimitry Andric     case ELF::PT_PHDR:
2480b57cec5SDimitry Andric       outs() << "    PHDR ";
2490b57cec5SDimitry Andric       break;
2500b57cec5SDimitry Andric     case ELF::PT_TLS:
2510b57cec5SDimitry Andric       outs() << "    TLS ";
2520b57cec5SDimitry Andric       break;
2530b57cec5SDimitry Andric     default:
2540b57cec5SDimitry Andric       outs() << " UNKNOWN ";
2550b57cec5SDimitry Andric     }
2560b57cec5SDimitry Andric 
2570b57cec5SDimitry Andric     const char *Fmt = ELFT::Is64Bits ? "0x%016" PRIx64 " " : "0x%08" PRIx64 " ";
2580b57cec5SDimitry Andric 
2590b57cec5SDimitry Andric     outs() << "off    " << format(Fmt, (uint64_t)Phdr.p_offset) << "vaddr "
2600b57cec5SDimitry Andric            << format(Fmt, (uint64_t)Phdr.p_vaddr) << "paddr "
2610b57cec5SDimitry Andric            << format(Fmt, (uint64_t)Phdr.p_paddr)
2620b57cec5SDimitry Andric            << format("align 2**%u\n",
2630b57cec5SDimitry Andric                      countTrailingZeros<uint64_t>(Phdr.p_align))
2640b57cec5SDimitry Andric            << "         filesz " << format(Fmt, (uint64_t)Phdr.p_filesz)
2650b57cec5SDimitry Andric            << "memsz " << format(Fmt, (uint64_t)Phdr.p_memsz) << "flags "
2660b57cec5SDimitry Andric            << ((Phdr.p_flags & ELF::PF_R) ? "r" : "-")
2670b57cec5SDimitry Andric            << ((Phdr.p_flags & ELF::PF_W) ? "w" : "-")
2680b57cec5SDimitry Andric            << ((Phdr.p_flags & ELF::PF_X) ? "x" : "-") << "\n";
2690b57cec5SDimitry Andric   }
2700b57cec5SDimitry Andric   outs() << "\n";
2710b57cec5SDimitry Andric }
2720b57cec5SDimitry Andric 
2730b57cec5SDimitry Andric template <class ELFT>
274*5ffd83dbSDimitry Andric static void printSymbolVersionDependency(ArrayRef<uint8_t> Contents,
2750b57cec5SDimitry Andric                                          StringRef StrTab) {
2760b57cec5SDimitry Andric   outs() << "Version References:\n";
2770b57cec5SDimitry Andric 
2780b57cec5SDimitry Andric   const uint8_t *Buf = Contents.data();
2790b57cec5SDimitry Andric   while (Buf) {
2800b57cec5SDimitry Andric     auto *Verneed = reinterpret_cast<const typename ELFT::Verneed *>(Buf);
2810b57cec5SDimitry Andric     outs() << "  required from "
2820b57cec5SDimitry Andric            << StringRef(StrTab.drop_front(Verneed->vn_file).data()) << ":\n";
2830b57cec5SDimitry Andric 
2840b57cec5SDimitry Andric     const uint8_t *BufAux = Buf + Verneed->vn_aux;
2850b57cec5SDimitry Andric     while (BufAux) {
2860b57cec5SDimitry Andric       auto *Vernaux = reinterpret_cast<const typename ELFT::Vernaux *>(BufAux);
2870b57cec5SDimitry Andric       outs() << "    "
2880b57cec5SDimitry Andric              << format("0x%08" PRIx32 " ", (uint32_t)Vernaux->vna_hash)
2890b57cec5SDimitry Andric              << format("0x%02" PRIx16 " ", (uint16_t)Vernaux->vna_flags)
2900b57cec5SDimitry Andric              << format("%02" PRIu16 " ", (uint16_t)Vernaux->vna_other)
2910b57cec5SDimitry Andric              << StringRef(StrTab.drop_front(Vernaux->vna_name).data()) << '\n';
2920b57cec5SDimitry Andric       BufAux = Vernaux->vna_next ? BufAux + Vernaux->vna_next : nullptr;
2930b57cec5SDimitry Andric     }
2940b57cec5SDimitry Andric     Buf = Verneed->vn_next ? Buf + Verneed->vn_next : nullptr;
2950b57cec5SDimitry Andric   }
2960b57cec5SDimitry Andric }
2970b57cec5SDimitry Andric 
2980b57cec5SDimitry Andric template <class ELFT>
299*5ffd83dbSDimitry Andric static void printSymbolVersionDefinition(const typename ELFT::Shdr &Shdr,
3000b57cec5SDimitry Andric                                          ArrayRef<uint8_t> Contents,
3010b57cec5SDimitry Andric                                          StringRef StrTab) {
3020b57cec5SDimitry Andric   outs() << "Version definitions:\n";
3030b57cec5SDimitry Andric 
3040b57cec5SDimitry Andric   const uint8_t *Buf = Contents.data();
3050b57cec5SDimitry Andric   uint32_t VerdefIndex = 1;
3060b57cec5SDimitry Andric   // sh_info contains the number of entries in the SHT_GNU_verdef section. To
3070b57cec5SDimitry Andric   // make the index column have consistent width, we should insert blank spaces
3080b57cec5SDimitry Andric   // according to sh_info.
3090b57cec5SDimitry Andric   uint16_t VerdefIndexWidth = std::to_string(Shdr.sh_info).size();
3100b57cec5SDimitry Andric   while (Buf) {
3110b57cec5SDimitry Andric     auto *Verdef = reinterpret_cast<const typename ELFT::Verdef *>(Buf);
3120b57cec5SDimitry Andric     outs() << format_decimal(VerdefIndex++, VerdefIndexWidth) << " "
3130b57cec5SDimitry Andric            << format("0x%02" PRIx16 " ", (uint16_t)Verdef->vd_flags)
3140b57cec5SDimitry Andric            << format("0x%08" PRIx32 " ", (uint32_t)Verdef->vd_hash);
3150b57cec5SDimitry Andric 
3160b57cec5SDimitry Andric     const uint8_t *BufAux = Buf + Verdef->vd_aux;
3170b57cec5SDimitry Andric     uint16_t VerdauxIndex = 0;
3180b57cec5SDimitry Andric     while (BufAux) {
3190b57cec5SDimitry Andric       auto *Verdaux = reinterpret_cast<const typename ELFT::Verdaux *>(BufAux);
3200b57cec5SDimitry Andric       if (VerdauxIndex)
3210b57cec5SDimitry Andric         outs() << std::string(VerdefIndexWidth + 17, ' ');
3220b57cec5SDimitry Andric       outs() << StringRef(StrTab.drop_front(Verdaux->vda_name).data()) << '\n';
3230b57cec5SDimitry Andric       BufAux = Verdaux->vda_next ? BufAux + Verdaux->vda_next : nullptr;
3240b57cec5SDimitry Andric       ++VerdauxIndex;
3250b57cec5SDimitry Andric     }
3260b57cec5SDimitry Andric     Buf = Verdef->vd_next ? Buf + Verdef->vd_next : nullptr;
3270b57cec5SDimitry Andric   }
3280b57cec5SDimitry Andric }
3290b57cec5SDimitry Andric 
3300b57cec5SDimitry Andric template <class ELFT>
331*5ffd83dbSDimitry Andric static void printSymbolVersionInfo(const ELFFile<ELFT> *Elf,
332*5ffd83dbSDimitry Andric                                    StringRef FileName) {
3330b57cec5SDimitry Andric   ArrayRef<typename ELFT::Shdr> Sections =
3340b57cec5SDimitry Andric       unwrapOrError(Elf->sections(), FileName);
3350b57cec5SDimitry Andric   for (const typename ELFT::Shdr &Shdr : Sections) {
3360b57cec5SDimitry Andric     if (Shdr.sh_type != ELF::SHT_GNU_verneed &&
3370b57cec5SDimitry Andric         Shdr.sh_type != ELF::SHT_GNU_verdef)
3380b57cec5SDimitry Andric       continue;
3390b57cec5SDimitry Andric 
3400b57cec5SDimitry Andric     ArrayRef<uint8_t> Contents =
3410b57cec5SDimitry Andric         unwrapOrError(Elf->getSectionContents(&Shdr), FileName);
3420b57cec5SDimitry Andric     const typename ELFT::Shdr *StrTabSec =
3430b57cec5SDimitry Andric         unwrapOrError(Elf->getSection(Shdr.sh_link), FileName);
3440b57cec5SDimitry Andric     StringRef StrTab = unwrapOrError(Elf->getStringTable(StrTabSec), FileName);
3450b57cec5SDimitry Andric 
3460b57cec5SDimitry Andric     if (Shdr.sh_type == ELF::SHT_GNU_verneed)
3470b57cec5SDimitry Andric       printSymbolVersionDependency<ELFT>(Contents, StrTab);
3480b57cec5SDimitry Andric     else
3490b57cec5SDimitry Andric       printSymbolVersionDefinition<ELFT>(Shdr, Contents, StrTab);
3500b57cec5SDimitry Andric   }
3510b57cec5SDimitry Andric }
3520b57cec5SDimitry Andric 
353*5ffd83dbSDimitry Andric void objdump::printELFFileHeader(const object::ObjectFile *Obj) {
3540b57cec5SDimitry Andric   if (const auto *ELFObj = dyn_cast<ELF32LEObjectFile>(Obj))
355*5ffd83dbSDimitry Andric     printProgramHeaders(ELFObj->getELFFile(), Obj->getFileName());
3560b57cec5SDimitry Andric   else if (const auto *ELFObj = dyn_cast<ELF32BEObjectFile>(Obj))
357*5ffd83dbSDimitry Andric     printProgramHeaders(ELFObj->getELFFile(), Obj->getFileName());
3580b57cec5SDimitry Andric   else if (const auto *ELFObj = dyn_cast<ELF64LEObjectFile>(Obj))
359*5ffd83dbSDimitry Andric     printProgramHeaders(ELFObj->getELFFile(), Obj->getFileName());
3600b57cec5SDimitry Andric   else if (const auto *ELFObj = dyn_cast<ELF64BEObjectFile>(Obj))
361*5ffd83dbSDimitry Andric     printProgramHeaders(ELFObj->getELFFile(), Obj->getFileName());
3620b57cec5SDimitry Andric }
3630b57cec5SDimitry Andric 
364*5ffd83dbSDimitry Andric void objdump::printELFDynamicSection(const object::ObjectFile *Obj) {
3650b57cec5SDimitry Andric   if (const auto *ELFObj = dyn_cast<ELF32LEObjectFile>(Obj))
3660b57cec5SDimitry Andric     printDynamicSection(ELFObj->getELFFile(), Obj->getFileName());
3670b57cec5SDimitry Andric   else if (const auto *ELFObj = dyn_cast<ELF32BEObjectFile>(Obj))
3680b57cec5SDimitry Andric     printDynamicSection(ELFObj->getELFFile(), Obj->getFileName());
3690b57cec5SDimitry Andric   else if (const auto *ELFObj = dyn_cast<ELF64LEObjectFile>(Obj))
3700b57cec5SDimitry Andric     printDynamicSection(ELFObj->getELFFile(), Obj->getFileName());
3710b57cec5SDimitry Andric   else if (const auto *ELFObj = dyn_cast<ELF64BEObjectFile>(Obj))
3720b57cec5SDimitry Andric     printDynamicSection(ELFObj->getELFFile(), Obj->getFileName());
3730b57cec5SDimitry Andric }
3740b57cec5SDimitry Andric 
375*5ffd83dbSDimitry Andric void objdump::printELFSymbolVersionInfo(const object::ObjectFile *Obj) {
3760b57cec5SDimitry Andric   if (const auto *ELFObj = dyn_cast<ELF32LEObjectFile>(Obj))
3770b57cec5SDimitry Andric     printSymbolVersionInfo(ELFObj->getELFFile(), Obj->getFileName());
3780b57cec5SDimitry Andric   else if (const auto *ELFObj = dyn_cast<ELF32BEObjectFile>(Obj))
3790b57cec5SDimitry Andric     printSymbolVersionInfo(ELFObj->getELFFile(), Obj->getFileName());
3800b57cec5SDimitry Andric   else if (const auto *ELFObj = dyn_cast<ELF64LEObjectFile>(Obj))
3810b57cec5SDimitry Andric     printSymbolVersionInfo(ELFObj->getELFFile(), Obj->getFileName());
3820b57cec5SDimitry Andric   else if (const auto *ELFObj = dyn_cast<ELF64BEObjectFile>(Obj))
3830b57cec5SDimitry Andric     printSymbolVersionInfo(ELFObj->getELFFile(), Obj->getFileName());
3840b57cec5SDimitry Andric }
385