Skip to content

Commit

Permalink
cmd/internal/objfile,cmd/objdump: add disassembly support for riscv64
Browse files Browse the repository at this point in the history
Add support to `go tool objdump` for disassembling riscv64 binaries.

Revendor to bring in cmd/vendor/golang.org/x/arch/riscv64/riscv64asm,
which provides the actual disassembly implementation.

Fixes #36738

Change-Id: I0f29968509041c0c5698fc2d6910a6a0bea9d3c0
Cq-Include-Trybots: luci.golang.try:gotip-linux-riscv64
Reviewed-on: https://go-review.googlesource.com/c/go/+/622257
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Cherry Mui <cherryyz@google.com>
Reviewed-by: Carlos Amedee <carlos@golang.org>
Reviewed-by: Meng Zhuo <mengzhuo1203@gmail.com>
Reviewed-by: Michael Pratt <mpratt@google.com>
  • Loading branch information
4a6f656c committed Oct 29, 2024
1 parent aef81a7 commit 851ebc2
Show file tree
Hide file tree
Showing 11 changed files with 3,939 additions and 3 deletions.
18 changes: 17 additions & 1 deletion src/cmd/internal/objfile/disasm.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"golang.org/x/arch/arm64/arm64asm"
"golang.org/x/arch/loong64/loong64asm"
"golang.org/x/arch/ppc64/ppc64asm"
"golang.org/x/arch/riscv64/riscv64asm"
"golang.org/x/arch/s390x/s390xasm"
"golang.org/x/arch/x86/x86asm"
)
Expand Down Expand Up @@ -62,7 +63,7 @@ func (e *Entry) Disasm() (*Disasm, error) {
disasm := disasms[goarch]
byteOrder := byteOrders[goarch]
if disasm == nil || byteOrder == nil {
return nil, fmt.Errorf("unsupported architecture")
return nil, fmt.Errorf("unsupported architecture %q", goarch)
}

// Filter out section symbols, overwriting syms in place.
Expand Down Expand Up @@ -398,6 +399,19 @@ func disasm_ppc64(code []byte, pc uint64, lookup lookupFunc, byteOrder binary.By
return text, size
}

func disasm_riscv64(code []byte, pc uint64, lookup lookupFunc, byteOrder binary.ByteOrder, gnuAsm bool) (string, int) {
inst, err := riscv64asm.Decode(code)
var text string
if err != nil || inst.Op == 0 {
text = "?"
} else if gnuAsm {
text = fmt.Sprintf("%-36s // %s", riscv64asm.GoSyntax(inst, pc, lookup, textReader{code, pc}), riscv64asm.GNUSyntax(inst))
} else {
text = riscv64asm.GoSyntax(inst, pc, lookup, textReader{code, pc})
}
return text, 4
}

func disasm_s390x(code []byte, pc uint64, lookup lookupFunc, _ binary.ByteOrder, gnuAsm bool) (string, int) {
inst, err := s390xasm.Decode(code)
var text string
Expand All @@ -423,6 +437,7 @@ var disasms = map[string]disasmFunc{
"loong64": disasm_loong64,
"ppc64": disasm_ppc64,
"ppc64le": disasm_ppc64,
"riscv64": disasm_riscv64,
"s390x": disasm_s390x,
}

Expand All @@ -434,6 +449,7 @@ var byteOrders = map[string]binary.ByteOrder{
"loong64": binary.LittleEndian,
"ppc64": binary.BigEndian,
"ppc64le": binary.LittleEndian,
"riscv64": binary.LittleEndian,
"s390x": binary.BigEndian,
}

Expand Down
4 changes: 4 additions & 0 deletions src/cmd/internal/objfile/elf.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ func (f *elfFile) goarch() string {
return "ppc64le"
}
return "ppc64"
case elf.EM_RISCV:
if f.elf.Class == elf.ELFCLASS64 {
return "riscv64"
}
case elf.EM_S390:
return "s390x"
}
Expand Down
2 changes: 0 additions & 2 deletions src/cmd/objdump/objdump_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,6 @@ func mustHaveDisasm(t *testing.T) {
switch runtime.GOARCH {
case "mips", "mipsle", "mips64", "mips64le":
t.Skipf("skipping on %s, issue 12559", runtime.GOARCH)
case "riscv64":
t.Skipf("skipping on %s, issue 36738", runtime.GOARCH)
}
}

Expand Down
116 changes: 116 additions & 0 deletions src/cmd/vendor/golang.org/x/arch/riscv64/riscv64asm/arg.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 851ebc2

Please sign in to comment.