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