8

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.

4

2 回答 2

4

您可以添加该部分,用例如 NUL 填充它,然后计算您的 blob。然后将该 blob 修补到此部分。稍后,当您检查 ELF 的完整性时,就好像该部分充满了 NUL 并再次计算 blob。最后,比较计算的 blob 和存储在 section 中的 blob。

于 2015-03-17T23:45:59.453 回答
0

没有具体回答您的问题,但我用于此类事情的一种方法是链接占位符块,然后在之后修补正确的值。

我知道这不是您想要做的,但这是一种非常简单可靠的方法。并且具有与工具链/平台无关的主要优势。

于 2013-11-08T23:01:47.263 回答