[v2,4/4] aarch64: encapsulate note.gnu.property emission into a class

Message ID 20241023103600.500910-5-matthieu.longo@arm.com
State New
Headers
Series aarch64: add minimal support of AEABI build attributes for GCS |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_gcc_build--master-arm fail Patch failed to apply
linaro-tcwg-bot/tcwg_gcc_build--master-aarch64 fail Patch failed to apply

Commit Message

Matthieu Longo Oct. 23, 2024, 10:36 a.m. UTC
  gcc/ChangeLog:

	* config.gcc: Add aarch64-dwarf-metadata.o to extra_objs.
	* config/aarch64/aarch64-dwarf-metadata.h
	(class section_note_gnu_property): Encapsulate GNU properties code
	into a class.
	* config/aarch64/aarch64.cc
	(GNU_PROPERTY_AARCH64_FEATURE_1_AND): Removed.
	(GNU_PROPERTY_AARCH64_FEATURE_1_BTI): Likewise.
	(GNU_PROPERTY_AARCH64_FEATURE_1_PAC): Likewise.
	(GNU_PROPERTY_AARCH64_FEATURE_1_GCS): Likewise.
	(aarch64_file_end_indicate_exec_stack): Move GNU properties code to
	aarch64-dwarf-metadata.cc
	* config/aarch64/t-aarch64: Declare target aarch64-dwarf-metadata.o
	* config/aarch64/aarch64-dwarf-metadata.cc: New file.
---
 gcc/config.gcc                               |   2 +-
 gcc/config/aarch64/aarch64-dwarf-metadata.cc | 145 +++++++++++++++++++
 gcc/config/aarch64/aarch64-dwarf-metadata.h  |  19 +++
 gcc/config/aarch64/aarch64.cc                |  79 +---------
 gcc/config/aarch64/t-aarch64                 |  10 ++
 5 files changed, 181 insertions(+), 74 deletions(-)
 create mode 100644 gcc/config/aarch64/aarch64-dwarf-metadata.cc
  

Patch

diff --git a/gcc/config.gcc b/gcc/config.gcc
index 71ac3badafd..8d26b8776e3 100644
--- a/gcc/config.gcc
+++ b/gcc/config.gcc
@@ -351,7 +351,7 @@  aarch64*-*-*)
 	c_target_objs="aarch64-c.o"
 	cxx_target_objs="aarch64-c.o"
 	d_target_objs="aarch64-d.o"
-	extra_objs="aarch64-builtins.o aarch-common.o aarch64-sve-builtins.o aarch64-sve-builtins-shapes.o aarch64-sve-builtins-base.o aarch64-sve-builtins-sve2.o aarch64-sve-builtins-sme.o cortex-a57-fma-steering.o aarch64-speculation.o falkor-tag-collision-avoidance.o aarch-bti-insert.o aarch64-cc-fusion.o aarch64-early-ra.o aarch64-ldp-fusion.o"
+	extra_objs="aarch64-builtins.o aarch-common.o aarch64-dwarf-metadata.o aarch64-sve-builtins.o aarch64-sve-builtins-shapes.o aarch64-sve-builtins-base.o aarch64-sve-builtins-sve2.o aarch64-sve-builtins-sme.o cortex-a57-fma-steering.o aarch64-speculation.o falkor-tag-collision-avoidance.o aarch-bti-insert.o aarch64-cc-fusion.o aarch64-early-ra.o aarch64-ldp-fusion.o"
 	target_gtfiles="\$(srcdir)/config/aarch64/aarch64-builtins.h \$(srcdir)/config/aarch64/aarch64-builtins.cc \$(srcdir)/config/aarch64/aarch64-sve-builtins.h \$(srcdir)/config/aarch64/aarch64-sve-builtins.cc"
 	target_has_targetm_common=yes
 	;;
