1 /* 2 * Copyright (c) 2001, 2003 Anders Magnusson (ragge@ludd.luth.se). 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. The name of the author may not be used to endorse or promote products 14 * derived from this software without specific prior written permission 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27 28 /* 29 * Code to deal with in-kernel symbol table management + /dev/ksyms. 30 * 31 * For each loaded module the symbol table info is kept track of by a 32 * struct, placed in a circular list. The first entry is the kernel 33 * symbol table. 34 */ 35 36 /* 37 * TODO: 38 * Change the ugly way of adding new symbols (comes with linker) 39 * Add kernel locking stuff. 40 * (Ev) add support for poll. 41 * (Ev) fix support for mmap. 42 * 43 * Export ksyms internal logic for use in post-mortem debuggers? 44 * Need to move struct symtab to ksyms.h for that. 45 */ 46 47 #include <sys/cdefs.h> 48 __KERNEL_RCSID(0, "$NetBSD: kern_ksyms.c,v 1.27 2005/12/11 12:24:29 christos Exp $"); 49 50 #ifdef _KERNEL 51 #include "opt_ddb.h" 52 #include "opt_ddbparam.h" /* for SYMTAB_SPACE */ 53 #endif 54 55 #include <sys/param.h> 56 #include <sys/errno.h> 57 #include <sys/queue.h> 58 #include <sys/exec.h> 59 #include <sys/systm.h> 60 #include <sys/conf.h> 61 #include <sys/device.h> 62 #include <sys/malloc.h> 63 #include <sys/proc.h> 64 65 #include <machine/elf_machdep.h> /* XXX */ 66 #define ELFSIZE ARCH_ELFSIZE 67 68 #include <sys/exec_elf.h> 69 #include <sys/ksyms.h> 70 71 #include <lib/libkern/libkern.h> 72 73 #ifdef DDB 74 #include <ddb/db_output.h> 75 #endif 76 77 #include "ksyms.h" 78 79 static int ksymsinited = 0; 80 81 #if NKSYMS 82 static void ksyms_hdr_init(caddr_t hdraddr); 83 static void ksyms_sizes_calc(void); 84 static int ksyms_isopen; 85 static int ksyms_maxlen; 86 #endif 87 88 #ifdef KSYMS_DEBUG 89 #define FOLLOW_CALLS 1 90 #define FOLLOW_MORE_CALLS 2 91 #define FOLLOW_DEVKSYMS 4 92 static int ksyms_debug; 93 #endif 94 95 #ifdef SYMTAB_SPACE 96 #define SYMTAB_FILLER "|This is the symbol table!" 97 98 char db_symtab[SYMTAB_SPACE] = SYMTAB_FILLER; 99 int db_symtabsize = SYMTAB_SPACE; 100 #endif 101 102 /* 103 * Store the different symbol tables in a double-linked list. 104 */ 105 struct symtab { 106 CIRCLEQ_ENTRY(symtab) sd_queue; 107 const char *sd_name; /* Name of this table */ 108 Elf_Sym *sd_symstart; /* Address of symbol table */ 109 caddr_t sd_strstart; /* Adderss of corresponding string table */ 110 int sd_usroffset; /* Real address for userspace */ 111 int sd_symsize; /* Size in bytes of symbol table */ 112 int sd_strsize; /* Size of string table */ 113 int *sd_symnmoff; /* Used when calculating the name offset */ 114 }; 115 116 static CIRCLEQ_HEAD(, symtab) symtab_queue = 117 CIRCLEQ_HEAD_INITIALIZER(symtab_queue); 118 119 static struct symtab kernel_symtab; 120 121 #define USE_PTREE 122 #ifdef USE_PTREE 123 /* 124 * Patricia-tree-based lookup structure for the in-kernel global symbols. 125 * Based on a design by Mikael Sundstrom, msm@sm.luth.se. 126 */ 127 struct ptree { 128 int16_t bitno; 129 int16_t lr[2]; 130 } *symb; 131 static int16_t baseidx; 132 static int treex = 1; 133 134 #define P_BIT(key, bit) ((key[bit >> 3] >> (bit & 7)) & 1) 135 #define STRING(idx) kernel_symtab.sd_symstart[idx].st_name + \ 136 kernel_symtab.sd_strstart 137 138 /* 139 * Walk down the tree until a terminal node is found. 140 */ 141 static int 142 symbol_traverse(const char *key) 143 { 144 int16_t nb, rbit = baseidx; 145 146 while (rbit > 0) { 147 nb = symb[rbit].bitno; 148 rbit = symb[rbit].lr[P_BIT(key, nb)]; 149 } 150 return -rbit; 151 } 152 153 static int 154 ptree_add(char *key, int val) 155 { 156 int idx; 157 int nix, cix, bit, rbit, sb, lastrbit, svbit = 0, ix; 158 char *m, *k; 159 160 if (baseidx == 0) { 161 baseidx = -val; 162 return 0; /* First element */ 163 } 164 165 /* Get string to match against */ 166 idx = symbol_traverse(key); 167 168 /* Find first mismatching bit */ 169 m = STRING(idx); 170 k = key; 171 if (strcmp(m, k) == 0) 172 return 1; 173 174 for (cix = 0; *m && *k && *m == *k; m++, k++, cix += 8) 175 ; 176 ix = ffs((int)*m ^ (int)*k) - 1; 177 cix += ix; 178 179 /* Create new node */ 180 nix = treex++; 181 bit = P_BIT(key, cix); 182 symb[nix].bitno = cix; 183 symb[nix].lr[bit] = -val; 184 185 /* Find where to insert node */ 186 rbit = baseidx; 187 lastrbit = 0; 188 for (;;) { 189 if (rbit < 0) 190 break; 191 sb = symb[rbit].bitno; 192 if (sb > cix) 193 break; 194 if (sb == cix) 195 printf("symb[rbit].bitno == cix!!!\n"); 196 lastrbit = rbit; 197 svbit = P_BIT(key, sb); 198 rbit = symb[rbit].lr[svbit]; 199 } 200 201 /* Do the actual insertion */ 202 if (lastrbit == 0) { 203 /* first element */ 204 symb[nix].lr[!bit] = baseidx; 205 baseidx = nix; 206 } else { 207 symb[nix].lr[!bit] = rbit; 208 symb[lastrbit].lr[svbit] = nix; 209 } 210 return 0; 211 } 212 213 static int 214 ptree_find(const char *key) 215 { 216 int idx; 217 218 if (baseidx == 0) 219 return 0; 220 idx = symbol_traverse(key); 221 222 if (strcmp(key, STRING(idx)) == 0) 223 return idx; 224 return 0; 225 } 226 227 static void 228 ptree_gen(char *off, struct symtab *tab) 229 { 230 Elf_Sym *sym; 231 int i, nsym; 232 233 if (off != NULL) 234 symb = (struct ptree *)ALIGN(off); 235 else 236 symb = malloc((tab->sd_symsize/sizeof(Elf_Sym)) * 237 sizeof(struct ptree), M_DEVBUF, M_WAITOK); 238 symb--; /* sym index won't be 0 */ 239 240 sym = tab->sd_symstart; 241 if ((nsym = tab->sd_symsize/sizeof(Elf_Sym)) > INT16_MAX) { 242 printf("Too many symbols for tree, skipping %d symbols\n", 243 nsym-INT16_MAX); 244 nsym = INT16_MAX; 245 } 246 for (i = 1; i < nsym; i++) { 247 if (ELF_ST_BIND(sym[i].st_info) != STB_GLOBAL) 248 continue; 249 ptree_add(tab->sd_strstart+sym[i].st_name, i); 250 } 251 } 252 #endif /* USE_PTREE */ 253 254 /* 255 * Finds a certain symbol name in a certain symbol table. 256 */ 257 static Elf_Sym * 258 findsym(const char *name, struct symtab *table) 259 { 260 Elf_Sym *start = table->sd_symstart; 261 int i, sz = table->sd_symsize/sizeof(Elf_Sym); 262 char *np; 263 caddr_t realstart = table->sd_strstart - table->sd_usroffset; 264 265 #ifdef USE_PTREE 266 if (table == &kernel_symtab && (i = ptree_find(name)) != 0) 267 return &start[i]; 268 #endif 269 270 for (i = 0; i < sz; i++) { 271 np = realstart + start[i].st_name; 272 if (name[0] == np[0] && name[1] == np[1] && 273 strcmp(name, np) == 0) 274 return &start[i]; 275 } 276 return NULL; 277 } 278 279 /* 280 * The "attach" is in reality done in ksyms_init(). 281 */ 282 void ksymsattach(int); 283 void 284 ksymsattach(int arg) 285 { 286 287 #ifdef USE_PTREE 288 if (baseidx == 0) 289 ptree_gen(0, &kernel_symtab); 290 #endif 291 292 } 293 294 /* 295 * Add a symbol table named name. 296 * This is intended for use when the kernel loader enters the table. 297 */ 298 static void 299 addsymtab(const char *name, Elf_Ehdr *ehdr, struct symtab *tab) 300 { 301 caddr_t start = (caddr_t)ehdr; 302 caddr_t send; 303 Elf_Shdr *shdr; 304 Elf_Sym *sym, *nsym; 305 int i, j, n, g; 306 char *str; 307 308 /* Find the symbol table and the corresponding string table. */ 309 shdr = (Elf_Shdr *)(start + ehdr->e_shoff); 310 for (i = 1; i < ehdr->e_shnum; i++) { 311 if (shdr[i].sh_type != SHT_SYMTAB) 312 continue; 313 if (shdr[i].sh_offset == 0) 314 continue; 315 tab->sd_symstart = (Elf_Sym *)(start + shdr[i].sh_offset); 316 tab->sd_symsize = shdr[i].sh_size; 317 j = shdr[i].sh_link; 318 if (shdr[j].sh_offset == 0) 319 continue; /* Can this happen? */ 320 tab->sd_strstart = start + shdr[j].sh_offset; 321 tab->sd_strsize = shdr[j].sh_size; 322 break; 323 } 324 tab->sd_name = name; 325 send = tab->sd_strstart + tab->sd_strsize; 326 327 #ifdef KSYMS_DEBUG 328 printf("start %p sym %p symsz %d str %p strsz %d send %p\n", 329 start, tab->sd_symstart, tab->sd_symsize, 330 tab->sd_strstart, tab->sd_strsize, send); 331 #endif 332 333 /* 334 * Pack symbol table by removing all file name references 335 * and overwrite the elf header. 336 */ 337 sym = tab->sd_symstart; 338 nsym = (Elf_Sym *)start; 339 str = tab->sd_strstart; 340 for (g = i = n = 0; i < tab->sd_symsize/sizeof(Elf_Sym); i++) { 341 if (i == 0) { 342 nsym[n++] = sym[i]; 343 continue; 344 } 345 /* 346 * Remove useless symbols. 347 * Should actually remove all typeless symbols. 348 */ 349 if (sym[i].st_name == 0) 350 continue; /* Skip nameless entries */ 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 361 #ifndef DDB 362 /* Only need global symbols */ 363 if (ELF_ST_BIND(sym[i].st_info) != STB_GLOBAL) 364 continue; 365 #endif 366 367 /* Save symbol. Set it as an absolute offset */ 368 nsym[n] = sym[i]; 369 nsym[n].st_shndx = SHN_ABS; 370 if (ELF_ST_BIND(nsym[n].st_info) == STB_GLOBAL) 371 g++; 372 #if NKSYMS 373 j = strlen(nsym[n].st_name + tab->sd_strstart) + 1; 374 if (j > ksyms_maxlen) 375 ksyms_maxlen = j; 376 #endif 377 n++; 378 379 } 380 tab->sd_symstart = nsym; 381 tab->sd_symsize = n * sizeof(Elf_Sym); 382 383 #ifdef notyet 384 /* 385 * Remove left-over strings. 386 */ 387 sym = tab->sd_symstart; 388 str = (caddr_t)tab->sd_symstart + tab->sd_symsize; 389 str[0] = 0; 390 n = 1; 391 for (i = 1; i < tab->sd_symsize/sizeof(Elf_Sym); i++) { 392 strcpy(str + n, tab->sd_strstart + sym[i].st_name); 393 sym[i].st_name = n; 394 n += strlen(str+n) + 1; 395 } 396 tab->sd_strstart = str; 397 tab->sd_strsize = n; 398 399 #ifdef KSYMS_DEBUG 400 printf("str %p strsz %d send %p\n", str, n, send); 401 #endif 402 #endif 403 404 CIRCLEQ_INSERT_HEAD(&symtab_queue, tab, sd_queue); 405 406 #ifdef notyet 407 #ifdef USE_PTREE 408 /* Try to use the freed space, if possible */ 409 if (send - str - n > g * sizeof(struct ptree)) 410 ptree_gen(str + n, tab); 411 #endif 412 #endif 413 } 414 415 /* 416 * Setup the kernel symbol table stuff. 417 */ 418 void 419 ksyms_init(int symsize, void *start, void *end) 420 { 421 Elf_Ehdr *ehdr; 422 423 #ifdef SYMTAB_SPACE 424 if (symsize <= 0 && 425 strncmp(db_symtab, SYMTAB_FILLER, sizeof(SYMTAB_FILLER))) { 426 symsize = db_symtabsize; 427 start = db_symtab; 428 end = db_symtab + db_symtabsize; 429 } 430 #endif 431 if (symsize <= 0) { 432 printf("[ Kernel symbol table missing! ]\n"); 433 return; 434 } 435 436 /* Sanity check */ 437 if (ALIGNED_POINTER(start, long) == 0) { 438 printf("[ Kernel symbol table has bad start address %p ]\n", 439 start); 440 return; 441 } 442 443 ehdr = (Elf_Ehdr *)start; 444 445 /* check if this is a valid ELF header */ 446 /* No reason to verify arch type, the kernel is actually running! */ 447 if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG) || 448 ehdr->e_ident[EI_CLASS] != ELFCLASS || 449 ehdr->e_version > 1) { 450 #ifdef notyet /* DDB */ 451 if (ddb_init(symsize, start, end)) 452 return; /* old-style symbol table */ 453 #endif 454 printf("[ Kernel symbol table invalid! ]\n"); 455 return; /* nothing to do */ 456 } 457 458 #if NKSYMS 459 /* Loaded header will be scratched in addsymtab */ 460 ksyms_hdr_init(start); 461 #endif 462 463 addsymtab("netbsd", ehdr, &kernel_symtab); 464 465 #if NKSYMS 466 ksyms_sizes_calc(); 467 #endif 468 469 ksymsinited = 1; 470 471 #ifdef DEBUG 472 printf("Loaded initial symtab at %p, strtab at %p, # entries %ld\n", 473 kernel_symtab.sd_symstart, kernel_symtab.sd_strstart, 474 (long)kernel_symtab.sd_symsize/sizeof(Elf_Sym)); 475 #endif 476 } 477 478 /* 479 * Get the value associated with a symbol. 480 * "mod" is the module name, or null if any module. 481 * "sym" is the symbol name. 482 * "val" is a pointer to the corresponding value, if call succeeded. 483 * Returns 0 if success or ENOENT if no such entry. 484 */ 485 int 486 ksyms_getval(const char *mod, const char *sym, unsigned long *val, int type) 487 { 488 struct symtab *st; 489 Elf_Sym *es; 490 491 if (ksymsinited == 0) 492 return ENOENT; 493 494 #ifdef KSYMS_DEBUG 495 if (ksyms_debug & FOLLOW_CALLS) 496 printf("ksyms_getval: mod %s sym %s valp %p\n", mod, sym, val); 497 #endif 498 499 CIRCLEQ_FOREACH(st, &symtab_queue, sd_queue) { 500 if (mod && strcmp(st->sd_name, mod)) 501 continue; 502 if ((es = findsym(sym, st)) == NULL) 503 continue; 504 505 /* Skip if bad binding */ 506 if (type == KSYMS_EXTERN && 507 ELF_ST_BIND(es->st_info) != STB_GLOBAL) 508 continue; 509 510 if (val) 511 *val = es->st_value; 512 return 0; 513 } 514 return ENOENT; 515 } 516 517 /* 518 * Get "mod" and "symbol" associated with an address. 519 * Returns 0 if success or ENOENT if no such entry. 520 */ 521 int 522 ksyms_getname(const char **mod, const char **sym, vaddr_t v, int f) 523 { 524 struct symtab *st; 525 Elf_Sym *les, *es = NULL; 526 vaddr_t laddr = 0; 527 const char *lmod = NULL; 528 char *stable = NULL; 529 int type, i, sz; 530 531 if (ksymsinited == 0) 532 return ENOENT; 533 534 CIRCLEQ_FOREACH(st, &symtab_queue, sd_queue) { 535 sz = st->sd_symsize/sizeof(Elf_Sym); 536 for (i = 0; i < sz; i++) { 537 les = st->sd_symstart + i; 538 type = ELF_ST_TYPE(les->st_info); 539 540 if ((f & KSYMS_PROC) && (type != STT_FUNC)) 541 continue; 542 543 if (type == STT_NOTYPE) 544 continue; 545 546 if (((f & KSYMS_ANY) == 0) && 547 (type != STT_FUNC) && (type != STT_OBJECT)) 548 continue; 549 550 if ((les->st_value <= v) && (les->st_value > laddr)) { 551 laddr = les->st_value; 552 es = les; 553 lmod = st->sd_name; 554 stable = st->sd_strstart - st->sd_usroffset; 555 } 556 } 557 } 558 if (es == NULL) 559 return ENOENT; 560 if ((f & KSYMS_EXACT) && (v != es->st_value)) 561 return ENOENT; 562 if (mod) 563 *mod = lmod; 564 if (sym) 565 *sym = stable + es->st_name; 566 return 0; 567 } 568 569 #if NKSYMS 570 static int symsz, strsz; 571 572 /* 573 * In case we exposing the symbol table to the userland using the pseudo- 574 * device /dev/ksyms, it is easier to provide all the tables as one. 575 * However, it means we have to change all the st_name fields for the 576 * symbols so they match the ELF image that the userland will read 577 * through the device. 578 * 579 * The actual (correct) value of st_name is preserved through a global 580 * offset stored in the symbol table structure. 581 */ 582 583 static void 584 ksyms_sizes_calc(void) 585 { 586 struct symtab *st; 587 int i; 588 589 symsz = strsz = 0; 590 CIRCLEQ_FOREACH(st, &symtab_queue, sd_queue) { 591 if (st != &kernel_symtab) { 592 for (i = 0; i < st->sd_symsize/sizeof(Elf_Sym); i++) 593 st->sd_symstart[i].st_name = 594 strsz + st->sd_symnmoff[i]; 595 st->sd_usroffset = strsz; 596 } 597 symsz += st->sd_symsize; 598 strsz += st->sd_strsize; 599 } 600 } 601 #endif /* NKSYMS */ 602 603 /* 604 * Temporary work structure for dynamic loaded symbol tables. 605 * Will go away when in-kernel linker is in place. 606 */ 607 608 struct syminfo { 609 size_t cursyms; 610 size_t curnamep; 611 size_t maxsyms; 612 size_t maxnamep; 613 Elf_Sym *syms; 614 int *symnmoff; 615 char *symnames; 616 }; 617 618 619 /* 620 * Add a symbol to the temporary save area for symbols. 621 * This routine will go away when the in-kernel linker is in place. 622 */ 623 static void 624 addsym(struct syminfo *info, const Elf_Sym *sym, const char *name, 625 const char *mod) 626 { 627 int len, mlen; 628 629 #ifdef KSYMS_DEBUG 630 if (ksyms_debug & FOLLOW_MORE_CALLS) 631 printf("addsym: name %s val %lx\n", name, (long)sym->st_value); 632 #endif 633 len = strlen(name) + 1; 634 if (mod) 635 mlen = 1 + strlen(mod); 636 else 637 mlen = 0; 638 if (info->cursyms == info->maxsyms || 639 (len + mlen + info->curnamep) > info->maxnamep) { 640 printf("addsym: too many symbols, skipping '%s'\n", name); 641 return; 642 } 643 strlcpy(&info->symnames[info->curnamep], name, 644 info->maxnamep - info->curnamep); 645 if (mlen) { 646 info->symnames[info->curnamep + len - 1] = '.'; 647 strlcpy(&info->symnames[info->curnamep + len], mod, 648 info->maxnamep - (info->curnamep + len)); 649 len += mlen; 650 } 651 info->syms[info->cursyms] = *sym; 652 info->syms[info->cursyms].st_name = info->curnamep; 653 info->symnmoff[info->cursyms] = info->curnamep; 654 info->curnamep += len; 655 #if NKSYMS 656 if (len > ksyms_maxlen) 657 ksyms_maxlen = len; 658 #endif 659 info->cursyms++; 660 } 661 /* 662 * Adds a symbol table. 663 * "name" is the module name, "start" and "size" is where the symbol table 664 * is located, and "type" is in which binary format the symbol table is. 665 * New memory for keeping the symbol table is allocated in this function. 666 * Returns 0 if success and EEXIST if the module name is in use. 667 */ 668 static int 669 specialsym(const char *symname) 670 { 671 return !strcmp(symname, "_bss_start") || 672 !strcmp(symname, "__bss_start") || 673 !strcmp(symname, "_bss_end__") || 674 !strcmp(symname, "__bss_end__") || 675 !strcmp(symname, "_edata") || 676 !strcmp(symname, "_end") || 677 !strcmp(symname, "__end") || 678 !strcmp(symname, "__end__") || 679 !strncmp(symname, "__start_link_set_", 17) || 680 !strncmp(symname, "__stop_link_set_", 16); 681 } 682 683 int 684 ksyms_addsymtab(const char *mod, void *symstart, vsize_t symsize, 685 char *strstart, vsize_t strsize) 686 { 687 Elf_Sym *sym = symstart; 688 struct symtab *st; 689 unsigned long rval; 690 int i; 691 char *name; 692 struct syminfo info; 693 694 #ifdef KSYMS_DEBUG 695 if (ksyms_debug & FOLLOW_CALLS) 696 printf("ksyms_addsymtab: mod %s symsize %lx strsize %lx\n", 697 mod, symsize, strsize); 698 #endif 699 700 #if NKSYMS 701 /* 702 * Do not try to add a symbol table while someone is reading 703 * from /dev/ksyms. 704 */ 705 while (ksyms_isopen != 0) 706 tsleep(&ksyms_isopen, PWAIT, "ksyms", 0); 707 #endif 708 709 /* Check if this symtab already loaded */ 710 CIRCLEQ_FOREACH(st, &symtab_queue, sd_queue) { 711 if (strcmp(mod, st->sd_name) == 0) 712 return EEXIST; 713 } 714 715 /* 716 * XXX - Only add a symbol if it do not exist already. 717 * This is because of a flaw in the current LKM implementation, 718 * these loops will be removed once the in-kernel linker is in place. 719 */ 720 memset(&info, 0, sizeof(info)); 721 for (i = 0; i < symsize/sizeof(Elf_Sym); i++) { 722 char * const symname = strstart + sym[i].st_name; 723 if (sym[i].st_name == 0) 724 continue; /* Just ignore */ 725 726 /* check validity of the symbol */ 727 /* XXX - save local symbols if DDB */ 728 if (ELF_ST_BIND(sym[i].st_info) != STB_GLOBAL) 729 continue; 730 731 /* Check if the symbol exists */ 732 if (ksyms_getval(NULL, symname, &rval, KSYMS_EXTERN) == 0) { 733 /* Check (and complain) about differing values */ 734 if (sym[i].st_value != rval) { 735 if (specialsym(symname)) { 736 info.maxsyms++; 737 info.maxnamep += strlen(symname) + 1 + 738 strlen(mod) + 1; 739 } else { 740 printf("%s: symbol '%s' redeclared with" 741 " different value (%lx != %lx)\n", 742 mod, symname, 743 rval, (long)sym[i].st_value); 744 } 745 } 746 } else { 747 /* 748 * Count this symbol 749 */ 750 info.maxsyms++; 751 info.maxnamep += strlen(symname) + 1; 752 } 753 } 754 755 /* 756 * Now that we know the sizes, malloc the structures. 757 */ 758 info.syms = malloc(sizeof(Elf_Sym)*info.maxsyms, M_DEVBUF, M_WAITOK); 759 info.symnames = malloc(info.maxnamep, M_DEVBUF, M_WAITOK); 760 info.symnmoff = malloc(sizeof(int)*info.maxsyms, M_DEVBUF, M_WAITOK); 761 762 /* 763 * Now that we have the symbols, actually fill in the structures. 764 */ 765 for (i = 0; i < symsize/sizeof(Elf_Sym); i++) { 766 char * const symname = strstart + sym[i].st_name; 767 if (sym[i].st_name == 0) 768 continue; /* Just ignore */ 769 770 /* check validity of the symbol */ 771 /* XXX - save local symbols if DDB */ 772 if (ELF_ST_BIND(sym[i].st_info) != STB_GLOBAL) 773 continue; 774 775 /* Check if the symbol exists */ 776 if (ksyms_getval(NULL, symname, &rval, KSYMS_EXTERN) == 0) { 777 if ((sym[i].st_value != rval) && specialsym(symname)) { 778 addsym(&info, &sym[i], symname, mod); 779 } 780 } else 781 /* Ok, save this symbol */ 782 addsym(&info, &sym[i], symname, NULL); 783 } 784 785 st = malloc(sizeof(struct symtab), M_DEVBUF, M_WAITOK); 786 i = strlen(mod) + 1; 787 name = malloc(i, M_DEVBUF, M_WAITOK); 788 strlcpy(name, mod, i); 789 st->sd_name = name; 790 st->sd_symnmoff = info.symnmoff; 791 st->sd_symstart = info.syms; 792 st->sd_symsize = sizeof(Elf_Sym)*info.maxsyms; 793 st->sd_strstart = info.symnames; 794 st->sd_strsize = info.maxnamep; 795 796 /* Make them absolute references */ 797 sym = st->sd_symstart; 798 for (i = 0; i < st->sd_symsize/sizeof(Elf_Sym); i++) 799 sym[i].st_shndx = SHN_ABS; 800 801 CIRCLEQ_INSERT_TAIL(&symtab_queue, st, sd_queue); 802 #if NKSYMS 803 ksyms_sizes_calc(); 804 #endif 805 return 0; 806 } 807 808 /* 809 * Remove a symbol table specified by name. 810 * Returns 0 if success, EBUSY if device open and ENOENT if no such name. 811 */ 812 int 813 ksyms_delsymtab(const char *mod) 814 { 815 struct symtab *st; 816 int found = 0; 817 818 #if NKSYMS 819 /* 820 * Do not try to delete a symbol table while someone is reading 821 * from /dev/ksyms. 822 */ 823 while (ksyms_isopen != 0) 824 tsleep(&ksyms_isopen, PWAIT, "ksyms", 0); 825 #endif 826 827 CIRCLEQ_FOREACH(st, &symtab_queue, sd_queue) { 828 if (strcmp(mod, st->sd_name) == 0) { 829 found = 1; 830 break; 831 } 832 } 833 if (found == 0) 834 return ENOENT; 835 CIRCLEQ_REMOVE(&symtab_queue, st, sd_queue); 836 free(st->sd_symstart, M_DEVBUF); 837 free(st->sd_strstart, M_DEVBUF); 838 free(st->sd_symnmoff, M_DEVBUF); 839 /* XXXUNCONST LINTED - const castaway */ 840 free(__UNCONST(st->sd_name), M_DEVBUF); 841 free(st, M_DEVBUF); 842 #if NKSYMS 843 ksyms_sizes_calc(); 844 #endif 845 return 0; 846 } 847 848 int 849 ksyms_rensymtab(const char *old, const char *new) 850 { 851 struct symtab *st, *oldst = NULL; 852 char *newstr; 853 854 CIRCLEQ_FOREACH(st, &symtab_queue, sd_queue) { 855 if (strcmp(old, st->sd_name) == 0) 856 oldst = st; 857 if (strcmp(new, st->sd_name) == 0) 858 return (EEXIST); 859 } 860 if (oldst == NULL) 861 return (ENOENT); 862 863 newstr = malloc(strlen(new)+1, M_DEVBUF, M_WAITOK); 864 if (!newstr) 865 return (ENOMEM); 866 strcpy(newstr, new); 867 /*XXXUNCONST*/ 868 free(__UNCONST(oldst->sd_name), M_DEVBUF); 869 oldst->sd_name = newstr; 870 871 return (0); 872 } 873 874 #ifdef DDB 875 /* 876 * Keep sifting stuff here, to avoid export of ksyms internals. 877 */ 878 int 879 ksyms_sift(char *mod, char *sym, int mode) 880 { 881 struct symtab *st; 882 char *sb; 883 int i, sz; 884 885 if (ksymsinited == 0) 886 return ENOENT; 887 888 CIRCLEQ_FOREACH(st, &symtab_queue, sd_queue) { 889 if (mod && strcmp(mod, st->sd_name)) 890 continue; 891 sb = st->sd_strstart; 892 893 sz = st->sd_symsize/sizeof(Elf_Sym); 894 for (i = 0; i < sz; i++) { 895 Elf_Sym *les = st->sd_symstart + i; 896 char c; 897 898 if (strstr(sb + les->st_name - st->sd_usroffset, sym) 899 == NULL) 900 continue; 901 902 if (mode == 'F') { 903 switch (ELF_ST_TYPE(les->st_info)) { 904 case STT_OBJECT: 905 c = '+'; 906 break; 907 case STT_FUNC: 908 c = '*'; 909 break; 910 case STT_SECTION: 911 c = '&'; 912 break; 913 case STT_FILE: 914 c = '/'; 915 break; 916 default: 917 c = ' '; 918 break; 919 } 920 db_printf("%s%c ", sb + les->st_name - 921 st->sd_usroffset, c); 922 } else 923 db_printf("%s ", sb + les->st_name - 924 st->sd_usroffset); 925 } 926 } 927 return ENOENT; 928 } 929 #endif /* DDB */ 930 931 #if NKSYMS 932 /* 933 * Static allocated ELF header. 934 * Basic info is filled in at attach, sizes at open. 935 */ 936 #define SYMTAB 1 937 #define STRTAB 2 938 #define SHSTRTAB 3 939 #define NSECHDR 4 940 941 #define NPRGHDR 2 942 #define SHSTRSIZ 28 943 944 static struct ksyms_hdr { 945 Elf_Ehdr kh_ehdr; 946 Elf_Phdr kh_phdr[NPRGHDR]; 947 Elf_Shdr kh_shdr[NSECHDR]; 948 char kh_strtab[SHSTRSIZ]; 949 } ksyms_hdr; 950 951 952 static void 953 ksyms_hdr_init(caddr_t hdraddr) 954 { 955 956 /* Copy the loaded elf exec header */ 957 memcpy(&ksyms_hdr.kh_ehdr, hdraddr, sizeof(Elf_Ehdr)); 958 959 /* Set correct program/section header sizes, offsets and numbers */ 960 ksyms_hdr.kh_ehdr.e_phoff = offsetof(struct ksyms_hdr, kh_phdr[0]); 961 ksyms_hdr.kh_ehdr.e_phentsize = sizeof(Elf_Phdr); 962 ksyms_hdr.kh_ehdr.e_phnum = NPRGHDR; 963 ksyms_hdr.kh_ehdr.e_shoff = offsetof(struct ksyms_hdr, kh_shdr[0]); 964 ksyms_hdr.kh_ehdr.e_shentsize = sizeof(Elf_Shdr); 965 ksyms_hdr.kh_ehdr.e_shnum = NSECHDR; 966 ksyms_hdr.kh_ehdr.e_shstrndx = NSECHDR - 1; /* Last section */ 967 968 /* 969 * Keep program headers zeroed (unused). 970 * The section headers are hand-crafted. 971 * First section is section zero. 972 */ 973 974 /* Second section header; ".symtab" */ 975 ksyms_hdr.kh_shdr[SYMTAB].sh_name = 1; /* Section 3 offset */ 976 ksyms_hdr.kh_shdr[SYMTAB].sh_type = SHT_SYMTAB; 977 ksyms_hdr.kh_shdr[SYMTAB].sh_offset = sizeof(struct ksyms_hdr); 978 /* ksyms_hdr.kh_shdr[SYMTAB].sh_size = filled in at open */ 979 ksyms_hdr.kh_shdr[SYMTAB].sh_link = 2; /* Corresponding strtab */ 980 ksyms_hdr.kh_shdr[SYMTAB].sh_info = 0; /* XXX */ 981 ksyms_hdr.kh_shdr[SYMTAB].sh_addralign = sizeof(long); 982 ksyms_hdr.kh_shdr[SYMTAB].sh_entsize = sizeof(Elf_Sym); 983 984 /* Third section header; ".strtab" */ 985 ksyms_hdr.kh_shdr[STRTAB].sh_name = 9; /* Section 3 offset */ 986 ksyms_hdr.kh_shdr[STRTAB].sh_type = SHT_STRTAB; 987 /* ksyms_hdr.kh_shdr[STRTAB].sh_offset = filled in at open */ 988 /* ksyms_hdr.kh_shdr[STRTAB].sh_size = filled in at open */ 989 /* ksyms_hdr.kh_shdr[STRTAB].sh_link = kept zero */ 990 ksyms_hdr.kh_shdr[STRTAB].sh_info = 0; 991 ksyms_hdr.kh_shdr[STRTAB].sh_addralign = sizeof(char); 992 ksyms_hdr.kh_shdr[STRTAB].sh_entsize = 0; 993 994 /* Fourth section, ".shstrtab" */ 995 ksyms_hdr.kh_shdr[SHSTRTAB].sh_name = 17; /* This section name offset */ 996 ksyms_hdr.kh_shdr[SHSTRTAB].sh_type = SHT_STRTAB; 997 ksyms_hdr.kh_shdr[SHSTRTAB].sh_offset = 998 offsetof(struct ksyms_hdr, kh_strtab); 999 ksyms_hdr.kh_shdr[SHSTRTAB].sh_size = SHSTRSIZ; 1000 ksyms_hdr.kh_shdr[SHSTRTAB].sh_addralign = sizeof(char); 1001 1002 /* Set section names */ 1003 strlcpy(&ksyms_hdr.kh_strtab[1], ".symtab", 1004 sizeof(ksyms_hdr.kh_strtab) - 1); 1005 strlcpy(&ksyms_hdr.kh_strtab[9], ".strtab", 1006 sizeof(ksyms_hdr.kh_strtab) - 9); 1007 strlcpy(&ksyms_hdr.kh_strtab[17], ".shstrtab", 1008 sizeof(ksyms_hdr.kh_strtab) - 17); 1009 }; 1010 1011 static int 1012 ksymsopen(dev_t dev, int oflags, int devtype, struct lwp *l) 1013 { 1014 1015 if (minor(dev)) 1016 return ENXIO; 1017 if (ksymsinited == 0) 1018 return ENXIO; 1019 1020 ksyms_hdr.kh_shdr[SYMTAB].sh_size = symsz; 1021 ksyms_hdr.kh_shdr[STRTAB].sh_offset = symsz + 1022 ksyms_hdr.kh_shdr[SYMTAB].sh_offset; 1023 ksyms_hdr.kh_shdr[STRTAB].sh_size = strsz; 1024 ksyms_isopen = 1; 1025 1026 #ifdef KSYMS_DEBUG 1027 if (ksyms_debug & FOLLOW_DEVKSYMS) 1028 printf("ksymsopen: symsz 0x%x strsz 0x%x\n", symsz, strsz); 1029 #endif 1030 1031 return 0; 1032 } 1033 1034 static int 1035 ksymsclose(dev_t dev, int oflags, int devtype, struct lwp *l) 1036 { 1037 1038 #ifdef KSYMS_DEBUG 1039 if (ksyms_debug & FOLLOW_DEVKSYMS) 1040 printf("ksymsclose\n"); 1041 #endif 1042 1043 ksyms_isopen = 0; 1044 wakeup(&ksyms_isopen); 1045 return 0; 1046 } 1047 1048 #define HDRSIZ sizeof(struct ksyms_hdr) 1049 1050 static int 1051 ksymsread(dev_t dev, struct uio *uio, int ioflag) 1052 { 1053 struct symtab *st; 1054 size_t filepos, inpos, off; 1055 1056 #ifdef KSYMS_DEBUG 1057 if (ksyms_debug & FOLLOW_DEVKSYMS) 1058 printf("ksymsread: offset 0x%llx resid 0x%zx\n", 1059 (long long)uio->uio_offset, uio->uio_resid); 1060 #endif 1061 1062 off = uio->uio_offset; 1063 if (off >= (strsz + symsz + HDRSIZ)) 1064 return 0; /* End of symtab */ 1065 /* 1066 * First: Copy out the ELF header. 1067 */ 1068 if (off < HDRSIZ) 1069 uiomove((char *)&ksyms_hdr + off, HDRSIZ - off, uio); 1070 1071 /* 1072 * Copy out the symbol table. 1073 */ 1074 filepos = HDRSIZ; 1075 CIRCLEQ_FOREACH(st, &symtab_queue, sd_queue) { 1076 if (uio->uio_resid == 0) 1077 return 0; 1078 if (uio->uio_offset <= st->sd_symsize + filepos) { 1079 inpos = uio->uio_offset - filepos; 1080 uiomove((char *)st->sd_symstart + inpos, 1081 st->sd_symsize - inpos, uio); 1082 } 1083 filepos += st->sd_symsize; 1084 } 1085 1086 if (filepos != HDRSIZ + symsz) 1087 panic("ksymsread: unsunc"); 1088 1089 /* 1090 * Copy out the string table 1091 */ 1092 CIRCLEQ_FOREACH(st, &symtab_queue, sd_queue) { 1093 if (uio->uio_resid == 0) 1094 return 0; 1095 if (uio->uio_offset <= st->sd_strsize + filepos) { 1096 inpos = uio->uio_offset - filepos; 1097 uiomove((char *)st->sd_strstart + inpos, 1098 st->sd_strsize - inpos, uio); 1099 } 1100 filepos += st->sd_strsize; 1101 } 1102 return 0; 1103 } 1104 1105 static int 1106 ksymswrite(dev_t dev, struct uio *uio, int ioflag) 1107 { 1108 return EROFS; 1109 } 1110 1111 static int 1112 ksymsioctl(dev_t dev, u_long cmd, caddr_t data, int fflag, struct lwp *l) 1113 { 1114 struct ksyms_gsymbol *kg = (struct ksyms_gsymbol *)data; 1115 struct symtab *st; 1116 Elf_Sym *sym = NULL; 1117 unsigned long val; 1118 int error = 0; 1119 char *str = NULL; 1120 1121 if (cmd == KIOCGVALUE || cmd == KIOCGSYMBOL) 1122 str = malloc(ksyms_maxlen, M_DEVBUF, M_WAITOK); 1123 1124 switch (cmd) { 1125 case KIOCGVALUE: 1126 /* 1127 * Use the in-kernel symbol lookup code for fast 1128 * retreival of a value. 1129 */ 1130 if ((error = copyinstr(kg->kg_name, str, ksyms_maxlen, NULL))) 1131 break; 1132 if ((error = ksyms_getval(NULL, str, &val, KSYMS_EXTERN))) 1133 break; 1134 error = copyout(&val, kg->kg_value, sizeof(long)); 1135 break; 1136 1137 case KIOCGSYMBOL: 1138 /* 1139 * Use the in-kernel symbol lookup code for fast 1140 * retreival of a symbol. 1141 */ 1142 if ((error = copyinstr(kg->kg_name, str, ksyms_maxlen, NULL))) 1143 break; 1144 CIRCLEQ_FOREACH(st, &symtab_queue, sd_queue) { 1145 if ((sym = findsym(str, st)) == NULL) /* from userland */ 1146 continue; 1147 1148 /* Skip if bad binding */ 1149 if (ELF_ST_BIND(sym->st_info) != STB_GLOBAL) { 1150 sym = NULL; 1151 continue; 1152 } 1153 break; 1154 } 1155 /* 1156 * XXX which value of sym->st_name should be returned? The real 1157 * one, or the one that matches what reading /dev/ksyms get? 1158 * 1159 * Currently, we're returning the /dev/ksyms one. 1160 */ 1161 if (sym != NULL) 1162 error = copyout(sym, kg->kg_sym, sizeof(Elf_Sym)); 1163 else 1164 error = ENOENT; 1165 break; 1166 1167 case KIOCGSIZE: 1168 /* 1169 * Get total size of symbol table. 1170 */ 1171 *(int *)data = strsz + symsz + HDRSIZ; 1172 break; 1173 1174 default: 1175 error = ENOTTY; 1176 break; 1177 } 1178 1179 if (cmd == KIOCGVALUE || cmd == KIOCGSYMBOL) 1180 free(str, M_DEVBUF); 1181 1182 return error; 1183 } 1184 1185 const struct cdevsw ksyms_cdevsw = { 1186 ksymsopen, ksymsclose, ksymsread, ksymswrite, ksymsioctl, 1187 nullstop, notty, nopoll, nommap, nullkqfilter, DV_DULL 1188 }; 1189 #endif /* NKSYMS */ 1190