From a566a51c09e7fe27ced76b45b7cbf99050f50c45 Mon Sep 17 00:00:00 2001
From: Angus Gratton <gus@projectgus.com>
Date: Thu, 9 Jul 2015 13:48:47 +1000
Subject: [PATCH] Use linker scripts rather than libcirom/objcopy tricks to
 arrange IRAM/IROM sections.

esp-open-rtos compiled code: .text goes into irom by default, symbols
explicitly marked with IRAM attribute go into iram.

SDK code: .text goes into iram by default, symbols marked with
ICACHE_FLASH_ATTR go into irom.

libgcc functions also go into iram by default.
---
 common.mk          |  3 +--
 ld/eagle.app.v6.ld | 35 +++++++++++++++++++++++++----------
 2 files changed, 26 insertions(+), 12 deletions(-)

diff --git a/common.mk b/common.mk
index ff505ff..4522f1d 100644
--- a/common.mk
+++ b/common.mk
@@ -65,7 +65,7 @@ COMPONENTS     ?= core FreeRTOS lwip axtls
 SDK_LIBS		?= main net80211 phy pp wpa
 
 # open source libraries linked in
-LIBS ?= gcc hal cirom
+LIBS ?= hal gcc c
 
 # Note: this isn't overridable without a not-yet-merged patch to esptool
 ENTRY_SYMBOL = call_user_start
@@ -171,7 +171,6 @@ $$($(1)_OBJ_DIR)%.o: $$($(1)_REAL_ROOT)%.c $$($(1)_MAKEFILE) $(wildcard $(ROOT)*
 	$(Q) mkdir -p $$(dir $$@)
 	$$($(1)_CC_ARGS) -c $$< -o $$@
 	$$($(1)_CC_ARGS) -MM -MT $$@ -MF $$(@:.o=.d) $$<
-	$(Q) $(OBJCOPY) --rename-section .text=.irom0.text --rename-section .literal=.irom0.literal $$@
 
 # the component is shown to depend on both obj and source files so we get a meaningful error message
 # for missing explicitly named source files
diff --git a/ld/eagle.app.v6.ld b/ld/eagle.app.v6.ld
index 3505fb8..c4d7d6b 100644
--- a/ld/eagle.app.v6.ld
+++ b/ld/eagle.app.v6.ld
@@ -33,7 +33,7 @@ MEMORY
 {
   dport0_0_seg :                      	org = 0x3FF00000, len = 0x10
   dram0_0_seg :                       	org = 0x3FFE8000, len = 0x14000
-  iram1_0_seg :                       	org = 0x40100000, len = 0x8000
+  iram1_0_seg :                       	org = 0x40100000, len = 0x08000
   irom0_0_seg :                       	org = 0x40240000, len = 0x7C000
 }
 
@@ -186,7 +186,7 @@ SECTIONS
   } >dram0_0_seg :dram0_0_bss_phdr
 /* __stack = 0x3ffc8000; */
 
-  .text : ALIGN(4)
+  .text : ALIGN(4) /* IRAM */
   {
     _stext = .;
     _text_start = ABSOLUTE(.);
@@ -217,7 +217,17 @@ SECTIONS
     *(.entry.text)
     *(.init.literal)
     *(.init)
-    *(.literal .text .literal.* .text.* .iram1.text iram1.literal .stub .gnu.warning .gnu.linkonce.literal.* .gnu.linkonce.t.*.literal .gnu.linkonce.t.*)
+    /* esp-open-rtos compiled source files use the .iram1.* section names for IRAM
+       functions, etc. */
+    *(.iram1.*)
+    /* SDK libraries expect their .text sections to link to iram, not irom */
+    *sdklib*:*(.literal .text .literal.* .text.*)
+    /* libgcc functions also need to be in .text, as some are called before
+       flash is mapped (also performance)
+    */
+    *libgcc.a:*.o(.literal .text .literal.* .text.*)
+
+    *(.stub .gnu.warning .gnu.linkonce.literal.* .gnu.linkonce.t.*.literal .gnu.linkonce.t.*)
     *(.fini.literal)
     *(.fini)
     *(.gnu.version)
@@ -225,6 +235,18 @@ SECTIONS
     _etext = .;
   } >iram1_0_seg :iram1_0_phdr
 
+  .irom0.text : ALIGN(4)
+  {
+    _irom0_text_start = ABSOLUTE(.);
+    /* esp-open-rtos compiled code goes into IROM by default
+       (except for libgcc which is matched above.)
+    */
+    *(.literal .text .literal.* .text.*)
+    /* SDK libraries expect ICACHE_FLASH_ATTR/etc functions to be loaded explicitly as IROM */
+    *sdklib*:*(.irom.* .irom.*.* .irom0.*)
+    _irom0_text_end = ABSOLUTE(.);
+  } >irom0_0_seg :irom0_0_phdr
+
   .lit4 : ALIGN(4)
   {
     _lit4_start = ABSOLUTE(.);
@@ -233,11 +255,4 @@ SECTIONS
     *(.gnu.linkonce.lit4.*)
     _lit4_end = ABSOLUTE(.);
   } >iram1_0_seg :iram1_0_phdr
-
-  .irom0.text : ALIGN(4)
-  {
-    _irom0_text_start = ABSOLUTE(.);
-    *(.irom0.literal .irom.literal .irom.text.literal .irom0.text .irom.text)
-    _irom0_text_end = ABSOLUTE(.);
-  } >irom0_0_seg :irom0_0_phdr
 }