0

I have some project on C. The question is how can I check the correct work of this program on big endian machine, having only little endian machine. Is there some tricks to compile be program on le machine?

I had try to use debian mips on qemu to emulate be machine, but I can't install programs to run my project.

Thanks.

  • 1
    Don't rely on implementation defined (or even undefined) behaviour. – too honest for this site Mar 11 '16 at 16:44
  • @Olaf: Indeed, and don't write buggy code while you're about it. The OP is asking for advice on how not to _inadvertently rely_ on implementation-defined behavior by exposing his code to alternate implementation choices. – doynax Mar 12 '16 at 18:07

2 Answers2

1

Try to write your operations in a way which are endian neutral. Endianness issues don't strike you everywhere, they mostly strike when:

  • you want to say write integers to files, then use approach like: this, which serializes integers using bit shifting.
  • you want to access multi byte values through char pointer, then depending on endianness you may read different values through this char pointer. To avoid this issue, you can try to avoid such code in the first place if possible.

Here is article about endian independent code.

Community
  • 1
  • 1
Giorgi Moniava
  • 27,046
  • 9
  • 53
  • 90
  • This isn't possible. There are a lot of operations like "+", "-" and "*", and they definitely must be. I try to check on endians in my code, but I can't say that it 100% works right, thats why I want to check it. – Kurokawa Masato Mar 11 '16 at 17:00
  • @KurokawaMasato Those operations don't have to do with endianess in general. You must be more specific then which parts of your code you think can be affected by endianness. Or try to read more about endianness to decide yourself to which part of your code endianness issues may apply. Check also the link I gave you. – Giorgi Moniava Mar 11 '16 at 17:01
  • Oh, sorry, didn't say, there are a lot of casts, that also must be, from uint8 to uint64 and uint32, so the endian is important. – Kurokawa Masato Mar 11 '16 at 17:03
  • @KurokawaMasato Ah sorry I can't help you further then. Let's hope someone else gives you other information. Or just try get a PC where you can test your code, but best is still to read for example the article I gave to get a good understanding about endianess. Those casts can be dangerous sometimes btw. strict aliasing etc. But I can't say exactly about your case. – Giorgi Moniava Mar 11 '16 at 17:06
  • I know how to check on endians and write endian independent code, all I need is to check, that the program works correctly on be machine. – Kurokawa Masato Mar 11 '16 at 17:06
  • @KurokawaMasato Then you could just try get a machine with other endianness I am not sure of other way, but it maybe possible – Giorgi Moniava Mar 11 '16 at 17:07
  • 1
    You could try QEmu for an ARM architecture + Linux - That would even allow you to check both BE + LE code. – tofro Mar 11 '16 at 17:08
  • 1
    Even casts among integer types shouldn't be a problem unless you're casting *pointers*. E.g., `(int16_t)int32var` and such is fine, but `*(int16_t *)int32p` is not. – Lee Daniel Crocker Mar 11 '16 at 17:11
  • @KurokawaMasato Regular casts is endian-neutral, basically everything in "registers" are. Endianness only matters for pointers, storage, and IO. Like trying to read a `char` form an `int*` or trying to read `int` from `char *`. – user3528438 Mar 11 '16 at 17:11
  • Yeah sure regular casts on integer types are okay, as far as endianess is concerned – Giorgi Moniava Mar 11 '16 at 17:17
  • Then you'll probably have issues. Realistically, though, 99+% of computers are little-endian these days, so it might be acceptable to just say your program only works on LE machines and leave it at that. – Lee Daniel Crocker Mar 11 '16 at 17:26
  • @LeeDanielCrocker I feel link like this https://www.securecoding.cert.org/confluence/display/c/EXP36-C.+Do+not+cast+pointers+into+more+strictly+aligned+pointer+types, should also be useful to OP? – Giorgi Moniava Mar 11 '16 at 17:30
1

This task was solved.

I installed qemu, downloaded mips for qemu, and installed mips-linux-gnu-gcc. It has gcc parametres like "-EB", "-LE", that allows you to compile program with little or bid endian. So all I was needed is to compile my project with mips-linux-gnu-gcc -EB and run execution file on be machine. As I expected, some error occurs.

Thanks everyone for help.