support: Also return fd when it is 0
[glibc.git] / elf / tst-dlmodcount.c
blobd8719a8bff1b5a00ab400bbe085fbe3140776335
1 /* Copyright (C) 2004-2021 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
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.
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.
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/>. */
18 #include <link.h>
19 #include <stddef.h>
20 #include <stdio.h>
21 #include <stdlib.h>
23 #define SET 0
24 #define ADD 1
25 #define REMOVE 2
27 #define leq(l,r) (((r) - (l)) <= ~0ULL / 2)
29 static int
30 callback (struct dl_phdr_info *info, size_t size, void *ptr)
32 static int last_adds = 0, last_subs = 0;
33 intptr_t cmd = (intptr_t) ptr;
35 printf (" size = %Zu\n", size);
36 if (size < (offsetof (struct dl_phdr_info, dlpi_subs)
37 + sizeof (info->dlpi_subs)))
39 fprintf (stderr, "dl_iterate_phdr failed to pass dlpi_adds/dlpi_subs\n");
40 exit (5);
43 printf (" dlpi_adds = %Lu dlpi_subs = %Lu\n",
44 info->dlpi_adds, info->dlpi_subs);
46 switch (cmd)
48 case SET:
49 break;
51 case ADD:
52 if (leq (info->dlpi_adds, last_adds))
54 fprintf (stderr, "dlpi_adds failed to get incremented!\n");
55 exit (3);
57 break;
59 case REMOVE:
60 if (leq (info->dlpi_subs, last_subs))
62 fprintf (stderr, "dlpi_subs failed to get incremented!\n");
63 exit (4);
65 break;
67 last_adds = info->dlpi_adds;
68 last_subs = info->dlpi_subs;
69 return -1;
72 static void *
73 load (const char *path)
75 void *handle;
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;
85 static void
86 unload (const char *path, void *handle)
88 printf ("unloading `%s'\n", path);
89 if (dlclose (handle) < 0)
90 exit (2);
91 dl_iterate_phdr (callback, (void *)(intptr_t) REMOVE);
94 static int
95 do_test (void)
97 void *handle1, *handle2;
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;
107 #include <support/test-driver.c>