Skip to content

Commit

Permalink
[nrfconnect] Do not link libCHIPShell.a with --whole-archive
Browse files Browse the repository at this point in the history
libCHIPShell.a is already part of libCHIP.a but the former
had to be linked additionally with --whole-archive flag to
process the shell and init objects defined with SHELL_XXX
and SYS_INIT Zephyr macros and, in turn, register Matter
shell commands properly.

This symbol duplication between the two libraries causes
issues when building Matter with LTO, so replace the current
approach with explicitly pulling in one symbol from
MainLoopZephyr.cpp file.

Signed-off-by: Damian Krolik <damian.krolik@nordicsemi.no>
  • Loading branch information
Damian-Nordic committed Jul 30, 2024
1 parent 5a4f11f commit 28dcbab
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
11 changes: 10 additions & 1 deletion config/nrfconnect/chip-module/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,6 @@ matter_generate_args_tmp_file()
# ==============================================================================

matter_build(chip
LIB_SHELL ${CONFIG_CHIP_LIB_SHELL}
LIB_TESTS ${CONFIG_CHIP_BUILD_TESTS}
DEVICE_INFO_EXAMPLE_PROVIDER ${CONFIG_CHIP_EXAMPLE_DEVICE_INFO_PROVIDER}
GN_DEPENDENCIES kernel
Expand All @@ -225,6 +224,16 @@ if (CONFIG_CHIP_MALLOC_SYS_HEAP_OVERRIDE)
)
endif()

if (CONFIG_CHIP_LIB_SHELL)
# Force pulling chip::Shell::Engine::RunMainLoop() in the final binary.
# Without this workaround, the linker script does not process the shell and
# init objects defined in MainLoopZephyr.cpp unless the Matter library or
# the Matter shell library is linked using the '--whole-archive' flag.
target_link_options(chip INTERFACE
-Wl,-u,_ZN4chip5Shell6Engine11RunMainLoopEv
)
endif()

# ==============================================================================
# Define 'chip-ota-image' target for building CHIP OTA image
# ==============================================================================
Expand Down
4 changes: 2 additions & 2 deletions src/lib/shell/MainLoopZephyr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,15 @@ int ExecCommandInShellThread(const struct shell * shell, size_t argc, char ** ar
return error == CHIP_NO_ERROR ? 0 : -ENOEXEC;
}

int RegisterCommands()
int RegisterMatterCommands()
{
Shell::Engine::Root().RegisterDefaultCommands();
return 0;
}

} // namespace

SYS_INIT(RegisterCommands, POST_KERNEL, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT);
SYS_INIT(RegisterMatterCommands, POST_KERNEL, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT);
SHELL_CMD_ARG_REGISTER(matter, NULL, "Matter commands", ExecCommandInShellThread, 1, CHIP_SHELL_MAX_TOKENS);

namespace chip {
Expand Down
4 changes: 2 additions & 2 deletions src/platform/Zephyr/SysHeapMalloc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ LockGuard::~LockGuard()
}
}

int initHeap()
int InitSysHeapMalloc()
{
sys_heap_init(&sHeap, sHeapMemory, sizeof(sHeapMemory));
return 0;
Expand All @@ -74,7 +74,7 @@ int initHeap()

// Initialize the heap in the POST_KERNEL phase to make sure that it is ready even before
// C++ static constructors are called (which happens prior to the APPLICATION initialization phase).
SYS_INIT(initHeap, POST_KERNEL, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT);
SYS_INIT(InitSysHeapMalloc, POST_KERNEL, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT);

namespace chip {
namespace DeviceLayer {
Expand Down

0 comments on commit 28dcbab

Please sign in to comment.