在您的链接器脚本片段中,firstfile 和 secondfile 是 MEMORY 区域而不是 SECTIONS,因此(我猜)部分属性将被忽略,因为这些部分不存在。
您必须创建MEMORY 区域,在其中放置SECTIONS,然后将目标代码中定义的部分分配给链接描述文件中声明的部分。请注意,定位的是 目标代码,而不是源文件 - 链接器对源文件一无所知:
类似:
MEMORY
{
FIRST_MEMORY : ORIGIN = 0x00000000, LENGTH = 0x2000
SECOND_MEMORY : ORIGIN = 0x00002000, LENGTH = 0x6000
}
SECTIONS
{
.firstsection :
{
. = ALIGN(4);
*firstfile.o (.text .text*) /* Locate firstfile text sections here */
} > FIRST_MEMORY
.secondsection :
{
. = ALIGN(4);
*secondfile.o (.text .text*) /* Locate secondfile text sections here */
} > SECOND_MEMORY
}
然后,您可以明确地为每个部分定位任意数量的模块。
您可能需要一个默认位置来放置未明确定位的模块。在这种情况下,您应该添加:
*(.text) /* .text sections (code) */
*(.text*) /* .text* sections (code) */
到其中一个部分(或创建一个单独的默认 .text 部分)。
如果你添加:
*(.firstsection*) /* Locate anything with firstsection attribute here */
或
*(.secondsection*) /* Locate anything with secondsection attribute here */
对于各个部分,您可以使用代码中的__section__ 属性将特定函数(或数据)定位到这些部分,就像您之前尝试的那样。但是最好定位整个模块,因为它不需要修改和维护代码。