Skip to contentSkip to conversation

Add support for flat m68k binaries

PATCH In review ~wojtekka 3 participants 5 messages Jul 16, 2025 – Aug 6, 2025

lore
Share & tools
lore

~wojtekka

author
#0 lore raw

cover Add support for flat m68k binaries

Flat m68k binaries apparently don't work out of the box and those two tiny patches fixed that for me. On top of that they didn't break support for ARM flat binaries (tested with test_arm_bflt.py) nor m68k ELF binaries (tested static busybox from Debian). Unfortunately I wasn't able to find any root filesystem for m68k uClinux so I didn't prepare any test similar to test_arm_bflt.py.

Wojtek Kaniewski (2):
Don't reverse bFLT endianess when not needed
Fill out m68k PIC register

 linux-user/elfload.c  | 1 +
 linux-user/flatload.c | 2 ++
 2 files changed, 3 insertions(+)
Signature · 1 line
2.45.3
author
#1 lore raw

PATCH 1/2 Don't reverse bFLT endianess when not needed

From: Wojtek Kaniewski <[email protected]>

bFLT format is big-endian. get_user_ual() returns host value so for little-endian target and little-endian host it's necessary to reverse words using ntohl(). For big-endian targets we end up with incorrect endianess:

    $ qemu-m68k-static ./test
    BINFMT_FLAT: reloc outside program 0x801f0000 (0 - 0x41f0/0x1e40)
    Aborted (core dumped)

For comparison the output of flthdr follows:

    $ m68k-elf-flthdr -P ./test
    ./test
        Magic:        bFLT
        Rev:          4
        Build Date:   Tue Jul 15 23:02:00 2025
        Entry:        0x44
        Data Start:   0x1e80
        Data End:     0x205c
        BSS End:      0x40a0
        Stack Size:   0x1000
        Reloc Start:  0x205c
        Reloc Count:  0x17
        Flags:        0x2 ( Has-PIC-GOT )
        Relocs:
        #	reloc      (  address )	data
        0	0x00001f80 (0x00001f80)	3c200000
                  ^^^^^^^^
        1	0x00001f84 (0x00001f84)	3c300000
        2	0x00001f88 (0x00001f88)	3c200000
        ...
---
 linux-user/flatload.c | 2 ++
 1 file changed, 2 insertions(+)
linux-user/flatload.c +2 -0
index 4beb3ed1b9..afaff4ac44 100644
@@ -413,7 +413,9 @@ static int load_flat_file(struct linux_binprm * bprm,
                relocated first).  */
             if (get_user_ual(relval, reloc + i * sizeof(abi_ulong)))
                 return -EFAULT;
+#if !TARGET_BIG_ENDIAN
             relval = ntohl(relval);
+#endif
             if (flat_set_persistent(relval, &persistent))
                 continue;
             addr = flat_get_relocate_addr(relval);
Signature · 1 line
2.45.3
#2 lore raw

Re: [PATCH qemu 1/2] Don't reverse bFLT endianess when not needed

Le 16/07/2025 à 21:24, ~wojtekka a écrit :

Quoted 44 lines

From: Wojtek Kaniewski <[email protected]>

bFLT format is big-endian. get_user_ual() returns host value so for little-endian target and little-endian host it's necessary to reverse words using ntohl(). For big-endian targets we end up with incorrect endianess:

     $ qemu-m68k-static ./test
     BINFMT_FLAT: reloc outside program 0x801f0000 (0 - 0x41f0/0x1e40)
     Aborted (core dumped)

For comparison the output of flthdr follows:

     $ m68k-elf-flthdr -P ./test
     ./test
         Magic:        bFLT
         Rev:          4
         Build Date:   Tue Jul 15 23:02:00 2025
         Entry:        0x44
         Data Start:   0x1e80
         Data End:     0x205c
         BSS End:      0x40a0
         Stack Size:   0x1000
         Reloc Start:  0x205c
         Reloc Count:  0x17
         Flags:        0x2 ( Has-PIC-GOT )
         Relocs:
         #	reloc      (  address )	data
         0	0x00001f80 (0x00001f80)	3c200000
                   ^^^^^^^^
         1	0x00001f84 (0x00001f84)	3c300000
         2	0x00001f88 (0x00001f88)	3c200000
         ...
---
  linux-user/flatload.c | 2 ++
  1 file changed, 2 insertions(+)
linux-user/flatload.c +0 -0
index 4beb3ed1b9..afaff4ac44 100644

