xref: /netbsd-src/sys/kern/kern_ksyms.c (revision 7f21db1c0118155e0dd40b75182e30c589d9f63e)
1 /*	$NetBSD: kern_ksyms.c,v 1.53 2010/01/31 00:43:37 hubertf Exp $	*/
2 
3 /*-
4  * Copyright (c) 2008 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software developed for The NetBSD Foundation
8  * by Andrew Doran.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 /*
33  * Copyright (c) 2001, 2003 Anders Magnusson (ragge@ludd.luth.se).
34  * All rights reserved.
35  *
36  * Redistribution and use in source and binary forms, with or without
37  * modification, are permitted provided that the following conditions
38  * are met:
39  * 1. Redistributions of source code must retain the above copyright
40  *    notice, this list of conditions and the following disclaimer.
41  * 2. Redistributions in binary form must reproduce the above copyright
42  *    notice, this list of conditions and the following disclaimer in the
43  *    documentation and/or other materials provided with the distribution.
44  * 3. The name of the author may not be used to endorse or promote products
45  *    derived from this software without specific prior written permission
46  *
47  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
48  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
49  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
50  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
51  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
52  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
53  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
54  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
55  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
56  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
57  */
58 
59 /*
60  * Code to deal with in-kernel symbol table management + /dev/ksyms.
61  *
62  * For each loaded module the symbol table info is kept track of by a
63  * struct, placed in a circular list. The first entry is the kernel
64  * symbol table.
65  */
66 
67 /*
68  * TODO:
69  *
70  *	Add support for mmap, poll.
71  */
72 
73 #include <sys/cdefs.h>
74 __KERNEL_RCSID(0, "$NetBSD: kern_ksyms.c,v 1.53 2010/01/31 00:43:37 hubertf Exp $");
75 
76 #if defined(_KERNEL) && defined(_KERNEL_OPT)
77 #include "opt_ddb.h"
78 #include "opt_ddbparam.h"	/* for SYMTAB_SPACE */
79 #endif
80 
81 #define _KSYMS_PRIVATE
82 
83 #include <sys/param.h>
84 #include <sys/queue.h>
85 #include <sys/exec.h>
86 #include <sys/systm.h>
87 #include <sys/conf.h>
88 #include <sys/kmem.h>
89 #include <sys/proc.h>
90 #include <sys/atomic.h>
91 #include <sys/ksyms.h>
92 
93 #include <uvm/uvm_extern.h>
94 
95 #ifdef DDB
96 #include <ddb/db_output.h>
97 #endif
98 
99 #include "ksyms.h"
100 
101 static int ksyms_maxlen;
102 static bool ksyms_isopen;
103 static bool ksyms_initted;
104 static struct ksyms_hdr ksyms_hdr;
105 static kmutex_t ksyms_lock;
106 
107 void ksymsattach(int);
108 static void ksyms_hdr_init(void *);
109 static void ksyms_sizes_calc(void);
110 
111 #ifdef KSYMS_DEBUG
112 #define	FOLLOW_CALLS		1
113 #define	FOLLOW_MORE_CALLS	2
114 #define	FOLLOW_DEVKSYMS		4
115 static int ksyms_debug;
116 #endif
117 
118 #ifdef SYMTAB_SPACE
119 #define		SYMTAB_FILLER	"|This is the symbol table!"
120 
121 char		db_symtab[SYMTAB_SPACE] = SYMTAB_FILLER;
122 int		db_symtabsize = SYMTAB_SPACE;
123 #endif
124 
125 int ksyms_symsz;
126 int ksyms_strsz;
127 TAILQ_HEAD(, ksyms_symtab) ksyms_symtabs =
128     TAILQ_HEAD_INITIALIZER(ksyms_symtabs);
129 static struct ksyms_symtab kernel_symtab;
130 
131 static int
132 ksyms_verify(void *symstart, void *strstart)
133 {
134 #if defined(DIAGNOSTIC) || defined(DEBUG)
135 	if (symstart == NULL)
136 		printf("ksyms: Symbol table not found\n");
137 	if (strstart == NULL)
138 		printf("ksyms: String table not found\n");
139 	if (symstart == NULL || strstart == NULL)
140 		printf("ksyms: Perhaps the kernel is stripped?\n");
141 #endif
142 	if (symstart == NULL || strstart == NULL)
143 		return 0;
144 	return 1;
145 }
146 
147 /*
148  * Finds a certain symbol name in a certain symbol table.
149  */
150 static Elf_Sym *
151 findsym(const char *name, struct ksyms_symtab *table, int type)
152 {
153 	Elf_Sym *sym, *maxsym;
154 	int low, mid, high, nglob;
155 	char *str, *cmp;
156 
157 	sym = table->sd_symstart;
158 	str = table->sd_strstart - table->sd_usroffset;
159 	nglob = table->sd_nglob;
160 	low = 0;
161 	high = nglob;
162 
163 	/*
164 	 * Start with a binary search of all global symbols in this table.
165 	 * Global symbols must have unique names.
166 	 */
167 	while (low < high) {
168 		mid = (low + high) >> 1;
169 		cmp = sym[mid].st_name + str;
170 		if (cmp[0] < name[0] || strcmp(cmp, name) < 0) {
171 			low = mid + 1;
172 		} else {
173 			high = mid;
174 		}
175 	}
176 	KASSERT(low == high);
177 	if (__predict_true(low < nglob &&
178 	    strcmp(sym[low].st_name + str, name) == 0)) {
179 		KASSERT(ELF_ST_BIND(sym[low].st_info) == STB_GLOBAL);
180 		return &sym[low];
181 	}
182 
183 	/*
184 	 * Perform a linear search of local symbols (rare).  Many local
185 	 * symbols with the same name can exist so are not included in
186 	 * the binary search.
187 	 */
188 	if (type != KSYMS_EXTERN) {
189 		maxsym = sym + table->sd_symsize / sizeof(Elf_Sym);
190 		for (sym += nglob; sym < maxsym; sym++) {
191 			if (strcmp(name, sym->st_name + str) == 0) {
192 				return sym;
193 			}
194 		}
195 	}
196 	return NULL;
197 }
198 
199 /*
200  * The "attach" is in reality done in ksyms_init().
201  */
202 void
203 ksymsattach(int arg)
204 {
205 
206 }
207 
208 void
209 ksyms_init(void)
210 {
211 
212 #ifdef SYMTAB_SPACE
213 	if (!ksyms_initted &&
214 	    strncmp(db_symtab, SYMTAB_FILLER, sizeof(SYMTAB_FILLER))) {
215 		ksyms_addsyms_elf(db_symtabsize, db_symtab,
216 		    db_symtab + db_symtabsize);
217 	}
218 #endif
219 
220 	mutex_init(&ksyms_lock, MUTEX_DEFAULT, IPL_NONE);
221 }
222 
223 /*
224  * Add a symbol table.
225  * This is intended for use when the symbol table and its corresponding
226  * string table are easily available.  If they are embedded in an ELF
227  * image, use addsymtab_elf() instead.
228  *
229  * name - Symbol's table name.
230  * symstart, symsize - Address and size of the symbol table.
231  * strstart, strsize - Address and size of the string table.
232  * tab - Symbol table to be updated with this information.
233  * newstart - Address to which the symbol table has to be copied during
234  *            shrinking.  If NULL, it is not moved.
235  */
236 static const char *addsymtab_strstart;
237 
238 static int
239 addsymtab_compar(const void *a, const void *b)
240 {
241 	const Elf_Sym *sa, *sb;
242 
243 	sa = a;
244 	sb = b;
245 
246 	/*
247 	 * Split the symbol table into two, with globals at the start
248 	 * and locals at the end.
249 	 */
250 	if (ELF_ST_BIND(sa->st_info) != ELF_ST_BIND(sb->st_info)) {
251 		if (ELF_ST_BIND(sa->st_info) == STB_GLOBAL) {
252 			return -1;
253 		}
254 		if (ELF_ST_BIND(sb->st_info) == STB_GLOBAL) {
255 			return 1;
256 		}
257 	}
258 
259 	/* Within each band, sort by name. */
260 	return strcmp(sa->st_name + addsymtab_strstart,
261 	    sb->st_name + addsymtab_strstart);
262 }
263 
264 static void
265 addsymtab(const char *name, void *symstart, size_t symsize,
266 	  void *strstart, size_t strsize, struct ksyms_symtab *tab,
267 	  void *newstart)
268 {
269 	Elf_Sym *sym, *nsym, ts;
270 	int i, j, n, nglob;
271 	char *str;
272 
273 	tab->sd_symstart = symstart;
274 	tab->sd_symsize = symsize;
275 	tab->sd_strstart = strstart;
276 	tab->sd_strsize = strsize;
277 	tab->sd_name = name;
278 	tab->sd_minsym = UINTPTR_MAX;
279 	tab->sd_maxsym = 0;
280 	tab->sd_usroffset = 0;
281 	tab->sd_gone = false;
282 #ifdef KSYMS_DEBUG
283 	printf("newstart %p sym %p ksyms_symsz %d str %p strsz %d send %p\n",
284 	    newstart, symstart, symsize, strstart, strsize,
285 	    tab->sd_strstart + tab->sd_strsize);
286 #endif
287 
288 	/* Pack symbol table by removing all file name references. */
289 	sym = tab->sd_symstart;
290 	nsym = (Elf_Sym *)newstart;
291 	str = tab->sd_strstart;
292 	nglob = 0;
293 	for (i = n = 0; i < tab->sd_symsize/sizeof(Elf_Sym); i++) {
294 		/*
295 		 * Remove useless symbols.
296 		 * Should actually remove all typeless symbols.
297 		 */
298 		if (sym[i].st_name == 0)
299 			continue; /* Skip nameless entries */
300 		if (sym[i].st_shndx == SHN_UNDEF)
301 			continue; /* Skip external references */
302 		if (ELF_ST_TYPE(sym[i].st_info) == STT_FILE)
303 			continue; /* Skip filenames */
304 		if (ELF_ST_TYPE(sym[i].st_info) == STT_NOTYPE &&
305 		    sym[i].st_value == 0 &&
306 		    strcmp(str + sym[i].st_name, "*ABS*") == 0)
307 			continue; /* XXX */
308 		if (ELF_ST_TYPE(sym[i].st_info) == STT_NOTYPE &&
309 		    strcmp(str + sym[i].st_name, "gcc2_compiled.") == 0)
310 			continue; /* XXX */
311 
312 		/* Save symbol. Set it as an absolute offset */
313 		nsym[n] = sym[i];
314 		nsym[n].st_shndx = SHBSS;
315 		j = strlen(nsym[n].st_name + str) + 1;
316 		if (j > ksyms_maxlen)
317 			ksyms_maxlen = j;
318 		nglob += (ELF_ST_BIND(nsym[n].st_info) == STB_GLOBAL);
319 
320 		/* Compute min and max symbols. */
321 		if (nsym[n].st_value < tab->sd_minsym) {
322 		    	tab->sd_minsym = nsym[n].st_value;
323 		}
324 		if (nsym[n].st_value > tab->sd_maxsym) {
325 		    	tab->sd_maxsym = nsym[n].st_value;
326 		}
327 		n++;
328 	}
329 
330 	/* Fill the rest of the record, and sort the symbols. */
331 	tab->sd_symstart = nsym;
332 	tab->sd_symsize = n * sizeof(Elf_Sym);
333 	tab->sd_nglob = nglob;
334 	addsymtab_strstart = str;
335 	if (kheapsort(nsym, n, sizeof(Elf_Sym), addsymtab_compar, &ts) != 0)
336 		panic("addsymtab");
337 
338 	/* ksymsread() is unlocked, so membar. */
339 	membar_producer();
340 	TAILQ_INSERT_TAIL(&ksyms_symtabs, tab, sd_queue);
341 	ksyms_sizes_calc();
342 	ksyms_initted = true;
343 }
344 
345 /*
346  * Setup the kernel symbol table stuff.
347  */
348 void
349 ksyms_addsyms_elf(int symsize, void *start, void *end)
350 {
351 	int i, j;
352 	Elf_Shdr *shdr;
353 	char *symstart = NULL, *strstart = NULL;
354 	size_t strsize = 0;
355 	Elf_Ehdr *ehdr;
356 
357 	if (symsize <= 0) {
358 		printf("[ Kernel symbol table missing! ]\n");
359 		return;
360 	}
361 
362 	/* Sanity check */
363 	if (ALIGNED_POINTER(start, long) == 0) {
364 		printf("[ Kernel symbol table has bad start address %p ]\n",
365 		    start);
366 		return;
367 	}
368 
369 	ehdr = (Elf_Ehdr *)start;
370 
371 	/* check if this is a valid ELF header */
372 	/* No reason to verify arch type, the kernel is actually running! */
373 	if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG) ||
374 	    ehdr->e_ident[EI_CLASS] != ELFCLASS ||
375 	    ehdr->e_version > 1) {
376 		printf("[ Kernel symbol table invalid! ]\n");
377 		return; /* nothing to do */
378 	}
379 
380 	/* Loaded header will be scratched in addsymtab */
381 	ksyms_hdr_init(start);
382 
383 	/* Find the symbol table and the corresponding string table. */
384 	shdr = (Elf_Shdr *)((uint8_t *)start + ehdr->e_shoff);
385 	for (i = 1; i < ehdr->e_shnum; i++) {
386 		if (shdr[i].sh_type != SHT_SYMTAB)
387 			continue;
388 		if (shdr[i].sh_offset == 0)
389 			continue;
390 		symstart = (uint8_t *)start + shdr[i].sh_offset;
391 		symsize = shdr[i].sh_size;
392 		j = shdr[i].sh_link;
393 		if (shdr[j].sh_offset == 0)
394 			continue; /* Can this happen? */
395 		strstart = (uint8_t *)start + shdr[j].sh_offset;
396 		strsize = shdr[j].sh_size;
397 		break;
398 	}
399 
400 	if (!ksyms_verify(symstart, strstart))
401 		return;
402 	addsymtab("netbsd", symstart, symsize, strstart, strsize,
403 	    &kernel_symtab, start);
404 
405 #ifdef DEBUG
406 	aprint_normal("Loaded initial symtab at %p, strtab at %p, # entries %ld\n",
407 	    kernel_symtab.sd_symstart, kernel_symtab.sd_strstart,
408 	    (long)kernel_symtab.sd_symsize/sizeof(Elf_Sym));
409 #endif
410 }
411 
412 /*
413  * Setup the kernel symbol table stuff.
414  * Use this when the address of the symbol and string tables are known;
415  * otherwise use ksyms_init with an ELF image.
416  * We need to pass a minimal ELF header which will later be completed by
417  * ksyms_hdr_init and handed off to userland through /dev/ksyms.  We use
418  * a void *rather than a pointer to avoid exposing the Elf_Ehdr type.
419  */
420 void
421 ksyms_addsyms_explicit(void *ehdr, void *symstart, size_t symsize,
422 		    void *strstart, size_t strsize)
423 {
424 
425 	if (!ksyms_verify(symstart, strstart))
426 		return;
427 
428 	ksyms_hdr_init(ehdr);
429 	addsymtab("netbsd", symstart, symsize, strstart, strsize,
430 	    &kernel_symtab, symstart);
431 }
432 
433 /*
434  * Get the value associated with a symbol.
435  * "mod" is the module name, or null if any module.
436  * "sym" is the symbol name.
437  * "val" is a pointer to the corresponding value, if call succeeded.
438  * Returns 0 if success or ENOENT if no such entry.
439  *
440  * Call with ksyms_lock, unless known that the symbol table can't change.
441  */
442 int
443 ksyms_getval_unlocked(const char *mod, const char *sym, unsigned long *val,
444 		      int type)
445 {
446 	struct ksyms_symtab *st;
447 	Elf_Sym *es;
448 
449 #ifdef KSYMS_DEBUG
450 	if (ksyms_debug & FOLLOW_CALLS)
451 		printf("ksyms_getval_unlocked: mod %s sym %s valp %p\n",
452 		    mod, sym, val);
453 #endif
454 
455 	TAILQ_FOREACH(st, &ksyms_symtabs, sd_queue) {
456 		if (__predict_false(st->sd_gone))
457 			continue;
458 		if (mod != NULL && strcmp(st->sd_name, mod))
459 			continue;
460 		if ((es = findsym(sym, st, type)) != NULL) {
461 			*val = es->st_value;
462 			return 0;
463 		}
464 	}
465 	return ENOENT;
466 }
467 
468 int
469 ksyms_getval(const char *mod, const char *sym, unsigned long *val, int type)
470 {
471 	int rc;
472 
473 	if (!ksyms_initted)
474 		return ENOENT;
475 
476 	mutex_enter(&ksyms_lock);
477 	rc = ksyms_getval_unlocked(mod, sym, val, type);
478 	mutex_exit(&ksyms_lock);
479 	return rc;
480 }
481 
482 /*
483  * Get "mod" and "symbol" associated with an address.
484  * Returns 0 if success or ENOENT if no such entry.
485  *
486  * Call with ksyms_lock, unless known that the symbol table can't change.
487  */
488 int
489 ksyms_getname(const char **mod, const char **sym, vaddr_t v, int f)
490 {
491 	struct ksyms_symtab *st;
492 	Elf_Sym *les, *es = NULL;
493 	vaddr_t laddr = 0;
494 	const char *lmod = NULL;
495 	char *stable = NULL;
496 	int type, i, sz;
497 
498 	if (!ksyms_initted)
499 		return ENOENT;
500 
501 	TAILQ_FOREACH(st, &ksyms_symtabs, sd_queue) {
502 		if (st->sd_gone)
503 			continue;
504 		if (v < st->sd_minsym || v > st->sd_maxsym)
505 			continue;
506 		sz = st->sd_symsize/sizeof(Elf_Sym);
507 		for (i = 0; i < sz; i++) {
508 			les = st->sd_symstart + i;
509 			type = ELF_ST_TYPE(les->st_info);
510 
511 			if ((f & KSYMS_PROC) && (type != STT_FUNC))
512 				continue;
513 
514 			if (type == STT_NOTYPE)
515 				continue;
516 
517 			if (((f & KSYMS_ANY) == 0) &&
518 			    (type != STT_FUNC) && (type != STT_OBJECT))
519 				continue;
520 
521 			if ((les->st_value <= v) && (les->st_value > laddr)) {
522 				laddr = les->st_value;
523 				es = les;
524 				lmod = st->sd_name;
525 				stable = st->sd_strstart - st->sd_usroffset;
526 			}
527 		}
528 	}
529 	if (es == NULL)
530 		return ENOENT;
531 	if ((f & KSYMS_EXACT) && (v != es->st_value))
532 		return ENOENT;
533 	if (mod)
534 		*mod = lmod;
535 	if (sym)
536 		*sym = stable + es->st_name;
537 	return 0;
538 }
539 
540 /*
541  * Add a symbol table from a loadable module.
542  */
543 void
544 ksyms_modload(const char *name, void *symstart, vsize_t symsize,
545 	      char *strstart, vsize_t strsize)
546 {
547 	struct ksyms_symtab *st;
548 
549 	st = kmem_zalloc(sizeof(*st), KM_SLEEP);
550 	mutex_enter(&ksyms_lock);
551 	addsymtab(name, symstart, symsize, strstart, strsize, st, symstart);
552 	mutex_exit(&ksyms_lock);
553 }
554 
555 /*
556  * Remove a symbol table from a loadable module.
557  */
558 void
559 ksyms_modunload(const char *name)
560 {
561 	struct ksyms_symtab *st;
562 
563 	mutex_enter(&ksyms_lock);
564 	TAILQ_FOREACH(st, &ksyms_symtabs, sd_queue) {
565 		if (st->sd_gone)
566 			continue;
567 		if (strcmp(name, st->sd_name) != 0)
568 			continue;
569 		st->sd_gone = true;
570 		if (!ksyms_isopen) {
571 			TAILQ_REMOVE(&ksyms_symtabs, st, sd_queue);
572 			ksyms_sizes_calc();
573 			kmem_free(st, sizeof(*st));
574 		}
575 		break;
576 	}
577 	mutex_exit(&ksyms_lock);
578 	KASSERT(st != NULL);
579 }
580 
581 #ifdef DDB
582 /*
583  * Keep sifting stuff here, to avoid export of ksyms internals.
584  *
585  * Systems is expected to be quiescent, so no locking done.
586  */
587 int
588 ksyms_sift(char *mod, char *sym, int mode)
589 {
590 	struct ksyms_symtab *st;
591 	char *sb;
592 	int i, sz;
593 
594 	if (!ksyms_initted)
595 		return ENOENT;
596 
597 	TAILQ_FOREACH(st, &ksyms_symtabs, sd_queue) {
598 		if (st->sd_gone)
599 			continue;
600 		if (mod && strcmp(mod, st->sd_name))
601 			continue;
602 		sb = st->sd_strstart - st->sd_usroffset;
603 
604 		sz = st->sd_symsize/sizeof(Elf_Sym);
605 		for (i = 0; i < sz; i++) {
606 			Elf_Sym *les = st->sd_symstart + i;
607 			char c;
608 
609 			if (strstr(sb + les->st_name, sym) == NULL)
610 				continue;
611 
612 			if (mode == 'F') {
613 				switch (ELF_ST_TYPE(les->st_info)) {
614 				case STT_OBJECT:
615 					c = '+';
616 					break;
617 				case STT_FUNC:
618 					c = '*';
619 					break;
620 				case STT_SECTION:
621 					c = '&';
622 					break;
623 				case STT_FILE:
624 					c = '/';
625 					break;
626 				default:
627 					c = ' ';
628 					break;
629 				}
630 				db_printf("%s%c ", sb + les->st_name, c);
631 			} else
632 				db_printf("%s ", sb + les->st_name);
633 		}
634 	}
635 	return ENOENT;
636 }
637 #endif /* DDB */
638 
639 /*
640  * In case we exposing the symbol table to the userland using the pseudo-
641  * device /dev/ksyms, it is easier to provide all the tables as one.
642  * However, it means we have to change all the st_name fields for the
643  * symbols so they match the ELF image that the userland will read
644  * through the device.
645  *
646  * The actual (correct) value of st_name is preserved through a global
647  * offset stored in the symbol table structure.
648  *
649  * Call with ksyms_lock held.
650  */
651 static void
652 ksyms_sizes_calc(void)
653 {
654         struct ksyms_symtab *st;
655 	int i, delta;
656 
657         ksyms_symsz = ksyms_strsz = 0;
658         TAILQ_FOREACH(st, &ksyms_symtabs, sd_queue) {
659 		delta = ksyms_strsz - st->sd_usroffset;
660 		if (delta != 0) {
661 			for (i = 0; i < st->sd_symsize/sizeof(Elf_Sym); i++)
662 				st->sd_symstart[i].st_name += delta;
663 			st->sd_usroffset = ksyms_strsz;
664 		}
665                 ksyms_symsz += st->sd_symsize;
666                 ksyms_strsz += st->sd_strsize;
667         }
668 }
669 
670 static void
671 ksyms_hdr_init(void *hdraddr)
672 {
673 
674 	/* Copy the loaded elf exec header */
675 	memcpy(&ksyms_hdr.kh_ehdr, hdraddr, sizeof(Elf_Ehdr));
676 
677 	/* Set correct program/section header sizes, offsets and numbers */
678 	ksyms_hdr.kh_ehdr.e_phoff = offsetof(struct ksyms_hdr, kh_phdr[0]);
679 	ksyms_hdr.kh_ehdr.e_phentsize = sizeof(Elf_Phdr);
680 	ksyms_hdr.kh_ehdr.e_phnum = NPRGHDR;
681 	ksyms_hdr.kh_ehdr.e_shoff = offsetof(struct ksyms_hdr, kh_shdr[0]);
682 	ksyms_hdr.kh_ehdr.e_shentsize = sizeof(Elf_Shdr);
683 	ksyms_hdr.kh_ehdr.e_shnum = NSECHDR;
684 	ksyms_hdr.kh_ehdr.e_shstrndx = SHSTRTAB;
685 
686 	/* Text/data - fake */
687 	ksyms_hdr.kh_phdr[0].p_type = PT_LOAD;
688 	ksyms_hdr.kh_phdr[0].p_memsz = (unsigned long)-1L;
689 	ksyms_hdr.kh_phdr[0].p_flags = PF_R | PF_X | PF_W;
690 
691 	/* First section is null */
692 
693 	/* Second section header; ".symtab" */
694 	ksyms_hdr.kh_shdr[SYMTAB].sh_name = 1; /* Section 3 offset */
695 	ksyms_hdr.kh_shdr[SYMTAB].sh_type = SHT_SYMTAB;
696 	ksyms_hdr.kh_shdr[SYMTAB].sh_offset = sizeof(struct ksyms_hdr);
697 /*	ksyms_hdr.kh_shdr[SYMTAB].sh_size = filled in at open */
698 	ksyms_hdr.kh_shdr[SYMTAB].sh_link = 2; /* Corresponding strtab */
699 	ksyms_hdr.kh_shdr[SYMTAB].sh_addralign = sizeof(long);
700 	ksyms_hdr.kh_shdr[SYMTAB].sh_entsize = sizeof(Elf_Sym);
701 
702 	/* Third section header; ".strtab" */
703 	ksyms_hdr.kh_shdr[STRTAB].sh_name = 9; /* Section 3 offset */
704 	ksyms_hdr.kh_shdr[STRTAB].sh_type = SHT_STRTAB;
705 /*	ksyms_hdr.kh_shdr[STRTAB].sh_offset = filled in at open */
706 /*	ksyms_hdr.kh_shdr[STRTAB].sh_size = filled in at open */
707 	ksyms_hdr.kh_shdr[STRTAB].sh_addralign = sizeof(char);
708 
709 	/* Fourth section, ".shstrtab" */
710 	ksyms_hdr.kh_shdr[SHSTRTAB].sh_name = 17; /* This section name offset */
711 	ksyms_hdr.kh_shdr[SHSTRTAB].sh_type = SHT_STRTAB;
712 	ksyms_hdr.kh_shdr[SHSTRTAB].sh_offset =
713 	    offsetof(struct ksyms_hdr, kh_strtab);
714 	ksyms_hdr.kh_shdr[SHSTRTAB].sh_size = SHSTRSIZ;
715 	ksyms_hdr.kh_shdr[SHSTRTAB].sh_addralign = sizeof(char);
716 
717 	/* Fifth section, ".bss". All symbols reside here. */
718 	ksyms_hdr.kh_shdr[SHBSS].sh_name = 27; /* This section name offset */
719 	ksyms_hdr.kh_shdr[SHBSS].sh_type = SHT_NOBITS;
720 	ksyms_hdr.kh_shdr[SHBSS].sh_offset = 0;
721 	ksyms_hdr.kh_shdr[SHBSS].sh_size = (unsigned long)-1L;
722 	ksyms_hdr.kh_shdr[SHBSS].sh_addralign = PAGE_SIZE;
723 	ksyms_hdr.kh_shdr[SHBSS].sh_flags = SHF_ALLOC | SHF_EXECINSTR;
724 
725 	/* Set section names */
726 	strlcpy(&ksyms_hdr.kh_strtab[1], ".symtab",
727 	    sizeof(ksyms_hdr.kh_strtab) - 1);
728 	strlcpy(&ksyms_hdr.kh_strtab[9], ".strtab",
729 	    sizeof(ksyms_hdr.kh_strtab) - 9);
730 	strlcpy(&ksyms_hdr.kh_strtab[17], ".shstrtab",
731 	    sizeof(ksyms_hdr.kh_strtab) - 17);
732 	strlcpy(&ksyms_hdr.kh_strtab[27], ".bss",
733 	    sizeof(ksyms_hdr.kh_strtab) - 27);
734 }
735 
736 static int
737 ksymsopen(dev_t dev, int oflags, int devtype, struct lwp *l)
738 {
739 
740 	if (minor(dev) != 0 || !ksyms_initted)
741 		return ENXIO;
742 
743 	/*
744 	 * Create a "snapshot" of the kernel symbol table.  Setting
745 	 * ksyms_isopen will prevent symbol tables from being freed.
746 	 */
747 	mutex_enter(&ksyms_lock);
748 	ksyms_hdr.kh_shdr[SYMTAB].sh_size = ksyms_symsz;
749 	ksyms_hdr.kh_shdr[SYMTAB].sh_info = ksyms_symsz / sizeof(Elf_Sym);
750 	ksyms_hdr.kh_shdr[STRTAB].sh_offset = ksyms_symsz +
751 	    ksyms_hdr.kh_shdr[SYMTAB].sh_offset;
752 	ksyms_hdr.kh_shdr[STRTAB].sh_size = ksyms_strsz;
753 	ksyms_isopen = true;
754 	mutex_exit(&ksyms_lock);
755 
756 	return 0;
757 }
758 
759 static int
760 ksymsclose(dev_t dev, int oflags, int devtype, struct lwp *l)
761 {
762 	struct ksyms_symtab *st, *next;
763 	bool resize;
764 
765 	/* Discard refernces to symbol tables. */
766 	mutex_enter(&ksyms_lock);
767 	ksyms_isopen = false;
768 	resize = false;
769 	for (st = TAILQ_FIRST(&ksyms_symtabs); st != NULL; st = next) {
770 		next = TAILQ_NEXT(st, sd_queue);
771 		if (st->sd_gone) {
772 			TAILQ_REMOVE(&ksyms_symtabs, st, sd_queue);
773 			kmem_free(st, sizeof(*st));
774 			resize = true;
775 		}
776 	}
777 	if (resize)
778 		ksyms_sizes_calc();
779 	mutex_exit(&ksyms_lock);
780 
781 	return 0;
782 }
783 
784 static int
785 ksymsread(dev_t dev, struct uio *uio, int ioflag)
786 {
787 	struct ksyms_symtab *st;
788 	size_t filepos, inpos, off;
789 	int error;
790 
791 	/*
792 	 * First: Copy out the ELF header.   XXX Lose if ksymsopen()
793 	 * occurs during read of the header.
794 	 */
795 	off = uio->uio_offset;
796 	if (off < sizeof(struct ksyms_hdr)) {
797 		error = uiomove((char *)&ksyms_hdr + off,
798 		    sizeof(struct ksyms_hdr) - off, uio);
799 		if (error != 0)
800 			return error;
801 	}
802 
803 	/*
804 	 * Copy out the symbol table.
805 	 */
806 	filepos = sizeof(struct ksyms_hdr);
807 	TAILQ_FOREACH(st, &ksyms_symtabs, sd_queue) {
808 		if (uio->uio_resid == 0)
809 			return 0;
810 		if (uio->uio_offset <= st->sd_symsize + filepos) {
811 			inpos = uio->uio_offset - filepos;
812 			error = uiomove((char *)st->sd_symstart + inpos,
813 			   st->sd_symsize - inpos, uio);
814 			if (error != 0)
815 				return error;
816 		}
817 		filepos += st->sd_symsize;
818 	}
819 
820 	/*
821 	 * Copy out the string table
822 	 */
823 	KASSERT(filepos == sizeof(struct ksyms_hdr) +
824 	    ksyms_hdr.kh_shdr[SYMTAB].sh_size);
825 	TAILQ_FOREACH(st, &ksyms_symtabs, sd_queue) {
826 		if (uio->uio_resid == 0)
827 			return 0;
828 		if (uio->uio_offset <= st->sd_strsize + filepos) {
829 			inpos = uio->uio_offset - filepos;
830 			error = uiomove((char *)st->sd_strstart + inpos,
831 			   st->sd_strsize - inpos, uio);
832 			if (error != 0)
833 				return error;
834 		}
835 		filepos += st->sd_strsize;
836 	}
837 
838 	return 0;
839 }
840 
841 static int
842 ksymswrite(dev_t dev, struct uio *uio, int ioflag)
843 {
844 
845 	return EROFS;
846 }
847 
848 static int
849 ksymsioctl(dev_t dev, u_long cmd, void *data, int fflag, struct lwp *l)
850 {
851 	struct ksyms_gsymbol *kg = (struct ksyms_gsymbol *)data;
852 	struct ksyms_symtab *st;
853 	Elf_Sym *sym = NULL, copy;
854 	unsigned long val;
855 	int error = 0;
856 	char *str = NULL;
857 	int len;
858 
859 	/* Read ksyms_maxlen only once while not holding the lock. */
860 	len = ksyms_maxlen;
861 
862 	if (cmd == KIOCGVALUE || cmd == KIOCGSYMBOL) {
863 		str = kmem_alloc(len, KM_SLEEP);
864 		if ((error = copyinstr(kg->kg_name, str, len, NULL)) != 0) {
865 			kmem_free(str, len);
866 			return error;
867 		}
868 	}
869 
870 	switch (cmd) {
871 	case KIOCGVALUE:
872 		/*
873 		 * Use the in-kernel symbol lookup code for fast
874 		 * retreival of a value.
875 		 */
876 		error = ksyms_getval(NULL, str, &val, KSYMS_EXTERN);
877 		if (error == 0)
878 			error = copyout(&val, kg->kg_value, sizeof(long));
879 		kmem_free(str, len);
880 		break;
881 
882 	case KIOCGSYMBOL:
883 		/*
884 		 * Use the in-kernel symbol lookup code for fast
885 		 * retreival of a symbol.
886 		 */
887 		mutex_enter(&ksyms_lock);
888 		TAILQ_FOREACH(st, &ksyms_symtabs, sd_queue) {
889 			if (st->sd_gone)
890 				continue;
891 			if ((sym = findsym(str, st, KSYMS_ANY)) == NULL)
892 				continue;
893 #ifdef notdef
894 			/* Skip if bad binding */
895 			if (ELF_ST_BIND(sym->st_info) != STB_GLOBAL) {
896 				sym = NULL;
897 				continue;
898 			}
899 #endif
900 			break;
901 		}
902 		if (sym != NULL) {
903 			memcpy(&copy, sym, sizeof(copy));
904 			mutex_exit(&ksyms_lock);
905 			error = copyout(&copy, kg->kg_sym, sizeof(Elf_Sym));
906 		} else {
907 			mutex_exit(&ksyms_lock);
908 			error = ENOENT;
909 		}
910 		kmem_free(str, len);
911 		break;
912 
913 	case KIOCGSIZE:
914 		/*
915 		 * Get total size of symbol table.
916 		 */
917 		mutex_enter(&ksyms_lock);
918 		*(int *)data = ksyms_strsz + ksyms_symsz +
919 		    sizeof(struct ksyms_hdr);
920 		mutex_exit(&ksyms_lock);
921 		break;
922 
923 	default:
924 		error = ENOTTY;
925 		break;
926 	}
927 
928 	return error;
929 }
930 
931 const struct cdevsw ksyms_cdevsw = {
932 	ksymsopen, ksymsclose, ksymsread, ksymswrite, ksymsioctl,
933 	nullstop, notty, nopoll, nommap, nullkqfilter, D_OTHER | D_MPSAFE
934 };
935