public inbox for elfutils@sourceware.org
 help / color / mirror / Atom feed
From: Luke Diamand <ldiamand@roku.com>
To: "elfutils-devel@sourceware.org" <elfutils-devel@sourceware.org>
Cc: Mark Wielaard <mark@klomp.org>, Luke Diamand <ldiamand@roku.com>
Subject: [PATCHv1 1/2] libdwfl: specify optional sysroot to search for shared libraries
Date: Sun, 20 Jan 2019 15:01:00 -0000	[thread overview]
Message-ID: <20190120150028.21580-2-ldiamand@roku.com> (raw)
In-Reply-To: <20190120150028.21580-1-ldiamand@roku.com>

When searching the list of modules in a core file, if the core was
generated on a different system to the current one, we need to look
in a sysroot for the various shared objects.

For example, we might be looking at a core file from an ARM system
using elfutils running on an x86 host.

This change adds a new function, dwfl_set_sysroot(), which then
gets used when searching for libraries.

Signed-off-by: Luke Diamand <ldiamand@roku.com>
---
 libdw/libdw.map    |  7 ++++++-
 libdwfl/libdwfl.h  |  5 +++++
 libdwfl/libdwflP.h |  1 +
 libdwfl/link_map.c | 25 +++++++++++++++++++++++--
 4 files changed, 35 insertions(+), 3 deletions(-)

diff --git a/libdw/libdw.map b/libdw/libdw.map
index 55482d58..43a9de2e 100644
--- a/libdw/libdw.map
+++ b/libdw/libdw.map
@@ -360,4 +360,9 @@ ELFUTILS_0.173 {
 ELFUTILS_0.175 {
   global:
     dwelf_elf_begin;
-} ELFUTILS_0.173;
\ No newline at end of file
+} ELFUTILS_0.173;
+
+ELFUTILS_0.176 {
+  global:
+    dwfl_set_sysroot;
+} ELFUTILS_0.175;
diff --git a/libdwfl/libdwfl.h b/libdwfl/libdwfl.h
index a0c1d357..c11e2f24 100644
--- a/libdwfl/libdwfl.h
+++ b/libdwfl/libdwfl.h
@@ -807,6 +807,11 @@ int dwfl_getthread_frames (Dwfl *dwfl, pid_t tid,
 bool dwfl_frame_pc (Dwfl_Frame *state, Dwarf_Addr *pc, bool *isactivation)
   __nonnull_attribute__ (1, 2);
 
+/* Set the sysroot to use when searching for shared libraries. If not
+ specified, search in the system root.  */
+void dwfl_set_sysroot (Dwfl *dwfl, const char *sysroot)
+  __nonnull_attribute__ (1);
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/libdwfl/libdwflP.h b/libdwfl/libdwflP.h
index 941a8b66..993a0e7c 100644
--- a/libdwfl/libdwflP.h
+++ b/libdwfl/libdwflP.h
@@ -138,6 +138,7 @@ struct Dwfl
   int lookup_tail_ndx;
 
   struct Dwfl_User_Core *user_core;
+  const char *sysroot;	/* sysroot, or NULL to search standard system paths */
 };
 
 #define OFFLINE_REDZONE		0x10000
diff --git a/libdwfl/link_map.c b/libdwfl/link_map.c
index 29307c74..0fae680d 100644
--- a/libdwfl/link_map.c
+++ b/libdwfl/link_map.c
@@ -388,8 +388,21 @@ report_r_debug (uint_fast8_t elfclass, uint_fast8_t elfdata,
       if (name != NULL)
 	{
 	  /* This code is mostly inlined dwfl_report_elf.  */
-	  // XXX hook for sysroot
-	  int fd = open (name, O_RDONLY);
+	  char *path_name;
+	  const char *sysroot = dwfl->sysroot;
+	  int rc;
+
+	  /* don't look in the sysroot if the path is already inside the sysroot */
+	  bool name_in_sysroot = strncmp(name, sysroot, strlen(sysroot)) == 0;
+
+	  if (!name_in_sysroot && sysroot)
+	    rc = asprintf(&path_name, "%s/%s", sysroot, name);
+	  else
+	    rc = asprintf(&path_name, "%s", name);
+	  if (unlikely(rc == -1))
+	    return release_buffer(-1);
+
+	  int fd = open (path_name, O_RDONLY);
 	  if (fd >= 0)
 	    {
 	      Elf *elf;
@@ -471,6 +484,7 @@ report_r_debug (uint_fast8_t elfclass, uint_fast8_t elfdata,
 		    close (fd);
 		}
 	    }
+          free(path_name);
 	}
 
       if (mod != NULL)
@@ -1037,3 +1051,10 @@ dwfl_link_map_report (Dwfl *dwfl, const void *auxv, size_t auxv_size,
 			 &integrated_memory_callback, &mcb, r_debug_info);
 }
 INTDEF (dwfl_link_map_report)
+
+void
+dwfl_set_sysroot (Dwfl *dwfl, const char *sysroot)
+{
+  dwfl->sysroot = sysroot;
+}
+INTDEF (dwfl_set_sysroot)
-- 
2.20.1

  reply	other threads:[~2019-01-20 15:01 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-01-20 15:01 [PATCHv1 0/2] specify a sysroot to search when examining a core file Luke Diamand
2019-01-20 15:01 ` Luke Diamand [this message]
2019-01-20 22:08   ` [PATCHv1 1/2] libdwfl: specify optional sysroot to search for shared libraries Dmitry V. Levin
2019-01-20 15:01 ` [PATCHv1 2/2] eu-stack: add support for sysroot option Luke Diamand
2019-01-29 13:47 ` [PATCHv1 0/2] specify a sysroot to search when examining a core file Mark Wielaard
     [not found]   ` <BN7PR01MB3953544F2EDA1FB7BF6E9470B0970@BN7PR01MB3953.prod.exchangelabs.com>
2019-01-31 15:22     ` Mark Wielaard

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20190120150028.21580-2-ldiamand@roku.com \
    --to=ldiamand@roku.com \
    --cc=elfutils-devel@sourceware.org \
    --cc=mark@klomp.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).