Skip to content

[教程] 使用 CodeArts IDE 的 binary-sign-tool 命令为任意 ELF 签名,让其有权限在鸿蒙 HiShell 中执行,免去 hnp 打包 #150

@SwimmingTiger

Description

@SwimmingTiger

需要鸿蒙6系统,鸿蒙5不支持。打开开发者模式后,从应用商店的应用尝鲜里安装 CodeArts IDE。
如果 CodeArts IDE 的终端打不开,请先关闭 CodeArts IDE 的窗口(必须关闭窗口,光重启没用),然后重启电脑。

注意:签名只是让ELF有权限运行,不能让原本就不兼容鸿蒙的ELF在鸿蒙里正常运行。
如果ELF不是静态链接的程序,并且依赖glibc,那大概率不能在鸿蒙内正常运行。
如果ELF依赖其他.so库,那所有.so库也都需要签名,并且需要配置LD_LIBRARY_PATH环境变量指向so所在文件夹。

如果不想使用 CodeArts IDE,也可以从华为应用商店安装 DevBox app,然后直接在鸿蒙 HiShell 终端里运行 binary-sign-tool 命令。
https://appgallery.huawei.com/app/detail?id=com.develop.opensource.devbox&channelId=SHARE&source=appshare
不过 DevBox 目前没有 objcopy 命令,你可以从 CodeArts IDE 里拷贝一个出来用。

签名命令:

binary-sign-tool sign -inFile ELF文件名 -outFile ELF文件名 -selfSign 1

批量签名命令:

签名当前目录及其子目录下的所有ELF文件:

find . -type f -exec file '{}' ';' | grep --line-buffered ': ELF ' | awk -F': ELF ' '{print $1}' | while read f; do echo "$f"; binary-sign-tool sign -inFile "$f" -outFile "$f" -selfSign 1; done

注意:while循环与fish不兼容,请在zshbash中执行。

移除签名:

objcopy --remove-section .codesign ELF文件名

如果签名已存在,重新签名之前要先移除旧签名,否则会报错:.codesign section already exists

localhost ~/work/test % binary-sign-tool sign -inFile busybox -outFile busybox -selfSign 1
[Error] [sign_elf.cpp] [WriteCodeSignBlock] [134] [SignElf] .codesign section already exists
[Error] [sign_elf.cpp] [Sign] [54] [SignElf] WriteCodeSignBlock error
[Error] [self_sign_sign_provider.cpp] [SignElf] [29] [SignElf] sign elf failed
12-25 12:39:55.715 INFO  - sign failed

批量移除签名:

find . -type f -exec file '{}' ';' | grep --line-buffered ': ELF ' | awk -F': ELF ' '{print $1}' | while read f; do echo "$f"; objcopy --remove-section .codesign "$f"; done

注意:while循环与fish不兼容,请在zshbash中执行。

示例:

以这个从 Termony 项目里提取的 busybox 命令为例:busybox.zip

签名前:

Image

签名:

binary-sign-tool sign -inFile busybox -outFile busybox -selfSign 1
Image

签名后:

Image

已知限制:

  1. 自签名ELF似乎没有JIT权限。想要JIT权限还是得打包成public hnp塞到hap里安装。
Image
  1. 自签名ELF似乎也没有调用 tcsetpgrp、tcsetattr 等终端控制 API 的权限,导致 htop 命令不能正常输出,less 命令完全无法使用。打包成public hnp塞到hap里安装是没问题的。
Image
  1. 签名绑定机器,在一台机器上签名的ELF复制到别的机器上运行不了,别的机器必须重新签名。

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions