// 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 #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 void_fn = void (*)(); using int_fn = int (*)(); auto* reset = reinterpret_cast(::dlsym(h, "counter_reset")); auto* step_up = reinterpret_cast (::dlsym(h, "counter_step_up")); auto* get = reinterpret_cast (::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(); }