Skip to content

Commit 84a2155

Browse files
committed
[lld-macho]Limit "cannot-export-hidden-symbol" warnings to only 3 to avoid crowding logs.
Details: We often use wildcard symbols in the exported_symbols list, and sometimes they match autohide symbols, which triggers these "cannot export hidden symbols" warnings that can be a bit noisy. It'd be more user-friendly if LLD could truncate these. Differential Revision: https://reviews.llvm.org/D159095
1 parent 4792ae5 commit 84a2155

File tree

2 files changed

+66
-3
lines changed

2 files changed

+66
-3
lines changed

lld/MachO/Driver.cpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1355,8 +1355,10 @@ static void createAliases() {
13551355
}
13561356

13571357
static void handleExplicitExports() {
1358+
static constexpr int kMaxWarnings = 3;
13581359
if (config->hasExplicitExports) {
1359-
parallelForEach(symtab->getSymbols(), [](Symbol *sym) {
1360+
std::atomic<uint64_t> warningsCount{0};
1361+
parallelForEach(symtab->getSymbols(), [&warningsCount](Symbol *sym) {
13601362
if (auto *defined = dyn_cast<Defined>(sym)) {
13611363
if (config->exportedSymbols.match(sym->getName())) {
13621364
if (defined->privateExtern) {
@@ -1367,8 +1369,12 @@ static void handleExplicitExports() {
13671369
// The former can be exported but the latter cannot.
13681370
defined->privateExtern = false;
13691371
} else {
1370-
warn("cannot export hidden symbol " + toString(*defined) +
1371-
"\n>>> defined in " + toString(defined->getFile()));
1372+
// Only print the first 3 warnings verbosely, and
1373+
// shorten the rest to avoid crowding logs.
1374+
if (warningsCount.fetch_add(1, std::memory_order_relaxed) <
1375+
kMaxWarnings)
1376+
warn("cannot export hidden symbol " + toString(*defined) +
1377+
"\n>>> defined in " + toString(defined->getFile()));
13721378
}
13731379
}
13741380
} else {
@@ -1378,6 +1384,9 @@ static void handleExplicitExports() {
13781384
dysym->shouldReexport = config->exportedSymbols.match(sym->getName());
13791385
}
13801386
});
1387+
if (warningsCount > kMaxWarnings)
1388+
warn("<... " + Twine(warningsCount - kMaxWarnings) +
1389+
" more similar warnings...>");
13811390
} else if (!config->unexportedSymbols.empty()) {
13821391
parallelForEach(symtab->getSymbols(), [](Symbol *sym) {
13831392
if (auto *defined = dyn_cast<Defined>(sym))

lld/test/MachO/export-options.s

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
# RUN: echo "" | llvm-mc -filetype=obj -triple=x86_64-apple-macos -o %t/empty.o
55
# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-macos %t/default.s -o %t/default.o
66
# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-macos %t/lazydef.s -o %t/lazydef.o
7+
# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-macos %t/too-many-warnings.s -o %t/too-many-warnings.o
78
# RUN: llvm-ar --format=darwin rcs %t/lazydef.a %t/lazydef.o
89

910
## Check that mixing exported and unexported symbol options yields an error
@@ -209,6 +210,17 @@
209210
# RUN: llvm-objdump --macho --exports-trie %t/unexport-dylib | FileCheck %s \
210211
# RUN: --check-prefix=EMPTY-TRIE
211212

213+
## Check that warnings are truncated to the first 3 only.
214+
# RUN: %no-fatal-warnings-lld -dylib %t/too-many-warnings.o -o %t/too-many.out \
215+
# RUN: -exported_symbol "_private_extern*" 2>&1 | \
216+
# RUN: FileCheck --check-prefix=TRUNCATE %s
217+
218+
# TRUNCATE: warning: cannot export hidden symbol _private_extern{{.+}}
219+
# TRUNCATE: warning: cannot export hidden symbol _private_extern{{.+}}
220+
# TRUNCATE: warning: cannot export hidden symbol _private_extern{{.+}}
221+
# TRUNCATE: warning: <... 7 more similar warnings...>
222+
# TRUNCATE-EMPTY:
223+
212224
#--- default.s
213225

214226
.globl _keep_globl, _hide_globl, _tlv
@@ -283,3 +295,45 @@ _foo:
283295
.private_extern _foo
284296
_foo:
285297
retq
298+
299+
#--- too-many-warnings.s
300+
.private_extern _private_extern1
301+
.private_extern _private_extern2
302+
.private_extern _private_extern3
303+
.private_extern _private_extern4
304+
.private_extern _private_extern5
305+
.private_extern _private_extern6
306+
.private_extern _private_extern7
307+
.private_extern _private_extern8
308+
.private_extern _private_extern9
309+
.private_extern _private_extern10
310+
311+
_private_extern1:
312+
retq
313+
314+
_private_extern2:
315+
retq
316+
317+
_private_extern3:
318+
retq
319+
320+
_private_extern4:
321+
retq
322+
323+
_private_extern5:
324+
retq
325+
326+
_private_extern6:
327+
retq
328+
329+
_private_extern7:
330+
retq
331+
332+
_private_extern8:
333+
retq
334+
335+
_private_extern9:
336+
retq
337+
338+
_private_extern10:
339+
retq

0 commit comments

Comments
 (0)