(root)/
glibc-2.38/
elf/
tst-tls-dlinfo.c
       1  #include <dlfcn.h>
       2  #include <stdio.h>
       3  #include <stdlib.h>
       4  
       5  
       6  static int
       7  do_test (void)
       8  {
       9    static const char modname[] = "tst-tlsmod2.so";
      10    int result = 0;
      11    int *foop;
      12    int (*fp) (int, int *);
      13    void *h;
      14  
      15    h = dlopen (modname, RTLD_LAZY);
      16    if (h == NULL)
      17      {
      18        printf ("cannot open '%s': %s\n", modname, dlerror ());
      19        exit (1);
      20      }
      21  
      22    fp = dlsym (h, "in_dso");
      23    if (fp == NULL)
      24      {
      25        printf ("cannot get symbol 'in_dso': %s\n", dlerror ());
      26        exit (1);
      27      }
      28  
      29    size_t modid = -1;
      30    if (dlinfo (h, RTLD_DI_TLS_MODID, &modid))
      31      {
      32        printf ("dlinfo RTLD_DI_TLS_MODID failed: %s\n", dlerror ());
      33        result = 1;
      34      }
      35    else
      36      printf ("dlinfo says TLS module ID %zu\n", modid);
      37  
      38    void *block;
      39    if (dlinfo (h, RTLD_DI_TLS_DATA, &block))
      40      {
      41        printf ("dlinfo RTLD_DI_TLS_DATA failed: %s\n", dlerror ());
      42        result = 1;
      43      }
      44    else if (block != NULL)
      45      {
      46        printf ("dlinfo RTLD_DI_TLS_DATA says %p but should be unallocated\n",
      47  	      block);
      48        result = 1;
      49      }
      50  
      51    result |= fp (0, NULL);
      52  
      53    foop = dlsym (h, "foo");
      54    if (foop == NULL)
      55      {
      56        printf ("cannot get symbol 'foo' the second time: %s\n", dlerror ());
      57        exit (1);
      58      }
      59    if (*foop != 16)
      60      {
      61        puts ("foo != 16");
      62        result = 1;
      63      }
      64  
      65    /* Now the module's TLS block has been used and should appear.  */
      66    if (dlinfo (h, RTLD_DI_TLS_DATA, &block))
      67      {
      68        printf ("dlinfo RTLD_DI_TLS_DATA failed the second time: %s\n",
      69  	      dlerror ());
      70        result = 1;
      71      }
      72    else if (block != foop)
      73      {
      74        printf ("dlinfo RTLD_DI_TLS_DATA says %p but should be %p\n",
      75  	      block, foop);
      76        result = 1;
      77      }
      78  
      79    dlclose (h);
      80  
      81    return result;
      82  }
      83  
      84  
      85  #include <support/test-driver.c>