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
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.
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:
21 // The above copyright notice and this permission notice shall be included in
22 // all copies or substantial portions of the Software.
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
36 "cmd/link/internal/loader"
37 "cmd/link/internal/sym"
47 func (ctxt *Link) readImportCfg(file string) {
48 ctxt.PackageFile = make(map[string]string)
49 ctxt.PackageShlib = make(map[string]string)
50 data, err := ioutil.ReadFile(file)
52 log.Fatalf("-importcfg: %v", err)
55 for lineNum, line := range strings.Split(string(data), "\n") {
57 line = strings.TrimSpace(line)
61 if line == "" || strings.HasPrefix(line, "#") {
66 if i := strings.Index(line, " "); i < 0 {
69 verb, args = line[:i], strings.TrimSpace(line[i+1:])
71 var before, after string
72 if i := strings.Index(args, "="); i >= 0 {
73 before, after = args[:i], args[i+1:]
77 log.Fatalf("%s:%d: unknown directive %q", file, lineNum, verb)
79 if before == "" || after == "" {
80 log.Fatalf(`%s:%d: invalid packagefile: syntax is "packagefile path=filename"`, file, lineNum)
82 ctxt.PackageFile[before] = after
84 if before == "" || after == "" {
85 log.Fatalf(`%s:%d: invalid packageshlib: syntax is "packageshlib path=filename"`, file, lineNum)
87 ctxt.PackageShlib[before] = after
89 s, err := strconv.Unquote(args)
91 log.Fatalf("%s:%d: invalid modinfo: %v", file, lineNum, err)
93 addstrdata1(ctxt, "runtime.modinfo="+s)
98 func pkgname(ctxt *Link, lib string) string {
99 return path.Clean(lib)
102 func findlib(ctxt *Link, lib string) (string, bool) {
103 name := path.Clean(lib)
108 if ctxt.linkShared && ctxt.PackageShlib[name] != "" {
109 pname = ctxt.PackageShlib[name]
111 } else if ctxt.PackageFile != nil {
112 pname = ctxt.PackageFile[name]
114 ctxt.Logf("cannot find package %s (using -importcfg)\n", name)
118 pkg := pkgname(ctxt, lib)
120 // search -L "libdir" directories
121 for _, dir := range ctxt.Libdir {
123 pname = filepath.Join(dir, pkg+".shlibname")
124 if _, err := os.Stat(pname); err == nil {
129 pname = filepath.Join(dir, name+".a")
130 if _, err := os.Stat(pname); err == nil {
133 pname = filepath.Join(dir, name+".o")
134 if _, err := os.Stat(pname); err == nil {
138 pname = filepath.Clean(pname)
141 return pname, isshlib
144 func addlib(ctxt *Link, src, obj, lib string, fingerprint goobj.FingerprintType) *sym.Library {
145 pkg := pkgname(ctxt, lib)
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
154 checkFingerprint(l, l.Fingerprint, src, fingerprint)
158 pname, isshlib := findlib(ctxt, lib)
160 if ctxt.Debugvlog > 1 {
161 ctxt.Logf("addlib: %s %s pulls in %s isshlib %v\n", obj, src, pname, isshlib)
165 return addlibpath(ctxt, src, obj, "", pkg, pname, fingerprint)
167 return addlibpath(ctxt, src, obj, pname, pkg, "", fingerprint)
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)
180 func addlibpath(ctxt *Link, srcref, objref, file, pkg, shlib string, fingerprint goobj.FingerprintType) *sym.Library {
181 if l := ctxt.LibraryByPkg[pkg]; l != nil {
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)
190 ctxt.LibraryByPkg[pkg] = l
191 ctxt.Library = append(ctxt.Library, l)
196 l.Fingerprint = fingerprint
198 if strings.HasSuffix(shlib, ".shlibname") {
199 data, err := ioutil.ReadFile(shlib)
201 Errorf(nil, "cannot read %s: %v", shlib, err)
203 shlib = strings.TrimSpace(string(data))
210 func atolwhex(s string) int64 {
211 n, _ := strconv.ParseInt(s, 0, 64)
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
221 func PrepareAddmoduledata(ctxt *Link) (*loader.SymbolBuilder, loader.Sym) {
222 if !ctxt.DynlinkingGo() {
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
231 ctxt.loader.SetAttrReachable(amd, true)
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)
241 // Add the init func and/or addmoduledata to Textp.
242 if ctxt.BuildMode == BuildModePlugin {
243 ctxt.Textp = append(ctxt.Textp, amd)
245 ctxt.Textp = append(ctxt.Textp, initfunc.Sym())
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)