Aarch64应用程序热补丁(一)

示例

外部动态库

mytest.c:

#include <stdio.h>

static void mytest_print(void)
{
    printf("[%s:%d] %s\n", __FILE__, __LINE__, __FUNCTION__);
}

void mytest2(void)
{
    printf("[%s:%d] %s\n", __FILE__, __LINE__, __FUNCTION__);
}

void mytest(void)
{
    mytest_print();
}

编译

aarch64-linux-gnu-gcc -g -shared -fPIC mytest.c -o libmytest.so

主函数

mymain.c:

#include <stdio.h>
#include <time.h>
#include <unistd.h>

extern void mytest2(void);
extern void mytest(void);

int main(int argc, const char *argv[])
{
    while (1) {
        sleep(1);

        mytest();
    }

    return 0;
}

编译

aarch64-linux-gnu-gcc -g -L. -Wl,-rpath=. mymain.c -lmytest

背景

  • 在上述示例main中,通过mytest2替换mytest的调用。

原理

  • 在本实例中主要利用动态库的重定位原理来,将重定位地址直接执行强制赋值操作,这样就会将原mytest的跳转地址修改为mytest2的跳转地址,完成了mytest2对mytest函数调用的替换操作。

查看a.out中的需要重定位的符号

  • /opt/rk3328/aarch64/bin/aarch64-linux-gnu-readelf -r a.out
  • Relocation section '.rela.dyn' at offset 0x5a0 contains 1 entries:
      Offset          Info           Type           Sym. Value    Sym. Name + Addend
    000000410ad8  000900000401 R_AARCH64_GLOB_DA 0000000000000000 __gmon_start__ + 0
    
    Relocation section '.rela.plt' at offset 0x5b8 contains 5 entries:
      Offset          Info           Type           Sym. Value    Sym. Name + Addend
    000000410af8  000700000402 R_AARCH64_JUMP_SL 0000000000000000 __libc_start_main@GLIBC_2.17 + 0
    000000410b00  000800000402 R_AARCH64_JUMP_SL 0000000000000000 sleep@GLIBC_2.17 + 0
    000000410b08  000900000402 R_AARCH64_JUMP_SL 0000000000000000 __gmon_start__ + 0
    000000410b10  000a00000402 R_AARCH64_JUMP_SL 0000000000000000 abort@GLIBC_2.17 + 0
    000000410b18  000b00000402 R_AARCH64_JUMP_SL 0000000000000000 mytest + 0
  • 注:在此处只关心mytest的重定位过程。此处mytest的重定位地址为0x410b18,即该地址指向的值为函数mytest的实际地址。

查看0x410b18的实际值

(gdb) x /a 0x410b18
0x410b18 <mytest@got.plt>:  0x7fb7fc1820 <mytest>

(gdb) print mytest
$1 = {void (void)} 0x7fb7fc1820 <mytest>

替换过程

  • 0x410b18地址对应的值为mytest对应函数的实际地址
  • 只要将0x410b18地址对应的值替换为mytest2的实际地址,即可完成mytest2对mytest的替换操作
(gdb) set *0x410b18 = 0x7fb7fc17e8
(gdb) continue 

替换前输出

[mytest.c:5] mytest_print
[mytest.c:5] mytest_print

替换后输出

[mytest.c:10] mytest2
[mytest.c:10] mytest2
[mytest.c:10] mytest2
[mytest.c:10] mytest2

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值