diff --git a/gcc/config/aarch64/aarch64-dwarf-metadata.cc b/gcc/config/aarch64/aarch64-dwarf-metadata.cc
new file mode 100644
index 00000000000..f272d290fed
--- /dev/null
+++ b/gcc/config/aarch64/aarch64-dwarf-metadata.cc
@@ -0,0 +1,145 @@ 
+/* DWARF metadata for AArch64 architecture.
+   Copyright (C) 2024 Free Software Foundation, Inc.
+   Contributed by ARM Ltd.
+
+   This file is part of GCC.
+
+   GCC is free software; you can redistribute it and/or modify it
+   under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3, or (at your option)
+   any later version.
+
+   GCC is distributed in the hope that it will be useful, but
+   WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with GCC; see the file COPYING3.  If not see
+   <http://www.gnu.org/licenses/>.  */
+
+#define INCLUDE_STRING
+#define INCLUDE_ALGORITHM
+#define INCLUDE_MEMORY
+#define INCLUDE_VECTOR
+#include "config.h"
+#include "system.h"
+#include "coretypes.h"
+#include "backend.h"
+#include "target.h"
+#include "rtl.h"
+#include "output.h"
+
+#include "aarch64-dwarf-metadata.h"
+
+/* Defined for convenience.  */
+#define POINTER_BYTES (POINTER_SIZE / BITS_PER_UNIT)
+
+namespace aarch64 {
+
+constexpr unsigned GNU_PROPERTY_AARCH64_FEATURE_1_AND = 0xc0000000;
+constexpr unsigned GNU_PROPERTY_AARCH64_FEATURE_1_BTI = (1U << 0);
+constexpr unsigned GNU_PROPERTY_AARCH64_FEATURE_1_PAC = (1U << 1);
+constexpr unsigned GNU_PROPERTY_AARCH64_FEATURE_1_GCS = (1U << 2);
+
+namespace {
+
+std::string
+gnu_property_features_to_string (unsigned feature_1_and)
+{
+  struct flag_name
+  {
+    unsigned int mask;
+    const char *name;
+  };
+
+  static const flag_name flags[] = {
+    {GNU_PROPERTY_AARCH64_FEATURE_1_BTI, "BTI"},
+    {GNU_PROPERTY_AARCH64_FEATURE_1_PAC, "PAC"},
+    {GNU_PROPERTY_AARCH64_FEATURE_1_GCS, "GCS"},
+  };
+
+  const char *separator = "";
+  std::string s_features;
+  for (auto &flag : flags)
+    if (feature_1_and & flag.mask)
+      {
+	s_features.append (separator).append (flag.name);
+	separator = ", ";
+      }
+  return s_features;
+};
+
+} // namespace anonymous
+
+section_note_gnu_property::section_note_gnu_property ()
+  : m_feature_1_and (0) {}
+
+void
+section_note_gnu_property::bti_enabled ()
+{
+  m_feature_1_and |= GNU_PROPERTY_AARCH64_FEATURE_1_BTI;
+}
+
+void
+section_note_gnu_property::pac_enabled ()
+{
+  m_feature_1_and |= GNU_PROPERTY_AARCH64_FEATURE_1_PAC;
+}
+
+void
+section_note_gnu_property::gcs_enabled ()
+{
+  m_feature_1_and |= GNU_PROPERTY_AARCH64_FEATURE_1_GCS;
+}
+
+void
+section_note_gnu_property::write () const
+{
+  if (m_feature_1_and)
+    {
+      /* Generate .note.gnu.property section.  */
+      switch_to_section (
+	get_section (".note.gnu.property", SECTION_NOTYPE, NULL));
+
+      /* PT_NOTE header: namesz, descsz, type.
+	 namesz = 4 ("GNU\0")
+	 descsz = 16 (Size of the program property array)
+		  [(12 + padding) * Number of array elements]
+	 type   = 5 (NT_GNU_PROPERTY_TYPE_0).  */
+      assemble_align (POINTER_SIZE);
+      assemble_integer (GEN_INT (4), 4, 32, 1);
+      assemble_integer (GEN_INT (ROUND_UP (12, POINTER_BYTES)), 4, 32, 1);
+      assemble_integer (GEN_INT (5), 4, 32, 1);
+
+      /* PT_NOTE name.  */
+      assemble_string ("GNU", 4);
+
+      /* PT_NOTE contents for NT_GNU_PROPERTY_TYPE_0:
+	 type   = GNU_PROPERTY_AARCH64_FEATURE_1_AND
+	 datasz = 4
+	 data   = feature_1_and.  */
+      fputs (integer_asm_op (4, true), asm_out_file);
+      fprint_whex (asm_out_file, GNU_PROPERTY_AARCH64_FEATURE_1_AND);
+      putc ('\n', asm_out_file);
+      assemble_integer (GEN_INT (4), 4, 32, 1);
+
+      fputs (integer_asm_op (4, true), asm_out_file);
+      fprint_whex (asm_out_file, m_feature_1_and);
+      if (flag_debug_asm)
+	{
+	  auto const &s_features
+	    = gnu_property_features_to_string (m_feature_1_and);
+	  asm_fprintf (asm_out_file,
+		       "\t%s GNU_PROPERTY_AARCH64_FEATURE_1_AND (%s)\n",
+		       ASM_COMMENT_START, s_features.c_str ());
+	}
+      else
+	putc ('\n', asm_out_file);
+
+      /* Pad the size of the note to the required alignment.  */
+      assemble_align (POINTER_SIZE);
+    }
+}
+
+} // namespace aarch64
\ No newline at end of file
diff --git a/gcc/config/aarch64/aarch64-dwarf-metadata.h b/gcc/config/aarch64/aarch64-dwarf-metadata.h
index 01f08ad073e..564319981ce 100644
--- a/gcc/config/aarch64/aarch64-dwarf-metadata.h
+++ b/gcc/config/aarch64/aarch64-dwarf-metadata.h
@@ -221,6 +221,25 @@  class aeabi_subsection
   auto_vec<aeabi_attribute<T_tag, T_val>, N> m_attributes;
 };
 
