https://gcc.gnu.org/g:0b4a3c146312fa9edde12abba31b1f285b5a378d
commit r16-1524-g0b4a3c146312fa9edde12abba31b1f285b5a378d Author: Matthieu Longo <matthieu.lo...@arm.com> Date: Wed Jun 4 12:10:05 2025 +0100 aarch64: encapsulate note.gnu.property emission into a class The code emitting the GNU properties was moved to a separate file to improve modularity and "releave" the 31000-lines long aarch64.cc file from a few lines. It introduces a new namespace "aarch64::" for AArch64 backend which reduce the length of function names by not prepending 'aarch64_' to each of them. gcc/ChangeLog: * Makefile.in: Add missing declaration of BACKEND_H. * config.gcc: Add aarch64-elf-metadata.o to extra_objs. * config/aarch64/aarch64-elf-metadata.h: New file * config/aarch64/aarch64-elf-metadata.cc: New file. * 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-elf-metadata.cc * config/aarch64/t-aarch64: Declare target aarch64-elf-metadata.o Diff: --- gcc/Makefile.in | 4 +- gcc/config.gcc | 2 +- gcc/config/aarch64/aarch64-elf-metadata.cc | 145 +++++++++++++++++++++++++++++ gcc/config/aarch64/aarch64-elf-metadata.h | 47 ++++++++++ gcc/config/aarch64/aarch64.cc | 82 ++-------------- gcc/config/aarch64/t-aarch64 | 11 +++ 6 files changed, 214 insertions(+), 77 deletions(-) diff --git a/gcc/Makefile.in b/gcc/Makefile.in index fe20b655477f..56fa5ac25ff9 100644 --- a/gcc/Makefile.in +++ b/gcc/Makefile.in @@ -972,6 +972,7 @@ endif DUMPFILE_H = $(srcdir)/../libcpp/include/line-map.h dumpfile.h VEC_H = vec.h statistics.h $(GGC_H) HASH_TABLE_H = $(HASHTAB_H) hash-table.h $(GGC_H) +BITMAP_H = bitmap.h $(HASHTAB_H) statistics.h EXCEPT_H = except.h $(HASHTAB_H) TARGET_DEF = target.def target-hooks-macros.h target-insns.def C_TARGET_DEF = c-family/c-target.def target-hooks-macros.h @@ -1023,6 +1024,8 @@ FLAGS_H = flags.h flag-types.h $(OPTIONS_H) OPTIONS_H = options.h flag-types.h $(OPTIONS_H_EXTRA) FUNCTION_H = function.h $(HASHTAB_H) $(TM_H) hard-reg-set.h \ $(VEC_H) $(INPUT_H) +BACKEND_H = backend.h $(TM_H) $(FUNCTION_H) $(BITMAP_H) sbitmap.h \ + $(BASIC_BLOCK_H) cfg.h EXPR_H = expr.h insn-config.h $(FUNCTION_H) $(RTL_H) $(FLAGS_H) $(TREE_H) \ $(EMIT_RTL_H) OPTABS_H = optabs.h insn-codes.h insn-opinit.h @@ -1076,7 +1079,6 @@ LTO_STREAMER_H = lto-streamer.h $(LINKER_PLUGIN_API_H) $(TARGET_H) \ $(CGRAPH_H) $(VEC_H) $(HASH_TABLE_H) $(TREE_H) $(GIMPLE_H) \ $(GCOV_IO_H) $(DIAGNOSTIC_H) alloc-pool.h IPA_PROP_H = ipa-prop.h $(TREE_H) $(VEC_H) $(CGRAPH_H) $(GIMPLE_H) alloc-pool.h -BITMAP_H = bitmap.h $(HASHTAB_H) statistics.h GCC_PLUGIN_H = gcc-plugin.h highlev-plugin-common.h plugin.def \ $(CONFIG_H) $(SYSTEM_H) $(HASHTAB_H) PLUGIN_H = plugin.h $(GCC_PLUGIN_H) diff --git a/gcc/config.gcc b/gcc/config.gcc index 8365b9170687..a6f6efec4e10 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 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-elf-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 aarch-bti-insert.o aarch64-cc-fusion.o aarch64-early-ra.o aarch64-ldp-fusion.o" target_gtfiles="\$(srcdir)/config/aarch64/aarch64-protos.h \$(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-elf-metadata.cc b/gcc/config/aarch64/aarch64-elf-metadata.cc new file mode 100644 index 000000000000..88fbb9347e85 --- /dev/null +++ b/gcc/config/aarch64/aarch64-elf-metadata.cc @@ -0,0 +1,145 @@ +/* ELF metadata for AArch64 architecture. + Copyright (C) 2024-2025 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-elf-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 diff --git a/gcc/config/aarch64/aarch64-elf-metadata.h b/gcc/config/aarch64/aarch64-elf-metadata.h new file mode 100644 index 000000000000..499260d7fffb --- /dev/null +++ b/gcc/config/aarch64/aarch64-elf-metadata.h @@ -0,0 +1,47 @@ +/* ELF metadata for AArch64 architecture. + Copyright (C) 2024-2025 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/>. */ + +#ifndef GCC_AARCH64_ELF_METADATA_H +#define GCC_AARCH64_ELF_METADATA_H + +namespace aarch64 { + +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_ELF_METADATA_H */ diff --git a/gcc/config/aarch64/aarch64.cc b/gcc/config/aarch64/aarch64.cc index f2c9322da584..cc3bcbcba9cb 100644 --- a/gcc/config/aarch64/aarch64.cc +++ b/gcc/config/aarch64/aarch64.cc @@ -83,6 +83,7 @@ #include "rtlanal.h" #include "tree-dfa.h" #include "asan.h" +#include "aarch64-elf-metadata.h" #include "aarch64-feature-deps.h" #include "config/arm/aarch-common.h" #include "config/arm/aarch-common-protos.h" @@ -29964,91 +29965,22 @@ 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; - if (aarch_bti_enabled ()) - feature_1_and |= GNU_PROPERTY_AARCH64_FEATURE_1_BTI; + aarch64::section_note_gnu_property gnu_properties; + if (aarch_bti_enabled ()) + 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 59571948479c..38a8c0637255 100644 --- a/gcc/config/aarch64/t-aarch64 +++ b/gcc/config/aarch64/t-aarch64 @@ -140,6 +140,17 @@ 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-elf-metadata.o: $(srcdir)/config/aarch64/aarch64-elf-metadata.cc \ + $(CONFIG_H) \ + $(BACKEND_H) \ + $(RTL_H) \ + $(SYSTEM_H) \ + $(TARGET_H) \ + $(srcdir)/config/aarch64/aarch64-elf-metadata.h \ + output.h + $(COMPILER) -c $(ALL_COMPILERFLAGS) $(ALL_SPPFLAGS) $(INCLUDES) \ + $(srcdir)/config/aarch64/aarch64-elf-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) \