From patchwork Thu May 30 12:20:21 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: mengqinggang X-Patchwork-Id: 91151 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id C8D5B384F4BE for ; Thu, 30 May 2024 12:21:02 +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 9B0183858288 for ; Thu, 30 May 2024 12:20:29 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 9B0183858288 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 9B0183858288 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=1717071636; cv=none; b=NfexlO8Z/lLbTEV0opsgg7VoRFfq5IJzkMW912d5yGLTeUr/CBvjLzbfgCFsni4SguQqDnAngOValIQr7/WCTBxouxzYgJlMjoQN6yvzb2hwA7SV7zzTIQfs/ANDTjxGwyk1VYcxaLVkOdGogw6j7Qe7nzcGc+IkjwHepuj/r98= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1717071636; c=relaxed/simple; bh=bQuPr1PGU+TFNHzq3pXtQiMcZm4bUnGt+9aWKqLMGZc=; h=From:To:Subject:Date:Message-Id:MIME-Version; b=Tt+1WzEMeve7nuts5SgEvJkdvL9DURx+p3zhc3PDHMyu8iU58I8CBET5/2EkiUZhSkjbL3BP/LHi02cQ5fpdDALhsa3IlKcxI8t2OniEqMKQaFl5bSG8DSq70+5YztnA1+t0KAkrIgDAkr9/I/pdLoKhD36t1ACNM7xyl1uLEwY= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from loongson.cn (unknown [10.2.6.5]) by gateway (Coremail) with SMTP id _____8DxSuoIb1hmPKMBAA--.7126S3; Thu, 30 May 2024 20:20:24 +0800 (CST) Received: from 5.5.5 (unknown [10.2.6.5]) by localhost.localdomain (Coremail) with SMTP id AQAAf8BxXccGb1hmwTEOAA--.37759S2; Thu, 30 May 2024 20:20:22 +0800 (CST) From: mengqinggang To: binutils@sourceware.org Cc: xuchenghua@loongson.cn, chenglulu@loongson.cn, cailulu@loongson.cn, xry111@xry111.site, i.swmail@xen0n.name, maskray@google.com, luweining@loongson.cn, hejinyang@loongson.cn, mengqinggang@loongson.cn, WANG Xuerui Subject: [PATCH v1] LoongArch: Make align symbol be in same section with alignment directive Date: Thu, 30 May 2024 20:20:21 +0800 Message-Id: <20240530122021.221595-1-mengqinggang@loongson.cn> X-Mailer: git-send-email 2.39.3 MIME-Version: 1.0 X-CM-TRANSID: AQAAf8BxXccGb1hmwTEOAA--.37759S2 X-CM-SenderInfo: 5phqw15lqjwttqj6z05rqj20fqof0/ X-Coremail-Antispam: 1Uk129KBj93XoWfGw17JFW3GFWxXr48Ar4UKFX_yoWDXFWrpF yUAr90kFW8CFn7Gw1qq345GF47X397uF12q3WxKw4IkrsxJFy8Zw4vqry5XFWDGw4kury5 Xr18Ka1j9F1vyagCm3ZEXasCq-sJn29KB7ZKAUJUUUUU529EdanIXcx71UUUUU7KY7ZEXa sCq-sGcSsGvfJ3Ic02F40EFcxC0VAKzVAqx4xG6I80ebIjqfuFe4nvWSU5nxnvy29KBjDU 0xBIdaVrnRJUUUk2b4IE77IF4wAFF20E14v26r1j6r4UM7CY07I20VC2zVCF04k26cxKx2 IYs7xG6rWj6s0DM7CIcVAFz4kK6r106r15M28lY4IEw2IIxxk0rwA2F7IY1VAKz4vEj48v e4kI8wA2z4x0Y4vE2Ix0cI8IcVAFwI0_Jr0_JF4l84ACjcxK6xIIjxv20xvEc7CjxVAFwI 0_Jr0_Gr1l84ACjcxK6I8E87Iv67AKxVW8Jr0_Cr1UM28EF7xvwVC2z280aVCY1x0267AK xVW8Jr0_Cr1UM2AIxVAIcxkEcVAq07x20xvEncxIr21l57IF6xkI12xvs2x26I8E6xACxx 1l5I8CrVACY4xI64kE6c02F40Ex7xfMcIj6xIIjxv20xvE14v26r1j6r18McIj6I8E87Iv 67AKxVWUJVW8JwAm72CE4IkC6x0Yz7v_Jr0_Gr1lF7xvr2IYc2Ij64vIr41l42xK82IYc2 Ij64vIr41l4I8I3I0E4IkC6x0Yz7v_Jr0_Gr1lx2IqxVAqx4xG67AKxVWUJVWUGwC20s02 6x8GjcxK67AKxVWUGVWUWwC2zVAF1VAY17CE14v26r1q6r43MIIYrxkI7VAKI48JMIIF0x vE2Ix0cI8IcVAFwI0_Jr0_JF4lIxAIcVC0I7IYx2IY6xkF7I0E14v26r1j6r4UMIIF0xvE 42xK8VAvwI8IcIk0rVWUJVWUCwCI42IY6I8E87Iv67AKxVW8JVWxJwCI42IY6I8E87Iv6x kF7I0E14v26r4j6r4UJbIYCTnIWIevJa73UjIFyTuYvjxU25EfUUUUU X-Spam-Status: No, score=-12.8 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.30 Precedence: list List-Id: Binutils mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: binutils-bounces+patchwork=sourceware.org@sourceware.org R_LARCH_ALIGN (psABI v2.30) requires a symbol index. The symbol is only created at the first time to handle alignment directive. This means that all other sections may use this symbol. If the section of this symbol is discarded, there may be problems. Search it in its own section. Remove elf_backend_data.is_rela_normal() function added at commit daeda14191c. Co-authored-by: Jinyang He Reported-by: WANG Xuerui Link: https://lore.kernel.org/loongarch/2abbb633-a10e-71cc-a5e1-4d9e39074066@loongson.cn/T/#t --- bfd/elf-bfd.h | 4 -- bfd/elflink.c | 5 +- bfd/elfnn-loongarch.c | 16 ------ bfd/elfxx-target.h | 5 -- gas/config/tc-loongarch.c | 63 +++++++++++++++++++++- gas/config/tc-loongarch.h | 3 ++ gas/testsuite/gas/loongarch/relax-align2.d | 24 +++++++++ gas/testsuite/gas/loongarch/relax-align2.s | 11 ++++ gas/testsuite/gas/loongarch/relax_align.d | 6 +-- 9 files changed, 104 insertions(+), 33 deletions(-) create mode 100644 gas/testsuite/gas/loongarch/relax-align2.d create mode 100644 gas/testsuite/gas/loongarch/relax-align2.s diff --git a/bfd/elf-bfd.h b/bfd/elf-bfd.h index 15a7156b3da..45d36a78976 100644 --- a/bfd/elf-bfd.h +++ b/bfd/elf-bfd.h @@ -1727,10 +1727,6 @@ struct elf_backend_data backend relocate_section routine for relocatable linking. */ unsigned rela_normal : 1; - /* Whether a relocation is rela_normal. Compared with rela_normal, - is_rela_normal can set part of relocations to rela_normal. */ - bool (*is_rela_normal) (Elf_Internal_Rela *); - /* Set if DT_REL/DT_RELA/DT_RELSZ/DT_RELASZ should not include PLT relocations. */ unsigned dtrel_excludes_plt : 1; diff --git a/bfd/elflink.c b/bfd/elflink.c index 6db6a9c0b47..cca0c67f960 100644 --- a/bfd/elflink.c +++ b/bfd/elflink.c @@ -11889,10 +11889,7 @@ elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd) { rel_hash = PTR_ADD (esdo->rela.hashes, esdo->rela.count); rela_hash_list = rel_hash; - if (bed->is_rela_normal != NULL) - rela_normal = bed->is_rela_normal (irela); - else - rela_normal = bed->rela_normal; + rela_normal = bed->rela_normal; } irela->r_offset = _bfd_elf_section_offset (output_bfd, diff --git a/bfd/elfnn-loongarch.c b/bfd/elfnn-loongarch.c index e60bd4db51e..93c036f6a53 100644 --- a/bfd/elfnn-loongarch.c +++ b/bfd/elfnn-loongarch.c @@ -5528,21 +5528,6 @@ elf_loongarch64_hash_symbol (struct elf_link_hash_entry *h) return _bfd_elf_hash_symbol (h); } -/* If a relocation is rela_normal and the symbol associated with the - relocation is STT_SECTION type, the addend of the relocation would add - sec->output_offset when partial linking (ld -r). - See elf_backend_data.rela_normal and elf_link_input_bfd(). - The addend of R_LARCH_ALIGN is used to represent the first and third - expression of .align, it should be a constant when linking. */ - -static bool -loongarch_elf_is_rela_normal (Elf_Internal_Rela *rel) -{ - if (R_LARCH_ALIGN == ELFNN_R_TYPE (rel->r_info)) - return false; - return true; -} - #define TARGET_LITTLE_SYM loongarch_elfNN_vec #define TARGET_LITTLE_NAME "elfNN-loongarch" #define ELF_ARCH bfd_arch_loongarch @@ -5578,7 +5563,6 @@ loongarch_elf_is_rela_normal (Elf_Internal_Rela *rel) #define elf_backend_grok_psinfo loongarch_elf_grok_psinfo #define elf_backend_hash_symbol elf_loongarch64_hash_symbol #define bfd_elfNN_bfd_relax_section loongarch_elf_relax_section -#define elf_backend_is_rela_normal loongarch_elf_is_rela_normal #define elf_backend_dtrel_excludes_plt 1 diff --git a/bfd/elfxx-target.h b/bfd/elfxx-target.h index 6e9b3f8d0a3..075f1bdd433 100644 --- a/bfd/elfxx-target.h +++ b/bfd/elfxx-target.h @@ -709,10 +709,6 @@ #define elf_backend_rela_normal 0 #endif -#ifndef elf_backend_is_rela_normal -#define elf_backend_is_rela_normal NULL -#endif - #ifndef elf_backend_dtrel_excludes_plt #define elf_backend_dtrel_excludes_plt 0 #endif @@ -959,7 +955,6 @@ static const struct elf_backend_data elfNN_bed = elf_backend_default_use_rela_p, elf_backend_rela_plts_and_copies_p, elf_backend_rela_normal, - elf_backend_is_rela_normal, elf_backend_dtrel_excludes_plt, elf_backend_sign_extend_vma, elf_backend_want_got_plt, diff --git a/gas/config/tc-loongarch.c b/gas/config/tc-loongarch.c index 00b8c80c3de..e7de9d86313 100644 --- a/gas/config/tc-loongarch.c +++ b/gas/config/tc-loongarch.c @@ -419,6 +419,55 @@ loongarch_target_format () return LARCH_opts.ase_lp64 ? "elf64-loongarch" : "elf32-loongarch"; } +typedef struct +{ + unsigned int sec_id; + symbolS *s; +} align_sec_sym; + +static htab_t align_hash; + +static hashval_t +align_sec_sym_hash (const void *entry) +{ + const align_sec_sym *e = entry; + return (hashval_t) (e->sec_id); +} + +static int +align_sec_sym_eq (const void *entry1, const void *entry2) +{ + const align_sec_sym *e1 = entry1, *e2 = entry2; + return e1->sec_id == e2->sec_id; +} + +/* Make align symbol be in same section with alignment directive. + If the symbol is only created at the first time to handle alignment + directive. This means that all other sections may use this symbol. + If the section of this symbol is discarded, there may be problems. */ + +static symbolS *get_align_symbol (segT sec) +{ + align_sec_sym search = { sec->id, NULL }; + align_sec_sym *pentry = htab_find (align_hash, &search); + if (pentry) + return pentry->s; + + /* If we not find the symbol in this section. Create and insert it. */ + symbolS *s = (symbolS *)local_symbol_make (".Lla-relax-align", sec, + &zero_address_frag, 0); + align_sec_sym entry = { sec->id, s }; + align_sec_sym **slot = (align_sec_sym **) htab_find_slot (align_hash, + &entry, INSERT); + if (slot == NULL) + return NULL; + *slot = (align_sec_sym *) xmalloc (sizeof (align_sec_sym)); + if (*slot == NULL) + return NULL; + **slot = entry; + return entry.s; +} + void md_begin () { @@ -440,11 +489,21 @@ md_begin () it->name, it->format, it->macro); } + align_hash = htab_create (10, align_sec_sym_hash, align_sec_sym_eq, free); + /* FIXME: expressionS use 'offsetT' as constant, * we want this is 64-bit type. */ assert (8 <= sizeof (offsetT)); } +/* Called just before the assembler exits. */ + +void +loongarch_md_end (void) +{ + htab_delete (align_hash); +} + unsigned long loongarch_mach (void) { @@ -1826,7 +1885,9 @@ loongarch_frag_align_code (int n, int max) if (fragP->fr_subtype != 0 && offset > fragP->fr_subtype). */ if (align_max) { - s = symbol_find (now_seg->name); + s = get_align_symbol (now_seg); + if (!s) + as_fatal (_("internal error: cannot get align symbol")); addend = ALIGN_MAX_ADDEND (n, max); } diff --git a/gas/config/tc-loongarch.h b/gas/config/tc-loongarch.h index f53e3584c7c..20434ac8a01 100644 --- a/gas/config/tc-loongarch.h +++ b/gas/config/tc-loongarch.h @@ -32,6 +32,9 @@ extern unsigned long loongarch_mach (void); #define WORKING_DOT_WORD 1 #define REPEAT_CONS_EXPRESSIONS +#define md_end loongarch_md_end +extern void loongarch_md_end (void); + /* Early than md_begin. */ #define md_after_parse_args loongarch_after_parse_args extern void loongarch_after_parse_args (void); diff --git a/gas/testsuite/gas/loongarch/relax-align2.d b/gas/testsuite/gas/loongarch/relax-align2.d new file mode 100644 index 00000000000..cbef84f8777 --- /dev/null +++ b/gas/testsuite/gas/loongarch/relax-align2.d @@ -0,0 +1,24 @@ +#as: --no-warn +#readelf: -rsW +#skip: loongarch32-*-* + +Relocation section '\.rela\.text' at offset .* contains 2 entries: +.* +0+04[ ]+0000000000000066[ ]+R_LARCH_ALIGN[ ]+c +0+14[ ]+0000000500000066[ ]+R_LARCH_ALIGN[ ]+0+[ ]+\.Lla-relax-align \+ 404 + +Relocation section '\.rela\.text2' at offset .* contains 2 entries: +.* +0+04[ ]+0000000000000066[ ]+R_LARCH_ALIGN[ ]+c +0+14[ ]+0000000600000066[ ]+R_LARCH_ALIGN[ ]+0+[ ]+\.Lla-relax-align \+ 404 + +Symbol table '\.symtab' contains .* entries: +#... +[ ]+.*:[ ]+0+[ ]+0[ ]+SECTION[ ]+LOCAL[ ]+DEFAULT[ ]+1[ ]+\.text +#... +[ ]+.*:[ ]+0+[ ]+0[ ]+SECTION[ ]+LOCAL[ ]+DEFAULT[ ]+5[ ]+\.text2 +#... +[ ]+.*:[ ]+0+[ ]+0[ ]+NOTYPE[ ]+LOCAL[ ]+DEFAULT[ ]+1[ ]+\.Lla-relax-align +#... +[ ]+.*:[ ]+0+[ ]+0[ ]+NOTYPE[ ]+LOCAL[ ]+DEFAULT[ ]+5[ ]+\.Lla-relax-align +#pass diff --git a/gas/testsuite/gas/loongarch/relax-align2.s b/gas/testsuite/gas/loongarch/relax-align2.s new file mode 100644 index 00000000000..6cd6bd8731b --- /dev/null +++ b/gas/testsuite/gas/loongarch/relax-align2.s @@ -0,0 +1,11 @@ +.section ".text", "ax" +nop +.align 4 +nop +.align 4, , 4 + +.section ".text2", "ax" +nop +.align 4 +nop +.align 4, , 4 diff --git a/gas/testsuite/gas/loongarch/relax_align.d b/gas/testsuite/gas/loongarch/relax_align.d index acd215a4abc..fc1fd032611 100644 --- a/gas/testsuite/gas/loongarch/relax_align.d +++ b/gas/testsuite/gas/loongarch/relax_align.d @@ -7,7 +7,7 @@ Disassembly of section .text: -[ ]*0000000000000000 <.text>: +[ ]*0000000000000000 <.Lla-relax-align>: [ ]+0:[ ]+4c000020[ ]+ret [ ]+4:[ ]+03400000[ ]+nop [ ]+4: R_LARCH_ALIGN[ ]+\*ABS\*\+0xc @@ -20,12 +20,12 @@ Disassembly of section .text: [ ]+1c:[ ]+03400000[ ]+nop [ ]+20:[ ]+4c000020[ ]+ret [ ]+24:[ ]+03400000[ ]+nop -[ ]+24: R_LARCH_ALIGN[ ]+.text\+0x104 +[ ]+24: R_LARCH_ALIGN[ ]+.Lla-relax-align\+0x104 [ ]+28:[ ]+03400000[ ]+nop [ ]+2c:[ ]+03400000[ ]+nop [ ]+30:[ ]+4c000020[ ]+ret [ ]+34:[ ]+03400000[ ]+nop -[ ]+34: R_LARCH_ALIGN[ ]+.text\+0xb04 +[ ]+34: R_LARCH_ALIGN[ ]+.Lla-relax-align\+0xb04 [ ]+38:[ ]+03400000[ ]+nop [ ]+3c:[ ]+03400000[ ]+nop [ ]+40:[ ]+4c000020[ ]+ret