forked from llvm/llvm-project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinaryData.cpp
139 lines (113 loc) · 3.87 KB
/
BinaryData.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
//===- bolt/Core/BinaryData.cpp - Objects in a binary file ----------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
//
// This file implements the BinaryData class.
//
//===----------------------------------------------------------------------===//
#include "bolt/Core/BinaryData.h"
#include "bolt/Core/BinarySection.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Regex.h"
using namespace llvm;
using namespace bolt;
#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));
}
bool BinaryData::isAbsolute() const { return Flags & SymbolRef::SF_Absolute; }
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);
Symbols.insert(Symbols.end(), Other->Symbols.begin(), Other->Symbols.end());
Flags |= Other->Flags;
if (!Size)
Size = Other->Size;
}
bool BinaryData::hasName(StringRef Name) const {
for (const MCSymbol *Symbol : Symbols)
if (Name == Symbol->getName())
return true;
return false;
}
bool BinaryData::hasNameRegex(StringRef NameRegex) const {
Regex MatchName(NameRegex);
for (const MCSymbol *Symbol : Symbols)
if (MatchName.match(Symbol->getName()))
return true;
return false;
}
bool BinaryData::nameStartsWith(StringRef Prefix) const {
for (const MCSymbol *Symbol : Symbols)
if (Symbol->getName().startswith(Prefix))
return true;
return false;
}
StringRef BinaryData::getSectionName() const { return getSection().getName(); }
StringRef BinaryData::getOutputSectionName() const {
return getOutputSection().getName();
}
uint64_t BinaryData::getOutputAddress() const {
assert(OutputSection->getOutputAddress());
return OutputSection->getOutputAddress() + OutputOffset;
}
uint64_t BinaryData::getOffset() const {
return Address - getSection().getAddress();
}
void BinaryData::setSection(BinarySection &NewSection) {
if (OutputSection == Section)
OutputSection = &NewSection;
Section = &NewSection;
}
bool BinaryData::isMoved() const {
return (getOffset() != OutputOffset || OutputSection != Section);
}
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) && Symbols.size() > 1) {
OS << ", aliases:";
for (unsigned I = 1u; I < Symbols.size(); ++I) {
OS << (I == 1 ? " (" : ", ") << Symbols[I]->getName();
}
OS << ")";
}
if (Parent) {
OS << " (parent: ";
Parent->printBrief(OS);
OS << ")";
}
OS << ", 0x" << Twine::utohexstr(getAddress()) << ":0x"
<< Twine::utohexstr(getEndAddress()) << "/" << getSize() << "/"
<< getAlignment() << "/0x" << Twine::utohexstr(Flags);
OS << ")";
}
BinaryData::BinaryData(MCSymbol &Symbol, uint64_t Address, uint64_t Size,
uint16_t Alignment, BinarySection &Section,
unsigned Flags)
: Section(&Section), Address(Address), Size(Size), Alignment(Alignment),
Flags(Flags), OutputSection(&Section), OutputOffset(getOffset()) {
Symbols.push_back(&Symbol);
}