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