public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
* Reading an elf file with binutils
@ 2009-08-06  7:20 Yan
  2009-08-06  7:58 ` Alan Modra
  2009-08-06 13:39 ` Simon Richter
  0 siblings, 2 replies; 5+ messages in thread
From: Yan @ 2009-08-06  7:20 UTC (permalink / raw)
  To: binutils

Hopefully this is the right place to send this to... If not, I
apologize in advance :-)

Basically, I'm trying to read an ELF file with BFD and get some data
out of it. However, the list of sections that BFD provides is blank,
and when I try to read symbols out of the file at all, the whole thing
segfaults. Here's my code:

=======================================================================
#include <iostream>
#include <bfd.h>

using std::cout;
using std::endl;

void print_section(bfd *abfd, bfd_section *s, void *arg)
{
        cout << s->name << endl;
}

int main()
{
        bfd_init();
        bfd *abfd = bfd_openr("/bin/bash", NULL);

        if (!abfd)
        {
                cout << "Error opening BFD." << endl;
                exit(1);
        }

        cout << "File info for " << abfd->filename << endl;

        cout << "Target name: " << abfd->xvec->name << endl;
        cout << "Target endianness: " << abfd->xvec->byteorder << endl;
        cout << "File format string: " <<
bfd_format_string(abfd->format) << endl;

        if (abfd->direction == 1) cout << "Open for reading." << endl;

        cout << "Sections address: " << abfd->sections << endl;
        cout << "Listing sections:" << endl;
        bfd_map_over_sections(abfd, print_section, NULL);

        /* Also prints nothing:
        for (bfd_section *p = abfd->sections; p != NULL; p = p->next)
                cout << "Section: " << p->name << endl;;
        */

        cout << "Reading symbols:" << endl;
        bfd_symbol **symbol_table;
        int storage_needed = bfd_get_symtab_upper_bound(abfd);
        int num_symbols;

        if (storage_needed < 0)
        {
                cout << "Error getting required storage." << endl;
                exit(1);
        }

        cout << "Storage needed: " << storage_needed << endl;

        symbol_table = (bfd_symbol **) malloc(storage_needed);
        num_symbols = bfd_canonicalize_symtab(abfd, symbol_table);

        bfd_close(abfd);
}

=======================================================================

And here's the output:

=======================================================================
File info for /bin/bash
Target name: elf32-i386
Target endianness: 1
File format string: unknown
Open for reading.
Sections address: 0
Listing sections:
Reading symbols:
Segmentation fault
=======================================================================

It seems to identify it properly (though maybe that's what "default"
would do anyways?), and it doesn't error out, seemingly, but the
sections are blank and the symbol call there segfaults. Also, the file
format is 0 ("unknown"). Not sure why it would set that...

Anyone want to help me figure this out? Is there something obvious I'm
missing? Alternatively, is there any documentation around that is more
newbie-friendly than
http://sourceware.org/binutils/docs-2.19/bfd/index.html ?

Thanks!
- Yan

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Reading an elf file with binutils
  2009-08-06  7:20 Reading an elf file with binutils Yan
@ 2009-08-06  7:58 ` Alan Modra
  2009-08-06 18:41   ` Yan
  2009-08-06 13:39 ` Simon Richter
  1 sibling, 1 reply; 5+ messages in thread
From: Alan Modra @ 2009-08-06  7:58 UTC (permalink / raw)
  To: Yan; +Cc: binutils

On Thu, Aug 06, 2009 at 12:19:30AM -0700, Yan wrote:
> Anyone want to help me figure this out? Is there something obvious I'm
> missing?

You need to do a lot more than just open the file.  binutils comes
with many examples of how to use BFD.  strings.c, nm.c, objcopy.c, ...

-- 
Alan Modra
Australia Development Lab, IBM

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Reading an elf file with binutils
  2009-08-06  7:20 Reading an elf file with binutils Yan
  2009-08-06  7:58 ` Alan Modra
@ 2009-08-06 13:39 ` Simon Richter
  1 sibling, 0 replies; 5+ messages in thread
From: Simon Richter @ 2009-08-06 13:39 UTC (permalink / raw)
  To: Yan; +Cc: binutils

Hi,

On Thu, Aug 06, 2009 at 12:19:30AM -0700, Yan wrote:

> Basically, I'm trying to read an ELF file with BFD and get some data
> out of it. However, the list of sections that BFD provides is blank,
> and when I try to read symbols out of the file at all, the whole thing
> segfaults.

You are looking at an executable, where section and symbol information is
optional as it's not used for anything (loading is controlled by the PHDRs,
and library symbol resolution uses the DYNAMIC segment).

   Simon

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Reading an elf file with binutils
  2009-08-06  7:58 ` Alan Modra
@ 2009-08-06 18:41   ` Yan
  2009-08-07 12:13     ` Alan Modra
  0 siblings, 1 reply; 5+ messages in thread
From: Yan @ 2009-08-06 18:41 UTC (permalink / raw)
  To: binutils

Awesome, thanks! The missing part turned out to be bfd_check_format.
Once I had that in, everything seems to work.

I have a follow-up question.. I'm using bfd_section::filepos to see
where in the file the section is, but the documentation says that it's
not valid for all backends and is not always updated. Is it fairly
reliable on average? Mostly I'll be using this code on ELF and Windows
PE executables...

Thanks for your help!
- Yan

On Thu, Aug 6, 2009 at 12:58 AM, Alan Modra <amodra@bigpond.net.au> wrote:
>
> On Thu, Aug 06, 2009 at 12:19:30AM -0700, Yan wrote:
> > Anyone want to help me figure this out? Is there something obvious I'm
> > missing?
>
> You need to do a lot more than just open the file.  binutils comes
> with many examples of how to use BFD.  strings.c, nm.c, objcopy.c, ...
>
> --
> Alan Modra
> Australia Development Lab, IBM

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Reading an elf file with binutils
  2009-08-06 18:41   ` Yan
@ 2009-08-07 12:13     ` Alan Modra
  0 siblings, 0 replies; 5+ messages in thread
From: Alan Modra @ 2009-08-07 12:13 UTC (permalink / raw)
  To: Yan; +Cc: binutils

On Thu, Aug 06, 2009 at 11:40:32AM -0700, Yan wrote:
> I have a follow-up question.. I'm using bfd_section::filepos to see
> where in the file the section is, but the documentation says that it's
> not valid for all backends and is not always updated. Is it fairly
> reliable on average? Mostly I'll be using this code on ELF and Windows
> PE executables...

Probably OK for those targets.

-- 
Alan Modra
Australia Development Lab, IBM

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2009-08-07 12:13 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-08-06  7:20 Reading an elf file with binutils Yan
2009-08-06  7:58 ` Alan Modra
2009-08-06 18:41   ` Yan
2009-08-07 12:13     ` Alan Modra
2009-08-06 13:39 ` Simon Richter

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