github.com/rsc/tmp@v0.0.0-20240517235954-6deaab19748b/bootstrap/internal/ld/ld.go (about)

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