From 6d757108a7a66293155f33e050c4817a5b5e5cb8 Mon Sep 17 00:00:00 2001 From: nganhkhoa Date: Mon, 5 Jun 2023 15:55:21 +0700 Subject: [PATCH] support for removing LC_DYLD_FIXUPS_CHAINS data --- macho-go/pkg/ios/macho/edit.go | 58 +++++++++++++--------------------- 1 file changed, 22 insertions(+), 36 deletions(-) diff --git a/macho-go/pkg/ios/macho/edit.go b/macho-go/pkg/ios/macho/edit.go index 9f4d48e..7022e14 100644 --- a/macho-go/pkg/ios/macho/edit.go +++ b/macho-go/pkg/ios/macho/edit.go @@ -278,40 +278,6 @@ func (mc *MachoContext) RemoveBindSymbols() { } else { mc.removeBindSymbolsLegacy() } -} - -func (mc *MachoContext) removeBindSymbolsModern() { - calculateHash := func(name string) uint32 { - var h uint32 = 0x811c9dc5 - for _, s := range name { - h ^= uint32(s) - h *= 0x01000193 - } - return h - } - - // due to some limitations when design this tool - // we write the c code to stdout lol - fmt.Println("struct imported_symbol {const char* name; const char* lib; uint32_t hash; uint64_t address;};") - fmt.Println("struct imported_symbol imported_table[] = {") - count := 0 - for _, symbol := range mc.CollectBindSymbols() { - count += 1 - dylib_hash := calculateHash(symbol.Dylib()) - fmt.Printf("{\"%s\", \"%s\", 0x%x, 0x%x},\n", - symbol.Name(), symbol.Dylib(), dylib_hash, symbol.Address()); - mc.file.WriteAt(make([]byte, 8), int64(symbol.file_address)) - } - fmt.Println("};") - fmt.Printf("uint32_t nimports = %d;\n", count); -} - -func (mc *MachoContext) removeBindSymbolsLegacy() { - start := mc.dyldinfo.lazy_bind_off - size := mc.dyldinfo.lazy_bind_size - // set lazy opcodes to 0x00 == DO_BIND - // but no symbol state to bind - mc.file.WriteAt(make([]byte, size), int64(start)) calculateHash := func(name string) uint32 { var h uint32 = 0x811c9dc5 @@ -334,12 +300,32 @@ func (mc *MachoContext) removeBindSymbolsLegacy() { count += 1 dylib_hash := calculateHash(symbol.Dylib()) seg := mc.segments[symbol.segment] - offset := symbol.address - seg.Vmaddr() + + var offset uint64 + + if symbol.address >= seg.Vmaddr() { + // this is virtual address + offset = symbol.address - seg.Vmaddr() + } else { + // this is file address + offset = symbol.address - seg.Fileoff() + } fmt.Printf("{\"%s\", \"%s\", 0x%x, 0x%x, 0x%x},\n", symbol.Name(), symbol.Dylib(), dylib_hash, symbol.segment, offset); - mc.file.WriteAt(make([]byte, 8), int64(seg.Fileoff() + offset)) + mc.file.WriteAt(make([]byte, 8), int64(symbol.file_address)) } fmt.Println("};") fmt.Printf("uint32_t nimports = %d;\n", count); } + +func (mc *MachoContext) removeBindSymbolsModern() { +} + +func (mc *MachoContext) removeBindSymbolsLegacy() { + start := mc.dyldinfo.lazy_bind_off + size := mc.dyldinfo.lazy_bind_size + // set lazy opcodes to 0x00 == DO_BIND + // but no symbol state to bind + mc.file.WriteAt(make([]byte, size), int64(start)) +}