From 23dff0fe815a55da0d58c4c6aeb55a960cfee912 Mon Sep 17 00:00:00 2001
From: shinyquagsire23 <mtinc2@gmail.com>
Date: Sat, 30 Jul 2016 16:54:13 -0700
Subject: [PATCH] Have elf2rpl write in same allignments as reflected in
 rpl.ld, fix issue with .tbss where GCC is dumb and doesn't increment the
 address

---
 rules/rpl.ld           | 27 +++++++++++++--------------
 tools/elf2rpl/main.cpp | 24 +++++++++---------------
 2 files changed, 22 insertions(+), 29 deletions(-)

diff --git a/rules/rpl.ld b/rules/rpl.ld
index 2d6baf6..9062a03 100644
--- a/rules/rpl.ld
+++ b/rules/rpl.ld
@@ -90,15 +90,6 @@ SECTIONS {
       __tdata_lma_end = .;
    } : hdr_data
 
-   .tbss ALIGN(256) :
-   {
-      *(.tbss)
-      *(.tbss.*)
-      *(.gnu.linkonce.tb.*)
-      *(.tcommon)
-      . = ALIGN(4);
-   } : hdr_data
-
    .preinit_array ALIGN(256) :
    {
       PROVIDE (__preinit_array_start = .);
@@ -139,31 +130,39 @@ SECTIONS {
    } : hdr_data
 
    __bss_start__ = .;
-   .bss ALIGN(4) :
+   .bss ALIGN(256) :
    {
       *(.dynbss)
       *(.bss)
       *(.bss.*)
       *(.gnu.linkonce.b*)
       *(COMMON)
-      . = ALIGN(4);
+      . = ALIGN(32);
 
       __sbss_start = .;
       *(.sbss)
       *(.sbss.*)
       __sbss_end = .;
-      . = ALIGN(4);
+      . = ALIGN(32);
 
       __sbss2_start = .;
       *(.sbss2)
       *(.sbss2.*)
       *(.gnu.linkonce.sb2.*)
       __sbss2_end = .;
-      . = ALIGN(4);
+      . = ALIGN(32);
 
       /* Reserve space for the TLS segment of the main thread */
       __tls_start = .;
-      . += + SIZEOF(.tdata) + SIZEOF(.tbss);
+      
+      __tbss_start = .;
+      *(.tbss)
+      *(.tbss.*)
+      *(.gnu.linkonce.tb.*)
+      *(.tcommon)
+      __tbss_end = .;
+      
+      . += + SIZEOF(.tdata);
       __tls_end = .;
       . = ALIGN(32);
    } : hdr_data
diff --git a/tools/elf2rpl/main.cpp b/tools/elf2rpl/main.cpp
index 9791fec..0f3499c 100644
--- a/tools/elf2rpl/main.cpp
+++ b/tools/elf2rpl/main.cpp
@@ -423,19 +423,11 @@ read(ElfFile &file, const std::string &filename)
          auto &sym = symTab[index];
          auto symType = sym.info & 0xf;
 
-         if (symType == elf::STT_NOTYPE && sym.value == 0) {
-            if (rela.offset < DataAddress) {
-               std::cout << "Unexpected symbol referenced in relocation section " << name << std::endl;
-               return false;
-            }
-         } else if (symType == elf::STT_SECTION && sym.value == CodeAddress) {
+         if (symType == elf::STT_SECTION && sym.value == CodeAddress) {
             if (rela.offset < CodeAddress || rela.offset >= DataAddress) {
                std::cout << "Unexpected symbol referenced in relocation section " << name << std::endl;
                return false;
             }
-         } else {
-            std::cout << "Unexpected symbol referenced in relocation section " << name << std::endl;
-            return false;
          }
 
          auto addend = static_cast<uint32_t>(rela.addend);
@@ -450,8 +442,12 @@ read(ElfFile &file, const std::string &filename)
             relocation->symbol = findSymbol(file, DataAddress);
             relocation->addend = addend - DataAddress;
          } else {
-            std::cout << "Unexpected addend " << std::hex << addend << " referenced in relocation section " << name << std::endl;
-            return false;
+            // If we can't find a proper symbol, write the addend in and hope for the best
+            auto ptr = getLoaderDataPtr<uint32_t>(inSections, rela.offset);
+            *ptr = addend;
+            
+            std::cout << "Unexpected addend " << std::hex << addend << " referenced in relocation section " << name << ", continuing." << std::endl;
+            continue;
          }
 
          relocation->target = rela.offset;
@@ -612,13 +608,11 @@ write(ElfFile &file, const std::string &filename)
       out->header.link = 0;
       out->header.info = 0;
 
-      if (section->type == elf::SHT_NOBITS) {
-         out->header.addralign = 256;
-      } else if (section->address == DataAddress) {
+      if (section->address == DataAddress) {
          out->header.addralign = 4096;
          out->header.flags |= elf::SHF_WRITE; // .rodata needs to be writable?
       } else {
-         out->header.addralign = 32;
+         out->header.addralign = 256;
       }
 
       out->header.entsize = 0;