Skip to content

Export std hash template specializations from dylibs #7618

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,10 @@ if(BUILD_STATIC_LIB)
else()
message(STATUS "Building libbinaryen as shared library.")
add_library(binaryen SHARED)
if(LINUX)
# Disable interposition and resolve Binaryen symbols locally.
add_link_flag("-Bsymbolic")
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This part is not actually necessary for the workaround. It's just a small optimization for DSO-local calls.

endif()
endif()
target_link_libraries(binaryen Threads::Threads)
if(BUILD_LLVM_DWARF)
Expand Down
19 changes: 19 additions & 0 deletions src/compiler-support.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,23 @@
#define WASM_BUILTIN_UNREACHABLE __assume(false)
#endif

// Forces symbols to be exported from libbinaryen dynamic library. Currently
// we are just using the default flags, causing most of our symbols to have
// "default" visibility, meaning they can all be used from the tool sources.
// However a recent libc++ change caused functions declared in namespace std
// (e.g. hash template specializations) to have hidden visibility, inherited.
// from the namespece (see https://github.com/llvm/llvm-project/pull/131156).
// So this macro forces them to be exported. Currently it is only applied to
// the hash specializations that are defined in libbinaryen and used in the
// tool code. In the future if we want to compile libbinaryen with
// -fvisibility-hidden or use a DLL on Windows, we'll need
// to explicitly annotate everything we want to export. But that's probably
// only useful if we want external users to link against libbinaryen.so
// (currently we don't; it's only used to reduce our install size).
#if defined(__ELF__) || defined(__MACH__)
#define DLLEXPORT [[gnu::visibility("default")]]
#else
#define DLLEXPORT
#endif

#endif // wasm_compiler_support_h
3 changes: 2 additions & 1 deletion src/wasm-type-shape.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <functional>
#include <vector>

#include "compiler-support.h"
#include "wasm-features.h"
#include "wasm-type.h"

Expand Down Expand Up @@ -74,7 +75,7 @@ namespace std {

template<> class hash<wasm::RecGroupShape> {
public:
size_t operator()(const wasm::RecGroupShape& shape) const;
DLLEXPORT size_t operator()(const wasm::RecGroupShape& shape) const;
};

} // namespace std
Expand Down
17 changes: 9 additions & 8 deletions src/wasm-type.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <variant>
#include <vector>

#include "compiler-support.h"
#include "support/index.h"
#include "support/name.h"
#include "support/parent_index_iterator.h"
Expand Down Expand Up @@ -1005,35 +1006,35 @@ namespace std {

template<> class hash<wasm::Type> {
public:
size_t operator()(const wasm::Type&) const;
DLLEXPORT size_t operator()(const wasm::Type&) const;
};
template<> class hash<wasm::Signature> {
public:
size_t operator()(const wasm::Signature&) const;
DLLEXPORT size_t operator()(const wasm::Signature&) const;
};
template<> class hash<wasm::Continuation> {
public:
size_t operator()(const wasm::Continuation&) const;
DLLEXPORT size_t operator()(const wasm::Continuation&) const;
};
template<> class hash<wasm::Field> {
public:
size_t operator()(const wasm::Field&) const;
DLLEXPORT size_t operator()(const wasm::Field&) const;
};
template<> class hash<wasm::Struct> {
public:
size_t operator()(const wasm::Struct&) const;
DLLEXPORT size_t operator()(const wasm::Struct&) const;
};
template<> class hash<wasm::Array> {
public:
size_t operator()(const wasm::Array&) const;
DLLEXPORT size_t operator()(const wasm::Array&) const;
};
template<> class hash<wasm::HeapType> {
public:
size_t operator()(const wasm::HeapType&) const;
DLLEXPORT size_t operator()(const wasm::HeapType&) const;
};
template<> class hash<wasm::RecGroup> {
public:
size_t operator()(const wasm::RecGroup&) const;
DLLEXPORT size_t operator()(const wasm::RecGroup&) const;
};

} // namespace std
Expand Down
Loading