Skip to content

symtabAPI: read dynamic relocations from the .dynamic ranges, not through section headers #2589

Open

Description

@kupsch

Found while fixing #2585 (#2588). SymtabAPI reads the relocation tags in .dynamic but uses them only to find section headers, and the rewriter keeps one tag per address. Each layout the section headers don't describe one-to-one has needed its own workaround. This proposes reading the relocations from the address ranges the tags give, as ld.so does.

What SymtabAPI does now

  • parseDynamic() (Object-elf.C:958) reads DT_RELA, DT_RELASZ, DT_JMPREL and DT_PLTRELSZ. The dynamic relocations are then read from whole sections, starting at the one whose address is DT_RELA (get_relocationDyn_entries(), Object-elf.C:1058). Where that range also covers .rela.plt, a special case counts only the entries before DT_JMPREL (Object-elf.C:1073).
  • The PLT relocations are read from the section matched to DT_JMPREL through secAddrTagMapping, a map from address to tag (Object-elf.C:782). .dynsym and .dynstr are matched the same way.
  • secTagRegionMapping (Object-elf.C:935) is built from that map, and emitElf uses it to name the sections it rebuilds. It sets each tag from the type of the section it writes.

What goes wrong

The map keeps one tag per address, and the reader assumes each range starts a section.

layout result status
DT_RELA = DT_JMPREL no binding table #2480 adds jmprel_addr_
same, rewritten wrong DT_RELASZ, misnamed section #2585, fixed by #2588
.rela.plt before a non-empty .rela.dyn no dynamic relocations read open
no section headers no relocations read open
  • The first two are every riscv64 object linked by GNU ld whose only dynamic relocations are PLT ones, such as an executable linked -no-pie. GNU ld's riscv64 target counts .rela.plt in DT_RELASZ.
  • The third comes from the overlap special case. DT_RELA would be .rela.plt's address, so the count of entries before DT_JMPREL is 0. Found by reading the code; the default linker scripts don't produce this layout.
  • The fourth: even .dynamic is found through its section header (Object-elf.C:479), so without section headers nothing is read. ld.so doesn't need them.

Proposal

  1. Reading. Read each relocation table as the address range its tags give, translated to file offsets through the PT_LOAD segments. Any part of (DT_RELA, DT_RELASZ) that falls inside (DT_JMPREL, DT_PLTRELSZ) holds PLT relocations, not dynamic ones. This covers all four layouts above with one rule.
  2. Rewriting. Replace secAddrTagMapping with a map from tag to address, so two tags can name one section. jmprel_addr_ and symtabAPI: size each rewritten relocation section by what it holds #2588's check against DT_JMPREL then go away.

The PLT reader has code for each architecture, and emitElf refers to secTagRegionMapping about 20 times, so this is more than a small fix. It doesn't block #2480 or #2588.

Activity

  1. added
    Symtab
    ELF
    on Nov 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    ELFSymtabenhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions