1/* addr2line.c -- convert addresses to line number and function name
2   Copyright 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2006
3   Free Software Foundation, Inc.
4   Contributed by Ulrich Lauther <Ulrich.Lauther@mchp.siemens.de>
5
6   This file is part of GNU Binutils.
7
8   This program is free software; you can redistribute it and/or modify
9   it under the terms of the GNU General Public License as published by
10   the Free Software Foundation; either version 2, or (at your option)
11   any later version.
12
13   This program is distributed in the hope that it will be useful,
14   but WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16   GNU General Public License for more details.
17
18   You should have received a copy of the GNU General Public License
19   along with this program; if not, write to the Free Software
20   Foundation, 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
21
22/* Derived from objdump.c and nm.c by Ulrich.Lauther@mchp.siemens.de
23
24   Usage:
25   addr2line [options] addr addr ...
26   or
27   addr2line [options]
28
29   both forms write results to stdout, the second form reads addresses
30   to be converted from stdin.  */
31
32#include "config.h"
33#include <string.h>
34
35#include "bfd.h"
36#include "getopt.h"
37#include "libiberty.h"
38#include "demangle.h"
39#include "bucomm.h"
40#include "budemang.h"
41
42static bfd_boolean unwind_inlines;	/* -i, unwind inlined functions. */
43static bfd_boolean with_addresses;	/* -a, show addresses.  */
44static bfd_boolean with_functions;	/* -f, show function names.  */
45static bfd_boolean do_demangle;		/* -C, demangle names.  */
46static bfd_boolean base_names;		/* -s, strip directory names.  */
47
48static int naddr;		/* Number of addresses to process.  */
49static char **addr;		/* Hex addresses to process.  */
50
51static asymbol **syms;		/* Symbol table.  */
52
53static struct option long_options[] =
54{
55  {"addresses", no_argument, NULL, 'a'},
56  {"basenames", no_argument, NULL, 's'},
57  {"demangle", optional_argument, NULL, 'C'},
58  {"exe", required_argument, NULL, 'e'},
59  {"functions", no_argument, NULL, 'f'},
60  {"inlines", no_argument, NULL, 'i'},
61  {"section", required_argument, NULL, 'j'},
62  {"target", required_argument, NULL, 'b'},
63  {"help", no_argument, NULL, 'H'},
64  {"version", no_argument, NULL, 'V'},
65  {0, no_argument, 0, 0}
66};
67
68static void usage (FILE *, int);
69static void slurp_symtab (bfd *);
70static void find_address_in_section (bfd *, asection *, void *);
71static void find_offset_in_section (bfd *, asection *);
72static void translate_addresses (bfd *, asection *);
73static void process_file (const char *, const char *, const char *);
74
75/* Print a usage message to STREAM and exit with STATUS.  */
76
77static void
78usage (FILE *stream, int status)
79{
80  fprintf (stream, _("Usage: %s [option(s)] [addr(s)]\n"), program_name);
81  fprintf (stream, _(" Convert addresses into line number/file name pairs.\n"));
82  fprintf (stream, _(" If no addresses are specified on the command line, they will be read from stdin\n"));
83  fprintf (stream, _(" The options are:\n\
84  @<file>                Read options from <file>\n\
85  -a --addresses         Show addresses\n\
86  -b --target=<bfdname>  Set the binary file format\n\
87  -e --exe=<executable>  Set the input file name (default is a.out)\n\
88  -i --inlines           Unwind inlined functions\n\
89  -j --section=<name>    Read section-relative offsets instead of addresses\n\
90  -s --basenames         Strip directory names\n\
91  -f --functions         Show function names\n\
92  -C --demangle[=style]  Demangle function names\n\
93  -h --help              Display this information\n\
94  -v --version           Display the program's version\n\
95\n"));
96
97  list_supported_targets (program_name, stream);
98  if (status == 0)
99    fprintf (stream, _("Report bugs to %s\n"), REPORT_BUGS_TO);
100  exit (status);
101}
102
103/* Read in the symbol table.  */
104
105static void
106slurp_symtab (bfd *abfd)
107{
108  long symcount;
109  unsigned int size;
110
111  if ((bfd_get_file_flags (abfd) & HAS_SYMS) == 0)
112    return;
113
114  symcount = bfd_read_minisymbols (abfd, FALSE, (void *) &syms, &size);
115  if (symcount == 0)
116    symcount = bfd_read_minisymbols (abfd, TRUE /* dynamic */, (void *) &syms, &size);
117
118  if (symcount < 0)
119    bfd_fatal (bfd_get_filename (abfd));
120}
121
122/* These global variables are used to pass information between
123   translate_addresses and find_address_in_section.  */
124
125static bfd_vma pc;
126static const char *filename;
127static const char *functionname;
128static unsigned int line;
129static bfd_boolean found;
130
131/* Look for an address in a section.  This is called via
132   bfd_map_over_sections.  */
133
134static void
135find_address_in_section (bfd *abfd, asection *section,
136			 void *data ATTRIBUTE_UNUSED)
137{
138  bfd_vma vma;
139  bfd_size_type size;
140
141  if (found)
142    return;
143
144  if ((bfd_get_section_flags (abfd, section) & SEC_ALLOC) == 0)
145    return;
146
147  vma = bfd_get_section_vma (abfd, section);
148  if (pc < vma)
149    return;
150
151  size = bfd_get_section_size (section);
152  if (pc >= vma + size)
153    return;
154
155  found = bfd_find_nearest_line (abfd, section, syms, pc - vma,
156				 &filename, &functionname, &line);
157}
158
159/* Look for an offset in a section.  This is directly called.  */
160
161static void
162find_offset_in_section (bfd *abfd, asection *section)
163{
164  bfd_size_type size;
165
166  if (found)
167    return;
168
169  if ((bfd_get_section_flags (abfd, section) & SEC_ALLOC) == 0)
170    return;
171
172  size = bfd_get_section_size (section);
173  if (pc >= size)
174    return;
175
176  found = bfd_find_nearest_line (abfd, section, syms, pc,
177				 &filename, &functionname, &line);
178}
179
180/* Read hexadecimal addresses from stdin, translate into
181   file_name:line_number and optionally function name.  */
182
183static void
184translate_addresses (bfd *abfd, asection *section)
185{
186  int read_stdin = (naddr == 0);
187
188  for (;;)
189    {
190      if (read_stdin)
191	{
192	  char addr_hex[100];
193
194	  if (fgets (addr_hex, sizeof addr_hex, stdin) == NULL)
195	    break;
196	  pc = bfd_scan_vma (addr_hex, NULL, 16);
197	}
198      else
199	{
200	  if (naddr <= 0)
201	    break;
202	  --naddr;
203	  pc = bfd_scan_vma (*addr++, NULL, 16);
204	}
205
206      if (with_addresses)
207        {
208	  printf ("0x");
209	  bfd_printf_vma (abfd, pc);
210	  printf ("\n");
211        }
212
213      found = FALSE;
214      if (section)
215	find_offset_in_section (abfd, section);
216      else
217	bfd_map_over_sections (abfd, find_address_in_section, NULL);
218
219      if (! found)
220	{
221	  if (with_functions)
222	    printf ("??\n");
223	  printf ("??:0\n");
224	}
225      else
226	{
227	  do {
228	    if (with_functions)
229	      {
230		const char *name;
231		char *alloc = NULL;
232
233		name = functionname;
234		if (name == NULL || *name == '\0')
235		  name = "??";
236		else if (do_demangle)
237		  {
238		    alloc = demangle (abfd, name);
239		    name = alloc;
240		  }
241
242		printf ("%s\n", name);
243
244		if (alloc != NULL)
245		  free (alloc);
246	      }
247
248	    if (base_names && filename != NULL)
249	      {
250		char *h;
251
252		h = strrchr (filename, '/');
253		if (h != NULL)
254		  filename = h + 1;
255	      }
256
257	    printf ("%s:%u\n", filename ? filename : "??", line);
258	    if (!unwind_inlines)
259	      found = FALSE;
260	    else
261	      found = bfd_find_inliner_info (abfd, &filename, &functionname, &line);
262	  } while (found);
263
264	}
265
266      /* fflush() is essential for using this command as a server
267         child process that reads addresses from a pipe and responds
268         with line number information, processing one address at a
269         time.  */
270      fflush (stdout);
271    }
272}
273
274/* Process a file.  */
275
276static void
277process_file (const char *file_name, const char *section_name,
278	      const char *target)
279{
280  bfd *abfd;
281  asection *section;
282  char **matching;
283
284  if (get_file_size (file_name) < 1)
285    return;
286
287  abfd = bfd_openr (file_name, target);
288  if (abfd == NULL)
289    bfd_fatal (file_name);
290
291  if (bfd_check_format (abfd, bfd_archive))
292    fatal (_("%s: cannot get addresses from archive"), file_name);
293
294  if (! bfd_check_format_matches (abfd, bfd_object, &matching))
295    {
296      bfd_nonfatal (bfd_get_filename (abfd));
297      if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
298	{
299	  list_matching_formats (matching);
300	  free (matching);
301	}
302      xexit (1);
303    }
304
305  if (section_name != NULL)
306    {
307      section = bfd_get_section_by_name (abfd, section_name);
308      if (section == NULL)
309	fatal (_("%s: cannot find section %s"), file_name, section_name);
310    }
311  else
312    section = NULL;
313
314  slurp_symtab (abfd);
315
316  translate_addresses (abfd, section);
317
318  if (syms != NULL)
319    {
320      free (syms);
321      syms = NULL;
322    }
323
324  bfd_close (abfd);
325}
326
327int
328main (int argc, char **argv)
329{
330  const char *file_name;
331  const char *section_name;
332  char *target;
333  int c;
334
335#if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
336  setlocale (LC_MESSAGES, "");
337#endif
338#if defined (HAVE_SETLOCALE)
339  setlocale (LC_CTYPE, "");
340#endif
341  bindtextdomain (PACKAGE, LOCALEDIR);
342  textdomain (PACKAGE);
343
344  if (pledge ("stdio rpath", NULL) == -1)
345    fatal (_("Failed to pledge"));
346
347  program_name = *argv;
348  xmalloc_set_program_name (program_name);
349
350  expandargv (&argc, &argv);
351
352  bfd_init ();
353  set_default_bfd_target ();
354
355  file_name = NULL;
356  section_name = NULL;
357  target = NULL;
358  while ((c = getopt_long (argc, argv, "ab:Ce:sfHhij:Vv", long_options, (int *) 0))
359	 != EOF)
360    {
361      switch (c)
362	{
363	case 0:
364	  break;		/* We've been given a long option.  */
365	case 'a':
366	  with_addresses = TRUE;
367	  break;
368	case 'b':
369	  target = optarg;
370	  break;
371	case 'C':
372	  do_demangle = TRUE;
373	  if (optarg != NULL)
374	    {
375	      enum demangling_styles style;
376
377	      style = cplus_demangle_name_to_style (optarg);
378	      if (style == unknown_demangling)
379		fatal (_("unknown demangling style `%s'"),
380		       optarg);
381
382	      cplus_demangle_set_style (style);
383	    }
384	  break;
385	case 'e':
386	  file_name = optarg;
387	  break;
388	case 's':
389	  base_names = TRUE;
390	  break;
391	case 'f':
392	  with_functions = TRUE;
393	  break;
394	case 'v':
395	case 'V':
396	  print_version ("addr2line");
397	  break;
398	case 'h':
399	case 'H':
400	  usage (stdout, 0);
401	  break;
402	case 'i':
403	  unwind_inlines = TRUE;
404	  break;
405	case 'j':
406	  section_name = optarg;
407	  break;
408	default:
409	  usage (stderr, 1);
410	  break;
411	}
412    }
413
414  if (file_name == NULL)
415    file_name = "a.out";
416
417  addr = argv + optind;
418  naddr = argc - optind;
419
420  process_file (file_name, section_name, target);
421
422  return 0;
423}
424