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