1 // Copyright 2016 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 // Package gccgoexportdata provides functions for reading export data
6 // files containing type information produced by the gccgo compiler.
8 // This package is a stop-gap until such time as gccgo uses the same
9 // export data format as gc; see Go issue 17573. Once that occurs, this
10 // package will be deprecated and eventually deleted.
11 package gccgoexportdata
13 // TODO(adonovan): add Find, Write, Importer to the API,
14 // for symmetry with gcexportdata.
27 "golang.org/x/tools/go/internal/gccgoimporter"
30 // CompilerInfo executes the specified gccgo compiler and returns
31 // information about it: its version (e.g. "4.8.0"), its target triple
32 // (e.g. "x86_64-unknown-linux-gnu"), and the list of directories it
33 // searches to find standard packages. The given arguments are passed
34 // directly to calls to the specified gccgo compiler.
35 func CompilerInfo(gccgo string, args ...string) (version, triple string, dirs []string, err error) {
36 var inst gccgoimporter.GccgoInstallation
37 err = inst.InitFromDriver(gccgo, args...)
39 version = inst.GccVersion
40 triple = inst.TargetTriple
41 dirs = inst.SearchPaths()
46 // NewReader returns a reader for the export data section of an object
47 // (.o) or archive (.a) file read from r.
48 func NewReader(r io.Reader) (io.Reader, error) {
49 data, err := ioutil.ReadAll(r)
54 // If the file is an archive, extract the first section.
55 const archiveMagic = "!<arch>\n"
56 if bytes.HasPrefix(data, []byte(archiveMagic)) {
57 section, err := firstSection(data[len(archiveMagic):])
64 // Data contains an ELF file with a .go_export section.
65 // ELF magic number is "\x7fELF".
66 ef, err := elf.NewFile(bytes.NewReader(data))
70 sec := ef.Section(".go_export")
72 return nil, fmt.Errorf("no .go_export section")
74 return sec.Open(), nil
77 // firstSection returns the contents of the first regular file in an ELF
78 // archive (http://www.sco.com/developers/devspecs/gabi41.pdf, ยง7.2).
79 func firstSection(a []byte) ([]byte, error) {
82 hdr, a = a[:60], a[60:]
84 name := strings.TrimSpace(string(hdr[:16]))
86 sizeStr := string(hdr[48:58])
87 size, err := strconv.Atoi(strings.TrimSpace(sizeStr))
89 return nil, fmt.Errorf("invalid size: %q", sizeStr)
93 return nil, fmt.Errorf("invalid section size: %d", size)
96 // The payload is padded to an even number of bytes.
98 payload, a = a[:size], a[size+size&1:]
100 // Skip special files:
101 // "/" archive symbol table
102 // "/SYM64/" archive symbol table on e.g. s390x
103 // "//" archive string table (if any filename is >15 bytes)
104 if name == "/" || name == "/SYM64/" || name == "//" {
110 return nil, fmt.Errorf("archive has no regular sections")
113 // Read reads export data from in, decodes it, and returns type
114 // information for the package.
115 // The package name is specified by path.
117 // The FileSet parameter is currently unused but exists for symmetry
118 // with gcexportdata.
120 // Read may inspect and add to the imports map to ensure that references
121 // within the export data to other packages are consistent. The caller
122 // must ensure that imports[path] does not exist, or exists but is
123 // incomplete (see types.Package.Complete), and Read inserts the
124 // resulting package into this map entry.
126 // On return, the state of the reader is undefined.
127 func Read(in io.Reader, _ *token.FileSet, imports map[string]*types.Package, path string) (*types.Package, error) {
128 return gccgoimporter.Parse(in, imports, path)