github.com/slayercat/go@v0.0.0-20170428012452-c51559813f61/src/cmd/link/internal/ld/data.go (about) 1 // Derived from Inferno utils/6l/obj.c and utils/6l/span.c 2 // https://bitbucket.org/inferno-os/inferno-os/src/default/utils/6l/obj.c 3 // https://bitbucket.org/inferno-os/inferno-os/src/default/utils/6l/span.c 4 // 5 // Copyright © 1994-1999 Lucent Technologies Inc. All rights reserved. 6 // Portions Copyright © 1995-1997 C H Forsyth (forsyth@terzarima.net) 7 // Portions Copyright © 1997-1999 Vita Nuova Limited 8 // Portions Copyright © 2000-2007 Vita Nuova Holdings Limited (www.vitanuova.com) 9 // Portions Copyright © 2004,2006 Bruce Ellis 10 // Portions Copyright © 2005-2007 C H Forsyth (forsyth@terzarima.net) 11 // Revisions Copyright © 2000-2007 Lucent Technologies Inc. and others 12 // Portions Copyright © 2009 The Go Authors. All rights reserved. 13 // 14 // Permission is hereby granted, free of charge, to any person obtaining a copy 15 // of this software and associated documentation files (the "Software"), to deal 16 // in the Software without restriction, including without limitation the rights 17 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 18 // copies of the Software, and to permit persons to whom the Software is 19 // furnished to do so, subject to the following conditions: 20 // 21 // The above copyright notice and this permission notice shall be included in 22 // all copies or substantial portions of the Software. 23 // 24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 25 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 26 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 27 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 28 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 29 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 30 // THE SOFTWARE. 31 32 package ld 33 34 import ( 35 "cmd/internal/gcprog" 36 "cmd/internal/objabi" 37 "cmd/internal/sys" 38 "fmt" 39 "log" 40 "os" 41 "sort" 42 "strconv" 43 "strings" 44 "sync" 45 ) 46 47 func Symgrow(s *Symbol, siz int64) { 48 if int64(int(siz)) != siz { 49 log.Fatalf("symgrow size %d too long", siz) 50 } 51 if int64(len(s.P)) >= siz { 52 return 53 } 54 if cap(s.P) < int(siz) { 55 p := make([]byte, 2*(siz+1)) 56 s.P = append(p[:0], s.P...) 57 } 58 s.P = s.P[:siz] 59 } 60 61 func Addrel(s *Symbol) *Reloc { 62 s.R = append(s.R, Reloc{}) 63 return &s.R[len(s.R)-1] 64 } 65 66 func setuintxx(ctxt *Link, s *Symbol, off int64, v uint64, wid int64) int64 { 67 if s.Type == 0 { 68 s.Type = SDATA 69 } 70 s.Attr |= AttrReachable 71 if s.Size < off+wid { 72 s.Size = off + wid 73 Symgrow(s, s.Size) 74 } 75 76 switch wid { 77 case 1: 78 s.P[off] = uint8(v) 79 case 2: 80 ctxt.Arch.ByteOrder.PutUint16(s.P[off:], uint16(v)) 81 case 4: 82 ctxt.Arch.ByteOrder.PutUint32(s.P[off:], uint32(v)) 83 case 8: 84 ctxt.Arch.ByteOrder.PutUint64(s.P[off:], v) 85 } 86 87 return off + wid 88 } 89 90 func Addbytes(s *Symbol, bytes []byte) int64 { 91 if s.Type == 0 { 92 s.Type = SDATA 93 } 94 s.Attr |= AttrReachable 95 s.P = append(s.P, bytes...) 96 s.Size = int64(len(s.P)) 97 98 return s.Size 99 } 100 101 func adduintxx(ctxt *Link, s *Symbol, v uint64, wid int) int64 { 102 off := s.Size 103 setuintxx(ctxt, s, off, v, int64(wid)) 104 return off 105 } 106 107 func Adduint8(ctxt *Link, s *Symbol, v uint8) int64 { 108 off := s.Size 109 if s.Type == 0 { 110 s.Type = SDATA 111 } 112 s.Attr |= AttrReachable 113 s.Size++ 114 s.P = append(s.P, v) 115 116 return off 117 } 118 119 func Adduint16(ctxt *Link, s *Symbol, v uint16) int64 { 120 return adduintxx(ctxt, s, uint64(v), 2) 121 } 122 123 func Adduint32(ctxt *Link, s *Symbol, v uint32) int64 { 124 return adduintxx(ctxt, s, uint64(v), 4) 125 } 126 127 func Adduint64(ctxt *Link, s *Symbol, v uint64) int64 { 128 return adduintxx(ctxt, s, v, 8) 129 } 130 131 func adduint(ctxt *Link, s *Symbol, v uint64) int64 { 132 return adduintxx(ctxt, s, v, SysArch.PtrSize) 133 } 134 135 func setuint8(ctxt *Link, s *Symbol, r int64, v uint8) int64 { 136 return setuintxx(ctxt, s, r, uint64(v), 1) 137 } 138 139 func setuint32(ctxt *Link, s *Symbol, r int64, v uint32) int64 { 140 return setuintxx(ctxt, s, r, uint64(v), 4) 141 } 142 143 func setuint(ctxt *Link, s *Symbol, r int64, v uint64) int64 { 144 return setuintxx(ctxt, s, r, v, int64(SysArch.PtrSize)) 145 } 146 147 func Addaddrplus(ctxt *Link, s *Symbol, t *Symbol, add int64) int64 { 148 if s.Type == 0 { 149 s.Type = SDATA 150 } 151 s.Attr |= AttrReachable 152 i := s.Size 153 s.Size += int64(ctxt.Arch.PtrSize) 154 Symgrow(s, s.Size) 155 r := Addrel(s) 156 r.Sym = t 157 r.Off = int32(i) 158 r.Siz = uint8(ctxt.Arch.PtrSize) 159 r.Type = objabi.R_ADDR 160 r.Add = add 161 return i + int64(r.Siz) 162 } 163 164 func Addpcrelplus(ctxt *Link, s *Symbol, t *Symbol, add int64) int64 { 165 if s.Type == 0 { 166 s.Type = SDATA 167 } 168 s.Attr |= AttrReachable 169 i := s.Size 170 s.Size += 4 171 Symgrow(s, s.Size) 172 r := Addrel(s) 173 r.Sym = t 174 r.Off = int32(i) 175 r.Add = add 176 r.Type = objabi.R_PCREL 177 r.Siz = 4 178 if SysArch.Family == sys.S390X { 179 r.Variant = RV_390_DBL 180 } 181 return i + int64(r.Siz) 182 } 183 184 func Addaddr(ctxt *Link, s *Symbol, t *Symbol) int64 { 185 return Addaddrplus(ctxt, s, t, 0) 186 } 187 188 func setaddrplus(ctxt *Link, s *Symbol, off int64, t *Symbol, add int64) int64 { 189 if s.Type == 0 { 190 s.Type = SDATA 191 } 192 s.Attr |= AttrReachable 193 if off+int64(ctxt.Arch.PtrSize) > s.Size { 194 s.Size = off + int64(ctxt.Arch.PtrSize) 195 Symgrow(s, s.Size) 196 } 197 198 r := Addrel(s) 199 r.Sym = t 200 r.Off = int32(off) 201 r.Siz = uint8(ctxt.Arch.PtrSize) 202 r.Type = objabi.R_ADDR 203 r.Add = add 204 return off + int64(r.Siz) 205 } 206 207 func setaddr(ctxt *Link, s *Symbol, off int64, t *Symbol) int64 { 208 return setaddrplus(ctxt, s, off, t, 0) 209 } 210 211 func addsize(ctxt *Link, s *Symbol, t *Symbol) int64 { 212 if s.Type == 0 { 213 s.Type = SDATA 214 } 215 s.Attr |= AttrReachable 216 i := s.Size 217 s.Size += int64(ctxt.Arch.PtrSize) 218 Symgrow(s, s.Size) 219 r := Addrel(s) 220 r.Sym = t 221 r.Off = int32(i) 222 r.Siz = uint8(ctxt.Arch.PtrSize) 223 r.Type = objabi.R_SIZE 224 return i + int64(r.Siz) 225 } 226 227 func addaddrplus4(ctxt *Link, s *Symbol, t *Symbol, add int64) int64 { 228 if s.Type == 0 { 229 s.Type = SDATA 230 } 231 s.Attr |= AttrReachable 232 i := s.Size 233 s.Size += 4 234 Symgrow(s, s.Size) 235 r := Addrel(s) 236 r.Sym = t 237 r.Off = int32(i) 238 r.Siz = 4 239 r.Type = objabi.R_ADDR 240 r.Add = add 241 return i + int64(r.Siz) 242 } 243 244 /* 245 * divide-and-conquer list-link (by Sub) sort of Symbol* by Value. 246 * Used for sub-symbols when loading host objects (see e.g. ldelf.go). 247 */ 248 249 func listsort(l *Symbol) *Symbol { 250 if l == nil || l.Sub == nil { 251 return l 252 } 253 254 l1 := l 255 l2 := l 256 for { 257 l2 = l2.Sub 258 if l2 == nil { 259 break 260 } 261 l2 = l2.Sub 262 if l2 == nil { 263 break 264 } 265 l1 = l1.Sub 266 } 267 268 l2 = l1.Sub 269 l1.Sub = nil 270 l1 = listsort(l) 271 l2 = listsort(l2) 272 273 /* set up lead element */ 274 if l1.Value < l2.Value { 275 l = l1 276 l1 = l1.Sub 277 } else { 278 l = l2 279 l2 = l2.Sub 280 } 281 282 le := l 283 284 for { 285 if l1 == nil { 286 for l2 != nil { 287 le.Sub = l2 288 le = l2 289 l2 = l2.Sub 290 } 291 292 le.Sub = nil 293 break 294 } 295 296 if l2 == nil { 297 for l1 != nil { 298 le.Sub = l1 299 le = l1 300 l1 = l1.Sub 301 } 302 303 break 304 } 305 306 if l1.Value < l2.Value { 307 le.Sub = l1 308 le = l1 309 l1 = l1.Sub 310 } else { 311 le.Sub = l2 312 le = l2 313 l2 = l2.Sub 314 } 315 } 316 317 le.Sub = nil 318 return l 319 } 320 321 // isRuntimeDepPkg returns whether pkg is the runtime package or its dependency 322 func isRuntimeDepPkg(pkg string) bool { 323 switch pkg { 324 case "runtime", 325 "sync/atomic": // runtime may call to sync/atomic, due to go:linkname 326 return true 327 } 328 return strings.HasPrefix(pkg, "runtime/internal/") && !strings.HasSuffix(pkg, "_test") 329 } 330 331 // detect too-far jumps in function s, and add trampolines if necessary 332 // ARM supports trampoline insertion for internal and external linking 333 // PPC64 & PPC64LE support trampoline insertion for internal linking only 334 func trampoline(ctxt *Link, s *Symbol) { 335 if Thearch.Trampoline == nil { 336 return // no need or no support of trampolines on this arch 337 } 338 339 if Linkmode == LinkExternal && SysArch.Family == sys.PPC64 { 340 return 341 } 342 343 for ri := range s.R { 344 r := &s.R[ri] 345 if !r.Type.IsDirectJump() { 346 continue 347 } 348 if Symaddr(r.Sym) == 0 && r.Sym.Type != SDYNIMPORT { 349 if r.Sym.File != s.File { 350 if !isRuntimeDepPkg(s.File) || !isRuntimeDepPkg(r.Sym.File) { 351 Errorf(s, "unresolved inter-package jump to %s(%s)", r.Sym, r.Sym.File) 352 } 353 // runtime and its dependent packages may call to each other. 354 // they are fine, as they will be laid down together. 355 } 356 continue 357 } 358 359 Thearch.Trampoline(ctxt, r, s) 360 } 361 362 } 363 364 // resolve relocations in s. 365 func relocsym(ctxt *Link, s *Symbol) { 366 var r *Reloc 367 var rs *Symbol 368 var i16 int16 369 var off int32 370 var siz int32 371 var fl int32 372 var o int64 373 374 for ri := int32(0); ri < int32(len(s.R)); ri++ { 375 r = &s.R[ri] 376 377 r.Done = 1 378 off = r.Off 379 siz = int32(r.Siz) 380 if off < 0 || off+siz > int32(len(s.P)) { 381 rname := "" 382 if r.Sym != nil { 383 rname = r.Sym.Name 384 } 385 Errorf(s, "invalid relocation %s: %d+%d not in [%d,%d)", rname, off, siz, 0, len(s.P)) 386 continue 387 } 388 389 if r.Sym != nil && (r.Sym.Type&(SMASK|SHIDDEN) == 0 || r.Sym.Type&SMASK == SXREF) { 390 // When putting the runtime but not main into a shared library 391 // these symbols are undefined and that's OK. 392 if Buildmode == BuildmodeShared { 393 if r.Sym.Name == "main.main" || r.Sym.Name == "main.init" { 394 r.Sym.Type = SDYNIMPORT 395 } else if strings.HasPrefix(r.Sym.Name, "go.info.") { 396 // Skip go.info symbols. They are only needed to communicate 397 // DWARF info between the compiler and linker. 398 continue 399 } 400 } else { 401 Errorf(s, "relocation target %s not defined", r.Sym.Name) 402 continue 403 } 404 } 405 406 if r.Type >= 256 { 407 continue 408 } 409 if r.Siz == 0 { // informational relocation - no work to do 410 continue 411 } 412 413 // We need to be able to reference dynimport symbols when linking against 414 // shared libraries, and Solaris needs it always 415 if Headtype != objabi.Hsolaris && r.Sym != nil && r.Sym.Type == SDYNIMPORT && !ctxt.DynlinkingGo() { 416 if !(SysArch.Family == sys.PPC64 && Linkmode == LinkExternal && r.Sym.Name == ".TOC.") { 417 Errorf(s, "unhandled relocation for %s (type %d rtype %d)", r.Sym.Name, r.Sym.Type, r.Type) 418 } 419 } 420 if r.Sym != nil && r.Sym.Type != STLSBSS && r.Type != objabi.R_WEAKADDROFF && !r.Sym.Attr.Reachable() { 421 Errorf(s, "unreachable sym in relocation: %s", r.Sym.Name) 422 } 423 424 // TODO(mundaym): remove this special case - see issue 14218. 425 if SysArch.Family == sys.S390X { 426 switch r.Type { 427 case objabi.R_PCRELDBL: 428 r.Type = objabi.R_PCREL 429 r.Variant = RV_390_DBL 430 case objabi.R_CALL: 431 r.Variant = RV_390_DBL 432 } 433 } 434 435 switch r.Type { 436 default: 437 switch siz { 438 default: 439 Errorf(s, "bad reloc size %#x for %s", uint32(siz), r.Sym.Name) 440 case 1: 441 o = int64(s.P[off]) 442 case 2: 443 o = int64(ctxt.Arch.ByteOrder.Uint16(s.P[off:])) 444 case 4: 445 o = int64(ctxt.Arch.ByteOrder.Uint32(s.P[off:])) 446 case 8: 447 o = int64(ctxt.Arch.ByteOrder.Uint64(s.P[off:])) 448 } 449 if Thearch.Archreloc(ctxt, r, s, &o) < 0 { 450 Errorf(s, "unknown reloc to %v: %v", r.Sym.Name, r.Type) 451 } 452 453 case objabi.R_TLS_LE: 454 isAndroidX86 := objabi.GOOS == "android" && (SysArch.InFamily(sys.AMD64, sys.I386)) 455 456 if Linkmode == LinkExternal && Iself && !isAndroidX86 { 457 r.Done = 0 458 if r.Sym == nil { 459 r.Sym = ctxt.Tlsg 460 } 461 r.Xsym = r.Sym 462 r.Xadd = r.Add 463 o = 0 464 if SysArch.Family != sys.AMD64 { 465 o = r.Add 466 } 467 break 468 } 469 470 if Iself && SysArch.Family == sys.ARM { 471 // On ELF ARM, the thread pointer is 8 bytes before 472 // the start of the thread-local data block, so add 8 473 // to the actual TLS offset (r->sym->value). 474 // This 8 seems to be a fundamental constant of 475 // ELF on ARM (or maybe Glibc on ARM); it is not 476 // related to the fact that our own TLS storage happens 477 // to take up 8 bytes. 478 o = 8 + r.Sym.Value 479 } else if Iself || Headtype == objabi.Hplan9 || Headtype == objabi.Hdarwin || isAndroidX86 { 480 o = int64(ctxt.Tlsoffset) + r.Add 481 } else if Headtype == objabi.Hwindows { 482 o = r.Add 483 } else { 484 log.Fatalf("unexpected R_TLS_LE relocation for %v", Headtype) 485 } 486 487 case objabi.R_TLS_IE: 488 isAndroidX86 := objabi.GOOS == "android" && (SysArch.InFamily(sys.AMD64, sys.I386)) 489 490 if Linkmode == LinkExternal && Iself && !isAndroidX86 { 491 r.Done = 0 492 if r.Sym == nil { 493 r.Sym = ctxt.Tlsg 494 } 495 r.Xsym = r.Sym 496 r.Xadd = r.Add 497 o = 0 498 if SysArch.Family != sys.AMD64 { 499 o = r.Add 500 } 501 break 502 } 503 if Buildmode == BuildmodePIE && Iself { 504 // We are linking the final executable, so we 505 // can optimize any TLS IE relocation to LE. 506 if Thearch.TLSIEtoLE == nil { 507 log.Fatalf("internal linking of TLS IE not supported on %v", SysArch.Family) 508 } 509 Thearch.TLSIEtoLE(s, int(off), int(r.Siz)) 510 o = int64(ctxt.Tlsoffset) 511 // TODO: o += r.Add when SysArch.Family != sys.AMD64? 512 // Why do we treat r.Add differently on AMD64? 513 // Is the external linker using Xadd at all? 514 } else { 515 log.Fatalf("cannot handle R_TLS_IE (sym %s) when linking internally", s.Name) 516 } 517 518 case objabi.R_ADDR: 519 if Linkmode == LinkExternal && r.Sym.Type != SCONST { 520 r.Done = 0 521 522 // set up addend for eventual relocation via outer symbol. 523 rs = r.Sym 524 525 r.Xadd = r.Add 526 for rs.Outer != nil { 527 r.Xadd += Symaddr(rs) - Symaddr(rs.Outer) 528 rs = rs.Outer 529 } 530 531 if rs.Type != SHOSTOBJ && rs.Type != SDYNIMPORT && rs.Sect == nil { 532 Errorf(s, "missing section for relocation target %s", rs.Name) 533 } 534 r.Xsym = rs 535 536 o = r.Xadd 537 if Iself { 538 if SysArch.Family == sys.AMD64 { 539 o = 0 540 } 541 } else if Headtype == objabi.Hdarwin { 542 // ld64 for arm64 has a bug where if the address pointed to by o exists in the 543 // symbol table (dynid >= 0), or is inside a symbol that exists in the symbol 544 // table, then it will add o twice into the relocated value. 545 // The workaround is that on arm64 don't ever add symaddr to o and always use 546 // extern relocation by requiring rs->dynid >= 0. 547 if rs.Type != SHOSTOBJ { 548 if SysArch.Family == sys.ARM64 && rs.Dynid < 0 { 549 Errorf(s, "R_ADDR reloc to %s+%d is not supported on darwin/arm64", rs.Name, o) 550 } 551 if SysArch.Family != sys.ARM64 { 552 o += Symaddr(rs) 553 } 554 } 555 } else if Headtype == objabi.Hwindows { 556 // nothing to do 557 } else { 558 Errorf(s, "unhandled pcrel relocation to %s on %v", rs.Name, Headtype) 559 } 560 561 break 562 } 563 564 o = Symaddr(r.Sym) + r.Add 565 566 // On amd64, 4-byte offsets will be sign-extended, so it is impossible to 567 // access more than 2GB of static data; fail at link time is better than 568 // fail at runtime. See https://golang.org/issue/7980. 569 // Instead of special casing only amd64, we treat this as an error on all 570 // 64-bit architectures so as to be future-proof. 571 if int32(o) < 0 && SysArch.PtrSize > 4 && siz == 4 { 572 Errorf(s, "non-pc-relative relocation address for %s is too big: %#x (%#x + %#x)", r.Sym.Name, uint64(o), Symaddr(r.Sym), r.Add) 573 errorexit() 574 } 575 576 case objabi.R_DWARFREF: 577 if r.Sym.Sect == nil { 578 Errorf(s, "missing DWARF section for relocation target %s", r.Sym.Name) 579 } 580 if Linkmode == LinkExternal { 581 r.Done = 0 582 // PE code emits IMAGE_REL_I386_SECREL and IMAGE_REL_AMD64_SECREL 583 // for R_DWARFREF relocations, while R_ADDR is replaced with 584 // IMAGE_REL_I386_DIR32, IMAGE_REL_AMD64_ADDR64 and IMAGE_REL_AMD64_ADDR32. 585 // Do not replace R_DWARFREF with R_ADDR for windows - 586 // let PE code emit correct relocations. 587 if Headtype != objabi.Hwindows { 588 r.Type = objabi.R_ADDR 589 } 590 591 r.Xsym = ctxt.Syms.ROLookup(r.Sym.Sect.Name, 0) 592 r.Xadd = r.Add + Symaddr(r.Sym) - int64(r.Sym.Sect.Vaddr) 593 o = r.Xadd 594 rs = r.Xsym 595 if Iself && SysArch.Family == sys.AMD64 { 596 o = 0 597 } 598 break 599 } 600 o = Symaddr(r.Sym) + r.Add - int64(r.Sym.Sect.Vaddr) 601 602 case objabi.R_WEAKADDROFF: 603 if !r.Sym.Attr.Reachable() { 604 continue 605 } 606 fallthrough 607 case objabi.R_ADDROFF: 608 // The method offset tables using this relocation expect the offset to be relative 609 // to the start of the first text section, even if there are multiple. 610 611 if r.Sym.Sect.Name == ".text" { 612 o = Symaddr(r.Sym) - int64(Segtext.Sections[0].Vaddr) + r.Add 613 } else { 614 o = Symaddr(r.Sym) - int64(r.Sym.Sect.Vaddr) + r.Add 615 } 616 617 // r->sym can be null when CALL $(constant) is transformed from absolute PC to relative PC call. 618 case objabi.R_GOTPCREL: 619 if ctxt.DynlinkingGo() && Headtype == objabi.Hdarwin && r.Sym != nil && r.Sym.Type != SCONST { 620 r.Done = 0 621 r.Xadd = r.Add 622 r.Xadd -= int64(r.Siz) // relative to address after the relocated chunk 623 r.Xsym = r.Sym 624 625 o = r.Xadd 626 o += int64(r.Siz) 627 break 628 } 629 fallthrough 630 case objabi.R_CALL, objabi.R_PCREL: 631 if Linkmode == LinkExternal && r.Sym != nil && r.Sym.Type != SCONST && (r.Sym.Sect != s.Sect || r.Type == objabi.R_GOTPCREL) { 632 r.Done = 0 633 634 // set up addend for eventual relocation via outer symbol. 635 rs = r.Sym 636 637 r.Xadd = r.Add 638 for rs.Outer != nil { 639 r.Xadd += Symaddr(rs) - Symaddr(rs.Outer) 640 rs = rs.Outer 641 } 642 643 r.Xadd -= int64(r.Siz) // relative to address after the relocated chunk 644 if rs.Type != SHOSTOBJ && rs.Type != SDYNIMPORT && rs.Sect == nil { 645 Errorf(s, "missing section for relocation target %s", rs.Name) 646 } 647 r.Xsym = rs 648 649 o = r.Xadd 650 if Iself { 651 if SysArch.Family == sys.AMD64 { 652 o = 0 653 } 654 } else if Headtype == objabi.Hdarwin { 655 if r.Type == objabi.R_CALL { 656 if rs.Type != SHOSTOBJ { 657 o += int64(uint64(Symaddr(rs)) - rs.Sect.Vaddr) 658 } 659 o -= int64(r.Off) // relative to section offset, not symbol 660 } else if SysArch.Family == sys.ARM { 661 // see ../arm/asm.go:/machoreloc1 662 o += Symaddr(rs) - int64(s.Value) - int64(r.Off) 663 } else { 664 o += int64(r.Siz) 665 } 666 } else if Headtype == objabi.Hwindows && SysArch.Family == sys.AMD64 { // only amd64 needs PCREL 667 // PE/COFF's PC32 relocation uses the address after the relocated 668 // bytes as the base. Compensate by skewing the addend. 669 o += int64(r.Siz) 670 } else { 671 Errorf(s, "unhandled pcrel relocation to %s on %v", rs.Name, Headtype) 672 } 673 674 break 675 } 676 677 o = 0 678 if r.Sym != nil { 679 o += Symaddr(r.Sym) 680 } 681 682 o += r.Add - (s.Value + int64(r.Off) + int64(r.Siz)) 683 684 case objabi.R_SIZE: 685 o = r.Sym.Size + r.Add 686 } 687 688 if r.Variant != RV_NONE { 689 o = Thearch.Archrelocvariant(ctxt, r, s, o) 690 } 691 692 if false { 693 nam := "<nil>" 694 if r.Sym != nil { 695 nam = r.Sym.Name 696 } 697 fmt.Printf("relocate %s %#x (%#x+%#x, size %d) => %s %#x +%#x [type %d/%d, %x]\n", s.Name, s.Value+int64(off), s.Value, r.Off, r.Siz, nam, Symaddr(r.Sym), r.Add, r.Type, r.Variant, o) 698 } 699 switch siz { 700 default: 701 Errorf(s, "bad reloc size %#x for %s", uint32(siz), r.Sym.Name) 702 fallthrough 703 704 // TODO(rsc): Remove. 705 case 1: 706 s.P[off] = byte(int8(o)) 707 708 case 2: 709 if o != int64(int16(o)) { 710 Errorf(s, "relocation address for %s is too big: %#x", r.Sym.Name, o) 711 } 712 i16 = int16(o) 713 ctxt.Arch.ByteOrder.PutUint16(s.P[off:], uint16(i16)) 714 715 case 4: 716 if r.Type == objabi.R_PCREL || r.Type == objabi.R_CALL { 717 if o != int64(int32(o)) { 718 Errorf(s, "pc-relative relocation address for %s is too big: %#x", r.Sym.Name, o) 719 } 720 } else { 721 if o != int64(int32(o)) && o != int64(uint32(o)) { 722 Errorf(s, "non-pc-relative relocation address for %s is too big: %#x", r.Sym.Name, uint64(o)) 723 } 724 } 725 726 fl = int32(o) 727 ctxt.Arch.ByteOrder.PutUint32(s.P[off:], uint32(fl)) 728 729 case 8: 730 ctxt.Arch.ByteOrder.PutUint64(s.P[off:], uint64(o)) 731 } 732 } 733 } 734 735 func (ctxt *Link) reloc() { 736 if ctxt.Debugvlog != 0 { 737 ctxt.Logf("%5.2f reloc\n", Cputime()) 738 } 739 740 for _, s := range ctxt.Textp { 741 relocsym(ctxt, s) 742 } 743 for _, sym := range datap { 744 relocsym(ctxt, sym) 745 } 746 for _, s := range dwarfp { 747 relocsym(ctxt, s) 748 } 749 } 750 751 func dynrelocsym(ctxt *Link, s *Symbol) { 752 if Headtype == objabi.Hwindows && Linkmode != LinkExternal { 753 rel := ctxt.Syms.Lookup(".rel", 0) 754 if s == rel { 755 return 756 } 757 for ri := 0; ri < len(s.R); ri++ { 758 r := &s.R[ri] 759 targ := r.Sym 760 if targ == nil { 761 continue 762 } 763 if !targ.Attr.Reachable() { 764 if r.Type == objabi.R_WEAKADDROFF { 765 continue 766 } 767 Errorf(s, "dynamic relocation to unreachable symbol %s", targ.Name) 768 } 769 if r.Sym.Plt == -2 && r.Sym.Got != -2 { // make dynimport JMP table for PE object files. 770 targ.Plt = int32(rel.Size) 771 r.Sym = rel 772 r.Add = int64(targ.Plt) 773 774 // jmp *addr 775 if SysArch.Family == sys.I386 { 776 Adduint8(ctxt, rel, 0xff) 777 Adduint8(ctxt, rel, 0x25) 778 Addaddr(ctxt, rel, targ) 779 Adduint8(ctxt, rel, 0x90) 780 Adduint8(ctxt, rel, 0x90) 781 } else { 782 Adduint8(ctxt, rel, 0xff) 783 Adduint8(ctxt, rel, 0x24) 784 Adduint8(ctxt, rel, 0x25) 785 addaddrplus4(ctxt, rel, targ, 0) 786 Adduint8(ctxt, rel, 0x90) 787 } 788 } else if r.Sym.Plt >= 0 { 789 r.Sym = rel 790 r.Add = int64(targ.Plt) 791 } 792 } 793 794 return 795 } 796 797 for ri := 0; ri < len(s.R); ri++ { 798 r := &s.R[ri] 799 if Buildmode == BuildmodePIE && Linkmode == LinkInternal { 800 // It's expected that some relocations will be done 801 // later by relocsym (R_TLS_LE, R_ADDROFF), so 802 // don't worry if Adddynrel returns false. 803 Thearch.Adddynrel(ctxt, s, r) 804 continue 805 } 806 if r.Sym != nil && r.Sym.Type == SDYNIMPORT || r.Type >= 256 { 807 if r.Sym != nil && !r.Sym.Attr.Reachable() { 808 Errorf(s, "dynamic relocation to unreachable symbol %s", r.Sym.Name) 809 } 810 if !Thearch.Adddynrel(ctxt, s, r) { 811 Errorf(s, "unsupported dynamic relocation for symbol %s (type=%d stype=%d)", r.Sym.Name, r.Type, r.Sym.Type) 812 } 813 } 814 } 815 } 816 817 func dynreloc(ctxt *Link, data *[SXREF][]*Symbol) { 818 // -d suppresses dynamic loader format, so we may as well not 819 // compute these sections or mark their symbols as reachable. 820 if *FlagD && Headtype != objabi.Hwindows { 821 return 822 } 823 if ctxt.Debugvlog != 0 { 824 ctxt.Logf("%5.2f reloc\n", Cputime()) 825 } 826 827 for _, s := range ctxt.Textp { 828 dynrelocsym(ctxt, s) 829 } 830 for _, syms := range data { 831 for _, sym := range syms { 832 dynrelocsym(ctxt, sym) 833 } 834 } 835 if Iself { 836 elfdynhash(ctxt) 837 } 838 } 839 840 func Codeblk(ctxt *Link, addr int64, size int64) { 841 CodeblkPad(ctxt, addr, size, zeros[:]) 842 } 843 func CodeblkPad(ctxt *Link, addr int64, size int64, pad []byte) { 844 if *flagA { 845 ctxt.Logf("codeblk [%#x,%#x) at offset %#x\n", addr, addr+size, coutbuf.Offset()) 846 } 847 848 blk(ctxt, ctxt.Textp, addr, size, pad) 849 850 /* again for printing */ 851 if !*flagA { 852 return 853 } 854 855 syms := ctxt.Textp 856 for i, sym := range syms { 857 if !sym.Attr.Reachable() { 858 continue 859 } 860 if sym.Value >= addr { 861 syms = syms[i:] 862 break 863 } 864 } 865 866 eaddr := addr + size 867 var q []byte 868 for _, sym := range syms { 869 if !sym.Attr.Reachable() { 870 continue 871 } 872 if sym.Value >= eaddr { 873 break 874 } 875 876 if addr < sym.Value { 877 ctxt.Logf("%-20s %.8x|", "_", uint64(addr)) 878 for ; addr < sym.Value; addr++ { 879 ctxt.Logf(" %.2x", 0) 880 } 881 ctxt.Logf("\n") 882 } 883 884 ctxt.Logf("%.6x\t%-20s\n", uint64(addr), sym.Name) 885 q = sym.P 886 887 for len(q) >= 16 { 888 ctxt.Logf("%.6x\t% x\n", uint64(addr), q[:16]) 889 addr += 16 890 q = q[16:] 891 } 892 893 if len(q) > 0 { 894 ctxt.Logf("%.6x\t% x\n", uint64(addr), q) 895 addr += int64(len(q)) 896 } 897 } 898 899 if addr < eaddr { 900 ctxt.Logf("%-20s %.8x|", "_", uint64(addr)) 901 for ; addr < eaddr; addr++ { 902 ctxt.Logf(" %.2x", 0) 903 } 904 } 905 } 906 907 func blk(ctxt *Link, syms []*Symbol, addr, size int64, pad []byte) { 908 for i, s := range syms { 909 if s.Type&SSUB == 0 && s.Value >= addr { 910 syms = syms[i:] 911 break 912 } 913 } 914 915 eaddr := addr + size 916 for _, s := range syms { 917 if s.Type&SSUB != 0 { 918 continue 919 } 920 if s.Value >= eaddr { 921 break 922 } 923 if s.Value < addr { 924 Errorf(s, "phase error: addr=%#x but sym=%#x type=%d", addr, s.Value, s.Type) 925 errorexit() 926 } 927 if addr < s.Value { 928 strnputPad("", int(s.Value-addr), pad) 929 addr = s.Value 930 } 931 Cwrite(s.P) 932 addr += int64(len(s.P)) 933 if addr < s.Value+s.Size { 934 strnputPad("", int(s.Value+s.Size-addr), pad) 935 addr = s.Value + s.Size 936 } 937 if addr != s.Value+s.Size { 938 Errorf(s, "phase error: addr=%#x value+size=%#x", addr, s.Value+s.Size) 939 errorexit() 940 } 941 if s.Value+s.Size >= eaddr { 942 break 943 } 944 } 945 946 if addr < eaddr { 947 strnputPad("", int(eaddr-addr), pad) 948 } 949 Cflush() 950 } 951 952 func Datblk(ctxt *Link, addr int64, size int64) { 953 if *flagA { 954 ctxt.Logf("datblk [%#x,%#x) at offset %#x\n", addr, addr+size, coutbuf.Offset()) 955 } 956 957 blk(ctxt, datap, addr, size, zeros[:]) 958 959 /* again for printing */ 960 if !*flagA { 961 return 962 } 963 964 syms := datap 965 for i, sym := range syms { 966 if sym.Value >= addr { 967 syms = syms[i:] 968 break 969 } 970 } 971 972 eaddr := addr + size 973 for _, sym := range syms { 974 if sym.Value >= eaddr { 975 break 976 } 977 if addr < sym.Value { 978 ctxt.Logf("\t%.8x| 00 ...\n", uint64(addr)) 979 addr = sym.Value 980 } 981 982 ctxt.Logf("%s\n\t%.8x|", sym.Name, uint64(addr)) 983 for i, b := range sym.P { 984 if i > 0 && i%16 == 0 { 985 ctxt.Logf("\n\t%.8x|", uint64(addr)+uint64(i)) 986 } 987 ctxt.Logf(" %.2x", b) 988 } 989 990 addr += int64(len(sym.P)) 991 for ; addr < sym.Value+sym.Size; addr++ { 992 ctxt.Logf(" %.2x", 0) 993 } 994 ctxt.Logf("\n") 995 996 if Linkmode != LinkExternal { 997 continue 998 } 999 for _, r := range sym.R { 1000 rsname := "" 1001 if r.Sym != nil { 1002 rsname = r.Sym.Name 1003 } 1004 typ := "?" 1005 switch r.Type { 1006 case objabi.R_ADDR: 1007 typ = "addr" 1008 case objabi.R_PCREL: 1009 typ = "pcrel" 1010 case objabi.R_CALL: 1011 typ = "call" 1012 } 1013 ctxt.Logf("\treloc %.8x/%d %s %s+%#x [%#x]\n", uint(sym.Value+int64(r.Off)), r.Siz, typ, rsname, r.Add, r.Sym.Value+r.Add) 1014 } 1015 } 1016 1017 if addr < eaddr { 1018 ctxt.Logf("\t%.8x| 00 ...\n", uint(addr)) 1019 } 1020 ctxt.Logf("\t%.8x|\n", uint(eaddr)) 1021 } 1022 1023 func Dwarfblk(ctxt *Link, addr int64, size int64) { 1024 if *flagA { 1025 ctxt.Logf("dwarfblk [%#x,%#x) at offset %#x\n", addr, addr+size, coutbuf.Offset()) 1026 } 1027 1028 blk(ctxt, dwarfp, addr, size, zeros[:]) 1029 } 1030 1031 var zeros [512]byte 1032 1033 // strnput writes the first n bytes of s. 1034 // If n is larger than len(s), 1035 // it is padded with NUL bytes. 1036 func strnput(s string, n int) { 1037 strnputPad(s, n, zeros[:]) 1038 } 1039 1040 // strnput writes the first n bytes of s. 1041 // If n is larger than len(s), 1042 // it is padded with the bytes in pad (repeated as needed). 1043 func strnputPad(s string, n int, pad []byte) { 1044 if len(s) >= n { 1045 Cwritestring(s[:n]) 1046 } else { 1047 Cwritestring(s) 1048 n -= len(s) 1049 for n > len(pad) { 1050 Cwrite(pad) 1051 n -= len(pad) 1052 1053 } 1054 Cwrite(pad[:n]) 1055 } 1056 } 1057 1058 var strdata []*Symbol 1059 1060 func addstrdata1(ctxt *Link, arg string) { 1061 eq := strings.Index(arg, "=") 1062 dot := strings.LastIndex(arg[:eq+1], ".") 1063 if eq < 0 || dot < 0 { 1064 Exitf("-X flag requires argument of the form importpath.name=value") 1065 } 1066 addstrdata(ctxt, objabi.PathToPrefix(arg[:dot])+arg[dot:eq], arg[eq+1:]) 1067 } 1068 1069 func addstrdata(ctxt *Link, name string, value string) { 1070 p := fmt.Sprintf("%s.str", name) 1071 sp := ctxt.Syms.Lookup(p, 0) 1072 1073 Addstring(sp, value) 1074 sp.Type = SRODATA 1075 1076 s := ctxt.Syms.Lookup(name, 0) 1077 s.Size = 0 1078 s.Attr |= AttrDuplicateOK 1079 reachable := s.Attr.Reachable() 1080 Addaddr(ctxt, s, sp) 1081 adduintxx(ctxt, s, uint64(len(value)), SysArch.PtrSize) 1082 1083 // addstring, addaddr, etc., mark the symbols as reachable. 1084 // In this case that is not necessarily true, so stick to what 1085 // we know before entering this function. 1086 s.Attr.Set(AttrReachable, reachable) 1087 1088 strdata = append(strdata, s) 1089 1090 sp.Attr.Set(AttrReachable, reachable) 1091 } 1092 1093 func (ctxt *Link) checkstrdata() { 1094 for _, s := range strdata { 1095 if s.Type == STEXT { 1096 Errorf(s, "cannot use -X with text symbol") 1097 } else if s.Gotype != nil && s.Gotype.Name != "type.string" { 1098 Errorf(s, "cannot use -X with non-string symbol") 1099 } 1100 } 1101 } 1102 1103 func Addstring(s *Symbol, str string) int64 { 1104 if s.Type == 0 { 1105 s.Type = SNOPTRDATA 1106 } 1107 s.Attr |= AttrReachable 1108 r := s.Size 1109 if s.Name == ".shstrtab" { 1110 elfsetstring(s, str, int(r)) 1111 } 1112 s.P = append(s.P, str...) 1113 s.P = append(s.P, 0) 1114 s.Size = int64(len(s.P)) 1115 return r 1116 } 1117 1118 // addgostring adds str, as a Go string value, to s. symname is the name of the 1119 // symbol used to define the string data and must be unique per linked object. 1120 func addgostring(ctxt *Link, s *Symbol, symname, str string) { 1121 sym := ctxt.Syms.Lookup(symname, 0) 1122 if sym.Type != Sxxx { 1123 Errorf(s, "duplicate symname in addgostring: %s", symname) 1124 } 1125 sym.Attr |= AttrReachable 1126 sym.Attr |= AttrLocal 1127 sym.Type = SRODATA 1128 sym.Size = int64(len(str)) 1129 sym.P = []byte(str) 1130 Addaddr(ctxt, s, sym) 1131 adduint(ctxt, s, uint64(len(str))) 1132 } 1133 1134 func addinitarrdata(ctxt *Link, s *Symbol) { 1135 p := s.Name + ".ptr" 1136 sp := ctxt.Syms.Lookup(p, 0) 1137 sp.Type = SINITARR 1138 sp.Size = 0 1139 sp.Attr |= AttrDuplicateOK 1140 Addaddr(ctxt, sp, s) 1141 } 1142 1143 func dosymtype(ctxt *Link) { 1144 for _, s := range ctxt.Syms.Allsym { 1145 if len(s.P) > 0 { 1146 if s.Type == SBSS { 1147 s.Type = SDATA 1148 } 1149 if s.Type == SNOPTRBSS { 1150 s.Type = SNOPTRDATA 1151 } 1152 } 1153 // Create a new entry in the .init_array section that points to the 1154 // library initializer function. 1155 switch Buildmode { 1156 case BuildmodeCArchive, BuildmodeCShared: 1157 if s.Name == *flagEntrySymbol { 1158 addinitarrdata(ctxt, s) 1159 } 1160 } 1161 } 1162 } 1163 1164 // symalign returns the required alignment for the given symbol s. 1165 func symalign(s *Symbol) int32 { 1166 min := int32(Thearch.Minalign) 1167 if s.Align >= min { 1168 return s.Align 1169 } else if s.Align != 0 { 1170 return min 1171 } 1172 if strings.HasPrefix(s.Name, "go.string.") || strings.HasPrefix(s.Name, "type..namedata.") { 1173 // String data is just bytes. 1174 // If we align it, we waste a lot of space to padding. 1175 return min 1176 } 1177 align := int32(Thearch.Maxalign) 1178 for int64(align) > s.Size && align > min { 1179 align >>= 1 1180 } 1181 return align 1182 } 1183 1184 func aligndatsize(datsize int64, s *Symbol) int64 { 1185 return Rnd(datsize, int64(symalign(s))) 1186 } 1187 1188 const debugGCProg = false 1189 1190 type GCProg struct { 1191 ctxt *Link 1192 sym *Symbol 1193 w gcprog.Writer 1194 } 1195 1196 func (p *GCProg) Init(ctxt *Link, name string) { 1197 p.ctxt = ctxt 1198 p.sym = ctxt.Syms.Lookup(name, 0) 1199 p.w.Init(p.writeByte(ctxt)) 1200 if debugGCProg { 1201 fmt.Fprintf(os.Stderr, "ld: start GCProg %s\n", name) 1202 p.w.Debug(os.Stderr) 1203 } 1204 } 1205 1206 func (p *GCProg) writeByte(ctxt *Link) func(x byte) { 1207 return func(x byte) { 1208 Adduint8(ctxt, p.sym, x) 1209 } 1210 } 1211 1212 func (p *GCProg) End(size int64) { 1213 p.w.ZeroUntil(size / int64(SysArch.PtrSize)) 1214 p.w.End() 1215 if debugGCProg { 1216 fmt.Fprintf(os.Stderr, "ld: end GCProg\n") 1217 } 1218 } 1219 1220 func (p *GCProg) AddSym(s *Symbol) { 1221 typ := s.Gotype 1222 // Things without pointers should be in SNOPTRDATA or SNOPTRBSS; 1223 // everything we see should have pointers and should therefore have a type. 1224 if typ == nil { 1225 switch s.Name { 1226 case "runtime.data", "runtime.edata", "runtime.bss", "runtime.ebss": 1227 // Ignore special symbols that are sometimes laid out 1228 // as real symbols. See comment about dyld on darwin in 1229 // the address function. 1230 return 1231 } 1232 Errorf(s, "missing Go type information for global symbol: size %d", s.Size) 1233 return 1234 } 1235 1236 ptrsize := int64(SysArch.PtrSize) 1237 nptr := decodetypePtrdata(p.ctxt.Arch, typ) / ptrsize 1238 1239 if debugGCProg { 1240 fmt.Fprintf(os.Stderr, "gcprog sym: %s at %d (ptr=%d+%d)\n", s.Name, s.Value, s.Value/ptrsize, nptr) 1241 } 1242 1243 if decodetypeUsegcprog(typ) == 0 { 1244 // Copy pointers from mask into program. 1245 mask := decodetypeGcmask(p.ctxt, typ) 1246 for i := int64(0); i < nptr; i++ { 1247 if (mask[i/8]>>uint(i%8))&1 != 0 { 1248 p.w.Ptr(s.Value/ptrsize + i) 1249 } 1250 } 1251 return 1252 } 1253 1254 // Copy program. 1255 prog := decodetypeGcprog(p.ctxt, typ) 1256 p.w.ZeroUntil(s.Value / ptrsize) 1257 p.w.Append(prog[4:], nptr) 1258 } 1259 1260 // dataSortKey is used to sort a slice of data symbol *Symbol pointers. 1261 // The sort keys are kept inline to improve cache behavior while sorting. 1262 type dataSortKey struct { 1263 size int64 1264 name string 1265 sym *Symbol 1266 } 1267 1268 type bySizeAndName []dataSortKey 1269 1270 func (d bySizeAndName) Len() int { return len(d) } 1271 func (d bySizeAndName) Swap(i, j int) { d[i], d[j] = d[j], d[i] } 1272 func (d bySizeAndName) Less(i, j int) bool { 1273 s1, s2 := d[i], d[j] 1274 if s1.size != s2.size { 1275 return s1.size < s2.size 1276 } 1277 return s1.name < s2.name 1278 } 1279 1280 const cutoff int64 = 2e9 // 2 GB (or so; looks better in errors than 2^31) 1281 1282 func checkdatsize(ctxt *Link, datsize int64, symn SymKind) { 1283 if datsize > cutoff { 1284 Errorf(nil, "too much data in section %v (over %d bytes)", symn, cutoff) 1285 } 1286 } 1287 1288 // datap is a collection of reachable data symbols in address order. 1289 // Generated by dodata. 1290 var datap []*Symbol 1291 1292 func (ctxt *Link) dodata() { 1293 if ctxt.Debugvlog != 0 { 1294 ctxt.Logf("%5.2f dodata\n", Cputime()) 1295 } 1296 1297 if ctxt.DynlinkingGo() && Headtype == objabi.Hdarwin { 1298 // The values in moduledata are filled out by relocations 1299 // pointing to the addresses of these special symbols. 1300 // Typically these symbols have no size and are not laid 1301 // out with their matching section. 1302 // 1303 // However on darwin, dyld will find the special symbol 1304 // in the first loaded module, even though it is local. 1305 // 1306 // (An hypothesis, formed without looking in the dyld sources: 1307 // these special symbols have no size, so their address 1308 // matches a real symbol. The dynamic linker assumes we 1309 // want the normal symbol with the same address and finds 1310 // it in the other module.) 1311 // 1312 // To work around this we lay out the symbls whose 1313 // addresses are vital for multi-module programs to work 1314 // as normal symbols, and give them a little size. 1315 bss := ctxt.Syms.Lookup("runtime.bss", 0) 1316 bss.Size = 8 1317 bss.Attr.Set(AttrSpecial, false) 1318 1319 ctxt.Syms.Lookup("runtime.ebss", 0).Attr.Set(AttrSpecial, false) 1320 1321 data := ctxt.Syms.Lookup("runtime.data", 0) 1322 data.Size = 8 1323 data.Attr.Set(AttrSpecial, false) 1324 1325 ctxt.Syms.Lookup("runtime.edata", 0).Attr.Set(AttrSpecial, false) 1326 1327 types := ctxt.Syms.Lookup("runtime.types", 0) 1328 types.Type = STYPE 1329 types.Size = 8 1330 types.Attr.Set(AttrSpecial, false) 1331 1332 etypes := ctxt.Syms.Lookup("runtime.etypes", 0) 1333 etypes.Type = SFUNCTAB 1334 etypes.Attr.Set(AttrSpecial, false) 1335 } 1336 1337 // Collect data symbols by type into data. 1338 var data [SXREF][]*Symbol 1339 for _, s := range ctxt.Syms.Allsym { 1340 if !s.Attr.Reachable() || s.Attr.Special() { 1341 continue 1342 } 1343 if s.Type <= STEXT || s.Type >= SXREF { 1344 continue 1345 } 1346 data[s.Type] = append(data[s.Type], s) 1347 } 1348 1349 // Now that we have the data symbols, but before we start 1350 // to assign addresses, record all the necessary 1351 // dynamic relocations. These will grow the relocation 1352 // symbol, which is itself data. 1353 // 1354 // On darwin, we need the symbol table numbers for dynreloc. 1355 if Headtype == objabi.Hdarwin { 1356 machosymorder(ctxt) 1357 } 1358 dynreloc(ctxt, &data) 1359 1360 if UseRelro() { 1361 // "read only" data with relocations needs to go in its own section 1362 // when building a shared library. We do this by boosting objects of 1363 // type SXXX with relocations to type SXXXRELRO. 1364 for _, symnro := range ReadOnly { 1365 symnrelro := RelROMap[symnro] 1366 1367 ro := []*Symbol{} 1368 relro := data[symnrelro] 1369 1370 for _, s := range data[symnro] { 1371 isRelro := len(s.R) > 0 1372 switch s.Type { 1373 case STYPE, STYPERELRO, SGOFUNCRELRO: 1374 // Symbols are not sorted yet, so it is possible 1375 // that an Outer symbol has been changed to a 1376 // relro Type before it reaches here. 1377 isRelro = true 1378 } 1379 if isRelro { 1380 s.Type = symnrelro 1381 if s.Outer != nil { 1382 s.Outer.Type = s.Type 1383 } 1384 relro = append(relro, s) 1385 } else { 1386 ro = append(ro, s) 1387 } 1388 } 1389 1390 // Check that we haven't made two symbols with the same .Outer into 1391 // different types (because references two symbols with non-nil Outer 1392 // become references to the outer symbol + offset it's vital that the 1393 // symbol and the outer end up in the same section). 1394 for _, s := range relro { 1395 if s.Outer != nil && s.Outer.Type != s.Type { 1396 Errorf(s, "inconsistent types for symbol and its Outer %s (%v != %v)", 1397 s.Outer.Name, s.Type, s.Outer.Type) 1398 } 1399 } 1400 1401 data[symnro] = ro 1402 data[symnrelro] = relro 1403 } 1404 } 1405 1406 // Sort symbols. 1407 var dataMaxAlign [SXREF]int32 1408 var wg sync.WaitGroup 1409 for symn := range data { 1410 symn := SymKind(symn) 1411 wg.Add(1) 1412 go func() { 1413 data[symn], dataMaxAlign[symn] = dodataSect(ctxt, symn, data[symn]) 1414 wg.Done() 1415 }() 1416 } 1417 wg.Wait() 1418 1419 // Allocate sections. 1420 // Data is processed before segtext, because we need 1421 // to see all symbols in the .data and .bss sections in order 1422 // to generate garbage collection information. 1423 datsize := int64(0) 1424 1425 // Writable data sections that do not need any specialized handling. 1426 writable := []SymKind{ 1427 SELFSECT, 1428 SMACHO, 1429 SMACHOGOT, 1430 SWINDOWS, 1431 } 1432 for _, symn := range writable { 1433 for _, s := range data[symn] { 1434 sect := addsection(&Segdata, s.Name, 06) 1435 sect.Align = symalign(s) 1436 datsize = Rnd(datsize, int64(sect.Align)) 1437 sect.Vaddr = uint64(datsize) 1438 s.Sect = sect 1439 s.Type = SDATA 1440 s.Value = int64(uint64(datsize) - sect.Vaddr) 1441 datsize += s.Size 1442 sect.Length = uint64(datsize) - sect.Vaddr 1443 } 1444 checkdatsize(ctxt, datsize, symn) 1445 } 1446 1447 // .got (and .toc on ppc64) 1448 if len(data[SELFGOT]) > 0 { 1449 sect := addsection(&Segdata, ".got", 06) 1450 sect.Align = dataMaxAlign[SELFGOT] 1451 datsize = Rnd(datsize, int64(sect.Align)) 1452 sect.Vaddr = uint64(datsize) 1453 var toc *Symbol 1454 for _, s := range data[SELFGOT] { 1455 datsize = aligndatsize(datsize, s) 1456 s.Sect = sect 1457 s.Type = SDATA 1458 s.Value = int64(uint64(datsize) - sect.Vaddr) 1459 1460 // Resolve .TOC. symbol for this object file (ppc64) 1461 toc = ctxt.Syms.ROLookup(".TOC.", int(s.Version)) 1462 if toc != nil { 1463 toc.Sect = sect 1464 toc.Outer = s 1465 toc.Sub = s.Sub 1466 s.Sub = toc 1467 1468 toc.Value = 0x8000 1469 } 1470 1471 datsize += s.Size 1472 } 1473 checkdatsize(ctxt, datsize, SELFGOT) 1474 sect.Length = uint64(datsize) - sect.Vaddr 1475 } 1476 1477 /* pointer-free data */ 1478 sect := addsection(&Segdata, ".noptrdata", 06) 1479 sect.Align = dataMaxAlign[SNOPTRDATA] 1480 datsize = Rnd(datsize, int64(sect.Align)) 1481 sect.Vaddr = uint64(datsize) 1482 ctxt.Syms.Lookup("runtime.noptrdata", 0).Sect = sect 1483 ctxt.Syms.Lookup("runtime.enoptrdata", 0).Sect = sect 1484 for _, s := range data[SNOPTRDATA] { 1485 datsize = aligndatsize(datsize, s) 1486 s.Sect = sect 1487 s.Type = SDATA 1488 s.Value = int64(uint64(datsize) - sect.Vaddr) 1489 datsize += s.Size 1490 } 1491 checkdatsize(ctxt, datsize, SNOPTRDATA) 1492 sect.Length = uint64(datsize) - sect.Vaddr 1493 1494 hasinitarr := *FlagLinkshared 1495 1496 /* shared library initializer */ 1497 switch Buildmode { 1498 case BuildmodeCArchive, BuildmodeCShared, BuildmodeShared, BuildmodePlugin: 1499 hasinitarr = true 1500 } 1501 if hasinitarr { 1502 sect := addsection(&Segdata, ".init_array", 06) 1503 sect.Align = dataMaxAlign[SINITARR] 1504 datsize = Rnd(datsize, int64(sect.Align)) 1505 sect.Vaddr = uint64(datsize) 1506 for _, s := range data[SINITARR] { 1507 datsize = aligndatsize(datsize, s) 1508 s.Sect = sect 1509 s.Value = int64(uint64(datsize) - sect.Vaddr) 1510 datsize += s.Size 1511 } 1512 sect.Length = uint64(datsize) - sect.Vaddr 1513 checkdatsize(ctxt, datsize, SINITARR) 1514 } 1515 1516 /* data */ 1517 sect = addsection(&Segdata, ".data", 06) 1518 sect.Align = dataMaxAlign[SDATA] 1519 datsize = Rnd(datsize, int64(sect.Align)) 1520 sect.Vaddr = uint64(datsize) 1521 ctxt.Syms.Lookup("runtime.data", 0).Sect = sect 1522 ctxt.Syms.Lookup("runtime.edata", 0).Sect = sect 1523 var gc GCProg 1524 gc.Init(ctxt, "runtime.gcdata") 1525 for _, s := range data[SDATA] { 1526 s.Sect = sect 1527 s.Type = SDATA 1528 datsize = aligndatsize(datsize, s) 1529 s.Value = int64(uint64(datsize) - sect.Vaddr) 1530 gc.AddSym(s) 1531 datsize += s.Size 1532 } 1533 checkdatsize(ctxt, datsize, SDATA) 1534 sect.Length = uint64(datsize) - sect.Vaddr 1535 gc.End(int64(sect.Length)) 1536 1537 /* bss */ 1538 sect = addsection(&Segdata, ".bss", 06) 1539 sect.Align = dataMaxAlign[SBSS] 1540 datsize = Rnd(datsize, int64(sect.Align)) 1541 sect.Vaddr = uint64(datsize) 1542 ctxt.Syms.Lookup("runtime.bss", 0).Sect = sect 1543 ctxt.Syms.Lookup("runtime.ebss", 0).Sect = sect 1544 gc = GCProg{} 1545 gc.Init(ctxt, "runtime.gcbss") 1546 for _, s := range data[SBSS] { 1547 s.Sect = sect 1548 datsize = aligndatsize(datsize, s) 1549 s.Value = int64(uint64(datsize) - sect.Vaddr) 1550 gc.AddSym(s) 1551 datsize += s.Size 1552 } 1553 checkdatsize(ctxt, datsize, SBSS) 1554 sect.Length = uint64(datsize) - sect.Vaddr 1555 gc.End(int64(sect.Length)) 1556 1557 /* pointer-free bss */ 1558 sect = addsection(&Segdata, ".noptrbss", 06) 1559 sect.Align = dataMaxAlign[SNOPTRBSS] 1560 datsize = Rnd(datsize, int64(sect.Align)) 1561 sect.Vaddr = uint64(datsize) 1562 ctxt.Syms.Lookup("runtime.noptrbss", 0).Sect = sect 1563 ctxt.Syms.Lookup("runtime.enoptrbss", 0).Sect = sect 1564 for _, s := range data[SNOPTRBSS] { 1565 datsize = aligndatsize(datsize, s) 1566 s.Sect = sect 1567 s.Value = int64(uint64(datsize) - sect.Vaddr) 1568 datsize += s.Size 1569 } 1570 1571 sect.Length = uint64(datsize) - sect.Vaddr 1572 ctxt.Syms.Lookup("runtime.end", 0).Sect = sect 1573 checkdatsize(ctxt, datsize, SNOPTRBSS) 1574 1575 if len(data[STLSBSS]) > 0 { 1576 var sect *Section 1577 if Iself && (Linkmode == LinkExternal || !*FlagD) { 1578 sect = addsection(&Segdata, ".tbss", 06) 1579 sect.Align = int32(SysArch.PtrSize) 1580 sect.Vaddr = 0 1581 } 1582 datsize = 0 1583 1584 for _, s := range data[STLSBSS] { 1585 datsize = aligndatsize(datsize, s) 1586 s.Sect = sect 1587 s.Value = datsize 1588 datsize += s.Size 1589 } 1590 checkdatsize(ctxt, datsize, STLSBSS) 1591 1592 if sect != nil { 1593 sect.Length = uint64(datsize) 1594 } 1595 } 1596 1597 /* 1598 * We finished data, begin read-only data. 1599 * Not all systems support a separate read-only non-executable data section. 1600 * ELF systems do. 1601 * OS X and Plan 9 do not. 1602 * Windows PE may, but if so we have not implemented it. 1603 * And if we're using external linking mode, the point is moot, 1604 * since it's not our decision; that code expects the sections in 1605 * segtext. 1606 */ 1607 var segro *Segment 1608 if Iself && Linkmode == LinkInternal { 1609 segro = &Segrodata 1610 } else { 1611 segro = &Segtext 1612 } 1613 1614 datsize = 0 1615 1616 /* read-only executable ELF, Mach-O sections */ 1617 if len(data[STEXT]) != 0 { 1618 Errorf(nil, "dodata found an STEXT symbol: %s", data[STEXT][0].Name) 1619 } 1620 for _, s := range data[SELFRXSECT] { 1621 sect := addsection(&Segtext, s.Name, 04) 1622 sect.Align = symalign(s) 1623 datsize = Rnd(datsize, int64(sect.Align)) 1624 sect.Vaddr = uint64(datsize) 1625 s.Sect = sect 1626 s.Type = SRODATA 1627 s.Value = int64(uint64(datsize) - sect.Vaddr) 1628 datsize += s.Size 1629 sect.Length = uint64(datsize) - sect.Vaddr 1630 checkdatsize(ctxt, datsize, SELFRXSECT) 1631 } 1632 1633 /* read-only data */ 1634 sect = addsection(segro, ".rodata", 04) 1635 1636 sect.Vaddr = 0 1637 ctxt.Syms.Lookup("runtime.rodata", 0).Sect = sect 1638 ctxt.Syms.Lookup("runtime.erodata", 0).Sect = sect 1639 if !UseRelro() { 1640 ctxt.Syms.Lookup("runtime.types", 0).Sect = sect 1641 ctxt.Syms.Lookup("runtime.etypes", 0).Sect = sect 1642 } 1643 for _, symn := range ReadOnly { 1644 align := dataMaxAlign[symn] 1645 if sect.Align < align { 1646 sect.Align = align 1647 } 1648 } 1649 datsize = Rnd(datsize, int64(sect.Align)) 1650 for _, symn := range ReadOnly { 1651 for _, s := range data[symn] { 1652 datsize = aligndatsize(datsize, s) 1653 s.Sect = sect 1654 s.Type = SRODATA 1655 s.Value = int64(uint64(datsize) - sect.Vaddr) 1656 datsize += s.Size 1657 } 1658 checkdatsize(ctxt, datsize, symn) 1659 } 1660 sect.Length = uint64(datsize) - sect.Vaddr 1661 1662 /* read-only ELF, Mach-O sections */ 1663 for _, s := range data[SELFROSECT] { 1664 sect = addsection(segro, s.Name, 04) 1665 sect.Align = symalign(s) 1666 datsize = Rnd(datsize, int64(sect.Align)) 1667 sect.Vaddr = uint64(datsize) 1668 s.Sect = sect 1669 s.Type = SRODATA 1670 s.Value = int64(uint64(datsize) - sect.Vaddr) 1671 datsize += s.Size 1672 sect.Length = uint64(datsize) - sect.Vaddr 1673 } 1674 checkdatsize(ctxt, datsize, SELFROSECT) 1675 1676 for _, s := range data[SMACHOPLT] { 1677 sect = addsection(segro, s.Name, 04) 1678 sect.Align = symalign(s) 1679 datsize = Rnd(datsize, int64(sect.Align)) 1680 sect.Vaddr = uint64(datsize) 1681 s.Sect = sect 1682 s.Type = SRODATA 1683 s.Value = int64(uint64(datsize) - sect.Vaddr) 1684 datsize += s.Size 1685 sect.Length = uint64(datsize) - sect.Vaddr 1686 } 1687 checkdatsize(ctxt, datsize, SMACHOPLT) 1688 1689 // There is some data that are conceptually read-only but are written to by 1690 // relocations. On GNU systems, we can arrange for the dynamic linker to 1691 // mprotect sections after relocations are applied by giving them write 1692 // permissions in the object file and calling them ".data.rel.ro.FOO". We 1693 // divide the .rodata section between actual .rodata and .data.rel.ro.rodata, 1694 // but for the other sections that this applies to, we just write a read-only 1695 // .FOO section or a read-write .data.rel.ro.FOO section depending on the 1696 // situation. 1697 // TODO(mwhudson): It would make sense to do this more widely, but it makes 1698 // the system linker segfault on darwin. 1699 addrelrosection := func(suffix string) *Section { 1700 return addsection(segro, suffix, 04) 1701 } 1702 1703 if UseRelro() { 1704 addrelrosection = func(suffix string) *Section { 1705 seg := &Segrelrodata 1706 if Linkmode == LinkExternal { 1707 // Using a separate segment with an external 1708 // linker results in some programs moving 1709 // their data sections unexpectedly, which 1710 // corrupts the moduledata. So we use the 1711 // rodata segment and let the external linker 1712 // sort out a rel.ro segment. 1713 seg = &Segrodata 1714 } 1715 return addsection(seg, ".data.rel.ro"+suffix, 06) 1716 } 1717 /* data only written by relocations */ 1718 sect = addrelrosection("") 1719 1720 sect.Vaddr = 0 1721 ctxt.Syms.Lookup("runtime.types", 0).Sect = sect 1722 ctxt.Syms.Lookup("runtime.etypes", 0).Sect = sect 1723 for _, symnro := range ReadOnly { 1724 symn := RelROMap[symnro] 1725 align := dataMaxAlign[symn] 1726 if sect.Align < align { 1727 sect.Align = align 1728 } 1729 } 1730 datsize = Rnd(datsize, int64(sect.Align)) 1731 for _, symnro := range ReadOnly { 1732 symn := RelROMap[symnro] 1733 for _, s := range data[symn] { 1734 datsize = aligndatsize(datsize, s) 1735 if s.Outer != nil && s.Outer.Sect != nil && s.Outer.Sect != sect { 1736 Errorf(s, "s.Outer (%s) in different section from s, %s != %s", s.Outer.Name, s.Outer.Sect.Name, sect.Name) 1737 } 1738 s.Sect = sect 1739 s.Type = SRODATA 1740 s.Value = int64(uint64(datsize) - sect.Vaddr) 1741 datsize += s.Size 1742 } 1743 checkdatsize(ctxt, datsize, symn) 1744 } 1745 1746 sect.Length = uint64(datsize) - sect.Vaddr 1747 } 1748 1749 /* typelink */ 1750 sect = addrelrosection(".typelink") 1751 sect.Align = dataMaxAlign[STYPELINK] 1752 datsize = Rnd(datsize, int64(sect.Align)) 1753 sect.Vaddr = uint64(datsize) 1754 typelink := ctxt.Syms.Lookup("runtime.typelink", 0) 1755 typelink.Sect = sect 1756 typelink.Type = SRODATA 1757 datsize += typelink.Size 1758 checkdatsize(ctxt, datsize, STYPELINK) 1759 sect.Length = uint64(datsize) - sect.Vaddr 1760 1761 /* itablink */ 1762 sect = addrelrosection(".itablink") 1763 sect.Align = dataMaxAlign[SITABLINK] 1764 datsize = Rnd(datsize, int64(sect.Align)) 1765 sect.Vaddr = uint64(datsize) 1766 ctxt.Syms.Lookup("runtime.itablink", 0).Sect = sect 1767 ctxt.Syms.Lookup("runtime.eitablink", 0).Sect = sect 1768 for _, s := range data[SITABLINK] { 1769 datsize = aligndatsize(datsize, s) 1770 s.Sect = sect 1771 s.Type = SRODATA 1772 s.Value = int64(uint64(datsize) - sect.Vaddr) 1773 datsize += s.Size 1774 } 1775 checkdatsize(ctxt, datsize, SITABLINK) 1776 sect.Length = uint64(datsize) - sect.Vaddr 1777 1778 /* gosymtab */ 1779 sect = addrelrosection(".gosymtab") 1780 sect.Align = dataMaxAlign[SSYMTAB] 1781 datsize = Rnd(datsize, int64(sect.Align)) 1782 sect.Vaddr = uint64(datsize) 1783 ctxt.Syms.Lookup("runtime.symtab", 0).Sect = sect 1784 ctxt.Syms.Lookup("runtime.esymtab", 0).Sect = sect 1785 for _, s := range data[SSYMTAB] { 1786 datsize = aligndatsize(datsize, s) 1787 s.Sect = sect 1788 s.Type = SRODATA 1789 s.Value = int64(uint64(datsize) - sect.Vaddr) 1790 datsize += s.Size 1791 } 1792 checkdatsize(ctxt, datsize, SSYMTAB) 1793 sect.Length = uint64(datsize) - sect.Vaddr 1794 1795 /* gopclntab */ 1796 sect = addrelrosection(".gopclntab") 1797 sect.Align = dataMaxAlign[SPCLNTAB] 1798 datsize = Rnd(datsize, int64(sect.Align)) 1799 sect.Vaddr = uint64(datsize) 1800 ctxt.Syms.Lookup("runtime.pclntab", 0).Sect = sect 1801 ctxt.Syms.Lookup("runtime.epclntab", 0).Sect = sect 1802 for _, s := range data[SPCLNTAB] { 1803 datsize = aligndatsize(datsize, s) 1804 s.Sect = sect 1805 s.Type = SRODATA 1806 s.Value = int64(uint64(datsize) - sect.Vaddr) 1807 datsize += s.Size 1808 } 1809 checkdatsize(ctxt, datsize, SRODATA) 1810 sect.Length = uint64(datsize) - sect.Vaddr 1811 1812 // 6g uses 4-byte relocation offsets, so the entire segment must fit in 32 bits. 1813 if datsize != int64(uint32(datsize)) { 1814 Errorf(nil, "read-only data segment too large: %d", datsize) 1815 } 1816 1817 for symn := SELFRXSECT; symn < SXREF; symn++ { 1818 datap = append(datap, data[symn]...) 1819 } 1820 1821 dwarfgeneratedebugsyms(ctxt) 1822 1823 var s *Symbol 1824 var i int 1825 for i, s = range dwarfp { 1826 if s.Type != SDWARFSECT { 1827 break 1828 } 1829 sect = addsection(&Segdwarf, s.Name, 04) 1830 sect.Align = 1 1831 datsize = Rnd(datsize, int64(sect.Align)) 1832 sect.Vaddr = uint64(datsize) 1833 s.Sect = sect 1834 s.Type = SRODATA 1835 s.Value = int64(uint64(datsize) - sect.Vaddr) 1836 datsize += s.Size 1837 sect.Length = uint64(datsize) - sect.Vaddr 1838 } 1839 checkdatsize(ctxt, datsize, SDWARFSECT) 1840 1841 if i < len(dwarfp) { 1842 sect = addsection(&Segdwarf, ".debug_info", 04) 1843 sect.Align = 1 1844 datsize = Rnd(datsize, int64(sect.Align)) 1845 sect.Vaddr = uint64(datsize) 1846 for _, s := range dwarfp[i:] { 1847 if s.Type != SDWARFINFO { 1848 break 1849 } 1850 s.Sect = sect 1851 s.Type = SRODATA 1852 s.Value = int64(uint64(datsize) - sect.Vaddr) 1853 s.Attr |= AttrLocal 1854 datsize += s.Size 1855 } 1856 sect.Length = uint64(datsize) - sect.Vaddr 1857 checkdatsize(ctxt, datsize, SDWARFINFO) 1858 } 1859 1860 /* number the sections */ 1861 n := int32(1) 1862 1863 for _, sect := range Segtext.Sections { 1864 sect.Extnum = int16(n) 1865 n++ 1866 } 1867 for _, sect := range Segrodata.Sections { 1868 sect.Extnum = int16(n) 1869 n++ 1870 } 1871 for _, sect := range Segrelrodata.Sections { 1872 sect.Extnum = int16(n) 1873 n++ 1874 } 1875 for _, sect := range Segdata.Sections { 1876 sect.Extnum = int16(n) 1877 n++ 1878 } 1879 for _, sect := range Segdwarf.Sections { 1880 sect.Extnum = int16(n) 1881 n++ 1882 } 1883 } 1884 1885 func dodataSect(ctxt *Link, symn SymKind, syms []*Symbol) (result []*Symbol, maxAlign int32) { 1886 if Headtype == objabi.Hdarwin { 1887 // Some symbols may no longer belong in syms 1888 // due to movement in machosymorder. 1889 newSyms := make([]*Symbol, 0, len(syms)) 1890 for _, s := range syms { 1891 if s.Type == symn { 1892 newSyms = append(newSyms, s) 1893 } 1894 } 1895 syms = newSyms 1896 } 1897 1898 var head, tail *Symbol 1899 symsSort := make([]dataSortKey, 0, len(syms)) 1900 for _, s := range syms { 1901 if s.Attr.OnList() { 1902 log.Fatalf("symbol %s listed multiple times", s.Name) 1903 } 1904 s.Attr |= AttrOnList 1905 switch { 1906 case s.Size < int64(len(s.P)): 1907 Errorf(s, "initialize bounds (%d < %d)", s.Size, len(s.P)) 1908 case s.Size < 0: 1909 Errorf(s, "negative size (%d bytes)", s.Size) 1910 case s.Size > cutoff: 1911 Errorf(s, "symbol too large (%d bytes)", s.Size) 1912 } 1913 1914 // If the usually-special section-marker symbols are being laid 1915 // out as regular symbols, put them either at the beginning or 1916 // end of their section. 1917 if ctxt.DynlinkingGo() && Headtype == objabi.Hdarwin { 1918 switch s.Name { 1919 case "runtime.text", "runtime.bss", "runtime.data", "runtime.types": 1920 head = s 1921 continue 1922 case "runtime.etext", "runtime.ebss", "runtime.edata", "runtime.etypes": 1923 tail = s 1924 continue 1925 } 1926 } 1927 1928 key := dataSortKey{ 1929 size: s.Size, 1930 name: s.Name, 1931 sym: s, 1932 } 1933 1934 switch s.Type { 1935 case SELFGOT: 1936 // For ppc64, we want to interleave the .got and .toc sections 1937 // from input files. Both are type SELFGOT, so in that case 1938 // we skip size comparison and fall through to the name 1939 // comparison (conveniently, .got sorts before .toc). 1940 key.size = 0 1941 } 1942 1943 symsSort = append(symsSort, key) 1944 } 1945 1946 sort.Sort(bySizeAndName(symsSort)) 1947 1948 off := 0 1949 if head != nil { 1950 syms[0] = head 1951 off++ 1952 } 1953 for i, symSort := range symsSort { 1954 syms[i+off] = symSort.sym 1955 align := symalign(symSort.sym) 1956 if maxAlign < align { 1957 maxAlign = align 1958 } 1959 } 1960 if tail != nil { 1961 syms[len(syms)-1] = tail 1962 } 1963 1964 if Iself && symn == SELFROSECT { 1965 // Make .rela and .rela.plt contiguous, the ELF ABI requires this 1966 // and Solaris actually cares. 1967 reli, plti := -1, -1 1968 for i, s := range syms { 1969 switch s.Name { 1970 case ".rel.plt", ".rela.plt": 1971 plti = i 1972 case ".rel", ".rela": 1973 reli = i 1974 } 1975 } 1976 if reli >= 0 && plti >= 0 && plti != reli+1 { 1977 var first, second int 1978 if plti > reli { 1979 first, second = reli, plti 1980 } else { 1981 first, second = plti, reli 1982 } 1983 rel, plt := syms[reli], syms[plti] 1984 copy(syms[first+2:], syms[first+1:second]) 1985 syms[first+0] = rel 1986 syms[first+1] = plt 1987 1988 // Make sure alignment doesn't introduce a gap. 1989 // Setting the alignment explicitly prevents 1990 // symalign from basing it on the size and 1991 // getting it wrong. 1992 rel.Align = int32(SysArch.RegSize) 1993 plt.Align = int32(SysArch.RegSize) 1994 } 1995 } 1996 1997 return syms, maxAlign 1998 } 1999 2000 // Add buildid to beginning of text segment, on non-ELF systems. 2001 // Non-ELF binary formats are not always flexible enough to 2002 // give us a place to put the Go build ID. On those systems, we put it 2003 // at the very beginning of the text segment. 2004 // This ``header'' is read by cmd/go. 2005 func (ctxt *Link) textbuildid() { 2006 if Iself || Buildmode == BuildmodePlugin || *flagBuildid == "" { 2007 return 2008 } 2009 2010 sym := ctxt.Syms.Lookup("go.buildid", 0) 2011 sym.Attr |= AttrReachable 2012 // The \xff is invalid UTF-8, meant to make it less likely 2013 // to find one of these accidentally. 2014 data := "\xff Go build ID: " + strconv.Quote(*flagBuildid) + "\n \xff" 2015 sym.Type = STEXT 2016 sym.P = []byte(data) 2017 sym.Size = int64(len(sym.P)) 2018 2019 ctxt.Textp = append(ctxt.Textp, nil) 2020 copy(ctxt.Textp[1:], ctxt.Textp) 2021 ctxt.Textp[0] = sym 2022 } 2023 2024 // assign addresses to text 2025 func (ctxt *Link) textaddress() { 2026 addsection(&Segtext, ".text", 05) 2027 2028 // Assign PCs in text segment. 2029 // Could parallelize, by assigning to text 2030 // and then letting threads copy down, but probably not worth it. 2031 sect := Segtext.Sections[0] 2032 2033 sect.Align = int32(Funcalign) 2034 2035 text := ctxt.Syms.Lookup("runtime.text", 0) 2036 text.Sect = sect 2037 2038 if ctxt.DynlinkingGo() && Headtype == objabi.Hdarwin { 2039 etext := ctxt.Syms.Lookup("runtime.etext", 0) 2040 etext.Sect = sect 2041 2042 ctxt.Textp = append(ctxt.Textp, etext, nil) 2043 copy(ctxt.Textp[1:], ctxt.Textp) 2044 ctxt.Textp[0] = text 2045 } 2046 2047 va := uint64(*FlagTextAddr) 2048 n := 1 2049 sect.Vaddr = va 2050 ntramps := 0 2051 for _, sym := range ctxt.Textp { 2052 sect, n, va = assignAddress(ctxt, sect, n, sym, va) 2053 2054 trampoline(ctxt, sym) // resolve jumps, may add trampolines if jump too far 2055 2056 // lay down trampolines after each function 2057 for ; ntramps < len(ctxt.tramps); ntramps++ { 2058 tramp := ctxt.tramps[ntramps] 2059 sect, n, va = assignAddress(ctxt, sect, n, tramp, va) 2060 } 2061 } 2062 2063 sect.Length = va - sect.Vaddr 2064 ctxt.Syms.Lookup("runtime.etext", 0).Sect = sect 2065 2066 // merge tramps into Textp, keeping Textp in address order 2067 if ntramps != 0 { 2068 newtextp := make([]*Symbol, 0, len(ctxt.Textp)+ntramps) 2069 i := 0 2070 for _, sym := range ctxt.Textp { 2071 for ; i < ntramps && ctxt.tramps[i].Value < sym.Value; i++ { 2072 newtextp = append(newtextp, ctxt.tramps[i]) 2073 } 2074 newtextp = append(newtextp, sym) 2075 } 2076 newtextp = append(newtextp, ctxt.tramps[i:ntramps]...) 2077 2078 ctxt.Textp = newtextp 2079 } 2080 } 2081 2082 // assigns address for a text symbol, returns (possibly new) section, its number, and the address 2083 // Note: once we have trampoline insertion support for external linking, this function 2084 // will not need to create new text sections, and so no need to return sect and n. 2085 func assignAddress(ctxt *Link, sect *Section, n int, sym *Symbol, va uint64) (*Section, int, uint64) { 2086 sym.Sect = sect 2087 if sym.Type&SSUB != 0 { 2088 return sect, n, va 2089 } 2090 if sym.Align != 0 { 2091 va = uint64(Rnd(int64(va), int64(sym.Align))) 2092 } else { 2093 va = uint64(Rnd(int64(va), int64(Funcalign))) 2094 } 2095 sym.Value = 0 2096 for sub := sym; sub != nil; sub = sub.Sub { 2097 sub.Value += int64(va) 2098 } 2099 2100 funcsize := uint64(MINFUNC) // spacing required for findfunctab 2101 if sym.Size > MINFUNC { 2102 funcsize = uint64(sym.Size) 2103 } 2104 2105 // On ppc64x a text section should not be larger than 2^26 bytes due to the size of 2106 // call target offset field in the bl instruction. Splitting into smaller text 2107 // sections smaller than this limit allows the GNU linker to modify the long calls 2108 // appropriately. The limit allows for the space needed for tables inserted by the linker. 2109 2110 // If this function doesn't fit in the current text section, then create a new one. 2111 2112 // Only break at outermost syms. 2113 2114 if SysArch.InFamily(sys.PPC64) && sym.Outer == nil && Iself && Linkmode == LinkExternal && va-sect.Vaddr+funcsize > 0x1c00000 { 2115 2116 // Set the length for the previous text section 2117 sect.Length = va - sect.Vaddr 2118 2119 // Create new section, set the starting Vaddr 2120 sect = addsection(&Segtext, ".text", 05) 2121 sect.Vaddr = va 2122 sym.Sect = sect 2123 2124 // Create a symbol for the start of the secondary text sections 2125 ctxt.Syms.Lookup(fmt.Sprintf("runtime.text.%d", n), 0).Sect = sect 2126 n++ 2127 } 2128 va += funcsize 2129 2130 return sect, n, va 2131 } 2132 2133 // assign addresses 2134 func (ctxt *Link) address() { 2135 va := uint64(*FlagTextAddr) 2136 Segtext.Rwx = 05 2137 Segtext.Vaddr = va 2138 Segtext.Fileoff = uint64(HEADR) 2139 for _, s := range Segtext.Sections { 2140 va = uint64(Rnd(int64(va), int64(s.Align))) 2141 s.Vaddr = va 2142 va += s.Length 2143 } 2144 2145 Segtext.Length = va - uint64(*FlagTextAddr) 2146 Segtext.Filelen = Segtext.Length 2147 if Headtype == objabi.Hnacl { 2148 va += 32 // room for the "halt sled" 2149 } 2150 2151 if len(Segrodata.Sections) > 0 { 2152 // align to page boundary so as not to mix 2153 // rodata and executable text. 2154 // 2155 // Note: gold or GNU ld will reduce the size of the executable 2156 // file by arranging for the relro segment to end at a page 2157 // boundary, and overlap the end of the text segment with the 2158 // start of the relro segment in the file. The PT_LOAD segments 2159 // will be such that the last page of the text segment will be 2160 // mapped twice, once r-x and once starting out rw- and, after 2161 // relocation processing, changed to r--. 2162 // 2163 // Ideally the last page of the text segment would not be 2164 // writable even for this short period. 2165 va = uint64(Rnd(int64(va), int64(*FlagRound))) 2166 2167 Segrodata.Rwx = 04 2168 Segrodata.Vaddr = va 2169 Segrodata.Fileoff = va - Segtext.Vaddr + Segtext.Fileoff 2170 Segrodata.Filelen = 0 2171 for _, s := range Segrodata.Sections { 2172 va = uint64(Rnd(int64(va), int64(s.Align))) 2173 s.Vaddr = va 2174 va += s.Length 2175 } 2176 2177 Segrodata.Length = va - Segrodata.Vaddr 2178 Segrodata.Filelen = Segrodata.Length 2179 } 2180 if len(Segrelrodata.Sections) > 0 { 2181 // align to page boundary so as not to mix 2182 // rodata, rel-ro data, and executable text. 2183 va = uint64(Rnd(int64(va), int64(*FlagRound))) 2184 2185 Segrelrodata.Rwx = 06 2186 Segrelrodata.Vaddr = va 2187 Segrelrodata.Fileoff = va - Segrodata.Vaddr + Segrodata.Fileoff 2188 Segrelrodata.Filelen = 0 2189 for _, s := range Segrelrodata.Sections { 2190 va = uint64(Rnd(int64(va), int64(s.Align))) 2191 s.Vaddr = va 2192 va += s.Length 2193 } 2194 2195 Segrelrodata.Length = va - Segrelrodata.Vaddr 2196 Segrelrodata.Filelen = Segrelrodata.Length 2197 } 2198 2199 va = uint64(Rnd(int64(va), int64(*FlagRound))) 2200 Segdata.Rwx = 06 2201 Segdata.Vaddr = va 2202 Segdata.Fileoff = va - Segtext.Vaddr + Segtext.Fileoff 2203 Segdata.Filelen = 0 2204 if Headtype == objabi.Hwindows { 2205 Segdata.Fileoff = Segtext.Fileoff + uint64(Rnd(int64(Segtext.Length), PEFILEALIGN)) 2206 } 2207 if Headtype == objabi.Hplan9 { 2208 Segdata.Fileoff = Segtext.Fileoff + Segtext.Filelen 2209 } 2210 var data *Section 2211 var noptr *Section 2212 var bss *Section 2213 var noptrbss *Section 2214 var vlen int64 2215 for i, s := range Segdata.Sections { 2216 if Iself && s.Name == ".tbss" { 2217 continue 2218 } 2219 vlen = int64(s.Length) 2220 if i+1 < len(Segdata.Sections) && !(Iself && Segdata.Sections[i+1].Name == ".tbss") { 2221 vlen = int64(Segdata.Sections[i+1].Vaddr - s.Vaddr) 2222 } 2223 s.Vaddr = va 2224 va += uint64(vlen) 2225 Segdata.Length = va - Segdata.Vaddr 2226 if s.Name == ".data" { 2227 data = s 2228 } 2229 if s.Name == ".noptrdata" { 2230 noptr = s 2231 } 2232 if s.Name == ".bss" { 2233 bss = s 2234 } 2235 if s.Name == ".noptrbss" { 2236 noptrbss = s 2237 } 2238 } 2239 2240 Segdata.Filelen = bss.Vaddr - Segdata.Vaddr 2241 2242 va = uint64(Rnd(int64(va), int64(*FlagRound))) 2243 Segdwarf.Rwx = 06 2244 Segdwarf.Vaddr = va 2245 Segdwarf.Fileoff = Segdata.Fileoff + uint64(Rnd(int64(Segdata.Filelen), int64(*FlagRound))) 2246 Segdwarf.Filelen = 0 2247 if Headtype == objabi.Hwindows { 2248 Segdwarf.Fileoff = Segdata.Fileoff + uint64(Rnd(int64(Segdata.Filelen), int64(PEFILEALIGN))) 2249 } 2250 for i, s := range Segdwarf.Sections { 2251 vlen = int64(s.Length) 2252 if i+1 < len(Segdwarf.Sections) { 2253 vlen = int64(Segdwarf.Sections[i+1].Vaddr - s.Vaddr) 2254 } 2255 s.Vaddr = va 2256 va += uint64(vlen) 2257 if Headtype == objabi.Hwindows { 2258 va = uint64(Rnd(int64(va), PEFILEALIGN)) 2259 } 2260 Segdwarf.Length = va - Segdwarf.Vaddr 2261 } 2262 2263 Segdwarf.Filelen = va - Segdwarf.Vaddr 2264 2265 var ( 2266 text = Segtext.Sections[0] 2267 rodata = ctxt.Syms.Lookup("runtime.rodata", 0).Sect 2268 itablink = ctxt.Syms.Lookup("runtime.itablink", 0).Sect 2269 symtab = ctxt.Syms.Lookup("runtime.symtab", 0).Sect 2270 pclntab = ctxt.Syms.Lookup("runtime.pclntab", 0).Sect 2271 types = ctxt.Syms.Lookup("runtime.types", 0).Sect 2272 ) 2273 lasttext := text 2274 // Could be multiple .text sections 2275 for _, sect := range Segtext.Sections { 2276 if sect.Name == ".text" { 2277 lasttext = sect 2278 } 2279 } 2280 2281 for _, s := range datap { 2282 if s.Sect != nil { 2283 s.Value += int64(s.Sect.Vaddr) 2284 } 2285 for sub := s.Sub; sub != nil; sub = sub.Sub { 2286 sub.Value += s.Value 2287 } 2288 } 2289 2290 for _, sym := range dwarfp { 2291 if sym.Sect != nil { 2292 sym.Value += int64(sym.Sect.Vaddr) 2293 } 2294 for sub := sym.Sub; sub != nil; sub = sub.Sub { 2295 sub.Value += sym.Value 2296 } 2297 } 2298 2299 if Buildmode == BuildmodeShared { 2300 s := ctxt.Syms.Lookup("go.link.abihashbytes", 0) 2301 sectSym := ctxt.Syms.Lookup(".note.go.abihash", 0) 2302 s.Sect = sectSym.Sect 2303 s.Value = int64(sectSym.Sect.Vaddr + 16) 2304 } 2305 2306 ctxt.xdefine("runtime.text", STEXT, int64(text.Vaddr)) 2307 ctxt.xdefine("runtime.etext", STEXT, int64(lasttext.Vaddr+lasttext.Length)) 2308 2309 // If there are multiple text sections, create runtime.text.n for 2310 // their section Vaddr, using n for index 2311 n := 1 2312 for _, sect := range Segtext.Sections[1:] { 2313 if sect.Name == ".text" { 2314 symname := fmt.Sprintf("runtime.text.%d", n) 2315 ctxt.xdefine(symname, STEXT, int64(sect.Vaddr)) 2316 n++ 2317 } else { 2318 break 2319 } 2320 } 2321 2322 ctxt.xdefine("runtime.rodata", SRODATA, int64(rodata.Vaddr)) 2323 ctxt.xdefine("runtime.erodata", SRODATA, int64(rodata.Vaddr+rodata.Length)) 2324 ctxt.xdefine("runtime.types", SRODATA, int64(types.Vaddr)) 2325 ctxt.xdefine("runtime.etypes", SRODATA, int64(types.Vaddr+types.Length)) 2326 ctxt.xdefine("runtime.itablink", SRODATA, int64(itablink.Vaddr)) 2327 ctxt.xdefine("runtime.eitablink", SRODATA, int64(itablink.Vaddr+itablink.Length)) 2328 2329 sym := ctxt.Syms.Lookup("runtime.gcdata", 0) 2330 sym.Attr |= AttrLocal 2331 ctxt.xdefine("runtime.egcdata", SRODATA, Symaddr(sym)+sym.Size) 2332 ctxt.Syms.Lookup("runtime.egcdata", 0).Sect = sym.Sect 2333 2334 sym = ctxt.Syms.Lookup("runtime.gcbss", 0) 2335 sym.Attr |= AttrLocal 2336 ctxt.xdefine("runtime.egcbss", SRODATA, Symaddr(sym)+sym.Size) 2337 ctxt.Syms.Lookup("runtime.egcbss", 0).Sect = sym.Sect 2338 2339 ctxt.xdefine("runtime.symtab", SRODATA, int64(symtab.Vaddr)) 2340 ctxt.xdefine("runtime.esymtab", SRODATA, int64(symtab.Vaddr+symtab.Length)) 2341 ctxt.xdefine("runtime.pclntab", SRODATA, int64(pclntab.Vaddr)) 2342 ctxt.xdefine("runtime.epclntab", SRODATA, int64(pclntab.Vaddr+pclntab.Length)) 2343 ctxt.xdefine("runtime.noptrdata", SNOPTRDATA, int64(noptr.Vaddr)) 2344 ctxt.xdefine("runtime.enoptrdata", SNOPTRDATA, int64(noptr.Vaddr+noptr.Length)) 2345 ctxt.xdefine("runtime.bss", SBSS, int64(bss.Vaddr)) 2346 ctxt.xdefine("runtime.ebss", SBSS, int64(bss.Vaddr+bss.Length)) 2347 ctxt.xdefine("runtime.data", SDATA, int64(data.Vaddr)) 2348 ctxt.xdefine("runtime.edata", SDATA, int64(data.Vaddr+data.Length)) 2349 ctxt.xdefine("runtime.noptrbss", SNOPTRBSS, int64(noptrbss.Vaddr)) 2350 ctxt.xdefine("runtime.enoptrbss", SNOPTRBSS, int64(noptrbss.Vaddr+noptrbss.Length)) 2351 ctxt.xdefine("runtime.end", SBSS, int64(Segdata.Vaddr+Segdata.Length)) 2352 } 2353 2354 // add a trampoline with symbol s (to be laid down after the current function) 2355 func (ctxt *Link) AddTramp(s *Symbol) { 2356 s.Type = STEXT 2357 s.Attr |= AttrReachable 2358 s.Attr |= AttrOnList 2359 ctxt.tramps = append(ctxt.tramps, s) 2360 if *FlagDebugTramp > 0 && ctxt.Debugvlog > 0 { 2361 ctxt.Logf("trampoline %s inserted\n", s) 2362 } 2363 }