Skip to content

Commit 7176f8a

Browse files
committed
[ELF] Enhance --no-allow-shlib-undefined to report non-exported definition
For a DSO with all DT_NEEDED entries accounted for, if it contains an undefined non-weak symbol that shares a name with a non-exported definition (hidden visibility or localized by a version script), and there is no DSO definition, we should also report an error. Because the definition is not exported, it cannot resolve the DSO reference at runtime. GNU ld introduced this error-checking in [April 2003](https://sourceware.org/pipermail/binutils/2003-April/026568.html). The feature is available for executable links but not for -shared, and it is orthogonal to --no-allow-shlib-undefined. We make the feature part of --no-allow-shlib-undefined and work with -shared when --no-allow-shlib-undefined is specified. A subset of this error-checking is covered by commit 1981b1b for --gc-sections discarded sections. This patch covers non-discarded sections as well. Internally, I have identified 2 bugs (which would fail with LD_BIND_NOW=1) covered by commit 1981b1b
1 parent af59938 commit 7176f8a

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

lld/ELF/Writer.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2023,10 +2023,16 @@ template <class ELFT> void Writer<ELFT>::finalizeSections() {
20232023
});
20242024
if (!allNeededIsKnown)
20252025
continue;
2026-
for (Symbol *sym : file->requiredSymbols)
2027-
if (sym->isUndefined() && !sym->isWeak())
2026+
for (Symbol *sym : file->requiredSymbols) {
2027+
if (sym->isUndefined() && !sym->isWeak()) {
20282028
diagnose("undefined reference due to --no-allow-shlib-undefined: " +
20292029
toString(*sym) + "\n>>> referenced by " + toString(file));
2030+
} else if (sym->isDefined() && sym->computeBinding() == STB_LOCAL) {
2031+
diagnose("non-exported symbol '" + toString(*sym) + "' in '" +
2032+
toString(sym->file) + "' is referenced by DSO '" +
2033+
toString(file) + "'");
2034+
}
2035+
}
20302036
}
20312037
}
20322038
}

lld/test/ELF/allow-shlib-undefined.s

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@
3030
# RUN: not ld.lld main.o a.so empty.so b.so -o /dev/null 2>&1 | FileCheck %s --check-prefix=CHECK2
3131

3232
## Test some cases when a relocatable object file provides a non-exported definition.
33-
# RUN: ld.lld main.o a.so def-hidden.o --fatal-warnings -o /dev/null
34-
# RUN: ld.lld main.o a.so def-hidden.o -shared --no-allow-shlib-undefined --fatal-warnings -o /dev/null
33+
# RUN: not ld.lld main.o a.so def-hidden.o -o /dev/null 2>&1 | FileCheck %s --check-prefix=NONEXPORTED
34+
# RUN: not ld.lld main.o a.so def-hidden.o -shared --no-allow-shlib-undefined -o /dev/null 2>&1 | FileCheck %s --check-prefix=NONEXPORTED
3535
# RUN: ld.lld main.o a.so def-hidden.o --allow-shlib-undefined --fatal-warnings -o /dev/null
3636
## Test a relocatable object file definition that is converted to STB_LOCAL.
37-
# RUN: ld.lld main.o a.so def-hidden.o --version-script=local.ver --fatal-warnings -o /dev/null
37+
# RUN: not ld.lld main.o a.so def-hidden.o --version-script=local.ver -o /dev/null 2>&1 | FileCheck %s --check-prefix=NONEXPORTED
3838

3939
## The section containing the definition is discarded, and we report an error.
4040
# RUN: not ld.lld --gc-sections main.o a.so def-hidden.o -o /dev/null 2>&1 | FileCheck %s
@@ -57,6 +57,10 @@
5757
# WARN: warning: undefined reference due to --no-allow-shlib-undefined: x1
5858
# WARN-NEXT: >>> referenced by a.so
5959

60+
# NONEXPORTED-NOT: error:
61+
# NONEXPORTED: error: non-exported symbol 'x1' in 'def-hidden.o' is referenced by DSO 'a.so'
62+
# NONEXPORTED-NOT: {{.}}
63+
6064
#--- main.s
6165
.globl _start
6266
_start:

0 commit comments

Comments
 (0)