Home | History | Annotate | Line # | Download | only in kern
kern_ksyms.c revision 1.41.4.1.4.1
      1  1.41.4.1.4.1      matt /*	$NetBSD: kern_ksyms.c,v 1.41.4.1.4.1 2010/04/21 00:28:16 matt 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  *	Consider replacing patricia tree with simpler binary search
     71          1.39        ad  *	for symbol tables.
     72          1.39        ad  *
     73          1.39        ad  *	Add support for mmap, poll.
     74           1.1     ragge  */
     75          1.11  jdolecek 
     76          1.11  jdolecek #include <sys/cdefs.h>
     77  1.41.4.1.4.1      matt __KERNEL_RCSID(0, "$NetBSD: kern_ksyms.c,v 1.41.4.1.4.1 2010/04/21 00:28:16 matt Exp $");
     78           1.1     ragge 
     79           1.1     ragge #ifdef _KERNEL
     80           1.1     ragge #include "opt_ddb.h"
     81           1.3     ragge #include "opt_ddbparam.h"	/* for SYMTAB_SPACE */
     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/errno.h>
     88           1.1     ragge #include <sys/queue.h>
     89           1.1     ragge #include <sys/exec.h>
     90           1.1     ragge #include <sys/systm.h>
     91           1.1     ragge #include <sys/conf.h>
     92           1.1     ragge #include <sys/malloc.h>
     93          1.39        ad #include <sys/kmem.h>
     94           1.1     ragge #include <sys/proc.h>
     95          1.39        ad #include <sys/module.h>
     96          1.39        ad #include <sys/atomic.h>
     97           1.1     ragge #include <sys/ksyms.h>
     98           1.1     ragge 
     99           1.1     ragge #include <lib/libkern/libkern.h>
    100           1.1     ragge 
    101           1.1     ragge #ifdef DDB
    102           1.1     ragge #include <ddb/db_output.h>
    103           1.1     ragge #endif
    104           1.1     ragge 
    105           1.1     ragge #include "ksyms.h"
    106           1.1     ragge 
    107          1.39        ad static int ksyms_maxlen;
    108          1.39        ad static bool ksyms_isopen;
    109          1.39        ad static bool ksyms_initted;
    110          1.39        ad static struct ksyms_hdr ksyms_hdr;
    111          1.40  christos static kmutex_t ksyms_lock;
    112           1.1     ragge 
    113          1.39        ad void ksymsattach(int);
    114          1.40  christos static void ksyms_hdr_init(void *);
    115           1.1     ragge static void ksyms_sizes_calc(void);
    116           1.1     ragge 
    117           1.1     ragge #ifdef KSYMS_DEBUG
    118           1.1     ragge #define	FOLLOW_CALLS		1
    119           1.1     ragge #define	FOLLOW_MORE_CALLS	2
    120           1.1     ragge #define	FOLLOW_DEVKSYMS		4
    121           1.1     ragge static int ksyms_debug;
    122           1.1     ragge #endif
    123           1.1     ragge 
    124           1.3     ragge #ifdef SYMTAB_SPACE
    125           1.3     ragge #define		SYMTAB_FILLER	"|This is the symbol table!"
    126           1.3     ragge 
    127           1.3     ragge char		db_symtab[SYMTAB_SPACE] = SYMTAB_FILLER;
    128           1.3     ragge int		db_symtabsize = SYMTAB_SPACE;
    129           1.3     ragge #endif
    130           1.1     ragge 
    131          1.39        ad int ksyms_symsz;
    132          1.39        ad int ksyms_strsz;
    133          1.39        ad TAILQ_HEAD(, ksyms_symtab) ksyms_symtabs =
    134          1.39        ad     TAILQ_HEAD_INITIALIZER(ksyms_symtabs);
    135          1.39        ad static struct ksyms_symtab kernel_symtab;
    136           1.1     ragge 
    137           1.8     ragge /*
    138           1.8     ragge  * Patricia-tree-based lookup structure for the in-kernel global symbols.
    139           1.8     ragge  * Based on a design by Mikael Sundstrom, msm (at) sm.luth.se.
    140           1.8     ragge  */
    141           1.8     ragge struct ptree {
    142           1.8     ragge 	int16_t bitno;
    143           1.8     ragge 	int16_t lr[2];
    144           1.8     ragge } *symb;
    145           1.8     ragge static int16_t baseidx;
    146           1.8     ragge static int treex = 1;
    147           1.8     ragge 
    148           1.8     ragge #define	P_BIT(key, bit) ((key[bit >> 3] >> (bit & 7)) & 1)
    149          1.32  christos #define	STRING(idx) (kernel_symtab.sd_symstart[idx].st_name + \
    150          1.32  christos 			kernel_symtab.sd_strstart)
    151           1.8     ragge 
    152          1.33  christos static int
    153          1.33  christos ksyms_verify(void *symstart, void *strstart)
    154          1.33  christos {
    155          1.33  christos #if defined(DIAGNOSTIC) || defined(DEBUG)
    156          1.33  christos 	if (symstart == NULL)
    157          1.33  christos 		printf("ksyms: Symbol table not found\n");
    158          1.33  christos 	if (strstart == NULL)
    159          1.33  christos 		printf("ksyms: String table not found\n");
    160          1.33  christos 	if (symstart == NULL || strstart == NULL)
    161          1.33  christos 		printf("ksyms: Perhaps the kernel is stripped?\n");
    162          1.33  christos #endif
    163          1.33  christos 	if (symstart == NULL || strstart == NULL)
    164          1.33  christos 		return 0;
    165          1.33  christos 	KASSERT(symstart <= strstart);
    166          1.33  christos 	return 1;
    167          1.33  christos }
    168          1.33  christos 
    169           1.8     ragge /*
    170           1.8     ragge  * Walk down the tree until a terminal node is found.
    171           1.8     ragge  */
    172           1.8     ragge static int
    173          1.24  christos symbol_traverse(const char *key)
    174           1.8     ragge {
    175           1.8     ragge 	int16_t nb, rbit = baseidx;
    176           1.8     ragge 
    177           1.8     ragge 	while (rbit > 0) {
    178           1.8     ragge 		nb = symb[rbit].bitno;
    179           1.8     ragge 		rbit = symb[rbit].lr[P_BIT(key, nb)];
    180           1.8     ragge 	}
    181           1.8     ragge 	return -rbit;
    182           1.8     ragge }
    183           1.8     ragge 
    184           1.8     ragge static int
    185           1.8     ragge ptree_add(char *key, int val)
    186           1.8     ragge {
    187           1.8     ragge 	int idx;
    188          1.15  christos 	int nix, cix, bit, rbit, sb, lastrbit, svbit = 0, ix;
    189           1.8     ragge 	char *m, *k;
    190           1.8     ragge 
    191           1.8     ragge 	if (baseidx == 0) {
    192           1.8     ragge 		baseidx = -val;
    193           1.8     ragge 		return 0; /* First element */
    194           1.8     ragge 	}
    195           1.8     ragge 
    196           1.8     ragge 	/* Get string to match against */
    197           1.8     ragge 	idx = symbol_traverse(key);
    198           1.8     ragge 
    199           1.8     ragge 	/* Find first mismatching bit */
    200           1.8     ragge 	m = STRING(idx);
    201           1.8     ragge 	k = key;
    202           1.8     ragge 	if (strcmp(m, k) == 0)
    203           1.8     ragge 		return 1;
    204           1.8     ragge 
    205           1.8     ragge 	for (cix = 0; *m && *k && *m == *k; m++, k++, cix += 8)
    206           1.8     ragge 		;
    207           1.8     ragge 	ix = ffs((int)*m ^ (int)*k) - 1;
    208           1.8     ragge 	cix += ix;
    209           1.8     ragge 
    210           1.8     ragge 	/* Create new node */
    211           1.8     ragge 	nix = treex++;
    212           1.8     ragge 	bit = P_BIT(key, cix);
    213           1.8     ragge 	symb[nix].bitno = cix;
    214           1.8     ragge 	symb[nix].lr[bit] = -val;
    215           1.8     ragge 
    216           1.8     ragge 	/* Find where to insert node */
    217           1.8     ragge 	rbit = baseidx;
    218           1.8     ragge 	lastrbit = 0;
    219           1.8     ragge 	for (;;) {
    220           1.8     ragge 		if (rbit < 0)
    221           1.8     ragge 			break;
    222           1.8     ragge 		sb = symb[rbit].bitno;
    223           1.8     ragge 		if (sb > cix)
    224           1.8     ragge 			break;
    225           1.8     ragge 		if (sb == cix)
    226           1.8     ragge 			printf("symb[rbit].bitno == cix!!!\n");
    227           1.8     ragge 		lastrbit = rbit;
    228           1.8     ragge 		svbit = P_BIT(key, sb);
    229           1.8     ragge 		rbit = symb[rbit].lr[svbit];
    230           1.8     ragge 	}
    231           1.8     ragge 
    232           1.8     ragge 	/* Do the actual insertion */
    233           1.8     ragge 	if (lastrbit == 0) {
    234           1.8     ragge 		/* first element */
    235           1.8     ragge 		symb[nix].lr[!bit] = baseidx;
    236           1.8     ragge 		baseidx = nix;
    237           1.8     ragge 	} else {
    238           1.8     ragge 		symb[nix].lr[!bit] = rbit;
    239           1.8     ragge 		symb[lastrbit].lr[svbit] = nix;
    240           1.8     ragge 	}
    241           1.8     ragge 	return 0;
    242           1.8     ragge }
    243           1.8     ragge 
    244           1.8     ragge static int
    245          1.24  christos ptree_find(const char *key)
    246           1.8     ragge {
    247           1.8     ragge 	int idx;
    248           1.8     ragge 
    249           1.8     ragge 	if (baseidx == 0)
    250           1.8     ragge 		return 0;
    251           1.8     ragge 	idx = symbol_traverse(key);
    252           1.8     ragge 
    253           1.8     ragge 	if (strcmp(key, STRING(idx)) == 0)
    254           1.8     ragge 		return idx;
    255           1.8     ragge 	return 0;
    256           1.8     ragge }
    257           1.8     ragge 
    258           1.8     ragge static void
    259          1.39        ad ptree_gen(char *off, struct ksyms_symtab *tab)
    260           1.8     ragge {
    261           1.8     ragge 	Elf_Sym *sym;
    262          1.16     ragge 	int i, nsym;
    263           1.8     ragge 
    264           1.8     ragge 	if (off != NULL)
    265           1.8     ragge 		symb = (struct ptree *)ALIGN(off);
    266           1.8     ragge 	else
    267           1.8     ragge 		symb = malloc((tab->sd_symsize/sizeof(Elf_Sym)) *
    268           1.8     ragge 		    sizeof(struct ptree), M_DEVBUF, M_WAITOK);
    269           1.8     ragge 	symb--; /* sym index won't be 0 */
    270           1.8     ragge 
    271           1.8     ragge 	sym = tab->sd_symstart;
    272          1.16     ragge 	if ((nsym = tab->sd_symsize/sizeof(Elf_Sym)) > INT16_MAX) {
    273          1.16     ragge 		printf("Too many symbols for tree, skipping %d symbols\n",
    274          1.16     ragge 		    nsym-INT16_MAX);
    275          1.16     ragge 		nsym = INT16_MAX;
    276          1.16     ragge 	}
    277          1.16     ragge 	for (i = 1; i < nsym; i++) {
    278           1.8     ragge 		if (ELF_ST_BIND(sym[i].st_info) != STB_GLOBAL)
    279           1.8     ragge 			continue;
    280           1.8     ragge 		ptree_add(tab->sd_strstart+sym[i].st_name, i);
    281          1.39        ad 		if (tab->sd_minsym == NULL ||
    282          1.39        ad 		    sym[i].st_value < tab->sd_minsym->st_value)
    283          1.35      matt 			tab->sd_minsym = &sym[i];
    284          1.39        ad 		if (tab->sd_maxsym == NULL ||
    285          1.39        ad 		    sym[i].st_value > tab->sd_maxsym->st_value)
    286          1.35      matt 			tab->sd_maxsym = &sym[i];
    287           1.8     ragge 	}
    288           1.8     ragge }
    289           1.8     ragge 
    290           1.1     ragge /*
    291           1.1     ragge  * Finds a certain symbol name in a certain symbol table.
    292           1.1     ragge  */
    293           1.1     ragge static Elf_Sym *
    294          1.39        ad findsym(const char *name, struct ksyms_symtab *table)
    295           1.1     ragge {
    296           1.1     ragge 	Elf_Sym *start = table->sd_symstart;
    297           1.1     ragge 	int i, sz = table->sd_symsize/sizeof(Elf_Sym);
    298           1.1     ragge 	char *np;
    299          1.32  christos 	char *realstart = table->sd_strstart - table->sd_usroffset;
    300           1.1     ragge 
    301           1.8     ragge 	if (table == &kernel_symtab && (i = ptree_find(name)) != 0)
    302           1.8     ragge 		return &start[i];
    303           1.8     ragge 
    304           1.1     ragge 	for (i = 0; i < sz; i++) {
    305          1.17      cube 		np = realstart + start[i].st_name;
    306           1.1     ragge 		if (name[0] == np[0] && name[1] == np[1] &&
    307           1.1     ragge 		    strcmp(name, np) == 0)
    308           1.1     ragge 			return &start[i];
    309           1.1     ragge 	}
    310           1.1     ragge 	return NULL;
    311           1.1     ragge }
    312           1.1     ragge 
    313           1.1     ragge /*
    314           1.1     ragge  * The "attach" is in reality done in ksyms_init().
    315           1.1     ragge  */
    316           1.1     ragge void
    317          1.30      yamt ksymsattach(int arg)
    318           1.1     ragge {
    319           1.8     ragge 	if (baseidx == 0)
    320           1.8     ragge 		ptree_gen(0, &kernel_symtab);
    321           1.1     ragge }
    322           1.1     ragge 
    323           1.1     ragge /*
    324          1.29      jmmv  * Add a symbol table.
    325          1.29      jmmv  * This is intended for use when the symbol table and its corresponding
    326          1.29      jmmv  * string table are easily available.  If they are embedded in an ELF
    327          1.29      jmmv  * image, use addsymtab_elf() instead.
    328          1.29      jmmv  *
    329          1.29      jmmv  * name - Symbol's table name.
    330          1.29      jmmv  * symstart, symsize - Address and size of the symbol table.
    331          1.29      jmmv  * strstart, strsize - Address and size of the string table.
    332          1.29      jmmv  * tab - Symbol table to be updated with this information.
    333          1.29      jmmv  * newstart - Address to which the symbol table has to be copied during
    334          1.29      jmmv  *            shrinking.  If NULL, it is not moved.
    335           1.1     ragge  */
    336           1.1     ragge static void
    337          1.39        ad addsymtab(const char *name, void *symstart, size_t symsize,
    338          1.39        ad 	  void *strstart, size_t strsize, struct ksyms_symtab *tab,
    339          1.39        ad 	  void *newstart)
    340           1.1     ragge {
    341           1.8     ragge 	Elf_Sym *sym, *nsym;
    342          1.39        ad 	int i, j, n;
    343           1.8     ragge 	char *str;
    344           1.1     ragge 
    345          1.39        ad 	tab->sd_symstart = symstart;
    346          1.29      jmmv 	tab->sd_symsize = symsize;
    347          1.29      jmmv 	tab->sd_strstart = strstart;
    348          1.29      jmmv 	tab->sd_strsize = strsize;
    349           1.1     ragge 	tab->sd_name = name;
    350          1.39        ad 	tab->sd_minsym = NULL;
    351          1.39        ad 	tab->sd_maxsym = NULL;
    352          1.39        ad 	tab->sd_usroffset = 0;
    353          1.39        ad 	tab->sd_gone = false;
    354          1.39        ad 	tab->sd_malloc = false;	/* XXXLKM */
    355           1.8     ragge #ifdef KSYMS_DEBUG
    356          1.39        ad 	printf("newstart %p sym %p ksyms_symsz %d str %p strsz %d send %p\n",
    357          1.39        ad 	    newstart, symstart, symsize, strstart, strsize,
    358          1.39        ad 	    tab->sd_strstart + tab->sd_strsize);
    359           1.8     ragge #endif
    360           1.1     ragge 
    361          1.39        ad 	/* Pack symbol table by removing all file name references. */
    362           1.8     ragge 	sym = tab->sd_symstart;
    363          1.29      jmmv 	nsym = (Elf_Sym *)newstart;
    364           1.8     ragge 	str = tab->sd_strstart;
    365          1.39        ad 	for (i = n = 0; i < tab->sd_symsize/sizeof(Elf_Sym); i++) {
    366           1.8     ragge 		/*
    367           1.8     ragge 		 * Remove useless symbols.
    368           1.8     ragge 		 * Should actually remove all typeless symbols.
    369           1.8     ragge 		 */
    370           1.5     ragge 		if (sym[i].st_name == 0)
    371           1.8     ragge 			continue; /* Skip nameless entries */
    372          1.34        ad 		if (sym[i].st_shndx == SHN_UNDEF)
    373          1.34        ad 			continue; /* Skip external references */
    374           1.8     ragge 		if (ELF_ST_TYPE(sym[i].st_info) == STT_FILE)
    375           1.8     ragge 			continue; /* Skip filenames */
    376           1.8     ragge 		if (ELF_ST_TYPE(sym[i].st_info) == STT_NOTYPE &&
    377           1.8     ragge 		    sym[i].st_value == 0 &&
    378           1.8     ragge 		    strcmp(str + sym[i].st_name, "*ABS*") == 0)
    379           1.8     ragge 			continue; /* XXX */
    380           1.8     ragge 		if (ELF_ST_TYPE(sym[i].st_info) == STT_NOTYPE &&
    381           1.8     ragge 		    strcmp(str + sym[i].st_name, "gcc2_compiled.") == 0)
    382           1.8     ragge 			continue; /* XXX */
    383           1.8     ragge 
    384           1.8     ragge 		/* Save symbol. Set it as an absolute offset */
    385           1.8     ragge 		nsym[n] = sym[i];
    386           1.8     ragge 		nsym[n].st_shndx = SHN_ABS;
    387          1.39        ad 		j = strlen(nsym[n].st_name + tab->sd_strstart) + 1;
    388          1.39        ad 		if (j > ksyms_maxlen)
    389          1.39        ad 			ksyms_maxlen = j;
    390           1.8     ragge 		n++;
    391           1.8     ragge 
    392           1.5     ragge 	}
    393           1.8     ragge 	tab->sd_symstart = nsym;
    394           1.8     ragge 	tab->sd_symsize = n * sizeof(Elf_Sym);
    395          1.39        ad 	/* ksymsread() is unlocked, so membar. */
    396          1.39        ad 	membar_producer();
    397          1.39        ad 	TAILQ_INSERT_TAIL(&ksyms_symtabs, tab, sd_queue);
    398          1.39        ad 	ksyms_sizes_calc();
    399          1.39        ad 	ksyms_initted = true;
    400           1.1     ragge }
    401           1.1     ragge 
    402      1.41.4.1       snj void
    403      1.41.4.1       snj ksyms_init_finalize()
    404      1.41.4.1       snj {
    405      1.41.4.1       snj 	mutex_init(&ksyms_lock, MUTEX_DEFAULT, IPL_NONE);
    406      1.41.4.1       snj }
    407      1.41.4.1       snj 
    408           1.1     ragge /*
    409          1.39        ad  * Setup the kernel symbol table stuff.
    410          1.29      jmmv  */
    411          1.39        ad void
    412          1.39        ad ksyms_init(int symsize, void *start, void *end)
    413          1.29      jmmv {
    414          1.29      jmmv 	int i, j;
    415          1.29      jmmv 	Elf_Shdr *shdr;
    416          1.32  christos 	char *symstart = NULL, *strstart = NULL;
    417          1.39        ad 	size_t strsize = 0;
    418           1.3     ragge 	Elf_Ehdr *ehdr;
    419           1.3     ragge 
    420           1.3     ragge #ifdef SYMTAB_SPACE
    421           1.3     ragge 	if (symsize <= 0 &&
    422           1.3     ragge 	    strncmp(db_symtab, SYMTAB_FILLER, sizeof(SYMTAB_FILLER))) {
    423           1.3     ragge 		symsize = db_symtabsize;
    424           1.3     ragge 		start = db_symtab;
    425           1.3     ragge 		end = db_symtab + db_symtabsize;
    426           1.3     ragge 	}
    427           1.3     ragge #endif
    428           1.3     ragge 	if (symsize <= 0) {
    429           1.3     ragge 		printf("[ Kernel symbol table missing! ]\n");
    430           1.3     ragge 		return;
    431           1.3     ragge 	}
    432           1.3     ragge 
    433           1.3     ragge 	/* Sanity check */
    434           1.3     ragge 	if (ALIGNED_POINTER(start, long) == 0) {
    435           1.3     ragge 		printf("[ Kernel symbol table has bad start address %p ]\n",
    436           1.3     ragge 		    start);
    437           1.3     ragge 		return;
    438           1.3     ragge 	}
    439           1.3     ragge 
    440           1.3     ragge 	ehdr = (Elf_Ehdr *)start;
    441           1.1     ragge 
    442           1.1     ragge 	/* check if this is a valid ELF header */
    443           1.1     ragge 	/* No reason to verify arch type, the kernel is actually running! */
    444           1.1     ragge 	if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG) ||
    445           1.1     ragge 	    ehdr->e_ident[EI_CLASS] != ELFCLASS ||
    446           1.1     ragge 	    ehdr->e_version > 1) {
    447           1.3     ragge 		printf("[ Kernel symbol table invalid! ]\n");
    448           1.1     ragge 		return; /* nothing to do */
    449           1.1     ragge 	}
    450           1.1     ragge 
    451           1.8     ragge 	/* Loaded header will be scratched in addsymtab */
    452           1.8     ragge 	ksyms_hdr_init(start);
    453           1.8     ragge 
    454          1.39        ad 	/* Find the symbol table and the corresponding string table. */
    455          1.39        ad 	shdr = (Elf_Shdr *)((uint8_t *)start + ehdr->e_shoff);
    456          1.39        ad 	for (i = 1; i < ehdr->e_shnum; i++) {
    457          1.39        ad 		if (shdr[i].sh_type != SHT_SYMTAB)
    458          1.39        ad 			continue;
    459          1.39        ad 		if (shdr[i].sh_offset == 0)
    460          1.39        ad 			continue;
    461          1.39        ad 		symstart = (uint8_t *)start + shdr[i].sh_offset;
    462          1.39        ad 		symsize = shdr[i].sh_size;
    463          1.39        ad 		j = shdr[i].sh_link;
    464          1.39        ad 		if (shdr[j].sh_offset == 0)
    465          1.39        ad 			continue; /* Can this happen? */
    466          1.39        ad 		strstart = (uint8_t *)start + shdr[j].sh_offset;
    467          1.39        ad 		strsize = shdr[j].sh_size;
    468          1.39        ad 		break;
    469          1.39        ad 	}
    470           1.8     ragge 
    471  1.41.4.1.4.1      matt 	if (!ksyms_verify(symstart, strstart)) {
    472  1.41.4.1.4.1      matt 		printf("[ Kernel symbol table failed verification! ]\n");
    473          1.39        ad 		return;
    474  1.41.4.1.4.1      matt 	}
    475          1.39        ad 	addsymtab("netbsd", symstart, symsize, strstart, strsize,
    476          1.39        ad 	    &kernel_symtab, start);
    477           1.8     ragge 
    478           1.1     ragge #ifdef DEBUG
    479  1.41.4.1.4.1      matt 	aprint_normal("Loaded initial symtab at %p, strtab at %p, # entries %ld\n",
    480           1.1     ragge 	    kernel_symtab.sd_symstart, kernel_symtab.sd_strstart,
    481           1.2     ragge 	    (long)kernel_symtab.sd_symsize/sizeof(Elf_Sym));
    482           1.1     ragge #endif
    483           1.1     ragge }
    484           1.1     ragge 
    485           1.1     ragge /*
    486          1.29      jmmv  * Setup the kernel symbol table stuff.
    487          1.29      jmmv  * Use this when the address of the symbol and string tables are known;
    488          1.29      jmmv  * otherwise use ksyms_init with an ELF image.
    489          1.31      jmmv  * We need to pass a minimal ELF header which will later be completed by
    490          1.31      jmmv  * ksyms_hdr_init and handed off to userland through /dev/ksyms.  We use
    491          1.32  christos  * a void *rather than a pointer to avoid exposing the Elf_Ehdr type.
    492          1.29      jmmv  */
    493          1.29      jmmv void
    494          1.32  christos ksyms_init_explicit(void *ehdr, void *symstart, size_t symsize,
    495          1.39        ad 		    void *strstart, size_t strsize)
    496          1.29      jmmv {
    497          1.29      jmmv 
    498          1.33  christos 	if (!ksyms_verify(symstart, strstart))
    499          1.33  christos 		return;
    500          1.29      jmmv 
    501          1.31      jmmv 	ksyms_hdr_init(ehdr);
    502          1.29      jmmv 	addsymtab("netbsd", symstart, symsize, strstart, strsize,
    503          1.39        ad 	    &kernel_symtab, symstart);
    504          1.29      jmmv }
    505          1.29      jmmv 
    506          1.29      jmmv /*
    507           1.1     ragge  * Get the value associated with a symbol.
    508          1.23     perry  * "mod" is the module name, or null if any module.
    509           1.1     ragge  * "sym" is the symbol name.
    510           1.1     ragge  * "val" is a pointer to the corresponding value, if call succeeded.
    511           1.1     ragge  * Returns 0 if success or ENOENT if no such entry.
    512          1.39        ad  *
    513          1.39        ad  * Call with ksyms_lock, unless known that the symbol table can't change.
    514           1.1     ragge  */
    515          1.41  christos int
    516          1.40  christos ksyms_getval_unlocked(const char *mod, const char *sym, unsigned long *val,
    517          1.40  christos     int type)
    518           1.1     ragge {
    519          1.39        ad 	struct ksyms_symtab *st;
    520           1.1     ragge 	Elf_Sym *es;
    521           1.1     ragge 
    522          1.39        ad 	if (!ksyms_initted)
    523           1.1     ragge 		return ENOENT;
    524           1.1     ragge 
    525           1.1     ragge #ifdef KSYMS_DEBUG
    526           1.1     ragge 	if (ksyms_debug & FOLLOW_CALLS)
    527          1.40  christos 		printf("ksyms_getval_unlocked: mod %s sym %s valp %p\n",
    528          1.40  christos 		    mod, sym, val);
    529           1.1     ragge #endif
    530           1.1     ragge 
    531          1.39        ad 	TAILQ_FOREACH(st, &ksyms_symtabs, sd_queue) {
    532          1.39        ad 		if (st->sd_gone)
    533          1.39        ad 			continue;
    534           1.1     ragge 		if (mod && strcmp(st->sd_name, mod))
    535           1.1     ragge 			continue;
    536          1.22      cube 		if ((es = findsym(sym, st)) == NULL)
    537           1.1     ragge 			continue;
    538          1.34        ad 		if (es->st_shndx == SHN_UNDEF)
    539          1.34        ad 			continue;
    540           1.1     ragge 
    541           1.1     ragge 		/* Skip if bad binding */
    542           1.1     ragge 		if (type == KSYMS_EXTERN &&
    543           1.1     ragge 		    ELF_ST_BIND(es->st_info) != STB_GLOBAL)
    544           1.1     ragge 			continue;
    545           1.1     ragge 
    546           1.1     ragge 		if (val)
    547           1.1     ragge 			*val = es->st_value;
    548           1.1     ragge 		return 0;
    549           1.1     ragge 	}
    550           1.1     ragge 	return ENOENT;
    551           1.1     ragge }
    552           1.1     ragge 
    553          1.40  christos int
    554          1.40  christos ksyms_getval(const char *mod, const char *sym, unsigned long *val, int type)
    555          1.40  christos {
    556          1.40  christos 	int rc;
    557          1.40  christos 
    558          1.40  christos 	mutex_enter(&ksyms_lock);
    559          1.40  christos 	rc = ksyms_getval_unlocked(mod, sym, val, type);
    560          1.40  christos 	mutex_exit(&ksyms_lock);
    561          1.40  christos 	return rc;
    562          1.40  christos }
    563          1.40  christos 
    564          1.40  christos 
    565           1.1     ragge /*
    566           1.1     ragge  * Get "mod" and "symbol" associated with an address.
    567           1.1     ragge  * Returns 0 if success or ENOENT if no such entry.
    568          1.39        ad  *
    569          1.39        ad  * Call with ksyms_lock, unless known that the symbol table can't change.
    570           1.1     ragge  */
    571           1.1     ragge int
    572          1.24  christos ksyms_getname(const char **mod, const char **sym, vaddr_t v, int f)
    573           1.1     ragge {
    574          1.39        ad 	struct ksyms_symtab *st;
    575           1.1     ragge 	Elf_Sym *les, *es = NULL;
    576           1.1     ragge 	vaddr_t laddr = 0;
    577          1.15  christos 	const char *lmod = NULL;
    578          1.15  christos 	char *stable = NULL;
    579           1.1     ragge 	int type, i, sz;
    580           1.1     ragge 
    581          1.39        ad 	if (!ksyms_initted)
    582           1.1     ragge 		return ENOENT;
    583           1.1     ragge 
    584          1.39        ad 	TAILQ_FOREACH(st, &ksyms_symtabs, sd_queue) {
    585          1.39        ad 		if (st->sd_gone)
    586          1.39        ad 			continue;
    587          1.35      matt 		if (st->sd_minsym != NULL && v < st->sd_minsym->st_value)
    588          1.35      matt 			continue;
    589          1.35      matt 		if (st->sd_maxsym != NULL && v > st->sd_maxsym->st_value)
    590          1.35      matt 			continue;
    591           1.1     ragge 		sz = st->sd_symsize/sizeof(Elf_Sym);
    592           1.1     ragge 		for (i = 0; i < sz; i++) {
    593           1.1     ragge 			les = st->sd_symstart + i;
    594           1.1     ragge 			type = ELF_ST_TYPE(les->st_info);
    595           1.1     ragge 
    596           1.1     ragge 			if ((f & KSYMS_PROC) && (type != STT_FUNC))
    597           1.1     ragge 				continue;
    598           1.1     ragge 
    599           1.1     ragge 			if (type == STT_NOTYPE)
    600           1.1     ragge 				continue;
    601           1.1     ragge 
    602           1.1     ragge 			if (((f & KSYMS_ANY) == 0) &&
    603           1.1     ragge 			    (type != STT_FUNC) && (type != STT_OBJECT))
    604           1.1     ragge 				continue;
    605           1.1     ragge 
    606           1.1     ragge 			if ((les->st_value <= v) && (les->st_value > laddr)) {
    607           1.1     ragge 				laddr = les->st_value;
    608           1.1     ragge 				es = les;
    609           1.1     ragge 				lmod = st->sd_name;
    610          1.17      cube 				stable = st->sd_strstart - st->sd_usroffset;
    611           1.1     ragge 			}
    612           1.1     ragge 		}
    613           1.1     ragge 	}
    614           1.1     ragge 	if (es == NULL)
    615           1.1     ragge 		return ENOENT;
    616           1.1     ragge 	if ((f & KSYMS_EXACT) && (v != es->st_value))
    617           1.1     ragge 		return ENOENT;
    618           1.1     ragge 	if (mod)
    619           1.1     ragge 		*mod = lmod;
    620           1.1     ragge 	if (sym)
    621           1.1     ragge 		*sym = stable + es->st_name;
    622           1.1     ragge 	return 0;
    623           1.1     ragge }
    624           1.1     ragge 
    625          1.22      cube /*
    626          1.39        ad  * Temporary work structure for dynamic loaded symbol tables.
    627          1.22      cube  *
    628          1.39        ad  * XXX REMOVE WHEN LKMS GO.
    629           1.1     ragge  */
    630          1.20      matt struct syminfo {
    631          1.20      matt 	size_t cursyms;
    632          1.20      matt 	size_t curnamep;
    633          1.20      matt 	size_t maxsyms;
    634          1.20      matt 	size_t maxnamep;
    635          1.20      matt 	Elf_Sym *syms;
    636          1.20      matt 	char *symnames;
    637          1.20      matt };
    638          1.23     perry 
    639           1.1     ragge /*
    640           1.1     ragge  * Add a symbol to the temporary save area for symbols.
    641          1.39        ad  *
    642          1.39        ad  * XXX REMOVE WHEN LKMS GO.
    643           1.1     ragge  */
    644           1.1     ragge static void
    645          1.20      matt addsym(struct syminfo *info, const Elf_Sym *sym, const char *name,
    646          1.20      matt        const char *mod)
    647           1.1     ragge {
    648          1.20      matt 	int len, mlen;
    649           1.1     ragge 
    650           1.1     ragge #ifdef KSYMS_DEBUG
    651           1.1     ragge 	if (ksyms_debug & FOLLOW_MORE_CALLS)
    652           1.1     ragge 		printf("addsym: name %s val %lx\n", name, (long)sym->st_value);
    653           1.1     ragge #endif
    654          1.20      matt 	len = strlen(name) + 1;
    655          1.20      matt 	if (mod)
    656          1.20      matt 		mlen = 1 + strlen(mod);
    657          1.20      matt 	else
    658          1.20      matt 		mlen = 0;
    659          1.23     perry 	if (info->cursyms == info->maxsyms ||
    660          1.20      matt 	    (len + mlen + info->curnamep) > info->maxnamep) {
    661          1.19      matt 		printf("addsym: too many symbols, skipping '%s'\n", name);
    662           1.1     ragge 		return;
    663           1.1     ragge 	}
    664          1.20      matt 	strlcpy(&info->symnames[info->curnamep], name,
    665          1.20      matt 	    info->maxnamep - info->curnamep);
    666          1.20      matt 	if (mlen) {
    667          1.20      matt 		info->symnames[info->curnamep + len - 1] = '.';
    668          1.20      matt 		strlcpy(&info->symnames[info->curnamep + len], mod,
    669          1.20      matt 		    info->maxnamep - (info->curnamep + len));
    670          1.20      matt 		len += mlen;
    671          1.20      matt 	}
    672          1.20      matt 	info->syms[info->cursyms] = *sym;
    673          1.20      matt 	info->syms[info->cursyms].st_name = info->curnamep;
    674          1.20      matt 	info->curnamep += len;
    675           1.5     ragge 	if (len > ksyms_maxlen)
    676           1.5     ragge 		ksyms_maxlen = len;
    677          1.20      matt 	info->cursyms++;
    678           1.1     ragge }
    679          1.39        ad 
    680           1.1     ragge /*
    681          1.39        ad  * XXX REMOVE WHEN LKMS GO.
    682           1.1     ragge  */
    683          1.21      matt static int
    684          1.21      matt specialsym(const char *symname)
    685          1.21      matt {
    686          1.39        ad 
    687          1.21      matt 	return	!strcmp(symname, "_bss_start") ||
    688          1.39        ad 	    !strcmp(symname, "__bss_start") ||
    689          1.39        ad 	    !strcmp(symname, "_bss_end__") ||
    690          1.39        ad 	    !strcmp(symname, "__bss_end__") ||
    691          1.39        ad 	    !strcmp(symname, "_edata") ||
    692          1.39        ad 	    !strcmp(symname, "_end") ||
    693          1.39        ad 	    !strcmp(symname, "__end") ||
    694          1.39        ad 	    !strcmp(symname, "__end__") ||
    695          1.39        ad 	    !strncmp(symname, "__start_link_set_", 17) ||
    696          1.39        ad 	    !strncmp(symname, "__stop_link_set_", 16);
    697          1.21      matt }
    698          1.21      matt 
    699          1.39        ad /*
    700          1.39        ad  * Adds a symbol table.
    701          1.39        ad  * "name" is the module name, "start" and "size" is where the symbol table
    702          1.39        ad  * is located, and "type" is in which binary format the symbol table is.
    703          1.39        ad  * New memory for keeping the symbol table is allocated in this function.
    704          1.39        ad  * Returns 0 if success and EEXIST if the module name is in use.
    705          1.39        ad  *
    706          1.39        ad  * XXX REMOVE WHEN LKMS GO.
    707          1.39        ad  */
    708           1.1     ragge int
    709           1.9  jdolecek ksyms_addsymtab(const char *mod, void *symstart, vsize_t symsize,
    710          1.39        ad 		char *strstart, vsize_t strsize)
    711           1.1     ragge {
    712           1.1     ragge 	Elf_Sym *sym = symstart;
    713          1.39        ad 	struct ksyms_symtab *st;
    714          1.14     ragge 	unsigned long rval;
    715           1.1     ragge 	int i;
    716          1.20      matt 	char *name;
    717          1.20      matt 	struct syminfo info;
    718           1.1     ragge 
    719           1.1     ragge #ifdef KSYMS_DEBUG
    720           1.1     ragge 	if (ksyms_debug & FOLLOW_CALLS)
    721           1.1     ragge 		printf("ksyms_addsymtab: mod %s symsize %lx strsize %lx\n",
    722           1.1     ragge 		    mod, symsize, strsize);
    723           1.1     ragge #endif
    724           1.1     ragge 
    725          1.39        ad 	mutex_enter(&ksyms_lock);
    726           1.1     ragge 
    727           1.1     ragge 	/* Check if this symtab already loaded */
    728          1.39        ad 	TAILQ_FOREACH(st, &ksyms_symtabs, sd_queue) {
    729          1.39        ad 		if (st->sd_gone)
    730          1.39        ad 			continue;
    731          1.39        ad 		if (strcmp(mod, st->sd_name) == 0) {
    732          1.39        ad 			mutex_exit(&ksyms_lock);
    733           1.1     ragge 			return EEXIST;
    734          1.39        ad 		}
    735           1.1     ragge 	}
    736           1.1     ragge 
    737           1.1     ragge 	/*
    738           1.1     ragge 	 * XXX - Only add a symbol if it do not exist already.
    739           1.1     ragge 	 * This is because of a flaw in the current LKM implementation,
    740          1.20      matt 	 * these loops will be removed once the in-kernel linker is in place.
    741           1.1     ragge 	 */
    742          1.20      matt 	memset(&info, 0, sizeof(info));
    743           1.1     ragge 	for (i = 0; i < symsize/sizeof(Elf_Sym); i++) {
    744          1.20      matt 		char * const symname = strstart + sym[i].st_name;
    745           1.1     ragge 		if (sym[i].st_name == 0)
    746           1.1     ragge 			continue; /* Just ignore */
    747           1.1     ragge 
    748           1.1     ragge 		/* check validity of the symbol */
    749           1.1     ragge 		/* XXX - save local symbols if DDB */
    750           1.1     ragge 		if (ELF_ST_BIND(sym[i].st_info) != STB_GLOBAL)
    751           1.1     ragge 			continue;
    752          1.23     perry 
    753           1.1     ragge 		/* Check if the symbol exists */
    754          1.40  christos 		if (ksyms_getval_unlocked(NULL, symname, &rval, KSYMS_EXTERN)
    755          1.40  christos 		    == 0) {
    756           1.1     ragge 			/* Check (and complain) about differing values */
    757          1.34        ad 			if (sym[i].st_value != rval &&
    758          1.34        ad 			    sym[i].st_shndx != SHN_UNDEF) {
    759          1.21      matt 				if (specialsym(symname)) {
    760          1.20      matt 					info.maxsyms++;
    761          1.20      matt 					info.maxnamep += strlen(symname) + 1 +
    762          1.20      matt 					    strlen(mod) + 1;
    763          1.20      matt 				} else {
    764          1.20      matt 					printf("%s: symbol '%s' redeclared with"
    765          1.20      matt 					    " different value (%lx != %lx)\n",
    766          1.20      matt 					    mod, symname,
    767          1.20      matt 					    rval, (long)sym[i].st_value);
    768          1.20      matt 				}
    769          1.20      matt 			}
    770          1.20      matt 		} else {
    771          1.20      matt 			/*
    772          1.20      matt 			 * Count this symbol
    773          1.20      matt 			 */
    774          1.20      matt 			info.maxsyms++;
    775          1.20      matt 			info.maxnamep += strlen(symname) + 1;
    776          1.20      matt 		}
    777          1.20      matt 	}
    778          1.20      matt 
    779          1.20      matt 	/*
    780          1.20      matt 	 * Now that we know the sizes, malloc the structures.
    781          1.20      matt 	 */
    782          1.20      matt 	info.syms = malloc(sizeof(Elf_Sym)*info.maxsyms, M_DEVBUF, M_WAITOK);
    783          1.20      matt 	info.symnames = malloc(info.maxnamep, M_DEVBUF, M_WAITOK);
    784          1.20      matt 
    785          1.20      matt 	/*
    786          1.20      matt 	 * Now that we have the symbols, actually fill in the structures.
    787          1.20      matt 	 */
    788          1.20      matt 	for (i = 0; i < symsize/sizeof(Elf_Sym); i++) {
    789          1.20      matt 		char * const symname = strstart + sym[i].st_name;
    790          1.20      matt 		if (sym[i].st_name == 0)
    791          1.20      matt 			continue; /* Just ignore */
    792          1.20      matt 
    793          1.20      matt 		/* check validity of the symbol */
    794          1.20      matt 		/* XXX - save local symbols if DDB */
    795          1.20      matt 		if (ELF_ST_BIND(sym[i].st_info) != STB_GLOBAL)
    796          1.20      matt 			continue;
    797          1.23     perry 
    798          1.20      matt 		/* Check if the symbol exists */
    799          1.40  christos 		if (ksyms_getval_unlocked(NULL, symname, &rval, KSYMS_EXTERN)
    800          1.40  christos 		    == 0) {
    801          1.21      matt 			if ((sym[i].st_value != rval) && specialsym(symname)) {
    802          1.20      matt 				addsym(&info, &sym[i], symname, mod);
    803           1.1     ragge 			}
    804           1.1     ragge 		} else
    805           1.1     ragge 			/* Ok, save this symbol */
    806          1.20      matt 			addsym(&info, &sym[i], symname, NULL);
    807           1.1     ragge 	}
    808           1.5     ragge 
    809          1.39        ad 	st = kmem_zalloc(sizeof(*st), KM_SLEEP);
    810          1.10    itojun 	i = strlen(mod) + 1;
    811          1.10    itojun 	name = malloc(i, M_DEVBUF, M_WAITOK);
    812          1.10    itojun 	strlcpy(name, mod, i);
    813           1.9  jdolecek 	st->sd_name = name;
    814          1.20      matt 	st->sd_symstart = info.syms;
    815          1.20      matt 	st->sd_symsize = sizeof(Elf_Sym)*info.maxsyms;
    816          1.20      matt 	st->sd_strstart = info.symnames;
    817          1.20      matt 	st->sd_strsize = info.maxnamep;
    818          1.39        ad 	st->sd_malloc = true;
    819           1.1     ragge 
    820           1.1     ragge 	/* Make them absolute references */
    821           1.1     ragge 	sym = st->sd_symstart;
    822           1.1     ragge 	for (i = 0; i < st->sd_symsize/sizeof(Elf_Sym); i++)
    823           1.1     ragge 		sym[i].st_shndx = SHN_ABS;
    824           1.1     ragge 
    825          1.39        ad 	/* ksymsread() is unlocked, so membar. */
    826          1.39        ad 	membar_producer();
    827          1.39        ad 	TAILQ_INSERT_TAIL(&ksyms_symtabs, st, sd_queue);
    828           1.1     ragge 	ksyms_sizes_calc();
    829          1.39        ad 	mutex_exit(&ksyms_lock);
    830          1.39        ad 
    831           1.1     ragge 	return 0;
    832           1.1     ragge }
    833           1.1     ragge 
    834           1.1     ragge /*
    835           1.1     ragge  * Remove a symbol table specified by name.
    836           1.1     ragge  * Returns 0 if success, EBUSY if device open and ENOENT if no such name.
    837          1.39        ad  *
    838          1.39        ad  * XXX REMOVE WHEN LKMS GO.
    839           1.1     ragge  */
    840           1.1     ragge int
    841           1.9  jdolecek ksyms_delsymtab(const char *mod)
    842           1.1     ragge {
    843          1.39        ad 	struct ksyms_symtab *st;
    844           1.1     ragge 
    845          1.39        ad 	mutex_enter(&ksyms_lock);
    846          1.39        ad 	TAILQ_FOREACH(st, &ksyms_symtabs, sd_queue) {
    847          1.39        ad 		if (st->sd_gone)
    848          1.39        ad 			continue;
    849          1.39        ad 		if (strcmp(mod, st->sd_name) != 0)
    850          1.39        ad 			continue;
    851          1.39        ad 		if (ksyms_isopen) {
    852          1.39        ad 			st->sd_gone = true;
    853          1.39        ad 			mutex_exit(&ksyms_lock);
    854          1.39        ad 			return 0;
    855          1.39        ad 		}
    856          1.39        ad 		TAILQ_REMOVE(&ksyms_symtabs, st, sd_queue);
    857          1.39        ad 		ksyms_sizes_calc();
    858          1.39        ad 		mutex_exit(&ksyms_lock);
    859          1.39        ad 		KASSERT(st->sd_malloc);
    860          1.39        ad 		free(st->sd_symstart, M_DEVBUF);
    861          1.39        ad 		free(st->sd_strstart, M_DEVBUF);
    862          1.39        ad 		/* XXXUNCONST LINTED - const castaway */
    863          1.39        ad 		free(__UNCONST(st->sd_name), M_DEVBUF);
    864          1.39        ad 		kmem_free(st, sizeof(*st));
    865          1.39        ad 		return 0;
    866           1.1     ragge 	}
    867          1.39        ad 	mutex_exit(&ksyms_lock);
    868          1.39        ad 	return ENOENT;
    869           1.1     ragge }
    870           1.1     ragge 
    871          1.39        ad /*
    872          1.39        ad  * Add a symbol table from a loadable module.
    873          1.39        ad  */
    874          1.39        ad void
    875          1.39        ad ksyms_modload(const char *name, void *symstart, vsize_t symsize,
    876          1.39        ad 	      char *strstart, vsize_t strsize)
    877          1.17      cube {
    878          1.39        ad 	struct ksyms_symtab *st;
    879          1.39        ad 
    880          1.39        ad 	st = kmem_zalloc(sizeof(*st), KM_SLEEP);
    881          1.39        ad 	mutex_enter(&ksyms_lock);
    882          1.39        ad 	addsymtab(name, symstart, symsize, strstart, strsize, st, symstart);
    883          1.39        ad 	mutex_exit(&ksyms_lock);
    884          1.39        ad }
    885          1.17      cube 
    886          1.39        ad /*
    887          1.39        ad  * Remove a symbol table from a loadable module.
    888          1.39        ad  */
    889          1.39        ad void
    890          1.39        ad ksyms_modunload(const char *name)
    891          1.39        ad {
    892          1.39        ad 	struct ksyms_symtab *st;
    893          1.17      cube 
    894          1.39        ad 	mutex_enter(&ksyms_lock);
    895          1.39        ad 	TAILQ_FOREACH(st, &ksyms_symtabs, sd_queue) {
    896          1.39        ad 		if (st->sd_gone)
    897          1.39        ad 			continue;
    898          1.39        ad 		if (strcmp(name, st->sd_name) != 0)
    899          1.39        ad 			continue;
    900          1.39        ad 		KASSERT(!st->sd_malloc);	/* XXXLKM */
    901          1.39        ad 		st->sd_gone = true;
    902          1.39        ad 		if (!ksyms_isopen) {
    903          1.39        ad 			TAILQ_REMOVE(&ksyms_symtabs, st, sd_queue);
    904          1.39        ad 			ksyms_sizes_calc();
    905          1.39        ad 			kmem_free(st, sizeof(*st));
    906          1.39        ad 		}
    907          1.39        ad 		break;
    908          1.39        ad 	}
    909          1.39        ad 	mutex_exit(&ksyms_lock);
    910          1.39        ad 	KASSERT(st != NULL);
    911          1.17      cube }
    912          1.17      cube 
    913           1.1     ragge #ifdef DDB
    914           1.1     ragge /*
    915           1.1     ragge  * Keep sifting stuff here, to avoid export of ksyms internals.
    916          1.39        ad  *
    917          1.39        ad  * Systems is expected to be quiescent, so no locking done.
    918           1.1     ragge  */
    919           1.1     ragge int
    920           1.1     ragge ksyms_sift(char *mod, char *sym, int mode)
    921           1.1     ragge {
    922          1.39        ad 	struct ksyms_symtab *st;
    923           1.1     ragge 	char *sb;
    924           1.1     ragge 	int i, sz;
    925           1.1     ragge 
    926          1.39        ad 	if (!ksyms_initted)
    927           1.1     ragge 		return ENOENT;
    928           1.1     ragge 
    929          1.39        ad 	TAILQ_FOREACH(st, &ksyms_symtabs, sd_queue) {
    930          1.39        ad 		if (st->sd_gone)
    931          1.39        ad 			continue;
    932           1.1     ragge 		if (mod && strcmp(mod, st->sd_name))
    933           1.1     ragge 			continue;
    934          1.39        ad 		sb = st->sd_strstart - st->sd_usroffset;
    935           1.1     ragge 
    936           1.1     ragge 		sz = st->sd_symsize/sizeof(Elf_Sym);
    937           1.1     ragge 		for (i = 0; i < sz; i++) {
    938           1.1     ragge 			Elf_Sym *les = st->sd_symstart + i;
    939           1.1     ragge 			char c;
    940           1.1     ragge 
    941          1.39        ad 			if (strstr(sb + les->st_name, sym) == NULL)
    942           1.1     ragge 				continue;
    943           1.1     ragge 
    944           1.1     ragge 			if (mode == 'F') {
    945           1.1     ragge 				switch (ELF_ST_TYPE(les->st_info)) {
    946           1.1     ragge 				case STT_OBJECT:
    947           1.1     ragge 					c = '+';
    948           1.1     ragge 					break;
    949           1.1     ragge 				case STT_FUNC:
    950           1.1     ragge 					c = '*';
    951           1.1     ragge 					break;
    952           1.1     ragge 				case STT_SECTION:
    953           1.1     ragge 					c = '&';
    954           1.1     ragge 					break;
    955           1.1     ragge 				case STT_FILE:
    956           1.1     ragge 					c = '/';
    957           1.1     ragge 					break;
    958           1.1     ragge 				default:
    959           1.1     ragge 					c = ' ';
    960           1.1     ragge 					break;
    961           1.1     ragge 				}
    962          1.39        ad 				db_printf("%s%c ", sb + les->st_name, c);
    963           1.1     ragge 			} else
    964          1.39        ad 				db_printf("%s ", sb + les->st_name);
    965           1.1     ragge 		}
    966           1.1     ragge 	}
    967           1.1     ragge 	return ENOENT;
    968           1.1     ragge }
    969          1.25   thorpej #endif /* DDB */
    970           1.1     ragge 
    971           1.1     ragge /*
    972          1.39        ad  * In case we exposing the symbol table to the userland using the pseudo-
    973          1.39        ad  * device /dev/ksyms, it is easier to provide all the tables as one.
    974          1.39        ad  * However, it means we have to change all the st_name fields for the
    975          1.39        ad  * symbols so they match the ELF image that the userland will read
    976          1.39        ad  * through the device.
    977          1.39        ad  *
    978          1.39        ad  * The actual (correct) value of st_name is preserved through a global
    979          1.39        ad  * offset stored in the symbol table structure.
    980          1.39        ad  *
    981          1.39        ad  * Call with ksyms_lock held.
    982           1.1     ragge  */
    983          1.39        ad static void
    984          1.39        ad ksyms_sizes_calc(void)
    985          1.39        ad {
    986          1.39        ad         struct ksyms_symtab *st;
    987          1.39        ad 	int i, delta;
    988           1.1     ragge 
    989          1.39        ad         ksyms_symsz = ksyms_strsz = 0;
    990          1.39        ad         TAILQ_FOREACH(st, &ksyms_symtabs, sd_queue) {
    991          1.39        ad 		delta = ksyms_strsz - st->sd_usroffset;
    992          1.39        ad 		if (delta != 0) {
    993          1.39        ad 			for (i = 0; i < st->sd_symsize/sizeof(Elf_Sym); i++)
    994          1.39        ad 				st->sd_symstart[i].st_name += delta;
    995          1.39        ad 			st->sd_usroffset = ksyms_strsz;
    996          1.39        ad 		}
    997          1.39        ad                 ksyms_symsz += st->sd_symsize;
    998          1.39        ad                 ksyms_strsz += st->sd_strsize;
    999          1.39        ad         }
   1000          1.39        ad }
   1001           1.1     ragge 
   1002          1.25   thorpej static void
   1003          1.32  christos ksyms_hdr_init(void *hdraddr)
   1004           1.1     ragge {
   1005           1.1     ragge 
   1006           1.1     ragge 	/* Copy the loaded elf exec header */
   1007           1.1     ragge 	memcpy(&ksyms_hdr.kh_ehdr, hdraddr, sizeof(Elf_Ehdr));
   1008           1.1     ragge 
   1009           1.1     ragge 	/* Set correct program/section header sizes, offsets and numbers */
   1010           1.1     ragge 	ksyms_hdr.kh_ehdr.e_phoff = offsetof(struct ksyms_hdr, kh_phdr[0]);
   1011           1.1     ragge 	ksyms_hdr.kh_ehdr.e_phentsize = sizeof(Elf_Phdr);
   1012           1.1     ragge 	ksyms_hdr.kh_ehdr.e_phnum = NPRGHDR;
   1013           1.1     ragge 	ksyms_hdr.kh_ehdr.e_shoff = offsetof(struct ksyms_hdr, kh_shdr[0]);
   1014           1.1     ragge 	ksyms_hdr.kh_ehdr.e_shentsize = sizeof(Elf_Shdr);
   1015           1.1     ragge 	ksyms_hdr.kh_ehdr.e_shnum = NSECHDR;
   1016           1.1     ragge 	ksyms_hdr.kh_ehdr.e_shstrndx = NSECHDR - 1; /* Last section */
   1017           1.1     ragge 
   1018          1.39        ad 	/* Text */
   1019          1.39        ad 	ksyms_hdr.kh_phdr[0].p_type = PT_LOAD;
   1020          1.39        ad 	ksyms_hdr.kh_phdr[0].p_memsz = (unsigned long)-1L;
   1021          1.39        ad 	ksyms_hdr.kh_phdr[0].p_flags = PF_R | PF_X;
   1022          1.39        ad 
   1023          1.39        ad 	/* Data */
   1024          1.39        ad 	ksyms_hdr.kh_phdr[1].p_type = PT_LOAD;
   1025          1.39        ad 	ksyms_hdr.kh_phdr[1].p_memsz = (unsigned long)-1L;
   1026          1.39        ad 	ksyms_hdr.kh_phdr[1].p_flags = PF_R | PF_W | PF_X;
   1027          1.39        ad 
   1028          1.39        ad 	/* First section is null */
   1029           1.1     ragge 
   1030           1.1     ragge 	/* Second section header; ".symtab" */
   1031           1.1     ragge 	ksyms_hdr.kh_shdr[SYMTAB].sh_name = 1; /* Section 3 offset */
   1032           1.1     ragge 	ksyms_hdr.kh_shdr[SYMTAB].sh_type = SHT_SYMTAB;
   1033           1.1     ragge 	ksyms_hdr.kh_shdr[SYMTAB].sh_offset = sizeof(struct ksyms_hdr);
   1034           1.1     ragge /*	ksyms_hdr.kh_shdr[SYMTAB].sh_size = filled in at open */
   1035           1.1     ragge 	ksyms_hdr.kh_shdr[SYMTAB].sh_link = 2; /* Corresponding strtab */
   1036           1.1     ragge 	ksyms_hdr.kh_shdr[SYMTAB].sh_addralign = sizeof(long);
   1037           1.1     ragge 	ksyms_hdr.kh_shdr[SYMTAB].sh_entsize = sizeof(Elf_Sym);
   1038           1.1     ragge 
   1039           1.1     ragge 	/* Third section header; ".strtab" */
   1040           1.1     ragge 	ksyms_hdr.kh_shdr[STRTAB].sh_name = 9; /* Section 3 offset */
   1041           1.1     ragge 	ksyms_hdr.kh_shdr[STRTAB].sh_type = SHT_STRTAB;
   1042           1.1     ragge /*	ksyms_hdr.kh_shdr[STRTAB].sh_offset = filled in at open */
   1043           1.1     ragge /*	ksyms_hdr.kh_shdr[STRTAB].sh_size = filled in at open */
   1044           1.1     ragge 	ksyms_hdr.kh_shdr[STRTAB].sh_addralign = sizeof(char);
   1045           1.1     ragge 
   1046           1.1     ragge 	/* Fourth section, ".shstrtab" */
   1047           1.1     ragge 	ksyms_hdr.kh_shdr[SHSTRTAB].sh_name = 17; /* This section name offset */
   1048           1.1     ragge 	ksyms_hdr.kh_shdr[SHSTRTAB].sh_type = SHT_STRTAB;
   1049           1.1     ragge 	ksyms_hdr.kh_shdr[SHSTRTAB].sh_offset =
   1050           1.1     ragge 	    offsetof(struct ksyms_hdr, kh_strtab);
   1051           1.1     ragge 	ksyms_hdr.kh_shdr[SHSTRTAB].sh_size = SHSTRSIZ;
   1052           1.1     ragge 	ksyms_hdr.kh_shdr[SHSTRTAB].sh_addralign = sizeof(char);
   1053           1.1     ragge 
   1054           1.1     ragge 	/* Set section names */
   1055          1.10    itojun 	strlcpy(&ksyms_hdr.kh_strtab[1], ".symtab",
   1056          1.10    itojun 	    sizeof(ksyms_hdr.kh_strtab) - 1);
   1057          1.10    itojun 	strlcpy(&ksyms_hdr.kh_strtab[9], ".strtab",
   1058          1.10    itojun 	    sizeof(ksyms_hdr.kh_strtab) - 9);
   1059          1.10    itojun 	strlcpy(&ksyms_hdr.kh_strtab[17], ".shstrtab",
   1060          1.10    itojun 	    sizeof(ksyms_hdr.kh_strtab) - 17);
   1061          1.39        ad }
   1062           1.1     ragge 
   1063          1.25   thorpej static int
   1064          1.30      yamt ksymsopen(dev_t dev, int oflags, int devtype, struct lwp *l)
   1065           1.1     ragge {
   1066           1.1     ragge 
   1067          1.39        ad 	if (minor(dev) != 0 || !ksyms_initted)
   1068          1.18      cube 		return ENXIO;
   1069           1.1     ragge 
   1070          1.39        ad 	/*
   1071          1.39        ad 	 * Create a "snapshot" of the kernel symbol table.  Setting
   1072          1.39        ad 	 * ksyms_isopen will prevent symbol tables from being freed.
   1073          1.39        ad 	 */
   1074          1.39        ad 	mutex_enter(&ksyms_lock);
   1075          1.39        ad 	ksyms_hdr.kh_shdr[SYMTAB].sh_size = ksyms_symsz;
   1076          1.39        ad 	ksyms_hdr.kh_shdr[SYMTAB].sh_info = ksyms_symsz / sizeof(Elf_Sym);
   1077          1.39        ad 	ksyms_hdr.kh_shdr[STRTAB].sh_offset = ksyms_symsz +
   1078           1.1     ragge 	    ksyms_hdr.kh_shdr[SYMTAB].sh_offset;
   1079          1.39        ad 	ksyms_hdr.kh_shdr[STRTAB].sh_size = ksyms_strsz;
   1080          1.39        ad 	ksyms_isopen = true;
   1081          1.39        ad 	mutex_exit(&ksyms_lock);
   1082           1.1     ragge 
   1083           1.1     ragge 	return 0;
   1084           1.1     ragge }
   1085           1.1     ragge 
   1086          1.25   thorpej static int
   1087          1.30      yamt ksymsclose(dev_t dev, int oflags, int devtype, struct lwp *l)
   1088           1.1     ragge {
   1089          1.39        ad 	struct ksyms_symtab *st, *next;
   1090          1.39        ad 	bool resize;
   1091           1.1     ragge 
   1092          1.39        ad 	/* Discard refernces to symbol tables. */
   1093          1.39        ad 	mutex_enter(&ksyms_lock);
   1094          1.39        ad 	ksyms_isopen = false;
   1095          1.39        ad 	resize = false;
   1096          1.39        ad 	for (st = TAILQ_FIRST(&ksyms_symtabs); st != NULL; st = next) {
   1097          1.39        ad 		next = TAILQ_NEXT(st, sd_queue);
   1098          1.39        ad 		if (st->sd_gone) {
   1099          1.39        ad 			TAILQ_REMOVE(&ksyms_symtabs, st, sd_queue);
   1100          1.39        ad 			kmem_free(st, sizeof(*st));
   1101          1.39        ad 			if (st->sd_malloc) {	/* XXXLKM */
   1102          1.39        ad 				free(st->sd_symstart, M_DEVBUF);
   1103          1.39        ad 				free(st->sd_strstart, M_DEVBUF);
   1104          1.39        ad 				/* XXXUNCONST LINTED - const castaway */
   1105          1.39        ad 				free(__UNCONST(st->sd_name), M_DEVBUF);
   1106          1.39        ad 			}
   1107          1.39        ad 			resize = true;
   1108          1.39        ad 		}
   1109          1.39        ad 	}
   1110          1.39        ad 	if (resize)
   1111          1.39        ad 		ksyms_sizes_calc();
   1112          1.39        ad 	mutex_exit(&ksyms_lock);
   1113           1.1     ragge 
   1114           1.1     ragge 	return 0;
   1115           1.1     ragge }
   1116           1.1     ragge 
   1117          1.25   thorpej static int
   1118          1.30      yamt ksymsread(dev_t dev, struct uio *uio, int ioflag)
   1119           1.1     ragge {
   1120          1.39        ad 	struct ksyms_symtab *st;
   1121           1.1     ragge 	size_t filepos, inpos, off;
   1122          1.39        ad 	int error;
   1123           1.1     ragge 
   1124           1.1     ragge 	/*
   1125          1.39        ad 	 * First: Copy out the ELF header.   XXX Lose if ksymsopen()
   1126          1.39        ad 	 * occurs during read of the header.
   1127           1.1     ragge 	 */
   1128          1.39        ad 	off = uio->uio_offset;
   1129          1.39        ad 	if (off < sizeof(struct ksyms_hdr)) {
   1130          1.39        ad 		error = uiomove((char *)&ksyms_hdr + off,
   1131          1.39        ad 		    sizeof(struct ksyms_hdr) - off, uio);
   1132          1.39        ad 		if (error != 0)
   1133          1.39        ad 			return error;
   1134          1.39        ad 	}
   1135           1.1     ragge 
   1136           1.1     ragge 	/*
   1137           1.1     ragge 	 * Copy out the symbol table.
   1138           1.1     ragge 	 */
   1139          1.39        ad 	filepos = sizeof(struct ksyms_hdr);
   1140          1.39        ad 	TAILQ_FOREACH(st, &ksyms_symtabs, sd_queue) {
   1141           1.1     ragge 		if (uio->uio_resid == 0)
   1142           1.1     ragge 			return 0;
   1143           1.1     ragge 		if (uio->uio_offset <= st->sd_symsize + filepos) {
   1144           1.1     ragge 			inpos = uio->uio_offset - filepos;
   1145          1.39        ad 			error = uiomove((char *)st->sd_symstart + inpos,
   1146           1.1     ragge 			   st->sd_symsize - inpos, uio);
   1147          1.39        ad 			if (error != 0)
   1148          1.39        ad 				return error;
   1149           1.1     ragge 		}
   1150           1.1     ragge 		filepos += st->sd_symsize;
   1151           1.1     ragge 	}
   1152           1.1     ragge 
   1153           1.1     ragge 	/*
   1154           1.1     ragge 	 * Copy out the string table
   1155           1.1     ragge 	 */
   1156          1.39        ad 	KASSERT(filepos == sizeof(struct ksyms_hdr) +
   1157          1.39        ad 	    ksyms_hdr.kh_shdr[SYMTAB].sh_size);
   1158          1.39        ad 	TAILQ_FOREACH(st, &ksyms_symtabs, sd_queue) {
   1159           1.1     ragge 		if (uio->uio_resid == 0)
   1160           1.1     ragge 			return 0;
   1161           1.1     ragge 		if (uio->uio_offset <= st->sd_strsize + filepos) {
   1162           1.1     ragge 			inpos = uio->uio_offset - filepos;
   1163          1.39        ad 			error = uiomove((char *)st->sd_strstart + inpos,
   1164           1.1     ragge 			   st->sd_strsize - inpos, uio);
   1165          1.39        ad 			if (error != 0)
   1166          1.39        ad 				return error;
   1167           1.1     ragge 		}
   1168           1.1     ragge 		filepos += st->sd_strsize;
   1169           1.1     ragge 	}
   1170          1.39        ad 
   1171           1.1     ragge 	return 0;
   1172           1.1     ragge }
   1173           1.1     ragge 
   1174          1.25   thorpej static int
   1175          1.30      yamt ksymswrite(dev_t dev, struct uio *uio, int ioflag)
   1176           1.1     ragge {
   1177          1.30      yamt 
   1178           1.1     ragge 	return EROFS;
   1179           1.1     ragge }
   1180           1.1     ragge 
   1181          1.25   thorpej static int
   1182          1.32  christos ksymsioctl(dev_t dev, u_long cmd, void *data, int fflag, struct lwp *l)
   1183           1.1     ragge {
   1184           1.1     ragge 	struct ksyms_gsymbol *kg = (struct ksyms_gsymbol *)data;
   1185          1.39        ad 	struct ksyms_symtab *st;
   1186          1.39        ad 	Elf_Sym *sym = NULL, copy;
   1187           1.1     ragge 	unsigned long val;
   1188           1.1     ragge 	int error = 0;
   1189          1.15  christos 	char *str = NULL;
   1190          1.39        ad 	int len;
   1191          1.39        ad 
   1192          1.39        ad 	/* Read ksyms_maxlen only once while not holding the lock. */
   1193          1.39        ad 	len = ksyms_maxlen;
   1194           1.5     ragge 
   1195          1.39        ad 	if (cmd == KIOCGVALUE || cmd == KIOCGSYMBOL) {
   1196          1.39        ad 		str = kmem_alloc(len, KM_SLEEP);
   1197          1.39        ad 		if ((error = copyinstr(kg->kg_name, str, len, NULL)) != 0) {
   1198          1.39        ad 			kmem_free(str, len);
   1199          1.39        ad 			return error;
   1200          1.39        ad 		}
   1201          1.39        ad 	}
   1202           1.1     ragge 
   1203           1.1     ragge 	switch (cmd) {
   1204           1.1     ragge 	case KIOCGVALUE:
   1205           1.1     ragge 		/*
   1206           1.1     ragge 		 * Use the in-kernel symbol lookup code for fast
   1207           1.1     ragge 		 * retreival of a value.
   1208           1.1     ragge 		 */
   1209          1.39        ad 		error = ksyms_getval(NULL, str, &val, KSYMS_EXTERN);
   1210          1.39        ad 		if (error == 0)
   1211          1.39        ad 			error = copyout(&val, kg->kg_value, sizeof(long));
   1212          1.39        ad 		kmem_free(str, len);
   1213           1.1     ragge 		break;
   1214           1.1     ragge 
   1215           1.1     ragge 	case KIOCGSYMBOL:
   1216           1.1     ragge 		/*
   1217           1.1     ragge 		 * Use the in-kernel symbol lookup code for fast
   1218           1.1     ragge 		 * retreival of a symbol.
   1219           1.1     ragge 		 */
   1220          1.39        ad 		mutex_enter(&ksyms_lock);
   1221          1.39        ad 		TAILQ_FOREACH(st, &ksyms_symtabs, sd_queue) {
   1222          1.39        ad 			if (st->sd_gone)
   1223          1.39        ad 				continue;
   1224          1.39        ad 			if ((sym = findsym(str, st)) == NULL)
   1225           1.1     ragge 				continue;
   1226          1.36  christos #ifdef notdef
   1227           1.1     ragge 			/* Skip if bad binding */
   1228           1.1     ragge 			if (ELF_ST_BIND(sym->st_info) != STB_GLOBAL) {
   1229           1.1     ragge 				sym = NULL;
   1230           1.1     ragge 				continue;
   1231           1.1     ragge 			}
   1232          1.36  christos #endif
   1233           1.1     ragge 			break;
   1234           1.1     ragge 		}
   1235          1.39        ad 		if (sym != NULL) {
   1236          1.39        ad 			memcpy(&copy, sym, sizeof(copy));
   1237          1.39        ad 			mutex_exit(&ksyms_lock);
   1238          1.39        ad 			error = copyout(&copy, kg->kg_sym, sizeof(Elf_Sym));
   1239          1.39        ad 		} else {
   1240          1.39        ad 			mutex_exit(&ksyms_lock);
   1241           1.1     ragge 			error = ENOENT;
   1242          1.39        ad 		}
   1243          1.39        ad 		kmem_free(str, len);
   1244           1.1     ragge 		break;
   1245           1.1     ragge 
   1246           1.1     ragge 	case KIOCGSIZE:
   1247           1.1     ragge 		/*
   1248           1.1     ragge 		 * Get total size of symbol table.
   1249           1.1     ragge 		 */
   1250          1.39        ad 		mutex_enter(&ksyms_lock);
   1251          1.39        ad 		*(int *)data = ksyms_strsz + ksyms_symsz +
   1252          1.39        ad 		    sizeof(struct ksyms_hdr);
   1253          1.39        ad 		mutex_exit(&ksyms_lock);
   1254           1.1     ragge 		break;
   1255           1.1     ragge 
   1256           1.1     ragge 	default:
   1257           1.1     ragge 		error = ENOTTY;
   1258           1.1     ragge 		break;
   1259           1.1     ragge 	}
   1260           1.5     ragge 
   1261           1.5     ragge 	return error;
   1262           1.1     ragge }
   1263          1.25   thorpej 
   1264          1.25   thorpej const struct cdevsw ksyms_cdevsw = {
   1265          1.25   thorpej 	ksymsopen, ksymsclose, ksymsread, ksymswrite, ksymsioctl,
   1266          1.39        ad 	nullstop, notty, nopoll, nommap, nullkqfilter, D_OTHER | D_MPSAFE
   1267          1.25   thorpej };
   1268