[PATCH] D148953: [llvm][ADT] Fix Any linker error with multiple compilers

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


This revision was automatically updated to reflect the committed changes.
Closed by commit rG920cbe84054f: [llvm][ADT] Fix Any linker error with multiple compilers (authored by sebastian-ne).

Changed prior to commit:
  https://reviews.llvm.org/D148953?vs=515879&id=516309#toc

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D148953/new/

https://reviews.llvm.org/D148953

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


Index: llvm/include/llvm/ADT/Any.h
===================================================================
--- llvm/include/llvm/ADT/Any.h
+++ llvm/include/llvm/ADT/Any.h
@@ -124,7 +124,16 @@
   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 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 <typename T> char Any::TypeId<T>::Id = 1;
 
 template <typename T>
 LLVM_DEPRECATED("Use any_cast(Any*) != nullptr instead", "any_cast")


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D148953.516309.patch
Type: text/x-patch
Size: 1074 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230424/47bb53d8/attachment.bin>


More information about the llvm-commits mailing list