如何在 GNU LD 中实现自定义输出部分?

How to implement custom output section in GNU LD?

我正在尝试了解如何在我的应用程序的 LD 文件中定义自定义输出部分。到目前为止,这就是我想出的...

MEMORY
{
  ...
  m_my_custom_section (RW)  : ORIGIN = 0x00002400, LENGTH = 0x00000400
  ...
}

SECTIONS
{
  ...
  .my_custom_section :
  {
    . = ALIGN(4);
    KEEP(*(.my_custom_section))
    . = ALIGN(4);
  } > m_my_custom_section
  ...
}

不幸的是,这就是我被困的地方。当应用程序链接时,我不确定如何指定要将我的代码的哪些部分分配给该部分。任何帮助都会很棒。 :)

我相信我找到了答案:

In Standard C or C++ there are no syntactic means to define sections. Section definition normally is entirely the compiler's business. A compiler may have non-standard extensions that let you assign an object to a named section. For GCC see the documentation of __attribute__ ((section ("<section-name>"))) in Common Variable Attributes and Common Function Attributes Such extensions are for specialized purposes.