4

Is there a GCC configuration which will produce an executable only containing ARM32 code? I know the -marm switch tells the compiler not to produce Thumb code, but it applies only to the user code of the program, while initialization routines (e.g. _start, frame_dummy, ...) still contain Thumb instructions. I am using the Linaro cross compiler tool-chain (arm-linux-gnueabihf-) on a Linux x86-64 system.

EDIT :

While recompiling the tool-chain I found the (probable) solution myself. The initialization routines encoded as Thumb are part of glibc and can be found in the object files crt1.o, crti.o and crtbegin.o. I haven't tried recompiling it, but there may be a configuration value which forces the whole libc to be encoded as ARM32.

Matteo Piano
  • 392
  • 4
  • 12
  • What is the goal of this? – fuz Sep 18 '17 at 10:33
  • 1
    I'm doing research on binary instrumentation and it would be nice to have an executable with such properties for testing purposes. – Matteo Piano Sep 18 '17 at 11:09
  • if you dont say -mthumb are you seeing thumb code? if so I would be very interested to know what your gcc command line is (as well as version). – old_timer Sep 18 '17 at 11:58
  • if you are seeing thumb code in the bootstrap then you have to re-write it, those are likely assembly language routines not compiled routines. – old_timer Sep 18 '17 at 11:59
  • 1
    @old_timer I'm using the Linaro `arm-linux-gnueabihf-gcc` version 5.4.0 (installed from the ubuntu xenial repository). It produces thumb code by default, so the use of the `-mthumb` option is not relevant. – Matteo Piano Sep 18 '17 at 12:11
  • _start, etc are assembly language. – old_timer Sep 18 '17 at 13:50
  • For a modern ARM CPU (called thumb2) where the 'thumb' code has a mix of 16/32bit opcode’s, it is the most efficient. There is no benefit to use older pure ARM. 99.9% of all code can run in thumb2. I think that ubuntu will only target ARMv6 or better. Ie, try to find a compiler that targets ARMv5. – artless noise Sep 18 '17 at 13:51
  • Do you have an -march or -mcpu on your command line? – old_timer Sep 18 '17 at 13:51
  • @artlessnoise I dont disagree, thumb is fine, the ARMv4/5 version is the most portable instruction set arm has across its platforms. Gcc continues to default to build arm code, unless when configured it was told otherwise. So linaro apparently specified otherwise. – old_timer Sep 18 '17 at 13:55
  • -marm works with the apt-gotten toolchain, is that the question? Your C libraries are likely built with thumb as dumpspecs to some extent leans toward that. Are you building bare metal or for an operating system? You can simply just build your own toolchain with defaults or without forcing thumb and then all but the boostrap will be arm – old_timer Sep 18 '17 at 14:01
  • @old_timer The problem does not occur for bare metal, but I want to build for an OS. If I understand correctly, you suggest that the code sections I want to address are strictly embedded in the compiler tool-chain and, consequently, the only way to change them is recompiling it with different configuration values. I'll try that and see what happens. – Matteo Piano Sep 18 '17 at 14:15
  • @old_timer But Linaro/Ubuntu don't target those platforms. So it doesn't matter what opinion we have of the ISA. Using a compiler for a platform that has Thumb2, it is unrealistic to expect it was configured for ARM. To the OP, this is called 'multi-lib'. There are permutation for the ARM (ISA and FPU). Build an ARM only compiler (start with crosstool-ng Linaro config and update) or use '-nostdlib' and provide your own library functions. – artless noise Sep 18 '17 at 16:32
  • Possible duplicate of [How can I select a static library to be linked while ARM cross compiling?](https://stackoverflow.com/questions/24616226/how-can-i-select-a-static-library-to-be-linked-while-arm-cross-compiling) – artless noise Sep 18 '17 at 16:37

1 Answers1

6

Is there a GCC configuration which will produce an executable only containing ARM32 code? I know the -marm switch ...

Your main problem is that that code (e.g. _start) is not produced by the compiler but it is already present pre-compiled (as thumb code).

If you want to have these functions to be non-thumb code you'll have to "replace" the existing files (thumb) by your own ones (non-thumb).

(You don't have to overwrite the existing files but you can instruct the linker to search for these files in a different directory.)

If you don't find pre-built non-thumb files you'll have to create them yourself (what may be a lot of work).

Martin Rosenau
  • 17,897
  • 3
  • 19
  • 38