2023-06-15 10:41:18 +07:00
|
|
|
package action
|
|
|
|
|
|
|
|
import (
|
2023-07-10 14:14:03 +07:00
|
|
|
// "fmt"
|
|
|
|
"sort"
|
2023-06-15 10:41:18 +07:00
|
|
|
// log "github.com/sirupsen/logrus"
|
|
|
|
|
|
|
|
. "ios-wrapper/internal/wrapper/ofile"
|
|
|
|
"ios-wrapper/pkg/protomodel"
|
|
|
|
)
|
|
|
|
|
|
|
|
type saveImports struct{}
|
|
|
|
|
|
|
|
func (action *saveImports) withMacho(mf *MachoFile) error {
|
2023-07-10 14:14:03 +07:00
|
|
|
// calculateHash := func(name string) uint32 {
|
|
|
|
// var h uint32 = 0x811c9dc5
|
|
|
|
// for _, s := range name {
|
|
|
|
// h ^= uint32(s)
|
|
|
|
// h *= 0x01000193
|
|
|
|
// }
|
|
|
|
// return h
|
|
|
|
// }
|
2023-06-15 10:41:18 +07:00
|
|
|
|
2023-06-26 15:28:16 +07:00
|
|
|
mc := mf.Context()
|
2023-07-10 14:14:03 +07:00
|
|
|
// symbols_storage := []*protomodel.MachoInfo_AllImportedSymbols{}
|
|
|
|
symbols_raw := mc.CollectBindSymbols()
|
|
|
|
sort.Slice(symbols_raw, func(i, j int) bool {
|
|
|
|
orderedByLibrary := symbols_raw[i].Dylib() < symbols_raw[j].Dylib()
|
|
|
|
if symbols_raw[i].Dylib() == symbols_raw[j].Dylib() {
|
|
|
|
orderedBySymbol := symbols_raw[i].Name() < symbols_raw[j].Name()
|
|
|
|
return orderedBySymbol
|
|
|
|
}
|
|
|
|
return orderedByLibrary
|
|
|
|
})
|
|
|
|
|
|
|
|
libs := []string{}
|
|
|
|
symbols := []string{}
|
|
|
|
tables := []*protomodel.MachoInfo_LibraryImportedSymbols{}
|
|
|
|
|
|
|
|
var current_table *protomodel.MachoInfo_LibraryImportedSymbols
|
|
|
|
|
|
|
|
current_lib := ""
|
|
|
|
current_symbol := ""
|
|
|
|
|
|
|
|
current_lib_idx := -1
|
|
|
|
current_symbol_idx := -1
|
|
|
|
|
|
|
|
// now we expect everything is sorted and easier to build strings tables
|
|
|
|
// this is not fully optimized, there can be repeated symbol name in different libraries
|
|
|
|
for _, symbol := range symbols_raw {
|
2023-06-26 15:28:16 +07:00
|
|
|
if symbol.Type() != "lazy" {
|
|
|
|
continue
|
|
|
|
}
|
2023-07-10 14:14:03 +07:00
|
|
|
// dylib_hash := calculateHash(symbol.Dylib())
|
2023-06-26 15:28:16 +07:00
|
|
|
seg := mc.Segments()[symbol.Segment()]
|
2023-06-15 10:41:18 +07:00
|
|
|
|
2023-06-26 15:28:16 +07:00
|
|
|
var offset uint64
|
2023-06-15 10:41:18 +07:00
|
|
|
|
2023-06-26 15:28:16 +07:00
|
|
|
if symbol.Address() >= seg.Vmaddr() {
|
|
|
|
// this is virtual address
|
|
|
|
offset = symbol.Address() - seg.Vmaddr()
|
|
|
|
} else {
|
|
|
|
// this is file address
|
|
|
|
offset = symbol.Address() - seg.Fileoff()
|
|
|
|
}
|
2023-06-15 10:41:18 +07:00
|
|
|
|
2023-07-10 14:14:03 +07:00
|
|
|
if current_lib != symbol.Dylib() {
|
|
|
|
current_lib_idx += len(current_lib) + 1
|
|
|
|
current_lib = symbol.Dylib()
|
|
|
|
libs = append(libs, symbol.Dylib())
|
|
|
|
tables = append(tables, &protomodel.MachoInfo_LibraryImportedSymbols{
|
|
|
|
LibIndex: uint32(current_lib_idx),
|
|
|
|
Nsymbols: 0,
|
|
|
|
Symbols: []*protomodel.MachoInfo_SymbolTable{},
|
2023-06-15 10:41:18 +07:00
|
|
|
})
|
2023-07-10 14:14:03 +07:00
|
|
|
current_table = tables[len(tables)-1]
|
|
|
|
}
|
|
|
|
if current_symbol != symbol.Name() {
|
|
|
|
current_symbol_idx += len(current_symbol) + 1
|
|
|
|
current_symbol = symbol.Name()
|
|
|
|
symbols = append(symbols, symbol.Name())
|
|
|
|
}
|
|
|
|
current_table.Nsymbols += 1
|
|
|
|
current_table.Symbols = append(current_table.Symbols, &protomodel.MachoInfo_SymbolTable{
|
|
|
|
SymbolIndex: uint32(current_symbol_idx),
|
|
|
|
SegmentIndex: symbol.Segment(),
|
|
|
|
Offset: uint32(offset),
|
|
|
|
})
|
2023-06-15 10:41:18 +07:00
|
|
|
|
2023-07-10 14:14:03 +07:00
|
|
|
// fmt.Printf("{\"%s\", \"%s\", 0x%x, 0x%x, 0x%x},\n",
|
|
|
|
// symbol.Name(), symbol.Dylib(), symbol.Segment(), offset)
|
|
|
|
|
|
|
|
// symbols = append(symbols,
|
|
|
|
// &protomodel.MachoInfo_BindSymbol{
|
|
|
|
// Name: symbol.Name(),
|
|
|
|
// Libname: symbol.Dylib(),
|
|
|
|
// Libhash: dylib_hash,
|
|
|
|
// Segment: symbol.Segment(),
|
|
|
|
// Offset: offset,
|
|
|
|
// })
|
|
|
|
}
|
|
|
|
mf.Info().Symbols = &protomodel.MachoInfo_AllImportedSymbols{
|
|
|
|
Libs: libs,
|
|
|
|
Symbols: symbols,
|
|
|
|
Tables: tables,
|
2023-06-26 15:28:16 +07:00
|
|
|
}
|
2023-07-10 14:14:03 +07:00
|
|
|
|
|
|
|
mf.Info().Main = mc.Main()
|
2023-06-15 10:41:18 +07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (action *saveImports) withFat(ff *FatFile) error {
|
|
|
|
return defaultWithFat(action, ff)
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewSaveImportsAction() *saveImports {
|
|
|
|
return &saveImports{}
|
|
|
|
}
|