To successfully compile and run bare-metal code, you will need the following files:

  • Linker Script: Defines memory layout and sections for the target hardware.
  • Startup Code: Initializes hardware and sets up the stack pointer.
  • Main Application Code: Contains the main logic of your application.
  • Makefile(optional): Automates the build process, linking the above components.

To make your life a bit easier, you should use newlib as your C standard library. As a clear example of why it’s so useful, newlib provides implementations for functions like memcpy, memset, and printf, which are essential for many applications. Without newlib, you would have to implement these functions yourself, which can be time-consuming and error-prone. With newlib, you only need to write a few system call stubs to interface with your hardware, and you can leverage the existing implementations of these functions.

For example:

  • To use printf, you implement the _write syscall to direct output to your desired interface (like UART).
  • To use malloc, you implement the _sbrk syscall to manage heap memory.

Key Concepts

  • Object files are compiled source code that has not yet been linked to create an executable.