[lld] 99580e2 - [ELF] --warn-backrefs: suppress warnings for backward references within the archive

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Tue Feb 8 21:46:00 PST 2022


Author: Fangrui Song
Date: 2022-02-08T21:45:55-08:00
New Revision: 99580e29d821beeaf75345deb3e4cc2c6808bfc0

URL: https://github.com/llvm/llvm-project/commit/99580e29d821beeaf75345deb3e4cc2c6808bfc0
DIFF: https://github.com/llvm/llvm-project/commit/99580e29d821beeaf75345deb3e4cc2c6808bfc0.diff

LOG: [ELF] --warn-backrefs: suppress warnings for backward references within the archive

Added: 
    

Modified: 
    lld/ELF/Driver.cpp
    lld/test/ELF/warn-backrefs.s

Removed: 
    


################################################################################
diff  --git a/lld/ELF/Driver.cpp b/lld/ELF/Driver.cpp
index 3a480fc444f6e..e6a7731f01cd4 100644
--- a/lld/ELF/Driver.cpp
+++ b/lld/ELF/Driver.cpp
@@ -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:

diff  --git a/lld/test/ELF/warn-backrefs.s b/lld/test/ELF/warn-backrefs.s
index a6866869054a4..907f042c0e818 100644
--- a/lld/test/ELF/warn-backrefs.s
+++ b/lld/test/ELF/warn-backrefs.s
@@ -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


        


More information about the llvm-commits mailing list