1 /*- 2 * Copyright (c) 2016 Mark Johnston <markj@FreeBSD.org> 3 * Copyright (c) 2010 The FreeBSD Foundation 4 * Copyright (c) 2008 John Birrell (jb@freebsd.org) 5 * All rights reserved. 6 * 7 * Portions of this software were developed by Rui Paulo under sponsorship 8 * from the FreeBSD Foundation. 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 AUTHOR AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> 33 __FBSDID("$FreeBSD$"); 34 35 #include <sys/types.h> 36 #ifndef NO_CTF 37 #include <sys/ctf.h> 38 #include <sys/ctf_api.h> 39 #endif 40 #include <sys/user.h> 41 42 #include <assert.h> 43 #include <err.h> 44 #include <fcntl.h> 45 #include <libgen.h> 46 #include <stdio.h> 47 #include <stdlib.h> 48 #include <string.h> 49 #include <unistd.h> 50 #ifndef NO_CTF 51 #include <libctf.h> 52 #endif 53 #include <libutil.h> 54 55 #include "crc32.h" 56 #include "_libproc.h" 57 58 #define PATH_DEBUG_DIR "/usr/lib/debug" 59 60 #ifdef NO_CTF 61 typedef struct ctf_file ctf_file_t; 62 #endif 63 64 #ifndef NO_CXA_DEMANGLE 65 extern char *__cxa_demangle(const char *, char *, size_t *, int *); 66 #endif /* NO_CXA_DEMANGLE */ 67 68 static int 69 crc32_file(int fd, uint32_t *crc) 70 { 71 uint8_t buf[PAGE_SIZE], *p; 72 size_t n; 73 74 *crc = ~0; 75 while ((n = read(fd, buf, sizeof(buf))) > 0) { 76 p = &buf[0]; 77 while (n-- > 0) 78 *crc = crc32_tab[(*crc ^ *p++) & 0xff] ^ (*crc >> 8); 79 } 80 *crc = ~*crc; 81 return (n); 82 } 83 84 static void 85 demangle(const char *symbol, char *buf, size_t len) 86 { 87 #ifndef NO_CXA_DEMANGLE 88 char *dembuf; 89 90 if (symbol[0] == '_' && symbol[1] == 'Z' && symbol[2]) { 91 dembuf = __cxa_demangle(symbol, NULL, NULL, NULL); 92 if (!dembuf) 93 goto fail; 94 strlcpy(buf, dembuf, len); 95 free(dembuf); 96 return; 97 } 98 fail: 99 #endif /* NO_CXA_DEMANGLE */ 100 strlcpy(buf, symbol, len); 101 } 102 103 static int 104 symvalcomp(void *thunk, const void *a1, const void *a2) 105 { 106 struct symtab *symtab; 107 GElf_Sym sym1, sym2; 108 u_int i1, i2; 109 int ret; 110 111 i1 = *(const u_int *)a1; 112 i2 = *(const u_int *)a2; 113 symtab = thunk; 114 115 (void)gelf_getsym(symtab->data, i1, &sym1); 116 (void)gelf_getsym(symtab->data, i2, &sym2); 117 if (sym1.st_value < sym2.st_value) 118 ret = -1; 119 else if (sym1.st_value == sym2.st_value) 120 ret = 0; 121 else 122 ret = 1; 123 return (ret); 124 } 125 126 static int 127 load_symtab(Elf *e, struct symtab *symtab, u_long sh_type) 128 { 129 GElf_Ehdr ehdr; 130 GElf_Shdr shdr; 131 Elf_Scn *scn; 132 u_int nsyms; 133 134 if (gelf_getehdr(e, &ehdr) == NULL) 135 return (-1); 136 137 scn = NULL; 138 while ((scn = elf_nextscn(e, scn)) != NULL) { 139 (void)gelf_getshdr(scn, &shdr); 140 if (shdr.sh_type == sh_type) 141 break; 142 } 143 if (scn == NULL) 144 return (-1); 145 146 if ((symtab->data = elf_getdata(scn, NULL)) == NULL) 147 return (-1); 148 149 nsyms = shdr.sh_size / shdr.sh_entsize; 150 151 symtab->index = calloc(nsyms, sizeof(u_int)); 152 if (symtab->index == NULL) 153 return (-1); 154 for (u_int i = 0; i < nsyms; i++) 155 symtab->index[i] = i; 156 qsort_r(symtab->index, nsyms, sizeof(u_int), symtab, symvalcomp); 157 symtab->nsyms = nsyms; 158 symtab->stridx = shdr.sh_link; 159 return (0); 160 } 161 162 static void 163 load_symtabs(struct file_info *file) 164 { 165 166 file->symtab.nsyms = file->dynsymtab.nsyms = 0; 167 (void)load_symtab(file->elf, &file->symtab, SHT_SYMTAB); 168 (void)load_symtab(file->elf, &file->dynsymtab, SHT_DYNSYM); 169 } 170 171 static int 172 open_debug_file(char *path, const char *debugfile, uint32_t crc) 173 { 174 size_t n; 175 uint32_t compcrc; 176 int fd; 177 178 fd = -1; 179 if ((n = strlcat(path, "/", PATH_MAX)) >= PATH_MAX) 180 return (fd); 181 if (strlcat(path, debugfile, PATH_MAX) >= PATH_MAX) 182 goto out; 183 if ((fd = open(path, O_RDONLY | O_CLOEXEC)) < 0) 184 goto out; 185 if (crc32_file(fd, &compcrc) != 0 || crc != compcrc) { 186 DPRINTFX("ERROR: CRC32 mismatch for %s", path); 187 (void)close(fd); 188 fd = -1; 189 } 190 out: 191 path[n] = '\0'; 192 return (fd); 193 } 194 195 /* 196 * Obtain an ELF descriptor for the specified mapped object. If a GNU debuglink 197 * section is present, a descriptor for the corresponding debug file is 198 * returned. 199 */ 200 static int 201 open_object(struct map_info *mapping) 202 { 203 char path[PATH_MAX]; 204 GElf_Shdr shdr; 205 Elf *e, *e2; 206 Elf_Data *data; 207 Elf_Scn *scn; 208 struct file_info *file; 209 prmap_t *map; 210 const char *debugfile, *scnname; 211 size_t ndx; 212 uint32_t crc; 213 int fd, fd2; 214 215 if (mapping->map.pr_mapname[0] == '\0') 216 return (-1); /* anonymous object */ 217 if (mapping->file->elf != NULL) 218 return (0); /* already loaded */ 219 220 file = mapping->file; 221 map = &mapping->map; 222 if ((fd = open(map->pr_mapname, O_RDONLY | O_CLOEXEC)) < 0) { 223 DPRINTF("ERROR: open %s failed", map->pr_mapname); 224 return (-1); 225 } 226 if ((e = elf_begin(fd, ELF_C_READ, NULL)) == NULL) { 227 DPRINTFX("ERROR: elf_begin() failed: %s", elf_errmsg(-1)); 228 goto err; 229 } 230 if (gelf_getehdr(e, &file->ehdr) != &file->ehdr) { 231 DPRINTFX("ERROR: elf_getehdr() failed: %s", elf_errmsg(-1)); 232 goto err; 233 } 234 235 scn = NULL; 236 while ((scn = elf_nextscn(e, scn)) != NULL) { 237 if (gelf_getshdr(scn, &shdr) != &shdr) { 238 DPRINTFX("ERROR: gelf_getshdr failed: %s", 239 elf_errmsg(-1)); 240 goto err; 241 } 242 if (shdr.sh_type != SHT_PROGBITS) 243 continue; 244 if (elf_getshdrstrndx(e, &ndx) != 0) { 245 DPRINTFX("ERROR: elf_getshdrstrndx failed: %s", 246 elf_errmsg(-1)); 247 goto err; 248 } 249 if ((scnname = elf_strptr(e, ndx, shdr.sh_name)) == NULL) 250 continue; 251 252 if (strcmp(scnname, ".gnu_debuglink") == 0) 253 break; 254 } 255 if (scn == NULL) 256 goto internal; 257 258 if ((data = elf_getdata(scn, NULL)) == NULL) { 259 DPRINTFX("ERROR: elf_getdata failed: %s", elf_errmsg(-1)); 260 goto err; 261 } 262 263 /* 264 * The data contains a null-terminated file name followed by a 4-byte 265 * CRC. 266 */ 267 if (data->d_size < sizeof(crc) + 1) { 268 DPRINTFX("ERROR: debuglink section is too small (%zd bytes)", 269 data->d_size); 270 goto internal; 271 } 272 if (strnlen(data->d_buf, data->d_size) >= data->d_size - sizeof(crc)) { 273 DPRINTFX("ERROR: no null-terminator in gnu_debuglink section"); 274 goto internal; 275 } 276 277 debugfile = data->d_buf; 278 memcpy(&crc, (char *)data->d_buf + data->d_size - sizeof(crc), 279 sizeof(crc)); 280 281 /* 282 * Search for the debug file using the algorithm described in the gdb 283 * documentation: 284 * - look in the directory containing the object, 285 * - look in the subdirectory ".debug" of the directory containing the 286 * object, 287 * - look in the global debug directories (currently /usr/lib/debug). 288 */ 289 (void)strlcpy(path, map->pr_mapname, sizeof(path)); 290 (void)dirname(path); 291 292 if ((fd2 = open_debug_file(path, debugfile, crc)) >= 0) 293 goto external; 294 295 if (strlcat(path, "/.debug", sizeof(path)) < sizeof(path) && 296 (fd2 = open_debug_file(path, debugfile, crc)) >= 0) 297 goto external; 298 299 (void)snprintf(path, sizeof(path), PATH_DEBUG_DIR); 300 if (strlcat(path, map->pr_mapname, sizeof(path)) < sizeof(path)) { 301 (void)dirname(path); 302 if ((fd2 = open_debug_file(path, debugfile, crc)) >= 0) 303 goto external; 304 } 305 306 internal: 307 /* We didn't find a debug file, just return the object's descriptor. */ 308 file->elf = e; 309 file->fd = fd; 310 load_symtabs(file); 311 return (0); 312 313 external: 314 if ((e2 = elf_begin(fd2, ELF_C_READ, NULL)) == NULL) { 315 DPRINTFX("ERROR: elf_begin failed: %s", elf_errmsg(-1)); 316 (void)close(fd2); 317 goto err; 318 } 319 (void)elf_end(e); 320 (void)close(fd); 321 file->elf = e2; 322 file->fd = fd2; 323 load_symtabs(file); 324 return (0); 325 326 err: 327 if (e != NULL) 328 (void)elf_end(e); 329 (void)close(fd); 330 return (-1); 331 } 332 333 char * 334 proc_objname(struct proc_handle *p, uintptr_t addr, char *objname, 335 size_t objnamesz) 336 { 337 prmap_t *map; 338 size_t i; 339 340 for (i = 0; i < p->nmappings; i++) { 341 map = &p->mappings[i].map; 342 if (addr >= map->pr_vaddr && 343 addr < map->pr_vaddr + map->pr_size) { 344 strlcpy(objname, map->pr_mapname, objnamesz); 345 return (objname); 346 } 347 } 348 return (NULL); 349 } 350 351 int 352 proc_iter_objs(struct proc_handle *p, proc_map_f *func, void *cd) 353 { 354 char last[MAXPATHLEN], path[MAXPATHLEN], *base; 355 prmap_t *map; 356 size_t i; 357 int error; 358 359 if (p->nmappings == 0) 360 if (proc_rdagent(p) == NULL) 361 return (-1); 362 363 error = 0; 364 memset(last, 0, sizeof(last)); 365 for (i = 0; i < p->nmappings; i++) { 366 map = &p->mappings[i].map; 367 strlcpy(path, map->pr_mapname, sizeof(path)); 368 base = basename(path); 369 /* 370 * We shouldn't call the callback twice with the same object. 371 * To do that we are assuming the fact that if there are 372 * repeated object names (i.e. different mappings for the 373 * same object) they occur next to each other. 374 */ 375 if (strcmp(base, last) == 0) 376 continue; 377 if ((error = (*func)(cd, map, base)) != 0) 378 break; 379 strlcpy(last, path, sizeof(last)); 380 } 381 return (error); 382 } 383 384 static struct map_info * 385 _proc_addr2map(struct proc_handle *p, uintptr_t addr) 386 { 387 struct map_info *mapping; 388 size_t i; 389 390 if (p->nmappings == 0) 391 if (proc_rdagent(p) == NULL) 392 return (NULL); 393 for (i = 0; i < p->nmappings; i++) { 394 mapping = &p->mappings[i]; 395 if (addr >= mapping->map.pr_vaddr && 396 addr < mapping->map.pr_vaddr + mapping->map.pr_size) 397 return (mapping); 398 } 399 return (NULL); 400 } 401 402 prmap_t * 403 proc_addr2map(struct proc_handle *p, uintptr_t addr) 404 { 405 406 return (&_proc_addr2map(p, addr)->map); 407 } 408 409 /* 410 * Look up the symbol at addr using a binary search, returning a copy of the 411 * symbol and its name. 412 */ 413 static int 414 lookup_symbol_by_addr(Elf *elf, struct symtab *symtab, uintptr_t addr, 415 const char **namep, GElf_Sym *sym) 416 { 417 Elf_Data *data; 418 const char *s; 419 int min, max, mid; 420 421 data = symtab->data; 422 min = 0; 423 max = symtab->nsyms - 1; 424 425 while (min <= max) { 426 mid = (max + min) / 2; 427 (void)gelf_getsym(data, symtab->index[mid], sym); 428 if (addr >= sym->st_value && 429 addr < sym->st_value + sym->st_size) { 430 s = elf_strptr(elf, symtab->stridx, sym->st_name); 431 if (s != NULL && namep != NULL) 432 *namep = s; 433 return (0); 434 } 435 436 if (addr < sym->st_value) 437 max = mid - 1; 438 else 439 min = mid + 1; 440 } 441 return (ENOENT); 442 } 443 444 int 445 proc_addr2sym(struct proc_handle *p, uintptr_t addr, char *name, 446 size_t namesz, GElf_Sym *symcopy) 447 { 448 struct file_info *file; 449 struct map_info *mapping; 450 const char *s; 451 uintptr_t off; 452 int error; 453 454 if ((mapping = _proc_addr2map(p, addr)) == NULL) { 455 DPRINTFX("ERROR: proc_addr2map failed to resolve 0x%jx", addr); 456 return (-1); 457 } 458 if (open_object(mapping) != 0) { 459 DPRINTFX("ERROR: failed to open object %s", 460 mapping->map.pr_mapname); 461 return (-1); 462 } 463 464 file = mapping->file; 465 off = file->ehdr.e_type == ET_DYN ? mapping->map.pr_vaddr : 0; 466 if (addr < off) 467 return (ENOENT); 468 addr -= off; 469 470 error = lookup_symbol_by_addr(file->elf, &file->dynsymtab, addr, &s, 471 symcopy); 472 if (error == ENOENT) 473 error = lookup_symbol_by_addr(file->elf, &file->symtab, addr, 474 &s, symcopy); 475 if (error == 0) { 476 symcopy->st_value += off; 477 demangle(s, name, namesz); 478 } 479 return (error); 480 } 481 482 static struct map_info * 483 _proc_name2map(struct proc_handle *p, const char *name) 484 { 485 char path[MAXPATHLEN], *base; 486 struct map_info *mapping; 487 size_t i, len; 488 489 if ((len = strlen(name)) == 0) 490 return (NULL); 491 if (p->nmappings == 0) 492 if (proc_rdagent(p) == NULL) 493 return (NULL); 494 for (i = 0; i < p->nmappings; i++) { 495 mapping = &p->mappings[i]; 496 (void)strlcpy(path, mapping->map.pr_mapname, sizeof(path)); 497 base = basename(path); 498 if (strcmp(base, name) == 0) 499 return (mapping); 500 } 501 /* If we didn't find a match, try matching prefixes of the basename. */ 502 for (i = 0; i < p->nmappings; i++) { 503 strlcpy(path, p->mappings[i].map.pr_mapname, sizeof(path)); 504 base = basename(path); 505 if (strncmp(base, name, len) == 0) 506 return (&p->mappings[i]); 507 } 508 if (strcmp(name, "a.out") == 0) 509 return (_proc_addr2map(p, p->exec_map->pr_vaddr)); 510 return (NULL); 511 } 512 513 prmap_t * 514 proc_name2map(struct proc_handle *p, const char *name) 515 { 516 517 return (&_proc_name2map(p, name)->map); 518 } 519 520 /* 521 * Look up the symbol with the given name and return a copy of it. 522 */ 523 static int 524 lookup_symbol_by_name(Elf *elf, struct symtab *symtab, const char *symbol, 525 GElf_Sym *symcopy, prsyminfo_t *si) 526 { 527 GElf_Sym sym; 528 Elf_Data *data; 529 char *s; 530 int i; 531 532 if (symtab->nsyms == 0) 533 return (ENOENT); 534 data = symtab->data; 535 for (i = 0; gelf_getsym(data, i, &sym) != NULL; i++) { 536 s = elf_strptr(elf, symtab->stridx, sym.st_name); 537 if (s != NULL && strcmp(s, symbol) == 0) { 538 memcpy(symcopy, &sym, sizeof(*symcopy)); 539 if (si != NULL) 540 si->prs_id = i; 541 return (0); 542 } 543 } 544 return (ENOENT); 545 } 546 547 int 548 proc_name2sym(struct proc_handle *p, const char *object, const char *symbol, 549 GElf_Sym *symcopy, prsyminfo_t *si) 550 { 551 struct file_info *file; 552 struct map_info *mapping; 553 uintptr_t off; 554 int error; 555 556 if ((mapping = _proc_name2map(p, object)) == NULL) { 557 DPRINTFX("ERROR: proc_name2map failed to resolve %s", object); 558 return (-1); 559 } 560 if (open_object(mapping) != 0) { 561 DPRINTFX("ERROR: failed to open object %s", 562 mapping->map.pr_mapname); 563 return (-1); 564 } 565 566 file = mapping->file; 567 off = file->ehdr.e_type == ET_DYN ? mapping->map.pr_vaddr : 0; 568 569 error = lookup_symbol_by_name(file->elf, &file->dynsymtab, symbol, 570 symcopy, si); 571 if (error == ENOENT) 572 error = lookup_symbol_by_name(file->elf, &file->symtab, symbol, 573 symcopy, si); 574 if (error == 0) 575 symcopy->st_value += off; 576 return (error); 577 } 578 579 ctf_file_t * 580 proc_name2ctf(struct proc_handle *p, const char *name) 581 { 582 #ifndef NO_CTF 583 ctf_file_t *ctf; 584 prmap_t *map; 585 int error; 586 587 if ((map = proc_name2map(p, name)) == NULL) 588 return (NULL); 589 590 ctf = ctf_open(map->pr_mapname, &error); 591 return (ctf); 592 #else 593 (void)p; 594 (void)name; 595 return (NULL); 596 #endif 597 } 598 599 int 600 proc_iter_symbyaddr(struct proc_handle *p, const char *object, int which, 601 int mask, proc_sym_f *func, void *cd) 602 { 603 GElf_Sym sym; 604 struct file_info *file; 605 struct map_info *mapping; 606 struct symtab *symtab; 607 const char *s; 608 int error, i; 609 610 if ((mapping = _proc_name2map(p, object)) == NULL) { 611 DPRINTFX("ERROR: proc_name2map failed to resolve %s", object); 612 return (-1); 613 } 614 if (open_object(mapping) != 0) { 615 DPRINTFX("ERROR: failed to open object %s", 616 mapping->map.pr_mapname); 617 return (-1); 618 } 619 620 file = mapping->file; 621 symtab = which == PR_SYMTAB ? &file->symtab : &file->dynsymtab; 622 if (symtab->nsyms == 0) 623 return (-1); 624 625 error = 0; 626 for (i = 0; gelf_getsym(symtab->data, i, &sym) != NULL; i++) { 627 if (GELF_ST_BIND(sym.st_info) == STB_LOCAL && 628 (mask & BIND_LOCAL) == 0) 629 continue; 630 if (GELF_ST_BIND(sym.st_info) == STB_GLOBAL && 631 (mask & BIND_GLOBAL) == 0) 632 continue; 633 if (GELF_ST_BIND(sym.st_info) == STB_WEAK && 634 (mask & BIND_WEAK) == 0) 635 continue; 636 if (GELF_ST_TYPE(sym.st_info) == STT_NOTYPE && 637 (mask & TYPE_NOTYPE) == 0) 638 continue; 639 if (GELF_ST_TYPE(sym.st_info) == STT_OBJECT && 640 (mask & TYPE_OBJECT) == 0) 641 continue; 642 if (GELF_ST_TYPE(sym.st_info) == STT_FUNC && 643 (mask & TYPE_FUNC) == 0) 644 continue; 645 if (GELF_ST_TYPE(sym.st_info) == STT_SECTION && 646 (mask & TYPE_SECTION) == 0) 647 continue; 648 if (GELF_ST_TYPE(sym.st_info) == STT_FILE && 649 (mask & TYPE_FILE) == 0) 650 continue; 651 s = elf_strptr(file->elf, symtab->stridx, sym.st_name); 652 if (file->ehdr.e_type == ET_DYN) 653 sym.st_value += mapping->map.pr_vaddr; 654 if ((error = (*func)(cd, &sym, s)) != 0) 655 break; 656 } 657 return (error); 658 } 659