[lld] r308955 - [ELF] - Fix calculation of memory region offset.

George Rimar via llvm-commits llvm-commits at lists.llvm.org
Tue Jul 25 01:29:29 PDT 2017


Author: grimar
Date: Tue Jul 25 01:29:29 2017
New Revision: 308955

URL: http://llvm.org/viewvc/llvm-project?rev=308955&view=rev
Log:
[ELF] - Fix calculation of memory region offset.

This is PR33714.

Previously for each input section offset of memory region
was incremented on a size of output section.

That resulted in a wrong error message saying about
overflow. Patch fixes that.

Differential revision: https://reviews.llvm.org/D35803

Added:
    lld/trunk/test/ELF/linkerscript/memory2.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=308955&r1=308954&r2=308955&view=diff
==============================================================================
--- lld/trunk/ELF/LinkerScript.cpp (original)
+++ lld/trunk/ELF/LinkerScript.cpp Tue Jul 25 01:29:29 2017
@@ -550,6 +550,7 @@ uint64_t LinkerScript::advance(uint64_t
 }
 
 void LinkerScript::output(InputSection *S) {
+  uint64_t Before = advance(0, 1);
   uint64_t Pos = advance(S->getSize(), S->Alignment);
   S->OutSecOff = Pos - S->getSize() - CurAddressState->OutSec->Addr;
 
@@ -563,7 +564,7 @@ void LinkerScript::output(InputSection *
   if (CurAddressState->MemRegion) {
     uint64_t &CurOffset =
         CurAddressState->MemRegionOffset[CurAddressState->MemRegion];
-    CurOffset += CurAddressState->OutSec->Size;
+    CurOffset += Pos - Before;
     uint64_t CurSize = CurOffset - CurAddressState->MemRegion->Origin;
     if (CurSize > CurAddressState->MemRegion->Length) {
       uint64_t OverflowAmt = CurSize - CurAddressState->MemRegion->Length;

Added: lld/trunk/test/ELF/linkerscript/memory2.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/linkerscript/memory2.s?rev=308955&view=auto
==============================================================================
--- lld/trunk/test/ELF/linkerscript/memory2.s (added)
+++ lld/trunk/test/ELF/linkerscript/memory2.s Tue Jul 25 01:29:29 2017
@@ -0,0 +1,14 @@
+# REQUIRES: x86
+# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t
+# RUN: echo "MEMORY { ram (rwx) : ORIGIN = 0, LENGTH = 2K } \
+# RUN: SECTIONS { .text : { *(.text*) } > ram }" > %t.script
+# RUN: ld.lld -o %t2 --script %t.script %t
+
+.text
+.global _start
+_start:
+  .zero 1024
+
+.section .text.foo,"ax",%progbits
+foo:
+  nop




More information about the llvm-commits mailing list