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 A9F873858295 for ; Thu, 16 Nov 2023 09:38:22 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org A9F873858295 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 A9F873858295 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=1700127505; cv=none; b=nJlwn/ZOkvLS2cluX42QQrWBygzqsJ8lSrZxejUk6evMOWgaZXJy7HxljpSDs9nWDC8luFOSmj4eYT5d7X17BMgQF8e5rxYWKdeyzH7UU6506DtwfVdXyVWsQ450KBJy7V0QmkmWv7jaRaBWBPJz6Iq9cNerLEyuxQnLkb1W+PQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1700127505; c=relaxed/simple; bh=CzOOZX3ZzBbNmlasP5E/e3du22tvhy387lXggTdYbyM=; h=Subject:To:From:Message-ID:Date:MIME-Version; b=nlJg5v2izjk7B3ruBtK4jokVZIa2Pr6u5nlvoYq86E6G6Jj6ci2brCCqXc7e+9EKsponpyYbTmSqoHczKjcd6s0UkF2haFrvcszRVkamI3YS4L9lax244RTkdReiv5KJ+spzUVOARQAOaWZ0JLrCHFStjygDgxHg/CqoN8gKAx8= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from loongson.cn (unknown [10.20.4.171]) by gateway (Coremail) with SMTP id _____8BxyeoL41VlA4M6AA--.40179S3; Thu, 16 Nov 2023 17:38:20 +0800 (CST) Received: from [10.20.4.171] (unknown [10.20.4.171]) by localhost.localdomain (Coremail) with SMTP id AQAAf8Bx3y8K41Vl6vhDAA--.18522S3; Thu, 16 Nov 2023 17:38:18 +0800 (CST) Subject: Re: [PATCH v1 4/6] LoongArch: Remove "elf_seg_map (info->output_bfd) == NULL" relaxation condition To: WANG Xuerui , binutils@sourceware.org Cc: xuchenghua@loongson.cn, chenglulu@loongson.cn, liuzhensong@loongson.cn, xry111@xry111.site, maskray@google.com References: <20231116062307.3292483-1-mengqinggang@loongson.cn> <20231116062307.3292483-5-mengqinggang@loongson.cn> <08376e92-4295-4aa6-aa93-ce4ab4eda5a8@xen0n.name> From: mengqinggang Message-ID: Date: Thu, 16 Nov 2023 17:38:18 +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: <08376e92-4295-4aa6-aa93-ce4ab4eda5a8@xen0n.name> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Content-Language: en-US X-CM-TRANSID:AQAAf8Bx3y8K41Vl6vhDAA--.18522S3 X-CM-SenderInfo: 5phqw15lqjwttqj6z05rqj20fqof0/ X-Coremail-Antispam: 1Uk129KBj93XoW3JFy3uw1UXw4UCrW3Ar4DWrX_yoWxJw4xpr ZYyrWrGFyrAFn3Jr1UJw1UWFy5Jw18Gw1UJr1SqFy7Ar4UJr10qr4UXr9F9F1UGw48Jr1U Xr18Jw17ZF17AwcCm3ZEXasCq-sJn29KB7ZKAUJUUUU5529EdanIXcx71UUUUU7KY7ZEXa sCq-sGcSsGvfJ3Ic02F40EFcxC0VAKzVAqx4xG6I80ebIjqfuFe4nvWSU5nxnvy29KBjDU 0xBIdaVrnRJUUUvIb4IE77IF4wAFF20E14v26r1j6r4UM7CY07I20VC2zVCF04k26cxKx2 IYs7xG6rWj6s0DM7CIcVAFz4kK6r1j6r18M28lY4IEw2IIxxk0rwA2F7IY1VAKz4vEj48v e4kI8wA2z4x0Y4vE2Ix0cI8IcVAFwI0_Jr0_JF4l84ACjcxK6xIIjxv20xvEc7CjxVAFwI 0_Jr0_Gr1l84ACjcxK6I8E87Iv67AKxVW8Jr0_Cr1UM28EF7xvwVC2z280aVCY1x0267AK xVW8Jr0_Cr1UM2AIxVAIcxkEcVAq07x20xvEncxIr21l57IF6xkI12xvs2x26I8E6xACxx 1l5I8CrVACY4xI64kE6c02F40Ex7xfMcIj6xIIjxv20xvE14v26r106r15McIj6I8E87Iv 67AKxVWUJVW8JwAm72CE4IkC6x0Yz7v_Jr0_Gr1lF7xvr2IY64vIr41lc7I2V7IY0VAS07 AlzVAYIcxG8wCF04k20xvY0x0EwIxGrwCFx2IqxVCFs4IE7xkEbVWUJVW8JwC20s026c02 F40E14v26r1j6r18MI8I3I0E7480Y4vE14v26r106r1rMI8E67AF67kF1VAFwI0_JF0_Jw 1lIxkGc2Ij64vIr41lIxAIcVC0I7IYx2IY67AKxVWUJVWUCwCI42IY6xIIjxv20xvEc7Cj xVAFwI0_Jr0_Gr1lIxAIcVCF04k26cxKx2IYs7xG6r1j6r1xMIIF0xvEx4A2jsIE14v26r 4j6F4UMIIF0xvEx4A2jsIEc7CjxVAFwI0_Gr0_Gr1UYxBIdaVFxhVjvjDU0xZFpf9x07UW HqcUUUUU= X-Spam-Status: No, score=-12.0 required=5.0 tests=BAYES_00,BODY_8BITS,GIT_PATCH_0,KAM_DMARC_STATUS,NICE_REPLY_A,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: 在 2023/11/16 下午3:43, WANG Xuerui 写道: > On 11/16/23 14:23, mengqinggang wrote: >> This condition cause .so(shared object) can't be relaxed. > "Previously the condition prevented shared objects from being relaxed." >> Without this condition, we need to update program header size and >> .eh_frame_hdr >> size before relaxation. > "To remove the limitation, we need to ..." >> --- >>   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.  */ > > This is rather hard to understand... > > "This section's output_offset may change after previous section(s) > have been relaxed, so it needs to be updated beforehand. > size_input_section already took care of updating it after relaxation, > so we additionally update once here." > > Does this sound okay? > >> +  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?  */ > Usually FIXME notes are spelled with all-caps to make it easier for > the eyes and tools. >> +  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.  */ > Out of curiosity (and unfamiliarity), is it only "increases" and no > decreases? After relaxation, the vma of data segment may decreases. But before relaxation, the vma of data segment should only increase due to the increase in the number of segments and the determination of the .eh_frame_hdr section size. >> +  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); */