2

我有一个功能:

void testfunction() {
 static char_t theChar1 = 1;
 static unsigned char smallArray[1];
 static unsigned char largeArray[135];
    ...
}

和一个链接器文件:

  . = ALIGN(4);
  _edata = . ;
  PROVIDE (edata = .);

  .bss (NOLOAD) :
  {
    __bss_start = . ;
    __bss_start__ = . ;
    *(.bss)
    *(.bss.*)
    *(COMMON)
    . = ALIGN(4);
  } > ramEXT

  . = ALIGN(4);
  __bss_end__ = . ;
  PROVIDE (__bss_end = .);

我需要静态数组(.bss 数据)在 4 字节边界上对齐,但似乎数组拒绝这样做。结构和原始类型可以很好地对齐(参见填充线),但是数组已经全部结束了。这是我的地图文件:

 .data.firstTimeFlag.7295
                0xa000098c        0x4 output/file1.o
 .data.theChar1.5869
                0xa0000990        0x1 output/file2.o
 *fill*         0xa0000991        0x3 00
 .data.debounce
                0xa0000994      0x270 output/file3.o

...

 .bss.initialized.5826
                0xa000812c        0x1 output/file2.o
 *fill*         0xa000812d        0x3 00
 .bss.allocator.5825
                0xa0008130       0x34 output/file2.o
 .bss.largeArray.5869
                0xa0008164       0x87 output/file2.o
 .bss.smallArray.5868
                0xa00081eb        0x1 output/file2.o
 .bss.initialized.5897
                0xa00081ec        0x1 output/file2.o
 *fill*         0xa00081ed        0x3 00
 .bss.allocator.5896

有人知道如何对齐数组吗?

4

2 回答 2

2

我不确定您是否可以使用链接器脚本来做到这一点,我对您的目标感到困惑;附加到每个汇编程序声明的对齐属性可能满足 ABI 和机器要求。您是否正在尝试提高缓存命中率?补偿源代码中的不合格类型双关语?

您可以“轻松”做的一件事是将 gnu 对齐扩展添加到 C 源代码中。

static unsigned char smallArray[1] __attribute__ ((aligned (4)));

更新:嗯,一个大型的第三方宏库,生成明显不合格的代码?(如果它是符合标准的代码,它可能会正常工作。:-) 好吧,这是一个可怕的组合,但我几乎可以保证它会“工作”,FSDO“工作”,并且不需要调试宏库.. .您可以对编译器的汇编语言输出进行后处理。局部静态 bss 符号对布局不敏感,通常在单个.comm.lcomm指令中声明,其最后一个参数可能是对齐量。

这个参数是什么__attribute__变化。您可以在构建过程中使用某种脚本更改它们...

于 2009-10-14T03:20:18.597 回答
1

In systems I've worked on, the linker parameters could be controlled to set the alignment of the start of each section--but not the alignment of individual variables within the section. The alignment of individual variables was determined by the compiler, according to the variable's type.

You may be able to use some sort of compiler pragma, platform-specific of course.

An alternative is to define your array as an array of whatever type you need aligned to, such as uint32_t.

I'm curious to know what you're trying to achieve--it sounds as though you're doing something a little unusual and platform-specific, if you have this alignment requirement. Best to create platform-independent code if at all possible, of course.

于 2009-10-14T03:25:12.920 回答