1 /* addr2line.c -- convert addresses to line number and function name
2 Copyright (C) 1997-2023 Free Software Foundation, Inc.
3 Contributed by Ulrich Lauther <Ulrich.Lauther@mchp.siemens.de>
5 This file is part of GNU Binutils.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
23 /* Derived from objdump.c and nm.c by Ulrich.Lauther@mchp.siemens.de
26 addr2line [options] addr addr ...
30 both forms write results to stdout, the second form reads addresses
31 to be converted from stdin. */
36 #include "libiberty.h"
40 #include "safe-ctype.h"
42 static bool unwind_inlines
; /* -i, unwind inlined functions. */
43 static bool with_addresses
; /* -a, show addresses. */
44 static bool with_functions
; /* -f, show function names. */
45 static bool do_demangle
; /* -C, demangle names. */
46 static bool pretty_print
; /* -p, print on one line. */
47 static bool base_names
; /* -s, strip directory names. */
49 /* Flags passed to the name demangler. */
50 static int demangle_flags
= DMGL_PARAMS
| DMGL_ANSI
;
52 static int naddr
; /* Number of addresses to process. */
53 static char **addr
; /* Hex addresses to process. */
56 static asymbol
**syms
; /* Symbol table. */
58 static struct option long_options
[] =
60 {"addresses", no_argument
, NULL
, 'a'},
61 {"basenames", no_argument
, NULL
, 's'},
62 {"demangle", optional_argument
, NULL
, 'C'},
63 {"exe", required_argument
, NULL
, 'e'},
64 {"functions", no_argument
, NULL
, 'f'},
65 {"inlines", no_argument
, NULL
, 'i'},
66 {"pretty-print", no_argument
, NULL
, 'p'},
67 {"recurse-limit", no_argument
, NULL
, 'R'},
68 {"recursion-limit", no_argument
, NULL
, 'R'},
69 {"no-recurse-limit", no_argument
, NULL
, 'r'},
70 {"no-recursion-limit", no_argument
, NULL
, 'r'},
71 {"section", required_argument
, NULL
, 'j'},
72 {"target", required_argument
, NULL
, 'b'},
73 {"help", no_argument
, NULL
, 'H'},
74 {"version", no_argument
, NULL
, 'V'},
75 {0, no_argument
, 0, 0}
78 static void usage (FILE *, int);
79 static void slurp_symtab (bfd
*);
80 static void find_address_in_section (bfd
*, asection
*, void *);
81 static void find_offset_in_section (bfd
*, asection
*);
82 static void translate_addresses (bfd
*, asection
*);
84 /* Print a usage message to STREAM and exit with STATUS. */
87 usage (FILE *stream
, int status
)
89 fprintf (stream
, _("Usage: %s [option(s)] [addr(s)]\n"), program_name
);
90 fprintf (stream
, _(" Convert addresses into line number/file name pairs.\n"));
91 fprintf (stream
, _(" If no addresses are specified on the command line, they will be read from stdin\n"));
92 fprintf (stream
, _(" The options are:\n\
93 @<file> Read options from <file>\n\
94 -a --addresses Show addresses\n\
95 -b --target=<bfdname> Set the binary file format\n\
96 -e --exe=<executable> Set the input file name (default is a.out)\n\
97 -i --inlines Unwind inlined functions\n\
98 -j --section=<name> Read section-relative offsets instead of addresses\n\
99 -p --pretty-print Make the output easier to read for humans\n\
100 -s --basenames Strip directory names\n\
101 -f --functions Show function names\n\
102 -C --demangle[=style] Demangle function names\n\
103 -R --recurse-limit Enable a limit on recursion whilst demangling. [Default]\n\
104 -r --no-recurse-limit Disable a limit on recursion whilst demangling\n\
105 -h --help Display this information\n\
106 -v --version Display the program's version\n\
109 list_supported_targets (program_name
, stream
);
110 if (REPORT_BUGS_TO
[0] && status
== 0)
111 fprintf (stream
, _("Report bugs to %s\n"), REPORT_BUGS_TO
);
115 /* Read in the symbol table. */
118 slurp_symtab (bfd
*abfd
)
121 bool dynamic
= false;
123 if ((bfd_get_file_flags (abfd
) & HAS_SYMS
) == 0)
126 storage
= bfd_get_symtab_upper_bound (abfd
);
129 storage
= bfd_get_dynamic_symtab_upper_bound (abfd
);
133 bfd_fatal (bfd_get_filename (abfd
));
135 syms
= (asymbol
**) xmalloc (storage
);
137 symcount
= bfd_canonicalize_dynamic_symtab (abfd
, syms
);
139 symcount
= bfd_canonicalize_symtab (abfd
, syms
);
141 bfd_fatal (bfd_get_filename (abfd
));
143 /* If there are no symbols left after canonicalization and
144 we have not tried the dynamic symbols then give them a go. */
147 && (storage
= bfd_get_dynamic_symtab_upper_bound (abfd
)) > 0)
150 syms
= xmalloc (storage
);
151 symcount
= bfd_canonicalize_dynamic_symtab (abfd
, syms
);
154 /* PR 17512: file: 2a1d3b5b.
155 Do not pretend that we have some symbols when we don't. */
163 /* These global variables are used to pass information between
164 translate_addresses and find_address_in_section. */
167 static const char *filename
;
168 static const char *functionname
;
169 static unsigned int line
;
170 static unsigned int discriminator
;
173 /* Look for an address in a section. This is called via
174 bfd_map_over_sections. */
177 find_address_in_section (bfd
*abfd
, asection
*section
,
178 void *data ATTRIBUTE_UNUSED
)
186 if ((bfd_section_flags (section
) & SEC_ALLOC
) == 0)
189 vma
= bfd_section_vma (section
);
193 size
= bfd_section_size (section
);
194 if (pc
>= vma
+ size
)
197 found
= bfd_find_nearest_line_discriminator (abfd
, section
, syms
, pc
- vma
,
198 &filename
, &functionname
,
199 &line
, &discriminator
);
202 /* Look for an offset in a section. This is directly called. */
205 find_offset_in_section (bfd
*abfd
, asection
*section
)
212 if ((bfd_section_flags (section
) & SEC_ALLOC
) == 0)
215 size
= bfd_section_size (section
);
219 found
= bfd_find_nearest_line_discriminator (abfd
, section
, syms
, pc
,
220 &filename
, &functionname
,
221 &line
, &discriminator
);
224 /* Lookup a symbol with offset in symbol table. */
227 lookup_symbol (bfd
*abfd
, char *sym
, size_t offset
)
231 for (i
= 0; i
< symcount
; i
++)
233 if (!strcmp (syms
[i
]->name
, sym
))
234 return syms
[i
]->value
+ offset
+ bfd_asymbol_section (syms
[i
])->vma
;
236 /* Try again mangled */
237 for (i
= 0; i
< symcount
; i
++)
239 char *d
= bfd_demangle (abfd
, syms
[i
]->name
, demangle_flags
);
240 bool match
= d
&& !strcmp (d
, sym
);
244 return syms
[i
]->value
+ offset
+ bfd_asymbol_section (syms
[i
])->vma
;
249 /* Split an symbol+offset expression. adr is modified. */
252 is_symbol (char *adr
, char **symp
, size_t *offset
)
256 while (ISSPACE (*adr
))
258 if (ISDIGIT (*adr
) || *adr
== 0)
260 /* Could be either symbol or hex number. Check if it has +. */
261 if (TOUPPER(*adr
) >= 'A' && TOUPPER(*adr
) <= 'F' && !strchr (adr
, '+'))
265 while (*adr
&& !ISSPACE (*adr
) && *adr
!= '+')
268 while (ISSPACE (*adr
))
274 *offset
= strtoul(adr
, NULL
, 0);
280 /* Read hexadecimal or symbolic with offset addresses from stdin, translate into
281 file_name:line_number and optionally function name. */
284 translate_addresses (bfd
*abfd
, asection
*section
)
286 int read_stdin
= (naddr
== 0);
296 if (fgets (addr_hex
, sizeof addr_hex
, stdin
) == NULL
)
308 if (is_symbol (adr
, &symp
, &offset
))
309 pc
= lookup_symbol (abfd
, symp
, offset
);
311 pc
= bfd_scan_vma (adr
, NULL
, 16);
312 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
)
314 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
315 bfd_vma sign
= (bfd_vma
) 1 << (bed
->s
->arch_size
- 1);
317 pc
&= (sign
<< 1) - 1;
318 if (bed
->sign_extend_vma
)
319 pc
= (pc
^ sign
) - sign
;
325 bfd_printf_vma (abfd
, pc
);
335 find_offset_in_section (abfd
, section
);
337 bfd_map_over_sections (abfd
, find_address_in_section
, NULL
);
360 if (name
== NULL
|| *name
== '\0')
362 else if (do_demangle
)
364 alloc
= bfd_demangle (abfd
, name
, demangle_flags
);
371 /* Note for translators: This printf is used to join the
372 function name just printed above to the line number/
373 file name pair that is about to be printed below. Eg:
383 if (base_names
&& filename
!= NULL
)
387 h
= strrchr (filename
, '/');
392 printf ("%s:", filename
? filename
: "??");
395 if (discriminator
!= 0)
396 printf ("%u (discriminator %u)\n", line
, discriminator
);
398 printf ("%u\n", line
);
405 found
= bfd_find_inliner_info (abfd
, &filename
, &functionname
,
410 /* Note for translators: This printf is used to join the
411 line number/file name pair that has just been printed with
412 the line number/file name pair that is going to be printed
413 by the next iteration of the while loop. Eg:
415 123:bar.c (inlined by) 456:main.c */
416 printf (_(" (inlined by) "));
420 /* fflush() is essential for using this command as a server
421 child process that reads addresses from a pipe and responds
422 with line number information, processing one address at a
428 /* Process a file. Returns an exit value for main(). */
431 process_file (const char *file_name
, const char *section_name
,
438 if (get_file_size (file_name
) < 1)
441 abfd
= bfd_openr (file_name
, target
);
443 bfd_fatal (file_name
);
445 /* Decompress sections. */
446 abfd
->flags
|= BFD_DECOMPRESS
;
448 if (bfd_check_format (abfd
, bfd_archive
))
449 fatal (_("%s: cannot get addresses from archive"), file_name
);
451 if (! bfd_check_format_matches (abfd
, bfd_object
, &matching
))
453 bfd_nonfatal (bfd_get_filename (abfd
));
454 if (bfd_get_error () == bfd_error_file_ambiguously_recognized
)
455 list_matching_formats (matching
);
459 if (section_name
!= NULL
)
461 section
= bfd_get_section_by_name (abfd
, section_name
);
463 fatal (_("%s: cannot find section %s"), file_name
, section_name
);
470 translate_addresses (abfd
, section
);
481 main (int argc
, char **argv
)
483 const char *file_name
;
484 const char *section_name
;
488 #ifdef HAVE_LC_MESSAGES
489 setlocale (LC_MESSAGES
, "");
491 setlocale (LC_CTYPE
, "");
492 bindtextdomain (PACKAGE
, LOCALEDIR
);
493 textdomain (PACKAGE
);
495 program_name
= *argv
;
496 xmalloc_set_program_name (program_name
);
497 bfd_set_error_program_name (program_name
);
499 expandargv (&argc
, &argv
);
501 if (bfd_init () != BFD_INIT_MAGIC
)
502 fatal (_("fatal error: libbfd ABI mismatch"));
503 set_default_bfd_target ();
508 while ((c
= getopt_long (argc
, argv
, "ab:Ce:rRsfHhij:pVv", long_options
, (int *) 0))
514 break; /* We've been given a long option. */
516 with_addresses
= true;
525 enum demangling_styles style
;
527 style
= cplus_demangle_name_to_style (optarg
);
528 if (style
== unknown_demangling
)
529 fatal (_("unknown demangling style `%s'"),
532 cplus_demangle_set_style (style
);
536 demangle_flags
|= DMGL_NO_RECURSE_LIMIT
;
539 demangle_flags
&= ~ DMGL_NO_RECURSE_LIMIT
;
548 with_functions
= true;
555 print_version ("addr2line");
562 unwind_inlines
= true;
565 section_name
= optarg
;
573 if (file_name
== NULL
)
576 addr
= argv
+ optind
;
577 naddr
= argc
- optind
;
579 return process_file (file_name
, section_name
, target
);