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 01E8B3860753 for ; Thu, 16 Nov 2023 06:23:28 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 01E8B3860753 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 01E8B3860753 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=1700115811; cv=none; b=U9qT1jrRMLPppc9J3NZY7xbpQwYi3ZuHwZYEz335G4qXUNbvDqXdXYuclK/+WUwTieD/vpfgp6zOQ4H4V1MlWPy+PVpbO5e4aPDO1QMwocHQzKc3ejGUhmWXirWFj6IjjPoueGRzT5hOebZZ36CzCYhdygxlXLG7ageam3O7hqM= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1700115811; c=relaxed/simple; bh=8q21KnGCcncrdLbIoQnl9B3Zc9qT5ru+DzuwEcICzR0=; h=From:To:Subject:Date:Message-Id:MIME-Version; b=n5xcRpNmKJAHFrL2SHUoNlhzsfoX8ID0DdZZ4qtjT9mZ75TnFjL/FKv0Xu+oDOLBoTydnmtcjcWj5PklLgH2oARaEJAB09sh0E71uKE/4KNjLq2AXxaxIajcXMyAtCxG9tTw7pZs4l5tWYEt1pvmekjxRFuivP55fIJtTsMBhks= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from loongson.cn (unknown [10.2.6.5]) by gateway (Coremail) with SMTP id _____8AxEvBftVVlTnk6AA--.49169S3; Thu, 16 Nov 2023 14:23:27 +0800 (CST) Received: from 5.5.5 (unknown [10.2.6.5]) by localhost.localdomain (Coremail) with SMTP id AQAAf8AxndxOtVVlfNJDAA--.16902S6; Thu, 16 Nov 2023 14:23:27 +0800 (CST) From: mengqinggang To: binutils@sourceware.org Cc: xuchenghua@loongson.cn, chenglulu@loongson.cn, liuzhensong@loongson.cn, xry111@xry111.site, i.swmail@xen0n.name, maskray@google.com, mengqinggang Subject: [PATCH v1 4/6] LoongArch: Remove "elf_seg_map (info->output_bfd) == NULL" relaxation condition Date: Thu, 16 Nov 2023 14:23:05 +0800 Message-Id: <20231116062307.3292483-5-mengqinggang@loongson.cn> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20231116062307.3292483-1-mengqinggang@loongson.cn> References: <20231116062307.3292483-1-mengqinggang@loongson.cn> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-CM-TRANSID:AQAAf8AxndxOtVVlfNJDAA--.16902S6 X-CM-SenderInfo: 5phqw15lqjwttqj6z05rqj20fqof0/ X-Coremail-Antispam: 1Uk129KBj93XoWxZF1DJw1kJF47Aw4DKFyDJwc_yoWrXFyfpa 9xu3s5Ga1rAFn3WwnFv3yrWFn8t34xur12qasxK3s7Cr45Wry0qF4Fyry7uF4DKw17JrWI vF1kt3W7ZFn7CabCm3ZEXasCq-sJn29KB7ZKAUJUUUU8529EdanIXcx71UUUUU7KY7ZEXa sCq-sGcSsGvfJ3Ic02F40EFcxC0VAKzVAqx4xG6I80ebIjqfuFe4nvWSU5nxnvy29KBjDU 0xBIdaVrnRJUUUk2b4IE77IF4wAFF20E14v26r1j6r4UM7CY07I20VC2zVCF04k26cxKx2 IYs7xG6rWj6s0DM7CIcVAFz4kK6r1Y6r17M28lY4IEw2IIxxk0rwA2F7IY1VAKz4vEj48v e4kI8wA2z4x0Y4vE2Ix0cI8IcVAFwI0_Gr0_Xr1l84ACjcxK6xIIjxv20xvEc7CjxVAFwI 0_Gr0_Cr1l84ACjcxK6I8E87Iv67AKxVW8Jr0_Cr1UM28EF7xvwVC2z280aVCY1x0267AK xVW8Jr0_Cr1UM2AIxVAIcxkEcVAq07x20xvEncxIr21l57IF6xkI12xvs2x26I8E6xACxx 1l5I8CrVACY4xI64kE6c02F40Ex7xfMcIj6xIIjxv20xvE14v26r1q6rW5McIj6I8E87Iv 67AKxVW8JVWxJwAm72CE4IkC6x0Yz7v_Jr0_Gr1lF7xvr2IYc2Ij64vIr41l42xK82IYc2 Ij64vIr41l4I8I3I0E4IkC6x0Yz7v_Jr0_Gr1lx2IqxVAqx4xG67AKxVWUJVWUGwC20s02 6x8GjcxK67AKxVWUGVWUWwC2zVAF1VAY17CE14v26r1q6r43MIIYrxkI7VAKI48JMIIF0x vE2Ix0cI8IcVAFwI0_Gr0_Xr1lIxAIcVC0I7IYx2IY6xkF7I0E14v26r4j6F4UMIIF0xvE 42xK8VAvwI8IcIk0rVWUJVWUCwCI42IY6I8E87Iv67AKxVW8JVWxJwCI42IY6I8E87Iv6x kF7I0E14v26r4j6r4UJbIYCTnIWIevJa73UjIFyTuYvjxUcCD7UUUUU X-Spam-Status: No, score=-12.5 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: This condition cause .so(shared object) can't be relaxed. Without this condition, we need to update program header size and .eh_frame_hdr size before relaxation. --- bfd/elfnn-loongarch.c | 24 ++++++++++++++++++++---- ld/emultempl/loongarchelf.em | 18 ++++++++++++++++++ 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/bfd/elfnn-loongarch.c b/bfd/elfnn-loongarch.c index 7436a14441f..d331eefb8ac 100644 --- a/bfd/elfnn-loongarch.c +++ b/bfd/elfnn-loongarch.c @@ -3738,7 +3738,7 @@ loongarch_relax_delete_bytes (bfd *abfd, /* Relax pcalau12i,addi.d => pcaddi. */ static bool -loongarch_relax_pcala_addi (bfd *abfd, asection *sec, +loongarch_relax_pcala_addi (bfd *abfd, asection *sec, asection *sym_sec, Elf_Internal_Rela *rel_hi, bfd_vma symval, struct bfd_link_info *info, bool *again) { @@ -3747,7 +3747,23 @@ loongarch_relax_pcala_addi (bfd *abfd, asection *sec, uint32_t pca = bfd_get (32, abfd, contents + rel_hi->r_offset); uint32_t add = bfd_get (32, abfd, contents + rel_lo->r_offset); uint32_t rd = pca & 0x1f; + + /* Because previous sections' relax, output_offset may increase and need to + be updated before relax. But it update after relax in + size_input_section defaultly, so we manually updating here. */ + sec->output_offset = sec->output_section->size; bfd_vma pc = sec_addr (sec) + rel_hi->r_offset; + + /* If pc and symbol not in the same segment, add/sub segment alignment. + Fixme: if there are multiple readonly segments? */ + if (!(sym_sec->flags & SEC_READONLY)) + { + if (symval > pc) + pc -= info->maxpagesize; + else if (symval < pc) + pc += info->maxpagesize; + } + const uint32_t addi_d = 0x02c00000; const uint32_t pcaddi = 0x18000000; @@ -3889,7 +3905,6 @@ loongarch_elf_relax_section (bfd *abfd, asection *sec, || sec->sec_flg0 || (sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0 - || elf_seg_map (info->output_bfd) == NULL || (info->disable_target_specific_optimizations && info->relax_pass == 0) /* The exp_seg_relro_adjust is enum phase_enum (0x4), @@ -4009,14 +4024,15 @@ loongarch_elf_relax_section (bfd *abfd, asection *sec, break; case R_LARCH_PCALA_HI20: if (0 == info->relax_pass && (i + 4) <= sec->reloc_count) - loongarch_relax_pcala_addi (abfd, sec, rel, symval, info, again); + loongarch_relax_pcala_addi (abfd, sec, sym_sec, rel, symval, + info, again); break; case R_LARCH_GOT_PC_HI20: if (local_got && 0 == info->relax_pass && (i + 4) <= sec->reloc_count) { if (loongarch_relax_pcala_ld (abfd, sec, rel)) - loongarch_relax_pcala_addi (abfd, sec, rel, symval, + loongarch_relax_pcala_addi (abfd, sec, sym_sec, rel, symval, info, again); } break; diff --git a/ld/emultempl/loongarchelf.em b/ld/emultempl/loongarchelf.em index 4850feb8767..d81c99da48b 100644 --- a/ld/emultempl/loongarchelf.em +++ b/ld/emultempl/loongarchelf.em @@ -62,6 +62,24 @@ gld${EMULATION_NAME}_after_allocation (void) } } + /* The program header size of executable file may increase. */ + if (bfd_get_flavour (link_info.output_bfd) == bfd_target_elf_flavour + && !bfd_link_relocatable (&link_info)) + { + if (lang_phdr_list == NULL) + elf_seg_map (link_info.output_bfd) = NULL; + if (!_bfd_elf_map_sections_to_segments (link_info.output_bfd, + &link_info, + NULL)) + einfo (_("%F%P: map sections to segments failed: %E\n")); + } + + /* Adjust program header size and .eh_frame_hdr size before + lang_relax_sections. Without it, the vma of data segment may increase. */ + lang_do_assignments (lang_allocating_phase_enum); + lang_reset_memory_regions (); + lang_size_sections (NULL, true); + enum phase_enum *phase = &(expld.dataseg.phase); bfd_elf${ELFSIZE}_loongarch_set_data_segment_info (&link_info, (int *) phase); /* gld${EMULATION_NAME}_map_segments (need_layout); */ -- 2.36.0