diff --git a/.gitignore b/.gitignore index 45a1bb9..dda9bef 100644 --- a/.gitignore +++ b/.gitignore @@ -24,5 +24,8 @@ go.work.sum # env file .env +# debug +__debug* + # misc *.proto \ No newline at end of file diff --git a/cmd/bindiff/cmd/root.go b/cmd/bindiff/cmd/root.go index 4dc9ba7..5450bea 100644 --- a/cmd/bindiff/cmd/root.go +++ b/cmd/bindiff/cmd/root.go @@ -49,8 +49,7 @@ var rootCmd = &cobra.Command{ log.SetLevel(log.DebugLevel) } - binexport := binexport.NewBinExport(args[0]) - if err := binexport.Run(); err != nil { + if err := binexport.NewBinExport(args[0]).Run(); err != nil { log.Fatalf("failed to run binexport: %v", err) } }, diff --git a/pkg/binexport/binexport.go b/pkg/binexport/binexport.go index b770245..5244a5c 100644 --- a/pkg/binexport/binexport.go +++ b/pkg/binexport/binexport.go @@ -17,15 +17,66 @@ func NewBinExport(path string) *BinExport { } } +// PACIBSP +// STP X20, X19, [SP,#-0x10+var_10]! +// STP X29, X30, [SP,#0x10+var_s0] +// ADD X29, SP, #0x10 +// CBZ s, loc_FFFFFE00072B4038 + func (b *BinExport) Run() error { data, err := os.ReadFile(b.path) if err != nil { return fmt.Errorf("failed to read file: %w", err) } - var exp BinExport2 - if err := proto.Unmarshal(data, &exp); err != nil { + + var bexp BinExport2 + if err := proto.Unmarshal(data, &bexp); err != nil { return fmt.Errorf("failed to unmarshal BinExport2: %w", err) } - fmt.Println(exp.MetaInformation) + + fmt.Println(bexp.GetMetaInformation()) + + for _, flow := range bexp.GetFlowGraph() { + bb := bexp.BasicBlock[flow.GetEntryBasicBlockIndex()] + for _, blockInst := range bb.GetInstructionIndex() { + var prevAddr uint64 + for _, inst := range bexp.Instruction[blockInst.GetBeginIndex():blockInst.GetEndIndex()] { + if inst.Address != nil { + mnemonic := bexp.Mnemonic[inst.GetMnemonicIndex()] + fmt.Printf("%#x: %s\n", inst.GetAddress(), mnemonic.GetName()) + prevAddr = inst.GetAddress() + } else { + mnemonic := bexp.Mnemonic[inst.GetMnemonicIndex()] + var out string + for _, oidx := range inst.GetOperandIndex() { + for _, eidx := range bexp.Operand[oidx].GetExpressionIndex() { + exp := bexp.Expression[eidx] + fmt.Printf("expression: %d) %s\n", eidx, exp) + switch exp.GetType() { + case BinExport2_Expression_SYMBOL: + out += exp.GetSymbol() + case BinExport2_Expression_IMMEDIATE_INT: + out += fmt.Sprintf("%d", int64(exp.GetImmediate())) + case BinExport2_Expression_IMMEDIATE_FLOAT: + out += fmt.Sprintf("%f", exp.GetImmediate()) + case BinExport2_Expression_OPERATOR: + out += exp.GetSymbol() + case BinExport2_Expression_REGISTER: + out += exp.GetSymbol() + ", " + case BinExport2_Expression_SIZE_PREFIX: + // out += exp.GetSymbol() + case BinExport2_Expression_DEREFERENCE: + out += exp.GetSymbol() + default: + out += "unknown" + } + } + } + fmt.Printf("%#x: %s\t%s\n", prevAddr, mnemonic.GetName(), out) + } + } + } + } + return nil }