17330f729Sjoerg //===-- ObjDumper.cpp - Base dumper class -----------------------*- C++ -*-===//
27330f729Sjoerg //
37330f729Sjoerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
47330f729Sjoerg // See https://llvm.org/LICENSE.txt for license information.
57330f729Sjoerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
67330f729Sjoerg //
77330f729Sjoerg //===----------------------------------------------------------------------===//
87330f729Sjoerg ///
97330f729Sjoerg /// \file
107330f729Sjoerg /// This file implements ObjDumper.
117330f729Sjoerg ///
127330f729Sjoerg //===----------------------------------------------------------------------===//
137330f729Sjoerg
147330f729Sjoerg #include "ObjDumper.h"
157330f729Sjoerg #include "llvm-readobj.h"
167330f729Sjoerg #include "llvm/Object/ObjectFile.h"
177330f729Sjoerg #include "llvm/Support/Error.h"
187330f729Sjoerg #include "llvm/Support/FormatVariadic.h"
197330f729Sjoerg #include "llvm/Support/ScopedPrinter.h"
207330f729Sjoerg #include "llvm/Support/raw_ostream.h"
217330f729Sjoerg #include <map>
227330f729Sjoerg
237330f729Sjoerg namespace llvm {
247330f729Sjoerg
createError(const Twine & Msg)257330f729Sjoerg static inline Error createError(const Twine &Msg) {
267330f729Sjoerg return createStringError(object::object_error::parse_failed, Msg);
277330f729Sjoerg }
287330f729Sjoerg
ObjDumper(ScopedPrinter & Writer,StringRef ObjName)29*82d56013Sjoerg ObjDumper::ObjDumper(ScopedPrinter &Writer, StringRef ObjName) : W(Writer) {
30*82d56013Sjoerg // Dumper reports all non-critical errors as warnings.
31*82d56013Sjoerg // It does not print the same warning more than once.
32*82d56013Sjoerg WarningHandler = [=](const Twine &Msg) {
33*82d56013Sjoerg if (Warnings.insert(Msg.str()).second)
34*82d56013Sjoerg reportWarning(createError(Msg), ObjName);
35*82d56013Sjoerg return Error::success();
36*82d56013Sjoerg };
37*82d56013Sjoerg }
387330f729Sjoerg
~ObjDumper()39*82d56013Sjoerg ObjDumper::~ObjDumper() {}
40*82d56013Sjoerg
reportUniqueWarning(Error Err) const41*82d56013Sjoerg void ObjDumper::reportUniqueWarning(Error Err) const {
42*82d56013Sjoerg reportUniqueWarning(toString(std::move(Err)));
43*82d56013Sjoerg }
44*82d56013Sjoerg
reportUniqueWarning(const Twine & Msg) const45*82d56013Sjoerg void ObjDumper::reportUniqueWarning(const Twine &Msg) const {
46*82d56013Sjoerg cantFail(WarningHandler(Msg),
47*82d56013Sjoerg "WarningHandler should always return ErrorSuccess");
487330f729Sjoerg }
497330f729Sjoerg
printAsPrintable(raw_ostream & W,const uint8_t * Start,size_t Len)507330f729Sjoerg static void printAsPrintable(raw_ostream &W, const uint8_t *Start, size_t Len) {
517330f729Sjoerg for (size_t i = 0; i < Len; i++)
527330f729Sjoerg W << (isPrint(Start[i]) ? static_cast<char>(Start[i]) : '.');
537330f729Sjoerg }
547330f729Sjoerg
557330f729Sjoerg static std::vector<object::SectionRef>
getSectionRefsByNameOrIndex(const object::ObjectFile & Obj,ArrayRef<std::string> Sections)56*82d56013Sjoerg getSectionRefsByNameOrIndex(const object::ObjectFile &Obj,
577330f729Sjoerg ArrayRef<std::string> Sections) {
587330f729Sjoerg std::vector<object::SectionRef> Ret;
597330f729Sjoerg std::map<std::string, bool> SecNames;
607330f729Sjoerg std::map<unsigned, bool> SecIndices;
617330f729Sjoerg unsigned SecIndex;
627330f729Sjoerg for (StringRef Section : Sections) {
637330f729Sjoerg if (!Section.getAsInteger(0, SecIndex))
647330f729Sjoerg SecIndices.emplace(SecIndex, false);
657330f729Sjoerg else
66*82d56013Sjoerg SecNames.emplace(std::string(Section), false);
677330f729Sjoerg }
687330f729Sjoerg
69*82d56013Sjoerg SecIndex = Obj.isELF() ? 0 : 1;
70*82d56013Sjoerg for (object::SectionRef SecRef : Obj.sections()) {
71*82d56013Sjoerg StringRef SecName = unwrapOrError(Obj.getFileName(), SecRef.getName());
72*82d56013Sjoerg auto NameIt = SecNames.find(std::string(SecName));
737330f729Sjoerg if (NameIt != SecNames.end())
747330f729Sjoerg NameIt->second = true;
757330f729Sjoerg auto IndexIt = SecIndices.find(SecIndex);
767330f729Sjoerg if (IndexIt != SecIndices.end())
777330f729Sjoerg IndexIt->second = true;
787330f729Sjoerg if (NameIt != SecNames.end() || IndexIt != SecIndices.end())
797330f729Sjoerg Ret.push_back(SecRef);
807330f729Sjoerg SecIndex++;
817330f729Sjoerg }
827330f729Sjoerg
83*82d56013Sjoerg for (const std::pair<const std::string, bool> &S : SecNames)
847330f729Sjoerg if (!S.second)
857330f729Sjoerg reportWarning(
867330f729Sjoerg createError(formatv("could not find section '{0}'", S.first).str()),
87*82d56013Sjoerg Obj.getFileName());
887330f729Sjoerg
897330f729Sjoerg for (std::pair<unsigned, bool> S : SecIndices)
907330f729Sjoerg if (!S.second)
917330f729Sjoerg reportWarning(
927330f729Sjoerg createError(formatv("could not find section {0}", S.first).str()),
93*82d56013Sjoerg Obj.getFileName());
947330f729Sjoerg
957330f729Sjoerg return Ret;
967330f729Sjoerg }
977330f729Sjoerg
printSectionsAsString(const object::ObjectFile & Obj,ArrayRef<std::string> Sections)98*82d56013Sjoerg void ObjDumper::printSectionsAsString(const object::ObjectFile &Obj,
997330f729Sjoerg ArrayRef<std::string> Sections) {
1007330f729Sjoerg bool First = true;
1017330f729Sjoerg for (object::SectionRef Section :
1027330f729Sjoerg getSectionRefsByNameOrIndex(Obj, Sections)) {
103*82d56013Sjoerg StringRef SectionName = unwrapOrError(Obj.getFileName(), Section.getName());
1047330f729Sjoerg
1057330f729Sjoerg if (!First)
1067330f729Sjoerg W.startLine() << '\n';
1077330f729Sjoerg First = false;
1087330f729Sjoerg W.startLine() << "String dump of section '" << SectionName << "':\n";
1097330f729Sjoerg
1107330f729Sjoerg StringRef SectionContent =
111*82d56013Sjoerg unwrapOrError(Obj.getFileName(), Section.getContents());
1127330f729Sjoerg
1137330f729Sjoerg const uint8_t *SecContent = SectionContent.bytes_begin();
1147330f729Sjoerg const uint8_t *CurrentWord = SecContent;
1157330f729Sjoerg const uint8_t *SecEnd = SectionContent.bytes_end();
1167330f729Sjoerg
1177330f729Sjoerg while (CurrentWord <= SecEnd) {
1187330f729Sjoerg size_t WordSize = strnlen(reinterpret_cast<const char *>(CurrentWord),
1197330f729Sjoerg SecEnd - CurrentWord);
1207330f729Sjoerg if (!WordSize) {
1217330f729Sjoerg CurrentWord++;
1227330f729Sjoerg continue;
1237330f729Sjoerg }
1247330f729Sjoerg W.startLine() << format("[%6tx] ", CurrentWord - SecContent);
1257330f729Sjoerg printAsPrintable(W.startLine(), CurrentWord, WordSize);
1267330f729Sjoerg W.startLine() << '\n';
1277330f729Sjoerg CurrentWord += WordSize + 1;
1287330f729Sjoerg }
1297330f729Sjoerg }
1307330f729Sjoerg }
1317330f729Sjoerg
printSectionsAsHex(const object::ObjectFile & Obj,ArrayRef<std::string> Sections)132*82d56013Sjoerg void ObjDumper::printSectionsAsHex(const object::ObjectFile &Obj,
1337330f729Sjoerg ArrayRef<std::string> Sections) {
1347330f729Sjoerg bool First = true;
1357330f729Sjoerg for (object::SectionRef Section :
1367330f729Sjoerg getSectionRefsByNameOrIndex(Obj, Sections)) {
137*82d56013Sjoerg StringRef SectionName = unwrapOrError(Obj.getFileName(), Section.getName());
1387330f729Sjoerg
1397330f729Sjoerg if (!First)
1407330f729Sjoerg W.startLine() << '\n';
1417330f729Sjoerg First = false;
1427330f729Sjoerg W.startLine() << "Hex dump of section '" << SectionName << "':\n";
1437330f729Sjoerg
1447330f729Sjoerg StringRef SectionContent =
145*82d56013Sjoerg unwrapOrError(Obj.getFileName(), Section.getContents());
1467330f729Sjoerg const uint8_t *SecContent = SectionContent.bytes_begin();
1477330f729Sjoerg const uint8_t *SecEnd = SecContent + SectionContent.size();
1487330f729Sjoerg
1497330f729Sjoerg for (const uint8_t *SecPtr = SecContent; SecPtr < SecEnd; SecPtr += 16) {
1507330f729Sjoerg const uint8_t *TmpSecPtr = SecPtr;
1517330f729Sjoerg uint8_t i;
1527330f729Sjoerg uint8_t k;
1537330f729Sjoerg
1547330f729Sjoerg W.startLine() << format_hex(Section.getAddress() + (SecPtr - SecContent),
1557330f729Sjoerg 10);
1567330f729Sjoerg W.startLine() << ' ';
1577330f729Sjoerg for (i = 0; TmpSecPtr < SecEnd && i < 4; ++i) {
1587330f729Sjoerg for (k = 0; TmpSecPtr < SecEnd && k < 4; k++, TmpSecPtr++) {
1597330f729Sjoerg uint8_t Val = *(reinterpret_cast<const uint8_t *>(TmpSecPtr));
1607330f729Sjoerg W.startLine() << format_hex_no_prefix(Val, 2);
1617330f729Sjoerg }
1627330f729Sjoerg W.startLine() << ' ';
1637330f729Sjoerg }
1647330f729Sjoerg
1657330f729Sjoerg // We need to print the correct amount of spaces to match the format.
1667330f729Sjoerg // We are adding the (4 - i) last rows that are 8 characters each.
1677330f729Sjoerg // Then, the (4 - i) spaces that are in between the rows.
1687330f729Sjoerg // Least, if we cut in a middle of a row, we add the remaining characters,
1697330f729Sjoerg // which is (8 - (k * 2)).
1707330f729Sjoerg if (i < 4)
171*82d56013Sjoerg W.startLine() << format("%*c", (4 - i) * 8 + (4 - i), ' ');
172*82d56013Sjoerg if (k < 4)
173*82d56013Sjoerg W.startLine() << format("%*c", 8 - k * 2, ' ');
1747330f729Sjoerg
1757330f729Sjoerg TmpSecPtr = SecPtr;
1767330f729Sjoerg for (i = 0; TmpSecPtr + i < SecEnd && i < 16; ++i)
1777330f729Sjoerg W.startLine() << (isPrint(TmpSecPtr[i])
1787330f729Sjoerg ? static_cast<char>(TmpSecPtr[i])
1797330f729Sjoerg : '.');
1807330f729Sjoerg
1817330f729Sjoerg W.startLine() << '\n';
1827330f729Sjoerg }
1837330f729Sjoerg }
1847330f729Sjoerg }
1857330f729Sjoerg
1867330f729Sjoerg } // namespace llvm
187