Modifying objects with MC

Hi,

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?

Thanks,
Simon

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).

David

A mutable interface to object files would be great to have but doesn't exist in any meaningful sense in LLVM today. David's hack and similar tricks are what's necessary right now.

I'd love to fix that as its a question that comes up not infrequently.

Hi,

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?

Just get a reference to the bytes you want to edit and then write through
the returned pointer. Just make that the file is mapped as writable,
otherwise you will obviously segfault.

-- Sean Silva

A mutable interface to object files would be great to have but doesn't
exist in any meaningful sense in LLVM today. David's hack and similar
tricks are what's necessary right now.

I'd love to fix that as its a question that comes up not infrequently.

Unfortunately this is a pretty tough problem to do in any generality.
Anything that isn't simply overwriting the contents of sections in the file
can effectively require rewriting the entire file. One of my realizations
when writing `yaml2obj -format=elf` was how intricately tied all the
offsets are; if you search for every use of
ContiguousBlobAccumulator::getOSAndAlignedOffset, essentially any or all of
those will need to be edited when making any nontrivial modification of an
ELF object file (and this isn't even considering e.g. PHDRS covering
sections etc.).

(there's a reason that most of this functionality is usually in a program
called objCOPY; basically objcopy is a program that copies an object file
in a semantic way (e.g. logically section by section or whatever); it then
has some if's and for's sprinkled in at various key places to handle the
various different options it has).

-- Sean Silva

A mutable interface to object files would be great to have but doesn’t exist in any meaningful sense in LLVM today. David’s hack and similar tricks are what’s necessary right now.

I’d love to fix that as its a question that comes up not infrequently.

Unfortunately this is a pretty tough problem to do in any generality. Anything that isn’t simply overwriting the contents of sections in the file can effectively require rewriting the entire file. One of my realizations when writing yaml2obj -format=elf was how intricately tied all the offsets are; if you search for every use of ContiguousBlobAccumulator::getOSAndAlignedOffset, essentially any or all of those will need to be edited when making any nontrivial modification of an ELF object file (and this isn’t even considering e.g. PHDRS covering sections etc.).

Yep, exactly. It’s a crazy hard problem in the general case, without always even having a clear right answer to some questions. For example, if I’m inserting a few bytes into a function, do I want to find and update all of the relative branch offsets and other such things to account for the extra size? Depends on what I want.

I suspect we could build something far less than the general case that’d still be useful, though. Defining it in such a way that you get intelligent failures back when asking it to do something it can’t handle may still be tricky. There’s definitely reasons this hasn’t been done yet.