]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/internal/objfile: correct file table reading for Go object file
authorCherry Zhang <cherryyz@google.com>
Thu, 15 Oct 2020 22:04:08 +0000 (18:04 -0400)
committerCherry Zhang <cherryyz@google.com>
Fri, 16 Oct 2020 14:40:50 +0000 (14:40 +0000)
Apparently I never actually understood the new file table in Go
object files. The PC value stream actually encodes the file index
in the per-CU table. I thought it was indexing into a per-function
table, which then contains index to the per-CU table. Remove the
extra indirection.

Change-Id: I0aea5629f7b3888ebe3a04fea437aa15ce89519e
Reviewed-on: https://go-review.googlesource.com/c/go/+/262779
Trust: Cherry Zhang <cherryyz@google.com>
Run-TryBot: Cherry Zhang <cherryyz@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Than McIntosh <thanm@google.com>
Reviewed-by: Jeremy Faller <jeremy@golang.org>
src/cmd/internal/objfile/goobj.go
src/cmd/objdump/objdump_test.go
src/cmd/objdump/testdata/testfilenum/a.go [new file with mode: 0644]
src/cmd/objdump/testdata/testfilenum/b.go [new file with mode: 0644]
src/cmd/objdump/testdata/testfilenum/c.go [new file with mode: 0644]
src/cmd/objdump/testdata/testfilenum/go.mod [new file with mode: 0644]

index 7f74a8256ca6cb63d357517744a6e3924e6a124f..f19bec5dcb2d8ab8d8f271ae4f7bd476d2d4debe 100644 (file)
@@ -267,13 +267,11 @@ func (f *goobjFile) PCToLine(pc uint64) (string, int, *gosym.Func) {
                }
                b := r.BytesAt(r.DataOff(isym), r.DataSize(isym))
                var info *goobj.FuncInfo
-               lengths := info.ReadFuncInfoLengths(b)
                pcline := getSymData(info.ReadPcline(b))
                line := int(pcValue(pcline, pc-addr, f.arch))
                pcfile := getSymData(info.ReadPcfile(b))
                fileID := pcValue(pcfile, pc-addr, f.arch)
-               globalFileID := info.ReadFile(b, lengths.FileOff, uint32(fileID))
-               fileName := r.File(int(globalFileID))
+               fileName := r.File(int(fileID))
                // Note: we provide only the name in the Func structure.
                // We could provide more if needed.
                return fileName, line, &gosym.Func{Sym: &gosym.Sym{Name: osym.Name(r)}}
index 85d1a2efb0c52175fe2711e5af6571374392b8b7..d136e2e6c39b434416fd2763d08b266ac3458a63 100644 (file)
@@ -333,3 +333,41 @@ func TestDisasmGoobj(t *testing.T) {
                t.Logf("full disassembly:\n%s", text)
        }
 }
+
+func TestGoobjFileNumber(t *testing.T) {
+       // Test that file table in Go object file is parsed correctly.
+       testenv.MustHaveGoBuild(t)
+
+       t.Parallel()
+
+       tmpdir, err := ioutil.TempDir("", "TestGoobjFileNumber")
+       if err != nil {
+               t.Fatal(err)
+       }
+       defer os.RemoveAll(tmpdir)
+
+       obj := filepath.Join(tmpdir, "p.a")
+       cmd := exec.Command(testenv.GoToolPath(t), "build", "-o", obj)
+       cmd.Dir = filepath.Join("testdata/testfilenum")
+       out, err := cmd.CombinedOutput()
+       if err != nil {
+               t.Fatalf("build failed: %v\n%s", err, out)
+       }
+
+       cmd = exec.Command(exe, obj)
+       out, err = cmd.CombinedOutput()
+       if err != nil {
+               t.Fatalf("objdump failed: %v\n%s", err, out)
+       }
+
+       text := string(out)
+       for _, s := range []string{"a.go", "b.go", "c.go"} {
+               if !strings.Contains(text, s) {
+                       t.Errorf("output missing '%s'", s)
+               }
+       }
+
+       if t.Failed() {
+               t.Logf("output:\n%s", text)
+       }
+}
diff --git a/src/cmd/objdump/testdata/testfilenum/a.go b/src/cmd/objdump/testdata/testfilenum/a.go
new file mode 100644 (file)
index 0000000..2729ae0
--- /dev/null
@@ -0,0 +1,7 @@
+// Copyright 2020 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package p
+
+func A() {}
diff --git a/src/cmd/objdump/testdata/testfilenum/b.go b/src/cmd/objdump/testdata/testfilenum/b.go
new file mode 100644 (file)
index 0000000..a632aaf
--- /dev/null
@@ -0,0 +1,7 @@
+// Copyright 2020 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package p
+
+func B() {}
diff --git a/src/cmd/objdump/testdata/testfilenum/c.go b/src/cmd/objdump/testdata/testfilenum/c.go
new file mode 100644 (file)
index 0000000..d73efa7
--- /dev/null
@@ -0,0 +1,7 @@
+// Copyright 2020 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package p
+
+func C() {}
diff --git a/src/cmd/objdump/testdata/testfilenum/go.mod b/src/cmd/objdump/testdata/testfilenum/go.mod
new file mode 100644 (file)
index 0000000..db43288
--- /dev/null
@@ -0,0 +1,3 @@
+module objdumptest
+
+go 1.16