[lld] r312656 - Detect linker script INCLUDE cycles.

Rui Ueyama via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 6 11:14:08 PDT 2017


Author: ruiu
Date: Wed Sep  6 11:14:08 2017
New Revision: 312656

URL: http://llvm.org/viewvc/llvm-project?rev=312656&view=rev
Log:
Detect linker script INCLUDE cycles.

Differential Revision: https://reviews.llvm.org/D37524

Added:
    lld/trunk/test/ELF/linkerscript/include-cycle.s
Modified:
    lld/trunk/ELF/ScriptParser.cpp

Modified: lld/trunk/ELF/ScriptParser.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/ScriptParser.cpp?rev=312656&r1=312655&r2=312656&view=diff
==============================================================================
--- lld/trunk/ELF/ScriptParser.cpp (original)
+++ lld/trunk/ELF/ScriptParser.cpp Wed Sep  6 11:14:08 2017
@@ -24,6 +24,7 @@
 #include "Target.h"
 #include "llvm/ADT/SmallString.h"
 #include "llvm/ADT/StringRef.h"
+#include "llvm/ADT/StringSet.h"
 #include "llvm/ADT/StringSwitch.h"
 #include "llvm/BinaryFormat/ELF.h"
 #include "llvm/Support/Casting.h"
@@ -113,6 +114,9 @@ private:
 
   // True if a script being read is in a subdirectory specified by -sysroot.
   bool IsUnderSysroot;
+
+  // A set to detect an INCLUDE() cycle.
+  StringSet<> Seen;
 };
 } // namespace
 
@@ -318,6 +322,11 @@ void ScriptParser::readGroup() {
 void ScriptParser::readInclude() {
   StringRef Tok = unquote(next());
 
+  if (!Seen.insert(Tok).second) {
+    setError("there is a cycle in linker script INCLUDEs");
+    return;
+  }
+
   // https://sourceware.org/binutils/docs/ld/File-Commands.html:
   // The file will be searched for in the current directory, and in any
   // directory specified with the -L option.

Added: lld/trunk/test/ELF/linkerscript/include-cycle.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/linkerscript/include-cycle.s?rev=312656&view=auto
==============================================================================
--- lld/trunk/test/ELF/linkerscript/include-cycle.s (added)
+++ lld/trunk/test/ELF/linkerscript/include-cycle.s Wed Sep  6 11:14:08 2017
@@ -0,0 +1,15 @@
+# REQUIRES: x86
+# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t.o
+
+# RUN: echo INCLUDE %t1.script > %t1.script
+# RUN: not ld.lld %t.o %t1.script 2>&1 | FileCheck %s
+
+# RUN: echo INCLUDE %t2.script > %t1.script
+# RUN: echo INCLUDE %t1.script > %t2.script
+# RUN: not ld.lld %t.o %t1.script 2>&1 | FileCheck %s
+
+# CHECK: there is a cycle in linker script INCLUDEs
+
+.globl _start
+_start:
+  ret




More information about the llvm-commits mailing list