FreeBSD/Linux Kernel Cross Reference
sys/kern/kern_ksyms.c
1 /* $NetBSD: kern_ksyms.c,v 1.21 2004/02/19 03:42:01 matt Exp $ */
2 /*
3 * Copyright (c) 2001, 2003 Anders Magnusson (ragge@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.21 2004/02/19 03:42:01 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@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 static int
672 specialsym(const char *symname)
673 {
674 return !strcmp(symname, "_bss_start") ||
675 !strcmp(symname, "__bss_start") ||
676 !strcmp(symname, "_bss_end__") ||
677 !strcmp(symname, "__bss_end__") ||
678 !strcmp(symname, "_edata") ||
679 !strcmp(symname, "_end") ||
680 !strcmp(symname, "__end") ||
681 !strcmp(symname, "__end__") ||
682 !strncmp(symname, "__start_link_set_", 17) ||
683 !strncmp(symname, "__stop_link_set_", 16);
684 }
685
686 int
687 ksyms_addsymtab(const char *mod, void *symstart, vsize_t symsize,
688 char *strstart, vsize_t strsize)
689 {
690 Elf_Sym *sym = symstart;
691 struct symtab *st;
692 unsigned long rval;
693 int i;
694 char *name;
695 struct syminfo info;
696
697 #ifdef KSYMS_DEBUG
698 if (ksyms_debug & FOLLOW_CALLS)
699 printf("ksyms_addsymtab: mod %s symsize %lx strsize %lx\n",
700 mod, symsize, strsize);
701 #endif
702
703 #if NKSYMS
704 /*
705 * Do not try to add a symbol table while someone is reading
706 * from /dev/ksyms.
707 */
708 while (ksyms_isopen != 0)
709 tsleep(&ksyms_isopen, PWAIT, "ksyms", 0);
710 #endif
711
712 /* Check if this symtab already loaded */
713 CIRCLEQ_FOREACH(st, &symtab_queue, sd_queue) {
714 if (strcmp(mod, st->sd_name) == 0)
715 return EEXIST;
716 }
717
718 /*
719 * XXX - Only add a symbol if it do not exist already.
720 * This is because of a flaw in the current LKM implementation,
721 * these loops will be removed once the in-kernel linker is in place.
722 */
723 memset(&info, 0, sizeof(info));
724 for (i = 0; i < symsize/sizeof(Elf_Sym); i++) {
725 char * const symname = strstart + sym[i].st_name;
726 if (sym[i].st_name == 0)
727 continue; /* Just ignore */
728
729 /* check validity of the symbol */
730 /* XXX - save local symbols if DDB */
731 if (ELF_ST_BIND(sym[i].st_info) != STB_GLOBAL)
732 continue;
733
734 /* Check if the symbol exists */
735 if (ksyms_getval_from_kernel(NULL, symname,
736 &rval, KSYMS_EXTERN) == 0) {
737 /* Check (and complain) about differing values */
738 if (sym[i].st_value != rval) {
739 if (specialsym(symname)) {
740 info.maxsyms++;
741 info.maxnamep += strlen(symname) + 1 +
742 strlen(mod) + 1;
743 } else {
744 printf("%s: symbol '%s' redeclared with"
745 " different value (%lx != %lx)\n",
746 mod, symname,
747 rval, (long)sym[i].st_value);
748 }
749 }
750 } else {
751 /*
752 * Count this symbol
753 */
754 info.maxsyms++;
755 info.maxnamep += strlen(symname) + 1;
756 }
757 }
758
759 /*
760 * Now that we know the sizes, malloc the structures.
761 */
762 info.syms = malloc(sizeof(Elf_Sym)*info.maxsyms, M_DEVBUF, M_WAITOK);
763 info.symnames = malloc(info.maxnamep, M_DEVBUF, M_WAITOK);
764 info.symnmoff = malloc(sizeof(int)*info.maxsyms, M_DEVBUF, M_WAITOK);
765
766 /*
767 * Now that we have the symbols, actually fill in the structures.
768 */
769 for (i = 0; i < symsize/sizeof(Elf_Sym); i++) {
770 char * const symname = strstart + sym[i].st_name;
771 if (sym[i].st_name == 0)
772 continue; /* Just ignore */
773
774 /* check validity of the symbol */
775 /* XXX - save local symbols if DDB */
776 if (ELF_ST_BIND(sym[i].st_info) != STB_GLOBAL)
777 continue;
778
779 /* Check if the symbol exists */
780 if (ksyms_getval_from_kernel(NULL, symname,
781 &rval, KSYMS_EXTERN) == 0) {
782 if ((sym[i].st_value != rval) && specialsym(symname)) {
783 addsym(&info, &sym[i], symname, mod);
784 }
785 } else
786 /* Ok, save this symbol */
787 addsym(&info, &sym[i], symname, NULL);
788 }
789
790 st = malloc(sizeof(struct symtab), M_DEVBUF, M_WAITOK);
791 i = strlen(mod) + 1;
792 name = malloc(i, M_DEVBUF, M_WAITOK);
793 strlcpy(name, mod, i);
794 st->sd_name = name;
795 st->sd_symnmoff = info.symnmoff;
796 st->sd_symstart = info.syms;
797 st->sd_symsize = sizeof(Elf_Sym)*info.maxsyms;
798 st->sd_strstart = info.symnames;
799 st->sd_strsize = info.maxnamep;
800
801 /* Make them absolute references */
802 sym = st->sd_symstart;
803 for (i = 0; i < st->sd_symsize/sizeof(Elf_Sym); i++)
804 sym[i].st_shndx = SHN_ABS;
805
806 CIRCLEQ_INSERT_TAIL(&symtab_queue, st, sd_queue);
807 #if NKSYMS
808 ksyms_sizes_calc();
809 #endif
810 return 0;
811 }
812
813 /*
814 * Remove a symbol table specified by name.
815 * Returns 0 if success, EBUSY if device open and ENOENT if no such name.
816 */
817 int
818 ksyms_delsymtab(const char *mod)
819 {
820 struct symtab *st;
821 int found = 0;
822
823 #if NKSYMS
824 /*
825 * Do not try to delete a symbol table while someone is reading
826 * from /dev/ksyms.
827 */
828 while (ksyms_isopen != 0)
829 tsleep(&ksyms_isopen, PWAIT, "ksyms", 0);
830 #endif
831
832 CIRCLEQ_FOREACH(st, &symtab_queue, sd_queue) {
833 if (strcmp(mod, st->sd_name) == 0) {
834 found = 1;
835 break;
836 }
837 }
838 if (found == 0)
839 return ENOENT;
840 CIRCLEQ_REMOVE(&symtab_queue, st, sd_queue);
841 free(st->sd_symstart, M_DEVBUF);
842 free(st->sd_strstart, M_DEVBUF);
843 free(st->sd_symnmoff, M_DEVBUF);
844 /* LINTED - const castaway */
845 free((void *)st->sd_name, M_DEVBUF);
846 free(st, M_DEVBUF);
847 #if NKSYMS
848 ksyms_sizes_calc();
849 #endif
850 return 0;
851 }
852
853 int
854 ksyms_rensymtab(const char *old, const char *new)
855 {
856 struct symtab *st, *oldst = NULL;
857 char *newstr;
858
859 CIRCLEQ_FOREACH(st, &symtab_queue, sd_queue) {
860 if (strcmp(old, st->sd_name) == 0)
861 oldst = st;
862 if (strcmp(new, st->sd_name) == 0)
863 return (EEXIST);
864 }
865 if (oldst == NULL)
866 return (ENOENT);
867
868 newstr = malloc(strlen(new)+1, M_DEVBUF, M_WAITOK);
869 if (!newstr)
870 return (ENOMEM);
871 strcpy(newstr, new);
872 free((char *)oldst->sd_name, M_DEVBUF);
873 oldst->sd_name = newstr;
874
875 return (0);
876 }
877
878 #ifdef DDB
879
880 /*
881 * Keep sifting stuff here, to avoid export of ksyms internals.
882 */
883 int
884 ksyms_sift(char *mod, char *sym, int mode)
885 {
886 struct symtab *st;
887 char *sb;
888 int i, sz;
889
890 if (ksymsinited == 0)
891 return ENOENT;
892
893 CIRCLEQ_FOREACH(st, &symtab_queue, sd_queue) {
894 if (mod && strcmp(mod, st->sd_name))
895 continue;
896 sb = st->sd_strstart;
897
898 sz = st->sd_symsize/sizeof(Elf_Sym);
899 for (i = 0; i < sz; i++) {
900 Elf_Sym *les = st->sd_symstart + i;
901 char c;
902
903 if (strstr(sb + les->st_name - st->sd_usroffset, sym)
904 == NULL)
905 continue;
906
907 if (mode == 'F') {
908 switch (ELF_ST_TYPE(les->st_info)) {
909 case STT_OBJECT:
910 c = '+';
911 break;
912 case STT_FUNC:
913 c = '*';
914 break;
915 case STT_SECTION:
916 c = '&';
917 break;
918 case STT_FILE:
919 c = '/';
920 break;
921 default:
922 c = ' ';
923 break;
924 }
925 db_printf("%s%c ", sb + les->st_name -
926 st->sd_usroffset, c);
927 } else
928 db_printf("%s ", sb + les->st_name -
929 st->sd_usroffset);
930 }
931 }
932 return ENOENT;
933 }
934 #endif
935
936 #if NKSYMS
937
938 /*
939 * Static allocated ELF header.
940 * Basic info is filled in at attach, sizes at open.
941 */
942 #define SYMTAB 1
943 #define STRTAB 2
944 #define SHSTRTAB 3
945 #define NSECHDR 4
946
947 #define NPRGHDR 2
948 #define SHSTRSIZ 28
949
950 static struct ksyms_hdr {
951 Elf_Ehdr kh_ehdr;
952 Elf_Phdr kh_phdr[NPRGHDR];
953 Elf_Shdr kh_shdr[NSECHDR];
954 char kh_strtab[SHSTRSIZ];
955 } ksyms_hdr;
956
957
958 void
959 ksyms_hdr_init(caddr_t hdraddr)
960 {
961
962 /* Copy the loaded elf exec header */
963 memcpy(&ksyms_hdr.kh_ehdr, hdraddr, sizeof(Elf_Ehdr));
964
965 /* Set correct program/section header sizes, offsets and numbers */
966 ksyms_hdr.kh_ehdr.e_phoff = offsetof(struct ksyms_hdr, kh_phdr[0]);
967 ksyms_hdr.kh_ehdr.e_phentsize = sizeof(Elf_Phdr);
968 ksyms_hdr.kh_ehdr.e_phnum = NPRGHDR;
969 ksyms_hdr.kh_ehdr.e_shoff = offsetof(struct ksyms_hdr, kh_shdr[0]);
970 ksyms_hdr.kh_ehdr.e_shentsize = sizeof(Elf_Shdr);
971 ksyms_hdr.kh_ehdr.e_shnum = NSECHDR;
972 ksyms_hdr.kh_ehdr.e_shstrndx = NSECHDR - 1; /* Last section */
973
974 /*
975 * Keep program headers zeroed (unused).
976 * The section headers are hand-crafted.
977 * First section is section zero.
978 */
979
980 /* Second section header; ".symtab" */
981 ksyms_hdr.kh_shdr[SYMTAB].sh_name = 1; /* Section 3 offset */
982 ksyms_hdr.kh_shdr[SYMTAB].sh_type = SHT_SYMTAB;
983 ksyms_hdr.kh_shdr[SYMTAB].sh_offset = sizeof(struct ksyms_hdr);
984 /* ksyms_hdr.kh_shdr[SYMTAB].sh_size = filled in at open */
985 ksyms_hdr.kh_shdr[SYMTAB].sh_link = 2; /* Corresponding strtab */
986 ksyms_hdr.kh_shdr[SYMTAB].sh_info = 0; /* XXX */
987 ksyms_hdr.kh_shdr[SYMTAB].sh_addralign = sizeof(long);
988 ksyms_hdr.kh_shdr[SYMTAB].sh_entsize = sizeof(Elf_Sym);
989
990 /* Third section header; ".strtab" */
991 ksyms_hdr.kh_shdr[STRTAB].sh_name = 9; /* Section 3 offset */
992 ksyms_hdr.kh_shdr[STRTAB].sh_type = SHT_STRTAB;
993 /* ksyms_hdr.kh_shdr[STRTAB].sh_offset = filled in at open */
994 /* ksyms_hdr.kh_shdr[STRTAB].sh_size = filled in at open */
995 /* ksyms_hdr.kh_shdr[STRTAB].sh_link = kept zero */
996 ksyms_hdr.kh_shdr[STRTAB].sh_info = 0;
997 ksyms_hdr.kh_shdr[STRTAB].sh_addralign = sizeof(char);
998 ksyms_hdr.kh_shdr[STRTAB].sh_entsize = 0;
999
1000 /* Fourth section, ".shstrtab" */
1001 ksyms_hdr.kh_shdr[SHSTRTAB].sh_name = 17; /* This section name offset */
1002 ksyms_hdr.kh_shdr[SHSTRTAB].sh_type = SHT_STRTAB;
1003 ksyms_hdr.kh_shdr[SHSTRTAB].sh_offset =
1004 offsetof(struct ksyms_hdr, kh_strtab);
1005 ksyms_hdr.kh_shdr[SHSTRTAB].sh_size = SHSTRSIZ;
1006 ksyms_hdr.kh_shdr[SHSTRTAB].sh_addralign = sizeof(char);
1007
1008 /* Set section names */
1009 strlcpy(&ksyms_hdr.kh_strtab[1], ".symtab",
1010 sizeof(ksyms_hdr.kh_strtab) - 1);
1011 strlcpy(&ksyms_hdr.kh_strtab[9], ".strtab",
1012 sizeof(ksyms_hdr.kh_strtab) - 9);
1013 strlcpy(&ksyms_hdr.kh_strtab[17], ".shstrtab",
1014 sizeof(ksyms_hdr.kh_strtab) - 17);
1015 };
1016
1017 int
1018 ksymsopen(dev_t dev, int oflags, int devtype, struct proc *p)
1019 {
1020
1021 if (minor(dev))
1022 return ENXIO;
1023 if (ksymsinited == 0)
1024 return ENXIO;
1025
1026 ksyms_hdr.kh_shdr[SYMTAB].sh_size = symsz;
1027 ksyms_hdr.kh_shdr[STRTAB].sh_offset = symsz +
1028 ksyms_hdr.kh_shdr[SYMTAB].sh_offset;
1029 ksyms_hdr.kh_shdr[STRTAB].sh_size = strsz;
1030 ksyms_isopen = 1;
1031
1032 #ifdef KSYMS_DEBUG
1033 if (ksyms_debug & FOLLOW_DEVKSYMS)
1034 printf("ksymsopen: symsz 0x%x strsz 0x%x\n", symsz, strsz);
1035 #endif
1036
1037 return 0;
1038 }
1039
1040 int
1041 ksymsclose(dev_t dev, int oflags, int devtype, struct proc *p)
1042 {
1043
1044 #ifdef KSYMS_DEBUG
1045 if (ksyms_debug & FOLLOW_DEVKSYMS)
1046 printf("ksymsclose\n");
1047 #endif
1048
1049 ksyms_isopen = 0;
1050 wakeup(&ksyms_isopen);
1051 return 0;
1052 }
1053
1054 #define HDRSIZ sizeof(struct ksyms_hdr)
1055
1056 int
1057 ksymsread(dev_t dev, struct uio *uio, int ioflag)
1058 {
1059 struct symtab *st;
1060 size_t filepos, inpos, off;
1061
1062 #ifdef KSYMS_DEBUG
1063 if (ksyms_debug & FOLLOW_DEVKSYMS)
1064 printf("ksymsread: offset 0x%llx resid 0x%lx\n",
1065 (long long)uio->uio_offset, uio->uio_resid);
1066 #endif
1067
1068 off = uio->uio_offset;
1069 if (off >= (strsz + symsz + HDRSIZ))
1070 return 0; /* End of symtab */
1071 /*
1072 * First: Copy out the ELF header.
1073 */
1074 if (off < HDRSIZ)
1075 uiomove((char *)&ksyms_hdr + off, HDRSIZ - off, uio);
1076
1077 /*
1078 * Copy out the symbol table.
1079 */
1080 filepos = HDRSIZ;
1081 CIRCLEQ_FOREACH(st, &symtab_queue, sd_queue) {
1082 if (uio->uio_resid == 0)
1083 return 0;
1084 if (uio->uio_offset <= st->sd_symsize + filepos) {
1085 inpos = uio->uio_offset - filepos;
1086 uiomove((char *)st->sd_symstart + inpos,
1087 st->sd_symsize - inpos, uio);
1088 }
1089 filepos += st->sd_symsize;
1090 }
1091
1092 if (filepos != HDRSIZ + symsz)
1093 panic("ksymsread: unsunc");
1094
1095 /*
1096 * Copy out the string table
1097 */
1098 CIRCLEQ_FOREACH(st, &symtab_queue, sd_queue) {
1099 if (uio->uio_resid == 0)
1100 return 0;
1101 if (uio->uio_offset <= st->sd_strsize + filepos) {
1102 inpos = uio->uio_offset - filepos;
1103 uiomove((char *)st->sd_strstart + inpos,
1104 st->sd_strsize - inpos, uio);
1105 }
1106 filepos += st->sd_strsize;
1107 }
1108 return 0;
1109 }
1110
1111 int
1112 ksymswrite(dev_t dev, struct uio *uio, int ioflag)
1113 {
1114 return EROFS;
1115 }
1116
1117 int
1118 ksymsioctl(dev_t dev, u_long cmd, caddr_t data, int fflag, struct proc *p)
1119 {
1120 struct ksyms_gsymbol *kg = (struct ksyms_gsymbol *)data;
1121 struct symtab *st;
1122 Elf_Sym *sym = NULL;
1123 unsigned long val;
1124 int error = 0;
1125 char *str = NULL;
1126
1127 if (cmd == KIOCGVALUE || cmd == KIOCGSYMBOL)
1128 str = malloc(ksyms_maxlen, M_DEVBUF, M_WAITOK);
1129
1130 switch (cmd) {
1131 case KIOCGVALUE:
1132 /*
1133 * Use the in-kernel symbol lookup code for fast
1134 * retreival of a value.
1135 */
1136 if ((error = copyinstr(kg->kg_name, str, ksyms_maxlen, NULL)))
1137 break;
1138 if ((error = ksyms_getval_from_userland(NULL, str, &val, KSYMS_EXTERN)))
1139 break;
1140 error = copyout(&val, kg->kg_value, sizeof(long));
1141 break;
1142
1143 case KIOCGSYMBOL:
1144 /*
1145 * Use the in-kernel symbol lookup code for fast
1146 * retreival of a symbol.
1147 */
1148 if ((error = copyinstr(kg->kg_name, str, ksyms_maxlen, NULL)))
1149 break;
1150 CIRCLEQ_FOREACH(st, &symtab_queue, sd_queue) {
1151 if ((sym = findsym(str, st, 1)) == NULL) /* from userland */
1152 continue;
1153
1154 /* Skip if bad binding */
1155 if (ELF_ST_BIND(sym->st_info) != STB_GLOBAL) {
1156 sym = NULL;
1157 continue;
1158 }
1159 break;
1160 }
1161 if (sym != NULL)
1162 error = copyout(sym, kg->kg_sym, sizeof(Elf_Sym));
1163 else
1164 error = ENOENT;
1165 break;
1166
1167 case KIOCGSIZE:
1168 /*
1169 * Get total size of symbol table.
1170 */
1171 *(int *)data = strsz + symsz + HDRSIZ;
1172 break;
1173
1174 default:
1175 error = ENOTTY;
1176 break;
1177 }
1178
1179 if (cmd == KIOCGVALUE || cmd == KIOCGSYMBOL)
1180 free(str, M_DEVBUF);
1181
1182 return error;
1183 }
1184 #endif
Cache object: 1b89a519e818cd03deaea4ccec980793
|