From patchwork Wed Jul 19 02:17:21 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: mengqinggang X-Patchwork-Id: 122359 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:c923:0:b0:3e4:2afc:c1 with SMTP id j3csp2142642vqt; Tue, 18 Jul 2023 19:17:38 -0700 (PDT) X-Google-Smtp-Source: APBJJlGDE/X+7czj3c0fwuerylv5rXnoa9ZhSW52A4Fj6bTTwlkWc16tA+KTCwjmjLmTxEIo5p0K X-Received: by 2002:a17:906:5345:b0:993:f996:52cf with SMTP id j5-20020a170906534500b00993f99652cfmr1319573ejo.28.1689733057900; Tue, 18 Jul 2023 19:17:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689733057; cv=none; d=google.com; s=arc-20160816; b=eFWYWyX5lpjeXuAkI68VXddrLdYG0SWcIskLSmBRxDDb//gNgnx2CYOakpUM9F+QMQ JCuC4+2B16wCGiAXM7IK1IwptpwT9VYU4a88UbbMAa7ducpjLpWBuHCEDrasREUyMjMo ldyn0zGwznEGz48+uea8zaZ6j8Nrfu05MvVELSN4LcjzZWqfiKmX5ctWW9sroZ3EUBXd 61H9exykyoqKyNQ/opgyHdqV/dvRL0yxw3fOhv1ZnnjE3+CPBtlf7/1taXYl67Y/KAth QRsWxPiMNu+EA+V8gOMLQvq0cY6fQJm8vbBjerF27bS+yW9LQdV1LJW+e/Bvi6uPTnDL J+SQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:message-id:date:subject:cc:to:from:dmarc-filter :delivered-to; bh=6v/87oZI0nbO56vjobQb762MO5kM8ODRZe6qsCc9UPc=; fh=QBSRBSUnFSUbjYPlYXRvmmbzYsSiOdYMaNH5lUTXA7g=; b=SG9uyDiOqyICJdrxKxTs+Pv0ZpgzaiyYMdg074dWntToqRBNAMGZO/FUMjYdwhGh/k y7toyI39xYG+U3fJ53AKKv+BoZ/NgC7djI1eeWiqVSkt3QO5j+kS+N5avLT4U6KnjqZg qFqrDOSyUt9LHMBk4++IA3rM3paHrK48Q6JPKc/8evOHPC3iPsEN+TfslETcYlbBj8SO J+bw9P+3qd5lmHfyML7o0c+O8fmZH3rhoz9ZLJL/qu8KmfuHrF+DD4jXqJoshoX7R27L PKWrcmf/h2pIyIfaiNoS2ssanBYikSEkfKr1Iy1Guhgm0tGhd1okuwdLSbLYNPLYg15D 47SA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of binutils-bounces+ouuuleilei=gmail.com@sourceware.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="binutils-bounces+ouuuleilei=gmail.com@sourceware.org" Received: from server2.sourceware.org (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id t14-20020a170906178e00b0098d2f718ac3si1910753eje.609.2023.07.18.19.17.37 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 18 Jul 2023 19:17:37 -0700 (PDT) Received-SPF: pass (google.com: domain of binutils-bounces+ouuuleilei=gmail.com@sourceware.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; spf=pass (google.com: domain of binutils-bounces+ouuuleilei=gmail.com@sourceware.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="binutils-bounces+ouuuleilei=gmail.com@sourceware.org" Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 1847F385C6D4 for ; Wed, 19 Jul 2023 02:17:35 +0000 (GMT) X-Original-To: binutils@sourceware.org Delivered-To: binutils@sourceware.org Received: from mail.loongson.cn (mail.loongson.cn [114.242.206.163]) by sourceware.org (Postfix) with ESMTP id C8501385800A for ; Wed, 19 Jul 2023 02:17:27 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org C8501385800A 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 loongson.cn (unknown [10.2.5.5]) by gateway (Coremail) with SMTP id _____8AxEvC0R7dkuwwHAA--.17046S3; Wed, 19 Jul 2023 10:17:25 +0800 (CST) Received: from 5.5.5 (unknown [10.2.5.5]) by localhost.localdomain (Coremail) with SMTP id AQAAf8CxvM6zR7dkNxs0AA--.12840S2; Wed, 19 Jul 2023 10:17:23 +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] LoongArch: ld: Simplify inserting IRELATIVE relocations to .rela.dyn Date: Wed, 19 Jul 2023 10:17:21 +0800 Message-Id: <20230719021721.776961-1-mengqinggang@loongson.cn> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 X-CM-TRANSID: AQAAf8CxvM6zR7dkNxs0AA--.12840S2 X-CM-SenderInfo: 5phqw15lqjwttqj6z05rqj20fqof0/ X-Coremail-Antispam: 1Uk129KBj93XoW3WF48GFyDZFW5Zr4fWw1Utwc_yoWxZr18pr 9rury7Gr4rJF4xWwnrt3y7Zw43u397ury7KF13tw4SkrsxXryaqF4I9rW7ZF15K34xuryj qryjva15ZF18AFgCm3ZEXasCq-sJn29KB7ZKAUJUUUUr529EdanIXcx71UUUUU7KY7ZEXa sCq-sGcSsGvfJ3Ic02F40EFcxC0VAKzVAqx4xG6I80ebIjqfuFe4nvWSU5nxnvy29KBjDU 0xBIdaVrnRJUUU90b4IE77IF4wAFF20E14v26r1j6r4UM7CY07I20VC2zVCF04k26cxKx2 IYs7xG6rWj6s0DM7CIcVAFz4kK6r1j6r18M28lY4IEw2IIxxk0rwA2F7IY1VAKz4vEj48v e4kI8wA2z4x0Y4vE2Ix0cI8IcVAFwI0_JFI_Gr1l84ACjcxK6xIIjxv20xvEc7CjxVAFwI 0_Jr0_Gr1l84ACjcxK6I8E87Iv67AKxVWxJVW8Jr1l84ACjcxK6I8E87Iv6xkF7I0E14v2 6r4UJVWxJr1ln4kS14v26r1Y6r17M2AIxVAIcxkEcVAq07x20xvEncxIr21l57IF6xkI12 xvs2x26I8E6xACxx1l5I8CrVACY4xI64kE6c02F40Ex7xfMcIj6xIIjxv20xvE14v26r1Y 6r17McIj6I8E87Iv67AKxVWUJVW8JwAm72CE4IkC6x0Yz7v_Jr0_Gr1lF7xvr2IYc2Ij64 vIr41l42xK82IYc2Ij64vIr41l4I8I3I0E4IkC6x0Yz7v_Jr0_Gr1l4IxYO2xFxVAFwI0_ Jrv_JF1lx2IqxVAqx4xG67AKxVWUJVWUGwC20s026x8GjcxK67AKxVWUGVWUWwC2zVAF1V AY17CE14v26r1q6r43MIIYrxkI7VAKI48JMIIF0xvE2Ix0cI8IcVAFwI0_Jr0_JF4lIxAI cVC0I7IYx2IY6xkF7I0E14v26r1j6r4UMIIF0xvE42xK8VAvwI8IcIk0rVWUJVWUCwCI42 IY6I8E87Iv67AKxVWUJVW8JwCI42IY6I8E87Iv6xkF7I0E14v26r1j6r4UYxBIdaVFxhVj vjDU0xZFpf9x07jjwZcUUUUU= X-Spam-Status: No, score=-12.3 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 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: , Errors-To: binutils-bounces+ouuuleilei=gmail.com@sourceware.org Sender: "Binutils" X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1771813530622928762 X-GMAIL-MSGID: 1771813530622928762 In LoongArch, the R_LARCH_IRELATIVE relocations for local ifunc symbols are in .rela.dyn. Before, this is done by loongarch_elf_finish_dynamic_sections. But this function is called after elf_link_sort_relocs, it need to find a null slot to insert IRELATIVE relocation. Now, it is processed by elf_loongarch_output_arch_local_syms before elf_link_sort_relocs, just need to call loongarch_elf_append_rela to insert IRELATIVE relocation. bfd/ChangeLog: * elfnn-loongarch.c (elfNN_allocate_local_ifunc_dynrelocs): Return type change to int. (loongarch_elf_size_dynamic_sections): Delete (void *). (loongarch_elf_finish_dynamic_symbol): Use loongarch_elf_append_rela insert IRELATIVE relocation to .rela.dyn. (elfNN_loongarch_finish_local_dynamic_symbol): Return type change to int. (loongarch_elf_finish_dynamic_sections): Delete process of local ifunc symbols. (elf_backend_output_arch_local_syms): New. ld/ChangeLog: * testsuite/ld-loongarch-elf/local-ifunc-reloc.d: Regenerated. --- bfd/elfnn-loongarch.c | 67 +++++++++---------- .../ld-loongarch-elf/local-ifunc-reloc.d | 2 +- 2 files changed, 34 insertions(+), 35 deletions(-) diff --git a/bfd/elfnn-loongarch.c b/bfd/elfnn-loongarch.c index d3d8419d80b..67a514b9b2b 100644 --- a/bfd/elfnn-loongarch.c +++ b/bfd/elfnn-loongarch.c @@ -1538,7 +1538,7 @@ elfNN_allocate_ifunc_dynrelocs (struct elf_link_hash_entry *h, void *inf) /* Allocate space in .plt, .got and associated reloc sections for ifunc dynamic relocs. */ -static bool +static int elfNN_allocate_local_ifunc_dynrelocs (void **slot, void *inf) { struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) *slot; @@ -1700,7 +1700,7 @@ loongarch_elf_size_dynamic_sections (bfd *output_bfd, /* Allocate .plt and .got entries, and space for local ifunc symbols. */ htab_traverse (htab->loc_hash_table, - (void *) elfNN_allocate_local_ifunc_dynrelocs, info); + elfNN_allocate_local_ifunc_dynrelocs, info); /* Don't allocate .got.plt section if there are no PLT. */ if (htab->elf.sgotplt && htab->elf.sgotplt->size == GOTPLT_HEADER_SIZE @@ -4001,12 +4001,6 @@ 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) { @@ -4016,7 +4010,6 @@ 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) { @@ -4074,26 +4067,7 @@ loongarch_elf_finish_dynamic_symbol (bfd *output_bfd, + h->root.u.def.section->output_section->vma + h->root.u.def.section->output_offset); - /* Find the space after dyn sort. */ - while (slot == last_slot || slot->r_offset != 0) - { - if (slot != last_slot) - { - slot++; - continue; - } - - 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++; + loongarch_elf_append_rela (output_bfd, relplt, &rela); } else { @@ -4260,7 +4234,7 @@ loongarch_finish_dyn (bfd *output_bfd, struct bfd_link_info *info, bfd *dynobj, /* Finish up local dynamic symbol handling. We set the contents of various dynamic sections here. */ -static bool +static int elfNN_loongarch_finish_local_dynamic_symbol (void **slot, void *inf) { struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) *slot; @@ -4269,6 +4243,33 @@ elfNN_loongarch_finish_local_dynamic_symbol (void **slot, void *inf) return loongarch_elf_finish_dynamic_symbol (info->output_bfd, info, h, NULL); } +/* Value of struct elf_backend_data->elf_backend_output_arch_local_syms, + this function is called before elf_link_sort_relocs. + So relocation R_LARCH_IRELATIVE for local ifunc can be append to + .rela.dyn (.rela.got) by loongarch_elf_append_rela. */ + +static bool +elf_loongarch_output_arch_local_syms + (bfd *output_bfd ATTRIBUTE_UNUSED, + struct bfd_link_info *info, + void *flaginfo ATTRIBUTE_UNUSED, + int (*func) (void *, const char *, + Elf_Internal_Sym *, + asection *, + struct elf_link_hash_entry *) ATTRIBUTE_UNUSED) +{ + struct loongarch_elf_link_hash_table *htab = loongarch_elf_hash_table (info); + if (htab == NULL) + return false; + + /* Fill PLT and GOT entries for local STT_GNU_IFUNC symbols. */ + htab_traverse (htab->loc_hash_table, + elfNN_loongarch_finish_local_dynamic_symbol, + info); + + return true; +} + static bool loongarch_elf_finish_dynamic_sections (bfd *output_bfd, struct bfd_link_info *info) @@ -4347,10 +4348,6 @@ loongarch_elf_finish_dynamic_sections (bfd *output_bfd, elf_section_data (output_section)->this_hdr.sh_entsize = GOT_ENTRY_SIZE; } - /* Fill PLT and GOT entries for local STT_GNU_IFUNC symbols. */ - htab_traverse (htab->loc_hash_table, - (void *) elfNN_loongarch_finish_local_dynamic_symbol, info); - return true; } @@ -4615,6 +4612,8 @@ elf_loongarch64_hash_symbol (struct elf_link_hash_entry *h) #define elf_backend_size_dynamic_sections loongarch_elf_size_dynamic_sections #define elf_backend_relocate_section loongarch_elf_relocate_section #define elf_backend_finish_dynamic_symbol loongarch_elf_finish_dynamic_symbol +#define elf_backend_output_arch_local_syms \ + elf_loongarch_output_arch_local_syms #define elf_backend_finish_dynamic_sections \ loongarch_elf_finish_dynamic_sections #define elf_backend_object_p loongarch_elf_object_p diff --git a/ld/testsuite/ld-loongarch-elf/local-ifunc-reloc.d b/ld/testsuite/ld-loongarch-elf/local-ifunc-reloc.d index 29f2d3f36d3..bf73d9f2875 100644 --- a/ld/testsuite/ld-loongarch-elf/local-ifunc-reloc.d +++ b/ld/testsuite/ld-loongarch-elf/local-ifunc-reloc.d @@ -6,5 +6,5 @@ DYNAMIC RELOCATION RECORDS OFFSET +TYPE +VALUE -[[:xdigit:]]+ R_LARCH_IRELATIVE +\*ABS\*\+0x[[:xdigit:]]+ [[:xdigit:]]+ R_LARCH_64 +test +[[:xdigit:]]+ R_LARCH_IRELATIVE +\*ABS\*\+0x[[:xdigit:]]+