From patchwork Fri Dec 27 08:21:12 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xi Ruoyao X-Patchwork-Id: 103715 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 5B2F23858C78 for ; Fri, 27 Dec 2024 08:24:55 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 5B2F23858C78 Authentication-Results: sourceware.org; dkim=pass (1024-bit key, unprotected) header.d=xry111.site header.i=@xry111.site header.a=rsa-sha256 header.s=default header.b=LtvJE2iu X-Original-To: binutils@sourceware.org Delivered-To: binutils@sourceware.org Received: from xry111.site (xry111.site [89.208.246.23]) by sourceware.org (Postfix) with ESMTPS id DE41B3858D20 for ; Fri, 27 Dec 2024 08:24:01 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org DE41B3858D20 Authentication-Results: sourceware.org; dmarc=pass (p=reject dis=none) header.from=xry111.site Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=xry111.site ARC-Filter: OpenARC Filter v1.0.0 sourceware.org DE41B3858D20 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=89.208.246.23 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1735287842; cv=none; b=MblZjs1GWZBPxCxk3zZcRN8E5dHm85JFlg/5vylisZYRlzFvodhHsRWgh6nVyGbwau3nWF/8IEkCtuRNyxZTdhvSlUYkZfB7Qf0oy6wjNdWzFZFKEW2+T8U3pCjrzlqCLxlRMk/iUNXUkhw2z6+GqJGcCiI1R4HCzoTXsANUM/w= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1735287842; c=relaxed/simple; bh=togGX0Irz+XIpy8imDBzSn+k4SS6wjOqqZ9toVXLcqI=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=Koi3hpGkrMITRP86wJywj73O7+aVCoswLbw47xdlOfdsh729cuhjEuHby06egk8gnwZxnGeWebX6I5/t94RY4ZzRE7QagmcoLM6HjYx6XbqQrqRbzmpQus+SRxF4iX/EK05z6JZ89OplFcCe2Oqx7GokfPEuQPWHgJIvr7rJuyM= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org DE41B3858D20 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=xry111.site; s=default; t=1735287840; bh=72efuZjXQO0xNbM0g2VNuYayo4OgPp0O6tcpbFkZuoY=; h=From:To:Cc:Subject:Date:From; b=LtvJE2iutB4KWQ13ZVnN2CmJ0X0gdXS2aKAkUB7W6VUSEPhjW62DZKwmGFEzX4wz1 JWwKp05cTIWxPgNf5ndHcMMk3mfRlnXNPCJtplJFpM60xLxPTfl6RDrkSusiRXPQ2e ZMWRZvaAkRJLYHUr2xiW8xhwWm/16Pp1/FCAydBg= Received: from stargazer.. (unknown [113.200.174.35]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (Client did not present a certificate) (Authenticated sender: xry111@xry111.site) by xry111.site (Postfix) with ESMTPSA id DAC2167037; Fri, 27 Dec 2024 03:23:58 -0500 (EST) From: Xi Ruoyao To: binutils@sourceware.org Cc: Lulu Cai , nickc@redhat.com, chenglulu@loongson.cn, mengqinggang@loongson.cn, WANG Xuerui , Xi Ruoyao , Icenowy Zheng Subject: [2.43 PATCH] LoongArch: Fix broken DESC => IE transition for 2.43 branch Date: Fri, 27 Dec 2024 16:21:12 +0800 Message-ID: <20241227082347.159392-1-xry111@xry111.site> X-Mailer: git-send-email 2.47.1 MIME-Version: 1.0 X-Spam-Status: No, score=-9.2 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, LIKELY_SPAM_FROM, SPF_HELO_PASS, 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 If code compiled with -fPIC -mtls-dialect=desc is linked into a PDE or PIE, and the code refers to external DSO symbols, we can produce broken link unit as check_relocs expects DESC => IE transition to happen and emits a TLS IE entry in the GOT, but a too early "continue" in relax_section actually jumps over the DESC => IE transition so the code sequence is unchanged and still expecting a TLS descriptor (instead of an IE entry) in the GOT. The bug is already fixed in master branch by commit 5c3d09c1855b ("LoongArch: Optimize the relaxation process") so this fix is only needed for the 2.43 branch. Reported-by: Icenowy Zheng Closes: https://gcc.gnu.org/PR118114 Tested-by: Icenowy Zheng Signed-off-by: Xi Ruoyao --- v1 => v2: "Fix" GCC false warning https://gcc.gnu.org/PR118216. bfd/elfnn-loongarch.c | 45 ++++++++++++++++++++++--------------------- 1 file changed, 23 insertions(+), 22 deletions(-) base-commit: 0cdfcb9b8c4b726e755367c30cd54131e3f017eb diff --git a/bfd/elfnn-loongarch.c b/bfd/elfnn-loongarch.c index 312707bb00b..7760b5279f8 100644 --- a/bfd/elfnn-loongarch.c +++ b/bfd/elfnn-loongarch.c @@ -5325,8 +5325,8 @@ loongarch_elf_relax_section (bfd *abfd, asection *sec, for (unsigned int i = 0; i < sec->reloc_count; i++) { char symtype; - bfd_vma symval; - asection *sym_sec; + bfd_vma symval = 0; /* "= 0" for https://gcc.gnu.org/PR118216 */ + asection *sym_sec = NULL; bool local_got = false; Elf_Internal_Rela *rel = relocs + i; struct elf_link_hash_entry *h = NULL; @@ -5418,14 +5418,33 @@ loongarch_elf_relax_section (bfd *abfd, asection *sec, symval = h->root.u.def.value; sym_sec = h->root.u.def.section; } - else - continue; if (h && LARCH_REF_LOCAL (info, h)) local_got = true; symtype = h->type; } + /* If the conditions for tls type transition are met, type + transition is performed instead of relax. + During the transition from DESC->IE/LE, there are 2 situations + depending on the different configurations of the relax/norelax + option. + If the -relax option is used, the extra nops will be removed, + and this transition is performed in pass 0. + If the --no-relax option is used, nop will be retained, and + this transition is performed in pass 1. */ + if (IS_LOONGARCH_TLS_TRANS_RELOC (r_type) + && (i + 1 != sec->reloc_count) + && ELFNN_R_TYPE (rel[1].r_info) == R_LARCH_RELAX + && loongarch_can_trans_tls (abfd, info, h, r_symndx, r_type)) + { + loongarch_tls_perform_trans (abfd, sec, rel, h, info); + r_type = ELFNN_R_TYPE (rel->r_info); + } + + if (!sym_sec) + continue; + if (sym_sec->sec_info_type == SEC_INFO_TYPE_MERGE && (sym_sec->flags & SEC_MERGE)) { @@ -5453,24 +5472,6 @@ loongarch_elf_relax_section (bfd *abfd, asection *sec, symval += sec_addr (sym_sec); - /* If the conditions for tls type transition are met, type - transition is performed instead of relax. - During the transition from DESC->IE/LE, there are 2 situations - depending on the different configurations of the relax/norelax - option. - If the -relax option is used, the extra nops will be removed, - and this transition is performed in pass 0. - If the --no-relax option is used, nop will be retained, and - this transition is performed in pass 1. */ - if (IS_LOONGARCH_TLS_TRANS_RELOC (r_type) - && (i + 1 != sec->reloc_count) - && ELFNN_R_TYPE (rel[1].r_info) == R_LARCH_RELAX - && loongarch_can_trans_tls (abfd, info, h, r_symndx, r_type)) - { - loongarch_tls_perform_trans (abfd, sec, rel, h, info); - r_type = ELFNN_R_TYPE (rel->r_info); - } - switch (r_type) { case R_LARCH_ALIGN: