Skip to content

Commit 429075e

Browse files
author
Alexander Yermolovich
committed
moved getRelocation* to Relocs.cpp
1 parent debeb6f commit 429075e

File tree

6 files changed

+73
-69
lines changed

6 files changed

+73
-69
lines changed

bolt/include/bolt/Core/BinaryContext.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -287,12 +287,6 @@ class BinaryContext {
287287
std::unique_ptr<DWARFContext> DwCtx,
288288
JournalingStreams Logger);
289289

290-
/// Returns addend of a relocation.
291-
static int64_t getRelocationAddend(const ELFObjectFileBase *Obj,
292-
const RelocationRef &Rel);
293-
/// Returns symbol of a relocation.
294-
static uint32_t getRelocationSymbol(const ELFObjectFileBase *Obj,
295-
const RelocationRef &Rel);
296290
/// Superset of compiler units that will contain overwritten code that needs
297291
/// new debug info. In a few cases, functions may end up not being
298292
/// overwritten, but it is okay to re-generate debug info for them.

bolt/include/bolt/Core/Relocation.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
#include "llvm/MC/MCExpr.h"
1818
#include "llvm/MC/MCStreamer.h"
19+
#include "llvm/Object/ELFObjectFile.h"
20+
#include "llvm/Object/ObjectFile.h"
1921
#include "llvm/TargetParser/Triple.h"
2022

2123
namespace llvm {
@@ -178,6 +180,11 @@ inline raw_ostream &operator<<(raw_ostream &OS, const Relocation &Rel) {
178180
return OS;
179181
}
180182

183+
uint32_t getRelocationSymbol(const object::ELFObjectFileBase *Obj,
184+
const object::RelocationRef &Rel);
185+
186+
int64_t getRelocationAddend(const object::ELFObjectFileBase *Obj,
187+
const object::RelocationRef &Rel);
181188
} // namespace bolt
182189
} // namespace llvm
183190

bolt/lib/Core/BinaryContext.cpp

Lines changed: 0 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -90,54 +90,6 @@ cl::opt<bolt::BinaryContext::ICFLevel>
9090
cl::ZeroOrMore, cl::ValueOptional, cl::cat(BoltOptCategory));
9191
} // namespace opts
9292

