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