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