Skip to content

[BOLT] Change Relocation Type to 32-bit NFCI #130792

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions bolt/include/bolt/Core/BinaryContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -1295,7 +1295,7 @@ class BinaryContext {
void foldFunction(BinaryFunction &ChildBF, BinaryFunction &ParentBF);

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

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

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

/// Return a dynamic relocation registered at a given \p Address, or nullptr
Expand Down
2 changes: 1 addition & 1 deletion bolt/include/bolt/Core/BinaryFunction.h
Original file line number Diff line number Diff line change
Expand Up @@ -1260,7 +1260,7 @@ class BinaryFunction {
/// Register relocation type \p RelType at a given \p Address in the function
/// against \p Symbol.
/// Assert if the \p Address is not inside this function.
void addRelocation(uint64_t Address, MCSymbol *Symbol, uint64_t RelType,
void addRelocation(uint64_t Address, MCSymbol *Symbol, uint32_t RelType,
uint64_t Addend, uint64_t Value);

/// Return the name of the section this function originated from.
Expand Down
4 changes: 2 additions & 2 deletions bolt/include/bolt/Core/BinarySection.h
Original file line number Diff line number Diff line change
Expand Up @@ -358,14 +358,14 @@ class BinarySection {
void clearRelocations();

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

/// Add a dynamic relocation at the given /p Offset.
void addDynamicRelocation(uint64_t Offset, MCSymbol *Symbol, uint64_t Type,
void addDynamicRelocation(uint64_t Offset, MCSymbol *Symbol, uint32_t Type,
uint64_t Addend, uint64_t Value = 0) {
addDynamicRelocation(Relocation{Offset, Symbol, Type, Addend, Value});
}
Expand Down
8 changes: 4 additions & 4 deletions bolt/include/bolt/Core/MCPlusBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ class MCPlusBuilder {
///
/// For X86, they might be used in scanExternalRefs when we want to skip
/// a function but still patch references inside it.
virtual bool shouldRecordCodeRelocation(uint64_t RelType) const {
virtual bool shouldRecordCodeRelocation(uint32_t RelType) const {
llvm_unreachable("not implemented");
return false;
}
Expand Down Expand Up @@ -1074,15 +1074,15 @@ class MCPlusBuilder {
/// MCExpr referencing \p Symbol + \p Addend.
virtual bool setOperandToSymbolRef(MCInst &Inst, int OpNum,
const MCSymbol *Symbol, int64_t Addend,
MCContext *Ctx, uint64_t RelType) const;
MCContext *Ctx, uint32_t RelType) const;

/// Replace an immediate operand in the instruction \p Inst with a reference
/// of the passed \p Symbol plus \p Addend. If the instruction does not have
/// an immediate operand or has more than one - then return false. Otherwise
/// return true.
virtual bool replaceImmWithSymbolRef(MCInst &Inst, const MCSymbol *Symbol,
int64_t Addend, MCContext *Ctx,
int64_t &Value, uint64_t RelType) const {
int64_t &Value, uint32_t RelType) const {
llvm_unreachable("not implemented");
return false;
}
Expand Down Expand Up @@ -1287,7 +1287,7 @@ class MCPlusBuilder {
/// Return the MCExpr used for absolute references in this target
virtual const MCExpr *getTargetExprFor(MCInst &Inst, const MCExpr *Expr,
MCContext &Ctx,
uint64_t RelType) const {
uint32_t RelType) const {
return Expr;
}

Expand Down
55 changes: 32 additions & 23 deletions bolt/include/bolt/Core/Relocation.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@

namespace llvm {
class MCSymbol;

namespace object {
class RelocationRef;
} // namespace object

class raw_ostream;

namespace ELF {
Expand All @@ -40,7 +45,7 @@ struct Relocation {
MCSymbol *Symbol;

/// Relocation type.
uint64_t Type;
uint32_t Type;

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

/// Return size in bytes of the given relocation \p Type.
static size_t getSizeForType(uint64_t Type);
static size_t getSizeForType(uint32_t Type);

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

/// Skip relocations that we don't want to handle in BOLT
static bool skipRelocationType(uint64_t Type);
static bool skipRelocationType(uint32_t Type);

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

// Adjust value depending on relocation type (make it PC relative or not)
static uint64_t encodeValue(uint64_t Type, uint64_t Value, uint64_t PC);
/// Adjust value depending on relocation type (make it PC relative or not).
static uint64_t encodeValue(uint32_t Type, uint64_t Value, uint64_t PC);

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

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

/// Check if \p Type is a supported relocation type.
static bool isSupported(uint64_t Type);
static bool isSupported(uint32_t Type);

/// Return true if relocation type implies the creation of a GOT entry
static bool isGOT(uint64_t Type);
static bool isGOT(uint32_t Type);

/// Special relocation type that allows the linker to modify the instruction.
static bool isX86GOTPCRELX(uint64_t Type);
static bool isX86GOTPC64(uint64_t Type);
static bool isX86GOTPCRELX(uint32_t Type);
static bool isX86GOTPC64(uint32_t Type);

/// Return true if relocation type is NONE
static bool isNone(uint64_t Type);
static bool isNone(uint32_t Type);

/// Return true if relocation type is RELATIVE
static bool isRelative(uint64_t Type);
static bool isRelative(uint32_t Type);

/// Return true if relocation type is IRELATIVE
static bool isIRelative(uint64_t Type);
static bool isIRelative(uint32_t Type);

/// Return true if relocation type is for thread local storage.
static bool isTLS(uint64_t Type);
static bool isTLS(uint32_t Type);

/// Return true of relocation type is for referencing a specific instruction
/// (as opposed to a function, basic block, etc).
static bool isInstructionReference(uint64_t Type);
static bool isInstructionReference(uint32_t Type);

/// Return the relocation type of \p Rel from llvm::object. It checks for
/// overflows as BOLT uses 32 bits for the type.
static uint32_t getType(const object::RelocationRef &Rel);

/// Return code for a NONE relocation
static uint64_t getNone();
static uint32_t getNone();

/// Return code for a PC-relative 4-byte relocation
static uint64_t getPC32();
static uint32_t getPC32();

/// Return code for a PC-relative 8-byte relocation
static uint64_t getPC64();
static uint32_t getPC64();

/// Return code for a ABS 8-byte relocation
static uint64_t getAbs64();
static uint32_t getAbs64();

/// Return code for a RELATIVE relocation
static uint64_t getRelative();
static uint32_t getRelative();

/// Return true if this relocation is PC-relative. Return false otherwise.
bool isPCRelative() const { return isPCRelative(Type); }
Expand Down Expand Up @@ -161,7 +170,7 @@ struct Relocation {
const MCExpr *createExpr(MCStreamer *Streamer) const;
const MCExpr *createExpr(MCStreamer *Streamer,
const MCExpr *RetainedValue) const;
static MCBinaryExpr::Opcode getComposeOpcodeFor(uint64_t Type);
static MCBinaryExpr::Opcode getComposeOpcodeFor(uint32_t Type);
};

/// Relocation ordering by offset.
Expand Down
2 changes: 1 addition & 1 deletion bolt/include/bolt/Rewrite/RewriteInstance.h
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ class RewriteInstance {
/// The \p SymbolName, \p SymbolAddress, \p Addend and \p ExtractedValue
/// parameters will be set on success. The \p Skip argument indicates
/// that the relocation was analyzed, but it must not be processed.
bool analyzeRelocation(const object::RelocationRef &Rel, uint64_t &RType,
bool analyzeRelocation(const object::RelocationRef &Rel, uint32_t &RType,
std::string &SymbolName, bool &IsSectionRelocation,
uint64_t &SymbolAddress, int64_t &Addend,
uint64_t &ExtractedValue, bool &Skip) const;
Expand Down
4 changes: 2 additions & 2 deletions bolt/lib/Core/BinaryContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2284,7 +2284,7 @@ ErrorOr<int64_t> BinaryContext::getSignedValueAtAddress(uint64_t Address,
}

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

void BinaryContext::addDynamicRelocation(uint64_t Address, MCSymbol *Symbol,
uint64_t Type, uint64_t Addend,
uint32_t Type, uint64_t Addend,
uint64_t Value) {
ErrorOr<BinarySection &> Section = getSectionForAddress(Address);
assert(Section && "cannot find section for address");
Expand Down
2 changes: 1 addition & 1 deletion bolt/lib/Core/BinaryFunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4614,7 +4614,7 @@ bool BinaryFunction::isAArch64Veneer() const {
}

void BinaryFunction::addRelocation(uint64_t Address, MCSymbol *Symbol,
uint64_t RelType, uint64_t Addend,
uint32_t RelType, uint64_t Addend,
uint64_t Value) {
assert(Address >= getAddress() && Address < getAddress() + getMaxSize() &&
"address is outside of the function");
Expand Down
2 changes: 1 addition & 1 deletion bolt/lib/Core/JumpTable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ void bolt::JumpTable::updateOriginal() {
const uint64_t BaseOffset = getAddress() - getSection().getAddress();
uint64_t EntryOffset = BaseOffset;
for (MCSymbol *Entry : Entries) {
const uint64_t RelType =
const uint32_t RelType =
Type == JTT_NORMAL ? ELF::R_X86_64_64 : ELF::R_X86_64_PC32;
const uint64_t RelAddend =
Type == JTT_NORMAL ? 0 : EntryOffset - BaseOffset;
Expand Down
2 changes: 1 addition & 1 deletion bolt/lib/Core/MCPlusBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,7 @@ void MCPlusBuilder::initSizeMap() {
bool MCPlusBuilder::setOperandToSymbolRef(MCInst &Inst, int OpNum,
const MCSymbol *Symbol,
int64_t Addend, MCContext *Ctx,
uint64_t RelType) const {
uint32_t RelType) const {
MCOperand Operand;
if (!Addend) {
Operand = MCOperand::createExpr(getTargetExprFor(
Expand Down
Loading
Loading