[英]Setup a custom Thread Local Storage
對於我的一些研究相關項目,我正在嘗試在創建新線程后設置第二個 TLS。
我想我能夠在pthread
庫中找到負責設置 TLS 的代碼。 例如,我感興趣的一個 function 是: _dl_allocate_tls ()
from here 。
所以理想情況下,我想寫一些類似的東西:
#include <elf/dl-tls.c>
#include <sys/syscall.h>
int main(int argc, char** argv) {
void* my_new_tls = _dl_allocate_tls();
syscall(SYS_arch_prctl, ARCH_SET_FS, my_new_tls);
}
在 Ubuntu 上安裝libc6-dev
不會讓我訪問上述文件/標題/代碼。 關於如何調用上述函數的任何想法?
此外,任何關於如何以更簡單、更正確的方式安裝自定義 TLS 的建議都非常受歡迎!
如果您想在 x86-64 上使用第二個 TLS 空間,您需要使用帶有%gs
段基礎的 TLS,而不是像系統的 rest 那樣的%fs
。 如果這樣做,您可以像管理任何其他 memory 一樣管理線程本地 memory,並且您不需要與 glibc 協調。 這就是 Wine 在內部所做的。
我實際上發現我可以在 libc 的加載器上搭載(或 piggy-bug)來為我完成這項工作。 這是我為將來參考所做的:
// This is an amazing hack to directly call libc's loader in: glibc/elf/dl-tls.c
void *_dl_allocate_tls(void *mem);
// Include related structs from glibc/sysdeps/generic/dl-dtv.h and
// glibc/sysdeps/x86_64/nptl/tls.h
void *new_tls() {
tcbhead_t *n_tls = (tcbhead_t *)_dl_allocate_tls(NULL);
// For the following see: glibc/nptl/pthread_create.c
/* Reference to the TCB itself. */
// pd->header.self = pd;
n_tls->self = n_tls;
/* Self-reference for TLS. */
// pd->header.tcb = pd;
n_tls->tcb = n_tls;
/* Copy the stack guard canary. */
// THREAD_COPY_STACK_GUARD (pd);
n_tls->stack_guard = current_tls->stack_guard;
/* Copy the pointer guard value. */
// THREAD_COPY_POINTER_GUARD (pd);
n_tls->pointer_guard = current_tls->pointer_guard;
/* Setup tcbhead. */
// tls_setup_tcbhead (pd);
n_tls->feature_1 = current_tls->feature_1;
// End of: glibc/nptl/pthread_create.c
return n_tls;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.