Skip to content

Commit

Permalink
cmd/objdump: add loong64 disassembler support
Browse files Browse the repository at this point in the history
This CL provides vendor support for loong64 disassembler gnu and plan9 syntax.

cd $GOROOT/src/cmd
go get golang.org/x/arch@master
go mod tidy
go mod vendor

Change-Id: Ic8b888de0aa11cba58cbf559f8f69337d1d69309
Reviewed-on: https://go-review.googlesource.com/c/go/+/609015
Reviewed-by: Meidan Li <[email protected]>
LUCI-TryBot-Result: Go LUCI <[email protected]>
Reviewed-by: David Chase <[email protected]>
Reviewed-by: Dmitri Shuralyov <[email protected]>
Reviewed-by: sophie zhao <[email protected]>
  • Loading branch information
abner-chenc committed Sep 19, 2024
1 parent 7ba074f commit 165bf24
Show file tree
Hide file tree
Showing 12 changed files with 2,871 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/cmd/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.24

require (
github.com/google/pprof v0.0.0-20240722153945-304e4f0156b8
golang.org/x/arch v0.9.1-0.20240807172201-9d90945922a7
golang.org/x/arch v0.10.1-0.20240910142527-7874f23b9c06
golang.org/x/build v0.0.0-20240722200705-b9910f320300
golang.org/x/mod v0.20.0
golang.org/x/sync v0.8.0
Expand Down
4 changes: 2 additions & 2 deletions src/cmd/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ github.com/ianlancetaylor/demangle v0.0.0-20240312041847-bd984b5ce465 h1:KwWnWVW
github.com/ianlancetaylor/demangle v0.0.0-20240312041847-bd984b5ce465/go.mod h1:gx7rwoVhcfuVKG5uya9Hs3Sxj7EIvldVofAWIUtGouw=
github.com/yuin/goldmark v1.6.0 h1:boZcn2GTjpsynOsC0iJHnBWa4Bi0qzfJjthwauItG68=
github.com/yuin/goldmark v1.6.0/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/arch v0.9.1-0.20240807172201-9d90945922a7 h1:4+03DsxQb03qtr7e32FA8tiq18ytCUClXaXxQBDRGbs=
golang.org/x/arch v0.9.1-0.20240807172201-9d90945922a7/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys=
golang.org/x/arch v0.10.1-0.20240910142527-7874f23b9c06 h1:UQRD9d43XiTfPsm0o04gmvWqcLLGkwqV+bOwtb7AP6c=
golang.org/x/arch v0.10.1-0.20240910142527-7874f23b9c06/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys=
golang.org/x/build v0.0.0-20240722200705-b9910f320300 h1:2Cqg4LnvfD2ZpG8+6KbyYUkweWhNS3SgfcN/eeVseJ0=
golang.org/x/build v0.0.0-20240722200705-b9910f320300/go.mod h1:YsGhg4JUVUWLzdqU2wCrtpRrOveOql6w56FLDHq/CJ4=
golang.org/x/mod v0.20.0 h1:utOm6MM3R3dnawAiJgn0y+xvuYRsm1RKM/4giyfDgV0=
Expand Down
16 changes: 16 additions & 0 deletions src/cmd/internal/objfile/disasm.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

"golang.org/x/arch/arm/armasm"
"golang.org/x/arch/arm64/arm64asm"
"golang.org/x/arch/loong64/loong64asm"
"golang.org/x/arch/ppc64/ppc64asm"
"golang.org/x/arch/s390x/s390xasm"
"golang.org/x/arch/x86/x86asm"
Expand Down Expand Up @@ -367,6 +368,19 @@ func disasm_arm64(code []byte, pc uint64, lookup lookupFunc, byteOrder binary.By
return text, 4
}

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

func disasm_ppc64(code []byte, pc uint64, lookup lookupFunc, byteOrder binary.ByteOrder, gnuAsm bool) (string, int) {
inst, err := ppc64asm.Decode(code, byteOrder)
var text string
Expand Down Expand Up @@ -406,6 +420,7 @@ var disasms = map[string]disasmFunc{
"amd64": disasm_amd64,
"arm": disasm_arm,
"arm64": disasm_arm64,
"loong64": disasm_loong64,
"ppc64": disasm_ppc64,
"ppc64le": disasm_ppc64,
"s390x": disasm_s390x,
Expand All @@ -416,6 +431,7 @@ var byteOrders = map[string]binary.ByteOrder{
"amd64": binary.LittleEndian,
"arm": binary.LittleEndian,
"arm64": binary.LittleEndian,
"loong64": binary.LittleEndian,
"ppc64": binary.BigEndian,
"ppc64le": binary.LittleEndian,
"s390x": binary.BigEndian,
Expand Down
2 changes: 2 additions & 0 deletions src/cmd/internal/objfile/elf.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ func (f *elfFile) goarch() string {
return "arm"
case elf.EM_AARCH64:
return "arm64"
case elf.EM_LOONGARCH:
return "loong64"
case elf.EM_PPC64:
if f.elf.ByteOrder == binary.LittleEndian {
return "ppc64le"
Expand Down
18 changes: 16 additions & 2 deletions src/cmd/objdump/objdump_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,18 @@ var armGnuNeed = []string{ // for both ARM and AMR64
"cmp",
}

var loong64Need = []string{
"JMP main.main(SB)",
"CALL main.Println(SB)",
"RET",
}

var loong64GnuNeed = []string{
"ld.b",
"bl",
"beq",
}

var ppcNeed = []string{
"BR main.main(SB)",
"CALL main.Println(SB)",
Expand All @@ -91,8 +103,6 @@ var s390xGnuNeed = []string{

func mustHaveDisasm(t *testing.T) {
switch runtime.GOARCH {
case "loong64":
t.Skipf("skipping on %s", runtime.GOARCH)
case "mips", "mipsle", "mips64", "mips64le":
t.Skipf("skipping on %s, issue 12559", runtime.GOARCH)
case "riscv64":
Expand Down Expand Up @@ -157,6 +167,8 @@ func testDisasm(t *testing.T, srcfname string, printCode bool, printGnuAsm bool,
need = append(need, armNeed...)
case "arm64":
need = append(need, arm64Need...)
case "loong64":
need = append(need, loong64Need...)
case "ppc64", "ppc64le":
var pie bool
for _, flag := range flags {
Expand All @@ -183,6 +195,8 @@ func testDisasm(t *testing.T, srcfname string, printCode bool, printGnuAsm bool,
need = append(need, i386GnuNeed...)
case "arm", "arm64":
need = append(need, armGnuNeed...)
case "loong64":
need = append(need, loong64GnuNeed...)
case "ppc64", "ppc64le":
need = append(need, ppcGnuNeed...)
case "s390x":
Expand Down
93 changes: 93 additions & 0 deletions src/cmd/vendor/golang.org/x/arch/loong64/loong64asm/arg.go

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

Loading

0 comments on commit 165bf24

Please sign in to comment.