load_elf.go 2.55 KB
Newer Older
1 2
package cmd

protolambda's avatar
protolambda committed
3 4 5 6 7 8
import (
	"debug/elf"
	"fmt"

	"github.com/urfave/cli/v2"

9 10
	"github.com/ethereum-optimism/optimism/cannon/mipsevm/program"
	"github.com/ethereum-optimism/optimism/cannon/mipsevm/singlethreaded"
11
	"github.com/ethereum-optimism/optimism/op-service/jsonutil"
protolambda's avatar
protolambda committed
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
)

var (
	LoadELFPathFlag = &cli.PathFlag{
		Name:      "path",
		Usage:     "Path to 32-bit big-endian MIPS ELF file",
		TakesFile: true,
		Required:  true,
	}
	LoadELFPatchFlag = &cli.StringSliceFlag{
		Name:     "patch",
		Usage:    "Type of patching to do",
		Value:    cli.NewStringSlice("go", "stack"),
		Required: false,
	}
	LoadELFOutFlag = &cli.PathFlag{
		Name:     "out",
29
		Usage:    "Output path to write JSON state to. State is dumped to stdout if set to -. Not written if empty.",
protolambda's avatar
protolambda committed
30 31 32
		Value:    "state.json",
		Required: false,
	}
33 34 35 36 37 38
	LoadELFMetaFlag = &cli.PathFlag{
		Name:     "meta",
		Usage:    "Write metadata file, for symbol lookup during program execution. None if empty.",
		Value:    "meta.json",
		Required: false,
	}
protolambda's avatar
protolambda committed
39
)
40 41

func LoadELF(ctx *cli.Context) error {
protolambda's avatar
protolambda committed
42 43 44 45 46
	elfPath := ctx.Path(LoadELFPathFlag.Name)
	elfProgram, err := elf.Open(elfPath)
	if err != nil {
		return fmt.Errorf("failed to open ELF file %q: %w", elfPath, err)
	}
47 48 49
	if elfProgram.Machine != elf.EM_MIPS {
		return fmt.Errorf("ELF is not big-endian MIPS R3000, but got %q", elfProgram.Machine.String())
	}
50
	state, err := program.LoadELF(elfProgram, singlethreaded.CreateInitialState)
protolambda's avatar
protolambda committed
51 52 53 54 55 56
	if err != nil {
		return fmt.Errorf("failed to load ELF data into VM state: %w", err)
	}
	for _, typ := range ctx.StringSlice(LoadELFPatchFlag.Name) {
		switch typ {
		case "stack":
57
			err = program.PatchStack(state)
protolambda's avatar
protolambda committed
58
		case "go":
59
			err = program.PatchGo(elfProgram, state)
protolambda's avatar
protolambda committed
60 61 62 63 64 65 66
		default:
			return fmt.Errorf("unrecognized form of patching: %q", typ)
		}
		if err != nil {
			return fmt.Errorf("failed to apply patch %s: %w", typ, err)
		}
	}
67
	meta, err := program.MakeMetadata(elfProgram)
68 69 70
	if err != nil {
		return fmt.Errorf("failed to compute program metadata: %w", err)
	}
71
	if err := jsonutil.WriteJSON[*program.Metadata](ctx.Path(LoadELFMetaFlag.Name), meta, OutFilePerm); err != nil {
72 73
		return fmt.Errorf("failed to output metadata: %w", err)
	}
74
	return jsonutil.WriteJSON[*singlethreaded.State](ctx.Path(LoadELFOutFlag.Name), state, OutFilePerm)
75 76 77 78
}

var LoadELFCommand = &cli.Command{
	Name:        "load-elf",
protolambda's avatar
protolambda committed
79 80
	Usage:       "Load ELF file into Cannon JSON state",
	Description: "Load ELF file into Cannon JSON state, optionally patch out functions",
81
	Action:      LoadELF,
protolambda's avatar
protolambda committed
82 83 84 85
	Flags: []cli.Flag{
		LoadELFPathFlag,
		LoadELFPatchFlag,
		LoadELFOutFlag,
86
		LoadELFMetaFlag,
protolambda's avatar
protolambda committed
87
	},
88
}