Index: lld/ELF/Config.h =================================================================== --- lld/ELF/Config.h +++ lld/ELF/Config.h @@ -40,6 +40,9 @@ // For --discard-{all,locals,none}. enum class DiscardPolicy { Default, All, Locals, None }; +// For --icf={none,safe,all}. +enum class ICFLevel { None, Safe, All }; + // For --strip-{all,debug}. enum class StripPolicy { None, All, Debug }; @@ -137,7 +140,6 @@ bool GnuUnique; bool HasDynamicList = false; bool HasDynSymTab; - bool ICF; bool IgnoreDataAddressEquality; bool IgnoreFunctionAddressEquality; bool LTODebugPassManager; @@ -183,6 +185,7 @@ bool ZRetpolineplt; bool ZWxneeded; DiscardPolicy Discard; + ICFLevel ICF; OrphanHandlingPolicy OrphanHandling; SortSectionPolicy SortSection; StripPolicy Strip; Index: lld/ELF/Driver.cpp =================================================================== --- lld/ELF/Driver.cpp +++ lld/ELF/Driver.cpp @@ -51,6 +51,7 @@ #include "llvm/ADT/StringSwitch.h" #include "llvm/Support/CommandLine.h" #include "llvm/Support/Compression.h" +#include "llvm/Support/LEB128.h" #include "llvm/Support/Path.h" #include "llvm/Support/TarWriter.h" #include "llvm/Support/TargetSelect.h" @@ -294,7 +295,7 @@ error("-r and -shared may not be used together"); if (Config->GcSections) error("-r and --gc-sections may not be used together"); - if (Config->ICF) + if (Config->ICF != ICFLevel::None) error("-r and --icf may not be used together"); if (Config->Pie) error("-r and -pie may not be used together"); @@ -497,6 +498,15 @@ return Arg->getValue(); } +static ICFLevel getICF(opt::InputArgList &Args) { + auto *Arg = Args.getLastArg(OPT_icf_none, OPT_icf_safe, OPT_icf_all); + if (!Arg || Arg->getOption().getID() == OPT_icf_none) + return ICFLevel::None; + if (Arg->getOption().getID() == OPT_icf_safe) + return ICFLevel::Safe; + return ICFLevel::All; +} + static StripPolicy getStrip(opt::InputArgList &Args) { if (Args.hasArg(OPT_relocatable)) return StripPolicy::None; @@ -710,7 +720,7 @@ Config->GcSections = Args.hasFlag(OPT_gc_sections, OPT_no_gc_sections, false); Config->GnuUnique = Args.hasFlag(OPT_gnu_unique, OPT_no_gnu_unique, true); Config->GdbIndex = Args.hasFlag(OPT_gdb_index, OPT_no_gdb_index, false); - Config->ICF = Args.hasFlag(OPT_icf_all, OPT_icf_none, false); + Config->ICF = getICF(Args); Config->IgnoreDataAddressEquality = Args.hasArg(OPT_ignore_data_address_equality); Config->IgnoreFunctionAddressEquality = @@ -1185,16 +1195,60 @@ } } +static bool keepUnique(Symbol *S) { + if (auto *D = dyn_cast_or_null(S)) { + if (D->Section) { + D->Section->KeepUnique = true; + return true; + } + } + return false; +} + // Record sections that define symbols mentioned in --keep-unique -// these sections are inelligible for ICF. +// and symbols referred to by address-significance tables. These sections are +// ineligible for ICF. +template static void findKeepUniqueSections(opt::InputArgList &Args) { for (auto *Arg : Args.filtered(OPT_keep_unique)) { StringRef Name = Arg->getValue(); - if (auto *Sym = dyn_cast_or_null(Symtab->find(Name))) - Sym->Section->KeepUnique = true; - else + if (!keepUnique(Symtab->find(Name))) warn("could not find symbol " + Name + " to keep unique"); } + + if (Config->ICF == ICFLevel::Safe) { + // Symbols in the dynsym could be address-significant in other executables + // or DSOs, so we conservatively mark them as address-significant. + for (Symbol *S : Symtab->getSymbols()) + if (S->includeInDynsym()) + keepUnique(S); + + // Visit the address-significance table in each object file and mark each + // referenced symbol as address-significant. + for (InputFile *F : ObjectFiles) { + auto *Obj = cast>(F); + ArrayRef Syms = Obj->getSymbols(); + if (Obj->AddrsigSec) { + ArrayRef Contents = + check(Obj->getObj().getSectionContents(Obj->AddrsigSec)); + const uint8_t *Cur = Contents.begin(); + while (Cur != Contents.end()) { + unsigned Size; + const char *Err; + uint64_t SymIndex = decodeULEB128(Cur, &Size, Contents.end(), &Err); + if (Err) + fatal(toString(F) + ": could not decode addrsig section: " + Err); + keepUnique(Syms[SymIndex]); + Cur += Size; + } + } else { + // If an object file does not have an address-significance table, + // conservatively mark all of its symbols as address-significant. + for (Symbol *S : Syms) + keepUnique(S); + } + } + } } // Do actual linking. Note that when this function is called, @@ -1369,8 +1423,8 @@ markLive(); demoteSymbols(); mergeSections(); - if (Config->ICF) { - findKeepUniqueSections(Args); + if (Config->ICF != ICFLevel::None) { + findKeepUniqueSections(Args); doIcf(); } Index: lld/ELF/ICF.cpp =================================================================== --- lld/ELF/ICF.cpp +++ lld/ELF/ICF.cpp @@ -173,8 +173,9 @@ return false; // Don't merge read only data sections unless - // --ignore-data-address-equality was passed. - if (!(S->Flags & SHF_EXECINSTR) && !Config->IgnoreDataAddressEquality) + // --ignore-data-address-equality or --icf=safe was passed. + if (!(S->Flags & SHF_EXECINSTR) && + !(Config->IgnoreDataAddressEquality || Config->ICF == ICFLevel::Safe)) return false; // Don't merge synthetic sections as their Data member is not valid and empty. Index: lld/ELF/InputFiles.h =================================================================== --- lld/ELF/InputFiles.h +++ lld/ELF/InputFiles.h @@ -210,6 +210,9 @@ // symbol table. StringRef SourceFile; + // Pointer to this input file's .llvm_addrsig section, if it has one. + const Elf_Shdr *AddrsigSec = nullptr; + private: void initializeSections(llvm::DenseSet &ComdatGroups); Index: lld/ELF/InputFiles.cpp =================================================================== --- lld/ELF/InputFiles.cpp +++ lld/ELF/InputFiles.cpp @@ -428,6 +428,17 @@ // if -r is given, we'll let the final link discard such sections. // This is compatible with GNU. if ((Sec.sh_flags & SHF_EXCLUDE) && !Config->Relocatable) { + if (Sec.sh_type == SHT_LLVM_ADDRSIG) { + // We ignore the address-significance table if we know that the object + // file was created by objcopy or ld -r. This is because these tools + // will reorder the symbols in the symbol table, invalidating the data + // in the address-significance table, which refers to symbols by index. + if (Sec.sh_link != 0) + this->AddrsigSec = &Sec; + else if (Config->ICF == ICFLevel::Safe) + warn(toString(this) + ": --icf=safe is incompatible with object " + "files created using objcopy or ld -r"); + } this->Sections[I] = &InputSection::Discarded; continue; } Index: lld/ELF/Options.td =================================================================== --- lld/ELF/Options.td +++ lld/ELF/Options.td @@ -170,6 +170,8 @@ def icf_all: F<"icf=all">, HelpText<"Enable identical code folding">; +def icf_safe: F<"icf=safe">, HelpText<"Enable safe identical code folding">; + def icf_none: F<"icf=none">, HelpText<"Disable identical code folding (default)">; def ignore_function_address_equality: F<"ignore-function-address-equality">, Index: lld/test/ELF/Inputs/icf-safe.s =================================================================== --- /dev/null +++ lld/test/ELF/Inputs/icf-safe.s @@ -0,0 +1,9 @@ +.section .text.non_addrsig1,"ax",@progbits +.globl non_addrsig1 +non_addrsig1: +ret + +.section .text.non_addrsig2,"ax",@progbits +.globl non_addrsig2 +non_addrsig2: +ret Index: lld/test/ELF/icf-safe.s =================================================================== --- /dev/null +++ lld/test/ELF/icf-safe.s @@ -0,0 +1,77 @@ +# REQUIRES: x86 + +# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t1.o +# RUN: llvm-objcopy %t1.o %t1copy.o +# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %S/Inputs/icf-safe.s -o %t2.o +# RUN: ld.lld %t1.o %t2.o -o %t2 --icf=safe --print-icf-sections | FileCheck %s +# RUN: ld.lld %t1.o %t2.o -o %t3 --icf=safe --print-icf-sections -shared | FileCheck --check-prefix=SHARED %s +# RUN: ld.lld %t1copy.o -o %t4 --icf=safe 2>&1 | FileCheck --check-prefix=OBJCOPY %s + +# CHECK-NOT: selected section {{.*}}:(.text.f1) +# CHECK: selected section {{.*}}:(.text.f3) +# CHECK: removing identical section {{.*}}:(.text.f4) + +# CHECK-NOT: selected section {{.*}}:(.rodata.g1) +# CHECK: selected section {{.*}}:(.rodata.g3) +# CHECK: removing identical section {{.*}}:(.rodata.g4) + +# CHECK-NOT: selected section {{.*}}:(.text.non_addrsig{{.}}) + +# llvm-mc normally emits an empty .text section into every object file. Since +# nothing actually refers to it via a relocation, it doesn't have any associated +# symbols (thus nor can anything refer to it via a relocation, making it safe to +# merge with the empty section in the other input file). Here we check that the +# only two sections merged are the two empty sections. The dynsym entries should +# have prevented anything else from being merged. +# SHARED-NOT: selected section +# SHARED: selected section {{.*}}:(.text) +# SHARED: removing identical section {{.*}}:(.text) +# SHARED-NOT: selected section + +# OBJCOPY: --icf=safe is incompatible with object files created using objcopy or ld -r + +.section .text.f1,"ax",@progbits +.globl f1 +f1: +ret + +.section .text.f2,"ax",@progbits +.globl f2 +f2: +ret + +.section .text.f3,"ax",@progbits +.globl f3 +f3: +ud2 + +.section .text.f4,"ax",@progbits +.globl f4 +f4: +ud2 + +.section .rodata.g1,"a",@progbits +.globl g1 +g1: +.byte 1 + +.section .rodata.g2,"a",@progbits +.globl g2 +g2: +.byte 1 + +.section .rodata.g3,"a",@progbits +.globl g3 +g3: +.byte 2 + +.section .rodata.g4,"a",@progbits +.globl g4 +g4: +.byte 2 + +.addrsig +.addrsig_sym f1 +.addrsig_sym f2 +.addrsig_sym g1 +.addrsig_sym g2