Path: blob/main/contrib/llvm-project/llvm/tools/llvm-readobj/ObjDumper.cpp
35231 views
//===-- ObjDumper.cpp - Base dumper class -----------------------*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7///8/// \file9/// This file implements ObjDumper.10///11//===----------------------------------------------------------------------===//1213#include "ObjDumper.h"14#include "llvm-readobj.h"15#include "llvm/Object/Archive.h"16#include "llvm/Object/Decompressor.h"17#include "llvm/Object/ObjectFile.h"18#include "llvm/Support/Error.h"19#include "llvm/Support/FormatVariadic.h"20#include "llvm/Support/ScopedPrinter.h"21#include "llvm/Support/SystemZ/zOSSupport.h"22#include "llvm/Support/raw_ostream.h"23#include <map>2425namespace llvm {2627static inline Error createError(const Twine &Msg) {28return createStringError(object::object_error::parse_failed, Msg);29}3031ObjDumper::ObjDumper(ScopedPrinter &Writer, StringRef ObjName) : W(Writer) {32// Dumper reports all non-critical errors as warnings.33// It does not print the same warning more than once.34WarningHandler = [=](const Twine &Msg) {35if (Warnings.insert(Msg.str()).second)36reportWarning(createError(Msg), ObjName);37return Error::success();38};39}4041ObjDumper::~ObjDumper() {}4243void ObjDumper::reportUniqueWarning(Error Err) const {44reportUniqueWarning(toString(std::move(Err)));45}4647void ObjDumper::reportUniqueWarning(const Twine &Msg) const {48cantFail(WarningHandler(Msg),49"WarningHandler should always return ErrorSuccess");50}5152static void printAsPrintable(raw_ostream &W, const uint8_t *Start, size_t Len) {53for (size_t i = 0; i < Len; i++)54W << (isPrint(Start[i]) ? static_cast<char>(Start[i]) : '.');55}5657void ObjDumper::printAsStringList(StringRef StringContent,58size_t StringDataOffset) {59size_t StrSize = StringContent.size();60if (StrSize == 0)61return;62if (StrSize < StringDataOffset) {63reportUniqueWarning("offset (0x" + Twine::utohexstr(StringDataOffset) +64") is past the end of the contents (size 0x" +65Twine::utohexstr(StrSize) + ")");66return;67}6869const uint8_t *StrContent = StringContent.bytes_begin();70// Some formats contain additional metadata at the start which should not be71// interpreted as strings. Skip these bytes, but account for them in the72// string offsets.73const uint8_t *CurrentWord = StrContent + StringDataOffset;74const uint8_t *StrEnd = StringContent.bytes_end();7576while (CurrentWord <= StrEnd) {77size_t WordSize = strnlen(reinterpret_cast<const char *>(CurrentWord),78StrEnd - CurrentWord);79if (!WordSize) {80CurrentWord++;81continue;82}83W.startLine() << format("[%6tx] ", CurrentWord - StrContent);84printAsPrintable(W.startLine(), CurrentWord, WordSize);85W.startLine() << '\n';86CurrentWord += WordSize + 1;87}88}8990void ObjDumper::printFileSummary(StringRef FileStr, object::ObjectFile &Obj,91ArrayRef<std::string> InputFilenames,92const object::Archive *A) {93W.startLine() << "\n";94W.printString("File", FileStr);95W.printString("Format", Obj.getFileFormatName());96W.printString("Arch", Triple::getArchTypeName(Obj.getArch()));97W.printString("AddressSize",98std::string(formatv("{0}bit", 8 * Obj.getBytesInAddress())));99this->printLoadName();100}101102static std::vector<object::SectionRef>103getSectionRefsByNameOrIndex(const object::ObjectFile &Obj,104ArrayRef<std::string> Sections) {105std::vector<object::SectionRef> Ret;106std::map<std::string, bool> SecNames;107std::map<unsigned, bool> SecIndices;108unsigned SecIndex;109for (StringRef Section : Sections) {110if (!Section.getAsInteger(0, SecIndex))111SecIndices.emplace(SecIndex, false);112else113SecNames.emplace(std::string(Section), false);114}115116SecIndex = Obj.isELF() ? 0 : 1;117for (object::SectionRef SecRef : Obj.sections()) {118StringRef SecName = unwrapOrError(Obj.getFileName(), SecRef.getName());119auto NameIt = SecNames.find(std::string(SecName));120if (NameIt != SecNames.end())121NameIt->second = true;122auto IndexIt = SecIndices.find(SecIndex);123if (IndexIt != SecIndices.end())124IndexIt->second = true;125if (NameIt != SecNames.end() || IndexIt != SecIndices.end())126Ret.push_back(SecRef);127SecIndex++;128}129130for (const std::pair<const std::string, bool> &S : SecNames)131if (!S.second)132reportWarning(133createError(formatv("could not find section '{0}'", S.first).str()),134Obj.getFileName());135136for (std::pair<unsigned, bool> S : SecIndices)137if (!S.second)138reportWarning(139createError(formatv("could not find section {0}", S.first).str()),140Obj.getFileName());141142return Ret;143}144145static void maybeDecompress(const object::ObjectFile &Obj,146StringRef SectionName, StringRef &SectionContent,147SmallString<0> &Out) {148Expected<object::Decompressor> Decompressor = object::Decompressor::create(149SectionName, SectionContent, Obj.isLittleEndian(), Obj.is64Bit());150if (!Decompressor)151reportWarning(Decompressor.takeError(), Obj.getFileName());152else if (auto Err = Decompressor->resizeAndDecompress(Out))153reportWarning(std::move(Err), Obj.getFileName());154else155SectionContent = Out;156}157158void ObjDumper::printSectionsAsString(const object::ObjectFile &Obj,159ArrayRef<std::string> Sections,160bool Decompress) {161SmallString<0> Out;162for (object::SectionRef Section :163getSectionRefsByNameOrIndex(Obj, Sections)) {164StringRef SectionName = unwrapOrError(Obj.getFileName(), Section.getName());165W.startLine() << "\nString dump of section '" << SectionName << "':\n";166167StringRef SectionContent =168unwrapOrError(Obj.getFileName(), Section.getContents());169if (Decompress && Section.isCompressed())170maybeDecompress(Obj, SectionName, SectionContent, Out);171printAsStringList(SectionContent);172}173}174175void ObjDumper::printSectionsAsHex(const object::ObjectFile &Obj,176ArrayRef<std::string> Sections,177bool Decompress) {178SmallString<0> Out;179for (object::SectionRef Section :180getSectionRefsByNameOrIndex(Obj, Sections)) {181StringRef SectionName = unwrapOrError(Obj.getFileName(), Section.getName());182W.startLine() << "\nHex dump of section '" << SectionName << "':\n";183184StringRef SectionContent =185unwrapOrError(Obj.getFileName(), Section.getContents());186if (Decompress && Section.isCompressed())187maybeDecompress(Obj, SectionName, SectionContent, Out);188const uint8_t *SecContent = SectionContent.bytes_begin();189const uint8_t *SecEnd = SecContent + SectionContent.size();190191for (const uint8_t *SecPtr = SecContent; SecPtr < SecEnd; SecPtr += 16) {192const uint8_t *TmpSecPtr = SecPtr;193uint8_t i;194uint8_t k;195196W.startLine() << format_hex(Section.getAddress() + (SecPtr - SecContent),19710);198W.startLine() << ' ';199for (i = 0; TmpSecPtr < SecEnd && i < 4; ++i) {200for (k = 0; TmpSecPtr < SecEnd && k < 4; k++, TmpSecPtr++) {201uint8_t Val = *(reinterpret_cast<const uint8_t *>(TmpSecPtr));202W.startLine() << format_hex_no_prefix(Val, 2);203}204W.startLine() << ' ';205}206207// We need to print the correct amount of spaces to match the format.208// We are adding the (4 - i) last rows that are 8 characters each.209// Then, the (4 - i) spaces that are in between the rows.210// Least, if we cut in a middle of a row, we add the remaining characters,211// which is (8 - (k * 2)).212if (i < 4)213W.startLine() << format("%*c", (4 - i) * 8 + (4 - i), ' ');214if (k < 4)215W.startLine() << format("%*c", 8 - k * 2, ' ');216217TmpSecPtr = SecPtr;218for (i = 0; TmpSecPtr + i < SecEnd && i < 16; ++i)219W.startLine() << (isPrint(TmpSecPtr[i])220? static_cast<char>(TmpSecPtr[i])221: '.');222223W.startLine() << '\n';224}225}226}227228} // namespace llvm229230231