github.com/d4l3k/go@v0.0.0-20151015000803-65fc379daeda/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 "fmt" 38 "log" 39 "os" 40 "strconv" 41 "strings" 42 ) 43 44 func Symgrow(ctxt *Link, s *LSym, siz int64) { 45 if int64(int(siz)) != siz { 46 log.Fatalf("symgrow size %d too long", siz) 47 } 48 if int64(len(s.P)) >= siz { 49 return 50 } 51 for cap(s.P) < int(siz) { 52 s.P = append(s.P[:len(s.P)], 0) 53 } 54 s.P = s.P[:siz] 55 } 56 57 func Addrel(s *LSym) *Reloc { 58 s.R = append(s.R, Reloc{}) 59 return &s.R[len(s.R)-1] 60 } 61 62 func setuintxx(ctxt *Link, s *LSym, off int64, v uint64, wid int64) int64 { 63 if s.Type == 0 { 64 s.Type = obj.SDATA 65 } 66 s.Reachable = true 67 if s.Size < off+wid { 68 s.Size = off + wid 69 Symgrow(ctxt, s, s.Size) 70 } 71 72 switch wid { 73 case 1: 74 s.P[off] = uint8(v) 75 case 2: 76 ctxt.Arch.ByteOrder.PutUint16(s.P[off:], uint16(v)) 77 case 4: 78 ctxt.Arch.ByteOrder.PutUint32(s.P[off:], uint32(v)) 79 case 8: 80 ctxt.Arch.ByteOrder.PutUint64(s.P[off:], uint64(v)) 81 } 82 83 return off + wid 84 } 85 86 func adduintxx(ctxt *Link, s *LSym, v uint64, wid int) int64 { 87 off := s.Size 88 setuintxx(ctxt, s, off, v, int64(wid)) 89 return off 90 } 91 92 func Adduint8(ctxt *Link, s *LSym, v uint8) int64 { 93 return adduintxx(ctxt, s, uint64(v), 1) 94 } 95 96 func Adduint16(ctxt *Link, s *LSym, v uint16) int64 { 97 return adduintxx(ctxt, s, uint64(v), 2) 98 } 99 100 func Adduint32(ctxt *Link, s *LSym, v uint32) int64 { 101 return adduintxx(ctxt, s, uint64(v), 4) 102 } 103 104 func Adduint64(ctxt *Link, s *LSym, v uint64) int64 { 105 return adduintxx(ctxt, s, v, 8) 106 } 107 108 func adduint(ctxt *Link, s *LSym, v uint64) int64 { 109 return adduintxx(ctxt, s, v, Thearch.Intsize) 110 } 111 112 func setuint8(ctxt *Link, s *LSym, r int64, v uint8) int64 { 113 return setuintxx(ctxt, s, r, uint64(v), 1) 114 } 115 116 func setuint32(ctxt *Link, s *LSym, r int64, v uint32) int64 { 117 return setuintxx(ctxt, s, r, uint64(v), 4) 118 } 119 120 func Addaddrplus(ctxt *Link, s *LSym, t *LSym, add int64) int64 { 121 if s.Type == 0 { 122 s.Type = obj.SDATA 123 } 124 s.Reachable = true 125 i := s.Size 126 s.Size += int64(ctxt.Arch.Ptrsize) 127 Symgrow(ctxt, s, s.Size) 128 r := Addrel(s) 129 r.Sym = t 130 r.Off = int32(i) 131 r.Siz = uint8(ctxt.Arch.Ptrsize) 132 r.Type = obj.R_ADDR 133 r.Add = add 134 return i + int64(r.Siz) 135 } 136 137 func Addpcrelplus(ctxt *Link, s *LSym, t *LSym, add int64) int64 { 138 if s.Type == 0 { 139 s.Type = obj.SDATA 140 } 141 s.Reachable = true 142 i := s.Size 143 s.Size += 4 144 Symgrow(ctxt, s, s.Size) 145 r := Addrel(s) 146 r.Sym = t 147 r.Off = int32(i) 148 r.Add = add 149 r.Type = obj.R_PCREL 150 r.Siz = 4 151 return i + int64(r.Siz) 152 } 153 154 func Addaddr(ctxt *Link, s *LSym, t *LSym) int64 { 155 return Addaddrplus(ctxt, s, t, 0) 156 } 157 158 func setaddrplus(ctxt *Link, s *LSym, off int64, t *LSym, add int64) int64 { 159 if s.Type == 0 { 160 s.Type = obj.SDATA 161 } 162 s.Reachable = true 163 if off+int64(ctxt.Arch.Ptrsize) > s.Size { 164 s.Size = off + int64(ctxt.Arch.Ptrsize) 165 Symgrow(ctxt, s, s.Size) 166 } 167 168 r := Addrel(s) 169 r.Sym = t 170 r.Off = int32(off) 171 r.Siz = uint8(ctxt.Arch.Ptrsize) 172 r.Type = obj.R_ADDR 173 r.Add = add 174 return off + int64(r.Siz) 175 } 176 177 func setaddr(ctxt *Link, s *LSym, off int64, t *LSym) int64 { 178 return setaddrplus(ctxt, s, off, t, 0) 179 } 180 181 func addsize(ctxt *Link, s *LSym, t *LSym) int64 { 182 if s.Type == 0 { 183 s.Type = obj.SDATA 184 } 185 s.Reachable = true 186 i := s.Size 187 s.Size += int64(ctxt.Arch.Ptrsize) 188 Symgrow(ctxt, s, s.Size) 189 r := Addrel(s) 190 r.Sym = t 191 r.Off = int32(i) 192 r.Siz = uint8(ctxt.Arch.Ptrsize) 193 r.Type = obj.R_SIZE 194 return i + int64(r.Siz) 195 } 196 197 func addaddrplus4(ctxt *Link, s *LSym, t *LSym, add int64) int64 { 198 if s.Type == 0 { 199 s.Type = obj.SDATA 200 } 201 s.Reachable = true 202 i := s.Size 203 s.Size += 4 204 Symgrow(ctxt, s, s.Size) 205 r := Addrel(s) 206 r.Sym = t 207 r.Off = int32(i) 208 r.Siz = 4 209 r.Type = obj.R_ADDR 210 r.Add = add 211 return i + int64(r.Siz) 212 } 213 214 /* 215 * divide-and-conquer list-link 216 * sort of LSym* structures. 217 * Used for the data block. 218 */ 219 func datcmp(s1 *LSym, s2 *LSym) int { 220 if s1.Type != s2.Type { 221 return int(s1.Type) - int(s2.Type) 222 } 223 224 // For ppc64, we want to interleave the .got and .toc sections 225 // from input files. Both are type SELFGOT, so in that case 226 // fall through to the name comparison (conveniently, .got 227 // sorts before .toc). 228 if s1.Type != obj.SELFGOT && s1.Size != s2.Size { 229 if s1.Size < s2.Size { 230 return -1 231 } 232 return +1 233 } 234 235 return stringsCompare(s1.Name, s2.Name) 236 } 237 238 func listnextp(s *LSym) **LSym { 239 return &s.Next 240 } 241 242 func listsubp(s *LSym) **LSym { 243 return &s.Sub 244 } 245 246 func listsort(l *LSym, cmp func(*LSym, *LSym) int, nextp func(*LSym) **LSym) *LSym { 247 if l == nil || *nextp(l) == nil { 248 return l 249 } 250 251 l1 := l 252 l2 := l 253 for { 254 l2 = *nextp(l2) 255 if l2 == nil { 256 break 257 } 258 l2 = *nextp(l2) 259 if l2 == nil { 260 break 261 } 262 l1 = *nextp(l1) 263 } 264 265 l2 = *nextp(l1) 266 *nextp(l1) = nil 267 l1 = listsort(l, cmp, nextp) 268 l2 = listsort(l2, cmp, nextp) 269 270 /* set up lead element */ 271 if cmp(l1, l2) < 0 { 272 l = l1 273 l1 = *nextp(l1) 274 } else { 275 l = l2 276 l2 = *nextp(l2) 277 } 278 279 le := l 280 281 for { 282 if l1 == nil { 283 for l2 != nil { 284 *nextp(le) = l2 285 le = l2 286 l2 = *nextp(l2) 287 } 288 289 *nextp(le) = nil 290 break 291 } 292 293 if l2 == nil { 294 for l1 != nil { 295 *nextp(le) = l1 296 le = l1 297 l1 = *nextp(l1) 298 } 299 300 break 301 } 302 303 if cmp(l1, l2) < 0 { 304 *nextp(le) = l1 305 le = l1 306 l1 = *nextp(l1) 307 } else { 308 *nextp(le) = l2 309 le = l2 310 l2 = *nextp(l2) 311 } 312 } 313 314 *nextp(le) = nil 315 return l 316 } 317 318 func relocsym(s *LSym) { 319 var r *Reloc 320 var rs *LSym 321 var i16 int16 322 var off int32 323 var siz int32 324 var fl int32 325 var o int64 326 327 Ctxt.Cursym = s 328 for ri := int32(0); ri < int32(len(s.R)); ri++ { 329 r = &s.R[ri] 330 r.Done = 1 331 off = r.Off 332 siz = int32(r.Siz) 333 if off < 0 || off+siz > int32(len(s.P)) { 334 Diag("%s: invalid relocation %d+%d not in [%d,%d)", s.Name, off, siz, 0, len(s.P)) 335 continue 336 } 337 338 if r.Sym != nil && (r.Sym.Type&(obj.SMASK|obj.SHIDDEN) == 0 || r.Sym.Type&obj.SMASK == obj.SXREF) { 339 // When putting the runtime but not main into a shared library 340 // these symbols are undefined and that's OK. 341 if Buildmode == BuildmodeShared && (r.Sym.Name == "main.main" || r.Sym.Name == "main.init") { 342 r.Sym.Type = obj.SDYNIMPORT 343 } else { 344 Diag("%s: not defined", r.Sym.Name) 345 continue 346 } 347 } 348 349 if r.Type >= 256 { 350 continue 351 } 352 if r.Siz == 0 { // informational relocation - no work to do 353 continue 354 } 355 356 // We need to be able to reference dynimport symbols when linking against 357 // shared libraries, and Solaris needs it always 358 if HEADTYPE != obj.Hsolaris && r.Sym != nil && r.Sym.Type == obj.SDYNIMPORT && !DynlinkingGo() { 359 Diag("unhandled relocation for %s (type %d rtype %d)", r.Sym.Name, r.Sym.Type, r.Type) 360 } 361 if r.Sym != nil && r.Sym.Type != obj.STLSBSS && !r.Sym.Reachable { 362 Diag("unreachable sym in relocation: %s %s", s.Name, r.Sym.Name) 363 } 364 365 switch r.Type { 366 default: 367 switch siz { 368 default: 369 Diag("bad reloc size %#x for %s", uint32(siz), r.Sym.Name) 370 case 1: 371 o = int64(s.P[off]) 372 case 2: 373 o = int64(Ctxt.Arch.ByteOrder.Uint16(s.P[off:])) 374 case 4: 375 o = int64(Ctxt.Arch.ByteOrder.Uint32(s.P[off:])) 376 case 8: 377 o = int64(Ctxt.Arch.ByteOrder.Uint64(s.P[off:])) 378 } 379 if Thearch.Archreloc(r, s, &o) < 0 { 380 Diag("unknown reloc %d", r.Type) 381 } 382 383 case obj.R_TLS_LE: 384 if Linkmode == LinkExternal && Iself && HEADTYPE != obj.Hopenbsd { 385 r.Done = 0 386 if r.Sym == nil { 387 r.Sym = Ctxt.Tlsg 388 } 389 r.Xsym = r.Sym 390 r.Xadd = r.Add 391 o = 0 392 if Thearch.Thechar != '6' { 393 o = r.Add 394 } 395 break 396 } 397 398 if Iself && Thearch.Thechar == '5' { 399 // On ELF ARM, the thread pointer is 8 bytes before 400 // the start of the thread-local data block, so add 8 401 // to the actual TLS offset (r->sym->value). 402 // This 8 seems to be a fundamental constant of 403 // ELF on ARM (or maybe Glibc on ARM); it is not 404 // related to the fact that our own TLS storage happens 405 // to take up 8 bytes. 406 o = 8 + r.Sym.Value 407 } else if Iself || Ctxt.Headtype == obj.Hplan9 || Ctxt.Headtype == obj.Hdarwin { 408 o = int64(Ctxt.Tlsoffset) + r.Add 409 } else if Ctxt.Headtype == obj.Hwindows { 410 o = r.Add 411 } else { 412 log.Fatalf("unexpected R_TLS_LE relocation for %s", Headstr(Ctxt.Headtype)) 413 } 414 415 case obj.R_TLS_IE: 416 if Linkmode == LinkExternal && Iself && HEADTYPE != obj.Hopenbsd { 417 r.Done = 0 418 if r.Sym == nil { 419 r.Sym = Ctxt.Tlsg 420 } 421 r.Xsym = r.Sym 422 r.Xadd = r.Add 423 o = 0 424 if Thearch.Thechar != '6' { 425 o = r.Add 426 } 427 break 428 } 429 log.Fatalf("cannot handle R_TLS_IE when linking internally") 430 431 case obj.R_ADDR: 432 if Linkmode == LinkExternal && r.Sym.Type != obj.SCONST { 433 r.Done = 0 434 435 // set up addend for eventual relocation via outer symbol. 436 rs = r.Sym 437 438 r.Xadd = r.Add 439 for rs.Outer != nil { 440 r.Xadd += Symaddr(rs) - Symaddr(rs.Outer) 441 rs = rs.Outer 442 } 443 444 if rs.Type != obj.SHOSTOBJ && rs.Type != obj.SDYNIMPORT && rs.Sect == nil { 445 Diag("missing section for %s", rs.Name) 446 } 447 r.Xsym = rs 448 449 o = r.Xadd 450 if Iself { 451 if Thearch.Thechar == '6' { 452 o = 0 453 } 454 } else if HEADTYPE == obj.Hdarwin { 455 // ld64 for arm64 has a bug where if the address pointed to by o exists in the 456 // symbol table (dynid >= 0), or is inside a symbol that exists in the symbol 457 // table, then it will add o twice into the relocated value. 458 // The workaround is that on arm64 don't ever add symaddr to o and always use 459 // extern relocation by requiring rs->dynid >= 0. 460 if rs.Type != obj.SHOSTOBJ { 461 if Thearch.Thechar == '7' && rs.Dynid < 0 { 462 Diag("R_ADDR reloc to %s+%d is not supported on darwin/arm64", rs.Name, o) 463 } 464 if Thearch.Thechar != '7' { 465 o += Symaddr(rs) 466 } 467 } 468 } else if HEADTYPE == obj.Hwindows { 469 // nothing to do 470 } else { 471 Diag("unhandled pcrel relocation for %s", headstring) 472 } 473 474 break 475 } 476 477 o = Symaddr(r.Sym) + r.Add 478 479 // On amd64, 4-byte offsets will be sign-extended, so it is impossible to 480 // access more than 2GB of static data; fail at link time is better than 481 // fail at runtime. See https://golang.org/issue/7980. 482 // Instead of special casing only amd64, we treat this as an error on all 483 // 64-bit architectures so as to be future-proof. 484 if int32(o) < 0 && Thearch.Ptrsize > 4 && siz == 4 { 485 Diag("non-pc-relative relocation address is too big: %#x (%#x + %#x)", uint64(o), Symaddr(r.Sym), r.Add) 486 errorexit() 487 } 488 489 // r->sym can be null when CALL $(constant) is transformed from absolute PC to relative PC call. 490 case obj.R_CALL, obj.R_GOTPCREL, obj.R_PCREL: 491 if Linkmode == LinkExternal && r.Sym != nil && r.Sym.Type != obj.SCONST && (r.Sym.Sect != Ctxt.Cursym.Sect || r.Type == obj.R_GOTPCREL) { 492 r.Done = 0 493 494 // set up addend for eventual relocation via outer symbol. 495 rs = r.Sym 496 497 r.Xadd = r.Add 498 for rs.Outer != nil { 499 r.Xadd += Symaddr(rs) - Symaddr(rs.Outer) 500 rs = rs.Outer 501 } 502 503 r.Xadd -= int64(r.Siz) // relative to address after the relocated chunk 504 if rs.Type != obj.SHOSTOBJ && rs.Type != obj.SDYNIMPORT && rs.Sect == nil { 505 Diag("missing section for %s", rs.Name) 506 } 507 r.Xsym = rs 508 509 o = r.Xadd 510 if Iself { 511 if Thearch.Thechar == '6' { 512 o = 0 513 } 514 } else if HEADTYPE == obj.Hdarwin { 515 if r.Type == obj.R_CALL { 516 if rs.Type != obj.SHOSTOBJ { 517 o += int64(uint64(Symaddr(rs)) - rs.Sect.Vaddr) 518 } 519 o -= int64(r.Off) // relative to section offset, not symbol 520 } else { 521 o += int64(r.Siz) 522 } 523 } else if HEADTYPE == obj.Hwindows && Thearch.Thechar == '6' { // only amd64 needs PCREL 524 // PE/COFF's PC32 relocation uses the address after the relocated 525 // bytes as the base. Compensate by skewing the addend. 526 o += int64(r.Siz) 527 // GNU ld always add VirtualAddress of the .text section to the 528 // relocated address, compensate that. 529 o -= int64(s.Sect.Vaddr - PEBASE) 530 } else { 531 Diag("unhandled pcrel relocation for %s", headstring) 532 } 533 534 break 535 } 536 537 o = 0 538 if r.Sym != nil { 539 o += Symaddr(r.Sym) 540 } 541 542 // NOTE: The (int32) cast on the next line works around a bug in Plan 9's 8c 543 // compiler. The expression s->value + r->off + r->siz is int32 + int32 + 544 // uchar, and Plan 9 8c incorrectly treats the expression as type uint32 545 // instead of int32, causing incorrect values when sign extended for adding 546 // to o. The bug only occurs on Plan 9, because this C program is compiled by 547 // the standard host compiler (gcc on most other systems). 548 o += r.Add - (s.Value + int64(r.Off) + int64(int32(r.Siz))) 549 550 case obj.R_SIZE: 551 o = r.Sym.Size + r.Add 552 } 553 554 if r.Variant != RV_NONE { 555 o = Thearch.Archrelocvariant(r, s, o) 556 } 557 558 if false { 559 nam := "<nil>" 560 if r.Sym != nil { 561 nam = r.Sym.Name 562 } 563 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) 564 } 565 switch siz { 566 default: 567 Ctxt.Cursym = s 568 Diag("bad reloc size %#x for %s", uint32(siz), r.Sym.Name) 569 fallthrough 570 571 // TODO(rsc): Remove. 572 case 1: 573 s.P[off] = byte(int8(o)) 574 575 case 2: 576 if o != int64(int16(o)) { 577 Diag("relocation address is too big: %#x", o) 578 } 579 i16 = int16(o) 580 Ctxt.Arch.ByteOrder.PutUint16(s.P[off:], uint16(i16)) 581 582 case 4: 583 if r.Type == obj.R_PCREL || r.Type == obj.R_CALL { 584 if o != int64(int32(o)) { 585 Diag("pc-relative relocation address is too big: %#x", o) 586 } 587 } else { 588 if o != int64(int32(o)) && o != int64(uint32(o)) { 589 Diag("non-pc-relative relocation address is too big: %#x", uint64(o)) 590 } 591 } 592 593 fl = int32(o) 594 Ctxt.Arch.ByteOrder.PutUint32(s.P[off:], uint32(fl)) 595 596 case 8: 597 Ctxt.Arch.ByteOrder.PutUint64(s.P[off:], uint64(o)) 598 } 599 } 600 } 601 602 func reloc() { 603 if Debug['v'] != 0 { 604 fmt.Fprintf(&Bso, "%5.2f reloc\n", obj.Cputime()) 605 } 606 Bso.Flush() 607 608 for s := Ctxt.Textp; s != nil; s = s.Next { 609 relocsym(s) 610 } 611 for s := datap; s != nil; s = s.Next { 612 relocsym(s) 613 } 614 } 615 616 func dynrelocsym(s *LSym) { 617 if HEADTYPE == obj.Hwindows && Linkmode != LinkExternal { 618 rel := Linklookup(Ctxt, ".rel", 0) 619 if s == rel { 620 return 621 } 622 var r *Reloc 623 var targ *LSym 624 for ri := 0; ri < len(s.R); ri++ { 625 r = &s.R[ri] 626 targ = r.Sym 627 if targ == nil { 628 continue 629 } 630 if !targ.Reachable { 631 Diag("internal inconsistency: dynamic symbol %s is not reachable.", targ.Name) 632 } 633 if r.Sym.Plt == -2 && r.Sym.Got != -2 { // make dynimport JMP table for PE object files. 634 targ.Plt = int32(rel.Size) 635 r.Sym = rel 636 r.Add = int64(targ.Plt) 637 638 // jmp *addr 639 if Thearch.Thechar == '8' { 640 Adduint8(Ctxt, rel, 0xff) 641 Adduint8(Ctxt, rel, 0x25) 642 Addaddr(Ctxt, rel, targ) 643 Adduint8(Ctxt, rel, 0x90) 644 Adduint8(Ctxt, rel, 0x90) 645 } else { 646 Adduint8(Ctxt, rel, 0xff) 647 Adduint8(Ctxt, rel, 0x24) 648 Adduint8(Ctxt, rel, 0x25) 649 addaddrplus4(Ctxt, rel, targ, 0) 650 Adduint8(Ctxt, rel, 0x90) 651 } 652 } else if r.Sym.Plt >= 0 { 653 r.Sym = rel 654 r.Add = int64(targ.Plt) 655 } 656 } 657 658 return 659 } 660 661 var r *Reloc 662 for ri := 0; ri < len(s.R); ri++ { 663 r = &s.R[ri] 664 if r.Sym != nil && r.Sym.Type == obj.SDYNIMPORT || r.Type >= 256 { 665 if r.Sym != nil && !r.Sym.Reachable { 666 Diag("internal inconsistency: dynamic symbol %s is not reachable.", r.Sym.Name) 667 } 668 Thearch.Adddynrel(s, r) 669 } 670 } 671 } 672 673 func dynreloc() { 674 // -d suppresses dynamic loader format, so we may as well not 675 // compute these sections or mark their symbols as reachable. 676 if Debug['d'] != 0 && HEADTYPE != obj.Hwindows { 677 return 678 } 679 if Debug['v'] != 0 { 680 fmt.Fprintf(&Bso, "%5.2f reloc\n", obj.Cputime()) 681 } 682 Bso.Flush() 683 684 for s := Ctxt.Textp; s != nil; s = s.Next { 685 dynrelocsym(s) 686 } 687 for s := datap; s != nil; s = s.Next { 688 dynrelocsym(s) 689 } 690 if Iself { 691 elfdynhash() 692 } 693 } 694 695 func blk(start *LSym, addr int64, size int64) { 696 var sym *LSym 697 698 for sym = start; sym != nil; sym = sym.Next { 699 if sym.Type&obj.SSUB == 0 && sym.Value >= addr { 700 break 701 } 702 } 703 704 eaddr := addr + size 705 var ep []byte 706 var p []byte 707 for ; sym != nil; sym = sym.Next { 708 if sym.Type&obj.SSUB != 0 { 709 continue 710 } 711 if sym.Value >= eaddr { 712 break 713 } 714 Ctxt.Cursym = sym 715 if sym.Value < addr { 716 Diag("phase error: addr=%#x but sym=%#x type=%d", int64(addr), int64(sym.Value), sym.Type) 717 errorexit() 718 } 719 720 for ; addr < sym.Value; addr++ { 721 Cput(0) 722 } 723 p = sym.P 724 ep = p[len(sym.P):] 725 for -cap(p) < -cap(ep) { 726 Cput(uint8(p[0])) 727 p = p[1:] 728 } 729 addr += int64(len(sym.P)) 730 for ; addr < sym.Value+sym.Size; addr++ { 731 Cput(0) 732 } 733 if addr != sym.Value+sym.Size { 734 Diag("phase error: addr=%#x value+size=%#x", int64(addr), int64(sym.Value)+sym.Size) 735 errorexit() 736 } 737 738 if sym.Value+sym.Size >= eaddr { 739 break 740 } 741 } 742 743 for ; addr < eaddr; addr++ { 744 Cput(0) 745 } 746 Cflush() 747 } 748 749 func Codeblk(addr int64, size int64) { 750 if Debug['a'] != 0 { 751 fmt.Fprintf(&Bso, "codeblk [%#x,%#x) at offset %#x\n", addr, addr+size, Cpos()) 752 } 753 754 blk(Ctxt.Textp, addr, size) 755 756 /* again for printing */ 757 if Debug['a'] == 0 { 758 return 759 } 760 761 var sym *LSym 762 for sym = Ctxt.Textp; sym != nil; sym = sym.Next { 763 if !sym.Reachable { 764 continue 765 } 766 if sym.Value >= addr { 767 break 768 } 769 } 770 771 eaddr := addr + size 772 var q []byte 773 for ; sym != nil; sym = sym.Next { 774 if !sym.Reachable { 775 continue 776 } 777 if sym.Value >= eaddr { 778 break 779 } 780 781 if addr < sym.Value { 782 fmt.Fprintf(&Bso, "%-20s %.8x|", "_", uint64(int64(addr))) 783 for ; addr < sym.Value; addr++ { 784 fmt.Fprintf(&Bso, " %.2x", 0) 785 } 786 fmt.Fprintf(&Bso, "\n") 787 } 788 789 fmt.Fprintf(&Bso, "%.6x\t%-20s\n", uint64(int64(addr)), sym.Name) 790 q = sym.P 791 792 for len(q) >= 16 { 793 fmt.Fprintf(&Bso, "%.6x\t% x\n", uint64(addr), q[:16]) 794 addr += 16 795 q = q[16:] 796 } 797 798 if len(q) > 0 { 799 fmt.Fprintf(&Bso, "%.6x\t% x\n", uint64(addr), q) 800 addr += int64(len(q)) 801 } 802 } 803 804 if addr < eaddr { 805 fmt.Fprintf(&Bso, "%-20s %.8x|", "_", uint64(int64(addr))) 806 for ; addr < eaddr; addr++ { 807 fmt.Fprintf(&Bso, " %.2x", 0) 808 } 809 } 810 811 Bso.Flush() 812 } 813 814 func Datblk(addr int64, size int64) { 815 if Debug['a'] != 0 { 816 fmt.Fprintf(&Bso, "datblk [%#x,%#x) at offset %#x\n", addr, addr+size, Cpos()) 817 } 818 819 blk(datap, addr, size) 820 821 /* again for printing */ 822 if Debug['a'] == 0 { 823 return 824 } 825 826 var sym *LSym 827 for sym = datap; sym != nil; sym = sym.Next { 828 if sym.Value >= addr { 829 break 830 } 831 } 832 833 eaddr := addr + size 834 var ep []byte 835 var i int64 836 var p []byte 837 var r *Reloc 838 var rsname string 839 var typ string 840 for ; sym != nil; sym = sym.Next { 841 if sym.Value >= eaddr { 842 break 843 } 844 if addr < sym.Value { 845 fmt.Fprintf(&Bso, "\t%.8x| 00 ...\n", uint64(addr)) 846 addr = sym.Value 847 } 848 849 fmt.Fprintf(&Bso, "%s\n\t%.8x|", sym.Name, uint(addr)) 850 p = sym.P 851 ep = p[len(sym.P):] 852 for -cap(p) < -cap(ep) { 853 if -cap(p) > -cap(sym.P) && int(-cap(p)+cap(sym.P))%16 == 0 { 854 fmt.Fprintf(&Bso, "\n\t%.8x|", uint(addr+int64(-cap(p)+cap(sym.P)))) 855 } 856 fmt.Fprintf(&Bso, " %.2x", p[0]) 857 p = p[1:] 858 } 859 860 addr += int64(len(sym.P)) 861 for ; addr < sym.Value+sym.Size; addr++ { 862 fmt.Fprintf(&Bso, " %.2x", 0) 863 } 864 fmt.Fprintf(&Bso, "\n") 865 866 if Linkmode == LinkExternal { 867 for i = 0; i < int64(len(sym.R)); i++ { 868 r = &sym.R[i] 869 rsname = "" 870 if r.Sym != nil { 871 rsname = r.Sym.Name 872 } 873 typ = "?" 874 switch r.Type { 875 case obj.R_ADDR: 876 typ = "addr" 877 878 case obj.R_PCREL: 879 typ = "pcrel" 880 881 case obj.R_CALL: 882 typ = "call" 883 } 884 885 fmt.Fprintf(&Bso, "\treloc %.8x/%d %s %s+%#x [%#x]\n", uint(sym.Value+int64(r.Off)), r.Siz, typ, rsname, int64(r.Add), int64(r.Sym.Value+r.Add)) 886 } 887 } 888 } 889 890 if addr < eaddr { 891 fmt.Fprintf(&Bso, "\t%.8x| 00 ...\n", uint(addr)) 892 } 893 fmt.Fprintf(&Bso, "\t%.8x|\n", uint(eaddr)) 894 } 895 896 func strnput(s string, n int) { 897 for ; n > 0 && s != ""; s = s[1:] { 898 Cput(uint8(s[0])) 899 n-- 900 } 901 902 for n > 0 { 903 Cput(0) 904 n-- 905 } 906 } 907 908 var strdata []*LSym 909 910 func addstrdata1(arg string) { 911 i := strings.Index(arg, "=") 912 if i < 0 { 913 Exitf("-X flag requires argument of the form importpath.name=value") 914 } 915 addstrdata(arg[:i], arg[i+1:]) 916 } 917 918 func addstrdata(name string, value string) { 919 p := fmt.Sprintf("%s.str", name) 920 sp := Linklookup(Ctxt, p, 0) 921 922 Addstring(sp, value) 923 sp.Type = obj.SRODATA 924 925 s := Linklookup(Ctxt, name, 0) 926 s.Size = 0 927 s.Dupok = 1 928 reachable := s.Reachable 929 Addaddr(Ctxt, s, sp) 930 adduintxx(Ctxt, s, uint64(len(value)), Thearch.Ptrsize) 931 932 // addstring, addaddr, etc., mark the symbols as reachable. 933 // In this case that is not necessarily true, so stick to what 934 // we know before entering this function. 935 s.Reachable = reachable 936 937 strdata = append(strdata, s) 938 939 sp.Reachable = reachable 940 } 941 942 func checkstrdata() { 943 for _, s := range strdata { 944 if s.Type == obj.STEXT { 945 Diag("cannot use -X with text symbol %s", s.Name) 946 } else if s.Gotype != nil && s.Gotype.Name != "type.string" { 947 Diag("cannot use -X with non-string symbol %s", s.Name) 948 } 949 } 950 } 951 952 func Addstring(s *LSym, str string) int64 { 953 if s.Type == 0 { 954 s.Type = obj.SNOPTRDATA 955 } 956 s.Reachable = true 957 r := int32(s.Size) 958 n := len(str) + 1 959 if s.Name == ".shstrtab" { 960 elfsetstring(str, int(r)) 961 } 962 Symgrow(Ctxt, s, int64(r)+int64(n)) 963 copy(s.P[r:], str) 964 s.P[int(r)+len(str)] = 0 965 s.Size += int64(n) 966 return int64(r) 967 } 968 969 // addgostring adds str, as a Go string value, to s. symname is the name of the 970 // symbol used to define the string data and must be unique per linked object. 971 func addgostring(s *LSym, symname, str string) { 972 sym := Linklookup(Ctxt, symname, 0) 973 if sym.Type != obj.Sxxx { 974 Diag("duplicate symname in addgostring: %s", symname) 975 } 976 sym.Reachable = true 977 sym.Local = true 978 sym.Type = obj.SRODATA 979 sym.Size = int64(len(str)) 980 sym.P = []byte(str) 981 Addaddr(Ctxt, s, sym) 982 adduint(Ctxt, s, uint64(len(str))) 983 } 984 985 func addinitarrdata(s *LSym) { 986 p := s.Name + ".ptr" 987 sp := Linklookup(Ctxt, p, 0) 988 sp.Type = obj.SINITARR 989 sp.Size = 0 990 sp.Dupok = 1 991 Addaddr(Ctxt, sp, s) 992 } 993 994 func dosymtype() { 995 for s := Ctxt.Allsym; s != nil; s = s.Allsym { 996 if len(s.P) > 0 { 997 if s.Type == obj.SBSS { 998 s.Type = obj.SDATA 999 } 1000 if s.Type == obj.SNOPTRBSS { 1001 s.Type = obj.SNOPTRDATA 1002 } 1003 } 1004 // Create a new entry in the .init_array section that points to the 1005 // library initializer function. 1006 switch Buildmode { 1007 case BuildmodeCArchive, BuildmodeCShared: 1008 if s.Name == INITENTRY { 1009 addinitarrdata(s) 1010 } 1011 } 1012 } 1013 } 1014 1015 func symalign(s *LSym) int32 { 1016 if s.Align != 0 { 1017 return s.Align 1018 } 1019 1020 align := int32(Thearch.Maxalign) 1021 for int64(align) > s.Size && align > 1 { 1022 align >>= 1 1023 } 1024 if align < s.Align { 1025 align = s.Align 1026 } 1027 return align 1028 } 1029 1030 func aligndatsize(datsize int64, s *LSym) int64 { 1031 return Rnd(datsize, int64(symalign(s))) 1032 } 1033 1034 // maxalign returns the maximum required alignment for 1035 // the list of symbols s; the list stops when s->type exceeds type. 1036 func maxalign(s *LSym, type_ int) int32 { 1037 var align int32 1038 1039 max := int32(0) 1040 for ; s != nil && int(s.Type) <= type_; s = s.Next { 1041 align = symalign(s) 1042 if max < align { 1043 max = align 1044 } 1045 } 1046 1047 return max 1048 } 1049 1050 const debugGCProg = false 1051 1052 type GCProg struct { 1053 sym *LSym 1054 w gcprog.Writer 1055 } 1056 1057 func (p *GCProg) Init(name string) { 1058 p.sym = Linklookup(Ctxt, name, 0) 1059 p.w.Init(p.writeByte) 1060 if debugGCProg { 1061 fmt.Fprintf(os.Stderr, "ld: start GCProg %s\n", name) 1062 p.w.Debug(os.Stderr) 1063 } 1064 } 1065 1066 func (p *GCProg) writeByte(x byte) { 1067 Adduint8(Ctxt, p.sym, x) 1068 } 1069 1070 func (p *GCProg) End(size int64) { 1071 p.w.ZeroUntil(size / int64(Thearch.Ptrsize)) 1072 p.w.End() 1073 if debugGCProg { 1074 fmt.Fprintf(os.Stderr, "ld: end GCProg\n") 1075 } 1076 } 1077 1078 func (p *GCProg) AddSym(s *LSym) { 1079 typ := s.Gotype 1080 // Things without pointers should be in SNOPTRDATA or SNOPTRBSS; 1081 // everything we see should have pointers and should therefore have a type. 1082 if typ == nil { 1083 Diag("missing Go type information for global symbol: %s size %d", s.Name, int(s.Size)) 1084 return 1085 } 1086 1087 ptrsize := int64(Thearch.Ptrsize) 1088 nptr := decodetype_ptrdata(typ) / ptrsize 1089 1090 if debugGCProg { 1091 fmt.Fprintf(os.Stderr, "gcprog sym: %s at %d (ptr=%d+%d)\n", s.Name, s.Value, s.Value/ptrsize, nptr) 1092 } 1093 1094 if decodetype_usegcprog(typ) == 0 { 1095 // Copy pointers from mask into program. 1096 mask := decodetype_gcmask(typ) 1097 for i := int64(0); i < nptr; i++ { 1098 if (mask[i/8]>>uint(i%8))&1 != 0 { 1099 p.w.Ptr(s.Value/ptrsize + i) 1100 } 1101 } 1102 return 1103 } 1104 1105 // Copy program. 1106 prog := decodetype_gcprog(typ) 1107 p.w.ZeroUntil(s.Value / ptrsize) 1108 p.w.Append(prog[4:], nptr) 1109 } 1110 1111 func growdatsize(datsizep *int64, s *LSym) { 1112 datsize := *datsizep 1113 const cutoff int64 = 2e9 // 2 GB (or so; looks better in errors than 2^31) 1114 switch { 1115 case s.Size < 0: 1116 Diag("%s: negative size (%d bytes)", s.Name, s.Size) 1117 case s.Size > cutoff: 1118 Diag("%s: symbol too large (%d bytes)", s.Name, s.Size) 1119 case datsize <= cutoff && datsize+s.Size > cutoff: 1120 Diag("%s: too much data (over %d bytes)", s.Name, cutoff) 1121 } 1122 *datsizep = datsize + s.Size 1123 } 1124 1125 func dodata() { 1126 if Debug['v'] != 0 { 1127 fmt.Fprintf(&Bso, "%5.2f dodata\n", obj.Cputime()) 1128 } 1129 Bso.Flush() 1130 1131 var last *LSym 1132 datap = nil 1133 1134 for s := Ctxt.Allsym; s != nil; s = s.Allsym { 1135 if !s.Reachable || s.Special != 0 { 1136 continue 1137 } 1138 if obj.STEXT < s.Type && s.Type < obj.SXREF { 1139 if s.Onlist != 0 { 1140 log.Fatalf("symbol %s listed multiple times", s.Name) 1141 } 1142 s.Onlist = 1 1143 if last == nil { 1144 datap = s 1145 } else { 1146 last.Next = s 1147 } 1148 s.Next = nil 1149 last = s 1150 } 1151 } 1152 1153 for s := datap; s != nil; s = s.Next { 1154 if int64(len(s.P)) > s.Size { 1155 Diag("%s: initialize bounds (%d < %d)", s.Name, int64(s.Size), len(s.P)) 1156 } 1157 } 1158 1159 /* 1160 * now that we have the datap list, but before we start 1161 * to assign addresses, record all the necessary 1162 * dynamic relocations. these will grow the relocation 1163 * symbol, which is itself data. 1164 * 1165 * on darwin, we need the symbol table numbers for dynreloc. 1166 */ 1167 if HEADTYPE == obj.Hdarwin { 1168 machosymorder() 1169 } 1170 dynreloc() 1171 1172 /* some symbols may no longer belong in datap (Mach-O) */ 1173 var l **LSym 1174 var s *LSym 1175 for l = &datap; ; { 1176 s = *l 1177 if s == nil { 1178 break 1179 } 1180 1181 if s.Type <= obj.STEXT || obj.SXREF <= s.Type { 1182 *l = s.Next 1183 } else { 1184 l = &s.Next 1185 } 1186 } 1187 1188 *l = nil 1189 1190 if UseRelro() { 1191 // "read only" data with relocations needs to go in its own section 1192 // when building a shared library. We do this by boosting objects of 1193 // type SXXX with relocations to type SXXXRELRO. 1194 for s := datap; s != nil; s = s.Next { 1195 if (s.Type >= obj.STYPE && s.Type <= obj.SFUNCTAB && len(s.R) > 0) || s.Type == obj.SGOSTRING { 1196 s.Type += (obj.STYPERELRO - obj.STYPE) 1197 if s.Outer != nil { 1198 s.Outer.Type = s.Type 1199 } 1200 } 1201 } 1202 // Check that we haven't made two symbols with the same .Outer into 1203 // different types (because references two symbols with non-nil Outer 1204 // become references to the outer symbol + offset it's vital that the 1205 // symbol and the outer end up in the same section). 1206 for s := datap; s != nil; s = s.Next { 1207 if s.Outer != nil && s.Outer.Type != s.Type { 1208 Diag("inconsistent types for %s and its Outer %s (%d != %d)", 1209 s.Name, s.Outer.Name, s.Type, s.Outer.Type) 1210 } 1211 } 1212 1213 } 1214 1215 datap = listsort(datap, datcmp, listnextp) 1216 1217 if Iself { 1218 // Make .rela and .rela.plt contiguous, the ELF ABI requires this 1219 // and Solaris actually cares. 1220 var relplt *LSym 1221 for l = &datap; *l != nil; l = &(*l).Next { 1222 if (*l).Name == ".rel.plt" || (*l).Name == ".rela.plt" { 1223 relplt = (*l) 1224 *l = (*l).Next 1225 break 1226 } 1227 } 1228 if relplt != nil { 1229 for s = datap; s != nil; s = s.Next { 1230 if s.Name == ".rel" || s.Name == ".rela" { 1231 relplt.Next = s.Next 1232 s.Next = relplt 1233 } 1234 } 1235 } 1236 } 1237 1238 /* 1239 * allocate sections. list is sorted by type, 1240 * so we can just walk it for each piece we want to emit. 1241 * segdata is processed before segtext, because we need 1242 * to see all symbols in the .data and .bss sections in order 1243 * to generate garbage collection information. 1244 */ 1245 1246 /* begin segdata */ 1247 1248 /* skip symbols belonging to segtext */ 1249 s = datap 1250 1251 for ; s != nil && s.Type < obj.SELFSECT; s = s.Next { 1252 } 1253 1254 /* writable ELF sections */ 1255 datsize := int64(0) 1256 1257 var sect *Section 1258 for ; s != nil && s.Type < obj.SELFGOT; s = s.Next { 1259 sect = addsection(&Segdata, s.Name, 06) 1260 sect.Align = symalign(s) 1261 datsize = Rnd(datsize, int64(sect.Align)) 1262 sect.Vaddr = uint64(datsize) 1263 s.Sect = sect 1264 s.Type = obj.SDATA 1265 s.Value = int64(uint64(datsize) - sect.Vaddr) 1266 growdatsize(&datsize, s) 1267 sect.Length = uint64(datsize) - sect.Vaddr 1268 } 1269 1270 /* .got (and .toc on ppc64) */ 1271 if s.Type == obj.SELFGOT { 1272 sect := addsection(&Segdata, ".got", 06) 1273 sect.Align = maxalign(s, obj.SELFGOT) 1274 datsize = Rnd(datsize, int64(sect.Align)) 1275 sect.Vaddr = uint64(datsize) 1276 var toc *LSym 1277 for ; s != nil && s.Type == obj.SELFGOT; s = s.Next { 1278 datsize = aligndatsize(datsize, s) 1279 s.Sect = sect 1280 s.Type = obj.SDATA 1281 s.Value = int64(uint64(datsize) - sect.Vaddr) 1282 1283 // Resolve .TOC. symbol for this object file (ppc64) 1284 toc = Linkrlookup(Ctxt, ".TOC.", int(s.Version)) 1285 1286 if toc != nil { 1287 toc.Sect = sect 1288 toc.Outer = s 1289 toc.Sub = s.Sub 1290 s.Sub = toc 1291 1292 toc.Value = 0x8000 1293 } 1294 1295 growdatsize(&datsize, s) 1296 } 1297 1298 sect.Length = uint64(datsize) - sect.Vaddr 1299 } 1300 1301 /* pointer-free data */ 1302 sect = addsection(&Segdata, ".noptrdata", 06) 1303 1304 sect.Align = maxalign(s, obj.SINITARR-1) 1305 datsize = Rnd(datsize, int64(sect.Align)) 1306 sect.Vaddr = uint64(datsize) 1307 Linklookup(Ctxt, "runtime.noptrdata", 0).Sect = sect 1308 Linklookup(Ctxt, "runtime.enoptrdata", 0).Sect = sect 1309 for ; s != nil && s.Type < obj.SINITARR; s = s.Next { 1310 datsize = aligndatsize(datsize, s) 1311 s.Sect = sect 1312 s.Type = obj.SDATA 1313 s.Value = int64(uint64(datsize) - sect.Vaddr) 1314 growdatsize(&datsize, s) 1315 } 1316 1317 sect.Length = uint64(datsize) - sect.Vaddr 1318 1319 hasinitarr := Linkshared 1320 1321 /* shared library initializer */ 1322 switch Buildmode { 1323 case BuildmodeCArchive, BuildmodeCShared, BuildmodeShared: 1324 hasinitarr = true 1325 } 1326 1327 if hasinitarr { 1328 sect := addsection(&Segdata, ".init_array", 06) 1329 sect.Align = maxalign(s, obj.SINITARR) 1330 datsize = Rnd(datsize, int64(sect.Align)) 1331 sect.Vaddr = uint64(datsize) 1332 for ; s != nil && s.Type == obj.SINITARR; s = s.Next { 1333 datsize = aligndatsize(datsize, s) 1334 s.Sect = sect 1335 s.Value = int64(uint64(datsize) - sect.Vaddr) 1336 growdatsize(&datsize, s) 1337 } 1338 1339 sect.Length = uint64(datsize) - sect.Vaddr 1340 } 1341 1342 /* data */ 1343 sect = addsection(&Segdata, ".data", 06) 1344 sect.Align = maxalign(s, obj.SBSS-1) 1345 datsize = Rnd(datsize, int64(sect.Align)) 1346 sect.Vaddr = uint64(datsize) 1347 Linklookup(Ctxt, "runtime.data", 0).Sect = sect 1348 Linklookup(Ctxt, "runtime.edata", 0).Sect = sect 1349 var gc GCProg 1350 gc.Init("runtime.gcdata") 1351 for ; s != nil && s.Type < obj.SBSS; s = s.Next { 1352 if s.Type == obj.SINITARR { 1353 Ctxt.Cursym = s 1354 Diag("unexpected symbol type %d", s.Type) 1355 } 1356 1357 s.Sect = sect 1358 s.Type = obj.SDATA 1359 datsize = aligndatsize(datsize, s) 1360 s.Value = int64(uint64(datsize) - sect.Vaddr) 1361 gc.AddSym(s) 1362 growdatsize(&datsize, s) 1363 } 1364 sect.Length = uint64(datsize) - sect.Vaddr 1365 gc.End(int64(sect.Length)) 1366 1367 /* bss */ 1368 sect = addsection(&Segdata, ".bss", 06) 1369 sect.Align = maxalign(s, obj.SNOPTRBSS-1) 1370 datsize = Rnd(datsize, int64(sect.Align)) 1371 sect.Vaddr = uint64(datsize) 1372 Linklookup(Ctxt, "runtime.bss", 0).Sect = sect 1373 Linklookup(Ctxt, "runtime.ebss", 0).Sect = sect 1374 gc = GCProg{} 1375 gc.Init("runtime.gcbss") 1376 for ; s != nil && s.Type < obj.SNOPTRBSS; s = s.Next { 1377 s.Sect = sect 1378 datsize = aligndatsize(datsize, s) 1379 s.Value = int64(uint64(datsize) - sect.Vaddr) 1380 gc.AddSym(s) 1381 growdatsize(&datsize, s) 1382 } 1383 sect.Length = uint64(datsize) - sect.Vaddr 1384 gc.End(int64(sect.Length)) 1385 1386 /* pointer-free bss */ 1387 sect = addsection(&Segdata, ".noptrbss", 06) 1388 1389 sect.Align = maxalign(s, obj.SNOPTRBSS) 1390 datsize = Rnd(datsize, int64(sect.Align)) 1391 sect.Vaddr = uint64(datsize) 1392 Linklookup(Ctxt, "runtime.noptrbss", 0).Sect = sect 1393 Linklookup(Ctxt, "runtime.enoptrbss", 0).Sect = sect 1394 for ; s != nil && s.Type == obj.SNOPTRBSS; s = s.Next { 1395 datsize = aligndatsize(datsize, s) 1396 s.Sect = sect 1397 s.Value = int64(uint64(datsize) - sect.Vaddr) 1398 growdatsize(&datsize, s) 1399 } 1400 1401 sect.Length = uint64(datsize) - sect.Vaddr 1402 Linklookup(Ctxt, "runtime.end", 0).Sect = sect 1403 1404 // 6g uses 4-byte relocation offsets, so the entire segment must fit in 32 bits. 1405 if datsize != int64(uint32(datsize)) { 1406 Diag("data or bss segment too large") 1407 } 1408 1409 if s != nil && s.Type == obj.STLSBSS { 1410 if Iself && (Linkmode == LinkExternal || Debug['d'] == 0) && HEADTYPE != obj.Hopenbsd { 1411 sect = addsection(&Segdata, ".tbss", 06) 1412 sect.Align = int32(Thearch.Ptrsize) 1413 sect.Vaddr = 0 1414 } else { 1415 sect = nil 1416 } 1417 datsize = 0 1418 1419 for ; s != nil && s.Type == obj.STLSBSS; s = s.Next { 1420 datsize = aligndatsize(datsize, s) 1421 s.Sect = sect 1422 s.Value = datsize 1423 growdatsize(&datsize, s) 1424 } 1425 1426 if sect != nil { 1427 sect.Length = uint64(datsize) 1428 } 1429 } 1430 1431 if s != nil { 1432 Ctxt.Cursym = nil 1433 Diag("unexpected symbol type %d for %s", s.Type, s.Name) 1434 } 1435 1436 /* 1437 * We finished data, begin read-only data. 1438 * Not all systems support a separate read-only non-executable data section. 1439 * ELF systems do. 1440 * OS X and Plan 9 do not. 1441 * Windows PE may, but if so we have not implemented it. 1442 * And if we're using external linking mode, the point is moot, 1443 * since it's not our decision; that code expects the sections in 1444 * segtext. 1445 */ 1446 var segro *Segment 1447 if Iself && Linkmode == LinkInternal { 1448 segro = &Segrodata 1449 } else { 1450 segro = &Segtext 1451 } 1452 1453 s = datap 1454 1455 datsize = 0 1456 1457 /* read-only executable ELF, Mach-O sections */ 1458 for ; s != nil && s.Type < obj.STYPE; s = s.Next { 1459 sect = addsection(&Segtext, s.Name, 04) 1460 sect.Align = symalign(s) 1461 datsize = Rnd(datsize, int64(sect.Align)) 1462 sect.Vaddr = uint64(datsize) 1463 s.Sect = sect 1464 s.Type = obj.SRODATA 1465 s.Value = int64(uint64(datsize) - sect.Vaddr) 1466 growdatsize(&datsize, s) 1467 sect.Length = uint64(datsize) - sect.Vaddr 1468 } 1469 1470 /* read-only data */ 1471 sect = addsection(segro, ".rodata", 04) 1472 1473 sect.Align = maxalign(s, obj.STYPERELRO-1) 1474 datsize = Rnd(datsize, int64(sect.Align)) 1475 sect.Vaddr = 0 1476 Linklookup(Ctxt, "runtime.rodata", 0).Sect = sect 1477 Linklookup(Ctxt, "runtime.erodata", 0).Sect = sect 1478 for ; s != nil && s.Type < obj.STYPERELRO; s = s.Next { 1479 datsize = aligndatsize(datsize, s) 1480 s.Sect = sect 1481 s.Type = obj.SRODATA 1482 s.Value = int64(uint64(datsize) - sect.Vaddr) 1483 growdatsize(&datsize, s) 1484 } 1485 1486 sect.Length = uint64(datsize) - sect.Vaddr 1487 1488 // There is some data that are conceptually read-only but are written to by 1489 // relocations. On GNU systems, we can arrange for the dynamic linker to 1490 // mprotect sections after relocations are applied by giving them write 1491 // permissions in the object file and calling them ".data.rel.ro.FOO". We 1492 // divide the .rodata section between actual .rodata and .data.rel.ro.rodata, 1493 // but for the other sections that this applies to, we just write a read-only 1494 // .FOO section or a read-write .data.rel.ro.FOO section depending on the 1495 // situation. 1496 // TODO(mwhudson): It would make sense to do this more widely, but it makes 1497 // the system linker segfault on darwin. 1498 relro_perms := 04 1499 relro_prefix := "" 1500 1501 if UseRelro() { 1502 relro_perms = 06 1503 relro_prefix = ".data.rel.ro" 1504 /* data only written by relocations */ 1505 sect = addsection(segro, ".data.rel.ro", 06) 1506 1507 sect.Align = maxalign(s, obj.STYPELINK-1) 1508 datsize = Rnd(datsize, int64(sect.Align)) 1509 sect.Vaddr = 0 1510 for ; s != nil && s.Type < obj.STYPELINK; s = s.Next { 1511 datsize = aligndatsize(datsize, s) 1512 if s.Outer != nil && s.Outer.Sect != nil && s.Outer.Sect != sect { 1513 Diag("s.Outer (%s) in different section from s (%s)", s.Outer.Name, s.Name) 1514 } 1515 s.Sect = sect 1516 s.Type = obj.SRODATA 1517 s.Value = int64(uint64(datsize) - sect.Vaddr) 1518 growdatsize(&datsize, s) 1519 } 1520 1521 sect.Length = uint64(datsize) - sect.Vaddr 1522 1523 } 1524 1525 /* typelink */ 1526 sect = addsection(segro, relro_prefix+".typelink", relro_perms) 1527 1528 sect.Align = maxalign(s, obj.STYPELINK) 1529 datsize = Rnd(datsize, int64(sect.Align)) 1530 sect.Vaddr = uint64(datsize) 1531 Linklookup(Ctxt, "runtime.typelink", 0).Sect = sect 1532 Linklookup(Ctxt, "runtime.etypelink", 0).Sect = sect 1533 for ; s != nil && s.Type == obj.STYPELINK; s = s.Next { 1534 datsize = aligndatsize(datsize, s) 1535 s.Sect = sect 1536 s.Type = obj.SRODATA 1537 s.Value = int64(uint64(datsize) - sect.Vaddr) 1538 growdatsize(&datsize, s) 1539 } 1540 1541 sect.Length = uint64(datsize) - sect.Vaddr 1542 1543 /* gosymtab */ 1544 sect = addsection(segro, relro_prefix+".gosymtab", relro_perms) 1545 1546 sect.Align = maxalign(s, obj.SPCLNTAB-1) 1547 datsize = Rnd(datsize, int64(sect.Align)) 1548 sect.Vaddr = uint64(datsize) 1549 Linklookup(Ctxt, "runtime.symtab", 0).Sect = sect 1550 Linklookup(Ctxt, "runtime.esymtab", 0).Sect = sect 1551 for ; s != nil && s.Type < obj.SPCLNTAB; s = s.Next { 1552 datsize = aligndatsize(datsize, s) 1553 s.Sect = sect 1554 s.Type = obj.SRODATA 1555 s.Value = int64(uint64(datsize) - sect.Vaddr) 1556 growdatsize(&datsize, s) 1557 } 1558 1559 sect.Length = uint64(datsize) - sect.Vaddr 1560 1561 /* gopclntab */ 1562 sect = addsection(segro, relro_prefix+".gopclntab", relro_perms) 1563 1564 sect.Align = maxalign(s, obj.SELFROSECT-1) 1565 datsize = Rnd(datsize, int64(sect.Align)) 1566 sect.Vaddr = uint64(datsize) 1567 Linklookup(Ctxt, "runtime.pclntab", 0).Sect = sect 1568 Linklookup(Ctxt, "runtime.epclntab", 0).Sect = sect 1569 for ; s != nil && s.Type < obj.SELFROSECT; s = s.Next { 1570 datsize = aligndatsize(datsize, s) 1571 s.Sect = sect 1572 s.Type = obj.SRODATA 1573 s.Value = int64(uint64(datsize) - sect.Vaddr) 1574 growdatsize(&datsize, s) 1575 } 1576 1577 sect.Length = uint64(datsize) - sect.Vaddr 1578 1579 /* read-only ELF, Mach-O sections */ 1580 for ; s != nil && s.Type < obj.SELFSECT; s = s.Next { 1581 sect = addsection(segro, s.Name, 04) 1582 sect.Align = symalign(s) 1583 datsize = Rnd(datsize, int64(sect.Align)) 1584 sect.Vaddr = uint64(datsize) 1585 s.Sect = sect 1586 s.Type = obj.SRODATA 1587 s.Value = int64(uint64(datsize) - sect.Vaddr) 1588 growdatsize(&datsize, s) 1589 sect.Length = uint64(datsize) - sect.Vaddr 1590 } 1591 1592 // 6g uses 4-byte relocation offsets, so the entire segment must fit in 32 bits. 1593 if datsize != int64(uint32(datsize)) { 1594 Diag("read-only data segment too large") 1595 } 1596 1597 /* number the sections */ 1598 n := int32(1) 1599 1600 for sect := Segtext.Sect; sect != nil; sect = sect.Next { 1601 sect.Extnum = int16(n) 1602 n++ 1603 } 1604 for sect := Segrodata.Sect; sect != nil; sect = sect.Next { 1605 sect.Extnum = int16(n) 1606 n++ 1607 } 1608 for sect := Segdata.Sect; sect != nil; sect = sect.Next { 1609 sect.Extnum = int16(n) 1610 n++ 1611 } 1612 } 1613 1614 // Add buildid to beginning of text segment, on non-ELF systems. 1615 // Non-ELF binary formats are not always flexible enough to 1616 // give us a place to put the Go build ID. On those systems, we put it 1617 // at the very beginning of the text segment. 1618 // This ``header'' is read by cmd/go. 1619 func textbuildid() { 1620 if Iself || buildid == "" { 1621 return 1622 } 1623 1624 sym := Linklookup(Ctxt, "go.buildid", 0) 1625 sym.Reachable = true 1626 // The \xff is invalid UTF-8, meant to make it less likely 1627 // to find one of these accidentally. 1628 data := "\xff Go build ID: " + strconv.Quote(buildid) + "\n \xff" 1629 sym.Type = obj.STEXT 1630 sym.P = []byte(data) 1631 sym.Size = int64(len(sym.P)) 1632 1633 sym.Next = Ctxt.Textp 1634 Ctxt.Textp = sym 1635 } 1636 1637 // assign addresses to text 1638 func textaddress() { 1639 var sub *LSym 1640 1641 addsection(&Segtext, ".text", 05) 1642 1643 // Assign PCs in text segment. 1644 // Could parallelize, by assigning to text 1645 // and then letting threads copy down, but probably not worth it. 1646 sect := Segtext.Sect 1647 1648 sect.Align = int32(Funcalign) 1649 Linklookup(Ctxt, "runtime.text", 0).Sect = sect 1650 Linklookup(Ctxt, "runtime.etext", 0).Sect = sect 1651 va := uint64(INITTEXT) 1652 sect.Vaddr = va 1653 for sym := Ctxt.Textp; sym != nil; sym = sym.Next { 1654 sym.Sect = sect 1655 if sym.Type&obj.SSUB != 0 { 1656 continue 1657 } 1658 if sym.Align != 0 { 1659 va = uint64(Rnd(int64(va), int64(sym.Align))) 1660 } else { 1661 va = uint64(Rnd(int64(va), int64(Funcalign))) 1662 } 1663 sym.Value = 0 1664 for sub = sym; sub != nil; sub = sub.Sub { 1665 sub.Value += int64(va) 1666 } 1667 if sym.Size == 0 && sym.Sub != nil { 1668 Ctxt.Cursym = sym 1669 } 1670 if sym.Size < MINFUNC { 1671 va += MINFUNC // spacing required for findfunctab 1672 } else { 1673 va += uint64(sym.Size) 1674 } 1675 } 1676 1677 sect.Length = va - sect.Vaddr 1678 } 1679 1680 // assign addresses 1681 func address() { 1682 va := uint64(INITTEXT) 1683 Segtext.Rwx = 05 1684 Segtext.Vaddr = va 1685 Segtext.Fileoff = uint64(HEADR) 1686 for s := Segtext.Sect; s != nil; s = s.Next { 1687 va = uint64(Rnd(int64(va), int64(s.Align))) 1688 s.Vaddr = va 1689 va += s.Length 1690 } 1691 1692 Segtext.Length = va - uint64(INITTEXT) 1693 Segtext.Filelen = Segtext.Length 1694 if HEADTYPE == obj.Hnacl { 1695 va += 32 // room for the "halt sled" 1696 } 1697 1698 if Segrodata.Sect != nil { 1699 // align to page boundary so as not to mix 1700 // rodata and executable text. 1701 va = uint64(Rnd(int64(va), int64(INITRND))) 1702 1703 Segrodata.Rwx = 04 1704 Segrodata.Vaddr = va 1705 Segrodata.Fileoff = va - Segtext.Vaddr + Segtext.Fileoff 1706 Segrodata.Filelen = 0 1707 for s := Segrodata.Sect; s != nil; s = s.Next { 1708 va = uint64(Rnd(int64(va), int64(s.Align))) 1709 s.Vaddr = va 1710 va += s.Length 1711 } 1712 1713 Segrodata.Length = va - Segrodata.Vaddr 1714 Segrodata.Filelen = Segrodata.Length 1715 } 1716 1717 va = uint64(Rnd(int64(va), int64(INITRND))) 1718 Segdata.Rwx = 06 1719 Segdata.Vaddr = va 1720 Segdata.Fileoff = va - Segtext.Vaddr + Segtext.Fileoff 1721 Segdata.Filelen = 0 1722 if HEADTYPE == obj.Hwindows { 1723 Segdata.Fileoff = Segtext.Fileoff + uint64(Rnd(int64(Segtext.Length), PEFILEALIGN)) 1724 } 1725 if HEADTYPE == obj.Hplan9 { 1726 Segdata.Fileoff = Segtext.Fileoff + Segtext.Filelen 1727 } 1728 var data *Section 1729 var noptr *Section 1730 var bss *Section 1731 var noptrbss *Section 1732 var vlen int64 1733 for s := Segdata.Sect; s != nil; s = s.Next { 1734 if Iself && s.Name == ".tbss" { 1735 continue 1736 } 1737 vlen = int64(s.Length) 1738 if s.Next != nil && !(Iself && s.Next.Name == ".tbss") { 1739 vlen = int64(s.Next.Vaddr - s.Vaddr) 1740 } 1741 s.Vaddr = va 1742 va += uint64(vlen) 1743 Segdata.Length = va - Segdata.Vaddr 1744 if s.Name == ".data" { 1745 data = s 1746 } 1747 if s.Name == ".noptrdata" { 1748 noptr = s 1749 } 1750 if s.Name == ".bss" { 1751 bss = s 1752 } 1753 if s.Name == ".noptrbss" { 1754 noptrbss = s 1755 } 1756 } 1757 1758 Segdata.Filelen = bss.Vaddr - Segdata.Vaddr 1759 1760 text := Segtext.Sect 1761 var rodata *Section 1762 if Segrodata.Sect != nil { 1763 rodata = Segrodata.Sect 1764 } else { 1765 rodata = text.Next 1766 } 1767 typelink := rodata.Next 1768 if UseRelro() { 1769 // There is another section (.data.rel.ro) when building a shared 1770 // object on elf systems. 1771 typelink = typelink.Next 1772 } 1773 symtab := typelink.Next 1774 pclntab := symtab.Next 1775 1776 var sub *LSym 1777 for sym := datap; sym != nil; sym = sym.Next { 1778 Ctxt.Cursym = sym 1779 if sym.Sect != nil { 1780 sym.Value += int64(sym.Sect.Vaddr) 1781 } 1782 for sub = sym.Sub; sub != nil; sub = sub.Sub { 1783 sub.Value += sym.Value 1784 } 1785 } 1786 1787 if Buildmode == BuildmodeShared { 1788 s := Linklookup(Ctxt, "go.link.abihashbytes", 0) 1789 sectSym := Linklookup(Ctxt, ".note.go.abihash", 0) 1790 s.Sect = sectSym.Sect 1791 s.Value = int64(sectSym.Sect.Vaddr + 16) 1792 } 1793 1794 xdefine("runtime.text", obj.STEXT, int64(text.Vaddr)) 1795 xdefine("runtime.etext", obj.STEXT, int64(text.Vaddr+text.Length)) 1796 xdefine("runtime.rodata", obj.SRODATA, int64(rodata.Vaddr)) 1797 xdefine("runtime.erodata", obj.SRODATA, int64(rodata.Vaddr+rodata.Length)) 1798 xdefine("runtime.typelink", obj.SRODATA, int64(typelink.Vaddr)) 1799 xdefine("runtime.etypelink", obj.SRODATA, int64(typelink.Vaddr+typelink.Length)) 1800 1801 sym := Linklookup(Ctxt, "runtime.gcdata", 0) 1802 sym.Local = true 1803 xdefine("runtime.egcdata", obj.SRODATA, Symaddr(sym)+sym.Size) 1804 Linklookup(Ctxt, "runtime.egcdata", 0).Sect = sym.Sect 1805 1806 sym = Linklookup(Ctxt, "runtime.gcbss", 0) 1807 sym.Local = true 1808 xdefine("runtime.egcbss", obj.SRODATA, Symaddr(sym)+sym.Size) 1809 Linklookup(Ctxt, "runtime.egcbss", 0).Sect = sym.Sect 1810 1811 xdefine("runtime.symtab", obj.SRODATA, int64(symtab.Vaddr)) 1812 xdefine("runtime.esymtab", obj.SRODATA, int64(symtab.Vaddr+symtab.Length)) 1813 xdefine("runtime.pclntab", obj.SRODATA, int64(pclntab.Vaddr)) 1814 xdefine("runtime.epclntab", obj.SRODATA, int64(pclntab.Vaddr+pclntab.Length)) 1815 xdefine("runtime.noptrdata", obj.SNOPTRDATA, int64(noptr.Vaddr)) 1816 xdefine("runtime.enoptrdata", obj.SNOPTRDATA, int64(noptr.Vaddr+noptr.Length)) 1817 xdefine("runtime.bss", obj.SBSS, int64(bss.Vaddr)) 1818 xdefine("runtime.ebss", obj.SBSS, int64(bss.Vaddr+bss.Length)) 1819 xdefine("runtime.data", obj.SDATA, int64(data.Vaddr)) 1820 xdefine("runtime.edata", obj.SDATA, int64(data.Vaddr+data.Length)) 1821 xdefine("runtime.noptrbss", obj.SNOPTRBSS, int64(noptrbss.Vaddr)) 1822 xdefine("runtime.enoptrbss", obj.SNOPTRBSS, int64(noptrbss.Vaddr+noptrbss.Length)) 1823 xdefine("runtime.end", obj.SBSS, int64(Segdata.Vaddr+Segdata.Length)) 1824 }