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

258 lines
8.9 KiB
C++

#include "builder.hpp"
#include "writer.hpp"
#include <cstring>
#include <fstream>
namespace voidlibc::voidlink {
using namespace voidlibc::format;
std::uint32_t ContainerBuilder::add_symbol(SymbolSpec spec) {
const auto id = static_cast<std::uint32_t>(symbols_.size());
symbols_.push_back(std::move(spec));
return id;
}
void ContainerBuilder::add_section(SectionSpec section) {
sections_.push_back(std::move(section));
}
namespace {
constexpr std::uint64_t align_up(std::uint64_t v, std::uint64_t a) {
return (v + (a - 1)) & ~(a - 1);
}
struct LayoutPlan {
std::uint64_t header_off = 0;
std::uint64_t section_dir_off = 0;
std::uint64_t symtab_off = 0;
std::uint64_t symtab_size = 0;
std::vector<std::uint64_t> reloc_offs;
std::vector<std::uint64_t> code_offs;
std::vector<std::uint64_t> data_offs; // writable data blob offsets
std::uint64_t file_size = 0;
// Computed name-pool data so the writer doesn't recompute it.
std::vector<char> name_pool;
std::vector<SymbolEntry> sym_entries;
};
void plan_layout(const std::vector<SymbolSpec>& symbols,
const std::vector<SectionSpec>& sections,
LayoutPlan& plan) {
plan.header_off = 0;
plan.section_dir_off = sizeof(FileHeader);
const auto sec_dir_end =
plan.section_dir_off + sections.size() * sizeof(SectionEntry);
// --- Symbol table: header + entries + name pool ---
plan.symtab_off = align_up(sec_dir_end, 8);
auto append_name = [&](std::string_view s,
std::uint32_t& off,
std::uint32_t& len) {
if (s.empty()) { off = 0; len = 0; return; }
off = static_cast<std::uint32_t>(plan.name_pool.size());
len = static_cast<std::uint32_t>(s.size());
plan.name_pool.insert(plan.name_pool.end(), s.begin(), s.end());
};
plan.sym_entries.reserve(symbols.size());
for (const auto& s : symbols) {
SymbolEntry e{};
append_name(s.name, e.name_off, e.name_len);
append_name(s.itanium_name, e.itanium_name_off, e.itanium_name_len);
append_name(s.msvc_name, e.msvc_name_off, e.msvc_name_len);
e.value = s.value;
e.size = s.size;
e.section_index = s.section_index;
e.kind = s.kind;
std::uint8_t flags = 0;
if (s.exported) flags |= static_cast<std::uint8_t>(SymbolFlags::Exported);
if (s.weak) flags |= static_cast<std::uint8_t>(SymbolFlags::Weak);
if (s.hidden) flags |= static_cast<std::uint8_t>(SymbolFlags::Hidden);
if (s.data_section) flags |= static_cast<std::uint8_t>(SymbolFlags::DataSection);
e.flags = flags;
e.reserved = 0;
plan.sym_entries.push_back(e);
}
const auto symtab_body =
sizeof(SymbolTableHeader)
+ plan.sym_entries.size() * sizeof(SymbolEntry)
+ plan.name_pool.size();
plan.symtab_size = symtab_body;
std::uint64_t cursor = align_up(plan.symtab_off + symtab_body, 8);
// --- Per-section reloc chunks (NativeBlob sections have none) ---
plan.reloc_offs.reserve(sections.size());
for (const auto& sec : sections) {
if (sec.kind == SectionKind::NativeBlob || sec.relocs.empty()) {
plan.reloc_offs.push_back(0);
} else {
plan.reloc_offs.push_back(cursor);
cursor = align_up(cursor + sizeof(RelocChunk)
+ sec.relocs.size() * sizeof(RelocEntry), 16);
}
}
// --- Per-section code ---
plan.code_offs.reserve(sections.size());
for (const auto& sec : sections) {
cursor = align_up(cursor, 16);
plan.code_offs.push_back(cursor);
cursor += sec.code.size();
}
// --- Per-section writable data blob (optional) ---
plan.data_offs.reserve(sections.size());
for (const auto& sec : sections) {
if (!sec.data.empty()) {
cursor = align_up(cursor, 16);
plan.data_offs.push_back(cursor);
cursor += sec.data.size();
} else {
plan.data_offs.push_back(0);
}
}
plan.file_size = cursor;
}
} // namespace
WriteResult ContainerBuilder::write(const std::filesystem::path& out_path) const {
LayoutPlan plan;
plan_layout(symbols_, sections_, plan);
std::ofstream out(out_path, std::ios::binary | std::ios::trunc);
if (!out) {
return {false, "could not open output: " + out_path.string()};
}
auto seek = [&](std::uint64_t off) {
out.seekp(static_cast<std::streamoff>(off));
};
auto write_raw = [&](const void* p, std::size_t n) {
out.write(reinterpret_cast<const char*>(p), static_cast<std::streamsize>(n));
};
auto pad_to = [&](std::uint64_t off) {
const auto cur = static_cast<std::uint64_t>(out.tellp());
if (cur < off) {
std::vector<char> zeros(off - cur, 0);
write_raw(zeros.data(), zeros.size());
}
};
// --- FileHeader ---
FileHeader hdr{};
hdr.magic = kMagic;
hdr.version_major = kVersionMajor;
hdr.version_minor = kVersionMinor;
std::uint32_t flags = static_cast<std::uint32_t>(ContainerFlags::None);
if (is_executable_) flags |= static_cast<std::uint32_t>(ContainerFlags::Executable);
if (is_static_library_) flags |= static_cast<std::uint32_t>(ContainerFlags::StaticLibrary);
// Set HasDynamicImports if any symbol is an import (undefined section).
for (const auto& sym : symbols_) {
if (sym.section_index == kSymUndefined) {
flags |= static_cast<std::uint32_t>(ContainerFlags::HasDynamicImports);
break;
}
}
hdr.flags = flags;
hdr.arch = TargetArch::X86_64;
hdr.reserved = 0;
hdr.section_count = static_cast<std::uint32_t>(sections_.size());
hdr.section_dir_off = plan.section_dir_off;
hdr.symtab_off = plan.symtab_off;
hdr.symtab_size = plan.symtab_size;
hdr.imports_off = plan.symtab_off; // placeholder until M1
hdr.imports_size = 0;
hdr.exports_off = plan.symtab_off; // placeholder until M1
hdr.exports_size = 0;
hdr.file_size = plan.file_size;
seek(0);
write_raw(&hdr, sizeof(hdr));
// --- Section directory ---
seek(plan.section_dir_off);
for (std::size_t i = 0; i < sections_.size(); ++i) {
const auto& s = sections_[i];
SectionEntry e{};
e.target_os = s.target_os;
e.kind = s.kind;
e.reserved1 = 0;
e.code_off = plan.code_offs[i];
e.code_size = s.code.size();
if (s.kind == SectionKind::NativeBlob || s.relocs.empty()) {
e.reloc_off = 0;
e.reloc_size = 0;
} else {
e.reloc_off = plan.reloc_offs[i];
e.reloc_size = sizeof(RelocChunk) + s.relocs.size() * sizeof(RelocEntry);
}
e.data_off = plan.data_offs[i];
e.data_size = s.data.size();
e.entry_offset = s.entry_offset;
write_raw(&e, sizeof(e));
}
// --- Symbol table ---
pad_to(plan.symtab_off);
SymbolTableHeader sth{};
sth.entry_count = static_cast<std::uint32_t>(plan.sym_entries.size());
sth.name_pool_size = static_cast<std::uint32_t>(plan.name_pool.size());
write_raw(&sth, sizeof(sth));
if (!plan.sym_entries.empty()) {
write_raw(plan.sym_entries.data(),
plan.sym_entries.size() * sizeof(SymbolEntry));
}
if (!plan.name_pool.empty()) {
write_raw(plan.name_pool.data(), plan.name_pool.size());
}
// --- Per-section reloc chunks (skipped for NativeBlob sections) ---
for (std::size_t i = 0; i < sections_.size(); ++i) {
const auto& s = sections_[i];
if (s.kind == SectionKind::NativeBlob || plan.reloc_offs[i] == 0) continue;
pad_to(plan.reloc_offs[i]);
RelocChunk rc{};
rc.target_os = s.target_os;
rc.reserved0 = 0;
rc.entry_count = static_cast<std::uint32_t>(s.relocs.size());
write_raw(&rc, sizeof(rc));
if (!s.relocs.empty()) {
write_raw(s.relocs.data(),
s.relocs.size() * sizeof(RelocEntry));
}
}
// --- Per-section code ---
for (std::size_t i = 0; i < sections_.size(); ++i) {
pad_to(plan.code_offs[i]);
if (!sections_[i].code.empty()) {
write_raw(sections_[i].code.data(), sections_[i].code.size());
}
}
// --- Per-section writable data blob ---
for (std::size_t i = 0; i < sections_.size(); ++i) {
if (plan.data_offs[i] != 0) {
pad_to(plan.data_offs[i]);
write_raw(sections_[i].data.data(), sections_[i].data.size());
}
}
pad_to(plan.file_size);
if (!out) {
return {false, "write failed"};
}
return {true, {}};
}
} // namespace voidlibc::voidlink