This is an archive of the discontinued LLVM Phabricator instance.

[elf2] Add support for R_X86_64_TLSLD.
ClosedPublic

Authored by Bigcheese on Nov 4 2015, 4:45 PM.

Details

Reviewers
ruiu
rafael
Summary

leaq symbol@tlsld(%rip), %rdi
call __tls_get_addr@plt

symbol@tlsld (R_X86_64_TLSLD) instructs the linker to generate a tls_index entry (two GOT slots) in the GOT for the entire module (shared object or executable) with an offset of 0. The symbol for this GOT entry doesn't matter (as long as it's either local to the module or null), and gold doesn't put a symbol in the dynamic R_X86_64_DTPMOD64 relocation for the GOT entry.

All other platforms defined in http://www.akkadia.org/drepper/tls.pdf except for Itanium use a similar model where global and local dynamic GOT entries take up 2 contiguous GOT slots, so we can handle this in a unified manner if we don't care about Itanium.

While scanning relocations we need to identify local dynamic relocations and generate a single tls_index entry in the GOT for the module and store the address of it somewhere so we can later statically resolve the offset for R_X86_64_TLSLD relocations. We also need to generate a R_X86_64_DTPMOD64 relocation in the RelaDyn relocation section.

This implementation is a bit hacky. It side steps the issue of GotSection and RelocationSection only handling SymbolBody entries by relying on a specific relocation type. The alternative to this seemed to be completely rewriting how GotSection and RelocationSection work, or using a different hacky signaling method.

Diff Detail

Event Timeline

Bigcheese updated this revision to Diff 39290.Nov 4 2015, 4:45 PM
Bigcheese retitled this revision from to [elf2] Add support for R_X86_64_TLSLD..
Bigcheese updated this object.
Bigcheese added reviewers: ruiu, rafael.
Bigcheese added a subscriber: llvm-commits.
emaste added a subscriber: emaste.Nov 4 2015, 8:17 PM
rafael edited edge metadata.Nov 6 2015, 2:27 PM
rafael added a subscriber: rafael.

rebased the patch and fixed the warnings.

rafael added inline comments.Nov 6 2015, 2:33 PM
test/elf2/tls-dynamic.s
4

When producing an executable gold produces no dynamic relocations. I understand that is just an optimization that we don't have yet. To avoid changing this test is the future, can you change it to use -shared? With that you can also remove

​ .text

		​  .globl  _start
		​_start:

Please add a second " leaq b@tlsld(%rip), %rdi" to show that only a
single R_X86_64_DTPMOD64 is created.

rafael added inline comments.Nov 6 2015, 3:03 PM
ELF/InputSection.cpp
106

Buf + RI.r_offset is now used 3 times is this function. Please factor it into a variable above, commit that and rebase.

The same is true for BaseAddr + RI.r_offset

ELF/OutputSections.cpp
85

Will this also be used for global tls? Change the name if so.

ruiu edited edge metadata.Nov 6 2015, 3:29 PM
ruiu added a subscriber: ruiu.

I'm out of office today. I'll review this in a few days. Sorry for the
delay.

Bigcheese added inline comments.Nov 6 2015, 3:43 PM
ELF/OutputSections.cpp
85

No. Global Dynamic symbols can be handled by addEntry already. Just needs to check the symbol type.

test/elf2/tls-dynamic.s
4

It's already -shared, I'll remove the _start symbol.

Bigcheese updated this revision to Diff 39755.Nov 9 2015, 2:36 PM
Bigcheese edited edge metadata.

Strengthened test and removed redundancy.

rafael accepted this revision.Nov 10 2015, 1:13 PM
rafael edited edge metadata.

LGTM with the {} removed.

ELF/Target.h
72

No other entry uses {}

This revision is now accepted and ready to land.Nov 10 2015, 1:13 PM
Bigcheese added inline comments.Nov 10 2015, 2:36 PM
ELF/Target.h
72

The other entries don't require a default value. This is only set for x86-64, and needs to be defaulted to zero otherwise. TlsModuleIndexReloc doesn't need it though.

ruiu accepted this revision.Nov 11 2015, 1:54 PM
ruiu edited edge metadata.

LGTM

Bigcheese closed this revision.Nov 11 2015, 2:04 PM