From 920cbe84054f7386a6bd31afa7111a2a3634b454 Mon Sep 17 00:00:00 2001 From: Sebastian Neubauer Date: Mon, 24 Apr 2023 10:34:59 +0200 Subject: [PATCH] [llvm][ADT] Fix Any linker error with multiple compilers Citing the comment in the source: Define the type id and initialize with a non-zero value. Initializing with a zero value means the variab can end up in either the .data or the .bss section. This can lead to multiple definition linker errors when some object files are compiled with a compiler that puts the variable into .data but they are linked to object files from a different compiler that put the variable into .bss. To prevent this issue from happening, initialize the variable with a non-zero value, which forces it to land in .data (because .bss is zero-initialized). Fixes https://github.com/llvm/llvm-project/issues/62270 A regression of D139974. Differential Revision: https://reviews.llvm.org/D148953 --- llvm/include/llvm/ADT/Any.h | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/llvm/include/llvm/ADT/Any.h b/llvm/include/llvm/ADT/Any.h index acb7101a5145f0..7486491914ef75 100644 --- a/llvm/include/llvm/ADT/Any.h +++ b/llvm/include/llvm/ADT/Any.h @@ -124,7 +124,16 @@ class LLVM_EXTERNAL_VISIBILITY Any { std::unique_ptr Storage; }; -template char Any::TypeId::Id = 0; +// Define the type id and initialize with a non-zero value. +// Initializing with a zero value means the variable can end up in either the +// .data or the .bss section. This can lead to multiple definition linker errors +// when some object files are compiled with a compiler that puts the variable +// into .data but they are linked to object files from a different compiler that +// put the variable into .bss. To prevent this issue from happening, initialize +// the variable with a non-zero value, which forces it to land in .data (because +// .bss is zero-initialized). +// See also https://github.com/llvm/llvm-project/issues/62270 +template char Any::TypeId::Id = 1; template LLVM_DEPRECATED("Use any_cast(Any*) != nullptr instead", "any_cast")