1 /*- 2 * Copyright (c) 2009 Kai Wang 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 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27 #include <sys/param.h> 28 #include <sys/tree.h> 29 30 #include <capsicum_helpers.h> 31 #include <dwarf.h> 32 #include <err.h> 33 #include <fcntl.h> 34 #include <gelf.h> 35 #include <getopt.h> 36 #include <libdwarf.h> 37 #include <libelftc.h> 38 #include <libgen.h> 39 #include <stdio.h> 40 #include <stdlib.h> 41 #include <string.h> 42 43 #include "_elftc.h" 44 45 ELFTC_VCSID("$Id: addr2line.c 3499 2016-11-25 16:06:29Z emaste $"); 46 47 struct Func { 48 char *name; 49 Dwarf_Unsigned lopc; 50 Dwarf_Unsigned hipc; 51 Dwarf_Unsigned call_file; 52 Dwarf_Unsigned call_line; 53 Dwarf_Ranges *ranges; 54 Dwarf_Signed ranges_cnt; 55 struct Func *inlined_caller; 56 STAILQ_ENTRY(Func) next; 57 }; 58 59 struct CU { 60 RB_ENTRY(CU) entry; 61 Dwarf_Off off; 62 Dwarf_Unsigned lopc; 63 Dwarf_Unsigned hipc; 64 char **srcfiles; 65 Dwarf_Signed nsrcfiles; 66 STAILQ_HEAD(, Func) funclist; 67 Dwarf_Die die; 68 }; 69 70 static struct option longopts[] = { 71 {"addresses", no_argument, NULL, 'a'}, 72 {"target" , required_argument, NULL, 'b'}, 73 {"demangle", no_argument, NULL, 'C'}, 74 {"exe", required_argument, NULL, 'e'}, 75 {"functions", no_argument, NULL, 'f'}, 76 {"inlines", no_argument, NULL, 'i'}, 77 {"section", required_argument, NULL, 'j'}, 78 {"pretty-print", no_argument, NULL, 'p'}, 79 {"basename", no_argument, NULL, 's'}, 80 {"help", no_argument, NULL, 'H'}, 81 {"version", no_argument, NULL, 'V'}, 82 {NULL, 0, NULL, 0} 83 }; 84 85 static int demangle, func, base, inlines, print_addr, pretty_print; 86 static char unknown[] = { '?', '?', '\0' }; 87 static Dwarf_Addr section_base; 88 /* Need a new curlopc that stores last lopc value. */ 89 static Dwarf_Unsigned curlopc = ~0ULL; 90 static RB_HEAD(cutree, CU) cuhead = RB_INITIALIZER(&cuhead); 91 92 static int 93 lopccmp(struct CU *e1, struct CU *e2) 94 { 95 return (e1->lopc < e2->lopc ? -1 : e1->lopc > e2->lopc); 96 } 97 98 RB_PROTOTYPE(cutree, CU, entry, lopccmp); 99 RB_GENERATE(cutree, CU, entry, lopccmp) 100 101 #define USAGE_MESSAGE "\ 102 Usage: %s [options] hexaddress...\n\ 103 Map program addresses to source file names and line numbers.\n\n\ 104 Options:\n\ 105 -a | --addresses Display address prior to line number info.\n\ 106 -b TGT | --target=TGT (Accepted but ignored).\n\ 107 -e EXE | --exe=EXE Use program \"EXE\" to translate addresses.\n\ 108 -f | --functions Display function names.\n\ 109 -i | --inlines Display caller info for inlined functions.\n\ 110 -j NAME | --section=NAME Values are offsets into section \"NAME\".\n\ 111 -p | --pretty-print Display line number info and function name\n\ 112 in human readable manner.\n\ 113 -s | --basename Only show the base name for each file name.\n\ 114 -C | --demangle Demangle C++ names.\n\ 115 -H | --help Print a help message.\n\ 116 -V | --version Print a version identifier and exit.\n" 117 118 static void 119 usage(void) 120 { 121 (void) fprintf(stderr, USAGE_MESSAGE, ELFTC_GETPROGNAME()); 122 exit(1); 123 } 124 125 static void 126 version(void) 127 { 128 129 fprintf(stderr, "%s (%s)\n", ELFTC_GETPROGNAME(), elftc_version()); 130 exit(0); 131 } 132 133 /* 134 * Handle DWARF 4 'offset from' DW_AT_high_pc. Although we don't 135 * fully support DWARF 4, some compilers (like FreeBSD Clang 3.5.1) 136 * generate DW_AT_high_pc as an offset from DW_AT_low_pc. 137 * 138 * "If the value of the DW_AT_high_pc is of class address, it is the 139 * relocated address of the first location past the last instruction 140 * associated with the entity; if it is of class constant, the value 141 * is an unsigned integer offset which when added to the low PC gives 142 * the address of the first location past the last instruction 143 * associated with the entity." 144 * 145 * DWARF4 spec, section 2.17.2. 146 */ 147 static int 148 handle_high_pc(Dwarf_Die die, Dwarf_Unsigned lopc, Dwarf_Unsigned *hipc) 149 { 150 Dwarf_Error de; 151 Dwarf_Half form; 152 Dwarf_Attribute at; 153 int ret; 154 155 ret = dwarf_attr(die, DW_AT_high_pc, &at, &de); 156 if (ret == DW_DLV_ERROR) { 157 warnx("dwarf_attr failed: %s", dwarf_errmsg(de)); 158 return (ret); 159 } 160 ret = dwarf_whatform(at, &form, &de); 161 if (ret == DW_DLV_ERROR) { 162 warnx("dwarf_whatform failed: %s", dwarf_errmsg(de)); 163 return (ret); 164 } 165 if (dwarf_get_form_class(2, 0, 0, form) == DW_FORM_CLASS_CONSTANT) 166 *hipc += lopc; 167 168 return (DW_DLV_OK); 169 } 170 171 static struct Func * 172 search_func(struct CU *cu, Dwarf_Unsigned addr) 173 { 174 struct Func *f, *f0; 175 Dwarf_Unsigned lopc, hipc, addr_base; 176 int i; 177 178 f0 = NULL; 179 180 STAILQ_FOREACH(f, &cu->funclist, next) { 181 if (f->ranges != NULL) { 182 addr_base = 0; 183 for (i = 0; i < f->ranges_cnt; i++) { 184 if (f->ranges[i].dwr_type == DW_RANGES_END) 185 break; 186 if (f->ranges[i].dwr_type == 187 DW_RANGES_ADDRESS_SELECTION) { 188 addr_base = f->ranges[i].dwr_addr2; 189 continue; 190 } 191 192 /* DW_RANGES_ENTRY */ 193 lopc = f->ranges[i].dwr_addr1 + addr_base; 194 hipc = f->ranges[i].dwr_addr2 + addr_base; 195 if (addr >= lopc && addr < hipc) { 196 if (f0 == NULL || 197 (lopc >= f0->lopc && 198 hipc <= f0->hipc)) { 199 f0 = f; 200 f0->lopc = lopc; 201 f0->hipc = hipc; 202 break; 203 } 204 } 205 } 206 } else if (addr >= f->lopc && addr < f->hipc) { 207 if (f0 == NULL || 208 (f->lopc >= f0->lopc && f->hipc <= f0->hipc)) 209 f0 = f; 210 } 211 } 212 213 return (f0); 214 } 215 216 static void 217 collect_func(Dwarf_Debug dbg, Dwarf_Die die, struct Func *parent, struct CU *cu) 218 { 219 Dwarf_Die ret_die, abst_die, spec_die; 220 Dwarf_Error de; 221 Dwarf_Half tag; 222 Dwarf_Unsigned lopc, hipc, ranges_off; 223 Dwarf_Signed ranges_cnt; 224 Dwarf_Off ref; 225 Dwarf_Attribute abst_at, spec_at; 226 Dwarf_Ranges *ranges; 227 const char *funcname; 228 struct Func *f; 229 int found_ranges, ret; 230 231 f = NULL; 232 abst_die = spec_die = NULL; 233 234 if (dwarf_tag(die, &tag, &de)) { 235 warnx("dwarf_tag: %s", dwarf_errmsg(de)); 236 goto cont_search; 237 } 238 if (tag == DW_TAG_subprogram || tag == DW_TAG_entry_point || 239 tag == DW_TAG_inlined_subroutine) { 240 /* 241 * Function address range can be specified by either 242 * a DW_AT_ranges attribute which points to a range list or 243 * by a pair of DW_AT_low_pc and DW_AT_high_pc attributes. 244 */ 245 ranges = NULL; 246 ranges_cnt = 0; 247 found_ranges = 0; 248 if (dwarf_attrval_unsigned(die, DW_AT_ranges, &ranges_off, 249 &de) == DW_DLV_OK && 250 dwarf_get_ranges(dbg, (Dwarf_Off) ranges_off, &ranges, 251 &ranges_cnt, NULL, &de) == DW_DLV_OK) { 252 if (ranges != NULL && ranges_cnt > 0) { 253 found_ranges = 1; 254 goto get_func_name; 255 } 256 } 257 258 /* 259 * Search for DW_AT_low_pc/DW_AT_high_pc if ranges pointer 260 * not found. 261 */ 262 if (dwarf_attrval_unsigned(die, DW_AT_low_pc, &lopc, &de) || 263 dwarf_attrval_unsigned(die, DW_AT_high_pc, &hipc, &de)) 264 goto cont_search; 265 if (handle_high_pc(die, lopc, &hipc) != DW_DLV_OK) 266 goto cont_search; 267 268 get_func_name: 269 /* 270 * Most common case the function name is stored in DW_AT_name 271 * attribute. 272 */ 273 if (dwarf_attrval_string(die, DW_AT_name, &funcname, &de) == 274 DW_DLV_OK) 275 goto add_func; 276 277 /* 278 * For inlined function, the actual name is probably in the DIE 279 * referenced by DW_AT_abstract_origin. (if present) 280 */ 281 if (dwarf_attr(die, DW_AT_abstract_origin, &abst_at, &de) == 282 DW_DLV_OK && 283 dwarf_global_formref(abst_at, &ref, &de) == DW_DLV_OK && 284 dwarf_offdie(dbg, ref, &abst_die, &de) == DW_DLV_OK && 285 dwarf_attrval_string(abst_die, DW_AT_name, &funcname, 286 &de) == DW_DLV_OK) 287 goto add_func; 288 289 /* 290 * If DW_AT_name is not present, but DW_AT_specification is 291 * present, then probably the actual name is in the DIE 292 * referenced by DW_AT_specification. 293 */ 294 if (dwarf_attr(die, DW_AT_specification, &spec_at, &de) == 295 DW_DLV_OK && 296 dwarf_global_formref(spec_at, &ref, &de) == DW_DLV_OK && 297 dwarf_offdie(dbg, ref, &spec_die, &de) == DW_DLV_OK && 298 dwarf_attrval_string(spec_die, DW_AT_name, &funcname, 299 &de) == DW_DLV_OK) 300 goto add_func; 301 302 /* Skip if no name associated with this DIE. */ 303 goto cont_search; 304 305 add_func: 306 if ((f = calloc(1, sizeof(*f))) == NULL) 307 err(EXIT_FAILURE, "calloc"); 308 if ((f->name = strdup(funcname)) == NULL) 309 err(EXIT_FAILURE, "strdup"); 310 if (found_ranges) { 311 f->ranges = ranges; 312 f->ranges_cnt = ranges_cnt; 313 } else { 314 f->lopc = lopc; 315 f->hipc = hipc; 316 } 317 if (tag == DW_TAG_inlined_subroutine) { 318 f->inlined_caller = parent; 319 dwarf_attrval_unsigned(die, DW_AT_call_file, 320 &f->call_file, &de); 321 dwarf_attrval_unsigned(die, DW_AT_call_line, 322 &f->call_line, &de); 323 } 324 STAILQ_INSERT_TAIL(&cu->funclist, f, next); 325 } 326 327 cont_search: 328 329 /* Search children. */ 330 ret = dwarf_child(die, &ret_die, &de); 331 if (ret == DW_DLV_ERROR) 332 warnx("dwarf_child: %s", dwarf_errmsg(de)); 333 else if (ret == DW_DLV_OK) { 334 if (f != NULL) 335 collect_func(dbg, ret_die, f, cu); 336 else 337 collect_func(dbg, ret_die, parent, cu); 338 } 339 340 /* Search sibling. */ 341 ret = dwarf_siblingof(dbg, die, &ret_die, &de); 342 if (ret == DW_DLV_ERROR) 343 warnx("dwarf_siblingof: %s", dwarf_errmsg(de)); 344 else if (ret == DW_DLV_OK) 345 collect_func(dbg, ret_die, parent, cu); 346 347 /* Cleanup */ 348 dwarf_dealloc(dbg, die, DW_DLA_DIE); 349 350 if (abst_die != NULL) 351 dwarf_dealloc(dbg, abst_die, DW_DLA_DIE); 352 353 if (spec_die != NULL) 354 dwarf_dealloc(dbg, spec_die, DW_DLA_DIE); 355 } 356 357 static void 358 print_inlines(struct CU *cu, struct Func *f, Dwarf_Unsigned call_file, 359 Dwarf_Unsigned call_line) 360 { 361 char demangled[1024]; 362 char *file; 363 364 if (call_file > 0 && (Dwarf_Signed) call_file <= cu->nsrcfiles) 365 file = cu->srcfiles[call_file - 1]; 366 else 367 file = unknown; 368 369 if (pretty_print) 370 printf(" (inlined by) "); 371 372 if (func) { 373 if (demangle && !elftc_demangle(f->name, demangled, 374 sizeof(demangled), 0)) { 375 if (pretty_print) 376 printf("%s at ", demangled); 377 else 378 printf("%s\n", demangled); 379 } else { 380 if (pretty_print) 381 printf("%s at ", f->name); 382 else 383 printf("%s\n", f->name); 384 } 385 } 386 (void) printf("%s:%ju\n", base ? basename(file) : file, 387 (uintmax_t) call_line); 388 389 if (f->inlined_caller != NULL) 390 print_inlines(cu, f->inlined_caller, f->call_file, 391 f->call_line); 392 } 393 394 static struct CU * 395 culookup(Dwarf_Unsigned addr) 396 { 397 struct CU find, *res; 398 399 find.lopc = addr; 400 res = RB_NFIND(cutree, &cuhead, &find); 401 if (res != NULL) { 402 if (res->lopc != addr) 403 res = RB_PREV(cutree, &cuhead, res); 404 if (res != NULL && addr >= res->lopc && addr < res->hipc) 405 return (res); 406 } else { 407 res = RB_MAX(cutree, &cuhead); 408 if (res != NULL && addr >= res->lopc && addr < res->hipc) 409 return (res); 410 } 411 return (NULL); 412 } 413 414 static void 415 translate(Dwarf_Debug dbg, Elf *e, const char* addrstr) 416 { 417 Dwarf_Die die, ret_die; 418 Dwarf_Line *lbuf; 419 Dwarf_Error de; 420 Dwarf_Half tag; 421 Dwarf_Unsigned lopc, hipc, addr, lineno, plineno; 422 Dwarf_Signed lcount; 423 Dwarf_Addr lineaddr, plineaddr; 424 Dwarf_Off off; 425 struct CU *cu; 426 struct Func *f; 427 const char *funcname; 428 char *file, *file0, *pfile; 429 char demangled[1024]; 430 int ec, i, ret; 431 432 addr = strtoull(addrstr, NULL, 16); 433 addr += section_base; 434 lineno = 0; 435 file = unknown; 436 die = NULL; 437 ret = DW_DLV_OK; 438 439 cu = culookup(addr); 440 if (cu != NULL) { 441 die = cu->die; 442 goto status_ok; 443 } 444 445 while (true) { 446 /* 447 * We resume the CU scan from the last place we found a match. 448 * Because when we have 2 sequential addresses, and the second 449 * one is of the next CU, it is faster to just go to the next CU 450 * instead of starting from the beginning. 451 */ 452 ret = dwarf_next_cu_header(dbg, NULL, NULL, NULL, NULL, NULL, 453 &de); 454 if (ret == DW_DLV_NO_ENTRY) { 455 if (curlopc == ~0ULL) 456 goto out; 457 ret = dwarf_next_cu_header(dbg, NULL, NULL, NULL, NULL, 458 NULL, &de); 459 } 460 die = NULL; 461 while (dwarf_siblingof(dbg, die, &ret_die, &de) == DW_DLV_OK) { 462 if (die != NULL) 463 dwarf_dealloc(dbg, die, DW_DLA_DIE); 464 die = ret_die; 465 if (dwarf_tag(die, &tag, &de) != DW_DLV_OK) { 466 warnx("dwarf_tag failed: %s", 467 dwarf_errmsg(de)); 468 goto next_cu; 469 } 470 471 /* XXX: What about DW_TAG_partial_unit? */ 472 if (tag == DW_TAG_compile_unit) 473 break; 474 } 475 476 if (ret_die == NULL) { 477 warnx("could not find DW_TAG_compile_unit die"); 478 goto next_cu; 479 } 480 if (dwarf_attrval_unsigned(die, DW_AT_low_pc, &lopc, &de) == 481 DW_DLV_OK) { 482 if (lopc == curlopc) 483 goto out; 484 if (dwarf_attrval_unsigned(die, DW_AT_high_pc, &hipc, 485 &de) == DW_DLV_OK) { 486 /* 487 * Check if the address falls into the PC 488 * range of this CU. 489 */ 490 if (handle_high_pc(die, lopc, &hipc) != 491 DW_DLV_OK) 492 goto out; 493 } else { 494 /* Assume ~0ULL if DW_AT_high_pc not present */ 495 hipc = ~0ULL; 496 } 497 498 if (dwarf_dieoffset(die, &off, &de) != DW_DLV_OK) { 499 warnx("dwarf_dieoffset failed: %s", 500 dwarf_errmsg(de)); 501 goto out; 502 } 503 504 if (addr >= lopc && addr < hipc) { 505 if ((cu = calloc(1, sizeof(*cu))) == NULL) 506 err(EXIT_FAILURE, "calloc"); 507 cu->off = off; 508 cu->lopc = lopc; 509 cu->hipc = hipc; 510 cu->die = die; 511 STAILQ_INIT(&cu->funclist); 512 RB_INSERT(cutree, &cuhead, cu); 513 514 curlopc = lopc; 515 break; 516 } 517 } 518 next_cu: 519 if (die != NULL) { 520 dwarf_dealloc(dbg, die, DW_DLA_DIE); 521 die = NULL; 522 } 523 } 524 525 if (ret != DW_DLV_OK || die == NULL) 526 goto out; 527 528 status_ok: 529 switch (dwarf_srclines(die, &lbuf, &lcount, &de)) { 530 case DW_DLV_OK: 531 break; 532 case DW_DLV_NO_ENTRY: 533 /* If a CU lacks debug info, just skip it. */ 534 goto out; 535 default: 536 warnx("dwarf_srclines: %s", dwarf_errmsg(de)); 537 goto out; 538 } 539 540 plineaddr = ~0ULL; 541 plineno = 0; 542 pfile = unknown; 543 for (i = 0; i < lcount; i++) { 544 if (dwarf_lineaddr(lbuf[i], &lineaddr, &de)) { 545 warnx("dwarf_lineaddr: %s", dwarf_errmsg(de)); 546 goto out; 547 } 548 if (dwarf_lineno(lbuf[i], &lineno, &de)) { 549 warnx("dwarf_lineno: %s", dwarf_errmsg(de)); 550 goto out; 551 } 552 if (dwarf_linesrc(lbuf[i], &file0, &de)) { 553 warnx("dwarf_linesrc: %s", dwarf_errmsg(de)); 554 } else 555 file = file0; 556 if (addr == lineaddr) 557 goto out; 558 else if (addr < lineaddr && addr > plineaddr) { 559 lineno = plineno; 560 file = pfile; 561 goto out; 562 } 563 plineaddr = lineaddr; 564 plineno = lineno; 565 pfile = file; 566 } 567 568 out: 569 f = NULL; 570 funcname = NULL; 571 if (ret == DW_DLV_OK && (func || inlines) && cu != NULL) { 572 if (cu->srcfiles == NULL) 573 if (dwarf_srcfiles(die, &cu->srcfiles, &cu->nsrcfiles, 574 &de)) 575 warnx("dwarf_srcfiles: %s", dwarf_errmsg(de)); 576 if (STAILQ_EMPTY(&cu->funclist)) { 577 collect_func(dbg, die, NULL, cu); 578 die = NULL; 579 } 580 f = search_func(cu, addr); 581 if (f != NULL) 582 funcname = f->name; 583 } 584 585 if (print_addr) { 586 if ((ec = gelf_getclass(e)) == ELFCLASSNONE) { 587 warnx("gelf_getclass failed: %s", elf_errmsg(-1)); 588 ec = ELFCLASS64; 589 } 590 if (ec == ELFCLASS32) { 591 if (pretty_print) 592 printf("0x%08jx: ", (uintmax_t) addr); 593 else 594 printf("0x%08jx\n", (uintmax_t) addr); 595 } else { 596 if (pretty_print) 597 printf("0x%016jx: ", (uintmax_t) addr); 598 else 599 printf("0x%016jx\n", (uintmax_t) addr); 600 } 601 } 602 603 if (func) { 604 if (funcname == NULL) 605 funcname = unknown; 606 if (demangle && !elftc_demangle(funcname, demangled, 607 sizeof(demangled), 0)) { 608 if (pretty_print) 609 printf("%s at ", demangled); 610 else 611 printf("%s\n", demangled); 612 } else { 613 if (pretty_print) 614 printf("%s at ", funcname); 615 else 616 printf("%s\n", funcname); 617 } 618 } 619 620 (void) printf("%s:%ju\n", base ? basename(file) : file, 621 (uintmax_t) lineno); 622 623 if (ret == DW_DLV_OK && inlines && cu != NULL && 624 cu->srcfiles != NULL && f != NULL && f->inlined_caller != NULL) 625 print_inlines(cu, f->inlined_caller, f->call_file, 626 f->call_line); 627 } 628 629 static void 630 find_section_base(const char *exe, Elf *e, const char *section) 631 { 632 Dwarf_Addr off; 633 Elf_Scn *scn; 634 GElf_Ehdr eh; 635 GElf_Shdr sh; 636 size_t shstrndx; 637 int elferr; 638 const char *name; 639 640 if (gelf_getehdr(e, &eh) != &eh) { 641 warnx("gelf_getehdr failed: %s", elf_errmsg(-1)); 642 return; 643 } 644 645 if (!elf_getshstrndx(e, &shstrndx)) { 646 warnx("elf_getshstrndx failed: %s", elf_errmsg(-1)); 647 return; 648 } 649 650 (void) elf_errno(); 651 off = 0; 652 scn = NULL; 653 while ((scn = elf_nextscn(e, scn)) != NULL) { 654 if (gelf_getshdr(scn, &sh) == NULL) { 655 warnx("gelf_getshdr failed: %s", elf_errmsg(-1)); 656 continue; 657 } 658 if ((name = elf_strptr(e, shstrndx, sh.sh_name)) == NULL) 659 goto next; 660 if (!strcmp(section, name)) { 661 if (eh.e_type == ET_EXEC || eh.e_type == ET_DYN) { 662 /* 663 * For executables, section base is the virtual 664 * address of the specified section. 665 */ 666 section_base = sh.sh_addr; 667 } else if (eh.e_type == ET_REL) { 668 /* 669 * For relocatables, section base is the 670 * relative offset of the specified section 671 * to the start of the first section. 672 */ 673 section_base = off; 674 } else 675 warnx("unknown e_type %u", eh.e_type); 676 return; 677 } 678 next: 679 off += sh.sh_size; 680 } 681 elferr = elf_errno(); 682 if (elferr != 0) 683 warnx("elf_nextscn failed: %s", elf_errmsg(elferr)); 684 685 errx(EXIT_FAILURE, "%s: cannot find section %s", exe, section); 686 } 687 688 int 689 main(int argc, char **argv) 690 { 691 cap_rights_t rights; 692 Elf *e; 693 Dwarf_Debug dbg; 694 Dwarf_Error de; 695 const char *exe, *section; 696 char line[1024]; 697 int fd, i, opt; 698 699 exe = NULL; 700 section = NULL; 701 while ((opt = getopt_long(argc, argv, "ab:Ce:fij:psHV", longopts, 702 NULL)) != -1) { 703 switch (opt) { 704 case 'a': 705 print_addr = 1; 706 break; 707 case 'b': 708 /* ignored */ 709 break; 710 case 'C': 711 demangle = 1; 712 break; 713 case 'e': 714 exe = optarg; 715 break; 716 case 'f': 717 func = 1; 718 break; 719 case 'i': 720 inlines = 1; 721 break; 722 case 'j': 723 section = optarg; 724 break; 725 case 'p': 726 pretty_print = 1; 727 break; 728 case 's': 729 base = 1; 730 break; 731 case 'H': 732 usage(); 733 case 'V': 734 version(); 735 default: 736 usage(); 737 } 738 } 739 740 argv += optind; 741 argc -= optind; 742 743 if (exe == NULL) 744 exe = "a.out"; 745 746 if ((fd = open(exe, O_RDONLY)) < 0) 747 err(EXIT_FAILURE, "%s", exe); 748 749 if (caph_rights_limit(fd, cap_rights_init(&rights, CAP_FSTAT, 750 CAP_MMAP_R)) < 0) 751 errx(EXIT_FAILURE, "caph_rights_limit"); 752 753 caph_cache_catpages(); 754 if (caph_limit_stdio() < 0) 755 errx(EXIT_FAILURE, "failed to limit stdio rights"); 756 if (caph_enter() < 0) 757 errx(EXIT_FAILURE, "failed to enter capability mode"); 758 759 if (dwarf_init(fd, DW_DLC_READ, NULL, NULL, &dbg, &de)) 760 errx(EXIT_FAILURE, "dwarf_init: %s", dwarf_errmsg(de)); 761 762 if (dwarf_get_elf(dbg, &e, &de) != DW_DLV_OK) 763 errx(EXIT_FAILURE, "dwarf_get_elf: %s", dwarf_errmsg(de)); 764 765 if (section) 766 find_section_base(exe, e, section); 767 else 768 section_base = 0; 769 770 if (argc > 0) 771 for (i = 0; i < argc; i++) 772 translate(dbg, e, argv[i]); 773 else { 774 setvbuf(stdout, NULL, _IOLBF, 0); 775 while (fgets(line, sizeof(line), stdin) != NULL) 776 translate(dbg, e, line); 777 } 778 779 dwarf_finish(dbg, &de); 780 781 (void) elf_end(e); 782 783 exit(0); 784 } 785