[lld/mac] Crash even less on undefined symbols with --icf=all
authorNico Weber <thakis@chromium.org>
Thu, 18 Nov 2021 21:59:21 +0000 (16:59 -0500)
committerNico Weber <thakis@chromium.org>
Fri, 19 Nov 2021 14:23:19 +0000 (09:23 -0500)
Follow-up to https://reviews.llvm.org/D112643. Even after that change, we were
still asserting if two separate functions that are eligible for ICF (same size,
same data, same number of relocs, same reloc types, ...) referred to
Undefineds. This fixes that oversight.

Differential Revision: https://reviews.llvm.org/D114195

lld/MachO/ICF.cpp
lld/test/MachO/invalid/undefined-symbol.s

index 8802f29..f9dea4b 100644 (file)
@@ -114,7 +114,8 @@ static bool equalsConstant(const ConcatInputSection *ia,
       if (sa->kind() != sb->kind())
         return false;
       if (!isa<Defined>(sa)) {
-        assert(isa<DylibSymbol>(sa));
+        // ICF runs before Undefineds are reported.
+        assert(isa<DylibSymbol>(sa) || isa<Undefined>(sa));
         return sa == sb;
       }
       const auto *da = cast<Defined>(sa);
@@ -275,7 +276,7 @@ void ICF::run() {
             } else {
               hash += defined->value;
             }
-          } else if (!isa<Undefined>(sym))
+          } else if (!isa<Undefined>(sym)) // ICF runs before Undefined diags.
             llvm_unreachable("foldIdenticalSections symbol kind");
         }
       }
index deb543a..97a3004 100644 (file)
@@ -20,9 +20,17 @@ _foo:
   retq
 
 #--- main.s
-.globl _main
 .text
+
+_anotherref:
+  callq _foo
+  movq $0, %rax
+  retq
+
+.globl _main
 _main:
   callq _foo
   movq $0, %rax
   retq
+
+.subsections_via_symbols