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