processuserrel - Process custom relocations


Main page on sourceforge.net
Source code page on sourceforge.net

Background

In some cases it is useful to allow user-defined relocations in object files. One example could be the following one:
    extern char start;
    extern char end;
    ...
    fwrite(&start, 1, (int)&end - (int)&start, fileHandle);
The expression "(int)&end-(int)&start" can be calculated at link time so it doesn't have to be calculated at runtime. However object files cannot store this information so up to now this calculation had to be done at runtime.

Even more obvious is the use case when object files shall contain code for a different architecture - for example if a machine contains a main CPU of one architecture and a co-processor of another architecture using different relocation types:
    # x86 main CPU code
    .text
    .code32
        ...
        mov $100, %ax
        mov $someSymbol, %ebx
        ...
        
    # co-processor code
    .section .cotext
    .codexyz
        ...
        movhi %hi19(someSymbol), %sx2
        lamid %mid7(someSymbol)(%sx2), %sx2
        trans (%sx3), (%dx2)
        trans %lo6(someSymbol)(%sx2), %lo6(otherSymbol)(%dx1)
        ...
In the (hypothetical) example above the linker will not know the relocation types for "%hi19", "%mid7" and "%lo6" so linking will not be possible.

This project

This project specifies a special file format which uses two special sections allowing the definition of special relocations. In the case of the example above an assembler supporting instructions for a special co-processor will actually generate the following code:
        ...
        # The following relocation can be processed normally
        mov $someSymbol, %ebx
        ...
        trans (%sx3), (%dx2)
    .reloc123:
        # trans %lo6(someSymbol)(%sx2), %lo6(otherSymbol)(%dx1)
        .long 0
        ...
    .section .customreloc
        ...
        .word 0xE1A5
        .byte 33
        .byte 20
        .long REL_XYZ_LO6_LO6
        .long .reloc123
        # 0x12345xxx = trans x(%sx2), y(%dx1)
        .long 0x12345000
        .long someSymbol
        .long otherSymbol
        ...
The project contains a special development tool. After linking this tool will parse the .customreloc section (which is an equivalent of the relocation table) and the .cusrelocinfo section (which may be defined in another object file or a library and which contains the actual "formulas" for the relocations).

The tool will then actually post-process the relocations (by modifying the linker output file)

Documentation

Manpage for this tool
File format specification