Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

tools/nolibc: move entrypoint specifics to compiler.h

The specific attributes for the _start entrypoint are duplicated for
each architecture.
Deduplicate it into a dedicated #define into compiler.h.

For clang compatibility, the epilogue will also need to be adapted, so
move that one, too.

Acked-by: Willy Tarreau <w@1wt.eu>
Link: https://lore.kernel.org/r/20240807-nolibc-llvm-v2-5-c20f2f5fc7c2@weissschuh.net
Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>

+21 -18
+2 -2
tools/include/nolibc/arch-aarch64.h
··· 142 142 }) 143 143 144 144 /* startup code */ 145 - void __attribute__((weak, noreturn, optimize("Os", "omit-frame-pointer"))) __no_stack_protector _start(void) 145 + void __attribute__((weak, noreturn)) __nolibc_entrypoint __no_stack_protector _start(void) 146 146 { 147 147 __asm__ volatile ( 148 148 "mov x0, sp\n" /* save stack pointer to x0, as arg1 of _start_c */ 149 149 "and sp, x0, -16\n" /* sp must be 16-byte aligned in the callee */ 150 150 "bl _start_c\n" /* transfer to c runtime */ 151 151 ); 152 - __builtin_unreachable(); 152 + __nolibc_entrypoint_epilogue(); 153 153 } 154 154 #endif /* _NOLIBC_ARCH_AARCH64_H */
+2 -2
tools/include/nolibc/arch-arm.h
··· 185 185 }) 186 186 187 187 /* startup code */ 188 - void __attribute__((weak, noreturn, optimize("Os", "omit-frame-pointer"))) __no_stack_protector _start(void) 188 + void __attribute__((weak, noreturn)) __nolibc_entrypoint __no_stack_protector _start(void) 189 189 { 190 190 __asm__ volatile ( 191 191 "mov r0, sp\n" /* save stack pointer to %r0, as arg1 of _start_c */ ··· 193 193 "mov sp, ip\n" 194 194 "bl _start_c\n" /* transfer to c runtime */ 195 195 ); 196 - __builtin_unreachable(); 196 + __nolibc_entrypoint_epilogue(); 197 197 } 198 198 199 199 #endif /* _NOLIBC_ARCH_ARM_H */
+2 -2
tools/include/nolibc/arch-i386.h
··· 162 162 * 2) The deepest stack frame should be set to zero 163 163 * 164 164 */ 165 - void __attribute__((weak, noreturn, optimize("Os", "omit-frame-pointer"))) __no_stack_protector _start(void) 165 + void __attribute__((weak, noreturn)) __nolibc_entrypoint __no_stack_protector _start(void) 166 166 { 167 167 __asm__ volatile ( 168 168 "xor %ebp, %ebp\n" /* zero the stack frame */ ··· 174 174 "call _start_c\n" /* transfer to c runtime */ 175 175 "hlt\n" /* ensure it does not return */ 176 176 ); 177 - __builtin_unreachable(); 177 + __nolibc_entrypoint_epilogue(); 178 178 } 179 179 180 180 #endif /* _NOLIBC_ARCH_I386_H */
+2 -2
tools/include/nolibc/arch-loongarch.h
··· 149 149 #endif 150 150 151 151 /* startup code */ 152 - void __attribute__((weak, noreturn, optimize("Os", "omit-frame-pointer"))) __no_stack_protector _start(void) 152 + void __attribute__((weak, noreturn)) __nolibc_entrypoint __no_stack_protector _start(void) 153 153 { 154 154 __asm__ volatile ( 155 155 "move $a0, $sp\n" /* save stack pointer to $a0, as arg1 of _start_c */ 156 156 LONG_BSTRINS " $sp, $zero, 3, 0\n" /* $sp must be 16-byte aligned */ 157 157 "bl _start_c\n" /* transfer to c runtime */ 158 158 ); 159 - __builtin_unreachable(); 159 + __nolibc_entrypoint_epilogue(); 160 160 } 161 161 162 162 #endif /* _NOLIBC_ARCH_LOONGARCH_H */
+2 -2
tools/include/nolibc/arch-mips.h
··· 179 179 }) 180 180 181 181 /* startup code, note that it's called __start on MIPS */ 182 - void __attribute__((weak, noreturn, optimize("Os", "omit-frame-pointer"))) __no_stack_protector __start(void) 182 + void __attribute__((weak, noreturn)) __nolibc_entrypoint __no_stack_protector __start(void) 183 183 { 184 184 __asm__ volatile ( 185 185 ".set push\n" ··· 200 200 " nop\n" /* delayed slot */ 201 201 ".set pop\n" 202 202 ); 203 - __builtin_unreachable(); 203 + __nolibc_entrypoint_epilogue(); 204 204 } 205 205 206 206 #endif /* _NOLIBC_ARCH_MIPS_H */
+2 -2
tools/include/nolibc/arch-powerpc.h
··· 184 184 #endif /* !__powerpc64__ */ 185 185 186 186 /* startup code */ 187 - void __attribute__((weak, noreturn, optimize("Os", "omit-frame-pointer"))) __no_stack_protector _start(void) 187 + void __attribute__((weak, noreturn)) __nolibc_entrypoint __no_stack_protector _start(void) 188 188 { 189 189 #ifdef __powerpc64__ 190 190 #if _CALL_ELF == 2 ··· 215 215 "bl _start_c\n" /* transfer to c runtime */ 216 216 ); 217 217 #endif 218 - __builtin_unreachable(); 218 + __nolibc_entrypoint_epilogue(); 219 219 } 220 220 221 221 #endif /* _NOLIBC_ARCH_POWERPC_H */
+2 -2
tools/include/nolibc/arch-riscv.h
··· 140 140 }) 141 141 142 142 /* startup code */ 143 - void __attribute__((weak, noreturn, optimize("Os", "omit-frame-pointer"))) __no_stack_protector _start(void) 143 + void __attribute__((weak, noreturn)) __nolibc_entrypoint __no_stack_protector _start(void) 144 144 { 145 145 __asm__ volatile ( 146 146 ".option push\n" ··· 151 151 "andi sp, a0, -16\n" /* sp must be 16-byte aligned */ 152 152 "call _start_c\n" /* transfer to c runtime */ 153 153 ); 154 - __builtin_unreachable(); 154 + __nolibc_entrypoint_epilogue(); 155 155 } 156 156 157 157 #endif /* _NOLIBC_ARCH_RISCV_H */
+2 -2
tools/include/nolibc/arch-s390.h
··· 139 139 }) 140 140 141 141 /* startup code */ 142 - void __attribute__((weak, noreturn, optimize("Os", "omit-frame-pointer"))) __no_stack_protector _start(void) 142 + void __attribute__((weak, noreturn)) __nolibc_entrypoint __no_stack_protector _start(void) 143 143 { 144 144 __asm__ volatile ( 145 145 "lgr %r2, %r15\n" /* save stack pointer to %r2, as arg1 of _start_c */ ··· 147 147 "xc 0(8,%r15), 0(%r15)\n" /* clear backchain */ 148 148 "brasl %r14, _start_c\n" /* transfer to c runtime */ 149 149 ); 150 - __builtin_unreachable(); 150 + __nolibc_entrypoint_epilogue(); 151 151 } 152 152 153 153 struct s390_mmap_arg_struct {
+2 -2
tools/include/nolibc/arch-x86_64.h
··· 161 161 * 2) The deepest stack frame should be zero (the %rbp). 162 162 * 163 163 */ 164 - void __attribute__((weak, noreturn, optimize("Os", "omit-frame-pointer"))) __no_stack_protector _start(void) 164 + void __attribute__((weak, noreturn)) __nolibc_entrypoint __no_stack_protector _start(void) 165 165 { 166 166 __asm__ volatile ( 167 167 "xor %ebp, %ebp\n" /* zero the stack frame */ ··· 170 170 "call _start_c\n" /* transfer to c runtime */ 171 171 "hlt\n" /* ensure it does not return */ 172 172 ); 173 - __builtin_unreachable(); 173 + __nolibc_entrypoint_epilogue(); 174 174 } 175 175 176 176 #define NOLIBC_ARCH_HAS_MEMMOVE
+3
tools/include/nolibc/compiler.h
··· 12 12 # define __nolibc_has_attribute(attr) 0 13 13 #endif 14 14 15 + #define __nolibc_entrypoint __attribute__((optimize("Os", "omit-frame-pointer"))) 16 + #define __nolibc_entrypoint_epilogue() __builtin_unreachable() 17 + 15 18 #if defined(__SSP__) || defined(__SSP_STRONG__) || defined(__SSP_ALL__) || defined(__SSP_EXPLICIT__) 16 19 17 20 #define _NOLIBC_STACKPROTECTOR