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