Skip to content

Commit

Permalink
chore: more binexport code
Browse files Browse the repository at this point in the history
  • Loading branch information
blacktop committed Jan 13, 2025
1 parent 6132e43 commit a98cf1a
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 5 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,8 @@ go.work.sum
# env file
.env

# debug
__debug*

# misc
*.proto
3 changes: 1 addition & 2 deletions cmd/bindiff/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
},
Expand Down
57 changes: 54 additions & 3 deletions pkg/binexport/binexport.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())

Check failure on line 61 in pkg/binexport/binexport.go

View workflow job for this annotation

GitHub Actions / build

fmt.Sprintf format %f has arg exp.GetImmediate() of wrong type uint64
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
}

0 comments on commit a98cf1a

Please sign in to comment.