commit
bba5c8a39a
109 files changed
+14085
No files matched your search
@@ -0,0 +1,66 @@
|
||||
#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
|
||||
@@ -0,0 +1,162 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
namespace voidlibc::format {
|
||||
|
||||
inline constexpr std::uint32_t kMagic = 0x00434C56u; // 'V','L','C','\0' LE
|
||||
inline constexpr std::uint16_t kVersionMajor = 0;
|
||||
inline constexpr std::uint16_t kVersionMinor = 1;
|
||||
|
||||
enum class TargetOS : std::uint16_t {
|
||||
Unknown = 0,
|
||||
Linux = 1,
|
||||
Windows = 2,
|
||||
MacOS = 3,
|
||||
};
|
||||
|
||||
enum class TargetArch : std::uint16_t {
|
||||
Unknown = 0,
|
||||
X86_64 = 1,
|
||||
};
|
||||
|
||||
enum class ContainerFlags : std::uint32_t {
|
||||
None = 0,
|
||||
HasDebug = 1u << 0,
|
||||
HasCppExports = 1u << 1,
|
||||
Executable = 1u << 2,
|
||||
HasDynamicImports = 1u << 3,
|
||||
StaticLibrary = 1u << 4,
|
||||
};
|
||||
|
||||
inline constexpr const char* kExtExecutable = ".vle";
|
||||
inline constexpr const char* kExtShared = ".vls";
|
||||
inline constexpr const char* kExtStatic = ".vla";
|
||||
|
||||
inline constexpr bool flag_set(std::uint32_t flags, ContainerFlags f) {
|
||||
return (flags & static_cast<std::uint32_t>(f)) != 0;
|
||||
}
|
||||
|
||||
inline const char* infer_extension(std::uint32_t flags) {
|
||||
if (flag_set(flags, ContainerFlags::Executable)) return kExtExecutable;
|
||||
if (flag_set(flags, ContainerFlags::StaticLibrary)) return kExtStatic;
|
||||
return kExtShared;
|
||||
}
|
||||
|
||||
// All on-disk integers are little-endian. Host is x86_64 in MVP, so
|
||||
// no byte-swapping helpers are wired up yet — add when a big-endian
|
||||
// host is in scope.
|
||||
|
||||
#pragma pack(push, 1)
|
||||
|
||||
struct FileHeader {
|
||||
std::uint32_t magic; // = kMagic
|
||||
std::uint16_t version_major; // = kVersionMajor
|
||||
std::uint16_t version_minor; // = kVersionMinor
|
||||
std::uint32_t flags; // ContainerFlags bitfield
|
||||
TargetArch arch; // = X86_64 in MVP
|
||||
std::uint16_t reserved; // = 0
|
||||
std::uint32_t section_count;
|
||||
std::uint64_t section_dir_off; // file offset of SectionEntry[section_count]
|
||||
std::uint64_t symtab_off; // file offset of SymbolTableHeader
|
||||
std::uint64_t symtab_size; // bytes
|
||||
std::uint64_t imports_off; // file offset of ImportsHeader
|
||||
std::uint64_t imports_size; // bytes
|
||||
std::uint64_t exports_off; // file offset of ExportsHeader
|
||||
std::uint64_t exports_size; // bytes
|
||||
std::uint64_t file_size; // total .vlc size; sanity check
|
||||
};
|
||||
|
||||
static_assert(sizeof(FileHeader) == 84,
|
||||
"FileHeader on-disk size must be stable");
|
||||
|
||||
// One per (target_os) at MVP. Each section holds raw native code + a
|
||||
// portable relocation IR blob describing how to patch it on load.
|
||||
// How the section's code_off/code_size blob is stored.
|
||||
enum class SectionKind : std::uint16_t {
|
||||
// Raw PIC code + portable reloc IR (reloc_off/reloc_size).
|
||||
// Requires load-time synthesis before handing to the OS loader.
|
||||
RawCode = 0,
|
||||
// Pre-synthesized native OS object (ELF ET_DYN / PE DLL / Mach-O dylib).
|
||||
// reloc_off/reloc_size = 0; loader passes the blob directly to the OS.
|
||||
NativeBlob = 1,
|
||||
};
|
||||
|
||||
struct SectionEntry {
|
||||
TargetOS target_os;
|
||||
SectionKind kind; // RawCode or NativeBlob
|
||||
std::uint32_t reserved1; // = 0
|
||||
std::uint64_t code_off;
|
||||
std::uint64_t code_size;
|
||||
std::uint64_t reloc_off; // file offset of RelocChunk (0 if NativeBlob)
|
||||
std::uint64_t reloc_size; // bytes (0 if NativeBlob)
|
||||
std::uint64_t data_off; // file offset of writable data blob; 0 if absent
|
||||
std::uint64_t data_size; // bytes of writable data blob; 0 if absent
|
||||
std::uint64_t entry_offset; // offset within code for executable entry; 0 for pure libraries
|
||||
};
|
||||
|
||||
static_assert(sizeof(SectionEntry) == 64,
|
||||
"SectionEntry on-disk size must be stable");
|
||||
|
||||
// Symbol table is a flat blob: header + array of SymbolEntry + a name
|
||||
// pool. Names are stored as offset+length into the pool; UTF-8.
|
||||
struct SymbolTableHeader {
|
||||
std::uint32_t entry_count;
|
||||
std::uint32_t name_pool_size;
|
||||
// SymbolEntry entries[entry_count];
|
||||
// char name_pool[name_pool_size];
|
||||
};
|
||||
|
||||
enum class SymbolKind : std::uint8_t {
|
||||
Undefined = 0,
|
||||
Function = 1,
|
||||
Object = 2,
|
||||
TLS = 3,
|
||||
};
|
||||
|
||||
enum class SymbolFlags : std::uint8_t {
|
||||
None = 0,
|
||||
Exported = 1u << 0,
|
||||
Weak = 1u << 1,
|
||||
Hidden = 1u << 2,
|
||||
DataSection = 1u << 3, // symbol lives in the writable data blob, not the code blob
|
||||
};
|
||||
|
||||
// `section_index` is the index into the .vlc section directory of the
|
||||
// section that owns this symbol; 0xFFFF (`kSymUndefined`) marks an
|
||||
// import. `value` is the byte offset of the symbol's definition within
|
||||
// its section's code blob (meaningless if undefined).
|
||||
inline constexpr std::uint16_t kSymUndefined = 0xFFFF;
|
||||
|
||||
struct SymbolEntry {
|
||||
std::uint32_t name_off; // offset into name_pool of canonical (C) name
|
||||
std::uint32_t name_len;
|
||||
std::uint32_t itanium_name_off; // 0 if not C++ Itanium-mangled
|
||||
std::uint32_t itanium_name_len;
|
||||
std::uint32_t msvc_name_off; // 0 if not C++ MSVC-mangled
|
||||
std::uint32_t msvc_name_len;
|
||||
std::uint64_t value; // byte offset within the owning section
|
||||
std::uint64_t size; // 0 for code symbols of unknown extent
|
||||
std::uint16_t section_index; // index into section dir; kSymUndefined = import
|
||||
SymbolKind kind;
|
||||
std::uint8_t flags;
|
||||
std::uint32_t reserved; // = 0
|
||||
};
|
||||
|
||||
static_assert(sizeof(SymbolEntry) == 48,
|
||||
"SymbolEntry on-disk size must be stable");
|
||||
|
||||
// Per-section relocation IR blob. Lives at SectionEntry::reloc_off.
|
||||
struct RelocChunk {
|
||||
TargetOS target_os; // matches owning section
|
||||
std::uint16_t reserved0; // = 0
|
||||
std::uint32_t entry_count;
|
||||
// RelocEntry entries[entry_count]; (see reloc_ir.hpp)
|
||||
};
|
||||
|
||||
static_assert(sizeof(RelocChunk) == 8,
|
||||
"RelocChunk on-disk size must be stable");
|
||||
|
||||
#pragma pack(pop)
|
||||
|
||||
} // namespace voidlibc::format
|
||||
Reference in new issue
Block a user