On 24 March 2017 at 12:53, Felix Poludov <fel...@ami.com> wrote:
> Trying to add GCC support to projects based on MSFT tool chain, I'm keep 
> stumbling into multiply defined symbol errors reported by GCC linker.
> An attempt to understand why the errors are not reported by the Microsoft 
> linker lead me to GLOBAL_REMOVE_IF_UNREFERENCED macro.
> The purpose of the macro is to enable link time optimization of global 
> variables.
> However, the way it's defined for MSFT tool chain (__declspec(selectany) ) 
> has a side effect of explicitly allowing multiple instances of a symbol 
> defined with GLOBAL_REMOVE_IF_UNREFERENCED.

This smells like a variant of the GCC COMMON issue that you quote
below. 'Select any' presumes that globally visible symbols with the
same name are guaranteed to refer to the same data item, and the
nature and architecture of EDK2 make it impossible to ever be sure
about that (i.e., cross-package library class resolution)

> For a while usage of the macro was the only option to enable global variable 
> optimization.
> Starting from VS2013 compiler supports /Gw flag that enables global variable 
> optimization without a special declarator.
>
> I propose to make the following modifications:
>
> 1.      Change GLOBAL_REMOVE_IF_UNREFERENCED definition to an empty macro.
>
> Or more specifically, update macro definition in Base.h as follows:
>
> #ifndef GLOBAL_REMOVE_IF_UNREFERENCED
>
> #define GLOBAL_REMOVE_IF_UNREFERENCED
>
> #endif
>
> 2.      Update VS2013 and VS2015 compiler flags to add /Gw option
>
> 3.      Update compiler flags for older MSFT tool chains to define 
> GLOBAL_REMOVE_IF_UNREFERENCED in a backward compatible manner for targets 
> that enable optimization.
>
> /D GLOBAL_REMOVE_IF_UNREFERENCED =_declspec(selectany)
>
>
> The advantages of these modifications are:
>
> -        Better detection of on potential errors by breaking the build when 
> symbol is defined more than once.
>
> -        Improved consistency between MSFT and GCC tool chains
>
> -        Improved link time optimization with VS2013 and newer MSFT tool 
> chains.
>
> For example, mGaugeData in 
> MdeModulePkg/Library/DxeCorePerformanceLib/DxeCorePerformanceLib.c is not 
> declared as GLOBAL_REMOVE_IF_UNREFERENCED, so
>
> today performance library is linked with DXE Core even when performance 
> measurements are disabled.
>
> The alternative option is to enable support of multiply defined symbols on 
> GCC tool chain.
> One way to do it is by defining the macro as
> #define GLOBAL_REMOVE_IF_UNREFERENCED __attribute__((weak))
>

No! That may fix your build, but it only papers over the problem.

> However, I'm not sure that embracing multiple symbol definitions is a good 
> idea.
> For example, see Ard's arguments in this commit comment
> https://github.com/tianocore/edk2/commit/214a3b79417f64bf2faae74af42c1b9d23f50dc8
>

GCC used -ffunction-sections and -fdata-sections, and performs link
time garbage collection (--gc-sections), and so globals that are never
referenced will ultimately be dropped anyway.

So in the mGaugeData case you mention above, GCC (or rather, GNU ld),
should notice that no references to it exists, and the section
containing just mGaugeData will be dropped from the build.

As I mentioned in the commit log of the above patch, STATIC is the key
here. In my opinion, STATIC should be mandatory for all function and
variables that are only referenced from the same compilation unit. Not
only does it help the compiler produce better code (in the absence of
LTO), it prevents namespace pollution and generally results in better
structured code (given that you can't easily link to some symbol in
another object)

Given that each of the linker errors you get points to a potential
problem in your code, perhaps the best approach would be to
temporarily define GLOBAL_REMOVE_IF_UNREFERENCED to STATIC instead, so
you can track down the occurrences that really require external
linkage, but have multiple definitions, and fix those up manually.
_______________________________________________
edk2-devel mailing list
edk2-devel@lists.01.org
https://lists.01.org/mailman/listinfo/edk2-devel

Reply via email to