xref: /freebsd/contrib/llvm-project/llvm/tools/llvm-objdump/ELFDump.cpp (revision 06c3fb2749bda94cb5201f81ffdb8fa6c3161b2e)
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 
27*06c3fb27SDimitry Andric namespace {
28*06c3fb27SDimitry Andric template <typename ELFT> class ELFDumper : public Dumper {
29*06c3fb27SDimitry Andric public:
30*06c3fb27SDimitry Andric   ELFDumper(const ELFObjectFile<ELFT> &O) : Dumper(O), Obj(O) {}
31*06c3fb27SDimitry Andric   void printPrivateHeaders(bool MachOOnlyFirst) override;
32*06c3fb27SDimitry Andric   void printDynamicRelocations() override;
33*06c3fb27SDimitry Andric 
34*06c3fb27SDimitry Andric private:
35*06c3fb27SDimitry Andric   const ELFObjectFile<ELFT> &Obj;
36*06c3fb27SDimitry Andric 
37*06c3fb27SDimitry Andric   const ELFFile<ELFT> &getELFFile() const { return Obj.getELFFile(); }
38*06c3fb27SDimitry Andric   void printDynamicSection();
39*06c3fb27SDimitry Andric   void printProgramHeaders();
40*06c3fb27SDimitry Andric   void printSymbolVersion();
41*06c3fb27SDimitry Andric };
42*06c3fb27SDimitry Andric } // namespace
43*06c3fb27SDimitry Andric 
44*06c3fb27SDimitry Andric template <class ELFT>
45*06c3fb27SDimitry Andric static std::unique_ptr<Dumper> createDumper(const ELFObjectFile<ELFT> &Obj) {
46*06c3fb27SDimitry Andric   return std::make_unique<ELFDumper<ELFT>>(Obj);
47*06c3fb27SDimitry Andric }
48*06c3fb27SDimitry Andric 
49*06c3fb27SDimitry Andric std::unique_ptr<Dumper>
50*06c3fb27SDimitry Andric objdump::createELFDumper(const object::ELFObjectFileBase &Obj) {
51*06c3fb27SDimitry Andric   if (const auto *O = dyn_cast<ELF32LEObjectFile>(&Obj))
52*06c3fb27SDimitry Andric     return createDumper(*O);
53*06c3fb27SDimitry Andric   if (const auto *O = dyn_cast<ELF32BEObjectFile>(&Obj))
54*06c3fb27SDimitry Andric     return createDumper(*O);
55*06c3fb27SDimitry Andric   if (const auto *O = dyn_cast<ELF64LEObjectFile>(&Obj))
56*06c3fb27SDimitry Andric     return createDumper(*O);
57*06c3fb27SDimitry Andric   return createDumper(cast<ELF64BEObjectFile>(Obj));
58*06c3fb27SDimitry Andric }
59*06c3fb27SDimitry Andric 
600b57cec5SDimitry Andric template <class ELFT>
61e8d8bef9SDimitry Andric static Expected<StringRef> getDynamicStrTab(const ELFFile<ELFT> &Elf) {
62e8d8bef9SDimitry Andric   auto DynamicEntriesOrError = Elf.dynamicEntries();
630b57cec5SDimitry Andric   if (!DynamicEntriesOrError)
640b57cec5SDimitry Andric     return DynamicEntriesOrError.takeError();
650b57cec5SDimitry Andric 
660b57cec5SDimitry Andric   for (const typename ELFT::Dyn &Dyn : *DynamicEntriesOrError) {
670b57cec5SDimitry Andric     if (Dyn.d_tag == ELF::DT_STRTAB) {
68e8d8bef9SDimitry Andric       auto MappedAddrOrError = Elf.toMappedAddr(Dyn.getPtr());
690b57cec5SDimitry Andric       if (!MappedAddrOrError)
700b57cec5SDimitry Andric         consumeError(MappedAddrOrError.takeError());
710b57cec5SDimitry Andric       return StringRef(reinterpret_cast<const char *>(*MappedAddrOrError));
720b57cec5SDimitry Andric     }
730b57cec5SDimitry Andric   }
740b57cec5SDimitry Andric 
750b57cec5SDimitry Andric   // If the dynamic segment is not present, we fall back on the sections.
76e8d8bef9SDimitry Andric   auto SectionsOrError = Elf.sections();
770b57cec5SDimitry Andric   if (!SectionsOrError)
780b57cec5SDimitry Andric     return SectionsOrError.takeError();
790b57cec5SDimitry Andric 
800b57cec5SDimitry Andric   for (const typename ELFT::Shdr &Sec : *SectionsOrError) {
810b57cec5SDimitry Andric     if (Sec.sh_type == ELF::SHT_DYNSYM)
82e8d8bef9SDimitry Andric       return Elf.getStringTableForSymtab(Sec);
830b57cec5SDimitry Andric   }
840b57cec5SDimitry Andric 
850b57cec5SDimitry Andric   return createError("dynamic string table not found");
860b57cec5SDimitry Andric }
870b57cec5SDimitry Andric 
880b57cec5SDimitry Andric template <class ELFT>
890b57cec5SDimitry Andric static Error getRelocationValueString(const ELFObjectFile<ELFT> *Obj,
900b57cec5SDimitry Andric                                       const RelocationRef &RelRef,
910b57cec5SDimitry Andric                                       SmallVectorImpl<char> &Result) {
92e8d8bef9SDimitry Andric   const ELFFile<ELFT> &EF = Obj->getELFFile();
930b57cec5SDimitry Andric   DataRefImpl Rel = RelRef.getRawDataRefImpl();
940b57cec5SDimitry Andric   auto SecOrErr = EF.getSection(Rel.d.a);
950b57cec5SDimitry Andric   if (!SecOrErr)
960b57cec5SDimitry Andric     return SecOrErr.takeError();
970b57cec5SDimitry Andric 
980b57cec5SDimitry Andric   int64_t Addend = 0;
990b57cec5SDimitry Andric   // If there is no Symbol associated with the relocation, we set the undef
1000b57cec5SDimitry Andric   // boolean value to 'true'. This will prevent us from calling functions that
1010b57cec5SDimitry Andric   // requires the relocation to be associated with a symbol.
1020b57cec5SDimitry Andric   //
1030b57cec5SDimitry Andric   // In SHT_REL case we would need to read the addend from section data.
1040b57cec5SDimitry Andric   // GNU objdump does not do that and we just follow for simplicity atm.
1050b57cec5SDimitry Andric   bool Undef = false;
1060b57cec5SDimitry Andric   if ((*SecOrErr)->sh_type == ELF::SHT_RELA) {
1070b57cec5SDimitry Andric     const typename ELFT::Rela *ERela = Obj->getRela(Rel);
1080b57cec5SDimitry Andric     Addend = ERela->r_addend;
1090b57cec5SDimitry Andric     Undef = ERela->getSymbol(false) == 0;
110fe6060f1SDimitry Andric   } else if ((*SecOrErr)->sh_type == ELF::SHT_REL) {
111fe6060f1SDimitry Andric     const typename ELFT::Rel *ERel = Obj->getRel(Rel);
112fe6060f1SDimitry Andric     Undef = ERel->getSymbol(false) == 0;
113fe6060f1SDimitry Andric   } else {
1140b57cec5SDimitry Andric     return make_error<BinaryError>();
1150b57cec5SDimitry Andric   }
1160b57cec5SDimitry Andric 
1170b57cec5SDimitry Andric   // Default scheme is to print Target, as well as "+ <addend>" for nonzero
1180b57cec5SDimitry Andric   // addend. Should be acceptable for all normal purposes.
1190b57cec5SDimitry Andric   std::string FmtBuf;
1200b57cec5SDimitry Andric   raw_string_ostream Fmt(FmtBuf);
1210b57cec5SDimitry Andric 
1220b57cec5SDimitry Andric   if (!Undef) {
1230b57cec5SDimitry Andric     symbol_iterator SI = RelRef.getSymbol();
124e8d8bef9SDimitry Andric     Expected<const typename ELFT::Sym *> SymOrErr =
125e8d8bef9SDimitry Andric         Obj->getSymbol(SI->getRawDataRefImpl());
126e8d8bef9SDimitry Andric     // TODO: test this error.
127e8d8bef9SDimitry Andric     if (!SymOrErr)
128e8d8bef9SDimitry Andric       return SymOrErr.takeError();
129e8d8bef9SDimitry Andric 
130e8d8bef9SDimitry Andric     if ((*SymOrErr)->getType() == ELF::STT_SECTION) {
1310b57cec5SDimitry Andric       Expected<section_iterator> SymSI = SI->getSection();
1320b57cec5SDimitry Andric       if (!SymSI)
1330b57cec5SDimitry Andric         return SymSI.takeError();
1340b57cec5SDimitry Andric       const typename ELFT::Shdr *SymSec =
1350b57cec5SDimitry Andric           Obj->getSection((*SymSI)->getRawDataRefImpl());
136e8d8bef9SDimitry Andric       auto SecName = EF.getSectionName(*SymSec);
1370b57cec5SDimitry Andric       if (!SecName)
1380b57cec5SDimitry Andric         return SecName.takeError();
1390b57cec5SDimitry Andric       Fmt << *SecName;
1400b57cec5SDimitry Andric     } else {
1410b57cec5SDimitry Andric       Expected<StringRef> SymName = SI->getName();
1420b57cec5SDimitry Andric       if (!SymName)
1430b57cec5SDimitry Andric         return SymName.takeError();
144*06c3fb27SDimitry Andric       Fmt << (Demangle ? demangle(*SymName) : *SymName);
1450b57cec5SDimitry Andric     }
1460b57cec5SDimitry Andric   } else {
1470b57cec5SDimitry Andric     Fmt << "*ABS*";
1480b57cec5SDimitry Andric   }
149480093f4SDimitry Andric   if (Addend != 0) {
150480093f4SDimitry Andric       Fmt << (Addend < 0
151480093f4SDimitry Andric           ? "-"
152480093f4SDimitry Andric           : "+") << format("0x%" PRIx64,
153480093f4SDimitry Andric                           (Addend < 0 ? -(uint64_t)Addend : (uint64_t)Addend));
154480093f4SDimitry Andric   }
1550b57cec5SDimitry Andric   Fmt.flush();
1560b57cec5SDimitry Andric   Result.append(FmtBuf.begin(), FmtBuf.end());
1570b57cec5SDimitry Andric   return Error::success();
1580b57cec5SDimitry Andric }
1590b57cec5SDimitry Andric 
1605ffd83dbSDimitry Andric Error objdump::getELFRelocationValueString(const ELFObjectFileBase *Obj,
1610b57cec5SDimitry Andric                                            const RelocationRef &Rel,
1620b57cec5SDimitry Andric                                            SmallVectorImpl<char> &Result) {
1630b57cec5SDimitry Andric   if (auto *ELF32LE = dyn_cast<ELF32LEObjectFile>(Obj))
1640b57cec5SDimitry Andric     return getRelocationValueString(ELF32LE, Rel, Result);
1650b57cec5SDimitry Andric   if (auto *ELF64LE = dyn_cast<ELF64LEObjectFile>(Obj))
1660b57cec5SDimitry Andric     return getRelocationValueString(ELF64LE, Rel, Result);
1670b57cec5SDimitry Andric   if (auto *ELF32BE = dyn_cast<ELF32BEObjectFile>(Obj))
1680b57cec5SDimitry Andric     return getRelocationValueString(ELF32BE, Rel, Result);
1690b57cec5SDimitry Andric   auto *ELF64BE = cast<ELF64BEObjectFile>(Obj);
1700b57cec5SDimitry Andric   return getRelocationValueString(ELF64BE, Rel, Result);
1710b57cec5SDimitry Andric }
1720b57cec5SDimitry Andric 
1730b57cec5SDimitry Andric template <class ELFT>
174e8d8bef9SDimitry Andric static uint64_t getSectionLMA(const ELFFile<ELFT> &Obj,
1750b57cec5SDimitry Andric                               const object::ELFSectionRef &Sec) {
176e8d8bef9SDimitry Andric   auto PhdrRangeOrErr = Obj.program_headers();
1770b57cec5SDimitry Andric   if (!PhdrRangeOrErr)
178349cc55cSDimitry Andric     report_fatal_error(Twine(toString(PhdrRangeOrErr.takeError())));
1790b57cec5SDimitry Andric 
1800b57cec5SDimitry Andric   // Search for a PT_LOAD segment containing the requested section. Use this
1810b57cec5SDimitry Andric   // segment's p_addr to calculate the section's LMA.
1820b57cec5SDimitry Andric   for (const typename ELFT::Phdr &Phdr : *PhdrRangeOrErr)
1830b57cec5SDimitry Andric     if ((Phdr.p_type == ELF::PT_LOAD) && (Phdr.p_vaddr <= Sec.getAddress()) &&
1840b57cec5SDimitry Andric         (Phdr.p_vaddr + Phdr.p_memsz > Sec.getAddress()))
1850b57cec5SDimitry Andric       return Sec.getAddress() - Phdr.p_vaddr + Phdr.p_paddr;
1860b57cec5SDimitry Andric 
1870b57cec5SDimitry Andric   // Return section's VMA if it isn't in a PT_LOAD segment.
1880b57cec5SDimitry Andric   return Sec.getAddress();
1890b57cec5SDimitry Andric }
1900b57cec5SDimitry Andric 
1915ffd83dbSDimitry Andric uint64_t objdump::getELFSectionLMA(const object::ELFSectionRef &Sec) {
1920b57cec5SDimitry Andric   if (const auto *ELFObj = dyn_cast<ELF32LEObjectFile>(Sec.getObject()))
1930b57cec5SDimitry Andric     return getSectionLMA(ELFObj->getELFFile(), Sec);
1940b57cec5SDimitry Andric   else if (const auto *ELFObj = dyn_cast<ELF32BEObjectFile>(Sec.getObject()))
1950b57cec5SDimitry Andric     return getSectionLMA(ELFObj->getELFFile(), Sec);
1960b57cec5SDimitry Andric   else if (const auto *ELFObj = dyn_cast<ELF64LEObjectFile>(Sec.getObject()))
1970b57cec5SDimitry Andric     return getSectionLMA(ELFObj->getELFFile(), Sec);
1980b57cec5SDimitry Andric   const auto *ELFObj = cast<ELF64BEObjectFile>(Sec.getObject());
1990b57cec5SDimitry Andric   return getSectionLMA(ELFObj->getELFFile(), Sec);
2000b57cec5SDimitry Andric }
2010b57cec5SDimitry Andric 
202*06c3fb27SDimitry Andric template <class ELFT> void ELFDumper<ELFT>::printDynamicSection() {
203*06c3fb27SDimitry Andric   const ELFFile<ELFT> &Elf = getELFFile();
2043a9a9c0cSDimitry Andric   auto DynamicEntriesOrErr = Elf.dynamicEntries();
2053a9a9c0cSDimitry Andric   if (!DynamicEntriesOrErr) {
206*06c3fb27SDimitry Andric     reportWarning(toString(DynamicEntriesOrErr.takeError()), Obj.getFileName());
2073a9a9c0cSDimitry Andric     return;
2083a9a9c0cSDimitry Andric   }
2093a9a9c0cSDimitry Andric   ArrayRef<typename ELFT::Dyn> DynamicEntries = *DynamicEntriesOrErr;
2105ffd83dbSDimitry Andric 
2115ffd83dbSDimitry Andric   // Find the maximum tag name length to format the value column properly.
2125ffd83dbSDimitry Andric   size_t MaxLen = 0;
2135ffd83dbSDimitry Andric   for (const typename ELFT::Dyn &Dyn : DynamicEntries)
214e8d8bef9SDimitry Andric     MaxLen = std::max(MaxLen, Elf.getDynamicTagAsString(Dyn.d_tag).size());
2155ffd83dbSDimitry Andric   std::string TagFmt = "  %-" + std::to_string(MaxLen) + "s ";
2165ffd83dbSDimitry Andric 
217fe6060f1SDimitry Andric   outs() << "\nDynamic Section:\n";
2180b57cec5SDimitry Andric   for (const typename ELFT::Dyn &Dyn : DynamicEntries) {
2190b57cec5SDimitry Andric     if (Dyn.d_tag == ELF::DT_NULL)
2200b57cec5SDimitry Andric       continue;
2210b57cec5SDimitry Andric 
222e8d8bef9SDimitry Andric     std::string Str = Elf.getDynamicTagAsString(Dyn.d_tag);
2235ffd83dbSDimitry Andric     outs() << format(TagFmt.c_str(), Str.c_str());
2240b57cec5SDimitry Andric 
2250b57cec5SDimitry Andric     const char *Fmt =
2260b57cec5SDimitry Andric         ELFT::Is64Bits ? "0x%016" PRIx64 "\n" : "0x%08" PRIx64 "\n";
2270b57cec5SDimitry Andric     if (Dyn.d_tag == ELF::DT_NEEDED || Dyn.d_tag == ELF::DT_RPATH ||
2280b57cec5SDimitry Andric         Dyn.d_tag == ELF::DT_RUNPATH || Dyn.d_tag == ELF::DT_SONAME ||
2290b57cec5SDimitry Andric         Dyn.d_tag == ELF::DT_AUXILIARY || Dyn.d_tag == ELF::DT_FILTER) {
2300b57cec5SDimitry Andric       Expected<StringRef> StrTabOrErr = getDynamicStrTab(Elf);
2310b57cec5SDimitry Andric       if (StrTabOrErr) {
2320b57cec5SDimitry Andric         const char *Data = StrTabOrErr.get().data();
2330b57cec5SDimitry Andric         outs() << (Data + Dyn.d_un.d_val) << "\n";
2340b57cec5SDimitry Andric         continue;
2350b57cec5SDimitry Andric       }
236*06c3fb27SDimitry Andric       reportWarning(toString(StrTabOrErr.takeError()), Obj.getFileName());
2370b57cec5SDimitry Andric       consumeError(StrTabOrErr.takeError());
2380b57cec5SDimitry Andric     }
2390b57cec5SDimitry Andric     outs() << format(Fmt, (uint64_t)Dyn.d_un.d_val);
2400b57cec5SDimitry Andric   }
2410b57cec5SDimitry Andric }
2420b57cec5SDimitry Andric 
243*06c3fb27SDimitry Andric template <class ELFT> void ELFDumper<ELFT>::printProgramHeaders() {
244fe6060f1SDimitry Andric   outs() << "\nProgram Header:\n";
245*06c3fb27SDimitry Andric   auto ProgramHeaderOrError = getELFFile().program_headers();
2465ffd83dbSDimitry Andric   if (!ProgramHeaderOrError) {
2475ffd83dbSDimitry Andric     reportWarning("unable to read program headers: " +
2485ffd83dbSDimitry Andric                       toString(ProgramHeaderOrError.takeError()),
249*06c3fb27SDimitry Andric                   Obj.getFileName());
2505ffd83dbSDimitry Andric     return;
2515ffd83dbSDimitry Andric   }
2525ffd83dbSDimitry Andric 
2530b57cec5SDimitry Andric   for (const typename ELFT::Phdr &Phdr : *ProgramHeaderOrError) {
2540b57cec5SDimitry Andric     switch (Phdr.p_type) {
2550b57cec5SDimitry Andric     case ELF::PT_DYNAMIC:
2560b57cec5SDimitry Andric       outs() << " DYNAMIC ";
2570b57cec5SDimitry Andric       break;
2580b57cec5SDimitry Andric     case ELF::PT_GNU_EH_FRAME:
2590b57cec5SDimitry Andric       outs() << "EH_FRAME ";
2600b57cec5SDimitry Andric       break;
2610b57cec5SDimitry Andric     case ELF::PT_GNU_RELRO:
2620b57cec5SDimitry Andric       outs() << "   RELRO ";
2630b57cec5SDimitry Andric       break;
264480093f4SDimitry Andric     case ELF::PT_GNU_PROPERTY:
265480093f4SDimitry Andric       outs() << "   PROPERTY ";
266480093f4SDimitry Andric       break;
2670b57cec5SDimitry Andric     case ELF::PT_GNU_STACK:
2680b57cec5SDimitry Andric       outs() << "   STACK ";
2690b57cec5SDimitry Andric       break;
2700b57cec5SDimitry Andric     case ELF::PT_INTERP:
2710b57cec5SDimitry Andric       outs() << "  INTERP ";
2720b57cec5SDimitry Andric       break;
2730b57cec5SDimitry Andric     case ELF::PT_LOAD:
2740b57cec5SDimitry Andric       outs() << "    LOAD ";
2750b57cec5SDimitry Andric       break;
2760b57cec5SDimitry Andric     case ELF::PT_NOTE:
2770b57cec5SDimitry Andric       outs() << "    NOTE ";
2780b57cec5SDimitry Andric       break;
2790b57cec5SDimitry Andric     case ELF::PT_OPENBSD_BOOTDATA:
2800b57cec5SDimitry Andric       outs() << "OPENBSD_BOOTDATA ";
2810b57cec5SDimitry Andric       break;
282bdd1243dSDimitry Andric     case ELF::PT_OPENBSD_MUTABLE:
283bdd1243dSDimitry Andric       outs() << "OPENBSD_MUTABLE ";
284bdd1243dSDimitry Andric       break;
2850b57cec5SDimitry Andric     case ELF::PT_OPENBSD_RANDOMIZE:
2860b57cec5SDimitry Andric       outs() << "OPENBSD_RANDOMIZE ";
2870b57cec5SDimitry Andric       break;
2880b57cec5SDimitry Andric     case ELF::PT_OPENBSD_WXNEEDED:
2890b57cec5SDimitry Andric       outs() << "OPENBSD_WXNEEDED ";
2900b57cec5SDimitry Andric       break;
2910b57cec5SDimitry Andric     case ELF::PT_PHDR:
2920b57cec5SDimitry Andric       outs() << "    PHDR ";
2930b57cec5SDimitry Andric       break;
2940b57cec5SDimitry Andric     case ELF::PT_TLS:
2950b57cec5SDimitry Andric       outs() << "    TLS ";
2960b57cec5SDimitry Andric       break;
2970b57cec5SDimitry Andric     default:
2980b57cec5SDimitry Andric       outs() << " UNKNOWN ";
2990b57cec5SDimitry Andric     }
3000b57cec5SDimitry Andric 
3010b57cec5SDimitry Andric     const char *Fmt = ELFT::Is64Bits ? "0x%016" PRIx64 " " : "0x%08" PRIx64 " ";
3020b57cec5SDimitry Andric 
3030b57cec5SDimitry Andric     outs() << "off    " << format(Fmt, (uint64_t)Phdr.p_offset) << "vaddr "
3040b57cec5SDimitry Andric            << format(Fmt, (uint64_t)Phdr.p_vaddr) << "paddr "
3050b57cec5SDimitry Andric            << format(Fmt, (uint64_t)Phdr.p_paddr)
306*06c3fb27SDimitry Andric            << format("align 2**%u\n", llvm::countr_zero<uint64_t>(Phdr.p_align))
3070b57cec5SDimitry Andric            << "         filesz " << format(Fmt, (uint64_t)Phdr.p_filesz)
3080b57cec5SDimitry Andric            << "memsz " << format(Fmt, (uint64_t)Phdr.p_memsz) << "flags "
3090b57cec5SDimitry Andric            << ((Phdr.p_flags & ELF::PF_R) ? "r" : "-")
3100b57cec5SDimitry Andric            << ((Phdr.p_flags & ELF::PF_W) ? "w" : "-")
3110b57cec5SDimitry Andric            << ((Phdr.p_flags & ELF::PF_X) ? "x" : "-") << "\n";
3120b57cec5SDimitry Andric   }
3130b57cec5SDimitry Andric }
3140b57cec5SDimitry Andric 
315*06c3fb27SDimitry Andric template <typename ELFT> void ELFDumper<ELFT>::printDynamicRelocations() {
316*06c3fb27SDimitry Andric   if (!any_of(Obj.sections(), [](const ELFSectionRef Sec) {
317*06c3fb27SDimitry Andric         return Sec.getType() == ELF::SHT_DYNAMIC;
318*06c3fb27SDimitry Andric       })) {
319*06c3fb27SDimitry Andric     reportError(Obj.getFileName(), "not a dynamic object");
320*06c3fb27SDimitry Andric     return;
321*06c3fb27SDimitry Andric   }
322*06c3fb27SDimitry Andric 
323*06c3fb27SDimitry Andric   std::vector<SectionRef> DynRelSec =
324*06c3fb27SDimitry Andric       cast<ObjectFile>(Obj).dynamic_relocation_sections();
325*06c3fb27SDimitry Andric   if (DynRelSec.empty())
326*06c3fb27SDimitry Andric     return;
327*06c3fb27SDimitry Andric 
328*06c3fb27SDimitry Andric   outs() << "\nDYNAMIC RELOCATION RECORDS\n";
329*06c3fb27SDimitry Andric   const uint32_t OffsetPadding = (Obj.getBytesInAddress() > 4 ? 16 : 8);
330*06c3fb27SDimitry Andric   const uint32_t TypePadding = 24;
331*06c3fb27SDimitry Andric   outs() << left_justify("OFFSET", OffsetPadding) << ' '
332*06c3fb27SDimitry Andric          << left_justify("TYPE", TypePadding) << " VALUE\n";
333*06c3fb27SDimitry Andric 
334*06c3fb27SDimitry Andric   StringRef Fmt = Obj.getBytesInAddress() > 4 ? "%016" PRIx64 : "%08" PRIx64;
335*06c3fb27SDimitry Andric   for (const SectionRef &Section : DynRelSec)
336*06c3fb27SDimitry Andric     for (const RelocationRef &Reloc : Section.relocations()) {
337*06c3fb27SDimitry Andric       uint64_t Address = Reloc.getOffset();
338*06c3fb27SDimitry Andric       SmallString<32> RelocName;
339*06c3fb27SDimitry Andric       SmallString<32> ValueStr;
340*06c3fb27SDimitry Andric       Reloc.getTypeName(RelocName);
341*06c3fb27SDimitry Andric       if (Error E = getELFRelocationValueString(&Obj, Reloc, ValueStr))
342*06c3fb27SDimitry Andric         reportError(std::move(E), Obj.getFileName());
343*06c3fb27SDimitry Andric       outs() << format(Fmt.data(), Address) << ' '
344*06c3fb27SDimitry Andric              << left_justify(RelocName, TypePadding) << ' ' << ValueStr << '\n';
345*06c3fb27SDimitry Andric     }
346*06c3fb27SDimitry Andric }
347*06c3fb27SDimitry Andric 
3480b57cec5SDimitry Andric template <class ELFT>
3496246ae0bSDimitry Andric static void printSymbolVersionDependency(StringRef FileName,
3506246ae0bSDimitry Andric                                          const ELFFile<ELFT> &Obj,
3516246ae0bSDimitry Andric                                          const typename ELFT::Shdr &Sec) {
352fe6060f1SDimitry Andric   outs() << "\nVersion References:\n";
3530b57cec5SDimitry Andric 
3546246ae0bSDimitry Andric   auto WarningHandler = [&](const Twine &Msg) {
3556246ae0bSDimitry Andric     reportWarning(Msg, FileName);
3566246ae0bSDimitry Andric     return Error::success();
3576246ae0bSDimitry Andric   };
3586246ae0bSDimitry Andric   Expected<std::vector<VerNeed>> V =
3596246ae0bSDimitry Andric       Obj.getVersionDependencies(Sec, WarningHandler);
3606246ae0bSDimitry Andric   if (!V) {
3616246ae0bSDimitry Andric     reportWarning(toString(V.takeError()), FileName);
3626246ae0bSDimitry Andric     return;
3630b57cec5SDimitry Andric   }
3646246ae0bSDimitry Andric 
3656246ae0bSDimitry Andric   raw_fd_ostream &OS = outs();
3666246ae0bSDimitry Andric   for (const VerNeed &VN : *V) {
3676246ae0bSDimitry Andric     OS << "  required from " << VN.File << ":\n";
3686246ae0bSDimitry Andric     for (const VernAux &Aux : VN.AuxV)
3696246ae0bSDimitry Andric       OS << format("    0x%08x 0x%02x %02u %s\n", Aux.Hash, Aux.Flags,
3706246ae0bSDimitry Andric                    Aux.Other, Aux.Name.c_str());
3710b57cec5SDimitry Andric   }
3720b57cec5SDimitry Andric }
3730b57cec5SDimitry Andric 
3740b57cec5SDimitry Andric template <class ELFT>
3755ffd83dbSDimitry Andric static void printSymbolVersionDefinition(const typename ELFT::Shdr &Shdr,
3760b57cec5SDimitry Andric                                          ArrayRef<uint8_t> Contents,
3770b57cec5SDimitry Andric                                          StringRef StrTab) {
378fe6060f1SDimitry Andric   outs() << "\nVersion definitions:\n";
3790b57cec5SDimitry Andric 
3800b57cec5SDimitry Andric   const uint8_t *Buf = Contents.data();
3810b57cec5SDimitry Andric   uint32_t VerdefIndex = 1;
3820b57cec5SDimitry Andric   // sh_info contains the number of entries in the SHT_GNU_verdef section. To
3830b57cec5SDimitry Andric   // make the index column have consistent width, we should insert blank spaces
3840b57cec5SDimitry Andric   // according to sh_info.
3850b57cec5SDimitry Andric   uint16_t VerdefIndexWidth = std::to_string(Shdr.sh_info).size();
3860b57cec5SDimitry Andric   while (Buf) {
3870b57cec5SDimitry Andric     auto *Verdef = reinterpret_cast<const typename ELFT::Verdef *>(Buf);
3880b57cec5SDimitry Andric     outs() << format_decimal(VerdefIndex++, VerdefIndexWidth) << " "
3890b57cec5SDimitry Andric            << format("0x%02" PRIx16 " ", (uint16_t)Verdef->vd_flags)
3900b57cec5SDimitry Andric            << format("0x%08" PRIx32 " ", (uint32_t)Verdef->vd_hash);
3910b57cec5SDimitry Andric 
3920b57cec5SDimitry Andric     const uint8_t *BufAux = Buf + Verdef->vd_aux;
3930b57cec5SDimitry Andric     uint16_t VerdauxIndex = 0;
3940b57cec5SDimitry Andric     while (BufAux) {
3950b57cec5SDimitry Andric       auto *Verdaux = reinterpret_cast<const typename ELFT::Verdaux *>(BufAux);
3960b57cec5SDimitry Andric       if (VerdauxIndex)
3970b57cec5SDimitry Andric         outs() << std::string(VerdefIndexWidth + 17, ' ');
3980b57cec5SDimitry Andric       outs() << StringRef(StrTab.drop_front(Verdaux->vda_name).data()) << '\n';
3990b57cec5SDimitry Andric       BufAux = Verdaux->vda_next ? BufAux + Verdaux->vda_next : nullptr;
4000b57cec5SDimitry Andric       ++VerdauxIndex;
4010b57cec5SDimitry Andric     }
4020b57cec5SDimitry Andric     Buf = Verdef->vd_next ? Buf + Verdef->vd_next : nullptr;
4030b57cec5SDimitry Andric   }
4040b57cec5SDimitry Andric }
4050b57cec5SDimitry Andric 
406*06c3fb27SDimitry Andric template <class ELFT> void ELFDumper<ELFT>::printSymbolVersion() {
407*06c3fb27SDimitry Andric   const ELFFile<ELFT> &Elf = getELFFile();
408*06c3fb27SDimitry Andric   StringRef FileName = Obj.getFileName();
4090b57cec5SDimitry Andric   ArrayRef<typename ELFT::Shdr> Sections =
410e8d8bef9SDimitry Andric       unwrapOrError(Elf.sections(), FileName);
4110b57cec5SDimitry Andric   for (const typename ELFT::Shdr &Shdr : Sections) {
4120b57cec5SDimitry Andric     if (Shdr.sh_type != ELF::SHT_GNU_verneed &&
4130b57cec5SDimitry Andric         Shdr.sh_type != ELF::SHT_GNU_verdef)
4140b57cec5SDimitry Andric       continue;
4150b57cec5SDimitry Andric 
4160b57cec5SDimitry Andric     ArrayRef<uint8_t> Contents =
417e8d8bef9SDimitry Andric         unwrapOrError(Elf.getSectionContents(Shdr), FileName);
4180b57cec5SDimitry Andric     const typename ELFT::Shdr *StrTabSec =
419e8d8bef9SDimitry Andric         unwrapOrError(Elf.getSection(Shdr.sh_link), FileName);
420e8d8bef9SDimitry Andric     StringRef StrTab = unwrapOrError(Elf.getStringTable(*StrTabSec), FileName);
4210b57cec5SDimitry Andric 
4220b57cec5SDimitry Andric     if (Shdr.sh_type == ELF::SHT_GNU_verneed)
4236246ae0bSDimitry Andric       printSymbolVersionDependency<ELFT>(FileName, Elf, Shdr);
4240b57cec5SDimitry Andric     else
4250b57cec5SDimitry Andric       printSymbolVersionDefinition<ELFT>(Shdr, Contents, StrTab);
4260b57cec5SDimitry Andric   }
4270b57cec5SDimitry Andric }
4280b57cec5SDimitry Andric 
429*06c3fb27SDimitry Andric template <class ELFT> void ELFDumper<ELFT>::printPrivateHeaders(bool) {
430*06c3fb27SDimitry Andric   printProgramHeaders();
431*06c3fb27SDimitry Andric   printDynamicSection();
432*06c3fb27SDimitry Andric   printSymbolVersion();
4330b57cec5SDimitry Andric }
434