kern_ksyms.c revision 1.35.12.2 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.12.2 2008/10/10 22:34:14 skrll 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 /*
305 * The "attach" is in reality done in ksyms_init().
306 */
307 void ksymsattach(int);
308 void
309 ksymsattach(int arg)
310 {
311
312 #ifdef USE_PTREE
313 if (baseidx == 0)
314 ptree_gen(0, &kernel_symtab);
315 #endif
316
317 }
318
319 /*
320 * Add a symbol table.
321 * This is intended for use when the symbol table and its corresponding
322 * string table are easily available. If they are embedded in an ELF
323 * image, use addsymtab_elf() instead.
324 *
325 * name - Symbol's table name.
326 * symstart, symsize - Address and size of the symbol table.
327 * strstart, strsize - Address and size of the string table.
328 * tab - Symbol table to be updated with this information.
329 * newstart - Address to which the symbol table has to be copied during
330 * shrinking. If NULL, it is not moved.
331 */
332 static void
333 addsymtab(const char *name,
334 void *symstart, size_t symsize,
335 void *strstart, size_t strsize,
336 struct symtab *tab,
337 void *newstart)
338 {
339 void *send;
340 Elf_Sym *sym, *nsym;
341 int i, n, g;
342 char *str;
343
344 if (newstart == NULL)
345 newstart = symstart;
346 KASSERT(newstart <= symstart && symstart <= strstart);
347
348 tab->sd_symstart = (Elf_Sym *)symstart;
349 tab->sd_symsize = symsize;
350 tab->sd_strstart = strstart;
351 tab->sd_strsize = strsize;
352 tab->sd_name = name;
353 send = tab->sd_strstart + tab->sd_strsize;
354
355 #ifdef KSYMS_DEBUG
356 printf("newstart %p sym %p symsz %d str %p strsz %d send %p\n",
357 newstart, symstart, symsize, strstart, strsize, send);
358 #endif
359
360 /*
361 * Pack symbol table by removing all file name references
362 * and overwrite the elf header.
363 */
364 sym = tab->sd_symstart;
365 nsym = (Elf_Sym *)newstart;
366 str = tab->sd_strstart;
367 for (g = i = n = 0; i < tab->sd_symsize/sizeof(Elf_Sym); i++) {
368 if (i == 0) {
369 nsym[n++] = sym[i];
370 continue;
371 }
372 /*
373 * Remove useless symbols.
374 * Should actually remove all typeless symbols.
375 */
376 if (sym[i].st_name == 0)
377 continue; /* Skip nameless entries */
378 if (sym[i].st_shndx == SHN_UNDEF)
379 continue; /* Skip external references */
380 if (ELF_ST_TYPE(sym[i].st_info) == STT_FILE)
381 continue; /* Skip filenames */
382 if (ELF_ST_TYPE(sym[i].st_info) == STT_NOTYPE &&
383 sym[i].st_value == 0 &&
384 strcmp(str + sym[i].st_name, "*ABS*") == 0)
385 continue; /* XXX */
386 if (ELF_ST_TYPE(sym[i].st_info) == STT_NOTYPE &&
387 strcmp(str + sym[i].st_name, "gcc2_compiled.") == 0)
388 continue; /* XXX */
389
390 #ifndef DDB
391 /* Only need global symbols */
392 if (ELF_ST_BIND(sym[i].st_info) != STB_GLOBAL)
393 continue;
394 #endif
395
396 /* Save symbol. Set it as an absolute offset */
397 nsym[n] = sym[i];
398 nsym[n].st_shndx = SHN_ABS;
399 if (ELF_ST_BIND(nsym[n].st_info) == STB_GLOBAL)
400 g++;
401 #if NKSYMS
402 {
403 int j;
404 j = strlen(nsym[n].st_name + tab->sd_strstart) + 1;
405 if (j > ksyms_maxlen)
406 ksyms_maxlen = j;
407 }
408 #endif
409 n++;
410
411 }
412 tab->sd_symstart = nsym;
413 tab->sd_symsize = n * sizeof(Elf_Sym);
414
415 #ifdef notyet
416 /*
417 * Remove left-over strings.
418 */
419 sym = tab->sd_symstart;
420 str = (void *)tab->sd_symstart + tab->sd_symsize;
421 str[0] = 0;
422 n = 1;
423 for (i = 1; i < tab->sd_symsize/sizeof(Elf_Sym); i++) {
424 strcpy(str + n, tab->sd_strstart + sym[i].st_name);
425 sym[i].st_name = n;
426 n += strlen(str+n) + 1;
427 }
428 tab->sd_strstart = str;
429 tab->sd_strsize = n;
430
431 #ifdef KSYMS_DEBUG
432 printf("str %p strsz %d send %p\n", str, n, send);
433 #endif
434 #endif
435
436 CIRCLEQ_INSERT_HEAD(&symtab_queue, tab, sd_queue);
437
438 #ifdef notyet
439 #ifdef USE_PTREE
440 /* Try to use the freed space, if possible */
441 if (send - str - n > g * sizeof(struct ptree))
442 ptree_gen(str + n, tab);
443 #endif
444 #endif
445 }
446
447 /*
448 * Add a symbol table named name.
449 * This is intended for use when the kernel loader enters the table.
450 */
451 static void
452 addsymtab_elf(const char *name, Elf_Ehdr *ehdr, struct symtab *tab)
453 {
454 int i, j;
455 char *start = (char *)ehdr;
456 Elf_Shdr *shdr;
457 char *symstart = NULL, *strstart = NULL;
458 size_t symsize = 0, strsize = 0;
459
460 /* Find the symbol table and the corresponding string table. */
461 shdr = (Elf_Shdr *)(start + ehdr->e_shoff);
462 for (i = 1; i < ehdr->e_shnum; i++) {
463 if (shdr[i].sh_type != SHT_SYMTAB)
464 continue;
465 if (shdr[i].sh_offset == 0)
466 continue;
467 symstart = start + shdr[i].sh_offset;
468 symsize = shdr[i].sh_size;
469 j = shdr[i].sh_link;
470 if (shdr[j].sh_offset == 0)
471 continue; /* Can this happen? */
472 strstart = start + shdr[j].sh_offset;
473 strsize = shdr[j].sh_size;
474 break;
475 }
476
477 if (!ksyms_verify(symstart, strstart))
478 return;
479
480 addsymtab(name, symstart, symsize, strstart, strsize, tab, start);
481 }
482
483 /*
484 * Setup the kernel symbol table stuff.
485 */
486 void
487 ksyms_init(int symsize, void *start, void *end)
488 {
489 Elf_Ehdr *ehdr;
490
491 #ifdef SYMTAB_SPACE
492 if (symsize <= 0 &&
493 strncmp(db_symtab, SYMTAB_FILLER, sizeof(SYMTAB_FILLER))) {
494 symsize = db_symtabsize;
495 start = db_symtab;
496 end = db_symtab + db_symtabsize;
497 }
498 #endif
499 if (symsize <= 0) {
500 printf("[ Kernel symbol table missing! ]\n");
501 return;
502 }
503
504 /* Sanity check */
505 if (ALIGNED_POINTER(start, long) == 0) {
506 printf("[ Kernel symbol table has bad start address %p ]\n",
507 start);
508 return;
509 }
510
511 ehdr = (Elf_Ehdr *)start;
512
513 /* check if this is a valid ELF header */
514 /* No reason to verify arch type, the kernel is actually running! */
515 if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG) ||
516 ehdr->e_ident[EI_CLASS] != ELFCLASS ||
517 ehdr->e_version > 1) {
518 #ifdef notyet /* DDB */
519 if (ddb_init(symsize, start, end))
520 return; /* old-style symbol table */
521 #endif
522 printf("[ Kernel symbol table invalid! ]\n");
523 return; /* nothing to do */
524 }
525
526 #if NKSYMS
527 /* Loaded header will be scratched in addsymtab */
528 ksyms_hdr_init(start);
529 #endif
530
531 addsymtab_elf("netbsd", ehdr, &kernel_symtab);
532
533 #if NKSYMS
534 ksyms_sizes_calc();
535 #endif
536
537 ksymsinited = 1;
538
539 #ifdef DEBUG
540 printf("Loaded initial symtab at %p, strtab at %p, # entries %ld\n",
541 kernel_symtab.sd_symstart, kernel_symtab.sd_strstart,
542 (long)kernel_symtab.sd_symsize/sizeof(Elf_Sym));
543 #endif
544 }
545
546 /*
547 * Setup the kernel symbol table stuff.
548 * Use this when the address of the symbol and string tables are known;
549 * otherwise use ksyms_init with an ELF image.
550 * We need to pass a minimal ELF header which will later be completed by
551 * ksyms_hdr_init and handed off to userland through /dev/ksyms. We use
552 * a void *rather than a pointer to avoid exposing the Elf_Ehdr type.
553 */
554 void
555 ksyms_init_explicit(void *ehdr, void *symstart, size_t symsize,
556 void *strstart, size_t strsize)
557 {
558
559 if (!ksyms_verify(symstart, strstart))
560 return;
561
562 #if NKSYMS
563 ksyms_hdr_init(ehdr);
564 #endif
565
566 addsymtab("netbsd", symstart, symsize, strstart, strsize,
567 &kernel_symtab, NULL);
568
569 #if NKSYMS
570 ksyms_sizes_calc();
571 #endif
572
573 ksymsinited = 1;
574 }
575
576 /*
577 * Get the value associated with a symbol.
578 * "mod" is the module name, or null if any module.
579 * "sym" is the symbol name.
580 * "val" is a pointer to the corresponding value, if call succeeded.
581 * Returns 0 if success or ENOENT if no such entry.
582 */
583 int
584 ksyms_getval(const char *mod, const char *sym, unsigned long *val, int type)
585 {
586 struct symtab *st;
587 Elf_Sym *es;
588
589 if (ksymsinited == 0)
590 return ENOENT;
591
592 #ifdef KSYMS_DEBUG
593 if (ksyms_debug & FOLLOW_CALLS)
594 printf("ksyms_getval: mod %s sym %s valp %p\n", mod, sym, val);
595 #endif
596
597 CIRCLEQ_FOREACH(st, &symtab_queue, sd_queue) {
598 if (mod && strcmp(st->sd_name, mod))
599 continue;
600 if ((es = findsym(sym, st)) == NULL)
601 continue;
602 if (es->st_shndx == SHN_UNDEF)
603 continue;
604
605 /* Skip if bad binding */
606 if (type == KSYMS_EXTERN &&
607 ELF_ST_BIND(es->st_info) != STB_GLOBAL)
608 continue;
609
610 if (val)
611 *val = es->st_value;
612 return 0;
613 }
614 return ENOENT;
615 }
616
617 /*
618 * Get "mod" and "symbol" associated with an address.
619 * Returns 0 if success or ENOENT if no such entry.
620 */
621 int
622 ksyms_getname(const char **mod, const char **sym, vaddr_t v, int f)
623 {
624 struct symtab *st;
625 Elf_Sym *les, *es = NULL;
626 vaddr_t laddr = 0;
627 const char *lmod = NULL;
628 char *stable = NULL;
629 int type, i, sz;
630
631 if (ksymsinited == 0)
632 return ENOENT;
633
634 CIRCLEQ_FOREACH(st, &symtab_queue, sd_queue) {
635 if (st->sd_minsym != NULL && v < st->sd_minsym->st_value)
636 continue;
637 if (st->sd_maxsym != NULL && v > st->sd_maxsym->st_value)
638 continue;
639 sz = st->sd_symsize/sizeof(Elf_Sym);
640 for (i = 0; i < sz; i++) {
641 les = st->sd_symstart + i;
642 type = ELF_ST_TYPE(les->st_info);
643
644 if ((f & KSYMS_PROC) && (type != STT_FUNC))
645 continue;
646
647 if (type == STT_NOTYPE)
648 continue;
649
650 if (((f & KSYMS_ANY) == 0) &&
651 (type != STT_FUNC) && (type != STT_OBJECT))
652 continue;
653
654 if ((les->st_value <= v) && (les->st_value > laddr)) {
655 laddr = les->st_value;
656 es = les;
657 lmod = st->sd_name;
658 stable = st->sd_strstart - st->sd_usroffset;
659 }
660 }
661 }
662 if (es == NULL)
663 return ENOENT;
664 if ((f & KSYMS_EXACT) && (v != es->st_value))
665 return ENOENT;
666 if (mod)
667 *mod = lmod;
668 if (sym)
669 *sym = stable + es->st_name;
670 return 0;
671 }
672
673 #if NKSYMS
674 static int symsz, strsz;
675
676 /*
677 * In case we exposing the symbol table to the userland using the pseudo-
678 * device /dev/ksyms, it is easier to provide all the tables as one.
679 * However, it means we have to change all the st_name fields for the
680 * symbols so they match the ELF image that the userland will read
681 * through the device.
682 *
683 * The actual (correct) value of st_name is preserved through a global
684 * offset stored in the symbol table structure.
685 */
686
687 static void
688 ksyms_sizes_calc(void)
689 {
690 struct symtab *st;
691 int i;
692
693 symsz = strsz = 0;
694 CIRCLEQ_FOREACH(st, &symtab_queue, sd_queue) {
695 if (st != &kernel_symtab) {
696 for (i = 0; i < st->sd_symsize/sizeof(Elf_Sym); i++)
697 st->sd_symstart[i].st_name =
698 strsz + st->sd_symnmoff[i];
699 st->sd_usroffset = strsz;
700 }
701 symsz += st->sd_symsize;
702 strsz += st->sd_strsize;
703 }
704 }
705 #endif /* NKSYMS */
706
707 /*
708 * Temporary work structure for dynamic loaded symbol tables.
709 * Will go away when in-kernel linker is in place.
710 */
711
712 struct syminfo {
713 size_t cursyms;
714 size_t curnamep;
715 size_t maxsyms;
716 size_t maxnamep;
717 Elf_Sym *syms;
718 int *symnmoff;
719 char *symnames;
720 };
721
722
723 /*
724 * Add a symbol to the temporary save area for symbols.
725 * This routine will go away when the in-kernel linker is in place.
726 */
727 static void
728 addsym(struct syminfo *info, const Elf_Sym *sym, const char *name,
729 const char *mod)
730 {
731 int len, mlen;
732
733 #ifdef KSYMS_DEBUG
734 if (ksyms_debug & FOLLOW_MORE_CALLS)
735 printf("addsym: name %s val %lx\n", name, (long)sym->st_value);
736 #endif
737 len = strlen(name) + 1;
738 if (mod)
739 mlen = 1 + strlen(mod);
740 else
741 mlen = 0;
742 if (info->cursyms == info->maxsyms ||
743 (len + mlen + info->curnamep) > info->maxnamep) {
744 printf("addsym: too many symbols, skipping '%s'\n", name);
745 return;
746 }
747 strlcpy(&info->symnames[info->curnamep], name,
748 info->maxnamep - info->curnamep);
749 if (mlen) {
750 info->symnames[info->curnamep + len - 1] = '.';
751 strlcpy(&info->symnames[info->curnamep + len], mod,
752 info->maxnamep - (info->curnamep + len));
753 len += mlen;
754 }
755 info->syms[info->cursyms] = *sym;
756 info->syms[info->cursyms].st_name = info->curnamep;
757 info->symnmoff[info->cursyms] = info->curnamep;
758 info->curnamep += len;
759 #if NKSYMS
760 if (len > ksyms_maxlen)
761 ksyms_maxlen = len;
762 #endif
763 info->cursyms++;
764 }
765 /*
766 * Adds a symbol table.
767 * "name" is the module name, "start" and "size" is where the symbol table
768 * is located, and "type" is in which binary format the symbol table is.
769 * New memory for keeping the symbol table is allocated in this function.
770 * Returns 0 if success and EEXIST if the module name is in use.
771 */
772 static int
773 specialsym(const char *symname)
774 {
775 return !strcmp(symname, "_bss_start") ||
776 !strcmp(symname, "__bss_start") ||
777 !strcmp(symname, "_bss_end__") ||
778 !strcmp(symname, "__bss_end__") ||
779 !strcmp(symname, "_edata") ||
780 !strcmp(symname, "_end") ||
781 !strcmp(symname, "__end") ||
782 !strcmp(symname, "__end__") ||
783 !strncmp(symname, "__start_link_set_", 17) ||
784 !strncmp(symname, "__stop_link_set_", 16);
785 }
786
787 int
788 ksyms_addsymtab(const char *mod, void *symstart, vsize_t symsize,
789 char *strstart, vsize_t strsize)
790 {
791 Elf_Sym *sym = symstart;
792 struct symtab *st;
793 unsigned long rval;
794 int i;
795 char *name;
796 struct syminfo info;
797
798 #ifdef KSYMS_DEBUG
799 if (ksyms_debug & FOLLOW_CALLS)
800 printf("ksyms_addsymtab: mod %s symsize %lx strsize %lx\n",
801 mod, symsize, strsize);
802 #endif
803
804 #if NKSYMS
805 /*
806 * Do not try to add a symbol table while someone is reading
807 * from /dev/ksyms.
808 */
809 while (ksyms_isopen != 0)
810 tsleep(&ksyms_isopen, PWAIT, "ksyms", 0);
811 #endif
812
813 /* Check if this symtab already loaded */
814 CIRCLEQ_FOREACH(st, &symtab_queue, sd_queue) {
815 if (strcmp(mod, st->sd_name) == 0)
816 return EEXIST;
817 }
818
819 /*
820 * XXX - Only add a symbol if it do not exist already.
821 * This is because of a flaw in the current LKM implementation,
822 * these loops will be removed once the in-kernel linker is in place.
823 */
824 memset(&info, 0, sizeof(info));
825 for (i = 0; i < symsize/sizeof(Elf_Sym); i++) {
826 char * const symname = strstart + sym[i].st_name;
827 if (sym[i].st_name == 0)
828 continue; /* Just ignore */
829
830 /* check validity of the symbol */
831 /* XXX - save local symbols if DDB */
832 if (ELF_ST_BIND(sym[i].st_info) != STB_GLOBAL)
833 continue;
834
835 /* Check if the symbol exists */
836 if (ksyms_getval(NULL, symname, &rval, KSYMS_EXTERN) == 0) {
837 /* Check (and complain) about differing values */
838 if (sym[i].st_value != rval &&
839 sym[i].st_shndx != SHN_UNDEF) {
840 if (specialsym(symname)) {
841 info.maxsyms++;
842 info.maxnamep += strlen(symname) + 1 +
843 strlen(mod) + 1;
844 } else {
845 printf("%s: symbol '%s' redeclared with"
846 " different value (%lx != %lx)\n",
847 mod, symname,
848 rval, (long)sym[i].st_value);
849 }
850 }
851 } else {
852 /*
853 * Count this symbol
854 */
855 info.maxsyms++;
856 info.maxnamep += strlen(symname) + 1;
857 }
858 }
859
860 /*
861 * Now that we know the sizes, malloc the structures.
862 */
863 info.syms = malloc(sizeof(Elf_Sym)*info.maxsyms, M_DEVBUF, M_WAITOK);
864 info.symnames = malloc(info.maxnamep, M_DEVBUF, M_WAITOK);
865 info.symnmoff = malloc(sizeof(int)*info.maxsyms, M_DEVBUF, M_WAITOK);
866
867 /*
868 * Now that we have the symbols, actually fill in the structures.
869 */
870 for (i = 0; i < symsize/sizeof(Elf_Sym); i++) {
871 char * const symname = strstart + sym[i].st_name;
872 if (sym[i].st_name == 0)
873 continue; /* Just ignore */
874
875 /* check validity of the symbol */
876 /* XXX - save local symbols if DDB */
877 if (ELF_ST_BIND(sym[i].st_info) != STB_GLOBAL)
878 continue;
879
880 /* Check if the symbol exists */
881 if (ksyms_getval(NULL, symname, &rval, KSYMS_EXTERN) == 0) {
882 if ((sym[i].st_value != rval) && specialsym(symname)) {
883 addsym(&info, &sym[i], symname, mod);
884 }
885 } else
886 /* Ok, save this symbol */
887 addsym(&info, &sym[i], symname, NULL);
888 }
889
890 st = malloc(sizeof(struct symtab), M_DEVBUF, M_WAITOK);
891 i = strlen(mod) + 1;
892 name = malloc(i, M_DEVBUF, M_WAITOK);
893 strlcpy(name, mod, i);
894 st->sd_name = name;
895 st->sd_symnmoff = info.symnmoff;
896 st->sd_symstart = info.syms;
897 st->sd_symsize = sizeof(Elf_Sym)*info.maxsyms;
898 st->sd_strstart = info.symnames;
899 st->sd_strsize = info.maxnamep;
900 st->sd_minsym = NULL;
901 st->sd_maxsym = NULL;
902
903 /* Make them absolute references */
904 sym = st->sd_symstart;
905 for (i = 0; i < st->sd_symsize/sizeof(Elf_Sym); i++)
906 sym[i].st_shndx = SHN_ABS;
907
908 CIRCLEQ_INSERT_TAIL(&symtab_queue, st, sd_queue);
909 #if NKSYMS
910 ksyms_sizes_calc();
911 #endif
912 return 0;
913 }
914
915 /*
916 * Remove a symbol table specified by name.
917 * Returns 0 if success, EBUSY if device open and ENOENT if no such name.
918 */
919 int
920 ksyms_delsymtab(const char *mod)
921 {
922 struct symtab *st;
923 int found = 0;
924
925 #if NKSYMS
926 /*
927 * Do not try to delete a symbol table while someone is reading
928 * from /dev/ksyms.
929 */
930 while (ksyms_isopen != 0)
931 tsleep(&ksyms_isopen, PWAIT, "ksyms", 0);
932 #endif
933
934 CIRCLEQ_FOREACH(st, &symtab_queue, sd_queue) {
935 if (strcmp(mod, st->sd_name) == 0) {
936 found = 1;
937 break;
938 }
939 }
940 if (found == 0)
941 return ENOENT;
942 CIRCLEQ_REMOVE(&symtab_queue, st, sd_queue);
943 free(st->sd_symstart, M_DEVBUF);
944 free(st->sd_strstart, M_DEVBUF);
945 free(st->sd_symnmoff, M_DEVBUF);
946 /* XXXUNCONST LINTED - const castaway */
947 free(__UNCONST(st->sd_name), M_DEVBUF);
948 free(st, M_DEVBUF);
949 #if NKSYMS
950 ksyms_sizes_calc();
951 #endif
952 return 0;
953 }
954
955 int
956 ksyms_rensymtab(const char *old, const char *new)
957 {
958 struct symtab *st, *oldst = NULL;
959 char *newstr;
960
961 CIRCLEQ_FOREACH(st, &symtab_queue, sd_queue) {
962 if (strcmp(old, st->sd_name) == 0)
963 oldst = st;
964 if (strcmp(new, st->sd_name) == 0)
965 return (EEXIST);
966 }
967 if (oldst == NULL)
968 return (ENOENT);
969
970 newstr = malloc(strlen(new)+1, M_DEVBUF, M_WAITOK);
971 if (!newstr)
972 return (ENOMEM);
973 strcpy(newstr, new);
974 /*XXXUNCONST*/
975 free(__UNCONST(oldst->sd_name), M_DEVBUF);
976 oldst->sd_name = newstr;
977
978 return (0);
979 }
980
981 #ifdef DDB
982 /*
983 * Keep sifting stuff here, to avoid export of ksyms internals.
984 */
985 int
986 ksyms_sift(char *mod, char *sym, int mode)
987 {
988 struct symtab *st;
989 char *sb;
990 int i, sz;
991
992 if (ksymsinited == 0)
993 return ENOENT;
994
995 CIRCLEQ_FOREACH(st, &symtab_queue, sd_queue) {
996 if (mod && strcmp(mod, st->sd_name))
997 continue;
998 sb = st->sd_strstart;
999
1000 sz = st->sd_symsize/sizeof(Elf_Sym);
1001 for (i = 0; i < sz; i++) {
1002 Elf_Sym *les = st->sd_symstart + i;
1003 char c;
1004
1005 if (strstr(sb + les->st_name - st->sd_usroffset, sym)
1006 == NULL)
1007 continue;
1008
1009 if (mode == 'F') {
1010 switch (ELF_ST_TYPE(les->st_info)) {
1011 case STT_OBJECT:
1012 c = '+';
1013 break;
1014 case STT_FUNC:
1015 c = '*';
1016 break;
1017 case STT_SECTION:
1018 c = '&';
1019 break;
1020 case STT_FILE:
1021 c = '/';
1022 break;
1023 default:
1024 c = ' ';
1025 break;
1026 }
1027 db_printf("%s%c ", sb + les->st_name -
1028 st->sd_usroffset, c);
1029 } else
1030 db_printf("%s ", sb + les->st_name -
1031 st->sd_usroffset);
1032 }
1033 }
1034 return ENOENT;
1035 }
1036 #endif /* DDB */
1037
1038 #if NKSYMS
1039 /*
1040 * Static allocated ELF header.
1041 * Basic info is filled in at attach, sizes at open.
1042 */
1043 #define SYMTAB 1
1044 #define STRTAB 2
1045 #define SHSTRTAB 3
1046 #define NSECHDR 4
1047
1048 #define NPRGHDR 2
1049 #define SHSTRSIZ 28
1050
1051 static struct ksyms_hdr {
1052 Elf_Ehdr kh_ehdr;
1053 Elf_Phdr kh_phdr[NPRGHDR];
1054 Elf_Shdr kh_shdr[NSECHDR];
1055 char kh_strtab[SHSTRSIZ];
1056 } ksyms_hdr;
1057
1058
1059 static void
1060 ksyms_hdr_init(void *hdraddr)
1061 {
1062
1063 /* Copy the loaded elf exec header */
1064 memcpy(&ksyms_hdr.kh_ehdr, hdraddr, sizeof(Elf_Ehdr));
1065
1066 /* Set correct program/section header sizes, offsets and numbers */
1067 ksyms_hdr.kh_ehdr.e_phoff = offsetof(struct ksyms_hdr, kh_phdr[0]);
1068 ksyms_hdr.kh_ehdr.e_phentsize = sizeof(Elf_Phdr);
1069 ksyms_hdr.kh_ehdr.e_phnum = NPRGHDR;
1070 ksyms_hdr.kh_ehdr.e_shoff = offsetof(struct ksyms_hdr, kh_shdr[0]);
1071 ksyms_hdr.kh_ehdr.e_shentsize = sizeof(Elf_Shdr);
1072 ksyms_hdr.kh_ehdr.e_shnum = NSECHDR;
1073 ksyms_hdr.kh_ehdr.e_shstrndx = NSECHDR - 1; /* Last section */
1074
1075 /*
1076 * Keep program headers zeroed (unused).
1077 * The section headers are hand-crafted.
1078 * First section is section zero.
1079 */
1080
1081 /* Second section header; ".symtab" */
1082 ksyms_hdr.kh_shdr[SYMTAB].sh_name = 1; /* Section 3 offset */
1083 ksyms_hdr.kh_shdr[SYMTAB].sh_type = SHT_SYMTAB;
1084 ksyms_hdr.kh_shdr[SYMTAB].sh_offset = sizeof(struct ksyms_hdr);
1085 /* ksyms_hdr.kh_shdr[SYMTAB].sh_size = filled in at open */
1086 ksyms_hdr.kh_shdr[SYMTAB].sh_link = 2; /* Corresponding strtab */
1087 ksyms_hdr.kh_shdr[SYMTAB].sh_info = 0; /* XXX */
1088 ksyms_hdr.kh_shdr[SYMTAB].sh_addralign = sizeof(long);
1089 ksyms_hdr.kh_shdr[SYMTAB].sh_entsize = sizeof(Elf_Sym);
1090
1091 /* Third section header; ".strtab" */
1092 ksyms_hdr.kh_shdr[STRTAB].sh_name = 9; /* Section 3 offset */
1093 ksyms_hdr.kh_shdr[STRTAB].sh_type = SHT_STRTAB;
1094 /* ksyms_hdr.kh_shdr[STRTAB].sh_offset = filled in at open */
1095 /* ksyms_hdr.kh_shdr[STRTAB].sh_size = filled in at open */
1096 /* ksyms_hdr.kh_shdr[STRTAB].sh_link = kept zero */
1097 ksyms_hdr.kh_shdr[STRTAB].sh_info = 0;
1098 ksyms_hdr.kh_shdr[STRTAB].sh_addralign = sizeof(char);
1099 ksyms_hdr.kh_shdr[STRTAB].sh_entsize = 0;
1100
1101 /* Fourth section, ".shstrtab" */
1102 ksyms_hdr.kh_shdr[SHSTRTAB].sh_name = 17; /* This section name offset */
1103 ksyms_hdr.kh_shdr[SHSTRTAB].sh_type = SHT_STRTAB;
1104 ksyms_hdr.kh_shdr[SHSTRTAB].sh_offset =
1105 offsetof(struct ksyms_hdr, kh_strtab);
1106 ksyms_hdr.kh_shdr[SHSTRTAB].sh_size = SHSTRSIZ;
1107 ksyms_hdr.kh_shdr[SHSTRTAB].sh_addralign = sizeof(char);
1108
1109 /* Set section names */
1110 strlcpy(&ksyms_hdr.kh_strtab[1], ".symtab",
1111 sizeof(ksyms_hdr.kh_strtab) - 1);
1112 strlcpy(&ksyms_hdr.kh_strtab[9], ".strtab",
1113 sizeof(ksyms_hdr.kh_strtab) - 9);
1114 strlcpy(&ksyms_hdr.kh_strtab[17], ".shstrtab",
1115 sizeof(ksyms_hdr.kh_strtab) - 17);
1116 };
1117
1118 static int
1119 ksymsopen(dev_t dev, int oflags, int devtype, struct lwp *l)
1120 {
1121
1122 if (minor(dev))
1123 return ENXIO;
1124 if (ksymsinited == 0)
1125 return ENXIO;
1126
1127 ksyms_hdr.kh_shdr[SYMTAB].sh_size = symsz;
1128 ksyms_hdr.kh_shdr[STRTAB].sh_offset = symsz +
1129 ksyms_hdr.kh_shdr[SYMTAB].sh_offset;
1130 ksyms_hdr.kh_shdr[STRTAB].sh_size = strsz;
1131 ksyms_isopen = 1;
1132
1133 #ifdef KSYMS_DEBUG
1134 if (ksyms_debug & FOLLOW_DEVKSYMS)
1135 printf("ksymsopen: symsz 0x%x strsz 0x%x\n", symsz, strsz);
1136 #endif
1137
1138 return 0;
1139 }
1140
1141 static int
1142 ksymsclose(dev_t dev, int oflags, int devtype, struct lwp *l)
1143 {
1144
1145 #ifdef KSYMS_DEBUG
1146 if (ksyms_debug & FOLLOW_DEVKSYMS)
1147 printf("ksymsclose\n");
1148 #endif
1149
1150 ksyms_isopen = 0;
1151 wakeup(&ksyms_isopen);
1152 return 0;
1153 }
1154
1155 #define HDRSIZ sizeof(struct ksyms_hdr)
1156
1157 static int
1158 ksymsread(dev_t dev, struct uio *uio, int ioflag)
1159 {
1160 struct symtab *st;
1161 size_t filepos, inpos, off;
1162
1163 #ifdef KSYMS_DEBUG
1164 if (ksyms_debug & FOLLOW_DEVKSYMS)
1165 printf("ksymsread: offset 0x%llx resid 0x%zx\n",
1166 (long long)uio->uio_offset, uio->uio_resid);
1167 #endif
1168
1169 off = uio->uio_offset;
1170 if (off >= (strsz + symsz + HDRSIZ))
1171 return 0; /* End of symtab */
1172 /*
1173 * First: Copy out the ELF header.
1174 */
1175 if (off < HDRSIZ)
1176 uiomove((char *)&ksyms_hdr + off, HDRSIZ - off, uio);
1177
1178 /*
1179 * Copy out the symbol table.
1180 */
1181 filepos = HDRSIZ;
1182 CIRCLEQ_FOREACH(st, &symtab_queue, sd_queue) {
1183 if (uio->uio_resid == 0)
1184 return 0;
1185 if (uio->uio_offset <= st->sd_symsize + filepos) {
1186 inpos = uio->uio_offset - filepos;
1187 uiomove((char *)st->sd_symstart + inpos,
1188 st->sd_symsize - inpos, uio);
1189 }
1190 filepos += st->sd_symsize;
1191 }
1192
1193 if (filepos != HDRSIZ + symsz)
1194 panic("ksymsread: unsunc");
1195
1196 /*
1197 * Copy out the string table
1198 */
1199 CIRCLEQ_FOREACH(st, &symtab_queue, sd_queue) {
1200 if (uio->uio_resid == 0)
1201 return 0;
1202 if (uio->uio_offset <= st->sd_strsize + filepos) {
1203 inpos = uio->uio_offset - filepos;
1204 uiomove((char *)st->sd_strstart + inpos,
1205 st->sd_strsize - inpos, uio);
1206 }
1207 filepos += st->sd_strsize;
1208 }
1209 return 0;
1210 }
1211
1212 static int
1213 ksymswrite(dev_t dev, struct uio *uio, int ioflag)
1214 {
1215
1216 return EROFS;
1217 }
1218
1219 static int
1220 ksymsioctl(dev_t dev, u_long cmd, void *data, int fflag, struct lwp *l)
1221 {
1222 struct ksyms_gsymbol *kg = (struct ksyms_gsymbol *)data;
1223 struct symtab *st;
1224 Elf_Sym *sym = NULL;
1225 unsigned long val;
1226 int error = 0;
1227 char *str = NULL;
1228
1229 if (cmd == KIOCGVALUE || cmd == KIOCGSYMBOL)
1230 str = malloc(ksyms_maxlen, M_DEVBUF, M_WAITOK);
1231
1232 switch (cmd) {
1233 case KIOCGVALUE:
1234 /*
1235 * Use the in-kernel symbol lookup code for fast
1236 * retreival of a value.
1237 */
1238 if ((error = copyinstr(kg->kg_name, str, ksyms_maxlen, NULL)))
1239 break;
1240 if ((error = ksyms_getval(NULL, str, &val, KSYMS_EXTERN)))
1241 break;
1242 error = copyout(&val, kg->kg_value, sizeof(long));
1243 break;
1244
1245 case KIOCGSYMBOL:
1246 /*
1247 * Use the in-kernel symbol lookup code for fast
1248 * retreival of a symbol.
1249 */
1250 if ((error = copyinstr(kg->kg_name, str, ksyms_maxlen, NULL)))
1251 break;
1252 CIRCLEQ_FOREACH(st, &symtab_queue, sd_queue) {
1253 if ((sym = findsym(str, st)) == NULL) /* from userland */
1254 continue;
1255
1256 #ifdef notdef
1257 /* Skip if bad binding */
1258 if (ELF_ST_BIND(sym->st_info) != STB_GLOBAL) {
1259 sym = NULL;
1260 continue;
1261 }
1262 #endif
1263 break;
1264 }
1265 /*
1266 * XXX which value of sym->st_name should be returned? The real
1267 * one, or the one that matches what reading /dev/ksyms get?
1268 *
1269 * Currently, we're returning the /dev/ksyms one.
1270 */
1271 if (sym != NULL)
1272 error = copyout(sym, kg->kg_sym, sizeof(Elf_Sym));
1273 else
1274 error = ENOENT;
1275 break;
1276
1277 case KIOCGSIZE:
1278 /*
1279 * Get total size of symbol table.
1280 */
1281 *(int *)data = strsz + symsz + HDRSIZ;
1282 break;
1283
1284 default:
1285 error = ENOTTY;
1286 break;
1287 }
1288
1289 if (cmd == KIOCGVALUE || cmd == KIOCGSYMBOL)
1290 free(str, M_DEVBUF);
1291
1292 return error;
1293 }
1294
1295 const struct cdevsw ksyms_cdevsw = {
1296 ksymsopen, ksymsclose, ksymsread, ksymswrite, ksymsioctl,
1297 nullstop, notty, nopoll, nommap, nullkqfilter, DV_DULL
1298 };
1299 #endif /* NKSYMS */
1300