From patchwork Wed Jan 15 07:53:46 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Monk Chiang X-Patchwork-Id: 104807 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 4692D385482F for ; Wed, 15 Jan 2025 07:56:31 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 4692D385482F Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=sifive.com header.i=@sifive.com header.a=rsa-sha256 header.s=google header.b=kXoiX/BZ X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-pl1-x62c.google.com (mail-pl1-x62c.google.com [IPv6:2607:f8b0:4864:20::62c]) by sourceware.org (Postfix) with ESMTPS id 4B2A13854828 for ; Wed, 15 Jan 2025 07:54:01 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 4B2A13854828 Authentication-Results: sourceware.org; dmarc=pass (p=reject dis=none) header.from=sifive.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=sifive.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 4B2A13854828 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::62c ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736927641; cv=none; b=xRRM/OzfwGq3JjlIGjHofL94SHQGlCfzc0N5dd8pknXr3aGfeMGO6v7o8Yg3cxPXHY44DsEOXH3a1TWAlLbniC69Es1vCMC6QkMNkb+RNw40KdKEyn6IEzveZh0qy6mjtko6u0pcIeX3MAjtdMmF6OZJjbEr9NF9SVueAaKDiBo= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736927641; c=relaxed/simple; bh=q5kMaWPCJ93wxujTRNBIXdFMLWCjAeeeUN6X1QqiOhE=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=L5cR/x6fWQkF+jLwx625746u1urDRJFt5YkguBIT9pDasQqW1/YFPaY1UBrKFMXe8/+1UXxDluy4IV0Rco2kChiFKGWOVFl1UzpjN/UddrblNUXy/LSkYn+PHiDnyvMqmqaIgl/T3bYPE0itEZhl6io3519u717cuXG8+FGCfVY= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 4B2A13854828 Received: by mail-pl1-x62c.google.com with SMTP id d9443c01a7336-218c8aca5f1so138734055ad.0 for ; Tue, 14 Jan 2025 23:54:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1736927640; x=1737532440; darn=gcc.gnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=PVjHN2NjUkb48UEl1B2aX2T7J2mUrg2C1ujBONEl9JM=; b=kXoiX/BZ4QKD2lQBbkXC6cwg9Em34Kk6rL76Uh9o20gecu2gF0RD+njtVQQfRlo1Sm bbhd0cYDQth+cS9eNhIe1lIFMXWt6UgbPdQaUVOzXio7O2bjlAyJjfnVdxE62OlDHhBh JJSaq1vCPwq8QfFBqDaaYpIlbCPeK1JbPc2FCWz8N8HEpFrDD8eioiwqCcTAdGvV5gUu rXxVmP+gilehySQvmnyHlBRUQ0KBgNl1sZUxqPV8xN7uGIUyd3LL7Ny/gga//08qb6pK WN+rg/DdkK4WzDDPZLG6b83LkgrZox/qALgRxnSKsIUVdRUPENqXj+8RbmQSbR5f7mmX v0JA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736927640; x=1737532440; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=PVjHN2NjUkb48UEl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:6a5e with SMTP id d9443c01a7336-21a83f57228mr363460475ad.18.1736927639966; Tue, 14 Jan 2025 23:53:59 -0800 (PST) Received: from hsinchu02.internal.sifive.com ([210.176.154.34]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2f72c19a5d4sm839877a91.17.2025.01.14.23.53.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 14 Jan 2025 23:53:59 -0800 (PST) From: Monk Chiang To: gcc-patches@gcc.gnu.org, kito@gcc.gnu.org Cc: Monk Chiang Subject: [PATCH v3 3/4] RISC-V: Add .note.gnu.property for ZICFILP and ZICFISS ISA extension Date: Wed, 15 Jan 2025 15:53:46 +0800 Message-ID: <20250115075347.61344-3-monk.chiang@sifive.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250115075347.61344-1-monk.chiang@sifive.com> References: <20250115075347.61344-1-monk.chiang@sifive.com> MIME-Version: 1.0 X-Spam-Status: No, score=-12.6 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, URIBL_BLACK 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: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces~patchwork=sourceware.org@gcc.gnu.org gcc/ChangeLog: * gcc/config/riscv/riscv.cc (riscv_file_end_indicate_exec_stack): Add .note.gnu.property. * gcc/config/riscv/linux.h (TARGET_ASM_FILE_END): Define. libgcc/ChangeLog: * libgcc/config/riscv/crti.S: Add lpad instructions. * libgcc/config/riscv/crtn.S: Likewise. * libgcc/config/riscv/save-restore.S: Likewise. * libgcc/config/riscv/riscv-asm.h: Add GNU_PROPERTY for ZICFILP, ZICFISS. --- gcc/config/riscv/linux.h | 2 + gcc/config/riscv/riscv.cc | 50 ++++++++++++++++++++++ libgcc/config/riscv/crti.S | 2 + libgcc/config/riscv/crtn.S | 2 + libgcc/config/riscv/riscv-asm.h | 69 +++++++++++++++++++++++++++++- libgcc/config/riscv/save-restore.S | 5 +++ 6 files changed, 129 insertions(+), 1 deletion(-) diff --git a/gcc/config/riscv/linux.h b/gcc/config/riscv/linux.h index 9060c940a44..c1cfa9bf28a 100644 --- a/gcc/config/riscv/linux.h +++ b/gcc/config/riscv/linux.h @@ -61,6 +61,8 @@ along with GCC; see the file COPYING3. If not see -dynamic-linker " GNU_USER_DYNAMIC_LINKER "}} \ %{static:-static} %{static-pie:-static -pie --no-dynamic-linker -z text}}" +#define TARGET_ASM_FILE_END riscv_file_end_indicate_exec_stack + #define STARTFILE_PREFIX_SPEC \ "/lib" XLEN_SPEC "/" ABI_SPEC "/ " \ "/usr/lib" XLEN_SPEC "/" ABI_SPEC "/ " \ diff --git a/gcc/config/riscv/riscv.cc b/gcc/config/riscv/riscv.cc index 4afb0b95839..fa4a706bf56 100644 --- a/gcc/config/riscv/riscv.cc +++ b/gcc/config/riscv/riscv.cc @@ -10334,6 +10334,56 @@ riscv_file_start (void) riscv_emit_attribute (); } +void +riscv_file_end_indicate_exec_stack () +{ + file_end_indicate_exec_stack (); + long GNU_PROPERTY_RISCV_FEATURE_1_AND = 0; + unsigned long feature_1_and = 0; + + if (TARGET_ZICFISS) + feature_1_and |= 0x1 << 0; + + if (TARGET_ZICFILP) + feature_1_and |= 0x1 << 1; + + if (feature_1_and) + { + /* Generate .note.gnu.property section. */ + switch_to_section (get_section (".note.gnu.property", + SECTION_NOTYPE, NULL)); + + /* The program property descriptor is aligned to 4 bytes in 32-bit + objects and 8 bytes in 64-bit objects. */ + unsigned p2align = TARGET_64BIT ? 3 : 2; + + fprintf (asm_out_file, "\t.p2align\t%u\n", p2align); + /* name length. */ + fprintf (asm_out_file, "\t.long\t1f - 0f\n"); + /* data length. */ + fprintf (asm_out_file, "\t.long\t5f - 2f\n"); + /* note type. */ + fprintf (asm_out_file, "\t.long\t5\n"); + fprintf (asm_out_file, "0:\n"); + /* vendor name: "GNU". */ + fprintf (asm_out_file, "\t.asciz\t\"GNU\"\n"); + fprintf (asm_out_file, "1:\n"); + + /* pr_type. */ + fprintf (asm_out_file, "\t.p2align\t3\n"); + fprintf (asm_out_file, "2:\n"); + fprintf (asm_out_file, "\t.long\t0xc0000000\n"); + /* pr_datasz. */ + fprintf (asm_out_file, "\t.long\t4f - 3f\n"); + fprintf (asm_out_file, "3:\n"); + /* zicfiss, zicfilp. */ + fprintf (asm_out_file, "\t.long\t%x\n", feature_1_and); + fprintf (asm_out_file, "4:\n"); + fprintf (asm_out_file, "\t.p2align\t%u\n", p2align); + fprintf (asm_out_file, "5:\n"); + } +} + /* Implement TARGET_ASM_OUTPUT_MI_THUNK. Generate rtl rather than asm text in order to avoid duplicating too much logic from elsewhere. */ diff --git a/libgcc/config/riscv/crti.S b/libgcc/config/riscv/crti.S index 89bac706c63..3a67fd77156 100644 --- a/libgcc/config/riscv/crti.S +++ b/libgcc/config/riscv/crti.S @@ -1 +1,3 @@ /* crti.S is empty because .init_array/.fini_array are used exclusively. */ + +#include "riscv-asm.h" diff --git a/libgcc/config/riscv/crtn.S b/libgcc/config/riscv/crtn.S index ca6ee7b6fba..cb80782bb55 100644 --- a/libgcc/config/riscv/crtn.S +++ b/libgcc/config/riscv/crtn.S @@ -1 +1,3 @@ /* crtn.S is empty because .init_array/.fini_array are used exclusively. */ + +#include "riscv-asm.h" diff --git a/libgcc/config/riscv/riscv-asm.h b/libgcc/config/riscv/riscv-asm.h index b6dbeaedc20..73bddb3f9e7 100644 --- a/libgcc/config/riscv/riscv-asm.h +++ b/libgcc/config/riscv/riscv-asm.h @@ -23,9 +23,11 @@ see the files COPYING3 and COPYING.RUNTIME respectively. If not, see #define FUNC_SIZE(X) .size X,.-X #define FUNC_BEGIN(X) \ + .align 2; \ .globl X; \ FUNC_TYPE (X); \ -X: +X: \ + LPAD #define FUNC_END(X) \ FUNC_SIZE(X) @@ -39,3 +41,68 @@ X: #define HIDDEN_JUMPTARGET(X) CONCAT1(__hidden_, X) #define HIDDEN_DEF(X) FUNC_ALIAS(HIDDEN_JUMPTARGET(X), X); \ .hidden HIDDEN_JUMPTARGET(X) + +/* GNU_PROPERTY_RISCV64_* macros from elf.h for use in asm code. */ +#define FEATURE_1_AND 0xc0000000 +#define FEATURE_1_FCFI 1 +#define FEATURE_1_BCFI 2 + +/* Add a NT_GNU_PROPERTY_TYPE_0 note. */ +#if __riscv_xlen == 32 +# define GNU_PROPERTY(type, value) \ + .section .note.gnu.property, "a"; \ + .p2align 2; \ + .word 4; \ + .word 12; \ + .word 5; \ + .asciz "GNU"; \ + .word type; \ + .word 4; \ + .word value; \ + .text +#else +# define GNU_PROPERTY(type, value) \ + .section .note.gnu.property, "a"; \ + .p2align 3; \ + .word 4; \ + .word 16; \ + .word 5; \ + .asciz "GNU"; \ + .word type; \ + .word 4; \ + .word value; \ + .word 0; \ + .text +#endif + +/* Add GNU property note with the supported features to all asm code + where sysdep.h is included. */ +#undef __VALUE_FOR_FEATURE_1_AND +#if defined (__riscv_zicfilp) || defined (__riscv_zicfiss) +# if defined (__riscv_zicfilp) +# if defined (__riscv_zicfiss) +# define __VALUE_FOR_FEATURE_1_AND 0x3 +# else +# define __VALUE_FOR_FEATURE_1_AND 0x1 +# endif +# else +# if defined (__riscv_zicfiss) +# define __VALUE_FOR_FEATURE_1_AND 0x2 +# else +# error "What?" +# endif +# endif +#endif + +#if defined (__VALUE_FOR_FEATURE_1_AND) +GNU_PROPERTY (FEATURE_1_AND, __VALUE_FOR_FEATURE_1_AND) +#endif +#undef __VALUE_FOR_FEATURE_1_AND + +#ifdef __riscv_zicfilp +# define SET_LPAD lui t2, 0 +# define LPAD lpad 0 +#else +# define SET_LPAD +# define LPAD +#endif diff --git a/libgcc/config/riscv/save-restore.S b/libgcc/config/riscv/save-restore.S index ae8312d27db..fc654472a4c 100644 --- a/libgcc/config/riscv/save-restore.S +++ b/libgcc/config/riscv/save-restore.S @@ -137,6 +137,7 @@ FUNC_BEGIN (__riscv_save_2) # CFA info is not correct in next 2 instruction since t1's # value is depend on how may register really save. add sp, sp, t1 + SET_LPAD jr t0 .cfi_endproc FUNC_END (__riscv_save_12) @@ -162,6 +163,7 @@ FUNC_BEGIN (__riscv_save_0) .cfi_offset 8, -16 sd ra, 8(sp) .cfi_offset 1, -8 + SET_LPAD jr t0 .cfi_endproc FUNC_END (__riscv_save_1) @@ -310,6 +312,7 @@ FUNC_BEGIN(__riscv_save_0) .cfi_offset 8, -8 sw ra, 8(sp) .cfi_offset 1, -4 + SET_LPAD jr t0 .cfi_endproc FUNC_END(__riscv_save_2) @@ -399,6 +402,7 @@ FUNC_BEGIN (__riscv_save_4) # CFA info is not correct in next 2 instruction since t1's # value is depend on how may register really save. sub sp, sp, t1 + SET_LPAD jr t0 .cfi_endproc FUNC_END (__riscv_save_12) @@ -428,6 +432,7 @@ FUNC_BEGIN (__riscv_save_0) .cfi_offset 8, -8 sw ra, 12(sp) .cfi_offset 1, -4 + SET_LPAD jr t0 .cfi_endproc FUNC_END (__riscv_save_3)