163 lines
5.7 KiB
C++
163 lines
5.7 KiB
C++
#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
|