[llvm-branch-commits] [lld] 7607a39 - [ELF] --warn-backrefs: suppress warnings for backward references within the archive

Fangrui Song via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Tue Feb 8 21:50:05 PST 2022


Author: Fangrui Song
Date: 2022-02-08T21:48:52-08:00
New Revision: 7607a39e9388b32ca90414ada56ce687b2272be4

URL: https://github.com/llvm/llvm-project/commit/7607a39e9388b32ca90414ada56ce687b2272be4
DIFF: https://github.com/llvm/llvm-project/commit/7607a39e9388b32ca90414ada56ce687b2272be4.diff

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

(cherry picked from commit 99580e29d821beeaf75345deb3e4cc2c6808bfc0)

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 406571e4e9fd5..ca56f5ee0f10f 100644
--- a/lld/ELF/Driver.cpp
+++ b/lld/ELF/Driver.cpp
@@ -235,22 +235,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-branch-commits mailing list