#include "handoff.hpp" #ifdef __linux__ #include #include #include #include #include #include #include #include #include #include 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(::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(w); n -= static_cast(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& 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(::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&, 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__