[ELF] --warn-backrefs: suppress warnings for backward references within the archive
authorFangrui Song <i@maskray.me>
Wed, 9 Feb 2022 05:45:55 +0000 (21:45 -0800)
committerFangrui Song <i@maskray.me>
Wed, 9 Feb 2022 05:45:55 +0000 (21:45 -0800)
lld/ELF/Driver.cpp
lld/test/ELF/warn-backrefs.s

index 3a480fc..e6a7731 100644 (file)
@@ -236,22 +236,28 @@ void LinkerDriver::addFile(StringRef path, bool withLOption) {
     // user is attempting LTO and using a default ar command that doesn't
     // understand the LLVM bitcode file. Treat the archive as a group of lazy
     // object files.
-    if (!file->isEmpty() && !file->hasSymbolTable()) {
-      for (const std::pair<MemoryBufferRef, uint64_t> &p :
-           getArchiveMembers(mbref)) {
-        auto magic = identify_magic(p.first.getBuffer());
-        if (magic == file_magic::bitcode ||
-            magic == file_magic::elf_relocatable)
-          files.push_back(createLazyFile(p.first, path, p.second));
-        else
-          error(path + ": archive member '" + p.first.getBufferIdentifier() +
-                "' is neither ET_REL nor LLVM bitcode");
-      }
+    if (file->isEmpty() || file->hasSymbolTable()) {
+      // Handle the regular case.
+      files.push_back(make<ArchiveFile>(std::move(file)));
       return;
     }
 
-    // Handle the regular case.
-    files.push_back(make<ArchiveFile>(std::move(file)));
+    // All files within the archive get the same group ID to allow mutual
+    // references for --warn-backrefs.
+    bool saved = InputFile::isInGroup;
+    InputFile::isInGroup = true;
+    for (const std::pair<MemoryBufferRef, uint64_t> &p :
+         getArchiveMembers(mbref)) {
+      auto magic = identify_magic(p.first.getBuffer());
+      if (magic == file_magic::bitcode || magic == file_magic::elf_relocatable)
+        files.push_back(createLazyFile(p.first, path, p.second));
+      else
+        error(path + ": archive member '" + p.first.getBufferIdentifier() +
+              "' is neither ET_REL nor LLVM bitcode");
+    }
+    InputFile::isInGroup = saved;
+    if (!saved)
+      ++InputFile::nextGroupId;
     return;
   }
   case file_magic::elf_shared_object:
index a686686..907f042 100644 (file)
@@ -63,6 +63,8 @@
 # RUN: echo '.globl bar; bar:' | llvm-mc -filetype=obj -triple=x86_64 - -o %t3.o
 # RUN: echo '.globl foo; foo: call bar' | llvm-mc -filetype=obj -triple=x86_64 - -o %t4.o
 # RUN: ld.lld --fatal-warnings --warn-backrefs %t1.o --start-lib %t3.o %t4.o --end-lib -o /dev/null
+# RUN: rm -f %t34.a && llvm-ar rcS %t34.a %t3.o %t4.o
+# RUN: ld.lld --fatal-warnings --warn-backrefs %t1.o %t34.a -o /dev/null
 
 ## We don't report backward references to weak symbols as they can be overridden later.
 # RUN: echo '.weak foo; foo:' | llvm-mc -filetype=obj -triple=x86_64 - -o %tweak.o