* using BFD to load and reloc a simple file
@ 2006-03-02 21:10 thockin
2006-03-02 21:18 ` Daniel Jacobowitz
0 siblings, 1 reply; 5+ messages in thread
From: thockin @ 2006-03-02 21:10 UTC (permalink / raw)
To: binutils
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? :)
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: using BFD to load and reloc a simple file
2006-03-02 21:10 using BFD to load and reloc a simple file thockin
@ 2006-03-02 21:18 ` Daniel Jacobowitz
2006-03-02 21:48 ` thockin
0 siblings, 1 reply; 5+ messages in thread
From: Daniel Jacobowitz @ 2006-03-02 21:18 UTC (permalink / raw)
To: thockin; +Cc: binutils
On Thu, Mar 02, 2006 at 01:15:18PM -0800, thockin@hockin.org wrote:
> 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.
That's because this is a routine for the linker, not a routine for
in-place relocation.
> 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.
You might be able to set the section to point at itself. However, I'm
not sure if bfd_perform_relocation even does the operation you want it
to.
--
Daniel Jacobowitz
CodeSourcery
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: using BFD to load and reloc a simple file
2006-03-02 21:18 ` Daniel Jacobowitz
@ 2006-03-02 21:48 ` thockin
2006-03-02 23:00 ` David Daney
2006-03-02 23:31 ` Daniel Jacobowitz
0 siblings, 2 replies; 5+ messages in thread
From: thockin @ 2006-03-02 21:48 UTC (permalink / raw)
To: binutils
On Thu, Mar 02, 2006 at 04:18:00PM -0500, Daniel Jacobowitz wrote:
> On Thu, Mar 02, 2006 at 01:15:18PM -0800, thockin@hockin.org wrote:
> > 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.
>
> That's because this is a routine for the linker, not a routine for
> in-place relocation.
Is there an in-place variant?
> > 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.
>
> You might be able to set the section to point at itself. However, I'm
> not sure if bfd_perform_relocation even does the operation you want it
> to.
I tried that and it stopped finding any relocations at all! I did not
debug further.
I'm not sure that I am doing what I want to do most effectively. The end
goal is a small self-contained executable with a few sections that will be
located into memory at some system-specific addresses (which can not be
known at build time).
I've succeeded in making a static binary that works for a given fixed
address. Now I need to make it relocateable. Linking with -r seems to
give me the binary I want (though I am not sure it would catch
unresolvable references anymore).
Would I be better off using -pie or some other method?
> --
> Daniel Jacobowitz
> CodeSourcery
--
Tim Hockin
thockin@hockin.org
Soon anyone who's not on the World Wide Web will qualify for a government
subsidy for the home-pageless.
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: using BFD to load and reloc a simple file
2006-03-02 21:48 ` thockin
@ 2006-03-02 23:00 ` David Daney
2006-03-02 23:31 ` Daniel Jacobowitz
1 sibling, 0 replies; 5+ messages in thread
From: David Daney @ 2006-03-02 23:00 UTC (permalink / raw)
To: thockin; +Cc: binutils
thockin@hockin.org wrote:
> I'm not sure that I am doing what I want to do most effectively. The end
> goal is a small self-contained executable with a few sections that will be
> located into memory at some system-specific addresses (which can not be
> known at build time).
>
> I've succeeded in making a static binary that works for a given fixed
> address. Now I need to make it relocateable. Linking with -r seems to
> give me the binary I want (though I am not sure it would catch
> unresolvable references anymore).
>
> Would I be better off using -pie or some other method?
>
>
You could take a look at the 2.6.x Linux kernel or the insmod program
for kernel 2.4.x. They both load and relocate elf objects and are GPL.
David Daney
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: using BFD to load and reloc a simple file
2006-03-02 21:48 ` thockin
2006-03-02 23:00 ` David Daney
@ 2006-03-02 23:31 ` Daniel Jacobowitz
1 sibling, 0 replies; 5+ messages in thread
From: Daniel Jacobowitz @ 2006-03-02 23:31 UTC (permalink / raw)
To: thockin; +Cc: binutils
On Thu, Mar 02, 2006 at 01:54:14PM -0800, thockin@hockin.org wrote:
> I've succeeded in making a static binary that works for a given fixed
> address. Now I need to make it relocateable. Linking with -r seems to
> give me the binary I want (though I am not sure it would catch
> unresolvable references anymore).
>
> Would I be better off using -pie or some other method?
The best way to do this is link it as a shared object, and then
resolve dynamic relocations (of which there are many fewer). You
can avoid BFD entirely.
--
Daniel Jacobowitz
CodeSourcery
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2006-03-02 23:31 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-03-02 21:10 using BFD to load and reloc a simple file thockin
2006-03-02 21:18 ` Daniel Jacobowitz
2006-03-02 21:48 ` thockin
2006-03-02 23:00 ` David Daney
2006-03-02 23:31 ` Daniel Jacobowitz
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).