mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] tools/nolibc: sh: enter _start_c with jsr instead of bsr
@ 2026-10-22 10:01 Daniel Palmer
  2026-10-22 16:12 ` Thomas Weißschuh
  0 siblings, 1 reply; 2+ messages in thread
From: Daniel Palmer @ 2026-10-22 10:01 UTC (permalink / raw)
  To: w, linux; +Cc: linux-kernel, linux-sh, Daniel Palmer

bsr has limited displacement and cannot reach _start_c if its
more than +/-4KiB away.

This starts to show up if you have big programs (static binary
of the game Quake in my case) and/or use LTO and things get moved
around.

Replace bsr with a calculation of the effective address of
_start_c and jsr.

Tested on QEMU and real SH4 and SH4a machines.

Signed-off-by: Daniel Palmer <daniel@0x0f.com>
---

I'm no SuperH asm expert but this works for me. :)
Maybe there is a way in C to get _start and _start_c to always
be close together that would avoid this entirely?

 tools/include/nolibc/arch-sh.h | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/tools/include/nolibc/arch-sh.h b/tools/include/nolibc/arch-sh.h
index 996cfbe1bee3..7318f52dcba9 100644
--- a/tools/include/nolibc/arch-sh.h
+++ b/tools/include/nolibc/arch-sh.h
@@ -154,8 +154,13 @@ void __attribute__((weak, noreturn)) __nolibc_entrypoint __nolibc_no_stack_prote
 		"_start:\n"
 
 		"mov sp, r4\n"               /* save argc pointer to r4, as arg1 of _start_c */
-		"bsr _start_c\n"             /* transfer to c runtime                        */
+		"mova 1f, r0\n"              /* load the effective address of 1: into r0     */
+		"mov.l @r0, r1\n"            /* load the distance to _start_c from 1:        */
+		"add r0, r1\n"               /* calculate address of _start_c                */
+		"jsr @r1\n"                  /* transfer to c runtime                        */
 		"nop\n"                      /* delay slot                                   */
+		".balign 4\n"
+		"1: .long _start_c - 1b\n"   /* Distance from here to _start_c               */
 
 		".size _start, .-_start\n"
 	);
-- 
2.55.0


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] tools/nolibc: sh: enter _start_c with jsr instead of bsr
  2026-10-22 10:01 [PATCH] tools/nolibc: sh: enter _start_c with jsr instead of bsr Daniel Palmer
@ 2026-10-22 16:12 ` Thomas Weißschuh
  0 siblings, 0 replies; 2+ messages in thread
From: Thomas Weißschuh @ 2026-10-22 16:12 UTC (permalink / raw)
  To: Daniel Palmer; +Cc: w, linux-kernel, linux-sh

Hi Daniel!

On 2026-10-22 19:01:59+0900, Daniel Palmer wrote:
> bsr has limited displacement and cannot reach _start_c if its
> more than +/-4KiB away.
> 
> This starts to show up if you have big programs (static binary
> of the game Quake in my case) and/or use LTO and things get moved
> around.

That is certainly a usecase that justifies making the two other SH4
nolibc users' binaries a bit larger :-)

> Replace bsr with a calculation of the effective address of
> _start_c and jsr.
> 
> Tested on QEMU and real SH4 and SH4a machines.
> 
> Signed-off-by: Daniel Palmer <daniel@0x0f.com>
> ---
> 
> I'm no SuperH asm expert but this works for me. :)
> Maybe there is a way in C to get _start and _start_c to always
> be close together that would avoid this entirely?

That would be my preference, too.

Maybe one of these works:

1) Put both _start() and _start_c() in a dedicated .text subsection.

Apparently the default GNU ld linkerscript contains '*(SORT(.text.sorted.*))'.
So if we use .text.sorted.nolibc_start for both functions they should
end up next to each other.
See https://sourceware.org/binutils/docs/ld/Input-Section-Wildcards.html.

2) Put _start() *into* _start_c:

diff --git a/tools/include/nolibc/crt.h b/tools/include/nolibc/crt.h
index 714256228914..ebcd0ea2b9f2 100644
--- a/tools/include/nolibc/crt.h
+++ b/tools/include/nolibc/crt.h
@@ -115,6 +115,19 @@ void _start_c(long *sp)
                (*--dtor_func)();

        exit(exitcode);
+
+       __asm__ volatile (
+               ".global _start\n"           /* The C function will have a prologue,         */
+               ".type _start, @function\n"  /* corrupting "sp"                              */
+               ".weak _start\n"
+               "_start:\n"
+
+               "mov sp, r4\n"               /* save argc pointer to r4, as arg1 of _start_c */
+               "bsr _start_c\n"             /* transfer to c runtime                        */
+               "nop\n"                      /* delay slot                                   */
+
+               ".size _start, .-_start\n"
+       );
 }

 #endif /* NOLIBC_NO_RUNTIME */

But that for all architectures, using macros etc.

If the .text.sorted. solution works, I'd prefer that.

>  tools/include/nolibc/arch-sh.h | 7 ++++++-
>  1 file changed, 6 insertions(+), 1 deletion(-)
> 
> diff --git a/tools/include/nolibc/arch-sh.h b/tools/include/nolibc/arch-sh.h
> index 996cfbe1bee3..7318f52dcba9 100644
> --- a/tools/include/nolibc/arch-sh.h
> +++ b/tools/include/nolibc/arch-sh.h
> @@ -154,8 +154,13 @@ void __attribute__((weak, noreturn)) __nolibc_entrypoint __nolibc_no_stack_prote
>  		"_start:\n"
>  
>  		"mov sp, r4\n"               /* save argc pointer to r4, as arg1 of _start_c */
> -		"bsr _start_c\n"             /* transfer to c runtime                        */
> +		"mova 1f, r0\n"              /* load the effective address of 1: into r0     */
> +		"mov.l @r0, r1\n"            /* load the distance to _start_c from 1:        */
> +		"add r0, r1\n"               /* calculate address of _start_c                */
> +		"jsr @r1\n"                  /* transfer to c runtime                        */
>  		"nop\n"                      /* delay slot                                   */
> +		".balign 4\n"
> +		"1: .long _start_c - 1b\n"   /* Distance from here to _start_c               */
>  
>  		".size _start, .-_start\n"
>  	);
> -- 
> 2.55.0
> 

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-10-22 16:12 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-10-22 10:01 [PATCH] tools/nolibc: sh: enter _start_c with jsr instead of bsr Daniel Palmer
2026-10-22 16:12 ` Thomas Weißschuh

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®