| lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1 | #include <dlfcn.h> | 
|  | 2 | #include <stdio.h> | 
|  | 3 | #include <stdlib.h> | 
|  | 4 |  | 
|  | 5 | #include <link.h> | 
|  | 6 |  | 
|  | 7 |  | 
|  | 8 | #define TEST_FUNCTION do_test () | 
|  | 9 | static int | 
|  | 10 | do_test (void) | 
|  | 11 | { | 
|  | 12 | static const char modname[] = "tst-tlsmod2.so"; | 
|  | 13 | int result = 0; | 
|  | 14 | int *foop; | 
|  | 15 | int *foop2; | 
|  | 16 | int (*fp) (int, int *); | 
|  | 17 | void *h; | 
|  | 18 | int i; | 
|  | 19 | int modid = -1; | 
|  | 20 |  | 
|  | 21 | for (i = 0; i < 10; ++i) | 
|  | 22 | { | 
|  | 23 | h = dlopen (modname, RTLD_LAZY); | 
|  | 24 | if (h == NULL) | 
|  | 25 | { | 
|  | 26 | printf ("cannot open '%s': %s\n", modname, dlerror ()); | 
|  | 27 | exit (1); | 
|  | 28 | } | 
|  | 29 |  | 
|  | 30 | /* Dirty test code here: we peek into a private data structure. | 
|  | 31 | We make sure that the module gets assigned the same ID every | 
|  | 32 | time.  The value of the first round is used.  */ | 
|  | 33 | if (modid == -1) | 
|  | 34 | modid = ((struct link_map *) h)->l_tls_modid; | 
|  | 35 | else if (((struct link_map *) h)->l_tls_modid != modid) | 
|  | 36 | { | 
|  | 37 | printf ("round %d: modid now %zd, initially %d\n", | 
|  | 38 | i, ((struct link_map *) h)->l_tls_modid, modid); | 
|  | 39 | result = 1; | 
|  | 40 | } | 
|  | 41 |  | 
|  | 42 | foop = dlsym (h, "foo"); | 
|  | 43 | if (foop == NULL) | 
|  | 44 | { | 
|  | 45 | printf ("cannot get symbol 'foo': %s\n", dlerror ()); | 
|  | 46 | exit (1); | 
|  | 47 | } | 
|  | 48 |  | 
|  | 49 | *foop = 42 + i; | 
|  | 50 |  | 
|  | 51 | fp = dlsym (h, "in_dso"); | 
|  | 52 | if (fp == NULL) | 
|  | 53 | { | 
|  | 54 | printf ("cannot get symbol 'in_dso': %s\n", dlerror ()); | 
|  | 55 | exit (1); | 
|  | 56 | } | 
|  | 57 |  | 
|  | 58 | result |= fp (42 + i, foop); | 
|  | 59 |  | 
|  | 60 | foop2 = dlsym (h, "foo"); | 
|  | 61 | if (foop2 == NULL) | 
|  | 62 | { | 
|  | 63 | printf ("cannot get symbol 'foo' the second time: %s\n", dlerror ()); | 
|  | 64 | exit (1); | 
|  | 65 | } | 
|  | 66 |  | 
|  | 67 | if (foop != foop2) | 
|  | 68 | { | 
|  | 69 | puts ("address of 'foo' different the second time"); | 
|  | 70 | result = 1; | 
|  | 71 | } | 
|  | 72 | else if (*foop != 16) | 
|  | 73 | { | 
|  | 74 | puts ("foo != 16"); | 
|  | 75 | result = 1; | 
|  | 76 | } | 
|  | 77 |  | 
|  | 78 | dlclose (h); | 
|  | 79 | } | 
|  | 80 |  | 
|  | 81 | return result; | 
|  | 82 | } | 
|  | 83 |  | 
|  | 84 |  | 
|  | 85 | #include "../test-skeleton.c" |