1

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 ? Or can this be achieved using objcopy also ?

Abhay
  • 19
  • 1
  • 3
  • Linker script, I guess. https://cygwin.com/ml/binutils/2003-05/msg00704.html. It would be wise to tell what platofrm/tools are you using – Severin Pappadeux Jul 29 '16 at 15:27
  • I was trying to edit a binary on a standard ubuntu system. Unfortunately, due to me being new to this I don't entirely understand the linker script approach in this. Also, I don't have access to the source code. Would the linker script work purely on the binary ? – Abhay Jul 29 '16 at 16:55
  • No, linker script is working when you link things together to make the binary. – Severin Pappadeux Jul 29 '16 at 17:35

1 Answers1

4

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

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

--update-section sectionname=filename

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.

Note: this requires binutils v2.26 or above.

Community
  • 1
  • 1
sdive
  • 2,047
  • 1
  • 20
  • 20