2017-11-14 20:05:11 -08:00
|
|
|
//===--- BinaryData.cpp - Representation of section data objects ----------===//
|
|
|
|
|
//
|
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
|
//
|
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
|
//
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
//
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
|
|
#include "BinaryData.h"
|
|
|
|
|
#include "BinarySection.h"
|
|
|
|
|
#include "llvm/Support/CommandLine.h"
|
|
|
|
|
#include "llvm/Support/Regex.h"
|
|
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
using namespace bolt;
|
|
|
|
|
|
|
|
|
|
#undef DEBUG_TYPE
|
|
|
|
|
#define DEBUG_TYPE "bolt"
|
|
|
|
|
|
|
|
|
|
namespace opts {
|
|
|
|
|
extern cl::OptionCategory BoltCategory;
|
|
|
|
|
extern cl::opt<unsigned> Verbosity;
|
|
|
|
|
|
|
|
|
|
cl::opt<bool>
|
|
|
|
|
PrintSymbolAliases("print-aliases",
|
|
|
|
|
cl::desc("print aliases when printing objects"),
|
|
|
|
|
cl::Hidden,
|
|
|
|
|
cl::ZeroOrMore,
|
|
|
|
|
cl::cat(BoltCategory));
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-06 03:17:32 -07:00
|
|
|
bool BinaryData::isAbsolute() const {
|
|
|
|
|
return Flags & SymbolRef::SF_Absolute;
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-14 20:05:11 -08:00
|
|
|
bool BinaryData::isMoveable() const {
|
|
|
|
|
return (!isAbsolute() &&
|
|
|
|
|
(IsMoveable &&
|
|
|
|
|
(!Parent || isTopLevelJumpTable())));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BinaryData::merge(const BinaryData *Other) {
|
|
|
|
|
assert(!Size || !Other->Size || Size == Other->Size);
|
|
|
|
|
assert(Address == Other->Address);
|
|
|
|
|
assert(*Section == *Other->Section);
|
|
|
|
|
assert(OutputOffset == Other->OutputOffset);
|
|
|
|
|
assert(OutputSection == Other->OutputSection);
|
|
|
|
|
Names.insert(Names.end(), Other->Names.begin(), Other->Names.end());
|
|
|
|
|
Symbols.insert(Symbols.end(), Other->Symbols.begin(), Other->Symbols.end());
|
|
|
|
|
MemData.insert(MemData.end(), Other->MemData.begin(), Other->MemData.end());
|
2018-04-20 20:03:31 -07:00
|
|
|
Flags |= Other->Flags;
|
2017-11-14 20:05:11 -08:00
|
|
|
if (!Size)
|
|
|
|
|
Size = Other->Size;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool BinaryData::hasNameRegex(StringRef NameRegex) const {
|
|
|
|
|
Regex MatchName(NameRegex);
|
|
|
|
|
for (auto &Name : Names)
|
|
|
|
|
if (MatchName.match(Name))
|
|
|
|
|
return true;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
StringRef BinaryData::getSectionName() const {
|
|
|
|
|
return getSection().getName();
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-20 20:03:31 -07:00
|
|
|
StringRef BinaryData::getOutputSectionName() const {
|
|
|
|
|
return getOutputSection().getName();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint64_t BinaryData::getOutputAddress() const {
|
|
|
|
|
assert(OutputSection->getFileAddress());
|
|
|
|
|
return OutputSection->getFileAddress() + OutputOffset;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint64_t BinaryData::getOffset() const {
|
2017-11-14 20:05:11 -08:00
|
|
|
return Address - getSection().getAddress();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BinaryData::setSection(BinarySection &NewSection) {
|
2018-04-20 20:03:31 -07:00
|
|
|
if (OutputSection == Section)
|
|
|
|
|
OutputSection = &NewSection;
|
2017-11-14 20:05:11 -08:00
|
|
|
Section = &NewSection;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool BinaryData::isMoved() const {
|
2018-04-20 20:03:31 -07:00
|
|
|
return (getOffset() != OutputOffset || OutputSection != Section);
|
2017-11-14 20:05:11 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BinaryData::print(raw_ostream &OS) const {
|
|
|
|
|
printBrief(OS);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BinaryData::printBrief(raw_ostream &OS) const {
|
|
|
|
|
OS << "(";
|
|
|
|
|
|
|
|
|
|
if (isJumpTable())
|
|
|
|
|
OS << "jump-table: ";
|
|
|
|
|
else
|
|
|
|
|
OS << "object: ";
|
|
|
|
|
|
|
|
|
|
OS << getName();
|
|
|
|
|
|
|
|
|
|
if ((opts::PrintSymbolAliases || opts::Verbosity > 1) && Names.size() > 1) {
|
|
|
|
|
OS << ", aliases:";
|
|
|
|
|
for (unsigned I = 1u; I < Names.size(); ++I) {
|
|
|
|
|
OS << (I == 1 ? " (" : ", ") << Names[I];
|
|
|
|
|
}
|
|
|
|
|
OS << ")";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (opts::Verbosity > 1 && Parent) {
|
|
|
|
|
OS << " (" << Parent->getName() << "/" << Parent->getSize() << ")";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
OS << ", 0x" << Twine::utohexstr(getAddress())
|
|
|
|
|
<< ":0x" << Twine::utohexstr(getEndAddress())
|
2018-04-20 20:03:31 -07:00
|
|
|
<< "/" << getSize() << "/" << getAlignment()
|
|
|
|
|
<< "/0x" << Twine::utohexstr(Flags);
|
2017-11-14 20:05:11 -08:00
|
|
|
|
|
|
|
|
if (opts::Verbosity > 1) {
|
|
|
|
|
for (auto &MI : memData()) {
|
|
|
|
|
OS << ", " << MI;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
OS << ")";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
BinaryData::BinaryData(StringRef Name,
|
|
|
|
|
uint64_t Address,
|
|
|
|
|
uint64_t Size,
|
|
|
|
|
uint16_t Alignment,
|
2018-04-20 20:03:31 -07:00
|
|
|
BinarySection &Section,
|
|
|
|
|
unsigned Flags)
|
2017-11-14 20:05:11 -08:00
|
|
|
: Names({Name}),
|
|
|
|
|
Section(&Section),
|
|
|
|
|
Address(Address),
|
|
|
|
|
Size(Size),
|
|
|
|
|
Alignment(Alignment),
|
2018-04-20 20:03:31 -07:00
|
|
|
Flags(Flags),
|
|
|
|
|
OutputSection(&Section),
|
|
|
|
|
OutputOffset(getOffset())
|
2017-11-14 20:05:11 -08:00
|
|
|
{ }
|