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