github.com/bir3/gocompiler@v0.3.205/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/master/utils/6l/obj.c 3 // https://bitbucket.org/inferno-os/inferno-os/src/master/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 "log" 36 "os" 37 "path" 38 "path/filepath" 39 "strconv" 40 "strings" 41 42 "github.com/bir3/gocompiler/src/cmd/internal/goobj" 43 "github.com/bir3/gocompiler/src/cmd/link/internal/loader" 44 "github.com/bir3/gocompiler/src/cmd/link/internal/sym" 45 ) 46 47 func (ctxt *Link) readImportCfg(file string) { 48 ctxt.PackageFile = make(map[string]string) 49 ctxt.PackageShlib = make(map[string]string) 50 data, err := os.ReadFile(file) 51 if err != nil { 52 log.Fatalf("-importcfg: %v", err) 53 } 54 55 for lineNum, line := range strings.Split(string(data), "\n") { 56 lineNum++ // 1-based 57 line = strings.TrimSpace(line) 58 if line == "" { 59 continue 60 } 61 if line == "" || strings.HasPrefix(line, "#") { 62 continue 63 } 64 65 var verb, args string 66 if i := strings.Index(line, " "); i < 0 { 67 verb = line 68 } else { 69 verb, args = line[:i], strings.TrimSpace(line[i+1:]) 70 } 71 var before, after string 72 if i := strings.Index(args, "="); i >= 0 { 73 before, after = args[:i], args[i+1:] 74 } 75 switch verb { 76 default: 77 log.Fatalf("%s:%d: unknown directive %q", file, lineNum, verb) 78 case "packagefile": 79 if before == "" || after == "" { 80 log.Fatalf(`%s:%d: invalid packagefile: syntax is "packagefile path=filename"`, file, lineNum) 81 } 82 ctxt.PackageFile[before] = after 83 case "packageshlib": 84 if before == "" || after == "" { 85 log.Fatalf(`%s:%d: invalid packageshlib: syntax is "packageshlib path=filename"`, file, lineNum) 86 } 87 ctxt.PackageShlib[before] = after 88 case "modinfo": 89 s, err := strconv.Unquote(args) 90 if err != nil { 91 log.Fatalf("%s:%d: invalid modinfo: %v", file, lineNum, err) 92 } 93 addstrdata1(ctxt, "runtime.modinfo="+s) 94 } 95 } 96 } 97 98 func pkgname(ctxt *Link, lib string) string { 99 return path.Clean(lib) 100 } 101 102 func findlib(ctxt *Link, lib string) (string, bool) { 103 name := path.Clean(lib) 104 105 var pname string 106 isshlib := false 107 108 if ctxt.linkShared && ctxt.PackageShlib[name] != "" { 109 pname = ctxt.PackageShlib[name] 110 isshlib = true 111 } else if ctxt.PackageFile != nil { 112 pname = ctxt.PackageFile[name] 113 if pname == "" { 114 ctxt.Logf("cannot find package %s (using -importcfg)\n", name) 115 return "", false 116 } 117 } else { 118 pkg := pkgname(ctxt, lib) 119 120 // search -L "libdir" directories 121 for _, dir := range ctxt.Libdir { 122 if ctxt.linkShared { 123 pname = filepath.Join(dir, pkg+".shlibname") 124 if _, err := os.Stat(pname); err == nil { 125 isshlib = true 126 break 127 } 128 } 129 pname = filepath.Join(dir, name+".a") 130 if _, err := os.Stat(pname); err == nil { 131 break 132 } 133 pname = filepath.Join(dir, name+".o") 134 if _, err := os.Stat(pname); err == nil { 135 break 136 } 137 } 138 pname = filepath.Clean(pname) 139 } 140 141 return pname, isshlib 142 } 143 144 func addlib(ctxt *Link, src, obj, lib string, fingerprint goobj.FingerprintType) *sym.Library { 145 pkg := pkgname(ctxt, lib) 146 147 // already loaded? 148 if l := ctxt.LibraryByPkg[pkg]; l != nil && !l.Fingerprint.IsZero() { 149 // Normally, packages are loaded in dependency order, and if l != nil 150 // l is already loaded with the actual fingerprint. In shared build mode, 151 // however, packages may be added not in dependency order, and it is 152 // possible that l's fingerprint is not yet loaded -- exclude it in 153 // checking. 154 checkFingerprint(l, l.Fingerprint, src, fingerprint) 155 return l 156 } 157 158 pname, isshlib := findlib(ctxt, lib) 159 160 if ctxt.Debugvlog > 1 { 161 ctxt.Logf("addlib: %s %s pulls in %s isshlib %v\n", obj, src, pname, isshlib) 162 } 163 164 if isshlib { 165 return addlibpath(ctxt, src, obj, "", pkg, pname, fingerprint) 166 } 167 return addlibpath(ctxt, src, obj, pname, pkg, "", fingerprint) 168 } 169 170 /* 171 * add library to library list, return added library. 172 * srcref: src file referring to package 173 * objref: object file referring to package 174 * file: object file, e.g., /home/rsc/go/pkg/container/vector.a 175 * pkg: package import path, e.g. container/vector 176 * shlib: path to shared library, or .shlibname file holding path 177 * fingerprint: if not 0, expected fingerprint for import from srcref 178 * fingerprint is 0 if the library is not imported (e.g. main) 179 */ 180 func addlibpath(ctxt *Link, srcref, objref, file, pkg, shlib string, fingerprint goobj.FingerprintType) *sym.Library { 181 if l := ctxt.LibraryByPkg[pkg]; l != nil { 182 return l 183 } 184 185 if ctxt.Debugvlog > 1 { 186 ctxt.Logf("addlibpath: srcref: %s objref: %s file: %s pkg: %s shlib: %s fingerprint: %x\n", srcref, objref, file, pkg, shlib, fingerprint) 187 } 188 189 l := &sym.Library{} 190 ctxt.LibraryByPkg[pkg] = l 191 ctxt.Library = append(ctxt.Library, l) 192 l.Objref = objref 193 l.Srcref = srcref 194 l.File = file 195 l.Pkg = pkg 196 l.Fingerprint = fingerprint 197 if shlib != "" { 198 if strings.HasSuffix(shlib, ".shlibname") { 199 data, err := os.ReadFile(shlib) 200 if err != nil { 201 Errorf(nil, "cannot read %s: %v", shlib, err) 202 } 203 shlib = strings.TrimSpace(string(data)) 204 } 205 l.Shlib = shlib 206 } 207 return l 208 } 209 210 func atolwhex(s string) int64 { 211 n, _ := strconv.ParseInt(s, 0, 64) 212 return n 213 } 214 215 // PrepareAddmoduledata returns a symbol builder that target-specific 216 // code can use to build up the linker-generated go.link.addmoduledata 217 // function, along with the sym for runtime.addmoduledata itself. If 218 // this function is not needed (for example in cases where we're 219 // linking a module that contains the runtime) the returned builder 220 // will be nil. 221 func PrepareAddmoduledata(ctxt *Link) (*loader.SymbolBuilder, loader.Sym) { 222 if !ctxt.DynlinkingGo() { 223 return nil, 0 224 } 225 amd := ctxt.loader.LookupOrCreateSym("runtime.addmoduledata", 0) 226 if ctxt.loader.SymType(amd) == sym.STEXT && ctxt.BuildMode != BuildModePlugin { 227 // we're linking a module containing the runtime -> no need for 228 // an init function 229 return nil, 0 230 } 231 ctxt.loader.SetAttrReachable(amd, true) 232 233 // Create a new init func text symbol. Caller will populate this 234 // sym with arch-specific content. 235 ifs := ctxt.loader.LookupOrCreateSym("go:link.addmoduledata", 0) 236 initfunc := ctxt.loader.MakeSymbolUpdater(ifs) 237 ctxt.loader.SetAttrReachable(ifs, true) 238 ctxt.loader.SetAttrLocal(ifs, true) 239 initfunc.SetType(sym.STEXT) 240 241 // Add the init func and/or addmoduledata to Textp. 242 if ctxt.BuildMode == BuildModePlugin { 243 ctxt.Textp = append(ctxt.Textp, amd) 244 } 245 ctxt.Textp = append(ctxt.Textp, initfunc.Sym()) 246 247 // Create an init array entry 248 amdi := ctxt.loader.LookupOrCreateSym("go:link.addmoduledatainit", 0) 249 initarray_entry := ctxt.loader.MakeSymbolUpdater(amdi) 250 ctxt.loader.SetAttrReachable(amdi, true) 251 ctxt.loader.SetAttrLocal(amdi, true) 252 initarray_entry.SetType(sym.SINITARR) 253 initarray_entry.AddAddr(ctxt.Arch, ifs) 254 255 return initfunc, amd 256 }