Skip to content

[X86] Fix R_X86_64_TPOFF32 against weak undefined TLS symbols - #1646

Open
Rachit Mehta (rachitmeht) wants to merge 1 commit into
qualcomm:mainfrom
rachitmeht:fix/weak-undef-tls-tpoff
Open

[X86] Fix R_X86_64_TPOFF32 against weak undefined TLS symbols#1646
Rachit Mehta (rachitmeht) wants to merge 1 commit into
qualcomm:mainfrom
rachitmeht:fix/weak-undef-tls-tpoff

Conversation

@rachitmeht

@rachitmeht Rachit Mehta (rachitmeht) commented Aug 6, 2026

Copy link
Copy Markdown

A weak undefined TLS symbol has no slot in the PT_TLS segment, so the thread-pointer-offset formula (S - templateSize) is meaningless for it. Previously relocTPOFF applied that formula with a bogus symbol value, producing an incorrect tpoff (e.g. -3) instead of the correct 0.

Two fixes, comparing lld's R_TPREL handling:

  • relocTPOFF (x86_64Relocator.cpp): short-circuit weak undefined symbols in a static executable, returning the addend directly instead of running the tpoff formula.

Add x86_64/linux/WeakUndefTLS covering TPOFF32/64 and DTPOFF32/64 against weak-undef symbols, a defined-symbol regression guard, and a non-zero addend case.

Resolves #1540

Comment thread lib/Target/GNULDBackend.cpp Outdated
// be 0 instead of 1 (true).
if (!pSymbol->hasFragRef())
return true;
return false;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be a seperate change.

(pParent.config().codeGenType() == LinkerConfig::Exec)) {
Relocator::DWord A = pReloc.addend();
return ApplyReloc(pReloc, A, pRelocDesc, DiagEngine, options, pParent);
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about shared/pie ?

what is the difference for other targets ?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TPOFF handling for Weak undef is not done for other architectures also.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I copied the check from relocAbs(), it mentions that weak-undef symbol resolving to 0 is only the correct link-time behavior in a static executable. So, for a shared/PIE, an unresolved symbol should be handled via dynamic relocations.

# Case 6: TPOFF32 + weak-undef + addend=2 -> 0x00000002
RUN: %clang %clangopts -c %p/Inputs/weak_tls_le_addend.s -o %t.addend.o
RUN: %link %linkopts -static %t.addend.o -o %t.addend.exe
RUN: llvm-objdump -s -j .data %t.addend.exe | FileCheck %s --check-prefix=ADDEND

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

%objdump

A weak undefined TLS symbol has no slot in the PT_TLS segment, so the
thread-pointer-offset formula (S - templateSize) is meaningless for it.
Previously relocTPOFF applied that formula with a bogus symbol value,
producing an incorrect tpoff (e.g. -3) instead of the correct 0.

Fix: add a weak-undef guard in relocTPOFF mirroring lld's R_TPREL
handling (if (sym.isUndefined()) return a) and the existing guard in
relocAbs (line 638).

Add x86_64/linux/WeakUndefTLS covering TPOFF32/64 and DTPOFF32/64
against weak-undef symbols, a defined-symbol regression guard, and a
non-zero addend case.

Resolves qualcomm#1540

Signed-off-by: Rachit Mehta <rachmeht@qti.qualcomm.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Weak undefined symbols causing wrong tpoff in Local Exec model

2 participants