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
A COMDAT section is one that can be defined by more than one object file; the flag IMAGE_SCN_LNK_COMDAT is set in its section header, and the Selection field of its section-definition auxiliary record decides how the linker resolves the multiple definitions. The first symbol that has the section number of the COMDAT section must be the section symbol: its name is the section’s name, its Value 0, its section number that of the COMDAT section, its Type IMAGE_SYM_TYPE_NULL, its storage class IMAGE_SYM_CLASS_STATIC, and it has one auxiliary record. The second symbol is the COMDAT symbol, which the linker uses together with the Selection field (PE Format). If the section’s COMDAT flag is set and it has line numbers, the first line-number record’s function symbol is also the COMDAT symbol name for the function. For selection 5 the section is tied to another section through the Number field.
Selection values
The Selection field of the section definition auxiliary record:
| Value | Constant | Meaning |
|---|---|---|
| 1 | IMAGE_COMDAT_SELECT_NODUPLICATES | If the symbol is already defined, the linker issues a “multiply defined symbol” error |
| 2 | IMAGE_COMDAT_SELECT_ANY | Any section that defines the same COMDAT symbol can be linked; the rest are removed |
| 3 | IMAGE_COMDAT_SELECT_SAME_SIZE | The linker chooses an arbitrary section among the definitions; if the definitions are not all the same size, a “multiply defined symbol” error is issued |
| 4 | IMAGE_COMDAT_SELECT_EXACT_MATCH | The linker chooses an arbitrary section among the definitions; if they do not all match exactly, a “multiply defined symbol” error is issued (how “exactly” is compared is not stated; the CheckSum field is the only content summary the record has — inference) |
| 5 | IMAGE_COMDAT_SELECT_ASSOCIATIVE | The section is linked if a certain other COMDAT section is linked, the one indicated by the Number field of the section-definition auxiliary record. Useful for definitions with components in several sections (code in one, data in another) that must be linked or discarded as a set. The other section must be a COMDAT section, which can itself be an associative one; the association chain cannot form a loop and must end at a COMDAT section that is not associative |
| 6 | IMAGE_COMDAT_SELECT_LARGEST | The linker chooses the largest definition; if several have that size, the choice between them is arbitrary |
| 7 | IMAGE_COMDAT_SELECT_NEWEST | Defined in LLVM’s COFF.h (COMDATType) but not in PE Format; no meaning is given there. The name suggests choosing the newest definition (inference) |
Selection values other than 1 to 7 have no defined meaning.