如何在ELF文件中制作重复部分 [英] How to make duplicate sections in ELF file

查看:117
本文介绍了如何在ELF文件中制作重复部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个需求,我需要在其中创建.data节的重复/复制节.

I have a requirement where I need to create a duplicate/copy section of .data section.

我尝试在链接描述文件中创建一个具有与数据段相同大小的虚拟段,并将数据段的内容复制到我的ELF图像的init函数中的虚拟段中,但这不符合我的要求,因为我希望复制/复制部分与最终的ELF图像一起创建,而不是在执行过程中创建.

I've tried creating a dummy section with same size of data section in linker script and copy the contents of data section to the dummy section in the init functions of my ELF image, but that doesn't suit my requirement, as I want the copy/duplicate section to be created along with final ELF image not during the execution of it.

下面是我想要的链接描述文件,

Below is what I wanted in my linker script,

SECTIONS {
    .data : { <data section contents> }
    .dummydata : { <copy of .data section> } 
}

任何人都可以帮助编写链接程序脚本以符合上述要求吗?

Can anyone help to write the linker script to match above requirement?

推荐答案

我不认为仅使用ld和链接描述文件就可以做到这一点.在此处:

I don't think this can be done with just ld and a linker script. Given this line from here:

如果文件名匹配多个通配符模式,或者文件 名称明确显示,并且也与通配符模式匹配, 链接器将使用链接器脚本中的第一个匹配项.

If a file name matches more than one wildcard pattern, or if a file name appears explicitly and is also matched by a wildcard pattern, the linker will use the first match in the linker script.

听起来链接程序脚本只会将数据(或任何内容)放在一个区域中.

It sounds like the linker script will only put the data (or anything) in one section.

但是,所有的希望并没有失去.您可以使用objcopy复制该部分,然后再次使用objcopy添加该部分

However all hope is not lost. You can copy the section using objcopy and then add the section using objcopy again

objcopy -O binary --only-section=.data your-file temp.bin
objcopy --add-section .dummydata=temp.bin your-file

这会将部分附加到VMA/LMA为0的最后部分.然后,您可以使用objcopy将该部分移至所需位置.

This will append the section to be the last section with a VMA/LMA of 0. You can then use objcopy to move the section to the desired location.

objcopy --change-section-address .dummydata=desired-address your-file

当然,如果已经有东西存在问题.幸运的是,您可以在第一个.data之后创建一个孔,如下所示:

Of course if there is something already there that would be problematic. Luckily you can create a hole right after your first .data with something like:

data_start = .;
.data : { *(.data) }
data_end = .;
. += (data_end - data_start);

这应该在您的第一个数据之后创建一个空洞,其大小足以在其后放置另一个数据副本.如果这不完全是您想要的位置,只需在要钻孔的位置添加(data_end - data_start).

This should create a hole right after your first data, big enough to put another copy of data right after it. If this isn't exactly where you want it to be just add (data_end - data_start) where you want the hole.

最后,您可以再次使用objcopy

Finally you can change the section flags, again with objcopy

objcopy --set-section-flags .dummydata=the-flags-you-want your-file

不像在链接程序脚本中复制某些内容那样干净,但是它应该可以工作.

Not as clean as just duplicating something in the linker script but it should work.

这篇关于如何在ELF文件中制作重复部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