Skip to content

Commit 722fd6e

Browse files
committed
fix(cmake): add -z nostart-stop-gc for LLVM/Clang toolchains
When using LLVM-based toolchains (e.g. ST STM32CubeCLT arm-clang), ld.lld discards __start_/__stop_ encapsulation symbols under --gc-sections, causing undefined symbol errors for ctshell_cmd_section. Export CTSHELL_LINK_OPTIONS with -z nostart-stop-gc automatically when LLVM linker is detected. Also update porting docs (en/zh) to clarify GCC vs LLVM vs MDK-ARM linker script requirements.
1 parent 9b5cf1c commit 722fd6e

File tree

3 files changed

+30
-4
lines changed

3 files changed

+30
-4
lines changed

CMakeLists.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,3 +112,13 @@ else()
112112

113113
set(CTSHELL_DEFINITIONS ${CTSHELL_DEFINITIONS} CACHE INTERNAL "ctshell definitions")
114114
endif()
115+
116+
# --- Linker options for section-based command registration ---
117+
# GCC (ld.bfd) handles __start_/__stop_ symbols for orphan sections automatically.
118+
# LLVM lld requires -z nostart-stop-gc to prevent these symbols from being
119+
# discarded when --gc-sections is enabled.
120+
set(CTSHELL_LINK_OPTIONS "")
121+
if(CMAKE_LINKER MATCHES "lld" OR CMAKE_C_COMPILER_ID STREQUAL "Clang" OR CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
122+
list(APPEND CTSHELL_LINK_OPTIONS "-z" "nostart-stop-gc")
123+
endif()
124+
set(CTSHELL_LINK_OPTIONS ${CTSHELL_LINK_OPTIONS} CACHE INTERNAL "ctshell linker options")

docs/en/quickstart/porting.rst

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,17 @@ RTOS Environment: It is recommended to create a dedicated task to run the Shell.
9292
9393
6. Linker Script Modification
9494

95-
gcc: No need to modify.
95+
**GCC (ld.bfd):** No need to modify the linker script.
9696

97-
MDK-ARM(v5 and v6): Add in scatter file, example:
97+
**LLVM/Clang (ld.lld):** If you are using an LLVM-based toolchain (e.g., ST's STM32CubeCLT with arm-clang), the linker script does not need modification either. Simply append the following to the end of your ``CMakeLists.txt``:
98+
99+
.. code-block:: cmake
100+
101+
target_link_options(${CMAKE_PROJECT_NAME} PRIVATE ${CTSHELL_LINK_OPTIONS})
102+
103+
``${CTSHELL_LINK_OPTIONS}`` is exported by ctshell and automatically adds ``-z nostart-stop-gc`` when an LLVM linker is detected, preventing ``__start_ctshell_cmd_section`` and ``__stop_ctshell_cmd_section`` symbols from being discarded under ``--gc-sections``.
104+
105+
**MDK-ARM (v5 and v6):** Add in scatter file, example:
98106

99107
.. code-block:: c
100108

docs/zh/quickstart/porting.rst

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,17 @@ RTOS 环境: 建议创建一个独立的任务来运行 Shell。
9292
9393
6. 链接脚本修改
9494

95-
gcc:无需修改
95+
**GCC (ld.bfd):** 无需修改链接脚本
9696

97-
MDK-ARM(v5 and v6):在scatter file中添加示例如下:
97+
**LLVM/Clang (ld.lld):** 如果你使用的是基于 LLVM 的工具链(如 ST 的 STM32CubeCLT arm-clang),则无需修改链接脚本,只需将以下内容追加到你的 ``CMakeLists.txt`` 末尾即可:
98+
99+
.. code-block:: cmake
100+
101+
target_link_options(${CMAKE_PROJECT_NAME} PRIVATE ${CTSHELL_LINK_OPTIONS})
102+
103+
``${CTSHELL_LINK_OPTIONS}`` 由 ctshell 导出,会在检测到 LLVM 链接器时自动添加 ``-z nostart-stop-gc``,防止 ``__start_ctshell_cmd_section`` 和 ``__stop_ctshell_cmd_section`` 符号在 ``--gc-sections`` 下被回收。
104+
105+
**MDK-ARM(v5 and v6):** 在scatter file中添加示例如下:
98106

99107
.. code-block:: c
100108

0 commit comments

Comments
 (0)