initial commit

Signed-off-by: Aleksey Selihov <faumaray@gmail.com>
This commit is contained in:
faumaray committed 2026-05-22 02:40:32 +03:00
commit bba5c8a39a
109 files changed
+14085

No files matched your search

+13
View File
@@ -0,0 +1,13 @@
# Placeholder; the Linux handoff source is added in the next task.
add_library(voidlibc_loader_linux STATIC
handoff.cpp
)
target_link_libraries(voidlibc_loader_linux
PUBLIC voidlibc_loader_common
PRIVATE ${CMAKE_DL_LIBS}
)
set_target_properties(voidlibc_loader_linux PROPERTIES POSITION_INDEPENDENT_CODE ON)
install(TARGETS voidlibc_loader_linux
EXPORT VoidLibCTargets
ARCHIVE DESTINATION lib
)
+136
View File
@@ -0,0 +1,136 @@
#include "handoff.hpp"
#ifdef __linux__
#include <cerrno>
#include <cstring>
#include <dlfcn.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/syscall.h>
#include <unistd.h>
#include <cstdio>
#include <cstdlib>
namespace voidlibc::loader::linux_handoff {
namespace {
int memfd_create_sys(const char* name, unsigned int flags) {
// glibc has memfd_create() but only in recent versions; go via
// syscall to keep this independent of host libc feature levels.
return static_cast<int>(::syscall(SYS_memfd_create, name, flags));
}
bool write_all(int fd, const std::uint8_t* p, std::size_t n) {
while (n > 0) {
const auto w = ::write(fd, p, n);
if (w < 0) {
if (errno == EINTR) continue;
return false;
}
if (w == 0) return false;
p += static_cast<std::size_t>(w);
n -= static_cast<std::size_t>(w);
}
return true;
}
std::string errno_msg(const char* where) {
char buf[256];
std::snprintf(buf, sizeof(buf), "%s: %s", where, std::strerror(errno));
return std::string(buf);
}
} // namespace
LoadResult dlopen_buffer(const std::vector<std::uint8_t>& elf_bytes,
const char* debug_name) {
LoadResult r{};
int fd = memfd_create_sys(debug_name ? debug_name : "voidlibc.elf", 0);
bool used_tmpfile = false;
char tmpl[] = "/tmp/voidlibc.elf.XXXXXX";
if (fd < 0) {
// Fallback: O_TMPFILE on /tmp (kernel < 3.17 won't have memfd).
fd = ::open("/tmp",
O_TMPFILE | O_RDWR | O_EXCL,
S_IRUSR | S_IWUSR | S_IXUSR);
if (fd < 0) {
// Final fallback: a named temp file we manually unlink.
fd = ::mkstemp(tmpl);
if (fd < 0) {
r.error = errno_msg("memfd/tmpfile open");
return r;
}
used_tmpfile = true;
}
}
if (!write_all(fd, elf_bytes.data(), elf_bytes.size())) {
r.error = errno_msg("write elf bytes");
::close(fd);
if (used_tmpfile) ::unlink(tmpl);
return r;
}
// dlopen via /proc/self/fd/N — this works for memfds and unnamed
// O_TMPFILE files.
char path[64];
std::snprintf(path, sizeof(path), "/proc/self/fd/%d", fd);
// Use RTLD_NEXT to bypass the libvoid dlopen interposer (which shadows
// dlopen in the consumer binary) and call the real OS dlopen directly.
using real_dlopen_t = void* (*)(const char*, int);
static real_dlopen_t real_dlopen =
reinterpret_cast<real_dlopen_t>(::dlsym(RTLD_NEXT, "dlopen"));
// RTLD_NOW catches resolution failures at load time; cleaner errors
// than RTLD_LAZY where a missing symbol surfaces on first call.
void* h = real_dlopen(path, RTLD_NOW);
if (!h) {
const char* dl = ::dlerror();
r.error = std::string("dlopen: ") + (dl ? dl : "unknown");
::close(fd);
if (used_tmpfile) ::unlink(tmpl);
return r;
}
// Once dlopen has mapped the segments, we can close the fd. The
// mappings remain valid until dlclose. Same for O_TMPFILE (and the
// mkstemp fallback, where we already-unlinked-via-fd-only-open).
::close(fd);
if (used_tmpfile) ::unlink(tmpl);
r.dl_handle = h;
r.ok = true;
return r;
}
void* sym(void* handle, const char* name) {
if (!handle || !name) return nullptr;
return ::dlsym(handle, name);
}
int close_(void* handle) {
if (!handle) return 0;
return ::dlclose(handle);
}
} // namespace voidlibc::loader::linux_handoff
#else // stub for non-Linux builds so the target compiles on all platforms
namespace voidlibc::loader::linux_handoff {
LoadResult dlopen_buffer(const std::vector<std::uint8_t>&, const char*) {
return {{}, "linux_handoff not available on this platform", false};
}
void* sym(void*, const char*) { return nullptr; }
int close_(void*) { return 0; }
} // namespace voidlibc::loader::linux_handoff
#endif // __linux__
+25
View File
@@ -0,0 +1,25 @@
#pragma once
#include <cstdint>
#include <string>
#include <vector>
namespace voidlibc::loader::linux_handoff {
struct LoadResult {
void* dl_handle = nullptr; // opaque handle from dlopen
std::string error; // empty on success
bool ok = false;
};
// Take a synthesized ELF DSO buffer, place it in a memfd (or fallback
// temp file), and dlopen it. Returns the raw dlopen handle. Callers
// own it and must dlclose() it.
LoadResult dlopen_buffer(const std::vector<std::uint8_t>& elf_bytes,
const char* debug_name = "voidlibc.elf");
// Thin wrappers so the caller never includes <dlfcn.h>.
void* sym(void* handle, const char* name);
int close_(void* handle);
} // namespace voidlibc::loader::linux_handoff