Home | History | Annotate | Line # | Download | only in kern
kern_ksyms.c revision 1.41
      1 /*	$NetBSD: kern_ksyms.c,v 1.41 2008/10/24 13:55:42 christos 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  *	Consider replacing patricia tree with simpler binary search
     71  *	for symbol tables.
     72  *
     73  *	Add support for mmap, poll.
     74  */
     75 
     76 #include <sys/cdefs.h>
     77 __KERNEL_RCSID(0, "$NetBSD: kern_ksyms.c,v 1.41 2008/10/24 13:55:42 christos Exp $");
     78 
     79 #ifdef _KERNEL
     80 #include "opt_ddb.h"
     81 #include "opt_ddbparam.h"	/* for SYMTAB_SPACE */
     82 #endif
     83 
     84 #define _KSYMS_PRIVATE
     85 
     86 #include <sys/param.h>
     87 #include <sys/errno.h>
     88 #include <sys/queue.h>
     89 #include <sys/exec.h>
     90 #include <sys/systm.h>
     91 #include <sys/conf.h>
     92 #include <sys/malloc.h>
     93 #include <sys/kmem.h>
     94 #include <sys/proc.h>
     95 #include <sys/module.h>
     96 #include <sys/atomic.h>
     97 #include <sys/ksyms.h>
     98 
     99 #include <lib/libkern/libkern.h>
    100 
    101 #ifdef DDB
    102 #include <ddb/db_output.h>
    103 #endif
    104 
    105 #include "ksyms.h"
    106 
    107 static int ksyms_maxlen;
    108 static bool ksyms_isopen;
    109 static bool ksyms_initted;
    110 static struct ksyms_hdr ksyms_hdr;
    111 static kmutex_t ksyms_lock;
    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 int ksyms_symsz;
    132 int ksyms_strsz;
    133 TAILQ_HEAD(, ksyms_symtab) ksyms_symtabs =
    134     TAILQ_HEAD_INITIALIZER(ksyms_symtabs);
    135 static struct ksyms_symtab kernel_symtab;
    136 
    137 /*
    138  * Patricia-tree-based lookup structure for the in-kernel global symbols.
    139  * Based on a design by Mikael Sundstrom, msm (at) sm.luth.se.
    140  */
    141 struct ptree {
    142 	int16_t bitno;
    143 	int16_t lr[2];
    144 } *symb;
    145 static int16_t baseidx;
    146 static int treex = 1;
    147 
    148 #define	P_BIT(key, bit) ((key[bit >> 3] >> (bit & 7)) & 1)
    149 #define	STRING(idx) (kernel_symtab.sd_symstart[idx].st_name + \
    150 			kernel_symtab.sd_strstart)
    151 
    152 static int
    153 ksyms_verify(void *symstart, void *strstart)
    154 {
    155 #if defined(DIAGNOSTIC) || defined(DEBUG)
    156 	if (symstart == NULL)
    157 		printf("ksyms: Symbol table not found\n");
    158 	if (strstart == NULL)
    159 		printf("ksyms: String table not found\n");
    160 	if (symstart == NULL || strstart == NULL)
    161 		printf("ksyms: Perhaps the kernel is stripped?\n");
    162 #endif
    163 	if (symstart == NULL || strstart == NULL)
    164 		return 0;
    165 	KASSERT(symstart <= strstart);
    166 	return 1;
    167 }
    168 
    169 /*
    170  * Walk down the tree until a terminal node is found.
    171  */
    172 static int
    173 symbol_traverse(const char *key)
    174 {
    175 	int16_t nb, rbit = baseidx;
    176 
    177 	while (rbit > 0) {
    178 		nb = symb[rbit].bitno;
    179 		rbit = symb[rbit].lr[P_BIT(key, nb)];
    180 	}
    181 	return -rbit;
    182 }
    183 
    184 static int
    185 ptree_add(char *key, int val)
    186 {
    187 	int idx;
    188 	int nix, cix, bit, rbit, sb, lastrbit, svbit = 0, ix;
    189 	char *m, *k;
    190 
    191 	if (baseidx == 0) {
    192 		baseidx = -val;
    193 		return 0; /* First element */
    194 	}
    195 
    196 	/* Get string to match against */
    197 	idx = symbol_traverse(key);
    198 
    199 	/* Find first mismatching bit */
    200 	m = STRING(idx);
    201 	k = key;
    202 	if (strcmp(m, k) == 0)
    203 		return 1;
    204 
    205 	for (cix = 0; *m && *k && *m == *k; m++, k++, cix += 8)
    206 		;
    207 	ix = ffs((int)*m ^ (int)*k) - 1;
    208 	cix += ix;
    209 
    210 	/* Create new node */
    211 	nix = treex++;
    212 	bit = P_BIT(key, cix);
    213 	symb[nix].bitno = cix;
    214 	symb[nix].lr[bit] = -val;
    215 
    216 	/* Find where to insert node */
    217 	rbit = baseidx;
    218 	lastrbit = 0;
    219 	for (;;) {
    220 		if (rbit < 0)
    221 			break;
    222 		sb = symb[rbit].bitno;
    223 		if (sb > cix)
    224 			break;
    225 		if (sb == cix)
    226 			printf("symb[rbit].bitno == cix!!!\n");
    227 		lastrbit = rbit;
    228 		svbit = P_BIT(key, sb);
    229 		rbit = symb[rbit].lr[svbit];
    230 	}
    231 
    232 	/* Do the actual insertion */
    233 	if (lastrbit == 0) {
    234 		/* first element */
    235 		symb[nix].lr[!bit] = baseidx;
    236 		baseidx = nix;
    237 	} else {
    238 		symb[nix].lr[!bit] = rbit;
    239 		symb[lastrbit].lr[svbit] = nix;
    240 	}
    241 	return 0;
    242 }
    243 
    244 static int
    245 ptree_find(const char *key)
    246 {
    247 	int idx;
    248 
    249 	if (baseidx == 0)
    250 		return 0;
    251 	idx = symbol_traverse(key);
    252 
    253 	if (strcmp(key, STRING(idx)) == 0)
    254 		return idx;
    255 	return 0;
    256 }
    257 
    258 static void
    259 ptree_gen(char *off, struct ksyms_symtab *tab)
    260 {
    261 	Elf_Sym *sym;
    262 	int i, nsym;
    263 
    264 	if (off != NULL)
    265 		symb = (struct ptree *)ALIGN(off);
    266 	else
    267 		symb = malloc((tab->sd_symsize/sizeof(Elf_Sym)) *
    268 		    sizeof(struct ptree), M_DEVBUF, M_WAITOK);
    269 	symb--; /* sym index won't be 0 */
    270 
    271 	sym = tab->sd_symstart;
    272 	if ((nsym = tab->sd_symsize/sizeof(Elf_Sym)) > INT16_MAX) {
    273 		printf("Too many symbols for tree, skipping %d symbols\n",
    274 		    nsym-INT16_MAX);
    275 		nsym = INT16_MAX;
    276 	}
    277 	for (i = 1; i < nsym; i++) {
    278 		if (ELF_ST_BIND(sym[i].st_info) != STB_GLOBAL)
    279 			continue;
    280 		ptree_add(tab->sd_strstart+sym[i].st_name, i);
    281 		if (tab->sd_minsym == NULL ||
    282 		    sym[i].st_value < tab->sd_minsym->st_value)
    283 			tab->sd_minsym = &sym[i];
    284 		if (tab->sd_maxsym == NULL ||
    285 		    sym[i].st_value > tab->sd_maxsym->st_value)
    286 			tab->sd_maxsym = &sym[i];
    287 	}
    288 }
    289 
    290 /*
    291  * Finds a certain symbol name in a certain symbol table.
    292  */
    293 static Elf_Sym *
    294 findsym(const char *name, struct ksyms_symtab *table)
    295 {
    296 	Elf_Sym *start = table->sd_symstart;
    297 	int i, sz = table->sd_symsize/sizeof(Elf_Sym);
    298 	char *np;
    299 	char *realstart = table->sd_strstart - table->sd_usroffset;
    300 
    301 	if (table == &kernel_symtab && (i = ptree_find(name)) != 0)
    302 		return &start[i];
    303 
    304 	for (i = 0; i < sz; i++) {
    305 		np = realstart + start[i].st_name;
    306 		if (name[0] == np[0] && name[1] == np[1] &&
    307 		    strcmp(name, np) == 0)
    308 			return &start[i];
    309 	}
    310 	return NULL;
    311 }
    312 
    313 /*
    314  * The "attach" is in reality done in ksyms_init().
    315  */
    316 void
    317 ksymsattach(int arg)
    318 {
    319 	if (baseidx == 0)
    320 		ptree_gen(0, &kernel_symtab);
    321 }
    322 
    323 /*
    324  * Add a symbol table.
    325  * This is intended for use when the symbol table and its corresponding
    326  * string table are easily available.  If they are embedded in an ELF
    327  * image, use addsymtab_elf() instead.
    328  *
    329  * name - Symbol's table name.
    330  * symstart, symsize - Address and size of the symbol table.
    331  * strstart, strsize - Address and size of the string table.
    332  * tab - Symbol table to be updated with this information.
    333  * newstart - Address to which the symbol table has to be copied during
    334  *            shrinking.  If NULL, it is not moved.
    335  */
    336 static void
    337 addsymtab(const char *name, void *symstart, size_t symsize,
    338 	  void *strstart, size_t strsize, struct ksyms_symtab *tab,
    339 	  void *newstart)
    340 {
    341 	Elf_Sym *sym, *nsym;
    342 	int i, j, n;
    343 	char *str;
    344 
    345 	tab->sd_symstart = symstart;
    346 	tab->sd_symsize = symsize;
    347 	tab->sd_strstart = strstart;
    348 	tab->sd_strsize = strsize;
    349 	tab->sd_name = name;
    350 	tab->sd_minsym = NULL;
    351 	tab->sd_maxsym = NULL;
    352 	tab->sd_usroffset = 0;
    353 	tab->sd_gone = false;
    354 	tab->sd_malloc = false;	/* XXXLKM */
    355 #ifdef KSYMS_DEBUG
    356 	printf("newstart %p sym %p ksyms_symsz %d str %p strsz %d send %p\n",
    357 	    newstart, symstart, symsize, strstart, strsize,
    358 	    tab->sd_strstart + tab->sd_strsize);
    359 #endif
    360 
    361 	/* Pack symbol table by removing all file name references. */
    362 	sym = tab->sd_symstart;
    363 	nsym = (Elf_Sym *)newstart;
    364 	str = tab->sd_strstart;
    365 	for (i = n = 0; i < tab->sd_symsize/sizeof(Elf_Sym); i++) {
    366 		/*
    367 		 * Remove useless symbols.
    368 		 * Should actually remove all typeless symbols.
    369 		 */
    370 		if (sym[i].st_name == 0)
    371 			continue; /* Skip nameless entries */
    372 		if (sym[i].st_shndx == SHN_UNDEF)
    373 			continue; /* Skip external references */
    374 		if (ELF_ST_TYPE(sym[i].st_info) == STT_FILE)
    375 			continue; /* Skip filenames */
    376 		if (ELF_ST_TYPE(sym[i].st_info) == STT_NOTYPE &&
    377 		    sym[i].st_value == 0 &&
    378 		    strcmp(str + sym[i].st_name, "*ABS*") == 0)
    379 			continue; /* XXX */
    380 		if (ELF_ST_TYPE(sym[i].st_info) == STT_NOTYPE &&
    381 		    strcmp(str + sym[i].st_name, "gcc2_compiled.") == 0)
    382 			continue; /* XXX */
    383 
    384 		/* Save symbol. Set it as an absolute offset */
    385 		nsym[n] = sym[i];
    386 		nsym[n].st_shndx = SHN_ABS;
    387 		j = strlen(nsym[n].st_name + tab->sd_strstart) + 1;
    388 		if (j > ksyms_maxlen)
    389 			ksyms_maxlen = j;
    390 		n++;
    391 
    392 	}
    393 	tab->sd_symstart = nsym;
    394 	tab->sd_symsize = n * sizeof(Elf_Sym);
    395 	/* ksymsread() is unlocked, so membar. */
    396 	membar_producer();
    397 	TAILQ_INSERT_TAIL(&ksyms_symtabs, tab, sd_queue);
    398 	ksyms_sizes_calc();
    399 	ksyms_initted = true;
    400 }
    401 
    402 /*
    403  * Setup the kernel symbol table stuff.
    404  */
    405 void
    406 ksyms_init(int symsize, void *start, void *end)
    407 {
    408 	int i, j;
    409 	Elf_Shdr *shdr;
    410 	char *symstart = NULL, *strstart = NULL;
    411 	size_t strsize = 0;
    412 	Elf_Ehdr *ehdr;
    413 
    414 	mutex_init(&ksyms_lock, MUTEX_DEFAULT, IPL_NONE);
    415 #ifdef SYMTAB_SPACE
    416 	if (symsize <= 0 &&
    417 	    strncmp(db_symtab, SYMTAB_FILLER, sizeof(SYMTAB_FILLER))) {
    418 		symsize = db_symtabsize;
    419 		start = db_symtab;
    420 		end = db_symtab + db_symtabsize;
    421 	}
    422 #endif
    423 	if (symsize <= 0) {
    424 		printf("[ Kernel symbol table missing! ]\n");
    425 		return;
    426 	}
    427 
    428 	/* Sanity check */
    429 	if (ALIGNED_POINTER(start, long) == 0) {
    430 		printf("[ Kernel symbol table has bad start address %p ]\n",
    431 		    start);
    432 		return;
    433 	}
    434 
    435 	ehdr = (Elf_Ehdr *)start;
    436 
    437 	/* check if this is a valid ELF header */
    438 	/* No reason to verify arch type, the kernel is actually running! */
    439 	if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG) ||
    440 	    ehdr->e_ident[EI_CLASS] != ELFCLASS ||
    441 	    ehdr->e_version > 1) {
    442 		printf("[ Kernel symbol table invalid! ]\n");
    443 		return; /* nothing to do */
    444 	}
    445 
    446 	/* Loaded header will be scratched in addsymtab */
    447 	ksyms_hdr_init(start);
    448 
    449 	/* Find the symbol table and the corresponding string table. */
    450 	shdr = (Elf_Shdr *)((uint8_t *)start + ehdr->e_shoff);
    451 	for (i = 1; i < ehdr->e_shnum; i++) {
    452 		if (shdr[i].sh_type != SHT_SYMTAB)
    453 			continue;
    454 		if (shdr[i].sh_offset == 0)
    455 			continue;
    456 		symstart = (uint8_t *)start + shdr[i].sh_offset;
    457 		symsize = shdr[i].sh_size;
    458 		j = shdr[i].sh_link;
    459 		if (shdr[j].sh_offset == 0)
    460 			continue; /* Can this happen? */
    461 		strstart = (uint8_t *)start + shdr[j].sh_offset;
    462 		strsize = shdr[j].sh_size;
    463 		break;
    464 	}
    465 
    466 	if (!ksyms_verify(symstart, strstart))
    467 		return;
    468 	addsymtab("netbsd", symstart, symsize, strstart, strsize,
    469 	    &kernel_symtab, start);
    470 
    471 #ifdef DEBUG
    472 	printf("Loaded initial symtab at %p, strtab at %p, # entries %ld\n",
    473 	    kernel_symtab.sd_symstart, kernel_symtab.sd_strstart,
    474 	    (long)kernel_symtab.sd_symsize/sizeof(Elf_Sym));
    475 #endif
    476 }
    477 
    478 /*
    479  * Setup the kernel symbol table stuff.
    480  * Use this when the address of the symbol and string tables are known;
    481  * otherwise use ksyms_init with an ELF image.
    482  * We need to pass a minimal ELF header which will later be completed by
    483  * ksyms_hdr_init and handed off to userland through /dev/ksyms.  We use
    484  * a void *rather than a pointer to avoid exposing the Elf_Ehdr type.
    485  */
    486 void
    487 ksyms_init_explicit(void *ehdr, void *symstart, size_t symsize,
    488 		    void *strstart, size_t strsize)
    489 {
    490 
    491 	if (!ksyms_verify(symstart, strstart))
    492 		return;
    493 
    494 	ksyms_hdr_init(ehdr);
    495 	addsymtab("netbsd", symstart, symsize, strstart, strsize,
    496 	    &kernel_symtab, symstart);
    497 }
    498 
    499 /*
    500  * Get the value associated with a symbol.
    501  * "mod" is the module name, or null if any module.
    502  * "sym" is the symbol name.
    503  * "val" is a pointer to the corresponding value, if call succeeded.
    504  * Returns 0 if success or ENOENT if no such entry.
    505  *
    506  * Call with ksyms_lock, unless known that the symbol table can't change.
    507  */
    508 int
    509 ksyms_getval_unlocked(const char *mod, const char *sym, unsigned long *val,
    510     int type)
    511 {
    512 	struct ksyms_symtab *st;
    513 	Elf_Sym *es;
    514 
    515 	if (!ksyms_initted)
    516 		return ENOENT;
    517 
    518 #ifdef KSYMS_DEBUG
    519 	if (ksyms_debug & FOLLOW_CALLS)
    520 		printf("ksyms_getval_unlocked: mod %s sym %s valp %p\n",
    521 		    mod, sym, val);
    522 #endif
    523 
    524 	TAILQ_FOREACH(st, &ksyms_symtabs, sd_queue) {
    525 		if (st->sd_gone)
    526 			continue;
    527 		if (mod && strcmp(st->sd_name, mod))
    528 			continue;
    529 		if ((es = findsym(sym, st)) == NULL)
    530 			continue;
    531 		if (es->st_shndx == SHN_UNDEF)
    532 			continue;
    533 
    534 		/* Skip if bad binding */
    535 		if (type == KSYMS_EXTERN &&
    536 		    ELF_ST_BIND(es->st_info) != STB_GLOBAL)
    537 			continue;
    538 
    539 		if (val)
    540 			*val = es->st_value;
    541 		return 0;
    542 	}
    543 	return ENOENT;
    544 }
    545 
    546 int
    547 ksyms_getval(const char *mod, const char *sym, unsigned long *val, int type)
    548 {
    549 	int rc;
    550 
    551 	mutex_enter(&ksyms_lock);
    552 	rc = ksyms_getval_unlocked(mod, sym, val, type);
    553 	mutex_exit(&ksyms_lock);
    554 	return rc;
    555 }
    556 
    557 
    558 /*
    559  * Get "mod" and "symbol" associated with an address.
    560  * Returns 0 if success or ENOENT if no such entry.
    561  *
    562  * Call with ksyms_lock, unless known that the symbol table can't change.
    563  */
    564 int
    565 ksyms_getname(const char **mod, const char **sym, vaddr_t v, int f)
    566 {
    567 	struct ksyms_symtab *st;
    568 	Elf_Sym *les, *es = NULL;
    569 	vaddr_t laddr = 0;
    570 	const char *lmod = NULL;
    571 	char *stable = NULL;
    572 	int type, i, sz;
    573 
    574 	if (!ksyms_initted)
    575 		return ENOENT;
    576 
    577 	TAILQ_FOREACH(st, &ksyms_symtabs, sd_queue) {
    578 		if (st->sd_gone)
    579 			continue;
    580 		if (st->sd_minsym != NULL && v < st->sd_minsym->st_value)
    581 			continue;
    582 		if (st->sd_maxsym != NULL && v > st->sd_maxsym->st_value)
    583 			continue;
    584 		sz = st->sd_symsize/sizeof(Elf_Sym);
    585 		for (i = 0; i < sz; i++) {
    586 			les = st->sd_symstart + i;
    587 			type = ELF_ST_TYPE(les->st_info);
    588 
    589 			if ((f & KSYMS_PROC) && (type != STT_FUNC))
    590 				continue;
    591 
    592 			if (type == STT_NOTYPE)
    593 				continue;
    594 
    595 			if (((f & KSYMS_ANY) == 0) &&
    596 			    (type != STT_FUNC) && (type != STT_OBJECT))
    597 				continue;
    598 
    599 			if ((les->st_value <= v) && (les->st_value > laddr)) {
    600 				laddr = les->st_value;
    601 				es = les;
    602 				lmod = st->sd_name;
    603 				stable = st->sd_strstart - st->sd_usroffset;
    604 			}
    605 		}
    606 	}
    607 	if (es == NULL)
    608 		return ENOENT;
    609 	if ((f & KSYMS_EXACT) && (v != es->st_value))
    610 		return ENOENT;
    611 	if (mod)
    612 		*mod = lmod;
    613 	if (sym)
    614 		*sym = stable + es->st_name;
    615 	return 0;
    616 }
    617 
    618 /*
    619  * Temporary work structure for dynamic loaded symbol tables.
    620  *
    621  * XXX REMOVE WHEN LKMS GO.
    622  */
    623 struct syminfo {
    624 	size_t cursyms;
    625 	size_t curnamep;
    626 	size_t maxsyms;
    627 	size_t maxnamep;
    628 	Elf_Sym *syms;
    629 	char *symnames;
    630 };
    631 
    632 /*
    633  * Add a symbol to the temporary save area for symbols.
    634  *
    635  * XXX REMOVE WHEN LKMS GO.
    636  */
    637 static void
    638 addsym(struct syminfo *info, const Elf_Sym *sym, const char *name,
    639        const char *mod)
    640 {
    641 	int len, mlen;
    642 
    643 #ifdef KSYMS_DEBUG
    644 	if (ksyms_debug & FOLLOW_MORE_CALLS)
    645 		printf("addsym: name %s val %lx\n", name, (long)sym->st_value);
    646 #endif
    647 	len = strlen(name) + 1;
    648 	if (mod)
    649 		mlen = 1 + strlen(mod);
    650 	else
    651 		mlen = 0;
    652 	if (info->cursyms == info->maxsyms ||
    653 	    (len + mlen + info->curnamep) > info->maxnamep) {
    654 		printf("addsym: too many symbols, skipping '%s'\n", name);
    655 		return;
    656 	}
    657 	strlcpy(&info->symnames[info->curnamep], name,
    658 	    info->maxnamep - info->curnamep);
    659 	if (mlen) {
    660 		info->symnames[info->curnamep + len - 1] = '.';
    661 		strlcpy(&info->symnames[info->curnamep + len], mod,
    662 		    info->maxnamep - (info->curnamep + len));
    663 		len += mlen;
    664 	}
    665 	info->syms[info->cursyms] = *sym;
    666 	info->syms[info->cursyms].st_name = info->curnamep;
    667 	info->curnamep += len;
    668 	if (len > ksyms_maxlen)
    669 		ksyms_maxlen = len;
    670 	info->cursyms++;
    671 }
    672 
    673 /*
    674  * XXX REMOVE WHEN LKMS GO.
    675  */
    676 static int
    677 specialsym(const char *symname)
    678 {
    679 
    680 	return	!strcmp(symname, "_bss_start") ||
    681 	    !strcmp(symname, "__bss_start") ||
    682 	    !strcmp(symname, "_bss_end__") ||
    683 	    !strcmp(symname, "__bss_end__") ||
    684 	    !strcmp(symname, "_edata") ||
    685 	    !strcmp(symname, "_end") ||
    686 	    !strcmp(symname, "__end") ||
    687 	    !strcmp(symname, "__end__") ||
    688 	    !strncmp(symname, "__start_link_set_", 17) ||
    689 	    !strncmp(symname, "__stop_link_set_", 16);
    690 }
    691 
    692 /*
    693  * Adds a symbol table.
    694  * "name" is the module name, "start" and "size" is where the symbol table
    695  * is located, and "type" is in which binary format the symbol table is.
    696  * New memory for keeping the symbol table is allocated in this function.
    697  * Returns 0 if success and EEXIST if the module name is in use.
    698  *
    699  * XXX REMOVE WHEN LKMS GO.
    700  */
    701 int
    702 ksyms_addsymtab(const char *mod, void *symstart, vsize_t symsize,
    703 		char *strstart, vsize_t strsize)
    704 {
    705 	Elf_Sym *sym = symstart;
    706 	struct ksyms_symtab *st;
    707 	unsigned long rval;
    708 	int i;
    709 	char *name;
    710 	struct syminfo info;
    711 
    712 #ifdef KSYMS_DEBUG
    713 	if (ksyms_debug & FOLLOW_CALLS)
    714 		printf("ksyms_addsymtab: mod %s symsize %lx strsize %lx\n",
    715 		    mod, symsize, strsize);
    716 #endif
    717 
    718 	mutex_enter(&ksyms_lock);
    719 
    720 	/* Check if this symtab already loaded */
    721 	TAILQ_FOREACH(st, &ksyms_symtabs, sd_queue) {
    722 		if (st->sd_gone)
    723 			continue;
    724 		if (strcmp(mod, st->sd_name) == 0) {
    725 			mutex_exit(&ksyms_lock);
    726 			return EEXIST;
    727 		}
    728 	}
    729 
    730 	/*
    731 	 * XXX - Only add a symbol if it do not exist already.
    732 	 * This is because of a flaw in the current LKM implementation,
    733 	 * these loops will be removed once the in-kernel linker is in place.
    734 	 */
    735 	memset(&info, 0, sizeof(info));
    736 	for (i = 0; i < symsize/sizeof(Elf_Sym); i++) {
    737 		char * const symname = strstart + sym[i].st_name;
    738 		if (sym[i].st_name == 0)
    739 			continue; /* Just ignore */
    740 
    741 		/* check validity of the symbol */
    742 		/* XXX - save local symbols if DDB */
    743 		if (ELF_ST_BIND(sym[i].st_info) != STB_GLOBAL)
    744 			continue;
    745 
    746 		/* Check if the symbol exists */
    747 		if (ksyms_getval_unlocked(NULL, symname, &rval, KSYMS_EXTERN)
    748 		    == 0) {
    749 			/* Check (and complain) about differing values */
    750 			if (sym[i].st_value != rval &&
    751 			    sym[i].st_shndx != SHN_UNDEF) {
    752 				if (specialsym(symname)) {
    753 					info.maxsyms++;
    754 					info.maxnamep += strlen(symname) + 1 +
    755 					    strlen(mod) + 1;
    756 				} else {
    757 					printf("%s: symbol '%s' redeclared with"
    758 					    " different value (%lx != %lx)\n",
    759 					    mod, symname,
    760 					    rval, (long)sym[i].st_value);
    761 				}
    762 			}
    763 		} else {
    764 			/*
    765 			 * Count this symbol
    766 			 */
    767 			info.maxsyms++;
    768 			info.maxnamep += strlen(symname) + 1;
    769 		}
    770 	}
    771 
    772 	/*
    773 	 * Now that we know the sizes, malloc the structures.
    774 	 */
    775 	info.syms = malloc(sizeof(Elf_Sym)*info.maxsyms, M_DEVBUF, M_WAITOK);
    776 	info.symnames = malloc(info.maxnamep, M_DEVBUF, M_WAITOK);
    777 
    778 	/*
    779 	 * Now that we have the symbols, actually fill in the structures.
    780 	 */
    781 	for (i = 0; i < symsize/sizeof(Elf_Sym); i++) {
    782 		char * const symname = strstart + sym[i].st_name;
    783 		if (sym[i].st_name == 0)
    784 			continue; /* Just ignore */
    785 
    786 		/* check validity of the symbol */
    787 		/* XXX - save local symbols if DDB */
    788 		if (ELF_ST_BIND(sym[i].st_info) != STB_GLOBAL)
    789 			continue;
    790 
    791 		/* Check if the symbol exists */
    792 		if (ksyms_getval_unlocked(NULL, symname, &rval, KSYMS_EXTERN)
    793 		    == 0) {
    794 			if ((sym[i].st_value != rval) && specialsym(symname)) {
    795 				addsym(&info, &sym[i], symname, mod);
    796 			}
    797 		} else
    798 			/* Ok, save this symbol */
    799 			addsym(&info, &sym[i], symname, NULL);
    800 	}
    801 
    802 	st = kmem_zalloc(sizeof(*st), KM_SLEEP);
    803 	i = strlen(mod) + 1;
    804 	name = malloc(i, M_DEVBUF, M_WAITOK);
    805 	strlcpy(name, mod, i);
    806 	st->sd_name = name;
    807 	st->sd_symstart = info.syms;
    808 	st->sd_symsize = sizeof(Elf_Sym)*info.maxsyms;
    809 	st->sd_strstart = info.symnames;
    810 	st->sd_strsize = info.maxnamep;
    811 	st->sd_malloc = true;
    812 
    813 	/* Make them absolute references */
    814 	sym = st->sd_symstart;
    815 	for (i = 0; i < st->sd_symsize/sizeof(Elf_Sym); i++)
    816 		sym[i].st_shndx = SHN_ABS;
    817 
    818 	/* ksymsread() is unlocked, so membar. */
    819 	membar_producer();
    820 	TAILQ_INSERT_TAIL(&ksyms_symtabs, st, sd_queue);
    821 	ksyms_sizes_calc();
    822 	mutex_exit(&ksyms_lock);
    823 
    824 	return 0;
    825 }
    826 
    827 /*
    828  * Remove a symbol table specified by name.
    829  * Returns 0 if success, EBUSY if device open and ENOENT if no such name.
    830  *
    831  * XXX REMOVE WHEN LKMS GO.
    832  */
    833 int
    834 ksyms_delsymtab(const char *mod)
    835 {
    836 	struct ksyms_symtab *st;
    837 
    838 	mutex_enter(&ksyms_lock);
    839 	TAILQ_FOREACH(st, &ksyms_symtabs, sd_queue) {
    840 		if (st->sd_gone)
    841 			continue;
    842 		if (strcmp(mod, st->sd_name) != 0)
    843 			continue;
    844 		if (ksyms_isopen) {
    845 			st->sd_gone = true;
    846 			mutex_exit(&ksyms_lock);
    847 			return 0;
    848 		}
    849 		TAILQ_REMOVE(&ksyms_symtabs, st, sd_queue);
    850 		ksyms_sizes_calc();
    851 		mutex_exit(&ksyms_lock);
    852 		KASSERT(st->sd_malloc);
    853 		free(st->sd_symstart, M_DEVBUF);
    854 		free(st->sd_strstart, M_DEVBUF);
    855 		/* XXXUNCONST LINTED - const castaway */
    856 		free(__UNCONST(st->sd_name), M_DEVBUF);
    857 		kmem_free(st, sizeof(*st));
    858 		return 0;
    859 	}
    860 	mutex_exit(&ksyms_lock);
    861 	return ENOENT;
    862 }
    863 
    864 /*
    865  * Add a symbol table from a loadable module.
    866  */
    867 void
    868 ksyms_modload(const char *name, void *symstart, vsize_t symsize,
    869 	      char *strstart, vsize_t strsize)
    870 {
    871 	struct ksyms_symtab *st;
    872 
    873 	st = kmem_zalloc(sizeof(*st), KM_SLEEP);
    874 	mutex_enter(&ksyms_lock);
    875 	addsymtab(name, symstart, symsize, strstart, strsize, st, symstart);
    876 	mutex_exit(&ksyms_lock);
    877 }
    878 
    879 /*
    880  * Remove a symbol table from a loadable module.
    881  */
    882 void
    883 ksyms_modunload(const char *name)
    884 {
    885 	struct ksyms_symtab *st;
    886 
    887 	mutex_enter(&ksyms_lock);
    888 	TAILQ_FOREACH(st, &ksyms_symtabs, sd_queue) {
    889 		if (st->sd_gone)
    890 			continue;
    891 		if (strcmp(name, st->sd_name) != 0)
    892 			continue;
    893 		KASSERT(!st->sd_malloc);	/* XXXLKM */
    894 		st->sd_gone = true;
    895 		if (!ksyms_isopen) {
    896 			TAILQ_REMOVE(&ksyms_symtabs, st, sd_queue);
    897 			ksyms_sizes_calc();
    898 			kmem_free(st, sizeof(*st));
    899 		}
    900 		break;
    901 	}
    902 	mutex_exit(&ksyms_lock);
    903 	KASSERT(st != NULL);
    904 }
    905 
    906 #ifdef DDB
    907 /*
    908  * Keep sifting stuff here, to avoid export of ksyms internals.
    909  *
    910  * Systems is expected to be quiescent, so no locking done.
    911  */
    912 int
    913 ksyms_sift(char *mod, char *sym, int mode)
    914 {
    915 	struct ksyms_symtab *st;
    916 	char *sb;
    917 	int i, sz;
    918 
    919 	if (!ksyms_initted)
    920 		return ENOENT;
    921 
    922 	TAILQ_FOREACH(st, &ksyms_symtabs, sd_queue) {
    923 		if (st->sd_gone)
    924 			continue;
    925 		if (mod && strcmp(mod, st->sd_name))
    926 			continue;
    927 		sb = st->sd_strstart - st->sd_usroffset;
    928 
    929 		sz = st->sd_symsize/sizeof(Elf_Sym);
    930 		for (i = 0; i < sz; i++) {
    931 			Elf_Sym *les = st->sd_symstart + i;
    932 			char c;
    933 
    934 			if (strstr(sb + les->st_name, sym) == NULL)
    935 				continue;
    936 
    937 			if (mode == 'F') {
    938 				switch (ELF_ST_TYPE(les->st_info)) {
    939 				case STT_OBJECT:
    940 					c = '+';
    941 					break;
    942 				case STT_FUNC:
    943 					c = '*';
    944 					break;
    945 				case STT_SECTION:
    946 					c = '&';
    947 					break;
    948 				case STT_FILE:
    949 					c = '/';
    950 					break;
    951 				default:
    952 					c = ' ';
    953 					break;
    954 				}
    955 				db_printf("%s%c ", sb + les->st_name, c);
    956 			} else
    957 				db_printf("%s ", sb + les->st_name);
    958 		}
    959 	}
    960 	return ENOENT;
    961 }
    962 #endif /* DDB */
    963 
    964 /*
    965  * In case we exposing the symbol table to the userland using the pseudo-
    966  * device /dev/ksyms, it is easier to provide all the tables as one.
    967  * However, it means we have to change all the st_name fields for the
    968  * symbols so they match the ELF image that the userland will read
    969  * through the device.
    970  *
    971  * The actual (correct) value of st_name is preserved through a global
    972  * offset stored in the symbol table structure.
    973  *
    974  * Call with ksyms_lock held.
    975  */
    976 static void
    977 ksyms_sizes_calc(void)
    978 {
    979         struct ksyms_symtab *st;
    980 	int i, delta;
    981 
    982         ksyms_symsz = ksyms_strsz = 0;
    983         TAILQ_FOREACH(st, &ksyms_symtabs, sd_queue) {
    984 		delta = ksyms_strsz - st->sd_usroffset;
    985 		if (delta != 0) {
    986 			for (i = 0; i < st->sd_symsize/sizeof(Elf_Sym); i++)
    987 				st->sd_symstart[i].st_name += delta;
    988 			st->sd_usroffset = ksyms_strsz;
    989 		}
    990                 ksyms_symsz += st->sd_symsize;
    991                 ksyms_strsz += st->sd_strsize;
    992         }
    993 }
    994 
    995 static void
    996 ksyms_hdr_init(void *hdraddr)
    997 {
    998 
    999 	/* Copy the loaded elf exec header */
   1000 	memcpy(&ksyms_hdr.kh_ehdr, hdraddr, sizeof(Elf_Ehdr));
   1001 
   1002 	/* Set correct program/section header sizes, offsets and numbers */
   1003 	ksyms_hdr.kh_ehdr.e_phoff = offsetof(struct ksyms_hdr, kh_phdr[0]);
   1004 	ksyms_hdr.kh_ehdr.e_phentsize = sizeof(Elf_Phdr);
   1005 	ksyms_hdr.kh_ehdr.e_phnum = NPRGHDR;
   1006 	ksyms_hdr.kh_ehdr.e_shoff = offsetof(struct ksyms_hdr, kh_shdr[0]);
   1007 	ksyms_hdr.kh_ehdr.e_shentsize = sizeof(Elf_Shdr);
   1008 	ksyms_hdr.kh_ehdr.e_shnum = NSECHDR;
   1009 	ksyms_hdr.kh_ehdr.e_shstrndx = NSECHDR - 1; /* Last section */
   1010 
   1011 	/* Text */
   1012 	ksyms_hdr.kh_phdr[0].p_type = PT_LOAD;
   1013 	ksyms_hdr.kh_phdr[0].p_memsz = (unsigned long)-1L;
   1014 	ksyms_hdr.kh_phdr[0].p_flags = PF_R | PF_X;
   1015 
   1016 	/* Data */
   1017 	ksyms_hdr.kh_phdr[1].p_type = PT_LOAD;
   1018 	ksyms_hdr.kh_phdr[1].p_memsz = (unsigned long)-1L;
   1019 	ksyms_hdr.kh_phdr[1].p_flags = PF_R | PF_W | PF_X;
   1020 
   1021 	/* First section is null */
   1022 
   1023 	/* Second section header; ".symtab" */
   1024 	ksyms_hdr.kh_shdr[SYMTAB].sh_name = 1; /* Section 3 offset */
   1025 	ksyms_hdr.kh_shdr[SYMTAB].sh_type = SHT_SYMTAB;
   1026 	ksyms_hdr.kh_shdr[SYMTAB].sh_offset = sizeof(struct ksyms_hdr);
   1027 /*	ksyms_hdr.kh_shdr[SYMTAB].sh_size = filled in at open */
   1028 	ksyms_hdr.kh_shdr[SYMTAB].sh_link = 2; /* Corresponding strtab */
   1029 	ksyms_hdr.kh_shdr[SYMTAB].sh_addralign = sizeof(long);
   1030 	ksyms_hdr.kh_shdr[SYMTAB].sh_entsize = sizeof(Elf_Sym);
   1031 
   1032 	/* Third section header; ".strtab" */
   1033 	ksyms_hdr.kh_shdr[STRTAB].sh_name = 9; /* Section 3 offset */
   1034 	ksyms_hdr.kh_shdr[STRTAB].sh_type = SHT_STRTAB;
   1035 /*	ksyms_hdr.kh_shdr[STRTAB].sh_offset = filled in at open */
   1036 /*	ksyms_hdr.kh_shdr[STRTAB].sh_size = filled in at open */
   1037 	ksyms_hdr.kh_shdr[STRTAB].sh_addralign = sizeof(char);
   1038 
   1039 	/* Fourth section, ".shstrtab" */
   1040 	ksyms_hdr.kh_shdr[SHSTRTAB].sh_name = 17; /* This section name offset */
   1041 	ksyms_hdr.kh_shdr[SHSTRTAB].sh_type = SHT_STRTAB;
   1042 	ksyms_hdr.kh_shdr[SHSTRTAB].sh_offset =
   1043 	    offsetof(struct ksyms_hdr, kh_strtab);
   1044 	ksyms_hdr.kh_shdr[SHSTRTAB].sh_size = SHSTRSIZ;
   1045 	ksyms_hdr.kh_shdr[SHSTRTAB].sh_addralign = sizeof(char);
   1046 
   1047 	/* Set section names */
   1048 	strlcpy(&ksyms_hdr.kh_strtab[1], ".symtab",
   1049 	    sizeof(ksyms_hdr.kh_strtab) - 1);
   1050 	strlcpy(&ksyms_hdr.kh_strtab[9], ".strtab",
   1051 	    sizeof(ksyms_hdr.kh_strtab) - 9);
   1052 	strlcpy(&ksyms_hdr.kh_strtab[17], ".shstrtab",
   1053 	    sizeof(ksyms_hdr.kh_strtab) - 17);
   1054 }
   1055 
   1056 static int
   1057 ksymsopen(dev_t dev, int oflags, int devtype, struct lwp *l)
   1058 {
   1059 
   1060 	if (minor(dev) != 0 || !ksyms_initted)
   1061 		return ENXIO;
   1062 
   1063 	/*
   1064 	 * Create a "snapshot" of the kernel symbol table.  Setting
   1065 	 * ksyms_isopen will prevent symbol tables from being freed.
   1066 	 */
   1067 	mutex_enter(&ksyms_lock);
   1068 	ksyms_hdr.kh_shdr[SYMTAB].sh_size = ksyms_symsz;
   1069 	ksyms_hdr.kh_shdr[SYMTAB].sh_info = ksyms_symsz / sizeof(Elf_Sym);
   1070 	ksyms_hdr.kh_shdr[STRTAB].sh_offset = ksyms_symsz +
   1071 	    ksyms_hdr.kh_shdr[SYMTAB].sh_offset;
   1072 	ksyms_hdr.kh_shdr[STRTAB].sh_size = ksyms_strsz;
   1073 	ksyms_isopen = true;
   1074 	mutex_exit(&ksyms_lock);
   1075 
   1076 	return 0;
   1077 }
   1078 
   1079 static int
   1080 ksymsclose(dev_t dev, int oflags, int devtype, struct lwp *l)
   1081 {
   1082 	struct ksyms_symtab *st, *next;
   1083 	bool resize;
   1084 
   1085 	/* Discard refernces to symbol tables. */
   1086 	mutex_enter(&ksyms_lock);
   1087 	ksyms_isopen = false;
   1088 	resize = false;
   1089 	for (st = TAILQ_FIRST(&ksyms_symtabs); st != NULL; st = next) {
   1090 		next = TAILQ_NEXT(st, sd_queue);
   1091 		if (st->sd_gone) {
   1092 			TAILQ_REMOVE(&ksyms_symtabs, st, sd_queue);
   1093 			kmem_free(st, sizeof(*st));
   1094 			if (st->sd_malloc) {	/* XXXLKM */
   1095 				free(st->sd_symstart, M_DEVBUF);
   1096 				free(st->sd_strstart, M_DEVBUF);
   1097 				/* XXXUNCONST LINTED - const castaway */
   1098 				free(__UNCONST(st->sd_name), M_DEVBUF);
   1099 			}
   1100 			resize = true;
   1101 		}
   1102 	}
   1103 	if (resize)
   1104 		ksyms_sizes_calc();
   1105 	mutex_exit(&ksyms_lock);
   1106 
   1107 	return 0;
   1108 }
   1109 
   1110 static int
   1111 ksymsread(dev_t dev, struct uio *uio, int ioflag)
   1112 {
   1113 	struct ksyms_symtab *st;
   1114 	size_t filepos, inpos, off;
   1115 	int error;
   1116 
   1117 	/*
   1118 	 * First: Copy out the ELF header.   XXX Lose if ksymsopen()
   1119 	 * occurs during read of the header.
   1120 	 */
   1121 	off = uio->uio_offset;
   1122 	if (off < sizeof(struct ksyms_hdr)) {
   1123 		error = uiomove((char *)&ksyms_hdr + off,
   1124 		    sizeof(struct ksyms_hdr) - off, uio);
   1125 		if (error != 0)
   1126 			return error;
   1127 	}
   1128 
   1129 	/*
   1130 	 * Copy out the symbol table.
   1131 	 */
   1132 	filepos = sizeof(struct ksyms_hdr);
   1133 	TAILQ_FOREACH(st, &ksyms_symtabs, sd_queue) {
   1134 		if (uio->uio_resid == 0)
   1135 			return 0;
   1136 		if (uio->uio_offset <= st->sd_symsize + filepos) {
   1137 			inpos = uio->uio_offset - filepos;
   1138 			error = uiomove((char *)st->sd_symstart + inpos,
   1139 			   st->sd_symsize - inpos, uio);
   1140 			if (error != 0)
   1141 				return error;
   1142 		}
   1143 		filepos += st->sd_symsize;
   1144 	}
   1145 
   1146 	/*
   1147 	 * Copy out the string table
   1148 	 */
   1149 	KASSERT(filepos == sizeof(struct ksyms_hdr) +
   1150 	    ksyms_hdr.kh_shdr[SYMTAB].sh_size);
   1151 	TAILQ_FOREACH(st, &ksyms_symtabs, sd_queue) {
   1152 		if (uio->uio_resid == 0)
   1153 			return 0;
   1154 		if (uio->uio_offset <= st->sd_strsize + filepos) {
   1155 			inpos = uio->uio_offset - filepos;
   1156 			error = uiomove((char *)st->sd_strstart + inpos,
   1157 			   st->sd_strsize - inpos, uio);
   1158 			if (error != 0)
   1159 				return error;
   1160 		}
   1161 		filepos += st->sd_strsize;
   1162 	}
   1163 
   1164 	return 0;
   1165 }
   1166 
   1167 static int
   1168 ksymswrite(dev_t dev, struct uio *uio, int ioflag)
   1169 {
   1170 
   1171 	return EROFS;
   1172 }
   1173 
   1174 static int
   1175 ksymsioctl(dev_t dev, u_long cmd, void *data, int fflag, struct lwp *l)
   1176 {
   1177 	struct ksyms_gsymbol *kg = (struct ksyms_gsymbol *)data;
   1178 	struct ksyms_symtab *st;
   1179 	Elf_Sym *sym = NULL, copy;
   1180 	unsigned long val;
   1181 	int error = 0;
   1182 	char *str = NULL;
   1183 	int len;
   1184 
   1185 	/* Read ksyms_maxlen only once while not holding the lock. */
   1186 	len = ksyms_maxlen;
   1187 
   1188 	if (cmd == KIOCGVALUE || cmd == KIOCGSYMBOL) {
   1189 		str = kmem_alloc(len, KM_SLEEP);
   1190 		if ((error = copyinstr(kg->kg_name, str, len, NULL)) != 0) {
   1191 			kmem_free(str, len);
   1192 			return error;
   1193 		}
   1194 	}
   1195 
   1196 	switch (cmd) {
   1197 	case KIOCGVALUE:
   1198 		/*
   1199 		 * Use the in-kernel symbol lookup code for fast
   1200 		 * retreival of a value.
   1201 		 */
   1202 		error = ksyms_getval(NULL, str, &val, KSYMS_EXTERN);
   1203 		if (error == 0)
   1204 			error = copyout(&val, kg->kg_value, sizeof(long));
   1205 		kmem_free(str, len);
   1206 		break;
   1207 
   1208 	case KIOCGSYMBOL:
   1209 		/*
   1210 		 * Use the in-kernel symbol lookup code for fast
   1211 		 * retreival of a symbol.
   1212 		 */
   1213 		mutex_enter(&ksyms_lock);
   1214 		TAILQ_FOREACH(st, &ksyms_symtabs, sd_queue) {
   1215 			if (st->sd_gone)
   1216 				continue;
   1217 			if ((sym = findsym(str, st)) == NULL)
   1218 				continue;
   1219 #ifdef notdef
   1220 			/* Skip if bad binding */
   1221 			if (ELF_ST_BIND(sym->st_info) != STB_GLOBAL) {
   1222 				sym = NULL;
   1223 				continue;
   1224 			}
   1225 #endif
   1226 			break;
   1227 		}
   1228 		if (sym != NULL) {
   1229 			memcpy(&copy, sym, sizeof(copy));
   1230 			mutex_exit(&ksyms_lock);
   1231 			error = copyout(&copy, kg->kg_sym, sizeof(Elf_Sym));
   1232 		} else {
   1233 			mutex_exit(&ksyms_lock);
   1234 			error = ENOENT;
   1235 		}
   1236 		kmem_free(str, len);
   1237 		break;
   1238 
   1239 	case KIOCGSIZE:
   1240 		/*
   1241 		 * Get total size of symbol table.
   1242 		 */
   1243 		mutex_enter(&ksyms_lock);
   1244 		*(int *)data = ksyms_strsz + ksyms_symsz +
   1245 		    sizeof(struct ksyms_hdr);
   1246 		mutex_exit(&ksyms_lock);
   1247 		break;
   1248 
   1249 	default:
   1250 		error = ENOTTY;
   1251 		break;
   1252 	}
   1253 
   1254 	return error;
   1255 }
   1256 
   1257 const struct cdevsw ksyms_cdevsw = {
   1258 	ksymsopen, ksymsclose, ksymsread, ksymswrite, ksymsioctl,
   1259 	nullstop, notty, nopoll, nommap, nullkqfilter, D_OTHER | D_MPSAFE
   1260 };
   1261