kern_ksyms.c revision 1.20 1 /* $NetBSD: kern_ksyms.c,v 1.20 2004/02/18 23:44:49 matt 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.20 2004/02/18 23:44:49 matt 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_usroffset; /* Real address for userspace */
125 int sd_symsize; /* Size in bytes of symbol table */
126 int sd_strsize; /* Size of string table */
127 int *sd_symnmoff; /* Used when calculating the name offset */
128 };
129
130 static CIRCLEQ_HEAD(, symtab) symtab_queue =
131 CIRCLEQ_HEAD_INITIALIZER(symtab_queue);
132
133 static struct symtab kernel_symtab;
134
135 #define USE_PTREE
136 #ifdef USE_PTREE
137 /*
138 * Patricia-tree-based lookup structure for the in-kernel global symbols.
139 * Based on a design by Mikael Sundstrom, msm (at) sm.luth.se.
140 */
141 struct ptree {
142 int16_t bitno;
143 int16_t lr[2];
144 } *symb;
145 static int16_t baseidx;
146 static int treex = 1;
147
148 #define P_BIT(key, bit) ((key[bit >> 3] >> (bit & 7)) & 1)
149 #define STRING(idx) kernel_symtab.sd_symstart[idx].st_name + \
150 kernel_symtab.sd_strstart
151
152 /*
153 * Walk down the tree until a terminal node is found.
154 */
155 static int
156 symbol_traverse(char *key)
157 {
158 int16_t nb, rbit = baseidx;
159
160 while (rbit > 0) {
161 nb = symb[rbit].bitno;
162 rbit = symb[rbit].lr[P_BIT(key, nb)];
163 }
164 return -rbit;
165 }
166
167 static int
168 ptree_add(char *key, int val)
169 {
170 int idx;
171 int nix, cix, bit, rbit, sb, lastrbit, svbit = 0, ix;
172 char *m, *k;
173
174 if (baseidx == 0) {
175 baseidx = -val;
176 return 0; /* First element */
177 }
178
179 /* Get string to match against */
180 idx = symbol_traverse(key);
181
182 /* Find first mismatching bit */
183 m = STRING(idx);
184 k = key;
185 if (strcmp(m, k) == 0)
186 return 1;
187
188 for (cix = 0; *m && *k && *m == *k; m++, k++, cix += 8)
189 ;
190 ix = ffs((int)*m ^ (int)*k) - 1;
191 cix += ix;
192
193 /* Create new node */
194 nix = treex++;
195 bit = P_BIT(key, cix);
196 symb[nix].bitno = cix;
197 symb[nix].lr[bit] = -val;
198
199 /* Find where to insert node */
200 rbit = baseidx;
201 lastrbit = 0;
202 for (;;) {
203 if (rbit < 0)
204 break;
205 sb = symb[rbit].bitno;
206 if (sb > cix)
207 break;
208 if (sb == cix)
209 printf("symb[rbit].bitno == cix!!!\n");
210 lastrbit = rbit;
211 svbit = P_BIT(key, sb);
212 rbit = symb[rbit].lr[svbit];
213 }
214
215 /* Do the actual insertion */
216 if (lastrbit == 0) {
217 /* first element */
218 symb[nix].lr[!bit] = baseidx;
219 baseidx = nix;
220 } else {
221 symb[nix].lr[!bit] = rbit;
222 symb[lastrbit].lr[svbit] = nix;
223 }
224 return 0;
225 }
226
227 static int
228 ptree_find(char *key)
229 {
230 int idx;
231
232 if (baseidx == 0)
233 return 0;
234 idx = symbol_traverse(key);
235
236 if (strcmp(key, STRING(idx)) == 0)
237 return idx;
238 return 0;
239 }
240
241 static void
242 ptree_gen(char *off, struct symtab *tab)
243 {
244 Elf_Sym *sym;
245 int i, nsym;
246
247 if (off != NULL)
248 symb = (struct ptree *)ALIGN(off);
249 else
250 symb = malloc((tab->sd_symsize/sizeof(Elf_Sym)) *
251 sizeof(struct ptree), M_DEVBUF, M_WAITOK);
252 symb--; /* sym index won't be 0 */
253
254 sym = tab->sd_symstart;
255 if ((nsym = tab->sd_symsize/sizeof(Elf_Sym)) > INT16_MAX) {
256 printf("Too many symbols for tree, skipping %d symbols\n",
257 nsym-INT16_MAX);
258 nsym = INT16_MAX;
259 }
260 for (i = 1; i < nsym; i++) {
261 if (ELF_ST_BIND(sym[i].st_info) != STB_GLOBAL)
262 continue;
263 ptree_add(tab->sd_strstart+sym[i].st_name, i);
264 }
265 }
266 #endif
267
268 /*
269 * Finds a certain symbol name in a certain symbol table.
270 */
271 static Elf_Sym *
272 findsym(char *name, struct symtab *table, int userreq)
273 {
274 Elf_Sym *start = table->sd_symstart;
275 int i, sz = table->sd_symsize/sizeof(Elf_Sym);
276 char *np;
277 caddr_t realstart = table->sd_strstart - (userreq ? 0 : table->sd_usroffset);
278
279 #ifdef USE_PTREE
280 if (table == &kernel_symtab && (i = ptree_find(name)) != 0)
281 return &start[i];
282 #endif
283
284 for (i = 0; i < sz; i++) {
285 np = realstart + start[i].st_name;
286 if (name[0] == np[0] && name[1] == np[1] &&
287 strcmp(name, np) == 0)
288 return &start[i];
289 }
290 return NULL;
291 }
292
293 /*
294 * The "attach" is in reality done in ksyms_init().
295 */
296 void ksymsattach(int);
297 void
298 ksymsattach(int arg)
299 {
300
301 #ifdef USE_PTREE
302 if (baseidx == 0)
303 ptree_gen(0, &kernel_symtab);
304 #endif
305
306 }
307
308 /*
309 * Add a symbol table named name.
310 * This is intended for use when the kernel loader enters the table.
311 */
312 static void
313 addsymtab(const char *name, Elf_Ehdr *ehdr, struct symtab *tab)
314 {
315 caddr_t start = (caddr_t)ehdr;
316 caddr_t send;
317 Elf_Shdr *shdr;
318 Elf_Sym *sym, *nsym;
319 int i, j, n, g;
320 char *str;
321
322 /* Find the symbol table and the corresponding string table. */
323 shdr = (Elf_Shdr *)(start + ehdr->e_shoff);
324 for (i = 1; i < ehdr->e_shnum; i++) {
325 if (shdr[i].sh_type != SHT_SYMTAB)
326 continue;
327 if (shdr[i].sh_offset == 0)
328 continue;
329 tab->sd_symstart = (Elf_Sym *)(start + shdr[i].sh_offset);
330 tab->sd_symsize = shdr[i].sh_size;
331 j = shdr[i].sh_link;
332 if (shdr[j].sh_offset == 0)
333 continue; /* Can this happen? */
334 tab->sd_strstart = start + shdr[j].sh_offset;
335 tab->sd_strsize = shdr[j].sh_size;
336 break;
337 }
338 tab->sd_name = name;
339 send = tab->sd_strstart + tab->sd_strsize;
340
341 #ifdef KSYMS_DEBUG
342 printf("start %p sym %p symsz %d str %p strsz %d send %p\n",
343 start, tab->sd_symstart, tab->sd_symsize,
344 tab->sd_strstart, tab->sd_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 *)start;
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 (ELF_ST_TYPE(sym[i].st_info) == STT_FILE)
366 continue; /* Skip filenames */
367 if (ELF_ST_TYPE(sym[i].st_info) == STT_NOTYPE &&
368 sym[i].st_value == 0 &&
369 strcmp(str + sym[i].st_name, "*ABS*") == 0)
370 continue; /* XXX */
371 if (ELF_ST_TYPE(sym[i].st_info) == STT_NOTYPE &&
372 strcmp(str + sym[i].st_name, "gcc2_compiled.") == 0)
373 continue; /* XXX */
374
375 #ifndef DDB
376 /* Only need global symbols */
377 if (ELF_ST_BIND(sym[i].st_info) != STB_GLOBAL)
378 continue;
379 #endif
380
381 /* Save symbol. Set it as an absolute offset */
382 nsym[n] = sym[i];
383 nsym[n].st_shndx = SHN_ABS;
384 if (ELF_ST_BIND(nsym[n].st_info) == STB_GLOBAL)
385 g++;
386 #if NKSYMS
387 j = strlen(nsym[n].st_name + tab->sd_strstart) + 1;
388 if (j > ksyms_maxlen)
389 ksyms_maxlen = j;
390 #endif
391 n++;
392
393 }
394 tab->sd_symstart = nsym;
395 tab->sd_symsize = n * sizeof(Elf_Sym);
396
397 #ifdef notyet
398 /*
399 * Remove left-over strings.
400 */
401 sym = tab->sd_symstart;
402 str = (caddr_t)tab->sd_symstart + tab->sd_symsize;
403 str[0] = 0;
404 n = 1;
405 for (i = 1; i < tab->sd_symsize/sizeof(Elf_Sym); i++) {
406 strcpy(str + n, tab->sd_strstart + sym[i].st_name);
407 sym[i].st_name = n;
408 n += strlen(str+n) + 1;
409 }
410 tab->sd_strstart = str;
411 tab->sd_strsize = n;
412
413 #ifdef KSYMS_DEBUG
414 printf("str %p strsz %d send %p\n", str, n, send);
415 #endif
416 #endif
417
418 CIRCLEQ_INSERT_HEAD(&symtab_queue, tab, sd_queue);
419
420 #ifdef notyet
421 #ifdef USE_PTREE
422 /* Try to use the freed space, if possible */
423 if (send - str - n > g * sizeof(struct ptree))
424 ptree_gen(str + n, tab);
425 #endif
426 #endif
427 }
428
429 /*
430 * Setup the kernel symbol table stuff.
431 */
432 void
433 ksyms_init(int symsize, void *start, void *end)
434 {
435 Elf_Ehdr *ehdr;
436
437 #ifdef SYMTAB_SPACE
438 if (symsize <= 0 &&
439 strncmp(db_symtab, SYMTAB_FILLER, sizeof(SYMTAB_FILLER))) {
440 symsize = db_symtabsize;
441 start = db_symtab;
442 end = db_symtab + db_symtabsize;
443 }
444 #endif
445 if (symsize <= 0) {
446 printf("[ Kernel symbol table missing! ]\n");
447 return;
448 }
449
450 /* Sanity check */
451 if (ALIGNED_POINTER(start, long) == 0) {
452 printf("[ Kernel symbol table has bad start address %p ]\n",
453 start);
454 return;
455 }
456
457 ehdr = (Elf_Ehdr *)start;
458
459 /* check if this is a valid ELF header */
460 /* No reason to verify arch type, the kernel is actually running! */
461 if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG) ||
462 ehdr->e_ident[EI_CLASS] != ELFCLASS ||
463 ehdr->e_version > 1) {
464 #ifdef notyet /* DDB */
465 if (ddb_init(symsize, start, end))
466 return; /* old-style symbol table */
467 #endif
468 printf("[ Kernel symbol table invalid! ]\n");
469 return; /* nothing to do */
470 }
471
472 #if NKSYMS
473 /* Loaded header will be scratched in addsymtab */
474 ksyms_hdr_init(start);
475 #endif
476
477 addsymtab("netbsd", ehdr, &kernel_symtab);
478
479 #if NKSYMS
480 ksyms_sizes_calc();
481 #endif
482
483 ksymsinited = 1;
484
485 #ifdef DEBUG
486 printf("Loaded initial symtab at %p, strtab at %p, # entries %ld\n",
487 kernel_symtab.sd_symstart, kernel_symtab.sd_strstart,
488 (long)kernel_symtab.sd_symsize/sizeof(Elf_Sym));
489 #endif
490 }
491
492 /*
493 * Get the value associated with a symbol.
494 * "mod" is the module name, or null if any module.
495 * "sym" is the symbol name.
496 * "val" is a pointer to the corresponding value, if call succeeded.
497 * Returns 0 if success or ENOENT if no such entry.
498 */
499 int
500 ksyms_getval(const char *mod, char *sym, unsigned long *val, int type, int userreq)
501 {
502 struct symtab *st;
503 Elf_Sym *es;
504
505 if (ksymsinited == 0)
506 return ENOENT;
507
508 #ifdef KSYMS_DEBUG
509 if (ksyms_debug & FOLLOW_CALLS)
510 printf("ksyms_getval: mod %s sym %s valp %p\n", mod, sym, val);
511 #endif
512
513 CIRCLEQ_FOREACH(st, &symtab_queue, sd_queue) {
514 if (mod && strcmp(st->sd_name, mod))
515 continue;
516 if ((es = findsym(sym, st, userreq)) == NULL)
517 continue;
518
519 /* Skip if bad binding */
520 if (type == KSYMS_EXTERN &&
521 ELF_ST_BIND(es->st_info) != STB_GLOBAL)
522 continue;
523
524 if (val)
525 *val = es->st_value;
526 return 0;
527 }
528 return ENOENT;
529 }
530
531 /*
532 * Get "mod" and "symbol" associated with an address.
533 * Returns 0 if success or ENOENT if no such entry.
534 */
535 int
536 ksyms_getname(const char **mod, char **sym, vaddr_t v, int f)
537 {
538 struct symtab *st;
539 Elf_Sym *les, *es = NULL;
540 vaddr_t laddr = 0;
541 const char *lmod = NULL;
542 char *stable = NULL;
543 int type, i, sz;
544
545 if (ksymsinited == 0)
546 return ENOENT;
547
548 CIRCLEQ_FOREACH(st, &symtab_queue, sd_queue) {
549 sz = st->sd_symsize/sizeof(Elf_Sym);
550 for (i = 0; i < sz; i++) {
551 les = st->sd_symstart + i;
552 type = ELF_ST_TYPE(les->st_info);
553
554 if ((f & KSYMS_PROC) && (type != STT_FUNC))
555 continue;
556
557 if (type == STT_NOTYPE)
558 continue;
559
560 if (((f & KSYMS_ANY) == 0) &&
561 (type != STT_FUNC) && (type != STT_OBJECT))
562 continue;
563
564 if ((les->st_value <= v) && (les->st_value > laddr)) {
565 laddr = les->st_value;
566 es = les;
567 lmod = st->sd_name;
568 stable = st->sd_strstart - st->sd_usroffset;
569 }
570 }
571 }
572 if (es == NULL)
573 return ENOENT;
574 if ((f & KSYMS_EXACT) && (v != es->st_value))
575 return ENOENT;
576 if (mod)
577 *mod = lmod;
578 if (sym)
579 *sym = stable + es->st_name;
580 return 0;
581 }
582
583 #if NKSYMS
584 static int symsz, strsz;
585
586 static void
587 ksyms_sizes_calc(void)
588 {
589 struct symtab *st;
590 int i;
591
592 symsz = strsz = 0;
593 CIRCLEQ_FOREACH(st, &symtab_queue, sd_queue) {
594 if (st != &kernel_symtab) {
595 for (i = 0; i < st->sd_symsize/sizeof(Elf_Sym); i++)
596 st->sd_symstart[i].st_name =
597 strsz + st->sd_symnmoff[i];
598 st->sd_usroffset = strsz;
599 }
600 symsz += st->sd_symsize;
601 strsz += st->sd_strsize;
602 }
603 }
604 #endif
605
606 /*
607 * Temporary work structure for dynamic loaded symbol tables.
608 * Will go away when in-kernel linker is in place.
609 */
610
611 struct syminfo {
612 size_t cursyms;
613 size_t curnamep;
614 size_t maxsyms;
615 size_t maxnamep;
616 Elf_Sym *syms;
617 int *symnmoff;
618 char *symnames;
619 };
620
621
622 /*
623 * Add a symbol to the temporary save area for symbols.
624 * This routine will go away when the in-kernel linker is in place.
625 */
626 static void
627 addsym(struct syminfo *info, const Elf_Sym *sym, const char *name,
628 const char *mod)
629 {
630 int len, mlen;
631
632 #ifdef KSYMS_DEBUG
633 if (ksyms_debug & FOLLOW_MORE_CALLS)
634 printf("addsym: name %s val %lx\n", name, (long)sym->st_value);
635 #endif
636 len = strlen(name) + 1;
637 if (mod)
638 mlen = 1 + strlen(mod);
639 else
640 mlen = 0;
641 if (info->cursyms == info->maxsyms ||
642 (len + mlen + info->curnamep) > info->maxnamep) {
643 printf("addsym: too many symbols, skipping '%s'\n", name);
644 return;
645 }
646 strlcpy(&info->symnames[info->curnamep], name,
647 info->maxnamep - info->curnamep);
648 if (mlen) {
649 info->symnames[info->curnamep + len - 1] = '.';
650 strlcpy(&info->symnames[info->curnamep + len], mod,
651 info->maxnamep - (info->curnamep + len));
652 len += mlen;
653 }
654 info->syms[info->cursyms] = *sym;
655 info->syms[info->cursyms].st_name = info->curnamep;
656 info->symnmoff[info->cursyms] = info->curnamep;
657 info->curnamep += len;
658 #if NKSYMS
659 if (len > ksyms_maxlen)
660 ksyms_maxlen = len;
661 #endif
662 info->cursyms++;
663 }
664 /*
665 * Adds a symbol table.
666 * "name" is the module name, "start" and "size" is where the symbol table
667 * is located, and "type" is in which binary format the symbol table is.
668 * New memory for keeping the symbol table is allocated in this function.
669 * Returns 0 if success and EEXIST if the module name is in use.
670 */
671 int
672 ksyms_addsymtab(const char *mod, void *symstart, vsize_t symsize,
673 char *strstart, vsize_t strsize)
674 {
675 Elf_Sym *sym = symstart;
676 struct symtab *st;
677 unsigned long rval;
678 int i;
679 char *name;
680 struct syminfo info;
681
682 #ifdef KSYMS_DEBUG
683 if (ksyms_debug & FOLLOW_CALLS)
684 printf("ksyms_addsymtab: mod %s symsize %lx strsize %lx\n",
685 mod, symsize, strsize);
686 #endif
687
688 #if NKSYMS
689 /*
690 * Do not try to add a symbol table while someone is reading
691 * from /dev/ksyms.
692 */
693 while (ksyms_isopen != 0)
694 tsleep(&ksyms_isopen, PWAIT, "ksyms", 0);
695 #endif
696
697 /* Check if this symtab already loaded */
698 CIRCLEQ_FOREACH(st, &symtab_queue, sd_queue) {
699 if (strcmp(mod, st->sd_name) == 0)
700 return EEXIST;
701 }
702
703 /*
704 * XXX - Only add a symbol if it do not exist already.
705 * This is because of a flaw in the current LKM implementation,
706 * these loops will be removed once the in-kernel linker is in place.
707 */
708 memset(&info, 0, sizeof(info));
709 for (i = 0; i < symsize/sizeof(Elf_Sym); i++) {
710 char * const symname = strstart + sym[i].st_name;
711 if (sym[i].st_name == 0)
712 continue; /* Just ignore */
713
714 /* check validity of the symbol */
715 /* XXX - save local symbols if DDB */
716 if (ELF_ST_BIND(sym[i].st_info) != STB_GLOBAL)
717 continue;
718
719 /* Check if the symbol exists */
720 if (ksyms_getval_from_kernel(NULL, symname,
721 &rval, KSYMS_EXTERN) == 0) {
722 /* Check (and complain) about differing values */
723 if (sym[i].st_value != rval) {
724 if (!strcmp(symname, "__bss_start") ||
725 !strcmp(symname, "_edata") ||
726 !strcmp(symname, "_end") ||
727 !strncmp(symname, "__start_link_set_",17) ||
728 !strncmp(symname, "__stop_link_set_",16)) {
729 info.maxsyms++;
730 info.maxnamep += strlen(symname) + 1 +
731 strlen(mod) + 1;
732 } else {
733 printf("%s: symbol '%s' redeclared with"
734 " different value (%lx != %lx)\n",
735 mod, symname,
736 rval, (long)sym[i].st_value);
737 }
738 }
739 } else {
740 /*
741 * Count this symbol
742 */
743 info.maxsyms++;
744 info.maxnamep += strlen(symname) + 1;
745 }
746 }
747
748 /*
749 * Now that we know the sizes, malloc the structures.
750 */
751 info.syms = malloc(sizeof(Elf_Sym)*info.maxsyms, M_DEVBUF, M_WAITOK);
752 info.symnames = malloc(info.maxnamep, M_DEVBUF, M_WAITOK);
753 info.symnmoff = malloc(sizeof(int)*info.maxsyms, M_DEVBUF, M_WAITOK);
754
755 /*
756 * Now that we have the symbols, actually fill in the structures.
757 */
758 for (i = 0; i < symsize/sizeof(Elf_Sym); i++) {
759 char * const symname = strstart + sym[i].st_name;
760 if (sym[i].st_name == 0)
761 continue; /* Just ignore */
762
763 /* check validity of the symbol */
764 /* XXX - save local symbols if DDB */
765 if (ELF_ST_BIND(sym[i].st_info) != STB_GLOBAL)
766 continue;
767
768 /* Check if the symbol exists */
769 if (ksyms_getval_from_kernel(NULL, symname,
770 &rval, KSYMS_EXTERN) == 0) {
771 if ((sym[i].st_value != rval) &&
772 (!strcmp(symname, "__bss_start") ||
773 !strcmp(symname, "_edata") ||
774 !strcmp(symname, "_end") ||
775 !strncmp(symname, "__start_link_set_",17) ||
776 !strncmp(symname, "__stop_link_set_",16))) {
777 addsym(&info, &sym[i], symname, mod);
778 }
779 } else
780 /* Ok, save this symbol */
781 addsym(&info, &sym[i], symname, NULL);
782 }
783
784 st = malloc(sizeof(struct symtab), M_DEVBUF, M_WAITOK);
785 i = strlen(mod) + 1;
786 name = malloc(i, M_DEVBUF, M_WAITOK);
787 strlcpy(name, mod, i);
788 st->sd_name = name;
789 st->sd_symnmoff = info.symnmoff;
790 st->sd_symstart = info.syms;
791 st->sd_symsize = sizeof(Elf_Sym)*info.maxsyms;
792 st->sd_strstart = info.symnames;
793 st->sd_strsize = info.maxnamep;
794
795 /* Make them absolute references */
796 sym = st->sd_symstart;
797 for (i = 0; i < st->sd_symsize/sizeof(Elf_Sym); i++)
798 sym[i].st_shndx = SHN_ABS;
799
800 CIRCLEQ_INSERT_TAIL(&symtab_queue, st, sd_queue);
801 #if NKSYMS
802 ksyms_sizes_calc();
803 #endif
804 return 0;
805 }
806
807 /*
808 * Remove a symbol table specified by name.
809 * Returns 0 if success, EBUSY if device open and ENOENT if no such name.
810 */
811 int
812 ksyms_delsymtab(const char *mod)
813 {
814 struct symtab *st;
815 int found = 0;
816
817 #if NKSYMS
818 /*
819 * Do not try to delete a symbol table while someone is reading
820 * from /dev/ksyms.
821 */
822 while (ksyms_isopen != 0)
823 tsleep(&ksyms_isopen, PWAIT, "ksyms", 0);
824 #endif
825
826 CIRCLEQ_FOREACH(st, &symtab_queue, sd_queue) {
827 if (strcmp(mod, st->sd_name) == 0) {
828 found = 1;
829 break;
830 }
831 }
832 if (found == 0)
833 return ENOENT;
834 CIRCLEQ_REMOVE(&symtab_queue, st, sd_queue);
835 free(st->sd_symstart, M_DEVBUF);
836 free(st->sd_strstart, M_DEVBUF);
837 free(st->sd_symnmoff, M_DEVBUF);
838 /* LINTED - const castaway */
839 free((void *)st->sd_name, M_DEVBUF);
840 free(st, M_DEVBUF);
841 #if NKSYMS
842 ksyms_sizes_calc();
843 #endif
844 return 0;
845 }
846
847 int
848 ksyms_rensymtab(const char *old, const char *new)
849 {
850 struct symtab *st, *oldst = NULL;
851 char *newstr;
852
853 CIRCLEQ_FOREACH(st, &symtab_queue, sd_queue) {
854 if (strcmp(old, st->sd_name) == 0)
855 oldst = st;
856 if (strcmp(new, st->sd_name) == 0)
857 return (EEXIST);
858 }
859 if (oldst == NULL)
860 return (ENOENT);
861
862 newstr = malloc(strlen(new)+1, M_DEVBUF, M_WAITOK);
863 if (!newstr)
864 return (ENOMEM);
865 strcpy(newstr, new);
866 free((char *)oldst->sd_name, M_DEVBUF);
867 oldst->sd_name = newstr;
868
869 return (0);
870 }
871
872 #ifdef DDB
873
874 /*
875 * Keep sifting stuff here, to avoid export of ksyms internals.
876 */
877 int
878 ksyms_sift(char *mod, char *sym, int mode)
879 {
880 struct symtab *st;
881 char *sb;
882 int i, sz;
883
884 if (ksymsinited == 0)
885 return ENOENT;
886
887 CIRCLEQ_FOREACH(st, &symtab_queue, sd_queue) {
888 if (mod && strcmp(mod, st->sd_name))
889 continue;
890 sb = st->sd_strstart;
891
892 sz = st->sd_symsize/sizeof(Elf_Sym);
893 for (i = 0; i < sz; i++) {
894 Elf_Sym *les = st->sd_symstart + i;
895 char c;
896
897 if (strstr(sb + les->st_name - st->sd_usroffset, sym)
898 == NULL)
899 continue;
900
901 if (mode == 'F') {
902 switch (ELF_ST_TYPE(les->st_info)) {
903 case STT_OBJECT:
904 c = '+';
905 break;
906 case STT_FUNC:
907 c = '*';
908 break;
909 case STT_SECTION:
910 c = '&';
911 break;
912 case STT_FILE:
913 c = '/';
914 break;
915 default:
916 c = ' ';
917 break;
918 }
919 db_printf("%s%c ", sb + les->st_name -
920 st->sd_usroffset, c);
921 } else
922 db_printf("%s ", sb + les->st_name -
923 st->sd_usroffset);
924 }
925 }
926 return ENOENT;
927 }
928 #endif
929
930 #if NKSYMS
931
932 /*
933 * Static allocated ELF header.
934 * Basic info is filled in at attach, sizes at open.
935 */
936 #define SYMTAB 1
937 #define STRTAB 2
938 #define SHSTRTAB 3
939 #define NSECHDR 4
940
941 #define NPRGHDR 2
942 #define SHSTRSIZ 28
943
944 static struct ksyms_hdr {
945 Elf_Ehdr kh_ehdr;
946 Elf_Phdr kh_phdr[NPRGHDR];
947 Elf_Shdr kh_shdr[NSECHDR];
948 char kh_strtab[SHSTRSIZ];
949 } ksyms_hdr;
950
951
952 void
953 ksyms_hdr_init(caddr_t hdraddr)
954 {
955
956 /* Copy the loaded elf exec header */
957 memcpy(&ksyms_hdr.kh_ehdr, hdraddr, sizeof(Elf_Ehdr));
958
959 /* Set correct program/section header sizes, offsets and numbers */
960 ksyms_hdr.kh_ehdr.e_phoff = offsetof(struct ksyms_hdr, kh_phdr[0]);
961 ksyms_hdr.kh_ehdr.e_phentsize = sizeof(Elf_Phdr);
962 ksyms_hdr.kh_ehdr.e_phnum = NPRGHDR;
963 ksyms_hdr.kh_ehdr.e_shoff = offsetof(struct ksyms_hdr, kh_shdr[0]);
964 ksyms_hdr.kh_ehdr.e_shentsize = sizeof(Elf_Shdr);
965 ksyms_hdr.kh_ehdr.e_shnum = NSECHDR;
966 ksyms_hdr.kh_ehdr.e_shstrndx = NSECHDR - 1; /* Last section */
967
968 /*
969 * Keep program headers zeroed (unused).
970 * The section headers are hand-crafted.
971 * First section is section zero.
972 */
973
974 /* Second section header; ".symtab" */
975 ksyms_hdr.kh_shdr[SYMTAB].sh_name = 1; /* Section 3 offset */
976 ksyms_hdr.kh_shdr[SYMTAB].sh_type = SHT_SYMTAB;
977 ksyms_hdr.kh_shdr[SYMTAB].sh_offset = sizeof(struct ksyms_hdr);
978 /* ksyms_hdr.kh_shdr[SYMTAB].sh_size = filled in at open */
979 ksyms_hdr.kh_shdr[SYMTAB].sh_link = 2; /* Corresponding strtab */
980 ksyms_hdr.kh_shdr[SYMTAB].sh_info = 0; /* XXX */
981 ksyms_hdr.kh_shdr[SYMTAB].sh_addralign = sizeof(long);
982 ksyms_hdr.kh_shdr[SYMTAB].sh_entsize = sizeof(Elf_Sym);
983
984 /* Third section header; ".strtab" */
985 ksyms_hdr.kh_shdr[STRTAB].sh_name = 9; /* Section 3 offset */
986 ksyms_hdr.kh_shdr[STRTAB].sh_type = SHT_STRTAB;
987 /* ksyms_hdr.kh_shdr[STRTAB].sh_offset = filled in at open */
988 /* ksyms_hdr.kh_shdr[STRTAB].sh_size = filled in at open */
989 /* ksyms_hdr.kh_shdr[STRTAB].sh_link = kept zero */
990 ksyms_hdr.kh_shdr[STRTAB].sh_info = 0;
991 ksyms_hdr.kh_shdr[STRTAB].sh_addralign = sizeof(char);
992 ksyms_hdr.kh_shdr[STRTAB].sh_entsize = 0;
993
994 /* Fourth section, ".shstrtab" */
995 ksyms_hdr.kh_shdr[SHSTRTAB].sh_name = 17; /* This section name offset */
996 ksyms_hdr.kh_shdr[SHSTRTAB].sh_type = SHT_STRTAB;
997 ksyms_hdr.kh_shdr[SHSTRTAB].sh_offset =
998 offsetof(struct ksyms_hdr, kh_strtab);
999 ksyms_hdr.kh_shdr[SHSTRTAB].sh_size = SHSTRSIZ;
1000 ksyms_hdr.kh_shdr[SHSTRTAB].sh_addralign = sizeof(char);
1001
1002 /* Set section names */
1003 strlcpy(&ksyms_hdr.kh_strtab[1], ".symtab",
1004 sizeof(ksyms_hdr.kh_strtab) - 1);
1005 strlcpy(&ksyms_hdr.kh_strtab[9], ".strtab",
1006 sizeof(ksyms_hdr.kh_strtab) - 9);
1007 strlcpy(&ksyms_hdr.kh_strtab[17], ".shstrtab",
1008 sizeof(ksyms_hdr.kh_strtab) - 17);
1009 };
1010
1011 int
1012 ksymsopen(dev_t dev, int oflags, int devtype, struct proc *p)
1013 {
1014
1015 if (minor(dev))
1016 return ENXIO;
1017 if (ksymsinited == 0)
1018 return ENXIO;
1019
1020 ksyms_hdr.kh_shdr[SYMTAB].sh_size = symsz;
1021 ksyms_hdr.kh_shdr[STRTAB].sh_offset = symsz +
1022 ksyms_hdr.kh_shdr[SYMTAB].sh_offset;
1023 ksyms_hdr.kh_shdr[STRTAB].sh_size = strsz;
1024 ksyms_isopen = 1;
1025
1026 #ifdef KSYMS_DEBUG
1027 if (ksyms_debug & FOLLOW_DEVKSYMS)
1028 printf("ksymsopen: symsz 0x%x strsz 0x%x\n", symsz, strsz);
1029 #endif
1030
1031 return 0;
1032 }
1033
1034 int
1035 ksymsclose(dev_t dev, int oflags, int devtype, struct proc *p)
1036 {
1037
1038 #ifdef KSYMS_DEBUG
1039 if (ksyms_debug & FOLLOW_DEVKSYMS)
1040 printf("ksymsclose\n");
1041 #endif
1042
1043 ksyms_isopen = 0;
1044 wakeup(&ksyms_isopen);
1045 return 0;
1046 }
1047
1048 #define HDRSIZ sizeof(struct ksyms_hdr)
1049
1050 int
1051 ksymsread(dev_t dev, struct uio *uio, int ioflag)
1052 {
1053 struct symtab *st;
1054 size_t filepos, inpos, off;
1055
1056 #ifdef KSYMS_DEBUG
1057 if (ksyms_debug & FOLLOW_DEVKSYMS)
1058 printf("ksymsread: offset 0x%llx resid 0x%lx\n",
1059 (long long)uio->uio_offset, uio->uio_resid);
1060 #endif
1061
1062 off = uio->uio_offset;
1063 if (off >= (strsz + symsz + HDRSIZ))
1064 return 0; /* End of symtab */
1065 /*
1066 * First: Copy out the ELF header.
1067 */
1068 if (off < HDRSIZ)
1069 uiomove((char *)&ksyms_hdr + off, HDRSIZ - off, uio);
1070
1071 /*
1072 * Copy out the symbol table.
1073 */
1074 filepos = HDRSIZ;
1075 CIRCLEQ_FOREACH(st, &symtab_queue, sd_queue) {
1076 if (uio->uio_resid == 0)
1077 return 0;
1078 if (uio->uio_offset <= st->sd_symsize + filepos) {
1079 inpos = uio->uio_offset - filepos;
1080 uiomove((char *)st->sd_symstart + inpos,
1081 st->sd_symsize - inpos, uio);
1082 }
1083 filepos += st->sd_symsize;
1084 }
1085
1086 if (filepos != HDRSIZ + symsz)
1087 panic("ksymsread: unsunc");
1088
1089 /*
1090 * Copy out the string table
1091 */
1092 CIRCLEQ_FOREACH(st, &symtab_queue, sd_queue) {
1093 if (uio->uio_resid == 0)
1094 return 0;
1095 if (uio->uio_offset <= st->sd_strsize + filepos) {
1096 inpos = uio->uio_offset - filepos;
1097 uiomove((char *)st->sd_strstart + inpos,
1098 st->sd_strsize - inpos, uio);
1099 }
1100 filepos += st->sd_strsize;
1101 }
1102 return 0;
1103 }
1104
1105 int
1106 ksymswrite(dev_t dev, struct uio *uio, int ioflag)
1107 {
1108 return EROFS;
1109 }
1110
1111 int
1112 ksymsioctl(dev_t dev, u_long cmd, caddr_t data, int fflag, struct proc *p)
1113 {
1114 struct ksyms_gsymbol *kg = (struct ksyms_gsymbol *)data;
1115 struct symtab *st;
1116 Elf_Sym *sym = NULL;
1117 unsigned long val;
1118 int error = 0;
1119 char *str = NULL;
1120
1121 if (cmd == KIOCGVALUE || cmd == KIOCGSYMBOL)
1122 str = malloc(ksyms_maxlen, M_DEVBUF, M_WAITOK);
1123
1124 switch (cmd) {
1125 case KIOCGVALUE:
1126 /*
1127 * Use the in-kernel symbol lookup code for fast
1128 * retreival of a value.
1129 */
1130 if ((error = copyinstr(kg->kg_name, str, ksyms_maxlen, NULL)))
1131 break;
1132 if ((error = ksyms_getval_from_userland(NULL, str, &val, KSYMS_EXTERN)))
1133 break;
1134 error = copyout(&val, kg->kg_value, sizeof(long));
1135 break;
1136
1137 case KIOCGSYMBOL:
1138 /*
1139 * Use the in-kernel symbol lookup code for fast
1140 * retreival of a symbol.
1141 */
1142 if ((error = copyinstr(kg->kg_name, str, ksyms_maxlen, NULL)))
1143 break;
1144 CIRCLEQ_FOREACH(st, &symtab_queue, sd_queue) {
1145 if ((sym = findsym(str, st, 1)) == NULL) /* from userland */
1146 continue;
1147
1148 /* Skip if bad binding */
1149 if (ELF_ST_BIND(sym->st_info) != STB_GLOBAL) {
1150 sym = NULL;
1151 continue;
1152 }
1153 break;
1154 }
1155 if (sym != NULL)
1156 error = copyout(sym, kg->kg_sym, sizeof(Elf_Sym));
1157 else
1158 error = ENOENT;
1159 break;
1160
1161 case KIOCGSIZE:
1162 /*
1163 * Get total size of symbol table.
1164 */
1165 *(int *)data = strsz + symsz + HDRSIZ;
1166 break;
1167
1168 default:
1169 error = ENOTTY;
1170 break;
1171 }
1172
1173 if (cmd == KIOCGVALUE || cmd == KIOCGSYMBOL)
1174 free(str, M_DEVBUF);
1175
1176 return error;
1177 }
1178 #endif
1179