From ecf4266cc71f52045460c4021fdd7b971f119562 Mon Sep 17 00:00:00 2001
From: Aaron Haun <aaron@haun.guru>
Date: Fri, 28 Apr 2023 22:19:32 +0000
Subject: [PATCH] armcm: Add exception index and dummy heap for newlib 4.3+

Signed-off-by: Aaron B. Haun <aaron@haun.guru>
---
 src/generic/armcm_link.lds.S | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)

diff --git a/src/generic/armcm_link.lds.S b/src/generic/armcm_link.lds.S
index 2f789f1301e..14f50b026fc 100644
--- a/src/generic/armcm_link.lds.S
+++ b/src/generic/armcm_link.lds.S
@@ -26,6 +26,12 @@ SECTIONS
         *(.rodata .rodata*)
     } > rom
 
+    .ARM.exidx : {
+        __exidx_start = .;
+        *(.ARM.exidx* .gnu.linkonce.armexidx.*)
+        __exidx_end = .;
+    } > rom
+
     . = ALIGN(4);
     _data_flash = .;
 
@@ -64,6 +70,23 @@ SECTIONS
         _stack_end = .;
     } > ram
 
+    // Versions of newlib with libnosys require a heap end marker even
+    // in C freestanding mode. This creates a one byte heap.
+    // !! WARNING: The version of sbrk in libnosys does NOT check
+    // for the heap overrunning its allocation. Trying to expand the heap
+    // will have undefined behavior.
+    // Note: 'end' below refers to the initial end of the heap,
+    // while __heap_end__ refers to the last available address
+    .heap (NOLOAD) :
+    {
+        __heap_start__ = .;
+        end = __heap_start__;
+        _end = end;
+        __end = end;
+        __heap_end__ = .;
+        __HeapLimit = __heap_end__;
+    } > ram
+
     /DISCARD/ : {
         // The .init/.fini sections are used by __libc_init_array(), but
         // that isn't needed so no need to include them in the binary.
