首页
社区
课程
招聘
[翻译]ARM汇编简介(二)ARM指令集
发表于: 2018-6-15 17:17 9078

[翻译]ARM汇编简介(二)ARM指令集

2018-6-15 17:17
9078

ARM processors have two main states they can operate in (let’s not count Jazelle here), ARM and Thumb. These states have nothing to do with privilege levels. For example, code running in SVC mode can be either ARM or Thumb. The main difference between these two states is the instruction set, where instructions in ARM state are always 32-bit, and  instructions in Thumb state are 16-bit (but can be 32-bit). Knowing when and how to use Thumb is especially important for our ARM exploit development purposes. When writing ARM shellcode, we need to get rid of NULL bytes and using 16-bit Thumb instructions instead of 32-bit ARM instructions reduces the chance of having them.

As mentioned before, there are different Thumb versions. The different naming is just for the sake of differentiating them from each other (the processor itself will always refer to it as Thumb).

Thumb-1 (16-bit instructions): was used in ARMv6 and earlier architectures.

Thumb-2 (16-bit and 32-bit instructions): extents Thumb-1 by adding more instructions and allowing them to be either 16-bit or 32-bit wide (ARMv6T2, ARMv7).

ThumbEE: includes some changes and additions aimed for dynamically generated code (code compiled on the device either shortly before or during execution).

Differences between ARM and Thumb:

Conditional execution: All instructions in ARM state support conditional execution. Some ARM processor versions allow conditional execution in Thumb by using the IT instruction. Conditional execution leads to higher code density because it reduces the number of instructions to be executed and reduces the number of expensive branch instructions.

32-bit ARM and Thumb instructions: 32-bit Thumb instructions have a .w suffix.

The barrel shifter is another unique ARM mode feature. It can be used to shrink multiple instructions into one. For example, instead of using two instructions for a multiply (multiplying register by 2 and using MOV to store result into another register), you can include the multiply inside a MOV instruction by using shift left by 1 -> Mov  R1, R0, LSL #1      ; R1 = R0 * 2

To switch the state in which the processor executes in, one of two conditions have to be met:

We can use the branch instruction BX (branch and exchange) or BLX (branch, link, and exchange) and set the destination register’s least significant bit to 1. This can be achieved by adding 1 to an offset, like 0x5530 + 1. You might think that this would cause alignment issues, since instructions are either 2- or 4-byte aligned. This is not a problem because the processor will ignore the least significant bit. More details in Part 6: Conditional Execution and Branching.

We know that we are in Thumb mode if the T bit in the current program status register is set.

本篇是ARM系列基础教程的第三篇,ARM指令集。原文链接 https://azeria-labs.com/arm-instruction-set-part-3/

ARM processors have two main states they can operate in (let’s not count Jazelle here), ARM and Thumb. These states have nothing to do with privilege levels. For example, code running in SVC mode can be either ARM or Thumb. The main difference between these two states is the instruction set, where instructions in ARM state are always 32-bit, and  instructions in Thumb state are 16-bit (but can be 32-bit). Knowing when and how to use Thumb is especially important for our ARM exploit development purposes. When writing ARM shellcode, we need to get rid of NULL bytes and using 16-bit Thumb instructions instead of 32-bit ARM instructions reduces the chance of having them.

ARM处理器有两种可让我们操作的状态(我们就不把 Jazelle 考虑在内了),他们是ARM和Thumb。这些状态和特权级别没任何关系。比如,在SVC模式下运行代码即可以是在ARM状态也可以是Thumb,主要的区别在于指令集,ARM状态下的指令集总是32位的,而Thumb下则是16位(也可能是32位)了解thumb指令在哪里使用,如何使用,对于我们达成开发目标而言是相当重要的。当我们编写ARM代码时,我们需要去掉空字节,并用16位的Thumb指令代替32位的ARM指令来减少获取到他们的机会

