kern_ksyms.c revision 1.79 1 /* $NetBSD: kern_ksyms.c,v 1.79 2015/08/21 06:55:25 christos 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 (at) 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.79 2015/08/21 06:55:25 christos Exp $");
75
76 #if defined(_KERNEL) && defined(_KERNEL_OPT)
77 #include "opt_ddb.h"
78 #include "opt_dtrace.h"
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 #ifdef DDB
94 #include <ddb/db_output.h>
95 #endif
96
97 #include "ksyms.h"
98 #if NKSYMS > 0
99 #include "ioconf.h"
100 #endif
101
102 #define KSYMS_MAX_ID 65536
103 #ifdef KDTRACE_HOOKS
104 static uint32_t ksyms_nmap[KSYMS_MAX_ID]; /* sorted symbol table map */
105 #else
106 static uint32_t *ksyms_nmap = NULL;
107 #endif
108
109 static int ksyms_maxlen;
110 static bool ksyms_isopen;
111 static bool ksyms_initted;
112 static bool ksyms_loaded;
113 static kmutex_t ksyms_lock __cacheline_aligned;
114 static struct ksyms_symtab kernel_symtab;
115
116 static void ksyms_hdr_init(void *);
117 static void ksyms_sizes_calc(void);
118
119 #ifdef KSYMS_DEBUG
120 #define FOLLOW_CALLS 1
121 #define FOLLOW_MORE_CALLS 2
122 #define FOLLOW_DEVKSYMS 4
123 static int ksyms_debug;
124 #endif
125
126 #define SYMTAB_FILLER "|This is the symbol table!"
127
128 #ifdef COPY_SYMTAB
129 extern char db_symtab[];
130 extern int db_symtabsize;
131 #endif
132
133 /*
134 * used by savecore(8) so non-static
135 */
136 struct ksyms_hdr ksyms_hdr;
137 int ksyms_symsz;
138 int ksyms_strsz;
139 int ksyms_ctfsz; /* this is not currently used by savecore(8) */
140 TAILQ_HEAD(, ksyms_symtab) ksyms_symtabs =
141 TAILQ_HEAD_INITIALIZER(ksyms_symtabs);
142
143 static int
144 ksyms_verify(void *symstart, void *strstart)
145 {
146 #if defined(DIAGNOSTIC) || defined(DEBUG)
147 if (symstart == NULL)
148 printf("ksyms: Symbol table not found\n");
149 if (strstart == NULL)
150 printf("ksyms: String table not found\n");
151 if (symstart == NULL || strstart == NULL)
152 printf("ksyms: Perhaps the kernel is stripped?\n");
153 #endif
154 if (symstart == NULL || strstart == NULL)
155 return 0;
156 return 1;
157 }
158
159 /*
160 * Finds a certain symbol name in a certain symbol table.
161 */
162 static Elf_Sym *
163 findsym(const char *name, struct ksyms_symtab *table, int type)
164 {
165 Elf_Sym *sym, *maxsym;
166 int low, mid, high, nglob;
167 char *str, *cmp;
168
169 sym = table->sd_symstart;
170 str = table->sd_strstart - table->sd_usroffset;
171 nglob = table->sd_nglob;
172 low = 0;
173 high = nglob;
174
175 /*
176 * Start with a binary search of all global symbols in this table.
177 * Global symbols must have unique names.
178 */
179 while (low < high) {
180 mid = (low + high) >> 1;
181 cmp = sym[mid].st_name + str;
182 if (cmp[0] < name[0] || strcmp(cmp, name) < 0) {
183 low = mid + 1;
184 } else {
185 high = mid;
186 }
187 }
188 KASSERT(low == high);
189 if (__predict_true(low < nglob &&
190 strcmp(sym[low].st_name + str, name) == 0)) {
191 KASSERT(ELF_ST_BIND(sym[low].st_info) == STB_GLOBAL);
192 return &sym[low];
193 }
194
195 /*
196 * Perform a linear search of local symbols (rare). Many local
197 * symbols with the same name can exist so are not included in
198 * the binary search.
199 */
200 if (type != KSYMS_EXTERN) {
201 maxsym = sym + table->sd_symsize / sizeof(Elf_Sym);
202 for (sym += nglob; sym < maxsym; sym++) {
203 if (strcmp(name, sym->st_name + str) == 0) {
204 return sym;
205 }
206 }
207 }
208 return NULL;
209 }
210
211 /*
212 * The "attach" is in reality done in ksyms_init().
213 */
214 #if NKSYMS > 0
215 /*
216 * ksyms can be loaded even if the kernel has a missing "pseudo-device ksyms"
217 * statement because ddb and modules require it. Fixing it properly requires
218 * fixing config to warn about required, but missing preudo-devices. For now,
219 * if we don't have the pseudo-device we don't need the attach function; this
220 * is fine, as it does nothing.
221 */
222 void
223 ksymsattach(int arg)
224 {
225 }
226 #endif
227
228 void
229 ksyms_init(void)
230 {
231
232 #ifdef COPY_SYMTAB
233 if (!ksyms_loaded &&
234 strncmp(db_symtab, SYMTAB_FILLER, sizeof(SYMTAB_FILLER))) {
235 ksyms_addsyms_elf(db_symtabsize, db_symtab,
236 db_symtab + db_symtabsize);
237 }
238 #endif
239
240 if (!ksyms_initted) {
241 mutex_init(&ksyms_lock, MUTEX_DEFAULT, IPL_NONE);
242 ksyms_initted = true;
243 }
244 }
245
246 /*
247 * Add a symbol table.
248 * This is intended for use when the symbol table and its corresponding
249 * string table are easily available. If they are embedded in an ELF
250 * image, use addsymtab_elf() instead.
251 *
252 * name - Symbol's table name.
253 * symstart, symsize - Address and size of the symbol table.
254 * strstart, strsize - Address and size of the string table.
255 * tab - Symbol table to be updated with this information.
256 * newstart - Address to which the symbol table has to be copied during
257 * shrinking. If NULL, it is not moved.
258 */
259 static const char *addsymtab_strstart;
260
261 static int
262 addsymtab_compar(const void *a, const void *b)
263 {
264 const Elf_Sym *sa, *sb;
265
266 sa = a;
267 sb = b;
268
269 /*
270 * Split the symbol table into two, with globals at the start
271 * and locals at the end.
272 */
273 if (ELF_ST_BIND(sa->st_info) != ELF_ST_BIND(sb->st_info)) {
274 if (ELF_ST_BIND(sa->st_info) == STB_GLOBAL) {
275 return -1;
276 }
277 if (ELF_ST_BIND(sb->st_info) == STB_GLOBAL) {
278 return 1;
279 }
280 }
281
282 /* Within each band, sort by name. */
283 return strcmp(sa->st_name + addsymtab_strstart,
284 sb->st_name + addsymtab_strstart);
285 }
286
287 static void
288 addsymtab(const char *name, void *symstart, size_t symsize,
289 void *strstart, size_t strsize, struct ksyms_symtab *tab,
290 void *newstart, void *ctfstart, size_t ctfsize, uint32_t *nmap)
291 {
292 Elf_Sym *sym, *nsym, ts;
293 int i, j, n, nglob;
294 char *str;
295 int nsyms = symsize / sizeof(Elf_Sym);
296
297 /* Sanity check for pre-allocated map table used during startup. */
298 if ((nmap == ksyms_nmap) && (nsyms >= KSYMS_MAX_ID)) {
299 printf("kern_ksyms: ERROR %d > %d, increase KSYMS_MAX_ID\n",
300 nsyms, KSYMS_MAX_ID);
301
302 /* truncate for now */
303 nsyms = KSYMS_MAX_ID - 1;
304 }
305
306 tab->sd_symstart = symstart;
307 tab->sd_symsize = symsize;
308 tab->sd_strstart = strstart;
309 tab->sd_strsize = strsize;
310 tab->sd_name = name;
311 tab->sd_minsym = UINTPTR_MAX;
312 tab->sd_maxsym = 0;
313 tab->sd_usroffset = 0;
314 tab->sd_gone = false;
315 tab->sd_ctfstart = ctfstart;
316 tab->sd_ctfsize = ctfsize;
317 tab->sd_nmap = nmap;
318 tab->sd_nmapsize = nsyms;
319 #ifdef KSYMS_DEBUG
320 printf("newstart %p sym %p ksyms_symsz %zu str %p strsz %zu send %p\n",
321 newstart, symstart, symsize, strstart, strsize,
322 tab->sd_strstart + tab->sd_strsize);
323 #endif
324
325 if (nmap) {
326 memset(nmap, 0, nsyms * sizeof(uint32_t));
327 }
328
329 /* Pack symbol table by removing all file name references. */
330 sym = tab->sd_symstart;
331 nsym = (Elf_Sym *)newstart;
332 str = tab->sd_strstart;
333 nglob = 0;
334 for (i = n = 0; i < nsyms; i++) {
335
336 /* This breaks CTF mapping, so don't do it when
337 * DTrace is enabled
338 */
339 #ifndef KDTRACE_HOOKS
340 /*
341 * Remove useless symbols.
342 * Should actually remove all typeless symbols.
343 */
344 if (sym[i].st_name == 0)
345 continue; /* Skip nameless entries */
346 if (sym[i].st_shndx == SHN_UNDEF)
347 continue; /* Skip external references */
348 if (ELF_ST_TYPE(sym[i].st_info) == STT_FILE)
349 continue; /* Skip filenames */
350 if (ELF_ST_TYPE(sym[i].st_info) == STT_NOTYPE &&
351 sym[i].st_value == 0 &&
352 strcmp(str + sym[i].st_name, "*ABS*") == 0)
353 continue; /* XXX */
354 if (ELF_ST_TYPE(sym[i].st_info) == STT_NOTYPE &&
355 strcmp(str + sym[i].st_name, "gcc2_compiled.") == 0)
356 continue; /* XXX */
357 #endif
358
359 /* Save symbol. Set it as an absolute offset */
360 nsym[n] = sym[i];
361
362 #ifdef KDTRACE_HOOKS
363 if (nmap != NULL) {
364 /*
365 * Save the size, replace it with the symbol id so
366 * the mapping can be done after the cleanup and sort.
367 */
368 nmap[i] = nsym[n].st_size;
369 nsym[n].st_size = i + 1; /* zero is reserved */
370 }
371 #endif
372
373 nsym[n].st_shndx = SHBSS;
374 j = strlen(nsym[n].st_name + str) + 1;
375 if (j > ksyms_maxlen)
376 ksyms_maxlen = j;
377 nglob += (ELF_ST_BIND(nsym[n].st_info) == STB_GLOBAL);
378
379 /* Compute min and max symbols. */
380 if (strcmp(str + sym[i].st_name, "*ABS*") != 0
381 && ELF_ST_TYPE(nsym[n].st_info) != STT_NOTYPE) {
382 if (nsym[n].st_value < tab->sd_minsym) {
383 tab->sd_minsym = nsym[n].st_value;
384 }
385 if (nsym[n].st_value > tab->sd_maxsym) {
386 tab->sd_maxsym = nsym[n].st_value;
387 }
388 }
389 n++;
390 }
391
392 /* Fill the rest of the record, and sort the symbols. */
393 tab->sd_symstart = nsym;
394 tab->sd_symsize = n * sizeof(Elf_Sym);
395 tab->sd_nglob = nglob;
396 addsymtab_strstart = str;
397 if (kheapsort(nsym, n, sizeof(Elf_Sym), addsymtab_compar, &ts) != 0)
398 panic("addsymtab");
399
400 #ifdef KDTRACE_HOOKS
401 /*
402 * Build the mapping from original symbol id to new symbol table.
403 * Deleted symbols will have a zero map, indices will be one based
404 * instead of zero based.
405 * Resulting map is sd_nmap[original_index] = new_index + 1
406 */
407 if (nmap != NULL) {
408 int new;
409 for (new = 0; new < n; new++) {
410 uint32_t orig = nsym[new].st_size - 1;
411 uint32_t size = nmap[orig];
412
413 nmap[orig] = new + 1;
414
415 /* restore the size */
416 nsym[new].st_size = size;
417 }
418 }
419 #endif
420
421 /* ksymsread() is unlocked, so membar. */
422 membar_producer();
423 TAILQ_INSERT_TAIL(&ksyms_symtabs, tab, sd_queue);
424 ksyms_sizes_calc();
425 ksyms_loaded = true;
426 }
427
428 /*
429 * Setup the kernel symbol table stuff.
430 */
431 void
432 ksyms_addsyms_elf(int symsize, void *start, void *end)
433 {
434 int i, j;
435 Elf_Shdr *shdr;
436 char *symstart = NULL, *strstart = NULL;
437 size_t strsize = 0;
438 Elf_Ehdr *ehdr;
439 char *ctfstart = NULL;
440 size_t ctfsize = 0;
441
442 if (symsize <= 0) {
443 printf("[ Kernel symbol table missing! ]\n");
444 return;
445 }
446
447 /* Sanity check */
448 if (ALIGNED_POINTER(start, long) == 0) {
449 printf("[ Kernel symbol table has bad start address %p ]\n",
450 start);
451 return;
452 }
453
454 ehdr = (Elf_Ehdr *)start;
455
456 /* check if this is a valid ELF header */
457 /* No reason to verify arch type, the kernel is actually running! */
458 if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG) ||
459 ehdr->e_ident[EI_CLASS] != ELFCLASS ||
460 ehdr->e_version > 1) {
461 printf("[ Kernel symbol table invalid! ]\n");
462 return; /* nothing to do */
463 }
464
465 /* Loaded header will be scratched in addsymtab */
466 ksyms_hdr_init(start);
467
468 /* Find the symbol table and the corresponding string table. */
469 shdr = (Elf_Shdr *)((uint8_t *)start + ehdr->e_shoff);
470 for (i = 1; i < ehdr->e_shnum; i++) {
471 if (shdr[i].sh_type != SHT_SYMTAB)
472 continue;
473 if (shdr[i].sh_offset == 0)
474 continue;
475 symstart = (uint8_t *)start + shdr[i].sh_offset;
476 symsize = shdr[i].sh_size;
477 j = shdr[i].sh_link;
478 if (shdr[j].sh_offset == 0)
479 continue; /* Can this happen? */
480 strstart = (uint8_t *)start + shdr[j].sh_offset;
481 strsize = shdr[j].sh_size;
482 break;
483 }
484
485 #ifdef KDTRACE_HOOKS
486 /* Find the CTF section */
487 shdr = (Elf_Shdr *)((uint8_t *)start + ehdr->e_shoff);
488 if (ehdr->e_shstrndx != 0) {
489 char *shstr = (uint8_t *)start +
490 shdr[ehdr->e_shstrndx].sh_offset;
491 for (i = 1; i < ehdr->e_shnum; i++) {
492 #ifdef DEBUG
493 printf("ksyms: checking %s\n", &shstr[shdr[i].sh_name]);
494 #endif
495 if (shdr[i].sh_type != SHT_PROGBITS)
496 continue;
497 if (strncmp(".SUNW_ctf", &shstr[shdr[i].sh_name], 10)
498 != 0)
499 continue;
500 ctfstart = (uint8_t *)start + shdr[i].sh_offset;
501 ctfsize = shdr[i].sh_size;
502 ksyms_ctfsz = ctfsize;
503 #ifdef DEBUG
504 aprint_normal("Found CTF at %p, size 0x%zx\n",
505 ctfstart, ctfsize);
506 #endif
507 break;
508 }
509 #ifdef DEBUG
510 } else {
511 printf("ksyms: e_shstrndx == 0\n");
512 #endif
513 }
514 #endif
515
516 if (!ksyms_verify(symstart, strstart))
517 return;
518
519 addsymtab("netbsd", symstart, symsize, strstart, strsize,
520 &kernel_symtab, symstart, ctfstart, ctfsize, ksyms_nmap);
521
522 #ifdef DEBUG
523 aprint_normal("Loaded initial symtab at %p, strtab at %p, # entries %ld\n",
524 kernel_symtab.sd_symstart, kernel_symtab.sd_strstart,
525 (long)kernel_symtab.sd_symsize/sizeof(Elf_Sym));
526 #endif
527 }
528
529 /*
530 * Setup the kernel symbol table stuff.
531 * Use this when the address of the symbol and string tables are known;
532 * otherwise use ksyms_init with an ELF image.
533 * We need to pass a minimal ELF header which will later be completed by
534 * ksyms_hdr_init and handed off to userland through /dev/ksyms. We use
535 * a void *rather than a pointer to avoid exposing the Elf_Ehdr type.
536 */
537 void
538 ksyms_addsyms_explicit(void *ehdr, void *symstart, size_t symsize,
539 void *strstart, size_t strsize)
540 {
541
542 if (!ksyms_verify(symstart, strstart))
543 return;
544
545 ksyms_hdr_init(ehdr);
546 addsymtab("netbsd", symstart, symsize, strstart, strsize,
547 &kernel_symtab, symstart, NULL, 0, ksyms_nmap);
548 }
549
550 /*
551 * Get the value associated with a symbol.
552 * "mod" is the module name, or null if any module.
553 * "sym" is the symbol name.
554 * "val" is a pointer to the corresponding value, if call succeeded.
555 * Returns 0 if success or ENOENT if no such entry.
556 *
557 * Call with ksyms_lock, unless known that the symbol table can't change.
558 */
559 int
560 ksyms_getval_unlocked(const char *mod, const char *sym, unsigned long *val,
561 int type)
562 {
563 struct ksyms_symtab *st;
564 Elf_Sym *es;
565
566 #ifdef KSYMS_DEBUG
567 if (ksyms_debug & FOLLOW_CALLS)
568 printf("ksyms_getval_unlocked: mod %s sym %s valp %p\n",
569 mod, sym, val);
570 #endif
571
572 TAILQ_FOREACH(st, &ksyms_symtabs, sd_queue) {
573 if (__predict_false(st->sd_gone))
574 continue;
575 if (mod != NULL && strcmp(st->sd_name, mod))
576 continue;
577 if ((es = findsym(sym, st, type)) != NULL) {
578 *val = es->st_value;
579 return 0;
580 }
581 }
582 return ENOENT;
583 }
584
585 int
586 ksyms_getval(const char *mod, const char *sym, unsigned long *val, int type)
587 {
588 int rc;
589
590 if (!ksyms_loaded)
591 return ENOENT;
592
593 mutex_enter(&ksyms_lock);
594 rc = ksyms_getval_unlocked(mod, sym, val, type);
595 mutex_exit(&ksyms_lock);
596 return rc;
597 }
598
599 struct ksyms_symtab *
600 ksyms_get_mod(const char *mod)
601 {
602 struct ksyms_symtab *st;
603
604 mutex_enter(&ksyms_lock);
605 TAILQ_FOREACH(st, &ksyms_symtabs, sd_queue) {
606 if (__predict_false(st->sd_gone))
607 continue;
608 if (mod != NULL && strcmp(st->sd_name, mod))
609 continue;
610 break;
611 }
612 mutex_exit(&ksyms_lock);
613
614 return st;
615 }
616
617
618 /*
619 * ksyms_mod_foreach()
620 *
621 * Iterate over the symbol table of the specified module, calling the callback
622 * handler for each symbol. Stop iterating if the handler return is non-zero.
623 *
624 */
625
626 int
627 ksyms_mod_foreach(const char *mod, ksyms_callback_t callback, void *opaque)
628 {
629 struct ksyms_symtab *st;
630 Elf_Sym *sym, *maxsym;
631 char *str;
632 int symindx;
633
634 if (!ksyms_loaded)
635 return ENOENT;
636
637 mutex_enter(&ksyms_lock);
638
639 /* find the module */
640 TAILQ_FOREACH(st, &ksyms_symtabs, sd_queue) {
641 if (__predict_false(st->sd_gone))
642 continue;
643 if (mod != NULL && strcmp(st->sd_name, mod))
644 continue;
645
646 sym = st->sd_symstart;
647 str = st->sd_strstart - st->sd_usroffset;
648
649 /* now iterate through the symbols */
650 maxsym = sym + st->sd_symsize / sizeof(Elf_Sym);
651 for (symindx = 0; sym < maxsym; sym++, symindx++) {
652 if (callback(str + sym->st_name, symindx,
653 (void *)sym->st_value,
654 sym->st_size,
655 sym->st_info,
656 opaque) != 0) {
657 break;
658 }
659 }
660 }
661 mutex_exit(&ksyms_lock);
662
663 return 0;
664 }
665
666 /*
667 * Get "mod" and "symbol" associated with an address.
668 * Returns 0 if success or ENOENT if no such entry.
669 *
670 * Call with ksyms_lock, unless known that the symbol table can't change.
671 */
672 int
673 ksyms_getname(const char **mod, const char **sym, vaddr_t v, int f)
674 {
675 struct ksyms_symtab *st;
676 Elf_Sym *les, *es = NULL;
677 vaddr_t laddr = 0;
678 const char *lmod = NULL;
679 char *stable = NULL;
680 int type, i, sz;
681
682 if (!ksyms_loaded)
683 return ENOENT;
684
685 TAILQ_FOREACH(st, &ksyms_symtabs, sd_queue) {
686 if (st->sd_gone)
687 continue;
688 if (v < st->sd_minsym || v > st->sd_maxsym)
689 continue;
690 sz = st->sd_symsize/sizeof(Elf_Sym);
691 for (i = 0; i < sz; i++) {
692 les = st->sd_symstart + i;
693 type = ELF_ST_TYPE(les->st_info);
694
695 if ((f & KSYMS_PROC) && (type != STT_FUNC))
696 continue;
697
698 if (type == STT_NOTYPE)
699 continue;
700
701 if (((f & KSYMS_ANY) == 0) &&
702 (type != STT_FUNC) && (type != STT_OBJECT))
703 continue;
704
705 if ((les->st_value <= v) && (les->st_value > laddr)) {
706 laddr = les->st_value;
707 es = les;
708 lmod = st->sd_name;
709 stable = st->sd_strstart - st->sd_usroffset;
710 }
711 }
712 }
713 if (es == NULL)
714 return ENOENT;
715 if ((f & KSYMS_EXACT) && (v != es->st_value))
716 return ENOENT;
717 if (mod)
718 *mod = lmod;
719 if (sym)
720 *sym = stable + es->st_name;
721 return 0;
722 }
723
724 /*
725 * Add a symbol table from a loadable module.
726 */
727 void
728 ksyms_modload(const char *name, void *symstart, vsize_t symsize,
729 char *strstart, vsize_t strsize)
730 {
731 struct ksyms_symtab *st;
732
733 st = kmem_zalloc(sizeof(*st), KM_SLEEP);
734 mutex_enter(&ksyms_lock);
735 addsymtab(name, symstart, symsize, strstart, strsize, st, symstart,
736 NULL, 0, NULL);
737 mutex_exit(&ksyms_lock);
738 }
739
740 /*
741 * Remove a symbol table from a loadable module.
742 */
743 void
744 ksyms_modunload(const char *name)
745 {
746 struct ksyms_symtab *st;
747
748 mutex_enter(&ksyms_lock);
749 TAILQ_FOREACH(st, &ksyms_symtabs, sd_queue) {
750 if (st->sd_gone)
751 continue;
752 if (strcmp(name, st->sd_name) != 0)
753 continue;
754 st->sd_gone = true;
755 if (!ksyms_isopen) {
756 TAILQ_REMOVE(&ksyms_symtabs, st, sd_queue);
757 ksyms_sizes_calc();
758 kmem_free(st, sizeof(*st));
759 }
760 break;
761 }
762 mutex_exit(&ksyms_lock);
763 KASSERT(st != NULL);
764 }
765
766 #ifdef DDB
767 /*
768 * Keep sifting stuff here, to avoid export of ksyms internals.
769 *
770 * Systems is expected to be quiescent, so no locking done.
771 */
772 int
773 ksyms_sift(char *mod, char *sym, int mode)
774 {
775 struct ksyms_symtab *st;
776 char *sb;
777 int i, sz;
778
779 if (!ksyms_loaded)
780 return ENOENT;
781
782 TAILQ_FOREACH(st, &ksyms_symtabs, sd_queue) {
783 if (st->sd_gone)
784 continue;
785 if (mod && strcmp(mod, st->sd_name))
786 continue;
787 sb = st->sd_strstart - st->sd_usroffset;
788
789 sz = st->sd_symsize/sizeof(Elf_Sym);
790 for (i = 0; i < sz; i++) {
791 Elf_Sym *les = st->sd_symstart + i;
792 char c;
793
794 if (strstr(sb + les->st_name, sym) == NULL)
795 continue;
796
797 if (mode == 'F') {
798 switch (ELF_ST_TYPE(les->st_info)) {
799 case STT_OBJECT:
800 c = '+';
801 break;
802 case STT_FUNC:
803 c = '*';
804 break;
805 case STT_SECTION:
806 c = '&';
807 break;
808 case STT_FILE:
809 c = '/';
810 break;
811 default:
812 c = ' ';
813 break;
814 }
815 db_printf("%s%c ", sb + les->st_name, c);
816 } else
817 db_printf("%s ", sb + les->st_name);
818 }
819 }
820 return ENOENT;
821 }
822 #endif /* DDB */
823
824 /*
825 * In case we exposing the symbol table to the userland using the pseudo-
826 * device /dev/ksyms, it is easier to provide all the tables as one.
827 * However, it means we have to change all the st_name fields for the
828 * symbols so they match the ELF image that the userland will read
829 * through the device.
830 *
831 * The actual (correct) value of st_name is preserved through a global
832 * offset stored in the symbol table structure.
833 *
834 * Call with ksyms_lock held.
835 */
836 static void
837 ksyms_sizes_calc(void)
838 {
839 struct ksyms_symtab *st;
840 int i, delta;
841
842 ksyms_symsz = ksyms_strsz = 0;
843 TAILQ_FOREACH(st, &ksyms_symtabs, sd_queue) {
844 delta = ksyms_strsz - st->sd_usroffset;
845 if (delta != 0) {
846 for (i = 0; i < st->sd_symsize/sizeof(Elf_Sym); i++)
847 st->sd_symstart[i].st_name += delta;
848 st->sd_usroffset = ksyms_strsz;
849 }
850 ksyms_symsz += st->sd_symsize;
851 ksyms_strsz += st->sd_strsize;
852 }
853 }
854
855 static void
856 ksyms_fill_note(void)
857 {
858 int32_t *note = ksyms_hdr.kh_note;
859 note[0] = ELF_NOTE_NETBSD_NAMESZ;
860 note[1] = ELF_NOTE_NETBSD_DESCSZ;
861 note[2] = ELF_NOTE_TYPE_NETBSD_TAG;
862 memcpy(¬e[3], "NetBSD\0", 8);
863 note[5] = __NetBSD_Version__;
864 }
865
866 static void
867 ksyms_hdr_init(void *hdraddr)
868 {
869 /* Copy the loaded elf exec header */
870 memcpy(&ksyms_hdr.kh_ehdr, hdraddr, sizeof(Elf_Ehdr));
871
872 /* Set correct program/section header sizes, offsets and numbers */
873 ksyms_hdr.kh_ehdr.e_phoff = offsetof(struct ksyms_hdr, kh_phdr[0]);
874 ksyms_hdr.kh_ehdr.e_phentsize = sizeof(Elf_Phdr);
875 ksyms_hdr.kh_ehdr.e_phnum = NPRGHDR;
876 ksyms_hdr.kh_ehdr.e_shoff = offsetof(struct ksyms_hdr, kh_shdr[0]);
877 ksyms_hdr.kh_ehdr.e_shentsize = sizeof(Elf_Shdr);
878 ksyms_hdr.kh_ehdr.e_shnum = NSECHDR;
879 ksyms_hdr.kh_ehdr.e_shstrndx = SHSTRTAB;
880
881 /* Text/data - fake */
882 ksyms_hdr.kh_phdr[0].p_type = PT_LOAD;
883 ksyms_hdr.kh_phdr[0].p_memsz = (unsigned long)-1L;
884 ksyms_hdr.kh_phdr[0].p_flags = PF_R | PF_X | PF_W;
885
886 #define SHTCOPY(name) strlcpy(&ksyms_hdr.kh_strtab[offs], (name), \
887 sizeof(ksyms_hdr.kh_strtab) - offs), offs += sizeof(name)
888
889 uint32_t offs = 1;
890 /* First section header ".note.netbsd.ident" */
891 ksyms_hdr.kh_shdr[SHNOTE].sh_name = offs;
892 ksyms_hdr.kh_shdr[SHNOTE].sh_type = SHT_NOTE;
893 ksyms_hdr.kh_shdr[SHNOTE].sh_offset =
894 offsetof(struct ksyms_hdr, kh_note[0]);
895 ksyms_hdr.kh_shdr[SHNOTE].sh_size = sizeof(ksyms_hdr.kh_note);
896 ksyms_hdr.kh_shdr[SHNOTE].sh_addralign = sizeof(int);
897 SHTCOPY(".note.netbsd.ident");
898 ksyms_fill_note();
899
900 /* Second section header; ".symtab" */
901 ksyms_hdr.kh_shdr[SYMTAB].sh_name = offs;
902 ksyms_hdr.kh_shdr[SYMTAB].sh_type = SHT_SYMTAB;
903 ksyms_hdr.kh_shdr[SYMTAB].sh_offset = sizeof(struct ksyms_hdr);
904 /* ksyms_hdr.kh_shdr[SYMTAB].sh_size = filled in at open */
905 ksyms_hdr.kh_shdr[SYMTAB].sh_link = STRTAB; /* Corresponding strtab */
906 ksyms_hdr.kh_shdr[SYMTAB].sh_addralign = sizeof(long);
907 ksyms_hdr.kh_shdr[SYMTAB].sh_entsize = sizeof(Elf_Sym);
908 SHTCOPY(".symtab");
909
910 /* Third section header; ".strtab" */
911 ksyms_hdr.kh_shdr[STRTAB].sh_name = offs;
912 ksyms_hdr.kh_shdr[STRTAB].sh_type = SHT_STRTAB;
913 /* ksyms_hdr.kh_shdr[STRTAB].sh_offset = filled in at open */
914 /* ksyms_hdr.kh_shdr[STRTAB].sh_size = filled in at open */
915 ksyms_hdr.kh_shdr[STRTAB].sh_addralign = sizeof(char);
916 SHTCOPY(".strtab");
917
918 /* Fourth section, ".shstrtab" */
919 ksyms_hdr.kh_shdr[SHSTRTAB].sh_name = offs;
920 ksyms_hdr.kh_shdr[SHSTRTAB].sh_type = SHT_STRTAB;
921 ksyms_hdr.kh_shdr[SHSTRTAB].sh_offset =
922 offsetof(struct ksyms_hdr, kh_strtab);
923 ksyms_hdr.kh_shdr[SHSTRTAB].sh_size = SHSTRSIZ;
924 ksyms_hdr.kh_shdr[SHSTRTAB].sh_addralign = sizeof(char);
925 SHTCOPY(".shstrtab");
926
927 /* Fifth section, ".bss". All symbols reside here. */
928 ksyms_hdr.kh_shdr[SHBSS].sh_name = offs;
929 ksyms_hdr.kh_shdr[SHBSS].sh_type = SHT_NOBITS;
930 ksyms_hdr.kh_shdr[SHBSS].sh_offset = 0;
931 ksyms_hdr.kh_shdr[SHBSS].sh_size = (unsigned long)-1L;
932 ksyms_hdr.kh_shdr[SHBSS].sh_addralign = PAGE_SIZE;
933 ksyms_hdr.kh_shdr[SHBSS].sh_flags = SHF_ALLOC | SHF_EXECINSTR;
934 SHTCOPY(".bss");
935
936 /* Sixth section header; ".SUNW_ctf" */
937 ksyms_hdr.kh_shdr[SHCTF].sh_name = offs;
938 ksyms_hdr.kh_shdr[SHCTF].sh_type = SHT_PROGBITS;
939 /* ksyms_hdr.kh_shdr[SHCTF].sh_offset = filled in at open */
940 /* ksyms_hdr.kh_shdr[SHCTF].sh_size = filled in at open */
941 ksyms_hdr.kh_shdr[SHCTF].sh_link = SYMTAB; /* Corresponding symtab */
942 ksyms_hdr.kh_shdr[SHCTF].sh_addralign = sizeof(char);
943 SHTCOPY(".SUNW_ctf");
944 }
945
946 static int
947 ksymsopen(dev_t dev, int oflags, int devtype, struct lwp *l)
948 {
949
950 if (minor(dev) != 0 || !ksyms_loaded)
951 return ENXIO;
952
953 /*
954 * Create a "snapshot" of the kernel symbol table. Setting
955 * ksyms_isopen will prevent symbol tables from being freed.
956 */
957 mutex_enter(&ksyms_lock);
958 ksyms_hdr.kh_shdr[SYMTAB].sh_size = ksyms_symsz;
959 ksyms_hdr.kh_shdr[SYMTAB].sh_info = ksyms_symsz / sizeof(Elf_Sym);
960 ksyms_hdr.kh_shdr[STRTAB].sh_offset = ksyms_symsz +
961 ksyms_hdr.kh_shdr[SYMTAB].sh_offset;
962 ksyms_hdr.kh_shdr[STRTAB].sh_size = ksyms_strsz;
963 ksyms_hdr.kh_shdr[SHCTF].sh_offset = ksyms_strsz +
964 ksyms_hdr.kh_shdr[STRTAB].sh_offset;
965 ksyms_hdr.kh_shdr[SHCTF].sh_size = ksyms_ctfsz;
966 ksyms_isopen = true;
967 mutex_exit(&ksyms_lock);
968
969 return 0;
970 }
971
972 static int
973 ksymsclose(dev_t dev, int oflags, int devtype, struct lwp *l)
974 {
975 struct ksyms_symtab *st, *next;
976 bool resize;
977
978 /* Discard refernces to symbol tables. */
979 mutex_enter(&ksyms_lock);
980 ksyms_isopen = false;
981 resize = false;
982 for (st = TAILQ_FIRST(&ksyms_symtabs); st != NULL; st = next) {
983 next = TAILQ_NEXT(st, sd_queue);
984 if (st->sd_gone) {
985 TAILQ_REMOVE(&ksyms_symtabs, st, sd_queue);
986 kmem_free(st, sizeof(*st));
987 resize = true;
988 }
989 }
990 if (resize)
991 ksyms_sizes_calc();
992 mutex_exit(&ksyms_lock);
993
994 return 0;
995 }
996
997 static int
998 ksymsread(dev_t dev, struct uio *uio, int ioflag)
999 {
1000 struct ksyms_symtab *st;
1001 size_t filepos, inpos, off;
1002 int error;
1003
1004 /*
1005 * First: Copy out the ELF header. XXX Lose if ksymsopen()
1006 * occurs during read of the header.
1007 */
1008 off = uio->uio_offset;
1009 if (off < sizeof(struct ksyms_hdr)) {
1010 error = uiomove((char *)&ksyms_hdr + off,
1011 sizeof(struct ksyms_hdr) - off, uio);
1012 if (error != 0)
1013 return error;
1014 }
1015
1016 /*
1017 * Copy out the symbol table.
1018 */
1019 filepos = sizeof(struct ksyms_hdr);
1020 TAILQ_FOREACH(st, &ksyms_symtabs, sd_queue) {
1021 if (uio->uio_resid == 0)
1022 return 0;
1023 if (uio->uio_offset <= st->sd_symsize + filepos) {
1024 inpos = uio->uio_offset - filepos;
1025 error = uiomove((char *)st->sd_symstart + inpos,
1026 st->sd_symsize - inpos, uio);
1027 if (error != 0)
1028 return error;
1029 }
1030 filepos += st->sd_symsize;
1031 }
1032
1033 /*
1034 * Copy out the string table
1035 */
1036 KASSERT(filepos == sizeof(struct ksyms_hdr) +
1037 ksyms_hdr.kh_shdr[SYMTAB].sh_size);
1038 TAILQ_FOREACH(st, &ksyms_symtabs, sd_queue) {
1039 if (uio->uio_resid == 0)
1040 return 0;
1041 if (uio->uio_offset <= st->sd_strsize + filepos) {
1042 inpos = uio->uio_offset - filepos;
1043 error = uiomove((char *)st->sd_strstart + inpos,
1044 st->sd_strsize - inpos, uio);
1045 if (error != 0)
1046 return error;
1047 }
1048 filepos += st->sd_strsize;
1049 }
1050
1051 /*
1052 * Copy out the CTF table.
1053 */
1054 st = TAILQ_FIRST(&ksyms_symtabs);
1055 if (st->sd_ctfstart != NULL) {
1056 if (uio->uio_resid == 0)
1057 return 0;
1058 if (uio->uio_offset <= st->sd_ctfsize + filepos) {
1059 inpos = uio->uio_offset - filepos;
1060 error = uiomove((char *)st->sd_ctfstart + inpos,
1061 st->sd_ctfsize - inpos, uio);
1062 if (error != 0)
1063 return error;
1064 }
1065 filepos += st->sd_ctfsize;
1066 }
1067
1068 return 0;
1069 }
1070
1071 static int
1072 ksymswrite(dev_t dev, struct uio *uio, int ioflag)
1073 {
1074
1075 return EROFS;
1076 }
1077
1078 __CTASSERT(offsetof(struct ksyms_ogsymbol, kg_name) == offsetof(struct ksyms_gsymbol, kg_name));
1079 __CTASSERT(offsetof(struct ksyms_gvalue, kv_name) == offsetof(struct ksyms_gsymbol, kg_name));
1080
1081 static int
1082 ksymsioctl(dev_t dev, u_long cmd, void *data, int fflag, struct lwp *l)
1083 {
1084 struct ksyms_ogsymbol *okg = (struct ksyms_ogsymbol *)data;
1085 struct ksyms_gsymbol *kg = (struct ksyms_gsymbol *)data;
1086 struct ksyms_gvalue *kv = (struct ksyms_gvalue *)data;
1087 struct ksyms_symtab *st;
1088 Elf_Sym *sym = NULL, copy;
1089 unsigned long val;
1090 int error = 0;
1091 char *str = NULL;
1092 int len;
1093
1094 /* Read ksyms_maxlen only once while not holding the lock. */
1095 len = ksyms_maxlen;
1096
1097 if (cmd == OKIOCGVALUE || cmd == OKIOCGSYMBOL
1098 || cmd == KIOCGVALUE || cmd == KIOCGSYMBOL) {
1099 str = kmem_alloc(len, KM_SLEEP);
1100 if ((error = copyinstr(kg->kg_name, str, len, NULL)) != 0) {
1101 kmem_free(str, len);
1102 return error;
1103 }
1104 }
1105
1106 switch (cmd) {
1107 case OKIOCGVALUE:
1108 /*
1109 * Use the in-kernel symbol lookup code for fast
1110 * retreival of a value.
1111 */
1112 error = ksyms_getval(NULL, str, &val, KSYMS_EXTERN);
1113 if (error == 0)
1114 error = copyout(&val, okg->kg_value, sizeof(long));
1115 kmem_free(str, len);
1116 break;
1117
1118 case OKIOCGSYMBOL:
1119 /*
1120 * Use the in-kernel symbol lookup code for fast
1121 * retreival of a symbol.
1122 */
1123 mutex_enter(&ksyms_lock);
1124 TAILQ_FOREACH(st, &ksyms_symtabs, sd_queue) {
1125 if (st->sd_gone)
1126 continue;
1127 if ((sym = findsym(str, st, KSYMS_ANY)) == NULL)
1128 continue;
1129 #ifdef notdef
1130 /* Skip if bad binding */
1131 if (ELF_ST_BIND(sym->st_info) != STB_GLOBAL) {
1132 sym = NULL;
1133 continue;
1134 }
1135 #endif
1136 break;
1137 }
1138 if (sym != NULL) {
1139 memcpy(©, sym, sizeof(copy));
1140 mutex_exit(&ksyms_lock);
1141 error = copyout(©, okg->kg_sym, sizeof(Elf_Sym));
1142 } else {
1143 mutex_exit(&ksyms_lock);
1144 error = ENOENT;
1145 }
1146 kmem_free(str, len);
1147 break;
1148
1149 case KIOCGVALUE:
1150 /*
1151 * Use the in-kernel symbol lookup code for fast
1152 * retreival of a value.
1153 */
1154 error = ksyms_getval(NULL, str, &val, KSYMS_EXTERN);
1155 if (error == 0)
1156 kv->kv_value = val;
1157 kmem_free(str, len);
1158 break;
1159
1160 case KIOCGSYMBOL:
1161 /*
1162 * Use the in-kernel symbol lookup code for fast
1163 * retreival of a symbol.
1164 */
1165 mutex_enter(&ksyms_lock);
1166 TAILQ_FOREACH(st, &ksyms_symtabs, sd_queue) {
1167 if (st->sd_gone)
1168 continue;
1169 if ((sym = findsym(str, st, KSYMS_ANY)) == NULL)
1170 continue;
1171 #ifdef notdef
1172 /* Skip if bad binding */
1173 if (ELF_ST_BIND(sym->st_info) != STB_GLOBAL) {
1174 sym = NULL;
1175 continue;
1176 }
1177 #endif
1178 break;
1179 }
1180 if (sym != NULL) {
1181 kg->kg_sym = *sym;
1182 } else {
1183 error = ENOENT;
1184 }
1185 mutex_exit(&ksyms_lock);
1186 kmem_free(str, len);
1187 break;
1188
1189 case KIOCGSIZE:
1190 /*
1191 * Get total size of symbol table.
1192 */
1193 mutex_enter(&ksyms_lock);
1194 *(int *)data = ksyms_strsz + ksyms_symsz +
1195 sizeof(struct ksyms_hdr);
1196 mutex_exit(&ksyms_lock);
1197 break;
1198
1199 default:
1200 error = ENOTTY;
1201 break;
1202 }
1203
1204 return error;
1205 }
1206
1207 const struct cdevsw ksyms_cdevsw = {
1208 .d_open = ksymsopen,
1209 .d_close = ksymsclose,
1210 .d_read = ksymsread,
1211 .d_write = ksymswrite,
1212 .d_ioctl = ksymsioctl,
1213 .d_stop = nullstop,
1214 .d_tty = notty,
1215 .d_poll = nopoll,
1216 .d_mmap = nommap,
1217 .d_kqfilter = nullkqfilter,
1218 .d_discard = nodiscard,
1219 .d_flag = D_OTHER | D_MPSAFE
1220 };
1221