如何使用 OBJCOPY 的 --add-section 开关? [英] How can the --add-section switch of OBJCOPY be used?

查看:24
本文介绍了如何使用 OBJCOPY 的 --add-section 开关?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

There are really two questions that revolve around the use of --add-section. The simple one is in the title. Based on my reading, I haven't been able to figure out how one could execute --add-section.

To use add-section, I have to pass a section name. If I use an existing section name the program responds with "can't add section '.data': File in wrong format." Perhaps I just need to pass another parameter. If I use a new section name, which I would prefer to do, I'm warned that "allocated section '.blob' not in segment."

Now, I have gotten my feature to work as I need it to aside from the "not in segment" warning. I'd like to figure out if there is a legitimate way to put this blob into the executable. I would link it in, but that isn't so easy because the data I'm adding is generated from the contents of the executable itself.

The second question is really what I care about. Is there a way to do the following given that the blob cannot be computed until after the link is complete.

  1. Link ELF file
  2. Generate blob from ELF file and other data
  3. Add blob to ELF file so that it is loaded at run-time to the correct location in memory

    objcopy --add-section .blob=blob.o --set-section-flags .blob=alloc,contents,load,readonly --change-section-address .blob=ADDRESS program.elf program.blobbed.elf

I'd be happy to add a section and/or segment to the ELF file as part of the link and insert this blob there. I'm not sure how to do that.

It has occurred to me that I could accomplish this feat with a second link, but objcopy would be cleaner.

  1. Link ELF file
  2. Generate blob from ELF file and other data
  3. Re-link ELF file including new blob.o

UPDATE: This last strategy may be workable as long as the relink doesn't change something in the portion of the program that was produced by the first link. It doesn't on first attempts, but it may be possible to work around it. Hence, the desire to use --add-section to add in this blob instead of going through a second link.

解决方案

You may add that section, fill it with, say, NULs, and then compute your blob. Then patch that blob into this section. Later, when you check the integrity of the ELF, do as if that section was full of NULs and compute the blob again. Finally, compare both computed blob and blob stored in section.

这篇关于如何使用 OBJCOPY 的 --add-section 开关?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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