align&&balignl代码对齐测试

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.
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值