[PATCH] D38238: [ELF] Add constant for querying the image base in linker scripts

James Henderson via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Sep 25 07:20:05 PDT 2017


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

LLD has defaults for the image base of all its targets, and switches to modify what these bases are (e.g. --image-base). These defaults and switches are ignored when processing linker scripts, and in the typical case, a linker script opens by setting the location counter to the desired base value.

In our case, we ship linker scripts, but we want our customers to be able to vary the image base whilst still using these scripts. This is not possible currently.

An alternative approach would be to set the location counter to the image base initially, instead of zero. However, this would be a change in existing behaviour that could affect users who are assuming the default value is zero, and are therefore not setting the value explicitly.


https://reviews.llvm.org/D38238

Files:
  ELF/ScriptParser.cpp
  test/ELF/linkerscript/image-base.s


Index: test/ELF/linkerscript/image-base.s
===================================================================
--- test/ELF/linkerscript/image-base.s
+++ test/ELF/linkerscript/image-base.s
@@ -0,0 +1,18 @@
+# REQUIRES: x86
+
+# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
+# RUN: echo "SECTIONS { mysym = CONSTANT(IMAGEBASE); }" > %t.script
+
+# RUN: ld.lld %t.o -o %t-default.elf -T %t.script
+# RUN: llvm-readobj --symbols %t-default.elf | FileCheck %s --check-prefix=DEFAULT
+# DEFAULT: Name: mysym
+# DEFAULT-NEXT: Value: 0x200000
+
+# RUN: ld.lld %t.o -o %t-switch.elf -T %t.script --image-base=0x100000
+# RUN: llvm-readobj --symbols %t-switch.elf | FileCheck %s --check-prefix=SWITCH
+# SWITCH: Name: mysym
+# SWITCH-NEXT: Value: 0x100000
+
+.global _start
+_start:
+    nop
Index: ELF/ScriptParser.cpp
===================================================================
--- ELF/ScriptParser.cpp
+++ ELF/ScriptParser.cpp
@@ -850,6 +850,8 @@
     return getPageSize();
   if (S == "MAXPAGESIZE")
     return [] { return Config->MaxPageSize; };
+  if (S == "IMAGEBASE")
+    return [] { return Config->ImageBase; };
   setError("unknown constant: " + S);
   return {};
 }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D38238.116553.patch
Type: text/x-patch
Size: 1220 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170925/6deec828/attachment.bin>


More information about the llvm-commits mailing list