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 From patchwork Thu Aug 31 17:13:32 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tatsuyuki Ishi X-Patchwork-Id: 137322 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:c792:0:b0:3f2:4152:657d with SMTP id b18csp383444vqu; Thu, 31 Aug 2023 10:14:44 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFGcyaw8oJ9M3myOlRUfSOCTEJIKB/C8+rJTbSK1+FB3CCTc/d2z8zB+4+eU0Vf3z0Wtm/5 X-Received: by 2002:a17:906:538c:b0:9a1:ca55:d0cb with SMTP id g12-20020a170906538c00b009a1ca55d0cbmr466971ejo.23.1693502084147; Thu, 31 Aug 2023 10:14:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1693502084; cv=none; d=google.com; s=arc-20160816; b=PGBvhJ/r0NYvT2pLyKCyg6/U2TV98x3qFzu7Mnm13TkdVj/YtCvjh3nwLkeYYTXQzf nn2Ms86sguuQXAlY7fMz/qX+6bM0bjo9hJnw/9/QN/+6v/ybfXkli2ff+fwvzEDSGb8M 3bqcOK21ZzDFC+Obj82rQk51NqWh4ghHcCyGQ+W1wbtHQeE8jrv+L+NXQAhxXc7B+si4 8aAHrI4OsBQ8aXRderQbu/tELzNo6Cv8ENAZo8LhO6mc9YlT9dMV4bdJSJOjjQ+dztRc axSlzbzy9cDx3kuJ/2fC06nVWQEUFTpCb6lHdbZY8+lOkhk7S9uuiJxmIENcrEq3yUAc b7Dw== 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=e6Fdik27kWympO6OppIOOukvQGljX0wsKR4PpdYu3wc=; fh=daQvw8iO4pGm+Xx4NLO3E4+0gdDDq/ixoODeemdNLuQ=; b=A0Ze1DJ9bPlLU9NZXo9fzcOmchRu2ng4Ui6wjpnnUqtP1c42CrDDUuCbSaN8vADm3h E6nQKtrP0LzIEk44dr1zhzcQ9fhbJbjfkxLJdIGPLVFd3rs5J7/LMVDmViDzEkEDvSbp 391tTveakA7D7o4OA3bx+wlmb4IsXH42As4dRQSaOQiSxHHc1A5jZEssjZOuim7mCcpf Xx+CgmxJRrdflzb41J7/ov5MTcX5O52qzYNppv7Cf0aupYga/nFg3NWQPVpICYLprVXb pUptGXJRi/v3QfLzS/c02mS3MnVZWjHxoAIYJM3LAF0IT7Bp5oO/+JGsVVgcp2PF8Ck/ rJiw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b="l/Mmcm0w"; 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 ks8-20020a170906f84800b0099bcf1c07cbsi1217948ejb.883.2023.08.31.10.14.43 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 31 Aug 2023 10:14:44 -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="l/Mmcm0w"; 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 C270C3858425 for ; Thu, 31 Aug 2023 17:14:34 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C270C3858425 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1693502074; bh=e6Fdik27kWympO6OppIOOukvQGljX0wsKR4PpdYu3wc=; 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=l/Mmcm0w8ov63nDo1/HaVYudzkAjcAp86WFP66a/Ic79bTKMDNQOgP7jDaWQUGFqO ainwdmCqMsrQxg3GSKsmeWvFTKLlVX+0BJ2y/5FLJHmH3JChy8i4wJsnHQ0376KWvy F1QcZJx5tAU8wWKkWsyfYR486ErQN0T2XyNtgtgc= X-Original-To: binutils@sourceware.org Delivered-To: binutils@sourceware.org Received: from mail-pl1-x632.google.com (mail-pl1-x632.google.com [IPv6:2607:f8b0:4864:20::632]) by sourceware.org (Postfix) with ESMTPS id 99BB73858414 for ; Thu, 31 Aug 2023 17:13:56 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 99BB73858414 Received: by mail-pl1-x632.google.com with SMTP id d9443c01a7336-1c0efe0c4acso1222125ad.0 for ; Thu, 31 Aug 2023 10:13:56 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1693502035; x=1694106835; 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=e6Fdik27kWympO6OppIOOukvQGljX0wsKR4PpdYu3wc=; b=XGQz8jsTt20R9aEiRJpMXHTPWaQm8E4rvWscLN09naGQUJ+S7Mn/kEGUUqZJYfnbC4 3sAI466wG0K6o1ncMd68jh7nq0UjgupL2kx48NxIin8MPQbieldXxcGNgdHd/j3XyGKH QqvpUys/IrEGuGqbf+9S0TI2FH7lvawj/8+xPgqiDtyBIUc6RzbA6akKzxyl+f++cjaB Mfu5tD1oN7mwyXBkwHsl0neJbokr4lbY+lierZXvO52yrnXobl1yr7TfUHWGRgfGjxa/ aJac/coZLdZIF3uzgDKK40NTqwDY0X4JAqGgoLk9WHGiJSq9/xp2SqwCo4BwnFV+JJsD AX/Q== X-Gm-Message-State: AOJu0Yzg4djdZ/XyP/7mBhTuvX7XAQtGqL6UINJtkT6cyW1SVx8Cu7dH 0HKnrB8vbQM3nXsVSsrC0BTiuWb4E8d+ypG9 X-Received: by 2002:a17:902:ea03:b0:1c1:fbec:bc3f with SMTP id s3-20020a170902ea0300b001c1fbecbc3fmr354179plg.5.1693502035043; Thu, 31 Aug 2023 10:13:55 -0700 (PDT) Received: from localhost (zz20184013906F627101.userreverse.dion.ne.jp. [111.98.113.1]) by smtp.gmail.com with ESMTPSA id a10-20020a170902ee8a00b001bba3650448sm1483216pld.258.2023.08.31.10.13.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 31 Aug 2023 10:13:54 -0700 (PDT) To: binutils@sourceware.org Subject: [PATCH v2 2/5] RISC-V: Add TLSDESC reloc definitions. Date: Fri, 1 Sep 2023 02:13:32 +0900 Message-ID: <20230831171345.49052-3-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.7 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: 1774500735283371203 X-GMAIL-MSGID: 1775765641399016709 bfd/ * elfxx-riscv.c: Add 5 TLSDESC reloc descriptions. * reloc.c: Likewise. * libbfd.h: Regenerate. * bfd-in2.h: Regenerate. include/ * elf/riscv.h: Add 5 TLSDESC reloc descriptions. --- v2: Properly regenerate the headers instead of updating them by hand. bfd/bfd-in2.h | 4 +++ bfd/elfxx-riscv.c | 75 ++++++++++++++++++++++++++++++++++++++++++++- bfd/libbfd.h | 4 +++ bfd/reloc.c | 8 +++++ include/elf/riscv.h | 5 +++ 5 files changed, 95 insertions(+), 1 deletion(-) diff --git a/bfd/bfd-in2.h b/bfd/bfd-in2.h index e2deb107494..ce324383dee 100644 --- a/bfd/bfd-in2.h +++ b/bfd/bfd-in2.h @@ -5411,6 +5411,10 @@ number for the SBIC, SBIS, SBI and CBI instructions */ BFD_RELOC_RISCV_TLS_DTPREL64, BFD_RELOC_RISCV_TLS_TPREL32, BFD_RELOC_RISCV_TLS_TPREL64, + BFD_RELOC_RISCV_TLSDESC_HI20, + BFD_RELOC_RISCV_TLSDESC_LOAD_LO12, + BFD_RELOC_RISCV_TLSDESC_ADD_LO12, + BFD_RELOC_RISCV_TLSDESC_CALL, BFD_RELOC_RISCV_ALIGN, BFD_RELOC_RISCV_RVC_BRANCH, BFD_RELOC_RISCV_RVC_JUMP, diff --git a/bfd/elfxx-riscv.c b/bfd/elfxx-riscv.c index e9852ef8fa1..43931a2be5d 100644 --- a/bfd/elfxx-riscv.c +++ b/bfd/elfxx-riscv.c @@ -218,8 +218,21 @@ static reloc_howto_type howto_table[] = MINUS_ONE, /* dst_mask */ false), /* pcrel_offset */ + HOWTO (R_RISCV_TLSDESC, /* type */ + 0, /* rightshift */ + 0, /* size is handled by dynamic linker */ + 0, /* bitsize */ + false, /* pc_relative */ + 0, /* bitpos */ + complain_overflow_dont, /* complain_on_overflow */ + bfd_elf_generic_reloc, /* special_function */ + "R_RISCV_TLSDESC", /* name */ + false, /* partial_inplace */ + 0, /* src_mask */ + MINUS_ONE, /* dst_mask */ + false), /* pcrel_offset */ + /* Reserved for future relocs that the dynamic linker must understand. */ - EMPTY_HOWTO (12), EMPTY_HOWTO (13), EMPTY_HOWTO (14), EMPTY_HOWTO (15), @@ -879,6 +892,62 @@ static reloc_howto_type howto_table[] = 0, /* src_mask */ 0, /* dst_mask */ false), /* pcrel_offset */ + + HOWTO (R_RISCV_TLSDESC_HI20, /* type */ + 0, /* rightshift */ + 4, /* size */ + 32, /* bitsize */ + true, /* pc_relative */ + 0, /* bitpos */ + complain_overflow_signed, /* complain_on_overflow */ + bfd_elf_generic_reloc, /* special_function */ + "R_RISCV_TLSDESC_HI20", /* name */ + true, /* partial_inplace */ + 0, /* src_mask */ + ENCODE_UTYPE_IMM (-1U), /* dst_mask */ + false), /* pcrel_offset */ + + HOWTO (R_RISCV_TLSDESC_LOAD_LO12, /* type */ + 0, /* rightshift */ + 4, /* size */ + 32, /* bitsize */ + false, /* pc_relative */ + 0, /* bitpos */ + complain_overflow_signed, /* complain_on_overflow */ + bfd_elf_generic_reloc, /* special_function */ + "R_RISCV_TLSDESC_LOAD_LO12", /* name */ + false, /* partial_inplace */ + 0, /* src_mask */ + ENCODE_ITYPE_IMM (-1U), /* dst_mask */ + false), /* pcrel_offset */ + + HOWTO (R_RISCV_TLSDESC_ADD_LO12, /* type */ + 0, /* rightshift */ + 4, /* size */ + 32, /* bitsize */ + false, /* pc_relative */ + 0, /* bitpos */ + complain_overflow_signed, /* complain_on_overflow */ + bfd_elf_generic_reloc, /* special_function */ + "R_RISCV_TLSDESC_ADD_LO12", /* name */ + false, /* partial_inplace */ + 0, /* src_mask */ + ENCODE_ITYPE_IMM (-1U), /* dst_mask */ + false), /* pcrel_offset */ + + HOWTO (R_RISCV_TLSDESC_CALL, /* type */ + 0, /* rightshift */ + 0, /* size */ + 0, /* bitsize */ + false, /* pc_relative */ + 0, /* bitpos */ + complain_overflow_dont, /* complain_on_overflow */ + bfd_elf_generic_reloc, /* special_function */ + "R_RISCV_TLSDESC_CALL", /* name */ + false, /* partial_inplace */ + 0, /* src_mask */ + ENCODE_ITYPE_IMM (-1U), /* dst_mask */ + false) /* pcrel_offset */ }; /* A mapping from BFD reloc types to RISC-V ELF reloc types. */ @@ -925,6 +994,10 @@ static const struct elf_reloc_map riscv_reloc_map[] = { BFD_RELOC_RISCV_TPREL_LO12_I, R_RISCV_TPREL_LO12_I }, { BFD_RELOC_RISCV_TLS_GOT_HI20, R_RISCV_TLS_GOT_HI20 }, { BFD_RELOC_RISCV_TLS_GD_HI20, R_RISCV_TLS_GD_HI20 }, + { BFD_RELOC_RISCV_TLSDESC_HI20, R_RISCV_TLSDESC_HI20 }, + { BFD_RELOC_RISCV_TLSDESC_LOAD_LO12, R_RISCV_TLSDESC_LOAD_LO12 }, + { BFD_RELOC_RISCV_TLSDESC_ADD_LO12, R_RISCV_TLSDESC_ADD_LO12 }, + { BFD_RELOC_RISCV_TLSDESC_CALL, R_RISCV_TLSDESC_CALL }, { BFD_RELOC_RISCV_ALIGN, R_RISCV_ALIGN }, { BFD_RELOC_RISCV_RVC_BRANCH, R_RISCV_RVC_BRANCH }, { BFD_RELOC_RISCV_RVC_JUMP, R_RISCV_RVC_JUMP }, diff --git a/bfd/libbfd.h b/bfd/libbfd.h index a84febd8257..62e0d43d2ab 100644 --- a/bfd/libbfd.h +++ b/bfd/libbfd.h @@ -2408,6 +2408,10 @@ static const char *const bfd_reloc_code_real_names[] = { "@@uninitialized@@", "BFD_RELOC_RISCV_TLS_DTPREL64", "BFD_RELOC_RISCV_TLS_TPREL32", "BFD_RELOC_RISCV_TLS_TPREL64", + "BFD_RELOC_RISCV_TLSDESC_HI20", + "BFD_RELOC_RISCV_TLSDESC_LOAD_LO12", + "BFD_RELOC_RISCV_TLSDESC_ADD_LO12", + "BFD_RELOC_RISCV_TLSDESC_CALL", "BFD_RELOC_RISCV_ALIGN", "BFD_RELOC_RISCV_RVC_BRANCH", "BFD_RELOC_RISCV_RVC_JUMP", diff --git a/bfd/reloc.c b/bfd/reloc.c index 07d35e45c8b..38e1bdfc754 100644 --- a/bfd/reloc.c +++ b/bfd/reloc.c @@ -5016,6 +5016,14 @@ ENUMX BFD_RELOC_RISCV_TLS_TPREL32 ENUMX BFD_RELOC_RISCV_TLS_TPREL64 +ENUMX + BFD_RELOC_RISCV_TLSDESC_HI20 +ENUMX + BFD_RELOC_RISCV_TLSDESC_LOAD_LO12 +ENUMX + BFD_RELOC_RISCV_TLSDESC_ADD_LO12 +ENUMX + BFD_RELOC_RISCV_TLSDESC_CALL ENUMX BFD_RELOC_RISCV_ALIGN ENUMX diff --git a/include/elf/riscv.h b/include/elf/riscv.h index 0aa8b3359c4..f32215f5d95 100644 --- a/include/elf/riscv.h +++ b/include/elf/riscv.h @@ -44,6 +44,7 @@ START_RELOC_NUMBERS (elf_riscv_reloc_type) RELOC_NUMBER (R_RISCV_TLS_DTPREL64, 9) RELOC_NUMBER (R_RISCV_TLS_TPREL32, 10) RELOC_NUMBER (R_RISCV_TLS_TPREL64, 11) + RELOC_NUMBER (R_RISCV_TLSDESC, 12) /* Relocation types not used by the dynamic linker. */ RELOC_NUMBER (R_RISCV_BRANCH, 16) @@ -90,6 +91,10 @@ START_RELOC_NUMBERS (elf_riscv_reloc_type) /* Reserved 59 for R_RISCV_PLT32. */ RELOC_NUMBER (R_RISCV_SET_ULEB128, 60) RELOC_NUMBER (R_RISCV_SUB_ULEB128, 61) + RELOC_NUMBER (R_RISCV_TLSDESC_HI20, 62) + RELOC_NUMBER (R_RISCV_TLSDESC_LOAD_LO12, 63) + RELOC_NUMBER (R_RISCV_TLSDESC_ADD_LO12, 64) + RELOC_NUMBER (R_RISCV_TLSDESC_CALL, 65) END_RELOC_NUMBERS (R_RISCV_max) /* Processor specific flags for the ELF header e_flags field. */ From patchwork Thu Aug 31 17:13:33 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tatsuyuki Ishi X-Patchwork-Id: 137323 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:c792:0:b0:3f2:4152:657d with SMTP id b18csp384303vqu; Thu, 31 Aug 2023 10:16:05 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGL79xCRwyy8KZsevjIyFvtwTnbvoDEB0sYkOIgLRV+0/fApmSPp1eZJdD0lGwZDZxkx+Uu X-Received: by 2002:a17:906:210a:b0:9a1:f4e8:87b9 with SMTP id 10-20020a170906210a00b009a1f4e887b9mr4567978ejt.45.1693502164830; Thu, 31 Aug 2023 10:16:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1693502164; cv=none; d=google.com; s=arc-20160816; b=AKDYOZnmRFGxh6cQNOGJmBaJrcM7LO6Juig1s8MHnkCW3Tzhs/EMp54I4p2ZUQWzP3 XBnLFMGpKJOUx0DsNZTLivWiv+L72QN8O3IVn7n0N7fi6A5uijy8xThke/sbfzmj2e+1 3RkeC9pFefLKaB8w2hRiYf5bRPGqicElcp9JGVxI53EOa5ofiV2tewwQ09jbMnFwABat 3Lfsnq6pVNQzV9EuikYpB0v0b229xCNauwrzdfplUDCIBGnXGTdOc2aDct6OKNGRYDCP 3S50se18R8s480X7HI5ay7nAwX9r0xV+Zl9J13obGqMaCxn6Zc0S53PwvOIUzuPei5f4 vTBQ== 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=5YCCMW7ND1vcCAG6lQW0Mpje5cKXt2TdFpII8jqPPSg=; fh=daQvw8iO4pGm+Xx4NLO3E4+0gdDDq/ixoODeemdNLuQ=; b=YtcnPFZbpGNBvBWygAQ9HdcuUyNJO4Th0k7zUQ59dKaD7BCOUFOJSw7t8Za/B/hix3 e10yfeI8I5OVMFcgWQQKbI3EfntnPfQCHjBRRVbEvHXEQLIUzAU+rGOdkjg+J0ic1vop XProwKHm71Hnn9Mm94EdJxc6heycE5jhJ0YZCmepZnBnJ8xxA1CFgmAnzAsis9Kxqnoe Q5MmY962PHiGXIa/z3yzXxWkZbDhDq/nWwg2+Mh+YYSPh/exik1gnO8CjBcx9PV83p3k 9T0Fi49m+Fw/KyAeB8a4zSjfeRQ4hQjrU2rFb4mChPFganw+m/yehaod/TeeZcaevquj gwxw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=uFaqnfFY; 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 t16-20020a1709064f1000b00992a9b11cf2si1225368eju.787.2023.08.31.10.16.04 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 31 Aug 2023 10:16:04 -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=uFaqnfFY; 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 9F9203857355 for ; Thu, 31 Aug 2023 17:15:23 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 9F9203857355 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1693502123; bh=5YCCMW7ND1vcCAG6lQW0Mpje5cKXt2TdFpII8jqPPSg=; 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=uFaqnfFYpAyJSnVxsrA+DgyOe5qJEGgV4zEs7QOx92XGgJsxE8ewqBmM36DG7gkYK lhQSWRbBipFKmGjygnE2nVBmFTrX37spGBeEbYytSrMEhNCc3L7kcw1JlgblQn/qNo 9GopR0Bjeu5rEHyO9Sox3EJHCNjqmbP9gZcShr+o= X-Original-To: binutils@sourceware.org Delivered-To: binutils@sourceware.org Received: from mail-pl1-x634.google.com (mail-pl1-x634.google.com [IPv6:2607:f8b0:4864:20::634]) by sourceware.org (Postfix) with ESMTPS id 0B5B53858413 for ; Thu, 31 Aug 2023 17:13:59 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 0B5B53858413 Received: by mail-pl1-x634.google.com with SMTP id d9443c01a7336-1c0efe0c4acso1222245ad.0 for ; Thu, 31 Aug 2023 10:13:58 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1693502037; x=1694106837; 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=5YCCMW7ND1vcCAG6lQW0Mpje5cKXt2TdFpII8jqPPSg=; b=hkTNh6s8PV5pWaxvjkl5kgDLLgLqweRjAIgM/05OAxW4FfclB8k7S86VrTWXdMfOE4 tMus6iLyAjB6mD/LQX7gaRg2AXuTFKBeWJd4isVqhIbdwWO6svxhnOEEqzd80b9iXmR8 sLomeJ6HfFf9I1N0n0vY81SGsj/mnUdKjb0ckXxHS+rWg0SwlvanV8eNswO54MKozbHe cVBETG3/fo3YoBteA962iXFoMfQ+dhMgVTxaWSSnB0/VAWukE7KjzggkDFwzb9oC521z /VSi3FVWCpAnp7CN7kTY08YGKZij+9g3GeY3Dym4PbVYzwZzRV4mjAx+PkzRkT22QyLn nL0Q== X-Gm-Message-State: AOJu0YyGDBmhACvqiIzOTwy3ySZkfzPORmAiZ9kbU/L/TiRN3Q7hTfMP WEBJvcLXsXSit2ZneTi+EFM+ymrLZTRvz6au X-Received: by 2002:a17:903:2301:b0:1c2:c60:8388 with SMTP id d1-20020a170903230100b001c20c608388mr323325plh.6.1693502037494; Thu, 31 Aug 2023 10:13:57 -0700 (PDT) Received: from localhost (zz20184013906F627101.userreverse.dion.ne.jp. [111.98.113.1]) by smtp.gmail.com with ESMTPSA id k1-20020a170902694100b001bc21222e34sm1477048plt.285.2023.08.31.10.13.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 31 Aug 2023 10:13:57 -0700 (PDT) To: binutils@sourceware.org Subject: [PATCH v2 3/5] RISC-V: Add assembly support for TLSDESC. Date: Fri, 1 Sep 2023 02:13:33 +0900 Message-ID: <20230831171345.49052-4-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.8 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: 1774500739865715451 X-GMAIL-MSGID: 1775765725995532463 gas/ * tc-riscv.c (percent_op_*): Add support for %tlsdesc_hi, %tlsdesc_load_lo, %tlsdesc_add_lo and %tlsdesc_call. percent_op_rtype renamed to percent_op_relax_only as this matcher is extended to handle jalr as well which is not R-type. (riscv_ip): Apply the percent_op_relax_only rename and update comment. (md_apply_fix): Add TLSDESC_* to relaxable list. Add TLSDESC_HI20 to TLS relocation check list. * testsuite/gas/riscv/tlsdesc.*: New test cases for TLSDESC relocation generation. opcodes/ * riscv-opc.c (riscv_opcodes): Add a new syntax for jalr with %tlsdesc_call annotations. --- gas/config/tc-riscv.c | 18 +++++++++++++----- gas/testsuite/gas/riscv/tlsdesc.d | 22 ++++++++++++++++++++++ gas/testsuite/gas/riscv/tlsdesc.s | 24 ++++++++++++++++++++++++ opcodes/riscv-opc.c | 1 + 4 files changed, 60 insertions(+), 5 deletions(-) create mode 100644 gas/testsuite/gas/riscv/tlsdesc.d create mode 100644 gas/testsuite/gas/riscv/tlsdesc.s diff --git a/gas/config/tc-riscv.c b/gas/config/tc-riscv.c index 959cbbc32a5..0a1fac9de9d 100644 --- a/gas/config/tc-riscv.c +++ b/gas/config/tc-riscv.c @@ -2195,6 +2195,7 @@ static const struct percent_op_match percent_op_utype[] = {"tprel_hi", BFD_RELOC_RISCV_TPREL_HI20}, {"pcrel_hi", BFD_RELOC_RISCV_PCREL_HI20}, {"got_pcrel_hi", BFD_RELOC_RISCV_GOT_HI20}, + {"tlsdesc_hi", BFD_RELOC_RISCV_TLSDESC_HI20}, {"tls_ie_pcrel_hi", BFD_RELOC_RISCV_TLS_GOT_HI20}, {"tls_gd_pcrel_hi", BFD_RELOC_RISCV_TLS_GD_HI20}, {"hi", BFD_RELOC_RISCV_HI20}, @@ -2206,6 +2207,8 @@ static const struct percent_op_match percent_op_itype[] = {"lo", BFD_RELOC_RISCV_LO12_I}, {"tprel_lo", BFD_RELOC_RISCV_TPREL_LO12_I}, {"pcrel_lo", BFD_RELOC_RISCV_PCREL_LO12_I}, + {"tlsdesc_load_lo", BFD_RELOC_RISCV_TLSDESC_LOAD_LO12}, + {"tlsdesc_add_lo", BFD_RELOC_RISCV_TLSDESC_ADD_LO12}, {0, 0} }; @@ -2217,8 +2220,9 @@ static const struct percent_op_match percent_op_stype[] = {0, 0} }; -static const struct percent_op_match percent_op_rtype[] = +static const struct percent_op_match percent_op_relax_only[] = { + {"tlsdesc_call", BFD_RELOC_RISCV_TLSDESC_CALL}, {"tprel_add", BFD_RELOC_RISCV_TPREL_ADD}, {0, 0} }; @@ -3326,10 +3330,10 @@ riscv_ip (char *str, struct riscv_cl_insn *ip, expressionS *imm_expr, *imm_reloc = BFD_RELOC_RISCV_LO12_I; goto load_store; case '1': - /* This is used for TLS, where the fourth operand is - %tprel_add, to get a relocation applied to an add - instruction, for relaxation to use. */ - p = percent_op_rtype; + /* This is used for TLS relocations that acts as relaxation + markers and do not change the instruction encoding, + i.e. %tprel_add and %tlsdesc_call. */ + p = percent_op_relax_only; goto alu_op; case '0': /* AMO displacement, which must be zero. */ load_store: @@ -4082,6 +4086,7 @@ md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED) case BFD_RELOC_RISCV_TPREL_LO12_I: case BFD_RELOC_RISCV_TPREL_LO12_S: case BFD_RELOC_RISCV_TPREL_ADD: + case BFD_RELOC_RISCV_TLSDESC_HI20: relaxable = true; /* Fall through. */ @@ -4255,6 +4260,9 @@ md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED) case BFD_RELOC_RISCV_CALL: case BFD_RELOC_RISCV_CALL_PLT: + case BFD_RELOC_RISCV_TLSDESC_LOAD_LO12: + case BFD_RELOC_RISCV_TLSDESC_ADD_LO12: + case BFD_RELOC_RISCV_TLSDESC_CALL: relaxable = true; break; diff --git a/gas/testsuite/gas/riscv/tlsdesc.d b/gas/testsuite/gas/riscv/tlsdesc.d new file mode 100644 index 00000000000..8ba35826130 --- /dev/null +++ b/gas/testsuite/gas/riscv/tlsdesc.d @@ -0,0 +1,22 @@ +#as: -march=rv32ia +#source: tlsdesc.s +#readelf: -Wr + +Relocation section '.rela.text' at offset 0x[0-9a-f]+ contains 16 entries: + +Offset +Info +Type +Sym. Value +Symbol's Name \+ Addend +0+ +0+a3e +R_RISCV_TLSDESC_HI20 +0+ +sg1 \+ 0 +0+ +0+33 +R_RISCV_RELAX + 0 +0+4 +0+63f +R_RISCV_TLSDESC_LOAD_LO12 0+ +\.desc1 \+ 0 +0+4 +0+33 +R_RISCV_RELAX + 0 +0+8 +0+640 +R_RISCV_TLSDESC_ADD_LO12 0+ +\.desc1 \+ 0 +0+8 +0+33 +R_RISCV_RELAX + 0 +0+c +0+641 +R_RISCV_TLSDESC_CALL +0+ +\.desc1 \+ 0 +0+c +0+33 +R_RISCV_RELAX + 0 +0+10 +0+53e +R_RISCV_TLSDESC_HI20 +0+4 +sl1 \+ 0 +0+10 +0+33 +R_RISCV_RELAX + 0 +0+14 +0+83f +R_RISCV_TLSDESC_LOAD_LO12 0+10 +\.desc2 \+ 0 +0+14 +0+33 +R_RISCV_RELAX + 0 +0+18 +0+840 +R_RISCV_TLSDESC_ADD_LO12 0+10 +\.desc2 \+ 0 +0+18 +0+33 +R_RISCV_RELAX +0 +0+1c +0+841 +R_RISCV_TLSDESC_CALL +0+10 +\.desc2 \+ 0 +0+1c +0+33 +R_RISCV_RELAX +0 \ No newline at end of file diff --git a/gas/testsuite/gas/riscv/tlsdesc.s b/gas/testsuite/gas/riscv/tlsdesc.s new file mode 100644 index 00000000000..15468d5f947 --- /dev/null +++ b/gas/testsuite/gas/riscv/tlsdesc.s @@ -0,0 +1,24 @@ + .section .tbss,"awT",@nobits + .global sg1 +sg1: + .zero 4 +sl1: + .zero 4 + + .text + .globl _start + .type _start,@function +_start: +.desc1: + auipc a0, %tlsdesc_hi(sg1) + lw t0, %tlsdesc_load_lo(.desc1)(a0) + addi a0, a0, %tlsdesc_add_lo(.desc1) + jalr t0, t0, %tlsdesc_call(.desc1) + +.desc2: + auipc a0, %tlsdesc_hi(sl1) + lw t0, %tlsdesc_load_lo(.desc2)(a0) + addi a0, a0, %tlsdesc_add_lo(.desc2) + jalr t0, t0, %tlsdesc_call(.desc2) + + ret diff --git a/opcodes/riscv-opc.c b/opcodes/riscv-opc.c index 067e9fdb611..467666b9805 100644 --- a/opcodes/riscv-opc.c +++ b/opcodes/riscv-opc.c @@ -370,6 +370,7 @@ const struct riscv_opcode riscv_opcodes[] = {"jalr", 0, INSN_CLASS_I, "s,j", MATCH_JALR|(X_RA << OP_SH_RD), MASK_JALR|MASK_RD, match_opcode, INSN_ALIAS|INSN_JSR }, {"jalr", 0, INSN_CLASS_I, "d,s", MATCH_JALR, MASK_JALR|MASK_IMM, match_opcode, INSN_ALIAS|INSN_JSR }, {"jalr", 0, INSN_CLASS_I, "d,o(s)", MATCH_JALR, MASK_JALR, match_opcode, INSN_JSR }, +{"jalr", 0, INSN_CLASS_I, "d,s,1", MATCH_JALR, MASK_JALR|MASK_IMM, match_opcode, INSN_JSR }, {"jalr", 0, INSN_CLASS_I, "d,s,j", MATCH_JALR, MASK_JALR, match_opcode, INSN_JSR }, {"j", 0, INSN_CLASS_C, "Ca", MATCH_C_J, MASK_C_J, match_opcode, INSN_ALIAS|INSN_BRANCH }, {"j", 0, INSN_CLASS_I, "a", MATCH_JAL, MASK_JAL|MASK_RD, match_opcode, INSN_ALIAS|INSN_BRANCH }, From patchwork Thu Aug 31 17:13:34 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tatsuyuki Ishi X-Patchwork-Id: 137324 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:c792:0:b0:3f2:4152:657d with SMTP id b18csp384492vqu; Thu, 31 Aug 2023 10:16:25 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHVniYX5NLsYZuD1ayF9SneuKN0VvJnJ/K+PMDm8qTaos0HItgvFR5Akvd4HOQh/d9neSO0 X-Received: by 2002:aa7:c95a:0:b0:52a:841:bc58 with SMTP id h26-20020aa7c95a000000b0052a0841bc58mr126638edt.30.1693502185019; Thu, 31 Aug 2023 10:16:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1693502185; cv=none; d=google.com; s=arc-20160816; b=fgs5UyfZDERrkiCJUi0Q/ftMC46iimbh6IAXRA7N7wyoD+4E1r7fRVTjx0HVQfWVkV vGGwnYiloYuoTHopsNVff0gd1BVsCPwH8zfTjIF0CafXhyvrEZMxknQKrH1Qkg/NnsYJ GaA8ebzgBB01St8MNCij8x9gRTySdffh7pNs5uOZytuuux6YPQsM0pbEvFQZgmOeJFUo 6k3IGO0LMeHrvOWrxUxh1GTuq+icVeBfrhZ8p/Uy/uzhEl84YpBTJ7hLz/BBMUZs5sYJ nOYVvxC/p7j89mbjH4wW0Vy03w2+3Viu3zA2bzJWY05BLelkPJnJOx51NRaadgiEgAU2 zITw== 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=KN+QN6M3KrqHG3eBVvdBKF2VYCLMTklLf+5TnZxcZHg=; fh=daQvw8iO4pGm+Xx4NLO3E4+0gdDDq/ixoODeemdNLuQ=; b=rNq07GSpOuBv7zY0aM6fB7CmZ17lk1PKspmTm0sVAEFhHS6R8udS0MWQqMkF3RzsAh hGxhH7KzjiNhOPNnnXGh8Wfep0I4AIFGl95HbNZSaCJbzDscGMgP3bGtq1nD0q4255ZI iDMmEOOza4h9x3NlJ4FyWCUK3D8Fn/xuJ3TbtjqFRIrFhBsAgpKtKxZNo8vwWdYlvz6U Ut2Og6aObh//Nb2xUWLN4h6wbah0okSMJuXwvv88/kBvkkIngSl/+2KNGL7vSOK/19Gh JJUawchJKFvoJPKQ9JtUjXZMmlGqWEg5za1ME0nq+bMhngnYyRbtJB3Fmivwo4TF+Ulu zcNQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=NkeluSQo; 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 y22-20020aa7d516000000b005294457cc0dsi1254423edq.588.2023.08.31.10.16.24 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 31 Aug 2023 10:16:25 -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=NkeluSQo; 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 942683856241 for ; Thu, 31 Aug 2023 17:15:36 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 942683856241 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1693502136; bh=KN+QN6M3KrqHG3eBVvdBKF2VYCLMTklLf+5TnZxcZHg=; 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=NkeluSQog5DvEKXePEH9MtXn5Sws8AicuTlHneCluq1+ZdU8yJzPDnZxmung3GqE/ UHKKD/0LhiYxSKLDSydy/ieJlm8VW7cKAIUhBlRJKRCK430vA9J2i1g4yAgjpDiNKU hdlgqGg3ywtNNrKrCH4hQKhU1I2eOc+cM2R62nys= X-Original-To: binutils@sourceware.org Delivered-To: binutils@sourceware.org Received: from mail-pf1-x42c.google.com (mail-pf1-x42c.google.com [IPv6:2607:f8b0:4864:20::42c]) by sourceware.org (Postfix) with ESMTPS id 7052C385842A for ; Thu, 31 Aug 2023 17:14:01 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 7052C385842A Received: by mail-pf1-x42c.google.com with SMTP id d2e1a72fcca58-68a56ed12c0so239950b3a.0 for ; Thu, 31 Aug 2023 10:14:01 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1693502040; x=1694106840; 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=KN+QN6M3KrqHG3eBVvdBKF2VYCLMTklLf+5TnZxcZHg=; b=iTHbAUEmSuKvNSfqDHrLfl1lySdzRsPrya+SFioqX0joSrxHK8W3jH6mDVrad8OBBh Xe8rVrX8Guwj7ZrmofeKhgCK2vidAhBzrmpCVmJET/7dAMbOOLfY+XzHAioL0GEqqBRo 13+5h4j/MHbtaZVrBXYZT1nkSgxtniUU8TXyPnVqZp+KXrchvACmlFcVxP1kbUn+Z0QC 37TiV4Mg8iyoGFnSiTxyE7nk+J5F8ndqsClq9Rv5Mi65TgGknAm/eF61LvJCcnbSlb5T 02c8iMupvGzKlHWKTAvcpLjQeuNjs57gkUO3q/Ou6mTd0oUMb6nGgc00mqbo+pwb/XvJ x8LA== X-Gm-Message-State: AOJu0YzRWO3waJ+HowgKfVlF64MSoD4qdgV/pneUilb2aFTA7DP79C4B 1/p4R185Ej/8B4t5e9JP9lzGPD3A7MJsC62E X-Received: by 2002:a05:6a20:7d97:b0:145:3bd9:1377 with SMTP id v23-20020a056a207d9700b001453bd91377mr419228pzj.5.1693502039940; Thu, 31 Aug 2023 10:13:59 -0700 (PDT) Received: from localhost (zz20184013906F627101.userreverse.dion.ne.jp. [111.98.113.1]) by smtp.gmail.com with ESMTPSA id v23-20020aa78097000000b0063f0068cf6csm1536462pff.198.2023.08.31.10.13.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 31 Aug 2023 10:13:59 -0700 (PDT) To: binutils@sourceware.org Subject: [PATCH v2 4/5] RISC-V: Define and use GOT entry size constants for TLS. Date: Fri, 1 Sep 2023 02:13:34 +0900 Message-ID: <20230831171345.49052-5-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.8 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: 1774500850174321211 X-GMAIL-MSGID: 1775765747617094308 As the size calculation is split by global and local symbols, using a shared constant definition for its size improves clarity. bfd/ * elfnn-riscv.c: Add macros for sizes of a normal GOT entry, TLS GD and TLS IE entry. (allocate_dynrelocs): Replace GOT size expressions with the new constants. (riscv_elf_size_dynamic_sections): Likewise. (riscv_elf_relocate_section): Likewise. --- bfd/elfnn-riscv.c | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/bfd/elfnn-riscv.c b/bfd/elfnn-riscv.c index 02b46835cc7..38883134828 100644 --- a/bfd/elfnn-riscv.c +++ b/bfd/elfnn-riscv.c @@ -301,6 +301,8 @@ riscv_is_insn_reloc (const reloc_howto_type *howto) #define PLT_HEADER_SIZE (PLT_HEADER_INSNS * 4) #define PLT_ENTRY_SIZE (PLT_ENTRY_INSNS * 4) #define GOT_ENTRY_SIZE RISCV_ELF_WORD_BYTES +#define TLS_GD_GOT_ENTRY_SIZE (RISCV_ELF_WORD_BYTES * 2) +#define TLS_IE_GOT_ENTRY_SIZE RISCV_ELF_WORD_BYTES /* Reserve two entries of GOTPLT for ld.so, one is used for PLT resolver, the other is used for link map. Other targets also reserve one more entry used for runtime profile? */ @@ -1323,7 +1325,7 @@ allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf) /* TLS_GD needs two dynamic relocs and two GOT slots. */ if (tls_type & GOT_TLS_GD) { - s->size += 2 * RISCV_ELF_WORD_BYTES; + s->size += TLS_GD_GOT_ENTRY_SIZE; if (need_reloc) htab->elf.srelgot->size += 2 * sizeof (ElfNN_External_Rela); } @@ -1331,14 +1333,14 @@ allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf) /* TLS_IE needs one dynamic reloc and one GOT slot. */ if (tls_type & GOT_TLS_IE) { - s->size += RISCV_ELF_WORD_BYTES; + s->size += TLS_IE_GOT_ENTRY_SIZE; if (need_reloc) htab->elf.srelgot->size += sizeof (ElfNN_External_Rela); } } else { - s->size += RISCV_ELF_WORD_BYTES; + s->size += GOT_ENTRY_SIZE; if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), h) && ! UNDEFWEAK_NO_DYNAMIC_RELOC (info, h)) htab->elf.srelgot->size += sizeof (ElfNN_External_Rela); @@ -1566,20 +1568,20 @@ riscv_elf_size_dynamic_sections (bfd *output_bfd, struct bfd_link_info *info) { if (*local_tls_type & GOT_TLS_GD) { - s->size += 2 * RISCV_ELF_WORD_BYTES; + s->size += TLS_GD_GOT_ENTRY_SIZE; if (bfd_link_dll (info)) srel->size += sizeof (ElfNN_External_Rela); } if (*local_tls_type & GOT_TLS_IE) { - s->size += RISCV_ELF_WORD_BYTES; + s->size += TLS_IE_GOT_ENTRY_SIZE; if (bfd_link_dll (info)) srel->size += sizeof (ElfNN_External_Rela); } } else { - s->size += RISCV_ELF_WORD_BYTES; + s->size += GOT_ENTRY_SIZE; if (bfd_link_pic (info)) srel->size += sizeof (ElfNN_External_Rela); } @@ -2905,7 +2907,7 @@ riscv_elf_relocate_section (bfd *output_bfd, reference's GOT slot follows the GD reference's slots. */ ie_off = 0; if ((tls_type & GOT_TLS_GD) && (tls_type & GOT_TLS_IE)) - ie_off = 2 * GOT_ENTRY_SIZE; + ie_off = TLS_GD_GOT_ENTRY_SIZE; if ((off & 1) != 0) off &= ~1; From patchwork Thu Aug 31 17:13:35 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tatsuyuki Ishi X-Patchwork-Id: 137326 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:c792:0:b0:3f2:4152:657d with SMTP id b18csp385147vqu; Thu, 31 Aug 2023 10:17:39 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFrwaNU3UsArZdi/q0yMftQE320zGJKKss+R7TtJt7DAZ0352a7QBvhyLsiGy+WViYqgbsA X-Received: by 2002:a2e:890d:0:b0:2ba:7b3b:4b7d with SMTP id d13-20020a2e890d000000b002ba7b3b4b7dmr4958630lji.17.1693502258981; Thu, 31 Aug 2023 10:17:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1693502258; cv=none; d=google.com; s=arc-20160816; b=OvwQl2/gvGUEpiaPkCnvcp9fd2b8+xZyLjQh/HjAXM4qxHdumh3yMNjBxB332QRvdk fv3jqEj3SKWBvjnpl3yJ18cy7WDvDgAZyi9C5gvl/TAwZaydqVEgWowyBvfRGZjFoVRn uCknqkmvtDR/0P4CJM4dNqQMMKwmCXh1pd2JjppTG1ezJWtAMwoVWnvLiX1antpoCxoX amXo/6byUkzH5w88P+IWQwVvldXGqRC3/hNcLwZsVZJQgAePyykTAj23TH9A+5csd0ow Tu3TEfwPMIbkcdyDF3FY2Is14pCWNaxNqJ1tT9kNHuRyIeor7klYF/pAQgxstYKS40mu IiJA== 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=RTP5R+WYlirXcEeLe/ajPpvDMiPRbWM0zT4y9PZB7/E=; fh=daQvw8iO4pGm+Xx4NLO3E4+0gdDDq/ixoODeemdNLuQ=; b=nN9psJP27JrFAKKcXZXrTA4MnVX8DzNzlJ1m9d7MzLrMpeKelg/J7nWTrwi3J3yQJk Aiq/b37FuGPZehVAEu/WbJ2KQt1HUmMLZHvKRLVInLAOB+omDL3qqCDUzl48hmqlqbTC 5aEVQBHDHoF+r5IqJPKGQ4XVtEHYmtIj02DM3sx0CsSjfViUXXB6GAU0HgiVzHfhn7ZO IBgleVmXbxFKteZaz6vB7/W2twHVm/g0TVatKZ2euGAGw8PlC1AeflU3iAcf5SkS3RWN GjUDdvTSGxeHi9ddzCKWu+gY/5PLoz/F8HSY/f/2K80m1LR4n3TCnEaHonMM/MUgpjCS VL1Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b="JKTGey/o"; spf=pass (google.com: domain of binutils-bounces+ouuuleilei=gmail.com@sourceware.org designates 2620:52:3:1:0:246e:9693:128c 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 (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id gz23-20020a170906f2d700b0099b4a25c9b5si1298903ejb.450.2023.08.31.10.17.38 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 31 Aug 2023 10:17:38 -0700 (PDT) Received-SPF: pass (google.com: domain of binutils-bounces+ouuuleilei=gmail.com@sourceware.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b="JKTGey/o"; spf=pass (google.com: domain of binutils-bounces+ouuuleilei=gmail.com@sourceware.org designates 2620:52:3:1:0:246e:9693:128c 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 733B538555B9 for ; Thu, 31 Aug 2023 17:16:44 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 733B538555B9 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1693502204; bh=RTP5R+WYlirXcEeLe/ajPpvDMiPRbWM0zT4y9PZB7/E=; 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=JKTGey/oNijWwZDnQgd4mrYu+wHafe3GkMWDCj5+lVKceddQFciflUH8pqkPZtoHN 6fzryr4N7GH3wo6VpBYDgUQ5797JIG8X4OuYl10QtihnPbXa1rCIxaO0+r+g49hTxK dt4vT4DFOC/VcqbYxOwd8ZwRFQ2sJGVRNqsiOYHY= X-Original-To: binutils@sourceware.org Delivered-To: binutils@sourceware.org Received: from mail-pf1-x42e.google.com (mail-pf1-x42e.google.com [IPv6:2607:f8b0:4864:20::42e]) by sourceware.org (Postfix) with ESMTPS id 309E93858D20 for ; Thu, 31 Aug 2023 17:14:05 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 309E93858D20 Received: by mail-pf1-x42e.google.com with SMTP id d2e1a72fcca58-68a56ed12c0so239962b3a.0 for ; Thu, 31 Aug 2023 10:14:05 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1693502043; x=1694106843; 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=RTP5R+WYlirXcEeLe/ajPpvDMiPRbWM0zT4y9PZB7/E=; b=WYjVl3LnmQnWvncemPqTXQnn3yVZHU+OP1rM5T7VpfmbegLe5bt9X4F0/11YSuXCF3 jFMH88Eessz4KzSiHxzn553Tz/4N871p/o79NByHliGBt7zmagaMzf4cB05WbMR+YBIW 9PzF0XXLWAaTqtJtjGtVuF0NU7jqRtrm3v09R1Nd92wtiMr46db/+hsrOyzyNOkR8mZo xrD3jU1KdW1M35nI4a3hTQeG+1RVI5ueFZQkvnslu0x77zfzdYm1h1drz7dh5kUxz6Ul cVQLrFESRAodlI64PNJLN5iY7//7uv3Ta4srS0KfPWl3dfe/x8qVTZW4iZxUzmBo16Qa bG0g== X-Gm-Message-State: AOJu0YzXcLG5DMvYe5gwcjb/UA7BAg5D/LYNvhoKUwoHG/6N8oGjOqbB MZAc/cw4YFu47F/SguYT5iI1epAbbpKHYR7O X-Received: by 2002:a05:6a20:4289:b0:133:7a67:b477 with SMTP id o9-20020a056a20428900b001337a67b477mr522939pzj.1.1693502043564; Thu, 31 Aug 2023 10:14:03 -0700 (PDT) Received: from localhost (zz20184013906F627101.userreverse.dion.ne.jp. [111.98.113.1]) by smtp.gmail.com with ESMTPSA id k5-20020aa790c5000000b00686bef984e2sm1538494pfk.80.2023.08.31.10.14.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 31 Aug 2023 10:14:02 -0700 (PDT) To: binutils@sourceware.org Subject: [PATCH v2 5/5] RISC-V: Initial ld.bfd support for TLSDESC. Date: Fri, 1 Sep 2023 02:13:35 +0900 Message-ID: <20230831171345.49052-6-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.9 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: 1774500943007427688 X-GMAIL-MSGID: 1775765824488642677 Only relocation handling for now; relaxation is not implemented yet. bfd/ * elfnn-riscv.c (riscv_elf_check_relocs): Record GOT reference and paired relocation for TLSDESC_HI20. (riscv_elf_adjust_dynamic_symbol): Allocate GOT and reloc slots for TLSDESC symbols. (riscv_elf_size_dynamic_sections): Likewise but for local symbols. (tlsdescoff): New helper to determine static addend for R_TLSDESC. (riscv_elf_relocate_section): Ignore TLSDESC_CALL reloc for now (it is relaxation only). Handle TLSDESC_{LOAD,ADD}_LO12 as paired pcrel relocs. For TLS GOT slot generation, generalize the logic to handle any combination of (GD, IE, TLSDESC). Add TLSDESC Rela generation. * ld/testsuite/ld-riscv-elf/tls*: Add TLSDESC instruction sequences next to the existing GD and IE sequences. Update expectations. --- v2: Fix one formatting issue, and an erroneous comment referring to the four-slot TLSDESC layout instead of the two-slot one. bfd/elfnn-riscv.c | 91 +++++++++++++++++++++++++++--- ld/testsuite/ld-riscv-elf/tls.d | 4 +- ld/testsuite/ld-riscv-elf/tls.s | 14 +++++ ld/testsuite/ld-riscv-elf/tlsbin.d | 5 ++ 4 files changed, 104 insertions(+), 10 deletions(-) diff --git a/bfd/elfnn-riscv.c b/bfd/elfnn-riscv.c index 38883134828..35011a25341 100644 --- a/bfd/elfnn-riscv.c +++ b/bfd/elfnn-riscv.c @@ -166,6 +166,7 @@ struct riscv_elf_link_hash_entry #define GOT_TLS_GD 2 #define GOT_TLS_IE 4 #define GOT_TLS_LE 8 +#define GOT_TLSDESC 16 char tls_type; }; @@ -303,6 +304,7 @@ riscv_is_insn_reloc (const reloc_howto_type *howto) #define GOT_ENTRY_SIZE RISCV_ELF_WORD_BYTES #define TLS_GD_GOT_ENTRY_SIZE (RISCV_ELF_WORD_BYTES * 2) #define TLS_IE_GOT_ENTRY_SIZE RISCV_ELF_WORD_BYTES +#define TLSDESC_GOT_ENTRY_SIZE (RISCV_ELF_WORD_BYTES * 2) /* Reserve two entries of GOTPLT for ld.so, one is used for PLT resolver, the other is used for link map. Other targets also reserve one more entry used for runtime profile? */ @@ -858,6 +860,12 @@ riscv_elf_check_relocs (bfd *abfd, struct bfd_link_info *info, return false; break; + case R_RISCV_TLSDESC_HI20: + if (!riscv_elf_record_got_reference (abfd, info, h, r_symndx) + || !riscv_elf_record_tls_type (abfd, h, r_symndx, GOT_TLSDESC)) + return false; + break; + case R_RISCV_CALL: case R_RISCV_CALL_PLT: /* These symbol requires a procedure linkage table entry. @@ -1316,7 +1324,7 @@ allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf) s = htab->elf.sgot; h->got.offset = s->size; dyn = htab->elf.dynamic_sections_created; - if (tls_type & (GOT_TLS_GD | GOT_TLS_IE)) + if (tls_type & (GOT_TLS_GD | GOT_TLS_IE | GOT_TLSDESC)) { int indx = 0; bool need_reloc = false; @@ -1337,6 +1345,14 @@ allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf) if (need_reloc) htab->elf.srelgot->size += sizeof (ElfNN_External_Rela); } + + /* TLSDESC needs one dynamic reloc and two GOT slots. */ + if (tls_type & GOT_TLSDESC) + { + s->size += TLSDESC_GOT_ENTRY_SIZE; + /* TLSDESC always use dynamic relocs. */ + htab->elf.srelgot->size += sizeof (ElfNN_External_Rela); + } } else { @@ -1564,7 +1580,7 @@ riscv_elf_size_dynamic_sections (bfd *output_bfd, struct bfd_link_info *info) if (*local_got > 0) { *local_got = s->size; - if (*local_tls_type & (GOT_TLS_GD | GOT_TLS_IE)) + if (*local_tls_type & (GOT_TLS_GD | GOT_TLS_IE | GOT_TLSDESC)) { if (*local_tls_type & GOT_TLS_GD) { @@ -1578,6 +1594,11 @@ riscv_elf_size_dynamic_sections (bfd *output_bfd, struct bfd_link_info *info) if (bfd_link_dll (info)) srel->size += sizeof (ElfNN_External_Rela); } + if (*local_tls_type & GOT_TLSDESC) + { + s->size += TLSDESC_GOT_ENTRY_SIZE; + srel->size += sizeof (ElfNN_External_Rela); + } } else { @@ -1728,6 +1749,17 @@ tpoff (struct bfd_link_info *info, bfd_vma address) return address - elf_hash_table (info)->tls_sec->vma - TP_OFFSET; } +/* Return the relocation value for a static TLSDESC relocation. */ + +static bfd_vma +tlsdescoff (struct bfd_link_info *info, bfd_vma address) +{ + /* If tls_sec is NULL, we should have signalled an error already. */ + if (elf_hash_table (info)->tls_sec == NULL) + return 0; + return address - elf_hash_table (info)->tls_sec->vma; +} + /* Return the global pointer's value, or 0 if it is not in use. */ static bfd_vma @@ -1764,6 +1796,7 @@ perform_relocation (const reloc_howto_type *howto, case R_RISCV_GOT_HI20: case R_RISCV_TLS_GOT_HI20: case R_RISCV_TLS_GD_HI20: + case R_RISCV_TLSDESC_HI20: if (ARCH_SIZE > 32 && !VALID_UTYPE_IMM (RISCV_CONST_HIGH_PART (value))) return bfd_reloc_overflow; value = ENCODE_UTYPE_IMM (RISCV_CONST_HIGH_PART (value)); @@ -1774,6 +1807,8 @@ perform_relocation (const reloc_howto_type *howto, case R_RISCV_TPREL_LO12_I: case R_RISCV_TPREL_I: case R_RISCV_PCREL_LO12_I: + case R_RISCV_TLSDESC_LOAD_LO12: + case R_RISCV_TLSDESC_ADD_LO12: value = ENCODE_ITYPE_IMM (value); break; @@ -2195,8 +2230,8 @@ riscv_elf_relocate_section (bfd *output_bfd, bfd_vma relocation; bfd_reloc_status_type r = bfd_reloc_ok; const char *name = NULL; - bfd_vma off, ie_off; - bool unresolved_reloc, is_ie = false; + bfd_vma off, ie_off, desc_off; + bool unresolved_reloc, is_ie = false, is_desc = false; bfd_vma pc = sec_addr (input_section) + rel->r_offset; int r_type = ELFNN_R_TYPE (rel->r_info), tls_type; reloc_howto_type *howto = riscv_elf_rtype_to_howto (input_bfd, r_type); @@ -2499,6 +2534,7 @@ riscv_elf_relocate_section (bfd *output_bfd, case R_RISCV_NONE: case R_RISCV_RELAX: case R_RISCV_TPREL_ADD: + case R_RISCV_TLSDESC_CALL: case R_RISCV_COPY: case R_RISCV_JUMP_SLOT: case R_RISCV_RELATIVE: @@ -2820,6 +2856,15 @@ riscv_elf_relocate_section (bfd *output_bfd, relocation = dtpoff (info, relocation); break; + case R_RISCV_TLSDESC_LOAD_LO12: + case R_RISCV_TLSDESC_ADD_LO12: + if (riscv_record_pcrel_lo_reloc (&pcrel_relocs, relocation, rel, + input_section, info, howto, + contents)) + continue; + r = bfd_reloc_overflow; + break; + case R_RISCV_32: /* Non ABS symbol should be blocked in check_relocs. */ if (ARCH_SIZE > 32) @@ -2885,11 +2930,16 @@ riscv_elf_relocate_section (bfd *output_bfd, } break; + case R_RISCV_TLSDESC_HI20: + is_desc = true; + goto tls; + case R_RISCV_TLS_GOT_HI20: is_ie = true; - /* Fall through. */ + goto tls; case R_RISCV_TLS_GD_HI20: + tls: if (h != NULL) { off = h->got.offset; @@ -2902,12 +2952,16 @@ riscv_elf_relocate_section (bfd *output_bfd, } tls_type = _bfd_riscv_elf_tls_type (input_bfd, h, r_symndx); - BFD_ASSERT (tls_type & (GOT_TLS_IE | GOT_TLS_GD)); + BFD_ASSERT (tls_type & (GOT_TLS_IE | GOT_TLS_GD | GOT_TLSDESC)); /* If this symbol is referenced by both GD and IE TLS, the IE reference's GOT slot follows the GD reference's slots. */ ie_off = 0; if ((tls_type & GOT_TLS_GD) && (tls_type & GOT_TLS_IE)) - ie_off = TLS_GD_GOT_ENTRY_SIZE; + ie_off += TLS_GD_GOT_ENTRY_SIZE; + + desc_off = ie_off; + if (tls_type & GOT_TLS_IE) + desc_off += TLS_IE_GOT_ENTRY_SIZE; if ((off & 1) != 0) off &= ~1; @@ -2989,10 +3043,29 @@ riscv_elf_relocate_section (bfd *output_bfd, htab->elf.sgot->contents + off + ie_off); } } + + if (tls_type & GOT_TLSDESC) + { + /* TLSDESC is always handled by the dynamic linker and always need + * a relocation. */ + bfd_put_NN (output_bfd, 0, + htab->elf.sgot->contents + off + desc_off); + outrel.r_offset = sec_addr (htab->elf.sgot) + + off + desc_off; + outrel.r_addend = 0; + if (indx == 0) + outrel.r_addend = tlsdescoff (info, relocation); + outrel.r_info = ELFNN_R_INFO (indx, R_RISCV_TLSDESC); + riscv_elf_append_rela (output_bfd, htab->elf.srelgot, &outrel); + } } BFD_ASSERT (off < (bfd_vma) -2); - relocation = sec_addr (htab->elf.sgot) + off + (is_ie ? ie_off : 0); + relocation = sec_addr (htab->elf.sgot) + off; + if (is_ie) + relocation += ie_off; + else if (is_desc) + relocation += desc_off; if (!riscv_record_pcrel_hi_reloc (&pcrel_relocs, pc, relocation, r_type, false)) @@ -3212,7 +3285,7 @@ riscv_elf_finish_dynamic_symbol (bfd *output_bfd, } if (h->got.offset != (bfd_vma) -1 - && !(riscv_elf_hash_entry (h)->tls_type & (GOT_TLS_GD | GOT_TLS_IE)) + && !(riscv_elf_hash_entry (h)->tls_type & (GOT_TLS_GD | GOT_TLS_IE | GOT_TLSDESC)) && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, h)) { asection *sgot; diff --git a/ld/testsuite/ld-riscv-elf/tls.d b/ld/testsuite/ld-riscv-elf/tls.d index 0e2ab5683ad..5019383070c 100644 --- a/ld/testsuite/ld-riscv-elf/tls.d +++ b/ld/testsuite/ld-riscv-elf/tls.d @@ -2,13 +2,15 @@ #ld: --shared tmpdir/tlslib.so #readelf: -Wr -Relocation section '.rela.dyn' at offset 0x[0-9a-f]+ contains 5 entries: +Relocation section '.rela.dyn' at offset 0x[0-9a-f]+ contains 7 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_TLSDESC +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 +[0-9a-f]+ +[0-9a-f]+ R_RISCV_TLSDESC +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 diff --git a/ld/testsuite/ld-riscv-elf/tls.s b/ld/testsuite/ld-riscv-elf/tls.s index 79e9bc20374..7cbf5ea6c72 100644 --- a/ld/testsuite/ld-riscv-elf/tls.s +++ b/ld/testsuite/ld-riscv-elf/tls.s @@ -17,6 +17,13 @@ _start: la.tls.ie a0,sg1 add a0,a0,tp + /* GD (TLSDESC), global var */ +.desc1: + auipc a0, %tlsdesc_hi(sg1) + lw t0, %tlsdesc_load_lo(.desc1)(a0) + addi a0, a0, %tlsdesc_add_lo(.desc1) + jalr t0, t0, %tlsdesc_call(.desc1) + /* GD, local var */ la.tls.gd a0,sl1 call __tls_get_addr @@ -25,4 +32,11 @@ _start: la.tls.ie a0,sl1 add a0,a0,tp + /* GD (TLSDESC), local var */ +.desc2: + auipc a0, %tlsdesc_hi(sl1) + lw t0, %tlsdesc_load_lo(.desc2)(a0) + addi a0, a0, %tlsdesc_add_lo(.desc2) + jalr t0, t0, %tlsdesc_call(.desc2) + ret diff --git a/ld/testsuite/ld-riscv-elf/tlsbin.d b/ld/testsuite/ld-riscv-elf/tlsbin.d index 12a4d0ea703..16545a90639 100644 --- a/ld/testsuite/ld-riscv-elf/tlsbin.d +++ b/ld/testsuite/ld-riscv-elf/tlsbin.d @@ -2,6 +2,11 @@ #ld: -no-pie tmpdir/tlslib.so #readelf: -Wr +Relocation section '.rela.dyn' at offset 0x[0-9a-f]+ contains 2 entries: + +Offset +Info +Type +Symbol's Value +Symbol's Name \+ Addend +[0-9a-f]+ +[0-9a-f]+ R_RISCV_TLSDESC +4 +[0-9a-f]+ +[0-9a-f]+ R_RISCV_TLSDESC +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-9a-f]+ __tls_get_addr \+ 0 \ No newline at end of file