aboutsummaryrefslogtreecommitdiff
path: root/gcc
diff options
context:
space:
mode:
authorJose E. Marchesi <jose.marchesi@oracle.com>2023-08-17 14:19:15 +0200
committerJose E. Marchesi <jose.marchesi@oracle.com>2023-08-17 14:39:46 +0200
commitb7c50f68f26146e7038e9793b6c623ff4c4895d3 (patch)
tree2f26cfb119123da68cfc7190dc586364aa1b0ea3 /gcc
parentd07bce478f9d770de5acb0480a3f0cec2f8b72d8 (diff)
downloadgcc-b7c50f68f26146e7038e9793b6c623ff4c4895d3.zip
gcc-b7c50f68f26146e7038e9793b6c623ff4c4895d3.tar.gz
gcc-b7c50f68f26146e7038e9793b6c623ff4c4895d3.tar.bz2
bpf: support `naked' function attributes in BPF targets
The kernel selftests and other BPF programs make extensive use of the `naked' function attribute with bodies written using basic inline assembly. This patch adds support for the attribute to bpf-unkonwn-none, makes it to inhibit warnings due to lack of explicit `return' statement, and updates documentation and testsuite accordingly. Tested in x86_64-linux-gnu host and bpf-unknown-none target. gcc/ChangeLog PR target/111046 * config/bpf/bpf.cc (bpf_attribute_table): Add entry for the `naked' function attribute. (bpf_warn_func_return): New function. (TARGET_WARN_FUNC_RETURN): Define. (bpf_expand_prologue): Add preventive comment. (bpf_expand_epilogue): Likewise. * doc/extend.texi (BPF Function Attributes): Document the `naked' function attribute. gcc/testsuite/ChangeLog * gcc.target/bpf/naked-1.c: New test.
Diffstat (limited to 'gcc')
-rw-r--r--gcc/config/bpf/bpf.cc25
-rw-r--r--gcc/doc/extend.texi11
-rw-r--r--gcc/testsuite/gcc.target/bpf/naked-1.c12
3 files changed, 48 insertions, 0 deletions
diff --git a/gcc/config/bpf/bpf.cc b/gcc/config/bpf/bpf.cc
index 1d0abd7..437bd65 100644
--- a/gcc/config/bpf/bpf.cc
+++ b/gcc/config/bpf/bpf.cc
@@ -154,6 +154,10 @@ static const struct attribute_spec bpf_attribute_table[] =
{ "preserve_access_index", 0, -1, false, true, false, true,
bpf_handle_preserve_access_index_attribute, NULL },
+ /* Support for `naked' function attribute. */
+ { "naked", 0, 1, false, false, false, false,
+ bpf_handle_fndecl_attribute, NULL },
+
/* The last attribute spec is set to be NULL. */
{ NULL, 0, 0, false, false, false, false, NULL, NULL }
};
@@ -335,6 +339,21 @@ bpf_function_value_regno_p (const unsigned int regno)
#undef TARGET_FUNCTION_VALUE_REGNO_P
#define TARGET_FUNCTION_VALUE_REGNO_P bpf_function_value_regno_p
+
+/* Determine whether to warn about lack of return statement in a
+ function. */
+
+static bool
+bpf_warn_func_return (tree decl)
+{
+ /* Naked functions are implemented entirely in assembly, including
+ the return instructions. */
+ return lookup_attribute ("naked", DECL_ATTRIBUTES (decl)) == NULL_TREE;
+}
+
+#undef TARGET_WARN_FUNC_RETURN
+#define TARGET_WARN_FUNC_RETURN bpf_warn_func_return
+
/* Compute the size of the function's stack frame, including the local
area and the register-save area. */
@@ -388,6 +407,9 @@ bpf_expand_prologue (void)
dynamically. This should have been checked already and an error
emitted. */
gcc_assert (!cfun->calls_alloca);
+
+ /* If we ever need to have a proper prologue here, please mind the
+ `naked' function attribute. */
}
/* Expand to the instructions in a function epilogue. This function
@@ -399,6 +421,9 @@ bpf_expand_epilogue (void)
/* See note in bpf_expand_prologue for an explanation on why we are
not restoring callee-saved registers in BPF. */
+ /* If we ever need to do anything else than just generating a return
+ instruction here, please mind the `naked' function attribute. */
+
emit_jump_insn (gen_exit ());
}
diff --git a/gcc/doc/extend.texi b/gcc/doc/extend.texi
index b363386..f657032 100644
--- a/gcc/doc/extend.texi
+++ b/gcc/doc/extend.texi
@@ -5172,6 +5172,17 @@ attribute. Example:
int bpf_probe_read (void *dst, int size, const void *unsafe_ptr)
__attribute__ ((kernel_helper (4)));
@end smallexample
+
+@cindex @code{naked} function attribute, BPF
+@item naked
+This attribute allows the compiler to construct the requisite function
+declaration, while allowing the body of the function to be assembly
+code. The specified function will not have prologue/epilogue
+sequences generated by the compiler. Only basic @code{asm} statements
+can safely be included in naked functions (@pxref{Basic Asm}). While
+using extended @code{asm} or a mixture of basic @code{asm} and C code
+may appear to work, they cannot be depended upon to work reliably and
+are not supported.
@end table
@node C-SKY Function Attributes
diff --git a/gcc/testsuite/gcc.target/bpf/naked-1.c b/gcc/testsuite/gcc.target/bpf/naked-1.c
new file mode 100644
index 0000000..cbbc4c5
--- /dev/null
+++ b/gcc/testsuite/gcc.target/bpf/naked-1.c
@@ -0,0 +1,12 @@
+/* Verify that __attribute__((naked)) is accepted and
+ produces a naked function. Also, the compiler must not
+ warn for the lack of return statement. */
+/* { dg-do compile } */
+/* { dg-options "-O0 -Wreturn-type" } */
+
+int __attribute__((naked)) foo()
+{
+ __asm__ volatile ("@ naked");
+}
+/* { dg-final { scan-assembler "\t@ naked" } } */
+/* { dg-final { scan-assembler "\texit\n" } } */