github.com/hikaru7719/go@v0.0.0-20181025140707-c8b2ac68906a/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 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 compUnits []*compilationUnit // DWARF compilation units 94 compUnitByPackage map[*sym.Library]*compilationUnit 95 } 96 97 type unresolvedSymKey struct { 98 from *sym.Symbol // Symbol that referenced unresolved "to" 99 to *sym.Symbol // Unresolved symbol referenced by "from" 100 } 101 102 // ErrorUnresolved prints unresolved symbol error for r.Sym that is referenced from s. 103 func (ctxt *Link) ErrorUnresolved(s *sym.Symbol, r *sym.Reloc) { 104 if ctxt.unresolvedSymSet == nil { 105 ctxt.unresolvedSymSet = make(map[unresolvedSymKey]bool) 106 } 107 108 k := unresolvedSymKey{from: s, to: r.Sym} 109 if !ctxt.unresolvedSymSet[k] { 110 ctxt.unresolvedSymSet[k] = true 111 // Give a special error message for main symbol (see #24809). 112 if r.Sym.Name == "main.main" { 113 Errorf(s, "function main is undeclared in the main package") 114 } else { 115 Errorf(s, "relocation target %s not defined", r.Sym.Name) 116 } 117 } 118 } 119 120 // The smallest possible offset from the hardware stack pointer to a local 121 // variable on the stack. Architectures that use a link register save its value 122 // on the stack in the function prologue and so always have a pointer between 123 // the hardware stack pointer and the local variable area. 124 func (ctxt *Link) FixedFrameSize() int64 { 125 switch ctxt.Arch.Family { 126 case sys.AMD64, sys.I386: 127 return 0 128 case sys.PPC64: 129 // PIC code on ppc64le requires 32 bytes of stack, and it's easier to 130 // just use that much stack always on ppc64x. 131 return int64(4 * ctxt.Arch.PtrSize) 132 default: 133 return int64(ctxt.Arch.PtrSize) 134 } 135 } 136 137 func (ctxt *Link) Logf(format string, args ...interface{}) { 138 fmt.Fprintf(ctxt.Bso, format, args...) 139 ctxt.Bso.Flush() 140 } 141 142 func addImports(ctxt *Link, l *sym.Library, pn string) { 143 pkg := objabi.PathToPrefix(l.Pkg) 144 for _, importStr := range l.ImportStrings { 145 lib := addlib(ctxt, pkg, pn, importStr) 146 if lib != nil { 147 l.Imports = append(l.Imports, lib) 148 } 149 } 150 l.ImportStrings = nil 151 } 152 153 type Pciter struct { 154 d sym.Pcdata 155 p []byte 156 pc uint32 157 nextpc uint32 158 pcscale uint32 159 value int32 160 start int 161 done int 162 }