Skip to content

Commit 1ca7b17

Browse files
committed
Refactor
1 parent ebf732f commit 1ca7b17

File tree

2 files changed

+18
-18
lines changed

2 files changed

+18
-18
lines changed

elf/output-chunks.cc

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -296,12 +296,11 @@ static std::vector<ElfPhdr<E>> create_phdr(Context<E> &ctx) {
296296

297297
// Create a PT_TLS.
298298
for (i64 i = 0; i < ctx.chunks.size(); i++) {
299-
if (!(ctx.chunks[i]->shdr.sh_flags & SHF_TLS))
300-
continue;
301-
302-
define(PT_TLS, PF_R, 1, ctx.chunks[i++]);
303-
while (i < ctx.chunks.size() && (ctx.chunks[i]->shdr.sh_flags & SHF_TLS))
304-
append(ctx.chunks[i++]);
299+
if (ctx.chunks[i]->shdr.sh_flags & SHF_TLS) {
300+
define(PT_TLS, PF_R, 1, ctx.chunks[i++]);
301+
while (i < ctx.chunks.size() && (ctx.chunks[i]->shdr.sh_flags & SHF_TLS))
302+
append(ctx.chunks[i++]);
303+
}
305304
}
306305

307306
// Add PT_DYNAMIC

elf/tls.cc

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,20 @@
3333
// for new threads, and no one write to it at runtime.
3434
//
3535
// Now, let's think about how to access a TLV. We need to know the TLV's
36-
// address to access it, and that can be done in various ways as follows:
36+
// address to access it which can be done in various ways as follows:
3737
//
3838
// 1. If we are creating an executable, we know the exact size of the TLS
3939
// template image we are creating, and we know where the TP will be
4040
// set to after the template is copied to the TLS block. Therefore,
41-
// the TP-relative address of a TLV in the main executable can be
42-
// computed at link-time. That means, computing a TLV's address can be
43-
// as easy as `add %dst, %tp, <link-time constant>`.
41+
// the TP-relative address of a TLV in the main executable is known at
42+
// link-time. That means, computing a TLV's address can be as easy as
43+
// `add %dst, %tp, <link-time constant>`.
4444
//
4545
// 2. If we are creating a shared library, we don't excatly know where
46-
// its TLS template image will be copied to relative to other files'
47-
// TLS blocks, because we don't know how large is the main
48-
// executable's and other libraries' TLS template images are. Only the
49-
// runtime knows the exact TP-relative address.
46+
// its TLS template image will be copied to relative TP, because we
47+
// don't know how large is the main executable's and other libraries'
48+
// TLS template images are. Only the runtime knows the exact
49+
// TP-relative address.
5050
//
5151
// We can solve the problem with an indirection. Specifically, for
5252
// each TLV whose TP-relative address is only known at process startup
@@ -141,7 +141,9 @@ u64 get_tls_begin(Context<E> &ctx) {
141141
}
142142

143143
// Returns the TP address which can be used for efficient TLV accesses in
144-
// the main executable.
144+
// the main executable. TP at runtime refers to a per-process TLS block
145+
// whose address is not known at link-time. So the address returned from
146+
// this function is the TP if the TLS template image were a TLS block.
145147
template <typename E>
146148
u64 get_tp_addr(Context<E> &ctx) {
147149
ElfPhdr<E> *phdr = get_tls_segment(ctx);
@@ -172,14 +174,13 @@ u64 get_tp_addr(Context<E> &ctx) {
172174
// TP. RISC-V load/store instructions usually take 12-bits signed
173175
// immediates, so the beginning of TLV ± 2 KiB is accessible with a
174176
// single load/store instruction.
175-
if (is_riscv<E>)
177+
if constexpr (is_riscv<E>)
176178
return phdr->p_vaddr;
177179

178180
unreachable();
179181
}
180182

181-
// Returns the address in the TLS template image when __tls_get_addr would
182-
// be called with offset 0.
183+
// Returns the address when __tls_get_addr would be called with offset 0.
183184
template <typename E>
184185
u64 get_dtp_addr(Context<E> &ctx) {
185186
ElfPhdr<E> *phdr = get_tls_segment(ctx);

0 commit comments

Comments
 (0)