diff --git a/kernel/CMakeLists.txt b/kernel/CMakeLists.txt index af311c38a42..4cecd2b3fd1 100644 --- a/kernel/CMakeLists.txt +++ b/kernel/CMakeLists.txt @@ -18,6 +18,7 @@ target_link_libraries(kernel INTERFACE ${libkernel}) else() list(APPEND kernel_files + main_weak.c banner.c device.c errno.c diff --git a/kernel/init.c b/kernel/init.c index ddf8c3dc285..90abce0adae 100644 --- a/kernel/init.c +++ b/kernel/init.c @@ -235,12 +235,6 @@ static void bg_thread_main(void *unused1, void *unused2, void *unused3) /* LCOV_EXCL_START */ -void __weak main(void) -{ - /* NOP default main() if the application does not provide one. */ - arch_nop(); -} - /* LCOV_EXCL_STOP */ #if defined(CONFIG_MULTITHREADING) diff --git a/kernel/main_weak.c b/kernel/main_weak.c new file mode 100644 index 00000000000..954d73e1de5 --- /dev/null +++ b/kernel/main_weak.c @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2010-2014 Wind River Systems, Inc. + * Copyright (c) 2021 Nordic Semiconductor ASA + * + * SPDX-License-Identifier: Apache-2.0 + */ + +/* Linkers may treat weak functions differently if they are located within + * the same object that calls the symbol or not. + * + * For example, when using armlink, then if the weak symbol is inside the object + * referring to it the weak symbol will be used. This will result in the symbol + * being multiply defined because both the weak and strong symbols are used. + * + * To GNU ld, it doesn't matter if the weak symbol is placed in the same object + * which uses the weak symbol. GNU ld will always link to the strong version. + * + * Having the weak main symbol in an independent file ensures that it will be + * correctly treated by multiple linkers. + */ + +#include + +void __weak main(void) +{ + /* NOP default main() if the application does not provide one. */ + arch_nop(); +}