Index: lld/ELF/ICF.cpp =================================================================== --- lld/ELF/ICF.cpp +++ lld/ELF/ICF.cpp @@ -462,9 +462,11 @@ forEachClassRange(0, Sections.size(), [&](size_t Begin, size_t End) { if (End - Begin == 1) return; - InputSection *Target = nullptr; - bool SeenUnique = false, Replaced = false; - for (size_t I = Begin; I < End; ++I) { + + bool SeenUnique = Sections[Begin]->KeepUnique; + bool Replaced = false; + + for (size_t I = Begin + 1; I < End; ++I) { // We aren't allowed to merge two KeepUnique sections as this would break // the guarantees provided by --keep-unique and --icf=safe, but there's // no reason why we can't merge a non-KeepUnique section with a KeepUnique @@ -476,14 +478,11 @@ continue; SeenUnique = true; } - if (!Target) { - Target = Sections[I]; - continue; - } + if (!Replaced) - print("selected section " + toString(Target)); + print("selected section " + toString(Sections[Begin])); print(" removing identical section " + toString(Sections[I])); - Target->replace(Sections[I]); + Sections[Begin]->replace(Sections[I]); Replaced = true; // At this point we know sections merged are fully identical and hence