aboutsummaryrefslogtreecommitdiff
path: root/binutils/addr2line.c
diff options
context:
space:
mode:
authorAndi Kleen <andi@firstfloor.org>2021-12-23 09:55:07 -0800
committerH.J. Lu <hjl.tools@gmail.com>2022-02-03 18:41:56 -0800
commitfd3c53675c01472caaa4eb976781b9d6d2d3c53d (patch)
treefbdc0290490829e5835d8da542470167b08cfdaf /binutils/addr2line.c
parent78110c3f7743e8177859fb03db1aeaf327a51459 (diff)
downloadgdb-fd3c53675c01472caaa4eb976781b9d6d2d3c53d.zip
gdb-fd3c53675c01472caaa4eb976781b9d6d2d3c53d.tar.gz
gdb-fd3c53675c01472caaa4eb976781b9d6d2d3c53d.tar.bz2
Support symbol+offset lookup in addr2line
The Linux kernel usually ouputs symbol+offset instead of plain code addresses these days, to avoid leaking ASLR secrets and to handle dynamically loaded modules. Converting those with addr2line is somewhat involved: it requires looking up the symbol first using nm and then manually compute the offset, and then pass it to addr2line. This patch implements the necessary steps directly in addr2line, by looking up the symbol (with demangling if needed) and computing the offset. It's possible that a symbol is ambigious with a hex number. In this case it uses the symbol lookup if the string contains a +. When it isn't ambigious the + is optional.
Diffstat (limited to 'binutils/addr2line.c')
-rw-r--r--binutils/addr2line.c75
1 files changed, 69 insertions, 6 deletions
diff --git a/binutils/addr2line.c b/binutils/addr2line.c
index 4d0405b..fd2ac80 100644
--- a/binutils/addr2line.c
+++ b/binutils/addr2line.c
@@ -37,6 +37,7 @@
#include "demangle.h"
#include "bucomm.h"
#include "elf-bfd.h"
+#include "safe-ctype.h"
static bool unwind_inlines; /* -i, unwind inlined functions. */
static bool with_addresses; /* -a, show addresses. */
@@ -51,6 +52,7 @@ static int demangle_flags = DMGL_PARAMS | DMGL_ANSI;
static int naddr; /* Number of addresses to process. */
static char **addr; /* Hex addresses to process. */
+static long symcount;
static asymbol **syms; /* Symbol table. */
static struct option long_options[] =
@@ -116,7 +118,6 @@ static void
slurp_symtab (bfd *abfd)
{
long storage;
- long symcount;
bool dynamic = false;
if ((bfd_get_file_flags (abfd) & HAS_SYMS) == 0)
@@ -220,32 +221,94 @@ find_offset_in_section (bfd *abfd, asection *section)
&line, &discriminator);
}
-/* Read hexadecimal addresses from stdin, translate into
+/* Lookup a symbol with offset in symbol table. */
+
+static bfd_vma
+lookup_symbol (bfd *abfd, char *sym, size_t offset)
+{
+ long i;
+
+ for (i = 0; i < symcount; i++)
+ {
+ if (!strcmp (syms[i]->name, sym))
+ return syms[i]->value + offset + bfd_asymbol_section (syms[i])->vma;
+ }
+ /* Try again mangled */
+ for (i = 0; i < symcount; i++)
+ {
+ char *d = bfd_demangle (abfd, syms[i]->name, demangle_flags);
+ bool match = d && !strcmp (d, sym);
+ free (d);
+
+ if (match)
+ return syms[i]->value + offset + bfd_asymbol_section (syms[i])->vma;
+ }
+ return 0;
+}
+
+/* Split an symbol+offset expression. adr is modified. */
+
+static bool
+is_symbol (char *adr, char **symp, size_t *offset)
+{
+ char *end;
+
+ while (ISSPACE (*adr))
+ adr++;
+ if (ISDIGIT (*adr) || *adr == 0)
+ return false;
+ /* Could be either symbol or hex number. Check if it has +. */
+ if (TOUPPER(*adr) >= 'A' && TOUPPER(*adr) <= 'F' && !strchr (adr, '+'))
+ return false;
+
+ *symp = adr;
+ while (*adr && !ISSPACE (*adr) && *adr != '+')
+ adr++;
+ end = adr;
+ while (ISSPACE (*adr))
+ adr++;
+ *offset = 0;
+ if (*adr == '+')
+ {
+ adr++;
+ *offset = strtoul(adr, NULL, 0);
+ }
+ *end = 0;
+ return true;
+}
+
+/* Read hexadecimal or symbolic with offset addresses from stdin, translate into
file_name:line_number and optionally function name. */
static void
translate_addresses (bfd *abfd, asection *section)
{
int read_stdin = (naddr == 0);
+ char *adr;
+ char addr_hex[100];
+ char *symp;
+ size_t offset;
for (;;)
{
if (read_stdin)
{
- char addr_hex[100];
-
if (fgets (addr_hex, sizeof addr_hex, stdin) == NULL)
break;
- pc = bfd_scan_vma (addr_hex, NULL, 16);
+ adr = addr_hex;
}
else
{
if (naddr <= 0)
break;
--naddr;
- pc = bfd_scan_vma (*addr++, NULL, 16);
+ adr = *addr++;
}
+ if (is_symbol (adr, &symp, &offset))
+ pc = lookup_symbol (abfd, symp, offset);
+ else
+ pc = bfd_scan_vma (adr, NULL, 16);
if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
{
const struct elf_backend_data *bed = get_elf_backend_data (abfd);