github.com/4ad/go@v0.0.0-20161219182952-69a12818b605/src/cmd/link/internal/sparc64/asm.go (about) 1 // Inferno utils/5l/asm.c 2 // http://code.google.com/p/inferno-os/source/browse/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 sparc64 32 33 import ( 34 "cmd/internal/obj" 35 "cmd/link/internal/ld" 36 "fmt" 37 "log" 38 ) 39 40 func gentext() { 41 if !ld.DynlinkingGo() { 42 return 43 } 44 log.Fatalf("gentext() not implemented") 45 } 46 47 func addgotsym(s *ld.LSym) { 48 if s.Got >= 0 { 49 return 50 } 51 52 ld.Adddynsym(ld.Ctxt, s) 53 got := ld.Linklookup(ld.Ctxt, ".got", 0) 54 s.Got = int32(got.Size) 55 ld.Adduint64(ld.Ctxt, got, 0) 56 57 if ld.Iself { 58 rela := ld.Linklookup(ld.Ctxt, ".rela.got", 0) 59 ld.Addaddrplus(ld.Ctxt, rela, got, int64(s.Got)) 60 ld.Adduint64(ld.Ctxt, rela, ld.ELF64_R_INFO(uint32(s.Dynid), ld.R_SPARC_GLOB_DAT)) 61 ld.Adduint64(ld.Ctxt, rela, 0) 62 } else { 63 ld.Diag("addgotsym: unsupported binary format") 64 } 65 } 66 67 func adddynrela(rela *ld.LSym, s *ld.LSym, r *ld.Reloc) { 68 log.Fatalf("adddynrela not implemented") 69 } 70 71 func adddynrel(s *ld.LSym, r *ld.Reloc) { 72 targ := r.Sym 73 ld.Ctxt.Cursym = s 74 75 switch r.Type { 76 default: 77 if r.Type >= 256 { 78 ld.Diag("unexpected relocation type %d (%d)", r.Type, r.Type-256) 79 return 80 } 81 case 256 + ld.R_SPARC_PC10: 82 if targ.Type == obj.SDYNIMPORT { 83 ld.Diag("unexpected R_SPARC_PC10 relocation for dynamic symbol %s", targ.Name) 84 } 85 if targ.Type == 0 || targ.Type == obj.SXREF { 86 ld.Diag("unknown symbol %s in pcrel", targ.Name) 87 } 88 r.Type = obj.R_PCREL 89 r.Add += int64(r.Siz) 90 println("R_SPARC_PC10 relocation for symbol ", targ.Name) 91 return 92 93 case 256 + ld.R_SPARC_PC22: 94 if targ.Type == obj.SDYNIMPORT { 95 ld.Diag("unexpected R_SPARC_PC22 relocation for dynamic symbol %s", targ.Name) 96 } 97 if targ.Type == 0 || targ.Type == obj.SXREF { 98 ld.Diag("unknown symbol %s in pcrel", targ.Name) 99 } 100 r.Type = obj.R_PCREL 101 r.Add += int64(r.Siz) 102 println("R_SPARC_PC22 relocation for symbol ", targ.Name) 103 return 104 105 case 256 + ld.R_SPARC_WPLT30: 106 r.Add += int64(r.Siz) 107 if targ.Type == obj.SDYNIMPORT { 108 addpltsym(targ) 109 r.Sym = ld.Linklookup(ld.Ctxt, ".plt", 0) 110 r.Add += int64(targ.Plt) 111 } 112 r.Type = obj.R_CALLSPARC64 113 println("R_SPARC_WPLT30 relocation for symbol ", targ.Name) 114 fmt.Printf("r %#v\n", r) 115 return 116 117 // TODO(shawn): 118 // The R_SPARC_GOTDATA_OP* relocations are an optimized form of 119 // relocation that only supports a range of +/- 2 Gbytes. We should 120 // eventually support these, but for now, simplify them to standard GOT 121 // relocations for simplicity in implementation. 122 case 256 + ld.R_SPARC_GOT10, 256 + ld.R_SPARC_GOTDATA_OP_LOX10: 123 addgotsym(targ) 124 r.Sym = ld.Linklookup(ld.Ctxt, ".got", 0) 125 r.Add += int64(targ.Got) 126 r.Type = obj.R_GOTOFF 127 println("R_SPARC_GOT10 relocation for symbol ", targ.Name) 128 return 129 130 case 256 + ld.R_SPARC_GOT22, 256 + ld.R_SPARC_GOTDATA_OP_HIX22: 131 addgotsym(targ) 132 r.Sym = ld.Linklookup(ld.Ctxt, ".got", 0) 133 r.Add += int64(targ.Got) 134 r.Type = obj.R_GOTOFF 135 println("R_SPARC_GOT22 relocation for symbol ", targ.Name) 136 return 137 138 case 256 + ld.R_SPARC_GOTDATA_OP: 139 r.Type = ld.R_SPARC_NONE 140 return 141 } 142 143 // Handle references to ELF symbols from our own object files. 144 if targ.Type != obj.SDYNIMPORT { 145 return 146 } 147 148 switch r.Type { 149 case obj.R_ADDRSPARC64HI, obj.R_ADDRSPARC64LO: 150 if s.Type == obj.STEXT && ld.Iself { 151 addpltsym(targ) 152 r.Sym = ld.Linkrlookup(ld.Ctxt, ".plt", 0) 153 r.Add += int64(targ.Plt) 154 return 155 } 156 } 157 158 ld.Ctxt.Cursym = s 159 ld.Diag("unsupported relocation for dynamic symbol %s (type=%d stype=%d)", targ.Name, r.Type, targ.Type) 160 161 } 162 163 func elfreloc1(r *ld.Reloc, sectoff int64) int { 164 ld.Thearch.Vput(uint64(sectoff)) 165 166 elfsym := r.Xsym.ElfsymForReloc() 167 switch r.Type { 168 default: 169 return -1 170 171 case obj.R_ADDR: 172 switch r.Siz { 173 case 4: 174 ld.Thearch.Vput(ld.R_SPARC_32 | uint64(elfsym)<<32) 175 case 8: 176 ld.Thearch.Vput(ld.R_SPARC_64 | uint64(elfsym)<<32) 177 default: 178 return -1 179 } 180 181 case obj.R_ADDRSPARC64LO: 182 ld.Thearch.Vput(ld.R_SPARC_LM22 | uint64(elfsym)<<32) 183 ld.Thearch.Vput(uint64(r.Xadd)) 184 ld.Thearch.Vput(uint64(sectoff + 4)) 185 ld.Thearch.Vput(ld.R_SPARC_LO10 | uint64(elfsym)<<32) 186 187 case obj.R_ADDRSPARC64HI: 188 ld.Thearch.Vput(ld.R_SPARC_HH22 | uint64(elfsym)<<32) 189 ld.Thearch.Vput(uint64(r.Xadd)) 190 ld.Thearch.Vput(uint64(sectoff + 4)) 191 ld.Thearch.Vput(ld.R_SPARC_HM10 | uint64(elfsym)<<32) 192 193 case obj.R_SPARC64_TLS_LE: 194 ld.Thearch.Vput(ld.R_SPARC_TLS_LE_HIX22 | uint64(elfsym)<<32) 195 ld.Thearch.Vput(uint64(r.Xadd)) 196 ld.Thearch.Vput(uint64(sectoff + 4)) 197 ld.Thearch.Vput(ld.R_SPARC_TLS_LE_LOX10 | uint64(elfsym)<<32) 198 199 case obj.R_CALLSPARC64: 200 if r.Siz != 4 { 201 return -1 202 } 203 ld.Thearch.Vput(ld.R_SPARC_WDISP30 | uint64(elfsym)<<32) 204 205 case obj.R_PCREL: 206 if r.Siz != 4 { 207 return -1 208 } 209 ld.Thearch.Vput(ld.R_SPARC_RELATIVE | uint64(elfsym)<<32) 210 } 211 ld.Thearch.Vput(uint64(r.Xadd)) 212 213 return 0 214 } 215 216 func elfsetupplt() { 217 plt := ld.Linklookup(ld.Ctxt, ".plt", 0) 218 if plt.Size == 0 { 219 // .plt entries are aligned at 32-byte boundaries, but the 220 // entire section at 256-byte boundaries. 221 plt.Align = 256 222 223 // Runtime linker will provide the initial plt; each entry is 224 // 32 bytes; reserve the first four entries for its use. 225 plt.Size = 4 * 32 226 227 // Create relocation table for .plt 228 rela := ld.Linklookup(ld.Ctxt, ".rela.plt", 0) 229 rela.Align = int32(ld.SysArch.RegSize) 230 231 // Create global offset table; first entry reserved for 232 // address of .dynamic section. 233 got := ld.Linklookup(ld.Ctxt, ".got", 0) 234 dyn := ld.Linklookup(ld.Ctxt, ".dynamic", 0) 235 ld.Addaddrplus(ld.Ctxt, got, dyn, 0) 236 237 // TODO(srwalker): pad end of plt with 10 entries for elfedit, etc. 238 // .strtab too; aslr-tagging, etc. 239 } 240 } 241 242 func machoreloc1(r *ld.Reloc, sectoff int64) int { 243 log.Fatalf("machoreloc1 not implemented") 244 return 0 245 } 246 247 func archreloc(r *ld.Reloc, s *ld.LSym, val *int64) int { 248 if ld.Linkmode == ld.LinkExternal { 249 switch r.Type { 250 default: 251 ld.Diag("unsupported LinkExternal archreloc %s", r.Type) 252 return -1 253 254 case obj.R_ADDRSPARC64LO, obj.R_ADDRSPARC64HI: 255 r.Done = 0 256 257 // set up addend for eventual relocation via outer symbol. 258 rs := r.Sym 259 r.Xadd = r.Add 260 for rs.Outer != nil { 261 r.Xadd += ld.Symaddr(rs) - ld.Symaddr(rs.Outer) 262 rs = rs.Outer 263 } 264 265 if rs.Type != obj.SHOSTOBJ && rs.Type != obj.SDYNIMPORT && rs.Sect == nil { 266 ld.Diag("missing section for %s", rs.Name) 267 } 268 r.Xsym = rs 269 270 return 0 271 272 case obj.R_CALLSPARC64, obj.R_SPARC64_TLS_LE: 273 r.Done = 0 274 r.Xsym = r.Sym 275 r.Xadd = r.Add 276 return 0 277 } 278 } 279 280 switch r.Type { 281 case obj.R_CONST: 282 *val = r.Add 283 return 0 284 285 case obj.R_ADDRSPARC64LO: 286 t := ld.Symaddr(r.Sym) + r.Add 287 288 o0 := uint32(*val >> 32) 289 o1 := uint32(*val) 290 291 o0 |= uint32(t) >> 10 292 o1 |= uint32(t) & 0x3ff 293 294 *val = int64(o0)<<32 | int64(o1) 295 return 0 296 297 case obj.R_ADDRSPARC64HI: 298 t := ld.Symaddr(r.Sym) + r.Add 299 300 o0 := uint32(*val >> 32) 301 o1 := uint32(*val) 302 303 o0 |= uint32(uint64(t)>>32) >> 10 304 o1 |= uint32(uint64(t)>>32) & 0x3ff 305 306 *val = int64(o0)<<32 | int64(o1) 307 return 0 308 309 case obj.R_CALLSPARC64: 310 t := (ld.Symaddr(r.Sym) + r.Add) - (s.Value + int64(r.Off)) 311 if t > 1<<31-4 || t < -1<<31 { 312 ld.Diag("program too large, call relocation distance = %d", t) 313 } 314 *val |= (t >> 2) & 0x3fffffff 315 return 0 316 317 case obj.R_GOTOFF: 318 // TODO(shawn): GOT10 needs (val) & 0x3ff 319 // GOT22 needs (val) >> 10 320 *val = ld.Symaddr(r.Sym) + r.Add - ld.Symaddr(ld.Linklookup(ld.Ctxt, ".got", 0)) 321 return 0 322 323 case obj.R_SPARC64_TLS_LE: 324 // The thread pointer points to the TCB, and then the 325 // address of the first TLS block follows, giving an 326 // offset of -16 for our static TLS variables. 327 v := r.Sym.Value - 16 328 if v < -4096 || 4095 < v { 329 ld.Diag("TLS offset out of range %d", v) 330 } 331 *val = (*val &^ 0x1fff) | (v & 0x1fff) 332 return 0 333 } 334 335 ld.Diag("unsupported LinkInternal archreloc %s", r.Type) 336 return -1 337 } 338 339 func archrelocvariant(r *ld.Reloc, s *ld.LSym, t int64) int64 { 340 log.Fatalf("unexpected relocation variant") 341 return -1 342 } 343 344 func addpltsym(s *ld.LSym) { 345 if s.Plt >= 0 { 346 return 347 } 348 349 ld.Adddynsym(ld.Ctxt, s) 350 351 if ld.Iself { 352 elfsetupplt() 353 plt := ld.Linkrlookup(ld.Ctxt, ".plt", 0) 354 rela := ld.Linkrlookup(ld.Ctxt, ".rela.plt", 0) 355 356 // Each of the first 32,767 procedure linkage table entries occupies 357 // 8 words (32 bytes), and must be aligned on a 32-byte boundary. 358 // 359 // NOTE: This only supports "near" .plt; entries beyond 360 // 32,767 are considered "far" and have a different format. 361 362 // The first eight bytes of each entry (excluding the initially 363 // reserved ones) should transfer control to the first or second 364 // reserved plt entry. For our use, the second reserved entry (.PLT1) 365 // should always be the target. 366 // 367 // 03 00 00 80 sethi (.-.PLT0), %g1 sethi %hi(0x20000), %g1 368 sethi := uint32(0x03000000) 369 sethi |= uint32(plt.Size) 370 ld.Adduint32(ld.Ctxt, plt, sethi) 371 372 // 30 6f ff e7 ba,a,pt %xcc, .PLT1 373 ba := uint32(0x30680000) 374 ba |= (((-uint32(plt.Size)) + 32) >> 2) & ((1 << (19)) - 1) 375 ld.Adduint32(ld.Ctxt, plt, ba) 376 377 // Fill remaining 24 bytes with nop; these will be provided by the 378 // runtime linker. 379 ld.Adduint32(ld.Ctxt, plt, 0x01000000) 380 ld.Adduint32(ld.Ctxt, plt, 0x01000000) 381 ld.Adduint32(ld.Ctxt, plt, 0x01000000) 382 ld.Adduint32(ld.Ctxt, plt, 0x01000000) 383 ld.Adduint32(ld.Ctxt, plt, 0x01000000) 384 ld.Adduint32(ld.Ctxt, plt, 0x01000000) 385 386 // rela 387 // offset 388 ld.Addaddrplus(ld.Ctxt, rela, plt, plt.Size-32) 389 // info 390 ld.Adduint64(ld.Ctxt, rela, ld.ELF64_R_INFO(uint32(s.Dynid), ld.R_SPARC_JMP_SLOT)) 391 // addend 392 ld.Adduint64(ld.Ctxt, rela, 0) 393 394 s.Plt = int32(plt.Size - 32) 395 } else { 396 ld.Diag("addpltsym: unsupported binary format") 397 } 398 399 return 400 } 401 402 func asmb() { 403 if ld.Debug['v'] != 0 { 404 fmt.Fprintf(ld.Bso, "%5.2f asmb\n", obj.Cputime()) 405 } 406 ld.Bso.Flush() 407 408 if ld.Iself { 409 ld.Asmbelfsetup() 410 } 411 412 sect := ld.Segtext.Sect 413 ld.Cseek(int64(sect.Vaddr - ld.Segtext.Vaddr + ld.Segtext.Fileoff)) 414 ld.CodeblkPad(int64(sect.Vaddr), int64(sect.Length), []byte{0x0, 0x0d, 0xea, 0xd1}) 415 for sect = sect.Next; sect != nil; sect = sect.Next { 416 ld.Cseek(int64(sect.Vaddr - ld.Segtext.Vaddr + ld.Segtext.Fileoff)) 417 ld.Datblk(int64(sect.Vaddr), int64(sect.Length)) 418 } 419 420 if ld.Segrodata.Filelen > 0 { 421 if ld.Debug['v'] != 0 { 422 fmt.Fprintf(ld.Bso, "%5.2f rodatblk\n", obj.Cputime()) 423 } 424 ld.Bso.Flush() 425 426 ld.Cseek(int64(ld.Segrodata.Fileoff)) 427 ld.Datblk(int64(ld.Segrodata.Vaddr), int64(ld.Segrodata.Filelen)) 428 } 429 430 if ld.Debug['v'] != 0 { 431 fmt.Fprintf(ld.Bso, "%5.2f datblk\n", obj.Cputime()) 432 } 433 ld.Bso.Flush() 434 435 ld.Cseek(int64(ld.Segdata.Fileoff)) 436 ld.Datblk(int64(ld.Segdata.Vaddr), int64(ld.Segdata.Filelen)) 437 438 /* output symbol table */ 439 ld.Symsize = 0 440 441 ld.Lcsize = 0 442 symo := uint32(0) 443 if ld.Debug['s'] == 0 { 444 // TODO: rationalize 445 if ld.Debug['v'] != 0 { 446 fmt.Fprintf(ld.Bso, "%5.2f sym\n", obj.Cputime()) 447 } 448 ld.Bso.Flush() 449 switch ld.HEADTYPE { 450 default: 451 if ld.Iself { 452 symo = uint32(ld.Segdata.Fileoff + ld.Segdata.Filelen) 453 symo = uint32(ld.Rnd(int64(symo), int64(ld.INITRND))) 454 } 455 } 456 457 ld.Cseek(int64(symo)) 458 switch ld.HEADTYPE { 459 default: 460 if ld.Iself { 461 if ld.Debug['v'] != 0 { 462 fmt.Fprintf(ld.Bso, "%5.2f elfsym\n", obj.Cputime()) 463 } 464 ld.Asmelfsym() 465 ld.Cflush() 466 ld.Cwrite(ld.Elfstrdat) 467 468 if ld.Debug['v'] != 0 { 469 fmt.Fprintf(ld.Bso, "%5.2f dwarf\n", obj.Cputime()) 470 } 471 472 if ld.Linkmode == ld.LinkExternal { 473 ld.Elfemitreloc() 474 } 475 } 476 } 477 } 478 479 ld.Ctxt.Cursym = nil 480 if ld.Debug['v'] != 0 { 481 fmt.Fprintf(ld.Bso, "%5.2f header\n", obj.Cputime()) 482 } 483 ld.Bso.Flush() 484 ld.Cseek(0) 485 switch ld.HEADTYPE { 486 default: 487 488 case obj.Hlinux, 489 obj.Hfreebsd, 490 obj.Hnetbsd, 491 obj.Hopenbsd, 492 obj.Hsolaris, 493 obj.Hnacl: 494 ld.Asmbelf(int64(symo)) 495 } 496 497 ld.Cflush() 498 if ld.Debug['c'] != 0 { 499 fmt.Printf("textsize=%d\n", ld.Segtext.Filelen) 500 fmt.Printf("datsize=%d\n", ld.Segdata.Filelen) 501 fmt.Printf("bsssize=%d\n", ld.Segdata.Length-ld.Segdata.Filelen) 502 fmt.Printf("symsize=%d\n", ld.Symsize) 503 fmt.Printf("lcsize=%d\n", ld.Lcsize) 504 fmt.Printf("total=%d\n", ld.Segtext.Filelen+ld.Segdata.Length+uint64(ld.Symsize)+uint64(ld.Lcsize)) 505 } 506 }