1 // Inferno utils/8l/asm.c
2 // https://bitbucket.org/inferno-os/inferno-os/src/default/utils/8l/asm.c
4 // Copyright © 1994-1999 Lucent Technologies Inc. All rights reserved.
5 // Portions Copyright © 1995-1997 C H Forsyth (forsyth@terzarima.net)
6 // Portions Copyright © 1997-1999 Vita Nuova Limited
7 // Portions Copyright © 2000-2007 Vita Nuova Holdings Limited (www.vitanuova.com)
8 // Portions Copyright © 2004,2006 Bruce Ellis
9 // Portions Copyright © 2005-2007 C H Forsyth (forsyth@terzarima.net)
10 // Revisions Copyright © 2000-2007 Lucent Technologies Inc. and others
11 // Portions Copyright © 2009 The Go Authors. All rights reserved.
13 // Permission is hereby granted, free of charge, to any person obtaining a copy
14 // of this software and associated documentation files (the "Software"), to deal
15 // in the Software without restriction, including without limitation the rights
16 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 // copies of the Software, and to permit persons to whom the Software is
18 // furnished to do so, subject to the following conditions:
20 // The above copyright notice and this permission notice shall be included in
21 // all copies or substantial portions of the Software.
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
39 "cmd/link/internal/loadelf"
40 "cmd/link/internal/loadmacho"
41 "cmd/link/internal/loadpe"
42 "cmd/link/internal/objfile"
43 "cmd/link/internal/sym"
61 // Data layout and relocation.
63 // Derived from Inferno utils/6l/l.h
64 // https://bitbucket.org/inferno-os/inferno-os/src/default/utils/6l/l.h
66 // Copyright © 1994-1999 Lucent Technologies Inc. All rights reserved.
67 // Portions Copyright © 1995-1997 C H Forsyth (forsyth@terzarima.net)
68 // Portions Copyright © 1997-1999 Vita Nuova Limited
69 // Portions Copyright © 2000-2007 Vita Nuova Holdings Limited (www.vitanuova.com)
70 // Portions Copyright © 2004,2006 Bruce Ellis
71 // Portions Copyright © 2005-2007 C H Forsyth (forsyth@terzarima.net)
72 // Revisions Copyright © 2000-2007 Lucent Technologies Inc. and others
73 // Portions Copyright © 2009 The Go Authors. All rights reserved.
75 // Permission is hereby granted, free of charge, to any person obtaining a copy
76 // of this software and associated documentation files (the "Software"), to deal
77 // in the Software without restriction, including without limitation the rights
78 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
79 // copies of the Software, and to permit persons to whom the Software is
80 // furnished to do so, subject to the following conditions:
82 // The above copyright notice and this permission notice shall be included in
83 // all copies or substantial portions of the Software.
85 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
86 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
87 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
88 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
89 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
90 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
103 Dragonflydynld string
105 Adddynrel func(*Link, *sym.Symbol, *sym.Reloc) bool
107 Archreloc func(*Link, *sym.Reloc, *sym.Symbol, *int64) bool
108 Archrelocvariant func(*Link, *sym.Reloc, *sym.Symbol, int64) int64
109 Trampoline func(*Link, *sym.Reloc, *sym.Symbol)
111 Elfreloc1 func(*Link, *sym.Reloc, int64) bool
112 Elfsetupplt func(*Link)
114 Machoreloc1 func(*sys.Arch, *OutBuf, *sym.Symbol, *sym.Reloc, int64) bool
115 PEreloc1 func(*sys.Arch, *OutBuf, *sym.Symbol, *sym.Reloc, int64) bool
117 // TLSIEtoLE converts a TLS Initial Executable relocation to
118 // a TLS Local Executable relocation.
120 // This is possible when a TLS IE relocation refers to a local
121 // symbol in an executable, which is typical when internally
122 // linking PIE binaries.
123 TLSIEtoLE func(s *sym.Symbol, off, size int)
125 // optional override for assignAddress
126 AssignAddress func(ctxt *Link, sect *sym.Section, n int, s *sym.Symbol, va uint64, isTramp bool) (*sym.Section, int, uint64)
138 MINFUNC = 16 // minimum size for a function
141 // DynlinkingGo returns whether we are producing Go code that can live
142 // in separate shared libraries linked together at runtime.
143 func (ctxt *Link) DynlinkingGo() bool {
145 panic("DynlinkingGo called before all symbols loaded")
147 return ctxt.BuildMode == BuildModeShared || ctxt.linkShared || ctxt.BuildMode == BuildModePlugin || ctxt.CanUsePlugins()
150 // CanUsePlugins returns whether a plugins can be used
151 func (ctxt *Link) CanUsePlugins() bool {
152 return ctxt.Syms.ROLookup("plugin.Open", 0) != nil
155 // UseRelro returns whether to make use of "read only relocations" aka
157 func (ctxt *Link) UseRelro() bool {
158 switch ctxt.BuildMode {
159 case BuildModeCArchive, BuildModeCShared, BuildModeShared, BuildModePIE, BuildModePlugin:
162 return ctxt.linkShared
176 debug_s bool // backup old value of debug['s']
185 Segrodata sym.Segment
186 Segrelrodata sym.Segment
191 const pkgdef = "__.PKGDEF"
194 // Set if we see an object compiled by the host compiler that is not
195 // from a package that is known to support internal linking mode.
200 func Lflag(ctxt *Link, arg string) {
201 ctxt.Libdir = append(ctxt.Libdir, arg)
205 * Unix doesn't like it when we write to a running (or, sometimes,
206 * recently run) binary, so remove the output file before writing it.
207 * On Windows 7, remove() can force a subsequent create() to fail.
208 * S_ISREG() does not exist on Plan 9.
210 func mayberemoveoutfile() {
211 if fi, err := os.Lstat(*flagOutfile); err == nil && !fi.Mode().IsRegular() {
214 os.Remove(*flagOutfile)
217 func libinit(ctxt *Link) {
218 Funcalign = thearch.Funcalign
220 // add goroot to the end of the libdir list.
224 if *flagInstallSuffix != "" {
226 suffix = *flagInstallSuffix
227 } else if *flagRace {
230 } else if *flagMsan {
235 Lflag(ctxt, filepath.Join(objabi.GOROOT, "pkg", fmt.Sprintf("%s_%s%s%s", objabi.GOOS, objabi.GOARCH, suffixsep, suffix)))
238 f, err := os.OpenFile(*flagOutfile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0775)
240 Exitf("cannot create %s: %v", *flagOutfile, err)
243 ctxt.Out.w = bufio.NewWriter(f)
246 if *flagEntrySymbol == "" {
247 switch ctxt.BuildMode {
248 case BuildModeCShared, BuildModeCArchive:
249 *flagEntrySymbol = fmt.Sprintf("_rt0_%s_%s_lib", objabi.GOARCH, objabi.GOOS)
250 case BuildModeExe, BuildModePIE:
251 *flagEntrySymbol = fmt.Sprintf("_rt0_%s_%s", objabi.GOARCH, objabi.GOOS)
252 case BuildModeShared, BuildModePlugin:
253 // No *flagEntrySymbol for -buildmode=shared and plugin
255 Errorf(nil, "unknown *flagEntrySymbol for buildmode %v", ctxt.BuildMode)
267 func loadinternal(ctxt *Link, name string) *sym.Library {
268 if ctxt.linkShared && ctxt.PackageShlib != nil {
269 if shlib := ctxt.PackageShlib[name]; shlib != "" {
270 return addlibpath(ctxt, "internal", "internal", "", name, shlib)
273 if ctxt.PackageFile != nil {
274 if pname := ctxt.PackageFile[name]; pname != "" {
275 return addlibpath(ctxt, "internal", "internal", pname, name, "")
277 ctxt.Logf("loadinternal: cannot find %s\n", name)
281 for _, libdir := range ctxt.Libdir {
283 shlibname := filepath.Join(libdir, name+".shlibname")
284 if ctxt.Debugvlog != 0 {
285 ctxt.Logf("searching for %s.a in %s\n", name, shlibname)
287 if _, err := os.Stat(shlibname); err == nil {
288 return addlibpath(ctxt, "internal", "internal", "", name, shlibname)
291 pname := filepath.Join(libdir, name+".a")
292 if ctxt.Debugvlog != 0 {
293 ctxt.Logf("searching for %s.a in %s\n", name, pname)
295 if _, err := os.Stat(pname); err == nil {
296 return addlibpath(ctxt, "internal", "internal", pname, name, "")
300 ctxt.Logf("warning: unable to find %s.a\n", name)
304 // findLibPathCmd uses cmd command to find gcc library libname.
305 // It returns library full path if found, or "none" if not found.
306 func (ctxt *Link) findLibPathCmd(cmd, libname string) string {
307 if *flagExtld == "" {
310 args := hostlinkArchArgs(ctxt.Arch)
311 args = append(args, cmd)
312 if ctxt.Debugvlog != 0 {
313 ctxt.Logf("%s %v\n", *flagExtld, args)
315 out, err := exec.Command(*flagExtld, args...).Output()
317 if ctxt.Debugvlog != 0 {
318 ctxt.Logf("not using a %s file because compiler failed\n%v\n%s\n", libname, err, out)
322 return strings.TrimSpace(string(out))
325 // findLibPath searches for library libname.
326 // It returns library full path if found, or "none" if not found.
327 func (ctxt *Link) findLibPath(libname string) string {
328 return ctxt.findLibPathCmd("--print-file-name="+libname, libname)
331 func (ctxt *Link) loadlib() {
332 switch ctxt.BuildMode {
333 case BuildModeCShared, BuildModePlugin:
334 s := ctxt.Syms.Lookup("runtime.islibrary", 0)
335 s.Attr |= sym.AttrDuplicateOK
337 case BuildModeCArchive:
338 s := ctxt.Syms.Lookup("runtime.isarchive", 0)
339 s.Attr |= sym.AttrDuplicateOK
343 loadinternal(ctxt, "runtime")
344 if ctxt.Arch.Family == sys.ARM {
345 loadinternal(ctxt, "math")
348 loadinternal(ctxt, "runtime/race")
351 loadinternal(ctxt, "runtime/msan")
354 // ctxt.Library grows during the loop, so not a range loop.
355 for i := 0; i < len(ctxt.Library); i++ {
356 lib := ctxt.Library[i]
358 if ctxt.Debugvlog > 1 {
359 ctxt.Logf("%5.2f autolib: %s (from %s)\n", Cputime(), lib.File, lib.Objref)
361 loadobjfile(ctxt, lib)
365 for _, lib := range ctxt.Library {
367 if ctxt.Debugvlog > 1 {
368 ctxt.Logf("%5.2f autolib: %s (from %s)\n", Cputime(), lib.Shlib, lib.Objref)
370 ldshlibsyms(ctxt, lib.Shlib)
374 iscgo = ctxt.Syms.ROLookup("x_cgo_init", 0) != nil
376 // We now have enough information to determine the link mode.
377 determineLinkMode(ctxt)
379 // Recalculate pe parameters now that we have ctxt.LinkMode set.
380 if ctxt.HeadType == objabi.Hwindows {
384 if ctxt.HeadType == objabi.Hdarwin && ctxt.LinkMode == LinkExternal {
388 if ctxt.LinkMode == LinkExternal && ctxt.Arch.Family == sys.PPC64 {
389 toc := ctxt.Syms.Lookup(".TOC.", 0)
390 toc.Type = sym.SDYNIMPORT
393 if ctxt.LinkMode == LinkExternal && !iscgo && ctxt.LibraryByPkg["runtime/cgo"] == nil && !(objabi.GOOS == "darwin" && (ctxt.Arch.Family == sys.AMD64 || ctxt.Arch.Family == sys.I386)) {
394 // This indicates a user requested -linkmode=external.
395 // The startup code uses an import of runtime/cgo to decide
396 // whether to initialize the TLS. So give it one. This could
397 // be handled differently but it's an unusual case.
398 if lib := loadinternal(ctxt, "runtime/cgo"); lib != nil {
400 ldshlibsyms(ctxt, lib.Shlib)
402 if ctxt.BuildMode == BuildModeShared || ctxt.linkShared {
403 Exitf("cannot implicitly include runtime/cgo in a shared library")
405 loadobjfile(ctxt, lib)
410 if ctxt.LinkMode == LinkInternal {
411 // Drop all the cgo_import_static declarations.
412 // Turns out we won't be needing them.
413 for _, s := range ctxt.Syms.Allsym {
414 if s.Type == sym.SHOSTOBJ {
415 // If a symbol was marked both
416 // cgo_import_static and cgo_import_dynamic,
417 // then we want to make it cgo_import_dynamic
419 if s.Extname != "" && s.Dynimplib != "" && !s.Attr.CgoExport() {
420 s.Type = sym.SDYNIMPORT
428 tlsg := ctxt.Syms.Lookup("runtime.tlsg", 0)
430 // runtime.tlsg is used for external linking on platforms that do not define
431 // a variable to hold g in assembly (currently only intel).
433 tlsg.Type = sym.STLSBSS
434 tlsg.Size = int64(ctxt.Arch.PtrSize)
435 } else if tlsg.Type != sym.SDYNIMPORT {
436 Errorf(nil, "runtime declared tlsg variable %v", tlsg.Type)
438 tlsg.Attr |= sym.AttrReachable
441 var moduledata *sym.Symbol
442 if ctxt.BuildMode == BuildModePlugin {
443 moduledata = ctxt.Syms.Lookup("local.pluginmoduledata", 0)
444 moduledata.Attr |= sym.AttrLocal
446 moduledata = ctxt.Syms.Lookup("runtime.firstmoduledata", 0)
448 if moduledata.Type != 0 && moduledata.Type != sym.SDYNIMPORT {
449 // If the module (toolchain-speak for "executable or shared
450 // library") we are linking contains the runtime package, it
451 // will define the runtime.firstmoduledata symbol and we
452 // truncate it back to 0 bytes so we can define its entire
453 // contents in symtab.go:symtab().
456 // In addition, on ARM, the runtime depends on the linker
457 // recording the value of GOARM.
458 if ctxt.Arch.Family == sys.ARM {
459 s := ctxt.Syms.Lookup("runtime.goarm", 0)
462 s.AddUint8(uint8(objabi.GOARM))
465 if objabi.Framepointer_enabled(objabi.GOOS, objabi.GOARCH) {
466 s := ctxt.Syms.Lookup("runtime.framepointer_enabled", 0)
472 // If OTOH the module does not contain the runtime package,
473 // create a local symbol for the moduledata.
474 moduledata = ctxt.Syms.Lookup("local.moduledata", 0)
475 moduledata.Attr |= sym.AttrLocal
477 // In all cases way we mark the moduledata as noptrdata to hide it from
479 moduledata.Type = sym.SNOPTRDATA
480 moduledata.Attr |= sym.AttrReachable
481 ctxt.Moduledata = moduledata
483 // Now that we know the link mode, trim the dynexp list.
484 x := sym.AttrCgoExportDynamic
486 if ctxt.LinkMode == LinkExternal {
487 x = sym.AttrCgoExportStatic
490 for i := range dynexp {
491 if dynexp[i].Attr&x != 0 {
492 dynexp[w] = dynexp[i]
498 // In internal link mode, read the host object files.
499 if ctxt.LinkMode == LinkInternal {
502 // If we have any undefined symbols in external
503 // objects, try to read them from the libgcc file.
505 for _, s := range ctxt.Syms.Allsym {
506 for _, r := range s.R {
507 if r.Sym != nil && r.Sym.Type == sym.SXREF && r.Sym.Name != ".got" {
514 if *flagLibGCC == "" {
515 *flagLibGCC = ctxt.findLibPathCmd("--print-libgcc-file-name", "libgcc")
517 if *flagLibGCC != "none" {
518 hostArchive(ctxt, *flagLibGCC)
520 if ctxt.HeadType == objabi.Hwindows {
521 if p := ctxt.findLibPath("libmingwex.a"); p != "none" {
524 if p := ctxt.findLibPath("libmingw32.a"); p != "none" {
527 // TODO: maybe do something similar to peimporteddlls to collect all lib names
528 // and try link them all to final exe just like libmingwex.a and libmingw32.a:
531 #cgo windows LDFLAGS: -lmsvcrt -lm
541 // We've loaded all the code now.
544 // If there are no dynamic libraries needed, gcc disables dynamic linking.
545 // Because of this, glibc's dynamic ELF loader occasionally (like in version 2.13)
546 // assumes that a dynamic binary always refers to at least one dynamic library.
547 // Rather than be a source of test cases for glibc, disable dynamic linking
548 // the same way that gcc would.
550 // Exception: on OS X, programs such as Shark only work with dynamic
551 // binaries, so leave it enabled on OS X (Mach-O) binaries.
552 // Also leave it enabled on Solaris which doesn't support
553 // statically linked binaries.
554 if ctxt.BuildMode == BuildModeExe {
555 if havedynamic == 0 && ctxt.HeadType != objabi.Hdarwin && ctxt.HeadType != objabi.Hsolaris {
560 // If type. symbols are visible in the symbol table, rename them
561 // using a SHA-1 prefix. This reduces binary size (the full
562 // string of a type symbol can be multiple kilobytes) and removes
563 // characters that upset external linkers.
565 // Keep the type.. prefix, which parts of the linker (like the
566 // DWARF generator) know means the symbol is not decodable.
568 // Leave type.runtime. symbols alone, because other parts of
569 // the linker manipulates them, and also symbols whose names
570 // would not be shortened by this process.
571 if typeSymbolMangling(ctxt) {
572 *FlagW = true // disable DWARF generation
573 for _, s := range ctxt.Syms.Allsym {
574 newName := typeSymbolMangle(s.Name)
575 if newName != s.Name {
576 ctxt.Syms.Rename(s.Name, newName, int(s.Version))
581 // If package versioning is required, generate a hash of the
582 // packages used in the link.
583 if ctxt.BuildMode == BuildModeShared || ctxt.BuildMode == BuildModePlugin || ctxt.CanUsePlugins() {
584 for _, lib := range ctxt.Library {
591 if ctxt.Arch == sys.Arch386 {
592 if (ctxt.BuildMode == BuildModeCArchive && ctxt.IsELF) || (ctxt.BuildMode == BuildModeCShared && ctxt.HeadType != objabi.Hwindows) || ctxt.BuildMode == BuildModePIE || ctxt.DynlinkingGo() {
593 got := ctxt.Syms.Lookup("_GLOBAL_OFFSET_TABLE_", 0)
594 got.Type = sym.SDYNIMPORT
595 got.Attr |= sym.AttrReachable
601 // put symbols into Textp
602 // do it in postorder so that packages are laid down in dependency order
603 // internal first, then everything else
604 ctxt.Library = postorder(ctxt.Library)
605 for _, doInternal := range [2]bool{true, false} {
606 for _, lib := range ctxt.Library {
607 if isRuntimeDepPkg(lib.Pkg) != doInternal {
610 ctxt.Textp = append(ctxt.Textp, lib.Textp...)
611 for _, s := range lib.DupTextSyms {
612 if !s.Attr.OnList() {
613 ctxt.Textp = append(ctxt.Textp, s)
614 s.Attr |= sym.AttrOnList
615 // dupok symbols may be defined in multiple packages. its
616 // associated package is chosen sort of arbitrarily (the
617 // first containing package that the linker loads). canonicalize
618 // it here to the package with which it will be laid down
620 s.File = objabi.PathToPrefix(lib.Pkg)
626 if len(ctxt.Shlibs) > 0 {
627 // We might have overwritten some functions above (this tends to happen for the
628 // autogenerated type equality/hashing functions) and we don't want to generated
629 // pcln table entries for these any more so remove them from Textp.
630 textp := make([]*sym.Symbol, 0, len(ctxt.Textp))
631 for _, s := range ctxt.Textp {
632 if s.Type != sym.SDYNIMPORT {
633 textp = append(textp, s)
640 // typeSymbolMangling reports whether the linker should shorten the
641 // names of symbols that represent Go types.
643 // As the names of these symbols are derived from the string of
644 // the type, they can run to many kilobytes long. So we shorten
645 // them using a SHA-1 when the name appears in the final binary.
647 // These are the symbols that begin with the prefix 'type.' and
648 // contain run-time type information used by the runtime and reflect
649 // packages. All Go binaries contain these symbols, but only only
650 // those programs loaded dynamically in multiple parts need these
651 // symbols to have entries in the symbol table.
652 func typeSymbolMangling(ctxt *Link) bool {
653 return ctxt.BuildMode == BuildModeShared || ctxt.linkShared || ctxt.BuildMode == BuildModePlugin || ctxt.Syms.ROLookup("plugin.Open", 0) != nil
656 // typeSymbolMangle mangles the given symbol name into something shorter.
657 func typeSymbolMangle(name string) string {
658 if !strings.HasPrefix(name, "type.") {
661 if strings.HasPrefix(name, "type.runtime.") {
664 if len(name) <= 14 && !strings.Contains(name, "@") { // Issue 19529
667 hash := sha1.Sum([]byte(name))
672 return prefix + base64.StdEncoding.EncodeToString(hash[:6])
676 * look for the next file in an archive.
677 * adapted from libmach.
679 func nextar(bp *bio.Reader, off int64, a *ArHdr) int64 {
684 var buf [SAR_HDR]byte
685 if n, err := io.ReadFull(bp, buf[:]); err != nil {
686 if n == 0 && err != io.EOF {
692 a.name = artrim(buf[0:16])
693 a.date = artrim(buf[16:28])
694 a.uid = artrim(buf[28:34])
695 a.gid = artrim(buf[34:40])
696 a.mode = artrim(buf[40:48])
697 a.size = artrim(buf[48:58])
698 a.fmag = artrim(buf[58:60])
700 arsize := atolwhex(a.size)
704 return arsize + SAR_HDR
707 func genhash(ctxt *Link, lib *sym.Library) {
708 f, err := bio.Open(lib.File)
710 Errorf(nil, "cannot open file %s for hash generation: %v", lib.File, err)
715 var magbuf [len(ARMAG)]byte
716 if _, err := io.ReadFull(f, magbuf[:]); err != nil {
717 Exitf("file %s too short", lib.File)
720 if string(magbuf[:]) != ARMAG {
721 Exitf("%s is not an archive file", lib.File)
725 l := nextar(f, f.Offset(), &arhdr)
727 Errorf(nil, "%s: short read on archive file symbol header", lib.File)
730 if arhdr.name != pkgdef {
731 Errorf(nil, "%s: missing package data entry", lib.File)
737 // To compute the hash of a package, we hash the first line of
738 // __.PKGDEF (which contains the toolchain version and any
739 // GOEXPERIMENT flags) and the export data (which is between
740 // the first two occurrences of "\n$$").
742 pkgDefBytes := make([]byte, atolwhex(arhdr.size))
743 _, err = io.ReadFull(f, pkgDefBytes)
745 Errorf(nil, "%s: error reading package data: %v", lib.File, err)
748 firstEOL := bytes.IndexByte(pkgDefBytes, '\n')
750 Errorf(nil, "cannot parse package data of %s for hash generation, no newline found", lib.File)
753 firstDoubleDollar := bytes.Index(pkgDefBytes, []byte("\n$$"))
754 if firstDoubleDollar < 0 {
755 Errorf(nil, "cannot parse package data of %s for hash generation, no \\n$$ found", lib.File)
758 secondDoubleDollar := bytes.Index(pkgDefBytes[firstDoubleDollar+1:], []byte("\n$$"))
759 if secondDoubleDollar < 0 {
760 Errorf(nil, "cannot parse package data of %s for hash generation, only one \\n$$ found", lib.File)
763 h.Write(pkgDefBytes[0:firstEOL])
764 h.Write(pkgDefBytes[firstDoubleDollar : firstDoubleDollar+secondDoubleDollar])
765 lib.Hash = hex.EncodeToString(h.Sum(nil))
768 func loadobjfile(ctxt *Link, lib *sym.Library) {
769 pkg := objabi.PathToPrefix(lib.Pkg)
771 if ctxt.Debugvlog > 1 {
772 ctxt.Logf("%5.2f ldobj: %s (%s)\n", Cputime(), lib.File, pkg)
774 f, err := bio.Open(lib.File)
776 Exitf("cannot open file %s: %v", lib.File, err)
780 if pkg == "main" && !lib.Main {
781 Exitf("%s: not package main", lib.File)
784 // Ideally, we'd check that *all* object files within
785 // the archive were marked safe, but here we settle
788 // Historically, cmd/link only checked the __.PKGDEF
789 // file, which in turn came from the first object
790 // file, typically produced by cmd/compile. The
791 // remaining object files are normally produced by
792 // cmd/asm, which doesn't support marking files as
793 // safe anyway. So at least in practice, this matches
794 // how safe mode has always worked.
795 if *flagU && !lib.Safe {
796 Exitf("%s: load of unsafe package %s", lib.File, pkg)
800 for i := 0; i < len(ARMAG); i++ {
801 if c, err := f.ReadByte(); err == nil && c == ARMAG[i] {
805 /* load it as a regular file */
808 ldobj(ctxt, f, lib, l, lib.File, lib.File)
813 * load all the object files from the archive now.
814 * this gives us sequential file access and keeps us
815 * from needing to come back later to pick up more
816 * objects. it breaks the usual C archive model, but
817 * this is Go, not C. the common case in Go is that
818 * we need to load all the objects, and then we throw away
819 * the individual symbols that are unused.
821 * loading every object will also make it possible to
822 * load foreign objects not referenced by __.PKGDEF.
827 l := nextar(f, off, &arhdr)
832 Exitf("%s: malformed archive", lib.File)
836 // __.PKGDEF isn't a real Go object file, and it's
837 // absent in -linkobj builds anyway. Skipping it
838 // ensures consistency between -linkobj and normal
840 if arhdr.name == pkgdef {
844 pname := fmt.Sprintf("%s(%s)", lib.File, arhdr.name)
845 l = atolwhex(arhdr.size)
846 ldobj(ctxt, f, lib, l, pname, lib.File)
850 type Hostobj struct {
851 ld func(*Link, *bio.Reader, string, int64, string)
859 var hostobj []Hostobj
861 // These packages can use internal linking mode.
862 // Others trigger external mode.
863 var internalpkg = []string{
864 "crypto/internal/boring",
873 func ldhostobj(ld func(*Link, *bio.Reader, string, int64, string), headType objabi.HeadType, f *bio.Reader, pkg string, length int64, pn string, file string) *Hostobj {
875 for _, intpkg := range internalpkg {
882 // DragonFly declares errno with __thread, which results in a symbol
883 // type of R_386_TLS_GD or R_X86_64_TLSGD. The Go linker does not
884 // currently know how to handle TLS relocations, hence we have to
885 // force external linking for any libraries that link in code that
886 // uses errno. This can be removed if the Go linker ever supports
887 // these relocation types.
888 if headType == objabi.Hdragonfly {
889 if pkg == "net" || pkg == "os/user" {
898 hostobj = append(hostobj, Hostobj{})
899 h := &hostobj[len(hostobj)-1]
909 func hostobjs(ctxt *Link) {
912 for i := 0; i < len(hostobj); i++ {
914 f, err := bio.Open(h.file)
916 Exitf("cannot reopen %s: %v", h.pn, err)
920 h.ld(ctxt, f, h.pkg, h.length, h.pn)
928 os.RemoveAll(*flagTmpdir)
931 func hostlinksetup(ctxt *Link) {
932 if ctxt.LinkMode != LinkExternal {
936 // For external link, record that we need to tell the external linker -s,
937 // and turn off -s internally: the external linker needs the symbol
938 // information for its final link.
942 // create temporary directory and arrange cleanup
943 if *flagTmpdir == "" {
944 dir, err := ioutil.TempDir("", "go-link-")
952 // change our output to temporary object file
956 p := filepath.Join(*flagTmpdir, "go.o")
958 f, err := os.OpenFile(p, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0775)
960 Exitf("cannot create %s: %v", p, err)
963 ctxt.Out.w = bufio.NewWriter(f)
968 // hostobjCopy creates a copy of the object files in hostobj in a
969 // temporary directory.
970 func hostobjCopy() (paths []string) {
971 var wg sync.WaitGroup
972 sema := make(chan struct{}, runtime.NumCPU()) // limit open file descriptors
973 for i, h := range hostobj {
975 dst := filepath.Join(*flagTmpdir, fmt.Sprintf("%06d.o", i))
976 paths = append(paths, dst)
985 f, err := os.Open(h.file)
987 Exitf("cannot reopen %s: %v", h.pn, err)
989 if _, err := f.Seek(h.off, 0); err != nil {
990 Exitf("cannot seek %s: %v", h.pn, err)
993 w, err := os.Create(dst)
995 Exitf("cannot create %s: %v", dst, err)
997 if _, err := io.CopyN(w, f, h.length); err != nil {
998 Exitf("cannot write %s: %v", dst, err)
1000 if err := w.Close(); err != nil {
1001 Exitf("cannot close %s: %v", dst, err)
1009 // writeGDBLinkerScript creates gcc linker script file in temp
1010 // directory. writeGDBLinkerScript returns created file path.
1011 // The script is used to work around gcc bug
1012 // (see https://golang.org/issue/20183 for details).
1013 func writeGDBLinkerScript() string {
1014 name := "fix_debug_gdb_scripts.ld"
1015 path := filepath.Join(*flagTmpdir, name)
1018 .debug_gdb_scripts BLOCK(__section_alignment__) (NOLOAD) :
1020 *(.debug_gdb_scripts)
1023 INSERT AFTER .debug_types;
1025 err := ioutil.WriteFile(path, []byte(src), 0666)
1027 Errorf(nil, "WriteFile %s failed: %v", name, err)
1032 // archive builds a .a archive from the hostobj object files.
1033 func (ctxt *Link) archive() {
1034 if ctxt.BuildMode != BuildModeCArchive {
1038 if *flagExtar == "" {
1042 mayberemoveoutfile()
1044 // Force the buffer to flush here so that external
1045 // tools will see a complete file.
1047 if err := ctxt.Out.f.Close(); err != nil {
1048 Exitf("close: %v", err)
1052 argv := []string{*flagExtar, "-q", "-c", "-s", *flagOutfile}
1053 argv = append(argv, filepath.Join(*flagTmpdir, "go.o"))
1054 argv = append(argv, hostobjCopy()...)
1056 if ctxt.Debugvlog != 0 {
1057 ctxt.Logf("archive: %s\n", strings.Join(argv, " "))
1060 if out, err := exec.Command(argv[0], argv[1:]...).CombinedOutput(); err != nil {
1061 Exitf("running %s failed: %v\n%s", argv[0], err, out)
1065 func (ctxt *Link) hostlink() {
1066 if ctxt.LinkMode != LinkExternal || nerrors > 0 {
1069 if ctxt.BuildMode == BuildModeCArchive {
1073 if *flagExtld == "" {
1078 argv = append(argv, *flagExtld)
1079 argv = append(argv, hostlinkArchArgs(ctxt.Arch)...)
1081 if *FlagS || debug_s {
1082 if ctxt.HeadType == objabi.Hdarwin {
1083 // Recent versions of macOS print
1084 // ld: warning: option -s is obsolete and being ignored
1085 // so do not pass any arguments.
1087 argv = append(argv, "-s")
1091 switch ctxt.HeadType {
1092 case objabi.Hdarwin:
1093 argv = append(argv, "-Wl,-headerpad,1144")
1094 if ctxt.DynlinkingGo() {
1095 argv = append(argv, "-Wl,-flat_namespace")
1097 if ctxt.BuildMode == BuildModeExe && !ctxt.Arch.InFamily(sys.ARM64) {
1098 argv = append(argv, "-Wl,-no_pie")
1100 case objabi.Hopenbsd:
1101 argv = append(argv, "-Wl,-nopie")
1102 case objabi.Hwindows:
1104 argv = append(argv, "-mwindows")
1106 argv = append(argv, "-mconsole")
1110 switch ctxt.BuildMode {
1112 if ctxt.HeadType == objabi.Hdarwin {
1113 if ctxt.Arch.Family == sys.ARM64 {
1114 // __PAGEZERO segment size determined empirically.
1115 // XCode 9.0.1 successfully uploads an iOS app with this value.
1116 argv = append(argv, "-Wl,-pagezero_size,100000000")
1118 argv = append(argv, "-Wl,-pagezero_size,4000000")
1123 if ctxt.HeadType != objabi.Hdarwin {
1124 if ctxt.UseRelro() {
1125 argv = append(argv, "-Wl,-z,relro")
1127 argv = append(argv, "-pie")
1129 case BuildModeCShared:
1130 if ctxt.HeadType == objabi.Hdarwin {
1131 argv = append(argv, "-dynamiclib")
1132 if ctxt.Arch.Family != sys.AMD64 {
1133 argv = append(argv, "-Wl,-read_only_relocs,suppress")
1137 argv = append(argv, "-Wl,-Bsymbolic")
1138 if ctxt.UseRelro() {
1139 argv = append(argv, "-Wl,-z,relro")
1141 argv = append(argv, "-shared")
1142 if ctxt.HeadType != objabi.Hwindows {
1143 // Pass -z nodelete to mark the shared library as
1144 // non-closeable: a dlclose will do nothing.
1145 argv = append(argv, "-Wl,-z,nodelete")
1148 case BuildModeShared:
1149 if ctxt.UseRelro() {
1150 argv = append(argv, "-Wl,-z,relro")
1152 argv = append(argv, "-shared")
1153 case BuildModePlugin:
1154 if ctxt.HeadType == objabi.Hdarwin {
1155 argv = append(argv, "-dynamiclib")
1157 if ctxt.UseRelro() {
1158 argv = append(argv, "-Wl,-z,relro")
1160 argv = append(argv, "-shared")
1164 if ctxt.IsELF && ctxt.DynlinkingGo() {
1165 // We force all symbol resolution to be done at program startup
1166 // because lazy PLT resolution can use large amounts of stack at
1167 // times we cannot allow it to do so.
1168 argv = append(argv, "-Wl,-znow")
1170 // Do not let the host linker generate COPY relocations. These
1171 // can move symbols out of sections that rely on stable offsets
1172 // from the beginning of the section (like sym.STYPE).
1173 argv = append(argv, "-Wl,-znocopyreloc")
1175 if ctxt.Arch.InFamily(sys.ARM, sys.ARM64) {
1176 // On ARM, the GNU linker will generate COPY relocations
1177 // even with -znocopyreloc set.
1178 // https://sourceware.org/bugzilla/show_bug.cgi?id=19962
1180 // On ARM64, the GNU linker will fail instead of
1181 // generating COPY relocations.
1183 // In both cases, switch to gold.
1184 argv = append(argv, "-fuse-ld=gold")
1186 // If gold is not installed, gcc will silently switch
1187 // back to ld.bfd. So we parse the version information
1188 // and provide a useful error if gold is missing.
1189 cmd := exec.Command(*flagExtld, "-fuse-ld=gold", "-Wl,--version")
1190 if out, err := cmd.CombinedOutput(); err == nil {
1191 if !bytes.Contains(out, []byte("GNU gold")) {
1192 log.Fatalf("ARM external linker must be gold (issue #15696), but is not: %s", out)
1198 if ctxt.IsELF && len(buildinfo) > 0 {
1199 argv = append(argv, fmt.Sprintf("-Wl,--build-id=0x%x", buildinfo))
1202 // On Windows, given -o foo, GCC will append ".exe" to produce
1203 // "foo.exe". We have decided that we want to honor the -o
1204 // option. To make this work, we append a '.' so that GCC
1205 // will decide that the file already has an extension. We
1206 // only want to do this when producing a Windows output file
1207 // on a Windows host.
1208 outopt := *flagOutfile
1209 if objabi.GOOS == "windows" && runtime.GOOS == "windows" && filepath.Ext(outopt) == "" {
1212 argv = append(argv, "-o")
1213 argv = append(argv, outopt)
1215 if rpath.val != "" {
1216 argv = append(argv, fmt.Sprintf("-Wl,-rpath,%s", rpath.val))
1219 // Force global symbols to be exported for dlopen, etc.
1221 argv = append(argv, "-rdynamic")
1224 if strings.Contains(argv[0], "clang") {
1225 argv = append(argv, "-Qunused-arguments")
1228 argv = append(argv, filepath.Join(*flagTmpdir, "go.o"))
1229 argv = append(argv, hostobjCopy()...)
1231 if ctxt.linkShared {
1232 seenDirs := make(map[string]bool)
1233 seenLibs := make(map[string]bool)
1234 addshlib := func(path string) {
1235 dir, base := filepath.Split(path)
1237 argv = append(argv, "-L"+dir)
1239 argv = append(argv, "-Wl,-rpath="+dir)
1241 seenDirs[dir] = true
1243 base = strings.TrimSuffix(base, ".so")
1244 base = strings.TrimPrefix(base, "lib")
1245 if !seenLibs[base] {
1246 argv = append(argv, "-l"+base)
1247 seenLibs[base] = true
1250 for _, shlib := range ctxt.Shlibs {
1251 addshlib(shlib.Path)
1252 for _, dep := range shlib.Deps {
1256 libpath := findshlib(ctxt, dep)
1264 argv = append(argv, ldflag...)
1266 // When building a program with the default -buildmode=exe the
1267 // gc compiler generates code requires DT_TEXTREL in a
1268 // position independent executable (PIE). On systems where the
1269 // toolchain creates PIEs by default, and where DT_TEXTREL
1270 // does not work, the resulting programs will not run. See
1271 // issue #17847. To avoid this problem pass -no-pie to the
1272 // toolchain if it is supported.
1273 if ctxt.BuildMode == BuildModeExe && !ctxt.linkShared {
1274 src := filepath.Join(*flagTmpdir, "trivial.c")
1275 if err := ioutil.WriteFile(src, []byte("int main() { return 0; }"), 0666); err != nil {
1276 Errorf(nil, "WriteFile trivial.c failed: %v", err)
1279 // GCC uses -no-pie, clang uses -nopie.
1280 for _, nopie := range []string{"-no-pie", "-nopie"} {
1281 cmd := exec.Command(argv[0], nopie, "trivial.c")
1282 cmd.Dir = *flagTmpdir
1283 cmd.Env = append([]string{"LC_ALL=C"}, os.Environ()...)
1284 out, err := cmd.CombinedOutput()
1285 // GCC says "unrecognized command line option ‘-no-pie’"
1286 // clang says "unknown argument: '-no-pie'"
1287 supported := err == nil && !bytes.Contains(out, []byte("unrecognized")) && !bytes.Contains(out, []byte("unknown"))
1289 argv = append(argv, nopie)
1295 for _, p := range strings.Fields(*flagExtldflags) {
1296 argv = append(argv, p)
1298 // clang, unlike GCC, passes -rdynamic to the linker
1299 // even when linking with -static, causing a linker
1300 // error when using GNU ld. So take out -rdynamic if
1301 // we added it. We do it in this order, rather than
1302 // only adding -rdynamic later, so that -*extldflags
1303 // can override -rdynamic without using -static.
1304 if ctxt.IsELF && p == "-static" {
1305 for i := range argv {
1306 if argv[i] == "-rdynamic" {
1312 if ctxt.HeadType == objabi.Hwindows {
1313 // use gcc linker script to work around gcc bug
1314 // (see https://golang.org/issue/20183 for details).
1315 p := writeGDBLinkerScript()
1316 argv = append(argv, "-Wl,-T,"+p)
1317 // libmingw32 and libmingwex have some inter-dependencies,
1318 // so must use linker groups.
1319 argv = append(argv, "-Wl,--start-group", "-lmingwex", "-lmingw32", "-Wl,--end-group")
1320 argv = append(argv, peimporteddlls()...)
1323 if ctxt.Debugvlog != 0 {
1324 ctxt.Logf("%5.2f host link:", Cputime())
1325 for _, v := range argv {
1331 if out, err := exec.Command(argv[0], argv[1:]...).CombinedOutput(); err != nil {
1332 Exitf("running %s failed: %v\n%s", argv[0], err, out)
1333 } else if len(out) > 0 {
1334 // always print external output even if the command is successful, so that we don't
1335 // swallow linker warnings (see https://golang.org/issue/17935).
1336 ctxt.Logf("%s", out)
1339 if !*FlagS && !*FlagW && !debug_s && ctxt.HeadType == objabi.Hdarwin {
1340 dsym := filepath.Join(*flagTmpdir, "go.dwarf")
1341 if out, err := exec.Command("dsymutil", "-f", *flagOutfile, "-o", dsym).CombinedOutput(); err != nil {
1342 Exitf("%s: running dsymutil failed: %v\n%s", os.Args[0], err, out)
1344 // Skip combining if `dsymutil` didn't generate a file. See #11994.
1345 if _, err := os.Stat(dsym); os.IsNotExist(err) {
1348 // For os.Rename to work reliably, must be in same directory as outfile.
1349 combinedOutput := *flagOutfile + "~"
1350 isIOS, err := machoCombineDwarf(*flagOutfile, dsym, combinedOutput, ctxt.BuildMode)
1352 Exitf("%s: combining dwarf failed: %v", os.Args[0], err)
1355 os.Remove(*flagOutfile)
1356 if err := os.Rename(combinedOutput, *flagOutfile); err != nil {
1357 Exitf("%s: %v", os.Args[0], err)
1363 // hostlinkArchArgs returns arguments to pass to the external linker
1364 // based on the architecture.
1365 func hostlinkArchArgs(arch *sys.Arch) []string {
1366 switch arch.Family {
1368 return []string{"-m32"}
1369 case sys.AMD64, sys.PPC64, sys.S390X:
1370 return []string{"-m64"}
1372 return []string{"-marm"}
1376 return []string{"-mabi=64"}
1378 return []string{"-mabi=32"}
1383 // ldobj loads an input object. If it is a host object (an object
1384 // compiled by a non-Go compiler) it returns the Hostobj pointer. If
1385 // it is a Go object, it returns nil.
1386 func ldobj(ctxt *Link, f *bio.Reader, lib *sym.Library, length int64, pn string, file string) *Hostobj {
1387 pkg := objabi.PathToPrefix(lib.Pkg)
1389 eof := f.Offset() + length
1397 magic := uint32(c1)<<24 | uint32(c2)<<16 | uint32(c3)<<8 | uint32(c4)
1398 if magic == 0x7f454c46 { // \x7F E L F
1399 ldelf := func(ctxt *Link, f *bio.Reader, pkg string, length int64, pn string) {
1400 textp, flags, err := loadelf.Load(ctxt.Arch, ctxt.Syms, f, pkg, length, pn, ehdr.flags)
1402 Errorf(nil, "%v", err)
1406 ctxt.Textp = append(ctxt.Textp, textp...)
1408 return ldhostobj(ldelf, ctxt.HeadType, f, pkg, length, pn, file)
1411 if magic&^1 == 0xfeedface || magic&^0x01000000 == 0xcefaedfe {
1412 ldmacho := func(ctxt *Link, f *bio.Reader, pkg string, length int64, pn string) {
1413 textp, err := loadmacho.Load(ctxt.Arch, ctxt.Syms, f, pkg, length, pn)
1415 Errorf(nil, "%v", err)
1418 ctxt.Textp = append(ctxt.Textp, textp...)
1420 return ldhostobj(ldmacho, ctxt.HeadType, f, pkg, length, pn, file)
1423 if c1 == 0x4c && c2 == 0x01 || c1 == 0x64 && c2 == 0x86 {
1424 ldpe := func(ctxt *Link, f *bio.Reader, pkg string, length int64, pn string) {
1425 textp, rsrc, err := loadpe.Load(ctxt.Arch, ctxt.Syms, f, pkg, length, pn)
1427 Errorf(nil, "%v", err)
1431 setpersrc(ctxt, rsrc)
1433 ctxt.Textp = append(ctxt.Textp, textp...)
1435 return ldhostobj(ldpe, ctxt.HeadType, f, pkg, length, pn, file)
1438 /* check the header */
1439 line, err := f.ReadString('\n')
1441 Errorf(nil, "truncated object file: %s: %v", pn, err)
1445 if !strings.HasPrefix(line, "go object ") {
1446 if strings.HasSuffix(pn, ".go") {
1447 Exitf("%s: uncompiled .go source file", pn)
1451 if line == ctxt.Arch.Name {
1452 // old header format: just $GOOS
1453 Errorf(nil, "%s: stale object file", pn)
1457 Errorf(nil, "%s: not an object file", pn)
1461 // First, check that the basic GOOS, GOARCH, and Version match.
1462 t := fmt.Sprintf("%s %s %s ", objabi.GOOS, objabi.GOARCH, objabi.Version)
1464 line = strings.TrimRight(line, "\n")
1465 if !strings.HasPrefix(line[10:]+" ", t) && !*flagF {
1466 Errorf(nil, "%s: object is [%s] expected [%s]", pn, line[10:], t)
1470 // Second, check that longer lines match each other exactly,
1471 // so that the Go compiler and write additional information
1472 // that must be the same from run to run.
1473 if len(line) >= len(t)+10 {
1476 } else if theline != line[10:] {
1477 Errorf(nil, "%s: object is [%s] expected [%s]", pn, line[10:], theline)
1482 // Skip over exports and other info -- ends with \n!\n.
1484 // Note: It's possible for "\n!\n" to appear within the binary
1485 // package export data format. To avoid truncating the package
1486 // definition prematurely (issue 21703), we keep keep track of
1487 // how many "$$" delimiters we've seen.
1489 import0 := f.Offset()
1491 c1 = '\n' // the last line ended in \n
1497 if markers%2 == 0 && c2 == '!' && c3 == '\n' {
1500 if c2 == '$' && c3 == '$' {
1509 Errorf(nil, "truncated object file: %s", pn)
1514 import1 := f.Offset()
1517 ldpkg(ctxt, f, lib, import1-import0-2, pn) // -2 for !\n
1520 objfile.Load(ctxt.Arch, ctxt.Syms, f, lib, eof-f.Offset(), pn)
1521 addImports(ctxt, lib, pn)
1525 func readelfsymboldata(ctxt *Link, f *elf.File, sym *elf.Symbol) []byte {
1526 data := make([]byte, sym.Size)
1527 sect := f.Sections[sym.Section]
1528 if sect.Type != elf.SHT_PROGBITS && sect.Type != elf.SHT_NOTE {
1529 Errorf(nil, "reading %s from non-data section", sym.Name)
1531 n, err := sect.ReadAt(data, int64(sym.Value-sect.Addr))
1532 if uint64(n) != sym.Size {
1533 Errorf(nil, "reading contents of %s: %v", sym.Name, err)
1538 func readwithpad(r io.Reader, sz int32) ([]byte, error) {
1539 data := make([]byte, Rnd(int64(sz), 4))
1540 _, err := io.ReadFull(r, data)
1548 func readnote(f *elf.File, name []byte, typ int32) ([]byte, error) {
1549 for _, sect := range f.Sections {
1550 if sect.Type != elf.SHT_NOTE {
1555 var namesize, descsize, noteType int32
1556 err := binary.Read(r, f.ByteOrder, &namesize)
1561 return nil, fmt.Errorf("read namesize failed: %v", err)
1563 err = binary.Read(r, f.ByteOrder, &descsize)
1565 return nil, fmt.Errorf("read descsize failed: %v", err)
1567 err = binary.Read(r, f.ByteOrder, ¬eType)
1569 return nil, fmt.Errorf("read type failed: %v", err)
1571 noteName, err := readwithpad(r, namesize)
1573 return nil, fmt.Errorf("read name failed: %v", err)
1575 desc, err := readwithpad(r, descsize)
1577 return nil, fmt.Errorf("read desc failed: %v", err)
1579 if string(name) == string(noteName) && typ == noteType {
1587 func findshlib(ctxt *Link, shlib string) string {
1588 if filepath.IsAbs(shlib) {
1591 for _, libdir := range ctxt.Libdir {
1592 libpath := filepath.Join(libdir, shlib)
1593 if _, err := os.Stat(libpath); err == nil {
1597 Errorf(nil, "cannot find shared library: %s", shlib)
1601 func ldshlibsyms(ctxt *Link, shlib string) {
1603 if filepath.IsAbs(shlib) {
1605 shlib = filepath.Base(shlib)
1607 libpath = findshlib(ctxt, shlib)
1612 for _, processedlib := range ctxt.Shlibs {
1613 if processedlib.Path == libpath {
1617 if ctxt.Debugvlog > 1 {
1618 ctxt.Logf("%5.2f ldshlibsyms: found library with name %s at %s\n", Cputime(), shlib, libpath)
1621 f, err := elf.Open(libpath)
1623 Errorf(nil, "cannot open shared library: %s", libpath)
1628 hash, err := readnote(f, ELF_NOTE_GO_NAME, ELF_NOTE_GOABIHASH_TAG)
1630 Errorf(nil, "cannot read ABI hash from shared library %s: %v", libpath, err)
1634 depsbytes, err := readnote(f, ELF_NOTE_GO_NAME, ELF_NOTE_GODEPS_TAG)
1636 Errorf(nil, "cannot read dep list from shared library %s: %v", libpath, err)
1640 for _, dep := range strings.Split(string(depsbytes), "\n") {
1644 if !filepath.IsAbs(dep) {
1645 // If the dep can be interpreted as a path relative to the shlib
1646 // in which it was found, do that. Otherwise, we will leave it
1647 // to be resolved by libdir lookup.
1648 abs := filepath.Join(filepath.Dir(libpath), dep)
1649 if _, err := os.Stat(abs); err == nil {
1653 deps = append(deps, dep)
1656 syms, err := f.DynamicSymbols()
1658 Errorf(nil, "cannot read symbols from shared library: %s", libpath)
1661 gcdataLocations := make(map[uint64]*sym.Symbol)
1662 for _, elfsym := range syms {
1663 if elf.ST_TYPE(elfsym.Info) == elf.STT_NOTYPE || elf.ST_TYPE(elfsym.Info) == elf.STT_SECTION {
1666 lsym := ctxt.Syms.Lookup(elfsym.Name, 0)
1667 // Because loadlib above loads all .a files before loading any shared
1668 // libraries, any non-dynimport symbols we find that duplicate symbols
1669 // already loaded should be ignored (the symbols from the .a files
1671 if lsym.Type != 0 && lsym.Type != sym.SDYNIMPORT {
1674 lsym.Type = sym.SDYNIMPORT
1675 lsym.ElfType = elf.ST_TYPE(elfsym.Info)
1676 lsym.Size = int64(elfsym.Size)
1677 if elfsym.Section != elf.SHN_UNDEF {
1678 // Set .File for the library that actually defines the symbol.
1680 // The decodetype_* functions in decodetype.go need access to
1682 if strings.HasPrefix(lsym.Name, "type.") && !strings.HasPrefix(lsym.Name, "type..") {
1683 lsym.P = readelfsymboldata(ctxt, f, &elfsym)
1684 gcdataLocations[elfsym.Value+2*uint64(ctxt.Arch.PtrSize)+8+1*uint64(ctxt.Arch.PtrSize)] = lsym
1688 gcdataAddresses := make(map[*sym.Symbol]uint64)
1689 if ctxt.Arch.Family == sys.ARM64 {
1690 for _, sect := range f.Sections {
1691 if sect.Type == elf.SHT_RELA {
1695 err := binary.Read(rdr, f.ByteOrder, &rela)
1698 } else if err != nil {
1699 Errorf(nil, "reading relocation failed %v", err)
1702 t := elf.R_AARCH64(rela.Info & 0xffff)
1703 if t != elf.R_AARCH64_RELATIVE {
1706 if lsym, ok := gcdataLocations[rela.Off]; ok {
1707 gcdataAddresses[lsym] = uint64(rela.Addend)
1714 ctxt.Shlibs = append(ctxt.Shlibs, Shlib{Path: libpath, Hash: hash, Deps: deps, File: f, gcdataAddresses: gcdataAddresses})
1717 func addsection(arch *sys.Arch, seg *sym.Segment, name string, rwx int) *sym.Section {
1718 sect := new(sym.Section)
1719 sect.Rwx = uint8(rwx)
1722 sect.Align = int32(arch.PtrSize) // everything is at least pointer-aligned
1723 seg.Sections = append(seg.Sections, sect)
1727 func Le16(b []byte) uint16 {
1728 return uint16(b[0]) | uint16(b[1])<<8
1731 func Le32(b []byte) uint32 {
1732 return uint32(b[0]) | uint32(b[1])<<8 | uint32(b[2])<<16 | uint32(b[3])<<24
1735 func Le64(b []byte) uint64 {
1736 return uint64(Le32(b)) | uint64(Le32(b[4:]))<<32
1739 func Be16(b []byte) uint16 {
1740 return uint16(b[0])<<8 | uint16(b[1])
1743 func Be32(b []byte) uint32 {
1744 return uint32(b[0])<<24 | uint32(b[1])<<16 | uint32(b[2])<<8 | uint32(b[3])
1750 limit int // limit on entry to sym
1753 var morestack *sym.Symbol
1755 // TODO: Record enough information in new object files to
1756 // allow stack checks here.
1758 func haslinkregister(ctxt *Link) bool {
1759 return ctxt.FixedFrameSize() != 0
1762 func callsize(ctxt *Link) int {
1763 if haslinkregister(ctxt) {
1766 return ctxt.Arch.RegSize
1769 func (ctxt *Link) dostkcheck() {
1772 morestack = ctxt.Syms.Lookup("runtime.morestack", 0)
1774 // Every splitting function ensures that there are at least StackLimit
1775 // bytes available below SP when the splitting prologue finishes.
1776 // If the splitting function calls F, then F begins execution with
1777 // at least StackLimit - callsize() bytes available.
1778 // Check that every function behaves correctly with this amount
1779 // of stack, following direct calls in order to piece together chains
1780 // of non-splitting functions.
1783 ch.limit = objabi.StackLimit - callsize(ctxt)
1785 // Check every function, but do the nosplit functions in a first pass,
1786 // to make the printed failure chains as short as possible.
1787 for _, s := range ctxt.Textp {
1788 // runtime.racesymbolizethunk is called from gcc-compiled C
1789 // code running on the operating system thread stack.
1790 // It uses more than the usual amount of stack but that's okay.
1791 if s.Name == "runtime.racesymbolizethunk" {
1795 if s.Attr.NoSplit() {
1797 stkcheck(ctxt, &ch, 0)
1801 for _, s := range ctxt.Textp {
1802 if !s.Attr.NoSplit() {
1804 stkcheck(ctxt, &ch, 0)
1809 func stkcheck(ctxt *Link, up *chain, depth int) int {
1813 // Don't duplicate work: only need to consider each
1814 // function at top of safe zone once.
1815 top := limit == objabi.StackLimit-callsize(ctxt)
1817 if s.Attr.StackCheck() {
1820 s.Attr |= sym.AttrStackCheck
1824 Errorf(s, "nosplit stack check too deep")
1825 stkbroke(ctxt, up, 0)
1829 if s.Attr.External() || s.FuncInfo == nil {
1830 // external function.
1831 // should never be called directly.
1832 // onlyctxt.Diagnose the direct caller.
1833 // TODO(mwhudson): actually think about this.
1834 // TODO(khr): disabled for now. Calls to external functions can only happen on the g0 stack.
1835 // See the trampolines in src/runtime/sys_darwin_$ARCH.go.
1836 if depth == 1 && s.Type != sym.SXREF && !ctxt.DynlinkingGo() &&
1837 ctxt.BuildMode != BuildModeCArchive && ctxt.BuildMode != BuildModePIE && ctxt.BuildMode != BuildModeCShared && ctxt.BuildMode != BuildModePlugin {
1838 //Errorf(s, "call to external function")
1844 stkbroke(ctxt, up, limit)
1848 // morestack looks like it calls functions,
1849 // but it switches the stack pointer first.
1857 if !s.Attr.NoSplit() {
1858 // Ensure we have enough stack to call morestack.
1859 ch.limit = limit - callsize(ctxt)
1861 if stkcheck(ctxt, &ch, depth+1) < 0 {
1867 // Raise limit to allow frame.
1869 if s.FuncInfo != nil {
1870 locals = s.FuncInfo.Locals
1872 limit = int(objabi.StackLimit+locals) + int(ctxt.FixedFrameSize())
1875 // Walk through sp adjustments in function, consuming relocs.
1882 for pciterinit(ctxt, &pcsp, &s.FuncInfo.Pcsp); pcsp.done == 0; pciternext(&pcsp) {
1883 // pcsp.value is in effect for [pcsp.pc, pcsp.nextpc).
1885 // Check stack size in effect for this span.
1886 if int32(limit)-pcsp.value < 0 {
1887 stkbroke(ctxt, up, int(int32(limit)-pcsp.value))
1891 // Process calls in this span.
1892 for ; ri < endr && uint32(s.R[ri].Off) < pcsp.nextpc; ri++ {
1896 case objabi.R_CALL, objabi.R_CALLARM, objabi.R_CALLARM64, objabi.R_CALLPOWER, objabi.R_CALLMIPS:
1897 ch.limit = int(int32(limit) - pcsp.value - int32(callsize(ctxt)))
1899 if stkcheck(ctxt, &ch, depth+1) < 0 {
1903 // Indirect call. Assume it is a call to a splitting function,
1904 // so we have to make sure it can call morestack.
1905 // Arrange the data structures to report both calls, so that
1906 // if there is an error, stkprint shows all the steps involved.
1907 case objabi.R_CALLIND:
1908 ch.limit = int(int32(limit) - pcsp.value - int32(callsize(ctxt)))
1911 ch1.limit = ch.limit - callsize(ctxt) // for morestack in called prologue
1914 if stkcheck(ctxt, &ch1, depth+2) < 0 {
1924 func stkbroke(ctxt *Link, ch *chain, limit int) {
1925 Errorf(ch.sym, "nosplit stack overflow")
1926 stkprint(ctxt, ch, limit)
1929 func stkprint(ctxt *Link, ch *chain, limit int) {
1934 if ch.sym.Attr.NoSplit() {
1935 name += " (nosplit)"
1938 name = "function pointer"
1942 // top of chain. ch->sym != nil.
1943 if ch.sym.Attr.NoSplit() {
1944 fmt.Printf("\t%d\tassumed on entry to %s\n", ch.limit, name)
1946 fmt.Printf("\t%d\tguaranteed after split check in %s\n", ch.limit, name)
1949 stkprint(ctxt, ch.up, ch.limit+callsize(ctxt))
1950 if !haslinkregister(ctxt) {
1951 fmt.Printf("\t%d\ton entry to %s\n", ch.limit, name)
1955 if ch.limit != limit {
1956 fmt.Printf("\t%d\tafter %s uses %d\n", limit, name, ch.limit-limit)
1961 fmt.Fprintf(os.Stderr, "usage: link [options] main.o\n")
1962 objabi.Flagprint(os.Stderr)
1966 type SymbolType int8
1969 // see also http://9p.io/magic/man2html/1/nm
1970 TextSym SymbolType = 'T'
1971 DataSym SymbolType = 'D'
1972 BSSSym SymbolType = 'B'
1973 UndefinedSym SymbolType = 'U'
1974 TLSSym SymbolType = 't'
1975 FrameSym SymbolType = 'm'
1976 ParamSym SymbolType = 'p'
1977 AutoSym SymbolType = 'a'
1979 // Deleted auto (not a real sym, just placeholder for type)
1980 DeletedAutoSym = 'x'
1983 func genasmsym(ctxt *Link, put func(*Link, *sym.Symbol, string, SymbolType, int64, *sym.Symbol)) {
1984 // These symbols won't show up in the first loop below because we
1985 // skip sym.STEXT symbols. Normal sym.STEXT symbols are emitted by walking textp.
1986 s := ctxt.Syms.Lookup("runtime.text", 0)
1987 if s.Type == sym.STEXT {
1988 // We've already included this symbol in ctxt.Textp
1989 // if ctxt.DynlinkingGo() && ctxt.HeadType == objabi.Hdarwin.
1990 // See data.go:/textaddress
1991 if !(ctxt.DynlinkingGo() && ctxt.HeadType == objabi.Hdarwin) {
1992 put(ctxt, s, s.Name, TextSym, s.Value, nil)
1998 // Generate base addresses for all text sections if there are multiple
1999 for _, sect := range Segtext.Sections {
2004 if sect.Name != ".text" {
2007 s = ctxt.Syms.ROLookup(fmt.Sprintf("runtime.text.%d", n), 0)
2011 if s.Type == sym.STEXT {
2012 put(ctxt, s, s.Name, TextSym, s.Value, nil)
2017 s = ctxt.Syms.Lookup("runtime.etext", 0)
2018 if s.Type == sym.STEXT {
2019 // We've already included this symbol in ctxt.Textp
2020 // if ctxt.DynlinkingGo() && ctxt.HeadType == objabi.Hdarwin.
2021 // See data.go:/textaddress
2022 if !(ctxt.DynlinkingGo() && ctxt.HeadType == objabi.Hdarwin) {
2023 put(ctxt, s, s.Name, TextSym, s.Value, nil)
2027 for _, s := range ctxt.Syms.Allsym {
2028 if s.Attr.NotInSymbolTable() {
2031 if (s.Name == "" || s.Name[0] == '.') && s.Version == 0 && s.Name != ".rathole" && s.Name != ".TOC." {
2058 if !s.Attr.Reachable() {
2061 put(ctxt, s, s.Name, DataSym, Symaddr(s), s.Gotype)
2063 case sym.SBSS, sym.SNOPTRBSS:
2064 if !s.Attr.Reachable() {
2068 Errorf(s, "should not be bss (size=%d type=%v special=%v)", len(s.P), s.Type, s.Attr.Special())
2070 put(ctxt, s, s.Name, BSSSym, Symaddr(s), s.Gotype)
2073 if ctxt.HeadType == objabi.Hwindows || ctxt.IsELF {
2074 put(ctxt, s, s.Name, UndefinedSym, s.Value, nil)
2077 case sym.SDYNIMPORT:
2078 if !s.Attr.Reachable() {
2081 put(ctxt, s, s.Extname, UndefinedSym, 0, nil)
2084 if ctxt.LinkMode == LinkExternal {
2085 put(ctxt, s, s.Name, TLSSym, Symaddr(s), s.Gotype)
2091 for _, s := range ctxt.Textp {
2092 put(ctxt, s, s.Name, TextSym, s.Value, s.Gotype)
2095 if s.FuncInfo != nil {
2096 locals = s.FuncInfo.Locals
2098 // NOTE(ality): acid can't produce a stack trace without .frame symbols
2099 put(ctxt, nil, ".frame", FrameSym, int64(locals)+int64(ctxt.Arch.PtrSize), nil)
2101 if s.FuncInfo == nil {
2104 for _, a := range s.FuncInfo.Autom {
2105 if a.Name == objabi.A_DELETED_AUTO {
2106 put(ctxt, nil, "", DeletedAutoSym, 0, a.Gotype)
2110 // Emit a or p according to actual offset, even if label is wrong.
2111 // This avoids negative offsets, which cannot be encoded.
2112 if a.Name != objabi.A_AUTO && a.Name != objabi.A_PARAM {
2116 // compute offset relative to FP
2117 if a.Name == objabi.A_PARAM {
2120 off = a.Aoffset - int32(ctxt.Arch.PtrSize)
2125 put(ctxt, nil, a.Asym.Name, ParamSym, int64(off), a.Gotype)
2130 if off <= int32(-ctxt.Arch.PtrSize) {
2131 put(ctxt, nil, a.Asym.Name, AutoSym, -(int64(off) + int64(ctxt.Arch.PtrSize)), a.Gotype)
2134 // Otherwise, off is addressing the saved program counter.
2135 // Something underhanded is going on. Say nothing.
2139 if ctxt.Debugvlog != 0 || *flagN {
2140 ctxt.Logf("%5.2f symsize = %d\n", Cputime(), uint32(Symsize))
2144 func Symaddr(s *sym.Symbol) int64 {
2145 if !s.Attr.Reachable() {
2146 Errorf(s, "unreachable symbol in symaddr")
2151 func (ctxt *Link) xdefine(p string, t sym.SymKind, v int64) {
2152 s := ctxt.Syms.Lookup(p, 0)
2155 s.Attr |= sym.AttrReachable
2156 s.Attr |= sym.AttrSpecial
2157 s.Attr |= sym.AttrLocal
2160 func datoff(s *sym.Symbol, addr int64) int64 {
2161 if uint64(addr) >= Segdata.Vaddr {
2162 return int64(uint64(addr) - Segdata.Vaddr + Segdata.Fileoff)
2164 if uint64(addr) >= Segtext.Vaddr {
2165 return int64(uint64(addr) - Segtext.Vaddr + Segtext.Fileoff)
2167 Errorf(s, "invalid datoff %#x", addr)
2171 func Entryvalue(ctxt *Link) int64 {
2172 a := *flagEntrySymbol
2173 if a[0] >= '0' && a[0] <= '9' {
2176 s := ctxt.Syms.Lookup(a, 0)
2178 return *FlagTextAddr
2180 if s.Type != sym.STEXT {
2181 Errorf(s, "entry not text")
2186 func undefsym(ctxt *Link, s *sym.Symbol) {
2189 for i := 0; i < len(s.R); i++ {
2191 if r.Sym == nil { // happens for some external ARM relocs
2194 // TODO(mwhudson): the test of VisibilityHidden here probably doesn't make
2195 // sense and should be removed when someone has thought about it properly.
2196 if (r.Sym.Type == sym.Sxxx || r.Sym.Type == sym.SXREF) && !r.Sym.Attr.VisibilityHidden() {
2197 Errorf(s, "undefined: %q", r.Sym.Name)
2199 if !r.Sym.Attr.Reachable() && r.Type != objabi.R_WEAKADDROFF {
2200 Errorf(s, "relocation target %q", r.Sym.Name)
2205 func (ctxt *Link) undef() {
2206 for _, s := range ctxt.Textp {
2209 for _, s := range datap {
2217 func (ctxt *Link) callgraph() {
2224 for _, s := range ctxt.Textp {
2225 for i = 0; i < len(s.R); i++ {
2230 if (r.Type == objabi.R_CALL || r.Type == objabi.R_CALLARM || r.Type == objabi.R_CALLPOWER || r.Type == objabi.R_CALLMIPS) && r.Sym.Type == sym.STEXT {
2231 ctxt.Logf("%s calls %s\n", s.Name, r.Sym.Name)
2237 func Rnd(v int64, r int64) int64 {
2250 func bgetc(r *bio.Reader) int {
2251 c, err := r.ReadByte()
2254 log.Fatalf("reading input: %v", err)
2261 type markKind uint8 // for postorder traversal
2268 func postorder(libs []*sym.Library) []*sym.Library {
2269 order := make([]*sym.Library, 0, len(libs)) // hold the result
2270 mark := make(map[*sym.Library]markKind, len(libs))
2271 for _, lib := range libs {
2272 dfs(lib, mark, &order)
2277 func dfs(lib *sym.Library, mark map[*sym.Library]markKind, order *[]*sym.Library) {
2278 if mark[lib] == visited {
2281 if mark[lib] == visiting {
2282 panic("found import cycle while visiting " + lib.Pkg)
2284 mark[lib] = visiting
2285 for _, i := range lib.Imports {
2289 *order = append(*order, lib)