From abd9807590fc10eb92eb22aea7b50dbf08db7e9d Mon Sep 17 00:00:00 2001 From: Fangrui Song Date: Fri, 5 Aug 2022 17:08:37 -0700 Subject: [PATCH] [ELF] mergeCmp: work around irreflexivity bug Some tests (e.g. aarch64-feature-pac.s) segfault in libstdc++ _GLIBCXX_DEBUG builds (enabled by LLVM_ENABLE_EXPENSIVE_CHECKS). dyn_cast is incorrectly true for any SyntheticSection. std::merge transitively calls mergeCmp(x, x) (due to __glibcxx_requires_irreflexive_pred) and will segfault in `ta->getTargetInputSection()`. The dyn_cast issue should be eventually fixed properly, bug `a != b` is robust enough for now. --- lld/ELF/Relocations.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lld/ELF/Relocations.cpp b/lld/ELF/Relocations.cpp index df33df4..b2e6499 100644 --- a/lld/ELF/Relocations.cpp +++ b/lld/ELF/Relocations.cpp @@ -1712,7 +1712,8 @@ static bool mergeCmp(const InputSection *a, const InputSection *b) { if (a->outSecOff < b->outSecOff) return true; - if (a->outSecOff == b->outSecOff) { + // FIXME dyn_cast is non-null for any SyntheticSection. + if (a->outSecOff == b->outSecOff && a != b) { auto *ta = dyn_cast(a); auto *tb = dyn_cast(b); -- 2.7.4