Repository navigation
Open
Labels
Description
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) readsDT_RELA,DT_RELASZ,DT_JMPRELandDT_PLTRELSZ. The dynamic relocations are then read from whole sections, starting at the one whose address isDT_RELA(get_relocationDyn_entries(),Object-elf.C:1058). Where that range also covers.rela.plt, a special case counts only the entries beforeDT_JMPREL(Object-elf.C:1073).- The PLT relocations are read from the section matched to
DT_JMPRELthroughsecAddrTagMapping, a map from address to tag (Object-elf.C:782)..dynsymand.dynstrare matched the same way. secTagRegionMapping(Object-elf.C:935) is built from that map, andemitElfuses 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.pltinDT_RELASZ. - The third comes from the overlap special case.
DT_RELAwould be.rela.plt's address, so the count of entries beforeDT_JMPRELis 0. Found by reading the code; the default linker scripts don't produce this layout. - The fourth: even
.dynamicis found through its section header (Object-elf.C:479), so without section headers nothing is read. ld.so doesn't need them.
Proposal
- Reading. Read each relocation table as the address range its tags give, translated to file offsets through the
PT_LOADsegments. 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. - Rewriting. Replace
secAddrTagMappingwith 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 againstDT_JMPRELthen 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