c - 将.data发送到其他部分 [英] c - send .data to a different section
问题描述
我想为特定C文件生成的 .data
部分中的符号放在不同的部分(例如 .mydata
)在最终的可执行文件中。例如,
normaldata.c:char * my_str =这应该出现在.data中;
specialdata.c:char * my_special_str =这应该出现在.mydata中;
默认情况下, my_str
和 my_special_str
出现在 .data
部分。但是,当我生成 specialdata.o
时,我想将任何出现在 .data
中的任何内容发送到 .mydata
。
我知道我可以使用 __属性__((section( .mydata)))
来实现这个效果,但是我不知道如何将它应用到结构指针成员的指定初始值设定项(我简化了我的代码)。所以相反,我想也许我可以使用链接器脚本并将特定文件中的所有数据发送到我的特殊部分。
我试过这个链接器脚本( link.lds
):
SECTIONS
{
.mydata:{*(。data)}
}
with gcc -c specialdata.c -T link.lds -o specialdata.o
,但是 objdump -x specialdata.o
的输出显示a .data
部分,但不包含 .mydata
部分。
我做错了什么?
在你的C声明中, section()
属性指定链接器的输入节名称。链接器脚本的 SECTIONS
部分中指定的 .mydata
名称是输出部分。在 .mydata
中,告诉链接器将来自 *(。data)
输入节的所有符号放入 .mydata
输出部分,但是您的C属性使用输出部分的名称。
获取链接器要将C变量放在正确的输出节中,必须在 .mydata
输出节定义和C section()
属性。
尝试更改输入节的名称并在链接器脚本和C属性中使用相同的名称。
链接器脚本:
SECTIONS
{
/ *将.my_data输入节中的所有符号放入
* .mydata输出节中。
* /
.mydata:{*(。my_data)}
}
specialdata.c source:
//请注意,输入节名称是.my_data,而不是。 MYDATA。
char * my_special_str __attribute __((section(。my_data)))=
this should appear in .mydata;
I want to take the symbols in the .data
section generated for a particular C file and place them in a different section (e.g. .mydata
) in the final executable. For example,
normaldata.c: char * my_str = "this should appear in .data";
specialdata.c: char * my_special_str = "this should appear in .mydata";
By default, both my_str
and my_special_str
appear in the .data
section. However, when I generate specialdata.o
, I want to send anything that would have appeared in .data
instead to .mydata
.
I am aware that I can use the __attribute__((section(".mydata")))
to achieve this effect, but I don't know how to apply this to a designated initializer of pointer member of a struct (I have simplified my code for this question). So instead, I'm thinking maybe I can use a linker script and send all the data from a particular file into my special section.
I tried this linker script (link.lds
):
SECTIONS
{
.mydata : { *(.data) }
}
with gcc -c specialdata.c -T link.lds -o specialdata.o
, but the output of objdump -x specialdata.o
shows a .data
section but no .mydata
section.
What am I doing wrong?
In your C declaration, the section()
attribute specifies an input section name for the linker. The .mydata
name, specified in the SECTIONS
part of the linker script is the name of an output section. In .mydata
you tell the linker to place all the symbols from the "*(.data)
" input sections into the .mydata
output section, but your C attribute uses the name of the output section.
To get the linker to place the C variable in the correct output section, you must use the same name for the input sections in both the .mydata
output section definition and the C section()
attribute.
Try changing the name of the input section and using teh same name in both the linker script and the C attibutes.
Linker script:
SECTIONS
{
/* Put all symbols from the .my_data input section into the
* .mydata output section.
*/
.mydata : { *(.my_data) }
}
specialdata.c source:
// Note that the input section name is ".my_data", not ".mydata".
char * my_special_str __attribute__((section(".my_data"))) =
"this should appear in .mydata";
这篇关于c - 将.data发送到其他部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!