[PATCH] D60744: lld: elf: Fix sections with explict addresses in regions

Gabriel Smith via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Apr 15 18:56:11 PDT 2019


yodaldevoid created this revision.
yodaldevoid added reviewers: espindola, ruiu.
Herald added subscribers: llvm-commits, MaskRay, arichardson, emaste.
Herald added a project: LLVM.

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.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D60744

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


Index: lld/test/ELF/linkerscript/out-of-order-section-in-region.s
===================================================================
--- /dev/null
+++ lld/test/ELF/linkerscript/out-of-order-section-in-region.s
@@ -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
Index: lld/ELF/LinkerScript.cpp
===================================================================
--- lld/ELF/LinkerScript.cpp
+++ lld/ELF/LinkerScript.cpp
@@ -135,7 +135,7 @@
   // 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 @@
 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)


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D60744.195286.patch
Type: text/x-patch
Size: 2021 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190416/cf81004f/attachment.bin>


More information about the llvm-commits mailing list