4

I am wondering if it is possible to change the endianness of a running application mid-execution on the ARM platform. As some of you know, ARM is bi-endian (supporting both big and little) and I am wondering if on Android (or linux in general) if it is possible to either set a single application to use a different endianness at execution or if it is possible to change the processor endianness mid-execution.

To head off some of the possible responses to this. Changing endianness (if possible on the application level) is almost assuredly a bad idea but I am building tools for the ARM platform and I am wondering if an application that I am using a set tools on could possibly do this. Also even if doing so could cause catastrophic consequences to other applications/OS I would like to know if it can be done by an application.

So far from my view of ARM it appears that only the OS can set the mode of the processor in this regard. However I have yet to find a definitive answer on this question. Thanks in Advance!

NothingMore
  • 1,211
  • 9
  • 19
  • 1
    It is technically possible on **SOME** ARM CPUs. `setend be` and `setend le` are in [this duplicate question](http://stackoverflow.com/questions/4286671/endianness-conversion-in-arm). [`setend` will manipulate the **CPSR E-bit**](http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0489f/Cjacabbf.html) which is saved/restored on context switches, so it is safe for an application under Linux to do this. – artless noise Feb 25 '15 at 18:04
  • While the other question does provide some interesting context as an example, **the actual question asked there is quite different** - that one asks how to convert data, and changing the CPU mode is merely a suggested method. This one is more about the likelihood of running into someone toggling the CPU mode - for which that does provide an example. – Chris Stratton Feb 25 '15 at 19:17
  • @ChrisStratton The accepted answer seems to work for either. If you parse every word, it is different. To me, it has all the same information so it is the same. I thought the purpose of 'duplicate' was to link the information when questions are asked in slightly different ways. – artless noise Feb 25 '15 at 22:27
  • The questions being asked are extremely different. At least one moderator has opinioned that having the same answers is a clue but not sufficient to make distinct questions duplicates: http://meta.stackoverflow.com/questions/278961/if-two-questions-are-identical-but-have-entirely-different-answers-are-they-dup – Chris Stratton Feb 25 '15 at 22:32
  • due to generity of the answer (use linux SETEND API function) I'd proposed to change the question accordingly. – Sergei Krivonos Oct 23 '15 at 20:04

1 Answers1

7

It is (on ARMv7 and older hardware at least) certainly possible, but your sentiment is entirely correct - anyone else, please, please, for the sake of sanity, forward-compatibility and angry kernel developers, don't do this in Linux/Android - use REV, REV16, REVSH or VREV on data as appropriate.

The SETEND instruction, introduced in ARMv6, allows switching the endianness of the current execution state at any privilege level, however from ARMv8 it is deprecated, disabled by default, and likely to disappear entirely in future. Supporting mixed-endianness in hardware is optional in ARMv8.

Despite being a terrible idea, it's apparently commonplace enough in Android apps currently in the wild (among possible other uses, it's supposedly the fastest way to implement strcmp() on ARM11, and maybe also Cortex-A8) that SETEND emulation for 32-bit tasks has recently had to be added to the arm64 kernel, so chances are your tools should at least be aware of it, too.

Notlikethat
  • 20,095
  • 3
  • 40
  • 77
  • Thanks for the response! This is basically fully answers my question. Kinda sucks application developers do this sort of thing. – NothingMore Feb 25 '15 at 19:40
  • The `SETEND` will probably execute faster as it can translate multiple instructions at a time; as opposed to `ldr rx, [mem]` and `rev rX`. ARMv8 should probably have the applications recompiled and I would hope it is a macro. But a good point none the less. – artless noise Feb 25 '15 at 22:30