~vtuson/scopecreator/twitter-template

« back to all changes in this revision

Viewing changes to src/go/src/code.google.com/p/go.tools/go/gccgoimporter/importer.go

  • Committer: Victor Palau
  • Date: 2015-03-11 14:24:42 UTC
  • Revision ID: vtuson@gmail.com-20150311142442-f2pxp111c8ynv232
public release

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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.
 
4
 
 
5
// Package gccgoimporter implements Import for gccgo-generated object files.
 
6
package gccgoimporter
 
7
 
 
8
import (
 
9
        "debug/elf"
 
10
        "fmt"
 
11
        "io"
 
12
        "os"
 
13
        "path/filepath"
 
14
        "strings"
 
15
 
 
16
        "code.google.com/p/go.tools/go/types"
 
17
)
 
18
 
 
19
// Locate the file from which to read export data.
 
20
// This is intended to replicate the logic in gofrontend.
 
21
func findExportFile(searchpaths []string, pkgpath string) (string, error) {
 
22
        for _, spath := range searchpaths {
 
23
                pkgfullpath := filepath.Join(spath, pkgpath)
 
24
                pkgdir, name := filepath.Split(pkgfullpath)
 
25
 
 
26
                for _, filepath := range [...]string{
 
27
                        pkgfullpath,
 
28
                        pkgfullpath + ".gox",
 
29
                        pkgdir + "lib" + name + ".so",
 
30
                        pkgdir + "lib" + name + ".a",
 
31
                        pkgfullpath + ".o",
 
32
                } {
 
33
                        fi, err := os.Stat(filepath)
 
34
                        if err == nil && !fi.IsDir() {
 
35
                                return filepath, nil
 
36
                        }
 
37
                }
 
38
        }
 
39
 
 
40
        return "", fmt.Errorf("%s: could not find export data (tried %s)", pkgpath, strings.Join(searchpaths, ":"))
 
41
}
 
42
 
 
43
// Opens the export data file at the given path. If this is an ELF file,
 
44
// searches for and opens the .go_export section.
 
45
// This is intended to replicate the logic in gofrontend, although it doesn't handle archive files yet.
 
46
func openExportFile(fpath string) (reader io.ReadSeeker, closer io.Closer, err error) {
 
47
        f, err := os.Open(fpath)
 
48
        if err != nil {
 
49
                return
 
50
        }
 
51
        closer = f
 
52
 
 
53
        var magic [4]byte
 
54
        _, err = f.ReadAt(magic[:], 0)
 
55
        if err != nil {
 
56
                f.Close()
 
57
                return
 
58
        }
 
59
 
 
60
        if string(magic[:]) == "v1;\n" {
 
61
                // Raw export data.
 
62
                reader = f
 
63
                return
 
64
        }
 
65
 
 
66
        ef, err := elf.NewFile(f)
 
67
        if err != nil {
 
68
                f.Close()
 
69
                return
 
70
        }
 
71
 
 
72
        sec := ef.Section(".go_export")
 
73
        if sec == nil {
 
74
                err = fmt.Errorf("%s: .go_export section not found", fpath)
 
75
                f.Close()
 
76
                return
 
77
        }
 
78
 
 
79
        reader = sec.Open()
 
80
        return
 
81
}
 
82
 
 
83
func GetImporter(searchpaths []string) types.Importer {
 
84
        return func(imports map[string]*types.Package, pkgpath string) (pkg *types.Package, err error) {
 
85
                if pkgpath == "unsafe" {
 
86
                        return types.Unsafe, nil
 
87
                }
 
88
 
 
89
                fpath, err := findExportFile(searchpaths, pkgpath)
 
90
                if err != nil {
 
91
                        return
 
92
                }
 
93
 
 
94
                reader, closer, err := openExportFile(fpath)
 
95
                if err != nil {
 
96
                        return
 
97
                }
 
98
                defer closer.Close()
 
99
 
 
100
                var p parser
 
101
                p.init(fpath, reader, imports)
 
102
                pkg = p.parsePackage()
 
103
                return
 
104
        }
 
105
}