Home | History | Annotate | Line # | Download | only in kern
kern_ksyms.c revision 1.65.2.1
      1 /*	$NetBSD: kern_ksyms.c,v 1.65.2.1 2012/04/17 00:08:24 yamt Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2008 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software developed for The NetBSD Foundation
      8  * by Andrew Doran.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 /*
     33  * Copyright (c) 2001, 2003 Anders Magnusson (ragge (at) ludd.luth.se).
     34  * All rights reserved.
     35  *
     36  * Redistribution and use in source and binary forms, with or without
     37  * modification, are permitted provided that the following conditions
     38  * are met:
     39  * 1. Redistributions of source code must retain the above copyright
     40  *    notice, this list of conditions and the following disclaimer.
     41  * 2. Redistributions in binary form must reproduce the above copyright
     42  *    notice, this list of conditions and the following disclaimer in the
     43  *    documentation and/or other materials provided with the distribution.
     44  * 3. The name of the author may not be used to endorse or promote products
     45  *    derived from this software without specific prior written permission
     46  *
     47  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     48  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     49  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     50  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     51  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     52  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     53  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     54  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     55  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     56  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     57  */
     58 
     59 /*
     60  * Code to deal with in-kernel symbol table management + /dev/ksyms.
     61  *
     62  * For each loaded module the symbol table info is kept track of by a
     63  * struct, placed in a circular list. The first entry is the kernel
     64  * symbol table.
     65  */
     66 
     67 /*
     68  * TODO:
     69  *
     70  *	Add support for mmap, poll.
     71  */
     72 
     73 #include <sys/cdefs.h>
     74 __KERNEL_RCSID(0, "$NetBSD: kern_ksyms.c,v 1.65.2.1 2012/04/17 00:08:24 yamt Exp $");
     75 
     76 #if defined(_KERNEL) && defined(_KERNEL_OPT)
     77 #include "opt_ddb.h"
     78 #include "opt_dtrace.h"
     79 #include "opt_ksyms.h"
     80 #endif
     81 
     82 #define _KSYMS_PRIVATE
     83 
     84 #include <sys/param.h>
     85 #include <sys/queue.h>
     86 #include <sys/exec.h>
     87 #include <sys/systm.h>
     88 #include <sys/conf.h>
     89 #include <sys/kmem.h>
     90 #include <sys/proc.h>
     91 #include <sys/atomic.h>
     92 #include <sys/ksyms.h>
     93 
     94 #ifdef DDB
     95 #include <ddb/db_output.h>
     96 #endif
     97 
     98 #include "ksyms.h"
     99 
    100 #define KSYMS_MAX_ID	65536
    101 #ifdef KDTRACE_HOOKS
    102 static uint32_t ksyms_nmap[KSYMS_MAX_ID];	/* sorted symbol table map */
    103 #else
    104 static uint32_t *ksyms_nmap = NULL;
    105 #endif
    106 
    107 static int ksyms_maxlen;
    108 static bool ksyms_isopen;
    109 static bool ksyms_initted;
    110 static kmutex_t ksyms_lock;
    111 static struct ksyms_symtab kernel_symtab;
    112 
    113 void ksymsattach(int);
    114 static void ksyms_hdr_init(void *);
    115 static void ksyms_sizes_calc(void);
    116 
    117 #ifdef KSYMS_DEBUG
    118 #define	FOLLOW_CALLS		1
    119 #define	FOLLOW_MORE_CALLS	2
    120 #define	FOLLOW_DEVKSYMS		4
    121 static int ksyms_debug;
    122 #endif
    123 
    124 #ifdef SYMTAB_SPACE
    125 #define		SYMTAB_FILLER	"|This is the symbol table!"
    126 
    127 char		db_symtab[SYMTAB_SPACE] = SYMTAB_FILLER;
    128 int		db_symtabsize = SYMTAB_SPACE;
    129 #endif
    130 
    131 /*
    132  * used by savecore(8) so non-static
    133  */
    134 struct ksyms_hdr ksyms_hdr;
    135 int ksyms_symsz;
    136 int ksyms_strsz;
    137 int ksyms_ctfsz;	/* this is not currently used by savecore(8) */
    138 TAILQ_HEAD(, ksyms_symtab) ksyms_symtabs =
    139     TAILQ_HEAD_INITIALIZER(ksyms_symtabs);
    140 
    141 static int
    142 ksyms_verify(void *symstart, void *strstart)
    143 {
    144 #if defined(DIAGNOSTIC) || defined(DEBUG)
    145 	if (symstart == NULL)
    146 		printf("ksyms: Symbol table not found\n");
    147 	if (strstart == NULL)
    148 		printf("ksyms: String table not found\n");
    149 	if (symstart == NULL || strstart == NULL)
    150 		printf("ksyms: Perhaps the kernel is stripped?\n");
    151 #endif
    152 	if (symstart == NULL || strstart == NULL)
    153 		return 0;
    154 	return 1;
    155 }
    156 
    157 /*
    158  * Finds a certain symbol name in a certain symbol table.
    159  */
    160 static Elf_Sym *
    161 findsym(const char *name, struct ksyms_symtab *table, int type)
    162 {
    163 	Elf_Sym *sym, *maxsym;
    164 	int low, mid, high, nglob;
    165 	char *str, *cmp;
    166 
    167 	sym = table->sd_symstart;
    168 	str = table->sd_strstart - table->sd_usroffset;
    169 	nglob = table->sd_nglob;
    170 	low = 0;
    171 	high = nglob;
    172 
    173 	/*
    174 	 * Start with a binary search of all global symbols in this table.
    175 	 * Global symbols must have unique names.
    176 	 */
    177 	while (low < high) {
    178 		mid = (low + high) >> 1;
    179 		cmp = sym[mid].st_name + str;
    180 		if (cmp[0] < name[0] || strcmp(cmp, name) < 0) {
    181 			low = mid + 1;
    182 		} else {
    183 			high = mid;
    184 		}
    185 	}
    186 	KASSERT(low == high);
    187 	if (__predict_true(low < nglob &&
    188 	    strcmp(sym[low].st_name + str, name) == 0)) {
    189 		KASSERT(ELF_ST_BIND(sym[low].st_info) == STB_GLOBAL);
    190 		return &sym[low];
    191 	}
    192 
    193 	/*
    194 	 * Perform a linear search of local symbols (rare).  Many local
    195 	 * symbols with the same name can exist so are not included in
    196 	 * the binary search.
    197 	 */
    198 	if (type != KSYMS_EXTERN) {
    199 		maxsym = sym + table->sd_symsize / sizeof(Elf_Sym);
    200 		for (sym += nglob; sym < maxsym; sym++) {
    201 			if (strcmp(name, sym->st_name + str) == 0) {
    202 				return sym;
    203 			}
    204 		}
    205 	}
    206 	return NULL;
    207 }
    208 
    209 /*
    210  * The "attach" is in reality done in ksyms_init().
    211  */
    212 void
    213 ksymsattach(int arg)
    214 {
    215 
    216 }
    217 
    218 void
    219 ksyms_init(void)
    220 {
    221 
    222 #ifdef SYMTAB_SPACE
    223 	if (!ksyms_initted &&
    224 	    strncmp(db_symtab, SYMTAB_FILLER, sizeof(SYMTAB_FILLER))) {
    225 		ksyms_addsyms_elf(db_symtabsize, db_symtab,
    226 		    db_symtab + db_symtabsize);
    227 	}
    228 #endif
    229 
    230 	mutex_init(&ksyms_lock, MUTEX_DEFAULT, IPL_NONE);
    231 }
    232 
    233 /*
    234  * Add a symbol table.
    235  * This is intended for use when the symbol table and its corresponding
    236  * string table are easily available.  If they are embedded in an ELF
    237  * image, use addsymtab_elf() instead.
    238  *
    239  * name - Symbol's table name.
    240  * symstart, symsize - Address and size of the symbol table.
    241  * strstart, strsize - Address and size of the string table.
    242  * tab - Symbol table to be updated with this information.
    243  * newstart - Address to which the symbol table has to be copied during
    244  *            shrinking.  If NULL, it is not moved.
    245  */
    246 static const char *addsymtab_strstart;
    247 
    248 static int
    249 addsymtab_compar(const void *a, const void *b)
    250 {
    251 	const Elf_Sym *sa, *sb;
    252 
    253 	sa = a;
    254 	sb = b;
    255 
    256 	/*
    257 	 * Split the symbol table into two, with globals at the start
    258 	 * and locals at the end.
    259 	 */
    260 	if (ELF_ST_BIND(sa->st_info) != ELF_ST_BIND(sb->st_info)) {
    261 		if (ELF_ST_BIND(sa->st_info) == STB_GLOBAL) {
    262 			return -1;
    263 		}
    264 		if (ELF_ST_BIND(sb->st_info) == STB_GLOBAL) {
    265 			return 1;
    266 		}
    267 	}
    268 
    269 	/* Within each band, sort by name. */
    270 	return strcmp(sa->st_name + addsymtab_strstart,
    271 	    sb->st_name + addsymtab_strstart);
    272 }
    273 
    274 static void
    275 addsymtab(const char *name, void *symstart, size_t symsize,
    276 	  void *strstart, size_t strsize, struct ksyms_symtab *tab,
    277 	  void *newstart, void *ctfstart, size_t ctfsize, uint32_t *nmap)
    278 {
    279 	Elf_Sym *sym, *nsym, ts;
    280 	int i, j, n, nglob;
    281 	char *str;
    282 	int nsyms = symsize / sizeof(Elf_Sym);
    283 
    284 	/* Sanity check for pre-allocated map table used during startup. */
    285 	if ((nmap == ksyms_nmap) && (nsyms >= KSYMS_MAX_ID)) {
    286 		printf("kern_ksyms: ERROR %d > %d, increase KSYMS_MAX_ID\n",
    287 		    nsyms, KSYMS_MAX_ID);
    288 
    289 		/* truncate for now */
    290 		nsyms = KSYMS_MAX_ID - 1;
    291 	}
    292 
    293 	tab->sd_symstart = symstart;
    294 	tab->sd_symsize = symsize;
    295 	tab->sd_strstart = strstart;
    296 	tab->sd_strsize = strsize;
    297 	tab->sd_name = name;
    298 	tab->sd_minsym = UINTPTR_MAX;
    299 	tab->sd_maxsym = 0;
    300 	tab->sd_usroffset = 0;
    301 	tab->sd_gone = false;
    302 #ifdef KDTRACE_HOOKS
    303 	tab->sd_ctfstart = ctfstart;
    304 	tab->sd_ctfsize = ctfsize;
    305 	tab->sd_nmap = nmap;
    306 	tab->sd_nmapsize = nsyms;
    307 #endif
    308 #ifdef KSYMS_DEBUG
    309 	printf("newstart %p sym %p ksyms_symsz %zu str %p strsz %zu send %p\n",
    310 	    newstart, symstart, symsize, strstart, strsize,
    311 	    tab->sd_strstart + tab->sd_strsize);
    312 #endif
    313 
    314 	if (nmap) {
    315 		memset(nmap, 0, nsyms * sizeof(uint32_t));
    316 	}
    317 
    318 	/* Pack symbol table by removing all file name references. */
    319 	sym = tab->sd_symstart;
    320 	nsym = (Elf_Sym *)newstart;
    321 	str = tab->sd_strstart;
    322 	nglob = 0;
    323 	for (i = n = 0; i < nsyms; i++) {
    324 
    325 	    	/* This breaks CTF mapping, so don't do it when
    326 		 * DTrace is enabled
    327 		 */
    328 #ifndef KDTRACE_HOOKS
    329 		/*
    330 		 * Remove useless symbols.
    331 		 * Should actually remove all typeless symbols.
    332 		 */
    333 		if (sym[i].st_name == 0)
    334 			continue; /* Skip nameless entries */
    335 		if (sym[i].st_shndx == SHN_UNDEF)
    336 			continue; /* Skip external references */
    337 		if (ELF_ST_TYPE(sym[i].st_info) == STT_FILE)
    338 			continue; /* Skip filenames */
    339 		if (ELF_ST_TYPE(sym[i].st_info) == STT_NOTYPE &&
    340 		    sym[i].st_value == 0 &&
    341 		    strcmp(str + sym[i].st_name, "*ABS*") == 0)
    342 			continue; /* XXX */
    343 		if (ELF_ST_TYPE(sym[i].st_info) == STT_NOTYPE &&
    344 		    strcmp(str + sym[i].st_name, "gcc2_compiled.") == 0)
    345 			continue; /* XXX */
    346 #endif
    347 
    348 		/* Save symbol. Set it as an absolute offset */
    349 		nsym[n] = sym[i];
    350 
    351 #ifdef KDTRACE_HOOKS
    352 		if (nmap != NULL) {
    353 			/*
    354 			 * Save the size, replace it with the symbol id so
    355 			 * the mapping can be done after the cleanup and sort.
    356 			 */
    357 			nmap[i] = nsym[n].st_size;
    358 			nsym[n].st_size = i + 1;	/* zero is reserved */
    359 		}
    360 #endif
    361 
    362 		nsym[n].st_shndx = SHBSS;
    363 		j = strlen(nsym[n].st_name + str) + 1;
    364 		if (j > ksyms_maxlen)
    365 			ksyms_maxlen = j;
    366 		nglob += (ELF_ST_BIND(nsym[n].st_info) == STB_GLOBAL);
    367 
    368 		/* Compute min and max symbols. */
    369 		if (strcmp(str + sym[i].st_name, "*ABS*") != 0
    370 		    && ELF_ST_TYPE(nsym[n].st_info) != STT_NOTYPE) {
    371 			if (nsym[n].st_value < tab->sd_minsym) {
    372 				tab->sd_minsym = nsym[n].st_value;
    373 			}
    374 			if (nsym[n].st_value > tab->sd_maxsym) {
    375 				tab->sd_maxsym = nsym[n].st_value;
    376 			}
    377 		}
    378 		n++;
    379 	}
    380 
    381 	/* Fill the rest of the record, and sort the symbols. */
    382 	tab->sd_symstart = nsym;
    383 	tab->sd_symsize = n * sizeof(Elf_Sym);
    384 	tab->sd_nglob = nglob;
    385 	addsymtab_strstart = str;
    386 	if (kheapsort(nsym, n, sizeof(Elf_Sym), addsymtab_compar, &ts) != 0)
    387 		panic("addsymtab");
    388 
    389 #ifdef KDTRACE_HOOKS
    390 	/*
    391 	 * Build the mapping from original symbol id to new symbol table.
    392 	 * Deleted symbols will have a zero map, indices will be one based
    393 	 * instead of zero based.
    394 	 * Resulting map is sd_nmap[original_index] = new_index + 1
    395 	 */
    396 	if (nmap != NULL) {
    397 		int new;
    398 		for (new = 0; new < n; new++) {
    399 			uint32_t orig = nsym[new].st_size - 1;
    400 			uint32_t size = nmap[orig];
    401 
    402 			nmap[orig] = new + 1;
    403 
    404 			/* restore the size */
    405 			nsym[new].st_size = size;
    406 		}
    407 	}
    408 #endif
    409 
    410 	/* ksymsread() is unlocked, so membar. */
    411 	membar_producer();
    412 	TAILQ_INSERT_TAIL(&ksyms_symtabs, tab, sd_queue);
    413 	ksyms_sizes_calc();
    414 	ksyms_initted = true;
    415 }
    416 
    417 /*
    418  * Setup the kernel symbol table stuff.
    419  */
    420 void
    421 ksyms_addsyms_elf(int symsize, void *start, void *end)
    422 {
    423 	int i, j;
    424 	Elf_Shdr *shdr;
    425 	char *symstart = NULL, *strstart = NULL;
    426 	size_t strsize = 0;
    427 	Elf_Ehdr *ehdr;
    428 	char *ctfstart = NULL;
    429 	size_t ctfsize = 0;
    430 
    431 	if (symsize <= 0) {
    432 		printf("[ Kernel symbol table missing! ]\n");
    433 		return;
    434 	}
    435 
    436 	/* Sanity check */
    437 	if (ALIGNED_POINTER(start, long) == 0) {
    438 		printf("[ Kernel symbol table has bad start address %p ]\n",
    439 		    start);
    440 		return;
    441 	}
    442 
    443 	ehdr = (Elf_Ehdr *)start;
    444 
    445 	/* check if this is a valid ELF header */
    446 	/* No reason to verify arch type, the kernel is actually running! */
    447 	if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG) ||
    448 	    ehdr->e_ident[EI_CLASS] != ELFCLASS ||
    449 	    ehdr->e_version > 1) {
    450 		printf("[ Kernel symbol table invalid! ]\n");
    451 		return; /* nothing to do */
    452 	}
    453 
    454 	/* Loaded header will be scratched in addsymtab */
    455 	ksyms_hdr_init(start);
    456 
    457 	/* Find the symbol table and the corresponding string table. */
    458 	shdr = (Elf_Shdr *)((uint8_t *)start + ehdr->e_shoff);
    459 	for (i = 1; i < ehdr->e_shnum; i++) {
    460 		if (shdr[i].sh_type != SHT_SYMTAB)
    461 			continue;
    462 		if (shdr[i].sh_offset == 0)
    463 			continue;
    464 		symstart = (uint8_t *)start + shdr[i].sh_offset;
    465 		symsize = shdr[i].sh_size;
    466 		j = shdr[i].sh_link;
    467 		if (shdr[j].sh_offset == 0)
    468 			continue; /* Can this happen? */
    469 		strstart = (uint8_t *)start + shdr[j].sh_offset;
    470 		strsize = shdr[j].sh_size;
    471 		break;
    472 	}
    473 
    474 #ifdef KDTRACE_HOOKS
    475 	/* Find the CTF section */
    476 	shdr = (Elf_Shdr *)((uint8_t *)start + ehdr->e_shoff);
    477 	if (ehdr->e_shstrndx != 0) {
    478 		char *shstr = (uint8_t *)start +
    479 		    shdr[ehdr->e_shstrndx].sh_offset;
    480 		for (i = 1; i < ehdr->e_shnum; i++) {
    481 #ifdef DEBUG
    482 		    	printf("ksyms: checking %s\n", &shstr[shdr[i].sh_name]);
    483 #endif
    484 			if (shdr[i].sh_type != SHT_PROGBITS)
    485 				continue;
    486 			if (strncmp(".SUNW_ctf", &shstr[shdr[i].sh_name], 10)
    487 			    != 0)
    488 				continue;
    489 			ctfstart = (uint8_t *)start + shdr[i].sh_offset;
    490 			ctfsize = shdr[i].sh_size;
    491 			ksyms_ctfsz = ctfsize;
    492 #ifdef DEBUG
    493 			aprint_normal("Found CTF at %p, size 0x%zx\n",
    494 			    ctfstart, ctfsize);
    495 #endif
    496 			break;
    497 		}
    498 #ifdef DEBUG
    499 	} else {
    500 	    	printf("ksyms: e_shstrndx == 0\n");
    501 #endif
    502 	}
    503 #endif
    504 
    505 	if (!ksyms_verify(symstart, strstart))
    506 		return;
    507 
    508 	addsymtab("netbsd", symstart, symsize, strstart, strsize,
    509 	    &kernel_symtab, start, ctfstart, ctfsize, ksyms_nmap);
    510 
    511 #ifdef DEBUG
    512 	aprint_normal("Loaded initial symtab at %p, strtab at %p, # entries %ld\n",
    513 	    kernel_symtab.sd_symstart, kernel_symtab.sd_strstart,
    514 	    (long)kernel_symtab.sd_symsize/sizeof(Elf_Sym));
    515 #endif
    516 }
    517 
    518 /*
    519  * Setup the kernel symbol table stuff.
    520  * Use this when the address of the symbol and string tables are known;
    521  * otherwise use ksyms_init with an ELF image.
    522  * We need to pass a minimal ELF header which will later be completed by
    523  * ksyms_hdr_init and handed off to userland through /dev/ksyms.  We use
    524  * a void *rather than a pointer to avoid exposing the Elf_Ehdr type.
    525  */
    526 void
    527 ksyms_addsyms_explicit(void *ehdr, void *symstart, size_t symsize,
    528 		    void *strstart, size_t strsize)
    529 {
    530 
    531 	if (!ksyms_verify(symstart, strstart))
    532 		return;
    533 
    534 	ksyms_hdr_init(ehdr);
    535 	addsymtab("netbsd", symstart, symsize, strstart, strsize,
    536 	    &kernel_symtab, symstart, NULL, 0, ksyms_nmap);
    537 }
    538 
    539 /*
    540  * Get the value associated with a symbol.
    541  * "mod" is the module name, or null if any module.
    542  * "sym" is the symbol name.
    543  * "val" is a pointer to the corresponding value, if call succeeded.
    544  * Returns 0 if success or ENOENT if no such entry.
    545  *
    546  * Call with ksyms_lock, unless known that the symbol table can't change.
    547  */
    548 int
    549 ksyms_getval_unlocked(const char *mod, const char *sym, unsigned long *val,
    550 		      int type)
    551 {
    552 	struct ksyms_symtab *st;
    553 	Elf_Sym *es;
    554 
    555 #ifdef KSYMS_DEBUG
    556 	if (ksyms_debug & FOLLOW_CALLS)
    557 		printf("ksyms_getval_unlocked: mod %s sym %s valp %p\n",
    558 		    mod, sym, val);
    559 #endif
    560 
    561 	TAILQ_FOREACH(st, &ksyms_symtabs, sd_queue) {
    562 		if (__predict_false(st->sd_gone))
    563 			continue;
    564 		if (mod != NULL && strcmp(st->sd_name, mod))
    565 			continue;
    566 		if ((es = findsym(sym, st, type)) != NULL) {
    567 			*val = es->st_value;
    568 			return 0;
    569 		}
    570 	}
    571 	return ENOENT;
    572 }
    573 
    574 int
    575 ksyms_getval(const char *mod, const char *sym, unsigned long *val, int type)
    576 {
    577 	int rc;
    578 
    579 	if (!ksyms_initted)
    580 		return ENOENT;
    581 
    582 	mutex_enter(&ksyms_lock);
    583 	rc = ksyms_getval_unlocked(mod, sym, val, type);
    584 	mutex_exit(&ksyms_lock);
    585 	return rc;
    586 }
    587 
    588 struct ksyms_symtab *
    589 ksyms_get_mod(const char *mod)
    590 {
    591 	struct ksyms_symtab *st;
    592 
    593 	mutex_enter(&ksyms_lock);
    594 	TAILQ_FOREACH(st, &ksyms_symtabs, sd_queue) {
    595 		if (__predict_false(st->sd_gone))
    596 			continue;
    597 		if (mod != NULL && strcmp(st->sd_name, mod))
    598 			continue;
    599 		break;
    600 	}
    601 	mutex_exit(&ksyms_lock);
    602 
    603 	return st;
    604 }
    605 
    606 
    607 /*
    608  * ksyms_mod_foreach()
    609  *
    610  * Iterate over the symbol table of the specified module, calling the callback
    611  * handler for each symbol. Stop iterating if the handler return is non-zero.
    612  *
    613  */
    614 
    615 int
    616 ksyms_mod_foreach(const char *mod, ksyms_callback_t callback, void *opaque)
    617 {
    618 	struct ksyms_symtab *st;
    619 	Elf_Sym *sym, *maxsym;
    620 	char *str;
    621 	int symindx;
    622 
    623 	if (!ksyms_initted)
    624 		return ENOENT;
    625 
    626 	mutex_enter(&ksyms_lock);
    627 
    628 	/* find the module */
    629 	TAILQ_FOREACH(st, &ksyms_symtabs, sd_queue) {
    630 		if (__predict_false(st->sd_gone))
    631 			continue;
    632 		if (mod != NULL && strcmp(st->sd_name, mod))
    633 			continue;
    634 
    635 		sym = st->sd_symstart;
    636 		str = st->sd_strstart - st->sd_usroffset;
    637 
    638 		/* now iterate through the symbols */
    639 		maxsym = sym + st->sd_symsize / sizeof(Elf_Sym);
    640 		for (symindx = 0; sym < maxsym; sym++, symindx++) {
    641 			if (callback(str + sym->st_name, symindx,
    642 			    (void *)sym->st_value,
    643 			    sym->st_size,
    644 			    sym->st_info,
    645 			    opaque) != 0) {
    646 				break;
    647 			}
    648 		}
    649 	}
    650 	mutex_exit(&ksyms_lock);
    651 
    652 	return 0;
    653 }
    654 
    655 /*
    656  * Get "mod" and "symbol" associated with an address.
    657  * Returns 0 if success or ENOENT if no such entry.
    658  *
    659  * Call with ksyms_lock, unless known that the symbol table can't change.
    660  */
    661 int
    662 ksyms_getname(const char **mod, const char **sym, vaddr_t v, int f)
    663 {
    664 	struct ksyms_symtab *st;
    665 	Elf_Sym *les, *es = NULL;
    666 	vaddr_t laddr = 0;
    667 	const char *lmod = NULL;
    668 	char *stable = NULL;
    669 	int type, i, sz;
    670 
    671 	if (!ksyms_initted)
    672 		return ENOENT;
    673 
    674 	TAILQ_FOREACH(st, &ksyms_symtabs, sd_queue) {
    675 		if (st->sd_gone)
    676 			continue;
    677 		if (v < st->sd_minsym || v > st->sd_maxsym)
    678 			continue;
    679 		sz = st->sd_symsize/sizeof(Elf_Sym);
    680 		for (i = 0; i < sz; i++) {
    681 			les = st->sd_symstart + i;
    682 			type = ELF_ST_TYPE(les->st_info);
    683 
    684 			if ((f & KSYMS_PROC) && (type != STT_FUNC))
    685 				continue;
    686 
    687 			if (type == STT_NOTYPE)
    688 				continue;
    689 
    690 			if (((f & KSYMS_ANY) == 0) &&
    691 			    (type != STT_FUNC) && (type != STT_OBJECT))
    692 				continue;
    693 
    694 			if ((les->st_value <= v) && (les->st_value > laddr)) {
    695 				laddr = les->st_value;
    696 				es = les;
    697 				lmod = st->sd_name;
    698 				stable = st->sd_strstart - st->sd_usroffset;
    699 			}
    700 		}
    701 	}
    702 	if (es == NULL)
    703 		return ENOENT;
    704 	if ((f & KSYMS_EXACT) && (v != es->st_value))
    705 		return ENOENT;
    706 	if (mod)
    707 		*mod = lmod;
    708 	if (sym)
    709 		*sym = stable + es->st_name;
    710 	return 0;
    711 }
    712 
    713 /*
    714  * Add a symbol table from a loadable module.
    715  */
    716 void
    717 ksyms_modload(const char *name, void *symstart, vsize_t symsize,
    718 	      char *strstart, vsize_t strsize)
    719 {
    720 	struct ksyms_symtab *st;
    721 
    722 	st = kmem_zalloc(sizeof(*st), KM_SLEEP);
    723 	mutex_enter(&ksyms_lock);
    724 	addsymtab(name, symstart, symsize, strstart, strsize, st, symstart,
    725 	    NULL, 0, NULL);
    726 	mutex_exit(&ksyms_lock);
    727 }
    728 
    729 /*
    730  * Remove a symbol table from a loadable module.
    731  */
    732 void
    733 ksyms_modunload(const char *name)
    734 {
    735 	struct ksyms_symtab *st;
    736 
    737 	mutex_enter(&ksyms_lock);
    738 	TAILQ_FOREACH(st, &ksyms_symtabs, sd_queue) {
    739 		if (st->sd_gone)
    740 			continue;
    741 		if (strcmp(name, st->sd_name) != 0)
    742 			continue;
    743 		st->sd_gone = true;
    744 		if (!ksyms_isopen) {
    745 			TAILQ_REMOVE(&ksyms_symtabs, st, sd_queue);
    746 			ksyms_sizes_calc();
    747 			kmem_free(st, sizeof(*st));
    748 		}
    749 		break;
    750 	}
    751 	mutex_exit(&ksyms_lock);
    752 	KASSERT(st != NULL);
    753 }
    754 
    755 #ifdef DDB
    756 /*
    757  * Keep sifting stuff here, to avoid export of ksyms internals.
    758  *
    759  * Systems is expected to be quiescent, so no locking done.
    760  */
    761 int
    762 ksyms_sift(char *mod, char *sym, int mode)
    763 {
    764 	struct ksyms_symtab *st;
    765 	char *sb;
    766 	int i, sz;
    767 
    768 	if (!ksyms_initted)
    769 		return ENOENT;
    770 
    771 	TAILQ_FOREACH(st, &ksyms_symtabs, sd_queue) {
    772 		if (st->sd_gone)
    773 			continue;
    774 		if (mod && strcmp(mod, st->sd_name))
    775 			continue;
    776 		sb = st->sd_strstart - st->sd_usroffset;
    777 
    778 		sz = st->sd_symsize/sizeof(Elf_Sym);
    779 		for (i = 0; i < sz; i++) {
    780 			Elf_Sym *les = st->sd_symstart + i;
    781 			char c;
    782 
    783 			if (strstr(sb + les->st_name, sym) == NULL)
    784 				continue;
    785 
    786 			if (mode == 'F') {
    787 				switch (ELF_ST_TYPE(les->st_info)) {
    788 				case STT_OBJECT:
    789 					c = '+';
    790 					break;
    791 				case STT_FUNC:
    792 					c = '*';
    793 					break;
    794 				case STT_SECTION:
    795 					c = '&';
    796 					break;
    797 				case STT_FILE:
    798 					c = '/';
    799 					break;
    800 				default:
    801 					c = ' ';
    802 					break;
    803 				}
    804 				db_printf("%s%c ", sb + les->st_name, c);
    805 			} else
    806 				db_printf("%s ", sb + les->st_name);
    807 		}
    808 	}
    809 	return ENOENT;
    810 }
    811 #endif /* DDB */
    812 
    813 /*
    814  * In case we exposing the symbol table to the userland using the pseudo-
    815  * device /dev/ksyms, it is easier to provide all the tables as one.
    816  * However, it means we have to change all the st_name fields for the
    817  * symbols so they match the ELF image that the userland will read
    818  * through the device.
    819  *
    820  * The actual (correct) value of st_name is preserved through a global
    821  * offset stored in the symbol table structure.
    822  *
    823  * Call with ksyms_lock held.
    824  */
    825 static void
    826 ksyms_sizes_calc(void)
    827 {
    828         struct ksyms_symtab *st;
    829 	int i, delta;
    830 
    831         ksyms_symsz = ksyms_strsz = 0;
    832         TAILQ_FOREACH(st, &ksyms_symtabs, sd_queue) {
    833 		delta = ksyms_strsz - st->sd_usroffset;
    834 		if (delta != 0) {
    835 			for (i = 0; i < st->sd_symsize/sizeof(Elf_Sym); i++)
    836 				st->sd_symstart[i].st_name += delta;
    837 			st->sd_usroffset = ksyms_strsz;
    838 		}
    839                 ksyms_symsz += st->sd_symsize;
    840                 ksyms_strsz += st->sd_strsize;
    841         }
    842 }
    843 
    844 static void
    845 ksyms_hdr_init(void *hdraddr)
    846 {
    847 
    848 	/* Copy the loaded elf exec header */
    849 	memcpy(&ksyms_hdr.kh_ehdr, hdraddr, sizeof(Elf_Ehdr));
    850 
    851 	/* Set correct program/section header sizes, offsets and numbers */
    852 	ksyms_hdr.kh_ehdr.e_phoff = offsetof(struct ksyms_hdr, kh_phdr[0]);
    853 	ksyms_hdr.kh_ehdr.e_phentsize = sizeof(Elf_Phdr);
    854 	ksyms_hdr.kh_ehdr.e_phnum = NPRGHDR;
    855 	ksyms_hdr.kh_ehdr.e_shoff = offsetof(struct ksyms_hdr, kh_shdr[0]);
    856 	ksyms_hdr.kh_ehdr.e_shentsize = sizeof(Elf_Shdr);
    857 	ksyms_hdr.kh_ehdr.e_shnum = NSECHDR;
    858 	ksyms_hdr.kh_ehdr.e_shstrndx = SHSTRTAB;
    859 
    860 	/* Text/data - fake */
    861 	ksyms_hdr.kh_phdr[0].p_type = PT_LOAD;
    862 	ksyms_hdr.kh_phdr[0].p_memsz = (unsigned long)-1L;
    863 	ksyms_hdr.kh_phdr[0].p_flags = PF_R | PF_X | PF_W;
    864 
    865 	/* First section is null */
    866 
    867 	/* Second section header; ".symtab" */
    868 	ksyms_hdr.kh_shdr[SYMTAB].sh_name = 1; /* Section 3 offset */
    869 	ksyms_hdr.kh_shdr[SYMTAB].sh_type = SHT_SYMTAB;
    870 	ksyms_hdr.kh_shdr[SYMTAB].sh_offset = sizeof(struct ksyms_hdr);
    871 /*	ksyms_hdr.kh_shdr[SYMTAB].sh_size = filled in at open */
    872 	ksyms_hdr.kh_shdr[SYMTAB].sh_link = 2; /* Corresponding strtab */
    873 	ksyms_hdr.kh_shdr[SYMTAB].sh_addralign = sizeof(long);
    874 	ksyms_hdr.kh_shdr[SYMTAB].sh_entsize = sizeof(Elf_Sym);
    875 
    876 	/* Third section header; ".strtab" */
    877 	ksyms_hdr.kh_shdr[STRTAB].sh_name = 9; /* Section 3 offset */
    878 	ksyms_hdr.kh_shdr[STRTAB].sh_type = SHT_STRTAB;
    879 /*	ksyms_hdr.kh_shdr[STRTAB].sh_offset = filled in at open */
    880 /*	ksyms_hdr.kh_shdr[STRTAB].sh_size = filled in at open */
    881 	ksyms_hdr.kh_shdr[STRTAB].sh_addralign = sizeof(char);
    882 
    883 	/* Fourth section, ".shstrtab" */
    884 	ksyms_hdr.kh_shdr[SHSTRTAB].sh_name = 17; /* This section name offset */
    885 	ksyms_hdr.kh_shdr[SHSTRTAB].sh_type = SHT_STRTAB;
    886 	ksyms_hdr.kh_shdr[SHSTRTAB].sh_offset =
    887 	    offsetof(struct ksyms_hdr, kh_strtab);
    888 	ksyms_hdr.kh_shdr[SHSTRTAB].sh_size = SHSTRSIZ;
    889 	ksyms_hdr.kh_shdr[SHSTRTAB].sh_addralign = sizeof(char);
    890 
    891 	/* Fifth section, ".bss". All symbols reside here. */
    892 	ksyms_hdr.kh_shdr[SHBSS].sh_name = 27; /* This section name offset */
    893 	ksyms_hdr.kh_shdr[SHBSS].sh_type = SHT_NOBITS;
    894 	ksyms_hdr.kh_shdr[SHBSS].sh_offset = 0;
    895 	ksyms_hdr.kh_shdr[SHBSS].sh_size = (unsigned long)-1L;
    896 	ksyms_hdr.kh_shdr[SHBSS].sh_addralign = PAGE_SIZE;
    897 	ksyms_hdr.kh_shdr[SHBSS].sh_flags = SHF_ALLOC | SHF_EXECINSTR;
    898 
    899 #ifdef KDTRACE_HOOKS
    900 	/* Sixth section header; ".SUNW_ctf" */
    901 	ksyms_hdr.kh_shdr[SHCTF].sh_name = 32; /* Section 6 offset */
    902 	ksyms_hdr.kh_shdr[SHCTF].sh_type = SHT_PROGBITS;
    903 /*	ksyms_hdr.kh_shdr[SHCTF].sh_offset = filled in at open */
    904 /*	ksyms_hdr.kh_shdr[SHCTF].sh_size = filled in at open */
    905 	ksyms_hdr.kh_shdr[SHCTF].sh_link = SYMTAB; /* Corresponding symtab */
    906 	ksyms_hdr.kh_shdr[SHCTF].sh_addralign = sizeof(char);
    907 #endif
    908 
    909 	/* Set section names */
    910 	strlcpy(&ksyms_hdr.kh_strtab[1], ".symtab",
    911 	    sizeof(ksyms_hdr.kh_strtab) - 1);
    912 	strlcpy(&ksyms_hdr.kh_strtab[9], ".strtab",
    913 	    sizeof(ksyms_hdr.kh_strtab) - 9);
    914 	strlcpy(&ksyms_hdr.kh_strtab[17], ".shstrtab",
    915 	    sizeof(ksyms_hdr.kh_strtab) - 17);
    916 	strlcpy(&ksyms_hdr.kh_strtab[27], ".bss",
    917 	    sizeof(ksyms_hdr.kh_strtab) - 27);
    918 #ifdef KDTRACE_HOOKS
    919 	strlcpy(&ksyms_hdr.kh_strtab[32], ".SUNW_ctf",
    920 	    sizeof(ksyms_hdr.kh_strtab) - 32);
    921 #endif
    922 }
    923 
    924 static int
    925 ksymsopen(dev_t dev, int oflags, int devtype, struct lwp *l)
    926 {
    927 
    928 	if (minor(dev) != 0 || !ksyms_initted)
    929 		return ENXIO;
    930 
    931 	/*
    932 	 * Create a "snapshot" of the kernel symbol table.  Setting
    933 	 * ksyms_isopen will prevent symbol tables from being freed.
    934 	 */
    935 	mutex_enter(&ksyms_lock);
    936 	ksyms_hdr.kh_shdr[SYMTAB].sh_size = ksyms_symsz;
    937 	ksyms_hdr.kh_shdr[SYMTAB].sh_info = ksyms_symsz / sizeof(Elf_Sym);
    938 	ksyms_hdr.kh_shdr[STRTAB].sh_offset = ksyms_symsz +
    939 	    ksyms_hdr.kh_shdr[SYMTAB].sh_offset;
    940 	ksyms_hdr.kh_shdr[STRTAB].sh_size = ksyms_strsz;
    941 #ifdef KDTRACE_HOOKS
    942 	ksyms_hdr.kh_shdr[SHCTF].sh_offset = ksyms_strsz +
    943 	    ksyms_hdr.kh_shdr[STRTAB].sh_offset;
    944 	ksyms_hdr.kh_shdr[SHCTF].sh_size = ksyms_ctfsz;
    945 #endif
    946 	ksyms_isopen = true;
    947 	mutex_exit(&ksyms_lock);
    948 
    949 	return 0;
    950 }
    951 
    952 static int
    953 ksymsclose(dev_t dev, int oflags, int devtype, struct lwp *l)
    954 {
    955 	struct ksyms_symtab *st, *next;
    956 	bool resize;
    957 
    958 	/* Discard refernces to symbol tables. */
    959 	mutex_enter(&ksyms_lock);
    960 	ksyms_isopen = false;
    961 	resize = false;
    962 	for (st = TAILQ_FIRST(&ksyms_symtabs); st != NULL; st = next) {
    963 		next = TAILQ_NEXT(st, sd_queue);
    964 		if (st->sd_gone) {
    965 			TAILQ_REMOVE(&ksyms_symtabs, st, sd_queue);
    966 			kmem_free(st, sizeof(*st));
    967 			resize = true;
    968 		}
    969 	}
    970 	if (resize)
    971 		ksyms_sizes_calc();
    972 	mutex_exit(&ksyms_lock);
    973 
    974 	return 0;
    975 }
    976 
    977 static int
    978 ksymsread(dev_t dev, struct uio *uio, int ioflag)
    979 {
    980 	struct ksyms_symtab *st;
    981 	size_t filepos, inpos, off;
    982 	int error;
    983 #ifdef KDTRACE_HOOKS
    984 	struct ksyms_symtab *cst;
    985 #endif
    986 
    987 	/*
    988 	 * First: Copy out the ELF header.   XXX Lose if ksymsopen()
    989 	 * occurs during read of the header.
    990 	 */
    991 	off = uio->uio_offset;
    992 	if (off < sizeof(struct ksyms_hdr)) {
    993 		error = uiomove((char *)&ksyms_hdr + off,
    994 		    sizeof(struct ksyms_hdr) - off, uio);
    995 		if (error != 0)
    996 			return error;
    997 	}
    998 
    999 	/*
   1000 	 * Copy out the symbol table.
   1001 	 */
   1002 	filepos = sizeof(struct ksyms_hdr);
   1003 	TAILQ_FOREACH(st, &ksyms_symtabs, sd_queue) {
   1004 		if (uio->uio_resid == 0)
   1005 			return 0;
   1006 		if (uio->uio_offset <= st->sd_symsize + filepos) {
   1007 			inpos = uio->uio_offset - filepos;
   1008 			error = uiomove((char *)st->sd_symstart + inpos,
   1009 			   st->sd_symsize - inpos, uio);
   1010 			if (error != 0)
   1011 				return error;
   1012 		}
   1013 		filepos += st->sd_symsize;
   1014 	}
   1015 
   1016 	/*
   1017 	 * Copy out the string table
   1018 	 */
   1019 	KASSERT(filepos == sizeof(struct ksyms_hdr) +
   1020 	    ksyms_hdr.kh_shdr[SYMTAB].sh_size);
   1021 	TAILQ_FOREACH(st, &ksyms_symtabs, sd_queue) {
   1022 		if (uio->uio_resid == 0)
   1023 			return 0;
   1024 		if (uio->uio_offset <= st->sd_strsize + filepos) {
   1025 			inpos = uio->uio_offset - filepos;
   1026 			error = uiomove((char *)st->sd_strstart + inpos,
   1027 			   st->sd_strsize - inpos, uio);
   1028 			if (error != 0)
   1029 				return error;
   1030 		}
   1031 		filepos += st->sd_strsize;
   1032 	}
   1033 
   1034 #ifdef KDTRACE_HOOKS
   1035 	/*
   1036 	 * Copy out the CTF table.
   1037 	 */
   1038 	cst = TAILQ_FIRST(&ksyms_symtabs);
   1039 	if (cst->sd_ctfstart != NULL) {
   1040 		if (uio->uio_resid == 0)
   1041 			return 0;
   1042 		if (uio->uio_offset <= cst->sd_ctfsize + filepos) {
   1043 			inpos = uio->uio_offset - filepos;
   1044 			error = uiomove((char *)cst->sd_ctfstart + inpos,
   1045 			   cst->sd_ctfsize - inpos, uio);
   1046 			if (error != 0)
   1047 				return error;
   1048 		}
   1049 		filepos += cst->sd_ctfsize;
   1050 	}
   1051 #endif
   1052 
   1053 	return 0;
   1054 }
   1055 
   1056 static int
   1057 ksymswrite(dev_t dev, struct uio *uio, int ioflag)
   1058 {
   1059 
   1060 	return EROFS;
   1061 }
   1062 
   1063 static int
   1064 ksymsioctl(dev_t dev, u_long cmd, void *data, int fflag, struct lwp *l)
   1065 {
   1066 	struct ksyms_gsymbol *kg = (struct ksyms_gsymbol *)data;
   1067 	struct ksyms_symtab *st;
   1068 	Elf_Sym *sym = NULL, copy;
   1069 	unsigned long val;
   1070 	int error = 0;
   1071 	char *str = NULL;
   1072 	int len;
   1073 
   1074 	/* Read ksyms_maxlen only once while not holding the lock. */
   1075 	len = ksyms_maxlen;
   1076 
   1077 	if (cmd == KIOCGVALUE || cmd == KIOCGSYMBOL) {
   1078 		str = kmem_alloc(len, KM_SLEEP);
   1079 		if ((error = copyinstr(kg->kg_name, str, len, NULL)) != 0) {
   1080 			kmem_free(str, len);
   1081 			return error;
   1082 		}
   1083 	}
   1084 
   1085 	switch (cmd) {
   1086 	case KIOCGVALUE:
   1087 		/*
   1088 		 * Use the in-kernel symbol lookup code for fast
   1089 		 * retreival of a value.
   1090 		 */
   1091 		error = ksyms_getval(NULL, str, &val, KSYMS_EXTERN);
   1092 		if (error == 0)
   1093 			error = copyout(&val, kg->kg_value, sizeof(long));
   1094 		kmem_free(str, len);
   1095 		break;
   1096 
   1097 	case KIOCGSYMBOL:
   1098 		/*
   1099 		 * Use the in-kernel symbol lookup code for fast
   1100 		 * retreival of a symbol.
   1101 		 */
   1102 		mutex_enter(&ksyms_lock);
   1103 		TAILQ_FOREACH(st, &ksyms_symtabs, sd_queue) {
   1104 			if (st->sd_gone)
   1105 				continue;
   1106 			if ((sym = findsym(str, st, KSYMS_ANY)) == NULL)
   1107 				continue;
   1108 #ifdef notdef
   1109 			/* Skip if bad binding */
   1110 			if (ELF_ST_BIND(sym->st_info) != STB_GLOBAL) {
   1111 				sym = NULL;
   1112 				continue;
   1113 			}
   1114 #endif
   1115 			break;
   1116 		}
   1117 		if (sym != NULL) {
   1118 			memcpy(&copy, sym, sizeof(copy));
   1119 			mutex_exit(&ksyms_lock);
   1120 			error = copyout(&copy, kg->kg_sym, sizeof(Elf_Sym));
   1121 		} else {
   1122 			mutex_exit(&ksyms_lock);
   1123 			error = ENOENT;
   1124 		}
   1125 		kmem_free(str, len);
   1126 		break;
   1127 
   1128 	case KIOCGSIZE:
   1129 		/*
   1130 		 * Get total size of symbol table.
   1131 		 */
   1132 		mutex_enter(&ksyms_lock);
   1133 		*(int *)data = ksyms_strsz + ksyms_symsz +
   1134 		    sizeof(struct ksyms_hdr);
   1135 		mutex_exit(&ksyms_lock);
   1136 		break;
   1137 
   1138 	default:
   1139 		error = ENOTTY;
   1140 		break;
   1141 	}
   1142 
   1143 	return error;
   1144 }
   1145 
   1146 const struct cdevsw ksyms_cdevsw = {
   1147 	ksymsopen, ksymsclose, ksymsread, ksymswrite, ksymsioctl,
   1148 	nullstop, notty, nopoll, nommap, nullkqfilter, D_OTHER | D_MPSAFE
   1149 };
   1150