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