Blame src/functional/dlopen.c

nsz 626789
#include <dlfcn.h>
nsz 626789
#include "test.h"
nsz 626789
Szabolcs Nagy cfa23c
int main(int argc, char *argv[])
nsz 626789
{
nsz 626789
	void *h, *g;
nsz 626789
	int *i, *i2;
nsz 626789
	char *s;
nsz 626789
	void (*f)(void);
Szabolcs Nagy cfa23c
	char buf[512];
nsz 626789
Szabolcs Nagy cfa23c
	if (!t_pathrel(buf, sizeof buf, argv[0], "dlopen_dso.so")) {
Szabolcs Nagy cfa23c
		t_error("failed to obtain relative path to dlopen_dso.so\n");
Szabolcs Nagy cfa23c
		return 1;
Szabolcs Nagy cfa23c
	}
Szabolcs Nagy cfa23c
	h = dlopen(buf, RTLD_LAZY|RTLD_LOCAL);
nsz 626789
	if (!h)
Szabolcs Nagy cfa23c
		t_error("dlopen %s failed: %s\n", buf, dlerror());
nsz 626789
	i = dlsym(h, "i");
nsz 626789
	if (!i)
Szabolcs Nagy cfa23c
		t_error("dlsym i failed: %s\n", dlerror());
nsz 626789
	if (*i != 1)
Szabolcs Nagy cfa23c
		t_error("initialization failed: want i=1 got i=%d\n", *i);
nsz 626789
	f = (void (*)(void))dlsym(h, "f");
nsz 626789
	if (!f)
Szabolcs Nagy cfa23c
		t_error("dlsym f failed: %s\n", dlerror());
nsz 626789
	f();
nsz 626789
	if (*i != 2)
Szabolcs Nagy cfa23c
		t_error("f call failed: want i=2 got i=%d\n", *i);
nsz 49b23c
nsz 626789
	g = dlopen(0, RTLD_LAZY|RTLD_LOCAL);
nsz 626789
	if (!g)
Szabolcs Nagy cfa23c
		t_error("dlopen 0 failed: %s\n", dlerror());
nsz 626789
	i2 = dlsym(g, "i");
nsz 626789
	s = dlerror();
nsz 626789
	if (i2 || s == 0)
Szabolcs Nagy cfa23c
		t_error("dlsym i should have failed\n");
Szabolcs Nagy cfa23c
	if (dlsym(g, "main") != (void*)main)
Szabolcs Nagy cfa23c
		t_error("dlsym main failed: %s\n", dlerror());
nsz 49b23c
Szabolcs Nagy cfa23c
	/* close+open reinitializes the dso with glibc but not with musl */
Szabolcs Nagy cfa23c
	h = dlopen(buf, RTLD_LAZY|RTLD_GLOBAL);
nsz 626789
	i2 = dlsym(g, "i");
nsz 626789
	if (!i2)
Szabolcs Nagy cfa23c
		t_error("dlsym i failed: %s\n", dlerror());
Szabolcs Nagy cfa23c
	if (i2 != i)
Szabolcs Nagy cfa23c
		t_error("reopened dso should have the same symbols, want %p, got %p\n", i, i2);
nsz 626789
	if (*i2 != 2)
Szabolcs Nagy cfa23c
		t_error("reopened dso should have the same symbols, want i2==2, got i2==%d\n", *i2);
nsz 49b23c
	if (dlclose(g))
Szabolcs Nagy cfa23c
		t_error("dlclose failed: %s\n", dlerror());
Szabolcs Nagy cfa23c
	if (dlclose(h))
Szabolcs Nagy cfa23c
		t_error("dlclose failed: %s\n", dlerror());
Szabolcs Nagy cfa23c
	return t_status;
nsz 626789
}