1 /* $NetBSD: t_rtld_r_debug.c,v 1.6 2024/09/08 09:36:52 rillig 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 ATF_CHECK(phdr != NULL); 64 ATF_CHECK(phnum != (Elf_Half)~0); 65 66 phlimit = phdr + phnum; 67 dynphdr = NULL; 68 69 for (; phdr < phlimit; ++phdr) { 70 if (phdr->p_type == PT_DYNAMIC) 71 dynphdr = phdr; 72 if (phdr->p_type == PT_PHDR) 73 relocbase = (uintptr_t)phdr - phdr->p_vaddr; 74 } 75 76 return (Elf_Dyn *)((uint8_t *)dynphdr->p_vaddr + relocbase); 77 } 78 79 static struct r_debug * 80 get_rtld_r_debug(void) 81 { 82 struct r_debug *debug = NULL; 83 Elf_Dyn *dynp; 84 85 for (dynp = get_dynamic_section(); dynp->d_tag != DT_NULL; dynp++) { 86 if (dynp->d_tag == DT_DEBUG) { 87 debug = (void *)dynp->d_un.d_val; 88 break; 89 } 90 } 91 ATF_CHECK(debug != NULL); 92 93 return debug; 94 } 95 96 static void 97 check_r_debug_return_link_map(const char *name, struct link_map **rmap) 98 { 99 struct r_debug *debug; 100 struct link_map *map; 101 void *loader; 102 bool found; 103 104 loader = NULL; 105 debug = get_rtld_r_debug(); 106 ATF_CHECK(debug != NULL); 107 ATF_CHECK_EQ_MSG(debug->r_version, R_DEBUG_VERSION, 108 "debug->r_version=%d R_DEBUG_VERSION=%d", 109 debug->r_version, R_DEBUG_VERSION); 110 map = debug->r_map; 111 ATF_CHECK(map != NULL); 112 113 for (found = false; map; map = map->l_next) { 114 if (strstr(map->l_name, name) != NULL) { 115 if (rmap) 116 *rmap = map; 117 found = true; 118 } else if (strstr(map->l_name, "ld.elf_so") != NULL) { 119 loader = (void *)map->l_addr; 120 } 121 } 122 ATF_CHECK(found); 123 ATF_CHECK(loader != NULL); 124 ATF_CHECK(debug->r_brk != NULL); 125 ATF_CHECK_EQ_MSG(debug->r_state, RT_CONSISTENT, 126 "debug->r_state=%d RT_CONSISTENT=%d", 127 debug->r_state, RT_CONSISTENT); 128 ATF_CHECK_EQ_MSG(debug->r_ldbase, loader, 129 "debug->r_ldbase=%p loader=%p", 130 debug->r_ldbase, loader); 131 } 132 133 ATF_TC(self); 134 ATF_TC_HEAD(self, tc) 135 { 136 atf_tc_set_md_var(tc, "descr", "check whether r_debug is well-formed"); 137 } 138 ATF_TC_BODY(self, tc) 139 { 140 check_r_debug_return_link_map("t_rtld_r_debug", NULL); 141 } 142 143 ATF_TC(dlopen); 144 ATF_TC_HEAD(dlopen, tc) 145 { 146 atf_tc_set_md_var(tc, "descr", 147 "check whether r_debug is well-formed after a dlopen(3) call"); 148 } 149 ATF_TC_BODY(dlopen, tc) 150 { 151 void *handle; 152 struct link_map *map, *r_map; 153 154 handle = dlopen("libutil.so", RTLD_LAZY); 155 ATF_REQUIRE_MSG(handle, "dlopen: %s", dlerror()); 156 157 check_r_debug_return_link_map("libutil.so", &r_map); 158 159 ATF_REQUIRE_EQ_MSG(dlinfo(handle, RTLD_DI_LINKMAP, &map), 0, 160 "dlinfo: %s", dlerror()); 161 162 ATF_CHECK_EQ_MSG(map, r_map, "map=%p r_map=%p", map, r_map); 163 ATF_CHECK_EQ_MSG(dlclose(handle), 0, "dlclose: %s", dlerror()); 164 } 165 166 ATF_TP_ADD_TCS(tp) 167 { 168 ATF_TP_ADD_TC(tp, self); 169 ATF_TP_ADD_TC(tp, dlopen); 170 return atf_no_error(); 171 } 172