macho/macho-go/internal/wrapper/action/save_imports.go

71 lines
1.7 KiB
Go
Raw Normal View History

2023-06-15 10:41:18 +07:00
package action
import (
2023-06-26 15:28:16 +07:00
"fmt"
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-06-26 15:28:16 +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-06-15 10:41:18 +07:00
symbols := []*protomodel.MachoInfo_BindSymbol{}
2023-06-26 15:28:16 +07:00
fmt.Println("struct imported_symbol {const char* name; const char* lib; uint32_t hash; int segment_i; uint64_t offset;};")
fmt.Println("const char* lib_to_resolve = \"main\";")
fmt.Println("struct imported_symbol imported_table[] = {")
for _, symbol := range mc.CollectBindSymbols() {
if symbol.Type() != "lazy" {
continue
}
dylib_hash := calculateHash(symbol.Dylib())
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-06-26 15:28:16 +07:00
fmt.Printf("{\"%s\", \"%s\", 0x%x, 0x%x, 0x%x},\n",
symbol.Name(), symbol.Dylib(), dylib_hash, symbol.Segment(), offset)
2023-06-15 10:41:18 +07:00
symbols = append(symbols,
&protomodel.MachoInfo_BindSymbol{
2023-06-26 15:28:16 +07:00
Name: symbol.Name(),
Libname: symbol.Dylib(),
Libhash: dylib_hash,
Segment: symbol.Segment(),
Offset: offset,
2023-06-15 10:41:18 +07:00
})
2023-06-26 15:28:16 +07:00
}
fmt.Println("};")
fmt.Printf("uint32_t nimports = %d;\n", len(symbols))
2023-06-15 10:41:18 +07:00
mf.Info().Symbols = symbols
return nil
}
func (action *saveImports) withFat(ff *FatFile) error {
return defaultWithFat(action, ff)
}
func NewSaveImportsAction() *saveImports {
return &saveImports{}
}