From patchwork Fri Oct 18 10:11:46 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lulu Cai X-Patchwork-Id: 99154 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 30F2D385841E for ; Fri, 18 Oct 2024 10:13: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 2418F3858C35 for ; Fri, 18 Oct 2024 10:11:57 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 2418F3858C35 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 2418F3858C35 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=1729246337; cv=none; b=M4uL+Ma/1XGhJeRsrtQtcFYSWxh4f/GRxcZQkXzD4lGJIJiepNrU+W4/vg0HfRYxTDoL+cDjV8DVlaY9SxH7v96mgbxEP44wLsP5e5VrC0S00lPqxRTMZ+FOCn0jzWfJFzg/2gQgW2KKLfqs1yYo2cJsJ8pcKP8qf9+OZtn/b30= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1729246337; c=relaxed/simple; bh=OOq6ZhZrrKUYCkLY4DZyC76RUe4gjaQgCVFpN1FkcSw=; h=From:To:Subject:Date:Message-Id:MIME-Version; b=u7M2+xGYJu5rfU8QtSjl2BDxWaJYnK8Zoqzc0auT0iEIzNwS2zbZJRs+BTV61Wfz+Bk1qZtL6uJWidni3JXhdos/e6CO89WelX2Lql/CM+ox0iW1IVnK24Vf6vc2PeXYnNsTldfalygnTStZeCetRUsilvSioQoFCWPVtIyhuh4= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from loongson.cn (unknown [10.2.6.5]) by gateway (Coremail) with SMTP id _____8DxeYpkNBJnPpojAA--.51495S3; Fri, 18 Oct 2024 18:11:49 +0800 (CST) Received: from 5.5.5 (unknown [10.2.6.5]) by front1 (Coremail) with SMTP id qMiowMAxQNZjNBJnfMUvAA--.25144S4; Fri, 18 Oct 2024 18:11:48 +0800 (CST) From: Lulu Cai To: binutils@sourceware.org Cc: xuchenghua@loongson.cn, chenglulu@loongson.cn, mengqinggang@loongson.cn, xry111@xry111.site, i.swmail@xen0n.name, maskray@google.com, luweining@loongson.cn, hejinyang@loongson.cn, Lulu Cai Subject: [PATCH] LoongArch: Force relocation for got reference Date: Fri, 18 Oct 2024 18:11:46 +0800 Message-Id: <20241018101146.845705-1-cailulu@loongson.cn> X-Mailer: git-send-email 2.39.3 MIME-Version: 1.0 X-CM-TRANSID: qMiowMAxQNZjNBJnfMUvAA--.25144S4 X-CM-SenderInfo: xfdlz3tox6z05rqj20fqof0/1tbiAgEOB2cQ-iIXsAAAsG X-Coremail-Antispam: 1Uk129KBj93XoW3Xw18ZFW7XF47tFyDGrWDJrc_yoWxtw1Upr W3ZFyFkF48uFZrG39xt34Uurnav3yxWr42ga4SqF18CFs7JryxXrWDtrykXF4UXw4xWr18 Xr4Fqa1UuF1DA3cCm3ZEXasCq-sJn29KB7ZKAUJUUUUU529EdanIXcx71UUUUU7KY7ZEXa sCq-sGcSsGvfJ3Ic02F40EFcxC0VAKzVAqx4xG6I80ebIjqfuFe4nvWSU5nxnvy29KBjDU 0xBIdaVrnRJUUUkFb4IE77IF4wAFF20E14v26r1j6r4UM7CY07I20VC2zVCF04k26cxKx2 IYs7xG6rWj6s0DM7CIcVAFz4kK6r1j6r18M28lY4IEw2IIxxk0rwA2F7IY1VAKz4vEj48v e4kI8wA2z4x0Y4vE2Ix0cI8IcVAFwI0_JFI_Gr1l84ACjcxK6xIIjxv20xvEc7CjxVAFwI 0_Gr0_Cr1l84ACjcxK6I8E87Iv67AKxVW8Jr0_Cr1UM28EF7xvwVC2z280aVCY1x0267AK xVW8Jr0_Cr1UM2AIxVAIcxkEcVAq07x20xvEncxIr21l57IF6xkI12xvs2x26I8E6xACxx 1l5I8CrVACY4xI64kE6c02F40Ex7xfMcIj6xIIjxv20xvE14v26r106r15McIj6I8E87Iv 67AKxVWUJVW8JwAm72CE4IkC6x0Yz7v_Jr0_Gr1lF7xvr2IYc2Ij64vIr41l42xK82IYc2 Ij64vIr41l4I8I3I0E4IkC6x0Yz7v_Jr0_Gr1lx2IqxVAqx4xG67AKxVWUJVWUGwC20s02 6x8GjcxK67AKxVWUGVWUWwC2zVAF1VAY17CE14v26r1q6r43MIIYrxkI7VAKI48JMIIF0x vE2Ix0cI8IcVAFwI0_Jr0_JF4lIxAIcVC0I7IYx2IY6xkF7I0E14v26r1j6r4UMIIF0xvE 42xK8VAvwI8IcIk0rVWUJVWUCwCI42IY6I8E87Iv67AKxVWUJVW8JwCI42IY6I8E87Iv6x kF7I0E14v26r1j6r4UYxBIdaVFxhVjvjDU0xZFpf9x07UWHqcUUUUU= X-Spam-Status: No, score=-13.1 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_STATUS, SPF_HELO_NONE, SPF_PASS, TXREP 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 Local absolute symbols are resolved at assembly time and the symbol value is placed in the relocation addend. Non-zero addend will cause an assertion failure at link time. Forces emission of relocations to defer resolution of local abs symbols until link time. bfd/ * elfnn-loongarch.c (loongarch_elf_relax_section): Determine absolute symbols in advance to avoid ld crash. gas/ * config/tc-loongarch.c (loongarch_force_relocation): New function to force relocation. * config/tc-loongarch.h (TC_FORCE_RELOCATION): New macros to force relocation. (loongarch_force_relocation): Function declaration. * testsuite/gas/loongarch/localpic.d: New test. * testsuite/gas/loongarch/localpic.s: New test. --- bfd/elfnn-loongarch.c | 16 ++++++++-------- gas/config/tc-loongarch.c | 24 ++++++++++++++++++++++++ gas/config/tc-loongarch.h | 3 +++ gas/testsuite/gas/loongarch/localpic.d | 22 ++++++++++++++++++++++ gas/testsuite/gas/loongarch/localpic.s | 26 ++++++++++++++++++++++++++ 5 files changed, 83 insertions(+), 8 deletions(-) create mode 100644 gas/testsuite/gas/loongarch/localpic.d create mode 100644 gas/testsuite/gas/loongarch/localpic.s diff --git a/bfd/elfnn-loongarch.c b/bfd/elfnn-loongarch.c index 3b1a6a4f95a..2486aa9500e 100644 --- a/bfd/elfnn-loongarch.c +++ b/bfd/elfnn-loongarch.c @@ -5350,7 +5350,6 @@ loongarch_elf_relax_section (bfd *abfd, asection *sec, bfd_vma symval; asection *sym_sec; bool local_got = false; - bool is_abs_symbol = false; Elf_Internal_Rela *rel = relocs + i; struct elf_link_hash_entry *h = NULL; unsigned long r_type = ELFNN_R_TYPE (rel->r_info); @@ -5456,8 +5455,9 @@ loongarch_elf_relax_section (bfd *abfd, asection *sec, Elf_Internal_Sym *sym = (Elf_Internal_Sym *)symtab_hdr->contents + r_symndx; - if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC - && r_type != R_LARCH_CALL36) + if ((ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC + && r_type != R_LARCH_CALL36) + || sym->st_shndx == SHN_ABS) continue; /* Only TLS instruction sequences that are accompanied by @@ -5486,12 +5486,13 @@ loongarch_elf_relax_section (bfd *abfd, asection *sec, symval = sym->st_value; } symtype = ELF_ST_TYPE (sym->st_info); - is_abs_symbol = sym->st_shndx == SHN_ABS; } else { - if (h != NULL && h->type == STT_GNU_IFUNC - && r_type != R_LARCH_CALL36) + if (h != NULL + && ((h->type == STT_GNU_IFUNC + && r_type != R_LARCH_CALL36) + || bfd_is_abs_section (h->root.u.def.section))) continue; /* The GOT entry of tls symbols must in current execute file or @@ -5533,7 +5534,6 @@ loongarch_elf_relax_section (bfd *abfd, asection *sec, else continue; - is_abs_symbol = bfd_is_abs_section (h->root.u.def.section); if (h && LARCH_REF_LOCAL (info, h)) local_got = true; symtype = h->type; @@ -5566,7 +5566,7 @@ loongarch_elf_relax_section (bfd *abfd, asection *sec, symval += sec_addr (sym_sec); - if (r_type == R_LARCH_GOT_PC_HI20 && (!local_got || is_abs_symbol)) + if (r_type == R_LARCH_GOT_PC_HI20 && !local_got) continue; if (relax_func (abfd, sec, sym_sec, rel, symval, diff --git a/gas/config/tc-loongarch.c b/gas/config/tc-loongarch.c index eee2badcdca..ab98a83e6f8 100644 --- a/gas/config/tc-loongarch.c +++ b/gas/config/tc-loongarch.c @@ -1456,6 +1456,30 @@ md_pcrel_from (fixS *fixP ATTRIBUTE_UNUSED) return 0; } +/* Return 1 if the relocation must be forced, and 0 if the relocation + should never be forced. */ +int +loongarch_force_relocation (struct fix *fixp) +{ + /* Ensure we emit a relocation for every reference to the global + offset table. */ + switch (fixp->fx_r_type) + { + case BFD_RELOC_LARCH_GOT_PC_HI20: + case BFD_RELOC_LARCH_GOT_PC_LO12: + case BFD_RELOC_LARCH_GOT64_PC_LO20: + case BFD_RELOC_LARCH_GOT64_PC_HI12: + case BFD_RELOC_LARCH_GOT_HI20: + case BFD_RELOC_LARCH_GOT_LO12: + case BFD_RELOC_LARCH_GOT64_LO20: + case BFD_RELOC_LARCH_GOT64_HI12: + return 1; + default: + break; + } + return generic_force_reloc (fixp); +} + static void fix_reloc_insn (fixS *fixP, bfd_vma reloc_val, char *buf) { reloc_howto_type *howto; diff --git a/gas/config/tc-loongarch.h b/gas/config/tc-loongarch.h index 64d95e44277..b815ebbef6e 100644 --- a/gas/config/tc-loongarch.h +++ b/gas/config/tc-loongarch.h @@ -74,6 +74,9 @@ extern bool loongarch_frag_align_code (int, int); relaxation, so do not resolve such expressions in the assembler. */ #define md_allow_local_subtract(l,r,s) 0 +#define TC_FORCE_RELOCATION(FIX) loongarch_force_relocation (FIX) +extern int loongarch_force_relocation (struct fix *); + /* If subsy of BFD_RELOC32/64 and PC in same segment, and without relax or PC at start of subsy or with relax but sub_symbol_segment not in SEC_CODE, we generate 32/64_PCREL. */ diff --git a/gas/testsuite/gas/loongarch/localpic.d b/gas/testsuite/gas/loongarch/localpic.d new file mode 100644 index 00000000000..bea1957818b --- /dev/null +++ b/gas/testsuite/gas/loongarch/localpic.d @@ -0,0 +1,22 @@ +#as: +#readelf: -rWs +#name: loongarch64 local PIC + +Relocation section '.rela.text' at offset 0x[0-9a-f]+ contains 12 entries: + Offset Info Type Symbol's Value Symbol's Name \+ Addend +[0-9a-f]+ [0-9a-f]+ R_LARCH_GOT_PC_HI20 [0-9a-f]+ sym \+ 0 +[0-9a-f]+ [0-9a-f]+ R_LARCH_GOT_PC_LO12 [0-9a-f]+ sym \+ 0 +[0-9a-f]+ [0-9a-f]+ R_LARCH_GOT_PC_HI20 [0-9a-f]+ foo \+ 0 +[0-9a-f]+ [0-9a-f]+ R_LARCH_GOT_PC_LO12 [0-9a-f]+ foo \+ 0 +[0-9a-f]+ [0-9a-f]+ R_LARCH_GOT64_PC_LO20 [0-9a-f]+ foo \+ 0 +[0-9a-f]+ [0-9a-f]+ R_LARCH_GOT64_PC_HI12 [0-9a-f]+ foo \+ 0 +[0-9a-f]+ [0-9a-f]+ R_LARCH_GOT_HI20 [0-9a-f]+ foo \+ 0 +[0-9a-f]+ [0-9a-f]+ R_LARCH_GOT_LO12 [0-9a-f]+ foo \+ 0 +[0-9a-f]+ [0-9a-f]+ R_LARCH_GOT_HI20 [0-9a-f]+ sym \+ 0 +[0-9a-f]+ [0-9a-f]+ R_LARCH_GOT_LO12 [0-9a-f]+ sym \+ 0 +[0-9a-f]+ [0-9a-f]+ R_LARCH_GOT64_LO20 [0-9a-f]+ sym \+ 0 +[0-9a-f]+ [0-9a-f]+ R_LARCH_GOT64_HI12 [0-9a-f]+ sym \+ 0 +#... + +[0-9a-f]+: +[0-9a-f]+ 0 NOTYPE LOCAL DEFAULT +[0-9a-f]+ foo + +[0-9a-f]+: 0+abba 0 NOTYPE LOCAL DEFAULT ABS sym +#pass diff --git a/gas/testsuite/gas/loongarch/localpic.s b/gas/testsuite/gas/loongarch/localpic.s new file mode 100644 index 00000000000..55548e4b62d --- /dev/null +++ b/gas/testsuite/gas/loongarch/localpic.s @@ -0,0 +1,26 @@ +.text +foo: + .quad 0 + # 32-bit PC-relative + pcalau12i $a0,%got_pc_hi20(sym) + ld.d $a0,$a0,%got_pc_lo12(sym) + # 64-bit PC-relative + pcalau12i $a0,%got_pc_hi20(foo) + addi.d $a1,$zero,%got_pc_lo12(foo) + lu32i.d $a1,%got64_pc_lo20(foo) + lu52i.d $a1,$a1,%got64_pc_hi12(foo) + ldx.d $a0,$a0,$a1 + + # 32-bit absolute + lu12i.w $a0,%got_hi20(foo) + ori $a0,$a0,%got_lo12(foo) + ld.w $a0,$a0,0 + + #64-bit absolute + lu12i.w $a0,%got_hi20(sym) + ori $a0,$a0,%got_lo12(sym) + lu32i.d $a0,%got64_lo20(sym) + lu52i.d $a0,$a0,%got64_hi12(sym) + ld.d $a0,$a0,0 + +.set sym,0xabba