Search code examples
x86linkerelfi386relocation

Difference between the calculation logic for R_386_32 and R_X86_64_64


As I know, the relocation calculation for both ways is the same, namely: S + A, but seems for R_386_32, the "A" here stands for the stored value on the fixed memory location. And for R_X86_64_64, it stands for the addend on the corresponding symbol, is this understanding correct?


Solution

  • Relocation calculation method S+A is valid for both 32bit and 64bit modes. This A represents the sum of two entities:

    1. Value inserted by compiler to the memory unit (DWORD ior QWORD) which is the object of relocation, and
    2. addend field of the relocation record R_386_32 or R_X86_64_64.

    Compilers and assemblers creating ELF64 prefer relocation records with explicit addend, emited in ELF section .rela.name.

    When they produce ELF32, they emit relocations without addend, emited in ELF section .rel.name. The addend needs to be added to the relocated memory unit at compile time.
    The same philosophy (no addend value in relocation record) is used when COFF object file is produced, both in 32 and 64bit variants.

    BTW GNU linker ld seems to not accept .rela records with addend in ELF32 object modules. I tried to link my handcrafted ELF32 module with explicit addends and they were ignored, although both Elf32_Rel and Elf32_Rela structures are declared in ELF specification.