对齐链接器文件的 .bss 部分中的静态数组

发布于 2024-08-07 21:15:08 字数 1363 浏览 2 评论 0原文

我有一个函数:

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

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

I have a function:

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

and a linker file:

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

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

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

I need static arrays (.bss data) to align on 4 byte boundaries, but it seems arrays refuse to do so. Structures and primitive types align fine (see the fill lines), but the arrays are all over. Here's my map file:

 .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

Anyone know how to align arrays?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

混浊又暗下来 2024-08-14 21:15:08

我不确定您是否可以使用链接器脚本来完成此操作,并且我对您的目标感到困惑;附加到每个汇编器声明的对齐属性大概满足 ABI 和机器要求。您是否想提高缓存命中率?补偿源代码中不合格的类型双关?

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

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

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

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

I'm not sure you can do this with a linker script, and I'm puzzled as to your goal; the alignment attribute attached to each assembler declaration presumably meets the ABI and machine requirements. Are you trying to increase cache hit rate? Compensate for non-conforming type punning in the source code?

One thing you can "easily" do is add the gnu alignment extension to the C source.

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

Update: Hmm, a large third-party macro library that generates obviously non-conforming code? (If it was standard-conforming code it would presumably work fine. :-) Ok, this is a frightful kludge, but I can almost guarantee it will "work", FSDO "work", and not require debugging the macro lib...You could post-process the compiler's assembly language output. Local static bss symbols are not layout sensitive and are typically declared in a single .comm or .lcomm directive, for which the last parameter is likely to be the alignment amount.

This parameter is what __attribute__ changes. You could change them all during the build with a script of some kind...

全部不再 2024-08-14 21:15:08

在我开发的系统中,可以控制链接器参数来设置每个部分开头的对齐方式,但不能设置该部分内各个变量的对齐方式。各个变量的对齐方式由编译器根据变量的类型确定。

您也许可以使用某种编译器杂注,当然是特定于平台的。

另一种方法是将数组定义为需要对齐的任何类型的数组,例如 uint32_t。

我很想知道你想要实现什么——如果你有这种对齐要求,听起来好像你正在做一些不寻常且特定于平台的事情。当然,如果可能的话,最好创建独立于平台的代码。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文