如何使用objcopy或libelf将elf文件的一部分替换为另一部分,以使其确实加载到内存中? [英] How to replace a section of an elf file with another using objcopy or libelf, such that it does get loaded into the Memory?

查看:143
本文介绍了如何使用objcopy或libelf将elf文件的一部分替换为另一部分,以使其确实加载到内存中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个项目,我需要删除ELF文件的一部分并将其替换为内容稍有修改的另一部分.我能够先删除原始部分,然后使用objcopy添加所需的部分.但我总是得到以下警告:

I have a project where I need to remove a section of an ELF file and replace it with another section with slightly modified contents. I was able to first remove the original section and then add my desired section using objcopy. But I always get the warning that:

 warning: allocated section `.nv_fatbin' not in segment

这将导致该节未加载到内存中,因此无法实现所需的功能.

This leads to the section not getting loaded in the memory and hence desired functionality isn't achieved.

将其添加并重新添加到细分中的最佳方法是什么?

What would be the best way to add it and bring it back into the segment ?

libelf?还是可以使用objcopy来实现?

libelf ? Or can this be achieved using objcopy also ?

推荐答案

您可能要使用objcopy的-update-section 选项.

You may want to use objcopy's --update-section option.

从objcopy手册中提取( https://sourceware.org/binutils/docs/binutils/objcopy.html ):

Extract from objcopy manual (https://sourceware.org/binutils/docs/binutils/objcopy.html):

-update-section sectionname =文件名

--update-section sectionname=filename

将名为sectionname的节的现有内容替换为文件filename的内容.该部分的大小将调整为文件的大小.sectionname的节标记将保持不变.对于ELF格式的文件,节到段的映射也将保持不变,使用--remove-section和--add-section不可能实现.可以多次指定该选项.

Replace the existing contents of a section named sectionname with the contents of file filename. The size of the section will be adjusted to the size of the file. The section flags for sectionname will be unchanged. For ELF format files the section to segment mapping will also remain unchanged, something which is not possible using --remove-section followed by --add-section. The option can be specified more than once.

注意:这需要binutils v2.26或更高版本.

Note: this requires binutils v2.26 or above.

这篇关于如何使用objcopy或libelf将elf文件的一部分替换为另一部分,以使其确实加载到内存中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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