t_rtld_r_debug.c revision 1.9 1 /* $NetBSD: t_rtld_r_debug.c,v 1.9 2025/04/14 02:07:07 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/types.h>
31
32 #include <atf-c.h>
33 #include <dlfcn.h>
34 #include <link_elf.h>
35 #include <stdbool.h>
36
37 #include "h_macros.h"
38
39 static long int
40 getauxval(unsigned int type)
41 {
42 const AuxInfo *aux;
43
44 for (aux = _dlauxinfo(); aux->a_type != AT_NULL; ++aux) {
45 if (type == aux->a_type)
46 return aux->a_v;
47 }
48
49 return 0;
50 }
51
52 static Elf_Dyn *
53 get_dynamic_section(void)
54 {
55 uintptr_t relocbase = (uintptr_t)~0U;
56 const Elf_Phdr *phdr;
57 Elf_Half phnum;
58 const Elf_Phdr *phlimit, *dynphdr;
59
60 phdr = (void *)getauxval(AT_PHDR);
61 phnum = (Elf_Half)getauxval(AT_PHNUM);
62
63 printf("AT_PHDR=%p\n", phdr);
64 printf("AT_PHNUM=%d\n", phnum);
65
66 ATF_REQUIRE(phdr != NULL);
67 ATF_REQUIRE(phnum != (Elf_Half)~0);
68
69 phlimit = phdr + phnum;
70 dynphdr = NULL;
71
72 for (; phdr < phlimit; ++phdr) {
73 printf("phdr %p: type=%d flags=0x%x"
74 " vaddr=0x%lx paddr=0x%lx"
75 " filesz=0x%lx memsz=0x%lx"
76 " align=0x%lx\n",
77 phdr, phdr->p_type, phdr->p_flags,
78 (long)phdr->p_vaddr, (long)phdr->p_paddr,
79 (long)phdr->p_filesz, (long)phdr->p_memsz,
80 (long)phdr->p_align);
81 if (phdr->p_type == PT_DYNAMIC)
82 dynphdr = phdr;
83 if (phdr->p_type == PT_PHDR)
84 relocbase = (uintptr_t)phdr - phdr->p_vaddr;
85 }
86
87 return (Elf_Dyn *)((uint8_t *)dynphdr->p_vaddr + relocbase);
88 }
89
90 static struct r_debug *
91 get_rtld_r_debug(void)
92 {
93 struct r_debug *debug = NULL;
94 Elf_Dyn *dynp;
95
96 for (dynp = get_dynamic_section(); dynp->d_tag != DT_NULL; dynp++) {
97 printf("dynp %p: tag=%ld val=0x%lx\n", dynp,
98 (long)dynp->d_tag, (long)dynp->d_un.d_val);
99 if (dynp->d_tag == DT_DEBUG) {
100 debug = (void *)dynp->d_un.d_val;
101 break;
102 }
103 }
104 ATF_REQUIRE(debug != NULL);
105
106 return debug;
107 }
108
109 static void
110 check_r_debug_return_link_map(const char *name, struct link_map **rmap)
111 {
112 struct r_debug *debug;
113 struct link_map *map;
114 void *loader;
115 bool found;
116
117 loader = NULL;
118 debug = get_rtld_r_debug();
119 #ifdef __mips__
120 atf_tc_expect_fail("PR port-mips/59296:"
121 " t_rtld_r_debug test is failing");
122 #endif
123 ATF_REQUIRE(debug != NULL);
124 ATF_CHECK_EQ_MSG(debug->r_version, R_DEBUG_VERSION,
125 "debug->r_version=%d R_DEBUG_VERSION=%d",
126 debug->r_version, R_DEBUG_VERSION);
127 map = debug->r_map;
128 ATF_CHECK(map != NULL);
129
130 for (found = false; map; map = map->l_next) {
131 if (strstr(map->l_name, name) != NULL) {
132 if (rmap)
133 *rmap = map;
134 found = true;
135 } else if (strstr(map->l_name, "ld.elf_so") != NULL) {
136 loader = (void *)map->l_addr;
137 }
138 }
139 ATF_CHECK(found);
140 ATF_CHECK(loader != NULL);
141 ATF_CHECK(debug->r_brk != NULL);
142 ATF_CHECK_EQ_MSG(debug->r_state, RT_CONSISTENT,
143 "debug->r_state=%d RT_CONSISTENT=%d",
144 debug->r_state, RT_CONSISTENT);
145 ATF_CHECK_EQ_MSG(debug->r_ldbase, loader,
146 "debug->r_ldbase=%p loader=%p",
147 debug->r_ldbase, loader);
148 }
149
150 ATF_TC(self);
151 ATF_TC_HEAD(self, tc)
152 {
153 atf_tc_set_md_var(tc, "descr", "check whether r_debug is well-formed");
154 }
155 ATF_TC_BODY(self, tc)
156 {
157 check_r_debug_return_link_map("t_rtld_r_debug", NULL);
158 }
159
160 ATF_TC(dlopen);
161 ATF_TC_HEAD(dlopen, tc)
162 {
163 atf_tc_set_md_var(tc, "descr",
164 "check whether r_debug is well-formed after a dlopen(3) call");
165 }
166 ATF_TC_BODY(dlopen, tc)
167 {
168 void *handle;
169 struct link_map *map, *r_map;
170
171 handle = dlopen("libutil.so", RTLD_LAZY);
172 ATF_REQUIRE_MSG(handle, "dlopen: %s", dlerror());
173
174 check_r_debug_return_link_map("libutil.so", &r_map);
175
176 ATF_REQUIRE_EQ_MSG(dlinfo(handle, RTLD_DI_LINKMAP, &map), 0,
177 "dlinfo: %s", dlerror());
178
179 ATF_CHECK_EQ_MSG(map, r_map, "map=%p r_map=%p", map, r_map);
180 ATF_CHECK_EQ_MSG(dlclose(handle), 0, "dlclose: %s", dlerror());
181 }
182
183 ATF_TP_ADD_TCS(tp)
184 {
185 ATF_TP_ADD_TC(tp, self);
186 ATF_TP_ADD_TC(tp, dlopen);
187 return atf_no_error();
188 }
189