#pragma once #include "format/reloc_ir.hpp" #include "format/vlc_format.hpp" #include "writer.hpp" #include #include #include #include namespace voidlibc::voidlink { struct SymbolSpec { std::string name; // canonical (C) name std::string itanium_name; // empty if not C++ Itanium std::string msvc_name; // empty if not C++ MSVC voidlibc::format::SymbolKind kind = voidlibc::format::SymbolKind::Function; std::uint64_t value = 0; // offset within owning blob (code or data) std::uint64_t size = 0; std::uint16_t section_index = voidlibc::format::kSymUndefined; bool exported = true; bool weak = false; bool hidden = false; bool data_section = false; // lives in data blob, not code blob // For undefined (imported) symbols: which host library provides this symbol. // Populated by voidlink at synthesis time from symlib_table or --symlib overrides. // Empty means "use per-OS default libc". std::string library; // GNU symbol version requirement forwarded from input .o (SHT_GNU_versym + verneed). // Empty version_name means no version requirement (emit VER_NDX_GLOBAL in .gnu.version). std::string version_name; std::uint32_t version_hash = 0; std::uint16_t version_flags = 0; }; struct SectionSpec { voidlibc::format::TargetOS target_os; voidlibc::format::SectionKind kind = voidlibc::format::SectionKind::RawCode; std::vector code; // raw PIC code (RawCode) or native blob (NativeBlob) std::vector relocs; // empty when kind == NativeBlob std::uint64_t entry_offset = 0; std::vector data; // .data + .bss zeros (RW); unused for NativeBlob }; class ContainerBuilder { public: // Mark the container as an executable with the given entry symbol name. // Exactly one section should have entry_offset != 0 to match this. void set_executable(std::string entry_symbol) { is_executable_ = true; entry_symbol_ = std::move(entry_symbol); } void set_static_library() { is_static_library_ = true; } // Returns the assigned symbol id (index into the symbol table). std::uint32_t add_symbol(SymbolSpec spec); void add_section(SectionSpec section); std::uint32_t symbol_count() const { return static_cast(symbols_.size()); } std::uint16_t section_count() const { return static_cast(sections_.size()); } // Serialize to disk. Lays out: FileHeader → section directory → // symbol table → per-section reloc chunks → per-section code → // per-section debug. file_size is computed. WriteResult write(const std::filesystem::path& out_path) const; private: std::vector symbols_; std::vector sections_; bool is_executable_ = false; bool is_static_library_ = false; std::string entry_symbol_; }; } // namespace voidlibc::voidlink