+class section_note_gnu_property
+{
+ public:
+  section_note_gnu_property ();
+
+  /* Add BTI flag to GNU properties.  */
+  void bti_enabled ();
+  /* Add GCS flag to GNU properties.  */
+  void gcs_enabled ();
+  /* Add PAC flag to GNU properties.  */
+  void pac_enabled ();
+
+  /* Write the data to the assembly file.  */
+  void write () const;
+
+ private:
+  unsigned m_feature_1_and;
+};
+
 } // namespace aarch64
 
 #endif /* GCC_AARCH64_DWARF_METADATA_H */
\ No newline at end of file
diff --git a/gcc/config/aarch64/aarch64.cc b/gcc/config/aarch64/aarch64.cc
index e5a94738f70..3883338db9f 100644
--- a/gcc/config/aarch64/aarch64.cc
+++ b/gcc/config/aarch64/aarch64.cc
@@ -29236,91 +29236,24 @@  aarch64_can_tag_addresses ()
 
 /* Implement TARGET_ASM_FILE_END for AArch64.  This adds the AArch64 GNU NOTE
    section at the end if needed.  */
-#define GNU_PROPERTY_AARCH64_FEATURE_1_AND	0xc0000000
-#define GNU_PROPERTY_AARCH64_FEATURE_1_BTI	(1U << 0)
-#define GNU_PROPERTY_AARCH64_FEATURE_1_PAC	(1U << 1)
-#define GNU_PROPERTY_AARCH64_FEATURE_1_GCS	(1U << 2)
 void
 aarch64_file_end_indicate_exec_stack ()
 {
   file_end_indicate_exec_stack ();
 
-  unsigned feature_1_and = 0;
+  aarch64::section_note_gnu_property gnu_properties;
+
   if (aarch_bti_enabled ())
-    feature_1_and |= GNU_PROPERTY_AARCH64_FEATURE_1_BTI;
+    gnu_properties.bti_enabled ();
 
   if (aarch_ra_sign_scope != AARCH_FUNCTION_NONE)
-    feature_1_and |= GNU_PROPERTY_AARCH64_FEATURE_1_PAC;
+    gnu_properties.pac_enabled ();
 
   if (aarch64_gcs_enabled ())
-    feature_1_and |= GNU_PROPERTY_AARCH64_FEATURE_1_GCS;
-
-  if (feature_1_and)
-    {
-      /* Generate .note.gnu.property section.  */
-      switch_to_section (get_section (".note.gnu.property",
-				      SECTION_NOTYPE, NULL));
-
-      /* PT_NOTE header: namesz, descsz, type.
-	 namesz = 4 ("GNU\0")
-	 descsz = 16 (Size of the program property array)
-		  [(12 + padding) * Number of array elements]
-	 type   = 5 (NT_GNU_PROPERTY_TYPE_0).  */
-      assemble_align (POINTER_SIZE);
-      assemble_integer (GEN_INT (4), 4, 32, 1);
-      assemble_integer (GEN_INT (ROUND_UP (12, POINTER_BYTES)), 4, 32, 1);
-      assemble_integer (GEN_INT (5), 4, 32, 1);
-
-      /* PT_NOTE name.  */
-      assemble_string ("GNU", 4);
-
-      /* PT_NOTE contents for NT_GNU_PROPERTY_TYPE_0:
-	 type   = GNU_PROPERTY_AARCH64_FEATURE_1_AND
-	 datasz = 4
-	 data   = feature_1_and.  */
-      fputs (integer_asm_op (4, true), asm_out_file);
-      fprint_whex (asm_out_file, GNU_PROPERTY_AARCH64_FEATURE_1_AND);
-      putc ('\n', asm_out_file);
-      assemble_integer (GEN_INT (4), 4, 32, 1);
-
-      fputs (integer_asm_op (4, true), asm_out_file);
-      fprint_whex (asm_out_file, feature_1_and);
-      if (flag_debug_asm)
-	{
-	  struct flag_name
-	  {
-	    unsigned int mask;
-	    const char *name;
-	  };
-	  static const flag_name flags[] = {
-	    { GNU_PROPERTY_AARCH64_FEATURE_1_BTI, "BTI" },
-	    { GNU_PROPERTY_AARCH64_FEATURE_1_PAC, "PAC" },
-	    { GNU_PROPERTY_AARCH64_FEATURE_1_GCS, "GCS" },
-	  };
-
-	  const char *separator = "";
-	  std::string s_features;
-	  for (auto &flag : flags)
-	    if (feature_1_and & flag.mask)
-	      {
-		s_features.append (separator).append (flag.name);
-		separator = ", ";
-	      }
+    gnu_properties.gcs_enabled ();
 
-	  asm_fprintf (asm_out_file,
-		       "\t%s GNU_PROPERTY_AARCH64_FEATURE_1_AND (%s)\n",
-		       ASM_COMMENT_START, s_features.c_str ());
-	}
-      else
-	putc ('\n', asm_out_file);
-      /* Pad the size of the note to the required alignment.  */
-      assemble_align (POINTER_SIZE);
-    }
+  gnu_properties.write ();
 }
