Skip to content

Commit 2f9d949

Browse files
[BOLT] Change Relocation Type to 32-bit NFCI (#130792)
1 parent e9fc768 commit 2f9d949

15 files changed

+118
-102
lines changed

bolt/include/bolt/Core/BinaryContext.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1295,7 +1295,7 @@ class BinaryContext {
12951295
void foldFunction(BinaryFunction &ChildBF, BinaryFunction &ParentBF);
12961296

12971297
/// Add a Section relocation at a given \p Address.
1298-
void addRelocation(uint64_t Address, MCSymbol *Symbol, uint64_t Type,
1298+
void addRelocation(uint64_t Address, MCSymbol *Symbol, uint32_t Type,
12991299
uint64_t Addend = 0, uint64_t Value = 0);
13001300

13011301
/// Return a relocation registered at a given \p Address, or nullptr if there
@@ -1308,7 +1308,7 @@ class BinaryContext {
13081308
}
13091309

13101310
/// Register dynamic relocation at \p Address.
1311-
void addDynamicRelocation(uint64_t Address, MCSymbol *Symbol, uint64_t Type,
1311+
void addDynamicRelocation(uint64_t Address, MCSymbol *Symbol, uint32_t Type,
13121312
uint64_t Addend, uint64_t Value = 0);
13131313

13141314
/// Return a dynamic relocation registered at a given \p Address, or nullptr

bolt/include/bolt/Core/BinaryFunction.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1260,7 +1260,7 @@ class BinaryFunction {
12601260
/// Register relocation type \p RelType at a given \p Address in the function
12611261
/// against \p Symbol.
12621262
/// Assert if the \p Address is not inside this function.
1263-
void addRelocation(uint64_t Address, MCSymbol *Symbol, uint64_t RelType,
1263+
void addRelocation(uint64_t Address, MCSymbol *Symbol, uint32_t RelType,
12641264
uint64_t Addend, uint64_t Value);
12651265

12661266
/// Return the name of the section this function originated from.

bolt/include/bolt/Core/BinarySection.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -358,14 +358,14 @@ class BinarySection {
358358
void clearRelocations();
359359

360360
/// Add a new relocation at the given /p Offset.
361-
void addRelocation(uint64_t Offset, MCSymbol *Symbol, uint64_t Type,
361+
void addRelocation(uint64_t Offset, MCSymbol *Symbol, uint32_t Type,
362362
uint64_t Addend, uint64_t Value = 0) {
363363
assert(Offset < getSize() && "offset not within section bounds");
364364
Relocations.emplace(Relocation{Offset, Symbol, Type, Addend, Value});
365365
}
366366

367367
/// Add a dynamic relocation at the given /p Offset.
368-
void addDynamicRelocation(uint64_t Offset, MCSymbol *Symbol, uint64_t Type,
368+
void addDynamicRelocation(uint64_t Offset, MCSymbol *Symbol, uint32_t Type,
369369
uint64_t Addend, uint64_t Value = 0) {
370370
addDynamicRelocation(Relocation{Offset, Symbol, Type, Addend, Value});
371371
}

bolt/include/bolt/Core/MCPlusBuilder.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,7 @@ class MCPlusBuilder {
473473
///
474474
/// For X86, they might be used in scanExternalRefs when we want to skip
475475
/// a function but still patch references inside it.
476-
virtual bool shouldRecordCodeRelocation(uint64_t RelType) const {
476+
virtual bool shouldRecordCodeRelocation(uint32_t RelType) const {
477477
llvm_unreachable("not implemented");
478478
return false;
479479
}
@@ -1074,15 +1074,15 @@ class MCPlusBuilder {
10741074
/// MCExpr referencing \p Symbol + \p Addend.
10751075
virtual bool setOperandToSymbolRef(MCInst &Inst, int OpNum,
10761076
const MCSymbol *Symbol, int64_t Addend,
1077-
MCContext *Ctx, uint64_t RelType) const;
1077+
MCContext *Ctx, uint32_t RelType) const;
10781078

10791079
/// Replace an immediate operand in the instruction \p Inst with a reference
10801080
/// of the passed \p Symbol plus \p Addend. If the instruction does not have
10811081
/// an immediate operand or has more than one - then return false. Otherwise
10821082
/// return true.
10831083
virtual bool replaceImmWithSymbolRef(MCInst &Inst, const MCSymbol *Symbol,
10841084
int64_t Addend, MCContext *Ctx,
1085-
int64_t &Value, uint64_t RelType) const {
1085+
int64_t &Value, uint32_t RelType) const {
10861086
llvm_unreachable("not implemented");
10871087
return false;
10881088
}
@@ -1287,7 +1287,7 @@ class MCPlusBuilder {
12871287
/// Return the MCExpr used for absolute references in this target
12881288
virtual const MCExpr *getTargetExprFor(MCInst &Inst, const MCExpr *Expr,
12891289
MCContext &Ctx,
1290-
uint64_t RelType) const {
1290+
uint32_t RelType) const {
12911291
return Expr;
12921292
}
12931293

bolt/include/bolt/Core/Relocation.h

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@
2020

2121
namespace llvm {
2222
class MCSymbol;
23+
24+
namespace object {
25+
class RelocationRef;
26+
} // namespace object
27+
2328
class raw_ostream;
2429

2530
namespace ELF {
@@ -40,7 +45,7 @@ struct Relocation {
4045
MCSymbol *Symbol;
4146

4247
/// Relocation type.
43-
uint64_t Type;
48+
uint32_t Type;
4449

4550
/// The offset from the \p Symbol base used to compute the final
4651
/// value of this relocation.
@@ -51,71 +56,75 @@ struct Relocation {
5156
uint64_t Value;
5257

5358
/// Return size in bytes of the given relocation \p Type.
54-
static size_t getSizeForType(uint64_t Type);
59+
static size_t getSizeForType(uint32_t Type);
5560

5661
/// Return size of this relocation.
5762
size_t getSize() const { return getSizeForType(Type); }
5863

5964
/// Skip relocations that we don't want to handle in BOLT
60-
static bool skipRelocationType(uint64_t Type);
65+
static bool skipRelocationType(uint32_t Type);
6166

6267
/// Handle special cases when relocation should not be processed by BOLT or
6368
/// change relocation \p Type to proper one before continuing if \p Contents
6469
/// and \P Type mismatch occurred.
65-
static bool skipRelocationProcess(uint64_t &Type, uint64_t Contents);
70+
static bool skipRelocationProcess(uint32_t &Type, uint64_t Contents);
6671

67-
// Adjust value depending on relocation type (make it PC relative or not)
68-
static uint64_t encodeValue(uint64_t Type, uint64_t Value, uint64_t PC);
72+
/// Adjust value depending on relocation type (make it PC relative or not).
73+
static uint64_t encodeValue(uint32_t Type, uint64_t Value, uint64_t PC);
6974

7075
/// Extract current relocated value from binary contents. This is used for
7176
/// RISC architectures where values are encoded in specific bits depending
7277
/// on the relocation value. For X86, we limit to sign extending the value
7378
/// if necessary.
74-
static uint64_t extractValue(uint64_t Type, uint64_t Contents, uint64_t PC);
79+
static uint64_t extractValue(uint32_t Type, uint64_t Contents, uint64_t PC);
7580

7681
/// Return true if relocation type is PC-relative. Return false otherwise.
77-
static bool isPCRelative(uint64_t Type);
82+
static bool isPCRelative(uint32_t Type);
7883

7984
/// Check if \p Type is a supported relocation type.
80-
static bool isSupported(uint64_t Type);
85+
static bool isSupported(uint32_t Type);
8186

8287
/// Return true if relocation type implies the creation of a GOT entry
83-
static bool isGOT(uint64_t Type);
88+
static bool isGOT(uint32_t Type);
8489

8590
/// Special relocation type that allows the linker to modify the instruction.
86-
static bool isX86GOTPCRELX(uint64_t Type);
87-
static bool isX86GOTPC64(uint64_t Type);
91+
static bool isX86GOTPCRELX(uint32_t Type);
92+
static bool isX86GOTPC64(uint32_t Type);
8893

8994
/// Return true if relocation type is NONE
90-
static bool isNone(uint64_t Type);
95+
static bool isNone(uint32_t Type);
9196

9297
/// Return true if relocation type is RELATIVE
93-
static bool isRelative(uint64_t Type);
98+
static bool isRelative(uint32_t Type);
9499

95100
/// Return true if relocation type is IRELATIVE
96-
static bool isIRelative(uint64_t Type);
101+
static bool isIRelative(uint32_t Type);
97102

98103
/// Return true if relocation type is for thread local storage.
99-
static bool isTLS(uint64_t Type);
104+
static bool isTLS(uint32_t Type);
100105

101106
/// Return true of relocation type is for referencing a specific instruction
102107
/// (as opposed to a function, basic block, etc).
103-
static bool isInstructionReference(uint64_t Type);
108+
static bool isInstructionReference(uint32_t Type);
109+
110+
/// Return the relocation type of \p Rel from llvm::object. It checks for
111+
/// overflows as BOLT uses 32 bits for the type.
112+
static uint32_t getType(const object::RelocationRef &Rel);
104113

105114
/// Return code for a NONE relocation
106-
static uint64_t getNone();
115+
static uint32_t getNone();
107116

108117
/// Return code for a PC-relative 4-byte relocation
109-
static uint64_t getPC32();
118+
static uint32_t getPC32();
110119

111120
/// Return code for a PC-relative 8-byte relocation
112-
static uint64_t getPC64();
121+
static uint32_t getPC64();
113122

114123
/// Return code for a ABS 8-byte relocation
115-
static uint64_t getAbs64();
124+
static uint32_t getAbs64();
116125

117126
/// Return code for a RELATIVE relocation
118-
static uint64_t getRelative();
127+
static uint32_t getRelative();
119128

120129
/// Return true if this relocation is PC-relative. Return false otherwise.
121130
bool isPCRelative() const { return isPCRelative(Type); }
@@ -161,7 +170,7 @@ struct Relocation {
161170
const MCExpr *createExpr(MCStreamer *Streamer) const;
162171
const MCExpr *createExpr(MCStreamer *Streamer,
163172
const MCExpr *RetainedValue) const;
164-
static MCBinaryExpr::Opcode getComposeOpcodeFor(uint64_t Type);
173+
static MCBinaryExpr::Opcode getComposeOpcodeFor(uint32_t Type);
165174
};
166175

167176
/// Relocation ordering by offset.

bolt/include/bolt/Rewrite/RewriteInstance.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ class RewriteInstance {
248248
/// The \p SymbolName, \p SymbolAddress, \p Addend and \p ExtractedValue
249249
/// parameters will be set on success. The \p Skip argument indicates
250250
/// that the relocation was analyzed, but it must not be processed.
251-
bool analyzeRelocation(const object::RelocationRef &Rel, uint64_t &RType,
251+
bool analyzeRelocation(const object::RelocationRef &Rel, uint32_t &RType,
252252
std::string &SymbolName, bool &IsSectionRelocation,
253253
uint64_t &SymbolAddress, int64_t &Addend,
254254
uint64_t &ExtractedValue, bool &Skip) const;

bolt/lib/Core/BinaryContext.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2284,7 +2284,7 @@ ErrorOr<int64_t> BinaryContext::getSignedValueAtAddress(uint64_t Address,
22842284
}
22852285

22862286
void BinaryContext::addRelocation(uint64_t Address, MCSymbol *Symbol,
2287-
uint64_t Type, uint64_t Addend,
2287+
uint32_t Type, uint64_t Addend,
22882288
uint64_t Value) {
22892289
ErrorOr<BinarySection &> Section = getSectionForAddress(Address);
22902290
assert(Section && "cannot find section for address");
@@ -2293,7 +2293,7 @@ void BinaryContext::addRelocation(uint64_t Address, MCSymbol *Symbol,
22932293
}
22942294

22952295
void BinaryContext::addDynamicRelocation(uint64_t Address, MCSymbol *Symbol,
2296-
uint64_t Type, uint64_t Addend,
2296+
uint32_t Type, uint64_t Addend,
22972297
uint64_t Value) {
22982298
ErrorOr<BinarySection &> Section = getSectionForAddress(Address);
22992299
assert(Section && "cannot find section for address");

bolt/lib/Core/BinaryFunction.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4614,7 +4614,7 @@ bool BinaryFunction::isAArch64Veneer() const {
46144614
}
46154615

46164616
void BinaryFunction::addRelocation(uint64_t Address, MCSymbol *Symbol,
4617-
uint64_t RelType, uint64_t Addend,
4617+
uint32_t RelType, uint64_t Addend,
46184618
uint64_t Value) {
46194619
assert(Address >= getAddress() && Address < getAddress() + getMaxSize() &&
46204620
"address is outside of the function");

bolt/lib/Core/JumpTable.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ void bolt::JumpTable::updateOriginal() {
8484
const uint64_t BaseOffset = getAddress() - getSection().getAddress();
8585
uint64_t EntryOffset = BaseOffset;
8686
for (MCSymbol *Entry : Entries) {
87-
const uint64_t RelType =
87+
const uint32_t RelType =
8888
Type == JTT_NORMAL ? ELF::R_X86_64_64 : ELF::R_X86_64_PC32;
8989
const uint64_t RelAddend =
9090
Type == JTT_NORMAL ? 0 : EntryOffset - BaseOffset;

bolt/lib/Core/MCPlusBuilder.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,7 @@ void MCPlusBuilder::initSizeMap() {
551551
bool MCPlusBuilder::setOperandToSymbolRef(MCInst &Inst, int OpNum,
552552
const MCSymbol *Symbol,
553553
int64_t Addend, MCContext *Ctx,
554-
uint64_t RelType) const {
554+
uint32_t RelType) const {
555555
MCOperand Operand;
556556
if (!Addend) {
557557
Operand = MCOperand::createExpr(getTargetExprFor(

0 commit comments

Comments
 (0)