From f2ab0eb644a60f946f35fcb5578fba53720edfa7 Mon Sep 17 00:00:00 2001
From: Anish Athalye <me@anishathalye.com>
Date: Mon, 21 Oct 2019 22:27:18 -0400
Subject: [PATCH] Clean up linker script

This patch does the following:

- Add .text.* to the .text section in the output
- Add an assertion that the trampoline does not overflow a page
- Add the .rodata section
- Make .sdata and .sdata.* (which is for small data) be absorbed into
  the .data section, because we don't need to distinguish between them;
  this prevents .sdata from appearing in the output
- Make the analogous change for .srodata and .sbss
- Make all the data sections 16-byte aligned

This patch also updates the .editorconfig for *.ld files.
---
 .editorconfig    |  3 +++
 kernel/kernel.ld | 36 ++++++++++++++++++++++++------------
 2 files changed, 27 insertions(+), 12 deletions(-)

diff --git a/.editorconfig b/.editorconfig
index cd9dfc3..c47611e 100644
--- a/.editorconfig
+++ b/.editorconfig
@@ -14,6 +14,9 @@ indent_size = 2
 [*.S]
 indent_size = 8
 
+[*.ld]
+indent_size = 2
+
 [Makefile]
 indent_style = tab
 indent_size = 8
diff --git a/kernel/kernel.ld b/kernel/kernel.ld
index acc3c8e..ee04f22 100644
--- a/kernel/kernel.ld
+++ b/kernel/kernel.ld
@@ -8,25 +8,37 @@ SECTIONS
    * where qemu's -kernel jumps.
    */
   . = 0x80000000;
-  .text :
-  {
-    *(.text)
+
+  .text : {
+    *(.text .text.*)
     . = ALIGN(0x1000);
+    _trampoline = .;
     *(trampsec)
+    . = ALIGN(0x1000);
+    ASSERT(. - _trampoline == 0x1000, "error: trampoline larger than one page");
+    PROVIDE(etext = .);
   }
 
-  . = ALIGN(0x1000);
-  PROVIDE(etext = .);
+  .rodata : {
+    . = ALIGN(16);
+    *(.srodata .srodata.*) /* do not need to distinguish this from .rodata */
+    . = ALIGN(16);
+    *(.rodata .rodata.*)
+  }
 
-  /*
-   * make sure end is after data and bss.
-   */
   .data : {
-    *(.data)
+    . = ALIGN(16);
+    *(.sdata .sdata.*) /* do not need to distinguish this from .data */
+    . = ALIGN(16);
+    *(.data .data.*)
   }
+
   .bss : {
-    *(.bss)
-    *(.sbss*)
-     PROVIDE(end = .);
+    . = ALIGN(16);
+    *(.sbss .sbss.*) /* do not need to distinguish this from .bss */
+    . = ALIGN(16);
+    *(.bss .bss.*)
   }
+
+  PROVIDE(end = .);
 }