How can I use the -add-section OBJCOPY switch?

There are really two questions that revolve around using --add-section. Simple in name. Based on my reading, I was not able to figure out how to perform the -add-section.

To use an additional section, I need to pass the name of the section. If I use the name of an existing section, the program says "cannot add the section .data": file in the wrong format ". Maybe I just need to pass another parameter. If I use a new section name, which I would prefer to do, I warned that the "highlighted section" .blob is "not in the segment".

Now I got my function to work as I need, aside from the warning "not in the segment." I would like to find out if there is a legal way to put this blob in an executable. I would link it, but it’s not so simple, because the data that I add 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 blob cannot be computed until the link is complete.

  • Link to ELF file
  • Generate blob from ELF file and other data
  • Add blob to ELF file so that it loads at runtime to the desired 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 would be happy to add a section and / or segment to the ELF file as part of the link and paste this blob there. I am not sure how to do this.

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

  • Link to ELF file
  • Generate blob from ELF file and other data
  • Rename the ELF file, including the new blob.o

UPDATE: This last strategy can be workable until the relink changes something in the part of the program that was created by the first link. This does not apply to the first attempts, but you can get around this. Therefore, the desire to use -add-section to add to this blob instead of going through the second link.

+4
source share
2 answers

You can add this section, fill it with, say, NUL, and then calculate your blob. Then paste the patch into this section. Later, when you check ELF integrity, make it as if this section was filled with NUL and computed blob again. Finally, compare both calculated blob and blob stored in the section.

+1
source

Not specifically answering your question, but one approach that I used for this kind of thing was to link it in a placeholder, and then just fix the correct value afterwards.

I know that this is not what you want to do, but it is a fairly simple and reliable way to do this. And it has the main advantage of being a chain / platform agnostic.

0
source

Source: https://habr.com/ru/post/1493080/


All Articles