[PATCH] D36140: [ELF] - Do not segfault if linkerscript tries to access Target too early.

George Rimar via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Aug 1 04:56:17 PDT 2017


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

Following possible scripts triggered accessing to Target when it was not yet
initialized (was nullptr).

  MEMORY { name : ORIGIN = DATA_SEGMENT_RELRO_END; }
  
  MEMORY { name : ORIGIN = CONSTANT(COMMONPAGESIZE); }

Patch errors out instead.


https://reviews.llvm.org/D36140

Files:
  ELF/ScriptParser.cpp
  test/ELF/linkerscript/memory-err.s


Index: test/ELF/linkerscript/memory-err.s
===================================================================
--- test/ELF/linkerscript/memory-err.s
+++ test/ELF/linkerscript/memory-err.s
@@ -0,0 +1,11 @@
+# REQUIRES: x86
+# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t
+# RUN: echo "MEMORY { name : ORIGIN = DATA_SEGMENT_RELRO_END; }" > %t.script
+# RUN: not ld.lld -shared -o %t2 --script %t.script %t 2>&1 |\
+# RUN:   FileCheck %s --check-prefix=ERR1
+# ERR1: error: {{.*}}.script:1: unable to calculate: DATA_SEGMENT_RELRO_END
+
+# RUN: echo "MEMORY { name : ORIGIN = CONSTANT(COMMONPAGESIZE); }" > %t.script
+# RUN: not ld.lld -shared -o %t2 --script %t.script %t 2>&1 |\
+# RUN:   FileCheck %s --check-prefix=ERR2
+# ERR2: error: {{.*}}.script:1: unable to calculate: COMMONPAGESIZE
Index: ELF/ScriptParser.cpp
===================================================================
--- ELF/ScriptParser.cpp
+++ ELF/ScriptParser.cpp
@@ -793,9 +793,13 @@
   return Lhs;
 }
 
-uint64_t static getConstant(StringRef S) {
-  if (S == "COMMONPAGESIZE")
-    return Target->PageSize;
+uint64_t static getConstant(StringRef S, StringRef Location) {
+  if (S == "COMMONPAGESIZE") {
+    if (Target)
+      return Target->PageSize;
+    error(Location + ": unable to calculate: " + S);
+    return 0;
+  }
   if (S == "MAXPAGESIZE")
     return Config->MaxPageSize;
   error("unknown constant: " + S);
@@ -921,7 +925,7 @@
     return readAssertExpr();
   if (Tok == "CONSTANT") {
     StringRef Name = readParenLiteral();
-    return [=] { return getConstant(Name); };
+    return [=] { return getConstant(Name, Location); };
   }
   if (Tok == "DATA_SEGMENT_ALIGN") {
     expect("(");
@@ -948,7 +952,13 @@
     expect(",");
     readExpr();
     expect(")");
-    return [] { return alignTo(Script->getDot(), Target->PageSize); };
+    return [=] {
+      if (!Target) {
+        error(Location + ": unable to calculate: " + Tok);
+        return (uint64_t)0;
+      }
+      return alignTo(Script->getDot(), Target->PageSize);
+    };
   }
   if (Tok == "DEFINED") {
     StringRef Name = readParenLiteral();


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D36140.109085.patch
Type: text/x-patch
Size: 2126 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170801/235ef588/attachment.bin>


More information about the llvm-commits mailing list