[lld] [ELF] Add target-specific relocation scanning for x86 (PR #178846)

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Fri Jan 30 10:10:43 PST 2026


MaskRay wrote:

> > > Rather than having to duplicate the implementation of these for each target (which, aside from being a bunch of extra code to write, will eventually lead to divergent implementations as features or bug fixes get added to one but not another), would it not be better to keep the single implementation but template it so the compiler can do the specialisation for us?
> > 
> > 
> > In `X86_64::scanSectionImpl`, although the initial code has some duplication when extracting relocation members, the switch table elegantly merges `getRelExpr` with the concrete action - dispatching to functions like `rs.scanPCRel`, `rs.scanTlsGd`, and `rs.handleTlsDesc` to ensure code sharing. Placing this code in `RelocScan.h` would make emitting a jump table code generation challenging. The switch table also keeps target-specific special cases (like AArch64 pauth, which I'll migrate next) from polluting the generic code.
> > In addition, the switch table dispatching on relocation types resembles `X86_64::relocate`. This will help us remove the expensive `sec.addReloc(...)` eventually.
> 
> But it becomes a maintenance nightmare for e.g. every target to reimplement the same logic for TLS IE optimisations like you have now:
> 
> ```c++
>     case R_386_TLS_IE:
>       ctx.hasTlsIe.store(true, std::memory_order_relaxed);
>       if (!ctx.arg.shared && !sym.isPreemptible) {
>         sec.addReloc({R_TPREL, type, offset, addend, &sym});
>       } else {
>         sym.setFlags(NEEDS_TLSIE);
>         // In PIC, the absolute GOT address needs a RELATIVE dynamic relocation.
>         if (ctx.arg.isPic)
>           sec.getPartition(ctx).relaDyn->addRelativeReloc(
>               ctx.target->relativeRel, sec, offset, sym, addend, type, R_GOT);
>         else
>           sec.addReloc({R_GOT, type, offset, addend, &sym});
>       }
>       continue;
> ```
> 
> This is not good software engineering. So can we please try and do it another way that gets the performance benefit without ending up with every target reimplementing all the logic for relocations?

This is a special TLS IE only used by i386 and SystemZ. In this case I think it can be merged into `rs.handleTlsIe`.

https://github.com/llvm/llvm-project/pull/178846


More information about the llvm-commits mailing list