STM32 Bare Metal

Step by step following Zhu

Mainly using GNU compiler software suite. Covers: ARM, AVR, x86-64, MIPS, Xtensa. The tools included are

  • compiler: .c -> .o or .asm
  • assembler: .asm -> .o
  • linker: .o+.a to .elf
  • objcopy: .elf to .bin
    .c: c files, .s: assembly, .o object files, .a: static libraries, .ld: linker script, .elf: execution+linking file,

Steps of turning .c code into a executable binary.

Start with

#include <stdio.h>
#include "lib.h"
 
#define PI 3.1415
#define AREA(r) (r*r*PI)
 
int global = 12;
int adder(int a, int b) {
	return a+b;
}
 
 
// this is a silly comment
 
int main(void) {
	int r = 1;
	global = adder(r,r);
	float area = AREA(r);
	
	printf("global is: %d\n", global);
	printf("area is: %f\n", area);
}

where the file lib.h is given as

// this is a silly include file
int age = 12;

🟡Running pre-processor
Now using gcc with -E flag will run the only.

gcc -E hello.c -o hello.i

will remove comments, replace the macro definitions, and copy and paste the include files.

🟡Compile To `.asm`
Turns pre-processed file to .s.

gcc -S hello.i -o hello.s

Note that if the include files are in a different folder called inc, to make the pre-processed file we need gcc -E hello.c -I inc -o hello.i. But after .i created, we don’t need -I flags to assemble (.h files are already copied).

🟡Assemble to object files
Object files are relocatable elf files.

gcc -c hello.s -o hello.o

Compilation and linking pipeline from C and assembly sources to ELF and binary files

🟡Inspecting object files

hexdump -C hello.o | bat
 
## or hexyl for better colors
hexyl hello.o | bat

bat command is to have better view.x