Home | History | Annotate | Line # | Download | only in tprof
      1 /*	$NetBSD: ksyms.c,v 1.3 2024/04/01 18:33:24 riastradh Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2010,2011,2012 YAMAMOTO Takashi,
      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 AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  * SUCH DAMAGE.
     27  */
     28 
     29 #include <sys/cdefs.h>
     30 #ifndef lint
     31 __RCSID("$NetBSD: ksyms.c,v 1.3 2024/04/01 18:33:24 riastradh Exp $");
     32 #endif /* not lint */
     33 
     34 #include <assert.h>
     35 #include <err.h>
     36 #include <fcntl.h>
     37 #include <gelf.h>
     38 #include <libelf.h>
     39 #include <paths.h>
     40 #include <stdlib.h>
     41 #include <string.h>
     42 #include <unistd.h>
     43 #include <util.h>
     44 #include "ksyms.h"
     45 
     46 static struct sym **syms = NULL;
     47 static size_t nsyms = 0;
     48 
     49 static int
     50 compare_value(const void *p1, const void *p2)
     51 {
     52 	const struct sym *s1 = *(const struct sym * const *)p1;
     53 	const struct sym *s2 = *(const struct sym * const *)p2;
     54 
     55 	if (s1->value > s2->value) {
     56 		return -1;
     57 	} else if (s1->value < s2->value) {
     58 		return 1;
     59 	}
     60 	/*
     61 	 * to produce a stable result, it's better not to return 0
     62 	 * even for __strong_alias.
     63 	 */
     64 	if (s1->size > s2->size) {
     65 		return -1;
     66 	} else if (s1->size < s2->size) {
     67 		return 1;
     68 	}
     69 	return strcmp(s1->name, s2->name);
     70 }
     71 
     72 struct sym **
     73 ksymload(size_t *nsymp)
     74 {
     75 	Elf *e;
     76 	Elf_Scn *s;
     77 	GElf_Shdr sh_store;
     78 	GElf_Shdr *sh;
     79 	Elf_Data *d;
     80 	int fd;
     81 	size_t size, i;
     82 
     83 	fd = open(_PATH_KSYMS, O_RDONLY);
     84 	if (fd == -1) {
     85 		err(EXIT_FAILURE, "open " _PATH_KSYMS);
     86 	}
     87 	if (elf_version(EV_CURRENT) == EV_NONE) {
     88 		goto elffail;
     89 	}
     90 	e = elf_begin(fd, ELF_C_READ, NULL);
     91 	if (e == NULL) {
     92 		goto elffail;
     93 	}
     94 	for (s = elf_nextscn(e, NULL); s != NULL; s = elf_nextscn(e, s)) {
     95 		sh = gelf_getshdr(s, &sh_store);
     96 		if (sh == NULL) {
     97 			goto elffail;
     98 		}
     99 		if (sh->sh_type == SHT_SYMTAB) {
    100 			break;
    101 		}
    102 	}
    103 	if (s == NULL) {
    104 		errx(EXIT_FAILURE, "no symtab");
    105 	}
    106 	d = elf_getdata(s, NULL);
    107 	if (d == NULL) {
    108 		goto elffail;
    109 	}
    110 	assert(sh->sh_size == d->d_size);
    111 	size = sh->sh_size / sh->sh_entsize;
    112 	for (i = 1; i < size; i++) {
    113 		GElf_Sym st_store;
    114 		GElf_Sym *st;
    115 		struct sym *sym;
    116 
    117 		st = gelf_getsym(d, (int)i, &st_store);
    118 		if (st == NULL) {
    119 			goto elffail;
    120 		}
    121 		if (GELF_ST_TYPE(st->st_info) != STT_FUNC) {
    122 			continue;
    123 		}
    124 		sym = emalloc(sizeof(*sym));
    125 		sym->name = estrdup(elf_strptr(e, sh->sh_link, st->st_name));
    126 		sym->value = (uint64_t)st->st_value;
    127 		sym->size = st->st_size;
    128 		nsyms++;
    129 		syms = erealloc(syms, sizeof(*syms) * nsyms);
    130 		syms[nsyms - 1] = sym;
    131 	}
    132 	elf_end(e);
    133 	close(fd);
    134 	qsort(syms, nsyms, sizeof(*syms), compare_value);
    135 	if (nsymp != NULL)
    136 		*nsymp = nsyms;
    137 	return syms;
    138 elffail:
    139 	errx(EXIT_FAILURE, "libelf: %s", elf_errmsg(elf_errno()));
    140 }
    141 
    142 const char *
    143 ksymlookup(uint64_t value, uint64_t *offset, size_t *n)
    144 {
    145 	size_t hi;
    146 	size_t lo;
    147 	size_t i;
    148 
    149 	/*
    150 	 * try to find the smallest i for which syms[i]->value <= value.
    151 	 * syms[] is ordered by syms[]->value in the descending order.
    152 	 */
    153 
    154 	hi = nsyms - 1;
    155 	lo = 0;
    156 	while (lo < hi) {
    157 		const size_t mid = (lo + hi) / 2;
    158 		const struct sym *sym = syms[mid];
    159 
    160 		assert(syms[lo]->value >= sym->value);
    161 		assert(sym->value >= syms[hi]->value);
    162 		if (sym->value <= value) {
    163 			hi = mid;
    164 			continue;
    165 		}
    166 		lo = mid + 1;
    167 	}
    168 	assert(lo == nsyms - 1 || syms[lo]->value <= value);
    169 	assert(lo == 0 || syms[lo - 1]->value > value);
    170 	for (i = lo; i < nsyms; i++) {
    171 		const struct sym *sym = syms[i];
    172 
    173 		if (sym->value <= value &&
    174 		    (sym->size == 0 || value - sym->value <= sym->size )) {
    175 			*offset = value - sym->value;
    176 			if (n != NULL)
    177 				*n = i;
    178 			return sym->name;
    179 		}
    180 		if (sym->size != 0 && sym->value + sym->size < value) {
    181 			break;
    182 		}
    183 	}
    184 	return NULL;
    185 }
    186