Skip to content

Commit 03c825c

Browse files
committed
[ELF] --warn-backrefs: don't warn for linking sandwich problems
This is an alternative design to D77512. D45195 added --warn-backrefs to detect * A. certain input orders which GNU ld either errors ("undefined reference") or has different resolution semantics * B. (byproduct) some latent multiple definition problems (-ldef1 -lref -ldef2) which I call "linking sandwich problems". def2 may or may not be the same as def1. When an archive appears more than once (-ldef -lref -ldef), lld and GNU ld may have the same resolution but --warn-backrefs may warn. This is not uncommon. For example, currently lld itself has such a problem: ``` liblldCommon.a liblldCOFF.a ... liblldCommon.a _ZN3lld10DWARFCache13getDILineInfoEmm in liblldCOFF.a refers to liblldCommon.a(DWARF.cpp.o) libLLVMSupport.a also appears twice and has a similar warning ``` glibc has such problems. It is somewhat destined because of its separate libc/libpthread/... and arbitrary grouping. The situation is getting improved over time but I have seen: ``` -lc __isnanl references -lm -lc _IO_funlockfile references -lpthread ``` There are also various issues in interaction with other runtime libraries such as libgcc_eh and libunwind: ``` -lc __gcc_personality_v0 references -lgcc_eh -lpthread __gcc_personality_v0 references -lgcc_eh -lpthread _Unwind_GetCFA references -lunwind ``` These problems are actually benign. We want --warn-backrefs to focus on its main task A and defer task B (which is also useful) to a more specific future feature (see gold --detect-odr-violations and https://bugs.llvm.org/show_bug.cgi?id=43110). Instead of warning immediately, we store the message and only report it if no subsequent lazy definition exists. The use of the static variable `backrefDiags` is similar to `undefs` in Relocations.cpp Reviewed By: grimar Differential Revision: https://reviews.llvm.org/D77522
1 parent 4e907e9 commit 03c825c

File tree

4 files changed

+47
-3
lines changed

4 files changed

+47
-3
lines changed

lld/ELF/Driver.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ bool link(ArrayRef<const char *> args, bool canExitEarly, raw_ostream &stdoutOS,
9494
bitcodeFiles.clear();
9595
objectFiles.clear();
9696
sharedFiles.clear();
97+
backwardReferences.clear();
9798

9899
config = make<Configuration>();
99100
driver = make<LinkerDriver>();
@@ -1921,6 +1922,9 @@ template <class ELFT> void LinkerDriver::link(opt::InputArgList &args) {
19211922
// With this the symbol table should be complete. After this, no new names
19221923
// except a few linker-synthesized ones will be added to the symbol table.
19231924
compileBitcodeFiles<ELFT>();
1925+
1926+
// Symbol resolution finished. Report backward reference problems.
1927+
reportBackrefs();
19241928
if (errorCount())
19251929
return;
19261930

lld/ELF/Symbols.cpp

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ Defined *ElfSym::relaIpltStart;
6363
Defined *ElfSym::relaIpltEnd;
6464
Defined *ElfSym::riscvGlobalPointer;
6565
Defined *ElfSym::tlsModuleBase;
66+
DenseMap<const Symbol *, const InputFile *> backwardReferences;
6667

6768
static uint64_t getSymVA(const Symbol &sym, int64_t &addend) {
6869
switch (sym.kind()) {
@@ -373,6 +374,14 @@ bool computeIsPreemptible(const Symbol &sym) {
373374
return true;
374375
}
375376

377+
void reportBackrefs() {
378+
for (auto &it : backwardReferences) {
379+
const Symbol &sym = *it.first;
380+
warn("backward reference detected: " + sym.getName() + " in " +
381+
toString(it.second) + " refers to " + toString(sym.file));
382+
}
383+
}
384+
376385
static uint8_t getMinVisibility(uint8_t va, uint8_t vb) {
377386
if (va == STV_DEFAULT)
378387
return vb;
@@ -509,9 +518,13 @@ void Symbol::resolveUndefined(const Undefined &other) {
509518

510519
// We don't report backward references to weak symbols as they can be
511520
// overridden later.
521+
//
522+
// A traditional linker does not error for -ldef1 -lref -ldef2 (linking
523+
// sandwich), where def2 may or may not be the same as def1. We don't want
524+
// to warn for this case, so dismiss the warning if we see a subsequent lazy
525+
// definition.
512526
if (backref && !isWeak())
513-
warn("backward reference detected: " + other.getName() + " in " +
514-
toString(other.file) + " refers to " + toString(file));
527+
backwardReferences.try_emplace(this, other.file);
515528
return;
516529
}
517530

@@ -668,8 +681,12 @@ void Symbol::resolveDefined(const Defined &other) {
668681
}
669682

670683
template <class LazyT> void Symbol::resolveLazy(const LazyT &other) {
671-
if (!isUndefined())
684+
if (!isUndefined()) {
685+
// See the comment in resolveUndefined().
686+
if (isDefined())
687+
backwardReferences.erase(this);
672688
return;
689+
}
673690

674691
// An undefined weak will not fetch archive members. See comment on Lazy in
675692
// Symbols.h for the details.

lld/ELF/Symbols.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "InputSection.h"
1818
#include "lld/Common/LLVM.h"
1919
#include "lld/Common/Strings.h"
20+
#include "llvm/ADT/DenseMap.h"
2021
#include "llvm/Object/Archive.h"
2122
#include "llvm/Object/ELF.h"
2223

@@ -556,6 +557,11 @@ void Symbol::replace(const Symbol &newSym) {
556557

557558
void maybeWarnUnorderableSymbol(const Symbol *sym);
558559
bool computeIsPreemptible(const Symbol &sym);
560+
void reportBackrefs();
561+
562+
// A mapping from a symbol to an InputFile referencing it backward. Used by
563+
// --warn-backrefs.
564+
extern llvm::DenseMap<const Symbol *, const InputFile *> backwardReferences;
559565

560566
} // namespace elf
561567
} // namespace lld

lld/test/ELF/warn-backrefs.s

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
# RUN: echo '.globl foo; foo:' | llvm-mc -filetype=obj -triple=x86_64 - -o %t2.o
55
# RUN: rm -f %t2.a
66
# RUN: llvm-ar rcs %t2.a %t2.o
7+
# RUN: ld.lld -shared %t2.o -o %t2.so
78

89
## A forward reference is accepted by a traditional Unix linker.
910
# RUN: ld.lld --fatal-warnings %t1.o %t2.a -o /dev/null
@@ -27,12 +28,16 @@
2728
# RUN: echo 'GROUP("%t2.a")' > %t3.lds
2829
# RUN: ld.lld --warn-backrefs %t3.lds %t1.o -o /dev/null 2>&1 | FileCheck %s
2930
# RUN: ld.lld --fatal-warnings --warn-backrefs '-(' %t3.lds %t1.o '-)' -o /dev/null
31+
## If a lazy definition appears after the backward reference, don't warn.
32+
# RUN: ld.lld --fatal-warnings --warn-backrefs %t3.lds %t1.o %t3.lds -o /dev/null
3033

3134
# CHECK: warning: backward reference detected: foo in {{.*}}1.o refers to {{.*}}2.a
3235

3336
## A backward reference from %t1.o to %t2.o
3437
# RUN: ld.lld --warn-backrefs --start-lib %t2.o --end-lib %t1.o -o /dev/null 2>&1 | \
3538
# RUN: FileCheck --check-prefix=OBJECT %s
39+
## If a lazy definition appears after the backward reference, don't warn.
40+
# RUN: ld.lld --fatal-warnings --warn-backrefs --start-lib %t2.o --end-lib %t1.o --start-lib %t2.o --end-lib -o /dev/null
3641

3742
# OBJECT: warning: backward reference detected: foo in {{.*}}1.o refers to {{.*}}2.o
3843

@@ -45,6 +50,18 @@
4550
# RUN: echo '.weak foo; foo:' | llvm-mc -filetype=obj -triple=x86_64 - -o %tweak.o
4651
# RUN: ld.lld --fatal-warnings --warn-backrefs --start-lib %tweak.o --end-lib %t1.o %t2.o -o /dev/null
4752

53+
## If a lazy definition appears after the backward reference, don't warn.
54+
## A traditional Unix linker will resolve the reference to the later definition.
55+
# RUN: ld.lld --fatal-warnings --warn-backrefs %t2.a %t1.o %t2.a -o /dev/null
56+
57+
## lld fetches the archive while GNU ld resolves the reference to the shared definition.
58+
## Warn because the resolution rules are different.
59+
# RUN: ld.lld --warn-backrefs %t2.a %t1.o %t2.so -o /dev/null 2>&1 | FileCheck %s
60+
61+
## This is a limitation. The resolution rules are different but
62+
## --warn-backrefs does not warn.
63+
# RUN: ld.lld --fatal-warnings --warn-backrefs %t2.a %t1.o %t2.so %t2.a -o /dev/null
64+
4865
.globl _start, foo
4966
_start:
5067
call foo

0 commit comments

Comments
 (0)