From a0fe3c18976a67c4ebea16f986d86e98e9489706 Mon Sep 17 00:00:00 2001 From: Peter Griess Date: Mon, 23 Sep 2013 18:25:18 -0500 Subject: [PATCH] Handle lack of weak symbols on some platforms Summary: - It turns out that it's not always desirable to perform runtime resolution of weak symbols. For example, on iOS, weak symbols are resolved at runtime only if *all* symbol resolution is deferred util then, which is undesirable for othe reasons. Detect such platforms at configure time and use that information to populate detail/Malloc.h with the correct declarations: weak symbols or extern symbols with a value of nullptr. Test Plan: - fbconfig -r folly && fbmake runtests - ./configure && make check on Ubuntu/FC/Mac Reviewed By: andrei.alexandrescu@fb.com FB internal diff: D1002959 --- folly/Makefile.am | 5 +++++ folly/Malloc.h | 13 +++--------- folly/configure.ac | 15 ++++++++++++++ folly/detail/Malloc.cpp | 24 ++++++++++++++++++++++ folly/detail/Malloc.h | 35 ++++++++++++++++++++++++++++++++ folly/detail/ThreadLocalDetail.h | 5 ----- 6 files changed, 82 insertions(+), 15 deletions(-) create mode 100644 folly/detail/Malloc.cpp create mode 100644 folly/detail/Malloc.h diff --git a/folly/Makefile.am b/folly/Makefile.am index b42082b3..a211ade3 100644 --- a/folly/Makefile.am +++ b/folly/Makefile.am @@ -39,6 +39,7 @@ nobase_follyinclude_HEADERS = \ detail/FingerprintPolynomial.h \ detail/Futex.h \ detail/GroupVarintDetail.h \ + detail/Malloc.h \ detail/MPMCPipelineDetail.h \ detail/SlowFingerprint.h \ detail/Stats.h \ @@ -161,6 +162,10 @@ nobase_follyinclude_HEADERS += detail/Clock.h libfolly_la_SOURCES += detail/Clock.cpp endif +if !HAVE_WEAK_SYMBOLS +libfolly_la_SOURCES += detail/Malloc.cpp +endif + FingerprintTables.cpp: generate_fingerprint_tables ./generate_fingerprint_tables diff --git a/folly/Malloc.h b/folly/Malloc.h index 8310a9aa..6a660086 100644 --- a/folly/Malloc.h +++ b/folly/Malloc.h @@ -26,6 +26,8 @@ // includes and uses fbstring. #if defined(_GLIBCXX_USE_FB) && !defined(_LIBSTDCXX_FBSTRING) +#include "folly/detail/Malloc.h" + #include namespace folly { using std::goodMallocSize; @@ -44,6 +46,7 @@ namespace folly { #include #define FOLLY_HAVE_MALLOC_H 1 #else +#include "folly/detail/Malloc.h" #include "folly/Portability.h" #include #endif @@ -87,16 +90,6 @@ namespace folly { #endif /* ALLOCM_SUCCESS */ -/** - * Declare rallocm() and malloc_usable_size() as weak symbols. It - * will be provided by jemalloc if we are using jemalloc, or it will - * be NULL if we are using another malloc implementation. - */ -extern "C" int rallocm(void**, size_t*, size_t, size_t, int) -__attribute__((weak)); -extern "C" int allocm(void**, size_t*, size_t, int) -__attribute__((weak)); - #ifdef _LIBSTDCXX_FBSTRING namespace std _GLIBCXX_VISIBILITY(default) { _GLIBCXX_BEGIN_NAMESPACE_VERSION diff --git a/folly/configure.ac b/folly/configure.ac index 2c28d5c2..70afc3f0 100644 --- a/folly/configure.ac +++ b/folly/configure.ac @@ -127,6 +127,20 @@ AC_COMPILE_IFELSE( [Define to 1 if we have a usable std::is_trivially_copyable implementation.])]) +# Figure out if we support weak symbols. If not, we will link in some null +# stubs for functions that would otherwise be weak. +AC_LINK_IFELSE( + [AC_LANG_SOURCE[ + extern "C" void configure_link_extern_weak_test() __attribute__((weak)); + int main(int argc, char** argv) { + return configure_link_extern_weak_test == nullptr; + }] + ], + [ + ac_have_weak_symbols="yes" + AC_DEFINE([HAVE_WEAK_SYMBOLS], [1], + [Define to 1 if the linker supports weak symbols.])]) + # Check for clock_gettime(2). This is not in an AC_CHECK_FUNCS() because we # want to link with librt if necessary. AC_SEARCH_LIBS([clock_gettime], [rt], @@ -160,6 +174,7 @@ AC_SUBST(AM_LDFLAGS, "$BOOST_LDFLAGS $BOOST_THREAD_LIB $BOOST_SYSTEM_LIB $BOOST_ AM_CONDITIONAL([HAVE_STD_THREAD], [test "$ac_cv_header_features" = "yes"]) AM_CONDITIONAL([HAVE_X86_64], [test "$build_cpu" = "x86_64"]) AM_CONDITIONAL([HAVE_LINUX], [test "$build_os" == "linux-gnu"]) +AM_CONDITIONAL([HAVE_WEAK_SYMBOLS], [test "$ac_have_weak_symbols" = "yes"]) # Output AC_CONFIG_FILES([Makefile diff --git a/folly/detail/Malloc.cpp b/folly/detail/Malloc.cpp new file mode 100644 index 00000000..df076d58 --- /dev/null +++ b/folly/detail/Malloc.cpp @@ -0,0 +1,24 @@ +/* + * Copyright 2013 Facebook, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "folly/detail/Malloc.h" + +extern "C" { + +int (*rallocm)(void**, size_t*, size_t, size_t, int) = nullptr; +int (*allocm)(void**, size_t*, size_t, int) = nullptr; + +} diff --git a/folly/detail/Malloc.h b/folly/detail/Malloc.h new file mode 100644 index 00000000..484745c0 --- /dev/null +++ b/folly/detail/Malloc.h @@ -0,0 +1,35 @@ +/* + * Copyright 2013 Facebook, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef FOLLY_DETAIL_MALLOC_H +#define FOLLY_DETAIL_MALLOC_H + +#include +#include "folly/folly-config.h" + +extern "C" { + +#if FOLLY_HAVE_WEAK_SYMBOLS +int rallocm(void**, size_t*, size_t, size_t, int) __attribute__((weak)); +int allocm(void**, size_t*, size_t, int) __attribute__((weak)); +#else +extern int (*rallocm)(void**, size_t*, size_t, size_t, int); +extern int (*allocm)(void**, size_t*, size_t, int); +#endif + +} + +#endif diff --git a/folly/detail/ThreadLocalDetail.h b/folly/detail/ThreadLocalDetail.h index 20a74fa5..7dbe6e7f 100644 --- a/folly/detail/ThreadLocalDetail.h +++ b/folly/detail/ThreadLocalDetail.h @@ -30,11 +30,6 @@ #include "folly/Exception.h" #include "folly/Malloc.h" -// TODO(tudorb): Remove this declaration after Malloc.h is pushed to -// third-party. -extern "C" int allocm(void**, size_t*, size_t, int) -__attribute__((weak)); - namespace folly { namespace threadlocal_detail { -- 2.34.1