链接描述文件中的对齐

Alignment in linker scripts

我正在查看 trezor 的引导加载程序链接器脚本:

/* TREZORv2 bootloader linker script */

ENTRY(reset_handler)

MEMORY {
  FLASH  (rx)  : ORIGIN = 0x08020000, LENGTH = 128K
  CCMRAM (wal) : ORIGIN = 0x10000000, LENGTH = 64K
  SRAM   (wal) : ORIGIN = 0x20000000, LENGTH = 192K
}

main_stack_base = ORIGIN(CCMRAM) + LENGTH(CCMRAM); /* 8-byte aligned full descending stack */

/* used by the startup code to populate variables used by the C code */
data_lma = LOADADDR(.data);
data_vma = ADDR(.data);
data_size = SIZEOF(.data);

/* used by the startup code to wipe memory */
ccmram_start = ORIGIN(CCMRAM);
ccmram_end = ORIGIN(CCMRAM) + LENGTH(CCMRAM);

/* used by the startup code to wipe memory */
sram_start = ORIGIN(SRAM);
sram_end = ORIGIN(SRAM) + LENGTH(SRAM);

_codelen = SIZEOF(.flash) + SIZEOF(.data);

SECTIONS {
  .header : ALIGN(4) {
    KEEP(*(.header));
  } >FLASH AT>FLASH

  .flash : ALIGN(512) {
    KEEP(*(.vector_table));
    . = ALIGN(4);
    *(.text*);
    . = ALIGN(4);
    *(.rodata*);
    . = ALIGN(512);
  } >FLASH AT>FLASH

  .data : ALIGN(4) {
    *(.data*);
    . = ALIGN(512);
  } >CCMRAM AT>FLASH

  .bss : ALIGN(4) {
    *(.bss*);
    . = ALIGN(4);
  } >CCMRAM

  .stack : ALIGN(8) {
    . = 4K; /* this acts as a build time assertion that at least this much memory is available for stack use */
  } >CCMRAM
}

可以找到here.

我知道代码需要 32 位 ( ALIGN(4) ) 对齐,因为如果 ARM 处理器试图访问未对齐的地址,它可能会崩溃,但我不明白为什么堆栈对齐是 8 字节,而且为什么你需要浪费(?)512 字节来对齐闪存部分?!

我想了解在编写链接器脚本时如何确定对齐方式。

提前感谢您的回答!

编辑:

我想我已经回答了我自己的问题:

1. .flash 部分:

它是这样对齐的,因为它里面的向量table总是需要"32-word aligned". This can also be seen be the case in Trezor's boardloader linker script。如您所见,向量 table 是 512 字节(4 x 32 字)对齐的。

2。 .stack 部分:

根据ARM's own documentation,堆栈部分需要始终按 8 字节对齐。

P.S。当然如果不是这样,请指正。

好的,既然 cooperised 证实了我的理论,我现在可以结束这个问题了。

1. .flash 部分:

它是这样对齐的,因为它里面的向量table总是需要“32字对齐”。在 Trezor 的 boardloader 链接器脚本中也可以看到这种情况。如您所见,向量 table 是 512 字节(4 x 32 字)对齐的。

2。 .stack 部分:

根据 ARM 自己的文档,堆栈部分需要始终对齐 8 字节。

感谢合作确认!