Skip to content

elf: absolute symbols in a shared object get R_*_RELATIVE GOT relocations, so they're rebased at load #200

Open

Description

@zackees

Bug

In a -shared output, a GOT slot for an absolute symbol (SHN_ABS) gets an R_*_RELATIVE dynamic relocation. RELATIVE adds the load base, so at runtime the code reads load_base + value instead of value.

Found by the new aarch64 acceptance leg (#194). It's architecture-general, not aarch64-specific.

Reproduction

Take crates/reld/tests/sources/elf/linker-script-provide/, whose script defines absolute symbols such as PROVIDE(provided_absolute = 0x1000), and link it as a shared object:

clang --target=aarch64-linux-gnu -fPIC -O1 -ffreestanding -c linker-script-provide.c -o lsp.o
reld   -m aarch64linux -shared -T linker-script-provide.ld -o reld.so lsp.o
ld.lld -m aarch64linux -shared -T linker-script-provide.ld -o lld.so  lsp.o

reld marks the symbols ABS in its own symbol table:

3: 0000000000001000  NOTYPE GLOBAL DEFAULT  ABS provided_absolute
8: ffffffff81000000  NOTYPE GLOBAL DEFAULT  ABS provided_expr2

but relocates their GOT slots as load-relative:

reld:  R_AARCH64_RELATIVE  1000
       R_AARCH64_RELATIVE  2000              (provided_hidden_absolute)
       R_AARCH64_RELATIVE  3000
       R_AARCH64_RELATIVE  ffffffff81000000
lld:   R_AARCH64_GLOB_DAT  provided_absolute + 0   (value 0x1000, ABS)

x86_64 does the same (-m elf_x86_64): R_X86_64_RELATIVE 1000 / 2000 / 3000 / ffffffff81000000.

Why the x86_64 leg never caught it

linker-script-provide is RunEnabled:false, and it already ignores .dynamic.*, section.got and section.rela.dyn under #13. On x86_64, GOTPCRELX relaxation also turns some of these accesses into lea, which moves the problem off the GOT.

Expected

  • A GOT slot for an absolute symbol needs no load-relative relocation. Either write the constant with no dynamic relocation (for a non-preemptible absolute symbol), or use a symbol-based GLOB_DAT as lld does (for a preemptible one).
  • A related question worth settling at the same time: reld binds these PROVIDEd default-visibility symbols locally in a shared object, while lld keeps them preemptible (GLOB_DAT). Decide which is right under -shared without -Bsymbolic.

Acceptance

  • The reproduction above emits no RELATIVE relocation for any ABS symbol.
  • A fixture that runs, and reads an absolute PROVIDE symbol through the GOT from a shared object, returns the exact constant.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions