From patchwork Sat Mar 25 00:41:13 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nelson Chu X-Patchwork-Id: 74796 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp118088vqo; Fri, 24 Mar 2023 17:41:29 -0700 (PDT) X-Google-Smtp-Source: AKy350Z3OObQy238170OV+ur5yoV8MGKeaXe2LkVNMD3WZpH65kDtgIn2nhn63evhY6fjgo1TWwh X-Received: by 2002:a17:906:a15a:b0:933:1967:a98a with SMTP id bu26-20020a170906a15a00b009331967a98amr4585534ejb.17.1679704889773; Fri, 24 Mar 2023 17:41:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1679704889; cv=none; d=google.com; s=arc-20160816; b=J7ROu2CK9W/fmtOoFCe9IivxbxV2aDBqRSNIZNXyiFvXnD7GDZfR1X0Ym9ELlbrPSt gNv21hmW7kqH9wp37/RoAtoWBQ/MQ58xsc1x8dcXmF8xggUZPfZODid+01nmVLZOfc4Z 1yFJ2gSSklNvjGF2NQ+pS8PESYgRX2K4wBJ7FJzxhQwMy5b1uqZecGh2Bva8gF6rG2Pl GeHvC9IW/zF1jwCV+qTwkTpLSxnbyheaFVf4b5M2lK4RimiVDf2Ip8cn9H70JPgBFAlE VFl9+rYNvDVfOKy7MagcQeV+8xZTv8heMg5Cud6DR4l/RZaMLmOwQT0eamMThPTvItDy m3sQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender: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:dmarc-filter:delivered-to; bh=L9hJPvquZhHOl73U10VGW4fcLpi+oRLCYjWTD/VVouI=; b=wg6LlrngSv36zK5u3BDVLzlELhrhmOMO1aOiwP8pAbFCPLJUvuYPIdRIdaIt+FCF2G PH26CasY33i5F9z4EWwFLjDj9OtJ2fh7PNKOain4pZVJ+HwT73EaxMuaFDUZTvpdHDd5 3Dxk4/lbm3aCOzG3Ts2+qn2JZR+rOLL3y+3KwkxzldbU0gQmWdVhPbcr7zJDJZ0WjSpi JVaYLqNIdo7iJVzggujI7SGS8Lcuv6eV8iOCr15ouTcnzzkOiWcV0VaQKKgxyJJGVgHM QYwbQI8GX9ofPOloYH5PZDS6LS8HPs8AViud9IUQJIgZS8yKY5gf+PpTiSAOh9pAGObc L4zw== ARC-Authentication-Results: i=1; mx.google.com; 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" Received: from sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id cr2-20020a170906d54200b008d6d20d1920si20296536ejc.35.2023.03.24.17.41.29 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 Mar 2023 17:41:29 -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; 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" Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 1B89D38708C1 for ; Sat, 25 Mar 2023 00:41:25 +0000 (GMT) X-Original-To: binutils@sourceware.org Delivered-To: binutils@sourceware.org Received: from NelsondeMBP.localdomain (114-25-107-156.dynamic-ip.hinet.net [114.25.107.156]) by sourceware.org (Postfix) with ESMTP id 22D693858CDA for ; Sat, 25 Mar 2023 00:41:18 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 22D693858CDA Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=rivosinc.com Authentication-Results: sourceware.org; spf=none smtp.mailfrom=NelsondeMBP.localdomain Received: by NelsondeMBP.localdomain (Postfix, from userid 501) id D27E5A59E06; Sat, 25 Mar 2023 08:41:16 +0800 (CST) From: Nelson Chu To: binutils@sourceware.org, jim.wilson.gcc@gmail.com, palmer@dabbelt.com Cc: nelson@rivosinc.com, Palmer Dabbelt Subject: [PATCH 3/3] RISC-V: PR28789, Reject R_RISCV_PCREL relocations with ABS symbol in PIC/PIE. Date: Sat, 25 Mar 2023 08:41:13 +0800 Message-Id: <20230325004113.22673-3-nelson@rivosinc.com> X-Mailer: git-send-email 2.37.1 (Apple Git-137.1) In-Reply-To: <20230325004113.22673-1-nelson@rivosinc.com> References: <20230325004113.22673-1-nelson@rivosinc.com> MIME-Version: 1.0 X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00, GIT_PATCH_0, HEADER_FROM_DIFFERENT_DOMAINS, KAM_DMARC_STATUS, KAM_LAZY_DOMAIN_SECURITY, KAM_STOCKGEN, KHOP_HELO_FCRDNS, NO_DNS_FOR_FROM, PDS_RDNS_DYNAMIC_FP, RCVD_IN_BARRACUDACENTRAL, RCVD_IN_PBL, RDNS_DYNAMIC, SPF_HELO_NONE, SPF_NONE, 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.29 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 Sender: "Binutils" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1761298234263418226?= X-GMAIL-MSGID: =?utf-8?q?1761298234263418226?= From: Palmer Dabbelt The non-preemptible SHN_ABS symbol with a pc-relative relocation should be disallowed when generating shared object (pic and pie). Generally, the following cases, which refer to pr25749, will cause a symbol be non-preemptible, * -pie, or -shared with -symbolic * STV_HIDDEN, STV_INTERNAL, STV_PROTECTED * Have dynamic symbol table, but without the symbol * VER_NDX_LOCAL However, PCREL_HI20/LO12 relocs are always bind locally when generating shared object, so not only the non-preemptible absolute symbol need to be disallowed, all absolute symbol references need but except that they are defined in linker script. If we also disallow the absolute symbol in linker script, then the glibc-linux toolchain build failed, so regard them as pc-relative symbols, just like what x86 did. Maybe we should add this check for all pc-relative relocations, rather than just handle in R_RISCV_PCREL relocs. Ideally, since the value of SHN_ABS symbol is a constant, only S - A relocations should be allowed in the shared object, so only BFD_RELOC_8/16/32/64 are allowed, which means R_RISCV_32/R_RISCV_64. bfd/ PR 28789 * elfnn-riscv.c (riscv_elf_check_relocs): The absolute symbol cannot be referneced with pc-relative relocation when generating shared object. ld/ PR 28789 * ld/testsuite/ld-riscv-elf/ld-riscv-elf.exp: Updated. * ld/testsuite/ld-riscv-elf/pcrel-reloc*: New testcases. --- bfd/elfnn-riscv.c | 41 +++++++++++++++++++ ld/testsuite/ld-riscv-elf/ld-riscv-elf.exp | 7 ++++ .../ld-riscv-elf/pcrel-reloc-abs-nopie.d | 14 +++++++ .../ld-riscv-elf/pcrel-reloc-abs-pie.d | 5 +++ ld/testsuite/ld-riscv-elf/pcrel-reloc-abs.s | 2 + .../ld-riscv-elf/pcrel-reloc-rel-nopie.d | 14 +++++++ .../ld-riscv-elf/pcrel-reloc-rel-pie.d | 14 +++++++ ld/testsuite/ld-riscv-elf/pcrel-reloc-rel.s | 9 ++++ ld/testsuite/ld-riscv-elf/pcrel-reloc.s | 5 +++ 9 files changed, 111 insertions(+) create mode 100644 ld/testsuite/ld-riscv-elf/pcrel-reloc-abs-nopie.d create mode 100644 ld/testsuite/ld-riscv-elf/pcrel-reloc-abs-pie.d create mode 100644 ld/testsuite/ld-riscv-elf/pcrel-reloc-abs.s create mode 100644 ld/testsuite/ld-riscv-elf/pcrel-reloc-rel-nopie.d create mode 100644 ld/testsuite/ld-riscv-elf/pcrel-reloc-rel-pie.d create mode 100644 ld/testsuite/ld-riscv-elf/pcrel-reloc-rel.s create mode 100644 ld/testsuite/ld-riscv-elf/pcrel-reloc.s diff --git a/bfd/elfnn-riscv.c b/bfd/elfnn-riscv.c index 00f034a6751..0dd9b27c8ae 100644 --- a/bfd/elfnn-riscv.c +++ b/bfd/elfnn-riscv.c @@ -862,6 +862,47 @@ riscv_elf_check_relocs (bfd *abfd, struct bfd_link_info *info, ifunc symbol. */ h->plt.refcount += 1; } + + /* The non-preemptible absolute symbol shouldn't be referneced with + pc-relative relocation when generating shared object. However, + PCREL_HI20/LO12 relocs are always bind locally when generating + shared object, so all absolute symbol referenced need to be + disallowed, except they are defined in linker script. + + Maybe we should add this check for all pc-relative relocations, + please see pr28789 and pr25749 for details. */ + if (bfd_link_pic (info) + /* (h == NULL || SYMBOL_REFERENCES_LOCAL (info, h)) */ + && is_abs_symbol) + { + if (h != NULL && (h)->root.ldscript_def) + /* Disallow the absolute symbol defined in linker script here + will cause the glibc-linux toolchain build failed, so regard + them as pc-relative symbols, just like what x86 did. */ + ; + else + { + const char *name; + if (h->root.root.string) + name = h->root.root.string; + else + { + Elf_Internal_Sym *sym; + sym = bfd_sym_from_r_symndx (&htab->elf.sym_cache, abfd, + r_symndx); + name = bfd_elf_sym_name (abfd, symtab_hdr, sym, NULL); + } + + reloc_howto_type *r_t = + riscv_elf_rtype_to_howto (abfd, r_type); + _bfd_error_handler + (_("%pB: relocation %s against absolute symbol `%s' can " + "not be used when making a shared object"), + abfd, r_t ? r_t->name : _(""), name); + bfd_set_error (bfd_error_bad_value); + return false; + } + } /* Fall through. */ case R_RISCV_JAL: diff --git a/ld/testsuite/ld-riscv-elf/ld-riscv-elf.exp b/ld/testsuite/ld-riscv-elf/ld-riscv-elf.exp index 1b2a5ce2cb2..43572c5286b 100644 --- a/ld/testsuite/ld-riscv-elf/ld-riscv-elf.exp +++ b/ld/testsuite/ld-riscv-elf/ld-riscv-elf.exp @@ -308,4 +308,11 @@ if [istarget "riscv*-*-*"] { run_dump_test "ifunc-seperate-plt-pic" run_dump_test "ifunc-seperate-pcrel-pie" run_dump_test "ifunc-seperate-pcrel-pic" + + # Tests related to mixing medany code into position-independent targets, + # where it's not always possible to generate correct addressing sequences. + run_dump_test "pcrel-reloc-rel-nopie" + run_dump_test "pcrel-reloc-rel-pie" + run_dump_test "pcrel-reloc-abs-nopie" + run_dump_test "pcrel-reloc-abs-pie" } diff --git a/ld/testsuite/ld-riscv-elf/pcrel-reloc-abs-nopie.d b/ld/testsuite/ld-riscv-elf/pcrel-reloc-abs-nopie.d new file mode 100644 index 00000000000..54026388b7a --- /dev/null +++ b/ld/testsuite/ld-riscv-elf/pcrel-reloc-abs-nopie.d @@ -0,0 +1,14 @@ +#source: pcrel-reloc.s +#source: pcrel-reloc-abs.s +#as: -march=rv64i -mabi=lp64 +#ld: -melf64lriscv --no-pie --no-relax +#objdump: -d + +.*:[ ]+file format .* + +Disassembly of section \.text: + +[0-9a-f]+ <_start>: +.*auipc.* +.*lw.*# [0-9a-f]* +#pass diff --git a/ld/testsuite/ld-riscv-elf/pcrel-reloc-abs-pie.d b/ld/testsuite/ld-riscv-elf/pcrel-reloc-abs-pie.d new file mode 100644 index 00000000000..7f5eaa321f7 --- /dev/null +++ b/ld/testsuite/ld-riscv-elf/pcrel-reloc-abs-pie.d @@ -0,0 +1,5 @@ +#source: pcrel-reloc.s +#source: pcrel-reloc-abs.s +#as: -march=rv64i -mabi=lp64 +#ld: -melf64lriscv --pie --no-relax +#error: .*relocation R_RISCV_PCREL_HI20 against absolute symbol `sym' can not be used when making a shared objec.*t diff --git a/ld/testsuite/ld-riscv-elf/pcrel-reloc-abs.s b/ld/testsuite/ld-riscv-elf/pcrel-reloc-abs.s new file mode 100644 index 00000000000..1df32a1a3fb --- /dev/null +++ b/ld/testsuite/ld-riscv-elf/pcrel-reloc-abs.s @@ -0,0 +1,2 @@ +.global sym +.set sym,0x8000 diff --git a/ld/testsuite/ld-riscv-elf/pcrel-reloc-rel-nopie.d b/ld/testsuite/ld-riscv-elf/pcrel-reloc-rel-nopie.d new file mode 100644 index 00000000000..ab2a3774cdd --- /dev/null +++ b/ld/testsuite/ld-riscv-elf/pcrel-reloc-rel-nopie.d @@ -0,0 +1,14 @@ +#source: pcrel-reloc.s +#source: pcrel-reloc-rel.s +#as: -march=rv64i -mabi=lp64 +#ld: -melf64lriscv --no-pie --no-relax +#objdump: -d + +.*:[ ]+file format .* + +Disassembly of section \.text: + +[0-9a-f]+ <_start>: +.*auipc.* +.*lw.*# [0-9a-f]* +#pass diff --git a/ld/testsuite/ld-riscv-elf/pcrel-reloc-rel-pie.d b/ld/testsuite/ld-riscv-elf/pcrel-reloc-rel-pie.d new file mode 100644 index 00000000000..aec612d4d2c --- /dev/null +++ b/ld/testsuite/ld-riscv-elf/pcrel-reloc-rel-pie.d @@ -0,0 +1,14 @@ +#source: pcrel-reloc.s +#source: pcrel-reloc-rel.s +#as: -march=rv64i -mabi=lp64 +#ld: -melf64lriscv --pie --no-relax +#objdump: -d + +.*:[ ]+file format .* + +Disassembly of section \.text: + +[0-9a-f]+ <_start>: +.*auipc.* +.*lw.*# [0-9a-f]* +#pass diff --git a/ld/testsuite/ld-riscv-elf/pcrel-reloc-rel.s b/ld/testsuite/ld-riscv-elf/pcrel-reloc-rel.s new file mode 100644 index 00000000000..fb0e6c09f22 --- /dev/null +++ b/ld/testsuite/ld-riscv-elf/pcrel-reloc-rel.s @@ -0,0 +1,9 @@ +.data +# Makes sure "sym" doesn't end up at the beginning of ".data", as that makes it +# tough to then later detect it from scripts. +.global buf +buf: + .fill 8192, 4, 1 +.global sym +sym: + .fill 8192, 4, 2 diff --git a/ld/testsuite/ld-riscv-elf/pcrel-reloc.s b/ld/testsuite/ld-riscv-elf/pcrel-reloc.s new file mode 100644 index 00000000000..db2103bafd1 --- /dev/null +++ b/ld/testsuite/ld-riscv-elf/pcrel-reloc.s @@ -0,0 +1,5 @@ +.text +.global _start +_start: + auipc t0, %pcrel_hi(sym) + lw t0, %pcrel_lo(_start)(t0)