Home | History | Annotate | Line # | Download | only in ld.elf_so
t_rtld_r_debug.c revision 1.1
      1 /*	$NetBSD: t_rtld_r_debug.c,v 1.1 2020/09/22 01:09:32 kamil 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;
     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(debug->r_version == R_DEBUG_VERSION);
    108 	map = debug->r_map;
    109 	ATF_CHECK(map != NULL);
    110 
    111 	for (found = false; map; map = map->l_next) {
    112 		if (strstr(map->l_name, name) != NULL) {
    113 			if (rmap)
    114 				*rmap = map;
    115 			found = true;
    116 		} else if (strstr(map->l_name, "ld.elf_so") != NULL) {
    117 			loader = (void *)map->l_addr;
    118 		}
    119 	}
    120 	ATF_CHECK(found);
    121 	ATF_CHECK(loader != NULL);
    122 	ATF_CHECK(debug->r_brk != NULL);
    123 	ATF_CHECK(debug->r_state == RT_CONSISTENT);
    124 	ATF_CHECK(debug->r_ldbase == loader);
    125 }
    126 
    127 ATF_TC(self);
    128 ATF_TC_HEAD(self, tc)
    129 {
    130 	atf_tc_set_md_var(tc, "descr", "check whether r_debug is well-formed");
    131 }
    132 ATF_TC_BODY(self, tc)
    133 {
    134 	check_r_debug_return_link_map("t_rtld_r_debug", NULL);
    135 }
    136 
    137 ATF_TC(dlopen);
    138 ATF_TC_HEAD(dlopen, tc)
    139 {
    140 	atf_tc_set_md_var(tc, "descr",
    141 	    "check whether r_debug is well-formed after an dlopen(3) call");
    142 }
    143 ATF_TC_BODY(dlopen, tc)
    144 {
    145 	void *handle;
    146 	struct link_map *map, *r_map;
    147 
    148 	handle = dlopen("libutil.so", RTLD_LAZY);
    149 	ATF_CHECK(handle);
    150 
    151 	check_r_debug_return_link_map("libutil.so", &r_map);
    152 
    153 	RZ(dlinfo(handle, RTLD_DI_LINKMAP, &map));
    154 
    155 	ATF_CHECK(map == r_map);
    156 	dlclose(handle);
    157 }
    158 
    159 ATF_TP_ADD_TCS(tp)
    160 {
    161 	ATF_TP_ADD_TC(tp, self);
    162 	ATF_TP_ADD_TC(tp, dlopen);
    163 	return atf_no_error();
    164 }
    165