align&&balignl代码对齐测试
学习uboot代码时,发现启动代码中有.balignl 16,0xdeadbeef
对齐,对于某些编译器来说,可能并不会代码对齐,所以需要进行手动对齐,否则就会出现编译报错。
举例说明(以下代码并无实际含义)
.globl _start
_start:
b reset
nop
.byte 0xff
reset:
ldr r0,=0x40000000
直接编译报错
book@book-virtual-machine:/work/test$ arm-S3C6410-linux-gnueabi-gcc -c arm.S -o arm.o
arm.S: Assembler messages:
arm.S:4: Error: misaligned branch destination
添加align对齐代码后编译
.globl _start
_start:
b reset
nop
.byte 0xff
.align
reset:
ldr r0,=0x40000000
book@book-virtual-machine:/work/test$ arm-S3C6410-linux-gnueabi-gcc -c arm.S -o arm.o
book@book-virtual-machine:/work/test$ arm-S3C6410-linux-gnueabi-objdump -D arm.o > arm.elf
arm.o: file format elf32-littlearm
Disassembly of section .text:
00000000 <_start>:
0: ea000001 b c <reset>
4: e1a00000 nop (mov r0,r0)
8: 000000ff .word 0x000000ff
0000000c <reset>:
c: e3a00101 mov r0, #1073741824 ; 0x40000000
Disassembly of section .ARM.attributes:
00000000 <.ARM.attributes>:
0: 00001641 andeq r1, r0, r1, asr #12
4: 61656100 cmnvs r5, r0, lsl #2
8: 01006962 tsteq r0, r2, ror #18
c: 0000000c andeq r0, r0, ip
10: 06003605 streq r3, [r0], -r5, lsl #12
14: Address 0x0000000000000014 is out of bounds.
使用另一种对齐方式balignl
.globl _start
_start:
b reset
nop
.byte 0xff
.balignl 16,0xdeadbeef
reset:
ldr r0,=0x40000000
出现编译报错,需要将.byte 0xff移除。
book@book-virtual-machine:/work/test$ arm-S3C6410-linux-gnueabi-gcc -c arm.S -o arm.o
arm.S: Assembler messages:
arm.S:11: Error: alignment padding (3 bytes) not a multiple of 4
arm.S:11: Error: alignment padding (3 bytes) not a multiple of 4
arm.S:4: Error: misaligned branch destination
修改后编译,查看反汇编编译指令按照16字节对齐。
book@book-virtual-machine:/work/test$ arm-S3C6410-linux-gnueabi-gcc -c arm.S -o arm.o
book@book-virtual-machine:/work/test$ arm-S3C6410-linux-gnueabi-objdump -D arm.o > arm.elf
book@book-virtual-machine:/work/test$ cat arm.elf
arm.o: file format elf32-littlearm
Disassembly of section .text:
00000000 <_start>:
0: ea000002 b 10 <reset>
4: e1a00000 nop (mov r0,r0)
8: deadbeef cdple 14, 10, cr11, cr13, cr15, {7}
c: deadbeef cdple 14, 10, cr11, cr13, cr15, {7}
00000010 <reset>:
10: e3a00101 mov r0, #1073741824 ; 0x40000000
14: e1a00000 nop (mov r0,r0)
18: e1a00000 nop (mov r0,r0)
1c: e1a00000 nop (mov r0,r0)
Disassembly of section .ARM.attributes:
00000000 <.ARM.attributes>:
0: 00001641 andeq r1, r0, r1, asr #12
4: 61656100 cmnvs r5, r0, lsl #2
8: 01006962 tsteq r0, r2, ror #18
c: 0000000c andeq r0, r0, ip
10: 06003605 streq r3, [r0], -r5, lsl #12
14: Address 0x0000000000000014 is out of bounds.