From 94cf54e8619e03671b33113d39306646235feb60 Mon Sep 17 00:00:00 2001 From: Shenghou Ma Date: Mon, 4 May 2015 01:21:27 -0400 Subject: [PATCH] cmd/internal/objfile: add ppc64/ppc64le disassembler support Change-Id: I7d213b4f8e4cda73ea7687fb97dbd22e58163948 Reviewed-on: https://go-review.googlesource.com/9682 Run-TryBot: Russ Cox TryBot-Result: Gobot Gobot Reviewed-by: Russ Cox --- src/cmd/internal/objfile/disasm.go | 33 ++++++++++++++++++++++-------- src/cmd/internal/objfile/elf.go | 4 ++++ 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/src/cmd/internal/objfile/disasm.go b/src/cmd/internal/objfile/disasm.go index 771187bfe4..315aaed2ab 100644 --- a/src/cmd/internal/objfile/disasm.go +++ b/src/cmd/internal/objfile/disasm.go @@ -16,6 +16,7 @@ import ( "text/tabwriter" "golang.org/x/arch/arm/armasm" + "golang.org/x/arch/ppc64/ppc64asm" "golang.org/x/arch/x86/x86asm" ) @@ -49,6 +50,7 @@ func (f *File) Disasm() (*Disasm, error) { } goarch := f.GOARCH() + println("GOARCH", goarch) disasm := disasms[goarch] byteOrder := byteOrders[goarch] if disasm == nil || byteOrder == nil { @@ -170,7 +172,7 @@ func (d *Disasm) Decode(start, end uint64, relocs []Reloc, f func(pc, size uint6 lookup := d.lookup for pc := start; pc < end; { i := pc - d.textStart - text, size := d.disasm(code[i:], pc, lookup) + text, size := d.disasm(code[i:], pc, lookup, d.byteOrder) file, line, _ := d.pcln.PCToLine(pc) text += "\t" first := true @@ -189,13 +191,13 @@ func (d *Disasm) Decode(start, end uint64, relocs []Reloc, f func(pc, size uint6 } type lookupFunc func(addr uint64) (sym string, base uint64) -type disasmFunc func(code []byte, pc uint64, lookup lookupFunc) (text string, size int) +type disasmFunc func(code []byte, pc uint64, lookup lookupFunc, ord binary.ByteOrder) (text string, size int) -func disasm_386(code []byte, pc uint64, lookup lookupFunc) (string, int) { +func disasm_386(code []byte, pc uint64, lookup lookupFunc, _ binary.ByteOrder) (string, int) { return disasm_x86(code, pc, lookup, 32) } -func disasm_amd64(code []byte, pc uint64, lookup lookupFunc) (string, int) { +func disasm_amd64(code []byte, pc uint64, lookup lookupFunc, _ binary.ByteOrder) (string, int) { return disasm_x86(code, pc, lookup, 64) } @@ -232,7 +234,7 @@ func (r textReader) ReadAt(data []byte, off int64) (n int, err error) { return } -func disasm_arm(code []byte, pc uint64, lookup lookupFunc) (string, int) { +func disasm_arm(code []byte, pc uint64, lookup lookupFunc, _ binary.ByteOrder) (string, int) { inst, err := armasm.Decode(code, armasm.ModeARM) var text string size := inst.Len @@ -245,10 +247,25 @@ func disasm_arm(code []byte, pc uint64, lookup lookupFunc) (string, int) { return text, size } +func disasm_ppc64(code []byte, pc uint64, lookup lookupFunc, byteOrder binary.ByteOrder) (string, int) { + inst, err := ppc64asm.Decode(code, byteOrder) + var text string + size := inst.Len + if err != nil || size == 0 || inst.Op == 0 { + size = 4 + text = "?" + } else { + text = ppc64asm.GoSyntax(inst, pc, lookup) + } + return text, size +} + var disasms = map[string]disasmFunc{ - "386": disasm_386, - "amd64": disasm_amd64, - "arm": disasm_arm, + "386": disasm_386, + "amd64": disasm_amd64, + "arm": disasm_arm, + "ppc64": disasm_ppc64, + "ppc64le": disasm_ppc64, } var byteOrders = map[string]binary.ByteOrder{ diff --git a/src/cmd/internal/objfile/elf.go b/src/cmd/internal/objfile/elf.go index c8114603d7..4ab7e6deb8 100644 --- a/src/cmd/internal/objfile/elf.go +++ b/src/cmd/internal/objfile/elf.go @@ -9,6 +9,7 @@ package objfile import ( "debug/dwarf" "debug/elf" + "encoding/binary" "fmt" "os" ) @@ -99,6 +100,9 @@ func (f *elfFile) goarch() string { case elf.EM_ARM: return "arm" case elf.EM_PPC64: + if f.elf.ByteOrder == binary.LittleEndian { + return "ppc64le" + } return "ppc64" case elf.EM_S390: return "s390x" -- 2.48.1