2015-02-27 22:57:28 -05:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
2015-04-24 20:24:49 -07:00
|
|
|
// go-specific code shared across loaders (5l, 6l, 8l).
|
|
|
|
|
|
2015-02-27 22:57:28 -05:00
|
|
|
package ld
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bytes"
|
|
|
|
|
"cmd/internal/obj"
|
|
|
|
|
"fmt"
|
|
|
|
|
"os"
|
2015-03-05 17:45:11 -08:00
|
|
|
"strconv"
|
2015-02-27 22:57:28 -05:00
|
|
|
"strings"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// go-specific code shared across loaders (5l, 6l, 8l).
|
|
|
|
|
|
|
|
|
|
// replace all "". with pkg.
|
|
|
|
|
func expandpkg(t0 string, pkg string) string {
|
|
|
|
|
return strings.Replace(t0, `"".`, pkg+".", -1)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// accumulate all type information from .6 files.
|
|
|
|
|
// check for inconsistencies.
|
|
|
|
|
|
|
|
|
|
// TODO:
|
|
|
|
|
// generate debugging section in binary.
|
|
|
|
|
// once the dust settles, try to move some code to
|
|
|
|
|
// libmach, so that other linkers and ar can share.
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* package import data
|
|
|
|
|
*/
|
|
|
|
|
type Import struct {
|
2015-03-05 17:45:11 -08:00
|
|
|
prefix string // "type", "var", "func", "const"
|
2015-02-27 22:57:28 -05:00
|
|
|
name string
|
|
|
|
|
def string
|
|
|
|
|
file string
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-05 17:45:11 -08:00
|
|
|
// importmap records type information about imported symbols to detect inconsistencies.
|
|
|
|
|
// Entries are keyed by qualified symbol name (e.g., "runtime.Callers" or "net/url.Error").
|
|
|
|
|
var importmap = map[string]*Import{}
|
2015-02-27 22:57:28 -05:00
|
|
|
|
2015-03-05 17:45:11 -08:00
|
|
|
func lookupImport(name string) *Import {
|
|
|
|
|
if x, ok := importmap[name]; ok {
|
|
|
|
|
return x
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
2015-03-05 17:45:11 -08:00
|
|
|
x := &Import{name: name}
|
|
|
|
|
importmap[name] = x
|
2015-02-27 22:57:28 -05:00
|
|
|
return x
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-02 12:44:49 +10:00
|
|
|
func ldpkg(f *obj.Biobuf, pkg string, length int64, filename string, whence int) {
|
2015-02-27 22:57:28 -05:00
|
|
|
var p0, p1 int
|
|
|
|
|
|
|
|
|
|
if Debug['g'] != 0 {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if int64(int(length)) != length {
|
|
|
|
|
fmt.Fprintf(os.Stderr, "%s: too much pkg data in %s\n", os.Args[0], filename)
|
|
|
|
|
if Debug['u'] != 0 {
|
2015-04-09 07:37:17 -04:00
|
|
|
errorexit()
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-02 12:35:15 -05:00
|
|
|
bdata := make([]byte, length)
|
2015-05-02 12:44:49 +10:00
|
|
|
if int64(obj.Bread(f, bdata)) != length {
|
2015-02-27 22:57:28 -05:00
|
|
|
fmt.Fprintf(os.Stderr, "%s: short pkg read %s\n", os.Args[0], filename)
|
|
|
|
|
if Debug['u'] != 0 {
|
2015-04-09 07:37:17 -04:00
|
|
|
errorexit()
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
2015-03-02 12:35:15 -05:00
|
|
|
data := string(bdata)
|
2015-02-27 22:57:28 -05:00
|
|
|
|
|
|
|
|
// first \n$$ marks beginning of exports - skip rest of line
|
|
|
|
|
p0 = strings.Index(data, "\n$$")
|
|
|
|
|
if p0 < 0 {
|
|
|
|
|
if Debug['u'] != 0 && whence != ArchiveObj {
|
2015-04-09 07:37:17 -04:00
|
|
|
Exitf("cannot find export data in %s", filename)
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
p0 += 3
|
2015-03-05 17:00:18 -08:00
|
|
|
for p0 < len(data) && data[p0] != '\n' {
|
2015-02-27 22:57:28 -05:00
|
|
|
p0++
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// second marks end of exports / beginning of local data
|
|
|
|
|
p1 = strings.Index(data[p0:], "\n$$")
|
|
|
|
|
if p1 < 0 {
|
|
|
|
|
fmt.Fprintf(os.Stderr, "%s: cannot find end of exports in %s\n", os.Args[0], filename)
|
|
|
|
|
if Debug['u'] != 0 {
|
2015-04-09 07:37:17 -04:00
|
|
|
errorexit()
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
p1 += p0
|
|
|
|
|
|
2015-03-05 17:00:18 -08:00
|
|
|
for p0 < p1 && (data[p0] == ' ' || data[p0] == '\t' || data[p0] == '\n') {
|
2015-02-27 22:57:28 -05:00
|
|
|
p0++
|
|
|
|
|
}
|
|
|
|
|
if p0 < p1 {
|
|
|
|
|
if !strings.HasPrefix(data[p0:], "package ") {
|
|
|
|
|
fmt.Fprintf(os.Stderr, "%s: bad package section in %s - %.20s\n", os.Args[0], filename, data[p0:])
|
|
|
|
|
if Debug['u'] != 0 {
|
2015-04-09 07:37:17 -04:00
|
|
|
errorexit()
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
p0 += 8
|
|
|
|
|
for p0 < p1 && (data[p0] == ' ' || data[p0] == '\t' || data[p0] == '\n') {
|
|
|
|
|
p0++
|
|
|
|
|
}
|
2015-03-05 17:00:18 -08:00
|
|
|
pname := p0
|
2015-02-27 22:57:28 -05:00
|
|
|
for p0 < p1 && data[p0] != ' ' && data[p0] != '\t' && data[p0] != '\n' {
|
|
|
|
|
p0++
|
|
|
|
|
}
|
|
|
|
|
if Debug['u'] != 0 && whence != ArchiveObj && (p0+6 > p1 || !strings.HasPrefix(data[p0:], " safe\n")) {
|
2015-04-09 07:37:17 -04:00
|
|
|
Exitf("load of unsafe package %s", filename)
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
2015-03-05 17:00:18 -08:00
|
|
|
name := data[pname:p0]
|
|
|
|
|
for p0 < p1 && data[p0] != '\n' {
|
|
|
|
|
p0++
|
|
|
|
|
}
|
2015-02-27 22:57:28 -05:00
|
|
|
if p0 < p1 {
|
2015-03-05 17:00:18 -08:00
|
|
|
p0++
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if pkg == "main" && name != "main" {
|
2015-04-09 07:37:17 -04:00
|
|
|
Exitf("%s: not package main (package %s)", filename, name)
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
loadpkgdata(filename, pkg, data[p0:p1])
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// __.PKGDEF has no cgo section - those are in the C compiler-generated object files.
|
|
|
|
|
if whence == Pkgdef {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// look for cgo section
|
|
|
|
|
p0 = strings.Index(data[p1:], "\n$$ // cgo")
|
|
|
|
|
if p0 >= 0 {
|
|
|
|
|
p0 += p1
|
|
|
|
|
i := strings.IndexByte(data[p0+1:], '\n')
|
|
|
|
|
if i < 0 {
|
|
|
|
|
fmt.Fprintf(os.Stderr, "%s: found $$ // cgo but no newline in %s\n", os.Args[0], filename)
|
|
|
|
|
if Debug['u'] != 0 {
|
2015-04-09 07:37:17 -04:00
|
|
|
errorexit()
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
p0 += 1 + i
|
|
|
|
|
|
|
|
|
|
p1 = strings.Index(data[p0:], "\n$$")
|
|
|
|
|
if p1 < 0 {
|
|
|
|
|
p1 = strings.Index(data[p0:], "\n!\n")
|
|
|
|
|
}
|
|
|
|
|
if p1 < 0 {
|
|
|
|
|
fmt.Fprintf(os.Stderr, "%s: cannot find end of // cgo section in %s\n", os.Args[0], filename)
|
|
|
|
|
if Debug['u'] != 0 {
|
2015-04-09 07:37:17 -04:00
|
|
|
errorexit()
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
p1 += p0
|
|
|
|
|
|
|
|
|
|
loadcgo(filename, pkg, data[p0:p1])
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func loadpkgdata(file string, pkg string, data string) {
|
|
|
|
|
var prefix string
|
|
|
|
|
var name string
|
|
|
|
|
var def string
|
|
|
|
|
|
2015-03-02 12:35:15 -05:00
|
|
|
p := data
|
2015-02-27 22:57:28 -05:00
|
|
|
for parsepkgdata(file, pkg, &p, &prefix, &name, &def) > 0 {
|
2015-03-05 17:45:11 -08:00
|
|
|
x := lookupImport(name)
|
2015-02-27 22:57:28 -05:00
|
|
|
if x.prefix == "" {
|
|
|
|
|
x.prefix = prefix
|
|
|
|
|
x.def = def
|
|
|
|
|
x.file = file
|
|
|
|
|
} else if x.prefix != prefix {
|
|
|
|
|
fmt.Fprintf(os.Stderr, "%s: conflicting definitions for %s\n", os.Args[0], name)
|
|
|
|
|
fmt.Fprintf(os.Stderr, "%s:\t%s %s ...\n", x.file, x.prefix, name)
|
|
|
|
|
fmt.Fprintf(os.Stderr, "%s:\t%s %s ...\n", file, prefix, name)
|
|
|
|
|
nerrors++
|
|
|
|
|
} else if x.def != def {
|
|
|
|
|
fmt.Fprintf(os.Stderr, "%s: conflicting definitions for %s\n", os.Args[0], name)
|
|
|
|
|
fmt.Fprintf(os.Stderr, "%s:\t%s %s %s\n", x.file, x.prefix, name, x.def)
|
|
|
|
|
fmt.Fprintf(os.Stderr, "%s:\t%s %s %s\n", file, prefix, name, def)
|
|
|
|
|
nerrors++
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func parsepkgdata(file string, pkg string, pp *string, prefixp *string, namep *string, defp *string) int {
|
|
|
|
|
// skip white space
|
2015-03-02 12:35:15 -05:00
|
|
|
p := *pp
|
2015-02-27 22:57:28 -05:00
|
|
|
|
|
|
|
|
loop:
|
|
|
|
|
for len(p) > 0 && (p[0] == ' ' || p[0] == '\t' || p[0] == '\n') {
|
|
|
|
|
p = p[1:]
|
|
|
|
|
}
|
|
|
|
|
if len(p) == 0 || strings.HasPrefix(p, "$$\n") {
|
|
|
|
|
return 0
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// prefix: (var|type|func|const)
|
2015-03-05 17:45:11 -08:00
|
|
|
prefix := p
|
2015-02-27 22:57:28 -05:00
|
|
|
|
|
|
|
|
if len(p) < 7 {
|
|
|
|
|
return -1
|
|
|
|
|
}
|
|
|
|
|
if strings.HasPrefix(p, "var ") {
|
|
|
|
|
p = p[4:]
|
|
|
|
|
} else if strings.HasPrefix(p, "type ") {
|
|
|
|
|
p = p[5:]
|
|
|
|
|
} else if strings.HasPrefix(p, "func ") {
|
|
|
|
|
p = p[5:]
|
|
|
|
|
} else if strings.HasPrefix(p, "const ") {
|
|
|
|
|
p = p[6:]
|
|
|
|
|
} else if strings.HasPrefix(p, "import ") {
|
|
|
|
|
p = p[7:]
|
|
|
|
|
for len(p) > 0 && p[0] != ' ' {
|
|
|
|
|
p = p[1:]
|
|
|
|
|
}
|
|
|
|
|
p = p[1:]
|
2015-03-05 17:45:11 -08:00
|
|
|
line := p
|
2015-02-27 22:57:28 -05:00
|
|
|
for len(p) > 0 && p[0] != '\n' {
|
|
|
|
|
p = p[1:]
|
|
|
|
|
}
|
|
|
|
|
if len(p) == 0 {
|
|
|
|
|
fmt.Fprintf(os.Stderr, "%s: %s: confused in import line\n", os.Args[0], file)
|
|
|
|
|
nerrors++
|
|
|
|
|
return -1
|
|
|
|
|
}
|
2015-03-05 17:45:11 -08:00
|
|
|
line = line[:len(line)-len(p)]
|
|
|
|
|
line = strings.TrimSuffix(line, " // indirect")
|
|
|
|
|
path, err := strconv.Unquote(line)
|
|
|
|
|
if err != nil {
|
|
|
|
|
fmt.Fprintf(os.Stderr, "%s: %s: confused in import path: %q\n", os.Args[0], file, line)
|
|
|
|
|
nerrors++
|
|
|
|
|
return -1
|
|
|
|
|
}
|
2015-02-27 22:57:28 -05:00
|
|
|
p = p[1:]
|
2015-03-05 17:45:11 -08:00
|
|
|
imported(pkg, path)
|
2015-02-27 22:57:28 -05:00
|
|
|
goto loop
|
|
|
|
|
} else {
|
|
|
|
|
fmt.Fprintf(os.Stderr, "%s: %s: confused in pkg data near <<%.40s>>\n", os.Args[0], file, prefix)
|
|
|
|
|
nerrors++
|
|
|
|
|
return -1
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
prefix = prefix[:len(prefix)-len(p)-1]
|
|
|
|
|
|
|
|
|
|
// name: a.b followed by space
|
2015-03-02 12:35:15 -05:00
|
|
|
name := p
|
2015-02-27 22:57:28 -05:00
|
|
|
|
2015-03-02 12:35:15 -05:00
|
|
|
inquote := false
|
2015-02-27 22:57:28 -05:00
|
|
|
for len(p) > 0 {
|
|
|
|
|
if p[0] == ' ' && !inquote {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if p[0] == '\\' {
|
|
|
|
|
p = p[1:]
|
|
|
|
|
} else if p[0] == '"' {
|
|
|
|
|
inquote = !inquote
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
p = p[1:]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(p) == 0 {
|
|
|
|
|
return -1
|
|
|
|
|
}
|
|
|
|
|
name = name[:len(name)-len(p)]
|
|
|
|
|
p = p[1:]
|
|
|
|
|
|
|
|
|
|
// def: free form to new line
|
2015-03-02 12:35:15 -05:00
|
|
|
def := p
|
2015-02-27 22:57:28 -05:00
|
|
|
|
|
|
|
|
for len(p) > 0 && p[0] != '\n' {
|
|
|
|
|
p = p[1:]
|
|
|
|
|
}
|
|
|
|
|
if len(p) == 0 {
|
|
|
|
|
return -1
|
|
|
|
|
}
|
|
|
|
|
def = def[:len(def)-len(p)]
|
|
|
|
|
var defbuf *bytes.Buffer
|
|
|
|
|
p = p[1:]
|
|
|
|
|
|
|
|
|
|
// include methods on successive lines in def of named type
|
2015-03-02 12:35:15 -05:00
|
|
|
var meth string
|
2015-02-27 22:57:28 -05:00
|
|
|
for parsemethod(&p, &meth) > 0 {
|
|
|
|
|
if defbuf == nil {
|
|
|
|
|
defbuf = new(bytes.Buffer)
|
|
|
|
|
defbuf.WriteString(def)
|
|
|
|
|
}
|
|
|
|
|
defbuf.WriteString("\n\t")
|
|
|
|
|
defbuf.WriteString(meth)
|
|
|
|
|
}
|
|
|
|
|
if defbuf != nil {
|
|
|
|
|
def = defbuf.String()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
name = expandpkg(name, pkg)
|
|
|
|
|
def = expandpkg(def, pkg)
|
|
|
|
|
|
|
|
|
|
// done
|
|
|
|
|
*pp = p
|
|
|
|
|
|
|
|
|
|
*prefixp = prefix
|
|
|
|
|
*namep = name
|
|
|
|
|
*defp = def
|
|
|
|
|
return 1
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func parsemethod(pp *string, methp *string) int {
|
|
|
|
|
// skip white space
|
2015-03-02 12:35:15 -05:00
|
|
|
p := *pp
|
2015-02-27 22:57:28 -05:00
|
|
|
|
|
|
|
|
for len(p) > 0 && (p[0] == ' ' || p[0] == '\t') {
|
|
|
|
|
p = p[1:]
|
|
|
|
|
}
|
|
|
|
|
if len(p) == 0 {
|
|
|
|
|
return 0
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// might be a comment about the method
|
|
|
|
|
if strings.HasPrefix(p, "//") {
|
|
|
|
|
goto useline
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// if it says "func (", it's a method
|
|
|
|
|
if strings.HasPrefix(p, "func (") {
|
|
|
|
|
goto useline
|
|
|
|
|
}
|
|
|
|
|
return 0
|
|
|
|
|
|
|
|
|
|
// definition to end of line
|
|
|
|
|
useline:
|
|
|
|
|
*methp = p
|
|
|
|
|
|
|
|
|
|
for len(p) > 0 && p[0] != '\n' {
|
|
|
|
|
p = p[1:]
|
|
|
|
|
}
|
|
|
|
|
if len(p) == 0 {
|
|
|
|
|
fmt.Fprintf(os.Stderr, "%s: lost end of line in method definition\n", os.Args[0])
|
|
|
|
|
*pp = ""
|
|
|
|
|
return -1
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
*methp = (*methp)[:len(*methp)-len(p)]
|
|
|
|
|
*pp = p[1:]
|
|
|
|
|
return 1
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func loadcgo(file string, pkg string, p string) {
|
|
|
|
|
var next string
|
|
|
|
|
var q string
|
|
|
|
|
var f []string
|
|
|
|
|
var local string
|
|
|
|
|
var remote string
|
|
|
|
|
var lib string
|
|
|
|
|
var s *LSym
|
|
|
|
|
|
2015-03-02 12:35:15 -05:00
|
|
|
p0 := ""
|
2015-02-27 22:57:28 -05:00
|
|
|
for ; p != ""; p = next {
|
|
|
|
|
if i := strings.Index(p, "\n"); i >= 0 {
|
|
|
|
|
p, next = p[:i], p[i+1:]
|
|
|
|
|
} else {
|
|
|
|
|
next = ""
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
p0 = p // save for error message
|
|
|
|
|
f = tokenize(p)
|
|
|
|
|
if len(f) == 0 {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if f[0] == "cgo_import_dynamic" {
|
|
|
|
|
if len(f) < 2 || len(f) > 4 {
|
|
|
|
|
goto err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
local = f[1]
|
|
|
|
|
remote = local
|
|
|
|
|
if len(f) > 2 {
|
|
|
|
|
remote = f[2]
|
|
|
|
|
}
|
|
|
|
|
lib = ""
|
|
|
|
|
if len(f) > 3 {
|
|
|
|
|
lib = f[3]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if Debug['d'] != 0 {
|
|
|
|
|
fmt.Fprintf(os.Stderr, "%s: %s: cannot use dynamic imports with -d flag\n", os.Args[0], file)
|
|
|
|
|
nerrors++
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if local == "_" && remote == "_" {
|
|
|
|
|
// allow #pragma dynimport _ _ "foo.so"
|
|
|
|
|
// to force a link of foo.so.
|
|
|
|
|
havedynamic = 1
|
|
|
|
|
|
|
|
|
|
Thearch.Adddynlib(lib)
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
local = expandpkg(local, pkg)
|
|
|
|
|
q = ""
|
|
|
|
|
if i := strings.Index(remote, "#"); i >= 0 {
|
|
|
|
|
remote, q = remote[:i], remote[i+1:]
|
|
|
|
|
}
|
|
|
|
|
s = Linklookup(Ctxt, local, 0)
|
|
|
|
|
if local != f[1] {
|
|
|
|
|
}
|
2015-04-19 19:33:58 -07:00
|
|
|
if s.Type == 0 || s.Type == obj.SXREF || s.Type == obj.SHOSTOBJ {
|
2015-02-27 22:57:28 -05:00
|
|
|
s.Dynimplib = lib
|
|
|
|
|
s.Extname = remote
|
|
|
|
|
s.Dynimpvers = q
|
2015-04-19 19:33:58 -07:00
|
|
|
if s.Type != obj.SHOSTOBJ {
|
|
|
|
|
s.Type = obj.SDYNIMPORT
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
havedynamic = 1
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if f[0] == "cgo_import_static" {
|
|
|
|
|
if len(f) != 2 {
|
|
|
|
|
goto err
|
|
|
|
|
}
|
|
|
|
|
local = f[1]
|
|
|
|
|
s = Linklookup(Ctxt, local, 0)
|
2015-04-19 19:33:58 -07:00
|
|
|
s.Type = obj.SHOSTOBJ
|
2015-02-27 22:57:28 -05:00
|
|
|
s.Size = 0
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if f[0] == "cgo_export_static" || f[0] == "cgo_export_dynamic" {
|
|
|
|
|
if len(f) < 2 || len(f) > 3 {
|
|
|
|
|
goto err
|
|
|
|
|
}
|
|
|
|
|
local = f[1]
|
|
|
|
|
if len(f) > 2 {
|
|
|
|
|
remote = f[2]
|
|
|
|
|
} else {
|
|
|
|
|
remote = local
|
|
|
|
|
}
|
|
|
|
|
local = expandpkg(local, pkg)
|
|
|
|
|
s = Linklookup(Ctxt, local, 0)
|
|
|
|
|
|
2015-04-09 10:44:05 -04:00
|
|
|
switch Buildmode {
|
|
|
|
|
case BuildmodeCShared, BuildmodeCArchive:
|
|
|
|
|
if s == Linklookup(Ctxt, "main", 0) {
|
|
|
|
|
continue
|
|
|
|
|
}
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// export overrides import, for openbsd/cgo.
|
|
|
|
|
// see issue 4878.
|
|
|
|
|
if s.Dynimplib != "" {
|
|
|
|
|
s.Dynimplib = ""
|
|
|
|
|
s.Extname = ""
|
|
|
|
|
s.Dynimpvers = ""
|
|
|
|
|
s.Type = 0
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if s.Cgoexport == 0 {
|
|
|
|
|
s.Extname = remote
|
|
|
|
|
dynexp = append(dynexp, s)
|
|
|
|
|
} else if s.Extname != remote {
|
|
|
|
|
fmt.Fprintf(os.Stderr, "%s: conflicting cgo_export directives: %s as %s and %s\n", os.Args[0], s.Name, s.Extname, remote)
|
|
|
|
|
nerrors++
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if f[0] == "cgo_export_static" {
|
|
|
|
|
s.Cgoexport |= CgoExportStatic
|
|
|
|
|
} else {
|
|
|
|
|
s.Cgoexport |= CgoExportDynamic
|
|
|
|
|
}
|
|
|
|
|
if local != f[1] {
|
|
|
|
|
}
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if f[0] == "cgo_dynamic_linker" {
|
|
|
|
|
if len(f) != 2 {
|
|
|
|
|
goto err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if Debug['I'] == 0 {
|
|
|
|
|
if interpreter != "" && interpreter != f[1] {
|
|
|
|
|
fmt.Fprintf(os.Stderr, "%s: conflict dynlinker: %s and %s\n", os.Args[0], interpreter, f[1])
|
|
|
|
|
nerrors++
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interpreter = f[1]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if f[0] == "cgo_ldflag" {
|
|
|
|
|
if len(f) != 2 {
|
|
|
|
|
goto err
|
|
|
|
|
}
|
|
|
|
|
ldflag = append(ldflag, f[1])
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
err:
|
|
|
|
|
fmt.Fprintf(os.Stderr, "%s: %s: invalid dynimport line: %s\n", os.Args[0], file, p0)
|
|
|
|
|
nerrors++
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var markq *LSym
|
|
|
|
|
|
|
|
|
|
var emarkq *LSym
|
|
|
|
|
|
|
|
|
|
func mark1(s *LSym, parent *LSym) {
|
|
|
|
|
if s == nil || s.Reachable {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if strings.HasPrefix(s.Name, "go.weak.") {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
s.Reachable = true
|
|
|
|
|
s.Reachparent = parent
|
|
|
|
|
if markq == nil {
|
|
|
|
|
markq = s
|
|
|
|
|
} else {
|
|
|
|
|
emarkq.Queue = s
|
|
|
|
|
}
|
|
|
|
|
emarkq = s
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func mark(s *LSym) {
|
|
|
|
|
mark1(s, nil)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func markflood() {
|
|
|
|
|
var a *Auto
|
|
|
|
|
var i int
|
|
|
|
|
|
2015-03-02 12:35:15 -05:00
|
|
|
for s := markq; s != nil; s = s.Queue {
|
2015-04-19 19:33:58 -07:00
|
|
|
if s.Type == obj.STEXT {
|
2015-02-27 22:57:28 -05:00
|
|
|
if Debug['v'] > 1 {
|
|
|
|
|
fmt.Fprintf(&Bso, "marktext %s\n", s.Name)
|
|
|
|
|
}
|
|
|
|
|
for a = s.Autom; a != nil; a = a.Link {
|
|
|
|
|
mark1(a.Gotype, s)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for i = 0; i < len(s.R); i++ {
|
|
|
|
|
mark1(s.R[i].Sym, s)
|
|
|
|
|
}
|
|
|
|
|
if s.Pcln != nil {
|
|
|
|
|
for i = 0; i < s.Pcln.Nfuncdata; i++ {
|
|
|
|
|
mark1(s.Pcln.Funcdata[i], s)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mark1(s.Gotype, s)
|
|
|
|
|
mark1(s.Sub, s)
|
|
|
|
|
mark1(s.Outer, s)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var markextra = []string{
|
|
|
|
|
"runtime.morestack",
|
|
|
|
|
"runtime.morestackx",
|
|
|
|
|
"runtime.morestack00",
|
|
|
|
|
"runtime.morestack10",
|
|
|
|
|
"runtime.morestack01",
|
|
|
|
|
"runtime.morestack11",
|
|
|
|
|
"runtime.morestack8",
|
|
|
|
|
"runtime.morestack16",
|
|
|
|
|
"runtime.morestack24",
|
|
|
|
|
"runtime.morestack32",
|
|
|
|
|
"runtime.morestack40",
|
|
|
|
|
"runtime.morestack48",
|
|
|
|
|
// on arm, lock in the div/mod helpers too
|
|
|
|
|
"_div",
|
|
|
|
|
"_divu",
|
|
|
|
|
"_mod",
|
|
|
|
|
"_modu",
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func deadcode() {
|
|
|
|
|
if Debug['v'] != 0 {
|
|
|
|
|
fmt.Fprintf(&Bso, "%5.2f deadcode\n", obj.Cputime())
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-16 15:53:05 -04:00
|
|
|
if Buildmode == BuildmodeShared {
|
2015-03-30 02:59:10 +00:00
|
|
|
// Mark all symbols as reachable when building a
|
|
|
|
|
// shared library.
|
|
|
|
|
for s := Ctxt.Allsym; s != nil; s = s.Allsym {
|
|
|
|
|
if s.Type != 0 {
|
|
|
|
|
mark(s)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
mark(Linkrlookup(Ctxt, "main.main", 0))
|
|
|
|
|
mark(Linkrlookup(Ctxt, "main.init", 0))
|
|
|
|
|
} else {
|
|
|
|
|
mark(Linklookup(Ctxt, INITENTRY, 0))
|
2015-04-01 14:57:34 +13:00
|
|
|
if Linkshared && Buildmode == BuildmodeExe {
|
|
|
|
|
mark(Linkrlookup(Ctxt, "main.main", 0))
|
|
|
|
|
mark(Linkrlookup(Ctxt, "main.init", 0))
|
|
|
|
|
}
|
2015-03-30 02:59:10 +00:00
|
|
|
for i := 0; i < len(markextra); i++ {
|
|
|
|
|
mark(Linklookup(Ctxt, markextra[i], 0))
|
|
|
|
|
}
|
2015-02-27 22:57:28 -05:00
|
|
|
|
2015-03-30 02:59:10 +00:00
|
|
|
for i := 0; i < len(dynexp); i++ {
|
|
|
|
|
mark(dynexp[i])
|
|
|
|
|
}
|
|
|
|
|
markflood()
|
2015-02-27 22:57:28 -05:00
|
|
|
|
2015-03-30 02:59:10 +00:00
|
|
|
// keep each beginning with 'typelink.' if the symbol it points at is being kept.
|
|
|
|
|
for s := Ctxt.Allsym; s != nil; s = s.Allsym {
|
|
|
|
|
if strings.HasPrefix(s.Name, "go.typelink.") {
|
|
|
|
|
s.Reachable = len(s.R) == 1 && s.R[0].Sym.Reachable
|
|
|
|
|
}
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
2015-03-30 02:59:10 +00:00
|
|
|
// remove dead text but keep file information (z symbols).
|
|
|
|
|
var last *LSym
|
2015-02-27 22:57:28 -05:00
|
|
|
|
2015-03-30 02:59:10 +00:00
|
|
|
for s := Ctxt.Textp; s != nil; s = s.Next {
|
|
|
|
|
if !s.Reachable {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NOTE: Removing s from old textp and adding to new, shorter textp.
|
|
|
|
|
if last == nil {
|
|
|
|
|
Ctxt.Textp = s
|
|
|
|
|
} else {
|
|
|
|
|
last.Next = s
|
|
|
|
|
}
|
|
|
|
|
last = s
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if last == nil {
|
2015-03-30 02:59:10 +00:00
|
|
|
Ctxt.Textp = nil
|
|
|
|
|
Ctxt.Etextp = nil
|
2015-02-27 22:57:28 -05:00
|
|
|
} else {
|
2015-03-30 02:59:10 +00:00
|
|
|
last.Next = nil
|
|
|
|
|
Ctxt.Etextp = last
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-02 12:35:15 -05:00
|
|
|
for s := Ctxt.Allsym; s != nil; s = s.Allsym {
|
2015-02-27 22:57:28 -05:00
|
|
|
if strings.HasPrefix(s.Name, "go.weak.") {
|
|
|
|
|
s.Special = 1 // do not lay out in data segment
|
|
|
|
|
s.Reachable = true
|
|
|
|
|
s.Hide = 1
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// record field tracking references
|
2015-03-03 22:20:16 -05:00
|
|
|
var buf bytes.Buffer
|
2015-03-02 12:35:15 -05:00
|
|
|
var p *LSym
|
|
|
|
|
for s := Ctxt.Allsym; s != nil; s = s.Allsym {
|
2015-02-27 22:57:28 -05:00
|
|
|
if strings.HasPrefix(s.Name, "go.track.") {
|
|
|
|
|
s.Special = 1 // do not lay out in data segment
|
|
|
|
|
s.Hide = 1
|
|
|
|
|
if s.Reachable {
|
2015-03-03 22:20:16 -05:00
|
|
|
buf.WriteString(s.Name[9:])
|
2015-02-27 22:57:28 -05:00
|
|
|
for p = s.Reachparent; p != nil; p = p.Reachparent {
|
2015-03-03 22:20:16 -05:00
|
|
|
buf.WriteString("\t")
|
|
|
|
|
buf.WriteString(p.Name)
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
2015-03-03 22:20:16 -05:00
|
|
|
buf.WriteString("\n")
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
2015-04-19 19:33:58 -07:00
|
|
|
s.Type = obj.SCONST
|
2015-02-27 22:57:28 -05:00
|
|
|
s.Value = 0
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if tracksym == "" {
|
|
|
|
|
return
|
|
|
|
|
}
|
2015-03-02 12:35:15 -05:00
|
|
|
s := Linklookup(Ctxt, tracksym, 0)
|
2015-02-27 22:57:28 -05:00
|
|
|
if !s.Reachable {
|
|
|
|
|
return
|
|
|
|
|
}
|
2015-03-03 22:20:16 -05:00
|
|
|
addstrdata(tracksym, buf.String())
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func doweak() {
|
|
|
|
|
var t *LSym
|
|
|
|
|
|
|
|
|
|
// resolve weak references only if
|
|
|
|
|
// target symbol will be in binary anyway.
|
2015-03-02 12:35:15 -05:00
|
|
|
for s := Ctxt.Allsym; s != nil; s = s.Allsym {
|
2015-02-27 22:57:28 -05:00
|
|
|
if strings.HasPrefix(s.Name, "go.weak.") {
|
|
|
|
|
t = Linkrlookup(Ctxt, s.Name[8:], int(s.Version))
|
|
|
|
|
if t != nil && t.Type != 0 && t.Reachable {
|
|
|
|
|
s.Value = t.Value
|
|
|
|
|
s.Type = t.Type
|
|
|
|
|
s.Outer = t
|
|
|
|
|
} else {
|
2015-04-19 19:33:58 -07:00
|
|
|
s.Type = obj.SCONST
|
2015-02-27 22:57:28 -05:00
|
|
|
s.Value = 0
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func addexport() {
|
2015-04-19 19:33:58 -07:00
|
|
|
if HEADTYPE == obj.Hdarwin {
|
2015-02-27 22:57:28 -05:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-02 12:35:15 -05:00
|
|
|
for i := 0; i < len(dynexp); i++ {
|
2015-02-27 22:57:28 -05:00
|
|
|
Thearch.Adddynsym(Ctxt, dynexp[i])
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type Pkg struct {
|
2015-03-05 17:45:11 -08:00
|
|
|
mark bool
|
|
|
|
|
checked bool
|
|
|
|
|
path string
|
2015-02-27 22:57:28 -05:00
|
|
|
impby []*Pkg
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-05 17:45:11 -08:00
|
|
|
var (
|
|
|
|
|
// pkgmap records the imported-by relationship between packages.
|
|
|
|
|
// Entries are keyed by package path (e.g., "runtime" or "net/url").
|
|
|
|
|
pkgmap = map[string]*Pkg{}
|
2015-02-27 22:57:28 -05:00
|
|
|
|
2015-03-05 17:45:11 -08:00
|
|
|
pkgall []*Pkg
|
|
|
|
|
)
|
2015-02-27 22:57:28 -05:00
|
|
|
|
2015-03-05 17:45:11 -08:00
|
|
|
func lookupPkg(path string) *Pkg {
|
|
|
|
|
if p, ok := pkgmap[path]; ok {
|
|
|
|
|
return p
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
2015-03-05 17:45:11 -08:00
|
|
|
p := &Pkg{path: path}
|
|
|
|
|
pkgmap[path] = p
|
|
|
|
|
pkgall = append(pkgall, p)
|
2015-02-27 22:57:28 -05:00
|
|
|
return p
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-05 17:45:11 -08:00
|
|
|
// imported records that package pkg imports package imp.
|
|
|
|
|
func imported(pkg, imp string) {
|
2015-02-27 22:57:28 -05:00
|
|
|
// everyone imports runtime, even runtime.
|
2015-03-05 17:45:11 -08:00
|
|
|
if imp == "runtime" {
|
2015-02-27 22:57:28 -05:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-05 17:45:11 -08:00
|
|
|
p := lookupPkg(pkg)
|
|
|
|
|
i := lookupPkg(imp)
|
2015-02-27 22:57:28 -05:00
|
|
|
i.impby = append(i.impby, p)
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-05 17:45:11 -08:00
|
|
|
func (p *Pkg) cycle() *Pkg {
|
|
|
|
|
if p.checked {
|
2015-02-27 22:57:28 -05:00
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-05 17:45:11 -08:00
|
|
|
if p.mark {
|
2015-02-27 22:57:28 -05:00
|
|
|
nerrors++
|
|
|
|
|
fmt.Printf("import cycle:\n")
|
2015-03-05 17:45:11 -08:00
|
|
|
fmt.Printf("\t%s\n", p.path)
|
2015-02-27 22:57:28 -05:00
|
|
|
return p
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-05 17:45:11 -08:00
|
|
|
p.mark = true
|
|
|
|
|
for _, q := range p.impby {
|
|
|
|
|
if bad := q.cycle(); bad != nil {
|
|
|
|
|
p.mark = false
|
|
|
|
|
p.checked = true
|
|
|
|
|
fmt.Printf("\timports %s\n", p.path)
|
2015-02-27 22:57:28 -05:00
|
|
|
if bad == p {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
return bad
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-05 17:45:11 -08:00
|
|
|
p.checked = true
|
|
|
|
|
p.mark = false
|
2015-02-27 22:57:28 -05:00
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func importcycles() {
|
2015-03-05 17:45:11 -08:00
|
|
|
for _, p := range pkgall {
|
|
|
|
|
p.cycle()
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func setlinkmode(arg string) {
|
|
|
|
|
if arg == "internal" {
|
|
|
|
|
Linkmode = LinkInternal
|
|
|
|
|
} else if arg == "external" {
|
|
|
|
|
Linkmode = LinkExternal
|
|
|
|
|
} else if arg == "auto" {
|
|
|
|
|
Linkmode = LinkAuto
|
|
|
|
|
} else {
|
2015-04-09 07:37:17 -04:00
|
|
|
Exitf("unknown link mode -linkmode %s", arg)
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
}
|