Skip to content

Commit 99580e2

Browse files
committed
[ELF] --warn-backrefs: suppress warnings for backward references within the archive
1 parent 43d3d88 commit 99580e2

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

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