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 ([email protected]) 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 ([email protected]) 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 31package ld 32 33import ( 34 "bufio" 35 "cmd/internal/objabi" 36 "cmd/link/internal/loader" 37 "cmd/link/internal/sym" 38 "debug/elf" 39 "fmt" 40) 41 42type Shlib struct { 43 Path string 44 Hash []byte 45 Deps []string 46 File *elf.File 47} 48 49// Link holds the context for writing object code from a compiler 50// or for reading that input into the linker. 51type Link struct { 52 Target 53 ErrorReporter 54 ArchSyms 55 56 outSem chan int // limits the number of output writers 57 Out *OutBuf 58 59 version int // current version number for static/file-local symbols 60 61 Debugvlog int 62 Bso *bufio.Writer 63 64 Loaded bool // set after all inputs have been loaded as symbols 65 66 compressDWARF bool 67 68 Libdir []string 69 Library []*sym.Library 70 LibraryByPkg map[string]*sym.Library 71 Shlibs []Shlib 72 Textp []loader.Sym 73 Moduledata loader.Sym 74 75 PackageFile map[string]string 76 PackageShlib map[string]string 77 78 tramps []loader.Sym // trampolines 79 80 compUnits []*sym.CompilationUnit // DWARF compilation units 81 runtimeCU *sym.CompilationUnit // One of the runtime CUs, the last one seen. 82 83 loader *loader.Loader 84 cgodata []cgodata // cgo directives to load, three strings are args for loadcgo 85 86 datap []loader.Sym 87 dynexp []loader.Sym 88 89 // Elf symtab variables. 90 numelfsym int // starts at 0, 1 is reserved 91 92 // These are symbols that created and written by the linker. 93 // Rather than creating a symbol, and writing all its data into the heap, 94 // you can create a symbol, and just a generation function will be called 95 // after the symbol's been created in the output mmap. 96 generatorSyms map[loader.Sym]generatorFunc 97} 98 99type cgodata struct { 100 file string 101 pkg string 102 directives [][]string 103} 104 105func (ctxt *Link) Logf(format string, args ...interface{}) { 106 fmt.Fprintf(ctxt.Bso, format, args...) 107 ctxt.Bso.Flush() 108} 109 110func addImports(ctxt *Link, l *sym.Library, pn string) { 111 pkg := objabi.PathToPrefix(l.Pkg) 112 for _, imp := range l.Autolib { 113 lib := addlib(ctxt, pkg, pn, imp.Pkg, imp.Fingerprint) 114 if lib != nil { 115 l.Imports = append(l.Imports, lib) 116 } 117 } 118 l.Autolib = nil 119} 120 121// Allocate a new version (i.e. symbol namespace). 122func (ctxt *Link) IncVersion() int { 123 ctxt.version++ 124 return ctxt.version - 1 125} 126 127// returns the maximum version number 128func (ctxt *Link) MaxVersion() int { 129 return ctxt.version 130} 131 132// generatorFunc is a convenience type. 133// Some linker-created Symbols are large and shouldn't really live in the heap. 134// Such Symbols can define a generator function. Their bytes can be generated 135// directly in the output mmap. 136// 137// Relocations are applied prior to emitting generator Symbol contents. 138// Generator Symbols that require relocations can be written in two passes. 139// The first pass, at Symbol creation time, adds only relocations. 140// The second pass, at content generation time, adds the rest. 141// See generateFunctab for an example. 142// 143// Generator functions shouldn't grow the Symbol size. 144// Generator functions must be safe for concurrent use. 145// 146// Generator Symbols have their Data set to the mmapped area when the 147// generator is called. 148type generatorFunc func(*Link, loader.Sym) 149 150// createGeneratorSymbol is a convenience method for creating a generator 151// symbol. 152func (ctxt *Link) createGeneratorSymbol(name string, version int, t sym.SymKind, size int64, gen generatorFunc) loader.Sym { 153 ldr := ctxt.loader 154 s := ldr.LookupOrCreateSym(name, version) 155 ldr.SetIsGeneratedSym(s, true) 156 sb := ldr.MakeSymbolUpdater(s) 157 sb.SetType(t) 158 sb.SetSize(size) 159 ctxt.generatorSyms[s] = gen 160 return s 161} 162