From patchwork Thu Aug 31 17:13:31 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tatsuyuki Ishi X-Patchwork-Id: 137321 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:c792:0:b0:3f2:4152:657d with SMTP id b18csp383137vqu; Thu, 31 Aug 2023 10:14:13 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHcCioHX/CVUl1T+S8D80HaM2vMwOXRrSltddgkdhbD3loQuls/QTlZfEO14wGrjvvbeuXq X-Received: by 2002:a2e:884a:0:b0:2b5:80c9:1266 with SMTP id z10-20020a2e884a000000b002b580c91266mr5550378ljj.43.1693502052780; Thu, 31 Aug 2023 10:14:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1693502052; cv=none; d=google.com; s=arc-20160816; b=kh02nmY8t+iqiZlgjAM5OGyfXhbhh283WXmbQP5O6jISlryALyVyBr+ro05wsq9Sak E5dH7D+uEnbBxZ7tKDkROH0/Cb+gREVbWCXIlFMBM0s3ShcjSIvzc/QgP9OvOl1TmDg2 3Jkqy7iESD6sYefSXdDFscDryjoH87toxfozcrPWOyZ+pxb/wYtjNk67HARx6h4ubIhv opqvPUghp66om6s9xUSmoHVrbj8Op95IkcuqSWzesDR+XhOsfk9WJZ537Nv6keqL86RO ZFHCVOSqR3ml1X1aPo5vHON7788HdKKUjqG5hiOlESCMroIaAeAvYaFdh3fv9kEQ6u5S pBvw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:cc:reply-to:from:list-subscribe:list-help :list-post:list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:dmarc-filter:delivered-to:dkim-signature :dkim-filter; bh=3qNc3tJ4U3dYMsZa+fMAVZUoZLqxbHZOKiVKI/PXlVM=; fh=daQvw8iO4pGm+Xx4NLO3E4+0gdDDq/ixoODeemdNLuQ=; b=sGHrR1ktdgKzOtN2io9bybtRwKFazouYll1neOauDkZvtz8zQw965wqNfpuBy79dyf 7MSEJYqanLx6iGmqAZSFawOGBBNcaj2NuA3BWMQW5ZeaXGXlkCNAF2rwno86BgpP4AKR 5f1uUWbSvi0S4EUg05M22gXLilf9h/MSPJG2xR8qhV4p4SmZRp1ZflMiSg1PI3mcF58D nbBGrvU0TX/pL35E5RD+8PUos6wYqrZOWESouh83mrnQ/4nUwcksvo2xH2qkNvcWY/+z xEJbPGtk+iP/OqTL6LHiFYKMZujmiSX4NNKtCUs6wcDd4Di9mHUO6vAr4hTOY+AkUJiI RRzQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=CWa6NEWt; spf=pass (google.com: domain of binutils-bounces+ouuuleilei=gmail.com@sourceware.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="binutils-bounces+ouuuleilei=gmail.com@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=sourceware.org Received: from server2.sourceware.org (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id rn1-20020a170906d92100b0098e15543465si1150344ejb.280.2023.08.31.10.14.12 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 31 Aug 2023 10:14:12 -0700 (PDT) Received-SPF: pass (google.com: domain of binutils-bounces+ouuuleilei=gmail.com@sourceware.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=CWa6NEWt; spf=pass (google.com: domain of binutils-bounces+ouuuleilei=gmail.com@sourceware.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="binutils-bounces+ouuuleilei=gmail.com@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 9B5CC3858426 for ; Thu, 31 Aug 2023 17:14:10 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 9B5CC3858426 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1693502050; bh=3qNc3tJ4U3dYMsZa+fMAVZUoZLqxbHZOKiVKI/PXlVM=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=CWa6NEWt7zLQC5CK6izpS7yRo/ny9qt88ujaP9joB2MT8iisdztEBSLxhx/FBmKZy tOW+LaRjP5YKOpr9gIkPwe0lKgwMSGRtB8QX34ut4SXou6dWkO/ESJFmNnRyQiVGQc FVbCXjOpz0NrQ+Y31DyBzJ0w+cmc4rwC89VLSMbE= X-Original-To: binutils@sourceware.org Delivered-To: binutils@sourceware.org Received: from mail-pf1-x431.google.com (mail-pf1-x431.google.com [IPv6:2607:f8b0:4864:20::431]) by sourceware.org (Postfix) with ESMTPS id 07E4F385840D for ; Thu, 31 Aug 2023 17:13:54 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 07E4F385840D Received: by mail-pf1-x431.google.com with SMTP id d2e1a72fcca58-68bbd45d0d5so207986b3a.1 for ; Thu, 31 Aug 2023 10:13:53 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1693502032; x=1694106832; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=3qNc3tJ4U3dYMsZa+fMAVZUoZLqxbHZOKiVKI/PXlVM=; b=fXNs63TdPZV2RpGB6e20UgwfLT9iD/K4TeGQFRpjv5wbaw+CdcMFDTXZ0iAr5QXqMB 4a1/uF0hS91uU4gUXI7oUoh3qZYth7HcdzAu55owU086lAinrPYgzXI+9KNc+vLx7B3C 1KdCs4knhyZPx+rOICEJDUGZHHyDs4Vtk0z9Io2XQBRr1T5SWfSr8pkoDS9cvDMaKq/1 /1Uv6PCEUtGiSUds6nUyAfP+V4SdpgFejhsaMlqx4yv6LbghRMRoE9CrHmF7A4guqVrK tiPggu50PBUkS/F+YE8Exzu0+9mLFf4RghS19onzIVZ1gbqOPzNpSNvvuS/A7ViDEZHH byIQ== X-Gm-Message-State: AOJu0YyeDyZZOsKmYLtE1moDfuCLaSWJcKFql9HRtDpV7efQV9BPlbpe VEV3AL391sYMOrRpj/y/C849HtTLAqIvgkkl X-Received: by 2002:a05:6a20:7da7:b0:145:3bd9:1389 with SMTP id v39-20020a056a207da700b001453bd91389mr345794pzj.6.1693502032368; Thu, 31 Aug 2023 10:13:52 -0700 (PDT) Received: from localhost (zz20184013906F627101.userreverse.dion.ne.jp. [111.98.113.1]) by smtp.gmail.com with ESMTPSA id h8-20020a62b408000000b00682d79199e7sm1528428pfn.200.2023.08.31.10.13.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 31 Aug 2023 10:13:51 -0700 (PDT) To: binutils@sourceware.org Subject: [PATCH v2 1/5] RISC-V: Fix local GOT and reloc size calculation for TLS. Date: Fri, 1 Sep 2023 02:13:31 +0900 Message-ID: <20230831171345.49052-2-ishitatsuyuki@gmail.com> X-Mailer: git-send-email 2.42.0 In-Reply-To: <20230831171345.49052-1-ishitatsuyuki@gmail.com> References: <20230817180852.121628-2-ishitatsuyuki@gmail.com> <20230831171345.49052-1-ishitatsuyuki@gmail.com> MIME-Version: 1.0 X-Spam-Status: No, score=-10.6 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: binutils@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Binutils mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Tatsuyuki Ishi via Binutils From: Tatsuyuki Ishi Reply-To: Tatsuyuki Ishi Cc: Tatsuyuki Ishi , rui314@gmail.com, ruiu@bluewhale.systems Errors-To: binutils-bounces+ouuuleilei=gmail.com@sourceware.org Sender: "Binutils" X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1775765608571220163 X-GMAIL-MSGID: 1775765608571220163 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 = s->size; - s->size += RISCV_ELF_WORD_BYTES; - if (*local_tls_type & GOT_TLS_GD) - s->size += RISCV_ELF_WORD_BYTES; - if (bfd_link_pic (info) - || (*local_tls_type & (GOT_TLS_GD | GOT_TLS_IE))) - srel->size += sizeof (ElfNN_External_Rela); + if (*local_tls_type & (GOT_TLS_GD | GOT_TLS_IE)) + { + if (*local_tls_type & GOT_TLS_GD) + { + s->size += 2 * RISCV_ELF_WORD_BYTES; + if (bfd_link_dll (info)) + srel->size += sizeof (ElfNN_External_Rela); + } + if (*local_tls_type & GOT_TLS_IE) + { + s->size += RISCV_ELF_WORD_BYTES; + if (bfd_link_dll (info)) + srel->size += sizeof (ElfNN_External_Rela); + } + } + else + { + s->size += RISCV_ELF_WORD_BYTES; + if (bfd_link_pic (info)) + srel->size += sizeof (ElfNN_External_Rela); + } } else *local_got = (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