From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail.loongson.cn (mail.loongson.cn [114.242.206.163]) by sourceware.org (Postfix) with ESMTP id 35BD83858D3C for ; Sun, 4 Feb 2024 06:53:43 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 35BD83858D3C 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 35BD83858D3C 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=1707029627; cv=none; b=Pi034oMdojRUrdKsiUCDYtvMMCk7gelUhIwfdfKRLBr7z4JF6/vK2AW/QIzwJ/wh7c13gy2Sloi8oKjMAYKVB8MbNDg/TEbBOGmdVhDoSOWe6t2FJIjBOi6ADnqN6/kuISi2/nN8dyHYl3GUOluHJfVMqt5nSQN0lxbhNN5kl6I= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1707029627; c=relaxed/simple; bh=sspt10xGvJy6U0I/XAnnNLnsGPQtjGtx7BS7HPZFphc=; h=From:To:Subject:Date:Message-Id:MIME-Version; b=HhJWyVwSP/YPdXgDUQ2xMO3huMqD7idzaTYac4O5YF2nwtW+70JFKFdbNRxT1T0/uvAydVyZPnjEDDKFxWYw/vbQXqz9+lQmU/0+6Y+Y4RUhtKdXwDsKxrB97B0lY7oX9x64ZYboFV7o0oBBCrJrmaTek3d9qaQPoZj74+h8dyw= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from loongson.cn (unknown [10.2.6.5]) by gateway (Coremail) with SMTP id _____8Bxyuh0NL9lJ4gKAA--.10478S3; Sun, 04 Feb 2024 14:53:41 +0800 (CST) Received: from 5.5.5 (unknown [10.2.6.5]) by localhost.localdomain (Coremail) with SMTP id AQAAf8Dxfs10NL9lqjgvAA--.45526S2; Sun, 04 Feb 2024 14:53:40 +0800 (CST) From: mengqinggang To: binutils@sourceware.org Cc: xuchenghua@loongson.cn, chenglulu@loongson.cn, liuzhensong@loongson.cn, cailulu@loongson.cn, xry111@xry111.site, i.swmail@xen0n.name, maskray@google.com, luweining@loongson.cn, wanglei@loongson.cn, hejinyang@loongson.cn, mengqinggang@loongson.cn Subject: [PATCH] LoongArch: Fix the bug of R_LARCH_AGLIN caused by discard section Date: Sun, 4 Feb 2024 14:53:38 +0800 Message-Id: <20240204065338.161932-1-mengqinggang@loongson.cn> X-Mailer: git-send-email 2.39.3 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-CM-TRANSID:AQAAf8Dxfs10NL9lqjgvAA--.45526S2 X-CM-SenderInfo: 5phqw15lqjwttqj6z05rqj20fqof0/ X-Coremail-Antispam: 1Uk129KBj93XoW3Gw4kXFWxKF48WFyfJw17CFX_yoW7Aw47pr y2vrsakFW8CFn7WwnrC3y5Cwn8Xws7uFWIqa4xtr42krsYqryvqw1vkrW3GayUGw1DCryr Zr10v34jqFyvy3gCm3ZEXasCq-sJn29KB7ZKAUJUUUU8529EdanIXcx71UUUUU7KY7ZEXa sCq-sGcSsGvfJ3Ic02F40EFcxC0VAKzVAqx4xG6I80ebIjqfuFe4nvWSU5nxnvy29KBjDU 0xBIdaVrnRJUUUk2b4IE77IF4wAFF20E14v26r1j6r4UM7CY07I20VC2zVCF04k26cxKx2 IYs7xG6rWj6s0DM7CIcVAFz4kK6r106r15M28lY4IEw2IIxxk0rwA2F7IY1VAKz4vEj48v e4kI8wA2z4x0Y4vE2Ix0cI8IcVAFwI0_Jr0_JF4l84ACjcxK6xIIjxv20xvEc7CjxVAFwI 0_Gr0_Cr1l84ACjcxK6I8E87Iv67AKxVW8Jr0_Cr1UM28EF7xvwVC2z280aVCY1x0267AK xVW8Jr0_Cr1UM2AIxVAIcxkEcVAq07x20xvEncxIr21l57IF6xkI12xvs2x26I8E6xACxx 1l5I8CrVACY4xI64kE6c02F40Ex7xfMcIj6xIIjxv20xvE14v26r1j6r18McIj6I8E87Iv 67AKxVW8JVWxJwAm72CE4IkC6x0Yz7v_Jr0_Gr1lF7xvr2IYc2Ij64vIr41l42xK82IYc2 Ij64vIr41l4I8I3I0E4IkC6x0Yz7v_Jr0_Gr1lx2IqxVAqx4xG67AKxVWUJVWUGwC20s02 6x8GjcxK67AKxVWUGVWUWwC2zVAF1VAY17CE14v26r1q6r43MIIYrxkI7VAKI48JMIIF0x vE2Ix0cI8IcVAFwI0_Jr0_JF4lIxAIcVC0I7IYx2IY6xkF7I0E14v26r1j6r4UMIIF0xvE 42xK8VAvwI8IcIk0rVWUJVWUCwCI42IY6I8E87Iv67AKxVW8JVWxJwCI42IY6I8E87Iv6x kF7I0E14v26r4j6r4UJbIYCTnIWIevJa73UjIFyTuYvjxUcyxRUUUUU X-Spam-Status: No, score=-12.6 required=5.0 tests=BAYES_00,GIT_PATCH_0,KAM_DMARC_STATUS,SPF_HELO_NONE,SPF_PASS,TXREP,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: To represent the first and third expression of .align, R_LARCH_ALIGN need to associate with a symbol. We defind a local symbol for R_LARCH_AGLIN. But if the section of the local symbo is discarded, it may result in a undefined symbol error. Instead, we use the section name symbols, and this does not need to add extra symbols. During partial linking (ld -r), if the symbol associated with a relocation is STT_SECTION type, the addend of relocation needs to add the section output offset. We prevent it for R_LARCH_ALIGN. --- bfd/elflink.c | 9 +++++++-- gas/config/tc-loongarch.c | 5 +---- gas/testsuite/gas/loongarch/relax_align.d | 20 +++++++++---------- .../ld-loongarch-elf/relax-align-discard.lds | 4 ++++ .../ld-loongarch-elf/relax-align-discard.s | 17 ++++++++++++++++ ld/testsuite/ld-loongarch-elf/relax.exp | 12 +++++++++++ 6 files changed, 51 insertions(+), 16 deletions(-) create mode 100644 ld/testsuite/ld-loongarch-elf/relax-align-discard.lds create mode 100644 ld/testsuite/ld-loongarch-elf/relax-align-discard.s diff --git a/bfd/elflink.c b/bfd/elflink.c index c2494b3e12e..33abac85fa2 100644 --- a/bfd/elflink.c +++ b/bfd/elflink.c @@ -11813,8 +11813,13 @@ elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd) } /* Adjust the addend according to where the - section winds up in the output section. */ - if (rela_normal) + section winds up in the output section. + The addend of R_LARCH_ALIGN is used to represent the + first and the third expression of .align and not need + to add sec->output_offset. */ + if (rela_normal + && !(bed->elf_machine_code == EM_LOONGARCH + && (irela->r_info & 0xff) == 0x66)) irela->r_addend += sec->output_offset; } else diff --git a/gas/config/tc-loongarch.c b/gas/config/tc-loongarch.c index 91f5f1d0681..3fa70c1c85f 100644 --- a/gas/config/tc-loongarch.c +++ b/gas/config/tc-loongarch.c @@ -1746,10 +1746,7 @@ loongarch_frag_align_code (int n, int max) nops = frag_more (worst_case_bytes); - s = symbol_find (".Lla-relax-align"); - if (s == NULL) - s = (symbolS *)local_symbol_make (".Lla-relax-align", now_seg, - &zero_address_frag, 0); + s = symbol_find (now_seg->name); ex.X_add_symbol = s; ex.X_op = O_symbol; diff --git a/gas/testsuite/gas/loongarch/relax_align.d b/gas/testsuite/gas/loongarch/relax_align.d index 2cc6c86d38a..6710927be1b 100644 --- a/gas/testsuite/gas/loongarch/relax_align.d +++ b/gas/testsuite/gas/loongarch/relax_align.d @@ -1,4 +1,4 @@ -#as: --no-warn +#as: #objdump: -dr #skip: loongarch32-*-* @@ -7,27 +7,27 @@ Disassembly of section .text: -[ ]*0000000000000000 <.Lla-relax-align>: +[ ]*0000000000000000 <.text>: [ ]+0:[ ]+1a000004[ ]+pcalau12i[ ]+\$a0, 0 [ ]+0: R_LARCH_PCALA_HI20[ ]+L1 [ ]+0: R_LARCH_RELAX[ ]+\*ABS\* [ ]+4:[ ]+02c00084[ ]+addi.d[ ]+\$a0, \$a0, 0 [ ]+4: R_LARCH_PCALA_LO12[ ]+L1 [ ]+4: R_LARCH_RELAX[ ]+\*ABS\* -[ ]+8:[ ]+03400000[ ]+nop.* -[ ]+8: R_LARCH_ALIGN[ ]+.Lla-relax-align\+0x4 -[ ]+c:[ ]+03400000[ ]+nop.* -[ ]+10:[ ]+03400000[ ]+nop.* +[ ]+8:[ ]+03400000[ ]+nop +[ ]+8: R_LARCH_ALIGN[ ]+.text\+0x4 +[ ]+c:[ ]+03400000[ ]+nop +[ ]+10:[ ]+03400000[ ]+nop [ ]+14:[ ]+1a000004[ ]+pcalau12i[ ]+\$a0, 0 [ ]+14: R_LARCH_PCALA_HI20[ ]+L1 [ ]+14: R_LARCH_RELAX[ ]+\*ABS\* [ ]+18:[ ]+02c00084[ ]+addi.d[ ]+\$a0, \$a0, 0 [ ]+18: R_LARCH_PCALA_LO12[ ]+L1 [ ]+18: R_LARCH_RELAX[ ]+\*ABS\* -[ ]+1c:[ ]+03400000[ ]+nop.* -[ ]+1c: R_LARCH_ALIGN[ ]+.Lla-relax-align\+0x404 -[ ]+20:[ ]+03400000[ ]+nop.* -[ ]+24:[ ]+03400000[ ]+nop.* +[ ]+1c:[ ]+03400000[ ]+nop +[ ]+1c: R_LARCH_ALIGN[ ]+.text\+0x404 +[ ]+20:[ ]+03400000[ ]+nop +[ ]+24:[ ]+03400000[ ]+nop [ ]+28:[ ]+1a000004[ ]+pcalau12i[ ]+\$a0, 0 [ ]+28: R_LARCH_PCALA_HI20[ ]+L1 [ ]+28: R_LARCH_RELAX[ ]+\*ABS\* diff --git a/ld/testsuite/ld-loongarch-elf/relax-align-discard.lds b/ld/testsuite/ld-loongarch-elf/relax-align-discard.lds new file mode 100644 index 00000000000..4a81323d926 --- /dev/null +++ b/ld/testsuite/ld-loongarch-elf/relax-align-discard.lds @@ -0,0 +1,4 @@ +SECTIONS +{ + /DISCARD/ : { *(.another.*) } +} diff --git a/ld/testsuite/ld-loongarch-elf/relax-align-discard.s b/ld/testsuite/ld-loongarch-elf/relax-align-discard.s new file mode 100644 index 00000000000..b65d63f370f --- /dev/null +++ b/ld/testsuite/ld-loongarch-elf/relax-align-discard.s @@ -0,0 +1,17 @@ +# Use the section name symbol for R_LARCH_ALIGN to avoid discard section problem +.section ".another.text", "ax" +.cfi_startproc +break 0 +.cfi_def_cfa_offset 16 +.p2align 5 +break 1 +.cfi_endproc + +.text +.cfi_startproc +break 0 +.cfi_def_cfa_offset 16 +.p2align 5 +break 1 +.cfi_endproc + diff --git a/ld/testsuite/ld-loongarch-elf/relax.exp b/ld/testsuite/ld-loongarch-elf/relax.exp index f421e8af8dd..a1465dca21c 100644 --- a/ld/testsuite/ld-loongarch-elf/relax.exp +++ b/ld/testsuite/ld-loongarch-elf/relax.exp @@ -295,6 +295,18 @@ if [istarget loongarch64-*-*] { "relax-align" \ ] \ ] + + run_ld_link_tests \ + [list \ + [list \ + "loongarch relax align discard" \ + "-e 0x0 -T relax-align-discard.lds -r" "" \ + "" \ + {relax-align-discard.s} \ + {} \ + "relax-align-discard" \ + ] \ + ] } set objdump_flags "-s -j .data" -- 2.36.0