From patchwork Sat Dec 2 06:53:30 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: changjiachen X-Patchwork-Id: 172745 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp1618893vqy; Fri, 1 Dec 2023 22:54:52 -0800 (PST) X-Google-Smtp-Source: AGHT+IHDqF+Fg7F/QyKNOvZtkSO6o4FJ5yYrJHQshJsrQoTnfoom2+RtFlvW4plTwCvDF6rtLrQX X-Received: by 2002:ac8:584c:0:b0:425:4043:18a1 with SMTP id h12-20020ac8584c000000b00425404318a1mr1090109qth.84.1701500092812; Fri, 01 Dec 2023 22:54:52 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1701500092; cv=pass; d=google.com; s=arc-20160816; b=ROqPz6jcjBMtRdUrKDpmWeBhNh+WZHrddZeSrTv6GAtLCSmFwon42hEZb4djPrXgb4 Gvm6saypCkGruqynKByfQLtjP05ysqrI2KAf1r6fPcb+gliYwRcE3VArXLsPisuI61wP BmeWBKDQczEXe0rDhLd95DilK2C0Jo0J0HXlvPseqlDPIkCmBJSYw1/0nPy5Kl4279jn 14E6zI/z8J9K0E7B2Iyqro9KvAbvZU+BZMCeiUQFSNHYM55o9f15LN6NfpNbcEQYDAd2 Svzf/6vEggQrSsyyA2Qa/cfcmVuKE/Vj+A4IVtzHIRcXmvZZdX0oUZUFeHUYaSrxRBqX dSgw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:arc-filter:dmarc-filter:delivered-to; bh=PQBOBIIS60ho6cyxPmwPjgIZiknpVLYQNSa04d3/r9E=; fh=uTF/VfpCAq2GtcL5NdCKul3VKQPlBSDYmUlZE/0CcYM=; b=jbQoZy/KmUPeEY0J07iIYxHzoA8Lo/ApaUBN/JnDWUqyK5G0xATW5OtNT5/9guKMlj cHDcs1tDx0Agk13udSxRKy8Oq4J7jI6rN87s7+KQM5VWtdjquebRXnQTFrbdb1ceJcyR wGNtKNi6LSQ9Xe4ypuCxxC0RZdt3M8mtLUcYt2R1C/WZyZtzZfgOS2A/0Pe3MPnEuk9r UdxG/FH8ezgPiXRNIwhdW6FGPJoELyV2gwv58ONe5coDftIm7pmoVFRMwRB4EBOMl2jH scJ3ay3Mzitgoc0fZHnxDjsO/20kOgTUBWQFGgghRFy+BeWW1ysX5IyTYeCMVFZySbbW a9Cw== ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1); 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"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=xupt.edu.cn Received: from server2.sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id jr11-20020a05622a800b00b004237f85a05esi4791865qtb.540.2023.12.01.22.54.52 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Dec 2023 22:54:52 -0800 (PST) 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; arc=pass (i=1); 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"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=xupt.edu.cn Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 72AC63860002 for ; Sat, 2 Dec 2023 06:54:42 +0000 (GMT) X-Original-To: binutils@sourceware.org Delivered-To: binutils@sourceware.org Received: from mail-m24121.xmail.ntesmail.com (mail-m24121.xmail.ntesmail.com [45.195.24.121]) by sourceware.org (Postfix) with ESMTPS id 22C84385828E for ; Sat, 2 Dec 2023 06:53:56 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 22C84385828E Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=stu.xupt.edu.cn Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=stu.xupt.edu.cn ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 22C84385828E Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=45.195.24.121 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701500042; cv=none; b=QRDxpanuCw4d5E9HLDP8pphPd1bTnxzw5OVQcKBA6BPgS77Tq9W3gu8yNZszoUxC8a8HxBJIa0MlmpTVK/Bg8RI/xDE3f97kU1ndy2qptDOK//MSdAyQKoYuhJs2AkQi1wxPrO0rwuxaoar4+p8KcNnSDDssPZ6bzv98y8z3Q+Q= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701500042; c=relaxed/simple; bh=Wx82xfbU0qzswW0HJrwa3N7qZm2H/cv3ItVcSYJ6oZM=; h=From:To:Subject:Date:Message-Id:MIME-Version; b=tmkD4hF7nxBX394h8jG5vPSaI9Mj2u4ky6+pvK86t5HuPrFMpa/m5wD4IfzkbtQJX3NIua0+W5pJXJo/1Tkjx/5XEiy6KHmV/nwAdgO8g7YgnDJe0ws/jkjfJnfoW/vagpwZYg1al8gRsZ3vpFUTRNZdS+3M5ZzWpCr62UNI3+U= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from ubuntu.localdomain (unknown [223.104.204.129]) by mail-m121144.qiye.163.com (Hmail) with ESMTPA id AF95DAC00C6; Sat, 2 Dec 2023 14:53:45 +0800 (CST) From: changjiachen To: binutils@sourceware.org Cc: xuchenghua@loongson.cn, chenglulu@loongson.cn, liuzhensong@loongson.cn, xry111@xry111.site, i.swmail@xen0n.name, maskray@google.com, cailulu@loongson.cn, luweining@loongson.cn, wanglei@loongson.cn, hejinyang@loongson.cn, Lazy_Linux@126.com, mengqinggang@loongson.cn, changjiachen Subject: [PATCH v2 1/5] LoongArch: bfd: Add support for tls le relax. Date: Sat, 2 Dec 2023 14:53:30 +0800 Message-Id: <20231202065334.25904-2-changjiachen@stu.xupt.edu.cn> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20231202065334.25904-1-changjiachen@stu.xupt.edu.cn> References: <20231202065334.25904-1-changjiachen@stu.xupt.edu.cn> MIME-Version: 1.0 X-HM-Spam-Status: e1kfGhgUHx5ZQUtXWQgPGg8OCBgUHx5ZQUlOS1dZFg8aDwILHllBWSg2Ly tZV1koWUFITzdXWS1ZQUlXWQ8JGhUIEh9ZQVkaGR8fVkhNTE9DH0xDTU9MGFUTARMWGhIXJBQOD1 lXWRgSC1lBWUlJSFVKS09VSUtPVUpJQllXWRYaDxIVHRRZQVlLVUtVS1VLWQY+ X-HM-Tid: 0a8c294dfe2eb039kuuuaf95dac00c6 X-HM-MType: 10 X-HM-Sender-Digest: e1kMHhlZQR0aFwgeV1kSHx4VD1lBWUc6Nxw6PTo5FDw3KAs1MB4PFyMV NRAaCw1VSlVKTEtKTktLS0lMTklDVTMWGhIXVRgTGhUcERIaGBMeFTsIDw5VAw4LD1UeHw5VGBVF WVdZEgtZQVlJSUhVSktPVUlLT1VKSUJZV1kIAVlBSktDTE03Bg++ X-Spam-Status: No, score=-12.2 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_STATUS, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H5, RCVD_IN_MSPIKE_WL, 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+ouuuleilei=gmail.com@sourceware.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1784152161339169612 X-GMAIL-MSGID: 1784152161339169612 Add tls le relax support and related relocs in bfd. This support does two main things: 1. Implement support for three new relocation items in bfd. The three new relocation items are shown below: R_LARCH_TLS_LE_ADD_R R_LARCH_TLS_LE_HI20_R R_LARCH_TLS_LE_LO12_R 2. ADD a new macro RELOCATE_TLS_TP32_HI20 Handle problems caused by symbol extensions in TLS LE, The processing is similar to the macro RELOCATE_CALC_PC32_HI20 method. 3. Implement the tls le relax function. bfd/ChangeLog: * bfd-in2.h: Add relocs related to tls le relax. * elfnn-loongarch.c: (loongarch_relax_tls_le): New function. (RELOCATE_TLS_TP32_HI20): New macro. (loongarch_elf_check_relocs): Add new reloc support. (perform_relocation): Likewise. (loongarch_elf_relocate_section): Handle new relocs related to relax. (loongarch_elf_relax_section): Likewise. * elfxx-loongarch.c: (LOONGARCH_HOWTO (R_LARCH_TLS_LE_ADD_R)): New reloc how to type. (LOONGARCH_HOWTO (R_LARCH_TLS_LE_HI20_R)): Likewise. (LOONGARCH_HOWTO (R_LARCH_TLS_LE_LO12_R)): Likewise. * libbfd.h: Add relocs related to tls le relax. * reloc.c: Likewise. --- bfd/bfd-in2.h | 4 +++ bfd/elfnn-loongarch.c | 74 +++++++++++++++++++++++++++++++++++++++++++ bfd/elfxx-loongarch.c | 50 +++++++++++++++++++++++++++++ bfd/libbfd.h | 3 ++ bfd/reloc.c | 6 ++++ 5 files changed, 137 insertions(+) diff --git a/bfd/bfd-in2.h b/bfd/bfd-in2.h index 79f7f24436c..3972b9b1a9a 100644 --- a/bfd/bfd-in2.h +++ b/bfd/bfd-in2.h @@ -7457,8 +7457,12 @@ enum bfd_reloc_code_real BFD_RELOC_LARCH_ADD_ULEB128, BFD_RELOC_LARCH_SUB_ULEB128, BFD_RELOC_LARCH_64_PCREL, + BFD_RELOC_LARCH_TLS_LE_HI20_R, + BFD_RELOC_LARCH_TLS_LE_ADD_R, + BFD_RELOC_LARCH_TLS_LE_LO12_R, BFD_RELOC_UNUSED }; + typedef enum bfd_reloc_code_real bfd_reloc_code_real_type; reloc_howto_type *bfd_reloc_type_lookup diff --git a/bfd/elfnn-loongarch.c b/bfd/elfnn-loongarch.c index 18ad3cc91ca..b8a3199d27a 100644 --- a/bfd/elfnn-loongarch.c +++ b/bfd/elfnn-loongarch.c @@ -738,6 +738,7 @@ loongarch_elf_check_relocs (bfd *abfd, struct bfd_link_info *info, break; case R_LARCH_TLS_LE_HI20: + case R_LARCH_TLS_LE_HI20_R: case R_LARCH_SOP_PUSH_TLS_TPREL: if (!bfd_link_executable (info)) return false; @@ -2105,6 +2106,8 @@ perform_relocation (const Elf_Internal_Rela *rel, asection *input_section, case R_LARCH_GOT64_HI12: case R_LARCH_TLS_LE_HI20: case R_LARCH_TLS_LE_LO12: + case R_LARCH_TLS_LE_HI20_R: + case R_LARCH_TLS_LE_LO12_R: case R_LARCH_TLS_LE64_LO20: case R_LARCH_TLS_LE64_HI12: case R_LARCH_TLS_IE_PC_HI20: @@ -2130,6 +2133,7 @@ perform_relocation (const Elf_Internal_Rela *rel, asection *input_section, break; case R_LARCH_RELAX: + case R_LARCH_TLS_LE_ADD_R: break; default: @@ -2310,6 +2314,16 @@ loongarch_reloc_is_fatal (struct bfd_link_info *info, relocation += 0x1000; \ }) +/* Handle problems caused by symbol extensions in TLS LE, The processing + is similar to the macro RELOCATE_CALC_PC32_HI20 method. */ +#define RELOCATE_TLS_TP32_HI20(relocation) \ + ({ \ + bfd_vma __lo = (relocation) & ((bfd_vma)0xfff); \ + if (__lo > 0x7ff) \ + relocation += 0xfff; \ + relocation = relocation & ~(bfd_vma)0xfff; \ + }) + /* For example: pc is 0x11000010000100, symbol is 0x1812348ffff812 offset = (0x1812348ffff812 & ~0xfff) - (0x11000010000100 & ~0xfff) = 0x712347ffff000 @@ -3205,6 +3219,13 @@ loongarch_elf_relocate_section (bfd *output_bfd, struct bfd_link_info *info, break; + case R_LARCH_TLS_LE_HI20_R: + relocation -= elf_hash_table (info)->tls_sec->vma; + + RELOCATE_TLS_TP32_HI20 (relocation); + + break; + case R_LARCH_PCALA_LO12: /* Not support if sym_addr in 2k page edge. pcalau12i pc_hi20 (sym_addr) @@ -3375,6 +3396,7 @@ loongarch_elf_relocate_section (bfd *output_bfd, struct bfd_link_info *info, case R_LARCH_TLS_LE_HI20: case R_LARCH_TLS_LE_LO12: + case R_LARCH_TLS_LE_LO12_R: case R_LARCH_TLS_LE64_LO20: case R_LARCH_TLS_LE64_HI12: BFD_ASSERT (resolved_local && elf_hash_table (info)->tls_sec); @@ -3735,6 +3757,47 @@ loongarch_relax_delete_bytes (bfd *abfd, return true; } +/* Relax tls le. */ +static bool +loongarch_relax_tls_le (bfd *abfd, asection *sec, + Elf_Internal_Rela *rel_hi, + struct bfd_link_info *link_info, + bfd_vma symval) +{ + bfd_byte *contents = elf_section_data (sec)->this_hdr.contents; + uint32_t insn = bfd_get (32, abfd, contents + rel_hi->r_offset); + static uint32_t insn_rj,insn_rd; + symval = symval - elf_hash_table (link_info)->tls_sec->vma; + /* Whether the symbol offset is in the interval (offset < 0x800). */ + if (ELFNN_R_TYPE ((rel_hi + 1)->r_info) + == R_LARCH_RELAX && symval < 0x800) + { + switch (ELFNN_R_TYPE (rel_hi->r_info)) + { + case R_LARCH_TLS_LE_HI20_R: + case R_LARCH_TLS_LE_ADD_R: + /* delete insn. */ + rel_hi->r_info = ELFNN_R_INFO (ELFNN_R_SYM (rel_hi->r_info), + R_LARCH_DELETE); + break; + case R_LARCH_TLS_LE_LO12_R: + /* Change rj to $tp. */ + insn_rj = 0x2 << 5; + /* Get rd register. */ + insn_rd = insn & 0x1f; + /* Write symbol offset. */ + symval <<= 10; + /* Writes the modified instruction. */ + insn = insn & 0xffc00000; + insn = insn | symval | insn_rj | insn_rd; + bfd_put (32, abfd, insn, contents + rel_hi->r_offset); + break; + default: + break; + } + } + return true; +} /* Relax pcalau12i,addi.d => pcaddi. */ static bool @@ -4002,6 +4065,17 @@ loongarch_elf_relax_section (bfd *abfd, asection *sec, rel->r_info = ELFNN_R_INFO (0, R_LARCH_NONE); } break; + case R_LARCH_TLS_LE_HI20_R: + case R_LARCH_TLS_LE_LO12_R: + case R_LARCH_TLS_LE_ADD_R: + if (info->relax_pass == 0) + { + if (i + 2 > sec->reloc_count) + break; + loongarch_relax_tls_le (abfd, sec, rel, info, symval); + } + break; + case R_LARCH_PCALA_HI20: if (info->relax_pass == 0) { diff --git a/bfd/elfxx-loongarch.c b/bfd/elfxx-loongarch.c index a970a257aa9..a24333e6838 100644 --- a/bfd/elfxx-loongarch.c +++ b/bfd/elfxx-loongarch.c @@ -1547,6 +1547,56 @@ static loongarch_reloc_howto_type loongarch_howto_table[] = NULL, /* adjust_reloc_bits */ NULL), /* larch_reloc_type_name */ + LOONGARCH_HOWTO (R_LARCH_TLS_LE_HI20_R, /* type (110). */ + 12, /* rightshift. */ + 4, /* size. */ + 20, /* bitsize. */ + false, /* pc_relative. */ + 5, /* bitpos. */ + complain_overflow_signed, /* complain_on_overflow. */ + bfd_elf_generic_reloc, /* special_function. */ + "R_LARCH_TLS_LE_HI20_R", /* name. */ + false, /* partial_inplace. */ + 0, /* src_mask. */ + 0x1ffffe0, /* dst_mask. */ + false, /* pcrel_offset. */ + BFD_RELOC_LARCH_TLS_LE_HI20_R, /* bfd_reloc_code_real_type. */ + reloc_bits, /* adjust_reloc_bits. */ + "le_hi20_r"), /* larch_reloc_type_name. */ + + LOONGARCH_HOWTO (R_LARCH_TLS_LE_ADD_R, /* type (111). */ + 0, /* rightshift. */ + 1, /* size. */ + 0, /* bitsize. */ + false, /* pc_relative. */ + 0, /* bitpos. */ + complain_overflow_dont, /* complain_on_overflow. */ + bfd_elf_generic_reloc, /* special_function. */ + "R_LARCH_TLS_LE_ADD_R", /* name. */ + false, /* partial_inplace. */ + 0, /* src_mask. */ + 0, /* dst_mask. */ + false, /* pcrel_offset. */ + BFD_RELOC_LARCH_TLS_LE_ADD_R, /* bfd_reloc_code_real_type. */ + NULL, /* adjust_reloc_bits. */ + "tls_le_add_r"), /* larch_reloc_type_name. */ + + LOONGARCH_HOWTO (R_LARCH_TLS_LE_LO12_R, /* type (112). */ + 0, /* rightshift. */ + 4, /* size. */ + 12, /* bitsize. */ + false, /* pc_relative. */ + 10, /* bitpos. */ + complain_overflow_signed, /* complain_on_overflow. */ + bfd_elf_generic_reloc, /* special_function. */ + "R_LARCH_TLS_LE_LO12_R", /* name. */ + false, /* partial_inplace. */ + 0, /* src_mask. */ + 0x3ffc00, /* dst_mask. */ + false, /* pcrel_offset. */ + BFD_RELOC_LARCH_TLS_LE_LO12_R, /* bfd_reloc_code_real_type. */ + reloc_bits, /* adjust_reloc_bits. */ + "le_lo12_r"), /* larch_reloc_type_name. */ }; reloc_howto_type * diff --git a/bfd/libbfd.h b/bfd/libbfd.h index cc432677a81..4b0e9405e76 100644 --- a/bfd/libbfd.h +++ b/bfd/libbfd.h @@ -3599,6 +3599,9 @@ static const char *const bfd_reloc_code_real_names[] = { "@@uninitialized@@", "BFD_RELOC_LARCH_ADD_ULEB128", "BFD_RELOC_LARCH_SUB_ULEB128", "BFD_RELOC_LARCH_64_PCREL", + "BFD_RELOC_LARCH_TLS_LE_HI20_R", + "BFD_RELOC_LARCH_TLS_LE_ADD_R", + "BFD_RELOC_LARCH_TLS_LE_LO12_R", "@@overflow: BFD_RELOC_UNUSED@@", }; #endif diff --git a/bfd/reloc.c b/bfd/reloc.c index 93ebad879e0..f6312ff9b77 100644 --- a/bfd/reloc.c +++ b/bfd/reloc.c @@ -8291,6 +8291,12 @@ ENUMX ENUMX BFD_RELOC_LARCH_64_PCREL +ENUMX + BFD_RELOC_LARCH_TLS_LE_HI20_R +ENUMX + BFD_RELOC_LARCH_TLS_LE_ADD_R +ENUMX + BFD_RELOC_LARCH_TLS_LE_LO12_R ENUMDOC LARCH relocations.