Home | History | Annotate | Line # | Download | only in kern
kern_ksyms.c revision 1.62.2.1
      1 /*	$NetBSD: kern_ksyms.c,v 1.62.2.1 2011/06/06 09:09:28 jruoho Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2008 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software developed for The NetBSD Foundation
      8  * by Andrew Doran.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 /*
     33  * Copyright (c) 2001, 2003 Anders Magnusson (ragge (at) ludd.luth.se).
     34  * All rights reserved.
     35  *
     36  * Redistribution and use in source and binary forms, with or without
     37  * modification, are permitted provided that the following conditions
     38  * are met:
     39  * 1. Redistributions of source code must retain the above copyright
     40  *    notice, this list of conditions and the following disclaimer.
     41  * 2. Redistributions in binary form must reproduce the above copyright
     42  *    notice, this list of conditions and the following disclaimer in the
     43  *    documentation and/or other materials provided with the distribution.
     44  * 3. The name of the author may not be used to endorse or promote products
     45  *    derived from this software without specific prior written permission
     46  *
     47  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     48  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     49  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     50  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     51  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     52  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     53  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     54  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     55  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     56  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     57  */
     58 
     59 /*
     60  * Code to deal with in-kernel symbol table management + /dev/ksyms.
     61  *
     62  * For each loaded module the symbol table info is kept track of by a
     63  * struct, placed in a circular list. The first entry is the kernel
     64  * symbol table.
     65  */
     66 
     67 /*
     68  * TODO:
     69  *
     70  *	Add support for mmap, poll.
     71  */
     72 
     73 #include <sys/cdefs.h>
     74 __KERNEL_RCSID(0, "$NetBSD: kern_ksyms.c,v 1.62.2.1 2011/06/06 09:09:28 jruoho Exp $");
     75 
     76 #if defined(_KERNEL) && defined(_KERNEL_OPT)
     77 #include "opt_ddb.h"
     78 #include "opt_ddbparam.h"	/* for SYMTAB_SPACE */
     79 #include "opt_dtrace.h"
     80 #endif
     81 
     82 #define _KSYMS_PRIVATE
     83 
     84 #include <sys/param.h>
     85 #include <sys/queue.h>
     86 #include <sys/exec.h>
     87 #include <sys/systm.h>
     88 #include <sys/conf.h>
     89 #include <sys/kmem.h>
     90 #include <sys/proc.h>
     91 #include <sys/atomic.h>
     92 #include <sys/ksyms.h>
     93 
     94 #include <uvm/uvm_extern.h>
     95 
     96 #ifdef DDB
     97 #include <ddb/db_output.h>
     98 #endif
     99 
    100 #include "ksyms.h"
    101 
    102 #define KSYMS_MAX_ID	65536
    103 #ifdef KDTRACE_HOOKS
    104 static uint32_t ksyms_nmap[KSYMS_MAX_ID];	/* sorted symbol table map */
    105 #else
    106 static uint32_t *ksyms_nmap = NULL;
    107 #endif
    108 
    109 static int ksyms_maxlen;
    110 static bool ksyms_isopen;
    111 static bool ksyms_initted;
    112 static struct ksyms_hdr ksyms_hdr;
    113 static kmutex_t ksyms_lock;
    114 
    115 void ksymsattach(int);
    116 static void ksyms_hdr_init(void *);
    117 static void ksyms_sizes_calc(void);
    118 
    119 #ifdef KSYMS_DEBUG
    120 #define	FOLLOW_CALLS		1
    121 #define	FOLLOW_MORE_CALLS	2
    122 #define	FOLLOW_DEVKSYMS		4
    123 static int ksyms_debug;
    124 #endif
    125 
    126 #ifdef SYMTAB_SPACE
    127 #define		SYMTAB_FILLER	"|This is the symbol table!"
    128 
    129 char		db_symtab[SYMTAB_SPACE] = SYMTAB_FILLER;
    130 int		db_symtabsize = SYMTAB_SPACE;
    131 #endif
    132 
    133 int ksyms_symsz;
    134 int ksyms_strsz;
    135 int ksyms_ctfsz;
    136 TAILQ_HEAD(, ksyms_symtab) ksyms_symtabs =
    137     TAILQ_HEAD_INITIALIZER(ksyms_symtabs);
    138 static struct ksyms_symtab kernel_symtab;
    139 
    140 static int
    141 ksyms_verify(void *symstart, void *strstart)
    142 {
    143 #if defined(DIAGNOSTIC) || defined(DEBUG)
    144 	if (symstart == NULL)
    145 		printf("ksyms: Symbol table not found\n");
    146 	if (strstart == NULL)
    147 		printf("ksyms: String table not found\n");
    148 	if (symstart == NULL || strstart == NULL)
    149 		printf("ksyms: Perhaps the kernel is stripped?\n");
    150 #endif
    151 	if (symstart == NULL || strstart == NULL)
    152 		return 0;
    153 	return 1;
    154 }
    155 
    156 /*
    157  * Finds a certain symbol name in a certain symbol table.
    158  */
    159 static Elf_Sym *
    160 findsym(const char *name, struct ksyms_symtab *table, int type)
    161 {
    162 	Elf_Sym *sym, *maxsym;
    163 	int low, mid, high, nglob;
    164 	char *str, *cmp;
    165 
    166 	sym = table->sd_symstart;
    167 	str = table->sd_strstart - table->sd_usroffset;
    168 	nglob = table->sd_nglob;
    169 	low = 0;
    170 	high = nglob;
    171 
    172 	/*
    173 	 * Start with a binary search of all global symbols in this table.
    174 	 * Global symbols must have unique names.
    175 	 */
    176 	while (low < high) {
    177 		mid = (low + high) >> 1;
    178 		cmp = sym[mid].st_name + str;
    179 		if (cmp[0] < name[0] || strcmp(cmp, name) < 0) {
    180 			low = mid + 1;
    181 		} else {
    182 			high = mid;
    183 		}
    184 	}
    185 	KASSERT(low == high);
    186 	if (__predict_true(low < nglob &&
    187 	    strcmp(sym[low].st_name + str, name) == 0)) {
    188 		KASSERT(ELF_ST_BIND(sym[low].st_info) == STB_GLOBAL);
    189 		return &sym[low];
    190 	}
    191 
    192 	/*
    193 	 * Perform a linear search of local symbols (rare).  Many local
    194 	 * symbols with the same name can exist so are not included in
    195 	 * the binary search.
    196 	 */
    197 	if (type != KSYMS_EXTERN) {
    198 		maxsym = sym + table->sd_symsize / sizeof(Elf_Sym);
    199 		for (sym += nglob; sym < maxsym; sym++) {
    200 			if (strcmp(name, sym->st_name + str) == 0) {
    201 				return sym;
    202 			}
    203 		}
    204 	}
    205 	return NULL;
    206 }
    207 
    208 /*
    209  * The "attach" is in reality done in ksyms_init().
    210  */
    211 void
    212 ksymsattach(int arg)
    213 {
    214 
    215 }
    216 
    217 void
    218 ksyms_init(void)
    219 {
    220 
    221 #ifdef SYMTAB_SPACE
    222 	if (!ksyms_initted &&
    223 	    strncmp(db_symtab, SYMTAB_FILLER, sizeof(SYMTAB_FILLER))) {
    224 		ksyms_addsyms_elf(db_symtabsize, db_symtab,
    225 		    db_symtab + db_symtabsize);
    226 	}
    227 #endif
    228 
    229 	mutex_init(&ksyms_lock, MUTEX_DEFAULT, IPL_NONE);
    230 }
    231 
    232 /*
    233  * Add a symbol table.
    234  * This is intended for use when the symbol table and its corresponding
    235  * string table are easily available.  If they are embedded in an ELF
    236  * image, use addsymtab_elf() instead.
    237  *
    238  * name - Symbol's table name.
    239  * symstart, symsize - Address and size of the symbol table.
    240  * strstart, strsize - Address and size of the string table.
    241  * tab - Symbol table to be updated with this information.
    242  * newstart - Address to which the symbol table has to be copied during
    243  *            shrinking.  If NULL, it is not moved.
    244  */
    245 static const char *addsymtab_strstart;
    246 
    247 static int
    248 addsymtab_compar(const void *a, const void *b)
    249 {
    250 	const Elf_Sym *sa, *sb;
    251 
    252 	sa = a;
    253 	sb = b;
    254 
    255 	/*
    256 	 * Split the symbol table into two, with globals at the start
    257 	 * and locals at the end.
    258 	 */
    259 	if (ELF_ST_BIND(sa->st_info) != ELF_ST_BIND(sb->st_info)) {
    260 		if (ELF_ST_BIND(sa->st_info) == STB_GLOBAL) {
    261 			return -1;
    262 		}
    263 		if (ELF_ST_BIND(sb->st_info) == STB_GLOBAL) {
    264 			return 1;
    265 		}
    266 	}
    267 
    268 	/* Within each band, sort by name. */
    269 	return strcmp(sa->st_name + addsymtab_strstart,
    270 	    sb->st_name + addsymtab_strstart);
    271 }
    272 
    273 static void
    274 addsymtab(const char *name, void *symstart, size_t symsize,
    275 	  void *strstart, size_t strsize, struct ksyms_symtab *tab,
    276 	  void *newstart, void *ctfstart, size_t ctfsize, uint32_t *nmap)
    277 {
    278 	Elf_Sym *sym, *nsym, ts;
    279 	int i, j, n, nglob;
    280 	char *str;
    281 	int nsyms = symsize / sizeof(Elf_Sym);
    282 
    283 	/* Sanity check for pre-allocated map table used during startup. */
    284 	if ((nmap == ksyms_nmap) && (nsyms >= KSYMS_MAX_ID)) {
    285 		printf("kern_ksyms: ERROR %d > %d, increase KSYMS_MAX_ID\n",
    286 		    nsyms, KSYMS_MAX_ID);
    287 
    288 		/* truncate for now */
    289 		nsyms = KSYMS_MAX_ID - 1;
    290 	}
    291 
    292 	tab->sd_symstart = symstart;
    293 	tab->sd_symsize = symsize;
    294 	tab->sd_strstart = strstart;
    295 	tab->sd_strsize = strsize;
    296 	tab->sd_name = name;
    297 	tab->sd_minsym = UINTPTR_MAX;
    298 	tab->sd_maxsym = 0;
    299 	tab->sd_usroffset = 0;
    300 	tab->sd_gone = false;
    301 #ifdef KDTRACE_HOOKS
    302 	tab->sd_ctfstart = ctfstart;
    303 	tab->sd_ctfsize = ctfsize;
    304 	tab->sd_nmap = nmap;
    305 	tab->sd_nmapsize = nsyms;
    306 #endif
    307 #ifdef KSYMS_DEBUG
    308 	printf("newstart %p sym %p ksyms_symsz %zu str %p strsz %zu send %p\n",
    309 	    newstart, symstart, symsize, strstart, strsize,
    310 	    tab->sd_strstart + tab->sd_strsize);
    311 #endif
    312 
    313 	if (nmap) {
    314 		memset(nmap, 0, nsyms * sizeof(uint32_t));
    315 	}
    316 
    317 	/* Pack symbol table by removing all file name references. */
    318 	sym = tab->sd_symstart;
    319 	nsym = (Elf_Sym *)newstart;
    320 	str = tab->sd_strstart;
    321 	nglob = 0;
    322 	for (i = n = 0; i < nsyms; i++) {
    323 
    324 	    	/* This breaks CTF mapping, so don't do it when
    325 		 * DTrace is enabled
    326 		 */
    327 #ifndef KDTRACE_HOOKS
    328 		/*
    329 		 * Remove useless symbols.
    330 		 * Should actually remove all typeless symbols.
    331 		 */
    332 		if (sym[i].st_name == 0)
    333 			continue; /* Skip nameless entries */
    334 		if (sym[i].st_shndx == SHN_UNDEF)
    335 			continue; /* Skip external references */
    336 		if (ELF_ST_TYPE(sym[i].st_info) == STT_FILE)
    337 			continue; /* Skip filenames */
    338 		if (ELF_ST_TYPE(sym[i].st_info) == STT_NOTYPE &&
    339 		    sym[i].st_value == 0 &&
    340 		    strcmp(str + sym[i].st_name, "*ABS*") == 0)
    341 			continue; /* XXX */
    342 		if (ELF_ST_TYPE(sym[i].st_info) == STT_NOTYPE &&
    343 		    strcmp(str + sym[i].st_name, "gcc2_compiled.") == 0)
    344 			continue; /* XXX */
    345 #endif
    346 
    347 		/* Save symbol. Set it as an absolute offset */
    348 		nsym[n] = sym[i];
    349 
    350 #ifdef KDTRACE_HOOKS
    351 		if (nmap != NULL) {
    352 			/*
    353 			 * Save the size, replace it with the symbol id so
    354 			 * the mapping can be done after the cleanup and sort.
    355 			 */
    356 			nmap[i] = nsym[n].st_size;
    357 			nsym[n].st_size = i + 1;	/* zero is reserved */
    358 		}
    359 #endif
    360 
    361 		nsym[n].st_shndx = SHBSS;
    362 		j = strlen(nsym[n].st_name + str) + 1;
    363 		if (j > ksyms_maxlen)
    364 			ksyms_maxlen = j;
    365 		nglob += (ELF_ST_BIND(nsym[n].st_info) == STB_GLOBAL);
    366 
    367 		/* Compute min and max symbols. */
    368 		if (strcmp(str + sym[i].st_name, "*ABS*") != 0
    369 		    && ELF_ST_TYPE(nsym[n].st_info) != STT_NOTYPE) {
    370 			if (nsym[n].st_value < tab->sd_minsym) {
    371 				tab->sd_minsym = nsym[n].st_value;
    372 			}
    373 			if (nsym[n].st_value > tab->sd_maxsym) {
    374 				tab->sd_maxsym = nsym[n].st_value;
    375 			}
    376 		}
    377 		n++;
    378 	}
    379 
    380 	/* Fill the rest of the record, and sort the symbols. */
    381 	tab->sd_symstart = nsym;
    382 	tab->sd_symsize = n * sizeof(Elf_Sym);
    383 	tab->sd_nglob = nglob;
    384 	addsymtab_strstart = str;
    385 	if (kheapsort(nsym, n, sizeof(Elf_Sym), addsymtab_compar, &ts) != 0)
    386 		panic("addsymtab");
    387 
    388 #ifdef KDTRACE_HOOKS
    389 	/*
    390 	 * Build the mapping from original symbol id to new symbol table.
    391 	 * Deleted symbols will have a zero map, indices will be one based
    392 	 * instead of zero based.
    393 	 * Resulting map is sd_nmap[original_index] = new_index + 1
    394 	 */
    395 	if (nmap != NULL) {
    396 		int new;
    397 		for (new = 0; new < n; new++) {
    398 			uint32_t orig = nsym[new].st_size - 1;
    399 			uint32_t size = nmap[orig];
    400 
    401 			nmap[orig] = new + 1;
    402 
    403 			/* restore the size */
    404 			nsym[new].st_size = size;
    405 		}
    406 	}
    407 #endif
    408 
    409 	/* ksymsread() is unlocked, so membar. */
    410 	membar_producer();
    411 	TAILQ_INSERT_TAIL(&ksyms_symtabs, tab, sd_queue);
    412 	ksyms_sizes_calc();
    413 	ksyms_initted = true;
    414 }
    415 
    416 /*
    417  * Setup the kernel symbol table stuff.
    418  */
    419 void
    420 ksyms_addsyms_elf(int symsize, void *start, void *end)
    421 {
    422 	int i, j;
    423 	Elf_Shdr *shdr;
    424 	char *symstart = NULL, *strstart = NULL;
    425 	size_t strsize = 0;
    426 	Elf_Ehdr *ehdr;
    427 	char *ctfstart = NULL;
    428 	size_t ctfsize = 0;
    429 
    430 	if (symsize <= 0) {
    431 		printf("[ Kernel symbol table missing! ]\n");
    432 		return;
    433 	}
    434 
    435 	/* Sanity check */
    436 	if (ALIGNED_POINTER(start, long) == 0) {
    437 		printf("[ Kernel symbol table has bad start address %p ]\n",
    438 		    start);
    439 		return;
    440 	}
    441 
    442 	ehdr = (Elf_Ehdr *)start;
    443 
    444 	/* check if this is a valid ELF header */
    445 	/* No reason to verify arch type, the kernel is actually running! */
    446 	if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG) ||
    447 	    ehdr->e_ident[EI_CLASS] != ELFCLASS ||
    448 	    ehdr->e_version > 1) {
    449 		printf("[ Kernel symbol table invalid! ]\n");
    450 		return; /* nothing to do */
    451 	}
    452 
    453 	/* Loaded header will be scratched in addsymtab */
    454 	ksyms_hdr_init(start);
    455 
    456 	/* Find the symbol table and the corresponding string table. */
    457 	shdr = (Elf_Shdr *)((uint8_t *)start + ehdr->e_shoff);
    458 	for (i = 1; i < ehdr->e_shnum; i++) {
    459 		if (shdr[i].sh_type != SHT_SYMTAB)
    460 			continue;
    461 		if (shdr[i].sh_offset == 0)
    462 			continue;
    463 		symstart = (uint8_t *)start + shdr[i].sh_offset;
    464 		symsize = shdr[i].sh_size;
    465 		j = shdr[i].sh_link;
    466 		if (shdr[j].sh_offset == 0)
    467 			continue; /* Can this happen? */
    468 		strstart = (uint8_t *)start + shdr[j].sh_offset;
    469 		strsize = shdr[j].sh_size;
    470 		break;
    471 	}
    472 
    473 #ifdef KDTRACE_HOOKS
    474 	/* Find the CTF section */
    475 	shdr = (Elf_Shdr *)((uint8_t *)start + ehdr->e_shoff);
    476 	if (ehdr->e_shstrndx != 0) {
    477 		char *shstr = (uint8_t *)start +
    478 		    shdr[ehdr->e_shstrndx].sh_offset;
    479 		for (i = 1; i < ehdr->e_shnum; i++) {
    480 #ifdef DEBUG
    481 		    	printf("ksyms: checking %s\n", &shstr[shdr[i].sh_name]);
    482 #endif
    483 			if (shdr[i].sh_type != SHT_PROGBITS)
    484 				continue;
    485 			if (strncmp(".SUNW_ctf", &shstr[shdr[i].sh_name], 10)
    486 			    != 0)
    487 				continue;
    488 			ctfstart = (uint8_t *)start + shdr[i].sh_offset;
    489 			ctfsize = shdr[i].sh_size;
    490 			ksyms_ctfsz = ctfsize;
    491 #ifdef DEBUG
    492 			aprint_normal("Found CTF at %p, size 0x%zx\n",
    493 			    ctfstart, ctfsize);
    494 #endif
    495 			break;
    496 		}
    497 #ifdef DEBUG
    498 	} else {
    499 	    	printf("ksyms: e_shstrndx == 0\n");
    500 #endif
    501 	}
    502 #endif
    503 
    504 	if (!ksyms_verify(symstart, strstart))
    505 		return;
    506 
    507 	addsymtab("netbsd", symstart, symsize, strstart, strsize,
    508 	    &kernel_symtab, start, ctfstart, ctfsize, ksyms_nmap);
    509 
    510 #ifdef DEBUG
    511 	aprint_normal("Loaded initial symtab at %p, strtab at %p, # entries %ld\n",
    512 	    kernel_symtab.sd_symstart, kernel_symtab.sd_strstart,
    513 	    (long)kernel_symtab.sd_symsize/sizeof(Elf_Sym));
    514 #endif
    515 }
    516 
    517 /*
    518  * Setup the kernel symbol table stuff.
    519  * Use this when the address of the symbol and string tables are known;
    520  * otherwise use ksyms_init with an ELF image.
    521  * We need to pass a minimal ELF header which will later be completed by
    522  * ksyms_hdr_init and handed off to userland through /dev/ksyms.  We use
    523  * a void *rather than a pointer to avoid exposing the Elf_Ehdr type.
    524  */
    525 void
    526 ksyms_addsyms_explicit(void *ehdr, void *symstart, size_t symsize,
    527 		    void *strstart, size_t strsize)
    528 {
    529 
    530 	if (!ksyms_verify(symstart, strstart))
    531 		return;
    532 
    533 	ksyms_hdr_init(ehdr);
    534 	addsymtab("netbsd", symstart, symsize, strstart, strsize,
    535 	    &kernel_symtab, symstart, NULL, 0, ksyms_nmap);
    536 }
    537 
    538 /*
    539  * Get the value associated with a symbol.
    540  * "mod" is the module name, or null if any module.
    541  * "sym" is the symbol name.
    542  * "val" is a pointer to the corresponding value, if call succeeded.
    543  * Returns 0 if success or ENOENT if no such entry.
    544  *
    545  * Call with ksyms_lock, unless known that the symbol table can't change.
    546  */
    547 int
    548 ksyms_getval_unlocked(const char *mod, const char *sym, unsigned long *val,
    549 		      int type)
    550 {
    551 	struct ksyms_symtab *st;
    552 	Elf_Sym *es;
    553 
    554 #ifdef KSYMS_DEBUG
    555 	if (ksyms_debug & FOLLOW_CALLS)
    556 		printf("ksyms_getval_unlocked: mod %s sym %s valp %p\n",
    557 		    mod, sym, val);
    558 #endif
    559 
    560 	TAILQ_FOREACH(st, &ksyms_symtabs, sd_queue) {
    561 		if (__predict_false(st->sd_gone))
    562 			continue;
    563 		if (mod != NULL && strcmp(st->sd_name, mod))
    564 			continue;
    565 		if ((es = findsym(sym, st, type)) != NULL) {
    566 			*val = es->st_value;
    567 			return 0;
    568 		}
    569 	}
    570 	return ENOENT;
    571 }
    572 
    573 int
    574 ksyms_getval(const char *mod, const char *sym, unsigned long *val, int type)
    575 {
    576 	int rc;
    577 
    578 	if (!ksyms_initted)
    579 		return ENOENT;
    580 
    581 	mutex_enter(&ksyms_lock);
    582 	rc = ksyms_getval_unlocked(mod, sym, val, type);
    583 	mutex_exit(&ksyms_lock);
    584 	return rc;
    585 }
    586 
    587 struct ksyms_symtab *
    588 ksyms_get_mod(const char *mod)
    589 {
    590 	struct ksyms_symtab *st;
    591 
    592 	mutex_enter(&ksyms_lock);
    593 	TAILQ_FOREACH(st, &ksyms_symtabs, sd_queue) {
    594 		if (__predict_false(st->sd_gone))
    595 			continue;
    596 		if (mod != NULL && strcmp(st->sd_name, mod))
    597 			continue;
    598 		break;
    599 	}
    600 	mutex_exit(&ksyms_lock);
    601 
    602 	return st;
    603 }
    604 
    605 
    606 /*
    607  * ksyms_mod_foreach()
    608  *
    609  * Iterate over the symbol table of the specified module, calling the callback
    610  * handler for each symbol. Stop iterating if the handler return is non-zero.
    611  *
    612  */
    613 
    614 int
    615 ksyms_mod_foreach(const char *mod, ksyms_callback_t callback, void *opaque)
    616 {
    617 	struct ksyms_symtab *st;
    618 	Elf_Sym *sym, *maxsym;
    619 	char *str;
    620 	int symindx;
    621 
    622 	if (!ksyms_initted)
    623 		return ENOENT;
    624 
    625 	mutex_enter(&ksyms_lock);
    626 
    627 	/* find the module */
    628 	TAILQ_FOREACH(st, &ksyms_symtabs, sd_queue) {
    629 		if (__predict_false(st->sd_gone))
    630 			continue;
    631 		if (mod != NULL && strcmp(st->sd_name, mod))
    632 			continue;
    633 
    634 		sym = st->sd_symstart;
    635 		str = st->sd_strstart - st->sd_usroffset;
    636 
    637 		/* now iterate through the symbols */
    638 		maxsym = sym + st->sd_symsize / sizeof(Elf_Sym);
    639 		for (symindx = 0; sym < maxsym; sym++, symindx++) {
    640 			if (callback(str + sym->st_name, symindx,
    641 			    (void *)sym->st_value,
    642 			    sym->st_size,
    643 			    sym->st_info,
    644 			    opaque) != 0) {
    645 				break;
    646 			}
    647 		}
    648 	}
    649 	mutex_exit(&ksyms_lock);
    650 
    651 	return 0;
    652 }
    653 
    654 /*
    655  * Get "mod" and "symbol" associated with an address.
    656  * Returns 0 if success or ENOENT if no such entry.
    657  *
    658  * Call with ksyms_lock, unless known that the symbol table can't change.
    659  */
    660 int
    661 ksyms_getname(const char **mod, const char **sym, vaddr_t v, int f)
    662 {
    663 	struct ksyms_symtab *st;
    664 	Elf_Sym *les, *es = NULL;
    665 	vaddr_t laddr = 0;
    666 	const char *lmod = NULL;
    667 	char *stable = NULL;
    668 	int type, i, sz;
    669 
    670 	if (!ksyms_initted)
    671 		return ENOENT;
    672 
    673 	TAILQ_FOREACH(st, &ksyms_symtabs, sd_queue) {
    674 		if (st->sd_gone)
    675 			continue;
    676 		if (v < st->sd_minsym || v > st->sd_maxsym)
    677 			continue;
    678 		sz = st->sd_symsize/sizeof(Elf_Sym);
    679 		for (i = 0; i < sz; i++) {
    680 			les = st->sd_symstart + i;
    681 			type = ELF_ST_TYPE(les->st_info);
    682 
    683 			if ((f & KSYMS_PROC) && (type != STT_FUNC))
    684 				continue;
    685 
    686 			if (type == STT_NOTYPE)
    687 				continue;
    688 
    689 			if (((f & KSYMS_ANY) == 0) &&
    690 			    (type != STT_FUNC) && (type != STT_OBJECT))
    691 				continue;
    692 
    693 			if ((les->st_value <= v) && (les->st_value > laddr)) {
    694 				laddr = les->st_value;
    695 				es = les;
    696 				lmod = st->sd_name;
    697 				stable = st->sd_strstart - st->sd_usroffset;
    698 			}
    699 		}
    700 	}
    701 	if (es == NULL)
    702 		return ENOENT;
    703 	if ((f & KSYMS_EXACT) && (v != es->st_value))
    704 		return ENOENT;
    705 	if (mod)
    706 		*mod = lmod;
    707 	if (sym)
    708 		*sym = stable + es->st_name;
    709 	return 0;
    710 }
    711 
    712 /*
    713  * Add a symbol table from a loadable module.
    714  */
    715 void
    716 ksyms_modload(const char *name, void *symstart, vsize_t symsize,
    717 	      char *strstart, vsize_t strsize)
    718 {
    719 	struct ksyms_symtab *st;
    720 
    721 	st = kmem_zalloc(sizeof(*st), KM_SLEEP);
    722 	mutex_enter(&ksyms_lock);
    723 	addsymtab(name, symstart, symsize, strstart, strsize, st, symstart,
    724 	    NULL, 0, NULL);
    725 	mutex_exit(&ksyms_lock);
    726 }
    727 
    728 /*
    729  * Remove a symbol table from a loadable module.
    730  */
    731 void
    732 ksyms_modunload(const char *name)
    733 {
    734 	struct ksyms_symtab *st;
    735 
    736 	mutex_enter(&ksyms_lock);
    737 	TAILQ_FOREACH(st, &ksyms_symtabs, sd_queue) {
    738 		if (st->sd_gone)
    739 			continue;
    740 		if (strcmp(name, st->sd_name) != 0)
    741 			continue;
    742 		st->sd_gone = true;
    743 		if (!ksyms_isopen) {
    744 			TAILQ_REMOVE(&ksyms_symtabs, st, sd_queue);
    745 			ksyms_sizes_calc();
    746 			kmem_free(st, sizeof(*st));
    747 		}
    748 		break;
    749 	}
    750 	mutex_exit(&ksyms_lock);
    751 	KASSERT(st != NULL);
    752 }
    753 
    754 #ifdef DDB
    755 /*
    756  * Keep sifting stuff here, to avoid export of ksyms internals.
    757  *
    758  * Systems is expected to be quiescent, so no locking done.
    759  */
    760 int
    761 ksyms_sift(char *mod, char *sym, int mode)
    762 {
    763 	struct ksyms_symtab *st;
    764 	char *sb;
    765 	int i, sz;
    766 
    767 	if (!ksyms_initted)
    768 		return ENOENT;
    769 
    770 	TAILQ_FOREACH(st, &ksyms_symtabs, sd_queue) {
    771 		if (st->sd_gone)
    772 			continue;
    773 		if (mod && strcmp(mod, st->sd_name))
    774 			continue;
    775 		sb = st->sd_strstart - st->sd_usroffset;
    776 
    777 		sz = st->sd_symsize/sizeof(Elf_Sym);
    778 		for (i = 0; i < sz; i++) {
    779 			Elf_Sym *les = st->sd_symstart + i;
    780 			char c;
    781 
    782 			if (strstr(sb + les->st_name, sym) == NULL)
    783 				continue;
    784 
    785 			if (mode == 'F') {
    786 				switch (ELF_ST_TYPE(les->st_info)) {
    787 				case STT_OBJECT:
    788 					c = '+';
    789 					break;
    790 				case STT_FUNC:
    791 					c = '*';
    792 					break;
    793 				case STT_SECTION:
    794 					c = '&';
    795 					break;
    796 				case STT_FILE:
    797 					c = '/';
    798 					break;
    799 				default:
    800 					c = ' ';
    801 					break;
    802 				}
    803 				db_printf("%s%c ", sb + les->st_name, c);
    804 			} else
    805 				db_printf("%s ", sb + les->st_name);
    806 		}
    807 	}
    808 	return ENOENT;
    809 }
    810 #endif /* DDB */
    811 
    812 /*
    813  * In case we exposing the symbol table to the userland using the pseudo-
    814  * device /dev/ksyms, it is easier to provide all the tables as one.
    815  * However, it means we have to change all the st_name fields for the
    816  * symbols so they match the ELF image that the userland will read
    817  * through the device.
    818  *
    819  * The actual (correct) value of st_name is preserved through a global
    820  * offset stored in the symbol table structure.
    821  *
    822  * Call with ksyms_lock held.
    823  */
    824 static void
    825 ksyms_sizes_calc(void)
    826 {
    827         struct ksyms_symtab *st;
    828 	int i, delta;
    829 
    830         ksyms_symsz = ksyms_strsz = 0;
    831         TAILQ_FOREACH(st, &ksyms_symtabs, sd_queue) {
    832 		delta = ksyms_strsz - st->sd_usroffset;
    833 		if (delta != 0) {
    834 			for (i = 0; i < st->sd_symsize/sizeof(Elf_Sym); i++)
    835 				st->sd_symstart[i].st_name += delta;
    836 			st->sd_usroffset = ksyms_strsz;
    837 		}
    838                 ksyms_symsz += st->sd_symsize;
    839                 ksyms_strsz += st->sd_strsize;
    840         }
    841 }
    842 
    843 static void
    844 ksyms_hdr_init(void *hdraddr)
    845 {
    846 
    847 	/* Copy the loaded elf exec header */
    848 	memcpy(&ksyms_hdr.kh_ehdr, hdraddr, sizeof(Elf_Ehdr));
    849 
    850 	/* Set correct program/section header sizes, offsets and numbers */
    851 	ksyms_hdr.kh_ehdr.e_phoff = offsetof(struct ksyms_hdr, kh_phdr[0]);
    852 	ksyms_hdr.kh_ehdr.e_phentsize = sizeof(Elf_Phdr);
    853 	ksyms_hdr.kh_ehdr.e_phnum = NPRGHDR;
    854 	ksyms_hdr.kh_ehdr.e_shoff = offsetof(struct ksyms_hdr, kh_shdr[0]);
    855 	ksyms_hdr.kh_ehdr.e_shentsize = sizeof(Elf_Shdr);
    856 	ksyms_hdr.kh_ehdr.e_shnum = NSECHDR;
    857 	ksyms_hdr.kh_ehdr.e_shstrndx = SHSTRTAB;
    858 
    859 	/* Text/data - fake */
    860 	ksyms_hdr.kh_phdr[0].p_type = PT_LOAD;
    861 	ksyms_hdr.kh_phdr[0].p_memsz = (unsigned long)-1L;
    862 	ksyms_hdr.kh_phdr[0].p_flags = PF_R | PF_X | PF_W;
    863 
    864 	/* First section is null */
    865 
    866 	/* Second section header; ".symtab" */
    867 	ksyms_hdr.kh_shdr[SYMTAB].sh_name = 1; /* Section 3 offset */
    868 	ksyms_hdr.kh_shdr[SYMTAB].sh_type = SHT_SYMTAB;
    869 	ksyms_hdr.kh_shdr[SYMTAB].sh_offset = sizeof(struct ksyms_hdr);
    870 /*	ksyms_hdr.kh_shdr[SYMTAB].sh_size = filled in at open */
    871 	ksyms_hdr.kh_shdr[SYMTAB].sh_link = 2; /* Corresponding strtab */
    872 	ksyms_hdr.kh_shdr[SYMTAB].sh_addralign = sizeof(long);
    873 	ksyms_hdr.kh_shdr[SYMTAB].sh_entsize = sizeof(Elf_Sym);
    874 
    875 	/* Third section header; ".strtab" */
    876 	ksyms_hdr.kh_shdr[STRTAB].sh_name = 9; /* Section 3 offset */
    877 	ksyms_hdr.kh_shdr[STRTAB].sh_type = SHT_STRTAB;
    878 /*	ksyms_hdr.kh_shdr[STRTAB].sh_offset = filled in at open */
    879 /*	ksyms_hdr.kh_shdr[STRTAB].sh_size = filled in at open */
    880 	ksyms_hdr.kh_shdr[STRTAB].sh_addralign = sizeof(char);
    881 
    882 	/* Fourth section, ".shstrtab" */
    883 	ksyms_hdr.kh_shdr[SHSTRTAB].sh_name = 17; /* This section name offset */
    884 	ksyms_hdr.kh_shdr[SHSTRTAB].sh_type = SHT_STRTAB;
    885 	ksyms_hdr.kh_shdr[SHSTRTAB].sh_offset =
    886 	    offsetof(struct ksyms_hdr, kh_strtab);
    887 	ksyms_hdr.kh_shdr[SHSTRTAB].sh_size = SHSTRSIZ;
    888 	ksyms_hdr.kh_shdr[SHSTRTAB].sh_addralign = sizeof(char);
    889 
    890 	/* Fifth section, ".bss". All symbols reside here. */
    891 	ksyms_hdr.kh_shdr[SHBSS].sh_name = 27; /* This section name offset */
    892 	ksyms_hdr.kh_shdr[SHBSS].sh_type = SHT_NOBITS;
    893 	ksyms_hdr.kh_shdr[SHBSS].sh_offset = 0;
    894 	ksyms_hdr.kh_shdr[SHBSS].sh_size = (unsigned long)-1L;
    895 	ksyms_hdr.kh_shdr[SHBSS].sh_addralign = PAGE_SIZE;
    896 	ksyms_hdr.kh_shdr[SHBSS].sh_flags = SHF_ALLOC | SHF_EXECINSTR;
    897 
    898 #ifdef KDTRACE_HOOKS
    899 	/* Sixth section header; ".SUNW_ctf" */
    900 	ksyms_hdr.kh_shdr[SHCTF].sh_name = 32; /* Section 6 offset */
    901 	ksyms_hdr.kh_shdr[SHCTF].sh_type = SHT_PROGBITS;
    902 /*	ksyms_hdr.kh_shdr[SHCTF].sh_offset = filled in at open */
    903 /*	ksyms_hdr.kh_shdr[SHCTF].sh_size = filled in at open */
    904 	ksyms_hdr.kh_shdr[SHCTF].sh_link = SYMTAB; /* Corresponding symtab */
    905 	ksyms_hdr.kh_shdr[SHCTF].sh_addralign = sizeof(char);
    906 #endif
    907 
    908 	/* Set section names */
    909 	strlcpy(&ksyms_hdr.kh_strtab[1], ".symtab",
    910 	    sizeof(ksyms_hdr.kh_strtab) - 1);
    911 	strlcpy(&ksyms_hdr.kh_strtab[9], ".strtab",
    912 	    sizeof(ksyms_hdr.kh_strtab) - 9);
    913 	strlcpy(&ksyms_hdr.kh_strtab[17], ".shstrtab",
    914 	    sizeof(ksyms_hdr.kh_strtab) - 17);
    915 	strlcpy(&ksyms_hdr.kh_strtab[27], ".bss",
    916 	    sizeof(ksyms_hdr.kh_strtab) - 27);
    917 #ifdef KDTRACE_HOOKS
    918 	strlcpy(&ksyms_hdr.kh_strtab[32], ".SUNW_ctf",
    919 	    sizeof(ksyms_hdr.kh_strtab) - 32);
    920 #endif
    921 }
    922 
    923 static int
    924 ksymsopen(dev_t dev, int oflags, int devtype, struct lwp *l)
    925 {
    926 
    927 	if (minor(dev) != 0 || !ksyms_initted)
    928 		return ENXIO;
    929 
    930 	/*
    931 	 * Create a "snapshot" of the kernel symbol table.  Setting
    932 	 * ksyms_isopen will prevent symbol tables from being freed.
    933 	 */
    934 	mutex_enter(&ksyms_lock);
    935 	ksyms_hdr.kh_shdr[SYMTAB].sh_size = ksyms_symsz;
    936 	ksyms_hdr.kh_shdr[SYMTAB].sh_info = ksyms_symsz / sizeof(Elf_Sym);
    937 	ksyms_hdr.kh_shdr[STRTAB].sh_offset = ksyms_symsz +
    938 	    ksyms_hdr.kh_shdr[SYMTAB].sh_offset;
    939 	ksyms_hdr.kh_shdr[STRTAB].sh_size = ksyms_strsz;
    940 #ifdef KDTRACE_HOOKS
    941 	ksyms_hdr.kh_shdr[SHCTF].sh_offset = ksyms_strsz +
    942 	    ksyms_hdr.kh_shdr[STRTAB].sh_offset;
    943 	ksyms_hdr.kh_shdr[SHCTF].sh_size = ksyms_ctfsz;
    944 #endif
    945 	ksyms_isopen = true;
    946 	mutex_exit(&ksyms_lock);
    947 
    948 	return 0;
    949 }
    950 
    951 static int
    952 ksymsclose(dev_t dev, int oflags, int devtype, struct lwp *l)
    953 {
    954 	struct ksyms_symtab *st, *next;
    955 	bool resize;
    956 
    957 	/* Discard refernces to symbol tables. */
    958 	mutex_enter(&ksyms_lock);
    959 	ksyms_isopen = false;
    960 	resize = false;
    961 	for (st = TAILQ_FIRST(&ksyms_symtabs); st != NULL; st = next) {
    962 		next = TAILQ_NEXT(st, sd_queue);
    963 		if (st->sd_gone) {
    964 			TAILQ_REMOVE(&ksyms_symtabs, st, sd_queue);
    965 			kmem_free(st, sizeof(*st));
    966 			resize = true;
    967 		}
    968 	}
    969 	if (resize)
    970 		ksyms_sizes_calc();
    971 	mutex_exit(&ksyms_lock);
    972 
    973 	return 0;
    974 }
    975 
    976 static int
    977 ksymsread(dev_t dev, struct uio *uio, int ioflag)
    978 {
    979 	struct ksyms_symtab *st;
    980 	size_t filepos, inpos, off;
    981 	int error;
    982 #ifdef KDTRACE_HOOKS
    983 	struct ksyms_symtab *cst;
    984 #endif
    985 
    986 	/*
    987 	 * First: Copy out the ELF header.   XXX Lose if ksymsopen()
    988 	 * occurs during read of the header.
    989 	 */
    990 	off = uio->uio_offset;
    991 	if (off < sizeof(struct ksyms_hdr)) {
    992 		error = uiomove((char *)&ksyms_hdr + off,
    993 		    sizeof(struct ksyms_hdr) - off, uio);
    994 		if (error != 0)
    995 			return error;
    996 	}
    997 
    998 	/*
    999 	 * Copy out the symbol table.
   1000 	 */
   1001 	filepos = sizeof(struct ksyms_hdr);
   1002 	TAILQ_FOREACH(st, &ksyms_symtabs, sd_queue) {
   1003 		if (uio->uio_resid == 0)
   1004 			return 0;
   1005 		if (uio->uio_offset <= st->sd_symsize + filepos) {
   1006 			inpos = uio->uio_offset - filepos;
   1007 			error = uiomove((char *)st->sd_symstart + inpos,
   1008 			   st->sd_symsize - inpos, uio);
   1009 			if (error != 0)
   1010 				return error;
   1011 		}
   1012 		filepos += st->sd_symsize;
   1013 	}
   1014 
   1015 	/*
   1016 	 * Copy out the string table
   1017 	 */
   1018 	KASSERT(filepos == sizeof(struct ksyms_hdr) +
   1019 	    ksyms_hdr.kh_shdr[SYMTAB].sh_size);
   1020 	TAILQ_FOREACH(st, &ksyms_symtabs, sd_queue) {
   1021 		if (uio->uio_resid == 0)
   1022 			return 0;
   1023 		if (uio->uio_offset <= st->sd_strsize + filepos) {
   1024 			inpos = uio->uio_offset - filepos;
   1025 			error = uiomove((char *)st->sd_strstart + inpos,
   1026 			   st->sd_strsize - inpos, uio);
   1027 			if (error != 0)
   1028 				return error;
   1029 		}
   1030 		filepos += st->sd_strsize;
   1031 	}
   1032 
   1033 #ifdef KDTRACE_HOOKS
   1034 	/*
   1035 	 * Copy out the CTF table.
   1036 	 */
   1037 	cst = TAILQ_FIRST(&ksyms_symtabs);
   1038 	if (cst->sd_ctfstart != NULL) {
   1039 		if (uio->uio_resid == 0)
   1040 			return 0;
   1041 		if (uio->uio_offset <= cst->sd_ctfsize + filepos) {
   1042 			inpos = uio->uio_offset - filepos;
   1043 			error = uiomove((char *)cst->sd_ctfstart + inpos,
   1044 			   cst->sd_ctfsize - inpos, uio);
   1045 			if (error != 0)
   1046 				return error;
   1047 		}
   1048 		filepos += cst->sd_ctfsize;
   1049 	}
   1050 #endif
   1051 
   1052 	return 0;
   1053 }
   1054 
   1055 static int
   1056 ksymswrite(dev_t dev, struct uio *uio, int ioflag)
   1057 {
   1058 
   1059 	return EROFS;
   1060 }
   1061 
   1062 static int
   1063 ksymsioctl(dev_t dev, u_long cmd, void *data, int fflag, struct lwp *l)
   1064 {
   1065 	struct ksyms_gsymbol *kg = (struct ksyms_gsymbol *)data;
   1066 	struct ksyms_symtab *st;
   1067 	Elf_Sym *sym = NULL, copy;
   1068 	unsigned long val;
   1069 	int error = 0;
   1070 	char *str = NULL;
   1071 	int len;
   1072 
   1073 	/* Read ksyms_maxlen only once while not holding the lock. */
   1074 	len = ksyms_maxlen;
   1075 
   1076 	if (cmd == KIOCGVALUE || cmd == KIOCGSYMBOL) {
   1077 		str = kmem_alloc(len, KM_SLEEP);
   1078 		if ((error = copyinstr(kg->kg_name, str, len, NULL)) != 0) {
   1079 			kmem_free(str, len);
   1080 			return error;
   1081 		}
   1082 	}
   1083 
   1084 	switch (cmd) {
   1085 	case KIOCGVALUE:
   1086 		/*
   1087 		 * Use the in-kernel symbol lookup code for fast
   1088 		 * retreival of a value.
   1089 		 */
   1090 		error = ksyms_getval(NULL, str, &val, KSYMS_EXTERN);
   1091 		if (error == 0)
   1092 			error = copyout(&val, kg->kg_value, sizeof(long));
   1093 		kmem_free(str, len);
   1094 		break;
   1095 
   1096 	case KIOCGSYMBOL:
   1097 		/*
   1098 		 * Use the in-kernel symbol lookup code for fast
   1099 		 * retreival of a symbol.
   1100 		 */
   1101 		mutex_enter(&ksyms_lock);
   1102 		TAILQ_FOREACH(st, &ksyms_symtabs, sd_queue) {
   1103 			if (st->sd_gone)
   1104 				continue;
   1105 			if ((sym = findsym(str, st, KSYMS_ANY)) == NULL)
   1106 				continue;
   1107 #ifdef notdef
   1108 			/* Skip if bad binding */
   1109 			if (ELF_ST_BIND(sym->st_info) != STB_GLOBAL) {
   1110 				sym = NULL;
   1111 				continue;
   1112 			}
   1113 #endif
   1114 			break;
   1115 		}
   1116 		if (sym != NULL) {
   1117 			memcpy(&copy, sym, sizeof(copy));
   1118 			mutex_exit(&ksyms_lock);
   1119 			error = copyout(&copy, kg->kg_sym, sizeof(Elf_Sym));
   1120 		} else {
   1121 			mutex_exit(&ksyms_lock);
   1122 			error = ENOENT;
   1123 		}
   1124 		kmem_free(str, len);
   1125 		break;
   1126 
   1127 	case KIOCGSIZE:
   1128 		/*
   1129 		 * Get total size of symbol table.
   1130 		 */
   1131 		mutex_enter(&ksyms_lock);
   1132 		*(int *)data = ksyms_strsz + ksyms_symsz +
   1133 		    sizeof(struct ksyms_hdr);
   1134 		mutex_exit(&ksyms_lock);
   1135 		break;
   1136 
   1137 	default:
   1138 		error = ENOTTY;
   1139 		break;
   1140 	}
   1141 
   1142 	return error;
   1143 }
   1144 
   1145 const struct cdevsw ksyms_cdevsw = {
   1146 	ksymsopen, ksymsclose, ksymsread, ksymswrite, ksymsioctl,
   1147 	nullstop, notty, nopoll, nommap, nullkqfilter, D_OTHER | D_MPSAFE
   1148 };
   1149