Index: lld/ELF/ICF.cpp =================================================================== --- lld/ELF/ICF.cpp +++ lld/ELF/ICF.cpp @@ -252,7 +252,7 @@ auto *DA = dyn_cast(&SA); auto *DB = dyn_cast(&SB); - if (!DA || !DB) + if (!DA || !DB || DA->ScriptDefined || DB->ScriptDefined) return false; // Relocations referring to absolute symbols are constant-equal if their Index: lld/ELF/LinkerScript.cpp =================================================================== --- lld/ELF/LinkerScript.cpp +++ lld/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: lld/ELF/Symbols.h =================================================================== --- lld/ELF/Symbols.h +++ lld/ELF/Symbols.h @@ -115,6 +115,9 @@ // True if this symbol is specified by --trace-symbol option. unsigned Traced : 1; + // True if this symbol is defined by a linker script. + unsigned ScriptDefined : 1; + bool includeInDynsym() const; uint8_t computeBinding() const; bool isWeak() const { return Binding == llvm::ELF::STB_WEAK; } @@ -157,9 +160,10 @@ Symbol(Kind K, InputFile *File, StringRefZ Name, uint8_t Binding, uint8_t StOther, uint8_t Type) : 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) {} + Type(Type), StOther(StOther), SymbolKind(K), ScriptDefined(false), + NeedsPltAddr(false), IsInIplt(false), IsInIgot(false), + IsPreemptible(false), Used(!Config->GcSections), + NeedsTocRestore(false) {} public: // True the symbol should point to its PLT entry. @@ -371,6 +375,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: lld/test/ELF/linkerscript/Inputs/icf.script =================================================================== --- /dev/null +++ lld/test/ELF/linkerscript/Inputs/icf.script @@ -0,0 +1,2 @@ +foo = 1; +bar = 2; Index: lld/test/ELF/linkerscript/icf.s =================================================================== --- /dev/null +++ lld/test/ELF/linkerscript/icf.s @@ -0,0 +1,10 @@ +# REQUIRES: x86 + +# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o +# RUN: ld.lld %t.o %S/Inputs/icf.script -o %t --icf=all --print-icf-sections | count 0 + +.section .text.foo,"ax",@progbits +jmp foo + +.section .text.bar,"ax",@progbits +jmp bar