[PATCH] D44730: [ELF] - Do not forget to expand the memory region.

George Rimar via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Mar 21 04:47:06 PDT 2018


grimar created this revision.
grimar added reviewers: ruiu, espindola.
Herald added subscribers: arichardson, emaste.

This is PR36799.

Currently, we might have a bug with scripts like below:

  .foo : ALIGN(8) 
  {
    *(.foo)
  } > ram

because do not expand the memory region when doing ALIGN.
This might result in file range overlaps. The patch fixes the issue.


https://reviews.llvm.org/D44730

Files:
  ELF/LinkerScript.cpp
  ELF/LinkerScript.h
  test/ELF/linkerscript/memory-region-alignment.test


Index: test/ELF/linkerscript/memory-region-alignment.test
===================================================================
--- test/ELF/linkerscript/memory-region-alignment.test
+++ test/ELF/linkerscript/memory-region-alignment.test
@@ -0,0 +1,36 @@
+# REQUIRES: x86
+# RUN: echo '.section .foo,"a"; .quad 0; .section .zed,"M", at progbits,1; .byte 0' > %t.s
+# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %t.s -o %t.o
+
+MEMORY {
+  ram (rwx): org = 0x1, len = 96K
+}
+
+SECTIONS {
+  .foo : ALIGN(8) {
+    *(.foo)
+  } > ram
+}
+
+# RUN: ld.lld %t.o -o %t --script %s
+# RUN: llvm-readobj -sections %t | FileCheck %s
+
+# CHECK: Name: .foo
+# CHECK: Address: 0x8
+# CHECK: Offset: 0x1008
+# CHECK: Size: 8
+
+# CHECK: Name: .text
+# CHECK: Address: 0x10
+# CHECK: Offset: 0x1010
+# CHECK: Size: 0
+
+# CHECK: Name: .zed
+# CHECK: Address: 0x0
+# CHECK: Offset: 0x1010
+# CHECK: Size: 1 
+
+# CHECK: Name: .comment
+# CHECK: Address: 0x0
+# CHECK: Offset: 0x1011
+# CHECK: Size: 8
Index: ELF/LinkerScript.h
===================================================================
--- ELF/LinkerScript.h
+++ ELF/LinkerScript.h
@@ -232,6 +232,7 @@
   void assignSymbol(SymbolAssignment *Cmd, bool InSec);
   void setDot(Expr E, const Twine &Loc, bool InSec);
   void expandOutputSection(uint64_t Size);
+  void expandMemoryRegions(uint64_t Size);
 
   std::vector<InputSection *>
   computeInputSections(const InputSectionDescription *);
Index: ELF/LinkerScript.cpp
===================================================================
--- ELF/LinkerScript.cpp
+++ ELF/LinkerScript.cpp
@@ -112,16 +112,20 @@
           "': overflowed by " + Twine(NewSize - MemRegion->Length) + " bytes");
 }
 
-void LinkerScript::expandOutputSection(uint64_t Size) {
-  Ctx->OutSec->Size += Size;
+void LinkerScript::expandMemoryRegions(uint64_t Size) {
   if (Ctx->MemRegion)
     expandMemoryRegion(Ctx->MemRegion, Size, Ctx->MemRegion->Name,
                        Ctx->OutSec->Name);
   if (Ctx->LMARegion)
     expandMemoryRegion(Ctx->LMARegion, Size, Ctx->LMARegion->Name,
                        Ctx->OutSec->Name);
 }
 
+void LinkerScript::expandOutputSection(uint64_t Size) {
+  Ctx->OutSec->Size += Size;
+  expandMemoryRegions(Size);
+}
+
 void LinkerScript::setDot(Expr E, const Twine &Loc, bool InSec) {
   uint64_t Val = E().getValue();
   if (Val < Dot && InSec)
@@ -691,9 +695,12 @@
 void LinkerScript::switchTo(OutputSection *Sec) {
   if (Ctx->OutSec == Sec)
     return;
-
   Ctx->OutSec = Sec;
-  Ctx->OutSec->Addr = advance(0, Ctx->OutSec->Alignment);
+
+  uint64_t Before = advance(0, 1);
+  uint64_t Pos = advance(0, Ctx->OutSec->Alignment);
+  Ctx->OutSec->Addr = Pos;
+  expandMemoryRegions(Pos - Before);
 }
 
 // This function searches for a memory region to place the given output


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D44730.139276.patch
Type: text/x-patch
Size: 2799 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180321/20a74b91/attachment.bin>


More information about the llvm-commits mailing list