Black Lives Matter. Support the Equal Justice Initiative.

Source file src/cmd/link/internal/ld/link.go

Documentation: cmd/link/internal/ld

     1  // Derived from Inferno utils/6l/l.h and related files.
     2  // https://bitbucket.org/inferno-os/inferno-os/src/master/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  	"cmd/internal/objabi"
    36  	"cmd/internal/sys"
    37  	"cmd/link/internal/loader"
    38  	"cmd/link/internal/sym"
    39  	"debug/elf"
    40  	"fmt"
    41  )
    42  
    43  type Shlib struct {
    44  	Path string
    45  	Hash []byte
    46  	Deps []string
    47  	File *elf.File
    48  }
    49  
    50  // Link holds the context for writing object code from a compiler
    51  // or for reading that input into the linker.
    52  type Link struct {
    53  	Target
    54  	ErrorReporter
    55  	ArchSyms
    56  
    57  	outSem chan int // limits the number of output writers
    58  	Out    *OutBuf
    59  
    60  	version int // current version number for static/file-local symbols
    61  
    62  	Debugvlog int
    63  	Bso       *bufio.Writer
    64  
    65  	Loaded bool // set after all inputs have been loaded as symbols
    66  
    67  	compressDWARF bool
    68  
    69  	Libdir       []string
    70  	Library      []*sym.Library
    71  	LibraryByPkg map[string]*sym.Library
    72  	Shlibs       []Shlib
    73  	Textp        []loader.Sym
    74  	Moduledata   loader.Sym
    75  
    76  	PackageFile  map[string]string
    77  	PackageShlib map[string]string
    78  
    79  	tramps []loader.Sym // trampolines
    80  
    81  	compUnits []*sym.CompilationUnit // DWARF compilation units
    82  	runtimeCU *sym.CompilationUnit   // One of the runtime CUs, the last one seen.
    83  
    84  	loader  *loader.Loader
    85  	cgodata []cgodata // cgo directives to load, three strings are args for loadcgo
    86  
    87  	cgo_export_static  map[string]bool
    88  	cgo_export_dynamic map[string]bool
    89  
    90  	datap  []loader.Sym
    91  	dynexp []loader.Sym
    92  
    93  	// Elf symtab variables.
    94  	numelfsym int // starts at 0, 1 is reserved
    95  
    96  	// These are symbols that created and written by the linker.
    97  	// Rather than creating a symbol, and writing all its data into the heap,
    98  	// you can create a symbol, and just a generation function will be called
    99  	// after the symbol's been created in the output mmap.
   100  	generatorSyms map[loader.Sym]generatorFunc
   101  }
   102  
   103  type cgodata struct {
   104  	file       string
   105  	pkg        string
   106  	directives [][]string
   107  }
   108  
   109  // The smallest possible offset from the hardware stack pointer to a local
   110  // variable on the stack. Architectures that use a link register save its value
   111  // on the stack in the function prologue and so always have a pointer between
   112  // the hardware stack pointer and the local variable area.
   113  func (ctxt *Link) FixedFrameSize() int64 {
   114  	switch ctxt.Arch.Family {
   115  	case sys.AMD64, sys.I386:
   116  		return 0
   117  	case sys.PPC64:
   118  		// PIC code on ppc64le requires 32 bytes of stack, and it's easier to
   119  		// just use that much stack always on ppc64x.
   120  		return int64(4 * ctxt.Arch.PtrSize)
   121  	default:
   122  		return int64(ctxt.Arch.PtrSize)
   123  	}
   124  }
   125  
   126  func (ctxt *Link) Logf(format string, args ...interface{}) {
   127  	fmt.Fprintf(ctxt.Bso, format, args...)
   128  	ctxt.Bso.Flush()
   129  }
   130  
   131  func addImports(ctxt *Link, l *sym.Library, pn string) {
   132  	pkg := objabi.PathToPrefix(l.Pkg)
   133  	for _, imp := range l.Autolib {
   134  		lib := addlib(ctxt, pkg, pn, imp.Pkg, imp.Fingerprint)
   135  		if lib != nil {
   136  			l.Imports = append(l.Imports, lib)
   137  		}
   138  	}
   139  	l.Autolib = nil
   140  }
   141  
   142  // Allocate a new version (i.e. symbol namespace).
   143  func (ctxt *Link) IncVersion() int {
   144  	ctxt.version++
   145  	return ctxt.version - 1
   146  }
   147  
   148  // returns the maximum version number
   149  func (ctxt *Link) MaxVersion() int {
   150  	return ctxt.version
   151  }
   152  
   153  // generatorFunc is a convenience type.
   154  // Linker created symbols that are large, and shouldn't really live in the
   155  // heap can define a generator function, and their bytes can be generated
   156  // directly in the output mmap.
   157  //
   158  // Generator symbols shouldn't grow the symbol size, and might be called in
   159  // parallel in the future.
   160  //
   161  // Generator Symbols have their Data set to the mmapped area when the
   162  // generator is called.
   163  type generatorFunc func(*Link, loader.Sym)
   164  
   165  // createGeneratorSymbol is a convenience method for creating a generator
   166  // symbol.
   167  func (ctxt *Link) createGeneratorSymbol(name string, version int, t sym.SymKind, size int64, gen generatorFunc) loader.Sym {
   168  	ldr := ctxt.loader
   169  	s := ldr.LookupOrCreateSym(name, version)
   170  	ldr.SetIsGeneratedSym(s, true)
   171  	sb := ldr.MakeSymbolUpdater(s)
   172  	sb.SetType(t)
   173  	sb.SetSize(size)
   174  	ctxt.generatorSyms[s] = gen
   175  	return s
   176  }
   177  

View as plain text