github.com/sanprasirt/go@v0.0.0-20170607001320-a027466e4b6d/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  	"io/ioutil"
    36  	"log"
    37  	"os"
    38  	"path"
    39  	"path/filepath"
    40  	"strconv"
    41  	"strings"
    42  )
    43  
    44  func (ctxt *Link) readImportCfg(file string) {
    45  	ctxt.PackageFile = make(map[string]string)
    46  	ctxt.PackageShlib = make(map[string]string)
    47  	data, err := ioutil.ReadFile(file)
    48  	if err != nil {
    49  		log.Fatalf("-importcfg: %v", err)
    50  	}
    51  
    52  	for lineNum, line := range strings.Split(string(data), "\n") {
    53  		lineNum++ // 1-based
    54  		line = strings.TrimSpace(line)
    55  		if line == "" {
    56  			continue
    57  		}
    58  		if line == "" || strings.HasPrefix(line, "#") {
    59  			continue
    60  		}
    61  
    62  		var verb, args string
    63  		if i := strings.Index(line, " "); i < 0 {
    64  			verb = line
    65  		} else {
    66  			verb, args = line[:i], strings.TrimSpace(line[i+1:])
    67  		}
    68  		var before, after string
    69  		if i := strings.Index(args, "="); i >= 0 {
    70  			before, after = args[:i], args[i+1:]
    71  		}
    72  		switch verb {
    73  		default:
    74  			log.Fatalf("%s:%d: unknown directive %q", file, lineNum, verb)
    75  		case "packagefile":
    76  			if before == "" || after == "" {
    77  				log.Fatalf(`%s:%d: invalid packagefile: syntax is "packagefile path=filename"`, file, lineNum)
    78  			}
    79  			ctxt.PackageFile[before] = after
    80  		case "packageshlib":
    81  			if before == "" || after == "" {
    82  				log.Fatalf(`%s:%d: invalid packageshlib: syntax is "packageshlib path=filename"`, file, lineNum)
    83  			}
    84  			ctxt.PackageShlib[before] = after
    85  		}
    86  	}
    87  }
    88  
    89  func addlib(ctxt *Link, src string, obj string, pathname string) *Library {
    90  	name := path.Clean(pathname)
    91  
    92  	// runtime.a -> runtime, runtime.6 -> runtime
    93  	pkg := name
    94  	if len(pkg) >= 2 && pkg[len(pkg)-2] == '.' {
    95  		pkg = pkg[:len(pkg)-2]
    96  	}
    97  
    98  	// already loaded?
    99  	if l := ctxt.LibraryByPkg[pkg]; l != nil {
   100  		return l
   101  	}
   102  
   103  	var pname string
   104  	isshlib := false
   105  
   106  	if *FlagLinkshared && ctxt.PackageShlib[name] != "" {
   107  		pname = ctxt.PackageShlib[name]
   108  		isshlib = true
   109  	} else if ctxt.PackageFile != nil {
   110  		pname = ctxt.PackageFile[name]
   111  		if pname == "" {
   112  			ctxt.Logf("cannot find package %s (using -importcfg)\n", name)
   113  			return nil
   114  		}
   115  	} else {
   116  		if filepath.IsAbs(name) {
   117  			pname = name
   118  		} else {
   119  			// try dot, -L "libdir", and then goroot.
   120  			for _, dir := range ctxt.Libdir {
   121  				if *FlagLinkshared {
   122  					pname = dir + "/" + pkg + ".shlibname"
   123  					if _, err := os.Stat(pname); err == nil {
   124  						isshlib = true
   125  						break
   126  					}
   127  				}
   128  				pname = dir + "/" + name
   129  				if _, err := os.Stat(pname); err == nil {
   130  					break
   131  				}
   132  			}
   133  		}
   134  		pname = path.Clean(pname)
   135  	}
   136  
   137  	if ctxt.Debugvlog > 1 {
   138  		ctxt.Logf("%5.2f addlib: %s %s pulls in %s isshlib %v\n", elapsed(), obj, src, pname, isshlib)
   139  	}
   140  
   141  	if isshlib {
   142  		return addlibpath(ctxt, src, obj, "", pkg, pname)
   143  	}
   144  	return addlibpath(ctxt, src, obj, pname, pkg, "")
   145  }
   146  
   147  /*
   148   * add library to library list, return added library.
   149   *	srcref: src file referring to package
   150   *	objref: object file referring to package
   151   *	file: object file, e.g., /home/rsc/go/pkg/container/vector.a
   152   *	pkg: package import path, e.g. container/vector
   153   */
   154  func addlibpath(ctxt *Link, srcref string, objref string, file string, pkg string, shlibnamefile string) *Library {
   155  	if l := ctxt.LibraryByPkg[pkg]; l != nil {
   156  		return l
   157  	}
   158  
   159  	if ctxt.Debugvlog > 1 {
   160  		ctxt.Logf("%5.2f addlibpath: srcref: %s objref: %s file: %s pkg: %s shlibnamefile: %s\n", Cputime(), srcref, objref, file, pkg, shlibnamefile)
   161  	}
   162  
   163  	l := &Library{}
   164  	ctxt.LibraryByPkg[pkg] = l
   165  	ctxt.Library = append(ctxt.Library, l)
   166  	l.Objref = objref
   167  	l.Srcref = srcref
   168  	l.File = file
   169  	l.Pkg = pkg
   170  	if shlibnamefile != "" {
   171  		shlibbytes, err := ioutil.ReadFile(shlibnamefile)
   172  		if err != nil {
   173  			Errorf(nil, "cannot read %s: %v", shlibnamefile, err)
   174  		}
   175  		l.Shlib = strings.TrimSpace(string(shlibbytes))
   176  	}
   177  	return l
   178  }
   179  
   180  func atolwhex(s string) int64 {
   181  	n, _ := strconv.ParseInt(s, 0, 64)
   182  	return n
   183  }