From patchwork Fri Nov 29 08:29:39 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lulu Cai X-Patchwork-Id: 102079 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 EEFF43858D28 for ; Fri, 29 Nov 2024 08:30:33 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org EEFF43858D28 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 C88F03858D20 for ; Fri, 29 Nov 2024 08:29:46 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org C88F03858D20 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 C88F03858D20 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=1732868987; cv=none; b=uPT1ttbmrG1vXKx4IlbrJ8/975GYksmoPMVyWSzt7foAh0t3hDsBDfqHwra7gJkmFzPQkXsehSiMPLoJpt6KkjxMNgBxlRYAIlFSp94i7c9P5VNrd8pJuiDa46dPe4lfOGQAZL5iFC4Du4UEORx+Z8BybuTjul0cS9dSbNYtApY= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1732868987; c=relaxed/simple; bh=Kj/PjQaoQ2x4VwmCGgdfA7Le2cQDiZa9CZJY54dx5hE=; h=From:To:Subject:Date:Message-Id:MIME-Version; b=UD0B0yZ2gI9m1n17DIJMBRDfDNj0RO88axXtyK7boNTB16O/Tm5K5DDXVpfhUGgNWxJxh30IZpugosORiZHY1ixMxRjyU1/Vr5COTwyXGn6KiDsw+wXUP/OUIOd0C3yNIhbu+gWmTDHUOjiJanm1Q0wyRVAmAV2s9odHQIAfkRc= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C88F03858D20 Received: from loongson.cn (unknown [10.2.6.5]) by gateway (Coremail) with SMTP id _____8CxMK94e0lnsW5LAA--.57624S3; Fri, 29 Nov 2024 16:29:44 +0800 (CST) Received: from 5.. (unknown [10.2.6.5]) by front1 (Coremail) with SMTP id qMiowMBxrsJ1e0lnyL1sAA--.17703S4; Fri, 29 Nov 2024 16:29:41 +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, i@maskray.me, luweining@loongson.cn, hejinyang@loongson.cn, Lulu Cai Subject: [PATCH v3] LoongArch: Fix the infinite loop caused by calling undefweak symbol Date: Fri, 29 Nov 2024 16:29:39 +0800 Message-Id: <20241129082939.1510203-1-cailulu@loongson.cn> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-CM-TRANSID: qMiowMBxrsJ1e0lnyL1sAA--.17703S4 X-CM-SenderInfo: xfdlz3tox6z05rqj20fqof0/1tbiAQEQB2dI8j8JewAAsH X-Coremail-Antispam: 1Uk129KBj93XoWxtFWfKF17GF4UCFWDtFyrGrX_yoW7ZF1xpr yDA3yakF4fJFnruF93Ca45Wrs8Wr97WFyIgFZ3JF1F9rs3Xry8Xwn2yrWaqFW5Aa1UJw1j qw1Iva45uF4DAwcCm3ZEXasCq-sJn29KB7ZKAUJUUUU5529EdanIXcx71UUUUU7KY7ZEXa sCq-sGcSsGvfJ3Ic02F40EFcxC0VAKzVAqx4xG6I80ebIjqfuFe4nvWSU5nxnvy29KBjDU 0xBIdaVrnRJUUUkFb4IE77IF4wAFF20E14v26r1j6r4UM7CY07I20VC2zVCF04k26cxKx2 IYs7xG6rWj6s0DM7CIcVAFz4kK6r1j6r18M28lY4IEw2IIxxk0rwA2F7IY1VAKz4vEj48v e4kI8wA2z4x0Y4vE2Ix0cI8IcVAFwI0_Gr0_Xr1l84ACjcxK6xIIjxv20xvEc7CjxVAFwI 0_Gr0_Cr1l84ACjcxK6I8E87Iv67AKxVW8Jr0_Cr1UM28EF7xvwVC2z280aVCY1x0267AK xVW8Jr0_Cr1UM2AIxVAIcxkEcVAq07x20xvEncxIr21l57IF6xkI12xvs2x26I8E6xACxx 1l5I8CrVACY4xI64kE6c02F40Ex7xfMcIj6xIIjxv20xvE14v26r1Y6r17McIj6I8E87Iv 67AKxVWUJVW8JwAm72CE4IkC6x0Yz7v_Jr0_Gr1lF7xvr2IYc2Ij64vIr41l42xK82IYc2 Ij64vIr41l4I8I3I0E4IkC6x0Yz7v_Jr0_Gr1lx2IqxVAqx4xG67AKxVWUJVWUGwC20s02 6x8GjcxK67AKxVWUGVWUWwC2zVAF1VAY17CE14v26r1q6r43MIIYrxkI7VAKI48JMIIF0x vE2Ix0cI8IcVAFwI0_Jr0_JF4lIxAIcVC0I7IYx2IY6xkF7I0E14v26r1j6r4UMIIF0xvE 42xK8VAvwI8IcIk0rVWUJVWUCwCI42IY6I8E87Iv67AKxVWUJVW8JwCI42IY6I8E87Iv6x kF7I0E14v26r1j6r4UYxBIdaVFxhVjvjDU0xZFpf9x07UNvtZUUUUU= 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 The undefweak symbol value of non-default visibility is 0 and does not use plt entry, and will not be relocated in the relocate_secion function. As a result, an infinite loop is generated because bl %plt(sym) => bl 0. Fix this by converting the call into a jump address 0. --- Changes in v3: - The call instruction is not deleted but converted to jump address 0. - Add b %plt(sym) => jirl $zero,$zero,0. - Convert in relocate_section instead of relax_section. Changes in v2: - Pass @var{again} to loongarch_relax_call_undefweak to continue the next relax. v2: https://sourceware.org/pipermail/binutils/2024-November/137744.html Changes in v1: - Convert undefweak calls that do not use plt to nop or delete them. v1: https://sourceware.org/pipermail/binutils/2024-November/137701.html --- bfd/elfnn-loongarch.c | 39 +++++++++++++++++++ .../ld-loongarch-elf/call_undefweak.d | 26 +++++++++++++ .../ld-loongarch-elf/call_undefweak.s | 33 ++++++++++++++++ .../ld-loongarch-elf/ld-loongarch-elf.exp | 11 ++++++ 4 files changed, 109 insertions(+) create mode 100644 ld/testsuite/ld-loongarch-elf/call_undefweak.d create mode 100644 ld/testsuite/ld-loongarch-elf/call_undefweak.s diff --git a/bfd/elfnn-loongarch.c b/bfd/elfnn-loongarch.c index 8189a23a3a9..7451153270f 100644 --- a/bfd/elfnn-loongarch.c +++ b/bfd/elfnn-loongarch.c @@ -222,6 +222,10 @@ loongarch_elf_new_section_hook (bfd *abfd, asection *sec) || (R_TYPE) == R_LARCH_TLS_LE64_LO20 \ || (R_TYPE) == R_LARCH_TLS_LE64_HI12) +#define IS_CALL_RELOC(R_TYPE) \ + ((R_TYPE) == R_LARCH_B26 \ + ||(R_TYPE) == R_LARCH_CALL36) + /* If TLS GD/IE need dynamic relocations, INDX will be the dynamic indx, and set NEED_RELOC to true used in allocate_dynrelocs and loongarch_elf_relocate_section for TLS GD/IE. */ @@ -4015,9 +4019,44 @@ loongarch_elf_relocate_section (bfd *output_bfd, struct bfd_link_info *info, case R_LARCH_B26: case R_LARCH_CALL36: unresolved_reloc = false; + bool via_plt = + plt != NULL && h != NULL && h->plt.offset != (bfd_vma) - 1; + if (is_undefweak) { relocation = 0; + + /* A call to an undefined weak symbol is converted to 0. */ + if (!via_plt && IS_CALL_RELOC (r_type)) + { + /* call36 fn1 => pcaddu18i $ra,0+jirl $ra,$zero,0 + tail36 $t0,fn1 => pcaddi18i $t0,0+jirl $zero,$zero,0 */ + if (R_LARCH_CALL36 == r_type) + { + uint32_t jirl = bfd_get (32, input_bfd, + contents + rel->r_offset + 4); + uint32_t rd = LARCH_GET_RD (jirl); + jirl = LARCH_OP_JIRL | rd; + + bfd_put (32, input_bfd, jirl, + contents + rel->r_offset + 4); + } + else + { + uint32_t b_bl = bfd_get (32, input_bfd, + contents + rel->r_offset); + /* b %plt(fn1) => jirl $zero,zero,0. */ + if (LARCH_INSN_B (b_bl)) + bfd_put (32, input_bfd, LARCH_OP_JIRL, + contents + rel->r_offset); + else + /* bl %plt(fn1) => jirl $ra,zero,0. */ + bfd_put (32, input_bfd, LARCH_OP_JIRL | 0x1, + contents + rel->r_offset); + } + r = bfd_reloc_continue; + break; + } } if (resolved_local) diff --git a/ld/testsuite/ld-loongarch-elf/call_undefweak.d b/ld/testsuite/ld-loongarch-elf/call_undefweak.d new file mode 100644 index 00000000000..4761651817c --- /dev/null +++ b/ld/testsuite/ld-loongarch-elf/call_undefweak.d @@ -0,0 +1,26 @@ +#... +Disassembly of section \.plt: +#... +0+1200004d0 : + 1200004d0: 1c00010f pcaddu12i \$t3, 8 + 1200004d4: 28ed01ef ld.d \$t3, \$t3, -1216 + 1200004d8: 4c0001ed jirl \$t1, \$t3, 0 + 1200004dc: 03400000 nop + +Disassembly of section \.text: +#... +0+120000668
: + 120000668: 4c000000 jr \$zero + 12000066c: 53fe67ff b -412 # 1200004d0 + 120000670: 4c000001 jirl \$ra, \$zero, 0 + 120000674: 57fe5fff bl -420 # 1200004d0 + +0+120000678 : + 120000678: 1e000001 pcaddu18i \$ra, 0 + 12000067c: 4c000001 jirl \$ra, \$zero, 0 + 120000680: 1e000001 pcaddu18i \$ra, 0 + 120000684: 4ffe5021 jirl \$ra, \$ra, -432 + 120000688: 1e00000c pcaddu18i \$t0, 0 + 12000068c: 4c000000 jr \$zero + 120000690: 1e00000c pcaddu18i \$t0, 0 + 120000694: 4ffe4180 jirl \$zero, \$t0, -448 diff --git a/ld/testsuite/ld-loongarch-elf/call_undefweak.s b/ld/testsuite/ld-loongarch-elf/call_undefweak.s new file mode 100644 index 00000000000..cc1405f0625 --- /dev/null +++ b/ld/testsuite/ld-loongarch-elf/call_undefweak.s @@ -0,0 +1,33 @@ + .text + .align 2 + .globl main + .type main, @function +main: + # undefweak symbol with .hidden and .protected + # do not need plt entry, Calls to these symbols + # are converted to jump to 0. +nornal_call_nop: + b %plt(fn1) + b %plt(fn2) + + bl %plt(fn1) + bl %plt(fn2) + + # Medium call. +medium_call_nop: + .option norelax + # call36 + pcaddu18i $r1,%call36(fn1) + jirl $r1,$r1,0 + pcaddu18i $r1,%call36(fn2) + jirl $r1,$r1,0 + # tail36 + pcaddu18i $r12,%call36(fn1) + jirl $r0,$r12,0 + pcaddu18i $r12,%call36(fn2) + jirl $r0,$r12,0 + + .weak fn1 + .hidden fn1 + + .weak fn2 diff --git a/ld/testsuite/ld-loongarch-elf/ld-loongarch-elf.exp b/ld/testsuite/ld-loongarch-elf/ld-loongarch-elf.exp index e1b038cb579..d7c2b311f2f 100644 --- a/ld/testsuite/ld-loongarch-elf/ld-loongarch-elf.exp +++ b/ld/testsuite/ld-loongarch-elf/ld-loongarch-elf.exp @@ -143,6 +143,17 @@ if [istarget "loongarch64-*-*"] { "abs-global.out" \ ] \ ] + + run_cc_link_tests [list \ + [list \ + "call undefweak symbol" \ + "" "" \ + {call_undefweak.s} \ + {{objdump {-d} call_undefweak.d}} \ + "call_undefweak" \ + ] \ + ] + } if [istarget "loongarch64-*-*"] {