github.com/sean-/go@v0.0.0-20151219100004-97f854cd7bb6/src/cmd/link/internal/arm64/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 arm64 32 33 import ( 34 "cmd/internal/obj" 35 "cmd/link/internal/ld" 36 "encoding/binary" 37 "fmt" 38 "log" 39 ) 40 41 func gentext() { 42 if !ld.DynlinkingGo() { 43 return 44 } 45 addmoduledata := ld.Linklookup(ld.Ctxt, "runtime.addmoduledata", 0) 46 if addmoduledata.Type == obj.STEXT { 47 // we're linking a module containing the runtime -> no need for 48 // an init function 49 return 50 } 51 addmoduledata.Reachable = true 52 initfunc := ld.Linklookup(ld.Ctxt, "go.link.addmoduledata", 0) 53 initfunc.Type = obj.STEXT 54 initfunc.Local = true 55 initfunc.Reachable = true 56 o := func(op uint32) { 57 ld.Adduint32(ld.Ctxt, initfunc, op) 58 } 59 // 0000000000000000 <local.dso_init>: 60 // 0: 90000000 adrp x0, 0 <runtime.firstmoduledata> 61 // 0: R_AARCH64_ADR_PREL_PG_HI21 local.moduledata 62 // 4: 91000000 add x0, x0, #0x0 63 // 4: R_AARCH64_ADD_ABS_LO12_NC local.moduledata 64 o(0x90000000) 65 o(0x91000000) 66 rel := ld.Addrel(initfunc) 67 rel.Off = 0 68 rel.Siz = 8 69 rel.Sym = ld.Ctxt.Moduledata 70 rel.Type = obj.R_ADDRARM64 71 72 // 8: 14000000 bl 0 <runtime.addmoduledata> 73 // 8: R_AARCH64_CALL26 runtime.addmoduledata 74 o(0x14000000) 75 rel = ld.Addrel(initfunc) 76 rel.Off = 8 77 rel.Siz = 4 78 rel.Sym = ld.Linklookup(ld.Ctxt, "runtime.addmoduledata", 0) 79 rel.Type = obj.R_CALLARM64 // Really should be R_AARCH64_JUMP26 but doesn't seem to make any difference 80 81 if ld.Ctxt.Etextp != nil { 82 ld.Ctxt.Etextp.Next = initfunc 83 } else { 84 ld.Ctxt.Textp = initfunc 85 } 86 ld.Ctxt.Etextp = initfunc 87 initarray_entry := ld.Linklookup(ld.Ctxt, "go.link.addmoduledatainit", 0) 88 initarray_entry.Reachable = true 89 initarray_entry.Local = true 90 initarray_entry.Type = obj.SINITARR 91 ld.Addaddr(ld.Ctxt, initarray_entry, initfunc) 92 } 93 94 func adddynrela(rel *ld.LSym, s *ld.LSym, r *ld.Reloc) { 95 log.Fatalf("adddynrela not implemented") 96 } 97 98 func adddynrel(s *ld.LSym, r *ld.Reloc) { 99 log.Fatalf("adddynrel not implemented") 100 } 101 102 func elfreloc1(r *ld.Reloc, sectoff int64) int { 103 ld.Thearch.Vput(uint64(sectoff)) 104 105 elfsym := r.Xsym.ElfsymForReloc() 106 switch r.Type { 107 default: 108 return -1 109 110 case obj.R_ADDR: 111 switch r.Siz { 112 case 4: 113 ld.Thearch.Vput(ld.R_AARCH64_ABS32 | uint64(elfsym)<<32) 114 case 8: 115 ld.Thearch.Vput(ld.R_AARCH64_ABS64 | uint64(elfsym)<<32) 116 default: 117 return -1 118 } 119 120 case obj.R_ADDRARM64: 121 // two relocations: R_AARCH64_ADR_PREL_PG_HI21 and R_AARCH64_ADD_ABS_LO12_NC 122 ld.Thearch.Vput(ld.R_AARCH64_ADR_PREL_PG_HI21 | uint64(elfsym)<<32) 123 ld.Thearch.Vput(uint64(r.Xadd)) 124 ld.Thearch.Vput(uint64(sectoff + 4)) 125 ld.Thearch.Vput(ld.R_AARCH64_ADD_ABS_LO12_NC | uint64(elfsym)<<32) 126 127 case obj.R_ARM64_TLS_LE: 128 ld.Thearch.Vput(ld.R_AARCH64_TLSLE_MOVW_TPREL_G0 | uint64(elfsym)<<32) 129 130 case obj.R_ARM64_TLS_IE: 131 ld.Thearch.Vput(ld.R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21 | uint64(elfsym)<<32) 132 ld.Thearch.Vput(uint64(r.Xadd)) 133 ld.Thearch.Vput(uint64(sectoff + 4)) 134 ld.Thearch.Vput(ld.R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC | uint64(elfsym)<<32) 135 136 case obj.R_ARM64_GOTPCREL: 137 ld.Thearch.Vput(ld.R_AARCH64_ADR_GOT_PAGE | uint64(elfsym)<<32) 138 ld.Thearch.Vput(uint64(r.Xadd)) 139 ld.Thearch.Vput(uint64(sectoff + 4)) 140 ld.Thearch.Vput(ld.R_AARCH64_LD64_GOT_LO12_NC | uint64(elfsym)<<32) 141 142 case obj.R_CALLARM64: 143 if r.Siz != 4 { 144 return -1 145 } 146 ld.Thearch.Vput(ld.R_AARCH64_CALL26 | uint64(elfsym)<<32) 147 148 } 149 ld.Thearch.Vput(uint64(r.Xadd)) 150 151 return 0 152 } 153 154 func elfsetupplt() { 155 // TODO(aram) 156 return 157 } 158 159 func machoreloc1(r *ld.Reloc, sectoff int64) int { 160 var v uint32 161 162 rs := r.Xsym 163 164 // ld64 has a bug handling MACHO_ARM64_RELOC_UNSIGNED with !extern relocation. 165 // see cmd/internal/ld/data.go for details. The workarond is that don't use !extern 166 // UNSIGNED relocation at all. 167 if rs.Type == obj.SHOSTOBJ || r.Type == obj.R_CALLARM64 || r.Type == obj.R_ADDRARM64 || r.Type == obj.R_ADDR { 168 if rs.Dynid < 0 { 169 ld.Diag("reloc %d to non-macho symbol %s type=%d", r.Type, rs.Name, rs.Type) 170 return -1 171 } 172 173 v = uint32(rs.Dynid) 174 v |= 1 << 27 // external relocation 175 } else { 176 v = uint32(rs.Sect.Extnum) 177 if v == 0 { 178 ld.Diag("reloc %d to symbol %s in non-macho section %s type=%d", r.Type, rs.Name, rs.Sect.Name, rs.Type) 179 return -1 180 } 181 } 182 183 switch r.Type { 184 default: 185 return -1 186 187 case obj.R_ADDR: 188 v |= ld.MACHO_ARM64_RELOC_UNSIGNED << 28 189 190 case obj.R_CALLARM64: 191 if r.Xadd != 0 { 192 ld.Diag("ld64 doesn't allow BR26 reloc with non-zero addend: %s+%d", rs.Name, r.Xadd) 193 } 194 195 v |= 1 << 24 // pc-relative bit 196 v |= ld.MACHO_ARM64_RELOC_BRANCH26 << 28 197 198 case obj.R_ADDRARM64: 199 r.Siz = 4 200 // Two relocation entries: MACHO_ARM64_RELOC_PAGEOFF12 MACHO_ARM64_RELOC_PAGE21 201 // if r.Xadd is non-zero, add two MACHO_ARM64_RELOC_ADDEND. 202 if r.Xadd != 0 { 203 ld.Thearch.Lput(uint32(sectoff + 4)) 204 ld.Thearch.Lput((ld.MACHO_ARM64_RELOC_ADDEND << 28) | (2 << 25) | uint32(r.Xadd&0xffffff)) 205 } 206 ld.Thearch.Lput(uint32(sectoff + 4)) 207 ld.Thearch.Lput(v | (ld.MACHO_ARM64_RELOC_PAGEOFF12 << 28) | (2 << 25)) 208 if r.Xadd != 0 { 209 ld.Thearch.Lput(uint32(sectoff)) 210 ld.Thearch.Lput((ld.MACHO_ARM64_RELOC_ADDEND << 28) | (2 << 25) | uint32(r.Xadd&0xffffff)) 211 } 212 v |= 1 << 24 // pc-relative bit 213 v |= ld.MACHO_ARM64_RELOC_PAGE21 << 28 214 } 215 216 switch r.Siz { 217 default: 218 return -1 219 220 case 1: 221 v |= 0 << 25 222 223 case 2: 224 v |= 1 << 25 225 226 case 4: 227 v |= 2 << 25 228 229 case 8: 230 v |= 3 << 25 231 } 232 233 ld.Thearch.Lput(uint32(sectoff)) 234 ld.Thearch.Lput(v) 235 return 0 236 } 237 238 func archreloc(r *ld.Reloc, s *ld.LSym, val *int64) int { 239 if ld.Linkmode == ld.LinkExternal { 240 switch r.Type { 241 default: 242 return -1 243 244 case obj.R_ARM64_GOTPCREL: 245 var o1, o2 uint32 246 if ld.Ctxt.Arch.ByteOrder == binary.BigEndian { 247 o1 = uint32(*val >> 32) 248 o2 = uint32(*val) 249 } else { 250 o1 = uint32(*val) 251 o2 = uint32(*val >> 32) 252 } 253 // Any relocation against a function symbol is redirected to 254 // be against a local symbol instead (see putelfsym in 255 // symtab.go) but unfortunately the system linker was buggy 256 // when confronted with a R_AARCH64_ADR_GOT_PAGE relocation 257 // against a local symbol until May 2015 258 // (https://sourceware.org/bugzilla/show_bug.cgi?id=18270). So 259 // we convert the adrp; ld64 + R_ARM64_GOTPCREL into adrp; 260 // add + R_ADDRARM64. 261 if !(r.Sym.Version != 0 || (r.Sym.Type&obj.SHIDDEN != 0) || r.Sym.Local) && r.Sym.Type == obj.STEXT && ld.DynlinkingGo() { 262 if o2&0xffc00000 != 0xf9400000 { 263 ld.Ctxt.Diag("R_ARM64_GOTPCREL against unexpected instruction %x", o2) 264 } 265 o2 = 0x91000000 | (o2 & 0x000003ff) 266 r.Type = obj.R_ADDRARM64 267 } 268 if ld.Ctxt.Arch.ByteOrder == binary.BigEndian { 269 *val = int64(o1)<<32 | int64(o2) 270 } else { 271 *val = int64(o2)<<32 | int64(o1) 272 } 273 fallthrough 274 275 case obj.R_ADDRARM64: 276 r.Done = 0 277 278 // set up addend for eventual relocation via outer symbol. 279 rs := r.Sym 280 r.Xadd = r.Add 281 for rs.Outer != nil { 282 r.Xadd += ld.Symaddr(rs) - ld.Symaddr(rs.Outer) 283 rs = rs.Outer 284 } 285 286 if rs.Type != obj.SHOSTOBJ && rs.Type != obj.SDYNIMPORT && rs.Sect == nil { 287 ld.Diag("missing section for %s", rs.Name) 288 } 289 r.Xsym = rs 290 291 // Note: ld64 currently has a bug that any non-zero addend for BR26 relocation 292 // will make the linking fail because it thinks the code is not PIC even though 293 // the BR26 relocation should be fully resolved at link time. 294 // That is the reason why the next if block is disabled. When the bug in ld64 295 // is fixed, we can enable this block and also enable duff's device in cmd/7g. 296 if false && ld.HEADTYPE == obj.Hdarwin { 297 var o0, o1 uint32 298 299 if ld.Ctxt.Arch.ByteOrder == binary.BigEndian { 300 o0 = uint32(*val >> 32) 301 o1 = uint32(*val) 302 } else { 303 o0 = uint32(*val) 304 o1 = uint32(*val >> 32) 305 } 306 // Mach-O wants the addend to be encoded in the instruction 307 // Note that although Mach-O supports ARM64_RELOC_ADDEND, it 308 // can only encode 24-bit of signed addend, but the instructions 309 // supports 33-bit of signed addend, so we always encode the 310 // addend in place. 311 o0 |= (uint32((r.Xadd>>12)&3) << 29) | (uint32((r.Xadd>>12>>2)&0x7ffff) << 5) 312 o1 |= uint32(r.Xadd&0xfff) << 10 313 r.Xadd = 0 314 315 // when laid out, the instruction order must always be o1, o2. 316 if ld.Ctxt.Arch.ByteOrder == binary.BigEndian { 317 *val = int64(o0)<<32 | int64(o1) 318 } else { 319 *val = int64(o1)<<32 | int64(o0) 320 } 321 } 322 323 return 0 324 325 case obj.R_CALLARM64, 326 obj.R_ARM64_TLS_LE, 327 obj.R_ARM64_TLS_IE: 328 r.Done = 0 329 r.Xsym = r.Sym 330 r.Xadd = r.Add 331 return 0 332 } 333 } 334 335 switch r.Type { 336 case obj.R_CONST: 337 *val = r.Add 338 return 0 339 340 case obj.R_GOTOFF: 341 *val = ld.Symaddr(r.Sym) + r.Add - ld.Symaddr(ld.Linklookup(ld.Ctxt, ".got", 0)) 342 return 0 343 344 case obj.R_ADDRARM64: 345 t := ld.Symaddr(r.Sym) + r.Add - ((s.Value + int64(r.Off)) &^ 0xfff) 346 if t >= 1<<32 || t < -1<<32 { 347 ld.Diag("program too large, address relocation distance = %d", t) 348 } 349 350 var o0, o1 uint32 351 352 if ld.Ctxt.Arch.ByteOrder == binary.BigEndian { 353 o0 = uint32(*val >> 32) 354 o1 = uint32(*val) 355 } else { 356 o0 = uint32(*val) 357 o1 = uint32(*val >> 32) 358 } 359 360 o0 |= (uint32((t>>12)&3) << 29) | (uint32((t>>12>>2)&0x7ffff) << 5) 361 o1 |= uint32(t&0xfff) << 10 362 363 // when laid out, the instruction order must always be o1, o2. 364 if ld.Ctxt.Arch.ByteOrder == binary.BigEndian { 365 *val = int64(o0)<<32 | int64(o1) 366 } else { 367 *val = int64(o1)<<32 | int64(o0) 368 } 369 return 0 370 371 case obj.R_ARM64_TLS_LE: 372 r.Done = 0 373 if ld.HEADTYPE != obj.Hlinux { 374 ld.Diag("TLS reloc on unsupported OS %s", ld.Headstr(int(ld.HEADTYPE))) 375 } 376 // The TCB is two pointers. This is not documented anywhere, but is 377 // de facto part of the ABI. 378 v := r.Sym.Value + int64(2*ld.Thearch.Ptrsize) 379 if v < 0 || v >= 32678 { 380 ld.Diag("TLS offset out of range %d", v) 381 } 382 *val |= v << 5 383 return 0 384 385 case obj.R_CALLARM64: 386 t := (ld.Symaddr(r.Sym) + r.Add) - (s.Value + int64(r.Off)) 387 if t >= 1<<27 || t < -1<<27 { 388 ld.Diag("program too large, call relocation distance = %d", t) 389 } 390 *val |= (t >> 2) & 0x03ffffff 391 return 0 392 } 393 394 return -1 395 } 396 397 func archrelocvariant(r *ld.Reloc, s *ld.LSym, t int64) int64 { 398 log.Fatalf("unexpected relocation variant") 399 return -1 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.Codeblk(int64(sect.Vaddr), int64(sect.Length)) 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 machlink := uint32(0) 439 if ld.HEADTYPE == obj.Hdarwin { 440 if ld.Debug['v'] != 0 { 441 fmt.Fprintf(&ld.Bso, "%5.2f dwarf\n", obj.Cputime()) 442 } 443 444 dwarfoff := uint32(ld.Rnd(int64(uint64(ld.HEADR)+ld.Segtext.Length), int64(ld.INITRND)) + ld.Rnd(int64(ld.Segdata.Filelen), int64(ld.INITRND))) 445 ld.Cseek(int64(dwarfoff)) 446 447 ld.Segdwarf.Fileoff = uint64(ld.Cpos()) 448 ld.Dwarfemitdebugsections() 449 ld.Segdwarf.Filelen = uint64(ld.Cpos()) - ld.Segdwarf.Fileoff 450 451 machlink = uint32(ld.Domacholink()) 452 } 453 454 /* output symbol table */ 455 ld.Symsize = 0 456 457 ld.Lcsize = 0 458 symo := uint32(0) 459 if ld.Debug['s'] == 0 { 460 // TODO: rationalize 461 if ld.Debug['v'] != 0 { 462 fmt.Fprintf(&ld.Bso, "%5.2f sym\n", obj.Cputime()) 463 } 464 ld.Bso.Flush() 465 switch ld.HEADTYPE { 466 default: 467 if ld.Iself { 468 symo = uint32(ld.Segdata.Fileoff + ld.Segdata.Filelen) 469 symo = uint32(ld.Rnd(int64(symo), int64(ld.INITRND))) 470 } 471 472 case obj.Hplan9: 473 symo = uint32(ld.Segdata.Fileoff + ld.Segdata.Filelen) 474 475 case obj.Hdarwin: 476 symo = uint32(ld.Segdwarf.Fileoff + uint64(ld.Rnd(int64(ld.Segdwarf.Filelen), int64(ld.INITRND))) + uint64(machlink)) 477 } 478 479 ld.Cseek(int64(symo)) 480 switch ld.HEADTYPE { 481 default: 482 if ld.Iself { 483 if ld.Debug['v'] != 0 { 484 fmt.Fprintf(&ld.Bso, "%5.2f elfsym\n", obj.Cputime()) 485 } 486 ld.Asmelfsym() 487 ld.Cflush() 488 ld.Cwrite(ld.Elfstrdat) 489 490 if ld.Debug['v'] != 0 { 491 fmt.Fprintf(&ld.Bso, "%5.2f dwarf\n", obj.Cputime()) 492 } 493 ld.Dwarfemitdebugsections() 494 495 if ld.Linkmode == ld.LinkExternal { 496 ld.Elfemitreloc() 497 } 498 } 499 500 case obj.Hplan9: 501 ld.Asmplan9sym() 502 ld.Cflush() 503 504 sym := ld.Linklookup(ld.Ctxt, "pclntab", 0) 505 if sym != nil { 506 ld.Lcsize = int32(len(sym.P)) 507 for i := 0; int32(i) < ld.Lcsize; i++ { 508 ld.Cput(uint8(sym.P[i])) 509 } 510 511 ld.Cflush() 512 } 513 514 case obj.Hdarwin: 515 if ld.Linkmode == ld.LinkExternal { 516 ld.Machoemitreloc() 517 } 518 } 519 } 520 521 ld.Ctxt.Cursym = nil 522 if ld.Debug['v'] != 0 { 523 fmt.Fprintf(&ld.Bso, "%5.2f header\n", obj.Cputime()) 524 } 525 ld.Bso.Flush() 526 ld.Cseek(0) 527 switch ld.HEADTYPE { 528 default: 529 case obj.Hplan9: /* plan 9 */ 530 ld.Thearch.Lput(0x647) /* magic */ 531 ld.Thearch.Lput(uint32(ld.Segtext.Filelen)) /* sizes */ 532 ld.Thearch.Lput(uint32(ld.Segdata.Filelen)) 533 ld.Thearch.Lput(uint32(ld.Segdata.Length - ld.Segdata.Filelen)) 534 ld.Thearch.Lput(uint32(ld.Symsize)) /* nsyms */ 535 ld.Thearch.Lput(uint32(ld.Entryvalue())) /* va of entry */ 536 ld.Thearch.Lput(0) 537 ld.Thearch.Lput(uint32(ld.Lcsize)) 538 539 case obj.Hlinux, 540 obj.Hfreebsd, 541 obj.Hnetbsd, 542 obj.Hopenbsd, 543 obj.Hnacl: 544 ld.Asmbelf(int64(symo)) 545 546 case obj.Hdarwin: 547 ld.Asmbmacho() 548 } 549 550 ld.Cflush() 551 if ld.Debug['c'] != 0 { 552 fmt.Printf("textsize=%d\n", ld.Segtext.Filelen) 553 fmt.Printf("datsize=%d\n", ld.Segdata.Filelen) 554 fmt.Printf("bsssize=%d\n", ld.Segdata.Length-ld.Segdata.Filelen) 555 fmt.Printf("symsize=%d\n", ld.Symsize) 556 fmt.Printf("lcsize=%d\n", ld.Lcsize) 557 fmt.Printf("total=%d\n", ld.Segtext.Filelen+ld.Segdata.Length+uint64(ld.Symsize)+uint64(ld.Lcsize)) 558 } 559 }