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