add keep imports action

This commit is contained in:
2023-07-11 10:06:59 +07:00
parent 557eed0254
commit 6815ea6556
11 changed files with 187 additions and 5 deletions

View File

@ -0,0 +1,26 @@
package action
import (
. "ios-wrapper/internal/wrapper/ofile"
)
type rewriteImports struct{
symbols []string
}
func (action *rewriteImports) withMacho(mf *MachoFile) error {
mc := mf.Context()
mc.RewriteImportsTable(action.symbols)
return nil
}
func (action *rewriteImports) withFat(ff *FatFile) error {
return defaultWithFat(action, ff)
}
func NewRewriteImportsWithKeepSymbolsAction(symbols []string) *rewriteImports {
return &rewriteImports{
symbols,
}
}

View File

@ -3,13 +3,16 @@ package action
import (
// "fmt"
"sort"
"strings"
// log "github.com/sirupsen/logrus"
. "ios-wrapper/internal/wrapper/ofile"
"ios-wrapper/pkg/protomodel"
)
type saveImports struct{}
type saveImports struct{
keepSymbols []string
}
func (action *saveImports) withMacho(mf *MachoFile) error {
// calculateHash := func(name string) uint32 {
@ -51,6 +54,30 @@ func (action *saveImports) withMacho(mf *MachoFile) error {
if symbol.Type() != "lazy" {
continue
}
skip := false
for _, keep := range action.keepSymbols {
name := keep
lib := ""
parts := strings.Split(keep, ",")
if len(parts) == 2 {
name = parts[0]
lib = parts[1]
}
if symbol.Name() != name {
continue
}
if lib == "" || lib == symbol.Dylib() {
skip = true
break
}
}
if skip {
continue
}
// dylib_hash := calculateHash(symbol.Dylib())
seg := mc.Segments()[symbol.Segment()]
@ -113,6 +140,8 @@ func (action *saveImports) withFat(ff *FatFile) error {
return defaultWithFat(action, ff)
}
func NewSaveImportsAction() *saveImports {
return &saveImports{}
func NewSaveImportsAction(keepSymbols []string) *saveImports {
return &saveImports{
keepSymbols,
}
}

View File

@ -128,6 +128,7 @@ func Cli() {
pc.rpath_to_add = arg.Rpath
pc.outfile = arg.Out
pc.bcellfile = arg.Bcell
pc.symbols_keep = arg.KeepImports
default:
return

View File

@ -71,6 +71,8 @@ type PepeArgument struct {
RemoveObjCString bool `default:"false" negatable:"" help:"(TODO) Remove references, string litteral to Objctive-C strings"`
RebuildLinkEdit bool `default:"false" negatable:"" help:"(TODO) Rebuild the LINKEDIT section"`
FullRemoval bool `default:"false" help:"Apply every removal possible"`
KeepImports []string `short:"keep" help:"Do not remove import symbols and allow dyld resolve. If symbols is found with more than 1 occurrances, specify the library with a comma or else all occurrances will be kept. e.g., _symbol,libLIB.dylib"`
}
type BcellToHeaderArgument struct {

View File

@ -46,6 +46,7 @@ type ProgramContext struct {
remove_symbol_table bool
dylib_to_add []string
rpath_to_add []string
symbols_keep []string
outfile string
@ -95,8 +96,9 @@ func (pc *ProgramContext) Process(ofile OFile) {
pc.AddAction(NewRemoveInitPointerAction())
}
if pc.remove_imports {
pc.AddAction(NewSaveImportsAction())
pc.AddAction(NewSaveImportsAction(pc.symbols_keep))
pc.AddAction(NewRemoveImportsAction())
pc.AddAction(NewRewriteImportsWithKeepSymbolsAction(pc.symbols_keep))
}
if pc.remove_symbol_table {
pc.AddAction(NewRemoveClassicSymbolAction())