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
29static int
30callback (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 (format: " size = %Zu\n", size);
36 if (size < (offsetof (struct dl_phdr_info, dlpi_subs)
37 + sizeof (info->dlpi_subs)))
38 {
39 fprintf (stderr, format: "dl_iterate_phdr failed to pass dlpi_adds/dlpi_subs\n");
40 exit (status: 5);
41 }
42
43 printf (format: " 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, format: "dlpi_adds failed to get incremented!\n");
55 exit (status: 3);
56 }
57 break;
58
59 case REMOVE:
60 if (leq (info->dlpi_subs, last_subs))
61 {
62 fprintf (stderr, format: "dlpi_subs failed to get incremented!\n");
63 exit (status: 4);
64 }
65 break;
66 }
67 last_adds = info->dlpi_adds;
68 last_subs = info->dlpi_subs;
69 return -1;
70}
71
72static void *
73load (const char *path)
74{
75 void *handle;
76
77 printf (format: "loading `%s'\n", path);
78 handle = dlopen (file: path, RTLD_LAZY);
79 if (!handle)
80 exit (status: 1);
81 dl_iterate_phdr (callback: callback, data: (void *)(intptr_t) ADD);
82 return handle;
83}
84
85static void
86unload (const char *path, void *handle)
87{
88 printf (format: "unloading `%s'\n", path);
89 if (dlclose (handle: handle) < 0)
90 exit (status: 2);
91 dl_iterate_phdr (callback: callback, data: (void *)(intptr_t) REMOVE);
92}
93
94static int
95do_test (void)
96{
97 void *handle1, *handle2;
98
99 dl_iterate_phdr (callback: callback, data: (void *)(intptr_t) SET);
100 handle1 = load (path: "firstobj.so");
101 handle2 = load (path: "globalmod1.so");
102 unload (path: "firstobj.so", handle: handle1);
103 unload (path: "globalmod1.so", handle: handle2);
104 return 0;
105}
106
107#include <support/test-driver.c>
108

source code of glibc/elf/tst-dlmodcount.c