예제 #1
0
파일: disasm.go 프로젝트: kuangchanglang/go
func disasm_x86(code []byte, pc uint64, lookup lookupFunc, arch int) (string, int) {
	inst, err := x86asm.Decode(code, 64)
	var text string
	size := inst.Len
	if err != nil || size == 0 || inst.Op == 0 {
		size = 1
		text = "?"
	} else {
		text = x86asm.GoSyntax(inst, pc, lookup)
	}
	return text, size
}
예제 #2
0
func dis(name string, r io.Reader) error {
	var buf []byte
	var err error

	if *size >= 0 {
		r = &io.LimitedReader{r, int64(*size)}
	}
	buf, err = ioutil.ReadAll(r)

	if err != nil {
		return err
	}
	if int(len(buf)) < *offset {
		return fmt.Errorf("%v: invalid offset", name)
	}
	buf = buf[*offset:]

	pos := *offset
	for len(buf) > 0 {
		inst, err := x86asm.Decode(buf, *mode)
		if err != nil {
			fmt.Printf("%v:%x: %#-16x %s\n", name, pos, buf[0], err)
			buf = buf[1:]
			pos++
			continue
		}

		var op string
		switch *syntax {
		case "intel":
			op = x86asm.IntelSyntax(inst)
		case "gnu":
			op = x86asm.GNUSyntax(inst)
		case "go":
			op = inst.String()
		default:
			return fmt.Errorf("unknown syntax %q", *syntax)
		}
		fmt.Printf("%v:%x: %#-16x %s\n", name, pos, inst.Opcode, op)

		buf = buf[inst.Len:]
		pos += inst.Len
	}
	return nil
}