github.com/c9s/go@v0.0.0-20180120015821-984e81f64e0c/src/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  	"cmd/internal/objabi"
    36  	"cmd/internal/sys"
    37  	"cmd/link/internal/sym"
    38  	"debug/elf"
    39  	"fmt"
    40  )
    41  
    42  type Shlib struct {
    43  	Path            string
    44  	Hash            []byte
    45  	Deps            []string
    46  	File            *elf.File
    47  	gcdataAddresses map[*sym.Symbol]uint64
    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  	Out *OutBuf
    54  
    55  	Syms *sym.Symbols
    56  
    57  	Arch      *sys.Arch
    58  	Debugvlog int
    59  	Bso       *bufio.Writer
    60  
    61  	Loaded bool // set after all inputs have been loaded as symbols
    62  
    63  	IsELF    bool
    64  	HeadType objabi.HeadType
    65  
    66  	linkShared bool // link against installed Go shared libraries
    67  	LinkMode   LinkMode
    68  	BuildMode  BuildMode
    69  
    70  	Tlsg         *sym.Symbol
    71  	Libdir       []string
    72  	Library      []*sym.Library
    73  	LibraryByPkg map[string]*sym.Library
    74  	Shlibs       []Shlib
    75  	Tlsoffset    int
    76  	Textp        []*sym.Symbol
    77  	Filesyms     []*sym.Symbol
    78  	Moduledata   *sym.Symbol
    79  
    80  	PackageFile  map[string]string
    81  	PackageShlib map[string]string
    82  
    83  	tramps []*sym.Symbol // trampolines
    84  }
    85  
    86  // The smallest possible offset from the hardware stack pointer to a local
    87  // variable on the stack. Architectures that use a link register save its value
    88  // on the stack in the function prologue and so always have a pointer between
    89  // the hardware stack pointer and the local variable area.
    90  func (ctxt *Link) FixedFrameSize() int64 {
    91  	switch ctxt.Arch.Family {
    92  	case sys.AMD64, sys.I386:
    93  		return 0
    94  	case sys.PPC64:
    95  		// PIC code on ppc64le requires 32 bytes of stack, and it's easier to
    96  		// just use that much stack always on ppc64x.
    97  		return int64(4 * ctxt.Arch.PtrSize)
    98  	default:
    99  		return int64(ctxt.Arch.PtrSize)
   100  	}
   101  }
   102  
   103  func (ctxt *Link) Logf(format string, args ...interface{}) {
   104  	fmt.Fprintf(ctxt.Bso, format, args...)
   105  	ctxt.Bso.Flush()
   106  }
   107  
   108  func addImports(ctxt *Link, l *sym.Library, pn string) {
   109  	pkg := objabi.PathToPrefix(l.Pkg)
   110  	for _, importStr := range l.ImportStrings {
   111  		lib := addlib(ctxt, pkg, pn, importStr)
   112  		if lib != nil {
   113  			l.Imports = append(l.Imports, lib)
   114  		}
   115  	}
   116  	l.ImportStrings = nil
   117  }
   118  
   119  type Pciter struct {
   120  	d       sym.Pcdata
   121  	p       []byte
   122  	pc      uint32
   123  	nextpc  uint32
   124  	pcscale uint32
   125  	value   int32
   126  	start   int
   127  	done    int
   128  }