[PATCH] D41477: [LLD] [ELF] Result of subtracting two symbols should be absolute

Erick Reyes via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Dec 20 19:49:31 PST 2017


erickreyes created this revision.
erickreyes added a reviewer: lld.
erickreyes added a project: lld.
Herald added subscribers: llvm-commits, emaste.

When two linker script symbols are subtracted, the result should be absolute.

This is the behavior of binutils' ld.


Repository:
  rLLD LLVM Linker

https://reviews.llvm.org/D41477

Files:
  ELF/ScriptParser.cpp
  test/ELF/linkerscript/operators.s


Index: test/ELF/linkerscript/operators.s
===================================================================
--- test/ELF/linkerscript/operators.s
+++ test/ELF/linkerscript/operators.s
@@ -1,6 +1,7 @@
 # REQUIRES: x86
 # RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t
 # RUN: echo "SECTIONS { \
+# RUN:  _start = .; \
 # RUN:  plus = 1 + 2 + 3; \
 # RUN:  minus = 5 - 1; \
 # RUN:  div = 6 / 2; \
@@ -26,6 +27,9 @@
 # RUN:  . = 0xfff0; \
 # RUN:  datasegmentalign = DATA_SEGMENT_ALIGN (0xffff, 0); \
 # RUN:  datasegmentalign2 = DATA_SEGMENT_ALIGN (0, 0); \
+# RUN:  _end = .; \
+# RUN:  minus_rel = _end - 0x10; \
+# RUN:  minus_abs = _end - _start; \
 # RUN: }" > %t.script
 # RUN: ld.lld %t --script %t.script -o %t2
 # RUN: llvm-objdump -t %t2 | FileCheck %s
@@ -53,6 +57,8 @@
 # CHECK: 00000000001000 *ABS* 00000000 commonpagesize
 # CHECK: 0000000000ffff *ABS* 00000000 datasegmentalign
 # CHECK: 0000000000fff0 *ABS* 00000000 datasegmentalign2
+# CHECK: 0000000000ffe0 .text 00000000 minus_rel
+# CHECK: 0000000000fff0 *ABS* 00000000 minus_abs
 
 ## Mailformed number error.
 # RUN: echo "SECTIONS { . = 0x12Q41; }" > %t.script
Index: ELF/ScriptParser.cpp
===================================================================
--- ELF/ScriptParser.cpp
+++ ELF/ScriptParser.cpp
@@ -151,6 +151,8 @@
 }
 
 static ExprValue sub(ExprValue A, ExprValue B) {
+  if (!A.isAbsolute() && !B.isAbsolute())
+    return A.getValue() - B.getValue();
   return {A.Sec, false, A.getSectionOffset() - B.getValue(), A.Loc};
 }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D41477.127824.patch
Type: text/x-patch
Size: 1533 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20171221/14c84c2e/attachment.bin>


More information about the llvm-commits mailing list