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