From 214a3b79417f64bf2faae74af42c1b9d23f50dc8 Mon Sep 17 00:00:00 2001 From: Ard Biesheuvel Date: Tue, 8 Dec 2015 07:40:12 +0000 Subject: BaseTools GCC: avoid the use of COMMON symbols The default behavior of the GCC compiler is to emit uninitialized globals with external linkage into a COMMON section, where duplicate definitions are merged. This may result in unexpected behavior, since global variables defined under the same name in different C files may not refer to the same logical data item. For instance, the definitions of EFI_EVENT mVirtualAddressChangeEvent that [used to] appear in the following files: CryptoPkg/Library/BaseCryptLib/SysCall/RuntimeMemAllocation.c MdeModulePkg/Universal/Variable/RuntimeDxe/VariableDxe.c will be folded into a single instance of the variable when the latter module includes the former library, which can lead to unexpected results. Even if some may argue that there are legal uses for COMMON allocation, the high modularity of EDK2 combined with the low level of awareness of the intracicies surrounding common allocation and the generally poor EDK2 developer discipline regarding the use of the STATIC keyword* make a strong case for disabling it by default, and re-enabling it explicitly for packages that depend on it. So prevent GCC from emitting variables into the COMMON section, by passing -fno-common to the compiler, and discarding the section in the GNU ld linker script. * Any function or variable that is only referenced from the translation unit that defines it could be made STATIC. This does not only prevent issues like the above, it also allows the compiler to generate better code, e.g., drop out of line function definitions after inlining all invocations or perform constant propagation on variables. Contributed-under: TianoCore Contribution Agreement 1.0 Signed-off-by: Ard Biesheuvel Reviewed-by: Liming Gao Reviewed-by: Laszlo Ersek Tested-by: Laszlo Ersek git-svn-id: https://svn.code.sf.net/p/edk2/code/trunk/edk2@19164 6f19259b-4bc3-4df7-8a09-765794883524 --- BaseTools/Scripts/GccBase.lds | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'BaseTools/Scripts') diff --git a/BaseTools/Scripts/GccBase.lds b/BaseTools/Scripts/GccBase.lds index 4ee6d99853..32310bc75d 100644 --- a/BaseTools/Scripts/GccBase.lds +++ b/BaseTools/Scripts/GccBase.lds @@ -46,7 +46,7 @@ SECTIONS { */ .data ALIGN(ALIGNOF(.text)) : ALIGN(CONSTANT(COMMONPAGESIZE)) { *(.data .data.* .gnu.linkonce.d.*) - *(.bss .bss.* *COM*) + *(.bss .bss.*) } .eh_frame ALIGN(CONSTANT(COMMONPAGESIZE)) : { @@ -66,5 +66,6 @@ SECTIONS { *(.dynamic) *(.hash) *(.comment) + *(COMMON) } } -- cgit v1.2.3