From patchwork Wed Oct 16 07:15:51 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lulu Cai X-Patchwork-Id: 98979 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 DAEF9385840B for ; Wed, 16 Oct 2024 07:16:41 +0000 (GMT) X-Original-To: binutils@sourceware.org Delivered-To: binutils@sourceware.org Received: from mail.loongson.cn (mail.loongson.cn [114.242.206.163]) by sourceware.org (Postfix) with ESMTP id 3265B3858CDA for ; Wed, 16 Oct 2024 07:16:06 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 3265B3858CDA Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=loongson.cn Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=loongson.cn ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 3265B3858CDA Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=114.242.206.163 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1729062971; cv=none; b=gANUAqpgE1vEy9yazjBOoBov/KcATj6yZq2vfZQAbVY2q2l8JZFb4lchfHMyXu9Vh5pOqacvOxquQ8AAc4DzoZjIMGw90afeurfo7eJVG3Pjvd9kaznAk3IAiEFV5YzWljlg8nhXRqj2Mv1v0+6YRDT3UgUyQZFngqmwWjF6We4= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1729062971; c=relaxed/simple; bh=pygIjSdilQYtMM+k7YPpRtfQJ7tA3NWxO7xZo8jDIKE=; h=From:To:Subject:Date:Message-Id:MIME-Version; b=I1rA+DV6G28NBKvwW2v/eReQWJsuyZKk/kD/rtw9pVsxKM/34hhVgZ9AeeJfh04Hc960Jb6Nr6G2yo6P8DNSLwyyaCpKfgkEv4fwhtrJXzKj/iRZtwq4zFDVhKqJEdFPgrONQSDMNyD7ESnQExPYYJ7YInkYg0JPEni52c6wmyY= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from loongson.cn (unknown [10.2.6.5]) by gateway (Coremail) with SMTP id _____8BxkugzaA9ndKUfAA--.45760S3; Wed, 16 Oct 2024 15:16:03 +0800 (CST) Received: from 5.5.5 (unknown [10.2.6.5]) by front1 (Coremail) with SMTP id qMiowMDxcNYyaA9nSFcsAA--.15934S4; Wed, 16 Oct 2024 15:16:03 +0800 (CST) From: Lulu Cai To: binutils@sourceware.org Cc: xuchenghua@loongson.cn, chenglulu@loongson.cn, mengqinggang@loongson.cn, xry111@xry111.site, i.swmail@xen0n.name, maskray@google.com, luweining@loongson.cn, hejinyang@loongson.cn, wangxin03@loongson.cn, wanglei@loongson.cn, Lulu Cai Subject: [PATCH v2] LoongArch: Check PC-relative relocations for shared libraries Date: Wed, 16 Oct 2024 15:15:51 +0800 Message-Id: <20241016071551.1346635-1-cailulu@loongson.cn> X-Mailer: git-send-email 2.39.3 MIME-Version: 1.0 X-CM-TRANSID: qMiowMDxcNYyaA9nSFcsAA--.15934S4 X-CM-SenderInfo: xfdlz3tox6z05rqj20fqof0/1tbiAQEMB2cOWn4PRwABsP X-Coremail-Antispam: 1Uk129KBj93XoWxtr1ftF17Wr47Xw1kCr48AFc_yoWfGw1kpw 1UZr1rKF4IyFn7Zr9rGa45XF1rWFs7CFy7tasxXF1I9rsaqrZ7Xw1IyrW3XFW3Cw45Zryr Zr1IqayfZFZ8AabCm3ZEXasCq-sJn29KB7ZKAUJUUUU8529EdanIXcx71UUUUU7KY7ZEXa sCq-sGcSsGvfJ3Ic02F40EFcxC0VAKzVAqx4xG6I80ebIjqfuFe4nvWSU5nxnvy29KBjDU 0xBIdaVrnRJUUUvIb4IE77IF4wAFF20E14v26r1j6r4UM7CY07I20VC2zVCF04k26cxKx2 IYs7xG6rWj6s0DM7CIcVAFz4kK6r1j6r18M28lY4IEw2IIxxk0rwA2F7IY1VAKz4vEj48v e4kI8wA2z4x0Y4vE2Ix0cI8IcVAFwI0_JFI_Gr1l84ACjcxK6xIIjxv20xvEc7CjxVAFwI 0_Gr0_Cr1l84ACjcxK6I8E87Iv67AKxVW8Jr0_Cr1UM28EF7xvwVC2z280aVCY1x0267AK xVW8Jr0_Cr1UM2AIxVAIcxkEcVAq07x20xvEncxIr21l57IF6xkI12xvs2x26I8E6xACxx 1l5I8CrVACY4xI64kE6c02F40Ex7xfMcIj6xIIjxv20xvE14v26r106r15McIj6I8E87Iv 67AKxVWUJVW8JwAm72CE4IkC6x0Yz7v_Jr0_Gr1lF7xvr2IYc2Ij64vIr41l42xK82IYc2 Ij64vIr41l4I8I3I0E4IkC6x0Yz7v_Jr0_Gr1l4IxYO2xFxVAFwI0_Jrv_JF1lx2IqxVAq x4xG67AKxVWUJVWUGwC20s026x8GjcxK67AKxVWUGVWUWwC2zVAF1VAY17CE14v26r1q6r 43MIIYrxkI7VAKI48JMIIF0xvE2Ix0cI8IcVAFwI0_Jr0_JF4lIxAIcVC0I7IYx2IY6xkF 7I0E14v26r1j6r4UMIIF0xvE42xK8VAvwI8IcIk0rVWUJVWUCwCI42IY6I8E87Iv67AKxV WUJVW8JwCI42IY6I8E87Iv6xkF7I0E14v26r1j6r4UYxBIdaVFxhVjvjDU0xZFpf9x07UW HqcUUUUU= X-Spam-Status: No, score=-13.1 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_STATUS, 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: , Errors-To: binutils-bounces~patchwork=sourceware.org@sourceware.org Building shared libraries should not be allowed for PC-relative relocations against external symbols. Currently LoongArch has no corresponding checks and silently generates wrong shared libraries. However, PC-relative relocations are allowed when used as a function call. --- Changes from v1: * Added .weak symbol test * Fixed data-plt test v1: https://sourceware.org/pipermail/binutils/2024-October/137098.html --- bfd/elfnn-loongarch.c | 20 +++++++++++++++++++ .../ld-loongarch-elf/bad_pcala_hi20_global.d | 5 +++++ .../ld-loongarch-elf/bad_pcala_hi20_global.s | 8 ++++++++ .../ld-loongarch-elf/bad_pcala_hi20_weak.d | 5 +++++ .../ld-loongarch-elf/bad_pcala_hi20_weak.s | 9 +++++++++ .../ld-loongarch-elf/bad_pcrel20_s2_global.d | 5 +++++ .../ld-loongarch-elf/bad_pcrel20_s2_global.s | 8 ++++++++ .../ld-loongarch-elf/bad_pcrel20_s2_weak.d | 5 +++++ .../ld-loongarch-elf/bad_pcrel20_s2_weak.s | 9 +++++++++ ld/testsuite/ld-loongarch-elf/data-plt.s | 1 + .../ld-loongarch-elf/ld-loongarch-elf.exp | 4 ++++ 11 files changed, 79 insertions(+) create mode 100644 ld/testsuite/ld-loongarch-elf/bad_pcala_hi20_global.d create mode 100644 ld/testsuite/ld-loongarch-elf/bad_pcala_hi20_global.s create mode 100644 ld/testsuite/ld-loongarch-elf/bad_pcala_hi20_weak.d create mode 100644 ld/testsuite/ld-loongarch-elf/bad_pcala_hi20_weak.s create mode 100644 ld/testsuite/ld-loongarch-elf/bad_pcrel20_s2_global.d create mode 100644 ld/testsuite/ld-loongarch-elf/bad_pcrel20_s2_global.s create mode 100644 ld/testsuite/ld-loongarch-elf/bad_pcrel20_s2_weak.d create mode 100644 ld/testsuite/ld-loongarch-elf/bad_pcrel20_s2_weak.s diff --git a/bfd/elfnn-loongarch.c b/bfd/elfnn-loongarch.c index 63c6fdffd6b..4d215a85c86 100644 --- a/bfd/elfnn-loongarch.c +++ b/bfd/elfnn-loongarch.c @@ -1079,6 +1079,18 @@ loongarch_elf_check_relocs (bfd *abfd, struct bfd_link_info *info, h->non_got_ref = 1; break; + /* Since shared library global symbols interpose, any + PC-relative relocations against external symbols + should not be used to build shared libraries. */ + case R_LARCH_PCREL20_S2: + if (bfd_link_pic (info) + && (sec->flags & SEC_ALLOC) != 0 + && (sec->flags & SEC_READONLY) != 0 + && ! LARCH_REF_LOCAL (info, h)) + return bad_static_reloc (abfd, rel, sec, r_type, h, NULL); + + break; + /* For normal cmodel, pcalau12i + addi.d/w used to data. For first version medium cmodel, pcalau12i + jirl are used to function call, it need to creat PLT entry for STT_FUNC and @@ -1096,6 +1108,14 @@ loongarch_elf_check_relocs (bfd *abfd, struct bfd_link_info *info, h->pointer_equality_needed = 1; } + /* PC-relative relocations are allowed when calling a function. */ + if (h != NULL && !h->needs_plt + && bfd_link_pic (info) + && (sec->flags & SEC_ALLOC) != 0 + && (sec->flags & SEC_READONLY) != 0 + && ! LARCH_REF_LOCAL (info, h)) + return bad_static_reloc (abfd, rel, sec, r_type, h, NULL); + break; case R_LARCH_B16: diff --git a/ld/testsuite/ld-loongarch-elf/bad_pcala_hi20_global.d b/ld/testsuite/ld-loongarch-elf/bad_pcala_hi20_global.d new file mode 100644 index 00000000000..6ecefd1d48a --- /dev/null +++ b/ld/testsuite/ld-loongarch-elf/bad_pcala_hi20_global.d @@ -0,0 +1,5 @@ +#name: PC-relative relocation making shared +#source: bad_pcala_hi20_global.s +#target: [check_shared_lib_support] +#ld: -shared --defsym global_a=0x10 --defsym global_b=0x20 +#error: .*: relocation R_LARCH_PCALA_HI20 against `global_b` can not be used when making a shared object; recompile with -fPIC diff --git a/ld/testsuite/ld-loongarch-elf/bad_pcala_hi20_global.s b/ld/testsuite/ld-loongarch-elf/bad_pcala_hi20_global.s new file mode 100644 index 00000000000..d8189e45370 --- /dev/null +++ b/ld/testsuite/ld-loongarch-elf/bad_pcala_hi20_global.s @@ -0,0 +1,8 @@ + .hidden global_a + .text + .align 2 +main: + # Symbols defined .hidden are bound local and + # the linker should differenciate them. + la.pcrel $a0, global_a + la.pcrel $a0, global_b diff --git a/ld/testsuite/ld-loongarch-elf/bad_pcala_hi20_weak.d b/ld/testsuite/ld-loongarch-elf/bad_pcala_hi20_weak.d new file mode 100644 index 00000000000..cefc42cf89f --- /dev/null +++ b/ld/testsuite/ld-loongarch-elf/bad_pcala_hi20_weak.d @@ -0,0 +1,5 @@ +#name: PC-relative relocation making shared +#source: bad_pcala_hi20_weak.s +#target: [check_shared_lib_support] +#ld: -shared --defsym global_a=0x10 --defsym global_b=0x20 +#error: .*: relocation R_LARCH_PCALA_HI20 against `global_b` can not be used when making a shared object; recompile with -fPIC diff --git a/ld/testsuite/ld-loongarch-elf/bad_pcala_hi20_weak.s b/ld/testsuite/ld-loongarch-elf/bad_pcala_hi20_weak.s new file mode 100644 index 00000000000..73c6ec5f9c3 --- /dev/null +++ b/ld/testsuite/ld-loongarch-elf/bad_pcala_hi20_weak.s @@ -0,0 +1,9 @@ + .hidden global_a + .weak global_b + .text + .align 2 +main: + # Symbols defined .hidden are bound local and + # the linker should differenciate them. + la.pcrel $a0, global_a + la.pcrel $a0, global_b diff --git a/ld/testsuite/ld-loongarch-elf/bad_pcrel20_s2_global.d b/ld/testsuite/ld-loongarch-elf/bad_pcrel20_s2_global.d new file mode 100644 index 00000000000..8e063dfb0ec --- /dev/null +++ b/ld/testsuite/ld-loongarch-elf/bad_pcrel20_s2_global.d @@ -0,0 +1,5 @@ +#name: PC-relative relocation making shared +#source: bad_pcrel20_s2_global.s +#target: [check_shared_lib_support] +#ld: -shared --defsym global_a=0x10 --defsym global_b=0x20 +#error: .*: relocation R_LARCH_PCREL20_S2 against `global_b` can not be used when making a shared object; recompile with -fPIC diff --git a/ld/testsuite/ld-loongarch-elf/bad_pcrel20_s2_global.s b/ld/testsuite/ld-loongarch-elf/bad_pcrel20_s2_global.s new file mode 100644 index 00000000000..39cedbd2743 --- /dev/null +++ b/ld/testsuite/ld-loongarch-elf/bad_pcrel20_s2_global.s @@ -0,0 +1,8 @@ + .hidden global_a + .text + .align 2 +main: + # Symbols defined .hidden are bound local and + # the linker should differenciate them. + pcaddi $a0, %pcrel_20(global_a) + pcaddi $a0, %pcrel_20(global_b) diff --git a/ld/testsuite/ld-loongarch-elf/bad_pcrel20_s2_weak.d b/ld/testsuite/ld-loongarch-elf/bad_pcrel20_s2_weak.d new file mode 100644 index 00000000000..605df24988a --- /dev/null +++ b/ld/testsuite/ld-loongarch-elf/bad_pcrel20_s2_weak.d @@ -0,0 +1,5 @@ +#name: PC-relative relocation making shared +#source: bad_pcrel20_s2_weak.s +#target: [check_shared_lib_support] +#ld: -shared --defsym global_a=0x10 --defsym global_b=0x20 +#error: .*: relocation R_LARCH_PCREL20_S2 against `global_b` can not be used when making a shared object; recompile with -fPIC diff --git a/ld/testsuite/ld-loongarch-elf/bad_pcrel20_s2_weak.s b/ld/testsuite/ld-loongarch-elf/bad_pcrel20_s2_weak.s new file mode 100644 index 00000000000..82faa5bbf86 --- /dev/null +++ b/ld/testsuite/ld-loongarch-elf/bad_pcrel20_s2_weak.s @@ -0,0 +1,9 @@ + .hidden global_a + .weak global_b + .text + .align 2 +main: + # Symbols defined .hidden are bound local and + # the linker should differenciate them. + pcaddi $a0, %pcrel_20(global_a) + pcaddi $a0, %pcrel_20(global_b) diff --git a/ld/testsuite/ld-loongarch-elf/data-plt.s b/ld/testsuite/ld-loongarch-elf/data-plt.s index faff052c628..6c1a8eac35c 100644 --- a/ld/testsuite/ld-loongarch-elf/data-plt.s +++ b/ld/testsuite/ld-loongarch-elf/data-plt.s @@ -2,6 +2,7 @@ # R_LARCH_PCALA_HI20 only need to generate PLT entry for function symbols. .text .globl a + .hidden a .data .align 2 diff --git a/ld/testsuite/ld-loongarch-elf/ld-loongarch-elf.exp b/ld/testsuite/ld-loongarch-elf/ld-loongarch-elf.exp index 6bef97c260f..4e0068d9f89 100644 --- a/ld/testsuite/ld-loongarch-elf/ld-loongarch-elf.exp +++ b/ld/testsuite/ld-loongarch-elf/ld-loongarch-elf.exp @@ -166,6 +166,10 @@ if [istarget "loongarch64-*-*"] { run_dump_test "relr-got-shared" run_dump_test "relr-text-shared" run_dump_test "abssym_shared" + run_dump_test "bad_pcala_hi20_global" + run_dump_test "bad_pcala_hi20_weak" + run_dump_test "bad_pcrel20_s2_global" + run_dump_test "bad_pcrel20_s2_weak" } if [check_pie_support] {