Skip to content

Commit

Permalink
options/rtld: Set up early TCB to cache TID
Browse files Browse the repository at this point in the history
  • Loading branch information
avdgrinten committed Jan 27, 2025
1 parent 1fda4e6 commit 1219d91
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions options/rtld/generic/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ mlibc::RtldConfig rtldConfig;
bool ldShowAuxv = false;

uintptr_t *entryStack;
static constinit Tcb earlyTcb{};
frg::manual_box<ObjectRepository> initialRepository;
frg::manual_box<Scope> globalScope;

Expand Down Expand Up @@ -282,6 +283,15 @@ extern "C" void *interpreterMain(uintptr_t *entry_stack) {
if(logEntryExit)
mlibc::infoLogger() << "Entering ld.so" << frg::endlog;
entryStack = entry_stack;

// Set up an early TCB such that we can cache our own TID.
// The TID is needed to use futexes, so this caching saves a lot of syscalls.
earlyTcb.selfPointer = &earlyTcb;
earlyTcb.tid = mlibc::this_tid();
if(mlibc::sys_tcb_set(&earlyTcb))
__ensure(!"sys_tcb_set() failed");
mlibc::tcb_available_flag = true;

runtimeTlsMap.initialize();
libraryPaths.initialize(getAllocator());
preloads.initialize(getAllocator());
Expand Down Expand Up @@ -540,10 +550,9 @@ extern "C" void *interpreterMain(uintptr_t *entry_stack) {
mlibc::initStackGuard(stack_entropy);

auto tcb = allocateTcb();
tcb->tid = earlyTcb.tid;
if(mlibc::sys_tcb_set(tcb))
__ensure(!"sys_tcb_set() failed");
tcb->tid = mlibc::this_tid();
mlibc::tcb_available_flag = true;

globalDebugInterface.ver = 1;
globalDebugInterface.brk = &dl_debug_state;
Expand Down

0 comments on commit 1219d91

Please sign in to comment.