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