load_elf.go 2.44 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
	"github.com/ethereum-optimism/optimism/cannon/mipsevm"
10
	"github.com/ethereum-optimism/optimism/op-service/jsonutil"
protolambda's avatar
protolambda committed
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
)

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",
28
		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
29 30 31
		Value:    "state.json",
		Required: false,
	}
32 33 34 35 36 37
	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
38
)
39 40

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

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