How to compile ARM32 only binary (no thumb)
Asked Answered
M

1

6

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.

Merganser answered 18/9, 2017 at 10:21 Comment(13)
What is the goal of this?Christiniachristis
I'm doing research on binary instrumentation and it would be nice to have an executable with such properties for testing purposes.Merganser
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).Darken
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.Darken
@Darken 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.Merganser
_start, etc are assembly language.Darken
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.Trousers
Do you have an -march or -mcpu on your command line?Darken
@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.Darken
-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 armDarken
@Darken 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.Merganser
@Darken 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.Trousers
Possible duplicate of How can I select a static library to be linked while ARM cross compiling?Trousers
I
8

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).

Indies answered 18/9, 2017 at 16:26 Comment(1)
Yep, that's exactly it. I got to this conclusion like 30 seconds before you answered. Thanks a low anyway.Merganser

© 2022 - 2024 — McMap. All rights reserved.