[BOLT] Make Relocations a class and add optional field (#131638)

This patch converts `Relocations` from a struct to a class, and
introduces the `Optional` field. Patch #116964 will use it.

Some optimizations, like `scanExternalRefs`, create relocations that
patch the old code. Under certain circumstances these may be skipped
without correctness implications.
This commit is contained in:
Paschalis Mpeis
2025-03-20 17:16:14 +00:00
committed by GitHub
parent 10624e67c3
commit 5f6d9b45e9

View File

@@ -35,7 +35,16 @@ enum { R_X86_64_converted_reloc_bit = 0x80 };
namespace bolt {
/// Relocation class.
struct Relocation {
class Relocation {
public:
Relocation(uint64_t Offset, MCSymbol *Symbol, uint32_t Type, uint64_t Addend,
uint64_t Value)
: Offset(Offset), Symbol(Symbol), Type(Type), Optional(false),
Addend(Addend), Value(Value) {}
Relocation()
: Offset(0), Symbol(0), Type(0), Optional(0), Addend(0), Value(0) {}
static Triple::ArchType Arch; /// set by BinaryContext ctor.
/// The offset of this relocation in the object it is contained in.
@@ -47,6 +56,11 @@ struct Relocation {
/// Relocation type.
uint32_t Type;
private:
/// Relocations added by optimizations can be optional.
bool Optional = false;
public:
/// The offset from the \p Symbol base used to compute the final
/// value of this relocation.
uint64_t Addend;
@@ -58,6 +72,10 @@ struct Relocation {
/// Return size in bytes of the given relocation \p Type.
static size_t getSizeForType(uint32_t Type);
/// Some relocations added by optimizations are optional, meaning they can be
/// omitted under certain circumstances.
void setOptional() { Optional = true; }
/// Return size of this relocation.
size_t getSize() const { return getSizeForType(Type); }