Skip to content

Commit 0b4a3c1

Browse files
author
Matthieu Longo
committed
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
1 parent 138d0a2 commit 0b4a3c1

File tree

6 files changed

+214
-77
lines changed

6 files changed

+214
-77
lines changed

gcc/Makefile.in

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -972,6 +972,7 @@ endif
972972
DUMPFILE_H = $(srcdir)/../libcpp/include/line-map.h dumpfile.h
973973
VEC_H = vec.h statistics.h $(GGC_H)
974974
HASH_TABLE_H = $(HASHTAB_H) hash-table.h $(GGC_H)
975+
BITMAP_H = bitmap.h $(HASHTAB_H) statistics.h
975976
EXCEPT_H = except.h $(HASHTAB_H)
976977
TARGET_DEF = target.def target-hooks-macros.h target-insns.def
977978
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)
10231024
OPTIONS_H = options.h flag-types.h $(OPTIONS_H_EXTRA)
10241025
FUNCTION_H = function.h $(HASHTAB_H) $(TM_H) hard-reg-set.h \
10251026
$(VEC_H) $(INPUT_H)
1027+
BACKEND_H = backend.h $(TM_H) $(FUNCTION_H) $(BITMAP_H) sbitmap.h \
1028+
$(BASIC_BLOCK_H) cfg.h
10261029
EXPR_H = expr.h insn-config.h $(FUNCTION_H) $(RTL_H) $(FLAGS_H) $(TREE_H) \
10271030
$(EMIT_RTL_H)
10281031
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) \
10761079
$(CGRAPH_H) $(VEC_H) $(HASH_TABLE_H) $(TREE_H) $(GIMPLE_H) \
10771080
$(GCOV_IO_H) $(DIAGNOSTIC_H) alloc-pool.h
10781081
IPA_PROP_H = ipa-prop.h $(TREE_H) $(VEC_H) $(CGRAPH_H) $(GIMPLE_H) alloc-pool.h
1079-
BITMAP_H = bitmap.h $(HASHTAB_H) statistics.h
10801082
GCC_PLUGIN_H = gcc-plugin.h highlev-plugin-common.h plugin.def \
10811083
$(CONFIG_H) $(SYSTEM_H) $(HASHTAB_H)
10821084
PLUGIN_H = plugin.h $(GCC_PLUGIN_H)

