Source file src/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/master/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/link/internal/loader" 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 // For every symbol defined in the shared library, record its address 48 // in the original shared library address space. 49 symAddr map[string]uint64 50 // For relocations in the shared library, map from the address 51 // (in the shared library address space) at which that 52 // relocation applies to the target symbol. We only keep 53 // track of a single kind of relocation: a standard absolute 54 // address relocation with no addend. These were R_ADDR 55 // relocations when the shared library was built. 56 relocTarget map[uint64]string 57 } 58 59 // A relocation that applies to part of the shared library. 60 type shlibReloc struct { 61 // Address (in the shared library address space) the relocation applies to. 62 addr uint64 63 // Target symbol name. 64 target string 65 } 66 67 type shlibRelocs []shlibReloc 68 69 func (s shlibRelocs) Len() int { return len(s) } 70 func (s shlibRelocs) Swap(i, j int) { s[i], s[j] = s[j], s[i] } 71 func (s shlibRelocs) Less(i, j int) bool { return s[i].addr < s[j].addr } 72 73 // Link holds the context for writing object code from a compiler 74 // or for reading that input into the linker. 75 type Link struct { 76 Target 77 ErrorReporter 78 ArchSyms 79 80 outSem chan int // limits the number of output writers 81 Out *OutBuf 82 83 version int // current version number for static/file-local symbols 84 85 Debugvlog int 86 Bso *bufio.Writer 87 88 Loaded bool // set after all inputs have been loaded as symbols 89 90 compressDWARF bool 91 92 Libdir []string 93 Library []*sym.Library 94 LibraryByPkg map[string]*sym.Library 95 Shlibs []Shlib 96 Textp []loader.Sym 97 Moduledata loader.Sym 98 99 moduledataTypeDescOffset int64 100 moduledataItabOffset int64 101 moduledataItabSizeOffset int64 102 103 PackageFile map[string]string 104 PackageShlib map[string]string 105 106 tramps []loader.Sym // trampolines 107 108 compUnits []*sym.CompilationUnit // DWARF compilation units 109 runtimeCU *sym.CompilationUnit // One of the runtime CUs, the last one seen. 110 111 loader *loader.Loader 112 cgodata []cgodata // cgo directives to load, three strings are args for loadcgo 113 114 datap []loader.Sym 115 dynexp []loader.Sym 116 117 // Elf symtab variables. 118 numelfsym int // starts at 0, 1 is reserved 119 120 // These are symbols that created and written by the linker. 121 // Rather than creating a symbol, and writing all its data into the heap, 122 // you can create a symbol, and just a generation function will be called 123 // after the symbol's been created in the output mmap. 124 generatorSyms map[loader.Sym]generatorFunc 125 } 126 127 type cgodata struct { 128 file string 129 pkg string 130 directives [][]string 131 } 132 133 func (ctxt *Link) Logf(format string, args ...any) { 134 fmt.Fprintf(ctxt.Bso, format, args...) 135 ctxt.Bso.Flush() 136 } 137 138 func addImports(ctxt *Link, l *sym.Library, pn string) { 139 pkg := objabi.PathToPrefix(l.Pkg) 140 for _, imp := range l.Autolib { 141 lib := addlib(ctxt, pkg, pn, imp.Pkg, imp.Fingerprint) 142 if lib != nil { 143 l.Imports = append(l.Imports, lib) 144 } 145 } 146 l.Autolib = nil 147 } 148 149 // Allocate a new version (i.e. symbol namespace). 150 func (ctxt *Link) IncVersion() int { 151 ctxt.version++ 152 return ctxt.version - 1 153 } 154 155 // returns the maximum version number 156 func (ctxt *Link) MaxVersion() int { 157 return ctxt.version 158 } 159 160 // generatorFunc is a convenience type. 161 // Some linker-created Symbols are large and shouldn't really live in the heap. 162 // Such Symbols can define a generator function. Their bytes can be generated 163 // directly in the output mmap. 164 // 165 // Relocations are applied prior to emitting generator Symbol contents. 166 // Generator Symbols that require relocations can be written in two passes. 167 // The first pass, at Symbol creation time, adds only relocations. 168 // The second pass, at content generation time, adds the rest. 169 // See generateFunctab for an example. 170 // 171 // Generator functions shouldn't grow the Symbol size. 172 // Generator functions must be safe for concurrent use. 173 // 174 // Generator Symbols have their Data set to the mmapped area when the 175 // generator is called. 176 type generatorFunc func(*Link, loader.Sym) 177 178 // createGeneratorSymbol is a convenience method for creating a generator 179 // symbol. 180 func (ctxt *Link) createGeneratorSymbol(name string, version int, t sym.SymKind, size int64, gen generatorFunc) loader.Sym { 181 ldr := ctxt.loader 182 s := ldr.LookupOrCreateSym(name, version) 183 ldr.SetIsGeneratedSym(s, true) 184 sb := ldr.MakeSymbolUpdater(s) 185 sb.SetType(t) 186 sb.SetSize(size) 187 ctxt.generatorSyms[s] = gen 188 return s 189 } 190