2008-10-20 14:24:18 +04:00
|
|
|
/* $NetBSD: kern_ksyms.c,v 1.39 2008/10/20 10:24:18 ad Exp $ */
|
|
|
|
|
|
|
|
/*-
|
|
|
|
* Copyright (c) 2008 The NetBSD Foundation, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software developed for The NetBSD Foundation
|
|
|
|
* by Andrew Doran.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
|
|
|
|
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
|
|
|
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
|
|
|
|
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
2008-10-11 03:09:19 +04:00
|
|
|
|
2003-04-24 23:56:43 +04:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2001, 2003 Anders Magnusson (ragge@ludd.luth.se).
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. The name of the author may not be used to endorse or promote products
|
|
|
|
* derived from this software without specific prior written permission
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Code to deal with in-kernel symbol table management + /dev/ksyms.
|
|
|
|
*
|
|
|
|
* For each loaded module the symbol table info is kept track of by a
|
|
|
|
* struct, placed in a circular list. The first entry is the kernel
|
|
|
|
* symbol table.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* TODO:
|
|
|
|
*
|
2008-10-20 14:24:18 +04:00
|
|
|
* Consider replacing patricia tree with simpler binary search
|
|
|
|
* for symbol tables.
|
|
|
|
*
|
|
|
|
* Add support for mmap, poll.
|
2003-04-24 23:56:43 +04:00
|
|
|
*/
|
|
|
|
|
2003-06-01 04:03:39 +04:00
|
|
|
#include <sys/cdefs.h>
|
2008-10-20 14:24:18 +04:00
|
|
|
__KERNEL_RCSID(0, "$NetBSD: kern_ksyms.c,v 1.39 2008/10/20 10:24:18 ad Exp $");
|
2003-06-01 04:03:39 +04:00
|
|
|
|
2003-04-24 23:56:43 +04:00
|
|
|
#ifdef _KERNEL
|
|
|
|
#include "opt_ddb.h"
|
2003-04-26 00:30:57 +04:00
|
|
|
#include "opt_ddbparam.h" /* for SYMTAB_SPACE */
|
2003-04-24 23:56:43 +04:00
|
|
|
#endif
|
|
|
|
|
2008-10-20 14:24:18 +04:00
|
|
|
#define _KSYMS_PRIVATE
|
|
|
|
|
2003-04-24 23:56:43 +04:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/errno.h>
|
|
|
|
#include <sys/queue.h>
|
|
|
|
#include <sys/exec.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/conf.h>
|
|
|
|
#include <sys/malloc.h>
|
2008-10-20 14:24:18 +04:00
|
|
|
#include <sys/kmem.h>
|
2003-04-24 23:56:43 +04:00
|
|
|
#include <sys/proc.h>
|
2008-10-20 14:24:18 +04:00
|
|
|
#include <sys/module.h>
|
|
|
|
#include <sys/atomic.h>
|
2003-04-24 23:56:43 +04:00
|
|
|
#include <sys/ksyms.h>
|
|
|
|
|
|
|
|
#include <lib/libkern/libkern.h>
|
|
|
|
|
|
|
|
#ifdef DDB
|
|
|
|
#include <ddb/db_output.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "ksyms.h"
|
|
|
|
|
2008-10-20 14:24:18 +04:00
|
|
|
static int ksyms_maxlen;
|
|
|
|
static bool ksyms_isopen;
|
|
|
|
static bool ksyms_initted;
|
|
|
|
static struct ksyms_hdr ksyms_hdr;
|
|
|
|
kmutex_t ksyms_lock;
|
2003-04-24 23:56:43 +04:00
|
|
|
|
2008-10-20 14:24:18 +04:00
|
|
|
void ksymsattach(int);
|
2007-03-04 08:59:00 +03:00
|
|
|
static void ksyms_hdr_init(void *hdraddr);
|
2003-04-24 23:56:43 +04:00
|
|
|
static void ksyms_sizes_calc(void);
|
|
|
|
|
|
|
|
#ifdef KSYMS_DEBUG
|
|
|
|
#define FOLLOW_CALLS 1
|
|
|
|
#define FOLLOW_MORE_CALLS 2
|
|
|
|
#define FOLLOW_DEVKSYMS 4
|
|
|
|
static int ksyms_debug;
|
|
|
|
#endif
|
|
|
|
|
2003-04-26 00:30:57 +04:00
|
|
|
#ifdef SYMTAB_SPACE
|
|
|
|
#define SYMTAB_FILLER "|This is the symbol table!"
|
|
|
|
|
|
|
|
char db_symtab[SYMTAB_SPACE] = SYMTAB_FILLER;
|
|
|
|
int db_symtabsize = SYMTAB_SPACE;
|
|
|
|
#endif
|
2003-04-24 23:56:43 +04:00
|
|
|
|
2008-10-20 14:24:18 +04:00
|
|
|
int ksyms_symsz;
|
|
|
|
int ksyms_strsz;
|
|
|
|
TAILQ_HEAD(, ksyms_symtab) ksyms_symtabs =
|
|
|
|
TAILQ_HEAD_INITIALIZER(ksyms_symtabs);
|
|
|
|
static struct ksyms_symtab kernel_symtab;
|
2003-04-24 23:56:43 +04:00
|
|
|
|
2003-05-08 01:28:16 +04:00
|
|
|
/*
|
|
|
|
* Patricia-tree-based lookup structure for the in-kernel global symbols.
|
|
|
|
* Based on a design by Mikael Sundstrom, msm@sm.luth.se.
|
|
|
|
*/
|
|
|
|
struct ptree {
|
|
|
|
int16_t bitno;
|
|
|
|
int16_t lr[2];
|
|
|
|
} *symb;
|
|
|
|
static int16_t baseidx;
|
|
|
|
static int treex = 1;
|
|
|
|
|
|
|
|
#define P_BIT(key, bit) ((key[bit >> 3] >> (bit & 7)) & 1)
|
2007-03-04 08:59:00 +03:00
|
|
|
#define STRING(idx) (kernel_symtab.sd_symstart[idx].st_name + \
|
|
|
|
kernel_symtab.sd_strstart)
|
2003-05-08 01:28:16 +04:00
|
|
|
|
2007-04-02 20:44:44 +04:00
|
|
|
static int
|
|
|
|
ksyms_verify(void *symstart, void *strstart)
|
|
|
|
{
|
|
|
|
#if defined(DIAGNOSTIC) || defined(DEBUG)
|
|
|
|
if (symstart == NULL)
|
|
|
|
printf("ksyms: Symbol table not found\n");
|
|
|
|
if (strstart == NULL)
|
|
|
|
printf("ksyms: String table not found\n");
|
|
|
|
if (symstart == NULL || strstart == NULL)
|
|
|
|
printf("ksyms: Perhaps the kernel is stripped?\n");
|
|
|
|
#endif
|
|
|
|
if (symstart == NULL || strstart == NULL)
|
|
|
|
return 0;
|
|
|
|
KASSERT(symstart <= strstart);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2003-05-08 01:28:16 +04:00
|
|
|
/*
|
|
|
|
* Walk down the tree until a terminal node is found.
|
|
|
|
*/
|
|
|
|
static int
|
2005-05-30 02:24:14 +04:00
|
|
|
symbol_traverse(const char *key)
|
2003-05-08 01:28:16 +04:00
|
|
|
{
|
|
|
|
int16_t nb, rbit = baseidx;
|
|
|
|
|
|
|
|
while (rbit > 0) {
|
|
|
|
nb = symb[rbit].bitno;
|
|
|
|
rbit = symb[rbit].lr[P_BIT(key, nb)];
|
|
|
|
}
|
|
|
|
return -rbit;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
ptree_add(char *key, int val)
|
|
|
|
{
|
|
|
|
int idx;
|
2003-10-25 13:03:59 +04:00
|
|
|
int nix, cix, bit, rbit, sb, lastrbit, svbit = 0, ix;
|
2003-05-08 01:28:16 +04:00
|
|
|
char *m, *k;
|
|
|
|
|
|
|
|
if (baseidx == 0) {
|
|
|
|
baseidx = -val;
|
|
|
|
return 0; /* First element */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get string to match against */
|
|
|
|
idx = symbol_traverse(key);
|
|
|
|
|
|
|
|
/* Find first mismatching bit */
|
|
|
|
m = STRING(idx);
|
|
|
|
k = key;
|
|
|
|
if (strcmp(m, k) == 0)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
for (cix = 0; *m && *k && *m == *k; m++, k++, cix += 8)
|
|
|
|
;
|
|
|
|
ix = ffs((int)*m ^ (int)*k) - 1;
|
|
|
|
cix += ix;
|
|
|
|
|
|
|
|
/* Create new node */
|
|
|
|
nix = treex++;
|
|
|
|
bit = P_BIT(key, cix);
|
|
|
|
symb[nix].bitno = cix;
|
|
|
|
symb[nix].lr[bit] = -val;
|
|
|
|
|
|
|
|
/* Find where to insert node */
|
|
|
|
rbit = baseidx;
|
|
|
|
lastrbit = 0;
|
|
|
|
for (;;) {
|
|
|
|
if (rbit < 0)
|
|
|
|
break;
|
|
|
|
sb = symb[rbit].bitno;
|
|
|
|
if (sb > cix)
|
|
|
|
break;
|
|
|
|
if (sb == cix)
|
|
|
|
printf("symb[rbit].bitno == cix!!!\n");
|
|
|
|
lastrbit = rbit;
|
|
|
|
svbit = P_BIT(key, sb);
|
|
|
|
rbit = symb[rbit].lr[svbit];
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Do the actual insertion */
|
|
|
|
if (lastrbit == 0) {
|
|
|
|
/* first element */
|
|
|
|
symb[nix].lr[!bit] = baseidx;
|
|
|
|
baseidx = nix;
|
|
|
|
} else {
|
|
|
|
symb[nix].lr[!bit] = rbit;
|
|
|
|
symb[lastrbit].lr[svbit] = nix;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2005-05-30 02:24:14 +04:00
|
|
|
ptree_find(const char *key)
|
2003-05-08 01:28:16 +04:00
|
|
|
{
|
|
|
|
int idx;
|
|
|
|
|
|
|
|
if (baseidx == 0)
|
|
|
|
return 0;
|
|
|
|
idx = symbol_traverse(key);
|
|
|
|
|
|
|
|
if (strcmp(key, STRING(idx)) == 0)
|
|
|
|
return idx;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2008-10-20 14:24:18 +04:00
|
|
|
ptree_gen(char *off, struct ksyms_symtab *tab)
|
2003-05-08 01:28:16 +04:00
|
|
|
{
|
|
|
|
Elf_Sym *sym;
|
2003-11-06 21:22:01 +03:00
|
|
|
int i, nsym;
|
2003-05-08 01:28:16 +04:00
|
|
|
|
|
|
|
if (off != NULL)
|
|
|
|
symb = (struct ptree *)ALIGN(off);
|
|
|
|
else
|
|
|
|
symb = malloc((tab->sd_symsize/sizeof(Elf_Sym)) *
|
|
|
|
sizeof(struct ptree), M_DEVBUF, M_WAITOK);
|
|
|
|
symb--; /* sym index won't be 0 */
|
|
|
|
|
|
|
|
sym = tab->sd_symstart;
|
2003-11-06 21:22:01 +03:00
|
|
|
if ((nsym = tab->sd_symsize/sizeof(Elf_Sym)) > INT16_MAX) {
|
|
|
|
printf("Too many symbols for tree, skipping %d symbols\n",
|
|
|
|
nsym-INT16_MAX);
|
|
|
|
nsym = INT16_MAX;
|
|
|
|
}
|
|
|
|
for (i = 1; i < nsym; i++) {
|
2003-05-08 01:28:16 +04:00
|
|
|
if (ELF_ST_BIND(sym[i].st_info) != STB_GLOBAL)
|
|
|
|
continue;
|
|
|
|
ptree_add(tab->sd_strstart+sym[i].st_name, i);
|
2008-10-20 14:24:18 +04:00
|
|
|
if (tab->sd_minsym == NULL ||
|
|
|
|
sym[i].st_value < tab->sd_minsym->st_value)
|
2008-02-20 05:30:51 +03:00
|
|
|
tab->sd_minsym = &sym[i];
|
2008-10-20 14:24:18 +04:00
|
|
|
if (tab->sd_maxsym == NULL ||
|
|
|
|
sym[i].st_value > tab->sd_maxsym->st_value)
|
2008-02-20 05:30:51 +03:00
|
|
|
tab->sd_maxsym = &sym[i];
|
2003-05-08 01:28:16 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-04-24 23:56:43 +04:00
|
|
|
/*
|
|
|
|
* Finds a certain symbol name in a certain symbol table.
|
|
|
|
*/
|
|
|
|
static Elf_Sym *
|
2008-10-20 14:24:18 +04:00
|
|
|
findsym(const char *name, struct ksyms_symtab *table)
|
2003-04-24 23:56:43 +04:00
|
|
|
{
|
|
|
|
Elf_Sym *start = table->sd_symstart;
|
|
|
|
int i, sz = table->sd_symsize/sizeof(Elf_Sym);
|
|
|
|
char *np;
|
2007-03-04 08:59:00 +03:00
|
|
|
char *realstart = table->sd_strstart - table->sd_usroffset;
|
2003-04-24 23:56:43 +04:00
|
|
|
|
2003-05-08 01:28:16 +04:00
|
|
|
if (table == &kernel_symtab && (i = ptree_find(name)) != 0)
|
|
|
|
return &start[i];
|
|
|
|
|
2003-04-24 23:56:43 +04:00
|
|
|
for (i = 0; i < sz; i++) {
|
2003-11-17 13:16:18 +03:00
|
|
|
np = realstart + start[i].st_name;
|
2003-04-24 23:56:43 +04:00
|
|
|
if (name[0] == np[0] && name[1] == np[1] &&
|
|
|
|
strcmp(name, np) == 0)
|
|
|
|
return &start[i];
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The "attach" is in reality done in ksyms_init().
|
|
|
|
*/
|
|
|
|
void
|
2006-11-01 13:17:58 +03:00
|
|
|
ksymsattach(int arg)
|
2003-04-24 23:56:43 +04:00
|
|
|
{
|
2003-05-08 01:28:16 +04:00
|
|
|
|
|
|
|
if (baseidx == 0)
|
|
|
|
ptree_gen(0, &kernel_symtab);
|
2003-04-24 23:56:43 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2006-10-25 17:46:36 +04:00
|
|
|
* Add a symbol table.
|
|
|
|
* This is intended for use when the symbol table and its corresponding
|
|
|
|
* string table are easily available. If they are embedded in an ELF
|
|
|
|
* image, use addsymtab_elf() instead.
|
|
|
|
*
|
|
|
|
* name - Symbol's table name.
|
|
|
|
* symstart, symsize - Address and size of the symbol table.
|
|
|
|
* strstart, strsize - Address and size of the string table.
|
|
|
|
* tab - Symbol table to be updated with this information.
|
|
|
|
* newstart - Address to which the symbol table has to be copied during
|
|
|
|
* shrinking. If NULL, it is not moved.
|
2003-04-24 23:56:43 +04:00
|
|
|
*/
|
|
|
|
static void
|
2008-10-20 14:24:18 +04:00
|
|
|
addsymtab(const char *name, void *symstart, size_t symsize,
|
|
|
|
void *strstart, size_t strsize, struct ksyms_symtab *tab,
|
|
|
|
void *newstart)
|
2003-04-24 23:56:43 +04:00
|
|
|
{
|
2003-05-08 01:28:16 +04:00
|
|
|
Elf_Sym *sym, *nsym;
|
2008-10-20 14:24:18 +04:00
|
|
|
int i, j, n;
|
2003-05-08 01:28:16 +04:00
|
|
|
char *str;
|
2003-04-24 23:56:43 +04:00
|
|
|
|
2008-10-20 14:24:18 +04:00
|
|
|
tab->sd_symstart = symstart;
|
2006-10-25 17:46:36 +04:00
|
|
|
tab->sd_symsize = symsize;
|
|
|
|
tab->sd_strstart = strstart;
|
|
|
|
tab->sd_strsize = strsize;
|
2003-04-24 23:56:43 +04:00
|
|
|
tab->sd_name = name;
|
2008-10-20 14:24:18 +04:00
|
|
|
tab->sd_minsym = NULL;
|
|
|
|
tab->sd_maxsym = NULL;
|
|
|
|
tab->sd_usroffset = 0;
|
|
|
|
tab->sd_gone = false;
|
|
|
|
tab->sd_malloc = false; /* XXXLKM */
|
2003-05-08 01:28:16 +04:00
|
|
|
#ifdef KSYMS_DEBUG
|
2008-10-20 14:24:18 +04:00
|
|
|
printf("newstart %p sym %p ksyms_symsz %d str %p strsz %d send %p\n",
|
|
|
|
newstart, symstart, symsize, strstart, strsize,
|
|
|
|
tab->sd_strstart + tab->sd_strsize);
|
2003-05-08 01:28:16 +04:00
|
|
|
#endif
|
2003-04-24 23:56:43 +04:00
|
|
|
|
2008-10-20 14:24:18 +04:00
|
|
|
/* Pack symbol table by removing all file name references. */
|
2003-05-08 01:28:16 +04:00
|
|
|
sym = tab->sd_symstart;
|
2006-10-25 17:46:36 +04:00
|
|
|
nsym = (Elf_Sym *)newstart;
|
2003-05-08 01:28:16 +04:00
|
|
|
str = tab->sd_strstart;
|
2008-10-20 14:24:18 +04:00
|
|
|
for (i = n = 0; i < tab->sd_symsize/sizeof(Elf_Sym); i++) {
|
2003-05-08 01:28:16 +04:00
|
|
|
/*
|
|
|
|
* Remove useless symbols.
|
|
|
|
* Should actually remove all typeless symbols.
|
|
|
|
*/
|
2003-05-02 00:46:20 +04:00
|
|
|
if (sym[i].st_name == 0)
|
2003-05-08 01:28:16 +04:00
|
|
|
continue; /* Skip nameless entries */
|
2008-01-04 14:28:13 +03:00
|
|
|
if (sym[i].st_shndx == SHN_UNDEF)
|
|
|
|
continue; /* Skip external references */
|
2003-05-08 01:28:16 +04:00
|
|
|
if (ELF_ST_TYPE(sym[i].st_info) == STT_FILE)
|
|
|
|
continue; /* Skip filenames */
|
|
|
|
if (ELF_ST_TYPE(sym[i].st_info) == STT_NOTYPE &&
|
|
|
|
sym[i].st_value == 0 &&
|
|
|
|
strcmp(str + sym[i].st_name, "*ABS*") == 0)
|
|
|
|
continue; /* XXX */
|
|
|
|
if (ELF_ST_TYPE(sym[i].st_info) == STT_NOTYPE &&
|
|
|
|
strcmp(str + sym[i].st_name, "gcc2_compiled.") == 0)
|
|
|
|
continue; /* XXX */
|
|
|
|
|
|
|
|
/* Save symbol. Set it as an absolute offset */
|
|
|
|
nsym[n] = sym[i];
|
|
|
|
nsym[n].st_shndx = SHN_ABS;
|
2008-10-20 14:24:18 +04:00
|
|
|
j = strlen(nsym[n].st_name + tab->sd_strstart) + 1;
|
|
|
|
if (j > ksyms_maxlen)
|
|
|
|
ksyms_maxlen = j;
|
2003-05-08 01:28:16 +04:00
|
|
|
n++;
|
|
|
|
|
|
|
|
}
|
|
|
|
tab->sd_symstart = nsym;
|
|
|
|
tab->sd_symsize = n * sizeof(Elf_Sym);
|
2008-10-20 14:24:18 +04:00
|
|
|
/* ksymsread() is unlocked, so membar. */
|
|
|
|
membar_producer();
|
|
|
|
TAILQ_INSERT_TAIL(&ksyms_symtabs, tab, sd_queue);
|
|
|
|
ksyms_sizes_calc();
|
|
|
|
ksyms_initted = true;
|
2006-10-25 17:46:36 +04:00
|
|
|
}
|
|
|
|
|
2003-04-24 23:56:43 +04:00
|
|
|
/*
|
|
|
|
* Setup the kernel symbol table stuff.
|
|
|
|
*/
|
|
|
|
void
|
2006-11-01 13:17:58 +03:00
|
|
|
ksyms_init(int symsize, void *start, void *end)
|
2003-04-24 23:56:43 +04:00
|
|
|
{
|
2008-10-20 14:24:18 +04:00
|
|
|
int i, j;
|
|
|
|
Elf_Shdr *shdr;
|
|
|
|
char *symstart = NULL, *strstart = NULL;
|
|
|
|
size_t strsize = 0;
|
2003-04-26 00:30:57 +04:00
|
|
|
Elf_Ehdr *ehdr;
|
|
|
|
|
|
|
|
#ifdef SYMTAB_SPACE
|
|
|
|
if (symsize <= 0 &&
|
|
|
|
strncmp(db_symtab, SYMTAB_FILLER, sizeof(SYMTAB_FILLER))) {
|
|
|
|
symsize = db_symtabsize;
|
|
|
|
start = db_symtab;
|
|
|
|
end = db_symtab + db_symtabsize;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
if (symsize <= 0) {
|
|
|
|
printf("[ Kernel symbol table missing! ]\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Sanity check */
|
|
|
|
if (ALIGNED_POINTER(start, long) == 0) {
|
|
|
|
printf("[ Kernel symbol table has bad start address %p ]\n",
|
|
|
|
start);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ehdr = (Elf_Ehdr *)start;
|
2003-04-24 23:56:43 +04:00
|
|
|
|
|
|
|
/* check if this is a valid ELF header */
|
|
|
|
/* No reason to verify arch type, the kernel is actually running! */
|
|
|
|
if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG) ||
|
|
|
|
ehdr->e_ident[EI_CLASS] != ELFCLASS ||
|
|
|
|
ehdr->e_version > 1) {
|
2003-04-26 00:30:57 +04:00
|
|
|
printf("[ Kernel symbol table invalid! ]\n");
|
2003-04-24 23:56:43 +04:00
|
|
|
return; /* nothing to do */
|
|
|
|
}
|
|
|
|
|
2003-05-08 01:28:16 +04:00
|
|
|
/* Loaded header will be scratched in addsymtab */
|
|
|
|
ksyms_hdr_init(start);
|
|
|
|
|
2008-10-20 14:24:18 +04:00
|
|
|
/* Find the symbol table and the corresponding string table. */
|
|
|
|
shdr = (Elf_Shdr *)((uint8_t *)start + ehdr->e_shoff);
|
|
|
|
for (i = 1; i < ehdr->e_shnum; i++) {
|
|
|
|
if (shdr[i].sh_type != SHT_SYMTAB)
|
|
|
|
continue;
|
|
|
|
if (shdr[i].sh_offset == 0)
|
|
|
|
continue;
|
|
|
|
symstart = (uint8_t *)start + shdr[i].sh_offset;
|
|
|
|
symsize = shdr[i].sh_size;
|
|
|
|
j = shdr[i].sh_link;
|
|
|
|
if (shdr[j].sh_offset == 0)
|
|
|
|
continue; /* Can this happen? */
|
|
|
|
strstart = (uint8_t *)start + shdr[j].sh_offset;
|
|
|
|
strsize = shdr[j].sh_size;
|
|
|
|
break;
|
|
|
|
}
|
2003-05-08 01:28:16 +04:00
|
|
|
|
2008-10-20 14:24:18 +04:00
|
|
|
if (!ksyms_verify(symstart, strstart))
|
|
|
|
return;
|
|
|
|
addsymtab("netbsd", symstart, symsize, strstart, strsize,
|
|
|
|
&kernel_symtab, start);
|
2003-05-08 01:28:16 +04:00
|
|
|
|
2003-04-24 23:56:43 +04:00
|
|
|
#ifdef DEBUG
|
|
|
|
printf("Loaded initial symtab at %p, strtab at %p, # entries %ld\n",
|
|
|
|
kernel_symtab.sd_symstart, kernel_symtab.sd_strstart,
|
2003-04-25 11:35:21 +04:00
|
|
|
(long)kernel_symtab.sd_symsize/sizeof(Elf_Sym));
|
2003-04-24 23:56:43 +04:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2006-10-25 17:46:36 +04:00
|
|
|
/*
|
|
|
|
* Setup the kernel symbol table stuff.
|
|
|
|
* Use this when the address of the symbol and string tables are known;
|
|
|
|
* otherwise use ksyms_init with an ELF image.
|
2006-11-06 16:35:35 +03:00
|
|
|
* We need to pass a minimal ELF header which will later be completed by
|
|
|
|
* ksyms_hdr_init and handed off to userland through /dev/ksyms. We use
|
2007-03-04 08:59:00 +03:00
|
|
|
* a void *rather than a pointer to avoid exposing the Elf_Ehdr type.
|
2006-10-25 17:46:36 +04:00
|
|
|
*/
|
|
|
|
void
|
2007-03-04 08:59:00 +03:00
|
|
|
ksyms_init_explicit(void *ehdr, void *symstart, size_t symsize,
|
2008-10-20 14:24:18 +04:00
|
|
|
void *strstart, size_t strsize)
|
2006-10-25 17:46:36 +04:00
|
|
|
{
|
|
|
|
|
2007-04-02 20:44:44 +04:00
|
|
|
if (!ksyms_verify(symstart, strstart))
|
|
|
|
return;
|
2006-10-25 17:46:36 +04:00
|
|
|
|
2006-11-06 16:35:35 +03:00
|
|
|
ksyms_hdr_init(ehdr);
|
2006-10-25 17:46:36 +04:00
|
|
|
addsymtab("netbsd", symstart, symsize, strstart, strsize,
|
2008-10-20 14:24:18 +04:00
|
|
|
&kernel_symtab, symstart);
|
2006-10-25 17:46:36 +04:00
|
|
|
}
|
|
|
|
|
2003-04-24 23:56:43 +04:00
|
|
|
/*
|
|
|
|
* Get the value associated with a symbol.
|
2005-02-27 00:34:55 +03:00
|
|
|
* "mod" is the module name, or null if any module.
|
2003-04-24 23:56:43 +04:00
|
|
|
* "sym" is the symbol name.
|
|
|
|
* "val" is a pointer to the corresponding value, if call succeeded.
|
|
|
|
* Returns 0 if success or ENOENT if no such entry.
|
2008-10-20 14:24:18 +04:00
|
|
|
*
|
|
|
|
* Call with ksyms_lock, unless known that the symbol table can't change.
|
2003-04-24 23:56:43 +04:00
|
|
|
*/
|
|
|
|
int
|
2005-05-30 02:24:14 +04:00
|
|
|
ksyms_getval(const char *mod, const char *sym, unsigned long *val, int type)
|
2003-04-24 23:56:43 +04:00
|
|
|
{
|
2008-10-20 14:24:18 +04:00
|
|
|
struct ksyms_symtab *st;
|
2003-04-24 23:56:43 +04:00
|
|
|
Elf_Sym *es;
|
|
|
|
|
2008-10-20 14:24:18 +04:00
|
|
|
if (!ksyms_initted)
|
2003-04-24 23:56:43 +04:00
|
|
|
return ENOENT;
|
|
|
|
|
|
|
|
#ifdef KSYMS_DEBUG
|
|
|
|
if (ksyms_debug & FOLLOW_CALLS)
|
|
|
|
printf("ksyms_getval: mod %s sym %s valp %p\n", mod, sym, val);
|
|
|
|
#endif
|
|
|
|
|
2008-10-20 14:24:18 +04:00
|
|
|
TAILQ_FOREACH(st, &ksyms_symtabs, sd_queue) {
|
|
|
|
if (st->sd_gone)
|
|
|
|
continue;
|
2003-04-24 23:56:43 +04:00
|
|
|
if (mod && strcmp(st->sd_name, mod))
|
|
|
|
continue;
|
2005-02-16 00:09:57 +03:00
|
|
|
if ((es = findsym(sym, st)) == NULL)
|
2003-04-24 23:56:43 +04:00
|
|
|
continue;
|
2008-01-04 14:28:13 +03:00
|
|
|
if (es->st_shndx == SHN_UNDEF)
|
|
|
|
continue;
|
2003-04-24 23:56:43 +04:00
|
|
|
|
|
|
|
/* Skip if bad binding */
|
|
|
|
if (type == KSYMS_EXTERN &&
|
|
|
|
ELF_ST_BIND(es->st_info) != STB_GLOBAL)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (val)
|
|
|
|
*val = es->st_value;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return ENOENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get "mod" and "symbol" associated with an address.
|
|
|
|
* Returns 0 if success or ENOENT if no such entry.
|
2008-10-20 14:24:18 +04:00
|
|
|
*
|
|
|
|
* Call with ksyms_lock, unless known that the symbol table can't change.
|
2003-04-24 23:56:43 +04:00
|
|
|
*/
|
|
|
|
int
|
2005-05-30 02:24:14 +04:00
|
|
|
ksyms_getname(const char **mod, const char **sym, vaddr_t v, int f)
|
2003-04-24 23:56:43 +04:00
|
|
|
{
|
2008-10-20 14:24:18 +04:00
|
|
|
struct ksyms_symtab *st;
|
2003-04-24 23:56:43 +04:00
|
|
|
Elf_Sym *les, *es = NULL;
|
|
|
|
vaddr_t laddr = 0;
|
2003-10-25 13:03:59 +04:00
|
|
|
const char *lmod = NULL;
|
|
|
|
char *stable = NULL;
|
2003-04-24 23:56:43 +04:00
|
|
|
int type, i, sz;
|
|
|
|
|
2008-10-20 14:24:18 +04:00
|
|
|
if (!ksyms_initted)
|
2003-04-24 23:56:43 +04:00
|
|
|
return ENOENT;
|
|
|
|
|
2008-10-20 14:24:18 +04:00
|
|
|
TAILQ_FOREACH(st, &ksyms_symtabs, sd_queue) {
|
|
|
|
if (st->sd_gone)
|
|
|
|
continue;
|
2008-02-20 05:30:51 +03:00
|
|
|
if (st->sd_minsym != NULL && v < st->sd_minsym->st_value)
|
|
|
|
continue;
|
|
|
|
if (st->sd_maxsym != NULL && v > st->sd_maxsym->st_value)
|
|
|
|
continue;
|
2003-04-24 23:56:43 +04:00
|
|
|
sz = st->sd_symsize/sizeof(Elf_Sym);
|
|
|
|
for (i = 0; i < sz; i++) {
|
|
|
|
les = st->sd_symstart + i;
|
|
|
|
type = ELF_ST_TYPE(les->st_info);
|
|
|
|
|
|
|
|
if ((f & KSYMS_PROC) && (type != STT_FUNC))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (type == STT_NOTYPE)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (((f & KSYMS_ANY) == 0) &&
|
|
|
|
(type != STT_FUNC) && (type != STT_OBJECT))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if ((les->st_value <= v) && (les->st_value > laddr)) {
|
|
|
|
laddr = les->st_value;
|
|
|
|
es = les;
|
|
|
|
lmod = st->sd_name;
|
2003-11-17 13:16:18 +03:00
|
|
|
stable = st->sd_strstart - st->sd_usroffset;
|
2003-04-24 23:56:43 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (es == NULL)
|
|
|
|
return ENOENT;
|
|
|
|
if ((f & KSYMS_EXACT) && (v != es->st_value))
|
|
|
|
return ENOENT;
|
|
|
|
if (mod)
|
|
|
|
*mod = lmod;
|
|
|
|
if (sym)
|
|
|
|
*sym = stable + es->st_name;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2004-02-19 02:44:49 +03:00
|
|
|
* Temporary work structure for dynamic loaded symbol tables.
|
2008-10-20 14:24:18 +04:00
|
|
|
*
|
|
|
|
* XXX REMOVE WHEN LKMS GO.
|
2003-04-24 23:56:43 +04:00
|
|
|
*/
|
2004-02-19 02:44:49 +03:00
|
|
|
struct syminfo {
|
|
|
|
size_t cursyms;
|
|
|
|
size_t curnamep;
|
|
|
|
size_t maxsyms;
|
|
|
|
size_t maxnamep;
|
|
|
|
Elf_Sym *syms;
|
|
|
|
char *symnames;
|
|
|
|
};
|
2005-02-27 00:34:55 +03:00
|
|
|
|
2003-04-24 23:56:43 +04:00
|
|
|
/*
|
|
|
|
* Add a symbol to the temporary save area for symbols.
|
2008-10-20 14:24:18 +04:00
|
|
|
*
|
|
|
|
* XXX REMOVE WHEN LKMS GO.
|
2003-04-24 23:56:43 +04:00
|
|
|
*/
|
|
|
|
static void
|
2004-02-19 02:44:49 +03:00
|
|
|
addsym(struct syminfo *info, const Elf_Sym *sym, const char *name,
|
|
|
|
const char *mod)
|
2003-04-24 23:56:43 +04:00
|
|
|
{
|
2004-02-19 02:44:49 +03:00
|
|
|
int len, mlen;
|
2003-04-24 23:56:43 +04:00
|
|
|
|
|
|
|
#ifdef KSYMS_DEBUG
|
|
|
|
if (ksyms_debug & FOLLOW_MORE_CALLS)
|
|
|
|
printf("addsym: name %s val %lx\n", name, (long)sym->st_value);
|
|
|
|
#endif
|
2004-02-19 02:44:49 +03:00
|
|
|
len = strlen(name) + 1;
|
|
|
|
if (mod)
|
|
|
|
mlen = 1 + strlen(mod);
|
|
|
|
else
|
|
|
|
mlen = 0;
|
2005-02-27 00:34:55 +03:00
|
|
|
if (info->cursyms == info->maxsyms ||
|
2004-02-19 02:44:49 +03:00
|
|
|
(len + mlen + info->curnamep) > info->maxnamep) {
|
2004-02-18 23:41:09 +03:00
|
|
|
printf("addsym: too many symbols, skipping '%s'\n", name);
|
2003-04-24 23:56:43 +04:00
|
|
|
return;
|
|
|
|
}
|
2004-02-19 02:44:49 +03:00
|
|
|
strlcpy(&info->symnames[info->curnamep], name,
|
|
|
|
info->maxnamep - info->curnamep);
|
|
|
|
if (mlen) {
|
|
|
|
info->symnames[info->curnamep + len - 1] = '.';
|
|
|
|
strlcpy(&info->symnames[info->curnamep + len], mod,
|
|
|
|
info->maxnamep - (info->curnamep + len));
|
|
|
|
len += mlen;
|
|
|
|
}
|
|
|
|
info->syms[info->cursyms] = *sym;
|
|
|
|
info->syms[info->cursyms].st_name = info->curnamep;
|
|
|
|
info->curnamep += len;
|
2003-05-02 00:46:20 +04:00
|
|
|
if (len > ksyms_maxlen)
|
|
|
|
ksyms_maxlen = len;
|
2004-02-19 02:44:49 +03:00
|
|
|
info->cursyms++;
|
2003-04-24 23:56:43 +04:00
|
|
|
}
|
2008-10-20 14:24:18 +04:00
|
|
|
|
2003-04-24 23:56:43 +04:00
|
|
|
/*
|
2008-10-20 14:24:18 +04:00
|
|
|
* XXX REMOVE WHEN LKMS GO.
|
2003-04-24 23:56:43 +04:00
|
|
|
*/
|
2004-02-19 06:42:01 +03:00
|
|
|
static int
|
|
|
|
specialsym(const char *symname)
|
|
|
|
{
|
2008-10-20 14:24:18 +04:00
|
|
|
|
2004-02-19 06:42:01 +03:00
|
|
|
return !strcmp(symname, "_bss_start") ||
|
2008-10-20 14:24:18 +04:00
|
|
|
!strcmp(symname, "__bss_start") ||
|
|
|
|
!strcmp(symname, "_bss_end__") ||
|
|
|
|
!strcmp(symname, "__bss_end__") ||
|
|
|
|
!strcmp(symname, "_edata") ||
|
|
|
|
!strcmp(symname, "_end") ||
|
|
|
|
!strcmp(symname, "__end") ||
|
|
|
|
!strcmp(symname, "__end__") ||
|
|
|
|
!strncmp(symname, "__start_link_set_", 17) ||
|
|
|
|
!strncmp(symname, "__stop_link_set_", 16);
|
2004-02-19 06:42:01 +03:00
|
|
|
}
|
|
|
|
|
2008-10-20 14:24:18 +04:00
|
|
|
/*
|
|
|
|
* Adds a symbol table.
|
|
|
|
* "name" is the module name, "start" and "size" is where the symbol table
|
|
|
|
* is located, and "type" is in which binary format the symbol table is.
|
|
|
|
* New memory for keeping the symbol table is allocated in this function.
|
|
|
|
* Returns 0 if success and EEXIST if the module name is in use.
|
|
|
|
*
|
|
|
|
* XXX REMOVE WHEN LKMS GO.
|
|
|
|
*/
|
2003-04-24 23:56:43 +04:00
|
|
|
int
|
2003-05-11 12:23:22 +04:00
|
|
|
ksyms_addsymtab(const char *mod, void *symstart, vsize_t symsize,
|
2008-10-20 14:24:18 +04:00
|
|
|
char *strstart, vsize_t strsize)
|
2003-04-24 23:56:43 +04:00
|
|
|
{
|
|
|
|
Elf_Sym *sym = symstart;
|
2008-10-20 14:24:18 +04:00
|
|
|
struct ksyms_symtab *st;
|
2003-09-03 14:55:22 +04:00
|
|
|
unsigned long rval;
|
2003-04-24 23:56:43 +04:00
|
|
|
int i;
|
2004-02-19 02:44:49 +03:00
|
|
|
char *name;
|
|
|
|
struct syminfo info;
|
2003-04-24 23:56:43 +04:00
|
|
|
|
|
|
|
#ifdef KSYMS_DEBUG
|
|
|
|
if (ksyms_debug & FOLLOW_CALLS)
|
|
|
|
printf("ksyms_addsymtab: mod %s symsize %lx strsize %lx\n",
|
|
|
|
mod, symsize, strsize);
|
|
|
|
#endif
|
|
|
|
|
2008-10-20 14:24:18 +04:00
|
|
|
mutex_enter(&ksyms_lock);
|
2003-04-24 23:56:43 +04:00
|
|
|
|
|
|
|
/* Check if this symtab already loaded */
|
2008-10-20 14:24:18 +04:00
|
|
|
TAILQ_FOREACH(st, &ksyms_symtabs, sd_queue) {
|
|
|
|
if (st->sd_gone)
|
|
|
|
continue;
|
|
|
|
if (strcmp(mod, st->sd_name) == 0) {
|
|
|
|
mutex_exit(&ksyms_lock);
|
2003-04-24 23:56:43 +04:00
|
|
|
return EEXIST;
|
2008-10-20 14:24:18 +04:00
|
|
|
}
|
2003-04-24 23:56:43 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* XXX - Only add a symbol if it do not exist already.
|
|
|
|
* This is because of a flaw in the current LKM implementation,
|
2004-02-19 02:44:49 +03:00
|
|
|
* these loops will be removed once the in-kernel linker is in place.
|
2003-04-24 23:56:43 +04:00
|
|
|
*/
|
2004-02-19 02:44:49 +03:00
|
|
|
memset(&info, 0, sizeof(info));
|
2003-04-24 23:56:43 +04:00
|
|
|
for (i = 0; i < symsize/sizeof(Elf_Sym); i++) {
|
2004-02-19 02:44:49 +03:00
|
|
|
char * const symname = strstart + sym[i].st_name;
|
2003-04-24 23:56:43 +04:00
|
|
|
if (sym[i].st_name == 0)
|
|
|
|
continue; /* Just ignore */
|
|
|
|
|
|
|
|
/* check validity of the symbol */
|
|
|
|
/* XXX - save local symbols if DDB */
|
|
|
|
if (ELF_ST_BIND(sym[i].st_info) != STB_GLOBAL)
|
|
|
|
continue;
|
2005-02-27 00:34:55 +03:00
|
|
|
|
2003-04-24 23:56:43 +04:00
|
|
|
/* Check if the symbol exists */
|
2005-02-16 00:09:57 +03:00
|
|
|
if (ksyms_getval(NULL, symname, &rval, KSYMS_EXTERN) == 0) {
|
2003-04-24 23:56:43 +04:00
|
|
|
/* Check (and complain) about differing values */
|
2008-01-04 14:28:13 +03:00
|
|
|
if (sym[i].st_value != rval &&
|
|
|
|
sym[i].st_shndx != SHN_UNDEF) {
|
2004-02-19 06:42:01 +03:00
|
|
|
if (specialsym(symname)) {
|
2004-02-19 02:44:49 +03:00
|
|
|
info.maxsyms++;
|
|
|
|
info.maxnamep += strlen(symname) + 1 +
|
|
|
|
strlen(mod) + 1;
|
|
|
|
} else {
|
|
|
|
printf("%s: symbol '%s' redeclared with"
|
|
|
|
" different value (%lx != %lx)\n",
|
|
|
|
mod, symname,
|
|
|
|
rval, (long)sym[i].st_value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* Count this symbol
|
|
|
|
*/
|
|
|
|
info.maxsyms++;
|
|
|
|
info.maxnamep += strlen(symname) + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Now that we know the sizes, malloc the structures.
|
|
|
|
*/
|
|
|
|
info.syms = malloc(sizeof(Elf_Sym)*info.maxsyms, M_DEVBUF, M_WAITOK);
|
|
|
|
info.symnames = malloc(info.maxnamep, M_DEVBUF, M_WAITOK);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Now that we have the symbols, actually fill in the structures.
|
|
|
|
*/
|
|
|
|
for (i = 0; i < symsize/sizeof(Elf_Sym); i++) {
|
|
|
|
char * const symname = strstart + sym[i].st_name;
|
|
|
|
if (sym[i].st_name == 0)
|
|
|
|
continue; /* Just ignore */
|
|
|
|
|
|
|
|
/* check validity of the symbol */
|
|
|
|
/* XXX - save local symbols if DDB */
|
|
|
|
if (ELF_ST_BIND(sym[i].st_info) != STB_GLOBAL)
|
|
|
|
continue;
|
2005-02-27 00:34:55 +03:00
|
|
|
|
2004-02-19 02:44:49 +03:00
|
|
|
/* Check if the symbol exists */
|
2005-02-16 00:09:57 +03:00
|
|
|
if (ksyms_getval(NULL, symname, &rval, KSYMS_EXTERN) == 0) {
|
2004-02-19 06:42:01 +03:00
|
|
|
if ((sym[i].st_value != rval) && specialsym(symname)) {
|
2004-02-19 02:44:49 +03:00
|
|
|
addsym(&info, &sym[i], symname, mod);
|
2003-04-24 23:56:43 +04:00
|
|
|
}
|
|
|
|
} else
|
|
|
|
/* Ok, save this symbol */
|
2004-02-19 02:44:49 +03:00
|
|
|
addsym(&info, &sym[i], symname, NULL);
|
2003-04-24 23:56:43 +04:00
|
|
|
}
|
2003-05-02 00:46:20 +04:00
|
|
|
|
2008-10-20 14:24:18 +04:00
|
|
|
st = kmem_zalloc(sizeof(*st), KM_SLEEP);
|
2003-05-16 18:25:02 +04:00
|
|
|
i = strlen(mod) + 1;
|
|
|
|
name = malloc(i, M_DEVBUF, M_WAITOK);
|
|
|
|
strlcpy(name, mod, i);
|
2003-05-11 12:23:22 +04:00
|
|
|
st->sd_name = name;
|
2004-02-19 02:44:49 +03:00
|
|
|
st->sd_symstart = info.syms;
|
|
|
|
st->sd_symsize = sizeof(Elf_Sym)*info.maxsyms;
|
|
|
|
st->sd_strstart = info.symnames;
|
|
|
|
st->sd_strsize = info.maxnamep;
|
2008-10-20 14:24:18 +04:00
|
|
|
st->sd_malloc = true;
|
2003-04-24 23:56:43 +04:00
|
|
|
|
|
|
|
/* Make them absolute references */
|
|
|
|
sym = st->sd_symstart;
|
|
|
|
for (i = 0; i < st->sd_symsize/sizeof(Elf_Sym); i++)
|
|
|
|
sym[i].st_shndx = SHN_ABS;
|
|
|
|
|
2008-10-20 14:24:18 +04:00
|
|
|
/* ksymsread() is unlocked, so membar. */
|
|
|
|
membar_producer();
|
|
|
|
TAILQ_INSERT_TAIL(&ksyms_symtabs, st, sd_queue);
|
2003-04-24 23:56:43 +04:00
|
|
|
ksyms_sizes_calc();
|
2008-10-20 14:24:18 +04:00
|
|
|
mutex_exit(&ksyms_lock);
|
|
|
|
|
2003-04-24 23:56:43 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Remove a symbol table specified by name.
|
|
|
|
* Returns 0 if success, EBUSY if device open and ENOENT if no such name.
|
2008-10-20 14:24:18 +04:00
|
|
|
*
|
|
|
|
* XXX REMOVE WHEN LKMS GO.
|
2003-04-24 23:56:43 +04:00
|
|
|
*/
|
|
|
|
int
|
2003-05-11 12:23:22 +04:00
|
|
|
ksyms_delsymtab(const char *mod)
|
2003-04-24 23:56:43 +04:00
|
|
|
{
|
2008-10-20 14:24:18 +04:00
|
|
|
struct ksyms_symtab *st;
|
2003-04-24 23:56:43 +04:00
|
|
|
|
2008-10-20 14:24:18 +04:00
|
|
|
mutex_enter(&ksyms_lock);
|
|
|
|
TAILQ_FOREACH(st, &ksyms_symtabs, sd_queue) {
|
|
|
|
if (st->sd_gone)
|
|
|
|
continue;
|
|
|
|
if (strcmp(mod, st->sd_name) != 0)
|
|
|
|
continue;
|
|
|
|
if (ksyms_isopen) {
|
|
|
|
st->sd_gone = true;
|
|
|
|
mutex_exit(&ksyms_lock);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
TAILQ_REMOVE(&ksyms_symtabs, st, sd_queue);
|
|
|
|
ksyms_sizes_calc();
|
|
|
|
mutex_exit(&ksyms_lock);
|
|
|
|
KASSERT(st->sd_malloc);
|
|
|
|
free(st->sd_symstart, M_DEVBUF);
|
|
|
|
free(st->sd_strstart, M_DEVBUF);
|
|
|
|
/* XXXUNCONST LINTED - const castaway */
|
|
|
|
free(__UNCONST(st->sd_name), M_DEVBUF);
|
|
|
|
kmem_free(st, sizeof(*st));
|
|
|
|
return 0;
|
2003-04-24 23:56:43 +04:00
|
|
|
}
|
2008-10-20 14:24:18 +04:00
|
|
|
mutex_exit(&ksyms_lock);
|
|
|
|
return ENOENT;
|
2003-04-24 23:56:43 +04:00
|
|
|
}
|
|
|
|
|
2008-10-20 14:24:18 +04:00
|
|
|
/*
|
|
|
|
* Add a symbol table from a loadable module.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
ksyms_modload(const char *name, void *symstart, vsize_t symsize,
|
|
|
|
char *strstart, vsize_t strsize)
|
|
|
|
{
|
|
|
|
struct ksyms_symtab *st;
|
|
|
|
|
|
|
|
st = kmem_zalloc(sizeof(*st), KM_SLEEP);
|
|
|
|
mutex_enter(&ksyms_lock);
|
|
|
|
addsymtab(name, symstart, symsize, strstart, strsize, st, symstart);
|
|
|
|
mutex_exit(&ksyms_lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Remove a symbol table from a loadable module.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
ksyms_modunload(const char *name)
|
2003-11-17 13:16:18 +03:00
|
|
|
{
|
2008-10-20 14:24:18 +04:00
|
|
|
struct ksyms_symtab *st;
|
|
|
|
|
|
|
|
mutex_enter(&ksyms_lock);
|
|
|
|
TAILQ_FOREACH(st, &ksyms_symtabs, sd_queue) {
|
|
|
|
if (st->sd_gone)
|
|
|
|
continue;
|
|
|
|
if (strcmp(name, st->sd_name) != 0)
|
|
|
|
continue;
|
|
|
|
KASSERT(!st->sd_malloc); /* XXXLKM */
|
|
|
|
st->sd_gone = true;
|
|
|
|
if (!ksyms_isopen) {
|
|
|
|
TAILQ_REMOVE(&ksyms_symtabs, st, sd_queue);
|
|
|
|
ksyms_sizes_calc();
|
|
|
|
kmem_free(st, sizeof(*st));
|
|
|
|
}
|
|
|
|
break;
|
2003-11-17 13:16:18 +03:00
|
|
|
}
|
2008-10-20 14:24:18 +04:00
|
|
|
mutex_exit(&ksyms_lock);
|
|
|
|
KASSERT(st != NULL);
|
2003-11-17 13:16:18 +03:00
|
|
|
}
|
|
|
|
|
2003-04-24 23:56:43 +04:00
|
|
|
#ifdef DDB
|
|
|
|
/*
|
|
|
|
* Keep sifting stuff here, to avoid export of ksyms internals.
|
2008-10-20 14:24:18 +04:00
|
|
|
*
|
|
|
|
* Systems is expected to be quiescent, so no locking done.
|
2003-04-24 23:56:43 +04:00
|
|
|
*/
|
|
|
|
int
|
|
|
|
ksyms_sift(char *mod, char *sym, int mode)
|
|
|
|
{
|
2008-10-20 14:24:18 +04:00
|
|
|
struct ksyms_symtab *st;
|
2003-04-24 23:56:43 +04:00
|
|
|
char *sb;
|
|
|
|
int i, sz;
|
|
|
|
|
2008-10-20 14:24:18 +04:00
|
|
|
if (!ksyms_initted)
|
2003-04-24 23:56:43 +04:00
|
|
|
return ENOENT;
|
|
|
|
|
2008-10-20 14:24:18 +04:00
|
|
|
TAILQ_FOREACH(st, &ksyms_symtabs, sd_queue) {
|
|
|
|
if (st->sd_gone)
|
|
|
|
continue;
|
2003-04-24 23:56:43 +04:00
|
|
|
if (mod && strcmp(mod, st->sd_name))
|
|
|
|
continue;
|
2008-10-20 14:24:18 +04:00
|
|
|
sb = st->sd_strstart - st->sd_usroffset;
|
2003-04-24 23:56:43 +04:00
|
|
|
|
|
|
|
sz = st->sd_symsize/sizeof(Elf_Sym);
|
|
|
|
for (i = 0; i < sz; i++) {
|
|
|
|
Elf_Sym *les = st->sd_symstart + i;
|
|
|
|
char c;
|
|
|
|
|
2008-10-20 14:24:18 +04:00
|
|
|
if (strstr(sb + les->st_name, sym) == NULL)
|
2003-04-24 23:56:43 +04:00
|
|
|
continue;
|
|
|
|
|
|
|
|
if (mode == 'F') {
|
|
|
|
switch (ELF_ST_TYPE(les->st_info)) {
|
|
|
|
case STT_OBJECT:
|
|
|
|
c = '+';
|
|
|
|
break;
|
|
|
|
case STT_FUNC:
|
|
|
|
c = '*';
|
|
|
|
break;
|
|
|
|
case STT_SECTION:
|
|
|
|
c = '&';
|
|
|
|
break;
|
|
|
|
case STT_FILE:
|
|
|
|
c = '/';
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
c = ' ';
|
|
|
|
break;
|
|
|
|
}
|
2008-10-20 14:24:18 +04:00
|
|
|
db_printf("%s%c ", sb + les->st_name, c);
|
2003-04-24 23:56:43 +04:00
|
|
|
} else
|
2008-10-20 14:24:18 +04:00
|
|
|
db_printf("%s ", sb + les->st_name);
|
2003-04-24 23:56:43 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return ENOENT;
|
|
|
|
}
|
2005-06-24 03:15:12 +04:00
|
|
|
#endif /* DDB */
|
2003-04-24 23:56:43 +04:00
|
|
|
|
|
|
|
/*
|
2008-10-20 14:24:18 +04:00
|
|
|
* In case we exposing the symbol table to the userland using the pseudo-
|
|
|
|
* device /dev/ksyms, it is easier to provide all the tables as one.
|
|
|
|
* However, it means we have to change all the st_name fields for the
|
|
|
|
* symbols so they match the ELF image that the userland will read
|
|
|
|
* through the device.
|
|
|
|
*
|
|
|
|
* The actual (correct) value of st_name is preserved through a global
|
|
|
|
* offset stored in the symbol table structure.
|
|
|
|
*
|
|
|
|
* Call with ksyms_lock held.
|
2003-04-24 23:56:43 +04:00
|
|
|
*/
|
2008-10-20 14:24:18 +04:00
|
|
|
static void
|
|
|
|
ksyms_sizes_calc(void)
|
|
|
|
{
|
|
|
|
struct ksyms_symtab *st;
|
|
|
|
int i, delta;
|
2003-04-24 23:56:43 +04:00
|
|
|
|
2008-10-20 14:24:18 +04:00
|
|
|
ksyms_symsz = ksyms_strsz = 0;
|
|
|
|
TAILQ_FOREACH(st, &ksyms_symtabs, sd_queue) {
|
|
|
|
delta = ksyms_strsz - st->sd_usroffset;
|
|
|
|
if (delta != 0) {
|
|
|
|
for (i = 0; i < st->sd_symsize/sizeof(Elf_Sym); i++)
|
|
|
|
st->sd_symstart[i].st_name += delta;
|
|
|
|
st->sd_usroffset = ksyms_strsz;
|
|
|
|
}
|
|
|
|
ksyms_symsz += st->sd_symsize;
|
|
|
|
ksyms_strsz += st->sd_strsize;
|
|
|
|
}
|
|
|
|
}
|
2003-04-24 23:56:43 +04:00
|
|
|
|
2005-06-24 03:15:12 +04:00
|
|
|
static void
|
2007-03-04 08:59:00 +03:00
|
|
|
ksyms_hdr_init(void *hdraddr)
|
2003-04-24 23:56:43 +04:00
|
|
|
{
|
|
|
|
|
|
|
|
/* Copy the loaded elf exec header */
|
|
|
|
memcpy(&ksyms_hdr.kh_ehdr, hdraddr, sizeof(Elf_Ehdr));
|
|
|
|
|
|
|
|
/* Set correct program/section header sizes, offsets and numbers */
|
|
|
|
ksyms_hdr.kh_ehdr.e_phoff = offsetof(struct ksyms_hdr, kh_phdr[0]);
|
|
|
|
ksyms_hdr.kh_ehdr.e_phentsize = sizeof(Elf_Phdr);
|
|
|
|
ksyms_hdr.kh_ehdr.e_phnum = NPRGHDR;
|
|
|
|
ksyms_hdr.kh_ehdr.e_shoff = offsetof(struct ksyms_hdr, kh_shdr[0]);
|
|
|
|
ksyms_hdr.kh_ehdr.e_shentsize = sizeof(Elf_Shdr);
|
|
|
|
ksyms_hdr.kh_ehdr.e_shnum = NSECHDR;
|
|
|
|
ksyms_hdr.kh_ehdr.e_shstrndx = NSECHDR - 1; /* Last section */
|
|
|
|
|
2008-10-20 14:24:18 +04:00
|
|
|
/* Text */
|
|
|
|
ksyms_hdr.kh_phdr[0].p_type = PT_LOAD;
|
|
|
|
ksyms_hdr.kh_phdr[0].p_memsz = (unsigned long)-1L;
|
|
|
|
ksyms_hdr.kh_phdr[0].p_flags = PF_R | PF_X;
|
|
|
|
|
|
|
|
/* Data */
|
|
|
|
ksyms_hdr.kh_phdr[1].p_type = PT_LOAD;
|
|
|
|
ksyms_hdr.kh_phdr[1].p_memsz = (unsigned long)-1L;
|
|
|
|
ksyms_hdr.kh_phdr[1].p_flags = PF_R | PF_W | PF_X;
|
|
|
|
|
|
|
|
/* First section is null */
|
2003-04-24 23:56:43 +04:00
|
|
|
|
|
|
|
/* Second section header; ".symtab" */
|
|
|
|
ksyms_hdr.kh_shdr[SYMTAB].sh_name = 1; /* Section 3 offset */
|
|
|
|
ksyms_hdr.kh_shdr[SYMTAB].sh_type = SHT_SYMTAB;
|
|
|
|
ksyms_hdr.kh_shdr[SYMTAB].sh_offset = sizeof(struct ksyms_hdr);
|
|
|
|
/* ksyms_hdr.kh_shdr[SYMTAB].sh_size = filled in at open */
|
|
|
|
ksyms_hdr.kh_shdr[SYMTAB].sh_link = 2; /* Corresponding strtab */
|
|
|
|
ksyms_hdr.kh_shdr[SYMTAB].sh_addralign = sizeof(long);
|
|
|
|
ksyms_hdr.kh_shdr[SYMTAB].sh_entsize = sizeof(Elf_Sym);
|
|
|
|
|
|
|
|
/* Third section header; ".strtab" */
|
|
|
|
ksyms_hdr.kh_shdr[STRTAB].sh_name = 9; /* Section 3 offset */
|
|
|
|
ksyms_hdr.kh_shdr[STRTAB].sh_type = SHT_STRTAB;
|
|
|
|
/* ksyms_hdr.kh_shdr[STRTAB].sh_offset = filled in at open */
|
|
|
|
/* ksyms_hdr.kh_shdr[STRTAB].sh_size = filled in at open */
|
|
|
|
ksyms_hdr.kh_shdr[STRTAB].sh_addralign = sizeof(char);
|
|
|
|
|
|
|
|
/* Fourth section, ".shstrtab" */
|
|
|
|
ksyms_hdr.kh_shdr[SHSTRTAB].sh_name = 17; /* This section name offset */
|
|
|
|
ksyms_hdr.kh_shdr[SHSTRTAB].sh_type = SHT_STRTAB;
|
|
|
|
ksyms_hdr.kh_shdr[SHSTRTAB].sh_offset =
|
|
|
|
offsetof(struct ksyms_hdr, kh_strtab);
|
|
|
|
ksyms_hdr.kh_shdr[SHSTRTAB].sh_size = SHSTRSIZ;
|
|
|
|
ksyms_hdr.kh_shdr[SHSTRTAB].sh_addralign = sizeof(char);
|
|
|
|
|
|
|
|
/* Set section names */
|
2003-05-16 18:25:02 +04:00
|
|
|
strlcpy(&ksyms_hdr.kh_strtab[1], ".symtab",
|
|
|
|
sizeof(ksyms_hdr.kh_strtab) - 1);
|
|
|
|
strlcpy(&ksyms_hdr.kh_strtab[9], ".strtab",
|
|
|
|
sizeof(ksyms_hdr.kh_strtab) - 9);
|
|
|
|
strlcpy(&ksyms_hdr.kh_strtab[17], ".shstrtab",
|
|
|
|
sizeof(ksyms_hdr.kh_strtab) - 17);
|
2008-10-20 14:24:18 +04:00
|
|
|
}
|
2003-04-24 23:56:43 +04:00
|
|
|
|
2005-06-24 03:15:12 +04:00
|
|
|
static int
|
2006-11-01 13:17:58 +03:00
|
|
|
ksymsopen(dev_t dev, int oflags, int devtype, struct lwp *l)
|
2003-04-24 23:56:43 +04:00
|
|
|
{
|
|
|
|
|
2008-10-20 14:24:18 +04:00
|
|
|
if (minor(dev) != 0 || !ksyms_initted)
|
2004-01-09 01:48:26 +03:00
|
|
|
return ENXIO;
|
2003-04-24 23:56:43 +04:00
|
|
|
|
2008-10-20 14:24:18 +04:00
|
|
|
/*
|
|
|
|
* Create a "snapshot" of the kernel symbol table. Setting
|
|
|
|
* ksyms_isopen will prevent symbol tables from being freed.
|
|
|
|
*/
|
|
|
|
mutex_enter(&ksyms_lock);
|
|
|
|
ksyms_hdr.kh_shdr[SYMTAB].sh_size = ksyms_symsz;
|
|
|
|
ksyms_hdr.kh_shdr[SYMTAB].sh_info = ksyms_symsz / sizeof(Elf_Sym);
|
|
|
|
ksyms_hdr.kh_shdr[STRTAB].sh_offset = ksyms_symsz +
|
2003-04-24 23:56:43 +04:00
|
|
|
ksyms_hdr.kh_shdr[SYMTAB].sh_offset;
|
2008-10-20 14:24:18 +04:00
|
|
|
ksyms_hdr.kh_shdr[STRTAB].sh_size = ksyms_strsz;
|
|
|
|
ksyms_isopen = true;
|
|
|
|
mutex_exit(&ksyms_lock);
|
2003-04-24 23:56:43 +04:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-06-24 03:15:12 +04:00
|
|
|
static int
|
2006-11-01 13:17:58 +03:00
|
|
|
ksymsclose(dev_t dev, int oflags, int devtype, struct lwp *l)
|
2003-04-24 23:56:43 +04:00
|
|
|
{
|
2008-10-20 14:24:18 +04:00
|
|
|
struct ksyms_symtab *st, *next;
|
|
|
|
bool resize;
|
|
|
|
|
|
|
|
/* Discard refernces to symbol tables. */
|
|
|
|
mutex_enter(&ksyms_lock);
|
|
|
|
ksyms_isopen = false;
|
|
|
|
resize = false;
|
|
|
|
for (st = TAILQ_FIRST(&ksyms_symtabs); st != NULL; st = next) {
|
|
|
|
next = TAILQ_NEXT(st, sd_queue);
|
|
|
|
if (st->sd_gone) {
|
|
|
|
TAILQ_REMOVE(&ksyms_symtabs, st, sd_queue);
|
|
|
|
kmem_free(st, sizeof(*st));
|
|
|
|
if (st->sd_malloc) { /* XXXLKM */
|
|
|
|
free(st->sd_symstart, M_DEVBUF);
|
|
|
|
free(st->sd_strstart, M_DEVBUF);
|
|
|
|
/* XXXUNCONST LINTED - const castaway */
|
|
|
|
free(__UNCONST(st->sd_name), M_DEVBUF);
|
|
|
|
}
|
|
|
|
resize = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (resize)
|
|
|
|
ksyms_sizes_calc();
|
|
|
|
mutex_exit(&ksyms_lock);
|
2003-04-24 23:56:43 +04:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-06-24 03:15:12 +04:00
|
|
|
static int
|
2006-11-01 13:17:58 +03:00
|
|
|
ksymsread(dev_t dev, struct uio *uio, int ioflag)
|
2003-04-24 23:56:43 +04:00
|
|
|
{
|
2008-10-20 14:24:18 +04:00
|
|
|
struct ksyms_symtab *st;
|
2003-04-24 23:56:43 +04:00
|
|
|
size_t filepos, inpos, off;
|
2008-10-20 14:24:18 +04:00
|
|
|
int error;
|
2003-04-24 23:56:43 +04:00
|
|
|
|
|
|
|
/*
|
2008-10-20 14:24:18 +04:00
|
|
|
* First: Copy out the ELF header. XXX Lose if ksymsopen()
|
|
|
|
* occurs during read of the header.
|
2003-04-24 23:56:43 +04:00
|
|
|
*/
|
2008-10-20 14:24:18 +04:00
|
|
|
off = uio->uio_offset;
|
|
|
|
if (off < sizeof(struct ksyms_hdr)) {
|
|
|
|
error = uiomove((char *)&ksyms_hdr + off,
|
|
|
|
sizeof(struct ksyms_hdr) - off, uio);
|
|
|
|
if (error != 0)
|
|
|
|
return error;
|
|
|
|
}
|
2003-04-24 23:56:43 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copy out the symbol table.
|
|
|
|
*/
|
2008-10-20 14:24:18 +04:00
|
|
|
filepos = sizeof(struct ksyms_hdr);
|
|
|
|
TAILQ_FOREACH(st, &ksyms_symtabs, sd_queue) {
|
2003-04-24 23:56:43 +04:00
|
|
|
if (uio->uio_resid == 0)
|
|
|
|
return 0;
|
|
|
|
if (uio->uio_offset <= st->sd_symsize + filepos) {
|
|
|
|
inpos = uio->uio_offset - filepos;
|
2008-10-20 14:24:18 +04:00
|
|
|
error = uiomove((char *)st->sd_symstart + inpos,
|
2003-04-24 23:56:43 +04:00
|
|
|
st->sd_symsize - inpos, uio);
|
2008-10-20 14:24:18 +04:00
|
|
|
if (error != 0)
|
|
|
|
return error;
|
2003-04-24 23:56:43 +04:00
|
|
|
}
|
|
|
|
filepos += st->sd_symsize;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copy out the string table
|
|
|
|
*/
|
2008-10-20 14:24:18 +04:00
|
|
|
KASSERT(filepos == sizeof(struct ksyms_hdr) +
|
|
|
|
ksyms_hdr.kh_shdr[SYMTAB].sh_size);
|
|
|
|
TAILQ_FOREACH(st, &ksyms_symtabs, sd_queue) {
|
2003-04-24 23:56:43 +04:00
|
|
|
if (uio->uio_resid == 0)
|
|
|
|
return 0;
|
|
|
|
if (uio->uio_offset <= st->sd_strsize + filepos) {
|
|
|
|
inpos = uio->uio_offset - filepos;
|
2008-10-20 14:24:18 +04:00
|
|
|
error = uiomove((char *)st->sd_strstart + inpos,
|
2003-04-24 23:56:43 +04:00
|
|
|
st->sd_strsize - inpos, uio);
|
2008-10-20 14:24:18 +04:00
|
|
|
if (error != 0)
|
|
|
|
return error;
|
2003-04-24 23:56:43 +04:00
|
|
|
}
|
|
|
|
filepos += st->sd_strsize;
|
|
|
|
}
|
2008-10-20 14:24:18 +04:00
|
|
|
|
2003-04-24 23:56:43 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-06-24 03:15:12 +04:00
|
|
|
static int
|
2006-11-01 13:17:58 +03:00
|
|
|
ksymswrite(dev_t dev, struct uio *uio, int ioflag)
|
2003-04-24 23:56:43 +04:00
|
|
|
{
|
2006-11-01 13:17:58 +03:00
|
|
|
|
2003-04-24 23:56:43 +04:00
|
|
|
return EROFS;
|
|
|
|
}
|
|
|
|
|
2005-06-24 03:15:12 +04:00
|
|
|
static int
|
2007-03-04 08:59:00 +03:00
|
|
|
ksymsioctl(dev_t dev, u_long cmd, void *data, int fflag, struct lwp *l)
|
2003-04-24 23:56:43 +04:00
|
|
|
{
|
|
|
|
struct ksyms_gsymbol *kg = (struct ksyms_gsymbol *)data;
|
2008-10-20 14:24:18 +04:00
|
|
|
struct ksyms_symtab *st;
|
|
|
|
Elf_Sym *sym = NULL, copy;
|
2003-04-24 23:56:43 +04:00
|
|
|
unsigned long val;
|
|
|
|
int error = 0;
|
2003-10-25 13:03:59 +04:00
|
|
|
char *str = NULL;
|
2008-10-20 14:24:18 +04:00
|
|
|
int len;
|
|
|
|
|
|
|
|
/* Read ksyms_maxlen only once while not holding the lock. */
|
|
|
|
len = ksyms_maxlen;
|
2003-05-02 00:46:20 +04:00
|
|
|
|
2008-10-20 14:24:18 +04:00
|
|
|
if (cmd == KIOCGVALUE || cmd == KIOCGSYMBOL) {
|
|
|
|
str = kmem_alloc(len, KM_SLEEP);
|
|
|
|
if ((error = copyinstr(kg->kg_name, str, len, NULL)) != 0) {
|
|
|
|
kmem_free(str, len);
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
}
|
2003-04-24 23:56:43 +04:00
|
|
|
|
|
|
|
switch (cmd) {
|
|
|
|
case KIOCGVALUE:
|
|
|
|
/*
|
|
|
|
* Use the in-kernel symbol lookup code for fast
|
|
|
|
* retreival of a value.
|
|
|
|
*/
|
2008-10-20 14:24:18 +04:00
|
|
|
mutex_enter(&ksyms_lock);
|
|
|
|
error = ksyms_getval(NULL, str, &val, KSYMS_EXTERN);
|
|
|
|
mutex_exit(&ksyms_lock);
|
|
|
|
if (error == 0)
|
|
|
|
error = copyout(&val, kg->kg_value, sizeof(long));
|
|
|
|
kmem_free(str, len);
|
2003-04-24 23:56:43 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case KIOCGSYMBOL:
|
|
|
|
/*
|
|
|
|
* Use the in-kernel symbol lookup code for fast
|
|
|
|
* retreival of a symbol.
|
|
|
|
*/
|
2008-10-20 14:24:18 +04:00
|
|
|
mutex_enter(&ksyms_lock);
|
|
|
|
TAILQ_FOREACH(st, &ksyms_symtabs, sd_queue) {
|
|
|
|
if (st->sd_gone)
|
|
|
|
continue;
|
|
|
|
if ((sym = findsym(str, st)) == NULL)
|
2003-04-24 23:56:43 +04:00
|
|
|
continue;
|
2008-07-23 01:18:35 +04:00
|
|
|
#ifdef notdef
|
2003-04-24 23:56:43 +04:00
|
|
|
/* Skip if bad binding */
|
|
|
|
if (ELF_ST_BIND(sym->st_info) != STB_GLOBAL) {
|
|
|
|
sym = NULL;
|
|
|
|
continue;
|
|
|
|
}
|
2008-07-23 01:18:35 +04:00
|
|
|
#endif
|
2003-04-24 23:56:43 +04:00
|
|
|
break;
|
|
|
|
}
|
2008-10-20 14:24:18 +04:00
|
|
|
if (sym != NULL) {
|
|
|
|
memcpy(©, sym, sizeof(copy));
|
|
|
|
mutex_exit(&ksyms_lock);
|
|
|
|
error = copyout(©, kg->kg_sym, sizeof(Elf_Sym));
|
|
|
|
} else {
|
|
|
|
mutex_exit(&ksyms_lock);
|
2003-04-24 23:56:43 +04:00
|
|
|
error = ENOENT;
|
2008-10-20 14:24:18 +04:00
|
|
|
}
|
|
|
|
kmem_free(str, len);
|
2003-04-24 23:56:43 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case KIOCGSIZE:
|
|
|
|
/*
|
|
|
|
* Get total size of symbol table.
|
|
|
|
*/
|
2008-10-20 14:24:18 +04:00
|
|
|
mutex_enter(&ksyms_lock);
|
|
|
|
*(int *)data = ksyms_strsz + ksyms_symsz +
|
|
|
|
sizeof(struct ksyms_hdr);
|
|
|
|
mutex_exit(&ksyms_lock);
|
2003-04-24 23:56:43 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
error = ENOTTY;
|
|
|
|
break;
|
|
|
|
}
|
2003-05-02 00:46:20 +04:00
|
|
|
|
|
|
|
return error;
|
2003-04-24 23:56:43 +04:00
|
|
|
}
|
2005-06-24 03:15:12 +04:00
|
|
|
|
|
|
|
const struct cdevsw ksyms_cdevsw = {
|
|
|
|
ksymsopen, ksymsclose, ksymsread, ksymswrite, ksymsioctl,
|
2008-10-20 14:24:18 +04:00
|
|
|
nullstop, notty, nopoll, nommap, nullkqfilter, D_OTHER | D_MPSAFE
|
2005-06-24 03:15:12 +04:00
|
|
|
};
|