diff --git a/lld/MachO/Driver.cpp b/lld/MachO/Driver.cpp --- a/lld/MachO/Driver.cpp +++ b/lld/MachO/Driver.cpp @@ -1354,7 +1354,7 @@ } } -static void handleExplicitExports() { +static void handleExplicitExports(bool warnExportHidden) { if (config->hasExplicitExports) { parallelForEach(symtab->getSymbols(), [](Symbol *sym) { if (auto *defined = dyn_cast(sym)) { @@ -1366,7 +1366,7 @@ // it is explicitly exported. // The former can be exported but the latter cannot. defined->privateExtern = false; - } else { + } else if (warnExportHidden) { warn("cannot export hidden symbol " + toString(*defined) + "\n>>> defined in " + toString(defined->getFile())); } @@ -1903,10 +1903,12 @@ addSynthenticMethnames(); createAliases(); + const bool warnExportHidden = + !args.hasFlag(OPT_no_warn_export_hidden, false); // If we are in "explicit exports" mode, hide everything that isn't // explicitly exported. Do this before running LTO so that LTO can better // optimize. - handleExplicitExports(); + handleExplicitExports(warnExportHidden); bool didCompileBitcodeFiles = compileBitcodeFiles(); @@ -1923,7 +1925,7 @@ // cross-module references to hidden symbols under ThinLTO. Thus, if we // compiled any bitcode files, we must redo the symbol hiding. if (didCompileBitcodeFiles) - handleExplicitExports(); + handleExplicitExports(warnExportHidden); replaceCommonSymbols(); StringRef orderFile = args.getLastArgValue(OPT_order_file); diff --git a/lld/MachO/Options.td b/lld/MachO/Options.td --- a/lld/MachO/Options.td +++ b/lld/MachO/Options.td @@ -684,6 +684,10 @@ HelpText<"Suppress warnings for static initializers in the output">, Flags<[HelpHidden]>, Group; +def no_warn_export_hidden : Flag<["-"], "no_warn_export_hidden">, + HelpText<"Suppress warnings for exporting weakdef-can-be-hidden symbols in the output">, + Flags<[HelpHidden]>, + Group; def unaligned_pointers : Separate<["-"], "unaligned_pointers">, MetaVarName<"">, HelpText<"Handle unaligned pointers in __DATA segments according to : warning, error, or suppress (default for arm64e is error, otherwise suppress)">, diff --git a/lld/test/MachO/export-options.s b/lld/test/MachO/export-options.s --- a/lld/test/MachO/export-options.s +++ b/lld/test/MachO/export-options.s @@ -153,6 +153,9 @@ # RUN: not %lld -dylib -exported_symbol "_foo" %t/autohide-private-extern.o \ # RUN: -o /dev/null 2>&1 | FileCheck %s --check-prefix=AUTOHIDE-PRIVATE +## Test that the warning/error message can be suppressed. +# RUN: %lld -dylib -exported_symbol "_foo" %t/autohide-private-extern.o -no_warn_export_hidden -o %t/autohide-pe-no-warn.out + # RUN: not %lld -dylib -exported_symbol "_foo" %t/autohide.o \ # RUN: %t/glob-private-extern.o -o /dev/null 2>&1 | \ # RUN: FileCheck %s --check-prefix=AUTOHIDE-PRIVATE