1 // Copyright 2013 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // Except for this comment and the import path, this file is a verbatim copy of the file
6 // with the same name in $GOROOT/src/go/internal/gccgoimporter.
8 // Package gccgoimporter implements Import for gccgo-generated object files.
9 package gccgoimporter // import "golang.org/x/tools/go/internal/gccgoimporter"
21 // A PackageInit describes an imported package that needs initialization.
22 type PackageInit struct {
23 Name string // short package name
24 InitFunc string // name of init function
25 Priority int // priority of init function, see InitData.Priority
28 // The gccgo-specific init data for a package.
29 type InitData struct {
30 // Initialization priority of this package relative to other packages.
31 // This is based on the maximum depth of the package's dependency graph;
32 // it is guaranteed to be greater than that of its dependencies.
35 // The list of packages which this package depends on to be initialized,
36 // including itself if needed. This is the subset of the transitive closure of
37 // the package's dependencies that need initialization.
41 // Locate the file from which to read export data.
42 // This is intended to replicate the logic in gofrontend.
43 func findExportFile(searchpaths []string, pkgpath string) (string, error) {
44 for _, spath := range searchpaths {
45 pkgfullpath := filepath.Join(spath, pkgpath)
46 pkgdir, name := filepath.Split(pkgfullpath)
48 for _, filepath := range [...]string{
51 pkgdir + "lib" + name + ".so",
52 pkgdir + "lib" + name + ".a",
55 fi, err := os.Stat(filepath)
56 if err == nil && !fi.IsDir() {
62 return "", fmt.Errorf("%s: could not find export data (tried %s)", pkgpath, strings.Join(searchpaths, ":"))
66 gccgov1Magic = "v1;\n"
67 gccgov2Magic = "v2;\n"
68 gccgov3Magic = "v3;\n"
69 goimporterMagic = "\n$$ "
73 // Opens the export data file at the given path. If this is an ELF file,
74 // searches for and opens the .go_export section. If this is an archive,
75 // reads the export data from the first member, which is assumed to be an ELF file.
76 // This is intended to replicate the logic in gofrontend.
77 func openExportFile(fpath string) (reader io.ReadSeeker, closer io.Closer, err error) {
78 f, err := os.Open(fpath)
84 if err != nil && closer != nil {
90 _, err = f.ReadAt(magic[:], 0)
95 var elfreader io.ReaderAt
96 switch string(magic[:]) {
97 case gccgov1Magic, gccgov2Magic, gccgov3Magic, goimporterMagic:
103 reader, err = arExportData(f)
110 ef, err := elf.NewFile(elfreader)
115 sec := ef.Section(".go_export")
117 err = fmt.Errorf("%s: .go_export section not found", fpath)
125 // An Importer resolves import paths to Packages. The imports map records
126 // packages already known, indexed by package path.
127 // An importer must determine the canonical package path and check imports
128 // to see if it is already present in the map. If so, the Importer can return
129 // the map entry. Otherwise, the importer must load the package data for the
130 // given path into a new *Package, record it in imports map, and return the
132 type Importer func(imports map[string]*types.Package, path, srcDir string, lookup func(string) (io.ReadCloser, error)) (*types.Package, error)
134 func GetImporter(searchpaths []string, initmap map[*types.Package]InitData) Importer {
135 return func(imports map[string]*types.Package, pkgpath, srcDir string, lookup func(string) (io.ReadCloser, error)) (pkg *types.Package, err error) {
136 // TODO(gri): Use srcDir.
137 // Or not. It's possible that srcDir will fade in importance as
138 // the go command and other tools provide a translation table
139 // for relative imports (like ./foo or vendored imports).
140 if pkgpath == "unsafe" {
141 return types.Unsafe, nil
144 var reader io.ReadSeeker
148 if p := imports[pkgpath]; p != nil && p.Complete() {
151 rc, err = lookup(pkgpath)
158 rs, ok := rc.(io.ReadSeeker)
160 return nil, fmt.Errorf("gccgo importer requires lookup to return an io.ReadSeeker, have %T", rc)
163 fpath = "<lookup " + pkgpath + ">"
164 // Take name from Name method (like on os.File) if present.
165 if n, ok := rc.(interface{ Name() string }); ok {
169 fpath, err = findExportFile(searchpaths, pkgpath)
174 r, closer, err := openExportFile(fpath)
185 magics, err = readMagic(reader)
190 if magics == archiveMagic {
191 reader, err = arExportData(reader)
195 magics, err = readMagic(reader)
202 case gccgov1Magic, gccgov2Magic, gccgov3Magic:
204 p.init(fpath, reader, imports)
205 pkg = p.parsePackage()
207 initmap[pkg] = p.initdata
210 // Excluded for now: Standard gccgo doesn't support this import format currently.
211 // case goimporterMagic:
213 // data, err = ioutil.ReadAll(reader)
218 // n, pkg, err = importer.ImportData(imports, data)
223 // if initmap != nil {
224 // suffixreader := bytes.NewReader(data[n:])
226 // p.init(fpath, suffixreader, nil)
228 // initmap[pkg] = p.initdata
232 err = fmt.Errorf("unrecognized magic string: %q", magics)
239 // readMagic reads the four bytes at the start of a ReadSeeker and
240 // returns them as a string.
241 func readMagic(reader io.ReadSeeker) (string, error) {
243 if _, err := reader.Read(magic[:]); err != nil {
246 if _, err := reader.Seek(0, io.SeekStart); err != nil {
249 return string(magic[:]), nil