// E2E: extern global variable access via GOTPCREL / GLOB_DAT. // extern_read.c references `the_answer` (an extern int defined here in the // test executable). The synthesized ELF must emit a .rela.dyn GLOB_DAT entry // so that RTLD resolves `the_answer` from the executable's global symbol table. #include #include #include #ifndef VOIDLIBC_TEST_VLC_PATH #error "VOIDLIBC_TEST_VLC_PATH must be set by CMake" #endif // Defined here; exported to the process's global symbol table so the // dlopen'd synthesized ELF can resolve `the_answer` via GLOB_DAT. extern "C" int the_answer = 42; 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 int_fn = int (*)(); auto* get = reinterpret_cast(::dlsym(h, "get_the_answer")); auto* dbl = reinterpret_cast(::dlsym(h, "double_the_answer")); REQUIRE(get != nullptr); REQUIRE(dbl != nullptr); REQUIRE(get() == 42); REQUIRE(dbl() == 84); the_answer = 7; REQUIRE(get() == 7); REQUIRE(dbl() == 14); REQUIRE(::dlclose(h) == 0); std::printf("PASS: e2e_load_extern_read — GOTPCREL / GLOB_DAT extern global works\n"); return 0; } } // namespace int main() { return run(); }