From 557eed0254f8c73a7b9e75190d94c5f27ce632cd Mon Sep 17 00:00:00 2001 From: nganhkhoa Date: Tue, 11 Jul 2023 10:05:58 +0700 Subject: [PATCH] small changes to remove imports action --- .../internal/wrapper/action/remove_imports.go | 2 -- macho-go/pkg/ios/macho/dyld_info.go | 15 +++++++++++---- macho-go/pkg/ios/macho/edit.go | 5 ++++- 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/macho-go/internal/wrapper/action/remove_imports.go b/macho-go/internal/wrapper/action/remove_imports.go index 310c21f..ada28d1 100644 --- a/macho-go/internal/wrapper/action/remove_imports.go +++ b/macho-go/internal/wrapper/action/remove_imports.go @@ -8,8 +8,6 @@ type removeImports struct{} func (action *removeImports) withMacho(mf *MachoFile) error { mf.Context().RemoveBindSymbols() - mf.Context().RemoveSymbolTable() - mf.Context().RemoveExportTrie() return nil } diff --git a/macho-go/pkg/ios/macho/dyld_info.go b/macho-go/pkg/ios/macho/dyld_info.go index 10c3c54..f0a271c 100644 --- a/macho-go/pkg/ios/macho/dyld_info.go +++ b/macho-go/pkg/ios/macho/dyld_info.go @@ -25,6 +25,7 @@ type ImportSymbol struct { segment_offset uint32 address uint64 file_address uint64 + lib_ordinal uint32 // push number pnum uint32 @@ -45,6 +46,10 @@ func (sym *ImportSymbol) Dylib() string { return sym.dylib } +func (sym *ImportSymbol) LibOrdinal() uint32 { + return sym.lib_ordinal +} + func (sym *ImportSymbol) Address() uint64 { return sym.address } @@ -116,16 +121,17 @@ func (mc *MachoContext) CollectBindSymbolsModern() []*ImportSymbol { // fmt.Printf("segment=%x format=%x page_count=%d\n", fix.segment, fix.format, fix.page_count) // fmt.Printf("pages=%x\n", fix.pages) pages := ([]C.ushort)(unsafe.Slice(fix.pages, fix.page_count)) + reader := bytes.NewReader(mc.buf) for page_i := 0; page_i < int(fix.page_count); page_i++ { // fmt.Printf(" page offset=%x\n", pages[page_i]) address := int64(fix.segment) + int64(pages[page_i]) - mc.file.Seek(address, io.SeekStart) + reader.Seek(address, io.SeekStart) code := make([]byte, 8) for { - mc.file.Read(code) + reader.Read(code) v := mc.byteorder.Uint64(code) var bind C.int @@ -145,6 +151,7 @@ func (mc *MachoContext) CollectBindSymbolsModern() []*ImportSymbol { sym.name = name sym.dylib = dylib sym.typ = "lazy" + sym.lib_ordinal = uint32(s.lib_ordinal) sym.segment = uint32(mc.findSegmentIndexAt(uint64(address))) sym.file_address = uint64(address) @@ -160,9 +167,9 @@ func (mc *MachoContext) CollectBindSymbolsModern() []*ImportSymbol { } // because the pointer move up 8 bytes already so we minus 8 address += int64(next * 4) - mc.file.Seek(int64(next*4)-8, io.SeekCurrent) + reader.Seek(int64(next*4)-8, io.SeekCurrent) } - mc.file.Seek(0, io.SeekStart) + reader.Seek(0, io.SeekStart) } } return syms diff --git a/macho-go/pkg/ios/macho/edit.go b/macho-go/pkg/ios/macho/edit.go index f4ba622..4442e75 100644 --- a/macho-go/pkg/ios/macho/edit.go +++ b/macho-go/pkg/ios/macho/edit.go @@ -282,7 +282,10 @@ func (mc *MachoContext) RemoveBindSymbols() { } else { mc.removeBindSymbolsLegacy() } - mc.ReworkForObjc() + // Objective-C stub replaces main which can only appears in executable + if mc.Header().IsExecutable() { + mc.ReworkForObjc() + } // due to some limitations when design this tool // we write the c code to stdout lol