Re: [RFC PATCH v2 04/10] tools/nolibc: m68k: Add relocation support

From: Thomas Weißschuh

Date: Mon Feb 16 2026 - 15:55:28 EST


On 2026-02-04 21:45:36+0900, Daniel Palmer wrote:
> Add support for handling relocations on m68k.
>
> This also changes the branch to _start_c to be relative.
> This might break FLAT binaries so needs to be checked
> and probably wrapped in some #ifdef .. magic.

You are the expert here, so some more definitive answer to this question
would be nice :-)

> Signed-off-by: Daniel Palmer <daniel@xxxxxxxxx>
> ---
> tools/include/nolibc/arch-m68k.h | 27 ++++++++++++++++++++++++++-
> 1 file changed, 26 insertions(+), 1 deletion(-)
>
> diff --git a/tools/include/nolibc/arch-m68k.h b/tools/include/nolibc/arch-m68k.h
> index 2a4fbada5e79..e82ff48d3fb8 100644
> --- a/tools/include/nolibc/arch-m68k.h
> +++ b/tools/include/nolibc/arch-m68k.h
> @@ -10,8 +10,16 @@
> #ifndef _NOLIBC_ARCH_M68K_H
> #define _NOLIBC_ARCH_M68K_H
>
> +#include "elf.h"
> +
> +#ifdef R_68K_RELATIVE
> +#define _NOLIBC_ARCH_HAS_RELOC
> +#define _NOLIBC_ARCH_ELF32
> +#endif
> +
> #include "compiler.h"
> #include "crt.h"
> +#include "reloc.h"
>
> #define _NOLIBC_SYSCALL_CLOBBERLIST "memory"
>
> @@ -129,12 +137,29 @@
> })
>
> #ifndef NOLIBC_NO_RUNTIME
> +
> +#ifdef NOLIBC_WANT_RELOC
> +static __inline__ int __relocate_rela(unsigned long base, _nolibc_elf_rela *entry)
> +{
> + switch (_nolibc_elf_r_type(entry->r_info)) {
> + case R_68K_RELATIVE:

It seems there is only ever a single relocation used per architecture.
So wouldn't it be possible to do something like this:

#define _NOLIBC_ARCH_HAS_RELOC
#define _NOLIBC_ARCH_RELA_RELOC R_68K_RELATIVE

This keeps the per-architecture code simpler and removes the ugly
'ifdef R_68K_RELATIVE'. The code will break if somebody actually tries
to build a static pie using old uapi headers, but then this has never
worked before anyways.

> + __relocate_rela_relative(base, entry);
> + break;
> + default:
> + return -1;
> + }
> +
> + return 0;
> +}
> +#endif /* NOLIBC_WANT_RELOC */
> +
> void _start(void);
> void __attribute__((weak, noreturn)) __nolibc_entrypoint __no_stack_protector _start(void)
> {
> __asm__ volatile (
> "movel %sp, %sp@-\n"
> - "jsr _start_c\n"
> + "lea _start_c(%pc), %a0\n"
> + "jsr (%a0)\n"
> );
> __nolibc_entrypoint_epilogue();
> }
> --
> 2.51.0
>