1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
|
#include <assert.h>
#include <dlfcn.h>
#include <pthread.h>
#include <stdio.h>
typedef int (*func_t)();
func_t g_one;
func_t g_two;
func_t g_three;
void* open_lib() {
void* handle = dlopen("liblib.so", RTLD_NOW|RTLD_GLOBAL);
if (!handle) {
printf("dlerror: %s\n", dlerror());
assert(handle);
}
printf("dlopen returned: %p\n", handle);
return handle;
}
static void test_order1() {
void* handle = open_lib();
g_one = dlsym(handle, "one");
g_two = dlsym(handle, "two");
g_three = dlsym(handle, "three");
assert(g_one() == 1);
assert(g_two() == 2);
assert(g_three() == 3);
}
static void test_order2() {
void* handle = open_lib();
func_t three = dlsym(handle, "three");
func_t two = dlsym(handle, "two");
func_t one = dlsym(handle, "one");
printf("one: %p -> %d\n", one, one());
printf("two: %p -> %d\n", two, two());
printf("three: %p -> %d\n", three, three());
assert(one() == 1);
assert(two() == 2);
assert(three() == 3);
assert(one == g_one);
assert(two == g_two);
assert(three == g_three);
}
static void* thread_main(void* arg) {
printf("in thread_main\n");
test_order2();
printf("thread_main done\n");
return 0;
}
int main() {
printf("in main\n");
test_order1();
pthread_t t;
int rc = pthread_create(&t, NULL, thread_main, NULL);
assert(rc == 0);
pthread_join(t, NULL);
return 0;
}
|