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