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: 203727 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:693c:2685:b0:108:e6aa:91d0 with SMTP id mn5csp571580dyc; Tue, 20 Feb 2024 10:02:02 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCWy8tAHpI1X60q+Uq3x+CnXu3t+KuC9+IyIJDm5cCtTK0vLNcYbcSNlRWmggyQ1IKxEu33VAOoEOuIlrDqYyZ7LKK+Gvg== X-Google-Smtp-Source: AGHT+IEK18QD6NWxWElCLdt2P8JoPl7X5KyjknQTv2LG+0P1wPYy79ZIXTPDyzL5VH3G7WSxDQL7 X-Received: by 2002:a05:622a:1012:b0:42e:1765:9cfa with SMTP id d18-20020a05622a101200b0042e17659cfamr5924084qte.53.1708452101543; Tue, 20 Feb 2024 10:01:41 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708452101; cv=pass; d=google.com; s=arc-20160816; b=m0LB+ejeiR0bkumpUopCxvsrNIdSxdEQlG6hxPvS6Q4rMLPZTymWth3g52oOXCHC2F rYBTur5TvokTL6gJcp7amvsab/rWDbcMIKz0LFU1lAcJld4KuZYrryWWsEiR0IuuqibV UdiaDmJlD5p37iGDT7mqk3y/cvCJ7RmsWBP1ATkH2Q8nxsp1MzeGFOePgXyeq2D4Ld2U 4d38hHil1OMYl4chF6o2FpYCnZcJG/GaDvrSLsIM+8guAq1y7gWznifktsDPS6iViYII GFWbu1MUskLGFdj0Ix5zxuCoYLVhnrJz9wnnZHmJH0oRA063MYjnc8zC4OzQnzbpeL+n WPEg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to: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:cc:to :from:dkim-signature:arc-filter:dmarc-filter:delivered-to; bh=UiRSxTi8TM/Tl+/3x8cehvIuX14pv48E4y/UdtEFzb8=; fh=YSa8tbAsFM03BU4mjhbbHsdQe1vIF6KegOb6Q0/FrUw=; b=cQHvFRpX3G09okQ9ptCHD8CBWCIle79sO0KcZXo9LWVWZlwltZJosM77ToenA1NcVh rywdC/DUYuQIkNh22sr1wruHAAgsuPIjmtvl25qb/c1Nx9uc5+P1Lfg9ptkEKEOD7Tvq PErVYUCbAG+nRCiLx0XTrAPLuG4eAVrFbzF9ESMGCTUtzfUw31Vu+wR9JBvalAjWMc2y yYnkaFLgFi8aX9vXM3EaixeGyZUP6ZCDAsleVAOkTtQ4N+ZR9wFew6uGPiCKbtQuISyR ZsXqGZnx86hGYuRQlpRT85HFBvI8ubwErhXkkcS/VD9HSeVd5Wu9lUI5RVVh676U6dkL 150w==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=Px7V+8d8; arc=pass (i=1); 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=QUARANTINE dis=NONE) header.from=gmail.com Received: from server2.sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id l11-20020a05622a050b00b0042c2fcc55ebsi9591818qtx.24.2024.02.20.10.01.41 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 Feb 2024 10:01:41 -0800 (PST) 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=@gmail.com header.s=20230601 header.b=Px7V+8d8; arc=pass (i=1); 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=QUARANTINE dis=NONE) header.from=gmail.com Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 00DBD384CBA1 for ; Tue, 20 Feb 2024 18:00:42 +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-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+ouuuleilei=gmail.com@sourceware.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1775765608571220163 X-GMAIL-MSGID: 1791441870577797732 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