github.com/euank/go@v0.0.0-20160829210321-495514729181/src/cmd/link/internal/ld/ld.go (about)

     1  // Derived from Inferno utils/6l/obj.c and utils/6l/span.c
     2  // https://bitbucket.org/inferno-os/inferno-os/src/default/utils/6l/obj.c
     3  // https://bitbucket.org/inferno-os/inferno-os/src/default/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  	"io/ioutil"
    37  	"os"
    38  	"path"
    39  	"strconv"
    40  	"strings"
    41  )
    42  
    43  func addlib(ctxt *Link, src string, obj string, pathname string) {
    44  	name := path.Clean(pathname)
    45  
    46  	// runtime.a -> runtime, runtime.6 -> runtime
    47  	pkg := name
    48  	if len(pkg) >= 2 && pkg[len(pkg)-2] == '.' {
    49  		pkg = pkg[:len(pkg)-2]
    50  	}
    51  
    52  	// already loaded?
    53  	for i := 0; i < len(ctxt.Library); i++ {
    54  		if ctxt.Library[i].Pkg == pkg {
    55  			return
    56  		}
    57  	}
    58  
    59  	var pname string
    60  	isshlib := false
    61  	if (ctxt.Windows == 0 && strings.HasPrefix(name, "/")) || (ctxt.Windows != 0 && len(name) >= 2 && name[1] == ':') {
    62  		pname = name
    63  	} else {
    64  		// try dot, -L "libdir", and then goroot.
    65  		for _, dir := range ctxt.Libdir {
    66  			if *FlagLinkshared {
    67  				pname = dir + "/" + pkg + ".shlibname"
    68  				if _, err := os.Stat(pname); err == nil {
    69  					isshlib = true
    70  					break
    71  				}
    72  			}
    73  			pname = dir + "/" + name
    74  			if _, err := os.Stat(pname); err == nil {
    75  				break
    76  			}
    77  		}
    78  	}
    79  
    80  	pname = path.Clean(pname)
    81  
    82  	if ctxt.Debugvlog > 1 {
    83  		ctxt.Logf("%5.2f addlib: %s %s pulls in %s isshlib %v\n", elapsed(), obj, src, pname, isshlib)
    84  	}
    85  
    86  	if isshlib {
    87  		addlibpath(ctxt, src, obj, "", pkg, pname)
    88  	} else {
    89  		addlibpath(ctxt, src, obj, pname, pkg, "")
    90  	}
    91  }
    92  
    93  /*
    94   * add library to library list.
    95   *	srcref: src file referring to package
    96   *	objref: object file referring to package
    97   *	file: object file, e.g., /home/rsc/go/pkg/container/vector.a
    98   *	pkg: package import path, e.g. container/vector
    99   */
   100  func addlibpath(ctxt *Link, srcref string, objref string, file string, pkg string, shlibnamefile string) {
   101  	for i := 0; i < len(ctxt.Library); i++ {
   102  		if pkg == ctxt.Library[i].Pkg {
   103  			return
   104  		}
   105  	}
   106  
   107  	if ctxt.Debugvlog > 1 {
   108  		ctxt.Logf("%5.2f addlibpath: srcref: %s objref: %s file: %s pkg: %s shlibnamefile: %s\n", obj.Cputime(), srcref, objref, file, pkg, shlibnamefile)
   109  	}
   110  
   111  	ctxt.Library = append(ctxt.Library, &Library{})
   112  	l := ctxt.Library[len(ctxt.Library)-1]
   113  	l.Objref = objref
   114  	l.Srcref = srcref
   115  	l.File = file
   116  	l.Pkg = pkg
   117  	if shlibnamefile != "" {
   118  		shlibbytes, err := ioutil.ReadFile(shlibnamefile)
   119  		if err != nil {
   120  			ctxt.Diag("cannot read %s: %v", shlibnamefile, err)
   121  		}
   122  		l.Shlib = strings.TrimSpace(string(shlibbytes))
   123  	}
   124  }
   125  
   126  func atolwhex(s string) int64 {
   127  	n, _ := strconv.ParseInt(s, 0, 64)
   128  	return n
   129  }