github.com/q45/go@v0.0.0-20151101211701-a4fb8c13db3f/src/cmd/link/internal/ld/ld.go (about)

     1  // Derived from Inferno utils/6l/obj.c and utils/6l/span.c
     2  // http://code.google.com/p/inferno-os/source/browse/utils/6l/obj.c
     3  // http://code.google.com/p/inferno-os/source/browse/utils/6l/span.c
     4  //
     5  //	Copyright © 1994-1999 Lucent Technologies Inc.  All rights reserved.
     6  //	Portions Copyright © 1995-1997 C H Forsyth (forsyth@terzarima.net)
     7  //	Portions Copyright © 1997-1999 Vita Nuova Limited
     8  //	Portions Copyright © 2000-2007 Vita Nuova Holdings Limited (www.vitanuova.com)
     9  //	Portions Copyright © 2004,2006 Bruce Ellis
    10  //	Portions Copyright © 2005-2007 C H Forsyth (forsyth@terzarima.net)
    11  //	Revisions Copyright © 2000-2007 Lucent Technologies Inc. and others
    12  //	Portions Copyright © 2009 The Go Authors.  All rights reserved.
    13  //
    14  // Permission is hereby granted, free of charge, to any person obtaining a copy
    15  // of this software and associated documentation files (the "Software"), to deal
    16  // in the Software without restriction, including without limitation the rights
    17  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    18  // copies of the Software, and to permit persons to whom the Software is
    19  // furnished to do so, subject to the following conditions:
    20  //
    21  // The above copyright notice and this permission notice shall be included in
    22  // all copies or substantial portions of the Software.
    23  //
    24  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    25  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    26  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
    27  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    28  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    29  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    30  // THE SOFTWARE.
    31  
    32  package ld
    33  
    34  import (
    35  	"cmd/internal/obj"
    36  	"fmt"
    37  	"io/ioutil"
    38  	"os"
    39  	"path"
    40  	"strconv"
    41  	"strings"
    42  )
    43  
    44  func addlib(ctxt *Link, src string, obj string, pathname string) {
    45  	name := path.Clean(pathname)
    46  
    47  	// runtime.a -> runtime, runtime.6 -> runtime
    48  	pkg := name
    49  	if len(pkg) >= 2 && pkg[len(pkg)-2] == '.' {
    50  		pkg = pkg[:len(pkg)-2]
    51  	}
    52  
    53  	// already loaded?
    54  	for i := 0; i < len(ctxt.Library); i++ {
    55  		if ctxt.Library[i].Pkg == pkg {
    56  			return
    57  		}
    58  	}
    59  
    60  	var pname string
    61  	isshlib := false
    62  	if (ctxt.Windows == 0 && strings.HasPrefix(name, "/")) || (ctxt.Windows != 0 && len(name) >= 2 && name[1] == ':') {
    63  		pname = name
    64  	} else {
    65  		// try dot, -L "libdir", and then goroot.
    66  		for _, dir := range ctxt.Libdir {
    67  			if Linkshared {
    68  				pname = dir + "/" + pkg + ".shlibname"
    69  				if _, err := os.Stat(pname); err == nil {
    70  					isshlib = true
    71  					break
    72  				}
    73  			}
    74  			pname = dir + "/" + name
    75  			if _, err := os.Stat(pname); err == nil {
    76  				break
    77  			}
    78  		}
    79  	}
    80  
    81  	pname = path.Clean(pname)
    82  
    83  	if ctxt.Debugvlog > 1 && ctxt.Bso != nil {
    84  		fmt.Fprintf(ctxt.Bso, "%5.2f addlib: %s %s pulls in %s isshlib %v\n", elapsed(), obj, src, pname, isshlib)
    85  	}
    86  
    87  	if isshlib {
    88  		addlibpath(ctxt, src, obj, "", pkg, pname)
    89  	} else {
    90  		addlibpath(ctxt, src, obj, pname, pkg, "")
    91  	}
    92  }
    93  
    94  /*
    95   * add library to library list.
    96   *	srcref: src file referring to package
    97   *	objref: object file referring to package
    98   *	file: object file, e.g., /home/rsc/go/pkg/container/vector.a
    99   *	pkg: package import path, e.g. container/vector
   100   */
   101  func addlibpath(ctxt *Link, srcref string, objref string, file string, pkg string, shlibnamefile string) {
   102  	for i := 0; i < len(ctxt.Library); i++ {
   103  		if pkg == ctxt.Library[i].Pkg {
   104  			return
   105  		}
   106  	}
   107  
   108  	if ctxt.Debugvlog > 1 && ctxt.Bso != nil {
   109  		fmt.Fprintf(ctxt.Bso, "%5.2f addlibpath: srcref: %s objref: %s file: %s pkg: %s shlibnamefile: %s\n", obj.Cputime(), srcref, objref, file, pkg, shlibnamefile)
   110  	}
   111  
   112  	ctxt.Library = append(ctxt.Library, &Library{})
   113  	l := ctxt.Library[len(ctxt.Library)-1]
   114  	l.Objref = objref
   115  	l.Srcref = srcref
   116  	l.File = file
   117  	l.Pkg = pkg
   118  	if shlibnamefile != "" {
   119  		shlibbytes, err := ioutil.ReadFile(shlibnamefile)
   120  		if err != nil {
   121  			Diag("cannot read %s: %v", shlibnamefile, err)
   122  		}
   123  		l.Shlib = strings.TrimSpace(string(shlibbytes))
   124  	}
   125  }
   126  
   127  func atolwhex(s string) int64 {
   128  	n, _ := strconv.ParseInt(s, 0, 64)
   129  	return n
   130  }