Skip to content

Commit c803483

Browse files
timothy-kingGo LUCI
authored and
Go LUCI
committed
internal/gcimporter: synchronize with internal/exportdata
Use the copies of the functions from internal/exportdata. The implementations are now quite uniform between exportdata.go in GOROOT/src/internal/exportdata and internal/gcimporter as well as gcimporter.go in GOROOT/src/go/internal/gcimporter and internal/gcimporter. Fixes golang/go#70651 Change-Id: I8c2a55ca8c09d504315b9ccd7c676a5c2023ca68 Reviewed-on: https://go-review.googlesource.com/c/tools/+/633660 Commit-Queue: Tim King <taking@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Robert Griesemer <gri@google.com> Reviewed-by: Robert Findley <rfindley@google.com>
1 parent c1ff179 commit c803483

File tree

2 files changed

+19
-71
lines changed

2 files changed

+19
-71
lines changed

internal/gcimporter/exportdata.go

+12-37
Original file line numberDiff line numberDiff line change
@@ -31,58 +31,33 @@ import (
3131
// accept inputs produced by the last two releases of cmd/compile,
3232
// plus tip.
3333
func FindExportData(r *bufio.Reader) (size int64, err error) {
34-
// Read first line to make sure this is an object file.
35-
line, err := r.ReadSlice('\n')
34+
arsize, err := FindPackageDefinition(r)
3635
if err != nil {
37-
err = fmt.Errorf("can't find export data (%v)", err)
38-
return
39-
}
40-
41-
// Is the first line an archive file signature?
42-
if string(line) != "!<arch>\n" {
43-
err = fmt.Errorf("not the start of an archive file (%q)", line)
44-
return
45-
}
46-
47-
// Archive file with the first file being __.PKGDEF.
48-
arsize := readArchiveHeader(r, "__.PKGDEF")
49-
if arsize <= 0 {
50-
err = fmt.Errorf("not a package file")
5136
return
5237
}
5338
size = int64(arsize)
5439

55-
// Read first line of __.PKGDEF data, so that line
56-
// is once again the first line of the input.
57-
if line, err = r.ReadSlice('\n'); err != nil {
58-
err = fmt.Errorf("can't find export data (%v)", err)
59-
return
60-
}
61-
size -= int64(len(line))
62-
63-
// Now at __.PKGDEF in archive or still at beginning of file.
64-
// Either way, line should begin with "go object ".
65-
if !strings.HasPrefix(string(line), "go object ") {
66-
err = fmt.Errorf("not a Go object file")
40+
objapi, headers, err := ReadObjectHeaders(r)
41+
if err != nil {
6742
return
6843
}
69-
70-
// Skip over object headers to get to the export data section header "$$B\n".
71-
// Object headers are lines that do not start with '$'.
72-
for line[0] != '$' {
73-
if line, err = r.ReadSlice('\n'); err != nil {
74-
err = fmt.Errorf("can't find export data (%v)", err)
75-
return
76-
}
77-
size -= int64(len(line))
44+
size -= int64(len(objapi))
45+
for _, h := range headers {
46+
size -= int64(len(h))
7847
}
7948

8049
// Check for the binary export data section header "$$B\n".
50+
// TODO(taking): Unify with ReadExportDataHeader so that it stops at the 'u' instead of reading
51+
line, err := r.ReadSlice('\n')
52+
if err != nil {
53+
return
54+
}
8155
hdr := string(line)
8256
if hdr != "$$B\n" {
8357
err = fmt.Errorf("unknown export data header: %q", hdr)
8458
return
8559
}
60+
size -= int64(len(hdr))
8661

8762
// For files with a binary export data header "$$B\n",
8863
// these are always terminated by an end-of-section marker "\n$$\n".

internal/gcimporter/gcimporter.go

+7-34
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ const (
4646
// Import is only used in tests.
4747
func Import(fset *token.FileSet, packages map[string]*types.Package, path, srcDir string, lookup func(path string) (io.ReadCloser, error)) (pkg *types.Package, err error) {
4848
var rc io.ReadCloser
49-
var filename, id string
49+
var id string
5050
if lookup != nil {
5151
// With custom lookup specified, assume that caller has
5252
// converted path to a canonical import path for use in the map.
@@ -65,6 +65,7 @@ func Import(fset *token.FileSet, packages map[string]*types.Package, path, srcDi
6565
}
6666
rc = f
6767
} else {
68+
var filename string
6869
filename, id, err = FindPkg(path, srcDir)
6970
if filename == "" {
7071
if path == "unsafe" {
@@ -93,43 +94,15 @@ func Import(fset *token.FileSet, packages map[string]*types.Package, path, srcDi
9394
}
9495
defer rc.Close()
9596

96-
var size int64
9797
buf := bufio.NewReader(rc)
98-
if size, err = FindExportData(buf); err != nil {
99-
return
100-
}
101-
102-
var data []byte
103-
data, err = io.ReadAll(buf)
98+
data, err := ReadUnified(buf)
10499
if err != nil {
100+
err = fmt.Errorf("import %q: %v", path, err)
105101
return
106102
}
107-
if len(data) == 0 {
108-
return nil, fmt.Errorf("no data to load a package from for path %s", id)
109-
}
110103

111-
// Select appropriate importer.
112-
switch data[0] {
113-
case 'v', 'c', 'd':
114-
// binary: emitted by cmd/compile till go1.10; obsolete.
115-
return nil, fmt.Errorf("binary (%c) import format is no longer supported", data[0])
104+
// unified: emitted by cmd/compile since go1.20.
105+
_, pkg, err = UImportData(fset, packages, data, id)
116106

117-
case 'i':
118-
// indexed: emitted by cmd/compile till go1.19;
119-
// now used only for serializing go/types.
120-
// See https://github.com/golang/go/issues/69491.
121-
return nil, fmt.Errorf("indexed (i) import format is no longer supported")
122-
123-
case 'u':
124-
// unified: emitted by cmd/compile since go1.20.
125-
_, pkg, err := UImportData(fset, packages, data[1:size], id)
126-
return pkg, err
127-
128-
default:
129-
l := len(data)
130-
if l > 10 {
131-
l = 10
132-
}
133-
return nil, fmt.Errorf("unexpected export data with prefix %q for path %s", string(data[:l]), id)
134-
}
107+
return
135108
}

0 commit comments

Comments
 (0)