Skip to content

Commit d0b7b3b

Browse files
committed
Fix duplicate symbols for malloc lock and unlock
When the malloc lock and unlock functions are inside a library they conflict with the standard libraries weak version of these functions. This is because of the way weak references are handled by the linker. This patch renames the lock and unlock functions defined inside RTX so they do not conflict. A thunk inside retarget.cpp then calls the RTX functions. This problem does not occur with retarget.cpp since it is always build into an object file rather than a library file.
1 parent 3db2c03 commit d0b7b3b

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

hal/common/retarget.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,35 @@ extern "C" WEAK void __iar_file_Mtxinit(__iar_Rmtx *mutex) {}
636636
extern "C" WEAK void __iar_file_Mtxdst(__iar_Rmtx *mutex) {}
637637
extern "C" WEAK void __iar_file_Mtxlock(__iar_Rmtx *mutex) {}
638638
extern "C" WEAK void __iar_file_Mtxunlock(__iar_Rmtx *mutex) {}
639+
#elif defined(__CC_ARM)
640+
// Do nothing
641+
#elif defined (__GNUC__)
642+
struct _reent;
643+
// Stub out locks when an rtos is not present
644+
extern "C" WEAK void __rtos_malloc_lock( struct _reent *_r ) {}
645+
extern "C" WEAK void __rtos_malloc_unlock( struct _reent *_r ) {}
646+
extern "C" WEAK void __rtos_env_lock( struct _reent *_r ) {}
647+
extern "C" WEAK void __rtos_env_unlock( struct _reent *_r ) {}
648+
649+
void __malloc_lock( struct _reent *_r )
650+
{
651+
__rtos_malloc_lock(_r);
652+
}
653+
654+
void __malloc_unlock( struct _reent *_r )
655+
{
656+
__rtos_malloc_unlock(_r);
657+
}
658+
659+
void __env_lock( struct _reent *_r )
660+
{
661+
__rtos_env_lock(_r);
662+
}
663+
664+
void __env_unlock( struct _reent *_r )
665+
{
666+
__rtos_env_unlock(_r);
667+
}
639668
#endif
640669

641670
} // namespace mbed

rtos/rtx/TARGET_CORTEX_M/RTX_CM_lib.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -684,22 +684,22 @@ __attribute__((naked)) void software_init_hook_rtos (void) {
684684
// Opaque declaration of _reent structure
685685
struct _reent;
686686

687-
void __malloc_lock( struct _reent *_r )
687+
void __rtos_malloc_lock( struct _reent *_r )
688688
{
689689
osMutexWait(malloc_mutex_id, osWaitForever);
690690
}
691691

692-
void __malloc_unlock( struct _reent *_r )
692+
void __rtos_malloc_unlock( struct _reent *_r )
693693
{
694694
osMutexRelease(malloc_mutex_id);
695695
}
696696

697-
void __env_lock( struct _reent *_r )
697+
void __rtos_env_lock( struct _reent *_r )
698698
{
699699
osMutexWait(env_mutex_id, osWaitForever);
700700
}
701701

702-
void __env_unlock( struct _reent *_r )
702+
void __rtos_env_unlock( struct _reent *_r )
703703
{
704704
osMutexRelease(env_mutex_id);
705705
}

0 commit comments

Comments
 (0)