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