From b2363ee9f6c37f0c0ac0dd6b992c0de54c7d40f5 Mon Sep 17 00:00:00 2001
From: Wei Xiao <wei.xiao@arm.com>
Date: Tue, 9 May 2017 16:25:13 +0800
Subject: [PATCH] cmd/internal/objabi: fix the bug of shrinking SymType down to
 a uint8

Previous CL (cmd/internal/objabi: shrink SymType down to a uint8) shrinks
SymType down to a uint8 but forgot making according change in goobj.

Fixes #20296
Also add a test to catch such Goobj format inconsistency bug

Change-Id: Ib43dd7122cfcacf611a643814e95f8c5a924941f
Reviewed-on: https://go-review.googlesource.com/42971
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Michael Hudson-Doyle <michael.hudson@canonical.com>
Reviewed-by: Cherry Zhang <cherryyz@google.com>
---
 src/cmd/internal/goobj/read.go  |  2 +-
 src/cmd/internal/objabi/doc.go  |  2 +-
 src/cmd/objdump/objdump_test.go | 47 +++++++++++++++++++++++++++++++++
 3 files changed, 49 insertions(+), 2 deletions(-)

diff --git a/src/cmd/internal/goobj/read.go b/src/cmd/internal/goobj/read.go
index 587274401b8..b6c90d3bd7b 100644
--- a/src/cmd/internal/goobj/read.go
+++ b/src/cmd/internal/goobj/read.go
@@ -507,7 +507,7 @@ func (r *objReader) parseObject(prefix []byte) error {
 			break
 		}
 
-		typ := r.readInt()
+		typ := r.readByte()
 		s := &Sym{SymID: r.readSymID()}
 		r.p.Syms = append(r.p.Syms, s)
 		s.Kind = objabi.SymKind(typ)
diff --git a/src/cmd/internal/objabi/doc.go b/src/cmd/internal/objabi/doc.go
index dc37817a618..7bd5ff63e56 100644
--- a/src/cmd/internal/objabi/doc.go
+++ b/src/cmd/internal/objabi/doc.go
@@ -56,7 +56,7 @@
 // Each symbol is laid out as the following fields:
 //
 //	- byte 0xfe (sanity check for synchronization)
-//	- type [int]
+//	- type [byte]
 //	- name & version [symref index]
 //	- flags [int]
 //		1<<0 dupok
diff --git a/src/cmd/objdump/objdump_test.go b/src/cmd/objdump/objdump_test.go
index 91adde3eb3c..47e51df3392 100644
--- a/src/cmd/objdump/objdump_test.go
+++ b/src/cmd/objdump/objdump_test.go
@@ -201,3 +201,50 @@ func TestDisasmExtld(t *testing.T) {
 	}
 	testDisasm(t, false, "-ldflags=-linkmode=external")
 }
+
+func TestDisasmGoobj(t *testing.T) {
+	switch runtime.GOARCH {
+	case "arm":
+		t.Skipf("skipping on %s, issue 19811", runtime.GOARCH)
+	case "arm64":
+		t.Skipf("skipping on %s, issue 10106", runtime.GOARCH)
+	case "mips", "mipsle", "mips64", "mips64le":
+		t.Skipf("skipping on %s, issue 12559", runtime.GOARCH)
+	case "s390x":
+		t.Skipf("skipping on %s, issue 15255", runtime.GOARCH)
+	}
+
+	hello := filepath.Join(tmp, "hello.o")
+	args := []string{"tool", "compile", "-o", hello}
+	args = append(args, "testdata/fmthello.go")
+	out, err := exec.Command(testenv.GoToolPath(t), args...).CombinedOutput()
+	if err != nil {
+		t.Fatalf("go tool compile fmthello.go: %v\n%s", err, out)
+	}
+	need := []string{
+		"main(SB)",
+		"fmthello.go:6",
+	}
+
+	args = []string{
+		"-s", "main",
+		hello,
+	}
+
+	out, err = exec.Command(exe, args...).CombinedOutput()
+	if err != nil {
+		t.Fatalf("objdump fmthello.o: %v\n%s", err, out)
+	}
+
+	text := string(out)
+	ok := true
+	for _, s := range need {
+		if !strings.Contains(text, s) {
+			t.Errorf("disassembly missing '%s'", s)
+			ok = false
+		}
+	}
+	if !ok {
+		t.Logf("full disassembly:\n%s", text)
+	}
+}
-- 
GitLab