diff mbox series

[04/12] tools/nolibc: use attribute((naked)) if available

Message ID 20240728-nolibc-llvm-v1-4-bc384269bc35@weissschuh.net
State New
Headers show
Series tools/nolibc: improve LLVM/clang support | expand

Commit Message

Thomas Weißschuh July 28, 2024, 10:09 a.m. UTC
The current entrypoint attributes optimize("Os", "omit-frame-pointer")
are intended to avoid all compiler generated code, like function
porologue and epilogue.
This is the exact usecase implemented by the attribute "naked".

Unfortunately this is not implemented by GCC for all targets,
so only use it where available.
This also provides compatibility with clang, which recognizes the
"naked" attribute but not the previously used attribute "optimized".

Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
---
 tools/include/nolibc/compiler.h | 9 +++++++++
 1 file changed, 9 insertions(+)

Comments

Thomas Weißschuh Aug. 3, 2024, 8:55 p.m. UTC | #1
Aug 3, 2024 20:33:11 Willy Tarreau <w@1wt.eu>:

> On Sat, Aug 03, 2024 at 08:28:08PM +0200, Thomas Weißschuh  wrote:
>>> I think that it can resolve to roughly this:
>>>
>>> #if defined(__has_attribute) && __has_attribute(naked)
>>> #  define __entrypoint __attribute__((naked))
>>> #  define __entrypoint_epilogue()
>>> #else
>>> #  define __entrypoint __attribute__((optimize("Os", "omit-frame-pointer")))
>>> #  define __entrypoint_epilogue() __builtin_unreachable()
>>> #endif
>>
>> We would need to duplicate the define for the
>> !defined(__has_attribute) case.
>
> I don't understand why. Above both are tested on the first line.
> Am I missing something ?

This specifically does not work [0]:

    a result, combining the two tests into a single expression as shown below would only be valid with a compiler that supports the operator but not with others that don’t.


>
>> I wanted to avoid that duplication.
>>> What do you think ?
>>
>> With the reasoning above I'll let you choose.
>
> I'm fine with avoiding duplication, I just don't understand why there
> should be.
>
> Willy

[0] https://gcc.gnu.org/onlinedocs/cpp/_005f_005fhas_005fattribute.html
diff mbox series

Patch

diff --git a/tools/include/nolibc/compiler.h b/tools/include/nolibc/compiler.h
index fe3701863634..f77bb7d3e1a8 100644
--- a/tools/include/nolibc/compiler.h
+++ b/tools/include/nolibc/compiler.h
@@ -9,6 +9,15 @@ 
 #define __entrypoint __attribute__((optimize("Os", "omit-frame-pointer")))
 #define __entrypoint_epilogue() __builtin_unreachable()
 
+#if defined(__has_attribute)
+#  if __has_attribute(naked)
+#    undef  __entrypoint
+#    define __entrypoint __attribute__((naked))
+#    undef __entrypoint_epilogue
+#    define __entrypoint_epilogue()
+#  endif
+#endif /* defined(__has_attribute) */
+
 #if defined(__SSP__) || defined(__SSP_STRONG__) || defined(__SSP_ALL__) || defined(__SSP_EXPLICIT__)
 
 #define _NOLIBC_STACKPROTECTOR