[llvm] 920cbe8 - [llvm][ADT] Fix Any linker error with multiple compilers

Sebastian Neubauer via llvm-commits llvm-commits at lists.llvm.org
Mon Apr 24 01:40:24 PDT 2023


Author: Sebastian Neubauer
Date: 2023-04-24T10:34:59+02:00
New Revision: 920cbe84054f7386a6bd31afa7111a2a3634b454

URL: https://github.com/llvm/llvm-project/commit/920cbe84054f7386a6bd31afa7111a2a3634b454
DIFF: https://github.com/llvm/llvm-project/commit/920cbe84054f7386a6bd31afa7111a2a3634b454.diff

LOG: [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

Added: 
    

Modified: 
    llvm/include/llvm/ADT/Any.h

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/ADT/Any.h b/llvm/include/llvm/ADT/Any.h
index acb7101a5145..7486491914ef 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<StorageBase> Storage;
 };
 
-template <typename T> char Any::TypeId<T>::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 
diff erent 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 <typename T> char Any::TypeId<T>::Id = 1;
 
 template <typename T>
 LLVM_DEPRECATED("Use any_cast(Any*) != nullptr instead", "any_cast")


        


More information about the llvm-commits mailing list