From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from smtp50.i.mail.ru (smtp50.i.mail.ru [94.100.177.110]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dev.tarantool.org (Postfix) with ESMTPS id 19AF64696CE for ; Sun, 12 Apr 2020 03:13:49 +0300 (MSK) Received: by smtp50.i.mail.ru with esmtpa (envelope-from ) id 1jNQFw-0005lJ-IR for tarantool-patches@dev.tarantool.org; Sun, 12 Apr 2020 03:13:48 +0300 From: Vladislav Shpilevoy Date: Sun, 12 Apr 2020 02:13:00 +0200 Message-Id: In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Subject: [Tarantool-patches] [PATCH 02/43] cmake: remove dynamic-list linker option List-Id: Tarantool development patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: tarantool-patches@dev.tarantool.org dynamic-list (exported_symbols_list on Mac) was used to forbid export of all symbols of the tarantool executable except a given list. Motivation of that was to avoid hacking the linker with false usage of symbols needed to be exported. As a consequence, symbols not listed in these options became invisible. Before these options, when a symbol was defined, but not used in the final executable, the linker could throw it away, even though many symbols were used by Lua FFI, or should be visible for user's dynamic modules. Where the linker, obviously, can't see if they are needed. To make the linker believe the symbols are actually needed there was a hack with getting pointers at these functions and doing something with them. For example, assume we have 'test()' function in 'box' static library: int test(void); It is not used anywhere in the final executable. So to trick the linker there is a function 'export()' declared, which takes a pointer at 'test()' and seemingly does something with it (or actually does - it does not matter): void export() { void *syms[] = {test}; if (time(NULL) == 0) { syms[0](); syms[1](); ... } } Some users want to use not documented but visible symbols, so the patch removes the dynamic-list option, and returns the linker hack back, but in a more structured form. Part of #2971 --- .gitignore | 1 - src/CMakeLists.txt | 50 +++---------------------------------- src/exports.c | 61 ++++++++++++++++++++++++++++++++++++++++++++++ src/main.cc | 5 ++++ 4 files changed, 69 insertions(+), 48 deletions(-) create mode 100644 src/exports.c diff --git a/.gitignore b/.gitignore index cda28d79f..a42c7db35 100644 --- a/.gitignore +++ b/.gitignore @@ -57,7 +57,6 @@ extra/dist/tarantool.logrotate extra/dist/tarantool@.service extra/dist/tarantool.tmpfiles.conf extra/dist/tarantool-generator -extra/exports.* cmake_install.cmake config.mk config.guess diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index a17da9901..3a8b670f2 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -245,56 +245,12 @@ if(BUILD_STATIC) endif() endif() -# Exports syntax is toolchain-dependent, preprocessing is necessary -set(exports_file ${CMAKE_BINARY_DIR}/extra/exports.${CMAKE_SYSTEM_NAME}) -add_custom_target(preprocess_exports - DEPENDS ${exports_file}) -add_custom_command( - OUTPUT ${exports_file} - DEPENDS ${CMAKE_SOURCE_DIR}/extra/exports - COMMAND ${CMAKE_SOURCE_DIR}/extra/mkexports - ${CMAKE_SOURCE_DIR}/extra/exports - ${exports_file} ${CMAKE_SYSTEM_NAME} - ${EXPORT_LIST} -) - add_executable( - tarantool main.cc + tarantool main.cc exports.c ${LIBUTIL_FREEBSD_SRC}/flopen.c ${LIBUTIL_FREEBSD_SRC}/pidfile.c) -add_dependencies(tarantool build_bundled_libs preprocess_exports sql) - -# Re-link if exports changed -set_target_properties(tarantool PROPERTIES LINK_DEPENDS ${exports_file}) - -# A note about linkers: -# [GNU linker] When linking an *executable* visibility is ignored, and -# either nothing is exported (default), or any non-static -# symbol is exported (-rdynamic), or explicitly listed -# symbols are exported (--dynamic-list). -# -# However, if a symbol listed lives in a static library it -# won't be automatically pulled, hence --whole-archive -# option. -# -# [Apple linker] One can provide an explicit export list; pulls symbols -# from static libraries. -# -if (TARGET_OS_DARWIN) - target_link_libraries(tarantool box ${common_libraries}) - set_target_properties(tarantool PROPERTIES - LINK_FLAGS "-Wl,-exported_symbols_list,${exports_file}") -else () - target_link_libraries(tarantool - -Wl,--whole-archive box ${reexport_libraries} - salad -Wl,--no-whole-archive - ${common_libraries} - ${generic_libraries}) - set_target_properties(tarantool PROPERTIES - LINK_FLAGS "-Wl,--dynamic-list,${exports_file}") - # get rid of -rdynamic - set(CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "") -endif() +add_dependencies(tarantool build_bundled_libs sql) +target_link_libraries(tarantool box ${common_libraries}) install (TARGETS tarantool DESTINATION bin) diff --git a/src/exports.c b/src/exports.c new file mode 100644 index 000000000..4bb423a57 --- /dev/null +++ b/src/exports.c @@ -0,0 +1,61 @@ +/* + * Copyright 2010-2020, Tarantool AUTHORS, please see AUTHORS file. + * + * Redistribution and use in source and binary forms, with or + * without modification, are permitted provided that the following + * conditions are met: + * + * 1. Redistributions of source code must retain the above + * copyright notice, this list of conditions and the + * following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL + * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR + * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF + * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ +#define EXPORT(func) extern void ** func(void) + +/** + * The file is a hack to force the linker keep the needed symbols + * in the result tarantool executable file. + * + * Problem is that if a symbol is defined but never used, the + * linker may throw it away. But many symbols are needed for Lua + * FFI and for the public C API used by dynamic modules. + * + * This file creates a 'false usage' of needed symbols. It takes + * pointers at them and does something with them, so as the + * compiler and linker couldn't remove it. + * + * Some exporters may represent modules having submodules, and may + * aggregate symbols from them. + * + * Add new exporters here. Keep them in alphabetical order. + */ + +void +export_syms(void) +{ + void *syms[] = { + }; + const int func_count = sizeof(syms) / sizeof(syms[0]); + for (int i = 0; i < func_count; ++i) + ((void **(*)(void))syms[i])(); +} + +#undef EXPORT diff --git a/src/main.cc b/src/main.cc index bb0794dfe..712c70bc0 100644 --- a/src/main.cc +++ b/src/main.cc @@ -711,6 +711,9 @@ break_loop(struct trigger *, void *) return 0; } +extern "C" void +export_syms(void); + int main(int argc, char **argv) { @@ -808,6 +811,8 @@ main(int argc, char **argv) title_set_script_name(argv[0]); } + export_syms(); + random_init(); crc32_init(); -- 2.21.1 (Apple Git-122.3)