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
2706c3fb27SDimitry Andric namespace {
2806c3fb27SDimitry Andric template <typename ELFT> class ELFDumper : public Dumper {
2906c3fb27SDimitry Andric public:
ELFDumper(const ELFObjectFile<ELFT> & O)3006c3fb27SDimitry Andric ELFDumper(const ELFObjectFile<ELFT> &O) : Dumper(O), Obj(O) {}
315f757f3fSDimitry Andric void printPrivateHeaders() override;
3206c3fb27SDimitry Andric void printDynamicRelocations() override;
3306c3fb27SDimitry Andric
3406c3fb27SDimitry Andric private:
3506c3fb27SDimitry Andric const ELFObjectFile<ELFT> &Obj;
3606c3fb27SDimitry Andric
getELFFile() const3706c3fb27SDimitry Andric const ELFFile<ELFT> &getELFFile() const { return Obj.getELFFile(); }
3806c3fb27SDimitry Andric void printDynamicSection();
3906c3fb27SDimitry Andric void printProgramHeaders();
4006c3fb27SDimitry Andric void printSymbolVersion();
415f757f3fSDimitry Andric void printSymbolVersionDependency(const typename ELFT::Shdr &Sec);
4206c3fb27SDimitry Andric };
4306c3fb27SDimitry Andric } // namespace
4406c3fb27SDimitry Andric
4506c3fb27SDimitry Andric template <class ELFT>
createDumper(const ELFObjectFile<ELFT> & Obj)4606c3fb27SDimitry Andric static std::unique_ptr<Dumper> createDumper(const ELFObjectFile<ELFT> &Obj) {
4706c3fb27SDimitry Andric return std::make_unique<ELFDumper<ELFT>>(Obj);
4806c3fb27SDimitry Andric }
4906c3fb27SDimitry Andric
5006c3fb27SDimitry Andric std::unique_ptr<Dumper>
createELFDumper(const object::ELFObjectFileBase & Obj)5106c3fb27SDimitry Andric objdump::createELFDumper(const object::ELFObjectFileBase &Obj) {
5206c3fb27SDimitry Andric if (const auto *O = dyn_cast<ELF32LEObjectFile>(&Obj))
5306c3fb27SDimitry Andric return createDumper(*O);
5406c3fb27SDimitry Andric if (const auto *O = dyn_cast<ELF32BEObjectFile>(&Obj))
5506c3fb27SDimitry Andric return createDumper(*O);
5606c3fb27SDimitry Andric if (const auto *O = dyn_cast<ELF64LEObjectFile>(&Obj))
5706c3fb27SDimitry Andric return createDumper(*O);
5806c3fb27SDimitry Andric return createDumper(cast<ELF64BEObjectFile>(Obj));
5906c3fb27SDimitry Andric }
6006c3fb27SDimitry Andric
610b57cec5SDimitry Andric template <class ELFT>
getDynamicStrTab(const ELFFile<ELFT> & Elf)62e8d8bef9SDimitry Andric static Expected<StringRef> getDynamicStrTab(const ELFFile<ELFT> &Elf) {
63e8d8bef9SDimitry Andric auto DynamicEntriesOrError = Elf.dynamicEntries();
640b57cec5SDimitry Andric if (!DynamicEntriesOrError)
650b57cec5SDimitry Andric return DynamicEntriesOrError.takeError();
660b57cec5SDimitry Andric
670b57cec5SDimitry Andric for (const typename ELFT::Dyn &Dyn : *DynamicEntriesOrError) {
680b57cec5SDimitry Andric if (Dyn.d_tag == ELF::DT_STRTAB) {
69e8d8bef9SDimitry Andric auto MappedAddrOrError = Elf.toMappedAddr(Dyn.getPtr());
700b57cec5SDimitry Andric if (!MappedAddrOrError)
71*0fca6ea1SDimitry Andric return MappedAddrOrError.takeError();
720b57cec5SDimitry Andric return StringRef(reinterpret_cast<const char *>(*MappedAddrOrError));
730b57cec5SDimitry Andric }
740b57cec5SDimitry Andric }
750b57cec5SDimitry Andric
760b57cec5SDimitry Andric // If the dynamic segment is not present, we fall back on the sections.
77e8d8bef9SDimitry Andric auto SectionsOrError = Elf.sections();
780b57cec5SDimitry Andric if (!SectionsOrError)
790b57cec5SDimitry Andric return SectionsOrError.takeError();
800b57cec5SDimitry Andric
810b57cec5SDimitry Andric for (const typename ELFT::Shdr &Sec : *SectionsOrError) {
820b57cec5SDimitry Andric if (Sec.sh_type == ELF::SHT_DYNSYM)
83e8d8bef9SDimitry Andric return Elf.getStringTableForSymtab(Sec);
840b57cec5SDimitry Andric }
850b57cec5SDimitry Andric
860b57cec5SDimitry Andric return createError("dynamic string table not found");
870b57cec5SDimitry Andric }
880b57cec5SDimitry Andric
890b57cec5SDimitry Andric template <class ELFT>
getRelocationValueString(const ELFObjectFile<ELFT> * Obj,const RelocationRef & RelRef,SmallVectorImpl<char> & Result)900b57cec5SDimitry Andric static Error getRelocationValueString(const ELFObjectFile<ELFT> *Obj,
910b57cec5SDimitry Andric const RelocationRef &RelRef,
920b57cec5SDimitry Andric SmallVectorImpl<char> &Result) {
93e8d8bef9SDimitry Andric const ELFFile<ELFT> &EF = Obj->getELFFile();
940b57cec5SDimitry Andric DataRefImpl Rel = RelRef.getRawDataRefImpl();
950b57cec5SDimitry Andric auto SecOrErr = EF.getSection(Rel.d.a);
960b57cec5SDimitry Andric if (!SecOrErr)
970b57cec5SDimitry Andric return SecOrErr.takeError();
980b57cec5SDimitry Andric
990b57cec5SDimitry Andric int64_t Addend = 0;
1000b57cec5SDimitry Andric // If there is no Symbol associated with the relocation, we set the undef
1010b57cec5SDimitry Andric // boolean value to 'true'. This will prevent us from calling functions that
1020b57cec5SDimitry Andric // requires the relocation to be associated with a symbol.
1030b57cec5SDimitry Andric //
1040b57cec5SDimitry Andric // In SHT_REL case we would need to read the addend from section data.
1050b57cec5SDimitry Andric // GNU objdump does not do that and we just follow for simplicity atm.
1060b57cec5SDimitry Andric bool Undef = false;
107*0fca6ea1SDimitry Andric if ((*SecOrErr)->sh_type == ELF::SHT_CREL) {
108*0fca6ea1SDimitry Andric auto ERela = Obj->getCrel(Rel);
109*0fca6ea1SDimitry Andric Addend = ERela.r_addend;
110*0fca6ea1SDimitry Andric Undef = ERela.getSymbol(false) == 0;
111*0fca6ea1SDimitry Andric } else if ((*SecOrErr)->sh_type == ELF::SHT_RELA) {
1120b57cec5SDimitry Andric const typename ELFT::Rela *ERela = Obj->getRela(Rel);
1130b57cec5SDimitry Andric Addend = ERela->r_addend;
1140b57cec5SDimitry Andric Undef = ERela->getSymbol(false) == 0;
115fe6060f1SDimitry Andric } else if ((*SecOrErr)->sh_type == ELF::SHT_REL) {
116fe6060f1SDimitry Andric const typename ELFT::Rel *ERel = Obj->getRel(Rel);
117fe6060f1SDimitry Andric Undef = ERel->getSymbol(false) == 0;
118fe6060f1SDimitry Andric } else {
1190b57cec5SDimitry Andric return make_error<BinaryError>();
1200b57cec5SDimitry Andric }
1210b57cec5SDimitry Andric
1220b57cec5SDimitry Andric // Default scheme is to print Target, as well as "+ <addend>" for nonzero
1230b57cec5SDimitry Andric // addend. Should be acceptable for all normal purposes.
1240b57cec5SDimitry Andric std::string FmtBuf;
1250b57cec5SDimitry Andric raw_string_ostream Fmt(FmtBuf);
1260b57cec5SDimitry Andric
1270b57cec5SDimitry Andric if (!Undef) {
1280b57cec5SDimitry Andric symbol_iterator SI = RelRef.getSymbol();
129e8d8bef9SDimitry Andric Expected<const typename ELFT::Sym *> SymOrErr =
130e8d8bef9SDimitry Andric Obj->getSymbol(SI->getRawDataRefImpl());
131e8d8bef9SDimitry Andric // TODO: test this error.
132e8d8bef9SDimitry Andric if (!SymOrErr)
133e8d8bef9SDimitry Andric return SymOrErr.takeError();
134e8d8bef9SDimitry Andric
135e8d8bef9SDimitry Andric if ((*SymOrErr)->getType() == ELF::STT_SECTION) {
1360b57cec5SDimitry Andric Expected<section_iterator> SymSI = SI->getSection();
1370b57cec5SDimitry Andric if (!SymSI)
1380b57cec5SDimitry Andric return SymSI.takeError();
1390b57cec5SDimitry Andric const typename ELFT::Shdr *SymSec =
1400b57cec5SDimitry Andric Obj->getSection((*SymSI)->getRawDataRefImpl());
141e8d8bef9SDimitry Andric auto SecName = EF.getSectionName(*SymSec);
1420b57cec5SDimitry Andric if (!SecName)
1430b57cec5SDimitry Andric return SecName.takeError();
1440b57cec5SDimitry Andric Fmt << *SecName;
1450b57cec5SDimitry Andric } else {
1460b57cec5SDimitry Andric Expected<StringRef> SymName = SI->getName();
1470b57cec5SDimitry Andric if (!SymName)
1480b57cec5SDimitry Andric return SymName.takeError();
14906c3fb27SDimitry Andric Fmt << (Demangle ? demangle(*SymName) : *SymName);
1500b57cec5SDimitry Andric }
1510b57cec5SDimitry Andric } else {
1520b57cec5SDimitry Andric Fmt << "*ABS*";
1530b57cec5SDimitry Andric }
154480093f4SDimitry Andric if (Addend != 0) {
155480093f4SDimitry Andric Fmt << (Addend < 0
156480093f4SDimitry Andric ? "-"
157480093f4SDimitry Andric : "+") << format("0x%" PRIx64,
158480093f4SDimitry Andric (Addend < 0 ? -(uint64_t)Addend : (uint64_t)Addend));
159480093f4SDimitry Andric }
1600b57cec5SDimitry Andric Fmt.flush();
1610b57cec5SDimitry Andric Result.append(FmtBuf.begin(), FmtBuf.end());
1620b57cec5SDimitry Andric return Error::success();
1630b57cec5SDimitry Andric }
1640b57cec5SDimitry Andric
getELFRelocationValueString(const ELFObjectFileBase * Obj,const RelocationRef & Rel,SmallVectorImpl<char> & Result)1655ffd83dbSDimitry Andric Error objdump::getELFRelocationValueString(const ELFObjectFileBase *Obj,
1660b57cec5SDimitry Andric const RelocationRef &Rel,
1670b57cec5SDimitry Andric SmallVectorImpl<char> &Result) {
1680b57cec5SDimitry Andric if (auto *ELF32LE = dyn_cast<ELF32LEObjectFile>(Obj))
1690b57cec5SDimitry Andric return getRelocationValueString(ELF32LE, Rel, Result);
1700b57cec5SDimitry Andric if (auto *ELF64LE = dyn_cast<ELF64LEObjectFile>(Obj))
1710b57cec5SDimitry Andric return getRelocationValueString(ELF64LE, Rel, Result);
1720b57cec5SDimitry Andric if (auto *ELF32BE = dyn_cast<ELF32BEObjectFile>(Obj))
1730b57cec5SDimitry Andric return getRelocationValueString(ELF32BE, Rel, Result);
1740b57cec5SDimitry Andric auto *ELF64BE = cast<ELF64BEObjectFile>(Obj);
1750b57cec5SDimitry Andric return getRelocationValueString(ELF64BE, Rel, Result);
1760b57cec5SDimitry Andric }
1770b57cec5SDimitry Andric
1780b57cec5SDimitry Andric template <class ELFT>
getSectionLMA(const ELFFile<ELFT> & Obj,const object::ELFSectionRef & Sec)179e8d8bef9SDimitry Andric static uint64_t getSectionLMA(const ELFFile<ELFT> &Obj,
1800b57cec5SDimitry Andric const object::ELFSectionRef &Sec) {
181e8d8bef9SDimitry Andric auto PhdrRangeOrErr = Obj.program_headers();
1820b57cec5SDimitry Andric if (!PhdrRangeOrErr)
183349cc55cSDimitry Andric report_fatal_error(Twine(toString(PhdrRangeOrErr.takeError())));
1840b57cec5SDimitry Andric
1850b57cec5SDimitry Andric // Search for a PT_LOAD segment containing the requested section. Use this
1860b57cec5SDimitry Andric // segment's p_addr to calculate the section's LMA.
1870b57cec5SDimitry Andric for (const typename ELFT::Phdr &Phdr : *PhdrRangeOrErr)
1885f757f3fSDimitry Andric if ((Phdr.p_type == ELF::PT_LOAD) &&
1895f757f3fSDimitry Andric (isSectionInSegment<ELFT>(
1905f757f3fSDimitry Andric Phdr, *cast<const ELFObjectFile<ELFT>>(Sec.getObject())
1915f757f3fSDimitry Andric ->getSection(Sec.getRawDataRefImpl()))))
1920b57cec5SDimitry Andric return Sec.getAddress() - Phdr.p_vaddr + Phdr.p_paddr;
1930b57cec5SDimitry Andric
1940b57cec5SDimitry Andric // Return section's VMA if it isn't in a PT_LOAD segment.
1950b57cec5SDimitry Andric return Sec.getAddress();
1960b57cec5SDimitry Andric }
1970b57cec5SDimitry Andric
getELFSectionLMA(const object::ELFSectionRef & Sec)1985ffd83dbSDimitry Andric uint64_t objdump::getELFSectionLMA(const object::ELFSectionRef &Sec) {
1990b57cec5SDimitry Andric if (const auto *ELFObj = dyn_cast<ELF32LEObjectFile>(Sec.getObject()))
2000b57cec5SDimitry Andric return getSectionLMA(ELFObj->getELFFile(), Sec);
2010b57cec5SDimitry Andric else if (const auto *ELFObj = dyn_cast<ELF32BEObjectFile>(Sec.getObject()))
2020b57cec5SDimitry Andric return getSectionLMA(ELFObj->getELFFile(), Sec);
2030b57cec5SDimitry Andric else if (const auto *ELFObj = dyn_cast<ELF64LEObjectFile>(Sec.getObject()))
2040b57cec5SDimitry Andric return getSectionLMA(ELFObj->getELFFile(), Sec);
2050b57cec5SDimitry Andric const auto *ELFObj = cast<ELF64BEObjectFile>(Sec.getObject());
2060b57cec5SDimitry Andric return getSectionLMA(ELFObj->getELFFile(), Sec);
2070b57cec5SDimitry Andric }
2080b57cec5SDimitry Andric
printDynamicSection()20906c3fb27SDimitry Andric template <class ELFT> void ELFDumper<ELFT>::printDynamicSection() {
21006c3fb27SDimitry Andric const ELFFile<ELFT> &Elf = getELFFile();
2113a9a9c0cSDimitry Andric auto DynamicEntriesOrErr = Elf.dynamicEntries();
2123a9a9c0cSDimitry Andric if (!DynamicEntriesOrErr) {
21306c3fb27SDimitry Andric reportWarning(toString(DynamicEntriesOrErr.takeError()), Obj.getFileName());
2143a9a9c0cSDimitry Andric return;
2153a9a9c0cSDimitry Andric }
2163a9a9c0cSDimitry Andric ArrayRef<typename ELFT::Dyn> DynamicEntries = *DynamicEntriesOrErr;
2175ffd83dbSDimitry Andric
2185ffd83dbSDimitry Andric // Find the maximum tag name length to format the value column properly.
2195ffd83dbSDimitry Andric size_t MaxLen = 0;
2205ffd83dbSDimitry Andric for (const typename ELFT::Dyn &Dyn : DynamicEntries)
221e8d8bef9SDimitry Andric MaxLen = std::max(MaxLen, Elf.getDynamicTagAsString(Dyn.d_tag).size());
2225ffd83dbSDimitry Andric std::string TagFmt = " %-" + std::to_string(MaxLen) + "s ";
2235ffd83dbSDimitry Andric
224fe6060f1SDimitry Andric outs() << "\nDynamic Section:\n";
2250b57cec5SDimitry Andric for (const typename ELFT::Dyn &Dyn : DynamicEntries) {
2260b57cec5SDimitry Andric if (Dyn.d_tag == ELF::DT_NULL)
2270b57cec5SDimitry Andric continue;
2280b57cec5SDimitry Andric
229e8d8bef9SDimitry Andric std::string Str = Elf.getDynamicTagAsString(Dyn.d_tag);
2300b57cec5SDimitry Andric
2310b57cec5SDimitry Andric const char *Fmt =
2320b57cec5SDimitry Andric ELFT::Is64Bits ? "0x%016" PRIx64 "\n" : "0x%08" PRIx64 "\n";
2330b57cec5SDimitry Andric if (Dyn.d_tag == ELF::DT_NEEDED || Dyn.d_tag == ELF::DT_RPATH ||
2340b57cec5SDimitry Andric Dyn.d_tag == ELF::DT_RUNPATH || Dyn.d_tag == ELF::DT_SONAME ||
2350b57cec5SDimitry Andric Dyn.d_tag == ELF::DT_AUXILIARY || Dyn.d_tag == ELF::DT_FILTER) {
2360b57cec5SDimitry Andric Expected<StringRef> StrTabOrErr = getDynamicStrTab(Elf);
2370b57cec5SDimitry Andric if (StrTabOrErr) {
238*0fca6ea1SDimitry Andric const char *Data = StrTabOrErr->data();
239*0fca6ea1SDimitry Andric outs() << format(TagFmt.c_str(), Str.c_str()) << Data + Dyn.getVal()
240*0fca6ea1SDimitry Andric << "\n";
2410b57cec5SDimitry Andric continue;
2420b57cec5SDimitry Andric }
24306c3fb27SDimitry Andric reportWarning(toString(StrTabOrErr.takeError()), Obj.getFileName());
2440b57cec5SDimitry Andric consumeError(StrTabOrErr.takeError());
2450b57cec5SDimitry Andric }
246*0fca6ea1SDimitry Andric outs() << format(TagFmt.c_str(), Str.c_str())
247*0fca6ea1SDimitry Andric << format(Fmt, (uint64_t)Dyn.getVal());
2480b57cec5SDimitry Andric }
2490b57cec5SDimitry Andric }
2500b57cec5SDimitry Andric
printProgramHeaders()25106c3fb27SDimitry Andric template <class ELFT> void ELFDumper<ELFT>::printProgramHeaders() {
252fe6060f1SDimitry Andric outs() << "\nProgram Header:\n";
25306c3fb27SDimitry Andric auto ProgramHeaderOrError = getELFFile().program_headers();
2545ffd83dbSDimitry Andric if (!ProgramHeaderOrError) {
2555ffd83dbSDimitry Andric reportWarning("unable to read program headers: " +
2565ffd83dbSDimitry Andric toString(ProgramHeaderOrError.takeError()),
25706c3fb27SDimitry Andric Obj.getFileName());
2585ffd83dbSDimitry Andric return;
2595ffd83dbSDimitry Andric }
2605ffd83dbSDimitry Andric
2610b57cec5SDimitry Andric for (const typename ELFT::Phdr &Phdr : *ProgramHeaderOrError) {
2620b57cec5SDimitry Andric switch (Phdr.p_type) {
2630b57cec5SDimitry Andric case ELF::PT_DYNAMIC:
2640b57cec5SDimitry Andric outs() << " DYNAMIC ";
2650b57cec5SDimitry Andric break;
2660b57cec5SDimitry Andric case ELF::PT_GNU_EH_FRAME:
2670b57cec5SDimitry Andric outs() << "EH_FRAME ";
2680b57cec5SDimitry Andric break;
2690b57cec5SDimitry Andric case ELF::PT_GNU_RELRO:
2700b57cec5SDimitry Andric outs() << " RELRO ";
2710b57cec5SDimitry Andric break;
272480093f4SDimitry Andric case ELF::PT_GNU_PROPERTY:
273480093f4SDimitry Andric outs() << " PROPERTY ";
274480093f4SDimitry Andric break;
2750b57cec5SDimitry Andric case ELF::PT_GNU_STACK:
2760b57cec5SDimitry Andric outs() << " STACK ";
2770b57cec5SDimitry Andric break;
2780b57cec5SDimitry Andric case ELF::PT_INTERP:
2790b57cec5SDimitry Andric outs() << " INTERP ";
2800b57cec5SDimitry Andric break;
2810b57cec5SDimitry Andric case ELF::PT_LOAD:
2820b57cec5SDimitry Andric outs() << " LOAD ";
2830b57cec5SDimitry Andric break;
2840b57cec5SDimitry Andric case ELF::PT_NOTE:
2850b57cec5SDimitry Andric outs() << " NOTE ";
2860b57cec5SDimitry Andric break;
2870b57cec5SDimitry Andric case ELF::PT_OPENBSD_BOOTDATA:
2880b57cec5SDimitry Andric outs() << "OPENBSD_BOOTDATA ";
2890b57cec5SDimitry Andric break;
290bdd1243dSDimitry Andric case ELF::PT_OPENBSD_MUTABLE:
291bdd1243dSDimitry Andric outs() << "OPENBSD_MUTABLE ";
292bdd1243dSDimitry Andric break;
2935f757f3fSDimitry Andric case ELF::PT_OPENBSD_NOBTCFI:
2945f757f3fSDimitry Andric outs() << "OPENBSD_NOBTCFI ";
2955f757f3fSDimitry Andric break;
2960b57cec5SDimitry Andric case ELF::PT_OPENBSD_RANDOMIZE:
2970b57cec5SDimitry Andric outs() << "OPENBSD_RANDOMIZE ";
2980b57cec5SDimitry Andric break;
29956727255SDimitry Andric case ELF::PT_OPENBSD_SYSCALLS:
30056727255SDimitry Andric outs() << "OPENBSD_SYSCALLS ";
30156727255SDimitry Andric break;
3020b57cec5SDimitry Andric case ELF::PT_OPENBSD_WXNEEDED:
3030b57cec5SDimitry Andric outs() << "OPENBSD_WXNEEDED ";
3040b57cec5SDimitry Andric break;
3050b57cec5SDimitry Andric case ELF::PT_PHDR:
3060b57cec5SDimitry Andric outs() << " PHDR ";
3070b57cec5SDimitry Andric break;
3080b57cec5SDimitry Andric case ELF::PT_TLS:
3090b57cec5SDimitry Andric outs() << " TLS ";
3100b57cec5SDimitry Andric break;
3110b57cec5SDimitry Andric default:
3120b57cec5SDimitry Andric outs() << " UNKNOWN ";
3130b57cec5SDimitry Andric }
3140b57cec5SDimitry Andric
3150b57cec5SDimitry Andric const char *Fmt = ELFT::Is64Bits ? "0x%016" PRIx64 " " : "0x%08" PRIx64 " ";
3160b57cec5SDimitry Andric
3170b57cec5SDimitry Andric outs() << "off " << format(Fmt, (uint64_t)Phdr.p_offset) << "vaddr "
3180b57cec5SDimitry Andric << format(Fmt, (uint64_t)Phdr.p_vaddr) << "paddr "
3190b57cec5SDimitry Andric << format(Fmt, (uint64_t)Phdr.p_paddr)
32006c3fb27SDimitry Andric << format("align 2**%u\n", llvm::countr_zero<uint64_t>(Phdr.p_align))
3210b57cec5SDimitry Andric << " filesz " << format(Fmt, (uint64_t)Phdr.p_filesz)
3220b57cec5SDimitry Andric << "memsz " << format(Fmt, (uint64_t)Phdr.p_memsz) << "flags "
3230b57cec5SDimitry Andric << ((Phdr.p_flags & ELF::PF_R) ? "r" : "-")
3240b57cec5SDimitry Andric << ((Phdr.p_flags & ELF::PF_W) ? "w" : "-")
3250b57cec5SDimitry Andric << ((Phdr.p_flags & ELF::PF_X) ? "x" : "-") << "\n";
3260b57cec5SDimitry Andric }
3270b57cec5SDimitry Andric }
3280b57cec5SDimitry Andric
printDynamicRelocations()32906c3fb27SDimitry Andric template <typename ELFT> void ELFDumper<ELFT>::printDynamicRelocations() {
33006c3fb27SDimitry Andric if (!any_of(Obj.sections(), [](const ELFSectionRef Sec) {
33106c3fb27SDimitry Andric return Sec.getType() == ELF::SHT_DYNAMIC;
33206c3fb27SDimitry Andric })) {
33306c3fb27SDimitry Andric reportError(Obj.getFileName(), "not a dynamic object");
33406c3fb27SDimitry Andric return;
33506c3fb27SDimitry Andric }
33606c3fb27SDimitry Andric
33706c3fb27SDimitry Andric std::vector<SectionRef> DynRelSec =
33806c3fb27SDimitry Andric cast<ObjectFile>(Obj).dynamic_relocation_sections();
33906c3fb27SDimitry Andric if (DynRelSec.empty())
34006c3fb27SDimitry Andric return;
34106c3fb27SDimitry Andric
34206c3fb27SDimitry Andric outs() << "\nDYNAMIC RELOCATION RECORDS\n";
34306c3fb27SDimitry Andric const uint32_t OffsetPadding = (Obj.getBytesInAddress() > 4 ? 16 : 8);
34406c3fb27SDimitry Andric const uint32_t TypePadding = 24;
34506c3fb27SDimitry Andric outs() << left_justify("OFFSET", OffsetPadding) << ' '
34606c3fb27SDimitry Andric << left_justify("TYPE", TypePadding) << " VALUE\n";
34706c3fb27SDimitry Andric
34806c3fb27SDimitry Andric StringRef Fmt = Obj.getBytesInAddress() > 4 ? "%016" PRIx64 : "%08" PRIx64;
34906c3fb27SDimitry Andric for (const SectionRef &Section : DynRelSec)
35006c3fb27SDimitry Andric for (const RelocationRef &Reloc : Section.relocations()) {
35106c3fb27SDimitry Andric uint64_t Address = Reloc.getOffset();
35206c3fb27SDimitry Andric SmallString<32> RelocName;
35306c3fb27SDimitry Andric SmallString<32> ValueStr;
35406c3fb27SDimitry Andric Reloc.getTypeName(RelocName);
35506c3fb27SDimitry Andric if (Error E = getELFRelocationValueString(&Obj, Reloc, ValueStr))
35606c3fb27SDimitry Andric reportError(std::move(E), Obj.getFileName());
35706c3fb27SDimitry Andric outs() << format(Fmt.data(), Address) << ' '
35806c3fb27SDimitry Andric << left_justify(RelocName, TypePadding) << ' ' << ValueStr << '\n';
35906c3fb27SDimitry Andric }
36006c3fb27SDimitry Andric }
36106c3fb27SDimitry Andric
3620b57cec5SDimitry Andric template <class ELFT>
printSymbolVersionDependency(const typename ELFT::Shdr & Sec)3635f757f3fSDimitry Andric void ELFDumper<ELFT>::printSymbolVersionDependency(
3646246ae0bSDimitry Andric const typename ELFT::Shdr &Sec) {
365fe6060f1SDimitry Andric outs() << "\nVersion References:\n";
3666246ae0bSDimitry Andric Expected<std::vector<VerNeed>> V =
3675f757f3fSDimitry Andric getELFFile().getVersionDependencies(Sec, this->WarningHandler);
3686246ae0bSDimitry Andric if (!V) {
3695f757f3fSDimitry Andric reportWarning(toString(V.takeError()), Obj.getFileName());
3706246ae0bSDimitry Andric return;
3710b57cec5SDimitry Andric }
3726246ae0bSDimitry Andric
3736246ae0bSDimitry Andric raw_fd_ostream &OS = outs();
3746246ae0bSDimitry Andric for (const VerNeed &VN : *V) {
3756246ae0bSDimitry Andric OS << " required from " << VN.File << ":\n";
3766246ae0bSDimitry Andric for (const VernAux &Aux : VN.AuxV)
3776246ae0bSDimitry Andric OS << format(" 0x%08x 0x%02x %02u %s\n", Aux.Hash, Aux.Flags,
3786246ae0bSDimitry Andric Aux.Other, Aux.Name.c_str());
3790b57cec5SDimitry Andric }
3800b57cec5SDimitry Andric }
3810b57cec5SDimitry Andric
3820b57cec5SDimitry Andric template <class ELFT>
printSymbolVersionDefinition(const typename ELFT::Shdr & Shdr,ArrayRef<uint8_t> Contents,StringRef StrTab)3835ffd83dbSDimitry Andric static void printSymbolVersionDefinition(const typename ELFT::Shdr &Shdr,
3840b57cec5SDimitry Andric ArrayRef<uint8_t> Contents,
3850b57cec5SDimitry Andric StringRef StrTab) {
386fe6060f1SDimitry Andric outs() << "\nVersion definitions:\n";
3870b57cec5SDimitry Andric
3880b57cec5SDimitry Andric const uint8_t *Buf = Contents.data();
3890b57cec5SDimitry Andric uint32_t VerdefIndex = 1;
3900b57cec5SDimitry Andric // sh_info contains the number of entries in the SHT_GNU_verdef section. To
3910b57cec5SDimitry Andric // make the index column have consistent width, we should insert blank spaces
3920b57cec5SDimitry Andric // according to sh_info.
3930b57cec5SDimitry Andric uint16_t VerdefIndexWidth = std::to_string(Shdr.sh_info).size();
3940b57cec5SDimitry Andric while (Buf) {
3950b57cec5SDimitry Andric auto *Verdef = reinterpret_cast<const typename ELFT::Verdef *>(Buf);
3960b57cec5SDimitry Andric outs() << format_decimal(VerdefIndex++, VerdefIndexWidth) << " "
3970b57cec5SDimitry Andric << format("0x%02" PRIx16 " ", (uint16_t)Verdef->vd_flags)
3980b57cec5SDimitry Andric << format("0x%08" PRIx32 " ", (uint32_t)Verdef->vd_hash);
3990b57cec5SDimitry Andric
4000b57cec5SDimitry Andric const uint8_t *BufAux = Buf + Verdef->vd_aux;
4010b57cec5SDimitry Andric uint16_t VerdauxIndex = 0;
4020b57cec5SDimitry Andric while (BufAux) {
4030b57cec5SDimitry Andric auto *Verdaux = reinterpret_cast<const typename ELFT::Verdaux *>(BufAux);
4040b57cec5SDimitry Andric if (VerdauxIndex)
4050b57cec5SDimitry Andric outs() << std::string(VerdefIndexWidth + 17, ' ');
4060b57cec5SDimitry Andric outs() << StringRef(StrTab.drop_front(Verdaux->vda_name).data()) << '\n';
4070b57cec5SDimitry Andric BufAux = Verdaux->vda_next ? BufAux + Verdaux->vda_next : nullptr;
4080b57cec5SDimitry Andric ++VerdauxIndex;
4090b57cec5SDimitry Andric }
4100b57cec5SDimitry Andric Buf = Verdef->vd_next ? Buf + Verdef->vd_next : nullptr;
4110b57cec5SDimitry Andric }
4120b57cec5SDimitry Andric }
4130b57cec5SDimitry Andric
printSymbolVersion()41406c3fb27SDimitry Andric template <class ELFT> void ELFDumper<ELFT>::printSymbolVersion() {
41506c3fb27SDimitry Andric const ELFFile<ELFT> &Elf = getELFFile();
41606c3fb27SDimitry Andric StringRef FileName = Obj.getFileName();
4170b57cec5SDimitry Andric ArrayRef<typename ELFT::Shdr> Sections =
418e8d8bef9SDimitry Andric unwrapOrError(Elf.sections(), FileName);
4190b57cec5SDimitry Andric for (const typename ELFT::Shdr &Shdr : Sections) {
4200b57cec5SDimitry Andric if (Shdr.sh_type != ELF::SHT_GNU_verneed &&
4210b57cec5SDimitry Andric Shdr.sh_type != ELF::SHT_GNU_verdef)
4220b57cec5SDimitry Andric continue;
4230b57cec5SDimitry Andric
4240b57cec5SDimitry Andric ArrayRef<uint8_t> Contents =
425e8d8bef9SDimitry Andric unwrapOrError(Elf.getSectionContents(Shdr), FileName);
4260b57cec5SDimitry Andric const typename ELFT::Shdr *StrTabSec =
427e8d8bef9SDimitry Andric unwrapOrError(Elf.getSection(Shdr.sh_link), FileName);
428e8d8bef9SDimitry Andric StringRef StrTab = unwrapOrError(Elf.getStringTable(*StrTabSec), FileName);
4290b57cec5SDimitry Andric
4300b57cec5SDimitry Andric if (Shdr.sh_type == ELF::SHT_GNU_verneed)
4315f757f3fSDimitry Andric printSymbolVersionDependency(Shdr);
4320b57cec5SDimitry Andric else
4330b57cec5SDimitry Andric printSymbolVersionDefinition<ELFT>(Shdr, Contents, StrTab);
4340b57cec5SDimitry Andric }
4350b57cec5SDimitry Andric }
4360b57cec5SDimitry Andric
printPrivateHeaders()4375f757f3fSDimitry Andric template <class ELFT> void ELFDumper<ELFT>::printPrivateHeaders() {
43806c3fb27SDimitry Andric printProgramHeaders();
43906c3fb27SDimitry Andric printDynamicSection();
44006c3fb27SDimitry Andric printSymbolVersion();
4410b57cec5SDimitry Andric }
442