Home | History | Annotate | Line # | Download | only in kern
kern_ksyms.c revision 1.35.6.4
      1 /*	$NetBSD: kern_ksyms.c,v 1.35.6.4 2009/01/17 20:17:09 mjf 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.35.6.4 2009/01/17 20:17:09 mjf Exp $");
     75 
     76 #if defined(_KERNEL) && defined(_KERNEL_OPT)
     77 #include "opt_ddb.h"
     78 #include "opt_ddbparam.h"	/* for SYMTAB_SPACE */
     79 #endif
     80 
     81 #define _KSYMS_PRIVATE
     82 
     83 #include <sys/param.h>
     84 #include <sys/queue.h>
     85 #include <sys/exec.h>
     86 #include <sys/systm.h>
     87 #include <sys/conf.h>
     88 #include <sys/kmem.h>
     89 #include <sys/proc.h>
     90 #include <sys/atomic.h>
     91 #include <sys/ksyms.h>
     92 
     93 #include <uvm/uvm_extern.h>
     94 
     95 #ifdef DDB
     96 #include <ddb/db_output.h>
     97 #endif
     98 
     99 #include "ksyms.h"
    100 
    101 static int ksyms_maxlen;
    102 static bool ksyms_isopen;
    103 static bool ksyms_initted;
    104 static struct ksyms_hdr ksyms_hdr;
    105 static kmutex_t ksyms_lock;
    106 
    107 void ksymsattach(int);
    108 static int ksymsopen(dev_t, int, int, struct lwp *);
    109 static int ksymsclose(dev_t, int, int, struct lwp *);
    110 static int ksymsread(dev_t, struct uio *, int);
    111 static int ksymswrite(dev_t, struct uio *, int);
    112 static int ksymsioctl(dev_t, u_long, void *, int, struct lwp *);
    113 static void ksyms_hdr_init(void *);
    114 static void ksyms_sizes_calc(void);
    115 
    116 #ifdef KSYMS_DEBUG
    117 #define	FOLLOW_CALLS		1
    118 #define	FOLLOW_MORE_CALLS	2
    119 #define	FOLLOW_DEVKSYMS		4
    120 static int ksyms_debug;
    121 #endif
    122 
    123 #ifdef SYMTAB_SPACE
    124 #define		SYMTAB_FILLER	"|This is the symbol table!"
    125 
    126 char		db_symtab[SYMTAB_SPACE] = SYMTAB_FILLER;
    127 int		db_symtabsize = SYMTAB_SPACE;
    128 #endif
    129 
    130 int ksyms_symsz;
    131 int ksyms_strsz;
    132 TAILQ_HEAD(, ksyms_symtab) ksyms_symtabs =
    133     TAILQ_HEAD_INITIALIZER(ksyms_symtabs);
    134 static struct ksyms_symtab kernel_symtab;
    135 
    136 static int
    137 ksyms_verify(void *symstart, void *strstart)
    138 {
    139 #if defined(DIAGNOSTIC) || defined(DEBUG)
    140 	if (symstart == NULL)
    141 		printf("ksyms: Symbol table not found\n");
    142 	if (strstart == NULL)
    143 		printf("ksyms: String table not found\n");
    144 	if (symstart == NULL || strstart == NULL)
    145 		printf("ksyms: Perhaps the kernel is stripped?\n");
    146 #endif
    147 	if (symstart == NULL || strstart == NULL)
    148 		return 0;
    149 	KASSERT(symstart <= strstart);
    150 	return 1;
    151 }
    152 
    153 /*
    154  * Finds a certain symbol name in a certain symbol table.
    155  */
    156 static Elf_Sym *
    157 findsym(const char *name, struct ksyms_symtab *table, int type)
    158 {
    159 	Elf_Sym *sym, *maxsym;
    160 	int low, mid, high, nglob;
    161 	char *str, *cmp;
    162 
    163 	sym = table->sd_symstart;
    164 	str = table->sd_strstart - table->sd_usroffset;
    165 	nglob = table->sd_nglob;
    166 	low = 0;
    167 	high = nglob;
    168 
    169 	/*
    170 	 * Start with a binary search of all global symbols in this table.
    171 	 * Global symbols must have unique names.
    172 	 */
    173 	while (low < high) {
    174 		mid = (low + high) >> 1;
    175 		cmp = sym[mid].st_name + str;
    176 		if (cmp[0] < name[0] || strcmp(cmp, name) < 0) {
    177 			low = mid + 1;
    178 		} else {
    179 			high = mid;
    180 		}
    181 	}
    182 	KASSERT(low == high);
    183 	if (__predict_true(low < nglob &&
    184 	    strcmp(sym[low].st_name + str, name) == 0)) {
    185 		KASSERT(ELF_ST_BIND(sym[low].st_info) == STB_GLOBAL);
    186 		return &sym[low];
    187 	}
    188 
    189 	/*
    190 	 * Perform a linear search of local symbols (rare).  Many local
    191 	 * symbols with the same name can exist so are not included in
    192 	 * the binary search.
    193 	 */
    194 	if (type != KSYMS_EXTERN) {
    195 		maxsym = sym + table->sd_symsize / sizeof(Elf_Sym);
    196 		for (sym += nglob; sym < maxsym; sym++) {
    197 			if (strcmp(name, sym->st_name + str) == 0) {
    198 				return sym;
    199 			}
    200 		}
    201 	}
    202 	return NULL;
    203 }
    204 
    205 const struct cdevsw ksyms_cdevsw = {
    206 	ksymsopen, ksymsclose, ksymsread, ksymswrite, ksymsioctl,
    207 	nullstop, notty, nopoll, nommap, nullkqfilter, D_OTHER | D_MPSAFE
    208 };
    209 
    210 /*
    211  * The "attach" is in reality done in ksyms_init().
    212  */
    213 void
    214 ksymsattach(int arg)
    215 {
    216 	int maj = cdevsw_lookup_major(&ksyms_cdevsw);
    217 
    218 	device_register_name(makedev(maj, 0), NULL, true, DEV_OTHER, "ksyms");
    219 }
    220 
    221 void
    222 ksyms_init()
    223 {
    224 
    225 	mutex_init(&ksyms_lock, MUTEX_DEFAULT, IPL_NONE);
    226 }
    227 
    228 /*
    229  * Add a symbol table.
    230  * This is intended for use when the symbol table and its corresponding
    231  * string table are easily available.  If they are embedded in an ELF
    232  * image, use addsymtab_elf() instead.
    233  *
    234  * name - Symbol's table name.
    235  * symstart, symsize - Address and size of the symbol table.
    236  * strstart, strsize - Address and size of the string table.
    237  * tab - Symbol table to be updated with this information.
    238  * newstart - Address to which the symbol table has to be copied during
    239  *            shrinking.  If NULL, it is not moved.
    240  */
    241 static const char *addsymtab_strstart;
    242 
    243 static int
    244 addsymtab_compar(const void *a, const void *b)
    245 {
    246 	const Elf_Sym *sa, *sb;
    247 
    248 	sa = a;
    249 	sb = b;
    250 
    251 	/*
    252 	 * Split the symbol table into two, with globals at the start
    253 	 * and locals at the end.
    254 	 */
    255 	if (ELF_ST_BIND(sa->st_info) != ELF_ST_BIND(sb->st_info)) {
    256 		if (ELF_ST_BIND(sa->st_info) == STB_GLOBAL) {
    257 			return -1;
    258 		}
    259 		if (ELF_ST_BIND(sb->st_info) == STB_GLOBAL) {
    260 			return 1;
    261 		}
    262 	}
    263 
    264 	/* Within each band, sort by name. */
    265 	return strcmp(sa->st_name + addsymtab_strstart,
    266 	    sb->st_name + addsymtab_strstart);
    267 }
    268 
    269 static void
    270 addsymtab(const char *name, void *symstart, size_t symsize,
    271 	  void *strstart, size_t strsize, struct ksyms_symtab *tab,
    272 	  void *newstart)
    273 {
    274 	Elf_Sym *sym, *nsym, ts;
    275 	int i, j, n, nglob;
    276 	char *str;
    277 
    278 	tab->sd_symstart = symstart;
    279 	tab->sd_symsize = symsize;
    280 	tab->sd_strstart = strstart;
    281 	tab->sd_strsize = strsize;
    282 	tab->sd_name = name;
    283 	tab->sd_minsym = UINTPTR_MAX;
    284 	tab->sd_maxsym = 0;
    285 	tab->sd_usroffset = 0;
    286 	tab->sd_gone = false;
    287 #ifdef KSYMS_DEBUG
    288 	printf("newstart %p sym %p ksyms_symsz %d str %p strsz %d send %p\n",
    289 	    newstart, symstart, symsize, strstart, strsize,
    290 	    tab->sd_strstart + tab->sd_strsize);
    291 #endif
    292 
    293 	/* Pack symbol table by removing all file name references. */
    294 	sym = tab->sd_symstart;
    295 	nsym = (Elf_Sym *)newstart;
    296 	str = tab->sd_strstart;
    297 	nglob = 0;
    298 	for (i = n = 0; i < tab->sd_symsize/sizeof(Elf_Sym); i++) {
    299 		/*
    300 		 * Remove useless symbols.
    301 		 * Should actually remove all typeless symbols.
    302 		 */
    303 		if (sym[i].st_name == 0)
    304 			continue; /* Skip nameless entries */
    305 		if (sym[i].st_shndx == SHN_UNDEF)
    306 			continue; /* Skip external references */
    307 		if (ELF_ST_TYPE(sym[i].st_info) == STT_FILE)
    308 			continue; /* Skip filenames */
    309 		if (ELF_ST_TYPE(sym[i].st_info) == STT_NOTYPE &&
    310 		    sym[i].st_value == 0 &&
    311 		    strcmp(str + sym[i].st_name, "*ABS*") == 0)
    312 			continue; /* XXX */
    313 		if (ELF_ST_TYPE(sym[i].st_info) == STT_NOTYPE &&
    314 		    strcmp(str + sym[i].st_name, "gcc2_compiled.") == 0)
    315 			continue; /* XXX */
    316 
    317 		/* Save symbol. Set it as an absolute offset */
    318 		nsym[n] = sym[i];
    319 		nsym[n].st_shndx = SHBSS;
    320 		j = strlen(nsym[n].st_name + str) + 1;
    321 		if (j > ksyms_maxlen)
    322 			ksyms_maxlen = j;
    323 		nglob += (ELF_ST_BIND(nsym[n].st_info) == STB_GLOBAL);
    324 
    325 		/* Compute min and max symbols. */
    326 		if (nsym[n].st_value < tab->sd_minsym) {
    327 		    	tab->sd_minsym = nsym[n].st_value;
    328 		}
    329 		if (nsym[n].st_value > tab->sd_maxsym) {
    330 		    	tab->sd_maxsym = nsym[n].st_value;
    331 		}
    332 		n++;
    333 	}
    334 
    335 	/* Fill the rest of the record, and sort the symbols. */
    336 	tab->sd_symstart = nsym;
    337 	tab->sd_symsize = n * sizeof(Elf_Sym);
    338 	tab->sd_nglob = nglob;
    339 	addsymtab_strstart = str;
    340 	if (kheapsort(nsym, n, sizeof(Elf_Sym), addsymtab_compar, &ts) != 0)
    341 		panic("addsymtab");
    342 
    343 	/* ksymsread() is unlocked, so membar. */
    344 	membar_producer();
    345 	TAILQ_INSERT_TAIL(&ksyms_symtabs, tab, sd_queue);
    346 	ksyms_sizes_calc();
    347 	ksyms_initted = true;
    348 }
    349 
    350 /*
    351  * Setup the kernel symbol table stuff.
    352  */
    353 void
    354 ksyms_addsyms_elf(int symsize, void *start, void *end)
    355 {
    356 	int i, j;
    357 	Elf_Shdr *shdr;
    358 	char *symstart = NULL, *strstart = NULL;
    359 	size_t strsize = 0;
    360 	Elf_Ehdr *ehdr;
    361 
    362 #ifdef SYMTAB_SPACE
    363 	if (symsize <= 0 &&
    364 	    strncmp(db_symtab, SYMTAB_FILLER, sizeof(SYMTAB_FILLER))) {
    365 		symsize = db_symtabsize;
    366 		start = db_symtab;
    367 		end = db_symtab + db_symtabsize;
    368 	}
    369 #endif
    370 	if (symsize <= 0) {
    371 		printf("[ Kernel symbol table missing! ]\n");
    372 		return;
    373 	}
    374 
    375 	/* Sanity check */
    376 	if (ALIGNED_POINTER(start, long) == 0) {
    377 		printf("[ Kernel symbol table has bad start address %p ]\n",
    378 		    start);
    379 		return;
    380 	}
    381 
    382 	ehdr = (Elf_Ehdr *)start;
    383 
    384 	/* check if this is a valid ELF header */
    385 	/* No reason to verify arch type, the kernel is actually running! */
    386 	if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG) ||
    387 	    ehdr->e_ident[EI_CLASS] != ELFCLASS ||
    388 	    ehdr->e_version > 1) {
    389 		printf("[ Kernel symbol table invalid! ]\n");
    390 		return; /* nothing to do */
    391 	}
    392 
    393 	/* Loaded header will be scratched in addsymtab */
    394 	ksyms_hdr_init(start);
    395 
    396 	/* Find the symbol table and the corresponding string table. */
    397 	shdr = (Elf_Shdr *)((uint8_t *)start + ehdr->e_shoff);
    398 	for (i = 1; i < ehdr->e_shnum; i++) {
    399 		if (shdr[i].sh_type != SHT_SYMTAB)
    400 			continue;
    401 		if (shdr[i].sh_offset == 0)
    402 			continue;
    403 		symstart = (uint8_t *)start + shdr[i].sh_offset;
    404 		symsize = shdr[i].sh_size;
    405 		j = shdr[i].sh_link;
    406 		if (shdr[j].sh_offset == 0)
    407 			continue; /* Can this happen? */
    408 		strstart = (uint8_t *)start + shdr[j].sh_offset;
    409 		strsize = shdr[j].sh_size;
    410 		break;
    411 	}
    412 
    413 	if (!ksyms_verify(symstart, strstart))
    414 		return;
    415 	addsymtab("netbsd", symstart, symsize, strstart, strsize,
    416 	    &kernel_symtab, start);
    417 
    418 #ifdef DEBUG
    419 	printf("Loaded initial symtab at %p, strtab at %p, # entries %ld\n",
    420 	    kernel_symtab.sd_symstart, kernel_symtab.sd_strstart,
    421 	    (long)kernel_symtab.sd_symsize/sizeof(Elf_Sym));
    422 #endif
    423 }
    424 
    425 /*
    426  * Setup the kernel symbol table stuff.
    427  * Use this when the address of the symbol and string tables are known;
    428  * otherwise use ksyms_init with an ELF image.
    429  * We need to pass a minimal ELF header which will later be completed by
    430  * ksyms_hdr_init and handed off to userland through /dev/ksyms.  We use
    431  * a void *rather than a pointer to avoid exposing the Elf_Ehdr type.
    432  */
    433 void
    434 ksyms_addsyms_explicit(void *ehdr, void *symstart, size_t symsize,
    435 		    void *strstart, size_t strsize)
    436 {
    437 
    438 	if (!ksyms_verify(symstart, strstart))
    439 		return;
    440 
    441 	ksyms_hdr_init(ehdr);
    442 	addsymtab("netbsd", symstart, symsize, strstart, strsize,
    443 	    &kernel_symtab, symstart);
    444 }
    445 
    446 /*
    447  * Get the value associated with a symbol.
    448  * "mod" is the module name, or null if any module.
    449  * "sym" is the symbol name.
    450  * "val" is a pointer to the corresponding value, if call succeeded.
    451  * Returns 0 if success or ENOENT if no such entry.
    452  *
    453  * Call with ksyms_lock, unless known that the symbol table can't change.
    454  */
    455 int
    456 ksyms_getval_unlocked(const char *mod, const char *sym, unsigned long *val,
    457 		      int type)
    458 {
    459 	struct ksyms_symtab *st;
    460 	Elf_Sym *es;
    461 
    462 #ifdef KSYMS_DEBUG
    463 	if (ksyms_debug & FOLLOW_CALLS)
    464 		printf("ksyms_getval_unlocked: mod %s sym %s valp %p\n",
    465 		    mod, sym, val);
    466 #endif
    467 
    468 	TAILQ_FOREACH(st, &ksyms_symtabs, sd_queue) {
    469 		if (__predict_false(st->sd_gone))
    470 			continue;
    471 		if (mod != NULL && strcmp(st->sd_name, mod))
    472 			continue;
    473 		if ((es = findsym(sym, st, type)) != NULL) {
    474 			*val = es->st_value;
    475 			return 0;
    476 		}
    477 	}
    478 	return ENOENT;
    479 }
    480 
    481 int
    482 ksyms_getval(const char *mod, const char *sym, unsigned long *val, int type)
    483 {
    484 	int rc;
    485 
    486 	if (!ksyms_initted)
    487 		return ENOENT;
    488 
    489 	mutex_enter(&ksyms_lock);
    490 	rc = ksyms_getval_unlocked(mod, sym, val, type);
    491 	mutex_exit(&ksyms_lock);
    492 	return rc;
    493 }
    494 
    495 /*
    496  * Get "mod" and "symbol" associated with an address.
    497  * Returns 0 if success or ENOENT if no such entry.
    498  *
    499  * Call with ksyms_lock, unless known that the symbol table can't change.
    500  */
    501 int
    502 ksyms_getname(const char **mod, const char **sym, vaddr_t v, int f)
    503 {
    504 	struct ksyms_symtab *st;
    505 	Elf_Sym *les, *es = NULL;
    506 	vaddr_t laddr = 0;
    507 	const char *lmod = NULL;
    508 	char *stable = NULL;
    509 	int type, i, sz;
    510 
    511 	if (!ksyms_initted)
    512 		return ENOENT;
    513 
    514 	TAILQ_FOREACH(st, &ksyms_symtabs, sd_queue) {
    515 		if (st->sd_gone)
    516 			continue;
    517 		if (v < st->sd_minsym || v > st->sd_maxsym)
    518 			continue;
    519 		sz = st->sd_symsize/sizeof(Elf_Sym);
    520 		for (i = 0; i < sz; i++) {
    521 			les = st->sd_symstart + i;
    522 			type = ELF_ST_TYPE(les->st_info);
    523 
    524 			if ((f & KSYMS_PROC) && (type != STT_FUNC))
    525 				continue;
    526 
    527 			if (type == STT_NOTYPE)
    528 				continue;
    529 
    530 			if (((f & KSYMS_ANY) == 0) &&
    531 			    (type != STT_FUNC) && (type != STT_OBJECT))
    532 				continue;
    533 
    534 			if ((les->st_value <= v) && (les->st_value > laddr)) {
    535 				laddr = les->st_value;
    536 				es = les;
    537 				lmod = st->sd_name;
    538 				stable = st->sd_strstart - st->sd_usroffset;
    539 			}
    540 		}
    541 	}
    542 	if (es == NULL)
    543 		return ENOENT;
    544 	if ((f & KSYMS_EXACT) && (v != es->st_value))
    545 		return ENOENT;
    546 	if (mod)
    547 		*mod = lmod;
    548 	if (sym)
    549 		*sym = stable + es->st_name;
    550 	return 0;
    551 }
    552 
    553 /*
    554  * Add a symbol table from a loadable module.
    555  */
    556 void
    557 ksyms_modload(const char *name, void *symstart, vsize_t symsize,
    558 	      char *strstart, vsize_t strsize)
    559 {
    560 	struct ksyms_symtab *st;
    561 
    562 	st = kmem_zalloc(sizeof(*st), KM_SLEEP);
    563 	mutex_enter(&ksyms_lock);
    564 	addsymtab(name, symstart, symsize, strstart, strsize, st, symstart);
    565 	mutex_exit(&ksyms_lock);
    566 }
    567 
    568 /*
    569  * Remove a symbol table from a loadable module.
    570  */
    571 void
    572 ksyms_modunload(const char *name)
    573 {
    574 	struct ksyms_symtab *st;
    575 
    576 	mutex_enter(&ksyms_lock);
    577 	TAILQ_FOREACH(st, &ksyms_symtabs, sd_queue) {
    578 		if (st->sd_gone)
    579 			continue;
    580 		if (strcmp(name, st->sd_name) != 0)
    581 			continue;
    582 		st->sd_gone = true;
    583 		if (!ksyms_isopen) {
    584 			TAILQ_REMOVE(&ksyms_symtabs, st, sd_queue);
    585 			ksyms_sizes_calc();
    586 			kmem_free(st, sizeof(*st));
    587 		}
    588 		break;
    589 	}
    590 	mutex_exit(&ksyms_lock);
    591 	KASSERT(st != NULL);
    592 }
    593 
    594 #ifdef DDB
    595 /*
    596  * Keep sifting stuff here, to avoid export of ksyms internals.
    597  *
    598  * Systems is expected to be quiescent, so no locking done.
    599  */
    600 int
    601 ksyms_sift(char *mod, char *sym, int mode)
    602 {
    603 	struct ksyms_symtab *st;
    604 	char *sb;
    605 	int i, sz;
    606 
    607 	if (!ksyms_initted)
    608 		return ENOENT;
    609 
    610 	TAILQ_FOREACH(st, &ksyms_symtabs, sd_queue) {
    611 		if (st->sd_gone)
    612 			continue;
    613 		if (mod && strcmp(mod, st->sd_name))
    614 			continue;
    615 		sb = st->sd_strstart - st->sd_usroffset;
    616 
    617 		sz = st->sd_symsize/sizeof(Elf_Sym);
    618 		for (i = 0; i < sz; i++) {
    619 			Elf_Sym *les = st->sd_symstart + i;
    620 			char c;
    621 
    622 			if (strstr(sb + les->st_name, sym) == NULL)
    623 				continue;
    624 
    625 			if (mode == 'F') {
    626 				switch (ELF_ST_TYPE(les->st_info)) {
    627 				case STT_OBJECT:
    628 					c = '+';
    629 					break;
    630 				case STT_FUNC:
    631 					c = '*';
    632 					break;
    633 				case STT_SECTION:
    634 					c = '&';
    635 					break;
    636 				case STT_FILE:
    637 					c = '/';
    638 					break;
    639 				default:
    640 					c = ' ';
    641 					break;
    642 				}
    643 				db_printf("%s%c ", sb + les->st_name, c);
    644 			} else
    645 				db_printf("%s ", sb + les->st_name);
    646 		}
    647 	}
    648 	return ENOENT;
    649 }
    650 #endif /* DDB */
    651 
    652 /*
    653  * In case we exposing the symbol table to the userland using the pseudo-
    654  * device /dev/ksyms, it is easier to provide all the tables as one.
    655  * However, it means we have to change all the st_name fields for the
    656  * symbols so they match the ELF image that the userland will read
    657  * through the device.
    658  *
    659  * The actual (correct) value of st_name is preserved through a global
    660  * offset stored in the symbol table structure.
    661  *
    662  * Call with ksyms_lock held.
    663  */
    664 static void
    665 ksyms_sizes_calc(void)
    666 {
    667         struct ksyms_symtab *st;
    668 	int i, delta;
    669 
    670         ksyms_symsz = ksyms_strsz = 0;
    671         TAILQ_FOREACH(st, &ksyms_symtabs, sd_queue) {
    672 		delta = ksyms_strsz - st->sd_usroffset;
    673 		if (delta != 0) {
    674 			for (i = 0; i < st->sd_symsize/sizeof(Elf_Sym); i++)
    675 				st->sd_symstart[i].st_name += delta;
    676 			st->sd_usroffset = ksyms_strsz;
    677 		}
    678                 ksyms_symsz += st->sd_symsize;
    679                 ksyms_strsz += st->sd_strsize;
    680         }
    681 }
    682 
    683 static void
    684 ksyms_hdr_init(void *hdraddr)
    685 {
    686 
    687 	/* Copy the loaded elf exec header */
    688 	memcpy(&ksyms_hdr.kh_ehdr, hdraddr, sizeof(Elf_Ehdr));
    689 
    690 	/* Set correct program/section header sizes, offsets and numbers */
    691 	ksyms_hdr.kh_ehdr.e_phoff = offsetof(struct ksyms_hdr, kh_phdr[0]);
    692 	ksyms_hdr.kh_ehdr.e_phentsize = sizeof(Elf_Phdr);
    693 	ksyms_hdr.kh_ehdr.e_phnum = NPRGHDR;
    694 	ksyms_hdr.kh_ehdr.e_shoff = offsetof(struct ksyms_hdr, kh_shdr[0]);
    695 	ksyms_hdr.kh_ehdr.e_shentsize = sizeof(Elf_Shdr);
    696 	ksyms_hdr.kh_ehdr.e_shnum = NSECHDR;
    697 	ksyms_hdr.kh_ehdr.e_shstrndx = SHSTRTAB;
    698 
    699 	/* Text/data - fake */
    700 	ksyms_hdr.kh_phdr[0].p_type = PT_LOAD;
    701 	ksyms_hdr.kh_phdr[0].p_memsz = (unsigned long)-1L;
    702 	ksyms_hdr.kh_phdr[0].p_flags = PF_R | PF_X | PF_W;
    703 
    704 	/* First section is null */
    705 
    706 	/* Second section header; ".symtab" */
    707 	ksyms_hdr.kh_shdr[SYMTAB].sh_name = 1; /* Section 3 offset */
    708 	ksyms_hdr.kh_shdr[SYMTAB].sh_type = SHT_SYMTAB;
    709 	ksyms_hdr.kh_shdr[SYMTAB].sh_offset = sizeof(struct ksyms_hdr);
    710 /*	ksyms_hdr.kh_shdr[SYMTAB].sh_size = filled in at open */
    711 	ksyms_hdr.kh_shdr[SYMTAB].sh_link = 2; /* Corresponding strtab */
    712 	ksyms_hdr.kh_shdr[SYMTAB].sh_addralign = sizeof(long);
    713 	ksyms_hdr.kh_shdr[SYMTAB].sh_entsize = sizeof(Elf_Sym);
    714 
    715 	/* Third section header; ".strtab" */
    716 	ksyms_hdr.kh_shdr[STRTAB].sh_name = 9; /* Section 3 offset */
    717 	ksyms_hdr.kh_shdr[STRTAB].sh_type = SHT_STRTAB;
    718 /*	ksyms_hdr.kh_shdr[STRTAB].sh_offset = filled in at open */
    719 /*	ksyms_hdr.kh_shdr[STRTAB].sh_size = filled in at open */
    720 	ksyms_hdr.kh_shdr[STRTAB].sh_addralign = sizeof(char);
    721 
    722 	/* Fourth section, ".shstrtab" */
    723 	ksyms_hdr.kh_shdr[SHSTRTAB].sh_name = 17; /* This section name offset */
    724 	ksyms_hdr.kh_shdr[SHSTRTAB].sh_type = SHT_STRTAB;
    725 	ksyms_hdr.kh_shdr[SHSTRTAB].sh_offset =
    726 	    offsetof(struct ksyms_hdr, kh_strtab);
    727 	ksyms_hdr.kh_shdr[SHSTRTAB].sh_size = SHSTRSIZ;
    728 	ksyms_hdr.kh_shdr[SHSTRTAB].sh_addralign = sizeof(char);
    729 
    730 	/* Fifth section, ".bss". All symbols reside here. */
    731 	ksyms_hdr.kh_shdr[SHBSS].sh_name = 27; /* This section name offset */
    732 	ksyms_hdr.kh_shdr[SHBSS].sh_type = SHT_NOBITS;
    733 	ksyms_hdr.kh_shdr[SHBSS].sh_offset = 0;
    734 	ksyms_hdr.kh_shdr[SHBSS].sh_size = (unsigned long)-1L;
    735 	ksyms_hdr.kh_shdr[SHBSS].sh_addralign = PAGE_SIZE;
    736 	ksyms_hdr.kh_shdr[SHBSS].sh_flags = SHF_ALLOC | SHF_EXECINSTR;
    737 
    738 	/* Set section names */
    739 	strlcpy(&ksyms_hdr.kh_strtab[1], ".symtab",
    740 	    sizeof(ksyms_hdr.kh_strtab) - 1);
    741 	strlcpy(&ksyms_hdr.kh_strtab[9], ".strtab",
    742 	    sizeof(ksyms_hdr.kh_strtab) - 9);
    743 	strlcpy(&ksyms_hdr.kh_strtab[17], ".shstrtab",
    744 	    sizeof(ksyms_hdr.kh_strtab) - 17);
    745 	strlcpy(&ksyms_hdr.kh_strtab[27], ".bss",
    746 	    sizeof(ksyms_hdr.kh_strtab) - 27);
    747 }
    748 
    749 static int
    750 ksymsopen(dev_t dev, int oflags, int devtype, struct lwp *l)
    751 {
    752 
    753 	if (minor(dev) != 0 || !ksyms_initted)
    754 		return ENXIO;
    755 
    756 	/*
    757 	 * Create a "snapshot" of the kernel symbol table.  Setting
    758 	 * ksyms_isopen will prevent symbol tables from being freed.
    759 	 */
    760 	mutex_enter(&ksyms_lock);
    761 	ksyms_hdr.kh_shdr[SYMTAB].sh_size = ksyms_symsz;
    762 	ksyms_hdr.kh_shdr[SYMTAB].sh_info = ksyms_symsz / sizeof(Elf_Sym);
    763 	ksyms_hdr.kh_shdr[STRTAB].sh_offset = ksyms_symsz +
    764 	    ksyms_hdr.kh_shdr[SYMTAB].sh_offset;
    765 	ksyms_hdr.kh_shdr[STRTAB].sh_size = ksyms_strsz;
    766 	ksyms_isopen = true;
    767 	mutex_exit(&ksyms_lock);
    768 
    769 	return 0;
    770 }
    771 
    772 static int
    773 ksymsclose(dev_t dev, int oflags, int devtype, struct lwp *l)
    774 {
    775 	struct ksyms_symtab *st, *next;
    776 	bool resize;
    777 
    778 	/* Discard refernces to symbol tables. */
    779 	mutex_enter(&ksyms_lock);
    780 	ksyms_isopen = false;
    781 	resize = false;
    782 	for (st = TAILQ_FIRST(&ksyms_symtabs); st != NULL; st = next) {
    783 		next = TAILQ_NEXT(st, sd_queue);
    784 		if (st->sd_gone) {
    785 			TAILQ_REMOVE(&ksyms_symtabs, st, sd_queue);
    786 			kmem_free(st, sizeof(*st));
    787 			resize = true;
    788 		}
    789 	}
    790 	if (resize)
    791 		ksyms_sizes_calc();
    792 	mutex_exit(&ksyms_lock);
    793 
    794 	return 0;
    795 }
    796 
    797 static int
    798 ksymsread(dev_t dev, struct uio *uio, int ioflag)
    799 {
    800 	struct ksyms_symtab *st;
    801 	size_t filepos, inpos, off;
    802 	int error;
    803 
    804 	/*
    805 	 * First: Copy out the ELF header.   XXX Lose if ksymsopen()
    806 	 * occurs during read of the header.
    807 	 */
    808 	off = uio->uio_offset;
    809 	if (off < sizeof(struct ksyms_hdr)) {
    810 		error = uiomove((char *)&ksyms_hdr + off,
    811 		    sizeof(struct ksyms_hdr) - off, uio);
    812 		if (error != 0)
    813 			return error;
    814 	}
    815 
    816 	/*
    817 	 * Copy out the symbol table.
    818 	 */
    819 	filepos = sizeof(struct ksyms_hdr);
    820 	TAILQ_FOREACH(st, &ksyms_symtabs, sd_queue) {
    821 		if (uio->uio_resid == 0)
    822 			return 0;
    823 		if (uio->uio_offset <= st->sd_symsize + filepos) {
    824 			inpos = uio->uio_offset - filepos;
    825 			error = uiomove((char *)st->sd_symstart + inpos,
    826 			   st->sd_symsize - inpos, uio);
    827 			if (error != 0)
    828 				return error;
    829 		}
    830 		filepos += st->sd_symsize;
    831 	}
    832 
    833 	/*
    834 	 * Copy out the string table
    835 	 */
    836 	KASSERT(filepos == sizeof(struct ksyms_hdr) +
    837 	    ksyms_hdr.kh_shdr[SYMTAB].sh_size);
    838 	TAILQ_FOREACH(st, &ksyms_symtabs, sd_queue) {
    839 		if (uio->uio_resid == 0)
    840 			return 0;
    841 		if (uio->uio_offset <= st->sd_strsize + filepos) {
    842 			inpos = uio->uio_offset - filepos;
    843 			error = uiomove((char *)st->sd_strstart + inpos,
    844 			   st->sd_strsize - inpos, uio);
    845 			if (error != 0)
    846 				return error;
    847 		}
    848 		filepos += st->sd_strsize;
    849 	}
    850 
    851 	return 0;
    852 }
    853 
    854 static int
    855 ksymswrite(dev_t dev, struct uio *uio, int ioflag)
    856 {
    857 
    858 	return EROFS;
    859 }
    860 
    861 static int
    862 ksymsioctl(dev_t dev, u_long cmd, void *data, int fflag, struct lwp *l)
    863 {
    864 	struct ksyms_gsymbol *kg = (struct ksyms_gsymbol *)data;
    865 	struct ksyms_symtab *st;
    866 	Elf_Sym *sym = NULL, copy;
    867 	unsigned long val;
    868 	int error = 0;
    869 	char *str = NULL;
    870 	int len;
    871 
    872 	/* Read ksyms_maxlen only once while not holding the lock. */
    873 	len = ksyms_maxlen;
    874 
    875 	if (cmd == KIOCGVALUE || cmd == KIOCGSYMBOL) {
    876 		str = kmem_alloc(len, KM_SLEEP);
    877 		if ((error = copyinstr(kg->kg_name, str, len, NULL)) != 0) {
    878 			kmem_free(str, len);
    879 			return error;
    880 		}
    881 	}
    882 
    883 	switch (cmd) {
    884 	case KIOCGVALUE:
    885 		/*
    886 		 * Use the in-kernel symbol lookup code for fast
    887 		 * retreival of a value.
    888 		 */
    889 		error = ksyms_getval(NULL, str, &val, KSYMS_EXTERN);
    890 		if (error == 0)
    891 			error = copyout(&val, kg->kg_value, sizeof(long));
    892 		kmem_free(str, len);
    893 		break;
    894 
    895 	case KIOCGSYMBOL:
    896 		/*
    897 		 * Use the in-kernel symbol lookup code for fast
    898 		 * retreival of a symbol.
    899 		 */
    900 		mutex_enter(&ksyms_lock);
    901 		TAILQ_FOREACH(st, &ksyms_symtabs, sd_queue) {
    902 			if (st->sd_gone)
    903 				continue;
    904 			if ((sym = findsym(str, st, KSYMS_ANY)) == NULL)
    905 				continue;
    906 #ifdef notdef
    907 			/* Skip if bad binding */
    908 			if (ELF_ST_BIND(sym->st_info) != STB_GLOBAL) {
    909 				sym = NULL;
    910 				continue;
    911 			}
    912 #endif
    913 			break;
    914 		}
    915 		if (sym != NULL) {
    916 			memcpy(&copy, sym, sizeof(copy));
    917 			mutex_exit(&ksyms_lock);
    918 			error = copyout(&copy, kg->kg_sym, sizeof(Elf_Sym));
    919 		} else {
    920 			mutex_exit(&ksyms_lock);
    921 			error = ENOENT;
    922 		}
    923 		kmem_free(str, len);
    924 		break;
    925 
    926 	case KIOCGSIZE:
    927 		/*
    928 		 * Get total size of symbol table.
    929 		 */
    930 		mutex_enter(&ksyms_lock);
    931 		*(int *)data = ksyms_strsz + ksyms_symsz +
    932 		    sizeof(struct ksyms_hdr);
    933 		mutex_exit(&ksyms_lock);
    934 		break;
    935 
    936 	default:
    937 		error = ENOTTY;
    938 		break;
    939 	}
    940 
    941 	return error;
    942 }
    943