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