svn commit: r328546 - head/contrib/llvm/tools/lld/ELF

Ed Maste emaste at FreeBSD.org
Mon Jan 29 13:52:43 UTC 2018


Author: emaste
Date: Mon Jan 29 13:52:42 2018
New Revision: 328546
URL: https://svnweb.freebsd.org/changeset/base/328546

Log:
  lld: Improve LMARegion handling.
  
  This fixes the crash reported at [LLVM] PR36083.
  
  The issue is that we were trying to put all the sections in the same
  PT_LOAD and crashing trying to write past the end of the file.
  
  This also adds accounting for used space in LMARegion, without it all
  3 PT_LOADs would have the same physical address.
  
  Obtained from:	LLVM r323449 by Rafael Espindola

Modified:
  head/contrib/llvm/tools/lld/ELF/LinkerScript.cpp
  head/contrib/llvm/tools/lld/ELF/LinkerScript.h
  head/contrib/llvm/tools/lld/ELF/Writer.cpp

Modified: head/contrib/llvm/tools/lld/ELF/LinkerScript.cpp
==============================================================================
--- head/contrib/llvm/tools/lld/ELF/LinkerScript.cpp	Mon Jan 29 13:51:13 2018	(r328545)
+++ head/contrib/llvm/tools/lld/ELF/LinkerScript.cpp	Mon Jan 29 13:52:42 2018	(r328546)
@@ -589,6 +589,10 @@ void LinkerScript::output(InputSection *S) {
 
   // If there is a memory region associated with this input section, then
   // place the section in that region and update the region index.
+  if (Ctx->LMARegion)
+    Ctx->LMARegion->CurPos += Pos - Before;
+  // FIXME: should we also produce overflow errors for LMARegion?
+
   if (Ctx->MemRegion) {
     uint64_t &CurOffset = Ctx->MemRegion->CurPos;
     CurOffset += Pos - Before;
@@ -651,6 +655,7 @@ void LinkerScript::assignOffsets(OutputSection *Sec) {
     setDot(Sec->AddrExpr, Sec->Location, false);
 
   Ctx->MemRegion = Sec->MemRegion;
+  Ctx->LMARegion = Sec->LMARegion;
   if (Ctx->MemRegion)
     Dot = Ctx->MemRegion->CurPos;
 
@@ -660,7 +665,7 @@ void LinkerScript::assignOffsets(OutputSection *Sec) {
     Ctx->LMAOffset = Sec->LMAExpr().getValue() - Dot;
 
   if (MemoryRegion *MR = Sec->LMARegion)
-    Ctx->LMAOffset = MR->Origin - Dot;
+    Ctx->LMAOffset = MR->CurPos - Dot;
 
   // If neither AT nor AT> is specified for an allocatable section, the linker
   // will set the LMA such that the difference between VMA and LMA for the
@@ -690,6 +695,8 @@ void LinkerScript::assignOffsets(OutputSection *Sec) {
       Dot += Cmd->Size;
       if (Ctx->MemRegion)
         Ctx->MemRegion->CurPos += Cmd->Size;
+      if (Ctx->LMARegion)
+        Ctx->LMARegion->CurPos += Cmd->Size;
       Ctx->OutSec->Size = Dot - Ctx->OutSec->Addr;
       continue;
     }

Modified: head/contrib/llvm/tools/lld/ELF/LinkerScript.h
==============================================================================
--- head/contrib/llvm/tools/lld/ELF/LinkerScript.h	Mon Jan 29 13:51:13 2018	(r328545)
+++ head/contrib/llvm/tools/lld/ELF/LinkerScript.h	Mon Jan 29 13:52:42 2018	(r328546)
@@ -206,6 +206,7 @@ class LinkerScript final {
     uint64_t ThreadBssOffset = 0;
     OutputSection *OutSec = nullptr;
     MemoryRegion *MemRegion = nullptr;
+    MemoryRegion *LMARegion = nullptr;
     uint64_t LMAOffset = 0;
   };
 

Modified: head/contrib/llvm/tools/lld/ELF/Writer.cpp
==============================================================================
--- head/contrib/llvm/tools/lld/ELF/Writer.cpp	Mon Jan 29 13:51:13 2018	(r328545)
+++ head/contrib/llvm/tools/lld/ELF/Writer.cpp	Mon Jan 29 13:52:42 2018	(r328546)
@@ -1626,7 +1626,8 @@ template <class ELFT> std::vector<PhdrEntry *> Writer<
     // different flags or is loaded at a discontiguous address using AT linker
     // script command.
     uint64_t NewFlags = computeFlags(Sec->getPhdrFlags());
-    if (Sec->LMAExpr || Flags != NewFlags) {
+    if (Sec->LMAExpr || Sec->MemRegion != Load->FirstSec->MemRegion ||
+        Flags != NewFlags) {
       Load = AddHdr(PT_LOAD, NewFlags);
       Flags = NewFlags;
     }


More information about the svn-src-all mailing list