implement /OPT:ICF

Add identical COMDAT folding using a color-refinement over foldable
read-only sections. Hash section contents, relocation targets, and target
colors until partitions stabilize, then redirect folded COMDAT symlinks
and discard duplicate sections.

Honor .llvm_addrsig via /LLVM_ADDRSIG so address-significant sections
are not folded, keep separate color space for code, unwind info, and
MSVC vftables, and update COMDAT symbol/section mapping after folding.

Add linker tests covering function folding, relocation-sensitive folds,
alignment, color-space separation, symlink chains, .llvm_addrsig,
pdata/xdata, and C++ ICF cases.
This commit is contained in:
Nikita Smith authored and Ryan Fleury committed 2026-07-27 14:47:08 -07:00
1 parent 1ea9fae835
commit d34e1a7802
11 files changed
+1869 -153

No files matched your search

+8
View File
@@ -24,6 +24,7 @@ global read_only LNK_CmdSwitch g_cmd_switch_map[] =
{ LNK_CmdSwitch_FailIfMismatch, 1, "FAILIFMISMATCH", "{id=value}", "Fails to link if same ids have conflicting values." },
{ LNK_CmdSwitch_FileAlign, 0, "FILEALIGN", ":#", "Set section alignment in the file." },
{ LNK_CmdSwitch_Fixed, 0, "FIXED", "[:NO]", "Load the image at the default base address." },
{ LNK_CmdSwitch_Force, 0, "FORCE", "", "Force image output despite errors." },
{ LNK_CmdSwitch_FunctionPadMin, 0, "FUNCTIONPADMIN", ":#", "Minimum function byte size." },
{ LNK_CmdSwitch_Heap, 0, "HEAP", "RESERVE[,COMMIT]", "Set reserve and commit size for the heap." },
{ LNK_CmdSwitch_HighEntropyVa, 0, "HIGHENTROPYVA", "[:NO]", "Indicate that image supports full 64-bit address space ASLR." },
@@ -106,6 +107,8 @@ global read_only LNK_CmdSwitch g_cmd_switch_map[] =
{ LNK_CmdSwitch_RadTypeServer, 0, "RAD_TYPE_SERVER", ":FILENAME", "Merge types and store them in the specified file. The filename must have the .rrt extension." },
{ LNK_CmdSwitch_LLVM_AddrSig, 0, "LLVM_ADDRSIG", "[:NO]", "Use .llvm_addrsig to guide ICF." },
{ LNK_CmdSwitch_Help, 0, "HELP", "", "" },
{ LNK_CmdSwitch_Help, 0, "?", "", "" },
};
@@ -2179,6 +2182,10 @@ lnk_apply_cmd_option_to_config(LNK_Config *config, String8 cmd_name, String8List
lnk_error_cmd_switch(LNK_Error_Cmdl, obj, cmd_switch, "missing type server file path");
}
} break;
case LNK_CmdSwitch_LLVM_AddrSig: {
lnk_cmd_switch_parse_flag(obj, cmd_switch, value_strings, &config->llvm_addrsig);
} break;
}
scratch_end(scratch);
@@ -2208,6 +2215,7 @@ lnk_config_init(LNK_CmdLine cmd_line)
config->arena = arena;
config->raw_cmd_line = str8_list_copy(arena, &cmd_line.raw_cmd_line);
config->work_dir = get_current_path(arena);
config->force = lnk_cmd_line_has_switch(cmd_line, LNK_CmdSwitch_Force);
// apply command line switches
for EachNode(cmd, LNK_CmdOption, cmd_line.first_option) {