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