kern_ksyms.c revision 1.19 1 /* $NetBSD: kern_ksyms.c,v 1.19 2004/02/18 20:41:09 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.19 2004/02/18 20:41:09 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 buffers for dynamic loaded symbol tables.
608 * Will go away when in-kernel linker is in place.
609 */
610 #define NSAVEDSYMS 512
611 #define SZSYMNAMES NSAVEDSYMS*8 /* Just an approximation */
612 static Elf_Sym savedsyms[NSAVEDSYMS];
613 static int symnmoff[NSAVEDSYMS];
614 static char symnames[SZSYMNAMES];
615 static int cursyms, curnamep;
616
617 /*
618 * Add a symbol to the temporary save area for symbols.
619 * This routine will go away when the in-kernel linker is in place.
620 */
621 static void
622 addsym(Elf_Sym *sym, char *name)
623 {
624 int len;
625
626 #ifdef KSYMS_DEBUG
627 if (ksyms_debug & FOLLOW_MORE_CALLS)
628 printf("addsym: name %s val %lx\n", name, (long)sym->st_value);
629 #endif
630 if (cursyms == NSAVEDSYMS ||
631 ((len = strlen(name) + 1) + curnamep) > SZSYMNAMES) {
632 printf("addsym: too many symbols, skipping '%s'\n", name);
633 return;
634 }
635 strlcpy(&symnames[curnamep], name, sizeof(symnames) - curnamep);
636 savedsyms[cursyms] = *sym;
637 symnmoff[cursyms] = savedsyms[cursyms].st_name = curnamep;
638 curnamep += len;
639 #if NKSYMS
640 if (len > ksyms_maxlen)
641 ksyms_maxlen = len;
642 #endif
643 cursyms++;
644 }
645 /*
646 * Adds a symbol table.
647 * "name" is the module name, "start" and "size" is where the symbol table
648 * is located, and "type" is in which binary format the symbol table is.
649 * New memory for keeping the symbol table is allocated in this function.
650 * Returns 0 if success and EEXIST if the module name is in use.
651 */
652 int
653 ksyms_addsymtab(const char *mod, void *symstart, vsize_t symsize,
654 char *strstart, vsize_t strsize)
655 {
656 Elf_Sym *sym = symstart;
657 struct symtab *st;
658 unsigned long rval;
659 int i;
660 char *str, *name;
661
662 #ifdef KSYMS_DEBUG
663 if (ksyms_debug & FOLLOW_CALLS)
664 printf("ksyms_addsymtab: mod %s symsize %lx strsize %lx\n",
665 mod, symsize, strsize);
666 #endif
667
668 #if NKSYMS
669 /*
670 * Do not try to add a symbol table while someone is reading
671 * from /dev/ksyms.
672 */
673 while (ksyms_isopen != 0)
674 tsleep(&ksyms_isopen, PWAIT, "ksyms", 0);
675 #endif
676
677 /* Check if this symtab already loaded */
678 CIRCLEQ_FOREACH(st, &symtab_queue, sd_queue) {
679 if (strcmp(mod, st->sd_name) == 0)
680 return EEXIST;
681 }
682
683 /*
684 * XXX - Only add a symbol if it do not exist already.
685 * This is because of a flaw in the current LKM implementation,
686 * the loop will be removed once the in-kernel linker is in place.
687 */
688 cursyms = curnamep = 0;
689 for (i = 0; i < symsize/sizeof(Elf_Sym); i++) {
690 if (sym[i].st_name == 0)
691 continue; /* Just ignore */
692
693 /* check validity of the symbol */
694 /* XXX - save local symbols if DDB */
695 if (ELF_ST_BIND(sym[i].st_info) != STB_GLOBAL)
696 continue;
697
698 /* Check if the symbol exists */
699 if (ksyms_getval_from_kernel(NULL, strstart + sym[i].st_name,
700 &rval, KSYMS_EXTERN) == 0) {
701 /* Check (and complain) about differing values */
702 if (sym[i].st_value != rval) {
703 printf("%s: symbol '%s' redeclared with "
704 "different value (%lx != %lx)\n",
705 mod, strstart + sym[i].st_name,
706 rval, (long)sym[i].st_value);
707 }
708 } else
709 /* Ok, save this symbol */
710 addsym(&sym[i], strstart + sym[i].st_name);
711 }
712
713 sym = malloc(sizeof(Elf_Sym)*cursyms, M_DEVBUF, M_WAITOK);
714 str = malloc(curnamep, M_DEVBUF, M_WAITOK);
715 memcpy(sym, savedsyms, sizeof(Elf_Sym)*cursyms);
716 memcpy(str, symnames, curnamep);
717
718 st = malloc(sizeof(struct symtab), M_DEVBUF, M_WAITOK);
719 i = strlen(mod) + 1;
720 name = malloc(i, M_DEVBUF, M_WAITOK);
721 strlcpy(name, mod, i);
722 st->sd_name = name;
723 st->sd_symnmoff = malloc(sizeof(int)*cursyms, M_DEVBUF, M_WAITOK);
724 memcpy(st->sd_symnmoff, symnmoff, sizeof(int)*cursyms);
725 st->sd_symstart = sym;
726 st->sd_symsize = sizeof(Elf_Sym)*cursyms;
727 st->sd_strstart = str;
728 st->sd_strsize = curnamep;
729
730 /* Make them absolute references */
731 sym = st->sd_symstart;
732 for (i = 0; i < st->sd_symsize/sizeof(Elf_Sym); i++)
733 sym[i].st_shndx = SHN_ABS;
734
735 CIRCLEQ_INSERT_TAIL(&symtab_queue, st, sd_queue);
736 #if NKSYMS
737 ksyms_sizes_calc();
738 #endif
739 return 0;
740 }
741
742 /*
743 * Remove a symbol table specified by name.
744 * Returns 0 if success, EBUSY if device open and ENOENT if no such name.
745 */
746 int
747 ksyms_delsymtab(const char *mod)
748 {
749 struct symtab *st;
750 int found = 0;
751
752 #if NKSYMS
753 /*
754 * Do not try to delete a symbol table while someone is reading
755 * from /dev/ksyms.
756 */
757 while (ksyms_isopen != 0)
758 tsleep(&ksyms_isopen, PWAIT, "ksyms", 0);
759 #endif
760
761 CIRCLEQ_FOREACH(st, &symtab_queue, sd_queue) {
762 if (strcmp(mod, st->sd_name) == 0) {
763 found = 1;
764 break;
765 }
766 }
767 if (found == 0)
768 return ENOENT;
769 CIRCLEQ_REMOVE(&symtab_queue, st, sd_queue);
770 free(st->sd_symstart, M_DEVBUF);
771 free(st->sd_strstart, M_DEVBUF);
772 free(st->sd_symnmoff, M_DEVBUF);
773 /* LINTED - const castaway */
774 free((void *)st->sd_name, M_DEVBUF);
775 free(st, M_DEVBUF);
776 #if NKSYMS
777 ksyms_sizes_calc();
778 #endif
779 return 0;
780 }
781
782 int
783 ksyms_rensymtab(const char *old, const char *new)
784 {
785 struct symtab *st, *oldst = NULL;
786 char *newstr;
787
788 CIRCLEQ_FOREACH(st, &symtab_queue, sd_queue) {
789 if (strcmp(old, st->sd_name) == 0)
790 oldst = st;
791 if (strcmp(new, st->sd_name) == 0)
792 return (EEXIST);
793 }
794 if (oldst == NULL)
795 return (ENOENT);
796
797 newstr = malloc(strlen(new)+1, M_DEVBUF, M_WAITOK);
798 if (!newstr)
799 return (ENOMEM);
800 strcpy(newstr, new);
801 free((char *)oldst->sd_name, M_DEVBUF);
802 oldst->sd_name = newstr;
803
804 return (0);
805 }
806
807 #ifdef DDB
808
809 /*
810 * Keep sifting stuff here, to avoid export of ksyms internals.
811 */
812 int
813 ksyms_sift(char *mod, char *sym, int mode)
814 {
815 struct symtab *st;
816 char *sb;
817 int i, sz;
818
819 if (ksymsinited == 0)
820 return ENOENT;
821
822 CIRCLEQ_FOREACH(st, &symtab_queue, sd_queue) {
823 if (mod && strcmp(mod, st->sd_name))
824 continue;
825 sb = st->sd_strstart;
826
827 sz = st->sd_symsize/sizeof(Elf_Sym);
828 for (i = 0; i < sz; i++) {
829 Elf_Sym *les = st->sd_symstart + i;
830 char c;
831
832 if (strstr(sb + les->st_name - st->sd_usroffset, sym)
833 == NULL)
834 continue;
835
836 if (mode == 'F') {
837 switch (ELF_ST_TYPE(les->st_info)) {
838 case STT_OBJECT:
839 c = '+';
840 break;
841 case STT_FUNC:
842 c = '*';
843 break;
844 case STT_SECTION:
845 c = '&';
846 break;
847 case STT_FILE:
848 c = '/';
849 break;
850 default:
851 c = ' ';
852 break;
853 }
854 db_printf("%s%c ", sb + les->st_name -
855 st->sd_usroffset, c);
856 } else
857 db_printf("%s ", sb + les->st_name -
858 st->sd_usroffset);
859 }
860 }
861 return ENOENT;
862 }
863 #endif
864
865 #if NKSYMS
866
867 /*
868 * Static allocated ELF header.
869 * Basic info is filled in at attach, sizes at open.
870 */
871 #define SYMTAB 1
872 #define STRTAB 2
873 #define SHSTRTAB 3
874 #define NSECHDR 4
875
876 #define NPRGHDR 2
877 #define SHSTRSIZ 28
878
879 static struct ksyms_hdr {
880 Elf_Ehdr kh_ehdr;
881 Elf_Phdr kh_phdr[NPRGHDR];
882 Elf_Shdr kh_shdr[NSECHDR];
883 char kh_strtab[SHSTRSIZ];
884 } ksyms_hdr;
885
886
887 void
888 ksyms_hdr_init(caddr_t hdraddr)
889 {
890
891 /* Copy the loaded elf exec header */
892 memcpy(&ksyms_hdr.kh_ehdr, hdraddr, sizeof(Elf_Ehdr));
893
894 /* Set correct program/section header sizes, offsets and numbers */
895 ksyms_hdr.kh_ehdr.e_phoff = offsetof(struct ksyms_hdr, kh_phdr[0]);
896 ksyms_hdr.kh_ehdr.e_phentsize = sizeof(Elf_Phdr);
897 ksyms_hdr.kh_ehdr.e_phnum = NPRGHDR;
898 ksyms_hdr.kh_ehdr.e_shoff = offsetof(struct ksyms_hdr, kh_shdr[0]);
899 ksyms_hdr.kh_ehdr.e_shentsize = sizeof(Elf_Shdr);
900 ksyms_hdr.kh_ehdr.e_shnum = NSECHDR;
901 ksyms_hdr.kh_ehdr.e_shstrndx = NSECHDR - 1; /* Last section */
902
903 /*
904 * Keep program headers zeroed (unused).
905 * The section headers are hand-crafted.
906 * First section is section zero.
907 */
908
909 /* Second section header; ".symtab" */
910 ksyms_hdr.kh_shdr[SYMTAB].sh_name = 1; /* Section 3 offset */
911 ksyms_hdr.kh_shdr[SYMTAB].sh_type = SHT_SYMTAB;
912 ksyms_hdr.kh_shdr[SYMTAB].sh_offset = sizeof(struct ksyms_hdr);
913 /* ksyms_hdr.kh_shdr[SYMTAB].sh_size = filled in at open */
914 ksyms_hdr.kh_shdr[SYMTAB].sh_link = 2; /* Corresponding strtab */
915 ksyms_hdr.kh_shdr[SYMTAB].sh_info = 0; /* XXX */
916 ksyms_hdr.kh_shdr[SYMTAB].sh_addralign = sizeof(long);
917 ksyms_hdr.kh_shdr[SYMTAB].sh_entsize = sizeof(Elf_Sym);
918
919 /* Third section header; ".strtab" */
920 ksyms_hdr.kh_shdr[STRTAB].sh_name = 9; /* Section 3 offset */
921 ksyms_hdr.kh_shdr[STRTAB].sh_type = SHT_STRTAB;
922 /* ksyms_hdr.kh_shdr[STRTAB].sh_offset = filled in at open */
923 /* ksyms_hdr.kh_shdr[STRTAB].sh_size = filled in at open */
924 /* ksyms_hdr.kh_shdr[STRTAB].sh_link = kept zero */
925 ksyms_hdr.kh_shdr[STRTAB].sh_info = 0;
926 ksyms_hdr.kh_shdr[STRTAB].sh_addralign = sizeof(char);
927 ksyms_hdr.kh_shdr[STRTAB].sh_entsize = 0;
928
929 /* Fourth section, ".shstrtab" */
930 ksyms_hdr.kh_shdr[SHSTRTAB].sh_name = 17; /* This section name offset */
931 ksyms_hdr.kh_shdr[SHSTRTAB].sh_type = SHT_STRTAB;
932 ksyms_hdr.kh_shdr[SHSTRTAB].sh_offset =
933 offsetof(struct ksyms_hdr, kh_strtab);
934 ksyms_hdr.kh_shdr[SHSTRTAB].sh_size = SHSTRSIZ;
935 ksyms_hdr.kh_shdr[SHSTRTAB].sh_addralign = sizeof(char);
936
937 /* Set section names */
938 strlcpy(&ksyms_hdr.kh_strtab[1], ".symtab",
939 sizeof(ksyms_hdr.kh_strtab) - 1);
940 strlcpy(&ksyms_hdr.kh_strtab[9], ".strtab",
941 sizeof(ksyms_hdr.kh_strtab) - 9);
942 strlcpy(&ksyms_hdr.kh_strtab[17], ".shstrtab",
943 sizeof(ksyms_hdr.kh_strtab) - 17);
944 };
945
946 int
947 ksymsopen(dev_t dev, int oflags, int devtype, struct proc *p)
948 {
949
950 if (minor(dev))
951 return ENXIO;
952 if (ksymsinited == 0)
953 return ENXIO;
954
955 ksyms_hdr.kh_shdr[SYMTAB].sh_size = symsz;
956 ksyms_hdr.kh_shdr[STRTAB].sh_offset = symsz +
957 ksyms_hdr.kh_shdr[SYMTAB].sh_offset;
958 ksyms_hdr.kh_shdr[STRTAB].sh_size = strsz;
959 ksyms_isopen = 1;
960
961 #ifdef KSYMS_DEBUG
962 if (ksyms_debug & FOLLOW_DEVKSYMS)
963 printf("ksymsopen: symsz 0x%x strsz 0x%x\n", symsz, strsz);
964 #endif
965
966 return 0;
967 }
968
969 int
970 ksymsclose(dev_t dev, int oflags, int devtype, struct proc *p)
971 {
972
973 #ifdef KSYMS_DEBUG
974 if (ksyms_debug & FOLLOW_DEVKSYMS)
975 printf("ksymsclose\n");
976 #endif
977
978 ksyms_isopen = 0;
979 wakeup(&ksyms_isopen);
980 return 0;
981 }
982
983 #define HDRSIZ sizeof(struct ksyms_hdr)
984
985 int
986 ksymsread(dev_t dev, struct uio *uio, int ioflag)
987 {
988 struct symtab *st;
989 size_t filepos, inpos, off;
990
991 #ifdef KSYMS_DEBUG
992 if (ksyms_debug & FOLLOW_DEVKSYMS)
993 printf("ksymsread: offset 0x%llx resid 0x%lx\n",
994 (long long)uio->uio_offset, uio->uio_resid);
995 #endif
996
997 off = uio->uio_offset;
998 if (off >= (strsz + symsz + HDRSIZ))
999 return 0; /* End of symtab */
1000 /*
1001 * First: Copy out the ELF header.
1002 */
1003 if (off < HDRSIZ)
1004 uiomove((char *)&ksyms_hdr + off, HDRSIZ - off, uio);
1005
1006 /*
1007 * Copy out the symbol table.
1008 */
1009 filepos = HDRSIZ;
1010 CIRCLEQ_FOREACH(st, &symtab_queue, sd_queue) {
1011 if (uio->uio_resid == 0)
1012 return 0;
1013 if (uio->uio_offset <= st->sd_symsize + filepos) {
1014 inpos = uio->uio_offset - filepos;
1015 uiomove((char *)st->sd_symstart + inpos,
1016 st->sd_symsize - inpos, uio);
1017 }
1018 filepos += st->sd_symsize;
1019 }
1020
1021 if (filepos != HDRSIZ + symsz)
1022 panic("ksymsread: unsunc");
1023
1024 /*
1025 * Copy out the string table
1026 */
1027 CIRCLEQ_FOREACH(st, &symtab_queue, sd_queue) {
1028 if (uio->uio_resid == 0)
1029 return 0;
1030 if (uio->uio_offset <= st->sd_strsize + filepos) {
1031 inpos = uio->uio_offset - filepos;
1032 uiomove((char *)st->sd_strstart + inpos,
1033 st->sd_strsize - inpos, uio);
1034 }
1035 filepos += st->sd_strsize;
1036 }
1037 return 0;
1038 }
1039
1040 int
1041 ksymswrite(dev_t dev, struct uio *uio, int ioflag)
1042 {
1043 return EROFS;
1044 }
1045
1046 int
1047 ksymsioctl(dev_t dev, u_long cmd, caddr_t data, int fflag, struct proc *p)
1048 {
1049 struct ksyms_gsymbol *kg = (struct ksyms_gsymbol *)data;
1050 struct symtab *st;
1051 Elf_Sym *sym = NULL;
1052 unsigned long val;
1053 int error = 0;
1054 char *str = NULL;
1055
1056 if (cmd == KIOCGVALUE || cmd == KIOCGSYMBOL)
1057 str = malloc(ksyms_maxlen, M_DEVBUF, M_WAITOK);
1058
1059 switch (cmd) {
1060 case KIOCGVALUE:
1061 /*
1062 * Use the in-kernel symbol lookup code for fast
1063 * retreival of a value.
1064 */
1065 if ((error = copyinstr(kg->kg_name, str, ksyms_maxlen, NULL)))
1066 break;
1067 if ((error = ksyms_getval_from_userland(NULL, str, &val, KSYMS_EXTERN)))
1068 break;
1069 error = copyout(&val, kg->kg_value, sizeof(long));
1070 break;
1071
1072 case KIOCGSYMBOL:
1073 /*
1074 * Use the in-kernel symbol lookup code for fast
1075 * retreival of a symbol.
1076 */
1077 if ((error = copyinstr(kg->kg_name, str, ksyms_maxlen, NULL)))
1078 break;
1079 CIRCLEQ_FOREACH(st, &symtab_queue, sd_queue) {
1080 if ((sym = findsym(str, st, 1)) == NULL) /* from userland */
1081 continue;
1082
1083 /* Skip if bad binding */
1084 if (ELF_ST_BIND(sym->st_info) != STB_GLOBAL) {
1085 sym = NULL;
1086 continue;
1087 }
1088 break;
1089 }
1090 if (sym != NULL)
1091 error = copyout(sym, kg->kg_sym, sizeof(Elf_Sym));
1092 else
1093 error = ENOENT;
1094 break;
1095
1096 case KIOCGSIZE:
1097 /*
1098 * Get total size of symbol table.
1099 */
1100 *(int *)data = strsz + symsz + HDRSIZ;
1101 break;
1102
1103 default:
1104 error = ENOTTY;
1105 break;
1106 }
1107
1108 if (cmd == KIOCGVALUE || cmd == KIOCGSYMBOL)
1109 free(str, M_DEVBUF);
1110
1111 return error;
1112 }
1113 #endif
1114