From d6db8501213380bfd930e2828da6932ca7fa6b9a Mon Sep 17 00:00:00 2001 From: Rui Ueyama Date: Sun, 8 Jan 2023 11:47:29 +0800 Subject: [PATCH] Refactor --- elf/output-chunks.cc | 11 +++++------ elf/tls.cc | 21 +++++++++++---------- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/elf/output-chunks.cc b/elf/output-chunks.cc index 5061c4628d..62cf345753 100644 --- a/elf/output-chunks.cc +++ b/elf/output-chunks.cc @@ -296,12 +296,11 @@ static std::vector> create_phdr(Context &ctx) { // Create a PT_TLS. for (i64 i = 0; i < ctx.chunks.size(); i++) { - if (!(ctx.chunks[i]->shdr.sh_flags & SHF_TLS)) - continue; - - define(PT_TLS, PF_R, 1, ctx.chunks[i++]); - while (i < ctx.chunks.size() && (ctx.chunks[i]->shdr.sh_flags & SHF_TLS)) - append(ctx.chunks[i++]); + if (ctx.chunks[i]->shdr.sh_flags & SHF_TLS) { + define(PT_TLS, PF_R, 1, ctx.chunks[i++]); + while (i < ctx.chunks.size() && (ctx.chunks[i]->shdr.sh_flags & SHF_TLS)) + append(ctx.chunks[i++]); + } } // Add PT_DYNAMIC diff --git a/elf/tls.cc b/elf/tls.cc index 7a3ebdbe74..c749740c7f 100644 --- a/elf/tls.cc +++ b/elf/tls.cc @@ -33,18 +33,18 @@ // for new threads, and no one write to it at runtime. // // Now, let's think about how to access a TLV. We need to know the TLV's -// address to access it, and that can be done in various ways as follows: +// address to access it which can be done in various ways as follows: // // 1. If we are creating an executable, we know the exact size of the TLS // template image we are creating, and we know where the TP will be // set to after the template is copied to the TLS block. Therefore, -// the TP-relative address of a TLV in the main executable can be -// computed at link-time. That means, computing a TLV's address can be -// as easy as `add %dst, %tp, `. +// the TP-relative address of a TLV in the main executable is known at +// link-time. That means, computing a TLV's address can be as easy as +// `add %dst, %tp, `. // // 2. If we are creating a shared library, we don't excatly know where -// its TLS template image will be copied to relative to other files' -// TLS blocks, because we don't know how large is the main +// its TLS template image will be copied to in terms of the +// TP-relative address, because we don't know how large is the main // executable's and other libraries' TLS template images are. Only the // runtime knows the exact TP-relative address. // @@ -141,7 +141,9 @@ u64 get_tls_begin(Context &ctx) { } // Returns the TP address which can be used for efficient TLV accesses in -// the main executable. +// the main executable. TP at runtime refers to a per-process TLS block +// whose address is not known at link-time. So the address returned from +// this function is the TP if the TLS template image were a TLS block. template u64 get_tp_addr(Context &ctx) { ElfPhdr *phdr = get_tls_segment(ctx); @@ -172,14 +174,13 @@ u64 get_tp_addr(Context &ctx) { // TP. RISC-V load/store instructions usually take 12-bits signed // immediates, so the beginning of TLV ± 2 KiB is accessible with a // single load/store instruction. - if (is_riscv) + if constexpr (is_riscv) return phdr->p_vaddr; unreachable(); } -// Returns the address in the TLS template image when __tls_get_addr would -// be called with offset 0. +// Returns the address when __tls_get_addr would be called with offset 0. template u64 get_dtp_addr(Context &ctx) { ElfPhdr *phdr = get_tls_segment(ctx);