github.com/gocuntian/go@v0.0.0-20160610041250-fee02d270bf8/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 if Debug['a'] != 0 { 794 fmt.Fprintf(Bso, "codeblk [%#x,%#x) at offset %#x\n", addr, addr+size, Cpos()) 795 } 796 797 blkSlice(Ctxt.Textp, addr, size) 798 799 /* again for printing */ 800 if Debug['a'] == 0 { 801 return 802 } 803 804 syms := Ctxt.Textp 805 for i, sym := range syms { 806 if !sym.Attr.Reachable() { 807 continue 808 } 809 if sym.Value >= addr { 810 syms = syms[i:] 811 break 812 } 813 } 814 815 eaddr := addr + size 816 var q []byte 817 for _, sym := range syms { 818 if !sym.Attr.Reachable() { 819 continue 820 } 821 if sym.Value >= eaddr { 822 break 823 } 824 825 if addr < sym.Value { 826 fmt.Fprintf(Bso, "%-20s %.8x|", "_", uint64(addr)) 827 for ; addr < sym.Value; addr++ { 828 fmt.Fprintf(Bso, " %.2x", 0) 829 } 830 fmt.Fprintf(Bso, "\n") 831 } 832 833 fmt.Fprintf(Bso, "%.6x\t%-20s\n", uint64(addr), sym.Name) 834 q = sym.P 835 836 for len(q) >= 16 { 837 fmt.Fprintf(Bso, "%.6x\t% x\n", uint64(addr), q[:16]) 838 addr += 16 839 q = q[16:] 840 } 841 842 if len(q) > 0 { 843 fmt.Fprintf(Bso, "%.6x\t% x\n", uint64(addr), q) 844 addr += int64(len(q)) 845 } 846 } 847 848 if addr < eaddr { 849 fmt.Fprintf(Bso, "%-20s %.8x|", "_", uint64(addr)) 850 for ; addr < eaddr; addr++ { 851 fmt.Fprintf(Bso, " %.2x", 0) 852 } 853 } 854 855 Bso.Flush() 856 } 857 858 // blkSlice is a variant of blk that processes slices. 859 // After text symbols are converted from a linked list to a slice, 860 // delete blk and give this function its name. 861 func blkSlice(syms []*LSym, addr, size int64) { 862 for i, s := range syms { 863 if s.Type&obj.SSUB == 0 && s.Value >= addr { 864 syms = syms[i:] 865 break 866 } 867 } 868 869 eaddr := addr + size 870 for _, s := range syms { 871 if s.Type&obj.SSUB != 0 { 872 continue 873 } 874 if s.Value >= eaddr { 875 break 876 } 877 Ctxt.Cursym = s 878 if s.Value < addr { 879 Diag("phase error: addr=%#x but sym=%#x type=%d", addr, s.Value, s.Type) 880 errorexit() 881 } 882 if addr < s.Value { 883 strnput("", int(s.Value-addr)) 884 addr = s.Value 885 } 886 Cwrite(s.P) 887 addr += int64(len(s.P)) 888 if addr < s.Value+s.Size { 889 strnput("", int(s.Value+s.Size-addr)) 890 addr = s.Value + s.Size 891 } 892 if addr != s.Value+s.Size { 893 Diag("phase error: addr=%#x value+size=%#x", addr, s.Value+s.Size) 894 errorexit() 895 } 896 if s.Value+s.Size >= eaddr { 897 break 898 } 899 } 900 901 if addr < eaddr { 902 strnput("", int(eaddr-addr)) 903 } 904 Cflush() 905 } 906 907 func Datblk(addr int64, size int64) { 908 if Debug['a'] != 0 { 909 fmt.Fprintf(Bso, "datblk [%#x,%#x) at offset %#x\n", addr, addr+size, Cpos()) 910 } 911 912 blkSlice(datap, addr, size) 913 914 /* again for printing */ 915 if Debug['a'] == 0 { 916 return 917 } 918 919 syms := datap 920 for i, sym := range syms { 921 if sym.Value >= addr { 922 syms = syms[i:] 923 break 924 } 925 } 926 927 eaddr := addr + size 928 for _, sym := range syms { 929 if sym.Value >= eaddr { 930 break 931 } 932 if addr < sym.Value { 933 fmt.Fprintf(Bso, "\t%.8x| 00 ...\n", uint64(addr)) 934 addr = sym.Value 935 } 936 937 fmt.Fprintf(Bso, "%s\n\t%.8x|", sym.Name, uint64(addr)) 938 for i, b := range sym.P { 939 if i > 0 && i%16 == 0 { 940 fmt.Fprintf(Bso, "\n\t%.8x|", uint64(addr)+uint64(i)) 941 } 942 fmt.Fprintf(Bso, " %.2x", b) 943 } 944 945 addr += int64(len(sym.P)) 946 for ; addr < sym.Value+sym.Size; addr++ { 947 fmt.Fprintf(Bso, " %.2x", 0) 948 } 949 fmt.Fprintf(Bso, "\n") 950 951 if Linkmode != LinkExternal { 952 continue 953 } 954 for _, r := range sym.R { 955 rsname := "" 956 if r.Sym != nil { 957 rsname = r.Sym.Name 958 } 959 typ := "?" 960 switch r.Type { 961 case obj.R_ADDR: 962 typ = "addr" 963 case obj.R_PCREL: 964 typ = "pcrel" 965 case obj.R_CALL: 966 typ = "call" 967 } 968 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) 969 } 970 } 971 972 if addr < eaddr { 973 fmt.Fprintf(Bso, "\t%.8x| 00 ...\n", uint(addr)) 974 } 975 fmt.Fprintf(Bso, "\t%.8x|\n", uint(eaddr)) 976 } 977 978 func Dwarfblk(addr int64, size int64) { 979 if Debug['a'] != 0 { 980 fmt.Fprintf(Bso, "dwarfblk [%#x,%#x) at offset %#x\n", addr, addr+size, Cpos()) 981 } 982 983 blk(dwarfp, addr, size) 984 } 985 986 var zeros [512]byte 987 988 // strnput writes the first n bytes of s. 989 // If n is larger then len(s), 990 // it is padded with NUL bytes. 991 func strnput(s string, n int) { 992 if len(s) >= n { 993 Cwritestring(s[:n]) 994 } else { 995 Cwritestring(s) 996 n -= len(s) 997 for n > 0 { 998 if len(zeros) >= n { 999 Cwrite(zeros[:n]) 1000 return 1001 } else { 1002 Cwrite(zeros[:]) 1003 n -= len(zeros) 1004 } 1005 } 1006 } 1007 } 1008 1009 var strdata []*LSym 1010 1011 func addstrdata1(arg string) { 1012 i := strings.Index(arg, "=") 1013 if i < 0 { 1014 Exitf("-X flag requires argument of the form importpath.name=value") 1015 } 1016 addstrdata(arg[:i], arg[i+1:]) 1017 } 1018 1019 func addstrdata(name string, value string) { 1020 p := fmt.Sprintf("%s.str", name) 1021 sp := Linklookup(Ctxt, p, 0) 1022 1023 Addstring(sp, value) 1024 sp.Type = obj.SRODATA 1025 1026 s := Linklookup(Ctxt, name, 0) 1027 s.Size = 0 1028 s.Attr |= AttrDuplicateOK 1029 reachable := s.Attr.Reachable() 1030 Addaddr(Ctxt, s, sp) 1031 adduintxx(Ctxt, s, uint64(len(value)), SysArch.PtrSize) 1032 1033 // addstring, addaddr, etc., mark the symbols as reachable. 1034 // In this case that is not necessarily true, so stick to what 1035 // we know before entering this function. 1036 s.Attr.Set(AttrReachable, reachable) 1037 1038 strdata = append(strdata, s) 1039 1040 sp.Attr.Set(AttrReachable, reachable) 1041 } 1042 1043 func checkstrdata() { 1044 for _, s := range strdata { 1045 if s.Type == obj.STEXT { 1046 Diag("cannot use -X with text symbol %s", s.Name) 1047 } else if s.Gotype != nil && s.Gotype.Name != "type.string" { 1048 Diag("cannot use -X with non-string symbol %s", s.Name) 1049 } 1050 } 1051 } 1052 1053 func Addstring(s *LSym, str string) int64 { 1054 if s.Type == 0 { 1055 s.Type = obj.SNOPTRDATA 1056 } 1057 s.Attr |= AttrReachable 1058 r := s.Size 1059 if s.Name == ".shstrtab" { 1060 elfsetstring(str, int(r)) 1061 } 1062 s.P = append(s.P, str...) 1063 s.P = append(s.P, 0) 1064 s.Size = int64(len(s.P)) 1065 return r 1066 } 1067 1068 // addgostring adds str, as a Go string value, to s. symname is the name of the 1069 // symbol used to define the string data and must be unique per linked object. 1070 func addgostring(s *LSym, symname, str string) { 1071 sym := Linklookup(Ctxt, symname, 0) 1072 if sym.Type != obj.Sxxx { 1073 Diag("duplicate symname in addgostring: %s", symname) 1074 } 1075 sym.Attr |= AttrReachable 1076 sym.Attr |= AttrLocal 1077 sym.Type = obj.SRODATA 1078 sym.Size = int64(len(str)) 1079 sym.P = []byte(str) 1080 Addaddr(Ctxt, s, sym) 1081 adduint(Ctxt, s, uint64(len(str))) 1082 } 1083 1084 func addinitarrdata(s *LSym) { 1085 p := s.Name + ".ptr" 1086 sp := Linklookup(Ctxt, p, 0) 1087 sp.Type = obj.SINITARR 1088 sp.Size = 0 1089 sp.Attr |= AttrDuplicateOK 1090 Addaddr(Ctxt, sp, s) 1091 } 1092 1093 func dosymtype() { 1094 for _, s := range Ctxt.Allsym { 1095 if len(s.P) > 0 { 1096 if s.Type == obj.SBSS { 1097 s.Type = obj.SDATA 1098 } 1099 if s.Type == obj.SNOPTRBSS { 1100 s.Type = obj.SNOPTRDATA 1101 } 1102 } 1103 // Create a new entry in the .init_array section that points to the 1104 // library initializer function. 1105 switch Buildmode { 1106 case BuildmodeCArchive, BuildmodeCShared: 1107 if s.Name == INITENTRY { 1108 addinitarrdata(s) 1109 } 1110 } 1111 } 1112 } 1113 1114 // symalign returns the required alignment for the given symbol s. 1115 func symalign(s *LSym) int32 { 1116 min := int32(Thearch.Minalign) 1117 if s.Align >= min { 1118 return s.Align 1119 } else if s.Align != 0 { 1120 return min 1121 } 1122 if (strings.HasPrefix(s.Name, "go.string.") && !strings.HasPrefix(s.Name, "go.string.hdr.")) || strings.HasPrefix(s.Name, "type..namedata.") { 1123 // String data is just bytes. 1124 // If we align it, we waste a lot of space to padding. 1125 return min 1126 } 1127 align := int32(Thearch.Maxalign) 1128 for int64(align) > s.Size && align > min { 1129 align >>= 1 1130 } 1131 return align 1132 } 1133 1134 func aligndatsize(datsize int64, s *LSym) int64 { 1135 return Rnd(datsize, int64(symalign(s))) 1136 } 1137 1138 const debugGCProg = false 1139 1140 type GCProg struct { 1141 sym *LSym 1142 w gcprog.Writer 1143 } 1144 1145 func (p *GCProg) Init(name string) { 1146 p.sym = Linklookup(Ctxt, name, 0) 1147 p.w.Init(p.writeByte) 1148 if debugGCProg { 1149 fmt.Fprintf(os.Stderr, "ld: start GCProg %s\n", name) 1150 p.w.Debug(os.Stderr) 1151 } 1152 } 1153 1154 func (p *GCProg) writeByte(x byte) { 1155 Adduint8(Ctxt, p.sym, x) 1156 } 1157 1158 func (p *GCProg) End(size int64) { 1159 p.w.ZeroUntil(size / int64(SysArch.PtrSize)) 1160 p.w.End() 1161 if debugGCProg { 1162 fmt.Fprintf(os.Stderr, "ld: end GCProg\n") 1163 } 1164 } 1165 1166 func (p *GCProg) AddSym(s *LSym) { 1167 typ := s.Gotype 1168 // Things without pointers should be in SNOPTRDATA or SNOPTRBSS; 1169 // everything we see should have pointers and should therefore have a type. 1170 if typ == nil { 1171 Diag("missing Go type information for global symbol: %s size %d", s.Name, int(s.Size)) 1172 return 1173 } 1174 1175 ptrsize := int64(SysArch.PtrSize) 1176 nptr := decodetype_ptrdata(typ) / ptrsize 1177 1178 if debugGCProg { 1179 fmt.Fprintf(os.Stderr, "gcprog sym: %s at %d (ptr=%d+%d)\n", s.Name, s.Value, s.Value/ptrsize, nptr) 1180 } 1181 1182 if decodetype_usegcprog(typ) == 0 { 1183 // Copy pointers from mask into program. 1184 mask := decodetype_gcmask(typ) 1185 for i := int64(0); i < nptr; i++ { 1186 if (mask[i/8]>>uint(i%8))&1 != 0 { 1187 p.w.Ptr(s.Value/ptrsize + i) 1188 } 1189 } 1190 return 1191 } 1192 1193 // Copy program. 1194 prog := decodetype_gcprog(typ) 1195 p.w.ZeroUntil(s.Value / ptrsize) 1196 p.w.Append(prog[4:], nptr) 1197 } 1198 1199 // dataSortKey is used to sort a slice of data symbol *LSym pointers. 1200 // The sort keys are kept inline to improve cache behaviour while sorting. 1201 type dataSortKey struct { 1202 size int64 1203 name string 1204 lsym *LSym 1205 } 1206 1207 type bySizeAndName []dataSortKey 1208 1209 func (d bySizeAndName) Len() int { return len(d) } 1210 func (d bySizeAndName) Swap(i, j int) { d[i], d[j] = d[j], d[i] } 1211 func (d bySizeAndName) Less(i, j int) bool { 1212 s1, s2 := d[i], d[j] 1213 if s1.size != s2.size { 1214 return s1.size < s2.size 1215 } 1216 return s1.name < s2.name 1217 } 1218 1219 const cutoff int64 = 2e9 // 2 GB (or so; looks better in errors than 2^31) 1220 1221 func checkdatsize(datsize int64, symn int) { 1222 if datsize > cutoff { 1223 Diag("too much data in section %v (over %d bytes)", symn, cutoff) 1224 } 1225 } 1226 1227 func list2slice(s *LSym) []*LSym { 1228 var syms []*LSym 1229 for ; s != nil; s = s.Next { 1230 syms = append(syms, s) 1231 } 1232 return syms 1233 } 1234 1235 // datap is a collection of reachable data symbols in address order. 1236 // Generated by dodata. 1237 var datap []*LSym 1238 1239 func dodata() { 1240 if Debug['v'] != 0 { 1241 fmt.Fprintf(Bso, "%5.2f dodata\n", obj.Cputime()) 1242 } 1243 Bso.Flush() 1244 1245 // Collect data symbols by type into data. 1246 var data [obj.SXREF][]*LSym 1247 for _, s := range Ctxt.Allsym { 1248 if !s.Attr.Reachable() || s.Attr.Special() { 1249 continue 1250 } 1251 if s.Type <= obj.STEXT || s.Type >= obj.SXREF { 1252 continue 1253 } 1254 data[s.Type] = append(data[s.Type], s) 1255 } 1256 1257 // Now that we have the data symbols, but before we start 1258 // to assign addresses, record all the necessary 1259 // dynamic relocations. These will grow the relocation 1260 // symbol, which is itself data. 1261 // 1262 // On darwin, we need the symbol table numbers for dynreloc. 1263 if HEADTYPE == obj.Hdarwin { 1264 machosymorder() 1265 } 1266 dynreloc(&data) 1267 1268 if UseRelro() { 1269 // "read only" data with relocations needs to go in its own section 1270 // when building a shared library. We do this by boosting objects of 1271 // type SXXX with relocations to type SXXXRELRO. 1272 for symnro := int16(obj.STYPE); symnro < obj.STYPERELRO; symnro++ { 1273 symnrelro := symnro + obj.STYPERELRO - obj.STYPE 1274 1275 ro := []*LSym{} 1276 relro := data[symnrelro] 1277 1278 for _, s := range data[symnro] { 1279 isRelro := len(s.R) > 0 1280 switch s.Type { 1281 case obj.STYPE, obj.SGOSTRINGHDR, obj.STYPERELRO, obj.SGOSTRINGHDRRELRO: 1282 // Symbols are not sorted yet, so it is possible 1283 // that an Outer symbol has been changed to a 1284 // relro Type before it reaches here. 1285 isRelro = true 1286 } 1287 if isRelro { 1288 s.Type = symnrelro 1289 if s.Outer != nil { 1290 s.Outer.Type = s.Type 1291 } 1292 relro = append(relro, s) 1293 } else { 1294 ro = append(ro, s) 1295 } 1296 } 1297 1298 // Check that we haven't made two symbols with the same .Outer into 1299 // different types (because references two symbols with non-nil Outer 1300 // become references to the outer symbol + offset it's vital that the 1301 // symbol and the outer end up in the same section). 1302 for _, s := range relro { 1303 if s.Outer != nil && s.Outer.Type != s.Type { 1304 Diag("inconsistent types for %s and its Outer %s (%d != %d)", 1305 s.Name, s.Outer.Name, s.Type, s.Outer.Type) 1306 } 1307 } 1308 1309 data[symnro] = ro 1310 data[symnrelro] = relro 1311 } 1312 } 1313 1314 // Sort symbols. 1315 var dataMaxAlign [obj.SXREF]int32 1316 var wg sync.WaitGroup 1317 for symn := range data { 1318 symn := symn 1319 wg.Add(1) 1320 go func() { 1321 data[symn], dataMaxAlign[symn] = dodataSect(symn, data[symn]) 1322 wg.Done() 1323 }() 1324 } 1325 wg.Wait() 1326 1327 // Allocate sections. 1328 // Data is processed before segtext, because we need 1329 // to see all symbols in the .data and .bss sections in order 1330 // to generate garbage collection information. 1331 datsize := int64(0) 1332 1333 // Writable sections. 1334 writableSects := []int{ 1335 obj.SELFSECT, 1336 obj.SMACHO, 1337 obj.SMACHOGOT, 1338 obj.SWINDOWS, 1339 } 1340 for _, symn := range writableSects { 1341 for _, s := range data[symn] { 1342 sect := addsection(&Segdata, s.Name, 06) 1343 sect.Align = symalign(s) 1344 datsize = Rnd(datsize, int64(sect.Align)) 1345 sect.Vaddr = uint64(datsize) 1346 s.Sect = sect 1347 s.Type = obj.SDATA 1348 s.Value = int64(uint64(datsize) - sect.Vaddr) 1349 datsize += s.Size 1350 sect.Length = uint64(datsize) - sect.Vaddr 1351 } 1352 checkdatsize(datsize, symn) 1353 } 1354 1355 // .got (and .toc on ppc64) 1356 if len(data[obj.SELFGOT]) > 0 { 1357 sect := addsection(&Segdata, ".got", 06) 1358 sect.Align = dataMaxAlign[obj.SELFGOT] 1359 datsize = Rnd(datsize, int64(sect.Align)) 1360 sect.Vaddr = uint64(datsize) 1361 var toc *LSym 1362 for _, s := range data[obj.SELFGOT] { 1363 datsize = aligndatsize(datsize, s) 1364 s.Sect = sect 1365 s.Type = obj.SDATA 1366 s.Value = int64(uint64(datsize) - sect.Vaddr) 1367 1368 // Resolve .TOC. symbol for this object file (ppc64) 1369 toc = Linkrlookup(Ctxt, ".TOC.", int(s.Version)) 1370 if toc != nil { 1371 toc.Sect = sect 1372 toc.Outer = s 1373 toc.Sub = s.Sub 1374 s.Sub = toc 1375 1376 toc.Value = 0x8000 1377 } 1378 1379 datsize += s.Size 1380 } 1381 checkdatsize(datsize, obj.SELFGOT) 1382 sect.Length = uint64(datsize) - sect.Vaddr 1383 } 1384 1385 /* pointer-free data */ 1386 sect := addsection(&Segdata, ".noptrdata", 06) 1387 sect.Align = dataMaxAlign[obj.SNOPTRDATA] 1388 datsize = Rnd(datsize, int64(sect.Align)) 1389 sect.Vaddr = uint64(datsize) 1390 Linklookup(Ctxt, "runtime.noptrdata", 0).Sect = sect 1391 Linklookup(Ctxt, "runtime.enoptrdata", 0).Sect = sect 1392 for _, s := range data[obj.SNOPTRDATA] { 1393 datsize = aligndatsize(datsize, s) 1394 s.Sect = sect 1395 s.Type = obj.SDATA 1396 s.Value = int64(uint64(datsize) - sect.Vaddr) 1397 datsize += s.Size 1398 } 1399 checkdatsize(datsize, obj.SNOPTRDATA) 1400 sect.Length = uint64(datsize) - sect.Vaddr 1401 1402 hasinitarr := Linkshared 1403 1404 /* shared library initializer */ 1405 switch Buildmode { 1406 case BuildmodeCArchive, BuildmodeCShared, BuildmodeShared: 1407 hasinitarr = true 1408 } 1409 if hasinitarr { 1410 sect := addsection(&Segdata, ".init_array", 06) 1411 sect.Align = dataMaxAlign[obj.SINITARR] 1412 datsize = Rnd(datsize, int64(sect.Align)) 1413 sect.Vaddr = uint64(datsize) 1414 for _, s := range data[obj.SINITARR] { 1415 datsize = aligndatsize(datsize, s) 1416 s.Sect = sect 1417 s.Value = int64(uint64(datsize) - sect.Vaddr) 1418 datsize += s.Size 1419 } 1420 sect.Length = uint64(datsize) - sect.Vaddr 1421 checkdatsize(datsize, obj.SINITARR) 1422 } 1423 1424 /* data */ 1425 sect = addsection(&Segdata, ".data", 06) 1426 sect.Align = dataMaxAlign[obj.SDATA] 1427 datsize = Rnd(datsize, int64(sect.Align)) 1428 sect.Vaddr = uint64(datsize) 1429 Linklookup(Ctxt, "runtime.data", 0).Sect = sect 1430 Linklookup(Ctxt, "runtime.edata", 0).Sect = sect 1431 var gc GCProg 1432 gc.Init("runtime.gcdata") 1433 for _, s := range data[obj.SDATA] { 1434 s.Sect = sect 1435 s.Type = obj.SDATA 1436 datsize = aligndatsize(datsize, s) 1437 s.Value = int64(uint64(datsize) - sect.Vaddr) 1438 gc.AddSym(s) 1439 datsize += s.Size 1440 } 1441 checkdatsize(datsize, obj.SDATA) 1442 sect.Length = uint64(datsize) - sect.Vaddr 1443 gc.End(int64(sect.Length)) 1444 1445 /* bss */ 1446 sect = addsection(&Segdata, ".bss", 06) 1447 sect.Align = dataMaxAlign[obj.SBSS] 1448 datsize = Rnd(datsize, int64(sect.Align)) 1449 sect.Vaddr = uint64(datsize) 1450 Linklookup(Ctxt, "runtime.bss", 0).Sect = sect 1451 Linklookup(Ctxt, "runtime.ebss", 0).Sect = sect 1452 gc = GCProg{} 1453 gc.Init("runtime.gcbss") 1454 for _, s := range data[obj.SBSS] { 1455 s.Sect = sect 1456 datsize = aligndatsize(datsize, s) 1457 s.Value = int64(uint64(datsize) - sect.Vaddr) 1458 gc.AddSym(s) 1459 datsize += s.Size 1460 } 1461 checkdatsize(datsize, obj.SBSS) 1462 sect.Length = uint64(datsize) - sect.Vaddr 1463 gc.End(int64(sect.Length)) 1464 1465 /* pointer-free bss */ 1466 sect = addsection(&Segdata, ".noptrbss", 06) 1467 sect.Align = dataMaxAlign[obj.SNOPTRBSS] 1468 datsize = Rnd(datsize, int64(sect.Align)) 1469 sect.Vaddr = uint64(datsize) 1470 Linklookup(Ctxt, "runtime.noptrbss", 0).Sect = sect 1471 Linklookup(Ctxt, "runtime.enoptrbss", 0).Sect = sect 1472 for _, s := range data[obj.SNOPTRBSS] { 1473 datsize = aligndatsize(datsize, s) 1474 s.Sect = sect 1475 s.Value = int64(uint64(datsize) - sect.Vaddr) 1476 datsize += s.Size 1477 } 1478 1479 sect.Length = uint64(datsize) - sect.Vaddr 1480 Linklookup(Ctxt, "runtime.end", 0).Sect = sect 1481 checkdatsize(datsize, obj.SNOPTRBSS) 1482 1483 if len(data[obj.STLSBSS]) > 0 { 1484 var sect *Section 1485 if Iself && (Linkmode == LinkExternal || Debug['d'] == 0) && HEADTYPE != obj.Hopenbsd { 1486 sect = addsection(&Segdata, ".tbss", 06) 1487 sect.Align = int32(SysArch.PtrSize) 1488 sect.Vaddr = 0 1489 } 1490 datsize = 0 1491 1492 for _, s := range data[obj.STLSBSS] { 1493 datsize = aligndatsize(datsize, s) 1494 s.Sect = sect 1495 s.Value = datsize 1496 datsize += s.Size 1497 } 1498 checkdatsize(datsize, obj.STLSBSS) 1499 1500 if sect != nil { 1501 sect.Length = uint64(datsize) 1502 } 1503 } 1504 1505 /* 1506 * We finished data, begin read-only data. 1507 * Not all systems support a separate read-only non-executable data section. 1508 * ELF systems do. 1509 * OS X and Plan 9 do not. 1510 * Windows PE may, but if so we have not implemented it. 1511 * And if we're using external linking mode, the point is moot, 1512 * since it's not our decision; that code expects the sections in 1513 * segtext. 1514 */ 1515 var segro *Segment 1516 if Iself && Linkmode == LinkInternal { 1517 segro = &Segrodata 1518 } else { 1519 segro = &Segtext 1520 } 1521 1522 datsize = 0 1523 1524 /* read-only executable ELF, Mach-O sections */ 1525 if len(data[obj.STEXT]) != 0 { 1526 Diag("dodata found an STEXT symbol: %s", data[obj.STEXT][0].Name) 1527 } 1528 for _, s := range data[obj.SELFRXSECT] { 1529 sect := addsection(&Segtext, s.Name, 04) 1530 sect.Align = symalign(s) 1531 datsize = Rnd(datsize, int64(sect.Align)) 1532 sect.Vaddr = uint64(datsize) 1533 s.Sect = sect 1534 s.Type = obj.SRODATA 1535 s.Value = int64(uint64(datsize) - sect.Vaddr) 1536 datsize += s.Size 1537 sect.Length = uint64(datsize) - sect.Vaddr 1538 checkdatsize(datsize, obj.SELFRXSECT) 1539 } 1540 1541 /* read-only data */ 1542 sect = addsection(segro, ".rodata", 04) 1543 1544 sect.Vaddr = 0 1545 Linklookup(Ctxt, "runtime.rodata", 0).Sect = sect 1546 Linklookup(Ctxt, "runtime.erodata", 0).Sect = sect 1547 if !UseRelro() { 1548 Linklookup(Ctxt, "runtime.types", 0).Sect = sect 1549 Linklookup(Ctxt, "runtime.etypes", 0).Sect = sect 1550 } 1551 roSects := []int{ 1552 obj.STYPE, 1553 obj.SSTRING, 1554 obj.SGOSTRING, 1555 obj.SGOSTRINGHDR, 1556 obj.SGOFUNC, 1557 obj.SGCBITS, 1558 obj.SRODATA, 1559 obj.SFUNCTAB, 1560 } 1561 for _, symn := range roSects { 1562 align := dataMaxAlign[symn] 1563 if sect.Align < align { 1564 sect.Align = align 1565 } 1566 } 1567 datsize = Rnd(datsize, int64(sect.Align)) 1568 for _, symn := range roSects { 1569 for _, s := range data[symn] { 1570 datsize = aligndatsize(datsize, s) 1571 s.Sect = sect 1572 s.Type = obj.SRODATA 1573 s.Value = int64(uint64(datsize) - sect.Vaddr) 1574 datsize += s.Size 1575 } 1576 checkdatsize(datsize, symn) 1577 } 1578 sect.Length = uint64(datsize) - sect.Vaddr 1579 1580 // There is some data that are conceptually read-only but are written to by 1581 // relocations. On GNU systems, we can arrange for the dynamic linker to 1582 // mprotect sections after relocations are applied by giving them write 1583 // permissions in the object file and calling them ".data.rel.ro.FOO". We 1584 // divide the .rodata section between actual .rodata and .data.rel.ro.rodata, 1585 // but for the other sections that this applies to, we just write a read-only 1586 // .FOO section or a read-write .data.rel.ro.FOO section depending on the 1587 // situation. 1588 // TODO(mwhudson): It would make sense to do this more widely, but it makes 1589 // the system linker segfault on darwin. 1590 relro_perms := 04 1591 relro_prefix := "" 1592 1593 if UseRelro() { 1594 relro_perms = 06 1595 relro_prefix = ".data.rel.ro" 1596 /* data only written by relocations */ 1597 sect = addsection(segro, ".data.rel.ro", 06) 1598 1599 sect.Vaddr = 0 1600 Linklookup(Ctxt, "runtime.types", 0).Sect = sect 1601 Linklookup(Ctxt, "runtime.etypes", 0).Sect = sect 1602 relroSects := []int{ 1603 obj.STYPERELRO, 1604 obj.SSTRINGRELRO, 1605 obj.SGOSTRINGRELRO, 1606 obj.SGOSTRINGHDRRELRO, 1607 obj.SGOFUNCRELRO, 1608 obj.SGCBITSRELRO, 1609 obj.SRODATARELRO, 1610 obj.SFUNCTABRELRO, 1611 } 1612 for _, symn := range relroSects { 1613 align := dataMaxAlign[symn] 1614 if sect.Align < align { 1615 sect.Align = align 1616 } 1617 } 1618 datsize = Rnd(datsize, int64(sect.Align)) 1619 for _, symn := range relroSects { 1620 for _, s := range data[symn] { 1621 datsize = aligndatsize(datsize, s) 1622 if s.Outer != nil && s.Outer.Sect != nil && s.Outer.Sect != sect { 1623 Diag("s.Outer (%s) in different section from s (%s)", s.Outer.Name, s.Name) 1624 } 1625 s.Sect = sect 1626 s.Type = obj.SRODATA 1627 s.Value = int64(uint64(datsize) - sect.Vaddr) 1628 datsize += s.Size 1629 } 1630 checkdatsize(datsize, symn) 1631 } 1632 1633 sect.Length = uint64(datsize) - sect.Vaddr 1634 1635 } 1636 1637 /* typelink */ 1638 sect = addsection(segro, relro_prefix+".typelink", relro_perms) 1639 sect.Align = dataMaxAlign[obj.STYPELINK] 1640 datsize = Rnd(datsize, int64(sect.Align)) 1641 sect.Vaddr = uint64(datsize) 1642 Linklookup(Ctxt, "runtime.typelink", 0).Sect = sect 1643 Linklookup(Ctxt, "runtime.etypelink", 0).Sect = sect 1644 for _, s := range data[obj.STYPELINK] { 1645 datsize = aligndatsize(datsize, s) 1646 s.Sect = sect 1647 s.Type = obj.SRODATA 1648 s.Value = int64(uint64(datsize) - sect.Vaddr) 1649 datsize += s.Size 1650 } 1651 checkdatsize(datsize, obj.STYPELINK) 1652 sect.Length = uint64(datsize) - sect.Vaddr 1653 1654 /* itablink */ 1655 sect = addsection(segro, relro_prefix+".itablink", relro_perms) 1656 sect.Align = dataMaxAlign[obj.SITABLINK] 1657 datsize = Rnd(datsize, int64(sect.Align)) 1658 sect.Vaddr = uint64(datsize) 1659 Linklookup(Ctxt, "runtime.itablink", 0).Sect = sect 1660 Linklookup(Ctxt, "runtime.eitablink", 0).Sect = sect 1661 for _, s := range data[obj.SITABLINK] { 1662 datsize = aligndatsize(datsize, s) 1663 s.Sect = sect 1664 s.Type = obj.SRODATA 1665 s.Value = int64(uint64(datsize) - sect.Vaddr) 1666 datsize += s.Size 1667 } 1668 checkdatsize(datsize, obj.SITABLINK) 1669 sect.Length = uint64(datsize) - sect.Vaddr 1670 1671 /* gosymtab */ 1672 sect = addsection(segro, relro_prefix+".gosymtab", relro_perms) 1673 sect.Align = dataMaxAlign[obj.SSYMTAB] 1674 datsize = Rnd(datsize, int64(sect.Align)) 1675 sect.Vaddr = uint64(datsize) 1676 Linklookup(Ctxt, "runtime.symtab", 0).Sect = sect 1677 Linklookup(Ctxt, "runtime.esymtab", 0).Sect = sect 1678 for _, s := range data[obj.SSYMTAB] { 1679 datsize = aligndatsize(datsize, s) 1680 s.Sect = sect 1681 s.Type = obj.SRODATA 1682 s.Value = int64(uint64(datsize) - sect.Vaddr) 1683 datsize += s.Size 1684 } 1685 checkdatsize(datsize, obj.SSYMTAB) 1686 sect.Length = uint64(datsize) - sect.Vaddr 1687 1688 /* gopclntab */ 1689 sect = addsection(segro, relro_prefix+".gopclntab", relro_perms) 1690 sect.Align = dataMaxAlign[obj.SPCLNTAB] 1691 datsize = Rnd(datsize, int64(sect.Align)) 1692 sect.Vaddr = uint64(datsize) 1693 Linklookup(Ctxt, "runtime.pclntab", 0).Sect = sect 1694 Linklookup(Ctxt, "runtime.epclntab", 0).Sect = sect 1695 for _, s := range data[obj.SPCLNTAB] { 1696 datsize = aligndatsize(datsize, s) 1697 s.Sect = sect 1698 s.Type = obj.SRODATA 1699 s.Value = int64(uint64(datsize) - sect.Vaddr) 1700 datsize += s.Size 1701 } 1702 checkdatsize(datsize, obj.SRODATA) 1703 sect.Length = uint64(datsize) - sect.Vaddr 1704 1705 /* read-only ELF, Mach-O sections */ 1706 for _, s := range data[obj.SELFROSECT] { 1707 sect = addsection(segro, s.Name, 04) 1708 sect.Align = symalign(s) 1709 datsize = Rnd(datsize, int64(sect.Align)) 1710 sect.Vaddr = uint64(datsize) 1711 s.Sect = sect 1712 s.Type = obj.SRODATA 1713 s.Value = int64(uint64(datsize) - sect.Vaddr) 1714 datsize += s.Size 1715 sect.Length = uint64(datsize) - sect.Vaddr 1716 } 1717 checkdatsize(datsize, obj.SELFROSECT) 1718 1719 for _, s := range data[obj.SMACHOPLT] { 1720 sect = addsection(segro, s.Name, 04) 1721 sect.Align = symalign(s) 1722 datsize = Rnd(datsize, int64(sect.Align)) 1723 sect.Vaddr = uint64(datsize) 1724 s.Sect = sect 1725 s.Type = obj.SRODATA 1726 s.Value = int64(uint64(datsize) - sect.Vaddr) 1727 datsize += s.Size 1728 sect.Length = uint64(datsize) - sect.Vaddr 1729 } 1730 checkdatsize(datsize, obj.SMACHOPLT) 1731 1732 // 6g uses 4-byte relocation offsets, so the entire segment must fit in 32 bits. 1733 if datsize != int64(uint32(datsize)) { 1734 Diag("read-only data segment too large") 1735 } 1736 1737 for symn := obj.SELFRXSECT; symn < obj.SXREF; symn++ { 1738 datap = append(datap, data[symn]...) 1739 } 1740 1741 dwarfgeneratedebugsyms() 1742 1743 var s *LSym 1744 for s = dwarfp; s != nil && s.Type == obj.SDWARFSECT; s = s.Next { 1745 sect = addsection(&Segdwarf, s.Name, 04) 1746 sect.Align = 1 1747 datsize = Rnd(datsize, int64(sect.Align)) 1748 sect.Vaddr = uint64(datsize) 1749 s.Sect = sect 1750 s.Type = obj.SRODATA 1751 s.Value = int64(uint64(datsize) - sect.Vaddr) 1752 datsize += s.Size 1753 sect.Length = uint64(datsize) - sect.Vaddr 1754 } 1755 checkdatsize(datsize, obj.SDWARFSECT) 1756 1757 if s != nil { 1758 sect = addsection(&Segdwarf, ".debug_info", 04) 1759 sect.Align = 1 1760 datsize = Rnd(datsize, int64(sect.Align)) 1761 sect.Vaddr = uint64(datsize) 1762 for ; s != nil && s.Type == obj.SDWARFINFO; s = s.Next { 1763 s.Sect = sect 1764 s.Type = obj.SRODATA 1765 s.Value = int64(uint64(datsize) - sect.Vaddr) 1766 s.Attr |= AttrLocal 1767 datsize += s.Size 1768 } 1769 sect.Length = uint64(datsize) - sect.Vaddr 1770 checkdatsize(datsize, obj.SDWARFINFO) 1771 } 1772 1773 /* number the sections */ 1774 n := int32(1) 1775 1776 for sect := Segtext.Sect; sect != nil; sect = sect.Next { 1777 sect.Extnum = int16(n) 1778 n++ 1779 } 1780 for sect := Segrodata.Sect; sect != nil; sect = sect.Next { 1781 sect.Extnum = int16(n) 1782 n++ 1783 } 1784 for sect := Segdata.Sect; sect != nil; sect = sect.Next { 1785 sect.Extnum = int16(n) 1786 n++ 1787 } 1788 for sect := Segdwarf.Sect; sect != nil; sect = sect.Next { 1789 sect.Extnum = int16(n) 1790 n++ 1791 } 1792 } 1793 1794 func dodataSect(symn int, syms []*LSym) (result []*LSym, maxAlign int32) { 1795 if HEADTYPE == obj.Hdarwin { 1796 // Some symbols may no longer belong in syms 1797 // due to movement in machosymorder. 1798 newSyms := make([]*LSym, 0, len(syms)) 1799 for _, s := range syms { 1800 if int(s.Type) == symn { 1801 newSyms = append(newSyms, s) 1802 } 1803 } 1804 syms = newSyms 1805 } 1806 1807 symsSort := make([]dataSortKey, len(syms)) 1808 for i, s := range syms { 1809 if s.Attr.OnList() { 1810 log.Fatalf("symbol %s listed multiple times", s.Name) 1811 } 1812 s.Attr |= AttrOnList 1813 switch { 1814 case s.Size < int64(len(s.P)): 1815 Diag("%s: initialize bounds (%d < %d)", s.Name, s.Size, len(s.P)) 1816 case s.Size < 0: 1817 Diag("%s: negative size (%d bytes)", s.Name, s.Size) 1818 case s.Size > cutoff: 1819 Diag("%s: symbol too large (%d bytes)", s.Name, s.Size) 1820 } 1821 1822 symsSort[i] = dataSortKey{ 1823 size: s.Size, 1824 name: s.Name, 1825 lsym: s, 1826 } 1827 1828 switch s.Type { 1829 case obj.SELFGOT: 1830 // For ppc64, we want to interleave the .got and .toc sections 1831 // from input files. Both are type SELFGOT, so in that case 1832 // we skip size comparison and fall through to the name 1833 // comparison (conveniently, .got sorts before .toc). 1834 symsSort[i].size = 0 1835 case obj.STYPELINK: 1836 // Sort typelinks by the rtype.string field so the reflect 1837 // package can binary search type links. 1838 symsSort[i].name = string(decodetype_str(s.R[0].Sym)) 1839 } 1840 } 1841 1842 sort.Sort(bySizeAndName(symsSort)) 1843 1844 for i, symSort := range symsSort { 1845 syms[i] = symSort.lsym 1846 align := symalign(symSort.lsym) 1847 if maxAlign < align { 1848 maxAlign = align 1849 } 1850 } 1851 1852 if Iself && symn == obj.SELFROSECT { 1853 // Make .rela and .rela.plt contiguous, the ELF ABI requires this 1854 // and Solaris actually cares. 1855 reli, plti := -1, -1 1856 for i, s := range syms { 1857 switch s.Name { 1858 case ".rel.plt", ".rela.plt": 1859 plti = i 1860 case ".rel", ".rela": 1861 reli = i 1862 } 1863 } 1864 if reli >= 0 && plti >= 0 && plti != reli+1 { 1865 var first, second int 1866 if plti > reli { 1867 first, second = reli, plti 1868 } else { 1869 first, second = plti, reli 1870 } 1871 rel, plt := syms[reli], syms[plti] 1872 copy(syms[first+2:], syms[first+1:second]) 1873 syms[first+0] = rel 1874 syms[first+1] = plt 1875 } 1876 } 1877 1878 return syms, maxAlign 1879 } 1880 1881 // Add buildid to beginning of text segment, on non-ELF systems. 1882 // Non-ELF binary formats are not always flexible enough to 1883 // give us a place to put the Go build ID. On those systems, we put it 1884 // at the very beginning of the text segment. 1885 // This ``header'' is read by cmd/go. 1886 func textbuildid() { 1887 if Iself || buildid == "" { 1888 return 1889 } 1890 1891 sym := Linklookup(Ctxt, "go.buildid", 0) 1892 sym.Attr |= AttrReachable 1893 // The \xff is invalid UTF-8, meant to make it less likely 1894 // to find one of these accidentally. 1895 data := "\xff Go build ID: " + strconv.Quote(buildid) + "\n \xff" 1896 sym.Type = obj.STEXT 1897 sym.P = []byte(data) 1898 sym.Size = int64(len(sym.P)) 1899 1900 Ctxt.Textp = append(Ctxt.Textp, nil) 1901 copy(Ctxt.Textp[1:], Ctxt.Textp) 1902 Ctxt.Textp[0] = sym 1903 } 1904 1905 // assign addresses to text 1906 func textaddress() { 1907 addsection(&Segtext, ".text", 05) 1908 1909 // Assign PCs in text segment. 1910 // Could parallelize, by assigning to text 1911 // and then letting threads copy down, but probably not worth it. 1912 sect := Segtext.Sect 1913 1914 sect.Align = int32(Funcalign) 1915 Linklookup(Ctxt, "runtime.text", 0).Sect = sect 1916 Linklookup(Ctxt, "runtime.etext", 0).Sect = sect 1917 if HEADTYPE == obj.Hwindows { 1918 Linklookup(Ctxt, ".text", 0).Sect = sect 1919 } 1920 va := uint64(INITTEXT) 1921 sect.Vaddr = va 1922 for _, sym := range Ctxt.Textp { 1923 sym.Sect = sect 1924 if sym.Type&obj.SSUB != 0 { 1925 continue 1926 } 1927 if sym.Align != 0 { 1928 va = uint64(Rnd(int64(va), int64(sym.Align))) 1929 } else { 1930 va = uint64(Rnd(int64(va), int64(Funcalign))) 1931 } 1932 sym.Value = 0 1933 for sub := sym; sub != nil; sub = sub.Sub { 1934 sub.Value += int64(va) 1935 } 1936 if sym.Size == 0 && sym.Sub != nil { 1937 Ctxt.Cursym = sym 1938 } 1939 if sym.Size < MINFUNC { 1940 va += MINFUNC // spacing required for findfunctab 1941 } else { 1942 va += uint64(sym.Size) 1943 } 1944 } 1945 1946 sect.Length = va - sect.Vaddr 1947 } 1948 1949 // assign addresses 1950 func address() { 1951 va := uint64(INITTEXT) 1952 Segtext.Rwx = 05 1953 Segtext.Vaddr = va 1954 Segtext.Fileoff = uint64(HEADR) 1955 for s := Segtext.Sect; s != nil; s = s.Next { 1956 va = uint64(Rnd(int64(va), int64(s.Align))) 1957 s.Vaddr = va 1958 va += s.Length 1959 } 1960 1961 Segtext.Length = va - uint64(INITTEXT) 1962 Segtext.Filelen = Segtext.Length 1963 if HEADTYPE == obj.Hnacl { 1964 va += 32 // room for the "halt sled" 1965 } 1966 1967 if Segrodata.Sect != nil { 1968 // align to page boundary so as not to mix 1969 // rodata and executable text. 1970 va = uint64(Rnd(int64(va), int64(INITRND))) 1971 1972 Segrodata.Rwx = 04 1973 Segrodata.Vaddr = va 1974 Segrodata.Fileoff = va - Segtext.Vaddr + Segtext.Fileoff 1975 Segrodata.Filelen = 0 1976 for s := Segrodata.Sect; s != nil; s = s.Next { 1977 va = uint64(Rnd(int64(va), int64(s.Align))) 1978 s.Vaddr = va 1979 va += s.Length 1980 } 1981 1982 Segrodata.Length = va - Segrodata.Vaddr 1983 Segrodata.Filelen = Segrodata.Length 1984 } 1985 1986 va = uint64(Rnd(int64(va), int64(INITRND))) 1987 Segdata.Rwx = 06 1988 Segdata.Vaddr = va 1989 Segdata.Fileoff = va - Segtext.Vaddr + Segtext.Fileoff 1990 Segdata.Filelen = 0 1991 if HEADTYPE == obj.Hwindows { 1992 Segdata.Fileoff = Segtext.Fileoff + uint64(Rnd(int64(Segtext.Length), PEFILEALIGN)) 1993 } 1994 if HEADTYPE == obj.Hplan9 { 1995 Segdata.Fileoff = Segtext.Fileoff + Segtext.Filelen 1996 } 1997 var data *Section 1998 var noptr *Section 1999 var bss *Section 2000 var noptrbss *Section 2001 var vlen int64 2002 for s := Segdata.Sect; s != nil; s = s.Next { 2003 if Iself && s.Name == ".tbss" { 2004 continue 2005 } 2006 vlen = int64(s.Length) 2007 if s.Next != nil && !(Iself && s.Next.Name == ".tbss") { 2008 vlen = int64(s.Next.Vaddr - s.Vaddr) 2009 } 2010 s.Vaddr = va 2011 va += uint64(vlen) 2012 Segdata.Length = va - Segdata.Vaddr 2013 if s.Name == ".data" { 2014 data = s 2015 } 2016 if s.Name == ".noptrdata" { 2017 noptr = s 2018 } 2019 if s.Name == ".bss" { 2020 bss = s 2021 } 2022 if s.Name == ".noptrbss" { 2023 noptrbss = s 2024 } 2025 } 2026 2027 Segdata.Filelen = bss.Vaddr - Segdata.Vaddr 2028 2029 va = uint64(Rnd(int64(va), int64(INITRND))) 2030 Segdwarf.Rwx = 06 2031 Segdwarf.Vaddr = va 2032 Segdwarf.Fileoff = Segdata.Fileoff + uint64(Rnd(int64(Segdata.Filelen), int64(INITRND))) 2033 Segdwarf.Filelen = 0 2034 if HEADTYPE == obj.Hwindows { 2035 Segdwarf.Fileoff = Segdata.Fileoff + uint64(Rnd(int64(Segdata.Filelen), int64(PEFILEALIGN))) 2036 } 2037 for s := Segdwarf.Sect; s != nil; s = s.Next { 2038 vlen = int64(s.Length) 2039 if s.Next != nil { 2040 vlen = int64(s.Next.Vaddr - s.Vaddr) 2041 } 2042 s.Vaddr = va 2043 va += uint64(vlen) 2044 if HEADTYPE == obj.Hwindows { 2045 va = uint64(Rnd(int64(va), PEFILEALIGN)) 2046 } 2047 Segdwarf.Length = va - Segdwarf.Vaddr 2048 } 2049 2050 Segdwarf.Filelen = va - Segdwarf.Vaddr 2051 2052 text := Segtext.Sect 2053 var rodata *Section 2054 if Segrodata.Sect != nil { 2055 rodata = Segrodata.Sect 2056 } else { 2057 rodata = text.Next 2058 } 2059 var relrodata *Section 2060 typelink := rodata.Next 2061 if UseRelro() { 2062 // There is another section (.data.rel.ro) when building a shared 2063 // object on elf systems. 2064 relrodata = typelink 2065 typelink = typelink.Next 2066 } 2067 itablink := typelink.Next 2068 symtab := itablink.Next 2069 pclntab := symtab.Next 2070 2071 for _, s := range datap { 2072 Ctxt.Cursym = s 2073 if s.Sect != nil { 2074 s.Value += int64(s.Sect.Vaddr) 2075 } 2076 for sub := s.Sub; sub != nil; sub = sub.Sub { 2077 sub.Value += s.Value 2078 } 2079 } 2080 2081 for sym := dwarfp; sym != nil; sym = sym.Next { 2082 Ctxt.Cursym = sym 2083 if sym.Sect != nil { 2084 sym.Value += int64(sym.Sect.Vaddr) 2085 } 2086 for sub := sym.Sub; sub != nil; sub = sub.Sub { 2087 sub.Value += sym.Value 2088 } 2089 } 2090 2091 if Buildmode == BuildmodeShared { 2092 s := Linklookup(Ctxt, "go.link.abihashbytes", 0) 2093 sectSym := Linklookup(Ctxt, ".note.go.abihash", 0) 2094 s.Sect = sectSym.Sect 2095 s.Value = int64(sectSym.Sect.Vaddr + 16) 2096 } 2097 2098 types := relrodata 2099 if types == nil { 2100 types = rodata 2101 } 2102 2103 xdefine("runtime.text", obj.STEXT, int64(text.Vaddr)) 2104 xdefine("runtime.etext", obj.STEXT, int64(text.Vaddr+text.Length)) 2105 if HEADTYPE == obj.Hwindows { 2106 xdefine(".text", obj.STEXT, int64(text.Vaddr)) 2107 } 2108 xdefine("runtime.rodata", obj.SRODATA, int64(rodata.Vaddr)) 2109 xdefine("runtime.erodata", obj.SRODATA, int64(rodata.Vaddr+rodata.Length)) 2110 xdefine("runtime.types", obj.SRODATA, int64(types.Vaddr)) 2111 xdefine("runtime.etypes", obj.SRODATA, int64(types.Vaddr+types.Length)) 2112 xdefine("runtime.typelink", obj.SRODATA, int64(typelink.Vaddr)) 2113 xdefine("runtime.etypelink", obj.SRODATA, int64(typelink.Vaddr+typelink.Length)) 2114 xdefine("runtime.itablink", obj.SRODATA, int64(itablink.Vaddr)) 2115 xdefine("runtime.eitablink", obj.SRODATA, int64(itablink.Vaddr+itablink.Length)) 2116 2117 sym := Linklookup(Ctxt, "runtime.gcdata", 0) 2118 sym.Attr |= AttrLocal 2119 xdefine("runtime.egcdata", obj.SRODATA, Symaddr(sym)+sym.Size) 2120 Linklookup(Ctxt, "runtime.egcdata", 0).Sect = sym.Sect 2121 2122 sym = Linklookup(Ctxt, "runtime.gcbss", 0) 2123 sym.Attr |= AttrLocal 2124 xdefine("runtime.egcbss", obj.SRODATA, Symaddr(sym)+sym.Size) 2125 Linklookup(Ctxt, "runtime.egcbss", 0).Sect = sym.Sect 2126 2127 xdefine("runtime.symtab", obj.SRODATA, int64(symtab.Vaddr)) 2128 xdefine("runtime.esymtab", obj.SRODATA, int64(symtab.Vaddr+symtab.Length)) 2129 xdefine("runtime.pclntab", obj.SRODATA, int64(pclntab.Vaddr)) 2130 xdefine("runtime.epclntab", obj.SRODATA, int64(pclntab.Vaddr+pclntab.Length)) 2131 xdefine("runtime.noptrdata", obj.SNOPTRDATA, int64(noptr.Vaddr)) 2132 xdefine("runtime.enoptrdata", obj.SNOPTRDATA, int64(noptr.Vaddr+noptr.Length)) 2133 xdefine("runtime.bss", obj.SBSS, int64(bss.Vaddr)) 2134 xdefine("runtime.ebss", obj.SBSS, int64(bss.Vaddr+bss.Length)) 2135 xdefine("runtime.data", obj.SDATA, int64(data.Vaddr)) 2136 xdefine("runtime.edata", obj.SDATA, int64(data.Vaddr+data.Length)) 2137 xdefine("runtime.noptrbss", obj.SNOPTRBSS, int64(noptrbss.Vaddr)) 2138 xdefine("runtime.enoptrbss", obj.SNOPTRBSS, int64(noptrbss.Vaddr+noptrbss.Length)) 2139 xdefine("runtime.end", obj.SBSS, int64(Segdata.Vaddr+Segdata.Length)) 2140 }