gcc/config.gcc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ aarch64*-*-*)
351351
c_target_objs="aarch64-c.o"
352352
cxx_target_objs="aarch64-c.o"
353353
d_target_objs="aarch64-d.o"
354-
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"
354+
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"
355355
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"
356356
target_has_targetm_common=yes
357357
;;
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
/* ELF metadata for AArch64 architecture.
2+
Copyright (C) 2024-2025 Free Software Foundation, Inc.
3+
Contributed by ARM Ltd.
4+
5+
This file is part of GCC.
6+
7+
GCC is free software; you can redistribute it and/or modify it
8+
under the terms of the GNU General Public License as published by
9+
the Free Software Foundation; either version 3, or (at your option)
10+
any later version.
11+
12+
GCC is distributed in the hope that it will be useful, but
13+
WITHOUT ANY WARRANTY; without even the implied warranty of
14+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15+
General Public License for more details.
16+
17+
You should have received a copy of the GNU General Public License
18+
along with GCC; see the file COPYING3. If not see
19+
<http://www.gnu.org/licenses/>. */
20+
21+
#define INCLUDE_STRING
22+
#define INCLUDE_ALGORITHM
23+
#define INCLUDE_MEMORY
24+
#define INCLUDE_VECTOR
25+
#include "config.h"
26+
#include "system.h"
27+
#include "coretypes.h"
28+
#include "backend.h"
29+
#include "target.h"
30+
#include "rtl.h"
31+
#include "output.h"
32+
33+
#include "aarch64-elf-metadata.h"
34+
35+
/* Defined for convenience. */
36+
#define POINTER_BYTES (POINTER_SIZE / BITS_PER_UNIT)
37+
38+
namespace aarch64 {
39+
40+
constexpr unsigned GNU_PROPERTY_AARCH64_FEATURE_1_AND = 0xc0000000;
41+
constexpr unsigned GNU_PROPERTY_AARCH64_FEATURE_1_BTI = (1U << 0);
42+
constexpr unsigned GNU_PROPERTY_AARCH64_FEATURE_1_PAC = (1U << 1);
43+
constexpr unsigned GNU_PROPERTY_AARCH64_FEATURE_1_GCS = (1U << 2);
44+
45+
namespace {
46+
47+
std::string
48+
gnu_property_features_to_string (unsigned feature_1_and)
49+
{
50+
struct flag_name
51+
{
52+
unsigned int mask;
53+
const char *name;
54+
};
55+
56+
static const flag_name flags[] = {
57+
{GNU_PROPERTY_AARCH64_FEATURE_1_BTI, "BTI"},
58+
{GNU_PROPERTY_AARCH64_FEATURE_1_PAC, "PAC"},
59+
{GNU_PROPERTY_AARCH64_FEATURE_1_GCS, "GCS"},
60+
};
61+
62+
const char *separator = "";
63+
std::string s_features;
64+
for (auto &flag : flags)
65+
if (feature_1_and & flag.mask)
66+
{
67+
s_features.append (separator).append (flag.name);
68+
separator = ", ";
69+
}
70+
return s_features;
71+
};
72+
73+
} // namespace anonymous
74+
75+
section_note_gnu_property::section_note_gnu_property ()
76+
: m_feature_1_and (0) {}
77+
78+
void
79+
section_note_gnu_property::bti_enabled ()
80+
{
81+
m_feature_1_and |= GNU_PROPERTY_AARCH64_FEATURE_1_BTI;
82+
}
83+
84+
void
85+
section_note_gnu_property::pac_enabled ()
86+
{
87+
m_feature_1_and |= GNU_PROPERTY_AARCH64_FEATURE_1_PAC;
88+
}
89+
90+
void
91+
section_note_gnu_property::gcs_enabled ()
92+
{
93+
m_feature_1_and |= GNU_PROPERTY_AARCH64_FEATURE_1_GCS;
94+
}
95+
96+
void
97+
section_note_gnu_property::write () const
98+
{
99+
if (m_feature_1_and)
100+
{
101+
/* Generate .note.gnu.property section. */
102+
switch_to_section (
103+
get_section (".note.gnu.property", SECTION_NOTYPE, NULL));
104+
105+
/* PT_NOTE header: namesz, descsz, type.
106+
namesz = 4 ("GNU\0")
107+
descsz = 16 (Size of the program property array)
108+
[(12 + padding) * Number of array elements]
109+
type = 5 (NT_GNU_PROPERTY_TYPE_0). */
110+
assemble_align (POINTER_SIZE);
111+
assemble_integer (GEN_INT (4), 4, 32, 1);
112+
assemble_integer (GEN_INT (ROUND_UP (12, POINTER_BYTES)), 4, 32, 1);
113+
assemble_integer (GEN_INT (5), 4, 32, 1);
114+
115+
/* PT_NOTE name. */
116+
assemble_string ("GNU", 4);
117+
118+
/* PT_NOTE contents for NT_GNU_PROPERTY_TYPE_0:
119+
type = GNU_PROPERTY_AARCH64_FEATURE_1_AND
120+
datasz = 4
121+
data = feature_1_and. */
122+
fputs (integer_asm_op (4, true), asm_out_file);
123+
fprint_whex (asm_out_file, GNU_PROPERTY_AARCH64_FEATURE_1_AND);
124+
putc ('\n', asm_out_file);
125+
assemble_integer (GEN_INT (4), 4, 32, 1);
126+
127+
fputs (integer_asm_op (4, true), asm_out_file);
128+
fprint_whex (asm_out_file, m_feature_1_and);
129+
if (flag_debug_asm)
130+
{
131+
auto const &s_features
132+
= gnu_property_features_to_string (m_feature_1_and);
133+
asm_fprintf (asm_out_file,
134+
"\t%s GNU_PROPERTY_AARCH64_FEATURE_1_AND (%s)\n",
135+
ASM_COMMENT_START, s_features.c_str ());
136+
}
137+
else
138+
putc ('\n', asm_out_file);
139+
140+
/* Pad the size of the note to the required alignment. */
141+
assemble_align (POINTER_SIZE);
142+
}
143+
}
144+
145+
} // namespace aarch64
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/* ELF metadata for AArch64 architecture.
2+
Copyright (C) 2024-2025 Free Software Foundation, Inc.
3+
Contributed by ARM Ltd.
4+
5+
This file is part of GCC.
6+
7+
GCC is free software; you can redistribute it and/or modify it
8+
under the terms of the GNU General Public License as published by
9+
the Free Software Foundation; either version 3, or (at your option)
10+
any later version.
11+
12+
GCC is distributed in the hope that it will be useful, but
13+
WITHOUT ANY WARRANTY; without even the implied warranty of
14+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15+
General Public License for more details.
16+
17+
You should have received a copy of the GNU General Public License
18+
along with GCC; see the file COPYING3. If not see
19+
<http://www.gnu.org/licenses/>. */
20+
21+
#ifndef GCC_AARCH64_ELF_METADATA_H
22+
#define GCC_AARCH64_ELF_METADATA_H
23+
24+
namespace aarch64 {
25+
26+
class section_note_gnu_property
27+
{
28+
public:
29+
section_note_gnu_property ();
30+
31+
/* Add BTI flag to GNU properties. */
32+
void bti_enabled ();
33+
/* Add GCS flag to GNU properties. */
34+
void gcs_enabled ();
35+
/* Add PAC flag to GNU properties. */
36+
void pac_enabled ();
37+
38+
/* Write the data to the assembly file. */
39+
void write () const;
40+
41+
private:
42+
unsigned m_feature_1_and;
43+
};
44+
45+
} // namespace aarch64
46+
47+
#endif /* GCC_AARCH64_ELF_METADATA_H */

