Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the COMDAT section used for?

Tags:

c++

windows

coff

I see the /Gy option and am wondering why I would use it? http://msdn.microsoft.com/en-us/library/xsa71f43.aspx

like image 784
Scott J Avatar asked Dec 02 '09 17:12

Scott J


2 Answers

The purpose of a COMDAT section is to allow "duplicate" sections to be defined in multiple object files. Normally, if the same symbol is defined in multiple object files, the linker will report errors. This can cause problems for some C++ language features, like templates, that may instantiate the same symbols in different cpp files.

COMDAT sections are used to get around this. When a section is marked as a COMDAT in an object file, it also specifies a flag that indicates how conflicts should be resolved. There are a bunch of options, including "just pick anyone you like", "make sure all dups. are the same size", "make sure all dups. have the same content", "pick the largest one", etc. See the COFF spec for a complete list.

In any case, unlike what other answers said, there's no requirement, one way or the other, on what the contents of a COMDAT section has to be. They can contain one procedure, many procedures, data, or any combination of both code and data.

like image 147
Scott Wisniewski Avatar answered Oct 06 '22 01:10

Scott Wisniewski


/Gy option is good to be used in release builds. 'cause every function has it's own section, linker can drop every unused piece of code. If you do not specify /Gy option you will get something like this: "a.cpp" defines 3 functions and compiler puts them in one code section when compiling. "main.cpp" uses only one function from "a.cpp", but when linking all of functions from "a.cpp" will be linked (two of them as a burden). And if every function had it's own section, linker could pick up only one that contained function needed by "main.cpp". Compiling without /Gy option is good for debug builds. When compiling a class, all of it's methods get their own separate sections by default.

like image 34
Dmitriy Yurchenko Avatar answered Oct 06 '22 00:10

Dmitriy Yurchenko