public inbox for elfutils@sourceware.org
 help / color / mirror / Atom feed
From: Sonal Santan <sonals@xilinx.com>
To: Mark Wielaard <mark@klomp.org>,
	"elfutils-devel@sourceware.org" <elfutils-devel@sourceware.org>
Subject: RE: Working with ELF already loaded in memory
Date: Fri, 9 Jul 2021 05:34:26 +0000	[thread overview]
Message-ID: <BY5PR02MB62604135F2380EEB6619AB06BB189@BY5PR02MB6260.namprd02.prod.outlook.com> (raw)
In-Reply-To: <19d95e93c233c8cf9abb1f065aba571d45657280.camel@klomp.org>

Hello Mark,

> -----Original Message-----
> From: Mark Wielaard <mark@klomp.org>
> Sent: Thursday, July 8, 2021 8:02 AM
> To: Sonal Santan <sonals@xilinx.com>; elfutils-devel@sourceware.org
> Subject: Re: Working with ELF already loaded in memory
> 
> 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.
> 

Thanks for the pointer. I was looking for APIs inside /usr/include/elfutils directory and
did not realize elfutils also places header files under /usr/include directory. I am working 
with this API now.

> 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);

Does this also work for ELF files which are loaded into heap as blob -- not really
_mapped_ into the address space? I guess I will have to provide some hints to dwfl
so it can locate the loaded blob when walking through the process map?

Thanks,
-Sonal

> 
> 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-09  5:34 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
2021-07-09  5:34   ` Sonal Santan [this message]
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=BY5PR02MB62604135F2380EEB6619AB06BB189@BY5PR02MB6260.namprd02.prod.outlook.com \
    --to=sonals@xilinx.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).