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