2013-04-04 02:31:38 +08:00
|
|
|
//===-- ObjDumper.cpp - Base dumper class -----------------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
///
|
|
|
|
/// \file
|
2018-05-01 23:54:18 +08:00
|
|
|
/// This file implements ObjDumper.
|
2013-04-04 02:31:38 +08:00
|
|
|
///
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "ObjDumper.h"
|
|
|
|
#include "Error.h"
|
2018-07-19 02:00:41 +08:00
|
|
|
#include "llvm-readobj.h"
|
2013-04-04 02:31:38 +08:00
|
|
|
#include "llvm/Object/ObjectFile.h"
|
2018-07-19 02:00:41 +08:00
|
|
|
#include "llvm/Support/Error.h"
|
2016-05-03 08:28:04 +08:00
|
|
|
#include "llvm/Support/ScopedPrinter.h"
|
2013-04-04 02:31:38 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
|
2016-05-03 08:28:04 +08:00
|
|
|
ObjDumper::ObjDumper(ScopedPrinter &Writer) : W(Writer) {}
|
2013-04-04 02:31:38 +08:00
|
|
|
|
|
|
|
ObjDumper::~ObjDumper() {
|
|
|
|
}
|
|
|
|
|
2018-07-19 02:00:41 +08:00
|
|
|
static void printAsPrintable(raw_ostream &W, const uint8_t *Start, size_t Len) {
|
|
|
|
for (size_t i = 0; i < Len; i++)
|
|
|
|
W << (isprint(Start[i]) ? static_cast<char>(Start[i]) : '.');
|
|
|
|
}
|
|
|
|
|
|
|
|
static Expected<object::SectionRef>
|
|
|
|
getSecNameOrIndexAsSecRef(const object::ObjectFile *Obj, StringRef SecName) {
|
|
|
|
char *StrPtr;
|
|
|
|
long SectionIndex = strtol(SecName.data(), &StrPtr, 10);
|
|
|
|
object::SectionRef Section;
|
2018-07-25 18:04:37 +08:00
|
|
|
long SecIndex;
|
|
|
|
if (Obj->isELF())
|
|
|
|
SecIndex = 0;
|
|
|
|
else
|
|
|
|
SecIndex = 1;
|
2018-07-19 02:00:41 +08:00
|
|
|
for (object::SectionRef SecRef : Obj->sections()) {
|
|
|
|
if (*StrPtr) {
|
|
|
|
StringRef SectionName;
|
|
|
|
|
|
|
|
if (std::error_code E = SecRef.getName(SectionName))
|
|
|
|
return errorCodeToError(E);
|
|
|
|
|
|
|
|
if (SectionName == SecName)
|
|
|
|
return SecRef;
|
|
|
|
} else if (SecIndex == SectionIndex)
|
|
|
|
return SecRef;
|
|
|
|
|
|
|
|
SecIndex++;
|
|
|
|
}
|
|
|
|
return make_error<StringError>("invalid section reference",
|
|
|
|
object::object_error::parse_failed);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ObjDumper::printSectionAsString(const object::ObjectFile *Obj,
|
|
|
|
StringRef SecName) {
|
|
|
|
Expected<object::SectionRef> SectionRefOrError =
|
|
|
|
getSecNameOrIndexAsSecRef(Obj, SecName);
|
|
|
|
if (!SectionRefOrError)
|
|
|
|
error(std::move(SectionRefOrError));
|
|
|
|
object::SectionRef Section = *SectionRefOrError;
|
|
|
|
StringRef SectionName;
|
|
|
|
|
|
|
|
if (std::error_code E = Section.getName(SectionName))
|
|
|
|
error(E);
|
|
|
|
W.startLine() << "String dump of section '" << SectionName << "':\n";
|
|
|
|
|
|
|
|
StringRef SectionContent;
|
|
|
|
Section.getContents(SectionContent);
|
|
|
|
|
|
|
|
const uint8_t *SecContent = SectionContent.bytes_begin();
|
|
|
|
const uint8_t *CurrentWord = SecContent;
|
|
|
|
const uint8_t *SecEnd = SectionContent.bytes_end();
|
|
|
|
|
|
|
|
while (CurrentWord <= SecEnd) {
|
|
|
|
size_t WordSize = strnlen(reinterpret_cast<const char *>(CurrentWord),
|
|
|
|
SecEnd - CurrentWord);
|
|
|
|
if (!WordSize) {
|
|
|
|
CurrentWord++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
W.startLine() << format("[%6tx] ", CurrentWord - SecContent);
|
|
|
|
printAsPrintable(W.startLine(), CurrentWord, WordSize);
|
|
|
|
W.startLine() << '\n';
|
|
|
|
CurrentWord += WordSize + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-25 18:04:37 +08:00
|
|
|
void ObjDumper::printSectionAsHex(const object::ObjectFile *Obj,
|
|
|
|
StringRef SecName) {
|
|
|
|
Expected<object::SectionRef> SectionRefOrError =
|
|
|
|
getSecNameOrIndexAsSecRef(Obj, SecName);
|
|
|
|
if (!SectionRefOrError)
|
|
|
|
error(std::move(SectionRefOrError));
|
|
|
|
object::SectionRef Section = *SectionRefOrError;
|
|
|
|
StringRef SectionName;
|
|
|
|
|
|
|
|
if (std::error_code E = Section.getName(SectionName))
|
|
|
|
error(E);
|
|
|
|
W.startLine() << "Hex dump of section '" << SectionName << "':\n";
|
|
|
|
|
|
|
|
StringRef SectionContent;
|
|
|
|
Section.getContents(SectionContent);
|
|
|
|
const uint8_t *SecContent = SectionContent.bytes_begin();
|
|
|
|
const uint8_t *SecEnd = SecContent + SectionContent.size();
|
2018-07-11 18:00:29 +08:00
|
|
|
|
|
|
|
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(SecPtr - SecContent, 10);
|
|
|
|
W.startLine() << ' ';
|
|
|
|
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));
|
|
|
|
W.startLine() << format_hex_no_prefix(Val, 2);
|
|
|
|
}
|
|
|
|
W.startLine() << ' ';
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
W.startLine() << format("%*c", (4 - i) * 8 + (4 - i) + (8 - (k * 2)),
|
|
|
|
' ');
|
|
|
|
|
|
|
|
TmpSecPtr = SecPtr;
|
2018-07-25 18:04:37 +08:00
|
|
|
for (i = 0; TmpSecPtr + i < SecEnd && i < 16; ++i)
|
|
|
|
W.startLine() << (isprint(TmpSecPtr[i]) ? static_cast<char>(TmpSecPtr[i])
|
|
|
|
: '.');
|
2018-07-11 18:00:29 +08:00
|
|
|
|
|
|
|
W.startLine() << '\n';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-04 02:31:38 +08:00
|
|
|
} // namespace llvm
|