From patchwork Sat Oct 5 09:57:13 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lulu Cai X-Patchwork-Id: 98425 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 97F4D385EC3B for ; Sat, 5 Oct 2024 09:57:49 +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 B7002385EC0F for ; Sat, 5 Oct 2024 09:57:19 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org B7002385EC0F 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 B7002385EC0F 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=1728122243; cv=none; b=j+/ISKBG/mJmW1PwKpKX2bNEF7+Qu14Tz2t4Yg1ol/LFJGF+EcHCKsijpARD4kQL0SBf0ATZ/WrB6dF6YH8hHGHSnEImoOhhjH4zoXZ6bwrsXSC6cqLNEoKmVohamTxK0Mn8RAFnfmf4HqUCWp/rxUt182HHpEK3y/kW5FIxAJI= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1728122243; c=relaxed/simple; bh=n36Zl3Zqb7qFveTb0Q4kOjWJs/8kdvkfZoH9PyCFmxs=; h=From:To:Subject:Date:Message-Id:MIME-Version; b=q/N9ka2i1+6yRLUENOdROhk1JS6AvySHxhs+Jko+TKPC9EfUq+zPvMpu7tGIeXQh3ZMiWD8ATAgHyhB+tDZEERRe4tqgHcsoNipZnq+7p5N4vZ950AAFZiK+dc35LpxADZ1EfGCGgen4rpX6n0O5EtddnFJtTZLLMClIIH70weg= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from loongson.cn (unknown [10.2.6.5]) by gateway (Coremail) with SMTP id _____8DxfWt8DQFnVfUJAA--.13318S3; Sat, 05 Oct 2024 17:57:16 +0800 (CST) Received: from 5.5.5 (unknown [10.2.6.5]) by front1 (Coremail) with SMTP id qMiowMCxodZ7DQFnhYwbAA--.16477S4; Sat, 05 Oct 2024 17:57:16 +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] LoongArch: Check PC-relative relocations for shared libraries Date: Sat, 5 Oct 2024 17:57:13 +0800 Message-Id: <20241005095713.2253909-1-cailulu@loongson.cn> X-Mailer: git-send-email 2.39.3 MIME-Version: 1.0 X-CM-TRANSID: qMiowMCxodZ7DQFnhYwbAA--.16477S4 X-CM-SenderInfo: xfdlz3tox6z05rqj20fqof0/1tbiAgEBB2b-2qAMOQAAsT X-Coremail-Antispam: 1Uk129KBj93XoW3Jry7Cw1UJF13ArW7WFW3urX_yoWxGrWDp3 43Zr1rKF4IyFn7Wr9rGay5XFn8WFs7GFy2qasIqF1I9rsIqr97Xw10yrW3XFW3A3yUA34Y vr10qay3ZF98ZabCm3ZEXasCq-sJn29KB7ZKAUJUUUUr529EdanIXcx71UUUUU7KY7ZEXa sCq-sGcSsGvfJ3Ic02F40EFcxC0VAKzVAqx4xG6I80ebIjqfuFe4nvWSU5nxnvy29KBjDU 0xBIdaVrnRJUUUvEb4IE77IF4wAFF20E14v26r1j6r4UM7CY07I20VC2zVCF04k26cxKx2 IYs7xG6rWj6s0DM7CIcVAFz4kK6r1Y6r17M28lY4IEw2IIxxk0rwA2F7IY1VAKz4vEj48v e4kI8wA2z4x0Y4vE2Ix0cI8IcVAFwI0_Jr0_JF4l84ACjcxK6xIIjxv20xvEc7CjxVAFwI 0_Jr0_Gr1l84ACjcxK6I8E87Iv67AKxVW8JVWxJwA2z4x0Y4vEx4A2jsIEc7CjxVAFwI0_ Gr0_Gr1UM2kKe7AKxVWUXVWUAwAS0I0E0xvYzxvE52x082IY62kv0487Mc804VCY07AIYI kI8VC2zVCFFI0UMc02F40EFcxC0VAKzVAqx4xG6I80ewAv7VC0I7IYx2IY67AKxVWUXVWU AwAv7VC2z280aVAFwI0_Jr0_Gr1lOx8S6xCaFVCjc4AY6r1j6r4UM4x0Y48IcxkI7VAKI4 8JMxAIw28IcxkI7VAKI48JMxC20s026xCaFVCjc4AY6r1j6r4UMxCIbckI1I0E14v26r1Y 6r17MI8I3I0E5I8CrVAFwI0_Jr0_Jr4lx2IqxVCjr7xvwVAFwI0_JrI_JrWlx4CE17CEb7 AF67AKxVWUtVW8ZwCIc40Y0x0EwIxGrwCI42IY6xIIjxv20xvE14v26r1j6r1xMIIF0xvE 2Ix0cI8IcVCY1x0267AKxVWUJVW8JwCI42IY6xAIw20EY4v20xvaj40_Jr0_JF4lIxAIcV C2z280aVAFwI0_Jr0_Gr1lIxAIcVC2z280aVCY1x0267AKxVWUJVW8JbIYCTnIWIevJa73 UjIFyTuYvjxU2MKZDUUUU 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 externally defined or undefined 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. --- bfd/elfnn-loongarch.c | 21 +++++++++++++++ .../ld-loongarch-elf/bad_pcala_hi20.d | 5 ++++ .../ld-loongarch-elf/bad_pcala_hi20.s | 8 ++++++ .../ld-loongarch-elf/bad_pcrel20_s2.d | 5 ++++ .../ld-loongarch-elf/bad_pcrel20_s2.s | 8 ++++++ ld/testsuite/ld-loongarch-elf/data-plt.d | 5 ++++ .../ld-loongarch-elf/ld-loongarch-elf.exp | 27 +++---------------- 7 files changed, 55 insertions(+), 24 deletions(-) create mode 100644 ld/testsuite/ld-loongarch-elf/bad_pcala_hi20.d create mode 100644 ld/testsuite/ld-loongarch-elf/bad_pcala_hi20.s create mode 100644 ld/testsuite/ld-loongarch-elf/bad_pcrel20_s2.d create mode 100644 ld/testsuite/ld-loongarch-elf/bad_pcrel20_s2.s create mode 100644 ld/testsuite/ld-loongarch-elf/data-plt.d diff --git a/bfd/elfnn-loongarch.c b/bfd/elfnn-loongarch.c index 63c6fdffd6b..6392a325465 100644 --- a/bfd/elfnn-loongarch.c +++ b/bfd/elfnn-loongarch.c @@ -1079,6 +1079,19 @@ 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 + (defined and undefined) 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 +1109,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.d b/ld/testsuite/ld-loongarch-elf/bad_pcala_hi20.d new file mode 100644 index 00000000000..481700b01ff --- /dev/null +++ b/ld/testsuite/ld-loongarch-elf/bad_pcala_hi20.d @@ -0,0 +1,5 @@ +#name: PC-relative relocation making shared +#source: bad_pcala_hi20.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.s b/ld/testsuite/ld-loongarch-elf/bad_pcala_hi20.s new file mode 100644 index 00000000000..d8189e45370 --- /dev/null +++ b/ld/testsuite/ld-loongarch-elf/bad_pcala_hi20.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_pcrel20_s2.d b/ld/testsuite/ld-loongarch-elf/bad_pcrel20_s2.d new file mode 100644 index 00000000000..bd948a457a9 --- /dev/null +++ b/ld/testsuite/ld-loongarch-elf/bad_pcrel20_s2.d @@ -0,0 +1,5 @@ +#name: PC-relative relocation making shared +#source: bad_pcrel20_s2.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.s b/ld/testsuite/ld-loongarch-elf/bad_pcrel20_s2.s new file mode 100644 index 00000000000..39cedbd2743 --- /dev/null +++ b/ld/testsuite/ld-loongarch-elf/bad_pcrel20_s2.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/data-plt.d b/ld/testsuite/ld-loongarch-elf/data-plt.d new file mode 100644 index 00000000000..90e878d7b3f --- /dev/null +++ b/ld/testsuite/ld-loongarch-elf/data-plt.d @@ -0,0 +1,5 @@ +#name: data plt +#source: data-plt.s +#target: [check_shared_lib_support] +#ld: -shared +#error: .*: relocation R_LARCH_PCALA_HI20 against `a` can not be used when making a shared object; recompile with -fPIC diff --git a/ld/testsuite/ld-loongarch-elf/ld-loongarch-elf.exp b/ld/testsuite/ld-loongarch-elf/ld-loongarch-elf.exp index 6bef97c260f..115c8c22f19 100644 --- a/ld/testsuite/ld-loongarch-elf/ld-loongarch-elf.exp +++ b/ld/testsuite/ld-loongarch-elf/ld-loongarch-elf.exp @@ -73,30 +73,6 @@ if [istarget "loongarch64-*-*"] { ] } - # loongarch*-elf target do not support -shared option - if [check_shared_lib_support] { - run_ld_link_tests \ - [list \ - [list \ - "data plt" \ - "-shared" "" \ - "" \ - {data-plt.s} \ - {} \ - "data-plt.so" \ - ] \ - ] - - if [file exist "tmpdir/data-plt.so"] { - set objdump_output [run_host_cmd "objdump" "-d tmpdir/data-plt.so"] - if { [ regexp "" $objdump_output] } { - fail "data plt" - } { - pass "data plt" - } - } - } - run_ld_link_tests \ [list \ [list \ @@ -166,6 +142,9 @@ if [istarget "loongarch64-*-*"] { run_dump_test "relr-got-shared" run_dump_test "relr-text-shared" run_dump_test "abssym_shared" + run_dump_test "data-plt" + run_dump_test "bad_pcrel20_s2" + run_dump_test "bad_pcala_hi20" } if [check_pie_support] {