-#undef GNU_PROPERTY_AARCH64_FEATURE_1_GCS
-#undef GNU_PROPERTY_AARCH64_FEATURE_1_PAC
-#undef GNU_PROPERTY_AARCH64_FEATURE_1_BTI
-#undef GNU_PROPERTY_AARCH64_FEATURE_1_AND
 
 /* Helper function for straight line speculation.
    Return what barrier should be emitted for straight line speculation
diff --git a/gcc/config/aarch64/t-aarch64 b/gcc/config/aarch64/t-aarch64
index c2a0715e9ab..c6b8ef9ccb8 100644
--- a/gcc/config/aarch64/t-aarch64
+++ b/gcc/config/aarch64/t-aarch64
@@ -139,6 +139,16 @@  aarch-common.o: $(srcdir)/config/arm/aarch-common.cc $(CONFIG_H) $(SYSTEM_H) \
 	$(COMPILER) -c $(ALL_COMPILERFLAGS) $(ALL_CPPFLAGS) $(INCLUDES) \
 		$(srcdir)/config/arm/aarch-common.cc
 
+aarch64-dwarf-metadata.o: $(srcdir)/config/aarch64/aarch64-dwarf-metadata.cc \
+    $(CONFIG_H) \
+    $(RTL_H) \
+    $(SYSTEM_H) \
+    $(TARGET_H) \
+    $(srcdir)/config/aarch64/aarch64-dwarf-metadata.h \
+    output.h
+	$(COMPILER) -c $(ALL_COMPILERFLAGS) $(ALL_SPPFLAGS) $(INCLUDES) \
+	  $(srcdir)/config/aarch64/aarch64-dwarf-metadata.cc
+
 aarch64-c.o: $(srcdir)/config/aarch64/aarch64-c.cc $(CONFIG_H) $(SYSTEM_H) \
     coretypes.h $(TM_H) $(TREE_H) output.h $(C_COMMON_H) $(TARGET_H)
 	$(COMPILER) -c $(ALL_COMPILERFLAGS) $(ALL_CPPFLAGS) $(INCLUDES) \