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