Skip to content

fix[klibc]: stop gcc from turning the kstring.c byte loops into memset/memcpy calls - #11965

Merged
Rbb666 merged 2 commits into
RT-Thread:masterfrom
sexenteen:fix/klibc-no-loop-distribute-patterns
Nov 2, 2026
Merged

Rbb666 merged 2 commits into
RT-Thread:masterfrom
sexenteen:fix/klibc-no-loop-distribute-patterns

Conversation

@sexenteen

@sexenteen sexenteen commented Nov 1, 2026 •

Copy link
Copy Markdown
Contributor

拉取/合并请求描述:(PR description)

[

为什么提交这份PR (why to submit this PR)

Fixes #6128.

rt_memset()/rt_memcpy()/rt_memmove() in src/klibc/kstring.c end with byte-wise loops. At
-O2/-Os, GCC recognises those loops (-ftree-loop-distribute-patterns) and replaces them
with calls to memset()/memcpy(). On a target built without a C library those symbols do not
exist, so the firmware fails to link - which is exactly what the issue reports for aarch64.

This is not aarch64-specific. Reproduced with arm-none-eabi-gcc 10.3.1 by building
bsp/qemu-vexpress-a9 with -Os (BUILD = 'release' in rtconfig.py, not part of this PR).
Before the change, build/kernel/src/klibc/kstring.o contains:

         U memset
...
     1c:  bl 0 <memset>
    264:  bl 0 <memset>

Both branches into memset are inside rt_memset() itself.

你的解决方案是什么 (what is your solution)

Compile the Libc group with -fno-tree-loop-distribute-patterns, as suggested in the issue.
The flag is only appended for GCC, so Keil/IAR builds are untouched:

local_cflags = ''
if rtconfig.CROSS_TOOL == 'gcc':
    local_cflags = ' -fno-tree-loop-distribute-patterns'

group = DefineGroup('Libc', src, depend = [''], LOCAL_CFLAGS = local_cflags)

Only src/klibc/SConscript is changed (+10/-1). No C source is modified, so targets that do
have a C library keep the same behaviour; on libc-less targets the loops simply stay in place.

请提供验证的bsp和config (provide the config and bsp)

Verification (nm -u / objdump -d on build/kernel/src/klibc/kstring.o):

build U memset bl memset inside rt_memset
-Os, before the change yes 2
-Os, after the change no 0
-O0 (BSP default), after the change no 0

Also checked standalone, using the exact rt_memset() body copied from kstring.c together with
the BSP CFLAGS: -O2, -Os and -O3 all emit U memset without the flag, and none with it.

Not covered: Keil/IAR (they do not receive this flag) and the aarch64 toolchain itself - the
problem was reproduced with arm-none-eabi-gcc, which performs the same optimisation.
]

当前拉取/合并请求的状态 Intent for your PR

必须选择一项 Choose one (Mandatory):

  • 本拉取/合并请求是一个草稿版本 This PR is for a code-review and is intended to get feedback
  • 本拉取/合并请求是一个成熟版本 This PR is mature, and ready to be integrated into the repo

代码质量 Code Quality:

我在这个拉取/合并请求中已经考虑了 As part of this pull request, I've considered the following:

  • 已经仔细查看过代码改动的对比 Already check the difference between PR and old code
  • 代码风格正确,包括缩进空格,命名及其他风格 Style guide is adhered to, including spacing, naming and other styles
  • 没有垃圾代码,代码尽量精简,不包含#if 0代码,不包含已经被注释了的代码 All redundant code is removed and cleaned up
  • 所有变更均有原因及合理的,并且不会影响到其他软件组件代码或BSP All modifications are justified and not affect other components or BSP
  • 对难懂代码均提供对应的注释 I've commented appropriately where code is tricky
  • 代码是高质量的 Code in this PR is of high quality
  • 已经使用clang-format 源码格式化工具确保格式符合RT-Thread代码规范 This PR has been formatted with clang-format and complies with RT-Thread code specification
  • 如果是新增bsp, 已经添加ci检查到.github/ALL_BSP_COMPILE.json 详细请参考链接BSP自查

