5

I have a Raspberry Pi 3 Model B and I switched from the 32-bit Raspbian to the 64-bit Gentoo Linux as I wanted to test some 64-bit applications. Now, I want to test an application which would access data bytes in big-endian order. I know I can do this manually by swapping the bytes in the word. But my main question is how to change the byte order to big-endian permanently on Raspberry Pi and may be later again switch back to little-endian after all the tests are done. Any leads would be greatly appreciated.

  • The data endianness at EL0 is controlled by `SCTLR_EL1.E0E`, which means the kernel has to set that bit for you. If supported at all, endianness usually applies to the entire process, and should be detected at process spawn time via the `e_ident[EI_DATA]` bit in the ELF header. If you want to hop back and forth between endiannesses at runtime, the kernel needs to give you a syscall for that. [32bit ARM had the `setend` assembly instruction](https://stackoverflow.com/q/28725421), however that was removed in AArch64, and I think it's fair to call it an architectural mistake in the first place. – Siguza Apr 30 '19 at 16:48
  • @Siguza Thanks for responding. I am aware of the 32-bit `setend` instruction and I wish we had a similar instruction for AArch64. How exactly can I manipulate `SCTLR_EL1.E0E` to change the data endianness on AArch64? – Saurav Kumar Sahu May 01 '19 at 00:55
  • Here is a Raspberry Pi 3 SDcard image with big endian Gentoo for you: https://github.com/zeldin/linux-1/releases – Marcus Apr 09 '21 at 09:54

1 Answers1

3

I have been reading quite a lot since seeing your question for the first time, and my answer is that you cannot execute a big-endian Linux application on a little-endian Linux kernel/distribution.
I even tried on my Armbian-based Orangepi-PC2 aarch64 system, which simply refused loading a sample program cross-compiled using ARM'latest big-endian toolchain for aarch64.

Now, if you really don't want to convert your data, and if performance is not an issue - it probably is not because you want to make your processing on a Raspberry-pi instead of on a PC - I would suggest you to:

  • install qemu (on a Linux PC or a virtual machine) for a big-endian 64 bits platform, say ppc64,
  • install a cross-compiler for ppc64,
  • compile your program and process your data.

On an Ubuntu 18.04 system, you would execute the following command:

sudo apt-get install gcc-8-powerpc64-linux-gnu qemu

Here is small example:

Create a binary file containing the 64 bits number 0x1122334455667788:

printf "\x11\x22\x33\x44\x55\x66\x77\x88">  example.dat
hexdump -C example.dat 
00000000  11 22 33 44 55 66 77 88                           |."3DUfw.|
00000008

Create this minimal, demo-only program:

// example.c

#include <stdio.h>
#include <stdlib.h>

int main()
{
    // demo code only - don't do this in real life...
    unsigned int value = 0;
    FILE* fp = fopen("example.dat", "rb");
    fread(&value, sizeof(value), 1, fp);
    printf("%d\n", value);
}

Compile it:

powerpc64-linux-gnu-gcc-8 -static -o example example.c

Execute it:

qemu-ppc64 ./example
0x1122334455667788

The same program compiled for x86_64 would display 0x8877665544332211, which is fine:

gcc -o example example.c
./example 
0x8877665544332211

qemu-ppc64 is emulating the ppc64, but system calls are being performed by your x86_64 linux kernel. I hope this help solving your original problem.

Frant
  • 5,382
  • 1
  • 16
  • 22
  • I really appreciate you taking the time to post an answer. I have already tried the emulation using QEMU that you have mentioned above. I really want to test some hardware instructions available on ARMv8-a with big-endian data access. Do you know of any such QEMU image with AArch64 Big-Endian byte order? – Saurav Kumar Sahu May 02 '19 at 07:30
  • This was the first trail I followed, to no avail. It seems they were no such machines available two years ago - see Peter Maydell's [post](https://stackoverflow.com/questions/41571643/emulatin-big-endian-arm-system-with-qemu). I would suggest you to ask the question [here](https://stackoverflow.com/questions/tagged/qemu). It seems that big endian aarch64 platforms are very rare. – Frant May 03 '19 at 02:27