github.com/gagliardetto/golang-go@v0.0.0-20201020153340-53909ea70814/cmd/link/internal/ld/link.go (about)

     1  // Derived from Inferno utils/6l/l.h and related files.
     2  // https://bitbucket.org/inferno-os/inferno-os/src/default/utils/6l/l.h
     3  //
     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.
    12  //
    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:
    19  //
    20  // The above copyright notice and this permission notice shall be included in
    21  // all copies or substantial portions of the Software.
    22  //
    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
    29  // THE SOFTWARE.
    30  
    31  package ld
    32  
    33  import (
    34  	"bufio"
    35  	"github.com/gagliardetto/golang-go/cmd/internal/obj"
    36  	"github.com/gagliardetto/golang-go/cmd/internal/objabi"
    37  	"github.com/gagliardetto/golang-go/cmd/internal/sys"
    38  	"github.com/gagliardetto/golang-go/cmd/link/internal/loader"
    39  	"github.com/gagliardetto/golang-go/cmd/link/internal/sym"
    40  	"debug/elf"
    41  	"fmt"
    42  )
    43  
    44  type Shlib struct {
    45  	Path            string
    46  	Hash            []byte
    47  	Deps            []string
    48  	File            *elf.File
    49  	gcdataAddresses map[*sym.Symbol]uint64
    50  }
    51  
    52  // Link holds the context for writing object code from a compiler
    53  // or for reading that input into the linker.
    54  type Link struct {
    55  	Out *OutBuf
    56  
    57  	Syms *sym.Symbols
    58  
    59  	Arch      *sys.Arch
    60  	Debugvlog int
    61  	Bso       *bufio.Writer
    62  
    63  	Loaded bool // set after all inputs have been loaded as symbols
    64  
    65  	IsELF    bool
    66  	HeadType objabi.HeadType
    67  
    68  	linkShared    bool // link against installed Go shared libraries
    69  	LinkMode      LinkMode
    70  	BuildMode     BuildMode
    71  	canUsePlugins bool // initialized when Loaded is set to true
    72  	compressDWARF bool
    73  
    74  	Tlsg         *sym.Symbol
    75  	Libdir       []string
    76  	Library      []*sym.Library
    77  	LibraryByPkg map[string]*sym.Library
    78  	Shlibs       []Shlib
    79  	Tlsoffset    int
    80  	Textp        []*sym.Symbol
    81  	Filesyms     []*sym.Symbol
    82  	Moduledata   *sym.Symbol
    83  
    84  	PackageFile  map[string]string
    85  	PackageShlib map[string]string
    86  
    87  	tramps []*sym.Symbol // trampolines
    88  
    89  	// unresolvedSymSet is a set of erroneous unresolved references.
    90  	// Used to avoid duplicated error messages.
    91  	unresolvedSymSet map[unresolvedSymKey]bool
    92  
    93  	// Used to implement field tracking.
    94  	Reachparent map[*sym.Symbol]*sym.Symbol
    95  
    96  	compUnits []*sym.CompilationUnit // DWARF compilation units
    97  	runtimeCU *sym.CompilationUnit   // One of the runtime CUs, the last one seen.
    98  
    99  	relocbuf []byte // temporary buffer for applying relocations
   100  
   101  	loader  *loader.Loader
   102  	cgodata []cgodata // cgo directives to load, three strings are args for loadcgo
   103  
   104  	cgo_export_static  map[string]bool
   105  	cgo_export_dynamic map[string]bool
   106  }
   107  
   108  type cgodata struct {
   109  	file       string
   110  	pkg        string
   111  	directives [][]string
   112  }
   113  
   114  type unresolvedSymKey struct {
   115  	from *sym.Symbol // Symbol that referenced unresolved "to"
   116  	to   *sym.Symbol // Unresolved symbol referenced by "from"
   117  }
   118  
   119  // ErrorUnresolved prints unresolved symbol error for r.Sym that is referenced from s.
   120  func (ctxt *Link) ErrorUnresolved(s *sym.Symbol, r *sym.Reloc) {
   121  	if ctxt.unresolvedSymSet == nil {
   122  		ctxt.unresolvedSymSet = make(map[unresolvedSymKey]bool)
   123  	}
   124  
   125  	k := unresolvedSymKey{from: s, to: r.Sym}
   126  	if !ctxt.unresolvedSymSet[k] {
   127  		ctxt.unresolvedSymSet[k] = true
   128  
   129  		// Try to find symbol under another ABI.
   130  		var reqABI, haveABI obj.ABI
   131  		haveABI = ^obj.ABI(0)
   132  		reqABI, ok := sym.VersionToABI(int(r.Sym.Version))
   133  		if ok {
   134  			for abi := obj.ABI(0); abi < obj.ABICount; abi++ {
   135  				v := sym.ABIToVersion(abi)
   136  				if v == -1 {
   137  					continue
   138  				}
   139  				if rs := ctxt.Syms.ROLookup(r.Sym.Name, v); rs != nil && rs.Type != sym.Sxxx {
   140  					haveABI = abi
   141  				}
   142  			}
   143  		}
   144  
   145  		// Give a special error message for main symbol (see #24809).
   146  		if r.Sym.Name == "main.main" {
   147  			Errorf(s, "function main is undeclared in the main package")
   148  		} else if haveABI != ^obj.ABI(0) {
   149  			Errorf(s, "relocation target %s not defined for %s (but is defined for %s)", r.Sym.Name, reqABI, haveABI)
   150  		} else {
   151  			Errorf(s, "relocation target %s not defined", r.Sym.Name)
   152  		}
   153  	}
   154  }
   155  
   156  // The smallest possible offset from the hardware stack pointer to a local
   157  // variable on the stack. Architectures that use a link register save its value
   158  // on the stack in the function prologue and so always have a pointer between
   159  // the hardware stack pointer and the local variable area.
   160  func (ctxt *Link) FixedFrameSize() int64 {
   161  	switch ctxt.Arch.Family {
   162  	case sys.AMD64, sys.I386:
   163  		return 0
   164  	case sys.PPC64:
   165  		// PIC code on ppc64le requires 32 bytes of stack, and it's easier to
   166  		// just use that much stack always on ppc64x.
   167  		return int64(4 * ctxt.Arch.PtrSize)
   168  	default:
   169  		return int64(ctxt.Arch.PtrSize)
   170  	}
   171  }
   172  
   173  func (ctxt *Link) Logf(format string, args ...interface{}) {
   174  	fmt.Fprintf(ctxt.Bso, format, args...)
   175  	ctxt.Bso.Flush()
   176  }
   177  
   178  func addImports(ctxt *Link, l *sym.Library, pn string) {
   179  	pkg := objabi.PathToPrefix(l.Pkg)
   180  	for _, importStr := range l.ImportStrings {
   181  		lib := addlib(ctxt, pkg, pn, importStr)
   182  		if lib != nil {
   183  			l.Imports = append(l.Imports, lib)
   184  		}
   185  	}
   186  	l.ImportStrings = nil
   187  }