81 lines
3.3 KiB
C++
81 lines
3.3 KiB
C++
#pragma once
|
|
|
|
#include "format/reloc_ir.hpp"
|
|
#include "format/vlc_format.hpp"
|
|
#include "writer.hpp"
|
|
|
|
#include <cstdint>
|
|
#include <filesystem>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
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<std::uint8_t> code; // raw PIC code (RawCode) or native blob (NativeBlob)
|
|
std::vector<voidlibc::format::RelocEntry> relocs; // empty when kind == NativeBlob
|
|
std::uint64_t entry_offset = 0;
|
|
std::vector<std::uint8_t> 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<std::uint32_t>(symbols_.size()); }
|
|
std::uint16_t section_count() const { return static_cast<std::uint16_t>(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<SymbolSpec> symbols_;
|
|
std::vector<SectionSpec> sections_;
|
|
bool is_executable_ = false;
|
|
bool is_static_library_ = false;
|
|
std::string entry_symbol_;
|
|
};
|
|
|
|
} // namespace voidlibc::voidlink
|