Skip to content

Commit 228970f

Browse files
author
spupyrev
committed
Revert "Rebase: [Facebook] Revert "[BOLT] Update dynamic relocations from section relocations""
This reverts commit 76029cc.
1 parent eecd41a commit 228970f

File tree

8 files changed

+925
-41
lines changed

8 files changed

+925
-41
lines changed

bolt/include/bolt/Core/BinarySection.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,16 @@ class BinarySection {
296296
return make_range(Relocations.begin(), Relocations.end());
297297
}
298298

299+
/// Iterate over all dynamic relocations for this section.
300+
iterator_range<RelocationSetType::iterator> dynamicRelocations() {
301+
return make_range(DynamicRelocations.begin(), DynamicRelocations.end());
302+
}
303+
304+
/// Iterate over all dynamic relocations for this section.
305+
iterator_range<RelocationSetType::const_iterator> dynamicRelocations() const {
306+
return make_range(DynamicRelocations.begin(), DynamicRelocations.end());
307+
}
308+
299309
/// Does this section have any non-pending relocations?
300310
bool hasRelocations() const { return !Relocations.empty(); }
301311

bolt/include/bolt/Core/Relocation.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,9 @@ struct Relocation {
9595
/// Return true if relocation type is for thread local storage.
9696
static bool isTLS(uint64_t Type);
9797

98+
/// Return code for a NONE relocation
99+
static uint64_t getNone();
100+
98101
/// Return code for a PC-relative 4-byte relocation
99102
static uint64_t getPC32();
100103

@@ -104,6 +107,10 @@ struct Relocation {
104107
/// Return true if this relocation is PC-relative. Return false otherwise.
105108
bool isPCRelative() const { return isPCRelative(Type); }
106109

110+
/// Return true if this relocation is R_*_RELATIVE type. Return false
111+
/// otherwise.
112+
bool isRelative() const { return isRelative(Type); }
113+
107114
/// Emit relocation at a current \p Streamer' position. The caller is
108115
/// responsible for setting the position correctly.
109116
size_t emit(MCStreamer *Streamer) const;

bolt/include/bolt/Rewrite/RewriteInstance.h

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "llvm/Support/Error.h"
2323
#include <map>
2424
#include <set>
25+
#include <unordered_map>
2526

2627
namespace llvm {
2728

@@ -131,7 +132,7 @@ class RewriteInstance {
131132
void processLKSMPLocks();
132133

133134
/// Read relocations from a given section.
134-
void readDynamicRelocations(const object::SectionRef &Section);
135+
void readDynamicRelocations(const object::SectionRef &Section, bool IsJmpRel);
135136

136137
/// Read relocations from a given section.
137138
void readRelocations(const object::SectionRef &Section);
@@ -208,6 +209,10 @@ class RewriteInstance {
208209
/// \p OldAddress address in the original binary.
209210
uint64_t getNewFunctionAddress(uint64_t OldAddress);
210211

212+
/// Return address of a function or moved data in the new binary
213+
/// corresponding to \p OldAddress address in the original binary.
214+
uint64_t getNewFunctionOrDataAddress(uint64_t OldAddress);
215+
211216
/// Return value for the symbol \p Name in the output.
212217
uint64_t getNewValueForSymbol(const StringRef Name);
213218

@@ -319,6 +324,14 @@ class RewriteInstance {
319324
const std::vector<uint32_t> &NewSectionIndex, WriteFuncTy Write,
320325
StrTabFuncTy AddToStrTab);
321326

327+
/// Get output index in dynamic symbol table.
328+
uint32_t getOutputDynamicSymbolIndex(const MCSymbol *Symbol) {
329+
auto It = SymbolIndex.find(Symbol);
330+
if (It != SymbolIndex.end())
331+
return It->second;
332+
return 0;
333+
}
334+
322335
/// Add a notes section containing the BOLT revision and command line options.
323336
void addBoltInfoSection();
324337

@@ -446,11 +459,19 @@ class RewriteInstance {
446459
/// Location and size of dynamic relocations.
447460
Optional<uint64_t> DynamicRelocationsAddress;
448461
uint64_t DynamicRelocationsSize{0};
462+
uint64_t DynamicRelativeRelocationsCount{0};
449463

450464
/// PLT relocations are special kind of dynamic relocations stored separately.
451465
Optional<uint64_t> PLTRelocationsAddress;
452466
uint64_t PLTRelocationsSize{0};
453467

468+
/// True if relocation of specified type came from .rela.plt
469+
DenseMap<uint64_t, bool> IsJmpRelocation;
470+
471+
/// Index of specified symbol in the dynamic symbol table. NOTE Currently it
472+
/// is filled and used only with the relocations-related symbols.
473+
std::unordered_map<const MCSymbol *, uint32_t> SymbolIndex;
474+
454475
/// Store all non-zero symbols in this map for a quick address lookup.
455476
std::map<uint64_t, llvm::object::SymbolRef> FileSymRefs;
456477

bolt/lib/Core/Relocation.cpp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -580,11 +580,7 @@ bool Relocation::isX86GOTPCRELX(uint64_t Type) {
580580
return Type == ELF::R_X86_64_GOTPCRELX || Type == ELF::R_X86_64_REX_GOTPCRELX;
581581
}
582582

583-
bool Relocation::isNone(uint64_t Type) {
584-
if (Arch == Triple::aarch64)
585-
return Type == ELF::R_AARCH64_NONE;
586-
return Type == ELF::R_X86_64_NONE;
587-
}
583+
bool Relocation::isNone(uint64_t Type) { return Type == getNone(); }
588584

589585
bool Relocation::isRelative(uint64_t Type) {
590586
if (Arch == Triple::aarch64)
@@ -604,10 +600,10 @@ bool Relocation::isTLS(uint64_t Type) {
604600
return isTLSX86(Type);
605601
}
606602

607-
bool Relocation::isPCRelative(uint64_t Type) {
603+
uint64_t Relocation::getNone() {
608604
if (Arch == Triple::aarch64)
609-
return isPCRelativeAArch64(Type);
610-
return isPCRelativeX86(Type);
605+
return ELF::R_AARCH64_NONE;
606+
return ELF::R_X86_64_NONE;
611607
}
612608

613609
uint64_t Relocation::getPC32() {
@@ -622,6 +618,12 @@ uint64_t Relocation::getPC64() {
622618
return ELF::R_X86_64_PC64;
623619
}
624620

621+
bool Relocation::isPCRelative(uint64_t Type) {
622+
if (Arch == Triple::aarch64)
623+
return isPCRelativeAArch64(Type);
624+
return isPCRelativeX86(Type);
625+
}
626+
625627
size_t Relocation::emit(MCStreamer *Streamer) const {
626628
const size_t Size = getSizeForType(Type);
627629
MCContext &Ctx = Streamer->getContext();

0 commit comments

Comments
 (0)