t_rtld_r_debug.c revision 1.10 1 /* $NetBSD: t_rtld_r_debug.c,v 1.10 2025/04/15 22:19:39 riastradh Exp $ */
2
3 /*
4 * Copyright (c) 2020 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
17 * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30 #include <sys/cdefs.h>
31 __RCSID("$NetBSD: t_rtld_r_debug.c,v 1.10 2025/04/15 22:19:39 riastradh Exp $");
32
33 #include <sys/types.h>
34
35 #include <atf-c.h>
36 #include <dlfcn.h>
37 #include <link_elf.h>
38 #include <stdbool.h>
39
40 #include "h_macros.h"
41
42 static long int
43 getauxval(unsigned int type)
44 {
45 const AuxInfo *aux;
46
47 for (aux = _dlauxinfo(); aux->a_type != AT_NULL; ++aux) {
48 if (type == aux->a_type)
49 return aux->a_v;
50 }
51
52 return 0;
53 }
54
55 static Elf_Dyn *
56 get_dynamic_section(void)
57 {
58 uintptr_t relocbase = (uintptr_t)~0U;
59 const Elf_Phdr *phdr;
60 Elf_Half phnum;
61 const Elf_Phdr *phlimit, *dynphdr;
62
63 phdr = (void *)getauxval(AT_PHDR);
64 phnum = (Elf_Half)getauxval(AT_PHNUM);
65
66 printf("AT_PHDR=%p\n", phdr);
67 printf("AT_PHNUM=%d\n", phnum);
68
69 ATF_REQUIRE(phdr != NULL);
70 ATF_REQUIRE(phnum != (Elf_Half)~0);
71
72 phlimit = phdr + phnum;
73 dynphdr = NULL;
74
75 for (; phdr < phlimit; ++phdr) {
76 printf("phdr %p: type=%d flags=0x%x"
77 " vaddr=0x%lx paddr=0x%lx"
78 " filesz=0x%lx memsz=0x%lx"
79 " align=0x%lx\n",
80 phdr, phdr->p_type, phdr->p_flags,
81 (long)phdr->p_vaddr, (long)phdr->p_paddr,
82 (long)phdr->p_filesz, (long)phdr->p_memsz,
83 (long)phdr->p_align);
84 if (phdr->p_type == PT_DYNAMIC)
85 dynphdr = phdr;
86 if (phdr->p_type == PT_PHDR)
87 relocbase = (uintptr_t)phdr - phdr->p_vaddr;
88 }
89
90 return (Elf_Dyn *)((uint8_t *)dynphdr->p_vaddr + relocbase);
91 }
92
93 static struct r_debug *
94 get_rtld_r_debug(void)
95 {
96 struct r_debug *debug = NULL;
97 Elf_Dyn *dynp;
98
99 for (dynp = get_dynamic_section(); dynp->d_tag != DT_NULL; dynp++) {
100 printf("dynp %p: tag=%ld val=0x%lx\n", dynp,
101 (long)dynp->d_tag, (long)dynp->d_un.d_val);
102 if (dynp->d_tag == DT_DEBUG) {
103 debug = (void *)dynp->d_un.d_val;
104 break;
105 }
106 }
107 #ifdef __mips__
108 atf_tc_expect_fail("PR port-mips/59296:"
109 " t_rtld_r_debug test is failing");
110 #endif
111 ATF_REQUIRE(debug != NULL);
112
113 return debug;
114 }
115
116 static void
117 check_r_debug_return_link_map(const char *name, struct link_map **rmap)
118 {
119 struct r_debug *debug;
120 struct link_map *map;
121 void *loader;
122 bool found;
123
124 loader = NULL;
125 debug = get_rtld_r_debug();
126 ATF_REQUIRE(debug != NULL);
127 ATF_CHECK_EQ_MSG(debug->r_version, R_DEBUG_VERSION,
128 "debug->r_version=%d R_DEBUG_VERSION=%d",
129 debug->r_version, R_DEBUG_VERSION);
130 map = debug->r_map;
131 ATF_CHECK(map != NULL);
132
133 for (found = false; map; map = map->l_next) {
134 if (strstr(map->l_name, name) != NULL) {
135 if (rmap)
136 *rmap = map;
137 found = true;
138 } else if (strstr(map->l_name, "ld.elf_so") != NULL) {
139 loader = (void *)map->l_addr;
140 }
141 }
142 ATF_CHECK(found);
143 ATF_CHECK(loader != NULL);
144 ATF_CHECK(debug->r_brk != NULL);
145 ATF_CHECK_EQ_MSG(debug->r_state, RT_CONSISTENT,
146 "debug->r_state=%d RT_CONSISTENT=%d",
147 debug->r_state, RT_CONSISTENT);
148 ATF_CHECK_EQ_MSG(debug->r_ldbase, loader,
149 "debug->r_ldbase=%p loader=%p",
150 debug->r_ldbase, loader);
151 }
152
153 ATF_TC(self);
154 ATF_TC_HEAD(self, tc)
155 {
156 atf_tc_set_md_var(tc, "descr", "check whether r_debug is well-formed");
157 }
158 ATF_TC_BODY(self, tc)
159 {
160 check_r_debug_return_link_map("t_rtld_r_debug", NULL);
161 }
162
163 ATF_TC(dlopen);
164 ATF_TC_HEAD(dlopen, tc)
165 {
166 atf_tc_set_md_var(tc, "descr",
167 "check whether r_debug is well-formed after a dlopen(3) call");
168 }
169 ATF_TC_BODY(dlopen, tc)
170 {
171 void *handle;
172 struct link_map *map, *r_map;
173
174 handle = dlopen("libutil.so", RTLD_LAZY);
175 ATF_REQUIRE_MSG(handle, "dlopen: %s", dlerror());
176
177 check_r_debug_return_link_map("libutil.so", &r_map);
178
179 ATF_REQUIRE_EQ_MSG(dlinfo(handle, RTLD_DI_LINKMAP, &map), 0,
180 "dlinfo: %s", dlerror());
181
182 ATF_CHECK_EQ_MSG(map, r_map, "map=%p r_map=%p", map, r_map);
183 ATF_CHECK_EQ_MSG(dlclose(handle), 0, "dlclose: %s", dlerror());
184 }
185
186 ATF_TP_ADD_TCS(tp)
187 {
188 ATF_TP_ADD_TC(tp, self);
189 ATF_TP_ADD_TC(tp, dlopen);
190 return atf_no_error();
191 }
192