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