[PATCH] D150637: [lld][ELF] Add option for suppressing section type mismatch warnings

Leonard Chan via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon May 15 22:28:41 PDT 2023

leonardchan created this revision.
leonardchan added reviewers: phosek, MaskRay.
leonardchan added a project: lld.
Herald added subscribers: arichardson, emaste.
Herald added a project: All.
leonardchan requested review of this revision.
Herald added a project: LLVM.

Usually when seeing this warning, the best approach would be to ensure the input section has the same type as the output section. However, if the input section comes from something like a prebuilt library, updating it won't be very quick/easy. To prevent potentially spamming the user with these warnings, having a toggle would be nice for temporarily suppressing them.

  rG LLVM Github Monorepo



Index: lld/test/ELF/linkerscript/noload.s
--- lld/test/ELF/linkerscript/noload.s
+++ lld/test/ELF/linkerscript/noload.s
@@ -21,8 +21,11 @@
 ## Issue a warning. See https://github.com/ClangBuiltLinux/linux/issues/1597
 # RUN: ld.lld --script %t/lds %t.o %t/mismatch.o -o %t/out 2>&1 | FileCheck %s --check-prefix=WARN
 # RUN: llvm-readelf -S -l %t/out | FileCheck %s --check-prefix=CHECK2
+# RUN: ld.lld --script %t/lds %t.o %t/mismatch.o -o %t/out --warn-section-type-mismatch 2>&1 | FileCheck %s --check-prefix=WARN
+# RUN: ld.lld --script %t/lds %t.o %t/mismatch.o -o %t/out --no-warn-section-type-mismatch 2>&1 | FileCheck %s --check-prefix=NOWARN
-# WARN:   warning: section type mismatch for .data_noload_a
+# WARN:         warning: section type mismatch for .data_noload_a
+# NOWARN-NOT:   warning: section type mismatch for .data_noload_a
 # CHECK2:      Name                 Type     Address          Off               Size
 # CHECK2:      .data_noload_a       NOBITS   0000000000000000 [[OFF:[0-9a-f]+]] 001001
Index: lld/ELF/OutputSections.cpp
--- lld/ELF/OutputSections.cpp
+++ lld/ELF/OutputSections.cpp
@@ -114,8 +114,9 @@
 void OutputSection::commitSection(InputSection *isec) {
   if (LLVM_UNLIKELY(type != isec->type)) {
     if (hasInputSections || typeIsSet) {
-      if (typeIsSet || !canMergeToProgbits(type) ||
-          !canMergeToProgbits(isec->type)) {
+      if ((typeIsSet || !canMergeToProgbits(type) ||
+           !canMergeToProgbits(isec->type)) &&
+          config->warnSectionTypeMismatch) {
         // Changing the type of a (NOLOAD) section is fishy, but some projects
         // (e.g. https://github.com/ClangBuiltLinux/linux/issues/1597)
         // traditionally rely on the behavior. Issue a warning to not break
Index: lld/ELF/Options.td
--- lld/ELF/Options.td
+++ lld/ELF/Options.td
@@ -502,6 +502,10 @@
 def warn_unresolved_symbols: F<"warn-unresolved-symbols">,
   HelpText<"Report unresolved symbols as warnings">;
+defm warn_section_type_mismatch: BB<"warn-section-type-mismatch",
+  "Warn about merging of sections with different types (default)",
+  "Do not warn about merging of sections with different types">;
 defm whole_archive: B<"whole-archive",
     "Force load of all members in a static library",
     "Do not force load of all members in a static library (default)">;
Index: lld/ELF/Driver.cpp
--- lld/ELF/Driver.cpp
+++ lld/ELF/Driver.cpp
@@ -1311,6 +1311,8 @@
       OPT_use_android_relr_tags, OPT_no_use_android_relr_tags, false);
   config->warnBackrefs =
       args.hasFlag(OPT_warn_backrefs, OPT_no_warn_backrefs, false);
+  config->warnSectionTypeMismatch = args.hasFlag(
+      OPT_warn_section_type_mismatch, OPT_no_warn_section_type_mismatch, true);
   config->warnCommon = args.hasFlag(OPT_warn_common, OPT_no_warn_common, false);
   config->warnSymbolOrdering =
       args.hasFlag(OPT_warn_symbol_ordering, OPT_no_warn_symbol_ordering, true);
Index: lld/ELF/Config.h
--- lld/ELF/Config.h
+++ lld/ELF/Config.h
@@ -276,6 +276,7 @@
   bool unique;
   bool useAndroidRelrTags = false;
   bool warnBackrefs;
+  bool warnSectionTypeMismatch;
   llvm::SmallVector<llvm::GlobPattern, 0> warnBackrefsExclude;
   bool warnCommon;
   bool warnMissingEntry;

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D150637.522435.patch
Type: text/x-patch
Size: 3569 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230516/cbcff182/attachment.bin>

More information about the llvm-commits mailing list