Index: ELF/Driver.cpp
===================================================================
--- ELF/Driver.cpp
+++ ELF/Driver.cpp
@@ -987,6 +987,7 @@
       if (!InputFile::IsInGroup)
         error("stray --end-group");
       InputFile::IsInGroup = false;
+      ++InputFile::NextGroupId;
       break;
     case OPT_start_lib:
       InLib = true;
Index: ELF/InputFiles.h
===================================================================
--- ELF/InputFiles.h
+++ ELF/InputFiles.h
@@ -118,6 +118,7 @@
   // ID. For more info, see checkDependency() in SymbolTable.cpp.
   uint32_t GroupId;
   static bool IsInGroup;
+  static uint32_t NextGroupId;
 
 protected:
   InputFile(Kind K, MemoryBufferRef M);
@@ -126,8 +127,6 @@
 
 private:
   const Kind FileKind;
-
-  static uint32_t NextGroupId;
 };
 
 template <typename ELFT> class ELFFileBase : public InputFile {
Index: ELF/ScriptParser.cpp
===================================================================
--- ELF/ScriptParser.cpp
+++ ELF/ScriptParser.cpp
@@ -333,6 +333,8 @@
   InputFile::IsInGroup = true;
   readInput();
   InputFile::IsInGroup = Orig;
+  if (!Orig)
+    ++InputFile::NextGroupId;
 }
 
 void ScriptParser::readInclude() {
Index: test/ELF/warn-backrefs.s
===================================================================
--- test/ELF/warn-backrefs.s
+++ test/ELF/warn-backrefs.s
@@ -21,6 +21,10 @@
 
 # RUN: not ld.lld --fatal-warnings --warn-backrefs -o %t.exe %t2.a %t1.o 2>&1 | FileCheck %s
 # RUN: not ld.lld --fatal-warnings --warn-backrefs -o %t.exe %t2.a "-(" %t1.o "-)" 2>&1 | FileCheck %s
+# RUN: not ld.lld --fatal-warnings --warn-backrefs -o %t.exe --start-group %t2.a --end-group %t1.o 2>&1 | FileCheck %s
+
+# RUN: echo "GROUP(\"%t2.a\")" > %t3.script
+# RUN: not ld.lld --fatal-warnings --warn-backrefs -o %t.exe %t3.script %t1.o 2>&1 | FileCheck %s
 
 # CHECK: backward reference detected: foo in {{.*}}1.o refers to {{.*}}2.a