linux-user/flatload.c static int load_flat_file(struct linux_binprm * bprm, · line 421

Quoted 9 lines
linux-user/flatload.c +2 -0
@@ -413,7 +413,9 @@ static int load_flat_file(struct linux_binprm * bprm,
                 relocated first).  */
              if (get_user_ual(relval, reloc + i * sizeof(abi_ulong)))
                  return -EFAULT;
+#if !TARGET_BIG_ENDIAN
              relval = ntohl(relval);
+#endif
              if (flat_set_persistent(relval, &persistent))
                  continue;
              addr = flat_get_relocate_addr(relval);

I think the ntohl() should be removed totally and flat_set_persistent() should use put_user_ual().

Could you test this?

Thanks,
Laurent

#3 lore raw

Re: [PATCH qemu 1/2] Don't reverse bFLT endianess when not needed

Quoted 64 lines · Laurent Vivier

Le 16/07/2025 à 21:24, ~wojtekka a écrit :

Quoted 50 lines

From: Wojtek Kaniewski <[email protected]>

bFLT format is big-endian. get_user_ual() returns host value so for little-endian target and little-endian host it's necessary to reverse
words using ntohl(). For big-endian targets we end up with incorrect
endianess:

     $ qemu-m68k-static ./test
     BINFMT_FLAT: reloc outside program 0x801f0000 (0 -
0x41f0/0x1e40)
     Aborted (core dumped)

For comparison the output of flthdr follows:

     $ m68k-elf-flthdr -P ./test
     ./test
         Magic:        bFLT
         Rev:          4
         Build Date:   Tue Jul 15 23:02:00 2025
         Entry:        0x44
         Data Start:   0x1e80
         Data End:     0x205c
         BSS End:      0x40a0
         Stack Size:   0x1000
         Reloc Start:  0x205c
         Reloc Count:  0x17
         Flags:        0x2 ( Has-PIC-GOT )
         Relocs:
         #	reloc      (  address )	data
         0	0x00001f80 (0x00001f80)	3c200000
                   ^^^^^^^^
         1	0x00001f84 (0x00001f84)	3c300000
         2	0x00001f88 (0x00001f88)	3c200000
         ...

---
  linux-user/flatload.c | 2 ++

  1 file changed, 2 insertions(+)
linux-user/flatload.c +0 -0
index 4beb3ed1b9..afaff4ac44 100644
@@ -413,7 +413,9 @@ static int load_flat_file(struct linux_binprm *
 bprm,

                 relocated first).  */
              if (get_user_ual(relval, reloc + i *

Quoted 8 lines

sizeof(abi_ulong)))
                  return -EFAULT;
+#if !TARGET_BIG_ENDIAN
              relval = ntohl(relval);
+#endif
              if (flat_set_persistent(relval, &persistent)) continue;
              addr = flat_get_relocate_addr(relval);

I think the ntohl() should be removed totally and
flat_set_persistent()
should use put_user_ual().

Quoted 1 line

Could you test this?

flat_set_persistent() is just a no-op macro copied from Linux kernel implementation and has been removed from the upstream a couple of years ago [1]. The actual address is relocated by flat_get_relocate_addr() a line below so it needs to be in host-endian. Dropping ntohl() would break it for little-endian targets because bFLT by definition is big- endian.

Is there a macro similar to get_user_ual() but without any endianess conversion? The closest thing I found was ldl_p(), but I assume it's not the best idea due to lack of locking.

(And sorry for such a late response. I haven't used mailing lists for years and it took me way too long to get my old e-mail account to work properly.)

Regards,
Wojtek

[1]
https://github.com/torvalds/linux/commit/2f3196d49b1e10f1d4bc64cce00dc95fde2b0ce1

author
#4 lore raw

PATCH 2/2 Fill out m68k PIC register

In reply to ~wojtekka

From: Wojtek Kaniewski <[email protected]>

D5 is expected to be set to data address on m68k without MMU. See
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/arch/m68k/include/asm/flat.h
for reference.

---
 linux-user/elfload.c | 1 +
 1 file changed, 1 insertion(+)
linux-user/elfload.c +1 -0
index ea214105ff..b151ebcba2 100644
@@ -1756,6 +1756,7 @@ static uint32_t get_elf_hwcap(void)
 static inline void init_thread(struct target_pt_regs *regs,
                                struct image_info *infop)
 {
+    regs->d5 = infop->start_data;   /* For uClinux PIC binaries. */
     regs->usp = infop->start_stack;
     regs->sr = 0;
     regs->pc = infop->entry;
Signature · 1 line
2.45.3