The calling conventions of ARM versions is more than confusing and not all ARM versions support the same Thumb instruction sets. At some point, ARM introduced an enhanced Thumb instruction set (pseudo name: Thumbv2) which allows 32-bit Thumb instructions and even conditional execution, which was not possible in the versions prior to that. In order to use conditional execution in Thumb state, the “it” instruction was introduced. However, this instruction got then removed in a later version and exchanged with something that was supposed to make things less complicated, but achieved the opposite. I don’t know all the different variations of ARM/Thumb instruction sets across all the different ARM versions, and I honestly don’t care. Neither should you. The only thing that you need to know is the ARM version of your target device and its specific Thumb support so that you can adjust your code. The ARM Infocenter should help you figure out the specifics of your ARM version (http://infocenter.arm.com/help/index.jsp).
ARM版本的调用规则让人难以感到困惑,并且不是所有的ARM版本都支持同样的Thumb指令集。在某个时间点,ARM引入了一个增强的Thumb指令集(伪名称:thumbv2),它允许执行32位Thumb指令,甚至是条件执行指令,这在之前的版本中是不可能的。为了在Thumb状态下使用条件执行,我们引入了“it”指令。然而,这一指令在后来的版本中被删除,并且被换成了可能让事情变得不那么复杂的指令集,然而却达到了相反的效果。我不知道所有不同ARM版本的ARM/Thumb指令集的所有不同的变化,我是真的不在乎。你也不应该在乎。唯一需要知道的是你的目标设备的ARM版本及其支持的Thumb版本,这样你就可以调整你的代码了。ARM息中心应该帮助你确定ARM版本的细节 (http://infocenter.arm.com/help/index.jsp). 。

As mentioned before, there are different Thumb versions. The different naming is just for the sake of differentiating them from each other (the processor itself will always refer to it as Thumb).

Thumb-1 (16-bit instructions): was used in ARMv6 and earlier architectures.

Thumb-2 (16-bit and 32-bit instructions): extents Thumb-1 by adding more instructions and allowing them to be either 16-bit or 32-bit wide (ARMv6T2, ARMv7).

ThumbEE: includes some changes and additions aimed for dynamically generated code (code compiled on the device either shortly before or during execution).

如前所述,有不同的Thumb版本。不同的命名只是为了区分它们(处理器本身总是把它视为Thumb指令)。
1. THEMP-1(16位指令):用于ARMv6和早期的体系结构。
2. THEMP-2(16位和32位指令):通过添加更多指令扩展了THUMP-1指令集,并允许它们既可以是16位也可以是32位位宽的指令(分别对应ARMV6T2,ARMV7)
3. ThumbEE:包括针对动态生成代码的一些更改和添加(在执行之前或执行过程中在设备上编译的代码)。
如前所述,有不同的Thumb版本。不同的命名只是为了区分它们(处理器本身总是把它视为Thumb指令)。
1. THEMP-1(16位指令):用于ARMv6和早期的体系结构。

[注意]传递专业知识、拓宽行业人脉——看雪讲师团队等你加入!

最后于 2018-6-15 17:31 被r0Cat编辑 ,原因:
上传的附件:
收藏
免费 1
支持
分享
打赏 + 2.00雪花
打赏次数 1 雪花 + 2.00
 
赞赏  junkboy   +2.00 2018/06/15
最新回复 (7)
雪    币: 11716
活跃值: (133)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
2
最后的图片挂了 
2018-6-15 17:27
0
雪    币: 8715
活跃值: (8619)
能力值: ( LV13,RANK:570 )
在线值:
发帖
回帖
粉丝
3
junkboy 最后的图片挂了
马上补上,感谢支持昂
2018-6-15 17:32
0
雪    币: 916
活跃值: (3434)
能力值: ( LV8,RANK:120 )
在线值:
发帖
回帖
粉丝
4
“32位ARM和Thumb指令:32位Thumb指令有一个.w后缀。”
因为今天正好在看这个,补充一下,这个.w其实是给汇编器看的,代表强制使用Thumb-2(也就是32位Thumb)编译这条指令,Instruction  byte中其实并没有与这个相关的标志位。

http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0204ic/ch04s11s01.html
最后于 2018-6-15 22:10 被葫芦娃编辑 ,原因:
2018-6-15 22:09
0
雪    币: 8715
活跃值: (8619)
能力值: ( LV13,RANK:570 )
在线值:
发帖
回帖
粉丝
5
葫芦娃 “32位ARM和Thumb指令:32位Thumb指令有一个.w后缀。”因为今天正好在看这个,补充一下,这个.w其实是给汇编器看的,代表强制使用Thumb-2(也就是32位Thumb)编译这条指令,In ...
w后缀强行转成32位Thumb指令  .n后缀强制转成16位,后缀加在助记符后。感谢分享,学习了,,,
最后于 2018-6-16 08:57 被r0Cat编辑 ,原因:
2018-6-16 08:54
0
雪    币: 416
活跃值: (711)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
6
去原文网站转了一圈,确实不错!话说,文章作者做的那几张图真是必须给赞!
2018-6-18 10:27
0
雪    币: 8715
活跃值: (8619)
能力值: ( LV13,RANK:570 )
在线值:
发帖
回帖
粉丝
7
看那排版,还有那几张图确实不是一般人做出来的,而且人家写的确实简明扼要,条理清晰,应该是大师手笔
最后于 2018-6-18 13:19 被r0Cat编辑 ,原因:
2018-6-18 11:47
0
雪    币: 888
活跃值: (2370)
能力值: ( LV3,RANK:20 )
在线值:
发帖
回帖
粉丝
8
amzilun 葫芦娃 “32位ARM和Thumb指令:32位Thumb指令有一个.w后缀。”因为今天正好在看这个,补充一下,这个.w其实是给汇编器看的,代表强制使用Thum ...
我葫芦娃师傅,对arm用户手册倒背如流啊。
2018-6-19 11:11
0
游客
登录 | 注册 方可回帖
返回
//