105 lines
3.5 KiB
C++
105 lines
3.5 KiB
C++
// voidloader — exec-mode entry for .vle files.
|
|
//
|
|
// Usage: voidloader <file.vle> [args...]
|
|
//
|
|
// Loads the .vle as a native DSO via libvoid's internal API, resolves its
|
|
// "main" symbol, and calls it as main(argc, argv) where argv[0] is the path.
|
|
// Registered with the kernel via binfmt_misc so that `./hello.vle args`
|
|
// transparently invokes this binary (see installer/linux/).
|
|
|
|
#include "internal.h"
|
|
|
|
#include <cstdint>
|
|
#include <cstdio>
|
|
#include <cstring>
|
|
|
|
#if defined(_WIN32)
|
|
# define WIN32_LEAN_AND_MEAN
|
|
# include <windows.h>
|
|
|
|
static const char* vlc_error_string() {
|
|
static char buf[512];
|
|
const DWORD err = ::GetLastError();
|
|
if (err == 0) return "unknown error";
|
|
::FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
|
|
nullptr, err, 0, buf, sizeof(buf) - 1, nullptr);
|
|
for (char* p = buf; *p; ++p) {
|
|
if (*p == '\r' || *p == '\n') { *p = '\0'; break; }
|
|
}
|
|
return buf;
|
|
}
|
|
# define VLC_SYM(h, name) \
|
|
reinterpret_cast<void*>(::GetProcAddress(static_cast<HMODULE>(h), (name)))
|
|
# define VLC_CLOSE(h) ::FreeLibrary(static_cast<HMODULE>(h))
|
|
# define VLC_ERROR() vlc_error_string()
|
|
#else
|
|
# include <dlfcn.h>
|
|
# define VLC_SYM(h, name) ::dlsym((h), (name))
|
|
# define VLC_CLOSE(h) ::dlclose(h)
|
|
# define VLC_ERROR() ::dlerror()
|
|
#endif
|
|
|
|
int main(int argc, char** argv) {
|
|
if (argc < 2) {
|
|
std::fprintf(stderr,
|
|
"usage: voidloader <file.vle> [args...]\n"
|
|
"\n"
|
|
"Load and run a VoidLibC executable in exec mode.\n"
|
|
"The .vle's main(argc, argv) is called; argv[0] is the .vle path.\n");
|
|
return 2;
|
|
}
|
|
|
|
const char* vlc_path = argv[1];
|
|
|
|
// Peek at the header to confirm this is an executable VLC, not a library.
|
|
{
|
|
std::FILE* f = std::fopen(vlc_path, "rb");
|
|
if (!f) {
|
|
std::fprintf(stderr, "voidloader: %s: cannot open\n", vlc_path);
|
|
return 1;
|
|
}
|
|
// FileHeader layout: magic(4) + version_major(2) + version_minor(2) + flags(4)
|
|
std::uint8_t hdr[12] = {};
|
|
const bool ok = std::fread(hdr, 1, sizeof(hdr), f) == sizeof(hdr);
|
|
std::fclose(f);
|
|
if (!ok) {
|
|
std::fprintf(stderr, "voidloader: %s: too small\n", vlc_path);
|
|
return 1;
|
|
}
|
|
std::uint32_t flags = 0;
|
|
std::memcpy(&flags, hdr + 8, 4);
|
|
constexpr std::uint32_t kExecutable = 1u << 2;
|
|
if (!(flags & kExecutable)) {
|
|
std::fprintf(stderr,
|
|
"voidloader: %s: not an executable (missing Executable flag)\n"
|
|
" Shared libraries (.vls) should be loaded via dlopen().\n"
|
|
" Static archives (.vla) should be linked with voidlink.\n",
|
|
vlc_path);
|
|
return 1;
|
|
}
|
|
}
|
|
|
|
// Use the internal API to bypass the dlopen interposer's allow_exec check.
|
|
void* h = voidlibc::detail::load_vlc(vlc_path, /*allow_exec=*/true);
|
|
if (!h) {
|
|
const char* err = VLC_ERROR();
|
|
std::fprintf(stderr, "voidloader: %s: %s\n",
|
|
vlc_path, err ? err : "unknown error");
|
|
return 1;
|
|
}
|
|
|
|
using MainFn = int (*)(int, char**);
|
|
auto* entry = reinterpret_cast<MainFn>(VLC_SYM(h, "main"));
|
|
if (!entry) {
|
|
std::fprintf(stderr,
|
|
"voidloader: %s: no exported 'main' symbol\n", vlc_path);
|
|
VLC_CLOSE(h);
|
|
return 1;
|
|
}
|
|
|
|
// argv[1] becomes the loaded program's argv[0]; remaining args follow.
|
|
const int rc = entry(argc - 1, argv + 1);
|
|
VLC_CLOSE(h);
|
|
return rc;
|
|
}
|