Debian Codegraph @

test cases/common/148 shared module resolving symbol in executable/prog.c

raw
1 #include <stdio.h>
2 #include <assert.h>
3 #ifdef _WIN32
4 #include <windows.h>
5 #else
6 #include <dlfcn.h>
7 #endif
8
9 #if defined _WIN32 || defined __CYGWIN__
10 #define DLL_PUBLIC __declspec(dllexport)
11 #else
12 #if defined __GNUC__
13 #define DLL_PUBLIC __attribute__ ((visibility("default")))
14 #else
15 #pragma message ("Compiler does not support symbol visibility.")
16 #define DLL_PUBLIC
17 #endif
18 #endif
19
20 typedef int (*fptr) (void);
21
22 int DLL_PUBLIC
23 func_from_executable(void)
24 {
25 return 42;
26 }
27
28 int main(int argc, char **argv)
29 {
30 int expected, actual;
31 fptr importedfunc;
32
33 (void)argc; // noop
34
35 #ifdef _WIN32
36 HMODULE h = LoadLibraryA(argv[1]);
37 #else
38 void *h = dlopen(argv[1], RTLD_NOW);
39 #endif
40 assert(h != NULL);
41
42 #ifdef _WIN32
43 importedfunc = (fptr) GetProcAddress (h, "func");
44 #else
45 importedfunc = (fptr) dlsym(h, "func");
46 #endif
47 assert(importedfunc != NULL);
48 assert(importedfunc != func_from_executable);
49
50 actual = (*importedfunc)();
51 expected = func_from_executable();
52 assert(actual == expected);
53
54 #ifdef _WIN32
55 FreeLibrary(h);
56 #else
57 dlclose(h);
58 #endif
59
60 return 0;
61 }