...
Run Format

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/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  	compressDWARF bool
    70  
    71  	Tlsg         *sym.Symbol
    72  	Libdir       []string
    73  	Library      []*sym.Library
    74  	LibraryByPkg map[string]*sym.Library
    75  	Shlibs       []Shlib
    76  	Tlsoffset    int
    77  	Textp        []*sym.Symbol
    78  	Filesyms     []*sym.Symbol
    79  	Moduledata   *sym.Symbol
    80  
    81  	PackageFile  map[string]string
    82  	PackageShlib map[string]string
    83  
    84  	tramps []*sym.Symbol // trampolines
    85  
    86  	// unresolvedSymSet is a set of erroneous unresolved references.
    87  	// Used to avoid duplicated error messages.
    88  	unresolvedSymSet map[unresolvedSymKey]bool
    89  
    90  	// Used to implement field tracking.
    91  	Reachparent map[*sym.Symbol]*sym.Symbol
    92  }
    93  
    94  type unresolvedSymKey struct {
    95  	from *sym.Symbol // Symbol that referenced unresolved "to"
    96  	to   *sym.Symbol // Unresolved symbol referenced by "from"
    97  }
    98  
    99  // ErrorUnresolved prints unresolved symbol error for r.Sym that is referenced from s.
   100  func (ctxt *Link) ErrorUnresolved(s *sym.Symbol, r *sym.Reloc) {
   101  	if ctxt.unresolvedSymSet == nil {
   102  		ctxt.unresolvedSymSet = make(map[unresolvedSymKey]bool)
   103  	}
   104  
   105  	k := unresolvedSymKey{from: s, to: r.Sym}
   106  	if !ctxt.unresolvedSymSet[k] {
   107  		ctxt.unresolvedSymSet[k] = true
   108  		// Give a special error message for main symbol (see #24809).
   109  		if r.Sym.Name == "main.main" {
   110  			Errorf(s, "function main is undeclared in the main package")
   111  		} else {
   112  			Errorf(s, "relocation target %s not defined", r.Sym.Name)
   113  		}
   114  	}
   115  }
   116  
   117  // The smallest possible offset from the hardware stack pointer to a local
   118  // variable on the stack. Architectures that use a link register save its value
   119  // on the stack in the function prologue and so always have a pointer between
   120  // the hardware stack pointer and the local variable area.
   121  func (ctxt *Link) FixedFrameSize() int64 {
   122  	switch ctxt.Arch.Family {
   123  	case sys.AMD64, sys.I386:
   124  		return 0
   125  	case sys.PPC64:
   126  		// PIC code on ppc64le requires 32 bytes of stack, and it's easier to
   127  		// just use that much stack always on ppc64x.
   128  		return int64(4 * ctxt.Arch.PtrSize)
   129  	default:
   130  		return int64(ctxt.Arch.PtrSize)
   131  	}
   132  }
   133  
   134  func (ctxt *Link) Logf(format string, args ...interface{}) {
   135  	fmt.Fprintf(ctxt.Bso, format, args...)
   136  	ctxt.Bso.Flush()
   137  }
   138  
   139  func addImports(ctxt *Link, l *sym.Library, pn string) {
   140  	pkg := objabi.PathToPrefix(l.Pkg)
   141  	for _, importStr := range l.ImportStrings {
   142  		lib := addlib(ctxt, pkg, pn, importStr)
   143  		if lib != nil {
   144  			l.Imports = append(l.Imports, lib)
   145  		}
   146  	}
   147  	l.ImportStrings = nil
   148  }
   149  
   150  type Pciter struct {
   151  	d       sym.Pcdata
   152  	p       []byte
   153  	pc      uint32
   154  	nextpc  uint32
   155  	pcscale uint32
   156  	value   int32
   157  	start   int
   158  	done    int
   159  }
   160  

View as plain text