| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| package ld |
|
|
| import ( |
| "log" |
| "os" |
| "path" |
| "path/filepath" |
| "strconv" |
| "strings" |
|
|
| "cmd/internal/goobj" |
| "cmd/link/internal/loader" |
| "cmd/link/internal/sym" |
| ) |
|
|
| func (ctxt *Link) readImportCfg(file string) { |
| ctxt.PackageFile = make(map[string]string) |
| ctxt.PackageShlib = make(map[string]string) |
| data, err := os.ReadFile(file) |
| if err != nil { |
| log.Fatalf("-importcfg: %v", err) |
| } |
|
|
| for lineNum, line := range strings.Split(string(data), "\n") { |
| lineNum++ |
| line = strings.TrimSpace(line) |
| if line == "" { |
| continue |
| } |
| if line == "" || strings.HasPrefix(line, "#") { |
| continue |
| } |
|
|
| verb, args, found := strings.Cut(line, " ") |
| if found { |
| args = strings.TrimSpace(args) |
| } |
| before, after, exist := strings.Cut(args, "=") |
| if !exist { |
| before = "" |
| } |
| switch verb { |
| default: |
| log.Fatalf("%s:%d: unknown directive %q", file, lineNum, verb) |
| case "packagefile": |
| if before == "" || after == "" { |
| log.Fatalf(`%s:%d: invalid packagefile: syntax is "packagefile path=filename"`, file, lineNum) |
| } |
| ctxt.PackageFile[before] = after |
| case "packageshlib": |
| if before == "" || after == "" { |
| log.Fatalf(`%s:%d: invalid packageshlib: syntax is "packageshlib path=filename"`, file, lineNum) |
| } |
| ctxt.PackageShlib[before] = after |
| case "modinfo": |
| s, err := strconv.Unquote(args) |
| if err != nil { |
| log.Fatalf("%s:%d: invalid modinfo: %v", file, lineNum, err) |
| } |
| addstrdata1(ctxt, "runtime.modinfo="+s) |
| } |
| } |
| } |
|
|
| func pkgname(ctxt *Link, lib string) string { |
| return path.Clean(lib) |
| } |
|
|
| func findlib(ctxt *Link, lib string) (string, bool) { |
| name := path.Clean(lib) |
|
|
| var pname string |
| isshlib := false |
|
|
| if ctxt.linkShared && ctxt.PackageShlib[name] != "" { |
| pname = ctxt.PackageShlib[name] |
| isshlib = true |
| } else if ctxt.PackageFile != nil { |
| pname = ctxt.PackageFile[name] |
| if pname == "" { |
| ctxt.Logf("cannot find package %s (using -importcfg)\n", name) |
| return "", false |
| } |
| } else { |
| pkg := pkgname(ctxt, lib) |
|
|
| |
| for _, dir := range ctxt.Libdir { |
| if ctxt.linkShared { |
| pname = filepath.Join(dir, pkg+".shlibname") |
| if _, err := os.Stat(pname); err == nil { |
| isshlib = true |
| break |
| } |
| } |
| pname = filepath.Join(dir, name+".a") |
| if _, err := os.Stat(pname); err == nil { |
| break |
| } |
| pname = filepath.Join(dir, name+".o") |
| if _, err := os.Stat(pname); err == nil { |
| break |
| } |
| } |
| pname = filepath.Clean(pname) |
| } |
|
|
| return pname, isshlib |
| } |
|
|
| func addlib(ctxt *Link, src, obj, lib string, fingerprint goobj.FingerprintType) *sym.Library { |
| pkg := pkgname(ctxt, lib) |
|
|
| |
| if l := ctxt.LibraryByPkg[pkg]; l != nil && !l.Fingerprint.IsZero() { |
| |
| |
| |
| |
| |
| checkFingerprint(l, l.Fingerprint, src, fingerprint) |
| return l |
| } |
|
|
| pname, isshlib := findlib(ctxt, lib) |
|
|
| if ctxt.Debugvlog > 1 { |
| ctxt.Logf("addlib: %s %s pulls in %s isshlib %v\n", obj, src, pname, isshlib) |
| } |
|
|
| if isshlib { |
| return addlibpath(ctxt, src, obj, "", pkg, pname, fingerprint) |
| } |
| return addlibpath(ctxt, src, obj, pname, pkg, "", fingerprint) |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| func addlibpath(ctxt *Link, srcref, objref, file, pkg, shlib string, fingerprint goobj.FingerprintType) *sym.Library { |
| if l := ctxt.LibraryByPkg[pkg]; l != nil { |
| return l |
| } |
|
|
| if ctxt.Debugvlog > 1 { |
| ctxt.Logf("addlibpath: srcref: %s objref: %s file: %s pkg: %s shlib: %s fingerprint: %x\n", srcref, objref, file, pkg, shlib, fingerprint) |
| } |
|
|
| l := &sym.Library{} |
| ctxt.LibraryByPkg[pkg] = l |
| ctxt.Library = append(ctxt.Library, l) |
| l.Objref = objref |
| l.Srcref = srcref |
| l.File = file |
| l.Pkg = pkg |
| l.Fingerprint = fingerprint |
| if shlib != "" { |
| if strings.HasSuffix(shlib, ".shlibname") { |
| data, err := os.ReadFile(shlib) |
| if err != nil { |
| Errorf("cannot read %s: %v", shlib, err) |
| } |
| shlib = strings.TrimSpace(string(data)) |
| } |
| l.Shlib = shlib |
| } |
| return l |
| } |
|
|
| func atolwhex(s string) int64 { |
| n, _ := strconv.ParseInt(s, 0, 64) |
| return n |
| } |
|
|
| |
| |
| |
| |
| |
| |
| func PrepareAddmoduledata(ctxt *Link) (*loader.SymbolBuilder, loader.Sym) { |
| if !ctxt.DynlinkingGo() { |
| return nil, 0 |
| } |
| amd := ctxt.loader.LookupOrCreateSym("runtime.addmoduledata", 0) |
| if ctxt.loader.SymType(amd).IsText() && ctxt.BuildMode != BuildModePlugin { |
| |
| |
| return nil, 0 |
| } |
| ctxt.loader.SetAttrReachable(amd, true) |
|
|
| |
| |
| ifs := ctxt.loader.LookupOrCreateSym("go:link.addmoduledata", 0) |
| initfunc := ctxt.loader.MakeSymbolUpdater(ifs) |
| ctxt.loader.SetAttrReachable(ifs, true) |
| ctxt.loader.SetAttrLocal(ifs, true) |
| initfunc.SetType(sym.STEXT) |
|
|
| |
| if ctxt.BuildMode == BuildModePlugin { |
| ctxt.Textp = append(ctxt.Textp, amd) |
| } |
| ctxt.Textp = append(ctxt.Textp, initfunc.Sym()) |
|
|
| |
| amdi := ctxt.loader.LookupOrCreateSym("go:link.addmoduledatainit", 0) |
| initarray_entry := ctxt.loader.MakeSymbolUpdater(amdi) |
| ctxt.loader.SetAttrReachable(amdi, true) |
| ctxt.loader.SetAttrLocal(amdi, true) |
| initarray_entry.SetType(sym.SINITARR) |
| initarray_entry.AddAddr(ctxt.Arch, ifs) |
|
|
| return initfunc, amd |
| } |
|
|