github.com/go-asm/go@v1.21.1-0.20240213172139-40c5ead50c48/cmd/link/mips64/asm.go (about) 1 // Inferno utils/5l/asm.c 2 // https://bitbucket.org/inferno-os/inferno-os/src/master/utils/5l/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 mips64 32 33 import ( 34 "debug/elf" 35 36 "github.com/go-asm/go/cmd/link/ld" 37 "github.com/go-asm/go/cmd/link/loader" 38 "github.com/go-asm/go/cmd/link/sym" 39 "github.com/go-asm/go/cmd/objabi" 40 "github.com/go-asm/go/cmd/sys" 41 ) 42 43 var ( 44 // dtOffsets contains offsets for entries within the .dynamic section. 45 // These are used to fix up symbol values once they are known. 46 dtOffsets map[elf.DynTag]int64 47 48 // dynSymCount contains the number of entries in the .dynsym section. 49 // This is used to populate the DT_MIPS_SYMTABNO entry in the .dynamic 50 // section. 51 dynSymCount uint64 52 53 // gotLocalCount contains the number of local global offset table 54 // entries. This is used to populate the DT_MIPS_LOCAL_GOTNO entry in 55 // the .dynamic section. 56 gotLocalCount uint64 57 58 // gotSymIndex contains the index of the first dynamic symbol table 59 // entry that corresponds to an entry in the global offset table. 60 // This is used to populate the DT_MIPS_GOTSYM entry in the .dynamic 61 // section. 62 gotSymIndex uint64 63 ) 64 65 func gentext(ctxt *ld.Link, ldr *loader.Loader) { 66 if *ld.FlagD || ctxt.Target.IsExternal() { 67 return 68 } 69 70 dynamic := ldr.MakeSymbolUpdater(ctxt.ArchSyms.Dynamic) 71 72 ld.Elfwritedynent(ctxt.Arch, dynamic, elf.DT_MIPS_RLD_VERSION, 1) 73 ld.Elfwritedynent(ctxt.Arch, dynamic, elf.DT_MIPS_BASE_ADDRESS, 0) 74 75 // elfsetupplt should have been called and gotLocalCount should now 76 // have its correct value. 77 if gotLocalCount == 0 { 78 ctxt.Errorf(0, "internal error: elfsetupplt has not been called") 79 } 80 ld.Elfwritedynent(ctxt.Arch, dynamic, elf.DT_MIPS_LOCAL_GOTNO, gotLocalCount) 81 82 // DT_* entries have to exist prior to elfdynhash(), which finalises the 83 // table by adding DT_NULL. However, the values for the following entries 84 // are not know until after dynreloc() has completed. Add the symbols now, 85 // then update their values prior to code generation. 86 dts := []elf.DynTag{ 87 elf.DT_MIPS_SYMTABNO, 88 elf.DT_MIPS_GOTSYM, 89 } 90 dtOffsets = make(map[elf.DynTag]int64) 91 for _, dt := range dts { 92 ld.Elfwritedynent(ctxt.Arch, dynamic, dt, 0) 93 dtOffsets[dt] = dynamic.Size() - 8 94 } 95 } 96 97 func adddynrel(target *ld.Target, ldr *loader.Loader, syms *ld.ArchSyms, s loader.Sym, r loader.Reloc, rIdx int) bool { 98 targ := r.Sym() 99 var targType sym.SymKind 100 if targ != 0 { 101 targType = ldr.SymType(targ) 102 } 103 104 if r.Type() >= objabi.ElfRelocOffset { 105 ldr.Errorf(s, "unexpected relocation type %d (%s)", r.Type(), sym.RelocName(target.Arch, r.Type())) 106 return false 107 } 108 109 switch r.Type() { 110 case objabi.R_CALLMIPS, objabi.R_JMPMIPS: 111 if targType != sym.SDYNIMPORT { 112 // Nothing to do, the relocation will be laid out in reloc 113 return true 114 } 115 if target.IsExternal() { 116 // External linker will do this relocation. 117 return true 118 } 119 120 // Internal linking, build a PLT entry and change the relocation 121 // target to that entry. 122 if r.Add() != 0 { 123 ldr.Errorf(s, "PLT call with non-zero addend (%v)", r.Add()) 124 } 125 addpltsym(target, ldr, syms, targ) 126 su := ldr.MakeSymbolUpdater(s) 127 su.SetRelocSym(rIdx, syms.PLT) 128 su.SetRelocAdd(rIdx, int64(ldr.SymPlt(targ))) 129 return true 130 } 131 132 return false 133 } 134 135 func elfreloc1(ctxt *ld.Link, out *ld.OutBuf, ldr *loader.Loader, s loader.Sym, r loader.ExtReloc, ri int, sectoff int64) bool { 136 137 // mips64 ELF relocation (endian neutral) 138 // offset uint64 139 // sym uint32 140 // ssym uint8 141 // type3 uint8 142 // type2 uint8 143 // type uint8 144 // addend int64 145 146 addend := r.Xadd 147 148 out.Write64(uint64(sectoff)) 149 150 elfsym := ld.ElfSymForReloc(ctxt, r.Xsym) 151 out.Write32(uint32(elfsym)) 152 out.Write8(0) 153 out.Write8(0) 154 out.Write8(0) 155 switch r.Type { 156 default: 157 return false 158 case objabi.R_ADDR, objabi.R_DWARFSECREF: 159 switch r.Size { 160 case 4: 161 out.Write8(uint8(elf.R_MIPS_32)) 162 case 8: 163 out.Write8(uint8(elf.R_MIPS_64)) 164 default: 165 return false 166 } 167 case objabi.R_ADDRMIPS: 168 out.Write8(uint8(elf.R_MIPS_LO16)) 169 case objabi.R_ADDRMIPSU: 170 out.Write8(uint8(elf.R_MIPS_HI16)) 171 case objabi.R_ADDRMIPSTLS: 172 out.Write8(uint8(elf.R_MIPS_TLS_TPREL_LO16)) 173 if ctxt.Target.IsOpenbsd() { 174 // OpenBSD mips64 does not currently offset TLS by 0x7000, 175 // as such we need to add this back to get the correct offset 176 // via the external linker. 177 addend += 0x7000 178 } 179 case objabi.R_CALLMIPS, 180 objabi.R_JMPMIPS: 181 out.Write8(uint8(elf.R_MIPS_26)) 182 } 183 out.Write64(uint64(addend)) 184 185 return true 186 } 187 188 func elfsetupplt(ctxt *ld.Link, ldr *loader.Loader, plt, gotplt *loader.SymbolBuilder, dynamic loader.Sym) { 189 if plt.Size() != 0 { 190 return 191 } 192 193 // Load resolver address from got[0] into r25. 194 plt.AddSymRef(ctxt.Arch, gotplt.Sym(), 0, objabi.R_ADDRMIPSU, 4) 195 plt.SetUint32(ctxt.Arch, plt.Size()-4, 0x3c0e0000) // lui $14, %hi(&GOTPLT[0]) 196 plt.AddSymRef(ctxt.Arch, gotplt.Sym(), 0, objabi.R_ADDRMIPS, 4) 197 plt.SetUint32(ctxt.Arch, plt.Size()-4, 0xddd90000) // ld $25, %lo(&GOTPLT[0])($14) 198 199 // Load return address into r15, the index of the got.plt entry into r24, then 200 // JALR to the resolver. The address of the got.plt entry is currently in r24, 201 // which we have to turn into an index. 202 plt.AddSymRef(ctxt.Arch, gotplt.Sym(), 0, objabi.R_ADDRMIPS, 4) 203 plt.SetUint32(ctxt.Arch, plt.Size()-4, 0x25ce0000) // addiu $14, $14, %lo(&GOTPLT[0]) 204 plt.AddUint32(ctxt.Arch, 0x030ec023) // subu $24, $24, $14 205 plt.AddUint32(ctxt.Arch, 0x03e07825) // move $15, $31 206 plt.AddUint32(ctxt.Arch, 0x0018c0c2) // srl $24, $24, 3 207 plt.AddUint32(ctxt.Arch, 0x0320f809) // jalr $25 208 plt.AddUint32(ctxt.Arch, 0x2718fffe) // subu $24, $24, 2 209 210 if gotplt.Size() != 0 { 211 ctxt.Errorf(gotplt.Sym(), "got.plt is not empty") 212 } 213 214 // Reserve got[0] for resolver address (populated by dynamic loader). 215 gotplt.AddUint32(ctxt.Arch, 0) 216 gotplt.AddUint32(ctxt.Arch, 0) 217 gotLocalCount++ 218 219 // Reserve got[1] for ELF object pointer (populated by dynamic loader). 220 gotplt.AddUint32(ctxt.Arch, 0) 221 gotplt.AddUint32(ctxt.Arch, 0) 222 gotLocalCount++ 223 } 224 225 func addpltsym(target *ld.Target, ldr *loader.Loader, syms *ld.ArchSyms, s loader.Sym) { 226 if ldr.SymPlt(s) >= 0 { 227 return 228 } 229 230 dynamic := ldr.MakeSymbolUpdater(syms.Dynamic) 231 232 const dynSymEntrySize = 20 233 if gotSymIndex == 0 { 234 // Compute and update GOT symbol index. 235 gotSymIndex = uint64(ldr.SymSize(syms.DynSym) / dynSymEntrySize) 236 dynamic.SetUint(target.Arch, dtOffsets[elf.DT_MIPS_GOTSYM], gotSymIndex) 237 } 238 if dynSymCount == 0 { 239 dynSymCount = uint64(ldr.SymSize(syms.DynSym) / dynSymEntrySize) 240 } 241 242 ld.Adddynsym(ldr, target, syms, s) 243 dynSymCount++ 244 245 if !target.IsElf() { 246 ldr.Errorf(s, "addpltsym: unsupported binary format") 247 } 248 249 plt := ldr.MakeSymbolUpdater(syms.PLT) 250 gotplt := ldr.MakeSymbolUpdater(syms.GOTPLT) 251 if plt.Size() == 0 { 252 panic("plt is not set up") 253 } 254 255 // Load got.plt entry into r25. 256 plt.AddSymRef(target.Arch, gotplt.Sym(), gotplt.Size(), objabi.R_ADDRMIPSU, 4) 257 plt.SetUint32(target.Arch, plt.Size()-4, 0x3c0f0000) // lui $15, %hi(.got.plt entry) 258 plt.AddSymRef(target.Arch, gotplt.Sym(), gotplt.Size(), objabi.R_ADDRMIPS, 4) 259 plt.SetUint32(target.Arch, plt.Size()-4, 0xddf90000) // ld $25, %lo(.got.plt entry)($15) 260 261 // Load address of got.plt entry into r24 and JALR to address in r25. 262 plt.AddUint32(target.Arch, 0x03200008) // jr $25 263 plt.AddSymRef(target.Arch, gotplt.Sym(), gotplt.Size(), objabi.R_ADDRMIPS, 4) 264 plt.SetUint32(target.Arch, plt.Size()-4, 0x65f80000) // daddiu $24, $15, %lo(.got.plt entry) 265 266 // Add pointer to plt[0] to got.plt 267 gotplt.AddAddrPlus(target.Arch, plt.Sym(), 0) 268 269 ldr.SetPlt(s, int32(plt.Size()-16)) 270 271 // Update dynamic symbol count. 272 dynamic.SetUint(target.Arch, dtOffsets[elf.DT_MIPS_SYMTABNO], dynSymCount) 273 } 274 275 func machoreloc1(*sys.Arch, *ld.OutBuf, *loader.Loader, loader.Sym, loader.ExtReloc, int64) bool { 276 return false 277 } 278 279 func archreloc(target *ld.Target, ldr *loader.Loader, syms *ld.ArchSyms, r loader.Reloc, s loader.Sym, val int64) (o int64, nExtReloc int, ok bool) { 280 if target.IsExternal() { 281 switch r.Type() { 282 default: 283 return val, 0, false 284 285 case objabi.R_ADDRMIPS, 286 objabi.R_ADDRMIPSU, 287 objabi.R_ADDRMIPSTLS, 288 objabi.R_CALLMIPS, 289 objabi.R_JMPMIPS: 290 return val, 1, true 291 } 292 } 293 294 const isOk = true 295 const noExtReloc = 0 296 rs := r.Sym() 297 switch r.Type() { 298 case objabi.R_ADDRMIPS, 299 objabi.R_ADDRMIPSU: 300 t := ldr.SymValue(rs) + r.Add() 301 if r.Type() == objabi.R_ADDRMIPS { 302 return int64(val&0xffff0000 | t&0xffff), noExtReloc, isOk 303 } 304 return int64(val&0xffff0000 | ((t+1<<15)>>16)&0xffff), noExtReloc, isOk 305 case objabi.R_ADDRMIPSTLS: 306 // thread pointer is at 0x7000 offset from the start of TLS data area 307 t := ldr.SymValue(rs) + r.Add() - 0x7000 308 if target.IsOpenbsd() { 309 // OpenBSD mips64 does not currently offset TLS by 0x7000, 310 // as such we need to add this back to get the correct offset. 311 t += 0x7000 312 } 313 if t < -32768 || t >= 32678 { 314 ldr.Errorf(s, "TLS offset out of range %d", t) 315 } 316 return int64(val&0xffff0000 | t&0xffff), noExtReloc, isOk 317 case objabi.R_CALLMIPS, 318 objabi.R_JMPMIPS: 319 // Low 26 bits = (S + A) >> 2 320 t := ldr.SymValue(rs) + r.Add() 321 return int64(val&0xfc000000 | (t>>2)&^0xfc000000), noExtReloc, isOk 322 } 323 324 return val, 0, false 325 } 326 327 func archrelocvariant(*ld.Target, *loader.Loader, loader.Reloc, sym.RelocVariant, loader.Sym, int64, []byte) int64 { 328 return -1 329 } 330 331 func extreloc(target *ld.Target, ldr *loader.Loader, r loader.Reloc, s loader.Sym) (loader.ExtReloc, bool) { 332 switch r.Type() { 333 case objabi.R_ADDRMIPS, 334 objabi.R_ADDRMIPSU: 335 return ld.ExtrelocViaOuterSym(ldr, r, s), true 336 337 case objabi.R_ADDRMIPSTLS, 338 objabi.R_CALLMIPS, 339 objabi.R_JMPMIPS: 340 return ld.ExtrelocSimple(ldr, r), true 341 } 342 return loader.ExtReloc{}, false 343 }