This is the mail archive of the
binutils@sourceware.org
mailing list for the binutils project.
Reading an elf file with binutils
- From: Yan <zardus at gmail dot com>
- To: binutils at sourceware dot org
- Date: Thu, 6 Aug 2009 00:19:30 -0700
- Subject: Reading an elf file with 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