# Linux Kernel Module Cheat

##### 30.1.3.2. ARM big endian mode

ARM can switch between big and little endian mode on the fly!

However, everyone only uses little endian, so the big endian ecosystem is not as supported.

TODO is there any advantage of using big endian?

Here Peter mentions that QEMU does "support" big endian in theory, but that there are no machines for it not sure what that implies: https://stackoverflow.com/questions/41571643/emulatin-big-endian-arm-system-with-qemu

We can try it out quickly in user mode with:

    touch userland/arch/aarch64/freestanding/linux/hello.S
    ./build-userland --arch aarch64 --ccflags=-mbig-endian userland/arch/aarch64/freestanding/linux/hello.S
    ./run --arch aarch64 --userland userland/arch/aarch64/freestanding/linux/hello.S

and it fails with:

    Invalid ELF image for this architecture

From this we can guess that the big endian metadata is actually stored in the ELF file, and confirm that with:

    ./run-toolchain \
      --arch aarch64 \
      readelf \
      -- \
      --file-header "$(./getvar --arch aarch64 userland_build_dir)/arch/aarch64/freestanding/linux/hello.out" \
    ;

which contains:

    Data:                              2's complement, big endian

instead of the default:

    Data:                              2's complement, little endian

TODO does the Linux kernel support running big endian executables? I tried after building the big endian executable:

    ./build-buildroot --arch aarch64
    ./run --arch aarch64 --eval-after ./arch/aarch64/freestanding/linux/hello.out

but that failed with:

    /lkmc/arch/aarch64/freestanding/linux/hello.out: line 1: ELF@x@0@8@: not found
    /lkmc/arch/aarch64/freestanding/linux/hello.out: line 2: @@: not found
    /lkmc/arch/aarch64/freestanding/linux/hello.out: line 3: syntax error: unexpected ")"

TODO:

- can you compile the Linux kernel itself as big endian? Looks like yes since there is a `config CPU_BIG_ENDIAN` See also: https://unix.stackexchange.com/questions/378829/getting-big-endian-linux-build-to-boot-on-arm-with-u-boot

- how can be is the endianess be checked and modified in the CPU?