[coreboot] Relocable payloads

Kevin O'Connor kevin at koconnor.net
Sun Feb 28 16:50:18 CET 2010

On Thu, Feb 25, 2010 at 01:03:58AM +0100, Rudolf Marek wrote:
> Hash: SHA1
> Hi,
> I have taken memtest reloc.c and glued it into libpayload. Check attached patch.
> It adds -fPIC too.


However, I think there is a simpler way to achieve the same goal.

The "-fPIC" option causes the compiler to place all relocations into
separate pages.  This really helps on multi-process operating systems
where the OS can then share all the code pages between multiple
applications.  When the same program/library is loaded twice it only
needs to duplicate the handful of relocation pages - all the code can
be shared.

On coreboot there isn't any value in placing all the relocations into
separate pages as the code isn't going to be "shared" like it would on
an OS.

In order to relocate, I think one really only needs to get at the
relocation information.  So, "-fPIC" shouldn't be needed - instead,
one should be able to use something like the ld "--emit-relocs" flag
to keep the relocation information.  Once the relocs are available,
the SELF format could store them as an array of offsets in a new
"section".  To relocate one should then be able to just do:

u32 reloc_count = ...;
u32 *relocs = ...;
void *final_code_loc = ...;
u32 relocation_delta = final_code_loc - SELF_header_code_loc;

for (i=0; i<reloc_count; i++)
    *(u32*)(final_code_loc + relocs[i]) += relocation_delta;

The relocations contain a list of all the places in the program that
rely on a fixed address.  Once we have that list, it should be
straight forward to update the program for a new fixed location.


More information about the coreboot mailing list