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