| lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1 | #include <dlfcn.h> | 
|  | 2 | #include <mcheck.h> | 
|  | 3 | #include <stdio.h> | 
|  | 4 | #include <stdlib.h> | 
|  | 5 |  | 
|  | 6 | int | 
|  | 7 | main (void) | 
|  | 8 | { | 
|  | 9 | void *h1; | 
|  | 10 | void *h2; | 
|  | 11 | int (*fp) (void); | 
|  | 12 | int *vp; | 
|  | 13 |  | 
|  | 14 | mtrace (); | 
|  | 15 |  | 
|  | 16 | /* Open the two objects.  */ | 
|  | 17 | h1 = dlopen ("reldepmod1.so", RTLD_LAZY | RTLD_GLOBAL); | 
|  | 18 | if (h1 == NULL) | 
|  | 19 | { | 
|  | 20 | printf ("cannot open reldepmod1.so: %s\n", dlerror ()); | 
|  | 21 | exit (1); | 
|  | 22 | } | 
|  | 23 | h2 = dlopen ("reldepmod3.so", RTLD_LAZY); | 
|  | 24 | if (h2 == NULL) | 
|  | 25 | { | 
|  | 26 | printf ("cannot open reldepmod3.so: %s\n", dlerror ()); | 
|  | 27 | exit (1); | 
|  | 28 | } | 
|  | 29 |  | 
|  | 30 | /* Get the address of the variable in reldepmod1.so.  */ | 
|  | 31 | vp = dlsym (h1, "some_var"); | 
|  | 32 | if (vp == NULL) | 
|  | 33 | { | 
|  | 34 | printf ("cannot get address of \"some_var\": %s\n", dlerror ()); | 
|  | 35 | exit (1); | 
|  | 36 | } | 
|  | 37 |  | 
|  | 38 | *vp = 42; | 
|  | 39 |  | 
|  | 40 | /* Get the function `call_me' in the second object.  This has a | 
|  | 41 | dependency which is resolved by a definition in reldepmod1.so.  */ | 
|  | 42 | fp = dlsym (h2, "call_me"); | 
|  | 43 | if (fp == NULL) | 
|  | 44 | { | 
|  | 45 | printf ("cannot get address of \"call_me\": %s\n", dlerror ()); | 
|  | 46 | exit (1); | 
|  | 47 | } | 
|  | 48 |  | 
|  | 49 | /* Call the function.  */ | 
|  | 50 | if (fp () != 0) | 
|  | 51 | { | 
|  | 52 | puts ("function \"call_me\" returned wrong result"); | 
|  | 53 | exit (1); | 
|  | 54 | } | 
|  | 55 |  | 
|  | 56 | /* Now close the first object.  It must still be around since we have | 
|  | 57 | an implicit dependency.  */ | 
|  | 58 | if (dlclose (h1) != 0) | 
|  | 59 | { | 
|  | 60 | printf ("closing h1 failed: %s\n", dlerror ()); | 
|  | 61 | exit (1); | 
|  | 62 | } | 
|  | 63 |  | 
|  | 64 | /* Open the first object again.   */ | 
|  | 65 | h1 = dlopen ("reldepmod1.so", RTLD_LAZY | RTLD_GLOBAL); | 
|  | 66 | if (h1 == NULL) | 
|  | 67 | { | 
|  | 68 | printf ("cannot open reldepmod1.so the second time: %s\n", dlerror ()); | 
|  | 69 | exit (1); | 
|  | 70 | } | 
|  | 71 |  | 
|  | 72 | /* Get the variable address again.  */ | 
|  | 73 | vp = dlsym (h1, "some_var"); | 
|  | 74 | if (vp == NULL) | 
|  | 75 | { | 
|  | 76 | printf ("cannot get address of \"some_var\" the second time: %s\n", | 
|  | 77 | dlerror ()); | 
|  | 78 | exit (1); | 
|  | 79 | } | 
|  | 80 |  | 
|  | 81 | /* The variable now must have its originial value.  */ | 
|  | 82 | if (*vp != 42) | 
|  | 83 | { | 
|  | 84 | puts ("variable \"some_var\" reset"); | 
|  | 85 | exit (1); | 
|  | 86 | } | 
|  | 87 |  | 
|  | 88 | /* Close the first object again, we are done.  */ | 
|  | 89 | if (dlclose (h1) != 0) | 
|  | 90 | { | 
|  | 91 | printf ("closing h1 failed: %s\n", dlerror ()); | 
|  | 92 | exit (1); | 
|  | 93 | } | 
|  | 94 | if (dlclose (h2) != 0) | 
|  | 95 | { | 
|  | 96 | printf ("closing h2 failed: %s\n", dlerror ()); | 
|  | 97 | exit (1); | 
|  | 98 | } | 
|  | 99 |  | 
|  | 100 | return 0; | 
|  | 101 | } |