交叉编译⽣成ARM汇编和反汇编⼆进制⽂件⽣成ARM汇编
(指定ARM架构及cpu类型)...
1、arm-linux-gcc:
⾸先编写C程序,假设名字为test.c,保存test.c⽂件内容:
#include<stdio.h>
int main()
{
printf("hello.world!\n");
return 0;
}
在X86架构下的电脑上⽣成ARM架构的汇编代码有两种⽅式:
1、使⽤交叉编译⼯具链:arm-linux-gcc,指定-S选项可以⽣成汇编中间⽂件。
2、使⽤arm-linux-objdump反汇编arm⼆进制⽂件。
1、arm-linux-gcc:
⾸先编写C程序,假设名字为test.c,保存test.c⽂件内容:
#include<stdio.h>
int main()
{
printf("hello.world!\n");
return 0;
}
使⽤⽅法如下:
在使⽤arm-linux-gcc编译C源⽂件时,使⽤-S选项可以将C⽂件(test.c为例)编译到汇编阶段,⽣成arm汇编代码,使⽤⽅式如下:arm-linux-gcc  -march=armv7-a -mtune=cortex-a9  test.c -S -o test.asm
⽣成arm汇编⽂件test.asm。
说明:
-march可以指定⽬标ARM的架构可选参数见(man gcc)
-mtune(类似于-mcpu)可以具体到ARM处理器类型。
注意:
Specifying both -march= and -mcpu= is redundant, and may not in fact have done what you expected in previous compiler versions (maybe even depending on the order in which the arguments were given). The -march switch selects a "generic" ARMv7-A CPU, and -mcpu selects specifically a Cortex-A8 CPU with tuning specific for that core.
Either use "-march=armv7-a -mtune=cortex-a8", or just use "-mcpu=cortex-a8".
所以只⽤⼀个-mcpu=cortex-a9也可以
test.asm内容:
1        .arch armv4t
2        .fpu softvfp
3        .eabi_attribute 20, 1
4        .eabi_attribute 21, 1
5        .eabi_attribute 23, 3
6        .eabi_attribute 24, 1
7        .eabi_attribute 25, 1
8        .eabi_attribute 26, 2
9        .eabi_attribute 30, 6
10        .eabi_attribute 18, 4
11        .file  "test.c"
12        .section        .rodata
13        .align  2
14 .LC0:
15        .ascii  "hello.world!\000"
16        .text
17        .align  2
18        .global main
19        .type  main, %function
20 main:
21        .fnstart
22 .LFB2:
23        @ Function supports interworking.
24        @ args = 0, pretend = 0, frame = 0
25        @ frame_needed = 1, uses_anonymous_args = 0
26        stmfd  sp!, {fp, lr}
27        .save {fp, lr}
28 .LCFI0:
29        .setfp fp, sp, #4
30        add    fp, sp, #4
31 .LCFI1:
32        ldr    r0, .L3
33        bl      puts
34        mov    r3, #0
35        mov    r0, r3
36        sub    sp, fp, #4
36        sub    sp, fp, #4
37        ldmfd  sp!, {fp, lr}
38        bx      lr
39 .L4:
40        .align  2
41 .L3:
42        .word  .LC0
43 .LFE2:
44        .fnend
45        .size  main, .-main
46        .ident  "GCC: (Sourcery G++ Lite 2009q1-176) 4.3.3"
47        .section        .note.GNU-stack,"",%progbits
另外,使⽤arm-linux-objdump 反汇编过程如下:
(1)交叉编译:
arm-linux-gcc test.c -o test ,⽣成test⼆进制⽂件(此处可以加⼊-O2选项优化代码: arm-linux-gcc test.c -O2 -o test)(2)反汇编:
arm-linux-objdump -alD test >
⽣成⽂件,内容如下:
test:    file format elf32-littlearm
test
Disassembly of section .interp:
00008134 <.interp>:
8134: 62696c2f  rsbvs r6, r9, #12032 ; 0x2f00    8138: 2d646c2f  stclcs 12, cr6, [r4, #-188]!
813c: 756e696c  strbvc r6, [lr, #-2412]!
8140: 6f732e78  svcvs 0x00732e78
8144: Address 0x00008144 is out of bounds. Disassembly of section .note.ABI-tag:
00008148 <.note.ABI-tag>:
8148: 00000004  .word 0x00000004
814c: 00000010  .word 0x00000010
8150: 00000001  .word 0x00000001
8154: 00554e47  .word 0x00554e47
8158: 00000000  .word 0x00000000
815c: 00000002  .word 0x00000002
8160: 00000006  .word 0x00000006
8164: 0000000e  .word 0x0000000e Disassembly of section .hash:
00008168 <.hash>:
8168: 00000003  andeq r0, r0, r3
816c: 00000008  andeq r0, r0, r8
8170: 00000005  andeq r0, r0, r5
8174: 00000006  andeq r0, r0, r6
8178: 00000007  andeq r0, r0, r7
...
8188: 00000002  andeq r0, r0, r2
818c: 00000000  andeq r0, r0, r0
8190: 00000004  andeq r0, r0, r4
8194: 00000003  andeq r0, r0, r3
8198: 00000001  andeq r0, r0, r1 Disassembly of section .dynsym:
0000819c <.dynsym>:
...
81ac: 0000006f  andeq r0, r0, pc, rrx
81b0: 00008354  andeq r8, r0, r4, asr r3
81b4: 00000000  andeq r0, r0, r0
81b8: 00000012  andeq r0, r0, r2, lsl r0
81bc: 00000075  andeq r0, r0, r5, ror r0
81c0: 00008360  andeq r8, r0, r0, ror #6
81c4: 00000000  andeq r0, r0, r0
81c8: 00000012  andeq r0, r0, r2, lsl r0
81cc: 0000000f  andeq r0, r0, pc
...
81d8: 00000012  andeq r0, r0, r2, lsl r0
81dc: 00000026  andeq r0, r0, r6, lsr #32 ...
81e8: 00000020  andeq r0, r0, r0, lsr #32
81ec: 00000035  andeq r0, r0, r5, lsr r0 ...
81f8: 00000020  andeq r0, r0, r0, lsr #32
81fc: 0000006a  andeq r0, r0, sl, rrx
8200: 00008378  andeq r8, r0, r8, ror r3
8204: 00000000  andeq r0, r0, r0
8208: 00000012  andeq r0, r0, r2, lsl r0
820c: 00000049  andeq r0, r0, r9, asr #32 ...
8218: 00000012  andeq r0, r0, r2, lsl r0
Disassembly of section .dynstr:
0000821c <.dynstr>:
821c: 62696c00  rsbvs r6, r9, #0 ; 0x0
8220: 5f636367  svcpl 0x00636367
8224: 6f732e73  svcvs 0x00732e73
8228: 5f00312e  svcpl 0x0000312e
822c: 6165615f  cmnvs r5, pc, asr r1
8230: 755f6962  ldrbvc r6, [pc, #-2402] ; 78d6 <_init-0xa5a>    8234: 6e69776e  cdpvs 7, 6, cr7, cr9, cr14, {3}
8238: 70635f64  rsbvc r5, r3, r4, ror #30
823c: 72705f70  rsbsvc r5, r0, #448 ; 0x1c0
8240: 5f5f0030  svcpl 0x005f0030
8244: 6e6f6d67  cdpvs 13, 6, cr6, cr15, cr7, {3}
8248: 6174735f  cmnvs r4, pc, asr r3
824c: 5f5f7472  svcpl 0x005f7472
8250: 764a5f00  strbvc r5, [sl], -r0, lsl #30
8254: 6765525f  undefined
8258: 65747369  ldrbvs r7, [r4, #-873]!
825c: 616c4372  smcvs 50226
8260: 73657373  cmnvc r5, #-872415231 ; 0xcc000001
8264: 615f5f00  cmpvs pc, r0, lsl #30
8268: 69626165  stmdbvs r2!, {r0, r2, r5, r6, r8, sp, lr}^
826c: 776e755f  undefined
8270: 5f646e69  svcpl 0x00646e69
8274: 5f707063  svcpl 0x00707063
8278: 00317270  eorseq r7, r1, r0, ror r2
827c: 6362696c  cmnvs r2, #1769472 ; 0x1b0000
8280: 2e6f732e  cdpcs 3, 6, cr7, cr15, cr14, {1}
8284: 75700036  ldrbvc r0, [r0, #-54]!
8288: 61007374  tstvs r0, r4, ror r3
828c: 74726f62  ldrbtvc r6, [r2], #-3938
8290: 6c5f5f00  mrrcvs 15, 0, r5, pc, cr0
8294: 5f636269  svcpl 0x00636269
8298: 72617473  rsbvc r7, r1, #1929379840 ; 0x73000000    829c: 616d5f74  smcvs 54772
82a0: 47006e69  strmi r6, [r0, -r9, ror #28]
82a4: 335f4343  cmpcc pc, #201326593 ; 0xc000001
82a8: 4700352e  strmi r3, [r0, -lr, lsr #10]
82ac: 4342494c  movtmi r4, #10572 ; 0x294c
82b0: 342e325f  strtcc r3, [lr], #-607
...
Disassembly of section .gnu.version:
000082b6 <.gnu.version>:
82b6: 00020000  andeq r0, r2, r0
82ba: 00030002  andeq r0, r3, r2
82be: 00000000  andeq r0, r0, r0
82c2: 00030002  andeq r0, r3, r2
Disassembly of section .gnu.version_r:
000082c8 <.gnu.version_r>:
82c8: 00010001  andeq r0, r1, r1
82cc: 00000001  andeq r0, r0, r1
linux下gcc编译的四个步骤82d0: 00000010  andeq r0, r0, r0, lsl r0
82d4: 00000020  andeq r0, r0, r0, lsr #32
82d8: 0b792655  bleq 1e51c34 <__bss_end__+0x1e415ac>    82dc: 00030000  andeq r0, r3, r0
82e0: 00000087  andeq r0, r0, r7, lsl #1
82e4: 00000000  andeq r0, r0, r0
82e8: 00010001  andeq r0, r1, r1
82ec: 00000060  andeq r0, r0, r0, rrx
82f0: 00000010  andeq r0, r0, r0, lsl r0
82f4: 00000000  andeq r0, r0, r0

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。