Why do I need a bare-metal toolchain when I want to compile a binary without the underlying OS? What restrictions do the compilers have, except from missing functions/features because there is no OS/stdlibrary. Can any clang/gcc be used as a bare-metal compiler? My target is the raspberry 2.
2 Answers
First of all, you need a compiler that can generate code for the target processor. With clang, it is usually a matter of supplying the proper command line parameters to select the target processor, floating point support (or lack thereof), etc. For gcc, you need a version built to support the specific target.
Second, think of a simple program like this:
int main()
{
}
You can compile this with your cross compiler and get an object file for the target. Now the problem is how to get main() executed in your bare-metal environment. At minimum you'll need a bit of code, usually assembly, that runs when the processor is reset, sets up the stack pointer, and calls main(). With no other library support, everything else is up to you. You'll have to write code to do I/O etc. that is specific to your hardware.
I'm working on a tool chain that targets Linux but is also has support for bare metal development. Currently, the tool chain targets ARM, Mips, PowerPC, and x86 targets for C/C++ development. Linux targets are fully supported and ARM bare-metal target support is coming along. "bare-metal" in this context ranges everywhere from a simple main() as I described above to support for many POSIX functions such as pthread_create(), mutex support, etc. It can run in MMU and non-MMU environments also. You might want to look at the code at http://ellcc.org to see how bare-metal is handled. You can also download pre-built binaries for Windows and Linux if you want to give it a try.

- 19,673
- 4
- 43
- 72
-
Newlib is a well known possibility: https://stackoverflow.com/questions/2681304/compile-for-freestanding-environment-with-gcc | https://electronics.stackexchange.com/questions/223929/c-standard-libraries-on-bare-metal/223931#223931 – Ciro Santilli OurBigBook.com Jun 21 '18 at 06:42
Replace bare-metal with freestanding, no such thing as a bare-metal compiler.
You don't need a freestanding compiler for bare-metal work. But then you need to use exactly the right combination of options to make a normal (cross-)compiler work as freestanding compiler. With gcc/clang that is certainly possible. Not even difficult if you know what you are doing.
On the other hand most people don't know what they are doing when they start bare-metal work and also people always make mistakes and then parts of the OS the compiler is for creep into your bare-metal work and screw things up. A freestanding compiler can't do that since it has no OS part it relies on. So rather than getting odd behaviour in your kernel or crashes you get compiler errors.
Overall building a freestanding (cross-)compiler for bare-metal work has 3 things speaking for it:
- Keeps you from making mistakes that will keep you up all night trying to understand what's wrong.
- In the likely event that you are building for a different arch then you develope on it gives you a cross-compiler with minimum requirements (no target libc needed for example).
- Gives a common setup for everyone, the same standardized headers, ... so the same problems have the same solutions for everyone.

- 11,875
- 2
- 24
- 42
-
No, that is contradictory to the answer. Use a true freestanding compiler. – Goswin von Brederlow Jun 21 '18 at 10:15
-
It would be good to give specific examples where `gcc -nostdinc -nostdlib -ffreestanding` fails, and how to build the compiler you mention in detail. Related: https://stackoverflow.com/questions/2681304/how-to-compile-for-a-freestanding-environment-with-gcc/2697100#2697100 – Ciro Santilli OurBigBook.com Jun 21 '18 at 10:32
-
Say your gcc is for ARM with hard floats and you cross compile for ARM without FPU then the libgcc.a will contain some code that uses hard floats and that gets included when you use those functions. What you want there are the functions using no FPU or soft floats instead. – Goswin von Brederlow Jun 21 '18 at 10:47
-
Sure, you have to aim at the right ISA version. I'm wondering is there is anything else besides that. – Ciro Santilli OurBigBook.com Jun 21 '18 at 12:16