一种将LLVM Pass集成到NDK中的通用方法

本文为看雪论坛优秀文章
看雪论坛作者ID:34r7hm4n
该方法有以下优点:
Windows、macOS、Linux通用,各NDK版本也通用
理论上加入自己的Pass后不会出现不兼容的问题
操作过程简单易懂
无法直接照搬OLLVM、Hikari、Armariris等现成项目的源码,需要手动做一些迁移
第一次编译比较耗时
一
操作流程
1、环境准备
本文以Windows+Linux虚拟机为例讲解,macOS下的操作大同小异。
以下使用的指令全部以root权限执行。
2、下载 llvm-android 源代码
curl https://storage.googleapis.com/git-repo-downloads/repo > /usr/bin/repo
chmod a+x /usr/bin/repo
curl https://mirrors.tuna.tsinghua.edu.cn/git/git-repo > /usr/bin/repo
chmod a+x /usr/bin/repo
12.0.8
based on r416183c1

Google的文档中给出了下载 llvm-android 源代码的方法,但这里默认下载的是最新版本:
mkdir llvm-toolchain && cd llvm-toolchain
repo init -u https://android.googlesource.com/platform/manifest -b llvm-toolchain
repo sync -c
mkdir llvm-toolchain && cd llvm-toolchain
repo init -u

继续执行:
repo -m manifest_7714059.xml
repo sync -c
mkdir llvm-toolchain && cd llvm-toolchain
repo init -u
https://mirrors.tuna.tsinghua.edu.cn/git/AOSP/platform/manifest -b llvm-toolchain
repo -m manifest_7714059.xml
repo sync -c

3、编译 llvm-android 源代码
apt install cmake bison

python toolchain/llvm_android/build.py --no-build linux
另外编译的时候最好把虚拟机内存开到8G以上,我开的是8G内存,编译的时候还会因为内存不足时不时中断,如果中断了重新运行编译指令就好。
编译结束后可以在out文件夹中找到编译好的内容:

4、加入自己的 Pass 并重新编译
此时我们需要向toolchain/llvm-project/llvm/lib/Transforms/Obfuscation/中加入自己的代码:




python toolchain/llvm_android/build.py --no-build linux


少了一些东西,但是无关紧要,我们直接替换就好。
二
效果测试

随便写一个Native项目测试:

设置NDK地址:

加上混淆参数:

编译然后查看混淆效果:

X86架构和ARM架构均混淆成功:


看雪ID:34r7hm4n
https://bbs.pediy.com/user-home-910514.htm

# 往期推荐


球分享

球点赞

球在看

点击“阅读原文”,了解更多!
[广告]赞助链接:
关注数据与安全,洞悉企业级服务市场:https://www.ijiandao.com/
让资讯触达的更精准有趣:https://www.0xu.cn/

随时掌握互联网精彩
赞助链接