2013-04-03 18:31:38 +00:00
|
|
|
//===-- ObjDumper.cpp - Base dumper class -----------------------*- C++ -*-===//
|
|
|
|
//
|
2019-01-19 08:50:56 +00:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2013-04-03 18:31:38 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
///
|
|
|
|
/// \file
|
2018-05-01 15:54:18 +00:00
|
|
|
/// This file implements ObjDumper.
|
2013-04-03 18:31:38 +00:00
|
|
|
///
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "ObjDumper.h"
|
2018-07-18 18:00:41 +00:00
|
|
|
#include "llvm-readobj.h"
|
2021-12-10 23:27:06 +00:00
|
|
|
#include "llvm/Object/Archive.h"
|
2024-02-22 09:24:21 -08:00
|
|
|
#include "llvm/Object/Decompressor.h"
|
2013-04-03 18:31:38 +00:00
|
|
|
#include "llvm/Object/ObjectFile.h"
|
2018-07-18 18:00:41 +00:00
|
|
|
#include "llvm/Support/Error.h"
|
2019-04-15 11:17:48 +00:00
|
|
|
#include "llvm/Support/FormatVariadic.h"
|
2016-05-03 00:28:04 +00:00
|
|
|
#include "llvm/Support/ScopedPrinter.h"
|
2023-12-13 13:13:53 -05:00
|
|
|
#include "llvm/Support/SystemZ/zOSSupport.h"
|
2013-04-03 18:31:38 +00:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2019-06-18 14:01:03 +00:00
|
|
|
#include <map>
|
2013-04-03 18:31:38 +00:00
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
|
Recommit r369190 "[llvm-readobj/llvm-readelf] - Improve/cleanup the error reporting API."
Fix: Add a `consumeError` call removed by mistake to 'printStackSize',
this should fix the "Expected<T> must be checked before access or destruction." reported by following bot:
http://lab.llvm.org:8011/builders/clang-x64-windows-msvc/builds/9743/steps/stage%201%20check/logs/stdio
Original commit message:
Currently we have the following functions for error reporting:
LLVM_ATTRIBUTE_NORETURN void reportError(Twine Msg);
void reportError(Error Err, StringRef Input);
void reportWarning(Twine Msg);
void reportWarning(StringRef Input, Error Err);
void warn(llvm::Error Err);
void error(std::error_code EC);
Problems are: naming is inconsistent, arguments order is inconsistent,
some of the functions looks excessive.
After applying this patch we have:
void reportError(Error Err, StringRef Input);
void reportError(std::error_code EC, StringRef Input);
void reportWarning(Error Err, StringRef Input);
I'd be happy to remove reportError(std::error_code EC, StringRef Input) too, but it
is used by COFF heavily.
Test cases were updated, they show an improvement introduced.
Differential revision: https://reviews.llvm.org/D66286
llvm-svn: 369194
2019-08-17 16:07:18 +00:00
|
|
|
static inline Error createError(const Twine &Msg) {
|
|
|
|
return createStringError(object::object_error::parse_failed, Msg);
|
|
|
|
}
|
|
|
|
|
2020-11-27 13:34:30 +03:00
|
|
|
ObjDumper::ObjDumper(ScopedPrinter &Writer, StringRef ObjName) : W(Writer) {
|
|
|
|
// Dumper reports all non-critical errors as warnings.
|
|
|
|
// It does not print the same warning more than once.
|
|
|
|
WarningHandler = [=](const Twine &Msg) {
|
|
|
|
if (Warnings.insert(Msg.str()).second)
|
|
|
|
reportWarning(createError(Msg), ObjName);
|
|
|
|
return Error::success();
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
ObjDumper::~ObjDumper() {}
|
2013-04-03 18:31:38 +00:00
|
|
|
|
2020-11-27 13:34:30 +03:00
|
|
|
void ObjDumper::reportUniqueWarning(Error Err) const {
|
2020-12-01 12:08:46 +03:00
|
|
|
reportUniqueWarning(toString(std::move(Err)));
|
|
|
|
}
|
|
|
|
|
|
|
|
void ObjDumper::reportUniqueWarning(const Twine &Msg) const {
|
|
|
|
cantFail(WarningHandler(Msg),
|
|
|
|
"WarningHandler should always return ErrorSuccess");
|
2013-04-03 18:31:38 +00:00
|
|
|
}
|
|
|
|
|
2018-07-18 18:00:41 +00:00
|
|
|
static void printAsPrintable(raw_ostream &W, const uint8_t *Start, size_t Len) {
|
|
|
|
for (size_t i = 0; i < Len; i++)
|
2018-07-26 15:31:41 +00:00
|
|
|
W << (isPrint(Start[i]) ? static_cast<char>(Start[i]) : '.');
|
2018-07-18 18:00:41 +00:00
|
|
|
}
|
|
|
|
|
2021-08-03 09:08:58 +00:00
|
|
|
void ObjDumper::printAsStringList(StringRef StringContent,
|
|
|
|
size_t StringDataOffset) {
|
2021-08-06 08:54:02 +00:00
|
|
|
size_t StrSize = StringContent.size();
|
|
|
|
if (StrSize == 0)
|
|
|
|
return;
|
|
|
|
if (StrSize < StringDataOffset) {
|
|
|
|
reportUniqueWarning("offset (0x" + Twine::utohexstr(StringDataOffset) +
|
|
|
|
") is past the end of the contents (size 0x" +
|
|
|
|
Twine::utohexstr(StrSize) + ")");
|
2021-08-03 10:29:30 -07:00
|
|
|
return;
|
2021-08-03 13:14:52 -07:00
|
|
|
}
|
2021-08-06 08:54:02 +00:00
|
|
|
|
2021-07-05 04:16:58 +00:00
|
|
|
const uint8_t *StrContent = StringContent.bytes_begin();
|
2021-08-03 09:08:58 +00:00
|
|
|
// Some formats contain additional metadata at the start which should not be
|
|
|
|
// interpreted as strings. Skip these bytes, but account for them in the
|
|
|
|
// string offsets.
|
|
|
|
const uint8_t *CurrentWord = StrContent + StringDataOffset;
|
2021-07-05 04:16:58 +00:00
|
|
|
const uint8_t *StrEnd = StringContent.bytes_end();
|
|
|
|
|
|
|
|
while (CurrentWord <= StrEnd) {
|
|
|
|
size_t WordSize = strnlen(reinterpret_cast<const char *>(CurrentWord),
|
|
|
|
StrEnd - CurrentWord);
|
|
|
|
if (!WordSize) {
|
|
|
|
CurrentWord++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
W.startLine() << format("[%6tx] ", CurrentWord - StrContent);
|
2024-08-06 18:04:23 +02:00
|
|
|
printAsPrintable(W.getOStream(), CurrentWord, WordSize);
|
|
|
|
W.getOStream() << '\n';
|
2021-07-05 04:16:58 +00:00
|
|
|
CurrentWord += WordSize + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-10 23:27:06 +00:00
|
|
|
void ObjDumper::printFileSummary(StringRef FileStr, object::ObjectFile &Obj,
|
|
|
|
ArrayRef<std::string> InputFilenames,
|
|
|
|
const object::Archive *A) {
|
2024-08-09 15:55:28 +02:00
|
|
|
if (!FileStr.empty()) {
|
|
|
|
W.getOStream() << "\n";
|
|
|
|
W.printString("File", FileStr);
|
|
|
|
}
|
2021-12-10 23:27:06 +00:00
|
|
|
W.printString("Format", Obj.getFileFormatName());
|
|
|
|
W.printString("Arch", Triple::getArchTypeName(Obj.getArch()));
|
|
|
|
W.printString("AddressSize",
|
|
|
|
std::string(formatv("{0}bit", 8 * Obj.getBytesInAddress())));
|
|
|
|
this->printLoadName();
|
|
|
|
}
|
|
|
|
|
2019-06-18 14:01:03 +00:00
|
|
|
static std::vector<object::SectionRef>
|
2020-09-19 19:53:51 +03:00
|
|
|
getSectionRefsByNameOrIndex(const object::ObjectFile &Obj,
|
2019-06-18 14:01:03 +00:00
|
|
|
ArrayRef<std::string> Sections) {
|
|
|
|
std::vector<object::SectionRef> Ret;
|
2024-11-05 09:38:11 -08:00
|
|
|
std::map<std::string, bool, std::less<>> SecNames;
|
2019-06-18 14:01:03 +00:00
|
|
|
std::map<unsigned, bool> SecIndices;
|
|
|
|
unsigned SecIndex;
|
|
|
|
for (StringRef Section : Sections) {
|
|
|
|
if (!Section.getAsInteger(0, SecIndex))
|
|
|
|
SecIndices.emplace(SecIndex, false);
|
|
|
|
else
|
2020-01-29 03:11:00 +01:00
|
|
|
SecNames.emplace(std::string(Section), false);
|
2019-06-18 14:01:03 +00:00
|
|
|
}
|
2018-07-18 18:00:41 +00:00
|
|
|
|
2020-09-19 19:53:51 +03:00
|
|
|
SecIndex = Obj.isELF() ? 0 : 1;
|
|
|
|
for (object::SectionRef SecRef : Obj.sections()) {
|
|
|
|
StringRef SecName = unwrapOrError(Obj.getFileName(), SecRef.getName());
|
2024-11-05 09:38:11 -08:00
|
|
|
auto NameIt = SecNames.find(SecName);
|
2019-06-18 14:01:03 +00:00
|
|
|
if (NameIt != SecNames.end())
|
|
|
|
NameIt->second = true;
|
|
|
|
auto IndexIt = SecIndices.find(SecIndex);
|
|
|
|
if (IndexIt != SecIndices.end())
|
|
|
|
IndexIt->second = true;
|
|
|
|
if (NameIt != SecNames.end() || IndexIt != SecIndices.end())
|
|
|
|
Ret.push_back(SecRef);
|
2018-07-18 18:00:41 +00:00
|
|
|
SecIndex++;
|
|
|
|
}
|
2019-06-18 14:01:03 +00:00
|
|
|
|
2019-12-22 19:11:17 +01:00
|
|
|
for (const std::pair<const std::string, bool> &S : SecNames)
|
2019-06-18 14:01:03 +00:00
|
|
|
if (!S.second)
|
Recommit r369190 "[llvm-readobj/llvm-readelf] - Improve/cleanup the error reporting API."
Fix: Add a `consumeError` call removed by mistake to 'printStackSize',
this should fix the "Expected<T> must be checked before access or destruction." reported by following bot:
http://lab.llvm.org:8011/builders/clang-x64-windows-msvc/builds/9743/steps/stage%201%20check/logs/stdio
Original commit message:
Currently we have the following functions for error reporting:
LLVM_ATTRIBUTE_NORETURN void reportError(Twine Msg);
void reportError(Error Err, StringRef Input);
void reportWarning(Twine Msg);
void reportWarning(StringRef Input, Error Err);
void warn(llvm::Error Err);
void error(std::error_code EC);
Problems are: naming is inconsistent, arguments order is inconsistent,
some of the functions looks excessive.
After applying this patch we have:
void reportError(Error Err, StringRef Input);
void reportError(std::error_code EC, StringRef Input);
void reportWarning(Error Err, StringRef Input);
I'd be happy to remove reportError(std::error_code EC, StringRef Input) too, but it
is used by COFF heavily.
Test cases were updated, they show an improvement introduced.
Differential revision: https://reviews.llvm.org/D66286
llvm-svn: 369194
2019-08-17 16:07:18 +00:00
|
|
|
reportWarning(
|
|
|
|
createError(formatv("could not find section '{0}'", S.first).str()),
|
2020-09-19 19:53:51 +03:00
|
|
|
Obj.getFileName());
|
Recommit r369190 "[llvm-readobj/llvm-readelf] - Improve/cleanup the error reporting API."
Fix: Add a `consumeError` call removed by mistake to 'printStackSize',
this should fix the "Expected<T> must be checked before access or destruction." reported by following bot:
http://lab.llvm.org:8011/builders/clang-x64-windows-msvc/builds/9743/steps/stage%201%20check/logs/stdio
Original commit message:
Currently we have the following functions for error reporting:
LLVM_ATTRIBUTE_NORETURN void reportError(Twine Msg);
void reportError(Error Err, StringRef Input);
void reportWarning(Twine Msg);
void reportWarning(StringRef Input, Error Err);
void warn(llvm::Error Err);
void error(std::error_code EC);
Problems are: naming is inconsistent, arguments order is inconsistent,
some of the functions looks excessive.
After applying this patch we have:
void reportError(Error Err, StringRef Input);
void reportError(std::error_code EC, StringRef Input);
void reportWarning(Error Err, StringRef Input);
I'd be happy to remove reportError(std::error_code EC, StringRef Input) too, but it
is used by COFF heavily.
Test cases were updated, they show an improvement introduced.
Differential revision: https://reviews.llvm.org/D66286
llvm-svn: 369194
2019-08-17 16:07:18 +00:00
|
|
|
|
2019-06-18 14:01:03 +00:00
|
|
|
for (std::pair<unsigned, bool> S : SecIndices)
|
|
|
|
if (!S.second)
|
Recommit r369190 "[llvm-readobj/llvm-readelf] - Improve/cleanup the error reporting API."
Fix: Add a `consumeError` call removed by mistake to 'printStackSize',
this should fix the "Expected<T> must be checked before access or destruction." reported by following bot:
http://lab.llvm.org:8011/builders/clang-x64-windows-msvc/builds/9743/steps/stage%201%20check/logs/stdio
Original commit message:
Currently we have the following functions for error reporting:
LLVM_ATTRIBUTE_NORETURN void reportError(Twine Msg);
void reportError(Error Err, StringRef Input);
void reportWarning(Twine Msg);
void reportWarning(StringRef Input, Error Err);
void warn(llvm::Error Err);
void error(std::error_code EC);
Problems are: naming is inconsistent, arguments order is inconsistent,
some of the functions looks excessive.
After applying this patch we have:
void reportError(Error Err, StringRef Input);
void reportError(std::error_code EC, StringRef Input);
void reportWarning(Error Err, StringRef Input);
I'd be happy to remove reportError(std::error_code EC, StringRef Input) too, but it
is used by COFF heavily.
Test cases were updated, they show an improvement introduced.
Differential revision: https://reviews.llvm.org/D66286
llvm-svn: 369194
2019-08-17 16:07:18 +00:00
|
|
|
reportWarning(
|
|
|
|
createError(formatv("could not find section {0}", S.first).str()),
|
2020-09-19 19:53:51 +03:00
|
|
|
Obj.getFileName());
|
2019-06-18 14:01:03 +00:00
|
|
|
|
|
|
|
return Ret;
|
2018-07-18 18:00:41 +00:00
|
|
|
}
|
|
|
|
|
2024-02-22 09:24:21 -08:00
|
|
|
static void maybeDecompress(const object::ObjectFile &Obj,
|
|
|
|
StringRef SectionName, StringRef &SectionContent,
|
|
|
|
SmallString<0> &Out) {
|
|
|
|
Expected<object::Decompressor> Decompressor = object::Decompressor::create(
|
|
|
|
SectionName, SectionContent, Obj.isLittleEndian(), Obj.is64Bit());
|
|
|
|
if (!Decompressor)
|
|
|
|
reportWarning(Decompressor.takeError(), Obj.getFileName());
|
|
|
|
else if (auto Err = Decompressor->resizeAndDecompress(Out))
|
|
|
|
reportWarning(std::move(Err), Obj.getFileName());
|
|
|
|
else
|
|
|
|
SectionContent = Out;
|
|
|
|
}
|
|
|
|
|
2020-09-19 19:53:51 +03:00
|
|
|
void ObjDumper::printSectionsAsString(const object::ObjectFile &Obj,
|
2024-02-22 09:24:21 -08:00
|
|
|
ArrayRef<std::string> Sections,
|
|
|
|
bool Decompress) {
|
|
|
|
SmallString<0> Out;
|
2019-06-18 14:01:03 +00:00
|
|
|
for (object::SectionRef Section :
|
|
|
|
getSectionRefsByNameOrIndex(Obj, Sections)) {
|
2020-09-19 19:53:51 +03:00
|
|
|
StringRef SectionName = unwrapOrError(Obj.getFileName(), Section.getName());
|
2024-08-06 18:04:23 +02:00
|
|
|
W.getOStream() << '\n';
|
|
|
|
W.startLine() << "String dump of section '" << SectionName << "':\n";
|
2019-06-18 14:01:03 +00:00
|
|
|
|
2019-08-09 10:53:12 +00:00
|
|
|
StringRef SectionContent =
|
2020-09-19 19:53:51 +03:00
|
|
|
unwrapOrError(Obj.getFileName(), Section.getContents());
|
2024-02-22 09:24:21 -08:00
|
|
|
if (Decompress && Section.isCompressed())
|
|
|
|
maybeDecompress(Obj, SectionName, SectionContent, Out);
|
2021-07-05 04:16:58 +00:00
|
|
|
printAsStringList(SectionContent);
|
2018-07-18 18:00:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-19 19:53:51 +03:00
|
|
|
void ObjDumper::printSectionsAsHex(const object::ObjectFile &Obj,
|
2024-02-22 09:24:21 -08:00
|
|
|
ArrayRef<std::string> Sections,
|
|
|
|
bool Decompress) {
|
|
|
|
SmallString<0> Out;
|
2019-06-18 14:01:03 +00:00
|
|
|
for (object::SectionRef Section :
|
|
|
|
getSectionRefsByNameOrIndex(Obj, Sections)) {
|
2020-09-19 19:53:51 +03:00
|
|
|
StringRef SectionName = unwrapOrError(Obj.getFileName(), Section.getName());
|
2024-08-06 18:04:23 +02:00
|
|
|
W.getOStream() << '\n';
|
|
|
|
W.startLine() << "Hex dump of section '" << SectionName << "':\n";
|
2019-06-18 14:01:03 +00:00
|
|
|
|
2019-08-09 10:53:12 +00:00
|
|
|
StringRef SectionContent =
|
2020-09-19 19:53:51 +03:00
|
|
|
unwrapOrError(Obj.getFileName(), Section.getContents());
|
2024-02-22 09:24:21 -08:00
|
|
|
if (Decompress && Section.isCompressed())
|
|
|
|
maybeDecompress(Obj, SectionName, SectionContent, Out);
|
2019-06-18 14:01:03 +00:00
|
|
|
const uint8_t *SecContent = SectionContent.bytes_begin();
|
|
|
|
const uint8_t *SecEnd = SecContent + SectionContent.size();
|
|
|
|
|
|
|
|
for (const uint8_t *SecPtr = SecContent; SecPtr < SecEnd; SecPtr += 16) {
|
|
|
|
const uint8_t *TmpSecPtr = SecPtr;
|
|
|
|
uint8_t i;
|
|
|
|
uint8_t k;
|
|
|
|
|
|
|
|
W.startLine() << format_hex(Section.getAddress() + (SecPtr - SecContent),
|
|
|
|
10);
|
2024-08-06 18:04:23 +02:00
|
|
|
W.getOStream() << ' ';
|
2019-06-18 14:01:03 +00:00
|
|
|
for (i = 0; TmpSecPtr < SecEnd && i < 4; ++i) {
|
|
|
|
for (k = 0; TmpSecPtr < SecEnd && k < 4; k++, TmpSecPtr++) {
|
|
|
|
uint8_t Val = *(reinterpret_cast<const uint8_t *>(TmpSecPtr));
|
2024-08-06 18:04:23 +02:00
|
|
|
W.getOStream() << format_hex_no_prefix(Val, 2);
|
2019-06-18 14:01:03 +00:00
|
|
|
}
|
2024-08-06 18:04:23 +02:00
|
|
|
W.getOStream() << ' ';
|
2019-06-18 14:01:03 +00:00
|
|
|
}
|
2018-07-11 10:00:29 +00:00
|
|
|
|
2019-06-18 14:01:03 +00:00
|
|
|
// We need to print the correct amount of spaces to match the format.
|
|
|
|
// We are adding the (4 - i) last rows that are 8 characters each.
|
|
|
|
// Then, the (4 - i) spaces that are in between the rows.
|
|
|
|
// Least, if we cut in a middle of a row, we add the remaining characters,
|
|
|
|
// which is (8 - (k * 2)).
|
|
|
|
if (i < 4)
|
2024-08-06 18:04:23 +02:00
|
|
|
W.getOStream() << format("%*c", (4 - i) * 8 + (4 - i), ' ');
|
2020-07-28 09:53:59 +08:00
|
|
|
if (k < 4)
|
2024-08-06 18:04:23 +02:00
|
|
|
W.getOStream() << format("%*c", 8 - k * 2, ' ');
|
2019-06-18 14:01:03 +00:00
|
|
|
|
|
|
|
TmpSecPtr = SecPtr;
|
|
|
|
for (i = 0; TmpSecPtr + i < SecEnd && i < 16; ++i)
|
2024-08-06 18:04:23 +02:00
|
|
|
W.getOStream() << (isPrint(TmpSecPtr[i])
|
|
|
|
? static_cast<char>(TmpSecPtr[i])
|
|
|
|
: '.');
|
2019-06-18 14:01:03 +00:00
|
|
|
|
2024-08-06 18:04:23 +02:00
|
|
|
W.getOStream() << '\n';
|
2019-06-18 14:01:03 +00:00
|
|
|
}
|
2018-07-11 10:00:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-03 18:31:38 +00:00
|
|
|
} // namespace llvm
|