`
xitong
  • 浏览: 6194507 次
文章分类
社区版块
存档分类
最新评论

“.rodata.str1.4”的连接(link)问题

 
阅读更多

关于“.rodata.str1.4”的连接(link)问题

【问题描述】
最近在写一些嵌入式底层相关的程序,有如下程序段(编译器为arm-linux-gcc 3.4.1):
while(1)
{
	unsigned char* s="abcd";
	uart0_puts(s);
}
但在编译的时候出现了如下问题:

/usr/local/arm/3.4.1/bin/../lib/gcc/arm-linux/3.4.1/../../../../arm-linux/bin/ld: section .rodata.str1.4 [00000080 -> 0000008c] overlaps section .text [00000000 -> 0000044f]

【问题分析】

这个 .rodata.str1.4 是 -O2 优化时才出现的,不带优化选项时没有。在google上找到了如下一些资料:
Sections like .rodata.str1.4 are generated by gcc to support merging of duplicate constants and strings by ld. Older gcc's won't do this, nor will a newer gcc built for a system without the requisite ld support. it could be a matter of gcc versions. Apparently, gcc versions prior to 3.1.x didn't generate .rodata.strx.y sections.
大意为: .rodata.str1.4 是gcc为了支持ld合并重复的常量和字符串而产生的。老版本的gcc不会产生这个段,如果ld不支持,新版本的gcc也不会产生这个段,这是一个gcc版本问题。显然,3.1.x版本前的gcc不会产生 .rodata.strx.y段。

【解决办法】

原来的连接脚本里边没加上 .rodata.str1。比如:

SECTIONS
{
	. =0x00000000;
	.text :
	{
		*(.text)
		*(.rodata.str1.4)
		*(.rodata)
	}
	. =ALIGN(32);

	.data :
	{
		*(.data)
	}

	. =ALIGN(32);
	__bss_start__ = .;
	.bss :
	{
		*(.bss)
	}
	__bss_end__ = .;
}
【其他】

如果没有在在连接脚本里边加上 .rodata.str1.4 还可能出现如下错误:
/usr/local/arm/3.4.1/lib/gcc/arm-linux/3.4.1/../../../../arm-linux/bin/ld: error: no memory region specified for loadable section `.rodata.str1.4'

【参考资料】

http://telltruth.blogbus.com/logs/15705483.html

http://blog.chinaunix.net/uid-121788-id-2955078.html

http://blog.21ic.com/user1/2983/archives/2008/53972.html

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics