Files
faumaray bba5c8a39a initial commit
Signed-off-by: Aleksey Selihov <faumaray@gmail.com>
2026-05-22 02:40:32 +03:00

50 lines
1.6 KiB
C++

// E2E: internal-call demo. sum_squares.c has two functions where
// sum_squares calls square (marked noinline). gcc emits a PC32/PLT32
// reloc on the call; voidlink lowers it to portable RelocOp::PC32 and
// the synthesizer patches it in-place 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 (*)(int, int);
auto* sum_squares = reinterpret_cast<fn_t>(::dlsym(h, "sum_squares"));
REQUIRE(sum_squares != nullptr);
const int result = sum_squares(3, 4); // 9 + 16 = 25
if (result != 25) {
std::fprintf(stderr, "FAIL: sum_squares(3, 4) = %d (expected 25)\n",
result);
std::exit(1);
}
REQUIRE(::dlclose(h) == 0);
std::printf("PASS: e2e_load_sum_squares — internal reloc patched, "
"sum_squares(3, 4) = %d\n", result);
return 0;
}
} // namespace
int main() { return run(); }