Skip to content

AD6006: EnableLinkTimeCodeGeneration

Summary

Property Value
ID AD6006
Name EnableLinkTimeCodeGeneration
Category Performance
Severity Warning
Applies to PE (Windows)

Description

Enabling Link Time Code Generation (LTCG) performs whole-program optimization, which is able to better optimize code across translation units. LTCG is also a prerequisite for Profile-Guided Optimization (PGO) which can further improve performance.

Why This Matters

Link Time Code Generation enables the most aggressive optimizations by giving the optimizer visibility into the entire program. This produces faster, smaller binaries with better security properties.

Whole-Program Visibility

Without LTCG:
  Compiler sees: file1.c only
  Can't inline functions from file2.c
  Can't prove file2.c functions are unused
  Conservative optimization

With LTCG:
  Linker sees: entire program
  Can inline across all files
  Can remove truly dead code
  Aggressive optimization

Optimization Opportunities

Optimization Without LTCG With LTCG
Cross-file inlining No Yes
Global dead code removal Limited Full
Interprocedural optimization No Yes
Devirtualization Limited Better
Register allocation Per-function Global

Security Benefits

LTCG improves security through:

1. Better dead code elimination
   → Fewer unused functions with bugs
   → Fewer ROP gadgets

2. Inlining security-critical functions
   → Removes call sites attackers could target
   → Enables further optimization

3. Devirtualization
   → Fewer indirect calls
   → Smaller CFG attack surface

CFI Enhancement

Control Flow Integrity works better with LTCG:

Without LTCG:
  CFI sees indirect calls to unknown targets
  Must allow all possible callees
  Conservative (weaker) protection

With LTCG:
  CFI sees actual call graph
  Can restrict to actual callees
  Precise (stronger) protection

Performance Impact

Metric Typical Improvement
Execution speed 5-20%
Binary size 5-15% smaller
Build time 2-5x longer
Memory (link) Higher

Build time increases significantly but runtime improves.

PGO Prerequisite

LTCG enables Profile-Guided Optimization:

Build chain for maximum performance:
  1. LTCG build (instrumented)
  2. Run with representative workload
  3. PGO build using profile data

Result: Optimal branch predictions, cache layout

LTCG provides significant benefits:

  1. Cross-Module Optimization: Functions can be inlined across compilation units
  2. Better Register Allocation: Optimizer sees the whole program
  3. Dead Code Elimination: Unused code from all modules can be removed
  4. COMDAT Folding: Identical functions across modules can be merged
  5. Security through Performance: Faster code may reduce timing attack surface

Detection Method

This rule checks for: - Presence of .ltcg or similar LTCG-specific sections - Other indicators that whole-program optimization was performed

Note: Detection of LTCG can be challenging as it modifies the code at link time without always leaving obvious markers.

Resolution

For Command Line Builds

Add /LTCG to the linker command line and /GL to the compiler:

cl /GL /O2 myfile.cpp
link /LTCG myfile.obj

For Visual Studio Projects

  1. Open Project Properties
  2. Navigate to Configuration Properties > General
  3. Set Whole Program Optimization to Use Link Time Code Generation or Profile Guided Optimization - Instrument/Optimize

Or in Linker settings: 1. Navigate to Linker > Optimization 2. Set Link Time Code Generation to Use Link Time Code Generation (/LTCG)

In MSBuild

<PropertyGroup Condition="'$(Configuration)'=='Release'">
  <WholeProgramOptimization>true</WholeProgramOptimization>
</PropertyGroup>

<ItemDefinitionGroup Condition="'$(Configuration)'=='Release'">
  <ClCompile>
    <WholeProgramOptimization>true</WholeProgramOptimization>
  </ClCompile>
  <Link>
    <LinkTimeCodeGeneration>UseLinkTimeCodeGeneration</LinkTimeCodeGeneration>
  </Link>
</ItemDefinitionGroup>

Trade-offs

Consideration Impact
Build Time Significantly longer link times
Memory Usage Higher memory during linking
Debugging May complicate debugging due to aggressive optimization
Incremental Builds Incompatible with incremental linking

When to Ignore

  • Debug builds where fast iteration is more important
  • Projects where link time is a significant bottleneck
  • Builds that require incremental linking
Flag Effect
/GL Compiler: Generate code for LTCG
/LTCG Linker: Enable Link Time Code Generation
/LTCG:PGOptimize Use Profile-Guided Optimization data
/LTCG:PGInstrument Instrument for PGO data collection

See Also