From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from xry111.site (xry111.site [89.208.246.23]) by sourceware.org (Postfix) with ESMTPS id 71EBB3850235 for ; Tue, 13 Sep 2022 15:44:52 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 71EBB3850235 Authentication-Results: sourceware.org; dmarc=pass (p=reject dis=none) header.from=xry111.site Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=xry111.site DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=xry111.site; s=default; t=1663083891; bh=4DpqDbvI7Nyk+Do/wgpxZ1xQd9IosKRbXFZhSJ1YDtA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=e6zAGkgc8O1HzWYnSsiDnMe6F3n/+5gOi+Un5hx6gs3gazfRW3rCMjHwSBFx+ik6t y4ha0/p37DGsYWTlUxyKKOQT5yuClhHxyA9J5/42PYOve8dZ+zVoXOzcZjn4EhFLtb gsBIdVTJ7K/GMrVDvoat08F0ULFhlB2Kitmw+qJE= Received: from xry111-x57s1.. (unknown [IPv6:240e:358:1123:8b00:dc73:854d:832e:4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (Client did not present a certificate) (Authenticated sender: xry111@xry111.site) by xry111.site (Postfix) with ESMTPSA id 81AE365C2A; Tue, 13 Sep 2022 11:44:47 -0400 (EDT) From: Xi Ruoyao To: binutils@sourceware.org Cc: liuzhensong , Lulu Cheng , Wang Xuerui , Chenghua Xu , Xi Ruoyao Subject: [PATCH 2/2] LoongArch: Fix R_LARCH_IRELATIVE insertion after elf_link_sort_relocs Date: Tue, 13 Sep 2022 23:44:14 +0800 Message-Id: <20220913154414.554861-3-xry111@xry111.site> X-Mailer: git-send-email 2.37.3 In-Reply-To: <20220913154414.554861-1-xry111@xry111.site> References: <20220913154414.554861-1-xry111@xry111.site> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.2 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FROM_SUSPICIOUS_NTLD,GIT_PATCH_0,LIKELY_SPAM_FROM,PDS_OTHER_BAD_TLD,SPF_HELO_PASS,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: loongarch_elf_finish_dynamic_symbol is called after elf_link_sort_relocs if -z combreloc. elf_link_sort_relocs redistributes the contents of .rela.* sections those would be merged into .rela.dyn, so the slot for R_LARCH_IRELATIVE may be out of relplt->contents now. To make things worse, the boundary check dyn < dyn + relplt->size / sizeof (*dyn) is obviously wrong ("x + 10 < x"? :), causing the issue undetected during the linking process and the resulted executable suddenly crashes at runtime. The issue was found during an attempt to add static-pie support to the toolchain. Fix it by iterating through the inputs of .rela.dyn to find the slot. --- bfd/elfnn-loongarch.c | 41 ++++++++++++++++++++++++----------------- 1 file changed, 24 insertions(+), 17 deletions(-) diff --git a/bfd/elfnn-loongarch.c b/bfd/elfnn-loongarch.c index 4b408b1db72..1de9dbfecfa 100644 --- a/bfd/elfnn-loongarch.c +++ b/bfd/elfnn-loongarch.c @@ -3511,6 +3511,12 @@ loongarch_elf_finish_dynamic_symbol (bfd *output_bfd, { struct loongarch_elf_link_hash_table *htab = loongarch_elf_hash_table (info); const struct elf_backend_data *bed = get_elf_backend_data (output_bfd); + asection *rela_dyn = bfd_get_section_by_name (output_bfd, ".rela.dyn"); + struct bfd_link_order *lo = NULL; + Elf_Internal_Rela *slot = NULL, *last_slot = NULL; + + if (rela_dyn) + lo = rela_dyn->map_head.link_order; if (h->plt.offset != MINUS_ONE) { @@ -3520,6 +3526,7 @@ loongarch_elf_finish_dynamic_symbol (bfd *output_bfd, uint32_t plt_entry[PLT_ENTRY_INSNS]; bfd_byte *loc; Elf_Internal_Rela rela; + asection *rela_sec = NULL; if (htab->elf.splt) { @@ -3572,31 +3579,31 @@ loongarch_elf_finish_dynamic_symbol (bfd *output_bfd, && (relplt == htab->elf.srelgot || relplt == htab->elf.irelplt)) { - { - rela.r_info = ELFNN_R_INFO (0, R_LARCH_IRELATIVE); - rela.r_addend = (h->root.u.def.value + rela.r_info = ELFNN_R_INFO (0, R_LARCH_IRELATIVE); + rela.r_addend = (h->root.u.def.value + h->root.u.def.section->output_section->vma + h->root.u.def.section->output_offset); - } - /* Find the space after dyn sort. */ + /* Find the space after dyn sort. */ + while (slot == last_slot || slot->r_offset != 0) { - Elf_Internal_Rela *dyn = (Elf_Internal_Rela *)relplt->contents; - bool fill = false; - for (;dyn < dyn + relplt->size / sizeof (*dyn); dyn++) + if (slot != last_slot) { - if (0 == dyn->r_offset) - { - bed->s->swap_reloca_out (output_bfd, &rela, - (bfd_byte *)dyn); - relplt->reloc_count++; - fill = true; - break; - } + slot++; + continue; } - BFD_ASSERT (fill); + + BFD_ASSERT (lo != NULL); + rela_sec = lo->u.indirect.section; + lo = lo->next; + + slot = (Elf_Internal_Rela *)rela_sec->contents; + last_slot = (Elf_Internal_Rela *)(rela_sec->contents + + rela_sec->size); } + bed->s->swap_reloca_out (output_bfd, &rela, (bfd_byte *)slot); + rela_sec->reloc_count++; } else { -- 2.37.0