登录
注册
开源
企业版
高校版
搜索
帮助中心
使用条款
关于我们
开源
企业版
高校版
私有云
模力方舟
AI 队友
登录
注册
代码拉取完成,页面将自动刷新
仓库状态说明
Watch
不关注
关注所有动态
仅关注版本发行动态
关注但不提醒动态
31
Star
96
Fork
619
OpenHarmony
/
third_party_musl
关闭
代码
Issues
60
Pull Requests
29
Wiki
统计
流水线
服务
JavaDoc
PHPDoc
质量分析
Jenkins for Gitee
腾讯云托管
腾讯云 Serverless
悬镜安全
阿里云 SAE
Codeblitz
SBOM
开发画像分析
我知道了,不再自动展开
更新失败,请稍后重试!
移除标识
内容风险标识
本任务被
标识为内容中包含有代码安全 Bug 、隐私泄露等敏感信息,仓库外成员不可访问
.so文件.note.ohos.ident读取错误问题
已完成
#IAMWM4
缺陷
yangbinfa
创建于
2024-08-27 19:12
**【任务描述】** 使用openharmony中llvm编译链接库中so文件,发现so文件中的.note.ohos.ident段读取有问题,使用`llvm-readelf` 指令读取so文件时报错`llvm-readelf: warning: 'libcrtbrandnew.so': unable to read notes from the SHT_NOTE section with index 2: ELF note overflows container` **【解决方案】** 该段note定义的汇编代码有问题。openharmony项目下,`"openharmony\third_party\musl\arch\generic\crtbrand.s"`该汇编文件编译后的so文件出现该读取错误。对该文件进行一定修改后可修复此问题。llvm-readelf指令阅读elf文件时,会按照4个字节长度拆分elf文件,不补齐就会出现解读错误的情况。name字符串后增加了一次对齐操作:.balign 4,确保描述符字段 desc 是 4 字节对齐的。即可解决此问题。 **【源码分析】** 根据报错信息,找到该错误定义位置在`llvm/toolchain/llvm-project/llvm/include/llvm/Object/ELFTypes.h` 与报错相关代码主要有三部分: 报错定义位置 ``` // Stop iteration and indicate an overflow. void stopWithOverflowError() { Nhdr = nullptr; *Err = make_error<StringError>("ELF note overflows container", object_error::parse_failed); } // Advance Nhdr by NoteSize bytes, starting from NhdrPos. // // Assumes NoteSize <= RemainingSize. Ensures Nhdr->getSize() <= RemainingSize // upon returning. Handles stopping iteration when reaching the end of the // container, either cleanly or with an overflow error. void advanceNhdr(const uint8_t *NhdrPos, size_t NoteSize) { RemainingSize -= NoteSize; if (RemainingSize == 0u) { // Ensure that if the iterator walks to the end, the error is checked // afterwards. *Err = Error::success(); Nhdr = nullptr; } else if (sizeof(*Nhdr) > RemainingSize) stopWithOverflowError(); else { Nhdr = reinterpret_cast<const Elf_Nhdr_Impl<ELFT> *>(NhdrPos + NoteSize); if (Nhdr->getSize() > RemainingSize) stopWithOverflowError(); else *Err = Error::success(); } } ``` 报错函数调用位置 ``` Elf_Note_Iterator_Impl(const uint8_t *Start, size_t Size, Error &Err) : RemainingSize(Size), Err(&Err) { consumeError(std::move(Err)); assert(Start && "ELF note iterator starting at NULL"); advanceNhdr(Start, 0u); } public: Elf_Note_Iterator_Impl &operator++() { assert(Nhdr && "incremented ELF note end iterator"); const uint8_t *NhdrPos = reinterpret_cast<const uint8_t *>(Nhdr); size_t NoteSize = Nhdr->getSize(); advanceNhdr(NhdrPos, NoteSize); return *this; } ``` getSize()函数 ``` /// Note header template <class ELFT> struct Elf_Nhdr_Impl { LLVM_ELF_IMPORT_TYPES_ELFT(ELFT) Elf_Word n_namesz; Elf_Word n_descsz; Elf_Word n_type; /// The alignment of the name and descriptor. /// /// Implementations differ from the specification here: in practice all /// variants align both the name and descriptor to 4-bytes. static const unsigned int Align = 4; /// Get the size of the note, including name, descriptor, and padding. size_t getSize() const { return sizeof(*this) + alignTo<Align>(n_namesz) + alignTo<Align>(n_descsz); } }; ``` 可以看到,源码中通过迭代的方式获取elf文件中的每个note段信息。每次迭代中,会比较全部note段剩余字节数量(RemainingSize)和.note段中head部分的长度(sizeof(*Nhdr)),而RemainingSize的计算使用上次剩余长度减去getSize()函数得到的当前note段长度,getSize()函数中使用了静态常量Align=4,计算长度时使用alignTo(Align)函数求得4位对齐后note段总长度,因此当实际note段未进行对齐时,对于最后一个note段,计算得到的RemainingSize为负数,小于sizeof(*Nhdr)(一般为12个字节),即会执行stopWithOverflowError(),从而报错。 **【修复测试过程】** 首先下载openahrmony全量代码,修改上述解决方案提到的文件,使用下面的指令重新编译sdk:`./build.sh --product-name ohos-sdk --gn-args sdk_platform=linux --gn-args is_use_check_deps=false` 注意要把之前编译的out文件夹删除掉,不然增量编译不会对该汇编文件进行重新编译。 编译完成后,使用编译得到的clang工具和sysroot根目录进行交叉编译测试,helloworld编译指令:`/mnt/data/y00906355/openharmony/out/sdk/packages/ohos-sdk/linux/native/llvm/bin/clang --sysroot=/mnt/data/y00906355/openharmony/out/sdk/packages/ohos-sdk/linux/native/sysroot --target=x86_64-linux-ohos-fPIC -shared helloworld.c -o helloworld.so` 再使用llvm-readelf指令读取helloworld.so,不再报错。
**【任务描述】** 使用openharmony中llvm编译链接库中so文件,发现so文件中的.note.ohos.ident段读取有问题,使用`llvm-readelf` 指令读取so文件时报错`llvm-readelf: warning: 'libcrtbrandnew.so': unable to read notes from the SHT_NOTE section with index 2: ELF note overflows container` **【解决方案】** 该段note定义的汇编代码有问题。openharmony项目下,`"openharmony\third_party\musl\arch\generic\crtbrand.s"`该汇编文件编译后的so文件出现该读取错误。对该文件进行一定修改后可修复此问题。llvm-readelf指令阅读elf文件时,会按照4个字节长度拆分elf文件,不补齐就会出现解读错误的情况。name字符串后增加了一次对齐操作:.balign 4,确保描述符字段 desc 是 4 字节对齐的。即可解决此问题。 **【源码分析】** 根据报错信息,找到该错误定义位置在`llvm/toolchain/llvm-project/llvm/include/llvm/Object/ELFTypes.h` 与报错相关代码主要有三部分: 报错定义位置 ``` // Stop iteration and indicate an overflow. void stopWithOverflowError() { Nhdr = nullptr; *Err = make_error<StringError>("ELF note overflows container", object_error::parse_failed); } // Advance Nhdr by NoteSize bytes, starting from NhdrPos. // // Assumes NoteSize <= RemainingSize. Ensures Nhdr->getSize() <= RemainingSize // upon returning. Handles stopping iteration when reaching the end of the // container, either cleanly or with an overflow error. void advanceNhdr(const uint8_t *NhdrPos, size_t NoteSize) { RemainingSize -= NoteSize; if (RemainingSize == 0u) { // Ensure that if the iterator walks to the end, the error is checked // afterwards. *Err = Error::success(); Nhdr = nullptr; } else if (sizeof(*Nhdr) > RemainingSize) stopWithOverflowError(); else { Nhdr = reinterpret_cast<const Elf_Nhdr_Impl<ELFT> *>(NhdrPos + NoteSize); if (Nhdr->getSize() > RemainingSize) stopWithOverflowError(); else *Err = Error::success(); } } ``` 报错函数调用位置 ``` Elf_Note_Iterator_Impl(const uint8_t *Start, size_t Size, Error &Err) : RemainingSize(Size), Err(&Err) { consumeError(std::move(Err)); assert(Start && "ELF note iterator starting at NULL"); advanceNhdr(Start, 0u); } public: Elf_Note_Iterator_Impl &operator++() { assert(Nhdr && "incremented ELF note end iterator"); const uint8_t *NhdrPos = reinterpret_cast<const uint8_t *>(Nhdr); size_t NoteSize = Nhdr->getSize(); advanceNhdr(NhdrPos, NoteSize); return *this; } ``` getSize()函数 ``` /// Note header template <class ELFT> struct Elf_Nhdr_Impl { LLVM_ELF_IMPORT_TYPES_ELFT(ELFT) Elf_Word n_namesz; Elf_Word n_descsz; Elf_Word n_type; /// The alignment of the name and descriptor. /// /// Implementations differ from the specification here: in practice all /// variants align both the name and descriptor to 4-bytes. static const unsigned int Align = 4; /// Get the size of the note, including name, descriptor, and padding. size_t getSize() const { return sizeof(*this) + alignTo<Align>(n_namesz) + alignTo<Align>(n_descsz); } }; ``` 可以看到,源码中通过迭代的方式获取elf文件中的每个note段信息。每次迭代中,会比较全部note段剩余字节数量(RemainingSize)和.note段中head部分的长度(sizeof(*Nhdr)),而RemainingSize的计算使用上次剩余长度减去getSize()函数得到的当前note段长度,getSize()函数中使用了静态常量Align=4,计算长度时使用alignTo(Align)函数求得4位对齐后note段总长度,因此当实际note段未进行对齐时,对于最后一个note段,计算得到的RemainingSize为负数,小于sizeof(*Nhdr)(一般为12个字节),即会执行stopWithOverflowError(),从而报错。 **【修复测试过程】** 首先下载openahrmony全量代码,修改上述解决方案提到的文件,使用下面的指令重新编译sdk:`./build.sh --product-name ohos-sdk --gn-args sdk_platform=linux --gn-args is_use_check_deps=false` 注意要把之前编译的out文件夹删除掉,不然增量编译不会对该汇编文件进行重新编译。 编译完成后,使用编译得到的clang工具和sysroot根目录进行交叉编译测试,helloworld编译指令:`/mnt/data/y00906355/openharmony/out/sdk/packages/ohos-sdk/linux/native/llvm/bin/clang --sysroot=/mnt/data/y00906355/openharmony/out/sdk/packages/ohos-sdk/linux/native/sysroot --target=x86_64-linux-ohos-fPIC -shared helloworld.c -o helloworld.so` 再使用llvm-readelf指令读取helloworld.so,不再报错。
评论 (
2
)
登录
后才可以发表评论
状态
已完成
待办的
已确认
技术评审中
修复中
验收中
已完成
已取消
已拒绝
挂起
负责人
未设置
标签
waiting_for_assign
异常关闭
未设置
项目
未立项任务
未立项任务
里程碑
未关联里程碑
未关联里程碑
Pull Requests
未关联
未关联
关联的 Pull Requests 被合并后可能会关闭此 issue
分支
未关联
分支 (
-
)
标签 (
-
)
开始日期   -   截止日期
-
置顶选项
不置顶
置顶等级:高
置顶等级:中
置顶等级:低
优先级
不指定
严重
主要
次要
不重要
预计工期
(小时)
参与者(2)
1
https://gitee.com/openharmony/third_party_musl.git
git@gitee.com:openharmony/third_party_musl.git
openharmony
third_party_musl
third_party_musl
点此查找更多帮助
搜索帮助
Git 命令在线学习
如何在 Gitee 导入 GitHub 仓库
Git 仓库基础操作
企业版和社区版功能对比
SSH 公钥设置
如何处理代码冲突
仓库体积过大,如何减小?
如何找回被删除的仓库数据
Gitee 产品配额说明
GitHub仓库快速导入Gitee及同步更新
什么是 Release(发行版)
将 PHP 项目自动发布到 packagist.org
仓库举报
回到顶部
登录提示
该操作需登录 Gitee 帐号,请先登录后再操作。
立即登录
没有帐号,去注册