67 lines
2.6 KiB
C++
67 lines
2.6 KiB
C++
#pragma once
|
|
|
|
#include <cstdint>
|
|
|
|
namespace voidlibc::format {
|
|
|
|
// Portable relocation IR. The synthesis core in loader/common/ lowers
|
|
// these to native relocation entries (ELF R_X86_64_*, PE
|
|
// IMAGE_REL_AMD64_*, Mach-O X86_64_RELOC_*) per target OS. voidlink
|
|
// raises native relocs from input .o files into this IR.
|
|
//
|
|
// Semantics use these conventions:
|
|
// base = run-time base address of the loaded code section
|
|
// P = base + dst_offset (place of the relocation)
|
|
// S = run-time address of symbol[sym_id]
|
|
// A = signed addend
|
|
// G = run-time address of the GOT slot for symbol[sym_id]
|
|
// L = run-time address of the PLT entry for symbol[sym_id]
|
|
//
|
|
// Operands are uniform: { dst_offset, sym_id, addend }. Opcodes that
|
|
// don't reference a symbol use sym_id = kNoSymbol.
|
|
|
|
enum class RelocOp : std::uint16_t {
|
|
// Direct fills
|
|
Abs64 = 1, // *(u64*)P = S + A
|
|
Abs32 = 2, // *(u32*)P = (u32)(S + A); overflow not checked at MVP
|
|
Abs32Signed = 3, // *(i32*)P = (i32)(S + A); overflow checked
|
|
|
|
// PC-relative
|
|
PC32 = 10, // *(i32*)P = (S + A) - P (R_X86_64_PC32 / IMAGE_REL_AMD64_REL32 / X86_64_RELOC_SIGNED)
|
|
PC32_1 = 11, // PC32 with -1 bias (REL32_1)
|
|
PC32_2 = 12, // PC32 with -2 bias
|
|
PC32_4 = 13, // PC32 with -4 bias
|
|
PC32_5 = 14, // PC32 with -5 bias (call/jmp imm32 forms)
|
|
|
|
// GOT / PLT
|
|
GotPC32 = 20, // *(i32*)P = (G + A) - P (R_X86_64_GOTPCREL)
|
|
GotPC32X = 21, // GOTPCREL relaxable form (R_X86_64_GOTPCRELX)
|
|
GotPC32XRex = 22, // REX-form relaxable (R_X86_64_REX_GOTPCRELX)
|
|
PLT32 = 30, // *(i32*)P = (L + A) - P (R_X86_64_PLT32)
|
|
|
|
// TLS — placeholders, each OS uses a different scheme (TLSGD, TLSLD,
|
|
// GOTTPOFF on ELF; SECREL on PE/TLS; TLVP on Mach-O). Lowering is
|
|
// future work; encoding the operation portably for now.
|
|
TlsLE = 40, // local-exec TLS offset
|
|
TlsIE = 41, // initial-exec TLS offset (via GOT)
|
|
TlsGD = 42, // general-dynamic TLS descriptor
|
|
TlsLD = 43, // local-dynamic TLS descriptor
|
|
};
|
|
|
|
inline constexpr std::uint32_t kNoSymbol = 0xFFFFFFFFu;
|
|
|
|
#pragma pack(push, 1)
|
|
struct RelocEntry {
|
|
RelocOp op;
|
|
std::uint16_t reserved; // = 0
|
|
std::uint32_t dst_offset; // byte offset within the section's code
|
|
std::uint32_t sym_id; // index into the symbol table, or kNoSymbol
|
|
std::int32_t addend;
|
|
};
|
|
#pragma pack(pop)
|
|
|
|
static_assert(sizeof(RelocEntry) == 16,
|
|
"RelocEntry on-disk size must be stable");
|
|
|
|
} // namespace voidlibc::format
|