github.com/goproxy0/go@v0.0.0-20171111080102-49cc0c489d2c/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/link/internal/sym" 36 "io/ioutil" 37 "log" 38 "os" 39 "path" 40 "path/filepath" 41 "strconv" 42 "strings" 43 ) 44 45 func (ctxt *Link) readImportCfg(file string) { 46 ctxt.PackageFile = make(map[string]string) 47 ctxt.PackageShlib = make(map[string]string) 48 data, err := ioutil.ReadFile(file) 49 if err != nil { 50 log.Fatalf("-importcfg: %v", err) 51 } 52 53 for lineNum, line := range strings.Split(string(data), "\n") { 54 lineNum++ // 1-based 55 line = strings.TrimSpace(line) 56 if line == "" { 57 continue 58 } 59 if line == "" || strings.HasPrefix(line, "#") { 60 continue 61 } 62 63 var verb, args string 64 if i := strings.Index(line, " "); i < 0 { 65 verb = line 66 } else { 67 verb, args = line[:i], strings.TrimSpace(line[i+1:]) 68 } 69 var before, after string 70 if i := strings.Index(args, "="); i >= 0 { 71 before, after = args[:i], args[i+1:] 72 } 73 switch verb { 74 default: 75 log.Fatalf("%s:%d: unknown directive %q", file, lineNum, verb) 76 case "packagefile": 77 if before == "" || after == "" { 78 log.Fatalf(`%s:%d: invalid packagefile: syntax is "packagefile path=filename"`, file, lineNum) 79 } 80 ctxt.PackageFile[before] = after 81 case "packageshlib": 82 if before == "" || after == "" { 83 log.Fatalf(`%s:%d: invalid packageshlib: syntax is "packageshlib path=filename"`, file, lineNum) 84 } 85 ctxt.PackageShlib[before] = after 86 } 87 } 88 } 89 90 func pkgname(lib string) string { 91 name := path.Clean(lib) 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 return pkg 98 } 99 100 func findlib(ctxt *Link, lib string) (string, bool) { 101 name := path.Clean(lib) 102 103 var pname string 104 isshlib := false 105 106 if ctxt.linkShared && 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 "", false 114 } 115 } else { 116 if filepath.IsAbs(name) { 117 pname = name 118 } else { 119 pkg := pkgname(lib) 120 // Add .a if needed; the new -importcfg modes 121 // do not put .a into the package name anymore. 122 // This only matters when people try to mix 123 // compiles using -importcfg with links not using -importcfg, 124 // such as when running quick things like 125 // 'go tool compile x.go && go tool link x.o' 126 // by hand against a standard library built using -importcfg. 127 if !strings.HasSuffix(name, ".a") && !strings.HasSuffix(name, ".o") { 128 name += ".a" 129 } 130 // try dot, -L "libdir", and then goroot. 131 for _, dir := range ctxt.Libdir { 132 if ctxt.linkShared { 133 pname = dir + "/" + pkg + ".shlibname" 134 if _, err := os.Stat(pname); err == nil { 135 isshlib = true 136 break 137 } 138 } 139 pname = dir + "/" + name 140 if _, err := os.Stat(pname); err == nil { 141 break 142 } 143 } 144 } 145 pname = path.Clean(pname) 146 } 147 148 return pname, isshlib 149 } 150 151 func addlib(ctxt *Link, src string, obj string, lib string) *sym.Library { 152 pkg := pkgname(lib) 153 154 // already loaded? 155 if l := ctxt.LibraryByPkg[pkg]; l != nil { 156 return l 157 } 158 159 pname, isshlib := findlib(ctxt, lib) 160 161 if ctxt.Debugvlog > 1 { 162 ctxt.Logf("%5.2f addlib: %s %s pulls in %s isshlib %v\n", elapsed(), obj, src, pname, isshlib) 163 } 164 165 if isshlib { 166 return addlibpath(ctxt, src, obj, "", pkg, pname) 167 } 168 return addlibpath(ctxt, src, obj, pname, pkg, "") 169 } 170 171 /* 172 * add library to library list, return added library. 173 * srcref: src file referring to package 174 * objref: object file referring to package 175 * file: object file, e.g., /home/rsc/go/pkg/container/vector.a 176 * pkg: package import path, e.g. container/vector 177 * shlib: path to shared library, or .shlibname file holding path 178 */ 179 func addlibpath(ctxt *Link, srcref string, objref string, file string, pkg string, shlib string) *sym.Library { 180 if l := ctxt.LibraryByPkg[pkg]; l != nil { 181 return l 182 } 183 184 if ctxt.Debugvlog > 1 { 185 ctxt.Logf("%5.2f addlibpath: srcref: %s objref: %s file: %s pkg: %s shlib: %s\n", Cputime(), srcref, objref, file, pkg, shlib) 186 } 187 188 l := &sym.Library{} 189 ctxt.LibraryByPkg[pkg] = l 190 ctxt.Library = append(ctxt.Library, l) 191 l.Objref = objref 192 l.Srcref = srcref 193 l.File = file 194 l.Pkg = pkg 195 if shlib != "" { 196 if strings.HasSuffix(shlib, ".shlibname") { 197 data, err := ioutil.ReadFile(shlib) 198 if err != nil { 199 Errorf(nil, "cannot read %s: %v", shlib, err) 200 } 201 shlib = strings.TrimSpace(string(data)) 202 } 203 l.Shlib = shlib 204 } 205 return l 206 } 207 208 func atolwhex(s string) int64 { 209 n, _ := strconv.ParseInt(s, 0, 64) 210 return n 211 }