#include "handoff.hpp" #ifdef __APPLE__ #include #include #include #include #include namespace voidlibc::loader::macos_handoff { LoadResult dlopen_buffer(const std::vector& blob) { LoadResult res{}; // Write blob to a temp file, dlopen it, then unlink. // macOS dyld requires a path — no memfd_create equivalent. char tmp[256]; const char* tmpdir = ::getenv("TMPDIR"); if (!tmpdir || !*tmpdir) tmpdir = "/tmp"; std::snprintf(tmp, sizeof(tmp), "%s/voidlib.%d.XXXXXX.dylib", tmpdir, static_cast(::getpid())); const int fd = ::mkstemps(tmp, 6 /* len(".dylib") */); if (fd < 0) { res.error = std::string("mkstemps: ") + std::strerror(errno); return res; } const std::uint8_t* p = blob.data(); std::size_t remaining = blob.size(); while (remaining > 0) { const ssize_t n = ::write(fd, p, remaining); if (n <= 0) { ::close(fd); ::unlink(tmp); res.error = std::string("write: ") + std::strerror(errno); return res; } p += n; remaining -= static_cast(n); } ::close(fd); // chmod +x so dyld will map it as executable ::chmod(tmp, 0755); void* handle = ::dlopen(tmp, RTLD_NOW | RTLD_LOCAL); ::unlink(tmp); if (!handle) { res.error = ::dlerror() ? ::dlerror() : "dlopen failed"; return res; } res.dl_handle = handle; res.ok = true; return res; } 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::macos_handoff #else // stub compilation so non-macOS hosts can include this TU namespace voidlibc::loader::macos_handoff { LoadResult dlopen_buffer(const std::vector&) { return {{}, "macos_handoff not available on this platform", false}; } void* sym(void*, const char*) { return nullptr; } int close_(void*) { return 0; } } // namespace voidlibc::loader::macos_handoff #endif // __APPLE__