[llvm-dev] Modifying objects with MC
David Chisnall via llvm-dev
llvm-dev at lists.llvm.org
Tue Aug 25 02:44:44 PDT 2015
On 25 Aug 2015, at 10:30, Simon Cook via llvm-dev <llvm-dev at lists.llvm.org> wrote:
> I've been trying to write a tool that reads in an ELF file and modifies
> one section within that ELF file based on the data contained within
> another section. Using llvm-objdump as a template of how to read an
> object file, I've been able to read the latter section that tells me
> what to edit, but I haven't found a way to edit the former, as
> everything in the MCSection/ELFObjectFile/SectionRef classes seem to be
> read-only as far as section contents are concerned.
> The changes I would want to make don't change the size of any section,
> all I need to do is for example zero the first n bytes of .text whilst
> keeping the rest of the object the same. What would be the recommended
> approach to achieving this goal?
I have a similar use case and would also be interested in an answer. In the meantime, I have a horrible hack that may work for you.
My current hack works because the returned StringRefs for the SymbolRef::getContents are within the range of the MemoryBuffer and so the offset relative to the memory buffer corresponds to the file offset and this offset can be used to write into the file directly.
It’s a horrible hack though, and it would be nice to simply have a mutable memory buffer that can be written back to the file (or to a different file).
More information about the llvm-dev