#include "builder.hpp" #include "coff_reader.hpp" #include "elf_reader.hpp" #include "format/vlc_format.hpp" #include "loader/common/elf_synth.hpp" #include "loader/macos/macho_synth.hpp" #include "loader/windows/pe_synth.hpp" #include "macho_reader.hpp" #include "merge.hpp" #include "reader.hpp" #include "reloc_lowering.hpp" #include "symlib_table.hpp" #include "writer.hpp" #include #include #include #include #include #include #include #include #include #include #include // Detect object format from the first four bytes of a file. // ELF: 0x7F 0x45 ('E'); COFF AMD64: LE 0x8664; Mach-O 64-bit LE: 0xFEEDFACF. enum class ObjFormat { Unknown, Elf, Coff, Macho }; static ObjFormat detect_format(const std::string& path) { std::FILE* f = std::fopen(path.c_str(), "rb"); if (!f) return ObjFormat::Unknown; unsigned char h[4] = {}; const bool got = std::fread(h, 1, 4, f) == 4; std::fclose(f); if (!got) return ObjFormat::Unknown; if (h[0] == 0x7F && h[1] == 0x45) return ObjFormat::Elf; // \x7fE if (h[0] == 0x64 && h[1] == 0x86) return ObjFormat::Coff; // LE 0x8664 if (h[0] == 0xCF && h[1] == 0xFA && h[2] == 0xED && h[3] == 0xFE) return ObjFormat::Macho; // 0xFEEDFACF little-endian return ObjFormat::Unknown; } using namespace voidlibc::voidlink; namespace { int usage(const char* argv0) { std::fprintf(stderr, "voidlink — VoidLibC universal linker\n" "\n" "File extensions:\n" " .vle executable (has entry point, run via voidloader)\n" " .vls shared library (loadable via voidlib_load / dlopen)\n" " .vla static archive (for build-time linking, not loadable at runtime)\n" "\n" "usage:\n" " %s create-empty \n" " write a minimal empty container\n" " %s dump \n" " parse and print container header\n" " %s pack-blob --os linux --code --symbol [--entry ] \n" " build a single-section container from a raw machine-code blob\n" " %s pack-elf --os [--need ]... [--entry ] [--static] ... -o \n" " build a single-OS container; --need libc.so.6 for dynamic linkage, omit for static\n" " --entry sets executable mode (.vle); --static sets archive mode (.vla); else .vls\n" " %s fuse [--linux f.o ...] [--windows f.obj ...] [--macos f.o ...]\n" " [--need ]... [--entry ] [--static] -o \n" " pack per-OS object files into a universal multi-OS container\n" " %s wrap [--linux ] [--windows ] [--macos ]\n" " [--entry ] -o \n" " embed pre-linked native binaries verbatim as NativeBlob sections\n" " %s vlcldd \n" " show dynamic dependencies of the Linux section (like ldd)\n" " %s verify \n" " structural check all sections: magic bytes, format, and dependency lists\n", argv0, argv0, argv0, argv0, argv0, argv0, argv0, argv0); return 2; } int cmd_create_empty(const char* argv0, int argc, char** argv) { if (argc != 1) { return usage(argv0); } const auto r = write_empty(argv[0]); if (!r.ok) { std::fprintf(stderr, "voidlink: %s\n", r.error.c_str()); return 1; } return 0; } int cmd_pack_blob(const char* argv0, int argc, char** argv) { using namespace voidlibc::format; using namespace voidlibc::voidlink; TargetOS os = TargetOS::Unknown; std::string code_path; std::string symbol_name; std::uint64_t entry_off = 0; std::string out_path; for (int i = 0; i < argc; ++i) { const std::string_view a = argv[i]; auto need = [&](const char* flag) -> const char* { if (i + 1 >= argc) { std::fprintf(stderr, "voidlink: %s requires a value\n", flag); std::exit(2); } return argv[++i]; }; if (a == "--os") { const std::string_view v = need("--os"); if (v == "linux") os = TargetOS::Linux; else if (v == "windows") os = TargetOS::Windows; else if (v == "macos") os = TargetOS::MacOS; else { std::fprintf(stderr, "voidlink: unknown os '%.*s'\n", static_cast(v.size()), v.data()); return 2; } } else if (a == "--code") code_path = need("--code"); else if (a == "--symbol") symbol_name = need("--symbol"); else if (a == "--entry") entry_off = std::strtoull(need("--entry"), nullptr, 0); else if (out_path.empty()) out_path = std::string(a); else { return usage(argv0); } } if (os == TargetOS::Unknown || code_path.empty() || symbol_name.empty() || out_path.empty()) { return usage(argv0); } std::ifstream in(code_path, std::ios::binary | std::ios::ate); if (!in) { std::fprintf(stderr, "voidlink: cannot open code blob %s\n", code_path.c_str()); return 1; } const auto size = static_cast(in.tellg()); in.seekg(0, std::ios::beg); std::vector code(static_cast(size)); in.read(reinterpret_cast(code.data()), size); ContainerBuilder b; SymbolSpec sym{}; sym.name = symbol_name; sym.kind = SymbolKind::Function; sym.size = 0; sym.exported = true; b.add_symbol(std::move(sym)); SectionSpec sec{}; sec.target_os = os; sec.code = std::move(code); sec.entry_offset = entry_off; b.add_section(std::move(sec)); const auto w = b.write(out_path); if (!w.ok) { std::fprintf(stderr, "voidlink: %s\n", w.error.c_str()); return 1; } return 0; } // Lower one object file (ELF, COFF, or Mach-O, auto-detected) into LoweredText. static bool lower_one_obj(const char* argv0, const std::string& path, voidlibc::format::TargetOS os, LoweredText& out) { const auto fmt = detect_format(path); if (fmt == ObjFormat::Coff) { auto parsed = coff::parse_coff_object(path); if (const auto* e = std::get_if(&parsed)) { std::fprintf(stderr, "%s: %s: %s\n", argv0, path.c_str(), e->c_str()); return false; } auto lowered = lower_coff_text(std::get(parsed), os); if (const auto* e = std::get_if(&lowered)) { std::fprintf(stderr, "%s: %s: %s\n", argv0, path.c_str(), e->c_str()); return false; } out = std::move(std::get(lowered)); return true; } if (fmt == ObjFormat::Macho) { auto parsed = macho::parse_macho_object(path); if (const auto* e = std::get_if(&parsed)) { std::fprintf(stderr, "%s: %s: %s\n", argv0, path.c_str(), e->message.c_str()); return false; } auto lowered = lower_macho_text(std::get(parsed), os); if (const auto* e = std::get_if(&lowered)) { std::fprintf(stderr, "%s: %s: %s\n", argv0, path.c_str(), e->c_str()); return false; } out = std::move(std::get(lowered)); return true; } // Default: ELF auto parsed = elf::parse_elf64_object(path); if (const auto* e = std::get_if(&parsed)) { std::fprintf(stderr, "%s: %s: %s\n", argv0, path.c_str(), e->c_str()); return false; } auto lowered = lower_elf_text(std::get(parsed), os); if (const auto* e = std::get_if(&lowered)) { std::fprintf(stderr, "%s: %s: %s\n", argv0, path.c_str(), e->c_str()); return false; } out = std::move(std::get(lowered)); return true; } // Lower a list of object files (ELF or COFF) and return a merged LoweredText. static bool lower_elf_group(const char* argv0, const std::vector& paths, voidlibc::format::TargetOS os, LoweredText& out) { std::vector objs; objs.reserve(paths.size()); for (const auto& path : paths) { LoweredText lt; if (!lower_one_obj(argv0, path, os, lt)) return false; objs.push_back(std::move(lt)); } auto merged = merge_lowered(std::move(objs)); if (const auto* e = std::get_if(&merged)) { std::fprintf(stderr, "%s: %s\n", argv0, e->c_str()); return false; } out = std::move(std::get(merged)); return true; } // Resolve the import library for a symbol, using (in priority order): // 1. symlib_overrides (from --symlib name=lib on CLI) // 2. built-in symlib_table // 3. per-OS default (libc.so.6 / ucrtbase.dll / libSystem.B.dylib) // Emits a warning (or error when strict) if falling back to default. static std::string resolve_library( const std::string& sym_name, voidlibc::format::TargetOS os, const std::unordered_map& overrides, bool strict_libs) { auto oit = overrides.find(sym_name); if (oit != overrides.end()) return oit->second; auto sv = voidlibc::voidlink::library_for(os, sym_name); if (!sv.empty()) return std::string(sv); auto def = voidlibc::voidlink::default_library(os); if (strict_libs) { std::fprintf(stderr, "voidlink: warning: unknown library for symbol '%s' on %s;" " defaulting to '%.*s'. Use --symlib to suppress.\n", sym_name.c_str(), os == voidlibc::format::TargetOS::Linux ? "linux" : os == voidlibc::format::TargetOS::Windows ? "windows" : "macos", static_cast(def.size()), def.data()); } return std::string(def); } // Convert LoweredText into a pre-synthesized native blob (ELF/PE/Mach-O). // On success, lt.section.kind == NativeBlob and lt.section.code holds // the complete native object image; lt.section.relocs is cleared. // Returns an error string on failure. static std::string synthesize_native_blob( LoweredText& lt, const std::unordered_map& symlib_overrides = {}, bool strict_libs = false, const std::vector& explicit_needed = {}) { using namespace voidlibc::format; using namespace voidlibc::loader; const TargetOS os = lt.section.target_os; if (os == TargetOS::Linux) { ElfSynthInput in{}; in.code = lt.section.code; in.data_blob = lt.section.data; in.relocs = lt.section.relocs; // explicit_needed (--need) overrides the auto-derived list entirely. in.needed_libs = explicit_needed; for (std::size_t i = 0; i < lt.symbols.size(); ++i) { const auto& s = lt.symbols[i]; if (s.exported && !s.data_section && s.section_index != kSymUndefined) { ExportedSymbol e{}; e.name = s.name; e.code_offset = s.value; e.size = s.size; e.is_function = s.kind == SymbolKind::Function; e.weak = s.weak; in.exports.push_back(std::move(e)); } if (!s.data_section && s.section_index != kSymUndefined) { InternalSymbol is{}; is.sym_id = static_cast(i); is.code_offset = s.value; is.name = s.name; in.internal_symbols.push_back(std::move(is)); } if (s.data_section && s.section_index != kSymUndefined) { DataSymbol ds{}; ds.sym_id = static_cast(i); ds.data_offset = s.value; ds.name = s.name; in.data_symbols.push_back(std::move(ds)); } if (s.section_index == kSymUndefined && !s.name.empty()) { ImportedSymbol im{}; im.sym_id = static_cast(i); im.name = s.name; im.library = !s.library.empty() ? s.library : (explicit_needed.empty() ? resolve_library(s.name, os, symlib_overrides, strict_libs) : std::string{}); im.version_name = s.version_name; im.version_hash = s.version_hash; im.version_flags = s.version_flags; in.imports.push_back(std::move(im)); } } const auto out = synthesize_elf(in); if (!out.ok) return out.error; lt.section.code = out.bytes; lt.section.relocs = {}; lt.section.data = {}; lt.section.kind = SectionKind::NativeBlob; return {}; } if (os == TargetOS::Windows) { windows::PeSynthInput in{}; in.code = lt.section.code; in.data_blob = lt.section.data; in.relocs = lt.section.relocs; in.dll_name = "output.dll"; // synthesized DLL's own name for (std::size_t i = 0; i < lt.symbols.size(); ++i) { const auto& s = lt.symbols[i]; if (s.exported && !s.data_section && s.section_index != kSymUndefined) { windows::WinExportedSymbol e{}; e.name = s.name; e.code_offset = s.value; e.size = s.size; e.is_function = s.kind == SymbolKind::Function; in.exports.push_back(std::move(e)); } if (!s.data_section && s.section_index != kSymUndefined) { windows::WinInternalSymbol is{}; is.sym_id = static_cast(i); is.code_offset = s.value; is.name = s.name; in.internal_symbols.push_back(std::move(is)); } if (s.data_section && s.section_index != kSymUndefined) { windows::WinDataSymbol ds{}; ds.sym_id = static_cast(i); ds.data_offset = s.value; ds.name = s.name; in.data_symbols.push_back(std::move(ds)); } if (s.section_index == kSymUndefined && !s.name.empty()) { windows::WinImportedSymbol im{}; im.sym_id = static_cast(i); im.name = s.name; im.library = !s.library.empty() ? s.library : resolve_library(s.name, os, symlib_overrides, strict_libs); in.imports.push_back(std::move(im)); } } const auto out = windows::synthesize_pe(in); if (!out.ok) return out.error; lt.section.code = out.bytes; lt.section.relocs = {}; lt.section.data = {}; lt.section.kind = SectionKind::NativeBlob; return {}; } if (os == TargetOS::MacOS) { macos::MachoSynthInput in{}; in.code = lt.section.code; in.data_blob = lt.section.data; in.relocs = lt.section.relocs; in.needed_libs = explicit_needed; for (std::size_t i = 0; i < lt.symbols.size(); ++i) { const auto& s = lt.symbols[i]; if (s.exported && !s.data_section && s.section_index != kSymUndefined) { macos::MachoExportedSymbol e{}; e.name = s.name; e.code_offset = s.value; e.size = s.size; e.is_function = s.kind == SymbolKind::Function; e.weak = s.weak; in.exports.push_back(std::move(e)); } if (!s.data_section && s.section_index != kSymUndefined) { macos::MachoInternalSymbol is{}; is.sym_id = static_cast(i); is.code_offset = s.value; is.name = s.name; in.internal_symbols.push_back(std::move(is)); } if (s.data_section && s.section_index != kSymUndefined) { macos::MachoDataSymbol ds{}; ds.sym_id = static_cast(i); ds.data_offset = s.value; ds.name = s.name; in.data_symbols.push_back(std::move(ds)); } if (s.section_index == kSymUndefined && !s.name.empty()) { macos::MachoImportedSymbol im{}; im.sym_id = static_cast(i); im.name = s.name; im.library = !s.library.empty() ? s.library : (explicit_needed.empty() ? resolve_library(s.name, os, symlib_overrides, strict_libs) : std::string{}); in.imports.push_back(std::move(im)); } } const auto out_m = macos::synthesize_macho(in); if (!out_m.ok) return out_m.error; lt.section.code = out_m.bytes; lt.section.relocs = {}; lt.section.data = {}; lt.section.kind = SectionKind::NativeBlob; return {}; } return "synthesize_native_blob: unsupported OS"; } int cmd_pack_elf(const char* argv0, int argc, char** argv) { using namespace voidlibc::format; using namespace voidlibc::voidlink; TargetOS os = TargetOS::Unknown; std::vector in_paths; std::string out_path; std::string entry_symbol; // non-empty → executable mode std::vector needed_libs; // explicit DT_NEEDED override (--need) std::unordered_map symlib_overrides; // --symlib name=lib bool is_static = false; bool strict_libs = false; for (int i = 0; i < argc; ++i) { const std::string_view a = argv[i]; auto need = [&](const char* flag) -> const char* { if (i + 1 >= argc) { std::fprintf(stderr, "voidlink: %s requires a value\n", flag); std::exit(2); } return argv[++i]; }; if (a == "--os") { const std::string_view v = need("--os"); if (v == "linux") os = TargetOS::Linux; else if (v == "windows") os = TargetOS::Windows; else if (v == "macos") os = TargetOS::MacOS; else { std::fprintf(stderr, "voidlink: unknown os\n"); return 2; } } else if (a == "--entry") entry_symbol = need("--entry"); else if (a == "--need") needed_libs.emplace_back(need("--need")); else if (a == "--static") is_static = true; else if (a == "--strict-libs") strict_libs = true; else if (a == "--symlib") { std::string_view kv = need("--symlib"); const auto eq = kv.find('='); if (eq == std::string_view::npos) { std::fprintf(stderr, "voidlink: --symlib requires name=library\n"); return 2; } symlib_overrides[std::string(kv.substr(0, eq))] = std::string(kv.substr(eq + 1)); } else if (a == "-o") out_path = need("-o"); else in_paths.emplace_back(a); } if (os == TargetOS::Unknown || in_paths.empty() || out_path.empty()) { return usage(argv0); } std::vector lowered_objects; lowered_objects.reserve(in_paths.size()); for (const auto& path : in_paths) { LoweredText lt; if (!lower_one_obj(argv0, path, os, lt)) return 1; lowered_objects.push_back(std::move(lt)); } auto merged = merge_lowered(std::move(lowered_objects)); if (const auto* e = std::get_if(&merged)) { std::fprintf(stderr, "voidlink: %s\n", e->c_str()); return 1; } auto& lt = std::get(merged); // If --entry was given, locate the symbol and record its offset. if (!entry_symbol.empty()) { bool found = false; for (const auto& s : lt.symbols) { if (s.name == entry_symbol && s.section_index != kSymUndefined) { lt.section.entry_offset = s.value; found = true; break; } } if (!found) { std::fprintf(stderr, "voidlink: entry symbol '%s' not found\n", entry_symbol.c_str()); return 1; } } bool any_exports = false; for (const auto& s : lt.symbols) if (s.exported) { any_exports = true; break; } if (!any_exports) { std::fprintf(stderr, "voidlink: no exported symbols found\n"); return 1; } if (const auto err = synthesize_native_blob(lt, symlib_overrides, strict_libs, needed_libs); !err.empty()) { std::fprintf(stderr, "voidlink: synthesis failed: %s\n", err.c_str()); return 1; } ContainerBuilder b; if (!entry_symbol.empty()) b.set_executable(entry_symbol); if (is_static) b.set_static_library(); for (auto& s : lt.symbols) b.add_symbol(std::move(s)); b.add_section(std::move(lt.section)); if (out_path.find('.') == std::string::npos) { std::uint32_t flags = 0; if (!entry_symbol.empty()) flags |= static_cast(ContainerFlags::Executable); if (is_static) flags |= static_cast(ContainerFlags::StaticLibrary); out_path += infer_extension(flags); } const auto w = b.write(out_path); if (!w.ok) { std::fprintf(stderr, "voidlink: %s\n", w.error.c_str()); return 1; } return 0; } // fuse [--linux f.o ...] [--windows f.obj ...] [--macos f.o ...] -o out.vlc // // Packs per-OS object files into one universal .vlc. Multiple --linux / // --windows / --macos groups may appear; each group's objects are merged and // stored as one section in the output container. All three platform readers // are implemented: ELF (.o) for --linux, COFF (.obj) for --windows, Mach-O // (.o) for --macos. int cmd_fuse(const char* argv0, int argc, char** argv) { using namespace voidlibc::format; struct OsGroup { TargetOS os; std::vector paths; }; std::vector groups; std::string out_path; std::string entry_symbol; std::vector needed_libs; // global --need (all OSes) std::vector linux_needs; // --linux-need std::vector windows_needs; // --windows-need std::vector macos_needs; // --macos-need std::unordered_map symlib_overrides; // --symlib name=lib bool is_static = false; bool strict_libs = false; OsGroup* cur = nullptr; auto find_or_add = [&](TargetOS os) -> OsGroup& { for (auto& g : groups) if (g.os == os) return g; groups.push_back({os, {}}); return groups.back(); }; for (int i = 0; i < argc; ++i) { const std::string_view a = argv[i]; if (a == "--linux") { cur = &find_or_add(TargetOS::Linux); } else if (a == "--windows") { cur = &find_or_add(TargetOS::Windows); } else if (a == "--macos") { cur = &find_or_add(TargetOS::MacOS); } else if (a == "--entry") { if (i + 1 >= argc) { std::fprintf(stderr, "%s: --entry needs a value\n", argv0); return 2; } entry_symbol = argv[++i]; } else if (a == "--need") { if (i + 1 >= argc) { std::fprintf(stderr, "%s: --need requires a value\n", argv0); return 2; } needed_libs.emplace_back(argv[++i]); } else if (a == "--linux-need") { if (i + 1 >= argc) { std::fprintf(stderr, "%s: --linux-need requires a value\n", argv0); return 2; } linux_needs.emplace_back(argv[++i]); } else if (a == "--windows-need") { if (i + 1 >= argc) { std::fprintf(stderr, "%s: --windows-need requires a value\n", argv0); return 2; } windows_needs.emplace_back(argv[++i]); } else if (a == "--macos-need") { if (i + 1 >= argc) { std::fprintf(stderr, "%s: --macos-need requires a value\n", argv0); return 2; } macos_needs.emplace_back(argv[++i]); } else if (a == "--symlib") { if (i + 1 >= argc) { std::fprintf(stderr, "%s: --symlib requires name=library\n", argv0); return 2; } std::string_view kv = argv[++i]; const auto eq = kv.find('='); if (eq == std::string_view::npos) { std::fprintf(stderr, "%s: --symlib requires name=library\n", argv0); return 2; } symlib_overrides[std::string(kv.substr(0, eq))] = std::string(kv.substr(eq + 1)); } else if (a == "--strict-libs") { strict_libs = true; } else if (a == "--static") { is_static = true; } else if (a == "-o") { if (i + 1 >= argc) { std::fprintf(stderr, "%s: -o requires a value\n", argv0); return 2; } out_path = argv[++i]; } else { if (!cur) { std::fprintf(stderr, "%s: fuse: input file '%.*s' appears before any --linux / --windows / --macos flag\n", argv0, static_cast(a.size()), a.data()); return 2; } cur->paths.emplace_back(a); } } if (groups.empty() || out_path.empty()) { std::fprintf(stderr, "usage: %s fuse [--linux f.o ...] [--windows f.obj ...] [--macos f.o ...]\n" " [--entry sym] [--static]\n" " [--need ]...\n" " [--linux-need ]... [--windows-need ]..." " [--macos-need ]...\n" " [--symlib name=lib]... [--strict-libs]\n" " -o \n", argv0); return 2; } ContainerBuilder b; for (auto& g : groups) { if (g.paths.empty()) continue; LoweredText lt; if (!lower_elf_group(argv0, g.paths, g.os, lt)) return 1; bool any_exports = false; for (const auto& s : lt.symbols) if (s.exported) { any_exports = true; break; } if (!any_exports) { const char* osname = g.os == TargetOS::Linux ? "linux" : g.os == TargetOS::Windows ? "windows" : "macos"; std::fprintf(stderr, "%s: fuse: no exported symbols in %s group\n", argv0, osname); return 1; } // If --entry specified, find the symbol's offset in this group. if (!entry_symbol.empty()) { for (const auto& s : lt.symbols) { if (s.name == entry_symbol && s.section_index != kSymUndefined) { lt.section.entry_offset = s.value; break; } } } // Pre-synthesize native blob (ELF/PE/Mach-O) so ldd/file work on the .vlc. // Merge global --need with per-OS --linux-need / --windows-need / --macos-need. std::vector merged_needs = needed_libs; { const auto* per_os = (g.os == TargetOS::Linux) ? &linux_needs : (g.os == TargetOS::Windows) ? &windows_needs : &macos_needs; merged_needs.insert(merged_needs.end(), per_os->begin(), per_os->end()); } if (const auto err = synthesize_native_blob(lt, symlib_overrides, strict_libs, merged_needs); !err.empty()) { std::fprintf(stderr, "%s: fuse: synthesis failed: %s\n", argv0, err.c_str()); return 1; } const std::uint32_t sym_offset = b.symbol_count(); const std::uint16_t sec_index = b.section_count(); for (auto& r : lt.section.relocs) { r.sym_id += sym_offset; } // Fix section_index: defined symbols have section_index==0 from the // lowerer (one section per LoweredText); remap to the actual slot. for (auto& s : lt.symbols) { if (s.section_index != kSymUndefined) { s.section_index = sec_index; } } for (auto& s : lt.symbols) b.add_symbol(std::move(s)); b.add_section(std::move(lt.section)); } if (!entry_symbol.empty()) b.set_executable(entry_symbol); if (is_static) b.set_static_library(); if (out_path.find('.') == std::string::npos) { std::uint32_t flags = 0; if (!entry_symbol.empty()) flags |= static_cast(ContainerFlags::Executable); if (is_static) flags |= static_cast(ContainerFlags::StaticLibrary); out_path += infer_extension(flags); } const auto w = b.write(out_path); if (!w.ok) { std::fprintf(stderr, "%s: %s\n", argv0, w.error.c_str()); return 1; } return 0; } // wrap [--linux ] [--windows ] [--macos ] // [--entry ] -o // Pack pre-built native shared libraries verbatim as NativeBlob sections. // Unlike pack-elf/fuse (which start from .o files and synthesize ELF/PE), // wrap embeds an already-linked binary directly. int cmd_wrap(const char* argv0, int argc, char** argv) { using namespace voidlibc::format; std::string linux_path; std::string windows_path; std::string macos_path; std::string out_path; std::string entry_symbol; for (int i = 0; i < argc; ++i) { const std::string_view a = argv[i]; auto need = [&](const char* flag) -> const char* { if (i + 1 >= argc) { std::fprintf(stderr, "%s: %s requires a value\n", argv0, flag); std::exit(2); } return argv[++i]; }; if (a == "--linux") linux_path = need("--linux"); else if (a == "--windows") windows_path = need("--windows"); else if (a == "--macos") macos_path = need("--macos"); else if (a == "--entry") entry_symbol = need("--entry"); else if (a == "-o") out_path = need("-o"); else { std::fprintf(stderr, "%s: wrap: unknown argument '%.*s'\n", argv0, static_cast(a.size()), a.data()); return usage(argv0); } } if (linux_path.empty() && windows_path.empty() && macos_path.empty()) { std::fprintf(stderr, "%s: wrap: at least one of --linux/--windows/--macos required\n", argv0); return 2; } if (out_path.empty()) { std::fprintf(stderr, "%s: wrap: -o required\n", argv0); return 2; } auto read_blob = [&](const std::string& path) -> std::vector { std::ifstream f(path, std::ios::binary | std::ios::ate); if (!f) { std::fprintf(stderr, "%s: wrap: cannot open '%s'\n", argv0, path.c_str()); std::exit(1); } const auto sz = static_cast(f.tellg()); f.seekg(0); std::vector buf(sz); f.read(reinterpret_cast(buf.data()), static_cast(sz)); if (!f) { std::fprintf(stderr, "%s: wrap: read failed: '%s'\n", argv0, path.c_str()); std::exit(1); } return buf; }; ContainerBuilder b; if (!entry_symbol.empty()) b.set_executable(entry_symbol); if (!linux_path.empty()) { SectionSpec sec; sec.target_os = TargetOS::Linux; sec.kind = SectionKind::NativeBlob; sec.code = read_blob(linux_path); b.add_section(std::move(sec)); } if (!windows_path.empty()) { SectionSpec sec; sec.target_os = TargetOS::Windows; sec.kind = SectionKind::NativeBlob; sec.code = read_blob(windows_path); b.add_section(std::move(sec)); } if (!macos_path.empty()) { SectionSpec sec; sec.target_os = TargetOS::MacOS; sec.kind = SectionKind::NativeBlob; sec.code = read_blob(macos_path); b.add_section(std::move(sec)); } if (out_path.find('.') == std::string::npos) { std::uint32_t flags = 0; if (!entry_symbol.empty()) flags |= static_cast(ContainerFlags::Executable); out_path += infer_extension(flags); } const auto w = b.write(out_path); if (!w.ok) { std::fprintf(stderr, "%s: %s\n", argv0, w.error.c_str()); return 1; } return 0; } // vlcldd — extract the Linux NativeBlob, write to a temp file, // run ldd on it, and print the output. Acts like `ldd` but for .vlc files. int cmd_vlcldd(const char* argv0, int argc, char** argv) { using namespace voidlibc::format; if (argc != 1) { std::fprintf(stderr, "usage: %s vlcldd \n", argv0); return 2; } // Load the entire .vlc into memory for direct section parsing. std::ifstream f(argv[0], std::ios::binary | std::ios::ate); if (!f) { std::fprintf(stderr, "%s: vlcldd: cannot open %s\n", argv0, argv[0]); return 1; } const auto fsz = static_cast(f.tellg()); f.seekg(0); std::vector buf(fsz); f.read(reinterpret_cast(buf.data()), static_cast(fsz)); if (!f || fsz < sizeof(FileHeader)) { std::fprintf(stderr, "%s: vlcldd: read failed\n", argv0); return 1; } const auto* hdr = reinterpret_cast(buf.data()); if (hdr->magic != kMagic) { std::fprintf(stderr, "%s: vlcldd: bad magic\n", argv0); return 1; } const auto sec_end = hdr->section_dir_off + static_cast(hdr->section_count) * sizeof(SectionEntry); if (sec_end > fsz) { std::fprintf(stderr, "%s: vlcldd: section directory truncated\n", argv0); return 1; } const auto* entries = reinterpret_cast(buf.data() + hdr->section_dir_off); // Find the Linux NativeBlob section. const SectionEntry* linux_sec = nullptr; for (std::uint32_t i = 0; i < hdr->section_count; ++i) { if (entries[i].target_os == TargetOS::Linux && entries[i].kind == SectionKind::NativeBlob) { linux_sec = &entries[i]; break; } } if (!linux_sec) { std::fprintf(stderr, "%s: vlcldd: no Linux NativeBlob section in this file\n" " (was it built with an older voidlink that used RawCode sections?)\n", argv0); return 1; } if (linux_sec->code_off + linux_sec->code_size > fsz) { std::fprintf(stderr, "%s: vlcldd: section blob truncated\n", argv0); return 1; } // Write blob to a temp file and run ldd on it. char tmp[256]; std::snprintf(tmp, sizeof(tmp), "/tmp/vlcldd_%d.so", static_cast(getpid())); { std::ofstream out(tmp, std::ios::binary | std::ios::trunc); out.write(reinterpret_cast(buf.data() + linux_sec->code_off), static_cast(linux_sec->code_size)); if (!out) { std::fprintf(stderr, "%s: vlcldd: write to %s failed\n", argv0, tmp); return 1; } } ::chmod(tmp, 0755); std::string ldd_cmd = "ldd "; ldd_cmd += tmp; const int rc = std::system(ldd_cmd.c_str()); std::remove(tmp); return rc == 0 ? 0 : 1; } // verify — structural check of every platform section. // Validates magic bytes, uses file(1) for format identification, and runs // readelf/objdump to list shared library dependencies. Exits 0 if all // present sections pass magic validation, 1 if any fail. int cmd_verify(const char* argv0, int argc, char** argv) { using namespace voidlibc::format; if (argc != 1) { std::fprintf(stderr, "usage: %s verify \n", argv0); return 2; } const char* path = argv[0]; std::ifstream f(path, std::ios::binary | std::ios::ate); if (!f) { std::fprintf(stderr, "%s: verify: cannot open %s\n", argv0, path); return 1; } const auto fsz = static_cast(f.tellg()); f.seekg(0); std::vector buf(fsz); f.read(reinterpret_cast(buf.data()), static_cast(fsz)); if (!f || fsz < sizeof(FileHeader)) { std::fprintf(stderr, "%s: verify: read failed or file too small\n", argv0); return 1; } const auto* hdr = reinterpret_cast(buf.data()); if (hdr->magic != kMagic) { std::fprintf(stderr, "%s: verify: bad magic — not a .vlc file\n", argv0); return 1; } std::printf("file: %s\n", path); std::printf("version: %u.%u\n", hdr->version_major, hdr->version_minor); std::printf("arch: %s\n", hdr->arch == TargetArch::X86_64 ? "x86_64" : "unknown"); std::printf("sections: %u\n\n", hdr->section_count); const auto sec_end = hdr->section_dir_off + static_cast(hdr->section_count) * sizeof(SectionEntry); if (sec_end > fsz) { std::fprintf(stderr, "%s: verify: section directory truncated\n", argv0); return 1; } const auto* entries = reinterpret_cast(buf.data() + hdr->section_dir_off); // Helper: run a shell command, return its exit code. auto run = [](const char* cmd) { return std::system(cmd); }; int failures = 0; for (std::uint32_t i = 0; i < hdr->section_count; ++i) { const auto& sec = entries[i]; const char* os_name = sec.target_os == TargetOS::Linux ? "linux" : sec.target_os == TargetOS::Windows ? "windows" : sec.target_os == TargetOS::MacOS ? "macos" : "unknown"; const char* ext = sec.target_os == TargetOS::Windows ? ".dll" : sec.target_os == TargetOS::MacOS ? ".dylib" : ".so"; std::printf("section[%u] os=%-8s code=%llu bytes", i, os_name, static_cast(sec.code_size)); if (sec.data_size > 0) std::printf(" data=%llu bytes", static_cast(sec.data_size)); std::printf("\n"); if (sec.code_off + sec.code_size > fsz) { std::printf(" FAIL blob truncated\n\n"); ++failures; continue; } const std::uint8_t* blob = buf.data() + sec.code_off; // Validate magic bytes. bool magic_ok = false; const char* magic_desc = "?"; if (sec.target_os == TargetOS::Linux && sec.code_size >= 4) { magic_ok = blob[0]==0x7F && blob[1]=='E' && blob[2]=='L' && blob[3]=='F'; magic_desc = "ELF (7f 45 4c 46)"; } else if (sec.target_os == TargetOS::Windows && sec.code_size >= 2) { magic_ok = blob[0]=='M' && blob[1]=='Z'; magic_desc = "PE/MZ (4d 5a)"; } else if (sec.target_os == TargetOS::MacOS && sec.code_size >= 4) { magic_ok = blob[0]==0xCF && blob[1]==0xFA && blob[2]==0xED && blob[3]==0xFE; magic_desc = "Mach-O LE64 (cf fa ed fe)"; } if (magic_ok) { std::printf(" magic OK %s\n", magic_desc); } else { const std::size_t nb = std::min(sec.code_size, 4); std::printf(" magic FAIL expected %s got", magic_desc); for (std::size_t b = 0; b < nb; ++b) std::printf(" %02x", blob[b]); std::printf("\n"); ++failures; } // Write blob to temp file for tool inspection. char tmp[256]; std::snprintf(tmp, sizeof(tmp), "/tmp/vlcverify_%d_%u%s", static_cast(::getpid()), i, ext); { std::ofstream out(tmp, std::ios::binary | std::ios::trunc); out.write(reinterpret_cast(blob), static_cast(sec.code_size)); } ::chmod(tmp, 0755); // file(1) — format identification. char cmd[1024]; std::snprintf(cmd, sizeof(cmd), "file -b %s 2>/dev/null", tmp); std::printf(" file "); std::fflush(stdout); if (run(cmd) != 0) std::printf("(file(1) not available)\n"); // Dependency listing — tool varies by platform. if (sec.target_os == TargetOS::Linux) { std::printf(" needs (DT_NEEDED via readelf -d):\n"); std::fflush(stdout); std::snprintf(cmd, sizeof(cmd), "readelf -d %s 2>/dev/null" " | grep '(NEEDED)' | sed 's/^/ /'", tmp); if (run(cmd) != 0) std::printf(" (readelf not available)\n"); } else if (sec.target_os == TargetOS::Windows) { std::printf(" needs (PE import DLL names):\n"); std::fflush(stdout); // Prefer llvm-readobj; fall back to objdump (cross-PE capable on Linux). std::snprintf(cmd, sizeof(cmd), "{ llvm-readobj --coff-imports %s 2>/dev/null" " | grep 'Name:' | sed 's/^/ /'" "; } || { objdump -p %s 2>/dev/null" " | grep 'DLL Name:' | sed 's/^/ /'; }", tmp, tmp); run(cmd); } else if (sec.target_os == TargetOS::MacOS) { std::printf(" needs (LC_LOAD_DYLIB names):\n"); std::fflush(stdout); std::snprintf(cmd, sizeof(cmd), "{ llvm-readobj --macho-dylibs-used %s 2>/dev/null" " | grep 'name:' | sed 's/^/ /'" "; } || { objdump -p %s 2>/dev/null" " | grep 'name ' | sed 's/^/ /'; }", tmp, tmp); run(cmd); } std::remove(tmp); std::printf("\n"); } if (failures == 0) std::printf("OK all %u section(s) passed\n", hdr->section_count); else std::fprintf(stderr, "FAIL %d section(s) failed verification\n", failures); return failures == 0 ? 0 : 1; } int cmd_dump(const char* argv0, int argc, char** argv) { if (argc != 1) { return usage(argv0); } const auto r = read_container(argv[0]); if (const auto* e = std::get_if(&r)) { std::fprintf(stderr, "voidlink: %s\n", e->message.c_str()); return 1; } dump_container(std::get(r)); return 0; } } // namespace int main(int argc, char** argv) { if (argc < 2) { return usage(argv[0]); } const std::string_view cmd = argv[1]; if (cmd == "create-empty") return cmd_create_empty(argv[0], argc - 2, argv + 2); if (cmd == "pack-blob") return cmd_pack_blob (argv[0], argc - 2, argv + 2); if (cmd == "pack-elf") return cmd_pack_elf (argv[0], argc - 2, argv + 2); if (cmd == "fuse") return cmd_fuse (argv[0], argc - 2, argv + 2); if (cmd == "wrap") return cmd_wrap (argv[0], argc - 2, argv + 2); if (cmd == "dump") return cmd_dump (argv[0], argc - 2, argv + 2); if (cmd == "vlcldd") return cmd_vlcldd (argv[0], argc - 2, argv + 2); if (cmd == "verify") return cmd_verify (argv[0], argc - 2, argv + 2); return usage(argv[0]); }