...

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

     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/obj"
    36		"cmd/internal/objabi"
    37		"cmd/internal/sys"
    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		gcdataAddresses map[*sym.Symbol]uint64
    49	}
    50	
    51	// Link holds the context for writing object code from a compiler
    52	// or for reading that input into the linker.
    53	type Link struct {
    54		Out *OutBuf
    55	
    56		Syms *sym.Symbols
    57	
    58		Arch      *sys.Arch
    59		Debugvlog int
    60		Bso       *bufio.Writer
    61	
    62		Loaded bool // set after all inputs have been loaded as symbols
    63	
    64		IsELF    bool
    65		HeadType objabi.HeadType
    66	
    67		linkShared    bool // link against installed Go shared libraries
    68		LinkMode      LinkMode
    69		BuildMode     BuildMode
    70		compressDWARF bool
    71	
    72		Tlsg         *sym.Symbol
    73		Libdir       []string
    74		Library      []*sym.Library
    75		LibraryByPkg map[string]*sym.Library
    76		Shlibs       []Shlib
    77		Tlsoffset    int
    78		Textp        []*sym.Symbol
    79		Filesyms     []*sym.Symbol
    80		Moduledata   *sym.Symbol
    81	
    82		PackageFile  map[string]string
    83		PackageShlib map[string]string
    84	
    85		tramps []*sym.Symbol // trampolines
    86	
    87		// unresolvedSymSet is a set of erroneous unresolved references.
    88		// Used to avoid duplicated error messages.
    89		unresolvedSymSet map[unresolvedSymKey]bool
    90	
    91		// Used to implement field tracking.
    92		Reachparent map[*sym.Symbol]*sym.Symbol
    93	
    94		compUnits         []*compilationUnit // DWARF compilation units
    95		compUnitByPackage map[*sym.Library]*compilationUnit
    96	
    97		relocbuf []byte // temporary buffer for applying relocations
    98	}
    99	
   100	type unresolvedSymKey struct {
   101		from *sym.Symbol // Symbol that referenced unresolved "to"
   102		to   *sym.Symbol // Unresolved symbol referenced by "from"
   103	}
   104	
   105	// ErrorUnresolved prints unresolved symbol error for r.Sym that is referenced from s.
   106	func (ctxt *Link) ErrorUnresolved(s *sym.Symbol, r *sym.Reloc) {
   107		if ctxt.unresolvedSymSet == nil {
   108			ctxt.unresolvedSymSet = make(map[unresolvedSymKey]bool)
   109		}
   110	
   111		k := unresolvedSymKey{from: s, to: r.Sym}
   112		if !ctxt.unresolvedSymSet[k] {
   113			ctxt.unresolvedSymSet[k] = true
   114	
   115			// Try to find symbol under another ABI.
   116			var reqABI, haveABI obj.ABI
   117			haveABI = ^obj.ABI(0)
   118			reqABI, ok := sym.VersionToABI(int(r.Sym.Version))
   119			if ok {
   120				for abi := obj.ABI(0); abi < obj.ABICount; abi++ {
   121					v := sym.ABIToVersion(abi)
   122					if v == -1 {
   123						continue
   124					}
   125					if rs := ctxt.Syms.ROLookup(r.Sym.Name, v); rs != nil && rs.Type != sym.Sxxx {
   126						haveABI = abi
   127					}
   128				}
   129			}
   130	
   131			// Give a special error message for main symbol (see #24809).
   132			if r.Sym.Name == "main.main" {
   133				Errorf(s, "function main is undeclared in the main package")
   134			} else if haveABI != ^obj.ABI(0) {
   135				Errorf(s, "relocation target %s not defined for %s (but is defined for %s)", r.Sym.Name, reqABI, haveABI)
   136			} else {
   137				Errorf(s, "relocation target %s not defined", r.Sym.Name)
   138			}
   139		}
   140	}
   141	
   142	// The smallest possible offset from the hardware stack pointer to a local
   143	// variable on the stack. Architectures that use a link register save its value
   144	// on the stack in the function prologue and so always have a pointer between
   145	// the hardware stack pointer and the local variable area.
   146	func (ctxt *Link) FixedFrameSize() int64 {
   147		switch ctxt.Arch.Family {
   148		case sys.AMD64, sys.I386:
   149			return 0
   150		case sys.PPC64:
   151			// PIC code on ppc64le requires 32 bytes of stack, and it's easier to
   152			// just use that much stack always on ppc64x.
   153			return int64(4 * ctxt.Arch.PtrSize)
   154		default:
   155			return int64(ctxt.Arch.PtrSize)
   156		}
   157	}
   158	
   159	func (ctxt *Link) Logf(format string, args ...interface{}) {
   160		fmt.Fprintf(ctxt.Bso, format, args...)
   161		ctxt.Bso.Flush()
   162	}
   163	
   164	func addImports(ctxt *Link, l *sym.Library, pn string) {
   165		pkg := objabi.PathToPrefix(l.Pkg)
   166		for _, importStr := range l.ImportStrings {
   167			lib := addlib(ctxt, pkg, pn, importStr)
   168			if lib != nil {
   169				l.Imports = append(l.Imports, lib)
   170			}
   171		}
   172		l.ImportStrings = nil
   173	}
   174	

View as plain text