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