目次: C言語とlibc
前回はスレッドローカル変数のアドレス取得部分のコードを見て、tp(スレッドポインタ)レジスタが重要な役割をしていることがわかりました。今回はtpレジスタの初期化コードを見ます。
レジスタの初期化は __libc_setup_tls() で行われます。少し長いですがコードを見ましょう。
// glibc/sysdeps/riscv/nptl/tls.h
/* The TP points to the start of the thread blocks. */
# define TLS_DTV_AT_TP 1
# define TLS_TCB_AT_TP 0
// glibc/csu/libc-tls.c
void
__libc_setup_tls (void)
{
void *tlsblock;
//...
/* We have to set up the TCB block which also (possibly) contains
'errno'. Therefore we avoid 'malloc' which might touch 'errno'.
Instead we use 'sbrk' which would only uses 'errno' if it fails.
In this case we are right away out of memory and the user gets
what she/he deserves. */
#if TLS_TCB_AT_TP
//...
#elif TLS_DTV_AT_TP //★★RISC-Vではこちらのコードが使われる★★
tcb_offset = roundup (TLS_INIT_TCB_SIZE, align ?: 1);
tlsblock = __sbrk (tcb_offset + memsz + max_align
+ TLS_PRE_TCB_SIZE + GLRO(dl_tls_static_surplus));
tlsblock += TLS_PRE_TCB_SIZE;
#else
/* In case a model with a different layout for the TCB and DTV
is defined add another #elif here and in the following #ifs. */
# error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
#endif
//...
/* Initialize the thread pointer. */
#if TLS_TCB_AT_TP
//...
#elif TLS_DTV_AT_TP //★★RISC-Vではこちらのコードが使われる★★
INSTALL_DTV (tlsblock, _dl_static_dtv);
const char *lossage = TLS_INIT_TP (tlsblock); //★★tpレジスタ初期化★★
#else
# error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
#endif
// glibc/sysdeps/riscv/nptl/tls.h
register void *__thread_self asm ("tp");
# define READ_THREAD_POINTER() ({ __thread_self; })
//...
/* Code to initially initialize the thread pointer. */
# define TLS_INIT_TP(tcbp) \
({ __thread_self = (char*)tcbp + TLS_TCB_OFFSET; NULL; })
// glibc/sysdeps/riscv/nptl/tls.h
/* The thread pointer tp points to the end of the TCB.
The pthread_descr structure is immediately in front of the TCB. */
# define TLS_TCB_OFFSET 0
メモリ領域を__sbrk() で確保してアドレスをtpに格納しています。#ifdefで多少見づらいですが、そんなに難しくないはずです。
今まではTLSに含まれるスレッドローカル変数のうち、実行時に値を初期化する変数について見てきました。スレッドローカル変数は実行時に初期化するだけではなく、起動時に初期化される変数もあります。起動時に初期化される変数は、実行ファイルに初期値が含まれていてTLSの初期化時に値がコピーされる仕組みです。
次回はTLSの初期化を見たいと思います。
< | 2022 | > | ||||
<< | < | 04 | > | >> | ||
日 | 月 | 火 | 水 | 木 | 金 | 土 |
- | - | - | - | - | 1 | 2 |
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
合計:
本日:
管理者: Katsuhiro Suzuki(katsuhiro( a t )katsuster.net)
This is Simple Diary 1.0
Copyright(C) Katsuhiro Suzuki 2006-2023.
Powered by PHP 8.2.15.
using GD bundled (2.1.0 compatible)(png support.)