Repository navigation
[dlmodule] fix: uninitialized globals in relocatable modules - #11915
usualtreee wants to merge 1 commit into
Conversation
Fix the loader of relocatable (ET_REL) dynamic modules so that uninitialized global variables get their own storage and correct addresses: 1. Advance the allocation pointer after loading a BSS (SHT_NOBITS) section, so multiple NOBITS sections no longer overlap in memory. 2. Relocate section symbols with section_load_addr - sec_sh_addr + sym->st_value, matching how text symbols are already handled. 3. Allocate a common block for SHN_COMMON symbols (tentative definitions of global variables) and relocate every reference to it instead of silently skipping them. 4. Build the module symbol table from .symtab for ET_REL modules so that dlsym() can resolve the module's global functions. Verified on bsp/qemu-vexpress-a9 with CONFIG_RT_USING_MODULE=y: before the fix an ET_REL module fails dlsym(), after the fix its globals get correct non-overlapping addresses and round-trip reads/writes succeed, and ET_DYN (-shared) modules are unaffected. Refs: RT-Thread#5705
|
👋 感谢您对 RT-Thread 的贡献!Thank you for your contribution to RT-Thread! 为确保代码符合 RT-Thread 的编码规范,请在你的仓库中执行以下步骤运行代码格式化工作流(如果格式化CI运行失败)。 🛠 操作步骤 | Steps
完成后,提交将自动更新至 如有问题欢迎联系我们,再次感谢您的贡献!💐 |
📌 Code Review Assignment🏷️ Tag: componentsReviewers: @Maihuanyi Changed Files (Click to expand)
🏷️ Tag: components_libcReviewers: @GorrayLi @mysterywolf Changed Files (Click to expand)
📊 Current Review Status (Last Updated: 2026-10-21 21:10 CST)
📝 Review Instructions
|
拉取/合并请求描述:(PR description)
为什么提交这份PR (why to submit this PR)
修复 issue #5705(动态模块中未初始化全局变量访问异常,2022-03 报告,至今 open)。该问题在最新 master(1c801efa)上仍可复现:
dlmodule_load_relocated_object()(ET_REL 动态模块加载路径,Cortex-M 等使用 RT_USING_MODULE 的 BSP 走此路径)存在以下缺陷,导致动态模块中未初始化全局变量的地址重叠/无法重定位:你的解决方案是什么 (what is your solution)
ptr += shdr[index].sh_size;;section_load_addr - sec_sh_addr + sym->st_value,与 text 段处理方式一致;st_value(对齐要求)对齐、st_size累计计入模块总大小;重定位阶段先于段名匹配处理 SHN_COMMON(避免越界读 shdr[0xfff2]),地址 = 公共块基址 + 符号在块内偏移(与尺寸核算共用同一算法,保证一致);修改文件:components/libc/posix/libdl/dlelf.c、dlelf.h(新增 SHN_COMMON 宏)。
请提供验证的bsp和config (provide the config and bsp)
BSP: bsp/qemu-vexpress-a9
.config: 需要开启
CONFIG_RT_USING_MODULE=y(RT_USING_DFS / RT_USING_DFS_ROMFS / RT_USING_POSIX_FS 为该 BSP 默认配置,无需改动)验证方法:qemu-system-arm 运行时加载三个测试动态模块:
-shared -fPIC -fcommon流程)-r、非 PIC、-fcommon;rx_sem/serial 为 SHN_COMMON),覆盖本次修复路径每个模块检查未初始化全局变量地址互不重叠,并做特征值写入回读(serial=0xdeadbeef、rx_sem=0xaa55)验证内存互不踩踏。
dlsym FAILED;action: (待触发;可在 fork 仓库 Actions 中运行 manual_dist 编译 qemu-vexpress-a9 后贴链接)
当前拉取/合并请求的状态 Intent for your PR
必须选择一项 Choose one (Mandatory):
代码质量 Code Quality:
我在这个拉取/合并请求中已经考虑了 As part of this pull request, I've considered the following:
#if 0代码,不包含已经被注释了的代码 All redundant code is removed and cleaned up