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