public inbox for elfutils@sourceware.org
 help / color / mirror / Atom feed
From: Mark Wielaard <mark@klomp.org>
To: Sonal Santan <sonals@xilinx.com>,
	"elfutils-devel@sourceware.org" <elfutils-devel@sourceware.org>
Subject: Re: Working with ELF already loaded in memory
Date: Thu, 08 Jul 2021 17:02:01 +0200	[thread overview]
Message-ID: <19d95e93c233c8cf9abb1f065aba571d45657280.camel@klomp.org> (raw)
In-Reply-To: <BY5PR02MB6260DD7F130AA98334CCF886BB199@BY5PR02MB6260.namprd02.prod.outlook.com>

Hi Sinal,

On Thu, 2021-07-08 at 05:02 +0000, Sonal Santan via Elfutils-devel
wrote:
> Going through the libdw it appears that all APIs require either a
> file handle or a file name of the ELF object to create a session.
> Since we do not have access to the ELF file -- but rather the ELF
> file contents are already loaded in memory -- is there any other
> mechanism to create a session for extracting DWARF information using
> libdw? 

Yes, if you just need the information already loaded into memory and
you know where the library is mapped you can use:

/* Create descriptor for memory region.  */
extern Elf *elf_memory (char *__image, size_t __size);

You can then use that Elf handle to extract the information that has
been mapped in. Which often is not the actual debug information though.

If you have a Elf handle you can use:

/* Returns the build ID as found in a NT_GNU_BUILD_ID note from either
   a SHT_NOTE section or from a PT_NOTE segment if the ELF file
   doesn't contain any section headers.  On success a pointer to the
   build ID is written to *BUILDID_P, and the positive length of the
   build ID is returned.  Returns 0 if the ELF lacks a NT_GNU_BUILD_ID
   note.  Returns -1 in case of malformed data or other errors.  */
extern ssize_t dwelf_elf_gnu_build_id (Elf *elf, const void **build_idp);

You can then use the build_id to lookup the debug information (file).

You can also use libdwfl (part of libdw) to do some of the above
automagically. See for example:

/* Call dwfl_report_module for each file mapped into the address space of PID.
   Returns zero on success, -1 if dwfl_report_module failed,
   or an errno code if opening the proc files failed.  */
extern int dwfl_linux_proc_report (Dwfl *dwfl, pid_t pid);

The Dwfl will then be a representation of the modules (executable and
shared libraries) of that particular process. You can then iterate
through those modules using dwfl_getmodules and get a Dwarf handle
using dwfl_module_getdwarf (or for all with dwfl_getdwarf). libdw will
then try to extract that build-id from each module and try various
lookups to get the (separate on disk) debuginfo.

Hope that helps,

Mark

  reply	other threads:[~2021-07-08 15:02 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-07-08  5:02 Sonal Santan
2021-07-08 15:02 ` Mark Wielaard [this message]
2021-07-09  5:34   ` Sonal Santan
2021-07-09 11:00     ` 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=19d95e93c233c8cf9abb1f065aba571d45657280.camel@klomp.org \
    --to=mark@klomp.org \
    --cc=elfutils-devel@sourceware.org \
    --cc=sonals@xilinx.com \
    /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).