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 F241A38362CF for ; Thu, 1 Sep 2022 01:28:01 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org F241A38362CF 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 AQAAf8Dx_2ufChBjHV4OAA--.60695S3; Thu, 01 Sep 2022 09:28:00 +0800 (CST) Subject: Re: [PATCH] LoongArch: Use copy relocation for %pc_lo12 against external symbol To: Xi Ruoyao , binutils@sourceware.org Cc: WANG Xuerui , chenglulu@loongson.cn References: <20220831132259.71417-1-xry111@xry111.site> From: liuzhensong Message-ID: Date: Thu, 1 Sep 2022 09:27:59 +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: <20220831132259.71417-1-xry111@xry111.site> Content-Language: en-US X-CM-TRANSID: AQAAf8Dx_2ufChBjHV4OAA--.60695S3 X-Coremail-Antispam: 1UD129KBjvJXoW3JF17ur45Jr15JFWrtrWxCrg_yoW7Gw1rpr y3ur9Igw48XF1fWwnFvay8Zw4aq3yxuayxXa43J3WrCwnrX34YgF4rtrWDXF4UGr47XrWj qryjva1UuF18Z3DanT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUvjb7Iv0xC_Kw4lb4IE77IF4wAFF20E14v26r1j6r4UM7CY07I2 0VC2zVCF04k26cxKx2IYs7xG6rWj6s0DM7CIcVAFz4kK6r1j6r18M28lY4IEw2IIxxk0rw A2F7IY1VAKz4vEj48ve4kI8wA2z4x0Y4vE2Ix0cI8IcVAFwI0_Ar0_tr1l84ACjcxK6xII jxv20xvEc7CjxVAFwI0_Cr0_Gr1UM28EF7xvwVC2z280aVAFwI0_Gr1j6F4UJwA2z4x0Y4 vEx4A2jsIEc7CjxVAFwI0_Gr1j6F4UJwAS0I0E0xvYzxvE52x082IY62kv0487McIj6xII jxv20xvE14v26r1j6r18McIj6I8E87Iv67AKxVWUJVW8JwAm72CE4IkC6x0Yz7v_Jr0_Gr 1lF7xvr2IY64vIr41l7480Y4vEI4kI2Ix0rVAqx4xJMxk0xIA0c2IEe2xFo4CEbIxvr21l c2xSY4AK6svPMxAIw28IcxkI7VAKI48JMxC20s026xCaFVCjc4AY6r1j6r4UMI8I3I0E5I 8CrVAFwI0_JrI_JrWlx2IqxVCjr7xvwVAFwI0_JrI_JrWlx4CE17CEb7AF67AKxVWUAVWU twCIc40Y0x0EwIxGrwCI42IY6xIIjxv20xvE14v26r1j6r1xMIIF0xvE2Ix0cI8IcVCY1x 0267AKxVWUJVW8JwCI42IY6xAIw20EY4v20xvaj40_Jr0_JF4lIxAIcVC2z280aVAFwI0_ Jr0_Gr1lIxAIcVC2z280aVCY1x0267AKxVWUJVW8JbIYCTnIWIevJa73UjIFyTuYvjxUqW xRDUUUU X-CM-SenderInfo: holx6xphqv003j6o00pqjv00gofq/ X-Spam-Status: No, score=-8.9 required=5.0 tests=BAYES_00, BODY_8BITS, GIT_PATCH_0, HTML_MESSAGE, KAM_DMARC_STATUS, MIME_CHARSET_FARAWAY, NICE_REPLY_A, 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 Content-Type: text/plain; charset=gbk; format=flowed Content-Transfer-Encoding: 8bit X-Content-Filtered-By: Mailman/MimeDel 2.1.29 X-BeenThere: binutils@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Binutils mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 01 Sep 2022 01:28:05 -0000 在 2022/8/31 下午9:22, Xi Ruoyao 写道: > We'd like to use PC-relative addressing instead of GOT for external > symbols in main executable images. This can improve code locality. > Doing so will need to implement copy relocation. > > Despite there was a comment saying "Glibc does not support copy > relocation yet", I've run a very simple test and it seems copy > relocation is handled by ld.so without a problem: > > pcalau12i $a1, %pc_hi20(stdout) > ld.d $a1, $a1, %pc_lo12(stdout) > pcalau12i $a0, %pc_hi20(msg) > addi.d $a0, $a0, %pc_lo12(msg) > pcalau12i $ra, %pc_hi20(fputs) > jirl $ra, $ra, %pc_lo12(fputs) > > With this patch, R_LARCH_COPY is correctly emitted for "stdout" in > Glibc, and the test program runs and outputs "Hello world" in "msg" > successfully. > --- > bfd/elfnn-loongarch.c | 93 ++++++++++++++++++++++++++++++++++++++----- > 1 file changed, 84 insertions(+), 9 deletions(-) > > diff --git a/bfd/elfnn-loongarch.c b/bfd/elfnn-loongarch.c > index ed42b8b6770..0cf24bd24d8 100644 > --- a/bfd/elfnn-loongarch.c > +++ b/bfd/elfnn-loongarch.c > @@ -743,14 +743,26 @@ loongarch_elf_check_relocs (bfd *abfd, struct bfd_link_info *info, > h->non_got_ref = 1; > break; > > - case R_LARCH_PCALA_HI20: > + case R_LARCH_PCALA_LO12: > if (h != NULL) > { > - /* For pcalau12i + jirl. */ > - h->needs_plt = 1; > - if (h->plt.refcount < 0) > - h->plt.refcount = 0; > - h->plt.refcount++; > + /* Check if it's a jirl instruction. */ > + bfd_byte *contents = elf_section_data (sec)->this_hdr.contents; > + uint32_t insn = 0; > + > + if (contents || bfd_malloc_and_get_section (abfd, sec, &contents)) > + memcpy(&insn, contents + rel->r_offset, sizeof(insn)); > + > + if ((insn & 0xfc000000) == 0x4c000000) > + { > + /* For pcalau12i + jirl. */ > + h->needs_plt = 1; > + if (h->plt.refcount < 0) > + h->plt.refcount = 0; > + h->plt.refcount++; > + } > + else > + need_dynreloc = 1; > > h->non_got_ref = 1; > h->pointer_equality_needed = 1; > @@ -949,7 +961,9 @@ loongarch_elf_adjust_dynamic_symbol (struct bfd_link_info *info, > struct elf_link_hash_entry *h) > { > struct loongarch_elf_link_hash_table *htab; > + struct loongarch_elf_link_hash_entry *hent; > bfd *dynobj; > + asection *s, *srel; > > htab = loongarch_elf_hash_table (info); > BFD_ASSERT (htab != NULL); > @@ -999,9 +1013,52 @@ loongarch_elf_adjust_dynamic_symbol (struct bfd_link_info *info, > return true; > } > > - /* R_LARCH_COPY is not adept glibc, not to generate. */ > - /* Can not print anything, because make check ld. */ > - return true; > + /* If copy relocations is disabled via -z nocopyreloc, or we don't find any > + dynamic relocs in read-only sections, avoid the copy reloc. */ > + if (info->nocopyreloc || !_bfd_elf_readonly_dynrelocs (h)) > + h->non_got_ref = 0; > + > + if (!h->non_got_ref) > + return true; > + > + /* We must allocate the symbol in our .dynbss section, which will > + become part of the .bss section of the executable. There will be > + an entry for this symbol in the .dynsym section. The dynamic > + object will contain position independent code, so all references > + from the dynamic object to this symbol will go through the global > + offset table. The dynamic linker will use the .dynsym entry to > + determine the address it must put in the global offset table, so > + both the dynamic object and the regular object will refer to the > + same memory location for the variable. */ > + > + /* Generate a copy relocation to tell the dynamic linker to copy the > + initial value out of the dynamic object and into the runtime process > + image. We need to remember the offset into the .rel.bss section we > + are going to use. */ > + hent = (struct loongarch_elf_link_hash_entry *) h; > + if (hent->tls_type & ~GOT_NORMAL) > + { > + s = htab->sdyntdata; > + srel = htab->elf.srelbss; > + } > + else if (h->root.u.def.section->flags & SEC_READONLY) > + { > + s = htab->elf.sdynrelro; > + srel = htab->elf.sreldynrelro; > + } > + else > + { > + s = htab->elf.sdynbss; > + srel = htab->elf.srelbss; > + } > + > + if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0) > + { > + srel->size += sizeof (ElfNN_External_Rela); > + h->needs_copy = 1; > + } > + > + return _bfd_elf_adjust_dynamic_copy (info, h, s); > } > > /* Allocate space in .plt, .got and associated reloc sections for > @@ -3702,6 +3759,24 @@ loongarch_elf_finish_dynamic_symbol (bfd *output_bfd, > loongarch_elf_append_rela (output_bfd, srela, &rela); > } > > + if (h->needs_copy) > + { > + Elf_Internal_Rela rela; > + asection *s; > + > + /* This symbols needs a copy reloc. Set it up. */ > + BFD_ASSERT (h->dynindx != -1); > + > + rela.r_offset = sec_addr (h->root.u.def.section) + h->root.u.def.value; > + rela.r_info = ELFNN_R_INFO (h->dynindx, R_LARCH_COPY); > + rela.r_addend = 0; > + if (h->root.u.def.section == htab->elf.sdynrelro) > + s = htab->elf.sreldynrelro; > + else > + s = htab->elf.srelbss; > + loongarch_elf_append_rela (output_bfd, s, &rela); > + } > + > /* Mark some specially defined symbols as absolute. */ > if (h == htab->elf.hdynamic || h == htab->elf.hgot || h == htab->elf.hplt) > sym->st_shndx = SHN_ABS; The R_LARCH_COPY is supported in older versions,we removed this feature and are not going to support. 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 F241A38362CF for ; Thu, 1 Sep 2022 01:28:01 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org F241A38362CF 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 AQAAf8Dx_2ufChBjHV4OAA--.60695S3; Thu, 01 Sep 2022 09:28:00 +0800 (CST) Subject: Re: [PATCH] LoongArch: Use copy relocation for %pc_lo12 against external symbol To: Xi Ruoyao , binutils@sourceware.org Cc: WANG Xuerui , chenglulu@loongson.cn References: <20220831132259.71417-1-xry111@xry111.site> From: liuzhensong Message-ID: Date: Thu, 1 Sep 2022 09:27:59 +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: <20220831132259.71417-1-xry111@xry111.site> Content-Type: multipart/alternative; boundary="------------CEFF25B349C46D131BF4FA48" Content-Language: en-US X-CM-TRANSID:AQAAf8Dx_2ufChBjHV4OAA--.60695S3 X-Coremail-Antispam: 1UD129KBjvJXoW3JF17ur45Jr15JFWrtrWxCrg_yoW7Gw1rpr y3ur9Igw48XF1fWwnFvay8Zw4aq3yxuayxXa43J3WrCwnrX34YgF4rtrWDXF4UGr47XrWj qryjva1UuF18Z3DanT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUvjb7Iv0xC_Kw4lb4IE77IF4wAFF20E14v26r1j6r4UM7CY07I2 0VC2zVCF04k26cxKx2IYs7xG6rWj6s0DM7CIcVAFz4kK6r1j6r18M28lY4IEw2IIxxk0rw A2F7IY1VAKz4vEj48ve4kI8wA2z4x0Y4vE2Ix0cI8IcVAFwI0_Ar0_tr1l84ACjcxK6xII jxv20xvEc7CjxVAFwI0_Cr0_Gr1UM28EF7xvwVC2z280aVAFwI0_Gr1j6F4UJwA2z4x0Y4 vEx4A2jsIEc7CjxVAFwI0_Gr1j6F4UJwAS0I0E0xvYzxvE52x082IY62kv0487McIj6xII jxv20xvE14v26r1j6r18McIj6I8E87Iv67AKxVWUJVW8JwAm72CE4IkC6x0Yz7v_Jr0_Gr 1lF7xvr2IY64vIr41l7480Y4vEI4kI2Ix0rVAqx4xJMxk0xIA0c2IEe2xFo4CEbIxvr21l c2xSY4AK6svPMxAIw28IcxkI7VAKI48JMxC20s026xCaFVCjc4AY6r1j6r4UMI8I3I0E5I 8CrVAFwI0_JrI_JrWlx2IqxVCjr7xvwVAFwI0_JrI_JrWlx4CE17CEb7AF67AKxVWUAVWU twCIc40Y0x0EwIxGrwCI42IY6xIIjxv20xvE14v26r1j6r1xMIIF0xvE2Ix0cI8IcVCY1x 0267AKxVWUJVW8JwCI42IY6xAIw20EY4v20xvaj40_Jr0_JF4lIxAIcVC2z280aVAFwI0_ Jr0_Gr1lIxAIcVC2z280aVCY1x0267AKxVWUJVW8JbIYCTnIWIevJa73UjIFyTuYvjxUqW xRDUUUU X-CM-SenderInfo: holx6xphqv003j6o00pqjv00gofq/ X-Spam-Status: No, score=-8.9 required=5.0 tests=BAYES_00,BODY_8BITS,GIT_PATCH_0,HTML_MESSAGE,KAM_DMARC_STATUS,MIME_CHARSET_FARAWAY,NICE_REPLY_A,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: Message-ID: <20220901012759.Pr7Pj1KD0T7d-0R5uJdJ5hWYGpIIXPYDBrzrezrHezY@z> This is a multi-part message in MIME format. --------------CEFF25B349C46D131BF4FA48 Content-Type: text/plain; charset=gbk; format=flowed Content-Transfer-Encoding: 8bit 在 2022/8/31 下午9:22, Xi Ruoyao 写道: > We'd like to use PC-relative addressing instead of GOT for external > symbols in main executable images. This can improve code locality. > Doing so will need to implement copy relocation. > > Despite there was a comment saying "Glibc does not support copy > relocation yet", I've run a very simple test and it seems copy > relocation is handled by ld.so without a problem: > > pcalau12i $a1, %pc_hi20(stdout) > ld.d $a1, $a1, %pc_lo12(stdout) > pcalau12i $a0, %pc_hi20(msg) > addi.d $a0, $a0, %pc_lo12(msg) > pcalau12i $ra, %pc_hi20(fputs) > jirl $ra, $ra, %pc_lo12(fputs) > > With this patch, R_LARCH_COPY is correctly emitted for "stdout" in > Glibc, and the test program runs and outputs "Hello world" in "msg" > successfully. > --- > bfd/elfnn-loongarch.c | 93 ++++++++++++++++++++++++++++++++++++++----- > 1 file changed, 84 insertions(+), 9 deletions(-) > > diff --git a/bfd/elfnn-loongarch.c b/bfd/elfnn-loongarch.c > index ed42b8b6770..0cf24bd24d8 100644 > --- a/bfd/elfnn-loongarch.c > +++ b/bfd/elfnn-loongarch.c > @@ -743,14 +743,26 @@ loongarch_elf_check_relocs (bfd *abfd, struct bfd_link_info *info, > h->non_got_ref = 1; > break; > > - case R_LARCH_PCALA_HI20: > + case R_LARCH_PCALA_LO12: > if (h != NULL) > { > - /* For pcalau12i + jirl. */ > - h->needs_plt = 1; > - if (h->plt.refcount < 0) > - h->plt.refcount = 0; > - h->plt.refcount++; > + /* Check if it's a jirl instruction. */ > + bfd_byte *contents = elf_section_data (sec)->this_hdr.contents; > + uint32_t insn = 0; > + > + if (contents || bfd_malloc_and_get_section (abfd, sec, &contents)) > + memcpy(&insn, contents + rel->r_offset, sizeof(insn)); > + > + if ((insn & 0xfc000000) == 0x4c000000) > + { > + /* For pcalau12i + jirl. */ > + h->needs_plt = 1; > + if (h->plt.refcount < 0) > + h->plt.refcount = 0; > + h->plt.refcount++; > + } > + else > + need_dynreloc = 1; > > h->non_got_ref = 1; > h->pointer_equality_needed = 1; > @@ -949,7 +961,9 @@ loongarch_elf_adjust_dynamic_symbol (struct bfd_link_info *info, > struct elf_link_hash_entry *h) > { > struct loongarch_elf_link_hash_table *htab; > + struct loongarch_elf_link_hash_entry *hent; > bfd *dynobj; > + asection *s, *srel; > > htab = loongarch_elf_hash_table (info); > BFD_ASSERT (htab != NULL); > @@ -999,9 +1013,52 @@ loongarch_elf_adjust_dynamic_symbol (struct bfd_link_info *info, > return true; > } > > - /* R_LARCH_COPY is not adept glibc, not to generate. */ > - /* Can not print anything, because make check ld. */ > - return true; > + /* If copy relocations is disabled via -z nocopyreloc, or we don't find any > + dynamic relocs in read-only sections, avoid the copy reloc. */ > + if (info->nocopyreloc || !_bfd_elf_readonly_dynrelocs (h)) > + h->non_got_ref = 0; > + > + if (!h->non_got_ref) > + return true; > + > + /* We must allocate the symbol in our .dynbss section, which will > + become part of the .bss section of the executable. There will be > + an entry for this symbol in the .dynsym section. The dynamic > + object will contain position independent code, so all references > + from the dynamic object to this symbol will go through the global > + offset table. The dynamic linker will use the .dynsym entry to > + determine the address it must put in the global offset table, so > + both the dynamic object and the regular object will refer to the > + same memory location for the variable. */ > + > + /* Generate a copy relocation to tell the dynamic linker to copy the > + initial value out of the dynamic object and into the runtime process > + image. We need to remember the offset into the .rel.bss section we > + are going to use. */ > + hent = (struct loongarch_elf_link_hash_entry *) h; > + if (hent->tls_type & ~GOT_NORMAL) > + { > + s = htab->sdyntdata; > + srel = htab->elf.srelbss; > + } > + else if (h->root.u.def.section->flags & SEC_READONLY) > + { > + s = htab->elf.sdynrelro; > + srel = htab->elf.sreldynrelro; > + } > + else > + { > + s = htab->elf.sdynbss; > + srel = htab->elf.srelbss; > + } > + > + if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0) > + { > + srel->size += sizeof (ElfNN_External_Rela); > + h->needs_copy = 1; > + } > + > + return _bfd_elf_adjust_dynamic_copy (info, h, s); > } > > /* Allocate space in .plt, .got and associated reloc sections for > @@ -3702,6 +3759,24 @@ loongarch_elf_finish_dynamic_symbol (bfd *output_bfd, > loongarch_elf_append_rela (output_bfd, srela, &rela); > } > > + if (h->needs_copy) > + { > + Elf_Internal_Rela rela; > + asection *s; > + > + /* This symbols needs a copy reloc. Set it up. */ > + BFD_ASSERT (h->dynindx != -1); > + > + rela.r_offset = sec_addr (h->root.u.def.section) + h->root.u.def.value; > + rela.r_info = ELFNN_R_INFO (h->dynindx, R_LARCH_COPY); > + rela.r_addend = 0; > + if (h->root.u.def.section == htab->elf.sdynrelro) > + s = htab->elf.sreldynrelro; > + else > + s = htab->elf.srelbss; > + loongarch_elf_append_rela (output_bfd, s, &rela); > + } > + > /* Mark some specially defined symbols as absolute. */ > if (h == htab->elf.hdynamic || h == htab->elf.hgot || h == htab->elf.hplt) > sym->st_shndx = SHN_ABS; The R_LARCH_COPY is supported in older versions,we removed this feature and are not going to support. --------------CEFF25B349C46D131BF4FA48--