github.com/dannin/go@v0.0.0-20161031215817-d35dfd405eaa/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 "path/filepath" 40 "strconv" 41 "strings" 42 ) 43 44 func addlib(ctxt *Link, src string, obj string, pathname string) *Library { 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 ctxt.Library[i] 57 } 58 } 59 60 var pname string 61 isshlib := false 62 if filepath.IsAbs(name) { 63 pname = name 64 } else { 65 // try dot, -L "libdir", and then goroot. 66 for _, dir := range ctxt.Libdir { 67 if *FlagLinkshared { 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 { 84 ctxt.Logf("%5.2f addlib: %s %s pulls in %s isshlib %v\n", elapsed(), obj, src, pname, isshlib) 85 } 86 87 if isshlib { 88 return addlibpath(ctxt, src, obj, "", pkg, pname) 89 } 90 return addlibpath(ctxt, src, obj, pname, pkg, "") 91 } 92 93 /* 94 * add library to library list, return added library. 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) *Library { 101 for i := 0; i < len(ctxt.Library); i++ { 102 if pkg == ctxt.Library[i].Pkg { 103 return ctxt.Library[i] 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 Errorf(nil, "cannot read %s: %v", shlibnamefile, err) 121 } 122 l.Shlib = strings.TrimSpace(string(shlibbytes)) 123 } 124 return l 125 } 126 127 func atolwhex(s string) int64 { 128 n, _ := strconv.ParseInt(s, 0, 64) 129 return n 130 }