From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-oa1-x30.google.com (mail-oa1-x30.google.com [IPv6:2001:4860:4864:20::30]) by sourceware.org (Postfix) with ESMTPS id C90983858D20 for ; Wed, 21 Feb 2024 07:04:16 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org C90983858D20 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=rivosinc.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=rivosinc.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org C90983858D20 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2001:4860:4864:20::30 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1708499061; cv=none; b=WBDJU10jCxMPjHoT16Vfx3BiWOuPpaCc2ThjBf5aUJjbObz9ETG+nGO4DD2HEfHjtOx2h2hNB1mqDi97kSK7Xyd4MGQp23swLbt5cxfW6YayNZU9NhbVGb2w0vO3x3LQ3HgFthjt4B1iP6jgoOLcwjGdvLfroyHTyCaK/ZbT9Mw= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1708499061; c=relaxed/simple; bh=Kp1vxYx2Af/AMjWNjGjk1xCqtcCpwjVNgXjuKneuGkg=; h=DKIM-Signature:MIME-Version:From:Date:Message-ID:Subject:To; b=fYvDbQo4hfT3U2363Y8J+jO6RfiS/nAR5EpIs/KivmX+D681+N/vFqsrwU8nFFhPwZNTQGSqbU0/0QQ3AFulyKK7DQ9+FgV9MqJfFReWL36+Nn+nulyP+R6f9hMnPeokSEDp9V51A8mcLcS+V3+l/ImdFGWQZjx/mDjPdjQ+svE= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-oa1-x30.google.com with SMTP id 586e51a60fabf-21e5fa2f7efso133644fac.0 for ; Tue, 20 Feb 2024 23:04:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1708499056; x=1709103856; darn=sourceware.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=a1Hnk9UMc0QkY9QD6E6ArSSJYN68Tz4bTkTNMdYoDn4=; b=vW7h55YLmdewnZ/mRJGrU2fT7COjlMaLU3aLK65rvu7YOyNShCYlmxp264QHgEa50j nfj/qVBxoYKY5HWmeV9TUVa478YX5qdPlrdGrz7ezP/B2KnSOj+TZSPP3z4tkFfKQUmH y1DeL/QxZDJJkbMChBb7OaEwxNwc9m6t4XvtzKl0gNjKey/P8zNxhw5LeYjSbuF17EKD 0AQKonOGYiepIVnEJCBpvHgZTt2EFeAjqWnVqPcQMmb3gYaTkiVswzcTu04bxhyCYenS FnNCW0cTlDrV9cqsfUOI/vMqnKjUfxJDRcShP9ziMAjlyhDSBZ076tRhpsnLvtpysoiC N6pA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708499056; x=1709103856; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=a1Hnk9UMc0QkY9QD6E6ArSSJYN68Tz4bTkTNMdYoDn4=; b=FwXEvKFgvooJDSBPdXz1LY3WkS+OSCtDFOQxjQZcDw16bbyG3S3ldXrzmnCtwCuqZQ WF3vENk8NY/FwDrMzFwxe5g5HvY7U9dGgZLk1PJL7b1OqkKLz9cw1oyT99nwivkvxDGq 6Xi4BpeFm77A6Mco+gJyLpjhEmcmVg8dk5YPH2JYB5k953u7NqAd4ffcjf/eheWpoy0w TBKsetC4wVZNCbShAwqNwdmRyWGEm3bXHhhIk3Dc99wdA542FMHmgvOxqxQr5E2mN8r8 Ci+HJwezpHTK06c5Vae54+fr+lOaQFVGLykwz/xk6w8VOGKtcUu92tAvva8kriLlaiNm Ew+g== X-Gm-Message-State: AOJu0YwYHhD/wzI6NGAmcKqVy8DN2xRDpSqVR/d8duOMVg6F7bk/FVsz zwxvfBxONQotbcps6liS6waSPY2a5/v4z4AWLWFB/yZ7PtCmLwcgQZ6T0O6VcX6c9zw5Z8fHCHB ZvGqB63bPePP/+zRvFJQaDWGTDi5CTnCxsIzKkw== X-Google-Smtp-Source: AGHT+IGcyQWJvYdyLsHiE5zC15mri8qNC2pip3/a65uz2+IN5Iyvx5rsMDbFe92zbIVLMD3ErgvyDtCUcaNd92QcdoM= X-Received: by 2002:a05:6870:8999:b0:21f:376e:c725 with SMTP id f25-20020a056870899900b0021f376ec725mr3047399oaq.43.1708499056113; Tue, 20 Feb 2024 23:04:16 -0800 (PST) MIME-Version: 1.0 References: <20230817180852.121628-2-ishitatsuyuki@gmail.com> <20230831171345.49052-1-ishitatsuyuki@gmail.com> <20230831171345.49052-2-ishitatsuyuki@gmail.com> In-Reply-To: From: Nelson Chu Date: Wed, 21 Feb 2024 15:04:05 +0800 Message-ID: Subject: Re: [PATCH v2 1/5] RISC-V: Fix local GOT and reloc size calculation for TLS. To: Tatsuyuki Ishi Cc: binutils@sourceware.org, rui314@gmail.com, ruiu@bluewhale.systems, i@maskray.me Content-Type: multipart/alternative; boundary="00000000000056cb840611deef3e" X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,GIT_PATCH_0,HTML_MESSAGE,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: --00000000000056cb840611deef3e Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Committed this one first after passing the gcc/binutils regressions of riscv-gnu-toolchain. Nelson On Wed, Feb 21, 2024 at 8:49=E2=80=AFAM Nelson Chu wr= ote: > Oops I lost this one. This seems to fix the redundant dynamic > R_RISCV_NONE in .rela.dyn if using --no-pie for tls gd and ie, so looks > good to me. > > Btw, I just noticed that I also reviewed four of the TLS patches since my > mail box looks weird and only shows the old patches, so I lost this one a= nd > others... I will see the remaining stuff. > > Thanks > Nelson > > On Fri, Sep 1, 2023 at 1:13=E2=80=AFAM Tatsuyuki Ishi > wrote: > >> The previous code did not account correctly for two cases: >> * A TLS symbol can be referenced with multiple TLS types (although rare), >> in which case it only allocated the maximum slot size among the types, >> instead of the sum. >> * TLS relocations are only needed for DLLs, unlike normal symbols which >> requires relocations for all PIE code. >> >> Modify the logic to account for the two cases. >> >> bfd/ >> * elfnn-riscv.c (riscv_elf_size_dynamic_sections): Handle relocation >> sizing for TLS and non-TLS symbols differently, with the former >> requiring relocs on DLL while the latter requiring on PIE. >> Allocate GOT slots and relocation slots for each TLS type separately, >> accounting for the possibility of a TLS variable getting referenced = by >> multiple symbols. >> ld/ >> * testsuite/ld-riscv-elf/ld-riscv-elf.exp: Updated. >> * testsuite/ld-riscv-elf/tls*: New testcase for TLS GD and IE, with >> symbols referred by both types and global and local symbols. >> --- >> v2: Add tests for GD and IE, testing both global and local symbols. >> Both -shared and -no-pie are tested. >> >> bfd/elfnn-riscv.c | 27 ++++++++++++++++----- >> ld/testsuite/ld-riscv-elf/ld-riscv-elf.exp | 8 +++++++ >> ld/testsuite/ld-riscv-elf/tls.d | 15 ++++++++++++ >> ld/testsuite/ld-riscv-elf/tls.s | 28 ++++++++++++++++++++++ >> ld/testsuite/ld-riscv-elf/tlsbin.d | 7 ++++++ >> ld/testsuite/ld-riscv-elf/tlslib.s | 6 +++++ >> 6 files changed, 85 insertions(+), 6 deletions(-) >> create mode 100644 ld/testsuite/ld-riscv-elf/tls.d >> create mode 100644 ld/testsuite/ld-riscv-elf/tls.s >> create mode 100644 ld/testsuite/ld-riscv-elf/tlsbin.d >> create mode 100644 ld/testsuite/ld-riscv-elf/tlslib.s >> >> diff --git a/bfd/elfnn-riscv.c b/bfd/elfnn-riscv.c >> index 09aa7be225e..02b46835cc7 100644 >> --- a/bfd/elfnn-riscv.c >> +++ b/bfd/elfnn-riscv.c >> @@ -1562,12 +1562,27 @@ riscv_elf_size_dynamic_sections (bfd *output_bfd, >> struct bfd_link_info *info) >> if (*local_got > 0) >> { >> *local_got =3D s->size; >> - s->size +=3D RISCV_ELF_WORD_BYTES; >> - if (*local_tls_type & GOT_TLS_GD) >> - s->size +=3D RISCV_ELF_WORD_BYTES; >> - if (bfd_link_pic (info) >> - || (*local_tls_type & (GOT_TLS_GD | GOT_TLS_IE))) >> - srel->size +=3D sizeof (ElfNN_External_Rela); >> + if (*local_tls_type & (GOT_TLS_GD | GOT_TLS_IE)) >> + { >> + if (*local_tls_type & GOT_TLS_GD) >> + { >> + s->size +=3D 2 * RISCV_ELF_WORD_BYTES; >> + if (bfd_link_dll (info)) >> + srel->size +=3D sizeof (ElfNN_External_Rela); >> + } >> + if (*local_tls_type & GOT_TLS_IE) >> + { >> + s->size +=3D RISCV_ELF_WORD_BYTES; >> + if (bfd_link_dll (info)) >> + srel->size +=3D sizeof (ElfNN_External_Rela); >> + } >> + } >> + else >> + { >> + s->size +=3D RISCV_ELF_WORD_BYTES; >> + if (bfd_link_pic (info)) >> + srel->size +=3D sizeof (ElfNN_External_Rela); >> + } >> } >> else >> *local_got =3D (bfd_vma) -1; >> diff --git a/ld/testsuite/ld-riscv-elf/ld-riscv-elf.exp >> b/ld/testsuite/ld-riscv-elf/ld-riscv-elf.exp >> index 947a266ba72..adb4ee75e4a 100644 >> --- a/ld/testsuite/ld-riscv-elf/ld-riscv-elf.exp >> +++ b/ld/testsuite/ld-riscv-elf/ld-riscv-elf.exp >> @@ -317,4 +317,12 @@ if [istarget "riscv*-*-*"] { >> run_dump_test "pcrel-reloc-rel-pie" >> run_dump_test "pcrel-reloc-abs-nopie" >> run_dump_test "pcrel-reloc-abs-pie" >> + >> + run_ld_link_tests { >> + { "Build shared library for TLS runtime" >> + "-shared" "" "" {tlslib.s} >> + {} "tlslib.so" } >> + } >> + run_dump_test "tls" >> + run_dump_test "tlsbin" >> } >> diff --git a/ld/testsuite/ld-riscv-elf/tls.d >> b/ld/testsuite/ld-riscv-elf/tls.d >> new file mode 100644 >> index 00000000000..0e2ab5683ad >> --- /dev/null >> +++ b/ld/testsuite/ld-riscv-elf/tls.d >> @@ -0,0 +1,15 @@ >> +#source: tls.s >> +#ld: --shared tmpdir/tlslib.so >> +#readelf: -Wr >> + >> +Relocation section '.rela.dyn' at offset 0x[0-9a-f]+ contains 5 entries: >> + +Offset +Info +Type +Symbol's Value +Symbol's Name \+ Addend >> +[0-9a-f]+ +[0-9a-f]+ R_RISCV_TLS_DTPMOD64 +0 >> +[0-9a-f]+ +[0-9a-f]+ R_RISCV_TLS_TPREL64 +4 >> +[0-9a-f]+ +[0-9a-f]+ R_RISCV_TLS_DTPMOD64 +0+ sg1 \+ 0 >> +[0-9a-f]+ +[0-9a-f]+ R_RISCV_TLS_DTPREL64 +0+ sg1 \+ 0 >> +[0-9a-f]+ +[0-9a-f]+ R_RISCV_TLS_TPREL64 +0+ sg1 \+ 0 >> + >> +Relocation section '.rela.plt' at offset 0x[0-9a-f]+ contains 1 entry: >> + +Offset +Info +Type +Symbol's Value +Symbol's Name \+ Addend >> +[0-9a-f]+ +[0-9a-f]+ R_RISCV_JUMP_SLOT +0+ __tls_get_addr \+ 0 >> \ No newline at end of file >> diff --git a/ld/testsuite/ld-riscv-elf/tls.s >> b/ld/testsuite/ld-riscv-elf/tls.s >> new file mode 100644 >> index 00000000000..79e9bc20374 >> --- /dev/null >> +++ b/ld/testsuite/ld-riscv-elf/tls.s >> @@ -0,0 +1,28 @@ >> + .section .tbss,"awT",@nobits >> + .global sg1 >> +sg1: >> + .zero 4 >> +sl1: >> + .zero 4 >> + >> + .text >> + .globl _start >> + .type _start,@function >> +_start: >> + /* GD, global var */ >> + la.tls.gd a0,sg1 >> + call __tls_get_addr >> + >> + /* IE, global var */ >> + la.tls.ie a0,sg1 >> + add a0,a0,tp >> + >> + /* GD, local var */ >> + la.tls.gd a0,sl1 >> + call __tls_get_addr >> + >> + /* IE, local var */ >> + la.tls.ie a0,sl1 >> + add a0,a0,tp >> + >> + ret >> diff --git a/ld/testsuite/ld-riscv-elf/tlsbin.d >> b/ld/testsuite/ld-riscv-elf/tlsbin.d >> new file mode 100644 >> index 00000000000..12a4d0ea703 >> --- /dev/null >> +++ b/ld/testsuite/ld-riscv-elf/tlsbin.d >> @@ -0,0 +1,7 @@ >> +#source: tls.s >> +#ld: -no-pie tmpdir/tlslib.so >> +#readelf: -Wr >> + >> +Relocation section '.rela.plt' at offset 0x[0-9a-f]+ contains 1 entry: >> + +Offset +Info +Type +Symbol's Value +Symbol's Name \+ Addend >> +[0-9a-f]+ +[0-9a-f]+ R_RISCV_JUMP_SLOT +[0-9a-f]+ __tls_get_addr \+ 0 >> \ No newline at end of file >> diff --git a/ld/testsuite/ld-riscv-elf/tlslib.s >> b/ld/testsuite/ld-riscv-elf/tlslib.s >> new file mode 100644 >> index 00000000000..17c770786d0 >> --- /dev/null >> +++ b/ld/testsuite/ld-riscv-elf/tlslib.s >> @@ -0,0 +1,6 @@ >> + .text >> + /* Dummy. */ >> + .globl __tls_get_addr >> + .type __tls_get_addr,@function >> +__tls_get_addr: >> + ret >> -- >> 2.42.0 >> >> --00000000000056cb840611deef3e--