gcc/config/aarch64/aarch64.cc

Lines changed: 7 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@
8383
#include "rtlanal.h"
8484
#include "tree-dfa.h"
8585
#include "asan.h"
86+
#include "aarch64-elf-metadata.h"
8687
#include "aarch64-feature-deps.h"
8788
#include "config/arm/aarch-common.h"
8889
#include "config/arm/aarch-common-protos.h"
@@ -29964,91 +29965,22 @@ aarch64_can_tag_addresses ()
2996429965

2996529966
/* Implement TARGET_ASM_FILE_END for AArch64. This adds the AArch64 GNU NOTE
2996629967
section at the end if needed. */
29967-
#define GNU_PROPERTY_AARCH64_FEATURE_1_AND 0xc0000000
29968-
#define GNU_PROPERTY_AARCH64_FEATURE_1_BTI (1U << 0)
29969-
#define GNU_PROPERTY_AARCH64_FEATURE_1_PAC (1U << 1)
29970-
#define GNU_PROPERTY_AARCH64_FEATURE_1_GCS (1U << 2)
2997129968
void
2997229969
aarch64_file_end_indicate_exec_stack ()
2997329970
{
2997429971
file_end_indicate_exec_stack ();
2997529972

29976-
unsigned feature_1_and = 0;
29977-
if (aarch_bti_enabled ())
29978-
feature_1_and |= GNU_PROPERTY_AARCH64_FEATURE_1_BTI;
29973+
aarch64::section_note_gnu_property gnu_properties;
2997929974

29975+
if (aarch_bti_enabled ())
29976+
gnu_properties.bti_enabled ();
2998029977
if (aarch_ra_sign_scope != AARCH_FUNCTION_NONE)
29981-
feature_1_and |= GNU_PROPERTY_AARCH64_FEATURE_1_PAC;
29982-
29978+
gnu_properties.pac_enabled ();
2998329979
if (aarch64_gcs_enabled ())
29984-
feature_1_and |= GNU_PROPERTY_AARCH64_FEATURE_1_GCS;
29985-
29986-
if (feature_1_and)
29987-
{
29988-
/* Generate .note.gnu.property section. */
29989-
switch_to_section (get_section (".note.gnu.property",
29990-
SECTION_NOTYPE, NULL));
29991-
29992-
/* PT_NOTE header: namesz, descsz, type.
29993-
namesz = 4 ("GNU\0")
29994-
descsz = 16 (Size of the program property array)
29995-
[(12 + padding) * Number of array elements]
29996-
type = 5 (NT_GNU_PROPERTY_TYPE_0). */
29997-
assemble_align (POINTER_SIZE);
29998-
assemble_integer (GEN_INT (4), 4, 32, 1);
29999-
assemble_integer (GEN_INT (ROUND_UP (12, POINTER_BYTES)), 4, 32, 1);
30000-
assemble_integer (GEN_INT (5), 4, 32, 1);
30001-
30002-
/* PT_NOTE name. */
30003-
assemble_string ("GNU", 4);
30004-
30005-
/* PT_NOTE contents for NT_GNU_PROPERTY_TYPE_0:
30006-
type = GNU_PROPERTY_AARCH64_FEATURE_1_AND
30007-
datasz = 4
30008-
data = feature_1_and. */
30009-
fputs (integer_asm_op (4, true), asm_out_file);
30010-
fprint_whex (asm_out_file, GNU_PROPERTY_AARCH64_FEATURE_1_AND);
30011-
putc ('\n', asm_out_file);
30012-
assemble_integer (GEN_INT (4), 4, 32, 1);
30013-
30014-
fputs (integer_asm_op (4, true), asm_out_file);
30015-
fprint_whex (asm_out_file, feature_1_and);
30016-
if (flag_debug_asm)
30017-
{
30018-
struct flag_name
30019-
{
30020-
unsigned int mask;
30021-
const char *name;
30022-
};
30023-
static const flag_name flags[] = {
30024-
{ GNU_PROPERTY_AARCH64_FEATURE_1_BTI, "BTI" },
30025-
{ GNU_PROPERTY_AARCH64_FEATURE_1_PAC, "PAC" },
30026-
{ GNU_PROPERTY_AARCH64_FEATURE_1_GCS, "GCS" },
30027-
};
30028-
30029-
const char *separator = "";
30030-
std::string s_features;
30031-
for (auto &flag : flags)
30032-
if (feature_1_and & flag.mask)
30033-
{
30034-
s_features.append (separator).append (flag.name);
30035-
separator = ", ";
30036-
}
29980+
gnu_properties.gcs_enabled ();
3003729981

30038-
asm_fprintf (asm_out_file,
30039-
"\t%s GNU_PROPERTY_AARCH64_FEATURE_1_AND (%s)\n",
30040-
ASM_COMMENT_START, s_features.c_str ());
30041-
}
30042-
else
30043-
putc ('\n', asm_out_file);
30044-
/* Pad the size of the note to the required alignment. */
30045-
assemble_align (POINTER_SIZE);
30046-
}
29982+
gnu_properties.write ();
3004729983
}
30048-
#undef GNU_PROPERTY_AARCH64_FEATURE_1_GCS
30049-
#undef GNU_PROPERTY_AARCH64_FEATURE_1_PAC
30050-
#undef GNU_PROPERTY_AARCH64_FEATURE_1_BTI
30051-
#undef GNU_PROPERTY_AARCH64_FEATURE_1_AND
3005229984

