Skip to content

Commit ef32e9b

Browse files
committed
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>
1 parent 02a62b5 commit ef32e9b

10 files changed

Lines changed: 21 additions & 18 deletions

File tree

tools/include/nolibc/arch-aarch64.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,13 +142,13 @@
142142
})
143143

144144
/* 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)
146146
{
147147
__asm__ volatile (
148148
"mov x0, sp\n" /* save stack pointer to x0, as arg1 of _start_c */
149149
"and sp, x0, -16\n" /* sp must be 16-byte aligned in the callee */
150150
"bl _start_c\n" /* transfer to c runtime */
151151
);
152-
__builtin_unreachable();
152+
__nolibc_entrypoint_epilogue();
153153
}
154154
#endif /* _NOLIBC_ARCH_AARCH64_H */

tools/include/nolibc/arch-arm.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,15 +185,15 @@
185185
})
186186

187187
/* 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)
189189
{
190190
__asm__ volatile (
191191
"mov r0, sp\n" /* save stack pointer to %r0, as arg1 of _start_c */
192192
"and ip, r0, #-8\n" /* sp must be 8-byte aligned in the callee */
193193
"mov sp, ip\n"
194194
"bl _start_c\n" /* transfer to c runtime */
195195
);
196-
__builtin_unreachable();
196+
__nolibc_entrypoint_epilogue();
197197
}
198198

199199
#endif /* _NOLIBC_ARCH_ARM_H */

tools/include/nolibc/arch-i386.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@
162162
* 2) The deepest stack frame should be set to zero
163163
*
164164
*/
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)
166166
{
167167
__asm__ volatile (
168168
"xor %ebp, %ebp\n" /* zero the stack frame */
@@ -174,7 +174,7 @@ void __attribute__((weak, noreturn, optimize("Os", "omit-frame-pointer"))) __no_
174174
"call _start_c\n" /* transfer to c runtime */
175175
"hlt\n" /* ensure it does not return */
176176
);
177-
__builtin_unreachable();
177+
__nolibc_entrypoint_epilogue();
178178
}
179179

180180
#endif /* _NOLIBC_ARCH_I386_H */

tools/include/nolibc/arch-loongarch.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,14 +149,14 @@
149149
#endif
150150

151151
/* 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)
153153
{
154154
__asm__ volatile (
155155
"move $a0, $sp\n" /* save stack pointer to $a0, as arg1 of _start_c */
156156
LONG_BSTRINS " $sp, $zero, 3, 0\n" /* $sp must be 16-byte aligned */
157157
"bl _start_c\n" /* transfer to c runtime */
158158
);
159-
__builtin_unreachable();
159+
__nolibc_entrypoint_epilogue();
160160
}
161161

162162
#endif /* _NOLIBC_ARCH_LOONGARCH_H */

tools/include/nolibc/arch-mips.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@
179179
})
180180

181181
/* 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)
183183
{
184184
__asm__ volatile (
185185
".set push\n"
@@ -200,7 +200,7 @@ void __attribute__((weak, noreturn, optimize("Os", "omit-frame-pointer"))) __no_
200200
" nop\n" /* delayed slot */
201201
".set pop\n"
202202
);
203-
__builtin_unreachable();
203+
__nolibc_entrypoint_epilogue();
204204
}
205205

206206
#endif /* _NOLIBC_ARCH_MIPS_H */

tools/include/nolibc/arch-powerpc.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@
184184
#endif /* !__powerpc64__ */
185185

186186
/* 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)
188188
{
189189
#ifdef __powerpc64__
190190
#if _CALL_ELF == 2
@@ -215,7 +215,7 @@ void __attribute__((weak, noreturn, optimize("Os", "omit-frame-pointer"))) __no_
215215
"bl _start_c\n" /* transfer to c runtime */
216216
);
217217
#endif
218-
__builtin_unreachable();
218+
__nolibc_entrypoint_epilogue();
219219
}
220220

221221
#endif /* _NOLIBC_ARCH_POWERPC_H */

tools/include/nolibc/arch-riscv.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@
140140
})
141141

142142
/* 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)
144144
{
145145
__asm__ volatile (
146146
".option push\n"
@@ -151,7 +151,7 @@ void __attribute__((weak, noreturn, optimize("Os", "omit-frame-pointer"))) __no_
151151
"andi sp, a0, -16\n" /* sp must be 16-byte aligned */
152152
"call _start_c\n" /* transfer to c runtime */
153153
);
154-
__builtin_unreachable();
154+
__nolibc_entrypoint_epilogue();
155155
}
156156

157157
#endif /* _NOLIBC_ARCH_RISCV_H */

tools/include/nolibc/arch-s390.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,15 +139,15 @@
139139
})
140140

141141
/* 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)
143143
{
144144
__asm__ volatile (
145145
"lgr %r2, %r15\n" /* save stack pointer to %r2, as arg1 of _start_c */
146146
"aghi %r15, -160\n" /* allocate new stackframe */
147147
"xc 0(8,%r15), 0(%r15)\n" /* clear backchain */
148148
"brasl %r14, _start_c\n" /* transfer to c runtime */
149149
);
150-
__builtin_unreachable();
150+
__nolibc_entrypoint_epilogue();
151151
}
152152

153153
struct s390_mmap_arg_struct {

tools/include/nolibc/arch-x86_64.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@
161161
* 2) The deepest stack frame should be zero (the %rbp).
162162
*
163163
*/
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)
165165
{
166166
__asm__ volatile (
167167
"xor %ebp, %ebp\n" /* zero the stack frame */
@@ -170,7 +170,7 @@ void __attribute__((weak, noreturn, optimize("Os", "omit-frame-pointer"))) __no_
170170
"call _start_c\n" /* transfer to c runtime */
171171
"hlt\n" /* ensure it does not return */
172172
);
173-
__builtin_unreachable();
173+
__nolibc_entrypoint_epilogue();
174174
}
175175

176176
#define NOLIBC_ARCH_HAS_MEMMOVE

tools/include/nolibc/compiler.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
# define __nolibc_has_attribute(attr) 0
1313
#endif
1414

15+
#define __nolibc_entrypoint __attribute__((optimize("Os", "omit-frame-pointer")))
16+
#define __nolibc_entrypoint_epilogue() __builtin_unreachable()
17+
1518
#if defined(__SSP__) || defined(__SSP_STRONG__) || defined(__SSP_ALL__) || defined(__SSP_EXPLICIT__)
1619

1720
#define _NOLIBC_STACKPROTECTOR

0 commit comments

Comments
 (0)