Home | History | Annotate | Line # | Download | only in kern
kern_ksyms.c revision 1.41.4.1.4.1
      1 /*	$NetBSD: kern_ksyms.c,v 1.41.4.1.4.1 2010/04/21 00:28:16 matt 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.4.1.4.1 2010/04/21 00:28:16 matt 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 void
    403 ksyms_init_finalize()
    404 {
    405 	mutex_init(&ksyms_lock, MUTEX_DEFAULT, IPL_NONE);
    406 }
    407 
    408 /*
    409  * Setup the kernel symbol table stuff.
    410  */
    411 void
    412 ksyms_init(int symsize, void *start, void *end)
    413 {
    414 	int i, j;
    415 	Elf_Shdr *shdr;
    416 	char *symstart = NULL, *strstart = NULL;
    417 	size_t strsize = 0;
    418 	Elf_Ehdr *ehdr;
    419 
    420 #ifdef SYMTAB_SPACE
    421 	if (symsize <= 0 &&
    422 	    strncmp(db_symtab, SYMTAB_FILLER, sizeof(SYMTAB_FILLER))) {
    423 		symsize = db_symtabsize;
    424 		start = db_symtab;
    425 		end = db_symtab + db_symtabsize;
    426 	}
    427 #endif
    428 	if (symsize <= 0) {
    429 		printf("[ Kernel symbol table missing! ]\n");
    430 		return;
    431 	}
    432 
    433 	/* Sanity check */
    434 	if (ALIGNED_POINTER(start, long) == 0) {
    435 		printf("[ Kernel symbol table has bad start address %p ]\n",
    436 		    start);
    437 		return;
    438 	}
    439 
    440 	ehdr = (Elf_Ehdr *)start;
    441 
    442 	/* check if this is a valid ELF header */
    443 	/* No reason to verify arch type, the kernel is actually running! */
    444 	if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG) ||
    445 	    ehdr->e_ident[EI_CLASS] != ELFCLASS ||
    446 	    ehdr->e_version > 1) {
    447 		printf("[ Kernel symbol table invalid! ]\n");
    448 		return; /* nothing to do */
    449 	}
    450 
    451 	/* Loaded header will be scratched in addsymtab */
    452 	ksyms_hdr_init(start);
    453 
    454 	/* Find the symbol table and the corresponding string table. */
    455 	shdr = (Elf_Shdr *)((uint8_t *)start + ehdr->e_shoff);
    456 	for (i = 1; i < ehdr->e_shnum; i++) {
    457 		if (shdr[i].sh_type != SHT_SYMTAB)
    458 			continue;
    459 		if (shdr[i].sh_offset == 0)
    460 			continue;
    461 		symstart = (uint8_t *)start + shdr[i].sh_offset;
    462 		symsize = shdr[i].sh_size;
    463 		j = shdr[i].sh_link;
    464 		if (shdr[j].sh_offset == 0)
    465 			continue; /* Can this happen? */
    466 		strstart = (uint8_t *)start + shdr[j].sh_offset;
    467 		strsize = shdr[j].sh_size;
    468 		break;
    469 	}
    470 
    471 	if (!ksyms_verify(symstart, strstart)) {
    472 		printf("[ Kernel symbol table failed verification! ]\n");
    473 		return;
    474 	}
    475 	addsymtab("netbsd", symstart, symsize, strstart, strsize,
    476 	    &kernel_symtab, start);
    477 
    478 #ifdef DEBUG
    479 	aprint_normal("Loaded initial symtab at %p, strtab at %p, # entries %ld\n",
    480 	    kernel_symtab.sd_symstart, kernel_symtab.sd_strstart,
    481 	    (long)kernel_symtab.sd_symsize/sizeof(Elf_Sym));
    482 #endif
    483 }
    484 
    485 /*
    486  * Setup the kernel symbol table stuff.
    487  * Use this when the address of the symbol and string tables are known;
    488  * otherwise use ksyms_init with an ELF image.
    489  * We need to pass a minimal ELF header which will later be completed by
    490  * ksyms_hdr_init and handed off to userland through /dev/ksyms.  We use
    491  * a void *rather than a pointer to avoid exposing the Elf_Ehdr type.
    492  */
    493 void
    494 ksyms_init_explicit(void *ehdr, void *symstart, size_t symsize,
    495 		    void *strstart, size_t strsize)
    496 {
    497 
    498 	if (!ksyms_verify(symstart, strstart))
    499 		return;
    500 
    501 	ksyms_hdr_init(ehdr);
    502 	addsymtab("netbsd", symstart, symsize, strstart, strsize,
    503 	    &kernel_symtab, symstart);
    504 }
    505 
    506 /*
    507  * Get the value associated with a symbol.
    508  * "mod" is the module name, or null if any module.
    509  * "sym" is the symbol name.
    510  * "val" is a pointer to the corresponding value, if call succeeded.
    511  * Returns 0 if success or ENOENT if no such entry.
    512  *
    513  * Call with ksyms_lock, unless known that the symbol table can't change.
    514  */
    515 int
    516 ksyms_getval_unlocked(const char *mod, const char *sym, unsigned long *val,
    517     int type)
    518 {
    519 	struct ksyms_symtab *st;
    520 	Elf_Sym *es;
    521 
    522 	if (!ksyms_initted)
    523 		return ENOENT;
    524 
    525 #ifdef KSYMS_DEBUG
    526 	if (ksyms_debug & FOLLOW_CALLS)
    527 		printf("ksyms_getval_unlocked: mod %s sym %s valp %p\n",
    528 		    mod, sym, val);
    529 #endif
    530 
    531 	TAILQ_FOREACH(st, &ksyms_symtabs, sd_queue) {
    532 		if (st->sd_gone)
    533 			continue;
    534 		if (mod && strcmp(st->sd_name, mod))
    535 			continue;
    536 		if ((es = findsym(sym, st)) == NULL)
    537 			continue;
    538 		if (es->st_shndx == SHN_UNDEF)
    539 			continue;
    540 
    541 		/* Skip if bad binding */
    542 		if (type == KSYMS_EXTERN &&
    543 		    ELF_ST_BIND(es->st_info) != STB_GLOBAL)
    544 			continue;
    545 
    546 		if (val)
    547 			*val = es->st_value;
    548 		return 0;
    549 	}
    550 	return ENOENT;
    551 }
    552 
    553 int
    554 ksyms_getval(const char *mod, const char *sym, unsigned long *val, int type)
    555 {
    556 	int rc;
    557 
    558 	mutex_enter(&ksyms_lock);
    559 	rc = ksyms_getval_unlocked(mod, sym, val, type);
    560 	mutex_exit(&ksyms_lock);
    561 	return rc;
    562 }
    563 
    564 
    565 /*
    566  * Get "mod" and "symbol" associated with an address.
    567  * Returns 0 if success or ENOENT if no such entry.
    568  *
    569  * Call with ksyms_lock, unless known that the symbol table can't change.
    570  */
    571 int
    572 ksyms_getname(const char **mod, const char **sym, vaddr_t v, int f)
    573 {
    574 	struct ksyms_symtab *st;
    575 	Elf_Sym *les, *es = NULL;
    576 	vaddr_t laddr = 0;
    577 	const char *lmod = NULL;
    578 	char *stable = NULL;
    579 	int type, i, sz;
    580 
    581 	if (!ksyms_initted)
    582 		return ENOENT;
    583 
    584 	TAILQ_FOREACH(st, &ksyms_symtabs, sd_queue) {
    585 		if (st->sd_gone)
    586 			continue;
    587 		if (st->sd_minsym != NULL && v < st->sd_minsym->st_value)
    588 			continue;
    589 		if (st->sd_maxsym != NULL && v > st->sd_maxsym->st_value)
    590 			continue;
    591 		sz = st->sd_symsize/sizeof(Elf_Sym);
    592 		for (i = 0; i < sz; i++) {
    593 			les = st->sd_symstart + i;
    594 			type = ELF_ST_TYPE(les->st_info);
    595 
    596 			if ((f & KSYMS_PROC) && (type != STT_FUNC))
    597 				continue;
    598 
    599 			if (type == STT_NOTYPE)
    600 				continue;
    601 
    602 			if (((f & KSYMS_ANY) == 0) &&
    603 			    (type != STT_FUNC) && (type != STT_OBJECT))
    604 				continue;
    605 
    606 			if ((les->st_value <= v) && (les->st_value > laddr)) {
    607 				laddr = les->st_value;
    608 				es = les;
    609 				lmod = st->sd_name;
    610 				stable = st->sd_strstart - st->sd_usroffset;
    611 			}
    612 		}
    613 	}
    614 	if (es == NULL)
    615 		return ENOENT;
    616 	if ((f & KSYMS_EXACT) && (v != es->st_value))
    617 		return ENOENT;
    618 	if (mod)
    619 		*mod = lmod;
    620 	if (sym)
    621 		*sym = stable + es->st_name;
    622 	return 0;
    623 }
    624 
    625 /*
    626  * Temporary work structure for dynamic loaded symbol tables.
    627  *
    628  * XXX REMOVE WHEN LKMS GO.
    629  */
    630 struct syminfo {
    631 	size_t cursyms;
    632 	size_t curnamep;
    633 	size_t maxsyms;
    634 	size_t maxnamep;
    635 	Elf_Sym *syms;
    636 	char *symnames;
    637 };
    638 
    639 /*
    640  * Add a symbol to the temporary save area for symbols.
    641  *
    642  * XXX REMOVE WHEN LKMS GO.
    643  */
    644 static void
    645 addsym(struct syminfo *info, const Elf_Sym *sym, const char *name,
    646        const char *mod)
    647 {
    648 	int len, mlen;
    649 
    650 #ifdef KSYMS_DEBUG
    651 	if (ksyms_debug & FOLLOW_MORE_CALLS)
    652 		printf("addsym: name %s val %lx\n", name, (long)sym->st_value);
    653 #endif
    654 	len = strlen(name) + 1;
    655 	if (mod)
    656 		mlen = 1 + strlen(mod);
    657 	else
    658 		mlen = 0;
    659 	if (info->cursyms == info->maxsyms ||
    660 	    (len + mlen + info->curnamep) > info->maxnamep) {
    661 		printf("addsym: too many symbols, skipping '%s'\n", name);
    662 		return;
    663 	}
    664 	strlcpy(&info->symnames[info->curnamep], name,
    665 	    info->maxnamep - info->curnamep);
    666 	if (mlen) {
    667 		info->symnames[info->curnamep + len - 1] = '.';
    668 		strlcpy(&info->symnames[info->curnamep + len], mod,
    669 		    info->maxnamep - (info->curnamep + len));
    670 		len += mlen;
    671 	}
    672 	info->syms[info->cursyms] = *sym;
    673 	info->syms[info->cursyms].st_name = info->curnamep;
    674 	info->curnamep += len;
    675 	if (len > ksyms_maxlen)
    676 		ksyms_maxlen = len;
    677 	info->cursyms++;
    678 }
    679 
    680 /*
    681  * XXX REMOVE WHEN LKMS GO.
    682  */
    683 static int
    684 specialsym(const char *symname)
    685 {
    686 
    687 	return	!strcmp(symname, "_bss_start") ||
    688 	    !strcmp(symname, "__bss_start") ||
    689 	    !strcmp(symname, "_bss_end__") ||
    690 	    !strcmp(symname, "__bss_end__") ||
    691 	    !strcmp(symname, "_edata") ||
    692 	    !strcmp(symname, "_end") ||
    693 	    !strcmp(symname, "__end") ||
    694 	    !strcmp(symname, "__end__") ||
    695 	    !strncmp(symname, "__start_link_set_", 17) ||
    696 	    !strncmp(symname, "__stop_link_set_", 16);
    697 }
    698 
    699 /*
    700  * Adds a symbol table.
    701  * "name" is the module name, "start" and "size" is where the symbol table
    702  * is located, and "type" is in which binary format the symbol table is.
    703  * New memory for keeping the symbol table is allocated in this function.
    704  * Returns 0 if success and EEXIST if the module name is in use.
    705  *
    706  * XXX REMOVE WHEN LKMS GO.
    707  */
    708 int
    709 ksyms_addsymtab(const char *mod, void *symstart, vsize_t symsize,
    710 		char *strstart, vsize_t strsize)
    711 {
    712 	Elf_Sym *sym = symstart;
    713 	struct ksyms_symtab *st;
    714 	unsigned long rval;
    715 	int i;
    716 	char *name;
    717 	struct syminfo info;
    718 
    719 #ifdef KSYMS_DEBUG
    720 	if (ksyms_debug & FOLLOW_CALLS)
    721 		printf("ksyms_addsymtab: mod %s symsize %lx strsize %lx\n",
    722 		    mod, symsize, strsize);
    723 #endif
    724 
    725 	mutex_enter(&ksyms_lock);
    726 
    727 	/* Check if this symtab already loaded */
    728 	TAILQ_FOREACH(st, &ksyms_symtabs, sd_queue) {
    729 		if (st->sd_gone)
    730 			continue;
    731 		if (strcmp(mod, st->sd_name) == 0) {
    732 			mutex_exit(&ksyms_lock);
    733 			return EEXIST;
    734 		}
    735 	}
    736 
    737 	/*
    738 	 * XXX - Only add a symbol if it do not exist already.
    739 	 * This is because of a flaw in the current LKM implementation,
    740 	 * these loops will be removed once the in-kernel linker is in place.
    741 	 */
    742 	memset(&info, 0, sizeof(info));
    743 	for (i = 0; i < symsize/sizeof(Elf_Sym); i++) {
    744 		char * const symname = strstart + sym[i].st_name;
    745 		if (sym[i].st_name == 0)
    746 			continue; /* Just ignore */
    747 
    748 		/* check validity of the symbol */
    749 		/* XXX - save local symbols if DDB */
    750 		if (ELF_ST_BIND(sym[i].st_info) != STB_GLOBAL)
    751 			continue;
    752 
    753 		/* Check if the symbol exists */
    754 		if (ksyms_getval_unlocked(NULL, symname, &rval, KSYMS_EXTERN)
    755 		    == 0) {
    756 			/* Check (and complain) about differing values */
    757 			if (sym[i].st_value != rval &&
    758 			    sym[i].st_shndx != SHN_UNDEF) {
    759 				if (specialsym(symname)) {
    760 					info.maxsyms++;
    761 					info.maxnamep += strlen(symname) + 1 +
    762 					    strlen(mod) + 1;
    763 				} else {
    764 					printf("%s: symbol '%s' redeclared with"
    765 					    " different value (%lx != %lx)\n",
    766 					    mod, symname,
    767 					    rval, (long)sym[i].st_value);
    768 				}
    769 			}
    770 		} else {
    771 			/*
    772 			 * Count this symbol
    773 			 */
    774 			info.maxsyms++;
    775 			info.maxnamep += strlen(symname) + 1;
    776 		}
    777 	}
    778 
    779 	/*
    780 	 * Now that we know the sizes, malloc the structures.
    781 	 */
    782 	info.syms = malloc(sizeof(Elf_Sym)*info.maxsyms, M_DEVBUF, M_WAITOK);
    783 	info.symnames = malloc(info.maxnamep, M_DEVBUF, M_WAITOK);
    784 
    785 	/*
    786 	 * Now that we have the symbols, actually fill in the structures.
    787 	 */
    788 	for (i = 0; i < symsize/sizeof(Elf_Sym); i++) {
    789 		char * const symname = strstart + sym[i].st_name;
    790 		if (sym[i].st_name == 0)
    791 			continue; /* Just ignore */
    792 
    793 		/* check validity of the symbol */
    794 		/* XXX - save local symbols if DDB */
    795 		if (ELF_ST_BIND(sym[i].st_info) != STB_GLOBAL)
    796 			continue;
    797 
    798 		/* Check if the symbol exists */
    799 		if (ksyms_getval_unlocked(NULL, symname, &rval, KSYMS_EXTERN)
    800 		    == 0) {
    801 			if ((sym[i].st_value != rval) && specialsym(symname)) {
    802 				addsym(&info, &sym[i], symname, mod);
    803 			}
    804 		} else
    805 			/* Ok, save this symbol */
    806 			addsym(&info, &sym[i], symname, NULL);
    807 	}
    808 
    809 	st = kmem_zalloc(sizeof(*st), KM_SLEEP);
    810 	i = strlen(mod) + 1;
    811 	name = malloc(i, M_DEVBUF, M_WAITOK);
    812 	strlcpy(name, mod, i);
    813 	st->sd_name = name;
    814 	st->sd_symstart = info.syms;
    815 	st->sd_symsize = sizeof(Elf_Sym)*info.maxsyms;
    816 	st->sd_strstart = info.symnames;
    817 	st->sd_strsize = info.maxnamep;
    818 	st->sd_malloc = true;
    819 
    820 	/* Make them absolute references */
    821 	sym = st->sd_symstart;
    822 	for (i = 0; i < st->sd_symsize/sizeof(Elf_Sym); i++)
    823 		sym[i].st_shndx = SHN_ABS;
    824 
    825 	/* ksymsread() is unlocked, so membar. */
    826 	membar_producer();
    827 	TAILQ_INSERT_TAIL(&ksyms_symtabs, st, sd_queue);
    828 	ksyms_sizes_calc();
    829 	mutex_exit(&ksyms_lock);
    830 
    831 	return 0;
    832 }
    833 
    834 /*
    835  * Remove a symbol table specified by name.
    836  * Returns 0 if success, EBUSY if device open and ENOENT if no such name.
    837  *
    838  * XXX REMOVE WHEN LKMS GO.
    839  */
    840 int
    841 ksyms_delsymtab(const char *mod)
    842 {
    843 	struct ksyms_symtab *st;
    844 
    845 	mutex_enter(&ksyms_lock);
    846 	TAILQ_FOREACH(st, &ksyms_symtabs, sd_queue) {
    847 		if (st->sd_gone)
    848 			continue;
    849 		if (strcmp(mod, st->sd_name) != 0)
    850 			continue;
    851 		if (ksyms_isopen) {
    852 			st->sd_gone = true;
    853 			mutex_exit(&ksyms_lock);
    854 			return 0;
    855 		}
    856 		TAILQ_REMOVE(&ksyms_symtabs, st, sd_queue);
    857 		ksyms_sizes_calc();
    858 		mutex_exit(&ksyms_lock);
    859 		KASSERT(st->sd_malloc);
    860 		free(st->sd_symstart, M_DEVBUF);
    861 		free(st->sd_strstart, M_DEVBUF);
    862 		/* XXXUNCONST LINTED - const castaway */
    863 		free(__UNCONST(st->sd_name), M_DEVBUF);
    864 		kmem_free(st, sizeof(*st));
    865 		return 0;
    866 	}
    867 	mutex_exit(&ksyms_lock);
    868 	return ENOENT;
    869 }
    870 
    871 /*
    872  * Add a symbol table from a loadable module.
    873  */
    874 void
    875 ksyms_modload(const char *name, void *symstart, vsize_t symsize,
    876 	      char *strstart, vsize_t strsize)
    877 {
    878 	struct ksyms_symtab *st;
    879 
    880 	st = kmem_zalloc(sizeof(*st), KM_SLEEP);
    881 	mutex_enter(&ksyms_lock);
    882 	addsymtab(name, symstart, symsize, strstart, strsize, st, symstart);
    883 	mutex_exit(&ksyms_lock);
    884 }
    885 
    886 /*
    887  * Remove a symbol table from a loadable module.
    888  */
    889 void
    890 ksyms_modunload(const char *name)
    891 {
    892 	struct ksyms_symtab *st;
    893 
    894 	mutex_enter(&ksyms_lock);
    895 	TAILQ_FOREACH(st, &ksyms_symtabs, sd_queue) {
    896 		if (st->sd_gone)
    897 			continue;
    898 		if (strcmp(name, st->sd_name) != 0)
    899 			continue;
    900 		KASSERT(!st->sd_malloc);	/* XXXLKM */
    901 		st->sd_gone = true;
    902 		if (!ksyms_isopen) {
    903 			TAILQ_REMOVE(&ksyms_symtabs, st, sd_queue);
    904 			ksyms_sizes_calc();
    905 			kmem_free(st, sizeof(*st));
    906 		}
    907 		break;
    908 	}
    909 	mutex_exit(&ksyms_lock);
    910 	KASSERT(st != NULL);
    911 }
    912 
    913 #ifdef DDB
    914 /*
    915  * Keep sifting stuff here, to avoid export of ksyms internals.
    916  *
    917  * Systems is expected to be quiescent, so no locking done.
    918  */
    919 int
    920 ksyms_sift(char *mod, char *sym, int mode)
    921 {
    922 	struct ksyms_symtab *st;
    923 	char *sb;
    924 	int i, sz;
    925 
    926 	if (!ksyms_initted)
    927 		return ENOENT;
    928 
    929 	TAILQ_FOREACH(st, &ksyms_symtabs, sd_queue) {
    930 		if (st->sd_gone)
    931 			continue;
    932 		if (mod && strcmp(mod, st->sd_name))
    933 			continue;
    934 		sb = st->sd_strstart - st->sd_usroffset;
    935 
    936 		sz = st->sd_symsize/sizeof(Elf_Sym);
    937 		for (i = 0; i < sz; i++) {
    938 			Elf_Sym *les = st->sd_symstart + i;
    939 			char c;
    940 
    941 			if (strstr(sb + les->st_name, sym) == NULL)
    942 				continue;
    943 
    944 			if (mode == 'F') {
    945 				switch (ELF_ST_TYPE(les->st_info)) {
    946 				case STT_OBJECT:
    947 					c = '+';
    948 					break;
    949 				case STT_FUNC:
    950 					c = '*';
    951 					break;
    952 				case STT_SECTION:
    953 					c = '&';
    954 					break;
    955 				case STT_FILE:
    956 					c = '/';
    957 					break;
    958 				default:
    959 					c = ' ';
    960 					break;
    961 				}
    962 				db_printf("%s%c ", sb + les->st_name, c);
    963 			} else
    964 				db_printf("%s ", sb + les->st_name);
    965 		}
    966 	}
    967 	return ENOENT;
    968 }
    969 #endif /* DDB */
    970 
    971 /*
    972  * In case we exposing the symbol table to the userland using the pseudo-
    973  * device /dev/ksyms, it is easier to provide all the tables as one.
    974  * However, it means we have to change all the st_name fields for the
    975  * symbols so they match the ELF image that the userland will read
    976  * through the device.
    977  *
    978  * The actual (correct) value of st_name is preserved through a global
    979  * offset stored in the symbol table structure.
    980  *
    981  * Call with ksyms_lock held.
    982  */
    983 static void
    984 ksyms_sizes_calc(void)
    985 {
    986         struct ksyms_symtab *st;
    987 	int i, delta;
    988 
    989         ksyms_symsz = ksyms_strsz = 0;
    990         TAILQ_FOREACH(st, &ksyms_symtabs, sd_queue) {
    991 		delta = ksyms_strsz - st->sd_usroffset;
    992 		if (delta != 0) {
    993 			for (i = 0; i < st->sd_symsize/sizeof(Elf_Sym); i++)
    994 				st->sd_symstart[i].st_name += delta;
    995 			st->sd_usroffset = ksyms_strsz;
    996 		}
    997                 ksyms_symsz += st->sd_symsize;
    998                 ksyms_strsz += st->sd_strsize;
    999         }
   1000 }
   1001 
   1002 static void
   1003 ksyms_hdr_init(void *hdraddr)
   1004 {
   1005 
   1006 	/* Copy the loaded elf exec header */
   1007 	memcpy(&ksyms_hdr.kh_ehdr, hdraddr, sizeof(Elf_Ehdr));
   1008 
   1009 	/* Set correct program/section header sizes, offsets and numbers */
   1010 	ksyms_hdr.kh_ehdr.e_phoff = offsetof(struct ksyms_hdr, kh_phdr[0]);
   1011 	ksyms_hdr.kh_ehdr.e_phentsize = sizeof(Elf_Phdr);
   1012 	ksyms_hdr.kh_ehdr.e_phnum = NPRGHDR;
   1013 	ksyms_hdr.kh_ehdr.e_shoff = offsetof(struct ksyms_hdr, kh_shdr[0]);
   1014 	ksyms_hdr.kh_ehdr.e_shentsize = sizeof(Elf_Shdr);
   1015 	ksyms_hdr.kh_ehdr.e_shnum = NSECHDR;
   1016 	ksyms_hdr.kh_ehdr.e_shstrndx = NSECHDR - 1; /* Last section */
   1017 
   1018 	/* Text */
   1019 	ksyms_hdr.kh_phdr[0].p_type = PT_LOAD;
   1020 	ksyms_hdr.kh_phdr[0].p_memsz = (unsigned long)-1L;
   1021 	ksyms_hdr.kh_phdr[0].p_flags = PF_R | PF_X;
   1022 
   1023 	/* Data */
   1024 	ksyms_hdr.kh_phdr[1].p_type = PT_LOAD;
   1025 	ksyms_hdr.kh_phdr[1].p_memsz = (unsigned long)-1L;
   1026 	ksyms_hdr.kh_phdr[1].p_flags = PF_R | PF_W | PF_X;
   1027 
   1028 	/* First section is null */
   1029 
   1030 	/* Second section header; ".symtab" */
   1031 	ksyms_hdr.kh_shdr[SYMTAB].sh_name = 1; /* Section 3 offset */
   1032 	ksyms_hdr.kh_shdr[SYMTAB].sh_type = SHT_SYMTAB;
   1033 	ksyms_hdr.kh_shdr[SYMTAB].sh_offset = sizeof(struct ksyms_hdr);
   1034 /*	ksyms_hdr.kh_shdr[SYMTAB].sh_size = filled in at open */
   1035 	ksyms_hdr.kh_shdr[SYMTAB].sh_link = 2; /* Corresponding strtab */
   1036 	ksyms_hdr.kh_shdr[SYMTAB].sh_addralign = sizeof(long);
   1037 	ksyms_hdr.kh_shdr[SYMTAB].sh_entsize = sizeof(Elf_Sym);
   1038 
   1039 	/* Third section header; ".strtab" */
   1040 	ksyms_hdr.kh_shdr[STRTAB].sh_name = 9; /* Section 3 offset */
   1041 	ksyms_hdr.kh_shdr[STRTAB].sh_type = SHT_STRTAB;
   1042 /*	ksyms_hdr.kh_shdr[STRTAB].sh_offset = filled in at open */
   1043 /*	ksyms_hdr.kh_shdr[STRTAB].sh_size = filled in at open */
   1044 	ksyms_hdr.kh_shdr[STRTAB].sh_addralign = sizeof(char);
   1045 
   1046 	/* Fourth section, ".shstrtab" */
   1047 	ksyms_hdr.kh_shdr[SHSTRTAB].sh_name = 17; /* This section name offset */
   1048 	ksyms_hdr.kh_shdr[SHSTRTAB].sh_type = SHT_STRTAB;
   1049 	ksyms_hdr.kh_shdr[SHSTRTAB].sh_offset =
   1050 	    offsetof(struct ksyms_hdr, kh_strtab);
   1051 	ksyms_hdr.kh_shdr[SHSTRTAB].sh_size = SHSTRSIZ;
   1052 	ksyms_hdr.kh_shdr[SHSTRTAB].sh_addralign = sizeof(char);
   1053 
   1054 	/* Set section names */
   1055 	strlcpy(&ksyms_hdr.kh_strtab[1], ".symtab",
   1056 	    sizeof(ksyms_hdr.kh_strtab) - 1);
   1057 	strlcpy(&ksyms_hdr.kh_strtab[9], ".strtab",
   1058 	    sizeof(ksyms_hdr.kh_strtab) - 9);
   1059 	strlcpy(&ksyms_hdr.kh_strtab[17], ".shstrtab",
   1060 	    sizeof(ksyms_hdr.kh_strtab) - 17);
   1061 }
   1062 
   1063 static int
   1064 ksymsopen(dev_t dev, int oflags, int devtype, struct lwp *l)
   1065 {
   1066 
   1067 	if (minor(dev) != 0 || !ksyms_initted)
   1068 		return ENXIO;
   1069 
   1070 	/*
   1071 	 * Create a "snapshot" of the kernel symbol table.  Setting
   1072 	 * ksyms_isopen will prevent symbol tables from being freed.
   1073 	 */
   1074 	mutex_enter(&ksyms_lock);
   1075 	ksyms_hdr.kh_shdr[SYMTAB].sh_size = ksyms_symsz;
   1076 	ksyms_hdr.kh_shdr[SYMTAB].sh_info = ksyms_symsz / sizeof(Elf_Sym);
   1077 	ksyms_hdr.kh_shdr[STRTAB].sh_offset = ksyms_symsz +
   1078 	    ksyms_hdr.kh_shdr[SYMTAB].sh_offset;
   1079 	ksyms_hdr.kh_shdr[STRTAB].sh_size = ksyms_strsz;
   1080 	ksyms_isopen = true;
   1081 	mutex_exit(&ksyms_lock);
   1082 
   1083 	return 0;
   1084 }
   1085 
   1086 static int
   1087 ksymsclose(dev_t dev, int oflags, int devtype, struct lwp *l)
   1088 {
   1089 	struct ksyms_symtab *st, *next;
   1090 	bool resize;
   1091 
   1092 	/* Discard refernces to symbol tables. */
   1093 	mutex_enter(&ksyms_lock);
   1094 	ksyms_isopen = false;
   1095 	resize = false;
   1096 	for (st = TAILQ_FIRST(&ksyms_symtabs); st != NULL; st = next) {
   1097 		next = TAILQ_NEXT(st, sd_queue);
   1098 		if (st->sd_gone) {
   1099 			TAILQ_REMOVE(&ksyms_symtabs, st, sd_queue);
   1100 			kmem_free(st, sizeof(*st));
   1101 			if (st->sd_malloc) {	/* XXXLKM */
   1102 				free(st->sd_symstart, M_DEVBUF);
   1103 				free(st->sd_strstart, M_DEVBUF);
   1104 				/* XXXUNCONST LINTED - const castaway */
   1105 				free(__UNCONST(st->sd_name), M_DEVBUF);
   1106 			}
   1107 			resize = true;
   1108 		}
   1109 	}
   1110 	if (resize)
   1111 		ksyms_sizes_calc();
   1112 	mutex_exit(&ksyms_lock);
   1113 
   1114 	return 0;
   1115 }
   1116 
   1117 static int
   1118 ksymsread(dev_t dev, struct uio *uio, int ioflag)
   1119 {
   1120 	struct ksyms_symtab *st;
   1121 	size_t filepos, inpos, off;
   1122 	int error;
   1123 
   1124 	/*
   1125 	 * First: Copy out the ELF header.   XXX Lose if ksymsopen()
   1126 	 * occurs during read of the header.
   1127 	 */
   1128 	off = uio->uio_offset;
   1129 	if (off < sizeof(struct ksyms_hdr)) {
   1130 		error = uiomove((char *)&ksyms_hdr + off,
   1131 		    sizeof(struct ksyms_hdr) - off, uio);
   1132 		if (error != 0)
   1133 			return error;
   1134 	}
   1135 
   1136 	/*
   1137 	 * Copy out the symbol table.
   1138 	 */
   1139 	filepos = sizeof(struct ksyms_hdr);
   1140 	TAILQ_FOREACH(st, &ksyms_symtabs, sd_queue) {
   1141 		if (uio->uio_resid == 0)
   1142 			return 0;
   1143 		if (uio->uio_offset <= st->sd_symsize + filepos) {
   1144 			inpos = uio->uio_offset - filepos;
   1145 			error = uiomove((char *)st->sd_symstart + inpos,
   1146 			   st->sd_symsize - inpos, uio);
   1147 			if (error != 0)
   1148 				return error;
   1149 		}
   1150 		filepos += st->sd_symsize;
   1151 	}
   1152 
   1153 	/*
   1154 	 * Copy out the string table
   1155 	 */
   1156 	KASSERT(filepos == sizeof(struct ksyms_hdr) +
   1157 	    ksyms_hdr.kh_shdr[SYMTAB].sh_size);
   1158 	TAILQ_FOREACH(st, &ksyms_symtabs, sd_queue) {
   1159 		if (uio->uio_resid == 0)
   1160 			return 0;
   1161 		if (uio->uio_offset <= st->sd_strsize + filepos) {
   1162 			inpos = uio->uio_offset - filepos;
   1163 			error = uiomove((char *)st->sd_strstart + inpos,
   1164 			   st->sd_strsize - inpos, uio);
   1165 			if (error != 0)
   1166 				return error;
   1167 		}
   1168 		filepos += st->sd_strsize;
   1169 	}
   1170 
   1171 	return 0;
   1172 }
   1173 
   1174 static int
   1175 ksymswrite(dev_t dev, struct uio *uio, int ioflag)
   1176 {
   1177 
   1178 	return EROFS;
   1179 }
   1180 
   1181 static int
   1182 ksymsioctl(dev_t dev, u_long cmd, void *data, int fflag, struct lwp *l)
   1183 {
   1184 	struct ksyms_gsymbol *kg = (struct ksyms_gsymbol *)data;
   1185 	struct ksyms_symtab *st;
   1186 	Elf_Sym *sym = NULL, copy;
   1187 	unsigned long val;
   1188 	int error = 0;
   1189 	char *str = NULL;
   1190 	int len;
   1191 
   1192 	/* Read ksyms_maxlen only once while not holding the lock. */
   1193 	len = ksyms_maxlen;
   1194 
   1195 	if (cmd == KIOCGVALUE || cmd == KIOCGSYMBOL) {
   1196 		str = kmem_alloc(len, KM_SLEEP);
   1197 		if ((error = copyinstr(kg->kg_name, str, len, NULL)) != 0) {
   1198 			kmem_free(str, len);
   1199 			return error;
   1200 		}
   1201 	}
   1202 
   1203 	switch (cmd) {
   1204 	case KIOCGVALUE:
   1205 		/*
   1206 		 * Use the in-kernel symbol lookup code for fast
   1207 		 * retreival of a value.
   1208 		 */
   1209 		error = ksyms_getval(NULL, str, &val, KSYMS_EXTERN);
   1210 		if (error == 0)
   1211 			error = copyout(&val, kg->kg_value, sizeof(long));
   1212 		kmem_free(str, len);
   1213 		break;
   1214 
   1215 	case KIOCGSYMBOL:
   1216 		/*
   1217 		 * Use the in-kernel symbol lookup code for fast
   1218 		 * retreival of a symbol.
   1219 		 */
   1220 		mutex_enter(&ksyms_lock);
   1221 		TAILQ_FOREACH(st, &ksyms_symtabs, sd_queue) {
   1222 			if (st->sd_gone)
   1223 				continue;
   1224 			if ((sym = findsym(str, st)) == NULL)
   1225 				continue;
   1226 #ifdef notdef
   1227 			/* Skip if bad binding */
   1228 			if (ELF_ST_BIND(sym->st_info) != STB_GLOBAL) {
   1229 				sym = NULL;
   1230 				continue;
   1231 			}
   1232 #endif
   1233 			break;
   1234 		}
   1235 		if (sym != NULL) {
   1236 			memcpy(&copy, sym, sizeof(copy));
   1237 			mutex_exit(&ksyms_lock);
   1238 			error = copyout(&copy, kg->kg_sym, sizeof(Elf_Sym));
   1239 		} else {
   1240 			mutex_exit(&ksyms_lock);
   1241 			error = ENOENT;
   1242 		}
   1243 		kmem_free(str, len);
   1244 		break;
   1245 
   1246 	case KIOCGSIZE:
   1247 		/*
   1248 		 * Get total size of symbol table.
   1249 		 */
   1250 		mutex_enter(&ksyms_lock);
   1251 		*(int *)data = ksyms_strsz + ksyms_symsz +
   1252 		    sizeof(struct ksyms_hdr);
   1253 		mutex_exit(&ksyms_lock);
   1254 		break;
   1255 
   1256 	default:
   1257 		error = ENOTTY;
   1258 		break;
   1259 	}
   1260 
   1261 	return error;
   1262 }
   1263 
   1264 const struct cdevsw ksyms_cdevsw = {
   1265 	ksymsopen, ksymsclose, ksymsread, ksymswrite, ksymsioctl,
   1266 	nullstop, notty, nopoll, nommap, nullkqfilter, D_OTHER | D_MPSAFE
   1267 };
   1268