Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for ELF thread-local storage #419

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

joboet
Copy link

@joboet joboet commented Dec 2, 2022

Handling thread-local storage in the binary format significantly simplifies its implementation by removing the need for dynamic allocation of keys and memory. The ELF format, used as a compiler target for SGX, does feature the required extensions1.

Because SGX binaries are statically linked and do not support dynamic loading of libraries, only the local-exec model needs to be supported, which has the added benefit of not needing any additional relocations. In this model, the fs segment is used as a thread pointer, pointing to a thread control block whose first entry contains the thread pointer itself (thereby allowing simpler address computations). The TLS data is placed in front of the TCB and is accessed by subtracting fixed offsets (obtained at link-time) from the thread pointer.

Because SGX supports only a fixed number of threads, this TLS area can be statically allocated for each thread, avoiding memory allocation failures. Also, since the fs segment was previously unused, the tool can remain backwards-compatible by setting the gs segment to the address of the old TCB block (located 8 bytes above the thread pointer). New software should probably access the TCB via the fs segment, however, so that the gs segment can be used for other purposes in the future.

Since this PR implements new features, I assume the toolchain version will need to be bumped, but I am unsure of the process for that.

Footnotes

  1. https://uclibc.org/docs/tls.pdf

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.

1 participant