This is the mail archive of the
binutils@sourceware.org
mailing list for the binutils project.
using BFD to load and reloc a simple file
- From: thockin at hockin dot org
- To: binutils at sourceware dot org
- Date: Thu, 2 Mar 2006 13:15:18 -0800
- Subject: using BFD to load and reloc a simple file
- Bcc: thockin at hockin dot org
I am trying to load and relocate a simple ELF executable. It's entirely
self contained, but needs to be loadable at an arbitrary 32 bit address.
I'm trying to use libbfd for this, but I am having trouble. The load goes
fine, and it copies the ELF data into memory where I tell it to. Very
nice and very easy. When I call perform_relocation(), though, it goes
haywire.
First, my code:
int
load_and_reloc(char *filename, uint8_t *mem)
{
bfd *payload;
asection *sect;
int i;
long symsize;
asymbol **symtab;
bfd_init();
/* open the payload */
payload = bfd_openr(filename, NULL);
if (!payload) {
fprintf(stderr, "error: can't open %s\n", filename);
return 1;
}
bfd_check_format(payload, bfd_object);
/* find the magic section */
sect = bfd_get_section_by_name(payload, ".aseg");
if (!sect) {
fprintf(stderr, "error: can't find .aseg section\n");
return 1;
}
/* load the symbol table */
symsize = bfd_get_symtab_upper_bound(payload);
if (symsize <= 0) {
fprintf(stderr, "error: can't get symbol table\n");
return 1;
}
symtab = malloc(symsize);
if (!symtab) {
perror("malloc()");
return 1;
}
bfd_canonicalize_symtab(payload, symtab);
/* load the section into memory */
bfd_get_section_contents(payload, sect, mem, 0, sect->size);
bfd_set_section_vma(payload, sect, (unsigned)mem);
/* do any relocations */
{
long rsize;
arelent **relocs;
long nrelocs;
int j;
char *err;
rsize = bfd_get_reloc_upper_bound(payload, sect);
relocs = malloc(rsize);
if (!relocs) {
perror("malloc()");
return 1;
}
nrelocs = bfd_canonicalize_reloc(payload, sect, relocs, symtab);
for (j = 0; j < nrelocs; j++) {
bfd_perform_relocation(payload, relocs[j],
mem+(i*1024*1024), sect, NULL, &err);
}
}
return 0;
}
bfd_perform_relocation() eventually does:
reloc_target_output_section = symbol->section->output_section;
The problem is that output_section is always NULL. I don't want to output
a BFD, I want to relocate it in memory.
Is this possible? Am I missing something important? The BFD docs are
very good in many areas, but the relocation docs are missing plenty of
details.
Help?
Tim
p.s. I am not on this mailing list, can you plase CC: me on replies? :)