xref: /minix/minix/tests/test63.c (revision 7f5f010b)
1 
2 /* Code to test runtime linking functionality.
3  * Load a shared object at runtime and verify that arguments passed to
4  * and from a function that is dynamically looked up make sense.
5  * This tests that (a) dynamic linking works at all (otherwise all the dl*
6  * functions don't work) and (b) the dynamic loading functionality works
7  * and (c) the PLT is sane and calling convention makes sense.
8  *
9  * We have to pass an absolute path to dlopen() for which we rely on
10  * the test run script.
11  *
12  * The module we load is in mod.c.
13  */
14 
15 #include <stdlib.h>
16 #include <stdio.h>
17 #include <dlfcn.h>
18 
19 int max_error = 2;
20 #include "common.h"
21 
22 
23 #include "magic.h"
24 
25 int main (int argc, char *argv[])
26 {
27   void *dlhandle;
28   long (*modf) (long, long *, long);
29   long v, *cookie = NULL, cookie2 = 0;
30 
31   start(63);
32 
33   if(argc != 2) {
34 	fprintf(stderr, "Usage: %s <module>\n", argv[0]);
35 	exit(1);
36   }
37 
38   if(!(dlhandle = dlopen(argv[1], RTLD_LAZY))) e(1);
39 
40   if(!(modf = dlsym(dlhandle, "modfunction"))) e(2);
41   if(!(cookie = (long *) dlsym(dlhandle, "cookie"))) e(3);
42 
43   if(*cookie == MAGIC2) { fprintf(stderr, "cookie already set\n"); e(4); }
44   if(cookie2 == MAGIC3) { fprintf(stderr, "cookie2 already set\n"); e(5); }
45 
46   v = modf(MAGIC4, &cookie2, MAGIC5);
47 
48   if(v != MAGIC1) { fprintf(stderr, "return value wrong\n"); e(9); }
49   if(*cookie != MAGIC2) { fprintf(stderr, "cookie set wrongly\n"); e(6); }
50   if(cookie2 != MAGIC3) { fprintf(stderr, "cookie2 set wrongly\n"); e(7); }
51 
52   dlclose(dlhandle);
53 
54   if(v != MAGIC1) { fprintf(stderr, "wrong return value.\n"); e(8); }
55 
56   quit();
57 
58   return(0);
59 }
60