1 /* Test for dlinfo.
2    Copyright (C) 2003-2022 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4 
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9 
10    The GNU C Library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Lesser General Public License for more details.
14 
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library; if not, see
17    <https://www.gnu.org/licenses/>.  */
18 
19 #include <dlfcn.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <error.h>
23 
24 #define TEST_FUNCTION do_test ()
25 
26 static int
do_test(void)27 do_test (void)
28 {
29   int status = 0;
30 
31   void *handle = dlopen ("glreflib3.so", RTLD_NOW);
32   if (handle == NULL)
33     error (EXIT_FAILURE, 0, "cannot load: glreflib1.so: %s", dlerror ());
34 
35 #define TRY(req, arg)							      \
36   if (dlinfo (handle, req, arg) != 0)					      \
37     {									      \
38       printf ("dlinfo failed for %s: %s\n", #req, dlerror ());		      \
39       status = 1;							      \
40     }									      \
41   else
42 
43   struct link_map *l;
44   TRY (RTLD_DI_LINKMAP, &l)
45     {
46       if (l != handle)
47 	{
48 	  printf ("bogus link_map? %p != %p\n", l, handle);
49 	  status = 1;
50 	}
51     }
52 
53   char origin[8192];		/* >= PATH_MAX, in theory */
54   TRY (RTLD_DI_ORIGIN, origin)
55     {
56       printf ("origin: %s\n", origin);
57     }
58 
59   Dl_serinfo counts;
60   TRY (RTLD_DI_SERINFOSIZE, &counts)
61     {
62       Dl_serinfo *buf = alloca (counts.dls_size);
63       buf->dls_cnt = counts.dls_cnt;
64       buf->dls_size = counts.dls_size;
65       printf ("%u library directories\n", buf->dls_cnt);
66       TRY (RTLD_DI_SERINFO, buf)
67 	{
68 	  if (counts.dls_cnt != buf->dls_cnt)
69 	    {
70 	      printf ("??? became %u library directories\n", buf->dls_cnt);
71 	      status = 1;
72 	    }
73 	  for (unsigned int i = 0; i < buf->dls_cnt; ++i)
74 	    printf ("\t%#02x\t%s\n",
75 		    buf->dls_serpath[i].dls_flags,
76 		    buf->dls_serpath[i].dls_name);
77 	}
78     }
79 
80   unsigned long int lmid = 0xdeadbeefUL;
81   if (dlinfo (handle, RTLD_DI_LMID, &lmid) != 0)
82     printf ("dlinfo refuses RTLD_DI_LMID: %s\n", dlerror ());
83   else
84     {
85       printf ("dlinfo RTLD_DI_LMID worked? %#lx\n", lmid);
86       status = lmid == 0xdeadbeefUL;
87     }
88 
89 #undef TRY
90   dlclose (handle);
91 
92   return status;
93 }
94 
95 #include "../test-skeleton.c"
96