交叉编译生成ARM汇编和反汇编二进制文件生成ARM汇编(指定ARM架构及cpu类型) .
分类: linux 2012-07-05 19:27 962人阅读 评论(0) 收藏 举报
[plain] view plaincopyprint?
01.
1、arm-linux-gcc:
首先编写C程序,假设名字为test.c,保存test.c文件内容:
[cpp] view plaincopyprint?
01.#include<stdio.h>
02.int main()
03.{
04. printf("hello.world!\n");
05. return 0;
06.}
#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文件内容:
[cpp] view plaincopyprint?
01.#include<stdio.h>
02.int main()
03.{
04. printf("hello.world!\n");
05. return 0;
06.}
#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内容:
[plain] view plaincopyprint?
01.<PRE class=plain name="code"> 1 .arch armv4t
02. 2 .fpu softvfp
03. 3 .eabi_attribute 20, 1
04. 4 .eabi_attribute 21, 1
05. 5 .eabi_attribute 23, 3
06. 6 .eabi_attribute 24, 1
07. 7 .eabi_attribute 25, 1
08. 8 .eabi_attribute 26, 2
09. 9 .eabi_attribute 30, 6
10. 10 .eabi_attribute 18, 4
11. 11 .file "test.c"
12. 12 .section .rodata
13. 13 .align 2
14. 14 .LC0:
15. 15 .ascii "hello.world!\000"
16. 16 .text
17. 17 .align 2
18. 18 .global main
19. 19 .type main, %function
20. 20 main:
21. 21 .fnstart
22. 22 .LFB2:
23. 23 @ Function supports interworking.
24. 24 @ args = 0, pretend = 0, frame = 0
25. 25 @ frame_needed = 1, uses_anonymous_args = 0
26. 26 stmfd sp!, {fp, lr}
27. 27 .save {fp, lr}
28. 28 .LCFI0:
29. 29 .setfp fp, sp, #4
30. 30 add fp, sp, #4
31. 31 .LCFI1:
32. 32 ldr r0, .L3
33. 33 bl puts
34. 34 mov r3, #0
35. 35 mov r0, r3
36. 36 sub sp, fp, #4
37. <PRE class=plain name="code"> 36 sub sp, fp, #4
38. 37 ldmfd sp!, {fp, lr}
39. 38 bx lr
40. 39 .L4:
41. 40 .align 2
42. 41 .L3:
43. 42 .word .LC0
44. 43 .LFE2:
45. 44 .fnend
46. 45 .size main, .-main
47. 46 .ident "GCC: (Sourcery G++ Lite 2009q1-176) 4.3.3"
48. 47 .section .note.GNU-stack,"",%progbits
49.</PRE>
50.<PRE></PRE>
51.<PRE></PRE>
52.<PRE></PRE>
53.<PRE></PRE>
54.<PRE></PRE>
55.<PRE></PRE>
56.<PRE></PRE>
57.<PRE></PRE>
58.<PRE></PRE>
59.<PRE></PRE>
60.<PRE></PRE>
61.<PRE></PRE>
62.<PRE></PRE>
63.<PRE></PRE>
64.<PRE></PRE>
65.<PRE></PRE>
66.<PRE></PRE>
67.<PRE></PRE>
68.<PRE></PRE>
69.<PRE></PRE>
70.<PRE></PRE>
71.<PRE></PRE>
72.<PRE></PRE>
73.<PRE></PRE>
74.</PRE>
[plain] view plaincopyprint? 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 <PRE class=plain name="code"> 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 </PRE> <PRE></PRE> <PRE></PRE> <PRE></PRE> <PRE></PRE> <PRE></PRE> <PRE></PRE> <PRE></PRE> <PRE></PRE> <PRE></PRE> <PRE></PRE> <PRE></PRE> <PRE></PRE> <PRE></PRE> <PRE></PRE> <PRE></PRE> <PRE></PRE> <PRE></PRE> <PRE></PRE> <PRE></PRE> <PRE></PRE> <PRE></PRE> <PRE></PRE> <PRE></PRE> <PRE></PRE> 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
[plain] view plaincopyprint?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 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.txt
生成test.txt文件,内容如下:
[plain] view plaincopyprint?
01.test: file format elf32-littlearm
02.test
03.
04.
05.Disassembly of section .interp:
06.
07.00008134 <.interp>:
08. 8134: 62696c2f rsbvs r6, r9, #12032 ; 0x2f00
09. 8138: 2d646c2f stclcs 12, cr6, [r4, #-188]!
10. 813c: 756e696c strbvc r6, [lr, #-2412]!
11. 8140: 6f732e78 svcvs 0x00732e78
12. 8144: Address 0x00008144 is out of bounds.
13.
14.
15.Disassembly of section .note.ABI-tag:
16.
17.00008148 <.note.ABI-tag>:
18. 8148: 00000004 .word 0x00000004
19. 814c: 00000010 .word 0x00000010
20. 8150: 00000001 .word 0x00000001
21. 8154: 00554e47 .word 0x00554e47
22. 8158: 00000000 .word 0x00000000
23. 815c: 00000002 .word 0x00000002
24. 8160: 00000006 .word 0x00000006
25. 8164: 0000000e .word 0x0000000e
26.
27.Disassembly of section .hash:
28.
29.00008168 <.hash>:
30. 8168: 00000003 andeq r0, r0, r3
31. 816c: 00000008 andeq r0, r0, r8
32. 8170: 00000005 andeq r0, r0, r5
33. 8174: 00000006 andeq r0, r0, r6
34. 8178: 00000007 andeq r0, r0, r7
35. ...
36. 8188: 00000002 andeq r0, r0, r2
37. 818c: 00000000 andeq r0, r0, r0
38. 8190: 00000004 andeq r0, r0, r4
39. 8194: 00000003 andeq r0, r0, r3
40. 8198: 00000001 andeq r0, r0, r1
41.
42.Disassembly of section .dynsym:
43.
44.0000819c <.dynsym>:
45. ...
46. 81ac: 0000006f andeq r0, r0, pc, rrx
47. 81b0: 00008354 andeq r8, r0, r4, asr r3
48. 81b4: 00000000 andeq r0, r0, r0
49. 81b8: 00000012 andeq r0, r0, r2, lsl r0
50. 81bc: 00000075 andeq r0, r0, r5, ror r0
51. 81c0: 00008360 andeq r8, r0, r0, ror #6
52. 81c4: 00000000 andeq r0, r0, r0
53. 81c8: 00000012 andeq r0, r0, r2, lsl r0
54. 81cc: 0000000f andeq r0, r0, pc
55. ...
56. 81d8: 00000012 andeq r0, r0, r2, lsl r0
57. 81dc: 00000026 andeq r0, r0, r6, lsr #32
58. ...
59. 81e8: 00000020 andeq r0, r0, r0, lsr #32
60. 81ec: 00000035 andeq r0, r0, r5, lsr r0
61. ...
62. 81f8: 00000020 andeq r0, r0, r0, lsr #32
63. 81fc: 0000006a andeq r0, r0, sl, rrx
64. 8200: 00008378 andeq r8, r0, r8, ror r3
65. 8204: 00000000 andeq r0, r0, r0
66. 8208: 00000012 andeq r0, r0, r2, lsl r0
67. 820c: 00000049 andeq r0, r0, r9, asr #32
68. ...
69. 8218: 00000012 andeq r0, r0, r2, lsl r0
70.
71.Disassembly of section .dynstr:
72.
73.0000821c <.dynstr>:
74. 821c: 62696c00 rsbvs r6, r9, #0 ; 0x0
75. 8220: 5f636367 svcpl 0x00636367
76. 8224: 6f732e73 svcvs 0x00732e73
77. 8228: 5f00312e svcpl 0x0000312e
78. 822c: 6165615f cmnvs r5, pc, asr r1
79. 8230: 755f6962 ldrbvc r6, [pc, #-2402] ; 78d6 <_init-0xa5a>
80. 8234: 6e69776e cdpvs 7, 6, cr7, cr9, cr14, {3}
81. 8238: 70635f64 rsbvc r5, r3, r4, ror #30
82. 823c: 72705f70 rsbsvc r5, r0, #448 ; 0x1c0
83. 8240: 5f5f0030 svcpl 0x005f0030
84. 8244: 6e6f6d67 cdpvs 13, 6, cr6, cr15, cr7, {3}
85. 8248: 6174735f cmnvs r4, pc, asr r3
86. 824c: 5f5f7472 svcpl 0x005f7472
87. 8250: 764a5f00 strbvc r5, [sl], -r0, lsl #30
88. 8254: 6765525f undefined
89. 8258: 65747369 ldrbvs r7, [r4, #-873]!
90. 825c: 616c4372 smcvs 50226
91. 8260: 73657373 cmnvc r5, #-872415231 ; 0xcc000001
92. 8264: 615f5f00 cmpvs pc, r0, lsl #30
93. 8268: 69626165 stmdbvs r2!, {r0, r2, r5, r6, r8, sp, lr}^
94. 826c: 776e755f undefined
95. 8270: 5f646e69 svcpl 0x00646e69
96. 8274: 5f707063 svcpl 0x00707063
97. 8278: 00317270 eorseq r7, r1, r0, ror r2
98. 827c: 6362696c cmnvs r2, #1769472 ; 0x1b0000
99. 8280: 2e6f732e cdpcs 3, 6, cr7, cr15, cr14, {1}
100. 8284: 75700036 ldrbvc r0, [r0, #-54]!
101. 8288: 61007374 tstvs r0, r4, ror r3
102. 828c: 74726f62 ldrbtvc r6, [r2], #-3938
103. 8290: 6c5f5f00 mrrcvs 15, 0, r5, pc, cr0
104. 8294: 5f636269 svcpl 0x00636269
105. 8298: 72617473 rsbvc r7, r1, #1929379840 ; 0x73000000
106. 829c: 616d5f74 smcvs 54772
107. 82a0: 47006e69 strmi r6, [r0, -r9, ror #28]
108. 82a4: 335f4343 cmpcc pc, #201326593 ; 0xc000001
109. 82a8: 4700352e strmi r3, [r0, -lr, lsr #10] |