From 661769527c16d6f3269b6a6254f0576135c8673d Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Sun, 28 Jul 2019 11:22:08 +0200 Subject: [PATCH] pthread: force linking pthread implementation from IDF Force linking pthread implementation from IDF, instead of the weak functions provided by gthread library. Previously this would either work or not depending on the linking order. Thanks @bpietsch for suggesting the fix. Closes https://github.com/espressif/esp-idf/issues/3709 --- components/pthread/CMakeLists.txt | 10 ++++++++++ components/pthread/component.mk | 8 ++++++++ components/pthread/pthread.c | 5 +++++ components/pthread/pthread_cond_var.c | 5 +++++ components/pthread/pthread_local_storage.c | 5 +++++ 5 files changed, 33 insertions(+) diff --git a/components/pthread/CMakeLists.txt b/components/pthread/CMakeLists.txt index d488c92a9..136bf9cf1 100644 --- a/components/pthread/CMakeLists.txt +++ b/components/pthread/CMakeLists.txt @@ -3,6 +3,16 @@ idf_component_register(SRCS "pthread.c" "pthread_local_storage.c" INCLUDE_DIRS include) +if(GCC_NOT_5_2_0) + set(extra_link_flags "-u pthread_include_pthread_impl") + list(APPEND extra_link_flags "-u pthread_include_pthread_cond_impl") + list(APPEND extra_link_flags "-u pthread_include_pthread_local_storage_impl") +endif() + if(CONFIG_FREERTOS_ENABLE_STATIC_TASK_CLEAN_UP) target_link_libraries(${COMPONENT_LIB} "-Wl,--wrap=vPortCleanUpTCB") endif() + +if(extra_link_flags) + target_link_libraries(${COMPONENT_LIB} INTERFACE "${extra_link_flags}") +endif() diff --git a/components/pthread/component.mk b/components/pthread/component.mk index 9c3eececf..c5793fded 100644 --- a/components/pthread/component.mk +++ b/components/pthread/component.mk @@ -11,3 +11,11 @@ COMPONENT_ADD_LDFLAGS := -lpthread ifdef CONFIG_FREERTOS_ENABLE_STATIC_TASK_CLEAN_UP COMPONENT_ADD_LDFLAGS += -Wl,--wrap=vPortCleanUpTCB endif + +ifeq ($(GCC_NOT_5_2_0), 1) +# Forces the linker to include pthread implementation from this component, +# instead of the weak implementations provided by libgcc. +COMPONENT_ADD_LDFLAGS += -u pthread_include_pthread_impl +COMPONENT_ADD_LDFLAGS += -u pthread_include_pthread_cond_impl +COMPONENT_ADD_LDFLAGS += -u pthread_include_pthread_local_storage_impl +endif # GCC_NOT_5_2_0 diff --git a/components/pthread/pthread.c b/components/pthread/pthread.c index 84997c9aa..f4fe4ded7 100644 --- a/components/pthread/pthread.c +++ b/components/pthread/pthread.c @@ -820,3 +820,8 @@ int pthread_attr_setdetachstate(pthread_attr_t *attr, int detachstate) } return EINVAL; } + +/* Hook function to force linking this file */ +void pthread_include_pthread_impl() +{ +} diff --git a/components/pthread/pthread_cond_var.c b/components/pthread/pthread_cond_var.c index fe3144512..8db28ae5e 100644 --- a/components/pthread/pthread_cond_var.c +++ b/components/pthread/pthread_cond_var.c @@ -198,3 +198,8 @@ int pthread_cond_destroy(pthread_cond_t *cv) return ret; } + +/* Hook function to force linking this file */ +void pthread_include_pthread_cond_var_impl() +{ +} diff --git a/components/pthread/pthread_local_storage.c b/components/pthread/pthread_local_storage.c index 5e2dbafd2..ecf252e4c 100644 --- a/components/pthread/pthread_local_storage.c +++ b/components/pthread/pthread_local_storage.c @@ -255,3 +255,8 @@ int pthread_setspecific(pthread_key_t key, const void *value) return 0; } + +/* Hook function to force linking this file */ +void pthread_include_pthread_local_storage_impl() +{ +}