github.com/shogo82148/std@v1.22.1-0.20240327122250-4e474527810c/cmd/link/internal/ld/lib.go (about) 1 // Inferno utils/8l/asm.c 2 // https://bitbucket.org/inferno-os/inferno-os/src/master/utils/8l/asm.c 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 "github.com/shogo82148/std/cmd/internal/bio" 35 "github.com/shogo82148/std/cmd/internal/sys" 36 "github.com/shogo82148/std/cmd/link/internal/loader" 37 "github.com/shogo82148/std/cmd/link/internal/sym" 38 ) 39 40 // ArchSyms holds a number of architecture specific symbols used during 41 // relocation. Rather than allowing them universal access to all symbols, 42 // we keep a subset for relocation application. 43 type ArchSyms struct { 44 Rel loader.Sym 45 Rela loader.Sym 46 RelPLT loader.Sym 47 RelaPLT loader.Sym 48 49 LinkEditGOT loader.Sym 50 LinkEditPLT loader.Sym 51 52 TOC loader.Sym 53 DotTOC []loader.Sym 54 55 GOT loader.Sym 56 PLT loader.Sym 57 GOTPLT loader.Sym 58 59 Tlsg loader.Sym 60 Tlsoffset int 61 62 Dynamic loader.Sym 63 DynSym loader.Sym 64 DynStr loader.Sym 65 66 unreachableMethod loader.Sym 67 68 // Symbol containing a list of all the inittasks that need 69 // to be run at startup. 70 mainInittasks loader.Sym 71 } 72 73 type Arch struct { 74 Funcalign int 75 Maxalign int 76 Minalign int 77 Dwarfregsp int 78 Dwarfreglr int 79 80 // Threshold of total text size, used for trampoline insertion. If the total 81 // text size is smaller than TrampLimit, we won't need to insert trampolines. 82 // It is pretty close to the offset range of a direct CALL machine instruction. 83 // We leave some room for extra stuff like PLT stubs. 84 TrampLimit uint64 85 86 // Empty spaces between codeblocks will be padded with this value. 87 // For example an architecture might want to pad with a trap instruction to 88 // catch wayward programs. Architectures that do not define a padding value 89 // are padded with zeros. 90 CodePad []byte 91 92 // Plan 9 variables. 93 Plan9Magic uint32 94 Plan9_64Bit bool 95 96 Adddynrel func(*Target, *loader.Loader, *ArchSyms, loader.Sym, loader.Reloc, int) bool 97 Archinit func(*Link) 98 // Archreloc is an arch-specific hook that assists in relocation processing 99 // (invoked by 'relocsym'); it handles target-specific relocation tasks. 100 // Here "rel" is the current relocation being examined, "sym" is the symbol 101 // containing the chunk of data to which the relocation applies, and "off" 102 // is the contents of the to-be-relocated data item (from sym.P). Return 103 // value is the appropriately relocated value (to be written back to the 104 // same spot in sym.P), number of external _host_ relocations needed (i.e. 105 // ELF/Mach-O/etc. relocations, not Go relocations, this must match ELF.Reloc1, 106 // etc.), and a boolean indicating success/failure (a failing value indicates 107 // a fatal error). 108 Archreloc func(*Target, *loader.Loader, *ArchSyms, loader.Reloc, loader.Sym, 109 int64) (relocatedOffset int64, nExtReloc int, ok bool) 110 // Archrelocvariant is a second arch-specific hook used for 111 // relocation processing; it handles relocations where r.Type is 112 // insufficient to describe the relocation (r.Variant != 113 // sym.RV_NONE). Here "rel" is the relocation being applied, "sym" 114 // is the symbol containing the chunk of data to which the 115 // relocation applies, and "off" is the contents of the 116 // to-be-relocated data item (from sym.P). Return is an updated 117 // offset value. 118 Archrelocvariant func(target *Target, ldr *loader.Loader, rel loader.Reloc, 119 rv sym.RelocVariant, sym loader.Sym, offset int64, data []byte) (relocatedOffset int64) 120 121 // Generate a trampoline for a call from s to rs if necessary. ri is 122 // index of the relocation. 123 Trampoline func(ctxt *Link, ldr *loader.Loader, ri int, rs, s loader.Sym) 124 125 // Assembling the binary breaks into two phases, writing the code/data/ 126 // dwarf information (which is rather generic), and some more architecture 127 // specific work like setting up the elf headers/dynamic relocations, etc. 128 // The phases are called "Asmb" and "Asmb2". Asmb2 needs to be defined for 129 // every architecture, but only if architecture has an Asmb function will 130 // it be used for assembly. Otherwise a generic assembly Asmb function is 131 // used. 132 Asmb func(*Link, *loader.Loader) 133 Asmb2 func(*Link, *loader.Loader) 134 135 // Extreloc is an arch-specific hook that converts a Go relocation to an 136 // external relocation. Return the external relocation and whether it is 137 // needed. 138 Extreloc func(*Target, *loader.Loader, loader.Reloc, loader.Sym) (loader.ExtReloc, bool) 139 140 Gentext func(*Link, *loader.Loader) 141 Machoreloc1 func(*sys.Arch, *OutBuf, *loader.Loader, loader.Sym, loader.ExtReloc, int64) bool 142 MachorelocSize uint32 143 PEreloc1 func(*sys.Arch, *OutBuf, *loader.Loader, loader.Sym, loader.ExtReloc, int64) bool 144 Xcoffreloc1 func(*sys.Arch, *OutBuf, *loader.Loader, loader.Sym, loader.ExtReloc, int64) bool 145 146 // Generate additional symbols for the native symbol table just prior to 147 // code generation. 148 GenSymsLate func(*Link, *loader.Loader) 149 150 // TLSIEtoLE converts a TLS Initial Executable relocation to 151 // a TLS Local Executable relocation. 152 // 153 // This is possible when a TLS IE relocation refers to a local 154 // symbol in an executable, which is typical when internally 155 // linking PIE binaries. 156 TLSIEtoLE func(P []byte, off, size int) 157 158 // optional override for assignAddress 159 AssignAddress func(ldr *loader.Loader, sect *sym.Section, n int, s loader.Sym, va uint64, isTramp bool) (*sym.Section, int, uint64) 160 161 // ELF specific information. 162 ELF ELFArch 163 } 164 165 // DynlinkingGo reports whether we are producing Go code that can live 166 // in separate shared libraries linked together at runtime. 167 func (ctxt *Link) DynlinkingGo() bool 168 169 // CanUsePlugins reports whether a plugins can be used 170 func (ctxt *Link) CanUsePlugins() bool 171 172 // NeedCodeSign reports whether we need to code-sign the output binary. 173 func (ctxt *Link) NeedCodeSign() bool 174 175 var ( 176 Funcalign int 177 178 HEADR int32 179 ) 180 181 var ( 182 Segtext sym.Segment 183 Segrodata sym.Segment 184 Segrelrodata sym.Segment 185 Segdata sym.Segment 186 Segdwarf sym.Segment 187 Segpdata sym.Segment 188 Segxdata sym.Segment 189 190 Segments = []*sym.Segment{&Segtext, &Segrodata, &Segrelrodata, &Segdata, &Segdwarf, &Segpdata, &Segxdata} 191 ) 192 193 func Lflag(ctxt *Link, arg string) 194 195 type Hostobj struct { 196 ld func(*Link, *bio.Reader, string, int64, string) 197 pkg string 198 pn string 199 file string 200 off int64 201 length int64 202 } 203 204 type SymbolType int8 205 206 const ( 207 // see also https://9p.io/magic/man2html/1/nm 208 TextSym SymbolType = 'T' 209 DataSym SymbolType = 'D' 210 BSSSym SymbolType = 'B' 211 UndefinedSym SymbolType = 'U' 212 TLSSym SymbolType = 't' 213 FrameSym SymbolType = 'm' 214 ParamSym SymbolType = 'p' 215 AutoSym SymbolType = 'a' 216 217 // Deleted auto (not a real sym, just placeholder for type) 218 DeletedAutoSym = 'x' 219 ) 220 221 func Entryvalue(ctxt *Link) int64 222 223 func Rnd(v int64, r int64) int64 224 225 const ( 226 _ markKind = iota 227 ) 228 229 func ElfSymForReloc(ctxt *Link, s loader.Sym) int32 230 231 func AddGotSym(target *Target, ldr *loader.Loader, syms *ArchSyms, s loader.Sym, elfRelocTyp uint32)