| 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. |
| 4 | |
| 5 | // Package gccgoexportdata provides functions for reading export data |
| 6 | // files containing type information produced by the gccgo compiler. |
| 7 | // |
| 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 gccgoexportdatascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/gccgoexportdata`/ |
| 12 | |
| 13 | // TODO(adonovan): add Find, Write, Importer to the API, |
| 14 | // for symmetry with gcexportdata. |
| 15 | |
| 16 | import ( |
| 17 | "bytesscip-go gomod github.com/golang/go/src go1.25.0 bytes/" |
| 18 | "debug/elfscip-go gomod github.com/golang/go/src go1.25.0 `debug/elf`/" |
| 19 | "fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/" |
| 20 | "go/tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/" |
| 21 | "go/typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/" |
| 22 | "ioscip-go gomod github.com/golang/go/src go1.25.0 io/" |
| 23 | "strconvscip-go gomod github.com/golang/go/src go1.25.0 strconv/" |
| 24 | "stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/" |
| 25 | |
| 26 | "golang.org/x/tools/go/internal/gccgoimporterscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/internal/gccgoimporter`/" |
| 27 | ) |
| 28 | |
| 29 | // CompilerInfo executes the specified gccgo compiler and returns |
| 30 | // information about it: its version (e.g. "4.8.0"), its target triple |
| 31 | // (e.g. "x86_64-unknown-linux-gnu"), and the list of directories it |
| 32 | // searches to find standard packages. The given arguments are passed |
| 33 | // directly to calls to the specified gccgo compiler. |
| 34 | func CompilerInfoscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/gccgoexportdata`/CompilerInfo().(gccgo string, args ...string) (version, triple string, dirs []string, err error) { |
| 35 | var inst gccgoimporterscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/internal/gccgoimporter`/.GccgoInstallationscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/internal/gccgoimporter`/GccgoInstallation# |
| 36 | err = inst.InitFromDriverscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/internal/gccgoimporter`/GccgoInstallation#InitFromDriver().(gccgo, args...) |
| 37 | if err == nil { |
| 38 | version = inst.GccVersionscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/internal/gccgoimporter`/GccgoInstallation#GccVersion. |
| 39 | triple = inst.TargetTriplescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/internal/gccgoimporter`/GccgoInstallation#TargetTriple. |
| 40 | dirs = inst.SearchPathsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/internal/gccgoimporter`/GccgoInstallation#SearchPaths().() |
| 41 | } |
| 42 | return |
| 43 | } |
| 44 | |
| 45 | // NewReader returns a reader for the export data section of an object |
| 46 | // (.o) or archive (.a) file read from r. |
| 47 | func NewReaderscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/gccgoexportdata`/NewReader().(r ioscip-go gomod github.com/golang/go/src go1.25.0 io/.Readerscip-go gomod github.com/golang/go/src go1.25.0 io/Reader#) (ioscip-go gomod github.com/golang/go/src go1.25.0 io/.Readerscip-go gomod github.com/golang/go/src go1.25.0 io/Reader#, error) { |
| 48 | data, err := ioscip-go gomod github.com/golang/go/src go1.25.0 io/.ReadAllscip-go gomod github.com/golang/go/src go1.25.0 io/ReadAll().(r) |
| 49 | if err != nil { |
| 50 | return nil, err |
| 51 | } |
| 52 | |
| 53 | // If the file is an archive, extract the first section. |
| 54 | const archiveMagic = "!<arch>\n" |
| 55 | if bytesscip-go gomod github.com/golang/go/src go1.25.0 bytes/.HasPrefixscip-go gomod github.com/golang/go/src go1.25.0 bytes/HasPrefix().(data, []byte(archiveMagic)) { |
| 56 | section, err := firstSectionscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/gccgoexportdata`/firstSection().(data[len(archiveMagic):]) |
| 57 | if err != nil { |
| 58 | return nil, err |
| 59 | } |
| 60 | data = section |
| 61 | } |
| 62 | |
| 63 | // Data contains an ELF file with a .go_export section. |
| 64 | // ELF magic number is "\x7fELF". |
| 65 | ef, err := elfscip-go gomod github.com/golang/go/src go1.25.0 `debug/elf`/.NewFilescip-go gomod github.com/golang/go/src go1.25.0 `debug/elf`/NewFile().(bytesscip-go gomod github.com/golang/go/src go1.25.0 bytes/.NewReaderscip-go gomod github.com/golang/go/src go1.25.0 bytes/NewReader().(data)) |
| 66 | if err != nil { |
| 67 | return nil, err |
| 68 | } |
| 69 | sec := ef.Sectionscip-go gomod github.com/golang/go/src go1.25.0 `debug/elf`/File#Section().(".go_export") |
| 70 | if sec == nil { |
| 71 | return nil, fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("no .go_export section") |
| 72 | } |
| 73 | return sec.Openscip-go gomod github.com/golang/go/src go1.25.0 `debug/elf`/Section#Open().(), nil |
| 74 | } |
| 75 | |
| 76 | // firstSection returns the contents of the first regular file in an ELF |
| 77 | // archive (http://www.sco.com/developers/devspecs/gabi41.pdf, §7.2). |
| 78 | func firstSectionscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/gccgoexportdata`/firstSection().(a []byte) ([]byte, error) { |
| 79 | for len(a) >= 60 { |
| 80 | var hdr []byte |
| 81 | hdr, a = a[:60], a[60:] |
| 82 | |
| 83 | name := stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.TrimSpacescip-go gomod github.com/golang/go/src go1.25.0 strings/TrimSpace().(string(hdr[:16])) |
| 84 | |
| 85 | sizeStr := string(hdr[48:58]) |
| 86 | size, err := strconvscip-go gomod github.com/golang/go/src go1.25.0 strconv/.Atoiscip-go gomod github.com/golang/go/src go1.25.0 strconv/Atoi().(stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.TrimSpacescip-go gomod github.com/golang/go/src go1.25.0 strings/TrimSpace().(sizeStr)) |
| 87 | if err != nil { |
| 88 | return nil, fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("invalid size: %q", sizeStr) |
| 89 | } |
| 90 | |
| 91 | if len(a) < size { |
| 92 | return nil, fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("invalid section size: %d", size) |
| 93 | } |
| 94 | |
| 95 | // The payload is padded to an even number of bytes. |
| 96 | var payload []byte |
| 97 | payload, a = a[:size], a[size+size&1:] |
| 98 | |
| 99 | // Skip special files: |
| 100 | // "/" archive symbol table |
| 101 | // "/SYM64/" archive symbol table on e.g. s390x |
| 102 | // "//" archive string table (if any filename is >15 bytes) |
| 103 | if name == "/" || name == "/SYM64/" || name == "//" { |
| 104 | continue |
| 105 | } |
| 106 | |
| 107 | return payload, nil |
| 108 | } |
| 109 | return nil, fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("archive has no regular sections") |
| 110 | } |
| 111 | |
| 112 | // Read reads export data from in, decodes it, and returns type |
| 113 | // information for the package. |
| 114 | // The package name is specified by path. |
| 115 | // |
| 116 | // The FileSet parameter is currently unused but exists for symmetry |
| 117 | // with gcexportdata. |
| 118 | // |
| 119 | // Read may inspect and add to the imports map to ensure that references |
| 120 | // within the export data to other packages are consistent. The caller |
| 121 | // must ensure that imports[path] does not exist, or exists but is |
| 122 | // incomplete (see types.Package.Complete), and Read inserts the |
| 123 | // resulting package into this map entry. |
| 124 | // |
| 125 | // On return, the state of the reader is undefined. |
| 126 | func Readscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/gccgoexportdata`/Read().(in ioscip-go gomod github.com/golang/go/src go1.25.0 io/.Readerscip-go gomod github.com/golang/go/src go1.25.0 io/Reader#, _ *tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/.FileSetscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/FileSet#, imports map[string]*typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Packagescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Package#, path string) (*typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Packagescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Package#, error) { |
| 127 | return gccgoimporterscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/internal/gccgoimporter`/.Parsescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/internal/gccgoimporter`/Parse().(in, imports, path) |
| 128 | } |