@github-actions

github-actions Bot commented Nov 1, 2026

Copy link
Copy Markdown

👋 感谢您对 RT-Thread 的贡献!Thank you for your contribution to RT-Thread!

为确保代码符合 RT-Thread 的编码规范,请在你的仓库中执行以下步骤运行代码格式化工作流(如果格式化CI运行失败)。
To ensure your code complies with RT-Thread's coding style, please run the code formatting workflow by following the steps below (If the formatting of CI fails to run).


🛠 操作步骤 | Steps

  1. 前往 Actions 页面 | Go to the Actions page
    点击进入工作流 → | Click to open workflow →

  2. 点击 Run workflow | Click Run workflow

  • Use workflow from 保持默认分支(通常为 master)
    Keep the default branch (usually master) in Use workflow from
  • 在 branch 输入框填写 PR 分支 fix/klibc-no-loop-distribute-patterns
    Enter PR branch fix/klibc-no-loop-distribute-patterns in the branch field
  • 设置需排除的文件/目录(目录请以"/"结尾)
    Set files/directories to exclude (directories should end with "/")
  1. 等待工作流完成 | Wait for the workflow to complete
    格式化后的代码将作为独立提交推送至你的分支。
    The formatting changes will be pushed to your branch as a separate commit.

完成后,提交将自动更新至 fix/klibc-no-loop-distribute-patterns 分支,关联的 Pull Request 也会同步更新。
Once completed, commits will be pushed to the fix/klibc-no-loop-distribute-patterns branch automatically, and the related Pull Request will be updated.

如有问题欢迎联系我们,再次感谢您的贡献!💐
If you have any questions, feel free to reach out. Thanks again for your contribution!

@github-actions github-actions Bot added the Kernel PR has src relate code label Nov 1, 2026
@github-actions

github-actions Bot commented Nov 1, 2026 •

Copy link
Copy Markdown

📌 Code Review Assignment

🏷️ Tag: kernel

Reviewers: @GorrayLi @ReviewSun @hamburger-os @lianux-mm @wdfk-prog @xu18838022837

Changed Files (Click to expand)
  • src/klibc/SConscript

📊 Current Review Status (Last Updated: 2026-11-02 14:57 CST)


📝 Review Instructions

  1. 维护者可以通过单击此处来刷新审查状态: 🔄 刷新状态
    Maintainers can refresh the review status by clicking here: 🔄 Refresh Status

  2. 确认审核通过后评论 LGTM/lgtm
    Comment LGTM/lgtm after confirming approval

  3. PR合并前需至少一位维护者确认
    PR must be confirmed by at least one maintainer before merging

ℹ️ 刷新CI状态操作需要具备仓库写入权限。
ℹ️ Refresh CI status operation requires repository Write permission.

Comment thread src/klibc/SConscript Outdated
if rtconfig.CROSS_TOOL == 'gcc':
local_cflags = ' -fno-tree-loop-distribute-patterns'

group = DefineGroup('Libc', src, depend = [''], LOCAL_CFLAGS = local_cflags)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里需要排除 --clang-analyzer。这个模式下,CROSS_TOOL 仍然是 gcc,但 building.py 已经把 CFLAGS 转成了列表。传入字符串形式的 LOCAL_CFLAGS 后,local_group() 会执行列表与字符串相加,直接报 TypeError,静态分析无法继续。请在该模式下不要传 LOCAL_CFLAGS,设为空字符串也不行,因为构建脚本判断的是这个字段是否存在。

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已按你的建议改好(commit 9ceb217b):

