github.com/zxy12/go_duplicate_112_new@v0.0.0-20200807091221-747231827200/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/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 98 type unresolvedSymKey struct { 99 from *sym.Symbol // Symbol that referenced unresolved "to" 100 to *sym.Symbol // Unresolved symbol referenced by "from" 101 } 102 103 // ErrorUnresolved prints unresolved symbol error for r.Sym that is referenced from s. 104 func (ctxt *Link) ErrorUnresolved(s *sym.Symbol, r *sym.Reloc) { 105 if ctxt.unresolvedSymSet == nil { 106 ctxt.unresolvedSymSet = make(map[unresolvedSymKey]bool) 107 } 108 109 k := unresolvedSymKey{from: s, to: r.Sym} 110 if !ctxt.unresolvedSymSet[k] { 111 ctxt.unresolvedSymSet[k] = true 112 113 // Try to find symbol under another ABI. 114 var reqABI, haveABI obj.ABI 115 haveABI = ^obj.ABI(0) 116 reqABI, ok := sym.VersionToABI(int(r.Sym.Version)) 117 if ok { 118 for abi := obj.ABI(0); abi < obj.ABICount; abi++ { 119 v := sym.ABIToVersion(abi) 120 if v == -1 { 121 continue 122 } 123 if rs := ctxt.Syms.ROLookup(r.Sym.Name, v); rs != nil && rs.Type != sym.Sxxx { 124 haveABI = abi 125 } 126 } 127 } 128 129 // Give a special error message for main symbol (see #24809). 130 if r.Sym.Name == "main.main" { 131 Errorf(s, "function main is undeclared in the main package") 132 } else if haveABI != ^obj.ABI(0) { 133 Errorf(s, "relocation target %s not defined for %s (but is defined for %s)", r.Sym.Name, reqABI, haveABI) 134 } else { 135 Errorf(s, "relocation target %s not defined", r.Sym.Name) 136 } 137 } 138 } 139 140 // The smallest possible offset from the hardware stack pointer to a local 141 // variable on the stack. Architectures that use a link register save its value 142 // on the stack in the function prologue and so always have a pointer between 143 // the hardware stack pointer and the local variable area. 144 func (ctxt *Link) FixedFrameSize() int64 { 145 switch ctxt.Arch.Family { 146 case sys.AMD64, sys.I386: 147 return 0 148 case sys.PPC64: 149 // PIC code on ppc64le requires 32 bytes of stack, and it's easier to 150 // just use that much stack always on ppc64x. 151 return int64(4 * ctxt.Arch.PtrSize) 152 default: 153 return int64(ctxt.Arch.PtrSize) 154 } 155 } 156 157 func (ctxt *Link) Logf(format string, args ...interface{}) { 158 fmt.Fprintf(ctxt.Bso, format, args...) 159 ctxt.Bso.Flush() 160 } 161 162 func addImports(ctxt *Link, l *sym.Library, pn string) { 163 pkg := objabi.PathToPrefix(l.Pkg) 164 for _, importStr := range l.ImportStrings { 165 lib := addlib(ctxt, pkg, pn, importStr) 166 if lib != nil { 167 l.Imports = append(l.Imports, lib) 168 } 169 } 170 l.ImportStrings = nil 171 } 172 173 type Pciter struct { 174 d sym.Pcdata 175 p []byte 176 pc uint32 177 nextpc uint32 178 pcscale uint32 179 value int32 180 start int 181 done int 182 }