I am in Linux, and I have seen this question a few times but never, nobody answered how to really make this work.
I need to add a section to an already compiled binary. Lets say for a moment is an ELF file. I'm using objcopy so this should be generic for any format because objcopy uses libbfd that handles many formats.
My process is as follows.
I create the bytecode for a section I want to append to an already compiled ELF file. Let's name this file bytecode.bin
Then I do:
objcopy --add-section .mysection=bytecode.bin \
--set-section-flags .mysection=code,contents,alloc,load,readonly \
Then I adjust the VMA of the secition:
objcopy --adjust-section-vma .mysection=$((16#XXXXX)) myprogram_edited myprogram_edited
Where XXXXXX is the new VMA address for the section.
I get the warning:
objcopy: stIbZt3t: warning: allocated section `.mysection' not in segment
When I do:
objdump -d myprogram_edited
Disassembly of section .mysection:
So I see the section is created OK and the VMA adjusted. But the section is not mapped to segments, so it can't be loaded at runtime.
How can I solve this?
I opted for using Intel's PIN tool. Very useful and powerful for RI and binary injection.