From 1219d919cac7f8e3c09b1bfd0804b60ac7daea36 Mon Sep 17 00:00:00 2001 From: Alexander van der Grinten Date: Mon, 27 Jan 2025 22:42:54 +0100 Subject: [PATCH] options/rtld: Set up early TCB to cache TID --- options/rtld/generic/main.cpp | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/options/rtld/generic/main.cpp b/options/rtld/generic/main.cpp index 48138174f..34f62d487 100644 --- a/options/rtld/generic/main.cpp +++ b/options/rtld/generic/main.cpp @@ -43,6 +43,7 @@ mlibc::RtldConfig rtldConfig; bool ldShowAuxv = false; uintptr_t *entryStack; +static constinit Tcb earlyTcb{}; frg::manual_box initialRepository; frg::manual_box globalScope; @@ -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()); @@ -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;