[lld] r323449 - Improve LMARegion handling.

Rafael Espindola via llvm-commits llvm-commits at lists.llvm.org
Thu Jan 25 09:42:03 PST 2018


Author: rafael
Date: Thu Jan 25 09:42:03 2018
New Revision: 323449

URL: http://llvm.org/viewvc/llvm-project?rev=323449&view=rev
Log:
Improve LMARegion handling.

This fixes the crash reported at 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.

Added:
    lld/trunk/test/ELF/linkerscript/at3.s
Modified:
    lld/trunk/ELF/LinkerScript.cpp
    lld/trunk/ELF/LinkerScript.h
    lld/trunk/ELF/Writer.cpp

Modified: lld/trunk/ELF/LinkerScript.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/LinkerScript.cpp?rev=323449&r1=323448&r2=323449&view=diff
==============================================================================
--- lld/trunk/ELF/LinkerScript.cpp (original)
+++ lld/trunk/ELF/LinkerScript.cpp Thu Jan 25 09:42:03 2018
@@ -589,6 +589,10 @@ void LinkerScript::output(InputSection *
 
   // 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(OutputS
     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(OutputS
     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(OutputS
       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: lld/trunk/ELF/LinkerScript.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/LinkerScript.h?rev=323449&r1=323448&r2=323449&view=diff
==============================================================================
--- lld/trunk/ELF/LinkerScript.h (original)
+++ lld/trunk/ELF/LinkerScript.h Thu Jan 25 09:42:03 2018
@@ -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: lld/trunk/ELF/Writer.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Writer.cpp?rev=323449&r1=323448&r2=323449&view=diff
==============================================================================
--- lld/trunk/ELF/Writer.cpp (original)
+++ lld/trunk/ELF/Writer.cpp Thu Jan 25 09:42:03 2018
@@ -1626,7 +1626,8 @@ template <class ELFT> std::vector<PhdrEn
     // 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;
     }

Added: lld/trunk/test/ELF/linkerscript/at3.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/linkerscript/at3.s?rev=323449&view=auto
==============================================================================
--- lld/trunk/test/ELF/linkerscript/at3.s (added)
+++ lld/trunk/test/ELF/linkerscript/at3.s Thu Jan 25 09:42:03 2018
@@ -0,0 +1,38 @@
+# REQUIRES: x86
+# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
+# RUN: echo "MEMORY {                                  \
+# RUN:   FOO   (ax) : ORIGIN = 0x1000, LENGTH = 0x100  \
+# RUN:   BAR   (ax) : ORIGIN = 0x2000, LENGTH = 0x100  \
+# RUN:   ZED   (ax) : ORIGIN = 0x3000, LENGTH = 0x100  \
+# RUN:   FLASH (ax) : ORIGIN = 0x6000, LENGTH = 0x200  \
+# RUN: }                                               \
+# RUN: SECTIONS {                                      \
+# RUN:  .foo1 : { *(.foo1) }            > FOO AT>FLASH \
+# RUN:  .foo2 : { *(.foo2) BYTE(0x42) } > BAR AT>FLASH \
+# RUN:  .foo3 : { *(.foo3) }            > ZED AT>FLASH \
+# RUN: }" > %t.script
+# RUN: ld.lld %t.o --script %t.script -o %t
+# RUN: llvm-readelf -sections -program-headers %t | FileCheck %s
+
+# CHECK: .foo1             PROGBITS        0000000000001000 001000
+# CHECK: .foo2             PROGBITS        0000000000002000 002000
+# CHECK: .foo3             PROGBITS        0000000000003000 003000
+
+# CHECK: Program Headers:
+# CHECK-NOT: LOAD
+
+# CHECK:      Type  Offset   VirtAddr           PhysAddr
+# CHECK-NEXT: LOAD  0x001000 0x0000000000001000 0x0000000000006000
+# CHECK-NEXT: LOAD  0x002000 0x0000000000002000 0x0000000000006008
+# CHECK-NEXT: LOAD  0x003000 0x0000000000003000 0x0000000000006011
+
+# CHECK-NOT: LOAD
+
+.section .foo1, "a"
+.quad 0
+
+.section .foo2, "ax"
+.quad 0
+
+.section .foo3, "ax"
+.quad 0




More information about the llvm-commits mailing list