Files
VoidUniveralFormat/tests/e2e/load_hello_printf.cpp
T
faumaray bba5c8a39a initial commit
Signed-off-by: Aleksey Selihov <faumaray@gmail.com>
2026-05-22 02:40:32 +03:00

47 lines
1.4 KiB
C++

// E2E: external-call demo. hello_printf.c calls printf (libc) —
// an external symbol resolved via a synthesized PLT stub at load time.
#include <cstdio>
#include <cstdlib>
#include <dlfcn.h>
#ifndef VOIDLIBC_TEST_VLC_PATH
#error "VOIDLIBC_TEST_VLC_PATH must be set by CMake"
#endif
namespace {
#define REQUIRE(cond) do { \
if (!(cond)) { \
std::fprintf(stderr, "FAIL: %s (line %d)\n", #cond, \
__LINE__); \
const char* le = ::dlerror(); \
if (le) std::fprintf(stderr, " dlerror: %s\n", le); \
std::exit(1); \
} \
} while (0)
int run() {
void* h = ::dlopen(VOIDLIBC_TEST_VLC_PATH, RTLD_NOW);
REQUIRE(h != nullptr);
using fn_t = int (*)();
auto* hello = reinterpret_cast<fn_t>(::dlsym(h, "hello"));
REQUIRE(hello != nullptr);
const int result = hello();
if (result != 42) {
std::fprintf(stderr, "FAIL: hello() = %d (expected 42)\n", result);
std::exit(1);
}
REQUIRE(::dlclose(h) == 0);
std::printf("PASS: e2e_load_hello_printf — PLT stub resolved, "
"hello() = %d\n", result);
return 0;
}
} // namespace
int main() { return run(); }