From patchwork Tue Feb 20 17:55:48 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tatsuyuki Ishi X-Patchwork-Id: 86063 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id BC562386EC6E for ; Tue, 20 Feb 2024 18:01:02 +0000 (GMT) X-Original-To: binutils@sourceware.org Delivered-To: binutils@sourceware.org Received: from mail-io1-xd2e.google.com (mail-io1-xd2e.google.com [IPv6:2607:f8b0:4864:20::d2e]) by sourceware.org (Postfix) with ESMTPS id C9D853858C74 for ; Tue, 20 Feb 2024 17:56:23 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org C9D853858C74 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org C9D853858C74 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::d2e ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1708451787; cv=none; b=pBfiJXEwMJOvM5ucbCbTZKmv2WwaqmF1HrVZA6pcsVzaMYFpbJWWzkrqb1lz9Rj47CI/B3cJNH3g1br9VUBmNrRuObibODqqIEFJflRiLqLxKKe8Y0MGj1doZJFt0nLHRyVXxh6nAfejU1pbycT6NDCVHeeIOpoI74Pig9BzbUs= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1708451787; c=relaxed/simple; bh=OYIspJG2Pjccv/pC9DMXo/catUXJ4lcI9FExlkmi8W0=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=Dl+Zjun7mUA8MgZcanoezyz7SmyZzS8ShlEdMWru7TfQIshMPR0+sytieeKaK/HFALiwKuAOh+2gHPjORkv3m8012y8mDejRp0Muw4YiovswioiKsWkygXe6+cPaQHDGufPXhl08b9gURvzz0z5J141NJucpT7z9U+gGhdeWKRk= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-io1-xd2e.google.com with SMTP id ca18e2360f4ac-7bf3283c18dso88456839f.0 for ; Tue, 20 Feb 2024 09:56:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1708451782; x=1709056582; darn=sourceware.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=UiRSxTi8TM/Tl+/3x8cehvIuX14pv48E4y/UdtEFzb8=; b=Px7V+8d8xDgcxhwcqdvOHdzuIFukFfLhShXStBzNWINAUivgEnQZN1A+QUAyP7J8NV Q4Z9uZ35qi1+VKYx7cbn6cwMOrlH6+xnfMcvtdRu6zq71Op3hh//t5d+j96W9P36G8fS h05K4rlJYU1l4N7Cq+jXjEl6ah7l8YFACqlTmiqWqxAiEdMOpAWpDaAJO+6oqTVVNVmg Us6cFxk3G6/DxxKlFwQtrND8dop4VwBiaMsAMI7LpBBtB9gxbFk8hKrmEq0GIl8D192y 9nMyims8LUTrVx1X9lKibJ6l1YdA13D3vBXWr8crGwFG+QXUGcbKdMMKTOft4xVCIBJW BmfQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708451782; x=1709056582; 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=UiRSxTi8TM/Tl+/3x8cehvIuX14pv48E4y/UdtEFzb8=; b=EPCyNmtQ/dIxpudBCCqFMHHACGJF57jKO4qzuJ4BJ6gJduCZN8eNcgVZ0QcRbGvp+A Su2Z/DqTsHb9yTfszH9odWrcQPENLV7K94G76af27vKXePqimxmydo05BOIeRS9VKISX uqHQykKtHq+9+zStRC9T1I459Pag3moc5dNwIFjo2yAXweO5zEGKDCYGsF64aPGAw6++ /yEwOKYd4SoTfmS2D+LvHjrnJjxS+zXBaJlUqB9LLPGjeLxQuFzoGoBvx7WcD9qYOv/K CU9BEy+hcGy3aQegEu7q2olDmed9J+gC5RLjiEKXY9oi4FRGRsQka6PxvRgCQ35vn9pZ KSGQ== X-Gm-Message-State: AOJu0Yyfuhvi1ezjM0x2pMn0NpTQDwD2sWlN+Iiyj/Wcr7gjxg9+oSE3 /FOTKpNNAFVd32Wf4PikQLe5wQjmaXQ90uXw9ORbPYWdVTWA+Vj2I1xXQDVRsgKLcA== X-Google-Smtp-Source: AGHT+IEG4Kmpt+nAsqr8hngLUde4TZ9wS2LxueJRNNsYZ5h8N94W7YFXqk7DLCoXZ0vOVkA4dQwuGw== X-Received: by 2002:a05:6e02:218c:b0:363:c63a:7960 with SMTP id j12-20020a056e02218c00b00363c63a7960mr11548580ila.3.1708451781829; Tue, 20 Feb 2024 09:56:21 -0800 (PST) Received: from localhost (zz20184013906F627101.userreverse.dion.ne.jp. [111.98.113.1]) by smtp.gmail.com with ESMTPSA id v18-20020a63f852000000b005dc87643cc3sm6872230pgj.27.2024.02.20.09.56.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 Feb 2024 09:56:21 -0800 (PST) From: Tatsuyuki Ishi To: binutils@sourceware.org Cc: i@maskray.me, nelson@rivosinc.com, rui314@gmail.com, ruiu@bluewhale.systems, Tatsuyuki Ishi Subject: [PATCH v4 1/9] RISC-V: Fix local GOT and reloc size calculation for TLS. Date: Wed, 21 Feb 2024 02:55:48 +0900 Message-ID: <20240220175556.304692-2-ishitatsuyuki@gmail.com> X-Mailer: git-send-email 2.43.2 In-Reply-To: <20240220175556.304692-1-ishitatsuyuki@gmail.com> References: <20230817180852.121628-2-ishitatsuyuki@gmail.com> <20240220175556.304692-1-ishitatsuyuki@gmail.com> MIME-Version: 1.0 X-Spam-Status: No, score=-11.2 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, 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 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: , Errors-To: binutils-bounces+patchwork=sourceware.org@sourceware.org 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. --- 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 951b19a911d..e5cecd79cf3 100644 --- a/bfd/elfnn-riscv.c +++ b/bfd/elfnn-riscv.c @@ -1559,12 +1559,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 7e1281d826b..a1dd0e5e37e 100644 --- a/ld/testsuite/ld-riscv-elf/ld-riscv-elf.exp +++ b/ld/testsuite/ld-riscv-elf/ld-riscv-elf.exp @@ -319,4 +319,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..e7f20309bf1 --- /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 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..cdcd51a9199 --- /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 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