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

Sebastian Neubauer via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Apr 21 12:46:33 PDT 2023


sebastian-ne created this revision.
sebastian-ne added reviewers: jloser, dblaikie.
Herald added a project: All.
sebastian-ne requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

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 <https://reviews.llvm.org/D139974>.


Repository:
  rG LLVM Github Monorepo

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 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).
+// 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.515879.patch
Type: text/x-patch
Size: 1072 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230421/12c7f41c/attachment.bin>


More information about the llvm-commits mailing list