Skip to content

Commit 7607a39

Browse files
committed
[ELF] --warn-backrefs: suppress warnings for backward references within the archive
(cherry picked from commit 99580e2)
1 parent a2f5832 commit 7607a39

File tree

2 files changed

+21
-13
lines changed

2 files changed

+21
-13
lines changed

lld/ELF/Driver.cpp

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -235,22 +235,28 @@ void LinkerDriver::addFile(StringRef path, bool withLOption) {
235235
// user is attempting LTO and using a default ar command that doesn't
236236
// understand the LLVM bitcode file. Treat the archive as a group of lazy
237237
// object files.
238-
if (!file->isEmpty() && !file->hasSymbolTable()) {
239-
for (const std::pair<MemoryBufferRef, uint64_t> &p :
240-
getArchiveMembers(mbref)) {
241-
auto magic = identify_magic(p.first.getBuffer());
242-
if (magic == file_magic::bitcode ||
243-
magic == file_magic::elf_relocatable)
244-
files.push_back(createLazyFile(p.first, path, p.second));
245-
else
246-
error(path + ": archive member '" + p.first.getBufferIdentifier() +
247-
"' is neither ET_REL nor LLVM bitcode");
248-
}
238+
if (file->isEmpty() || file->hasSymbolTable()) {
239+
// Handle the regular case.
240+
files.push_back(make<ArchiveFile>(std::move(file)));
249241
return;
250242
}
251243

252-
// Handle the regular case.
253-
files.push_back(make<ArchiveFile>(std::move(file)));
244+
// All files within the archive get the same group ID to allow mutual
245+
// references for --warn-backrefs.
246+
bool saved = InputFile::isInGroup;
247+
InputFile::isInGroup = true;
248+
for (const std::pair<MemoryBufferRef, uint64_t> &p :
249+
getArchiveMembers(mbref)) {
250+
auto magic = identify_magic(p.first.getBuffer());
251+
if (magic == file_magic::bitcode || magic == file_magic::elf_relocatable)
252+
files.push_back(createLazyFile(p.first, path, p.second));
253+
else
254+
error(path + ": archive member '" + p.first.getBufferIdentifier() +
255+
"' is neither ET_REL nor LLVM bitcode");
256+
}
257+
InputFile::isInGroup = saved;
258+
if (!saved)
259+
++InputFile::nextGroupId;
254260
return;
255261
}
256262
case file_magic::elf_shared_object:

lld/test/ELF/warn-backrefs.s

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@
6363
# RUN: echo '.globl bar; bar:' | llvm-mc -filetype=obj -triple=x86_64 - -o %t3.o
6464
# RUN: echo '.globl foo; foo: call bar' | llvm-mc -filetype=obj -triple=x86_64 - -o %t4.o
6565
# RUN: ld.lld --fatal-warnings --warn-backrefs %t1.o --start-lib %t3.o %t4.o --end-lib -o /dev/null
66+
# RUN: rm -f %t34.a && llvm-ar rcS %t34.a %t3.o %t4.o
67+
# RUN: ld.lld --fatal-warnings --warn-backrefs %t1.o %t34.a -o /dev/null
6668

6769
## We don't report backward references to weak symbols as they can be overridden later.
6870
# RUN: echo '.weak foo; foo:' | llvm-mc -filetype=obj -triple=x86_64 - -o %tweak.o

0 commit comments

Comments
 (0)