93-
namespace {
94-
template <typename ELFT>
95-
int64_t getRelocationAddend(const ELFObjectFile<ELFT> *Obj,
96-
const RelocationRef &RelRef) {
97-
using ELFShdrTy = typename ELFT::Shdr;
98-
using Elf_Rela = typename ELFT::Rela;
99-
int64_t Addend = 0;
100-
const ELFFile<ELFT> &EF = Obj->getELFFile();
101-
DataRefImpl Rel = RelRef.getRawDataRefImpl();
102-
const ELFShdrTy *RelocationSection = cantFail(EF.getSection(Rel.d.a));
103-
switch (RelocationSection->sh_type) {
104-
default:
105-
llvm_unreachable("unexpected relocation section type");
106-
case ELF::SHT_REL:
107-
break;
108-
case ELF::SHT_RELA: {
109-
const Elf_Rela *RelA = Obj->getRela(Rel);
110-
Addend = RelA->r_addend;
111-
break;
112-
}
113-
}
114-
115-
return Addend;
116-
}
117-
118-
template <typename ELFT>
119-
uint32_t getRelocationSymbol(const ELFObjectFile<ELFT> *Obj,
120-
const RelocationRef &RelRef) {
121-
using ELFShdrTy = typename ELFT::Shdr;
122-
uint32_t Symbol = 0;
123-
const ELFFile<ELFT> &EF = Obj->getELFFile();
124-
DataRefImpl Rel = RelRef.getRawDataRefImpl();
125-
const ELFShdrTy *RelocationSection = cantFail(EF.getSection(Rel.d.a));
126-
switch (RelocationSection->sh_type) {
127-
default:
128-
llvm_unreachable("unexpected relocation section type");
129-
case ELF::SHT_REL:
130-
Symbol = Obj->getRel(Rel)->getSymbol(EF.isMips64EL());
131-
break;
132-
case ELF::SHT_RELA:
133-
Symbol = Obj->getRela(Rel)->getSymbol(EF.isMips64EL());
134-
break;
135-
}
136-
137-
return Symbol;
138-
}
139-
} // anonymous namespace
140-
14193
namespace llvm {
14294
namespace bolt {
14395

@@ -218,16 +170,6 @@ BinaryContext::~BinaryContext() {
218170
clearBinaryData();
219171
}
220172

221-
uint32_t BinaryContext::getRelocationSymbol(const ELFObjectFileBase *Obj,
222-
const RelocationRef &Rel) {
223-
return ::getRelocationSymbol(cast<ELF64LEObjectFile>(Obj), Rel);
224-
}
225-
226-
int64_t BinaryContext::getRelocationAddend(const ELFObjectFileBase *Obj,
227-
const RelocationRef &Rel) {
228-
return ::getRelocationAddend(cast<ELF64LEObjectFile>(Obj), Rel);
229-
}
230-
231173
/// Create BinaryContext for a given architecture \p ArchName and
232174
/// triple \p TripleName.
233175
Expected<std::unique_ptr<BinaryContext>> BinaryContext::createBinaryContext(

bolt/lib/Core/Relocation.cpp

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1115,3 +1115,65 @@ void Relocation::print(raw_ostream &OS) const {
11151115
OS << ", 0x" << Twine::utohexstr(Addend);
11161116
OS << ", 0x" << Twine::utohexstr(Value);
11171117
}
1118+
1119+
namespace {
1120+
template <typename ELFT>
1121+
int64_t getRelocationAddend(const llvm::object::ELFObjectFile<ELFT> *Obj,
1122+
const llvm::object::RelocationRef &RelRef) {
1123+
using ELFShdrTy = typename ELFT::Shdr;
1124+
using Elf_Rela = typename ELFT::Rela;
1125+
int64_t Addend = 0;
1126+
const llvm::object::ELFFile<ELFT> &EF = Obj->getELFFile();
1127+
llvm::object::DataRefImpl Rel = RelRef.getRawDataRefImpl();
1128+
const ELFShdrTy *RelocationSection = cantFail(EF.getSection(Rel.d.a));
1129+
switch (RelocationSection->sh_type) {
1130+
default:
1131+
llvm_unreachable("unexpected relocation section type");
1132+
case ELF::SHT_REL:
1133+
break;
1134+
case ELF::SHT_RELA: {
1135+
const Elf_Rela *RelA = Obj->getRela(Rel);
1136+
Addend = RelA->r_addend;
1137+
break;
1138+
}
1139+
}
1140+
1141+
return Addend;
1142+
}
1143+
1144+
template <typename ELFT>
1145+
uint32_t getRelocationSymbol(const llvm::object::ELFObjectFile<ELFT> *Obj,
1146+
const llvm::object::RelocationRef &RelRef) {
1147+
using ELFShdrTy = typename ELFT::Shdr;
1148+
uint32_t Symbol = 0;
1149+
const llvm::object::ELFFile<ELFT> &EF = Obj->getELFFile();
1150+
llvm::object::DataRefImpl Rel = RelRef.getRawDataRefImpl();
1151+
const ELFShdrTy *RelocationSection = cantFail(EF.getSection(Rel.d.a));
1152+
switch (RelocationSection->sh_type) {
1153+
default:
1154+
llvm_unreachable("unexpected relocation section type");
1155+
case ELF::SHT_REL:
1156+
Symbol = Obj->getRel(Rel)->getSymbol(EF.isMips64EL());
1157+
break;
1158+
case ELF::SHT_RELA:
1159+
Symbol = Obj->getRela(Rel)->getSymbol(EF.isMips64EL());
1160+
break;
1161+
}
1162+
1163+
return Symbol;
1164+
}
1165+
} // namespace
1166+
1167+
namespace llvm {
1168+
namespace bolt {
1169+
uint32_t getRelocationSymbol(const llvm::object::ELFObjectFileBase *Obj,
1170+
const llvm::object::RelocationRef &Rel) {
1171+
return ::getRelocationSymbol(cast<llvm::object::ELF64LEObjectFile>(Obj), Rel);
1172+
}
1173+
1174+
int64_t getRelocationAddend(const llvm::object::ELFObjectFileBase *Obj,
1175+
const llvm::object::RelocationRef &Rel) {
1176+
return ::getRelocationAddend(cast<llvm::object::ELF64LEObjectFile>(Obj), Rel);
1177+
}
1178+
} // namespace bolt
1179+
} // namespace llvm

bolt/lib/Passes/IdenticalCodeFolding.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
#include "bolt/Passes/IdenticalCodeFolding.h"
14-
#include "bolt/Core/BinaryContext.h"
1514
#include "bolt/Core/HashUtilities.h"
1615
#include "bolt/Core/ParallelUtilities.h"
1716
#include "bolt/Rewrite/RewriteInstance.h"
@@ -356,7 +355,7 @@ void IdenticalCodeFolding::processDataRelocations(
356355
const ELFObjectFileBase *ELFObj = dyn_cast<ELFObjectFileBase>(OwningObj);
357356
if (!ELFObj)
358357
llvm_unreachable("Only ELFObjectFileBase is supported");
359-
const int64_t Addend = BinaryContext::getRelocationAddend(ELFObj, Rel);
358+
const int64_t Addend = getRelocationAddend(ELFObj, Rel);
360359
BinaryFunction *BF = BC.getBinaryFunctionAtAddress(SymbolAddress + Addend);
361360
if (!BF)
362361
continue;

bolt/lib/Rewrite/RewriteInstance.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2138,7 +2138,7 @@ bool RewriteInstance::analyzeRelocation(
21382138
return true;
21392139

21402140
ExtractedValue = Relocation::extractValue(RType, *Value, Rel.getOffset());
2141-
Addend = BinaryContext::getRelocationAddend(InputFile, Rel);
2141+
Addend = getRelocationAddend(InputFile, Rel);
21422142

21432143
const bool IsPCRelative = Relocation::isPCRelative(RType);
21442144
const uint64_t PCRelOffset = IsPCRelative && !IsAArch64 ? Rel.getOffset() : 0;
@@ -2338,7 +2338,7 @@ void RewriteInstance::readDynamicRelocations(const SectionRef &Section,
23382338
StringRef SymbolName = "<none>";
23392339
MCSymbol *Symbol = nullptr;
23402340
uint64_t SymbolAddress = 0;
2341-
const uint64_t Addend = BinaryContext::getRelocationAddend(InputFile, Rel);
2341+
const uint64_t Addend = getRelocationAddend(InputFile, Rel);
23422342

23432343
symbol_iterator SymbolIter = Rel.getSymbol();
23442344
if (SymbolIter != InputFile->symbol_end()) {
@@ -2363,7 +2363,7 @@ void RewriteInstance::readDynamicRelocations(const SectionRef &Section,
23632363
IsJmpRelocation[RType] = true;
23642364

23652365
if (Symbol)
2366-
SymbolIndex[Symbol] = BinaryContext::getRelocationSymbol(InputFile, Rel);
2366+
SymbolIndex[Symbol] = getRelocationSymbol(InputFile, Rel);
23672367

23682368
BC->addDynamicRelocation(Rel.getOffset(), Symbol, RType, Addend);
23692369
}

0 commit comments

Comments
 (0)