[lld] [ELF] Add target-specific relocation scanning for x86 (PR #178846)
Fangrui Song via llvm-commits
llvm-commits at lists.llvm.org
Tue Mar 10 10:18:26 PDT 2026
MaskRay wrote:
> The -u adds an undefined which does not have type STT_TLS. When the undefined symbol in the object file is encountered this does not set the type to STT_TLS.
>
> As ELF requires TLS relocations to target TLS symbols, it may be worth forcing the type of undefined symbols to STT_TLS if they have the lld pseudo object file used by -u and other directives where we can't know the symbol type. Probably should be an error message if the undefined symbol is from an object file.
>
> I think this used to work, probably by accident due to [`main`/lld/ELF/Relocations.cpp#L956](https://github.com/llvm/llvm-project/blob/main/lld/ELF/Relocations.cpp#L956) as `sym->isTls()` won't be true in this case.
>
> > if (needsGot(expr)) {
> > if (ctx.arg.emachine == EM_MIPS) {
> > // MIPS ABI has special rules to process GOT entries and doesn't
> > // require relocation entries for them. A special case is TLS
> > // relocations. In that case dynamic loader applies dynamic
> > // relocations to initialize TLS GOT entries.
> > // See "Global Offset Table" in Chapter 5 in the following document
> > // for detailed description:
> > // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
> > ctx.in.mipsGot->addEntry(*sec->file, sym, addend, expr);
> > } else if (!sym.isTls() || ctx.arg.emachine != EM_LOONGARCH) {
> > // Many LoongArch TLS relocs reuse the RE_LOONGARCH_GOT type, in which
> > // case the NEEDS_GOT flag shouldn't get set.
> > sym.setFlags(NEEDS_GOT | NEEDS_GOT_NONAUTH);
> > }
> > }
With old lld, the output of `ld.lld -shared reduced.o -o a.so -u __msan_va_arg_overflow_size_tls` is also wrong - the symbol `__msan_va_arg_overflow_size_tls` has a type of STT_NOTYPE (instead of STT_TLS).
We handle `-u` symbols with `SymbolTable::addUnusedUndefined`, which sets the type to 0 (STT_NOTYPE). During symbol resolution, ` void Symbol::resolve(Ctx &ctx, const Undefined &other)` cannot change `type` to `STT_TLS`.
I believe we have never supported `-u` on a TLS symbol. All the usage should be considered unsupported.
https://github.com/llvm/llvm-project/pull/178846
More information about the llvm-commits
mailing list