// E2E: external-call demo. hello_printf.c calls printf (libc) — // an external symbol resolved via a synthesized PLT stub at load time. #include #include #include #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(::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(); }