From 9b85e4938f7a13886def9bd7dcf4d96db9b88a3a Mon Sep 17 00:00:00 2001 From: nganhkhoa Date: Thu, 22 Aug 2024 17:39:31 +0700 Subject: [PATCH] add rewrite header function fully rewrite the header from commands list --- macho-go/pkg/ios/macho/edit.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/macho-go/pkg/ios/macho/edit.go b/macho-go/pkg/ios/macho/edit.go index 1d75c04..b49b1f5 100644 --- a/macho-go/pkg/ios/macho/edit.go +++ b/macho-go/pkg/ios/macho/edit.go @@ -194,6 +194,29 @@ func (mc *MachoContext) RemoveUnnecessaryInfo() bool { return false } +func (mc *MachoContext) RewriteHeader() { + var offset uint64 + var start uint64 + + if mc.Is64bit() { + start = Header_size_64 + } else { + start = Header_size + } + + mc.file.Seek(0, io.SeekStart) + + offset = start + for _, cmd := range mc.commands { + nwrite, _ := mc.file.WriteAt(cmd.Serialize(mc), int64(offset)) + offset += uint64(nwrite) + } + + mc.header.ncmds = uint32(len(mc.commands)) + mc.header.sizeofcmds = uint32(offset - start) + mc.file.WriteAt(mc.header.Serialize(mc), 0) +} + func (mc *MachoContext) AddLoadCmd(lcmd LoadCommand) { var offset uint64 payload := lcmd.Serialize(mc)