Skip to content

Commit

Permalink
cmd/bin2ll: add -o flag
Browse files Browse the repository at this point in the history
  • Loading branch information
mewmew committed Jun 13, 2017
1 parent c611f82 commit c471854
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 10 deletions.
62 changes: 53 additions & 9 deletions cmd/bin2ll/main.go
Expand Up @@ -8,13 +8,15 @@ import (
"io/ioutil"
"log"
"os"
"sort"

"github.com/decomp/exp/bin"
_ "github.com/decomp/exp/bin/elf" // register ELF decoder
_ "github.com/decomp/exp/bin/pe" // register PE decoder
_ "github.com/decomp/exp/bin/pef" // register PEF decoder
"github.com/decomp/exp/bin/raw"
"github.com/decomp/exp/lift"
"github.com/llir/llvm/ir"
"github.com/mewkiz/pkg/term"
"github.com/pkg/errors"
)
Expand Down Expand Up @@ -56,6 +58,8 @@ func main() {
// TODO: Remove -last flag and lastAddr.
// lastAddr specifies the last function address to disassemble.
lastAddr bin.Address
// output specifies the output path.
output string
// quiet specifies whether to suppress non-error messages.
quiet bool
// rawArch specifies the machine architecture of a raw binary executable.
Expand All @@ -70,6 +74,7 @@ func main() {
flag.Var(&firstAddr, "first", "first function address to lift")
flag.Var(&funcAddr, "func", "function address to lift")
flag.Var(&lastAddr, "last", "last function address to lift")
flag.StringVar(&output, "o", "", "output path")
flag.BoolVar(&quiet, "q", false, "suppress non-error messages")
flag.Var(&rawArch, "raw", "machine architecture of raw binary executable (x86_32, x86_64, PowerPC_32, ...)")
flag.Var(&rawEntry, "rawentry", "entry point of raw binary executable")
Expand Down Expand Up @@ -103,21 +108,25 @@ func main() {
}

// Lift function specified by `-func` flag.
funcAddrs := l.FuncAddrs
var funcAddrs bin.Addresses
if funcAddr != 0 {
funcAddrs = []bin.Address{funcAddr}
} else {
for _, funcAddr := range l.FuncAddrs {
if firstAddr != 0 && funcAddr < firstAddr {
// skip functions before first address.
continue
}
if lastAddr != 0 && funcAddr >= lastAddr {
// skip functions after last address.
break
}
funcAddrs = append(funcAddrs, funcAddr)
}
}

// Create function lifters.
for _, funcAddr := range funcAddrs {
if firstAddr != 0 && funcAddr < firstAddr {
// skip functions before first address.
continue
}
if lastAddr != 0 && funcAddr >= lastAddr {
// skip functions after last address.
break
}
asmFunc, err := l.DecodeFunc(funcAddr)
if err != nil {
log.Fatalf("%+v", err)
Expand All @@ -138,6 +147,41 @@ func main() {
f.Lift()
fmt.Println(f)
}

// Store LLVM IR output.
w := os.Stdout
if len(output) > 0 {
f, err := os.Create(output)
if err != nil {
log.Fatal(err)
}
defer f.Close()
w = f
}
var funcs []*ir.Function
sort.Sort(funcAddrs)
for _, funcAddr := range funcAddrs {
f := l.Funcs[funcAddr]
funcs = append(funcs, f.Function)
}
var globals []*ir.Global
var globalAddrs bin.Addresses
for globalAddr := range l.Globals {
globalAddrs = append(globalAddrs, globalAddr)
}
sort.Sort(globalAddrs)
for _, globalAddr := range globalAddrs {
g := l.Globals[globalAddr]
globals = append(globals, g)
}
m := &ir.Module{
Types: l.Types,
Globals: globals,
Funcs: funcs,
}
if _, err := fmt.Fprintln(w, m); err != nil {
log.Fatalf("%+v", err)
}
}

// newLifter returns a new x86 to LLVM IR lifter for the given binary
Expand Down
2 changes: 1 addition & 1 deletion lift/func.go
Expand Up @@ -129,7 +129,7 @@ func (l *Lifter) NewFunc(asmFunc *x86.Func) *Func {

// Lift lifts the function from input assembly to LLVM IR.
func (f *Func) Lift() {
dbg.Printf("lifting function at %v", f.AsmFunc.Addr)
dbg.Printf("lifting function %q at %v", f.Name, f.AsmFunc.Addr)
// Allocate a local variable for the FPU stack top used within the function.
if f.usesFPU {
v := ir.NewAlloca(types.I8)
Expand Down

0 comments on commit c471854

Please sign in to comment.