1 /* Copyright (C) 2004-2022 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3 
4    The GNU C Library is free software; you can redistribute it and/or
5    modify it under the terms of the GNU Lesser General Public
6    License as published by the Free Software Foundation; either
7    version 2.1 of the License, or (at your option) any later version.
8 
9    The GNU C Library is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    Lesser General Public License for more details.
13 
14    You should have received a copy of the GNU Lesser General Public
15    License along with the GNU C Library; if not, see
16    <https://www.gnu.org/licenses/>.  */
17 
18 #include <link.h>
19 #include <stddef.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 
23 #define SET	0
24 #define ADD	1
25 #define REMOVE	2
26 
27 #define leq(l,r)	(((r) - (l)) <= ~0ULL / 2)
28 
29 static int
callback(struct dl_phdr_info * info,size_t size,void * ptr)30 callback (struct dl_phdr_info *info, size_t size, void *ptr)
31 {
32   static int last_adds = 0, last_subs = 0;
33   intptr_t cmd = (intptr_t) ptr;
34 
35   printf ("  size = %Zu\n", size);
36   if (size < (offsetof (struct dl_phdr_info, dlpi_subs)
37 	      + sizeof (info->dlpi_subs)))
38     {
39       fprintf (stderr, "dl_iterate_phdr failed to pass dlpi_adds/dlpi_subs\n");
40       exit (5);
41     }
42 
43   printf ("  dlpi_adds = %Lu dlpi_subs = %Lu\n",
44 	  info->dlpi_adds, info->dlpi_subs);
45 
46   switch (cmd)
47     {
48     case SET:
49       break;
50 
51     case ADD:
52       if (leq (info->dlpi_adds, last_adds))
53 	{
54 	  fprintf (stderr, "dlpi_adds failed to get incremented!\n");
55 	  exit (3);
56 	}
57       break;
58 
59     case REMOVE:
60       if (leq (info->dlpi_subs, last_subs))
61 	{
62 	  fprintf (stderr, "dlpi_subs failed to get incremented!\n");
63 	  exit (4);
64 	}
65       break;
66     }
67   last_adds = info->dlpi_adds;
68   last_subs = info->dlpi_subs;
69   return -1;
70 }
71 
72 static void *
load(const char * path)73 load (const char *path)
74 {
75   void *handle;
76 
77   printf ("loading `%s'\n", path);
78   handle = dlopen (path, RTLD_LAZY);
79   if (!handle)
80     exit (1);
81   dl_iterate_phdr (callback, (void *)(intptr_t) ADD);
82   return handle;
83 }
84 
85 static void
unload(const char * path,void * handle)86 unload (const char *path, void *handle)
87 {
88   printf ("unloading `%s'\n", path);
89   if (dlclose (handle) < 0)
90     exit (2);
91   dl_iterate_phdr (callback, (void *)(intptr_t) REMOVE);
92 }
93 
94 static int
do_test(void)95 do_test (void)
96 {
97   void *handle1, *handle2;
98 
99   dl_iterate_phdr (callback, (void *)(intptr_t) SET);
100   handle1 = load ("firstobj.so");
101   handle2 = load ("globalmod1.so");
102   unload ("firstobj.so", handle1);
103   unload ("globalmod1.so", handle2);
104   return 0;
105 }
106 
107 #include <support/test-driver.c>
108