From 7211bd49452e67e0b53ea06ca474f4e54af991ae Mon Sep 17 00:00:00 2001
From: xengineering <me@xengineering.eu>
Date: Sat, 25 Sep 2021 15:47:25 +0200
Subject: Implement Linker Script and Startup File

---
 .gitignore         |  3 +++
 Makefile           | 12 ++++++++++++
 ld/stm32.ld        | 32 ++++++++++++++++++++++++++++++++
 src/startup.c      | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++
 src/test_program.c |  8 ++++++++
 5 files changed, 105 insertions(+)
 create mode 100644 .gitignore
 create mode 100644 Makefile
 create mode 100644 ld/stm32.ld
 create mode 100644 src/startup.c
 create mode 100644 src/test_program.c

diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..8765efc
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,3 @@
+docs
+baremetal
+build
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..4ef61dd
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,12 @@
+
+.PHONY: all clean
+
+all:
+	mkdir build
+	arm-none-eabi-gcc -O0 -c -g -mcpu=cortex-m3 -mthumb -o build/test_program.o src/test_program.c
+	arm-none-eabi-gcc -O0 -c -g -mcpu=cortex-m3 -mthumb -o build/startup.o src/startup.c
+	arm-none-eabi-ld -Tld/stm32.ld -o build/test_program.elf build/startup.o build/test_program.o
+	arm-none-eabi-objcopy -O binary build/test_program.elf build/test_program.bin
+
+clean:
+	rm -rf build
diff --git a/ld/stm32.ld b/ld/stm32.ld
new file mode 100644
index 0000000..9f74206
--- /dev/null
+++ b/ld/stm32.ld
@@ -0,0 +1,32 @@
+SECTIONS
+{
+    .  = 0x0;         /* From 0x00000000 */
+
+    .text : 
+    {
+        *(vectors)    /* Vector table */
+        *(.text)      /* Program code */
+    }
+    .rodata : 
+    {
+        *(.rodata)    /* Read only data */
+    }
+    _DATA_ROM_START = .;
+
+    .  = 0x20000000;  /* From 0x20000000 */      
+
+    _DATA_RAM_START = .;
+    .data : AT(_DATA_ROM_START) 
+    {
+        *(.data)      /* Data memory */
+    }
+    _DATA_RAM_END = .;
+
+    _BSS_START = .;   /* Indicates where BSS section starts in RAM */
+    .bss :
+    {
+        *(.bss)       /* Zero-filled run time allocate data memory */
+    }
+    _BSS_END = .;     /* Indicates where BSS section ends in RAM */
+}
+
diff --git a/src/startup.c b/src/startup.c
new file mode 100644
index 0000000..b71a805
--- /dev/null
+++ b/src/startup.c
@@ -0,0 +1,50 @@
+
+
+#define STACK_TOP 0x20005000
+
+
+extern unsigned int _BSS_START;
+extern unsigned int _BSS_END;
+extern unsigned int _DATA_ROM_START;
+extern unsigned int _DATA_RAM_START;
+extern unsigned int _DATA_RAM_END;
+
+void startup();
+void main();
+
+// Define the vector table
+unsigned int * myvectors[2] 
+__attribute__ ((section("vectors")))= {
+    (unsigned int *)    STACK_TOP,  // stack pointer
+    (unsigned int *)    startup     // code entry point
+};
+
+void startup()
+{
+    /* Set memory in bss segment to zeros */
+    unsigned int * bss_start_p = &_BSS_START; 
+    unsigned int * bss_end_p = &_BSS_END;
+
+    while(bss_start_p != bss_end_p)
+    {
+        *bss_start_p = 0;
+        bss_start_p++;
+    }
+
+    /* Copy memory in data segment from its position
+     * in the flash image to its position in the
+     * read/write memory */
+    unsigned int * data_rom_start_p = &_DATA_ROM_START;
+    unsigned int * data_ram_start_p = &_DATA_RAM_START;
+    unsigned int * data_ram_end_p = &_DATA_RAM_END;
+
+    while(data_ram_start_p != data_ram_end_p)
+    {
+        *data_ram_start_p = *data_rom_start_p;
+        data_ram_start_p++;
+        data_rom_start_p++;
+    }
+
+    /* Now we are ready to start the main function */
+    main();
+}
diff --git a/src/test_program.c b/src/test_program.c
new file mode 100644
index 0000000..80f569b
--- /dev/null
+++ b/src/test_program.c
@@ -0,0 +1,8 @@
+static const int a = 7;
+static int b = 8;
+static int sum;
+
+void main()
+{
+    sum = a + b;
+}
-- 
cgit v1.2.3-70-g09d2