From d534d62f5eeb1145120249a05449b7229f04733e Mon Sep 17 00:00:00 2001 From: nganhkhoa Date: Thu, 22 Aug 2024 17:37:28 +0700 Subject: [PATCH] add cli parsing for remove strings --- .../internal/wrapper/action/remove_strings.go | 21 +++++ macho-go/internal/wrapper/cli.go | 79 +++++++++++-------- macho-go/internal/wrapper/cli_parser.go | 1 + macho-go/internal/wrapper/program_context.go | 4 + 4 files changed, 73 insertions(+), 32 deletions(-) create mode 100644 macho-go/internal/wrapper/action/remove_strings.go diff --git a/macho-go/internal/wrapper/action/remove_strings.go b/macho-go/internal/wrapper/action/remove_strings.go new file mode 100644 index 0000000..360b1e5 --- /dev/null +++ b/macho-go/internal/wrapper/action/remove_strings.go @@ -0,0 +1,21 @@ +package action + +import ( + . "ios-wrapper/internal/wrapper/ofile" +) + +type removeStrings struct{} + +func (action *removeStrings) withMacho(mf *MachoFile) error { + mf.Context().RemoveStrings() + return nil +} + +func (action *removeStrings) withFat(ff *FatFile) error { + return defaultWithFat(action, ff) +} + +func NewRemoveStringsAction() *removeStrings { + return &removeStrings{} +} + diff --git a/macho-go/internal/wrapper/cli.go b/macho-go/internal/wrapper/cli.go index fffc048..7a7b8bb 100644 --- a/macho-go/internal/wrapper/cli.go +++ b/macho-go/internal/wrapper/cli.go @@ -117,6 +117,7 @@ func Cli() { pc.remove_inits = true pc.remove_codesign = true pc.remove_others = true + pc.remove_string = true } pc.remove_imports = arg.RemoveBindSymbols pc.remove_codesign = arg.RemoveCodeSign @@ -124,6 +125,7 @@ func Cli() { pc.remove_others = arg.RemoveOthers pc.remove_exports = arg.RemoveExports pc.remove_symbol_table = arg.RemoveSymbolTable + pc.remove_string = arg.RemoveStrings pc.dylib_to_add = arg.Dylibs pc.rpath_to_add = arg.Rpath pc.outfile = arg.Out @@ -264,41 +266,54 @@ func bcell2header(bfile string, header string) { } fmt.Fprintf(w, "};\n") - fmt.Fprintf(w, "__attribute__((section(\"__DATA,bshield\")))\n") - fmt.Fprintf(w, "char libs[] =\n") - for _, lib := range info.Symbols.Libs { - fmt.Fprintf(w, " \"%s\\0\"\n", lib) - } - fmt.Fprintf(w, ";\n") + if info.Symbols != nil { + fmt.Fprintf(w, "__attribute__((section(\"__DATA,bshield\")))\n") + fmt.Fprintf(w, "char libs[] =\n") + for _, lib := range info.Symbols.Libs { + fmt.Fprintf(w, " \"%s\\0\"\n", lib) + } + fmt.Fprintf(w, ";\n") - fmt.Fprintf(w, "__attribute__((section(\"__DATA,bshield\")))\n") - fmt.Fprintf(w, "char symbols[] =\n") - for _, symbol := range info.Symbols.Symbols { - fmt.Fprintf(w, " \"%s\\0\"\n", symbol) - } - fmt.Fprintf(w, ";\n") + fmt.Fprintf(w, "__attribute__((section(\"__DATA,bshield\")))\n") + fmt.Fprintf(w, "char symbols[] =\n") + for _, symbol := range info.Symbols.Symbols { + fmt.Fprintf(w, " \"%s\\0\"\n", symbol) + } + fmt.Fprintf(w, ";\n") - fmt.Fprintf(w, "// very compact symbol table,\n") - fmt.Fprintf(w, "// [lib idx/*4 bytes*/, nsymbol/*4 byte*/]\n") - fmt.Fprintf(w, "// repeat nsymbol times [name offset/*3 bytes*/, segment idx/**/, offset /*4 btyes*/]\n") - fmt.Fprintf(w, "// name offset is 3 bytes because we don't think we should have a table size > 2^(3 * 8)\n") + fmt.Fprintf(w, "// very compact symbol table,\n") + fmt.Fprintf(w, "// [lib idx/*4 bytes*/, nsymbol/*4 byte*/]\n") + fmt.Fprintf(w, "// repeat nsymbol times [name offset/*3 bytes*/, segment idx/**/, offset /*4 btyes*/]\n") + fmt.Fprintf(w, "// name offset is 3 bytes because we don't think we should have a table size > 2^(3 * 8)\n") - fmt.Fprintf(w, "__attribute__((section(\"__DATA,bshield\")))\n") - fmt.Fprintf(w, "uint32_t encoded_table[] = {\n") - n_instructions := 0 - for i, table := range info.Symbols.Tables { - fmt.Fprintf(w, " // %s\n", info.Symbols.Libs[i]) - fmt.Fprintf(w, " %d/*lib offset*/,\n", table.LibIndex) - fmt.Fprintf(w, " %d/*nsymbols*/,\n", table.Nsymbols) - n_instructions += 2 - for _, symbol := range table.Symbols { - fmt.Fprintf(w, " %d, 0x%x,\n", (symbol.SymbolIndex<<8)|symbol.SegmentIndex, symbol.Offset) - n_instructions += 2 - } - fmt.Fprintf(w, "\n") - } - fmt.Fprintf(w, "};\n") - fmt.Fprintf(w, "uint32_t n_instructions = %d;\n", n_instructions) + fmt.Fprintf(w, "__attribute__((section(\"__DATA,bshield\")))\n") + fmt.Fprintf(w, "uint32_t encoded_table[] = {\n") + n_instructions := 0 + for i, table := range info.Symbols.Tables { + fmt.Fprintf(w, " // %s\n", info.Symbols.Libs[i]) + fmt.Fprintf(w, " %d/*lib offset*/,\n", table.LibIndex) + fmt.Fprintf(w, " %d/*nsymbols*/,\n", table.Nsymbols) + n_instructions += 2 + for _, symbol := range table.Symbols { + fmt.Fprintf(w, " %d, 0x%x,\n", (symbol.SymbolIndex<<8)|symbol.SegmentIndex, symbol.Offset) + n_instructions += 2 + } + fmt.Fprintf(w, "\n") + } + fmt.Fprintf(w, "};\n") + fmt.Fprintf(w, "uint32_t n_instructions = %d;\n", n_instructions) + } else { + fmt.Fprintf(w, "__attribute__((section(\"__DATA,bshield\")))\n") + fmt.Fprintf(w, "char libs[] = {};\n") + + fmt.Fprintf(w, "__attribute__((section(\"__DATA,bshield\")))\n") + fmt.Fprintf(w, "char symbols[] = {};\n") + + fmt.Fprintf(w, "__attribute__((section(\"__DATA,bshield\")))\n") + fmt.Fprintf(w, "uint32_t encoded_table[] = {};\n") + + fmt.Fprintf(w, "uint32_t n_instructions = %d;\n", 0) + } fmt.Fprintf(w, "__attribute__((section(\"__DATA,bshield\")))\n") fmt.Fprintf(w, "uint32_t special_selectors_idx[] = {\n") diff --git a/macho-go/internal/wrapper/cli_parser.go b/macho-go/internal/wrapper/cli_parser.go index 2f2ed25..bfbc546 100644 --- a/macho-go/internal/wrapper/cli_parser.go +++ b/macho-go/internal/wrapper/cli_parser.go @@ -61,6 +61,7 @@ type PepeArgument struct { OFile string `arg help:"Path to Mach-O/Fat binary file" type:"existingfile"` Dylibs []string `short:"l" help:"Add more LC_DYLIB"` Rpath []string `short:"r" help:"Add more LC_RPATH"` + RemoveStrings bool `default:"false" negatable:"" help:"Remove static strings found in DATA and DATA_CONST section"` RemoveCodeSign bool `default:"false" negatable:"" help:"Remove LC_CODE_SIGNATURE"` RemoveExports bool `default:"false" negatable:"" help:"Clear the export table/trie"` RemoveSymbolTable bool `default:"false" negatable:"" help:"Remove LC_SYMTAB and LC_DYSYMTAB"` diff --git a/macho-go/internal/wrapper/program_context.go b/macho-go/internal/wrapper/program_context.go index 4cb706a..893fe42 100644 --- a/macho-go/internal/wrapper/program_context.go +++ b/macho-go/internal/wrapper/program_context.go @@ -44,6 +44,7 @@ type ProgramContext struct { remove_others bool remove_exports bool remove_symbol_table bool + remove_string bool dylib_to_add []string rpath_to_add []string symbols_keep []string @@ -106,6 +107,9 @@ func (pc *ProgramContext) Process(ofile OFile) { if pc.remove_exports { pc.AddAction(NewRemoveExportsAction()) } + if pc.remove_string { + pc.AddAction(NewRemoveStringsAction()) + } ExperimentalFeature("Remove Unnecessary Info", func() { if pc.remove_others { pc.AddAction(NewRemoveUnnecessaryInfoAction())