Index: ELF/ICF.cpp =================================================================== --- ELF/ICF.cpp +++ ELF/ICF.cpp @@ -252,7 +252,10 @@ auto *DA = dyn_cast(&SA); auto *DB = dyn_cast(&SB); - if (!DA || !DB) + + // Placeholder symbols generated by linker scripts look the same now but + // may have different values later. + if (!DA || !DB || DA->ScriptDefined || DB->ScriptDefined) return false; // Relocations referring to absolute symbols are constant-equal if their Index: ELF/LinkerScript.cpp =================================================================== --- ELF/LinkerScript.cpp +++ ELF/LinkerScript.cpp @@ -209,6 +209,7 @@ STT_NOTYPE, 0, 0, nullptr); Cmd->Sym = cast(Sym); Cmd->Provide = false; + Sym->ScriptDefined = true; } // This method is used to handle INSERT AFTER statement. Here we rebuild Index: ELF/Symbols.h =================================================================== --- ELF/Symbols.h +++ ELF/Symbols.h @@ -164,7 +164,8 @@ : File(File), NameData(Name.Data), NameSize(Name.Size), Binding(Binding), Type(Type), StOther(StOther), SymbolKind(K), NeedsPltAddr(false), IsInIplt(false), IsInIgot(false), IsPreemptible(false), - Used(!Config->GcSections), NeedsTocRestore(false) {} + Used(!Config->GcSections), NeedsTocRestore(false), + ScriptDefined(false) {} public: // True the symbol should point to its PLT entry. @@ -187,6 +188,9 @@ // PPC64 toc pointer. unsigned NeedsTocRestore : 1; + // True if this symbol is defined by a linker script. + unsigned ScriptDefined : 1; + // The Type field may also have this value. It means that we have not yet seen // a non-Lazy symbol with this name, so we don't know what its type is. The // Type field is normally set to this value for Lazy symbols unless we saw a @@ -376,6 +380,7 @@ S->ExportDynamic = Sym.ExportDynamic; S->CanInline = Sym.CanInline; S->Traced = Sym.Traced; + S->ScriptDefined = Sym.ScriptDefined; // Print out a log message if --trace-symbol was specified. // This is for debugging. Index: test/ELF/linkerscript/icf.s =================================================================== --- test/ELF/linkerscript/icf.s +++ test/ELF/linkerscript/icf.s @@ -0,0 +1,11 @@ +# REQUIRES: x86 + +# RUN: echo "foo = 1; bar = 2;" > %t.script +# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o +# RUN: ld.lld %t.o %t.script -o %t --icf=all --print-icf-sections | count 0 + +.section .text.foo,"ax",@progbits +jmp foo + +.section .text.bar,"ax",@progbits +jmp bar