3005329985
/* Helper function for straight line speculation.
3005429986
Return what barrier should be emitted for straight line speculation

gcc/config/aarch64/t-aarch64

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,17 @@ aarch-common.o: $(srcdir)/config/arm/aarch-common.cc $(CONFIG_H) $(SYSTEM_H) \
140140
$(COMPILER) -c $(ALL_COMPILERFLAGS) $(ALL_CPPFLAGS) $(INCLUDES) \
141141
$(srcdir)/config/arm/aarch-common.cc
142142

143+
aarch64-elf-metadata.o: $(srcdir)/config/aarch64/aarch64-elf-metadata.cc \
144+
$(CONFIG_H) \
145+
$(BACKEND_H) \
146+
$(RTL_H) \
147+
$(SYSTEM_H) \
148+
$(TARGET_H) \
149+
$(srcdir)/config/aarch64/aarch64-elf-metadata.h \
150+
output.h
151+
$(COMPILER) -c $(ALL_COMPILERFLAGS) $(ALL_SPPFLAGS) $(INCLUDES) \
152+
$(srcdir)/config/aarch64/aarch64-elf-metadata.cc
153+
143154
aarch64-c.o: $(srcdir)/config/aarch64/aarch64-c.cc $(CONFIG_H) $(SYSTEM_H) \
144155
coretypes.h $(TM_H) $(TREE_H) output.h $(C_COMMON_H) $(TARGET_H)
145156
$(COMPILER) -c $(ALL_COMPILERFLAGS) $(ALL_CPPFLAGS) $(INCLUDES) \

0 commit comments

Comments
 (0)