Link Lab:Phase 2 【输出学号】

本阶段主题是链接中的“重定位”。两次重定位,一次是绝对地址重定位,一次是PC相对地址重定位。

本题目标依旧是输出学号,反汇编phase2.o,看到学号“0000000000”已经存放在只读数据区了。现在任务就是改do_pheas的指令和重定位表(.rel.test)的内容。

step1 仿照phase1的代码,先写出指令的框架

00000030 <do_phase>:
  30:	55                   	push   %ebp
  31:	89 e5                	mov    %esp,%ebp
  33:	83 ec 08             	sub    $0x8,%esp
  36:	b8 xx xx xx xx       	mov    xxxxxxxx,%eax
  3b:	83 ec 0c             	sub    $0xc,%esp
  3e:	50                   	push   %eax
  3f:	e8 xx xx xx xx       	call   xxxxxxxx
  44:	83 c4 10             	add    $0x10,%esp
  47:	90                   	nop
  48:	c9                   	leave  
  49:	c3                   	ret    
  4a:	90                   	nop
  4b:	90                   	nop
  4c:	90                   	nop
  4d:	90                   	nop
  4e:	90                   	nop
  4f:	90                   	nop
  50:	90                   	nop
  51:	90                   	nop
  52:	90                   	nop
  53:	90                   	nop
  54:	5d                   	pop    %ebp
  55:	c3                   	ret    

两个都是x的地方就是接下来要改的地方。

step2 判断是绝对地址重定位还是PC相对地址重定位

有两种办法.

办法一:参考phase1链接后的可执行文件的机器代码

0804845d <do_phase>:
 804845d:	55                   	push   %ebp
 804845e:	89 e5                	mov    %esp,%ebp
 8048460:	83 ec 08             	sub    $0x8,%esp
 8048463:	b8 61 a0 04 08       	mov    $0x804a061,%eax
 8048468:	83 ec 0c             	sub    $0xc,%esp
 804846b:	50                   	push   %eax
 804846c:	e8 7f fe ff ff       	call   80482f0 <puts@plt>
 8048471:	83 c4 10             	add    $0x10,%esp
 8048474:	90                   	nop
 8048475:	c9                   	leave  
 8048476:	c3                   	ret    
 8048477:	66 90                	xchg   %ax,%ax
 8048479:	66 90                	xchg   %ax,%ax
 804847b:	66 90                	xchg   %ax,%ax
 804847d:	66 90                	xchg   %ax,%ax
 804847f:	90                   	nop

如上,ax那里是绝对地址,call那里用的e8,就是PC相对

办法二:看phase2.o的重定位节,命令为readelf -r phase2.o

类型那一列直接写明了。

step3 改重定位表

找出框架,eax那行需要重定位的地方的字节是37,call那是40,则用hexedit编辑phase2.o,改重定位表。

00000030 <do_phase>:
  30:	55                   	push   %ebp
  31:	89 e5                	mov    %esp,%ebp
  33:	83 ec 08             	sub    $0x8,%esp
  36:	b8 xx xx xx xx       	mov    xxxxxxxx,%eax
  3b:	83 ec 0c             	sub    $0xc,%esp
  3e:	50                   	push   %eax
  3f:	e8 xx xx xx xx       	call   xxxxxxxx
  44:	83 c4 10             	add    $0x10,%esp
  47:	90                   	nop
  48:	c9                   	leave  
  49:	c3                   	ret    
  4a:	90                   	nop
  4b:	90                   	nop
  4c:	90                   	nop
  4d:	90                   	nop
  4e:	90                   	nop
  4f:	90                   	nop
  50:	90                   	nop
  51:	90                   	nop
  52:	90                   	nop
  53:	90                   	nop
  54:	5d                   	pop    %ebp
  55:	c3                   	ret    

step4 代码填完整

这一步需要了解重定位的完整过程。

00000030 <do_phase>:
  30:	55                   	push   %ebp
  31:	89 e5                	mov    %esp,%ebp
  33:	83 ec 08             	sub    $0x8,%esp
  36:	b8 00 00 00 00       	mov    xxxxxxxx,%eax
  3b:	83 ec 0c             	sub    $0xc,%esp
  3e:	50                   	push   %eax
  3f:	e8 fc ff ff ff      	call   xxxxxxxx
  44:	83 c4 10             	add    $0x10,%esp
  47:	90                   	nop
  48:	c9                   	leave  
  49:	c3                   	ret    
  4a:	90                   	nop
  4b:	90                   	nop
  4c:	90                   	nop
  4d:	90                   	nop
  4e:	90                   	nop
  4f:	90                   	nop
  50:	90                   	nop
  51:	90                   	nop
  52:	90                   	nop
  53:	90                   	nop
  54:	5d                   	pop    %ebp
  55:	c3                   	ret    

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值