如何解决链接脚本中的错误?

5
我创建了一个内存链接脚本,并将其保存为memory.ld在eclipse ide中: Project:属性:gcc链接器:miscellaneous:我添加了-M-T memory.ld
memory.ld:
    MEMORY
{
        ram (rw)   : ORIGIN = 0x4000000 ,  LENGTH = 2M  
}

SECTIONS
{
  RAM : { *(.myvarloc) 

} > ram }

在我的 C 程序中:我进行了如下全局声明:

__attribute__ ((section(".myvarloc")))

 uint8 measurements [30];

错误:

/usr/bin/ld: FEBRUARY section `.text' will not fit in region `ram'
/usr/bin/ld: region `ram' overflowed by 20018 bytes
/usr/lib/i386-linux-gnu/libc_nonshared.a(elf-init.oS): In function `__libc_csu_init':
(.text+0x2b): undefined reference to `__init_array_end'
/usr/lib/i386-linux-gnu/libc_nonshared.a(elf-init.oS): In function `__libc_csu_init':
(.text+0x31): undefined reference to `__init_array_start'
/usr/lib/i386-linux-gnu/libc_nonshared.a(elf-init.oS): In function `__libc_csu_init':
(.text+0x57): undefined reference to `__init_array_start'
/usr/bin/ld: FEBRUARY: hidden symbol `__init_array_end' isn't defined
/usr/bin/ld: final link failed: Bad value
collect2: error: ld returned 1 exit status

错误与.text/ram溢出有关,而不是.myvarloc - anishsane
2
您的链接脚本可能需要告诉链接器如何处理剩下的代码/数据等,而不仅仅是如何处理.myvarloc部分。 - nos
非常感谢您的回复。您能否给我一个例子? - user3252048
我按照上述修改了代码,没有出现任何错误。如何检查上述.ld文件是否有效? - user3252048
如果我在我的C程序中像上面那样做,那么变量measurements[30]将存储在RAM区域。请问有人可以回答我的问题吗? - user3252048
显示剩余7条评论
1个回答

1
根据你使用的编译器(GCC?)和编译的处理器(x86?),编译器将在目标文件中生成几个段引用。最常见的是.text用于代码段,.data用于已初始化数据,.bss用于未初始化数据。 您可以使用nm实用程序查看编译器生成的哪些段。
我假设在您提供自己的链接器脚本之前,环境已经自动或隐式地提供了一些默认脚本。但现在您已经决定“自己动手”,您必须自己处理所有细节。

我无法验证详细信息,但您可以从以下SECTIONS开始:

SECTIONS
{
  .bss : { *(.myvarloc) } 
  .bss : { *(.bss) }
  .data : { *(.data) }
  .text : { *(.text) }
}

我不确定这是否是你的GCC链接器的确切语法(这有点取决于版本),但你可以在手册中找到更多信息。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接