C++ puts inline functions, templates and similar things in every object file that uses
them. A COMDAT section lets the linker keep one copy and throw away the rest. A compiler
invoked with function-level linking (/Gy) puts every function in its own
COMDAT section.
How a COMDAT is written
section headerIMAGE_SCN_LNK_COMDAT set in Characteristicsflag 0x1000symbolsection symbol: the section’s name, STATIC, Value 018 bytesaux recordsection definition, with the selection number18 bytessymbolCOMDAT symbol: the first symbol defined in the section18 bytes
The section’s first symbol is the one that names the section; the next symbol defined in that section is the COMDAT symbol, the name the linker compares between object files. For selection 5 the symbol’s name doesn’t matter: the section goes with another one.
Selection values
The Selection field of the section definition auxiliary record:
| Value | Constant | Meaning |
|---|---|---|
| 1 | IMAGE_COMDAT_SELECT_NODUPLICATES | A second definition is an error |
| 2 | IMAGE_COMDAT_SELECT_ANY | Link any one and discard the rest |
| 3 | IMAGE_COMDAT_SELECT_SAME_SIZE | Pick one; an error if the sizes differ |
| 4 | IMAGE_COMDAT_SELECT_EXACT_MATCH | Pick one; an error if the contents differ (by CheckSum) |
| 5 | IMAGE_COMDAT_SELECT_ASSOCIATIVE | Linked only if the section named in Number is linked |
| 6 | IMAGE_COMDAT_SELECT_LARGEST | Pick the largest definition |