Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can the --add-section switch of OBJCOPY be used?

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.

like image 424
user2600958 Avatar asked Jul 24 '13 00:07

user2600958


People also ask

How does objcopy work?

objcopy creates temporary files to do its translations and deletes them afterward. objcopy uses BFD to do all its translation work; it has access to all the formats described in BFD and thus is able to recognize most formats without being told explicitly.

Why use objcopy?

objcopy can be used to generate S-records by using an output target of srec (e.g., use -O srec). objcopy can be used to generate a raw binary file by using an output target of binary (e.g., use -O binary).

What is ELF file used for?

In computing, the Executable and Linkable Format (ELF, formerly named Extensible Linking Format), is a common standard file format for executable files, object code, shared libraries, and core dumps.


1 Answers

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.

like image 97
Farid H. Avatar answered Oct 02 '22 15:10

Farid H.