Skip to content

Commit

Permalink
[llvm][ADT] Fix Any linker error with multiple compilers
Browse files Browse the repository at this point in the history
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 #62270
A regression of D139974.

Differential Revision: https://reviews.llvm.org/D148953
  • Loading branch information
Flakebi committed Apr 24, 2023
1 parent 61cddaa commit 920cbe8
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion llvm/include/llvm/ADT/Any.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 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")
Expand Down

0 comments on commit 920cbe8

Please sign in to comment.