Run a Big Endian program on a Little Endian operating system

Do you have a question about makefiles, a compiler or IDE you are using and need to know how to set it up for wxWidgets or why it doesn't compile but other IDE's do ? Post your questions here.
Post Reply
Virchanza
I live to help wx-kind
I live to help wx-kind
Posts: 172
Joined: Sun Jul 19, 2009 6:12 am

Run a Big Endian program on a Little Endian operating system

Post by Virchanza »

Today I posted the following on the Raspberry Pi forums located at: https://forums.raspberrypi.com/viewforum.php?f=29
. I wish to get opinions here also though.

I have a Raspberry Pi 2 Model B. The CPU is 32-Bit ARM Cortex A7, which can be configured to run both in Little Endian and Big Endian mode. All the raspberry pies come out of the factory configured for Little Endian. It's also possible with this CPU to switch the endianness just for the current process by executing the CPU instruction "setend be". I have tested this and it works.

I am the author of the network analysis tool, Dynamo, which I wrote in C++ using the wxWidgets cross-platform framework. I've got my program building and running well on Linux, MS-Windows, macOS. This week I've successfully gotten my program to build and run well on my Raspberry Pi 2 Model B in the Pixel desktop environment. I want to build my program for Big Endian to test it (and also just because it sounds cool). If there were a Big Endian image of the raspberry pi operating system that I could load onto my pi, then I'd do that. I don't see one anywhere though.

What I want to do now is build my program for 32-Bit ARM Big Endian and run it on my pi. I've tried building small programs with gcc for Big Endian at the command line, and when I check the executable file type it says "MSB executable" so that means it's Big Endian. (Little Endian is LSB). However when I try to run the MSB executable at the command line, Linux won't run it. It will only run LSB executables.

So here's what my next idea is:

I want to get the source code for wxWidgets, and build it to statically link with every library (including libgcc and libstdc++) to produce an MSB static library. Then I will build my Dynamo program to link statically with every library in order to produce an MSB executable file that is fully self-sufficient (i.e. if you run ldd on it then it won't list any dynamic shared libraries, or if you run "readelf | grep NEEDED" on it then it won't come back with anything).

So then I'll have an MSB executable for my Dynamo program which doesn't link with any dynamic shared libraries. But of course, I can't run an MSB executable on my Raspberry Pi.

So here's the last thing I do:

Step 1) Edit the first 5 bytes in the ELF header of my Dynamo binary to change it from MSB to LSB

If I were to then run my Dynamo binary, it would of course crash because all the memory locations are in Big Endian instead of Little Endian, and so it will segfault. And so that's why I move on to step 2:

Step 2) Use readelf on my binary to find the entry point: "readelf -h dynamo | grep Entry", and then at that address insert one instruction "setend be" so that the remainder of the code will be executed with the CPU in Big Endian mode

So then my program might run in Big Endian mode (even though the statically-linked executable file might be 100 megabytes instead of 5 megabytes).

Will this be possible?
Virchanza
I live to help wx-kind
I live to help wx-kind
Posts: 172
Joined: Sun Jul 19, 2009 6:12 am

Re: Run a Big Endian program on a Little Endian operating system

Post by Virchanza »

Just a test post to get around the forum errors.
User avatar
doublemax
Moderator
Moderator
Posts: 19103
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Run a Big Endian program on a Little Endian operating system

Post by doublemax »

Virchanza wrote: Thu Feb 10, 2022 11:10 pm Just a test post to get around the forum errors.
Are you talking about recent forum errors? If yes, please explain what kind of errors.

The login issues should hopefully be resolved since last week.
Use the source, Luke!
Virchanza
I live to help wx-kind
I live to help wx-kind
Posts: 172
Joined: Sun Jul 19, 2009 6:12 am

Re: Run a Big Endian program on a Little Endian operating system

Post by Virchanza »

doublemax wrote: Thu Feb 10, 2022 11:15 pm
Virchanza wrote: Thu Feb 10, 2022 11:10 pm Just a test post to get around the forum errors.
Are you talking about recent forum errors? If yes, please explain what kind of errors.

The login issues should hopefully be resolved since last week.
This is my first time logging in in about 3 weeks or so. From now on I'll just post as normal without a prior 'test' post.
Post Reply