From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from loongson.cn (mail.loongson.cn [114.242.206.163]) by sourceware.org (Postfix) with ESMTP id BD1EB3856248 for ; Thu, 15 Sep 2022 13:03:36 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org BD1EB3856248 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=loongson.cn Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=loongson.cn Received: from [10.20.4.152] (unknown [10.20.4.152]) by localhost.localdomain (Coremail) with SMTP id AQAAf8CxkOClIiNjazEaAA--.35640S3; Thu, 15 Sep 2022 21:03:33 +0800 (CST) Subject: Re: [PATCH 2/2] LoongArch: Fix R_LARCH_IRELATIVE insertion after elf_link_sort_relocs To: Xi Ruoyao , binutils@sourceware.org Cc: Lulu Cheng , Wang Xuerui , Chenghua Xu References: <20220913154414.554861-1-xry111@xry111.site> <20220913154414.554861-3-xry111@xry111.site> From: liuzhensong Message-ID: <939eebd1-365d-72c5-5b2c-43bd6e0f1ff2@loongson.cn> Date: Thu, 15 Sep 2022 21:03:33 +0800 User-Agent: Mozilla/5.0 (X11; Linux mips64; rv:68.0) Gecko/20100101 Thunderbird/68.7.0 MIME-Version: 1.0 In-Reply-To: <20220913154414.554861-3-xry111@xry111.site> Content-Type: text/plain; charset=gbk; format=flowed Content-Transfer-Encoding: 8bit Content-Language: en-US X-CM-TRANSID:AQAAf8CxkOClIiNjazEaAA--.35640S3 X-Coremail-Antispam: 1UD129KBjvJXoWxXFWrCFyfGF4fWw47WFWxZwb_yoW5tFWDpr ZxuFy7Kr1rAa17XwnFq34Uuw43Kw4xWFW2gw13Gw1rX3y5GryagF4FkrWxZFW5WF93ArWS vr1Yga1Dua1kZFDanT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUva14x267AKxVWUJVW8JwAFc2x0x2IEx4CE42xK8VAvwI8IcIk0 rVWrJVCq3wAFIxvE14AKwVWUJVWUGwA2ocxC64kIII0Yj41l84x0c7CEw4AK67xGY2AK02 1l84ACjcxK6xIIjxv20xvE14v26ryj6F1UM28EF7xvwVC0I7IYx2IY6xkF7I0E14v26r4U JVWxJr1l84ACjcxK6I8E87Iv67AKxVW0oVCq3wA2z4x0Y4vEx4A2jsIEc7CjxVAFwI0_Gc CE3s1le2I262IYc4CY6c8Ij28IcVAaY2xG8wAqx4xG64xvF2IEw4CE5I8CrVC2j2WlYx0E 2Ix0cI8IcVAFwI0_Jr0_Jr4lYx0Ex4A2jsIE14v26r1j6r4UMcvjeVCFs4IE7xkEbVWUJV W8JwACjcxG0xvEwIxGrwACjI8F5VA0II8E6IAqYI8I648v4I1lc7I2V7IY0VAS07AlzVAY IcxG8wCY02Avz4vE-syl42xK82IYc2Ij64vIr41l4I8I3I0E4IkC6x0Yz7v_Jr0_Gr1lx2 IqxVAqx4xG67AKxVWUJVWUGwC20s026x8GjcxK67AKxVWUGVWUWwC2zVAF1VAY17CE14v2 6r126r1DMIIYrxkI7VAKI48JMIIF0xvE2Ix0cI8IcVAFwI0_Jr0_JF4lIxAIcVC0I7IYx2 IY6xkF7I0E14v26r1j6r4UMIIF0xvE42xK8VAvwI8IcIk0rVWUJVWUCwCI42IY6I8E87Iv 67AKxVWUJVW8JwCI42IY6I8E87Iv6xkF7I0E14v26r4j6r4UJbIYCTnIWIevJa73UjIFyT uYvjfU5WlkUUUUU X-CM-SenderInfo: holx6xphqv003j6o00pqjv00gofq/ X-Spam-Status: No, score=-11.2 required=5.0 tests=BAYES_00,GIT_PATCH_0,KAM_DMARC_STATUS,MIME_CHARSET_FARAWAY,NICE_REPLY_A,SPF_HELO_PASS,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 List-Id: ÔÚ 2022/9/13 ÏÂÎç11:44, Xi Ruoyao дµÀ: > 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. It May be unnecessary find the slot in section of .rela.dyn. The slot of R_LARCH_IRELATIVE is within the scope relplt->contents (the space is calculated in function local_allocate_ifunc_dyn_relocs). We can find the slot just in relplt->contents(Only for dynamic ifunc. I did not test for static ifunc.). > > 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++) My mistake. > + 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 > {