Files
faumaray bba5c8a39a initial commit
Signed-off-by: Aleksey Selihov <faumaray@gmail.com>
2026-05-22 02:40:32 +03:00

78 lines
3.0 KiB
C++

#pragma once
#include "format/reloc_ir.hpp"
#include <cstdint>
#include <string>
#include <vector>
namespace voidlibc::loader {
struct ExportedSymbol {
std::string name; // C-name (null-terminated when written)
std::uint64_t code_offset; // offset into the code blob
std::uint64_t size; // 0 if unknown
bool is_function = true;
bool weak = false;
};
// For Slice 1B the synthesizer needs to resolve internal relocations
// at synthesis time (PC-relative). Each `InternalSymbol` is a defined
// symbol referenced by a relocation, identified by `sym_id` matching
// the relocation's sym_id, and located at `code_offset` within the
// code blob.
struct InternalSymbol {
std::uint32_t sym_id = 0;
std::uint64_t code_offset = 0;
std::string name; // for error messages
};
// External (undefined) symbol resolved by the native linker via .rela.plt.
struct ImportedSymbol {
std::uint32_t sym_id = 0;
std::string name;
std::string library; // DT_NEEDED soname that provides this symbol; "" → default libc
// GNU symbol version requirement forwarded from input .o (SHT_GNU_verneed).
// Empty version_name → emit VER_NDX_GLOBAL (1) in .gnu.version; no .gnu.version_r entry.
std::string version_name;
std::uint32_t version_hash = 0;
std::uint16_t version_flags = 0;
};
// Writable symbol in the data blob (mapped in the RW segment).
// PC32 relocs from .text to data symbols are patched using vaddr arithmetic.
struct DataSymbol {
std::uint32_t sym_id = 0;
std::uint64_t data_offset = 0; // byte offset within data_blob
std::string name;
};
struct ElfSynthInput {
std::vector<std::uint8_t> code;
std::vector<ExportedSymbol> exports;
std::vector<voidlibc::format::RelocEntry> relocs;
std::vector<InternalSymbol> internal_symbols;
std::vector<ImportedSymbol> imports;
std::vector<std::uint8_t> data_blob; // .data + .bss (RW)
std::vector<DataSymbol> data_symbols;
std::string soname;
// Explicit DT_NEEDED entries for the synthesized ELF's .dynamic section.
// Empty = no host dependencies (statically linked VLC, ldd shows "statically linked").
// Set via voidlink --need <soname> at pack time.
std::vector<std::string> needed_libs;
};
struct ElfSynthOutput {
std::vector<std::uint8_t> bytes; // synthesized in-memory ELF64 DSO
std::uint64_t code_vaddr = 0; // base vaddr where code is mapped
std::string error; // empty on success
bool ok = true;
};
// Produce a valid in-memory ELF64 DSO (ET_DYN, EM_X86_64) that dlopen
// will accept. Single contiguous .text + .dynsym/.dynstr/.hash/.dynamic.
// No relocations, imports, or section headers — enough for M1 export.
ElfSynthOutput synthesize_elf(const ElfSynthInput& input);
} // namespace voidlibc::loader