group_args = {'depend': ['']}
if rtconfig.CROSS_TOOL == 'gcc' and not GetOption('clang-analyzer'):
    # --clang-analyzer 下 building.py 会把 CFLAGS 变成 list,
    # local_group() 里 list + str 会 TypeError,所以该模式不传这个字段
    group_args['LOCAL_CFLAGS'] = ' -fno-tree-loop-distribute-patterns'
group = DefineGroup('Libc', src, **group_args)
  • 常规构建:仍然传 -fno-tree-loop-distribute-patterns,行为与之前一致;
  • --clang-analyzer:不再传这个字段(不是传空字符串)。

再提供一个信息,可能对 CI 有用:我在 bsp/qemu-vexpress-a9 上试 scons --clang-analyzer 时发现,即使把 src/klibc/SConscript 还原成 master 版本(完全不传 LOCAL_CFLAGS),也会在 building.py:859 报同样的 TypeError。定位到的原因是另外几个组本来就用字符串形式传:

  • components/finsh/SConscript:12,15(LOCAL_CFLAGS = '' 然后 += '-Wstack-usage=...')
  • components/dfs/dfs_v1/SConscript:11,20(+= ' -std=c99')
  • components/drivers/spi/SConscript:9,39(同上)

也就是说这个 BSP 上的静态分析在此之前就会失败;我这条改动只是不再新增一个同样的触发点。如果希望一起治理,我可以另提一个 PR 专门处理这几处(不混进这个修复里)。

@github-actions

github-actions Bot commented Nov 2, 2026 •

Copy link
Copy Markdown

MemBrowse Memory Report · Explore what changed →

at32f415-start

  • RAM: .bss -8 B (-0.2%, 4,572 B / 32,768 B, total: 14% used)
  • ROM: .text +4 B (+0.0%, 65,424 B / 262,144 B, total: 25% used)

gd32105r-start

  • DATA: .bss -4 B (-0.1%, 3,748 B / 98,304 B, total: 4% used)

hc32f334

  • RAM: .bss -4 B (-0.1%, 5,328 B / 32,768 B, total: 16% used)

hpmicro-hpm5100evk

  • XPI0: .text -24 B (-0.0%, 102,924 B / 1,048,576 B, total: 10% used)

k230

  • SRAM: .bss -3,176 B, .data +40 B, .sbss +8 B, .stack +24 B (-0.3%, 1,206,068 B / 268,300,288 B, total: 0% used)

loongson-ls1cdev

  • Data: .bss -4 B (-0.0%, 59,808 B)

nuvoton-m487

  • CODE: .text +108 B (+0.0%, 356,308 B / 524,288 B, total: 68% used)
  • DATA: .bss -4 B (-0.0%, 101,928 B / 163,840 B, total: 62% used)

qemu-virt64-aarch64

  • Code: .text -1,024 B (-0.1%, 1,009,100 B)
  • Data: .bss +944 B (+0.1%, 1,068,104 B)

raspberry-pico-rp2040

  • FLASH: .text -8 B (-0.0%, 114,152 B / 2,097,152 B, total: 5% used)

stm32f407-rt-spark

  • RAM1: .bss -4 B (-0.1%, 5,472 B / 131,072 B, total: 4% used)

wch-ch32v208w-r0

  • FLASH: .text +56 B (+0.0%, 159,656 B / 491,520 B, total: 32% used)

x86

  • Data: .bss -32 B (-0.2%, 14,548 B)

xuantie-e901plus

  • DSRAM: .bss -8 B (-0.1%, 14,004 B / 524,288 B, total: 3% used)
  • ISRAM: .rodata +16 B, .text +48 B (+0.1%, 46,424 B / 131,072 B, total: 35% used)

No memory changes detected for:


Catch memory regressions in CI with MemBrowse. Free for open source.

@Rbb666
Rbb666 merged commit 2254560 into RT-Thread:master Nov 2, 2026
112 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Kernel PR has src relate code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

aarch64-gcc编译,rt_memset等函数编译问题

2 participants