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