[PATCH] D36138: [ELF] - Don't segfault when accessing location counter inside MEMORY command.

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


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

We would previously crash on next script:
`MEMORY { name : ORIGIN = .; }`

Patch fixes that.


https://reviews.llvm.org/D36138

Files:
  ELF/LinkerScript.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,5 @@
+# REQUIRES: x86
+# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t
+# RUN: echo "MEMORY { name : ORIGIN = .; }" > %t.script
+# RUN: not ld.lld -shared -o %t2 --script %t.script %t 2>&1 | FileCheck %s
+# CHECK: error: {{.*}}.script:1: unable to get location counter value
Index: ELF/LinkerScript.cpp
===================================================================
--- ELF/LinkerScript.cpp
+++ ELF/LinkerScript.cpp
@@ -812,8 +812,13 @@
 }
 
 ExprValue LinkerScript::getSymbolValue(const Twine &Loc, StringRef S) {
-  if (S == ".")
-    return {CurAddressState->OutSec, Dot - CurAddressState->OutSec->Addr, Loc};
+  if (S == ".") {
+    if (CurAddressState)
+      return {CurAddressState->OutSec, Dot - CurAddressState->OutSec->Addr,
+              Loc};
+    error(Loc + ": unable to get location counter value");
+    return 0;
+  }
   if (SymbolBody *B = Symtab->find(S)) {
     if (auto *D = dyn_cast<DefinedRegular>(B))
       return {D->Section, D->Value, Loc};


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D36138.109079.patch
Type: text/x-patch
Size: 1192 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170801/69714795/attachment.bin>


More information about the llvm-commits mailing list