[lld] r358638 - lld: elf: Fix sections with explict addresses in regions

Rui Ueyama via llvm-commits llvm-commits at lists.llvm.org
Wed Apr 17 19:32:12 PDT 2019


Author: ruiu
Date: Wed Apr 17 19:32:12 2019
New Revision: 358638

URL: http://llvm.org/viewvc/llvm-project?rev=358638&view=rev
Log:
lld: elf: Fix sections with explict addresses in regions

Patch by Gabriel Smith.

The address for a section would be evaluated before the region was
switched to. Because of this, the position within the region would not
be updated. After the region is swapped to the dot would be set to the
out of date position within the region, undoing the section address
evaluation.

To fix this, the region is swapped to before the section's address is
evaluated. As part of the fallout of this, expandMemoryRegions needed
to be gated in setDot on the condition that the evaluated address is
less than the dot. This is for the case where sections are not listed
from lowest address to highest address.

Finally, a test for the case where sections are listed "out of order"
was added.

Differential Revision: https://reviews.llvm.org/D60744

Added:
    lld/trunk/test/ELF/linkerscript/out-of-order-section-in-region.s
Modified:
    lld/trunk/ELF/LinkerScript.cpp

Modified: lld/trunk/ELF/LinkerScript.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/LinkerScript.cpp?rev=358638&r1=358637&r2=358638&view=diff
==============================================================================
--- lld/trunk/ELF/LinkerScript.cpp (original)
+++ lld/trunk/ELF/LinkerScript.cpp Wed Apr 17 19:32:12 2019
@@ -135,7 +135,7 @@ void LinkerScript::setDot(Expr E, const
   // Update to location counter means update to section size.
   if (InSec)
     expandOutputSection(Val - Dot);
-  else
+  else if (Val > Dot)
     expandMemoryRegions(Val - Dot);
 
   Dot = Val;
@@ -760,14 +760,15 @@ static OutputSection *findFirstSection(P
 void LinkerScript::assignOffsets(OutputSection *Sec) {
   if (!(Sec->Flags & SHF_ALLOC))
     Dot = 0;
-  else if (Sec->AddrExpr)
-    setDot(Sec->AddrExpr, Sec->Location, false);
 
   Ctx->MemRegion = Sec->MemRegion;
   Ctx->LMARegion = Sec->LMARegion;
   if (Ctx->MemRegion)
     Dot = Ctx->MemRegion->CurPos;
 
+  if ((Sec->Flags & SHF_ALLOC) && Sec->AddrExpr)
+    setDot(Sec->AddrExpr, Sec->Location, false);
+
   switchTo(Sec);
 
   if (Sec->LMAExpr)

Added: lld/trunk/test/ELF/linkerscript/out-of-order-section-in-region.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/linkerscript/out-of-order-section-in-region.s?rev=358638&view=auto
==============================================================================
--- lld/trunk/test/ELF/linkerscript/out-of-order-section-in-region.s (added)
+++ lld/trunk/test/ELF/linkerscript/out-of-order-section-in-region.s Wed Apr 17 19:32:12 2019
@@ -0,0 +1,22 @@
+# REQUIRES: x86
+# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t
+# RUN: echo "MEMORY {                                       \
+# RUN:   REGION (rwx) : ORIGIN = 0x1000, LENGTH = 0x100     \
+# RUN: }                                                    \
+# RUN:                                                      \
+# RUN: SECTIONS {                                           \
+# RUN:   .aaa ORIGIN(REGION) + 0x8 : { *(.aaa) } > REGION   \
+# RUN:   _stext = .;                                        \
+# RUN:   .bbb ORIGIN(REGION) : { *(.bbb) } > REGION         \
+# RUN:   . = _stext;                                        \
+# RUN: }" > %t.script
+# RUN: ld.lld %t --script %t.script -o %t2
+# RUN: llvm-objdump -section-headers %t2 | FileCheck %s
+# CHECK: .aaa        00000008 0000000000001008 DATA
+# CHECK: .bbb        00000008 0000000000001000 DATA
+
+.section .aaa, "a"
+.quad 0
+
+.section .bbb, "a"
+.quad 0




More information about the llvm-commits mailing list