github.com/Filosottile/go@v0.0.0-20170906193555-dbed9972d994/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 pkgname(lib string) string { 90 name := path.Clean(lib) 91 // runtime.a -> runtime, runtime.6 -> runtime 92 pkg := name 93 if len(pkg) >= 2 && pkg[len(pkg)-2] == '.' { 94 pkg = pkg[:len(pkg)-2] 95 } 96 return pkg 97 } 98 99 func findlib(ctxt *Link, lib string) (string, bool) { 100 name := path.Clean(lib) 101 102 var pname string 103 isshlib := false 104 105 if *FlagLinkshared && ctxt.PackageShlib[name] != "" { 106 pname = ctxt.PackageShlib[name] 107 isshlib = true 108 } else if ctxt.PackageFile != nil { 109 pname = ctxt.PackageFile[name] 110 if pname == "" { 111 ctxt.Logf("cannot find package %s (using -importcfg)\n", name) 112 return "", false 113 } 114 } else { 115 if filepath.IsAbs(name) { 116 pname = name 117 } else { 118 pkg := pkgname(lib) 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 return pname, isshlib 138 } 139 140 func addlib(ctxt *Link, src string, obj string, lib string) *Library { 141 pkg := pkgname(lib) 142 143 // already loaded? 144 if l := ctxt.LibraryByPkg[pkg]; l != nil { 145 return l 146 } 147 148 pname, isshlib := findlib(ctxt, lib) 149 150 if ctxt.Debugvlog > 1 { 151 ctxt.Logf("%5.2f addlib: %s %s pulls in %s isshlib %v\n", elapsed(), obj, src, pname, isshlib) 152 } 153 154 if isshlib { 155 return addlibpath(ctxt, src, obj, "", pkg, pname) 156 } 157 return addlibpath(ctxt, src, obj, pname, pkg, "") 158 } 159 160 /* 161 * add library to library list, return added library. 162 * srcref: src file referring to package 163 * objref: object file referring to package 164 * file: object file, e.g., /home/rsc/go/pkg/container/vector.a 165 * pkg: package import path, e.g. container/vector 166 */ 167 func addlibpath(ctxt *Link, srcref string, objref string, file string, pkg string, shlibnamefile string) *Library { 168 if l := ctxt.LibraryByPkg[pkg]; l != nil { 169 return l 170 } 171 172 if ctxt.Debugvlog > 1 { 173 ctxt.Logf("%5.2f addlibpath: srcref: %s objref: %s file: %s pkg: %s shlibnamefile: %s\n", Cputime(), srcref, objref, file, pkg, shlibnamefile) 174 } 175 176 l := &Library{} 177 ctxt.LibraryByPkg[pkg] = l 178 ctxt.Library = append(ctxt.Library, l) 179 l.Objref = objref 180 l.Srcref = srcref 181 l.File = file 182 l.Pkg = pkg 183 if shlibnamefile != "" { 184 shlibbytes, err := ioutil.ReadFile(shlibnamefile) 185 if err != nil { 186 Errorf(nil, "cannot read %s: %v", shlibnamefile, err) 187 } 188 l.Shlib = strings.TrimSpace(string(shlibbytes)) 189 } 190 return l 191 } 192 193 func atolwhex(s string) int64 { 194 n, _ := strconv.ParseInt(s, 0, 64) 195 return n 196 }