In addition to the other answers, I must say that linkers usually work in terms of sections, not functions.
Compilers usually configure whether they put all of your object code in a single monolithic section or split them into several smaller ones. For example, the GCC options for enabling splitting are -ffunction-sections (for code) and -fdata-sections (for data); MSVC /Gy option (for both). -fnofunction-sections , -fnodata-sections , /Gy- respectively, to put all the code or data in one section.
You can “play” with compiling your modules in both modes and then reset them ( objdump for GCC, dumpbin for MSVC) to see the generated structure of the object files.
Once a section is formed by the compiler, for the linker it is one. Sections define characters and refer to characters defined in other sections. The compiler will build a graph of dependencies between partitions (starting with several roots), and then either decompose or save each of them entirely. So, if you have an unused and unused function in a section, an unused function will be saved.
Both modes have advantages and disadvantages. Turning shards means smaller executables, but larger object files and longer time snaps.
It should also be noted that in C ++, unlike C, there are certain situations where the rule of one definition is relaxed and multiple definitions of a function or data object are allowed (for example, in the case of built-in functions). The rules are formulated in such a way that the linker is allowed to choose any definition.
From the point of view of sections, the inclusion of built-in functions along with non-lowercase ones would mean that in a typical use case, the linker usually must force to save almost every definition of each built-in function; this will mean excessive bloating of the code. Therefore, such functions and data are usually placed in their sections regardless of the compiler command line parameters.
UPDATE: As @janm correctly recalled in his comment, the linker should also be instructed to get rid of unwritten sections by specifying --gc-sections (GNU) or /opt:ref (MS).
ach 09 Sep '14 at 1:44 2014-09-09 01:44
source share