[PATCH] D37469: [ELF] - Linkerscript: set load address correctly if MEMORY command used.

George Rimar via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Sep 5 05:30:13 PDT 2017


grimar created this revision.
Herald added a subscriber: emaste.

Previously LLD did not calculate `LMAOffset` correctly when
AT and MEMORy were used together.

Patch fixes PR34407.


https://reviews.llvm.org/D37469

Files:
  ELF/LinkerScript.cpp
  test/ELF/linkerscript/memory-at.s


Index: test/ELF/linkerscript/memory-at.s
===================================================================
--- test/ELF/linkerscript/memory-at.s
+++ test/ELF/linkerscript/memory-at.s
@@ -0,0 +1,46 @@
+# REQUIRES: x86
+# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t
+# RUN: echo "MEMORY {                                   \
+# RUN:   FLASH (rx) : ORIGIN = 0x1000, LENGTH = 0x100   \
+# RUN:   RAM (rwx)  : ORIGIN = 0x2000, LENGTH = 0x100 } \
+# RUN: SECTIONS {                                       \
+# RUN:  .text : { *(.text*) } > FLASH                   \
+# RUN:  __etext = .;                                    \
+# RUN:  .data : AT (__etext) { *(.data*) } > RAM        \
+# RUN: }" > %t.script
+# RUN: ld.lld %t --script %t.script -o %t2
+# RUN: llvm-readobj -program-headers %t2 | FileCheck %s
+
+# CHECK:      ProgramHeaders [
+# CHECK-NEXT:   ProgramHeader {
+# CHECK-NEXT:     Type: PT_LOAD
+# CHECK-NEXT:     Offset: 0x1000
+# CHECK-NEXT:     VirtualAddress: 0x1000
+# CHECK-NEXT:     PhysicalAddress: 0x1000
+# CHECK-NEXT:     FileSize: 8
+# CHECK-NEXT:     MemSize: 8
+# CHECK-NEXT:     Flags [
+# CHECK-NEXT:       PF_R
+# CHECK-NEXT:       PF_X
+# CHECK-NEXT:     ]
+# CHECK-NEXT:     Alignment:
+# CHECK-NEXT:   }
+# CHECK-NEXT:   ProgramHeader {
+# CHECK-NEXT:     Type: PT_LOAD
+# CHECK-NEXT:     Offset: 0x2000
+# CHECK-NEXT:     VirtualAddress: 0x2000
+# CHECK-NEXT:     PhysicalAddress: 0x1008
+# CHECK-NEXT:     FileSize: 8
+# CHECK-NEXT:     MemSize: 8
+# CHECK-NEXT:     Flags [
+# CHECK-NEXT:       PF_R
+# CHECK-NEXT:       PF_W
+# CHECK-NEXT:     ]
+# CHECK-NEXT:     Alignment:
+# CHECK-NEXT:   }
+
+.section .text, "ax"
+.quad 0
+
+.section .data, "aw"
+.quad 0
Index: ELF/LinkerScript.cpp
===================================================================
--- ELF/LinkerScript.cpp
+++ ELF/LinkerScript.cpp
@@ -616,14 +616,15 @@
   else if (Sec->AddrExpr)
     setDot(Sec->AddrExpr, Sec->Location, false);
 
+  CurAddressState->MemRegion = Sec->MemRegion;
+  if (CurAddressState->MemRegion)
+    Dot = CurAddressState->MemRegionOffset[CurAddressState->MemRegion];
+
   if (Sec->LMAExpr) {
     uint64_t D = Dot;
     CurAddressState->LMAOffset = [=] { return Sec->LMAExpr().getValue() - D; };
   }
 
-  CurAddressState->MemRegion = Sec->MemRegion;
-  if (CurAddressState->MemRegion)
-    Dot = CurAddressState->MemRegionOffset[CurAddressState->MemRegion];
   switchTo(Sec);
 
   // We do not support custom layout for compressed debug sectons.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D37469.113840.patch
Type: text/x-patch
Size: 2497 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170905/54429f17/attachment.bin>


More information about the llvm-commits mailing list