This is an archive of the discontinued LLVM Phabricator instance.

[ELF] Don't copy STT_TLS in copy relocation
ClosedPublic

Authored by MaskRay on Aug 3 2018, 5:25 PM.

Details

Summary

During copy relocation of a variable defined in a DSO, if a TLS variable in that DSO happens to have the same st_value, it would also be copied. This was incorrect because the addresses of TLS variables are relative to TLS segment. They don't interfere with non-TLS variables.

This copying behavior can be harmful in the following scenario:

For function-scope thread-local variables with non-trivial constructors,
they have guard variables. In the case of x86_64 general-dynamic model:

thread_local std::string a;

GOT[n]   R_X86_64_DTPMOD64 guard variable for a
GOT[n+1] R_X86_64_DTPOFF64 guard variable for a
GOT[n+2] R_X86_64_DTPMOD64 a
GOT[n+3] R_X86_64_DTPOFF64 a

a and its guard variable are both represented as TLS variables, which
should be within the same module. If one is copy relocated to the main
module while the other is not, their module ID will mismatch and can
cause access without prior construction.

Diff Detail

Repository
rLLD LLVM Linker

Event Timeline

MaskRay created this revision.Aug 3 2018, 5:25 PM
MaskRay edited the summary of this revision. (Show Details)Aug 3 2018, 5:32 PM
MaskRay edited the summary of this revision. (Show Details)Aug 3 2018, 5:38 PM
MaskRay edited the summary of this revision. (Show Details)Aug 3 2018, 5:42 PM
grimar added a subscriber: grimar.Aug 3 2018, 9:42 PM
ruiu accepted this revision.Aug 6 2018, 11:09 AM

LGTM

ELF/Relocations.cpp
469

nit: I'd check the value equality at the last of these boolean expressions. (i.e. instead of adding your new expression at the end, insert it to the place where it feels more natural, which is after the comparison against SHN_ABS).

This revision is now accepted and ready to land.Aug 6 2018, 11:09 AM
MaskRay updated this revision to Diff 159350.Aug 6 2018, 12:09 PM

Address ruiu's comment

This revision was automatically updated to reflect the committed changes.
MaskRay marked an inline comment as done.Aug 6 2018, 12:16 PM
echristo added inline comments.
ELF/Relocations.cpp
468

Drive by comment: It'd be great to get a comment about everything in this conditional :)

MaskRay added inline comments.Aug 7 2018, 2:11 PM
ELF/Relocations.cpp
468

Agree. I didn't comment them because I still don't understand the stuff enough and feel comfortable to document it :( Hope the conditions are comprehensive now.