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