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
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). 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. Sent from my iPhone> On Aug 25, 2015, at 2:44 AM, David Chisnall via llvm-dev <llvm-dev at lists.llvm.org> wrote: > >> 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). > > David > > _______________________________________________ > LLVM Developers mailing list > llvm-dev at lists.llvm.org > https://urldefense.proofpoint.com/v2/url?u=http-3A__lists.llvm.org_cgi-2Dbin_mailman_listinfo_llvm-2Ddev&d=BQIGaQ&c=eEvniauFctOgLOKGJOplqw&r=k9CvPunowVJLjY-2Ua_VyCOW6auehHN4cbqltF2kp_A&m=GczEuegfI8sGgibmqcHyOw0fHoAApuDIg-XPJ_y2K_Q&s=au_QoXAJpl_xH8RN_0W4DnzdE3-SZNPJNTAWagUlrbI&e=
On Tue, Aug 25, 2015 at 2:30 AM, Simon Cook via llvm-dev < llvm-dev at lists.llvm.org> wrote:> 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> > Thanks, > Simon > _______________________________________________ > LLVM Developers mailing list > llvm-dev at lists.llvm.org > http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20150826/b649149f/attachment.html>
Maybe Matching Threads
- Modifying objects with MC
- [LLVMdev] Win32 COFF Support - Patch 3
- [LLVMdev] ELFObjectFile changes, llvm-objdump showing 'wrong' values?
- [LLVMdev] ELFObjectFile changes, llvm-objdump showing 'wrong' values?
- [LLVMdev] ELFObjectFile changes, llvm-objdump showing 'wrong' values?