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

57 lines
1.7 KiB
C++

// E2E: global-data demo. static_counter.c uses a BSS global (call_count)
// and an initialised .data global (step). PC32 relocs from .text to both
// sections are patched at synthesis time using vaddr arithmetic.
#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 void_fn = void (*)();
using int_fn = int (*)();
auto* reset = reinterpret_cast<void_fn>(::dlsym(h, "counter_reset"));
auto* step_up = reinterpret_cast<int_fn> (::dlsym(h, "counter_step_up"));
auto* get = reinterpret_cast<int_fn> (::dlsym(h, "counter_get"));
REQUIRE(reset != nullptr);
REQUIRE(step_up != nullptr);
REQUIRE(get != nullptr);
REQUIRE(get() == 0);
REQUIRE(step_up() == 1);
REQUIRE(step_up() == 2);
REQUIRE(step_up() == 3);
REQUIRE(get() == 3);
reset();
REQUIRE(get() == 0);
REQUIRE(step_up() == 1);
REQUIRE(::dlclose(h) == 0);
std::printf("PASS: e2e_load_static_counter — BSS/data globals work\n");
return 0;
}
} // namespace
int main() { return run(); }