[PATCH] D37524: Detect linker script INCLUDE cycles.

Rui Ueyama via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 6 11:02:33 PDT 2017


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

Detect linker script INCLUDE cycles.


https://reviews.llvm.org/D37524

Files:
  lld/ELF/ScriptParser.cpp
  lld/test/ELF/linkerscript/include-cycle.s


Index: lld/test/ELF/linkerscript/include-cycle.s
===================================================================
--- /dev/null
+++ lld/test/ELF/linkerscript/include-cycle.s
@@ -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
Index: lld/ELF/ScriptParser.cpp
===================================================================
--- lld/ELF/ScriptParser.cpp
+++ lld/ELF/ScriptParser.cpp
@@ -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"
@@ -111,7 +112,11 @@
   std::pair<std::vector<SymbolVersion>, std::vector<SymbolVersion>>
   readSymbols();
 
+  // 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
 
@@ -317,6 +322,11 @@
 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.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D37524.114033.patch
Type: text/x-patch
Size: 1718 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170906/cb6b74ec/attachment.bin>


More information about the llvm-commits mailing list