github.com/go-asm/go@v1.21.1-0.20240213172139-40c5ead50c48/cmd/link/ld/data.go (about) 1 // Derived from Inferno utils/6l/obj.c and utils/6l/span.c 2 // https://bitbucket.org/inferno-os/inferno-os/src/master/utils/6l/obj.c 3 // https://bitbucket.org/inferno-os/inferno-os/src/master/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 "bytes" 36 "compress/zlib" 37 "debug/elf" 38 "encoding/binary" 39 "fmt" 40 "log" 41 "os" 42 "sort" 43 "strconv" 44 "strings" 45 "sync" 46 "sync/atomic" 47 48 "github.com/go-asm/go/cmd/gcprog" 49 "github.com/go-asm/go/cmd/link/loader" 50 "github.com/go-asm/go/cmd/link/loadpe" 51 "github.com/go-asm/go/cmd/link/sym" 52 "github.com/go-asm/go/cmd/objabi" 53 "github.com/go-asm/go/cmd/sys" 54 ) 55 56 // isRuntimeDepPkg reports whether pkg is the runtime package or its dependency. 57 func isRuntimeDepPkg(pkg string) bool { 58 switch pkg { 59 case "runtime", 60 "sync/atomic", // runtime may call to sync/atomic, due to go:linkname 61 "github.com/go-asm/go/abi", // used by reflectcall (and maybe more) 62 "github.com/go-asm/go/bytealg", // for IndexByte 63 "github.com/go-asm/go/chacha8rand", // for rand 64 "github.com/go-asm/go/cpu": // for cpu features 65 return true 66 } 67 return strings.HasPrefix(pkg, "runtime/github.com/go-asm/go/") && !strings.HasSuffix(pkg, "_test") 68 } 69 70 // Estimate the max size needed to hold any new trampolines created for this function. This 71 // is used to determine when the section can be split if it becomes too large, to ensure that 72 // the trampolines are in the same section as the function that uses them. 73 func maxSizeTrampolines(ctxt *Link, ldr *loader.Loader, s loader.Sym, isTramp bool) uint64 { 74 // If thearch.Trampoline is nil, then trampoline support is not available on this arch. 75 // A trampoline does not need any dependent trampolines. 76 if thearch.Trampoline == nil || isTramp { 77 return 0 78 } 79 80 n := uint64(0) 81 relocs := ldr.Relocs(s) 82 for ri := 0; ri < relocs.Count(); ri++ { 83 r := relocs.At(ri) 84 if r.Type().IsDirectCallOrJump() { 85 n++ 86 } 87 } 88 89 switch { 90 case ctxt.IsARM(): 91 return n * 20 // Trampolines in ARM range from 3 to 5 instructions. 92 case ctxt.IsARM64(): 93 return n * 12 // Trampolines in ARM64 are 3 instructions. 94 case ctxt.IsPPC64(): 95 return n * 16 // Trampolines in PPC64 are 4 instructions. 96 case ctxt.IsRISCV64(): 97 return n * 8 // Trampolines in RISCV64 are 2 instructions. 98 } 99 panic("unreachable") 100 } 101 102 // Detect too-far jumps in function s, and add trampolines if necessary. 103 // ARM, PPC64, PPC64LE and RISCV64 support trampoline insertion for internal 104 // and external linking. On PPC64 and PPC64LE the text sections might be split 105 // but will still insert trampolines where necessary. 106 func trampoline(ctxt *Link, s loader.Sym) { 107 if thearch.Trampoline == nil { 108 return // no need or no support of trampolines on this arch 109 } 110 111 ldr := ctxt.loader 112 relocs := ldr.Relocs(s) 113 for ri := 0; ri < relocs.Count(); ri++ { 114 r := relocs.At(ri) 115 rt := r.Type() 116 if !rt.IsDirectCallOrJump() && !isPLTCall(rt) { 117 continue 118 } 119 rs := r.Sym() 120 if !ldr.AttrReachable(rs) || ldr.SymType(rs) == sym.Sxxx { 121 continue // something is wrong. skip it here and we'll emit a better error later 122 } 123 124 if ldr.SymValue(rs) == 0 && ldr.SymType(rs) != sym.SDYNIMPORT && ldr.SymType(rs) != sym.SUNDEFEXT { 125 // Symbols in the same package are laid out together. 126 // Except that if SymPkg(s) == "", it is a host object symbol 127 // which may call an external symbol via PLT. 128 if ldr.SymPkg(s) != "" && ldr.SymPkg(rs) == ldr.SymPkg(s) { 129 // RISC-V is only able to reach +/-1MiB via a JAL instruction. 130 // We need to generate a trampoline when an address is 131 // currently unknown. 132 if !ctxt.Target.IsRISCV64() { 133 continue 134 } 135 } 136 // Runtime packages are laid out together. 137 if isRuntimeDepPkg(ldr.SymPkg(s)) && isRuntimeDepPkg(ldr.SymPkg(rs)) { 138 continue 139 } 140 } 141 thearch.Trampoline(ctxt, ldr, ri, rs, s) 142 } 143 } 144 145 // whether rt is a (host object) relocation that will be turned into 146 // a call to PLT. 147 func isPLTCall(rt objabi.RelocType) bool { 148 const pcrel = 1 149 switch rt { 150 // ARM64 151 case objabi.ElfRelocOffset + objabi.RelocType(elf.R_AARCH64_CALL26), 152 objabi.ElfRelocOffset + objabi.RelocType(elf.R_AARCH64_JUMP26), 153 objabi.MachoRelocOffset + MACHO_ARM64_RELOC_BRANCH26*2 + pcrel: 154 return true 155 156 // ARM 157 case objabi.ElfRelocOffset + objabi.RelocType(elf.R_ARM_CALL), 158 objabi.ElfRelocOffset + objabi.RelocType(elf.R_ARM_PC24), 159 objabi.ElfRelocOffset + objabi.RelocType(elf.R_ARM_JUMP24): 160 return true 161 } 162 // TODO: other architectures. 163 return false 164 } 165 166 // FoldSubSymbolOffset computes the offset of symbol s to its top-level outer 167 // symbol. Returns the top-level symbol and the offset. 168 // This is used in generating external relocations. 169 func FoldSubSymbolOffset(ldr *loader.Loader, s loader.Sym) (loader.Sym, int64) { 170 outer := ldr.OuterSym(s) 171 off := int64(0) 172 if outer != 0 { 173 off += ldr.SymValue(s) - ldr.SymValue(outer) 174 s = outer 175 } 176 return s, off 177 } 178 179 // relocsym resolve relocations in "s", updating the symbol's content 180 // in "P". 181 // The main loop walks through the list of relocations attached to "s" 182 // and resolves them where applicable. Relocations are often 183 // architecture-specific, requiring calls into the 'archreloc' and/or 184 // 'archrelocvariant' functions for the architecture. When external 185 // linking is in effect, it may not be possible to completely resolve 186 // the address/offset for a symbol, in which case the goal is to lay 187 // the groundwork for turning a given relocation into an external reloc 188 // (to be applied by the external linker). For more on how relocations 189 // work in general, see 190 // 191 // "Linkers and Loaders", by John R. Levine (Morgan Kaufmann, 1999), ch. 7 192 // 193 // This is a performance-critical function for the linker; be careful 194 // to avoid introducing unnecessary allocations in the main loop. 195 func (st *relocSymState) relocsym(s loader.Sym, P []byte) { 196 ldr := st.ldr 197 relocs := ldr.Relocs(s) 198 if relocs.Count() == 0 { 199 return 200 } 201 target := st.target 202 syms := st.syms 203 nExtReloc := 0 // number of external relocations 204 for ri := 0; ri < relocs.Count(); ri++ { 205 r := relocs.At(ri) 206 off := r.Off() 207 siz := int32(r.Siz()) 208 rs := r.Sym() 209 rt := r.Type() 210 weak := r.Weak() 211 if off < 0 || off+siz > int32(len(P)) { 212 rname := "" 213 if rs != 0 { 214 rname = ldr.SymName(rs) 215 } 216 st.err.Errorf(s, "invalid relocation %s: %d+%d not in [%d,%d)", rname, off, siz, 0, len(P)) 217 continue 218 } 219 if siz == 0 { // informational relocation - no work to do 220 continue 221 } 222 223 var rst sym.SymKind 224 if rs != 0 { 225 rst = ldr.SymType(rs) 226 } 227 228 if rs != 0 && (rst == sym.Sxxx || rst == sym.SXREF) { 229 // When putting the runtime but not main into a shared library 230 // these symbols are undefined and that's OK. 231 if target.IsShared() || target.IsPlugin() { 232 if ldr.SymName(rs) == "main.main" || (!target.IsPlugin() && ldr.SymName(rs) == "main..inittask") { 233 sb := ldr.MakeSymbolUpdater(rs) 234 sb.SetType(sym.SDYNIMPORT) 235 } else if strings.HasPrefix(ldr.SymName(rs), "go:info.") { 236 // Skip go.info symbols. They are only needed to communicate 237 // DWARF info between the compiler and linker. 238 continue 239 } 240 } else if target.IsPPC64() && ldr.SymName(rs) == ".TOC." { 241 // TOC symbol doesn't have a type but we do assign a value 242 // (see the address pass) and we can resolve it. 243 // TODO: give it a type. 244 } else { 245 st.err.errorUnresolved(ldr, s, rs) 246 continue 247 } 248 } 249 250 if rt >= objabi.ElfRelocOffset { 251 continue 252 } 253 254 // We need to be able to reference dynimport symbols when linking against 255 // shared libraries, and AIX, Darwin, OpenBSD and Solaris always need it. 256 if !target.IsAIX() && !target.IsDarwin() && !target.IsSolaris() && !target.IsOpenbsd() && rs != 0 && rst == sym.SDYNIMPORT && !target.IsDynlinkingGo() && !ldr.AttrSubSymbol(rs) { 257 if !(target.IsPPC64() && target.IsExternal() && ldr.SymName(rs) == ".TOC.") { 258 st.err.Errorf(s, "unhandled relocation for %s (type %d (%s) rtype %d (%s))", ldr.SymName(rs), rst, rst, rt, sym.RelocName(target.Arch, rt)) 259 } 260 } 261 if rs != 0 && rst != sym.STLSBSS && !weak && rt != objabi.R_METHODOFF && !ldr.AttrReachable(rs) { 262 st.err.Errorf(s, "unreachable sym in relocation: %s", ldr.SymName(rs)) 263 } 264 265 var rv sym.RelocVariant 266 if target.IsPPC64() || target.IsS390X() { 267 rv = ldr.RelocVariant(s, ri) 268 } 269 270 // TODO(mundaym): remove this special case - see issue 14218. 271 if target.IsS390X() { 272 switch rt { 273 case objabi.R_PCRELDBL: 274 rt = objabi.R_PCREL 275 rv = sym.RV_390_DBL 276 case objabi.R_CALL: 277 rv = sym.RV_390_DBL 278 } 279 } 280 281 var o int64 282 switch rt { 283 default: 284 switch siz { 285 default: 286 st.err.Errorf(s, "bad reloc size %#x for %s", uint32(siz), ldr.SymName(rs)) 287 case 1: 288 o = int64(P[off]) 289 case 2: 290 o = int64(target.Arch.ByteOrder.Uint16(P[off:])) 291 case 4: 292 o = int64(target.Arch.ByteOrder.Uint32(P[off:])) 293 case 8: 294 o = int64(target.Arch.ByteOrder.Uint64(P[off:])) 295 } 296 out, n, ok := thearch.Archreloc(target, ldr, syms, r, s, o) 297 if target.IsExternal() { 298 nExtReloc += n 299 } 300 if ok { 301 o = out 302 } else { 303 st.err.Errorf(s, "unknown reloc to %v: %d (%s)", ldr.SymName(rs), rt, sym.RelocName(target.Arch, rt)) 304 } 305 case objabi.R_TLS_LE: 306 if target.IsExternal() && target.IsElf() { 307 nExtReloc++ 308 o = 0 309 if !target.IsAMD64() { 310 o = r.Add() 311 } 312 break 313 } 314 315 if target.IsElf() && target.IsARM() { 316 // On ELF ARM, the thread pointer is 8 bytes before 317 // the start of the thread-local data block, so add 8 318 // to the actual TLS offset (r->sym->value). 319 // This 8 seems to be a fundamental constant of 320 // ELF on ARM (or maybe Glibc on ARM); it is not 321 // related to the fact that our own TLS storage happens 322 // to take up 8 bytes. 323 o = 8 + ldr.SymValue(rs) 324 } else if target.IsElf() || target.IsPlan9() || target.IsDarwin() { 325 o = int64(syms.Tlsoffset) + r.Add() 326 } else if target.IsWindows() { 327 o = r.Add() 328 } else { 329 log.Fatalf("unexpected R_TLS_LE relocation for %v", target.HeadType) 330 } 331 case objabi.R_TLS_IE: 332 if target.IsExternal() && target.IsElf() { 333 nExtReloc++ 334 o = 0 335 if !target.IsAMD64() { 336 o = r.Add() 337 } 338 if target.Is386() { 339 nExtReloc++ // need two ELF relocations on 386, see ../x86/asm.go:elfreloc1 340 } 341 break 342 } 343 if target.IsPIE() && target.IsElf() { 344 // We are linking the final executable, so we 345 // can optimize any TLS IE relocation to LE. 346 if thearch.TLSIEtoLE == nil { 347 log.Fatalf("internal linking of TLS IE not supported on %v", target.Arch.Family) 348 } 349 thearch.TLSIEtoLE(P, int(off), int(siz)) 350 o = int64(syms.Tlsoffset) 351 } else { 352 log.Fatalf("cannot handle R_TLS_IE (sym %s) when linking internally", ldr.SymName(s)) 353 } 354 case objabi.R_ADDR, objabi.R_PEIMAGEOFF: 355 if weak && !ldr.AttrReachable(rs) { 356 // Redirect it to runtime.unreachableMethod, which will throw if called. 357 rs = syms.unreachableMethod 358 } 359 if target.IsExternal() { 360 nExtReloc++ 361 362 // set up addend for eventual relocation via outer symbol. 363 rs := rs 364 rs, off := FoldSubSymbolOffset(ldr, rs) 365 xadd := r.Add() + off 366 rst := ldr.SymType(rs) 367 if rst != sym.SHOSTOBJ && rst != sym.SDYNIMPORT && rst != sym.SUNDEFEXT && ldr.SymSect(rs) == nil { 368 st.err.Errorf(s, "missing section for relocation target %s", ldr.SymName(rs)) 369 } 370 371 o = xadd 372 if target.IsElf() { 373 if target.IsAMD64() { 374 o = 0 375 } 376 } else if target.IsDarwin() { 377 if ldr.SymType(s).IsDWARF() { 378 // We generally use symbol-targeted relocations. 379 // DWARF tools seem to only handle section-targeted relocations, 380 // so generate section-targeted relocations in DWARF sections. 381 // See also machoreloc1. 382 o += ldr.SymValue(rs) 383 } 384 } else if target.IsWindows() { 385 // nothing to do 386 } else if target.IsAIX() { 387 o = ldr.SymValue(rs) + xadd 388 } else { 389 st.err.Errorf(s, "unhandled pcrel relocation to %s on %v", ldr.SymName(rs), target.HeadType) 390 } 391 392 break 393 } 394 395 // On AIX, a second relocation must be done by the loader, 396 // as section addresses can change once loaded. 397 // The "default" symbol address is still needed by the loader so 398 // the current relocation can't be skipped. 399 if target.IsAIX() && rst != sym.SDYNIMPORT { 400 // It's not possible to make a loader relocation in a 401 // symbol which is not inside .data section. 402 // FIXME: It should be forbidden to have R_ADDR from a 403 // symbol which isn't in .data. However, as .text has the 404 // same address once loaded, this is possible. 405 if ldr.SymSect(s).Seg == &Segdata { 406 Xcoffadddynrel(target, ldr, syms, s, r, ri) 407 } 408 } 409 410 o = ldr.SymValue(rs) + r.Add() 411 if rt == objabi.R_PEIMAGEOFF { 412 // The R_PEIMAGEOFF offset is a RVA, so subtract 413 // the base address for the executable. 414 o -= PEBASE 415 } 416 417 // On amd64, 4-byte offsets will be sign-extended, so it is impossible to 418 // access more than 2GB of static data; fail at link time is better than 419 // fail at runtime. See https://golang.org/issue/7980. 420 // Instead of special casing only amd64, we treat this as an error on all 421 // 64-bit architectures so as to be future-proof. 422 if int32(o) < 0 && target.Arch.PtrSize > 4 && siz == 4 { 423 st.err.Errorf(s, "non-pc-relative relocation address for %s is too big: %#x (%#x + %#x)", ldr.SymName(rs), uint64(o), ldr.SymValue(rs), r.Add()) 424 errorexit() 425 } 426 case objabi.R_DWARFSECREF: 427 if ldr.SymSect(rs) == nil { 428 st.err.Errorf(s, "missing DWARF section for relocation target %s", ldr.SymName(rs)) 429 } 430 431 if target.IsExternal() { 432 // On most platforms, the external linker needs to adjust DWARF references 433 // as it combines DWARF sections. However, on Darwin, dsymutil does the 434 // DWARF linking, and it understands how to follow section offsets. 435 // Leaving in the relocation records confuses it (see 436 // https://golang.org/issue/22068) so drop them for Darwin. 437 if !target.IsDarwin() { 438 nExtReloc++ 439 } 440 441 xadd := r.Add() + ldr.SymValue(rs) - int64(ldr.SymSect(rs).Vaddr) 442 443 o = xadd 444 if target.IsElf() && target.IsAMD64() { 445 o = 0 446 } 447 break 448 } 449 o = ldr.SymValue(rs) + r.Add() - int64(ldr.SymSect(rs).Vaddr) 450 case objabi.R_METHODOFF: 451 if !ldr.AttrReachable(rs) { 452 // Set it to a sentinel value. The runtime knows this is not pointing to 453 // anything valid. 454 o = -1 455 break 456 } 457 fallthrough 458 case objabi.R_ADDROFF: 459 if weak && !ldr.AttrReachable(rs) { 460 continue 461 } 462 sect := ldr.SymSect(rs) 463 if sect == nil { 464 if rst == sym.SDYNIMPORT { 465 st.err.Errorf(s, "cannot target DYNIMPORT sym in section-relative reloc: %s", ldr.SymName(rs)) 466 } else if rst == sym.SUNDEFEXT { 467 st.err.Errorf(s, "undefined symbol in relocation: %s", ldr.SymName(rs)) 468 } else { 469 st.err.Errorf(s, "missing section for relocation target %s", ldr.SymName(rs)) 470 } 471 continue 472 } 473 474 // The method offset tables using this relocation expect the offset to be relative 475 // to the start of the first text section, even if there are multiple. 476 if sect.Name == ".text" { 477 o = ldr.SymValue(rs) - int64(Segtext.Sections[0].Vaddr) + r.Add() 478 } else { 479 o = ldr.SymValue(rs) - int64(ldr.SymSect(rs).Vaddr) + r.Add() 480 } 481 482 case objabi.R_ADDRCUOFF: 483 // debug_range and debug_loc elements use this relocation type to get an 484 // offset from the start of the compile unit. 485 o = ldr.SymValue(rs) + r.Add() - ldr.SymValue(loader.Sym(ldr.SymUnit(rs).Textp[0])) 486 487 // r.Sym() can be 0 when CALL $(constant) is transformed from absolute PC to relative PC call. 488 case objabi.R_GOTPCREL: 489 if target.IsDynlinkingGo() && target.IsDarwin() && rs != 0 { 490 nExtReloc++ 491 o = r.Add() 492 break 493 } 494 if target.Is386() && target.IsExternal() && target.IsELF { 495 nExtReloc++ // need two ELF relocations on 386, see ../x86/asm.go:elfreloc1 496 } 497 fallthrough 498 case objabi.R_CALL, objabi.R_PCREL: 499 if target.IsExternal() && rs != 0 && rst == sym.SUNDEFEXT { 500 // pass through to the external linker. 501 nExtReloc++ 502 o = 0 503 break 504 } 505 if target.IsExternal() && rs != 0 && (ldr.SymSect(rs) != ldr.SymSect(s) || rt == objabi.R_GOTPCREL) { 506 nExtReloc++ 507 508 // set up addend for eventual relocation via outer symbol. 509 rs := rs 510 rs, off := FoldSubSymbolOffset(ldr, rs) 511 xadd := r.Add() + off - int64(siz) // relative to address after the relocated chunk 512 rst := ldr.SymType(rs) 513 if rst != sym.SHOSTOBJ && rst != sym.SDYNIMPORT && ldr.SymSect(rs) == nil { 514 st.err.Errorf(s, "missing section for relocation target %s", ldr.SymName(rs)) 515 } 516 517 o = xadd 518 if target.IsElf() { 519 if target.IsAMD64() { 520 o = 0 521 } 522 } else if target.IsDarwin() { 523 if rt == objabi.R_CALL { 524 if target.IsExternal() && rst == sym.SDYNIMPORT { 525 if target.IsAMD64() { 526 // AMD64 dynamic relocations are relative to the end of the relocation. 527 o += int64(siz) 528 } 529 } else { 530 if rst != sym.SHOSTOBJ { 531 o += int64(uint64(ldr.SymValue(rs)) - ldr.SymSect(rs).Vaddr) 532 } 533 o -= int64(off) // relative to section offset, not symbol 534 } 535 } else { 536 o += int64(siz) 537 } 538 } else if target.IsWindows() && target.IsAMD64() { // only amd64 needs PCREL 539 // PE/COFF's PC32 relocation uses the address after the relocated 540 // bytes as the base. Compensate by skewing the addend. 541 o += int64(siz) 542 } else { 543 st.err.Errorf(s, "unhandled pcrel relocation to %s on %v", ldr.SymName(rs), target.HeadType) 544 } 545 546 break 547 } 548 549 o = 0 550 if rs != 0 { 551 o = ldr.SymValue(rs) 552 } 553 554 o += r.Add() - (ldr.SymValue(s) + int64(off) + int64(siz)) 555 case objabi.R_SIZE: 556 o = ldr.SymSize(rs) + r.Add() 557 558 case objabi.R_XCOFFREF: 559 if !target.IsAIX() { 560 st.err.Errorf(s, "find XCOFF R_REF on non-XCOFF files") 561 } 562 if !target.IsExternal() { 563 st.err.Errorf(s, "find XCOFF R_REF with internal linking") 564 } 565 nExtReloc++ 566 continue 567 568 case objabi.R_DWARFFILEREF: 569 // We don't renumber files in dwarf.go:writelines anymore. 570 continue 571 572 case objabi.R_CONST: 573 o = r.Add() 574 575 case objabi.R_GOTOFF: 576 o = ldr.SymValue(rs) + r.Add() - ldr.SymValue(syms.GOT) 577 } 578 579 if target.IsPPC64() || target.IsS390X() { 580 if rv != sym.RV_NONE { 581 o = thearch.Archrelocvariant(target, ldr, r, rv, s, o, P) 582 } 583 } 584 585 switch siz { 586 default: 587 st.err.Errorf(s, "bad reloc size %#x for %s", uint32(siz), ldr.SymName(rs)) 588 case 1: 589 P[off] = byte(int8(o)) 590 case 2: 591 if (rt == objabi.R_PCREL || rt == objabi.R_CALL) && o != int64(int16(o)) { 592 st.err.Errorf(s, "pc-relative relocation address for %s is too big: %#x", ldr.SymName(rs), o) 593 } else if o != int64(int16(o)) && o != int64(uint16(o)) { 594 st.err.Errorf(s, "non-pc-relative relocation address for %s is too big: %#x", ldr.SymName(rs), uint64(o)) 595 } 596 target.Arch.ByteOrder.PutUint16(P[off:], uint16(o)) 597 case 4: 598 if (rt == objabi.R_PCREL || rt == objabi.R_CALL) && o != int64(int32(o)) { 599 st.err.Errorf(s, "pc-relative relocation address for %s is too big: %#x", ldr.SymName(rs), o) 600 } else if o != int64(int32(o)) && o != int64(uint32(o)) { 601 st.err.Errorf(s, "non-pc-relative relocation address for %s is too big: %#x", ldr.SymName(rs), uint64(o)) 602 } 603 target.Arch.ByteOrder.PutUint32(P[off:], uint32(o)) 604 case 8: 605 target.Arch.ByteOrder.PutUint64(P[off:], uint64(o)) 606 } 607 } 608 if target.IsExternal() { 609 // We'll stream out the external relocations in asmb2 (e.g. elfrelocsect) 610 // and we only need the count here. 611 atomic.AddUint32(&ldr.SymSect(s).Relcount, uint32(nExtReloc)) 612 } 613 } 614 615 // Convert a Go relocation to an external relocation. 616 func extreloc(ctxt *Link, ldr *loader.Loader, s loader.Sym, r loader.Reloc) (loader.ExtReloc, bool) { 617 var rr loader.ExtReloc 618 target := &ctxt.Target 619 siz := int32(r.Siz()) 620 if siz == 0 { // informational relocation - no work to do 621 return rr, false 622 } 623 624 rt := r.Type() 625 if rt >= objabi.ElfRelocOffset { 626 return rr, false 627 } 628 rr.Type = rt 629 rr.Size = uint8(siz) 630 631 // TODO(mundaym): remove this special case - see issue 14218. 632 if target.IsS390X() { 633 switch rt { 634 case objabi.R_PCRELDBL: 635 rt = objabi.R_PCREL 636 } 637 } 638 639 switch rt { 640 default: 641 return thearch.Extreloc(target, ldr, r, s) 642 643 case objabi.R_TLS_LE, objabi.R_TLS_IE: 644 if target.IsElf() { 645 rs := r.Sym() 646 rr.Xsym = rs 647 if rr.Xsym == 0 { 648 rr.Xsym = ctxt.Tlsg 649 } 650 rr.Xadd = r.Add() 651 break 652 } 653 return rr, false 654 655 case objabi.R_ADDR, objabi.R_PEIMAGEOFF: 656 // set up addend for eventual relocation via outer symbol. 657 rs := r.Sym() 658 if r.Weak() && !ldr.AttrReachable(rs) { 659 rs = ctxt.ArchSyms.unreachableMethod 660 } 661 rs, off := FoldSubSymbolOffset(ldr, rs) 662 rr.Xadd = r.Add() + off 663 rr.Xsym = rs 664 665 case objabi.R_DWARFSECREF: 666 // On most platforms, the external linker needs to adjust DWARF references 667 // as it combines DWARF sections. However, on Darwin, dsymutil does the 668 // DWARF linking, and it understands how to follow section offsets. 669 // Leaving in the relocation records confuses it (see 670 // https://golang.org/issue/22068) so drop them for Darwin. 671 if target.IsDarwin() { 672 return rr, false 673 } 674 rs := r.Sym() 675 rr.Xsym = loader.Sym(ldr.SymSect(rs).Sym) 676 rr.Xadd = r.Add() + ldr.SymValue(rs) - int64(ldr.SymSect(rs).Vaddr) 677 678 // r.Sym() can be 0 when CALL $(constant) is transformed from absolute PC to relative PC call. 679 case objabi.R_GOTPCREL, objabi.R_CALL, objabi.R_PCREL: 680 rs := r.Sym() 681 if rt == objabi.R_GOTPCREL && target.IsDynlinkingGo() && target.IsDarwin() && rs != 0 { 682 rr.Xadd = r.Add() 683 rr.Xadd -= int64(siz) // relative to address after the relocated chunk 684 rr.Xsym = rs 685 break 686 } 687 if rs != 0 && ldr.SymType(rs) == sym.SUNDEFEXT { 688 // pass through to the external linker. 689 rr.Xadd = 0 690 if target.IsElf() { 691 rr.Xadd -= int64(siz) 692 } 693 rr.Xsym = rs 694 break 695 } 696 if rs != 0 && (ldr.SymSect(rs) != ldr.SymSect(s) || rt == objabi.R_GOTPCREL) { 697 // set up addend for eventual relocation via outer symbol. 698 rs := rs 699 rs, off := FoldSubSymbolOffset(ldr, rs) 700 rr.Xadd = r.Add() + off 701 rr.Xadd -= int64(siz) // relative to address after the relocated chunk 702 rr.Xsym = rs 703 break 704 } 705 return rr, false 706 707 case objabi.R_XCOFFREF: 708 return ExtrelocSimple(ldr, r), true 709 710 // These reloc types don't need external relocations. 711 case objabi.R_ADDROFF, objabi.R_METHODOFF, objabi.R_ADDRCUOFF, 712 objabi.R_SIZE, objabi.R_CONST, objabi.R_GOTOFF: 713 return rr, false 714 } 715 return rr, true 716 } 717 718 // ExtrelocSimple creates a simple external relocation from r, with the same 719 // symbol and addend. 720 func ExtrelocSimple(ldr *loader.Loader, r loader.Reloc) loader.ExtReloc { 721 var rr loader.ExtReloc 722 rs := r.Sym() 723 rr.Xsym = rs 724 rr.Xadd = r.Add() 725 rr.Type = r.Type() 726 rr.Size = r.Siz() 727 return rr 728 } 729 730 // ExtrelocViaOuterSym creates an external relocation from r targeting the 731 // outer symbol and folding the subsymbol's offset into the addend. 732 func ExtrelocViaOuterSym(ldr *loader.Loader, r loader.Reloc, s loader.Sym) loader.ExtReloc { 733 // set up addend for eventual relocation via outer symbol. 734 var rr loader.ExtReloc 735 rs := r.Sym() 736 rs, off := FoldSubSymbolOffset(ldr, rs) 737 rr.Xadd = r.Add() + off 738 rst := ldr.SymType(rs) 739 if rst != sym.SHOSTOBJ && rst != sym.SDYNIMPORT && rst != sym.SUNDEFEXT && ldr.SymSect(rs) == nil { 740 ldr.Errorf(s, "missing section for %s", ldr.SymName(rs)) 741 } 742 rr.Xsym = rs 743 rr.Type = r.Type() 744 rr.Size = r.Siz() 745 return rr 746 } 747 748 // relocSymState hold state information needed when making a series of 749 // successive calls to relocsym(). The items here are invariant 750 // (meaning that they are set up once initially and then don't change 751 // during the execution of relocsym), with the exception of a slice 752 // used to facilitate batch allocation of external relocations. Calls 753 // to relocsym happen in parallel; the assumption is that each 754 // parallel thread will have its own state object. 755 type relocSymState struct { 756 target *Target 757 ldr *loader.Loader 758 err *ErrorReporter 759 syms *ArchSyms 760 } 761 762 // makeRelocSymState creates a relocSymState container object to 763 // pass to relocsym(). If relocsym() calls happen in parallel, 764 // each parallel thread should have its own state object. 765 func (ctxt *Link) makeRelocSymState() *relocSymState { 766 return &relocSymState{ 767 target: &ctxt.Target, 768 ldr: ctxt.loader, 769 err: &ctxt.ErrorReporter, 770 syms: &ctxt.ArchSyms, 771 } 772 } 773 774 // windynrelocsym examines a text symbol 's' and looks for relocations 775 // from it that correspond to references to symbols defined in DLLs, 776 // then fixes up those relocations as needed. A reference to a symbol 777 // XYZ from some DLL will fall into one of two categories: an indirect 778 // ref via "__imp_XYZ", or a direct ref to "XYZ". Here's an example of 779 // an indirect ref (this is an excerpt from objdump -ldr): 780 // 781 // 1c1: 48 89 c6 movq %rax, %rsi 782 // 1c4: ff 15 00 00 00 00 callq *(%rip) 783 // 00000000000001c6: IMAGE_REL_AMD64_REL32 __imp__errno 784 // 785 // In the assembly above, the code loads up the value of __imp_errno 786 // and then does an indirect call to that value. 787 // 788 // Here is what a direct reference might look like: 789 // 790 // 137: e9 20 06 00 00 jmp 0x75c <pow+0x75c> 791 // 13c: e8 00 00 00 00 callq 0x141 <pow+0x141> 792 // 000000000000013d: IMAGE_REL_AMD64_REL32 _errno 793 // 794 // The assembly below dispenses with the import symbol and just makes 795 // a direct call to _errno. 796 // 797 // The code below handles indirect refs by redirecting the target of 798 // the relocation from "__imp_XYZ" to "XYZ" (since the latter symbol 799 // is what the Windows loader is expected to resolve). For direct refs 800 // the call is redirected to a stub, where the stub first loads the 801 // symbol and then direct an indirect call to that value. 802 // 803 // Note that for a given symbol (as above) it is perfectly legal to 804 // have both direct and indirect references. 805 func windynrelocsym(ctxt *Link, rel *loader.SymbolBuilder, s loader.Sym) error { 806 var su *loader.SymbolBuilder 807 relocs := ctxt.loader.Relocs(s) 808 for ri := 0; ri < relocs.Count(); ri++ { 809 r := relocs.At(ri) 810 if r.IsMarker() { 811 continue // skip marker relocations 812 } 813 targ := r.Sym() 814 if targ == 0 { 815 continue 816 } 817 if !ctxt.loader.AttrReachable(targ) { 818 if r.Weak() { 819 continue 820 } 821 return fmt.Errorf("dynamic relocation to unreachable symbol %s", 822 ctxt.loader.SymName(targ)) 823 } 824 tgot := ctxt.loader.SymGot(targ) 825 if tgot == loadpe.RedirectToDynImportGotToken { 826 827 // Consistency check: name should be __imp_X 828 sname := ctxt.loader.SymName(targ) 829 if !strings.HasPrefix(sname, "__imp_") { 830 return fmt.Errorf("internal error in windynrelocsym: redirect GOT token applied to non-import symbol %s", sname) 831 } 832 833 // Locate underlying symbol (which originally had type 834 // SDYNIMPORT but has since been retyped to SWINDOWS). 835 ds, err := loadpe.LookupBaseFromImport(targ, ctxt.loader, ctxt.Arch) 836 if err != nil { 837 return err 838 } 839 dstyp := ctxt.loader.SymType(ds) 840 if dstyp != sym.SWINDOWS { 841 return fmt.Errorf("internal error in windynrelocsym: underlying sym for %q has wrong type %s", sname, dstyp.String()) 842 } 843 844 // Redirect relocation to the dynimport. 845 r.SetSym(ds) 846 continue 847 } 848 849 tplt := ctxt.loader.SymPlt(targ) 850 if tplt == loadpe.CreateImportStubPltToken { 851 852 // Consistency check: don't want to see both PLT and GOT tokens. 853 if tgot != -1 { 854 return fmt.Errorf("internal error in windynrelocsym: invalid GOT setting %d for reloc to %s", tgot, ctxt.loader.SymName(targ)) 855 } 856 857 // make dynimport JMP table for PE object files. 858 tplt := int32(rel.Size()) 859 ctxt.loader.SetPlt(targ, tplt) 860 861 if su == nil { 862 su = ctxt.loader.MakeSymbolUpdater(s) 863 } 864 r.SetSym(rel.Sym()) 865 r.SetAdd(int64(tplt)) 866 867 // jmp *addr 868 switch ctxt.Arch.Family { 869 default: 870 return fmt.Errorf("internal error in windynrelocsym: unsupported arch %v", ctxt.Arch.Family) 871 case sys.I386: 872 rel.AddUint8(0xff) 873 rel.AddUint8(0x25) 874 rel.AddAddrPlus(ctxt.Arch, targ, 0) 875 rel.AddUint8(0x90) 876 rel.AddUint8(0x90) 877 case sys.AMD64: 878 rel.AddUint8(0xff) 879 rel.AddUint8(0x24) 880 rel.AddUint8(0x25) 881 rel.AddAddrPlus4(ctxt.Arch, targ, 0) 882 rel.AddUint8(0x90) 883 } 884 } else if tplt >= 0 { 885 if su == nil { 886 su = ctxt.loader.MakeSymbolUpdater(s) 887 } 888 r.SetSym(rel.Sym()) 889 r.SetAdd(int64(tplt)) 890 } 891 } 892 return nil 893 } 894 895 // windynrelocsyms generates jump table to C library functions that will be 896 // added later. windynrelocsyms writes the table into .rel symbol. 897 func (ctxt *Link) windynrelocsyms() { 898 if !(ctxt.IsWindows() && iscgo && ctxt.IsInternal()) { 899 return 900 } 901 902 rel := ctxt.loader.CreateSymForUpdate(".rel", 0) 903 rel.SetType(sym.STEXT) 904 905 for _, s := range ctxt.Textp { 906 if err := windynrelocsym(ctxt, rel, s); err != nil { 907 ctxt.Errorf(s, "%v", err) 908 } 909 } 910 911 ctxt.Textp = append(ctxt.Textp, rel.Sym()) 912 } 913 914 func dynrelocsym(ctxt *Link, s loader.Sym) { 915 target := &ctxt.Target 916 ldr := ctxt.loader 917 syms := &ctxt.ArchSyms 918 relocs := ldr.Relocs(s) 919 for ri := 0; ri < relocs.Count(); ri++ { 920 r := relocs.At(ri) 921 if r.IsMarker() { 922 continue // skip marker relocations 923 } 924 rSym := r.Sym() 925 if r.Weak() && !ldr.AttrReachable(rSym) { 926 continue 927 } 928 if ctxt.BuildMode == BuildModePIE && ctxt.LinkMode == LinkInternal { 929 // It's expected that some relocations will be done 930 // later by relocsym (R_TLS_LE, R_ADDROFF), so 931 // don't worry if Adddynrel returns false. 932 thearch.Adddynrel(target, ldr, syms, s, r, ri) 933 continue 934 } 935 936 if rSym != 0 && ldr.SymType(rSym) == sym.SDYNIMPORT || r.Type() >= objabi.ElfRelocOffset { 937 if rSym != 0 && !ldr.AttrReachable(rSym) { 938 ctxt.Errorf(s, "dynamic relocation to unreachable symbol %s", ldr.SymName(rSym)) 939 } 940 if !thearch.Adddynrel(target, ldr, syms, s, r, ri) { 941 ctxt.Errorf(s, "unsupported dynamic relocation for symbol %s (type=%d (%s) stype=%d (%s))", ldr.SymName(rSym), r.Type(), sym.RelocName(ctxt.Arch, r.Type()), ldr.SymType(rSym), ldr.SymType(rSym)) 942 } 943 } 944 } 945 } 946 947 func (state *dodataState) dynreloc(ctxt *Link) { 948 if ctxt.HeadType == objabi.Hwindows { 949 return 950 } 951 // -d suppresses dynamic loader format, so we may as well not 952 // compute these sections or mark their symbols as reachable. 953 if *FlagD { 954 return 955 } 956 957 for _, s := range ctxt.Textp { 958 dynrelocsym(ctxt, s) 959 } 960 for _, syms := range state.data { 961 for _, s := range syms { 962 dynrelocsym(ctxt, s) 963 } 964 } 965 if ctxt.IsELF { 966 elfdynhash(ctxt) 967 } 968 } 969 970 func CodeblkPad(ctxt *Link, out *OutBuf, addr int64, size int64, pad []byte) { 971 writeBlocks(ctxt, out, ctxt.outSem, ctxt.loader, ctxt.Textp, addr, size, pad) 972 } 973 974 const blockSize = 1 << 20 // 1MB chunks written at a time. 975 976 // writeBlocks writes a specified chunk of symbols to the output buffer. It 977 // breaks the write up into ≥blockSize chunks to write them out, and schedules 978 // as many goroutines as necessary to accomplish this task. This call then 979 // blocks, waiting on the writes to complete. Note that we use the sem parameter 980 // to limit the number of concurrent writes taking place. 981 func writeBlocks(ctxt *Link, out *OutBuf, sem chan int, ldr *loader.Loader, syms []loader.Sym, addr, size int64, pad []byte) { 982 for i, s := range syms { 983 if ldr.SymValue(s) >= addr && !ldr.AttrSubSymbol(s) { 984 syms = syms[i:] 985 break 986 } 987 } 988 989 var wg sync.WaitGroup 990 max, lastAddr, written := int64(blockSize), addr+size, int64(0) 991 for addr < lastAddr { 992 // Find the last symbol we'd write. 993 idx := -1 994 for i, s := range syms { 995 if ldr.AttrSubSymbol(s) { 996 continue 997 } 998 999 // If the next symbol's size would put us out of bounds on the total length, 1000 // stop looking. 1001 end := ldr.SymValue(s) + ldr.SymSize(s) 1002 if end > lastAddr { 1003 break 1004 } 1005 1006 // We're gonna write this symbol. 1007 idx = i 1008 1009 // If we cross over the max size, we've got enough symbols. 1010 if end > addr+max { 1011 break 1012 } 1013 } 1014 1015 // If we didn't find any symbols to write, we're done here. 1016 if idx < 0 { 1017 break 1018 } 1019 1020 // Compute the length to write, including padding. 1021 // We need to write to the end address (lastAddr), or the next symbol's 1022 // start address, whichever comes first. If there is no more symbols, 1023 // just write to lastAddr. This ensures we don't leave holes between the 1024 // blocks or at the end. 1025 length := int64(0) 1026 if idx+1 < len(syms) { 1027 // Find the next top-level symbol. 1028 // Skip over sub symbols so we won't split a container symbol 1029 // into two blocks. 1030 next := syms[idx+1] 1031 for ldr.AttrSubSymbol(next) { 1032 idx++ 1033 next = syms[idx+1] 1034 } 1035 length = ldr.SymValue(next) - addr 1036 } 1037 if length == 0 || length > lastAddr-addr { 1038 length = lastAddr - addr 1039 } 1040 1041 // Start the block output operator. 1042 if o, err := out.View(uint64(out.Offset() + written)); err == nil { 1043 sem <- 1 1044 wg.Add(1) 1045 go func(o *OutBuf, ldr *loader.Loader, syms []loader.Sym, addr, size int64, pad []byte) { 1046 writeBlock(ctxt, o, ldr, syms, addr, size, pad) 1047 wg.Done() 1048 <-sem 1049 }(o, ldr, syms, addr, length, pad) 1050 } else { // output not mmaped, don't parallelize. 1051 writeBlock(ctxt, out, ldr, syms, addr, length, pad) 1052 } 1053 1054 // Prepare for the next loop. 1055 if idx != -1 { 1056 syms = syms[idx+1:] 1057 } 1058 written += length 1059 addr += length 1060 } 1061 wg.Wait() 1062 } 1063 1064 func writeBlock(ctxt *Link, out *OutBuf, ldr *loader.Loader, syms []loader.Sym, addr, size int64, pad []byte) { 1065 1066 st := ctxt.makeRelocSymState() 1067 1068 // This doesn't distinguish the memory size from the file 1069 // size, and it lays out the file based on Symbol.Value, which 1070 // is the virtual address. DWARF compression changes file sizes, 1071 // so dwarfcompress will fix this up later if necessary. 1072 eaddr := addr + size 1073 for _, s := range syms { 1074 if ldr.AttrSubSymbol(s) { 1075 continue 1076 } 1077 val := ldr.SymValue(s) 1078 if val >= eaddr { 1079 break 1080 } 1081 if val < addr { 1082 ldr.Errorf(s, "phase error: addr=%#x but val=%#x sym=%s type=%v sect=%v sect.addr=%#x", addr, val, ldr.SymName(s), ldr.SymType(s), ldr.SymSect(s).Name, ldr.SymSect(s).Vaddr) 1083 errorexit() 1084 } 1085 if addr < val { 1086 out.WriteStringPad("", int(val-addr), pad) 1087 addr = val 1088 } 1089 P := out.WriteSym(ldr, s) 1090 st.relocsym(s, P) 1091 if ldr.IsGeneratedSym(s) { 1092 f := ctxt.generatorSyms[s] 1093 f(ctxt, s) 1094 } 1095 addr += int64(len(P)) 1096 siz := ldr.SymSize(s) 1097 if addr < val+siz { 1098 out.WriteStringPad("", int(val+siz-addr), pad) 1099 addr = val + siz 1100 } 1101 if addr != val+siz { 1102 ldr.Errorf(s, "phase error: addr=%#x value+size=%#x", addr, val+siz) 1103 errorexit() 1104 } 1105 if val+siz >= eaddr { 1106 break 1107 } 1108 } 1109 1110 if addr < eaddr { 1111 out.WriteStringPad("", int(eaddr-addr), pad) 1112 } 1113 } 1114 1115 type writeFn func(*Link, *OutBuf, int64, int64) 1116 1117 // writeParallel handles scheduling parallel execution of data write functions. 1118 func writeParallel(wg *sync.WaitGroup, fn writeFn, ctxt *Link, seek, vaddr, length uint64) { 1119 if out, err := ctxt.Out.View(seek); err != nil { 1120 ctxt.Out.SeekSet(int64(seek)) 1121 fn(ctxt, ctxt.Out, int64(vaddr), int64(length)) 1122 } else { 1123 wg.Add(1) 1124 go func() { 1125 defer wg.Done() 1126 fn(ctxt, out, int64(vaddr), int64(length)) 1127 }() 1128 } 1129 } 1130 1131 func datblk(ctxt *Link, out *OutBuf, addr, size int64) { 1132 writeDatblkToOutBuf(ctxt, out, addr, size) 1133 } 1134 1135 // Used only on Wasm for now. 1136 func DatblkBytes(ctxt *Link, addr int64, size int64) []byte { 1137 buf := make([]byte, size) 1138 out := &OutBuf{heap: buf} 1139 writeDatblkToOutBuf(ctxt, out, addr, size) 1140 return buf 1141 } 1142 1143 func writeDatblkToOutBuf(ctxt *Link, out *OutBuf, addr int64, size int64) { 1144 writeBlocks(ctxt, out, ctxt.outSem, ctxt.loader, ctxt.datap, addr, size, zeros[:]) 1145 } 1146 1147 func dwarfblk(ctxt *Link, out *OutBuf, addr int64, size int64) { 1148 // Concatenate the section symbol lists into a single list to pass 1149 // to writeBlocks. 1150 // 1151 // NB: ideally we would do a separate writeBlocks call for each 1152 // section, but this would run the risk of undoing any file offset 1153 // adjustments made during layout. 1154 n := 0 1155 for i := range dwarfp { 1156 n += len(dwarfp[i].syms) 1157 } 1158 syms := make([]loader.Sym, 0, n) 1159 for i := range dwarfp { 1160 syms = append(syms, dwarfp[i].syms...) 1161 } 1162 writeBlocks(ctxt, out, ctxt.outSem, ctxt.loader, syms, addr, size, zeros[:]) 1163 } 1164 1165 func pdatablk(ctxt *Link, out *OutBuf, addr int64, size int64) { 1166 writeBlocks(ctxt, out, ctxt.outSem, ctxt.loader, sehp.pdata, addr, size, zeros[:]) 1167 } 1168 1169 func xdatablk(ctxt *Link, out *OutBuf, addr int64, size int64) { 1170 writeBlocks(ctxt, out, ctxt.outSem, ctxt.loader, sehp.xdata, addr, size, zeros[:]) 1171 } 1172 1173 var covCounterDataStartOff, covCounterDataLen uint64 1174 1175 var zeros [512]byte 1176 1177 var ( 1178 strdata = make(map[string]string) 1179 strnames []string 1180 ) 1181 1182 func addstrdata1(ctxt *Link, arg string) { 1183 eq := strings.Index(arg, "=") 1184 dot := strings.LastIndex(arg[:eq+1], ".") 1185 if eq < 0 || dot < 0 { 1186 Exitf("-X flag requires argument of the form importpath.name=value") 1187 } 1188 pkg := arg[:dot] 1189 if ctxt.BuildMode == BuildModePlugin && pkg == "main" { 1190 pkg = *flagPluginPath 1191 } 1192 pkg = objabi.PathToPrefix(pkg) 1193 name := pkg + arg[dot:eq] 1194 value := arg[eq+1:] 1195 if _, ok := strdata[name]; !ok { 1196 strnames = append(strnames, name) 1197 } 1198 strdata[name] = value 1199 } 1200 1201 // addstrdata sets the initial value of the string variable name to value. 1202 func addstrdata(arch *sys.Arch, l *loader.Loader, name, value string) { 1203 s := l.Lookup(name, 0) 1204 if s == 0 { 1205 return 1206 } 1207 if goType := l.SymGoType(s); goType == 0 { 1208 return 1209 } else if typeName := l.SymName(goType); typeName != "type:string" { 1210 Errorf(nil, "%s: cannot set with -X: not a var of type string (%s)", name, typeName) 1211 return 1212 } 1213 if !l.AttrReachable(s) { 1214 return // don't bother setting unreachable variable 1215 } 1216 bld := l.MakeSymbolUpdater(s) 1217 if bld.Type() == sym.SBSS { 1218 bld.SetType(sym.SDATA) 1219 } 1220 1221 p := fmt.Sprintf("%s.str", name) 1222 sbld := l.CreateSymForUpdate(p, 0) 1223 sbld.Addstring(value) 1224 sbld.SetType(sym.SRODATA) 1225 1226 // Don't reset the variable's size. String variable usually has size of 1227 // 2*PtrSize, but in ASAN build it can be larger due to red zone. 1228 // (See issue 56175.) 1229 bld.SetData(make([]byte, arch.PtrSize*2)) 1230 bld.SetReadOnly(false) 1231 bld.ResetRelocs() 1232 bld.SetAddrPlus(arch, 0, sbld.Sym(), 0) 1233 bld.SetUint(arch, int64(arch.PtrSize), uint64(len(value))) 1234 } 1235 1236 func (ctxt *Link) dostrdata() { 1237 for _, name := range strnames { 1238 addstrdata(ctxt.Arch, ctxt.loader, name, strdata[name]) 1239 } 1240 } 1241 1242 // addgostring adds str, as a Go string value, to s. symname is the name of the 1243 // symbol used to define the string data and must be unique per linked object. 1244 func addgostring(ctxt *Link, ldr *loader.Loader, s *loader.SymbolBuilder, symname, str string) { 1245 sdata := ldr.CreateSymForUpdate(symname, 0) 1246 if sdata.Type() != sym.Sxxx { 1247 ctxt.Errorf(s.Sym(), "duplicate symname in addgostring: %s", symname) 1248 } 1249 sdata.SetLocal(true) 1250 sdata.SetType(sym.SRODATA) 1251 sdata.SetSize(int64(len(str))) 1252 sdata.SetData([]byte(str)) 1253 s.AddAddr(ctxt.Arch, sdata.Sym()) 1254 s.AddUint(ctxt.Arch, uint64(len(str))) 1255 } 1256 1257 func addinitarrdata(ctxt *Link, ldr *loader.Loader, s loader.Sym) { 1258 p := ldr.SymName(s) + ".ptr" 1259 sp := ldr.CreateSymForUpdate(p, 0) 1260 sp.SetType(sym.SINITARR) 1261 sp.SetSize(0) 1262 sp.SetDuplicateOK(true) 1263 sp.AddAddr(ctxt.Arch, s) 1264 } 1265 1266 // symalign returns the required alignment for the given symbol s. 1267 func symalign(ldr *loader.Loader, s loader.Sym) int32 { 1268 min := int32(thearch.Minalign) 1269 align := ldr.SymAlign(s) 1270 if align >= min { 1271 return align 1272 } else if align != 0 { 1273 return min 1274 } 1275 align = int32(thearch.Maxalign) 1276 ssz := ldr.SymSize(s) 1277 for int64(align) > ssz && align > min { 1278 align >>= 1 1279 } 1280 ldr.SetSymAlign(s, align) 1281 return align 1282 } 1283 1284 func aligndatsize(state *dodataState, datsize int64, s loader.Sym) int64 { 1285 return Rnd(datsize, int64(symalign(state.ctxt.loader, s))) 1286 } 1287 1288 const debugGCProg = false 1289 1290 type GCProg struct { 1291 ctxt *Link 1292 sym *loader.SymbolBuilder 1293 w gcprog.Writer 1294 } 1295 1296 func (p *GCProg) Init(ctxt *Link, name string) { 1297 p.ctxt = ctxt 1298 p.sym = ctxt.loader.CreateSymForUpdate(name, 0) 1299 p.w.Init(p.writeByte()) 1300 if debugGCProg { 1301 fmt.Fprintf(os.Stderr, "ld: start GCProg %s\n", name) 1302 p.w.Debug(os.Stderr) 1303 } 1304 } 1305 1306 func (p *GCProg) writeByte() func(x byte) { 1307 return func(x byte) { 1308 p.sym.AddUint8(x) 1309 } 1310 } 1311 1312 func (p *GCProg) End(size int64) { 1313 p.w.ZeroUntil(size / int64(p.ctxt.Arch.PtrSize)) 1314 p.w.End() 1315 if debugGCProg { 1316 fmt.Fprintf(os.Stderr, "ld: end GCProg\n") 1317 } 1318 } 1319 1320 func (p *GCProg) AddSym(s loader.Sym) { 1321 ldr := p.ctxt.loader 1322 typ := ldr.SymGoType(s) 1323 1324 // Things without pointers should be in sym.SNOPTRDATA or sym.SNOPTRBSS; 1325 // everything we see should have pointers and should therefore have a type. 1326 if typ == 0 { 1327 switch ldr.SymName(s) { 1328 case "runtime.data", "runtime.edata", "runtime.bss", "runtime.ebss": 1329 // Ignore special symbols that are sometimes laid out 1330 // as real symbols. See comment about dyld on darwin in 1331 // the address function. 1332 return 1333 } 1334 p.ctxt.Errorf(p.sym.Sym(), "missing Go type information for global symbol %s: size %d", ldr.SymName(s), ldr.SymSize(s)) 1335 return 1336 } 1337 1338 ptrsize := int64(p.ctxt.Arch.PtrSize) 1339 typData := ldr.Data(typ) 1340 nptr := decodetypePtrdata(p.ctxt.Arch, typData) / ptrsize 1341 1342 if debugGCProg { 1343 fmt.Fprintf(os.Stderr, "gcprog sym: %s at %d (ptr=%d+%d)\n", ldr.SymName(s), ldr.SymValue(s), ldr.SymValue(s)/ptrsize, nptr) 1344 } 1345 1346 sval := ldr.SymValue(s) 1347 if decodetypeUsegcprog(p.ctxt.Arch, typData) == 0 { 1348 // Copy pointers from mask into program. 1349 mask := decodetypeGcmask(p.ctxt, typ) 1350 for i := int64(0); i < nptr; i++ { 1351 if (mask[i/8]>>uint(i%8))&1 != 0 { 1352 p.w.Ptr(sval/ptrsize + i) 1353 } 1354 } 1355 return 1356 } 1357 1358 // Copy program. 1359 prog := decodetypeGcprog(p.ctxt, typ) 1360 p.w.ZeroUntil(sval / ptrsize) 1361 p.w.Append(prog[4:], nptr) 1362 } 1363 1364 // cutoff is the maximum data section size permitted by the linker 1365 // (see issue #9862). 1366 const cutoff = 2e9 // 2 GB (or so; looks better in errors than 2^31) 1367 1368 // check accumulated size of data sections 1369 func (state *dodataState) checkdatsize(symn sym.SymKind) { 1370 if state.datsize > cutoff { 1371 Errorf(nil, "too much data, last section %v (%d, over %v bytes)", symn, state.datsize, cutoff) 1372 } 1373 } 1374 1375 func checkSectSize(sect *sym.Section) { 1376 // TODO: consider using 4 GB size limit for DWARF sections, and 1377 // make sure we generate unsigned offset in relocations and check 1378 // for overflow. 1379 if sect.Length > cutoff { 1380 Errorf(nil, "too much data in section %s (%d, over %v bytes)", sect.Name, sect.Length, cutoff) 1381 } 1382 } 1383 1384 // fixZeroSizedSymbols gives a few special symbols with zero size some space. 1385 func fixZeroSizedSymbols(ctxt *Link) { 1386 // The values in moduledata are filled out by relocations 1387 // pointing to the addresses of these special symbols. 1388 // Typically these symbols have no size and are not laid 1389 // out with their matching section. 1390 // 1391 // However on darwin, dyld will find the special symbol 1392 // in the first loaded module, even though it is local. 1393 // 1394 // (An hypothesis, formed without looking in the dyld sources: 1395 // these special symbols have no size, so their address 1396 // matches a real symbol. The dynamic linker assumes we 1397 // want the normal symbol with the same address and finds 1398 // it in the other module.) 1399 // 1400 // To work around this we lay out the symbls whose 1401 // addresses are vital for multi-module programs to work 1402 // as normal symbols, and give them a little size. 1403 // 1404 // On AIX, as all DATA sections are merged together, ld might not put 1405 // these symbols at the beginning of their respective section if there 1406 // aren't real symbols, their alignment might not match the 1407 // first symbol alignment. Therefore, there are explicitly put at the 1408 // beginning of their section with the same alignment. 1409 if !(ctxt.DynlinkingGo() && ctxt.HeadType == objabi.Hdarwin) && !(ctxt.HeadType == objabi.Haix && ctxt.LinkMode == LinkExternal) { 1410 return 1411 } 1412 1413 ldr := ctxt.loader 1414 bss := ldr.CreateSymForUpdate("runtime.bss", 0) 1415 bss.SetSize(8) 1416 ldr.SetAttrSpecial(bss.Sym(), false) 1417 1418 ebss := ldr.CreateSymForUpdate("runtime.ebss", 0) 1419 ldr.SetAttrSpecial(ebss.Sym(), false) 1420 1421 data := ldr.CreateSymForUpdate("runtime.data", 0) 1422 data.SetSize(8) 1423 ldr.SetAttrSpecial(data.Sym(), false) 1424 1425 edata := ldr.CreateSymForUpdate("runtime.edata", 0) 1426 ldr.SetAttrSpecial(edata.Sym(), false) 1427 1428 if ctxt.HeadType == objabi.Haix { 1429 // XCOFFTOC symbols are part of .data section. 1430 edata.SetType(sym.SXCOFFTOC) 1431 } 1432 1433 noptrbss := ldr.CreateSymForUpdate("runtime.noptrbss", 0) 1434 noptrbss.SetSize(8) 1435 ldr.SetAttrSpecial(noptrbss.Sym(), false) 1436 1437 enoptrbss := ldr.CreateSymForUpdate("runtime.enoptrbss", 0) 1438 ldr.SetAttrSpecial(enoptrbss.Sym(), false) 1439 1440 noptrdata := ldr.CreateSymForUpdate("runtime.noptrdata", 0) 1441 noptrdata.SetSize(8) 1442 ldr.SetAttrSpecial(noptrdata.Sym(), false) 1443 1444 enoptrdata := ldr.CreateSymForUpdate("runtime.enoptrdata", 0) 1445 ldr.SetAttrSpecial(enoptrdata.Sym(), false) 1446 1447 types := ldr.CreateSymForUpdate("runtime.types", 0) 1448 types.SetType(sym.STYPE) 1449 types.SetSize(8) 1450 ldr.SetAttrSpecial(types.Sym(), false) 1451 1452 etypes := ldr.CreateSymForUpdate("runtime.etypes", 0) 1453 etypes.SetType(sym.SFUNCTAB) 1454 ldr.SetAttrSpecial(etypes.Sym(), false) 1455 1456 if ctxt.HeadType == objabi.Haix { 1457 rodata := ldr.CreateSymForUpdate("runtime.rodata", 0) 1458 rodata.SetType(sym.SSTRING) 1459 rodata.SetSize(8) 1460 ldr.SetAttrSpecial(rodata.Sym(), false) 1461 1462 erodata := ldr.CreateSymForUpdate("runtime.erodata", 0) 1463 ldr.SetAttrSpecial(erodata.Sym(), false) 1464 } 1465 } 1466 1467 // makeRelroForSharedLib creates a section of readonly data if necessary. 1468 func (state *dodataState) makeRelroForSharedLib(target *Link) { 1469 if !target.UseRelro() { 1470 return 1471 } 1472 1473 // "read only" data with relocations needs to go in its own section 1474 // when building a shared library. We do this by boosting objects of 1475 // type SXXX with relocations to type SXXXRELRO. 1476 ldr := target.loader 1477 for _, symnro := range sym.ReadOnly { 1478 symnrelro := sym.RelROMap[symnro] 1479 1480 ro := []loader.Sym{} 1481 relro := state.data[symnrelro] 1482 1483 for _, s := range state.data[symnro] { 1484 relocs := ldr.Relocs(s) 1485 isRelro := relocs.Count() > 0 1486 switch state.symType(s) { 1487 case sym.STYPE, sym.STYPERELRO, sym.SGOFUNCRELRO: 1488 // Symbols are not sorted yet, so it is possible 1489 // that an Outer symbol has been changed to a 1490 // relro Type before it reaches here. 1491 isRelro = true 1492 case sym.SFUNCTAB: 1493 if ldr.SymName(s) == "runtime.etypes" { 1494 // runtime.etypes must be at the end of 1495 // the relro data. 1496 isRelro = true 1497 } 1498 case sym.SGOFUNC: 1499 // The only SGOFUNC symbols that contain relocations are .stkobj, 1500 // and their relocations are of type objabi.R_ADDROFF, 1501 // which always get resolved during linking. 1502 isRelro = false 1503 } 1504 if isRelro { 1505 state.setSymType(s, symnrelro) 1506 if outer := ldr.OuterSym(s); outer != 0 { 1507 state.setSymType(outer, symnrelro) 1508 } 1509 relro = append(relro, s) 1510 } else { 1511 ro = append(ro, s) 1512 } 1513 } 1514 1515 // Check that we haven't made two symbols with the same .Outer into 1516 // different types (because references two symbols with non-nil Outer 1517 // become references to the outer symbol + offset it's vital that the 1518 // symbol and the outer end up in the same section). 1519 for _, s := range relro { 1520 if outer := ldr.OuterSym(s); outer != 0 { 1521 st := state.symType(s) 1522 ost := state.symType(outer) 1523 if st != ost { 1524 state.ctxt.Errorf(s, "inconsistent types for symbol and its Outer %s (%v != %v)", 1525 ldr.SymName(outer), st, ost) 1526 } 1527 } 1528 } 1529 1530 state.data[symnro] = ro 1531 state.data[symnrelro] = relro 1532 } 1533 } 1534 1535 // dodataState holds bits of state information needed by dodata() and the 1536 // various helpers it calls. The lifetime of these items should not extend 1537 // past the end of dodata(). 1538 type dodataState struct { 1539 // Link context 1540 ctxt *Link 1541 // Data symbols bucketed by type. 1542 data [sym.SXREF][]loader.Sym 1543 // Max alignment for each flavor of data symbol. 1544 dataMaxAlign [sym.SXREF]int32 1545 // Overridden sym type 1546 symGroupType []sym.SymKind 1547 // Current data size so far. 1548 datsize int64 1549 } 1550 1551 // A note on symType/setSymType below: 1552 // 1553 // In the legacy linker, the types of symbols (notably data symbols) are 1554 // changed during the symtab() phase so as to insure that similar symbols 1555 // are bucketed together, then their types are changed back again during 1556 // dodata. Symbol to section assignment also plays tricks along these lines 1557 // in the case where a relro segment is needed. 1558 // 1559 // The value returned from setType() below reflects the effects of 1560 // any overrides made by symtab and/or dodata. 1561 1562 // symType returns the (possibly overridden) type of 's'. 1563 func (state *dodataState) symType(s loader.Sym) sym.SymKind { 1564 if int(s) < len(state.symGroupType) { 1565 if override := state.symGroupType[s]; override != 0 { 1566 return override 1567 } 1568 } 1569 return state.ctxt.loader.SymType(s) 1570 } 1571 1572 // setSymType sets a new override type for 's'. 1573 func (state *dodataState) setSymType(s loader.Sym, kind sym.SymKind) { 1574 if s == 0 { 1575 panic("bad") 1576 } 1577 if int(s) < len(state.symGroupType) { 1578 state.symGroupType[s] = kind 1579 } else { 1580 su := state.ctxt.loader.MakeSymbolUpdater(s) 1581 su.SetType(kind) 1582 } 1583 } 1584 1585 func (ctxt *Link) dodata(symGroupType []sym.SymKind) { 1586 1587 // Give zeros sized symbols space if necessary. 1588 fixZeroSizedSymbols(ctxt) 1589 1590 // Collect data symbols by type into data. 1591 state := dodataState{ctxt: ctxt, symGroupType: symGroupType} 1592 ldr := ctxt.loader 1593 for s := loader.Sym(1); s < loader.Sym(ldr.NSym()); s++ { 1594 if !ldr.AttrReachable(s) || ldr.AttrSpecial(s) || ldr.AttrSubSymbol(s) || 1595 !ldr.TopLevelSym(s) { 1596 continue 1597 } 1598 1599 st := state.symType(s) 1600 1601 if st <= sym.STEXT || st >= sym.SXREF { 1602 continue 1603 } 1604 state.data[st] = append(state.data[st], s) 1605 1606 // Similarly with checking the onlist attr. 1607 if ldr.AttrOnList(s) { 1608 log.Fatalf("symbol %s listed multiple times", ldr.SymName(s)) 1609 } 1610 ldr.SetAttrOnList(s, true) 1611 } 1612 1613 // Now that we have the data symbols, but before we start 1614 // to assign addresses, record all the necessary 1615 // dynamic relocations. These will grow the relocation 1616 // symbol, which is itself data. 1617 // 1618 // On darwin, we need the symbol table numbers for dynreloc. 1619 if ctxt.HeadType == objabi.Hdarwin { 1620 machosymorder(ctxt) 1621 } 1622 state.dynreloc(ctxt) 1623 1624 // Move any RO data with relocations to a separate section. 1625 state.makeRelroForSharedLib(ctxt) 1626 1627 // Set alignment for the symbol with the largest known index, 1628 // so as to trigger allocation of the loader's internal 1629 // alignment array. This will avoid data races in the parallel 1630 // section below. 1631 lastSym := loader.Sym(ldr.NSym() - 1) 1632 ldr.SetSymAlign(lastSym, ldr.SymAlign(lastSym)) 1633 1634 // Sort symbols. 1635 var wg sync.WaitGroup 1636 for symn := range state.data { 1637 symn := sym.SymKind(symn) 1638 wg.Add(1) 1639 go func() { 1640 state.data[symn], state.dataMaxAlign[symn] = state.dodataSect(ctxt, symn, state.data[symn]) 1641 wg.Done() 1642 }() 1643 } 1644 wg.Wait() 1645 1646 if ctxt.IsELF { 1647 // Make .rela and .rela.plt contiguous, the ELF ABI requires this 1648 // and Solaris actually cares. 1649 syms := state.data[sym.SELFROSECT] 1650 reli, plti := -1, -1 1651 for i, s := range syms { 1652 switch ldr.SymName(s) { 1653 case ".rel.plt", ".rela.plt": 1654 plti = i 1655 case ".rel", ".rela": 1656 reli = i 1657 } 1658 } 1659 if reli >= 0 && plti >= 0 && plti != reli+1 { 1660 var first, second int 1661 if plti > reli { 1662 first, second = reli, plti 1663 } else { 1664 first, second = plti, reli 1665 } 1666 rel, plt := syms[reli], syms[plti] 1667 copy(syms[first+2:], syms[first+1:second]) 1668 syms[first+0] = rel 1669 syms[first+1] = plt 1670 1671 // Make sure alignment doesn't introduce a gap. 1672 // Setting the alignment explicitly prevents 1673 // symalign from basing it on the size and 1674 // getting it wrong. 1675 ldr.SetSymAlign(rel, int32(ctxt.Arch.RegSize)) 1676 ldr.SetSymAlign(plt, int32(ctxt.Arch.RegSize)) 1677 } 1678 state.data[sym.SELFROSECT] = syms 1679 } 1680 1681 if ctxt.HeadType == objabi.Haix && ctxt.LinkMode == LinkExternal { 1682 // These symbols must have the same alignment as their section. 1683 // Otherwise, ld might change the layout of Go sections. 1684 ldr.SetSymAlign(ldr.Lookup("runtime.data", 0), state.dataMaxAlign[sym.SDATA]) 1685 ldr.SetSymAlign(ldr.Lookup("runtime.bss", 0), state.dataMaxAlign[sym.SBSS]) 1686 } 1687 1688 // Create *sym.Section objects and assign symbols to sections for 1689 // data/rodata (and related) symbols. 1690 state.allocateDataSections(ctxt) 1691 1692 state.allocateSEHSections(ctxt) 1693 1694 // Create *sym.Section objects and assign symbols to sections for 1695 // DWARF symbols. 1696 state.allocateDwarfSections(ctxt) 1697 1698 /* number the sections */ 1699 n := int16(1) 1700 1701 for _, sect := range Segtext.Sections { 1702 sect.Extnum = n 1703 n++ 1704 } 1705 for _, sect := range Segrodata.Sections { 1706 sect.Extnum = n 1707 n++ 1708 } 1709 for _, sect := range Segrelrodata.Sections { 1710 sect.Extnum = n 1711 n++ 1712 } 1713 for _, sect := range Segdata.Sections { 1714 sect.Extnum = n 1715 n++ 1716 } 1717 for _, sect := range Segdwarf.Sections { 1718 sect.Extnum = n 1719 n++ 1720 } 1721 for _, sect := range Segpdata.Sections { 1722 sect.Extnum = n 1723 n++ 1724 } 1725 for _, sect := range Segxdata.Sections { 1726 sect.Extnum = n 1727 n++ 1728 } 1729 } 1730 1731 // allocateDataSectionForSym creates a new sym.Section into which a 1732 // single symbol will be placed. Here "seg" is the segment into which 1733 // the section will go, "s" is the symbol to be placed into the new 1734 // section, and "rwx" contains permissions for the section. 1735 func (state *dodataState) allocateDataSectionForSym(seg *sym.Segment, s loader.Sym, rwx int) *sym.Section { 1736 ldr := state.ctxt.loader 1737 sname := ldr.SymName(s) 1738 if strings.HasPrefix(sname, "go:") { 1739 sname = ".go." + sname[len("go:"):] 1740 } 1741 sect := addsection(ldr, state.ctxt.Arch, seg, sname, rwx) 1742 sect.Align = symalign(ldr, s) 1743 state.datsize = Rnd(state.datsize, int64(sect.Align)) 1744 sect.Vaddr = uint64(state.datsize) 1745 return sect 1746 } 1747 1748 // allocateNamedDataSection creates a new sym.Section for a category 1749 // of data symbols. Here "seg" is the segment into which the section 1750 // will go, "sName" is the name to give to the section, "types" is a 1751 // range of symbol types to be put into the section, and "rwx" 1752 // contains permissions for the section. 1753 func (state *dodataState) allocateNamedDataSection(seg *sym.Segment, sName string, types []sym.SymKind, rwx int) *sym.Section { 1754 sect := addsection(state.ctxt.loader, state.ctxt.Arch, seg, sName, rwx) 1755 if len(types) == 0 { 1756 sect.Align = 1 1757 } else if len(types) == 1 { 1758 sect.Align = state.dataMaxAlign[types[0]] 1759 } else { 1760 for _, symn := range types { 1761 align := state.dataMaxAlign[symn] 1762 if sect.Align < align { 1763 sect.Align = align 1764 } 1765 } 1766 } 1767 state.datsize = Rnd(state.datsize, int64(sect.Align)) 1768 sect.Vaddr = uint64(state.datsize) 1769 return sect 1770 } 1771 1772 // assignDsymsToSection assigns a collection of data symbols to a 1773 // newly created section. "sect" is the section into which to place 1774 // the symbols, "syms" holds the list of symbols to assign, 1775 // "forceType" (if non-zero) contains a new sym type to apply to each 1776 // sym during the assignment, and "aligner" is a hook to call to 1777 // handle alignment during the assignment process. 1778 func (state *dodataState) assignDsymsToSection(sect *sym.Section, syms []loader.Sym, forceType sym.SymKind, aligner func(state *dodataState, datsize int64, s loader.Sym) int64) { 1779 ldr := state.ctxt.loader 1780 for _, s := range syms { 1781 state.datsize = aligner(state, state.datsize, s) 1782 ldr.SetSymSect(s, sect) 1783 if forceType != sym.Sxxx { 1784 state.setSymType(s, forceType) 1785 } 1786 ldr.SetSymValue(s, int64(uint64(state.datsize)-sect.Vaddr)) 1787 state.datsize += ldr.SymSize(s) 1788 } 1789 sect.Length = uint64(state.datsize) - sect.Vaddr 1790 } 1791 1792 func (state *dodataState) assignToSection(sect *sym.Section, symn sym.SymKind, forceType sym.SymKind) { 1793 state.assignDsymsToSection(sect, state.data[symn], forceType, aligndatsize) 1794 state.checkdatsize(symn) 1795 } 1796 1797 // allocateSingleSymSections walks through the bucketed data symbols 1798 // with type 'symn', creates a new section for each sym, and assigns 1799 // the sym to a newly created section. Section name is set from the 1800 // symbol name. "Seg" is the segment into which to place the new 1801 // section, "forceType" is the new sym.SymKind to assign to the symbol 1802 // within the section, and "rwx" holds section permissions. 1803 func (state *dodataState) allocateSingleSymSections(seg *sym.Segment, symn sym.SymKind, forceType sym.SymKind, rwx int) { 1804 ldr := state.ctxt.loader 1805 for _, s := range state.data[symn] { 1806 sect := state.allocateDataSectionForSym(seg, s, rwx) 1807 ldr.SetSymSect(s, sect) 1808 state.setSymType(s, forceType) 1809 ldr.SetSymValue(s, int64(uint64(state.datsize)-sect.Vaddr)) 1810 state.datsize += ldr.SymSize(s) 1811 sect.Length = uint64(state.datsize) - sect.Vaddr 1812 } 1813 state.checkdatsize(symn) 1814 } 1815 1816 // allocateNamedSectionAndAssignSyms creates a new section with the 1817 // specified name, then walks through the bucketed data symbols with 1818 // type 'symn' and assigns each of them to this new section. "Seg" is 1819 // the segment into which to place the new section, "secName" is the 1820 // name to give to the new section, "forceType" (if non-zero) contains 1821 // a new sym type to apply to each sym during the assignment, and 1822 // "rwx" holds section permissions. 1823 func (state *dodataState) allocateNamedSectionAndAssignSyms(seg *sym.Segment, secName string, symn sym.SymKind, forceType sym.SymKind, rwx int) *sym.Section { 1824 1825 sect := state.allocateNamedDataSection(seg, secName, []sym.SymKind{symn}, rwx) 1826 state.assignDsymsToSection(sect, state.data[symn], forceType, aligndatsize) 1827 return sect 1828 } 1829 1830 // allocateDataSections allocates sym.Section objects for data/rodata 1831 // (and related) symbols, and then assigns symbols to those sections. 1832 func (state *dodataState) allocateDataSections(ctxt *Link) { 1833 // Allocate sections. 1834 // Data is processed before segtext, because we need 1835 // to see all symbols in the .data and .bss sections in order 1836 // to generate garbage collection information. 1837 1838 // Writable data sections that do not need any specialized handling. 1839 writable := []sym.SymKind{ 1840 sym.SBUILDINFO, 1841 sym.SELFSECT, 1842 sym.SMACHO, 1843 sym.SMACHOGOT, 1844 sym.SWINDOWS, 1845 } 1846 for _, symn := range writable { 1847 state.allocateSingleSymSections(&Segdata, symn, sym.SDATA, 06) 1848 } 1849 ldr := ctxt.loader 1850 1851 // .got 1852 if len(state.data[sym.SELFGOT]) > 0 { 1853 state.allocateNamedSectionAndAssignSyms(&Segdata, ".got", sym.SELFGOT, sym.SDATA, 06) 1854 } 1855 1856 /* pointer-free data */ 1857 sect := state.allocateNamedSectionAndAssignSyms(&Segdata, ".noptrdata", sym.SNOPTRDATA, sym.SDATA, 06) 1858 ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.noptrdata", 0), sect) 1859 ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.enoptrdata", 0), sect) 1860 1861 hasinitarr := ctxt.linkShared 1862 1863 /* shared library initializer */ 1864 switch ctxt.BuildMode { 1865 case BuildModeCArchive, BuildModeCShared, BuildModeShared, BuildModePlugin: 1866 hasinitarr = true 1867 } 1868 1869 if ctxt.HeadType == objabi.Haix { 1870 if len(state.data[sym.SINITARR]) > 0 { 1871 Errorf(nil, "XCOFF format doesn't allow .init_array section") 1872 } 1873 } 1874 1875 if hasinitarr && len(state.data[sym.SINITARR]) > 0 { 1876 state.allocateNamedSectionAndAssignSyms(&Segdata, ".init_array", sym.SINITARR, sym.Sxxx, 06) 1877 } 1878 1879 /* data */ 1880 sect = state.allocateNamedSectionAndAssignSyms(&Segdata, ".data", sym.SDATA, sym.SDATA, 06) 1881 ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.data", 0), sect) 1882 ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.edata", 0), sect) 1883 dataGcEnd := state.datsize - int64(sect.Vaddr) 1884 1885 // On AIX, TOC entries must be the last of .data 1886 // These aren't part of gc as they won't change during the runtime. 1887 state.assignToSection(sect, sym.SXCOFFTOC, sym.SDATA) 1888 state.checkdatsize(sym.SDATA) 1889 sect.Length = uint64(state.datsize) - sect.Vaddr 1890 1891 /* bss */ 1892 sect = state.allocateNamedSectionAndAssignSyms(&Segdata, ".bss", sym.SBSS, sym.Sxxx, 06) 1893 ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.bss", 0), sect) 1894 ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.ebss", 0), sect) 1895 bssGcEnd := state.datsize - int64(sect.Vaddr) 1896 1897 // Emit gcdata for bss symbols now that symbol values have been assigned. 1898 gcsToEmit := []struct { 1899 symName string 1900 symKind sym.SymKind 1901 gcEnd int64 1902 }{ 1903 {"runtime.gcdata", sym.SDATA, dataGcEnd}, 1904 {"runtime.gcbss", sym.SBSS, bssGcEnd}, 1905 } 1906 for _, g := range gcsToEmit { 1907 var gc GCProg 1908 gc.Init(ctxt, g.symName) 1909 for _, s := range state.data[g.symKind] { 1910 gc.AddSym(s) 1911 } 1912 gc.End(g.gcEnd) 1913 } 1914 1915 /* pointer-free bss */ 1916 sect = state.allocateNamedSectionAndAssignSyms(&Segdata, ".noptrbss", sym.SNOPTRBSS, sym.Sxxx, 06) 1917 ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.noptrbss", 0), sect) 1918 ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.enoptrbss", 0), sect) 1919 ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.end", 0), sect) 1920 1921 // Code coverage counters are assigned to the .noptrbss section. 1922 // We assign them in a separate pass so that they stay aggregated 1923 // together in a single blob (coverage runtime depends on this). 1924 covCounterDataStartOff = sect.Length 1925 state.assignToSection(sect, sym.SCOVERAGE_COUNTER, sym.SNOPTRBSS) 1926 covCounterDataLen = sect.Length - covCounterDataStartOff 1927 ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.covctrs", 0), sect) 1928 ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.ecovctrs", 0), sect) 1929 1930 // Coverage instrumentation counters for libfuzzer. 1931 if len(state.data[sym.SLIBFUZZER_8BIT_COUNTER]) > 0 { 1932 sect := state.allocateNamedSectionAndAssignSyms(&Segdata, ".go.fuzzcntrs", sym.SLIBFUZZER_8BIT_COUNTER, sym.Sxxx, 06) 1933 ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.__start___sancov_cntrs", 0), sect) 1934 ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.__stop___sancov_cntrs", 0), sect) 1935 ldr.SetSymSect(ldr.LookupOrCreateSym("github.com/go-asm/go/fuzz._counters", 0), sect) 1936 ldr.SetSymSect(ldr.LookupOrCreateSym("github.com/go-asm/go/fuzz._ecounters", 0), sect) 1937 } 1938 1939 if len(state.data[sym.STLSBSS]) > 0 { 1940 var sect *sym.Section 1941 // FIXME: not clear why it is sometimes necessary to suppress .tbss section creation. 1942 if (ctxt.IsELF || ctxt.HeadType == objabi.Haix) && (ctxt.LinkMode == LinkExternal || !*FlagD) { 1943 sect = addsection(ldr, ctxt.Arch, &Segdata, ".tbss", 06) 1944 sect.Align = int32(ctxt.Arch.PtrSize) 1945 // FIXME: why does this need to be set to zero? 1946 sect.Vaddr = 0 1947 } 1948 state.datsize = 0 1949 1950 for _, s := range state.data[sym.STLSBSS] { 1951 state.datsize = aligndatsize(state, state.datsize, s) 1952 if sect != nil { 1953 ldr.SetSymSect(s, sect) 1954 } 1955 ldr.SetSymValue(s, state.datsize) 1956 state.datsize += ldr.SymSize(s) 1957 } 1958 state.checkdatsize(sym.STLSBSS) 1959 1960 if sect != nil { 1961 sect.Length = uint64(state.datsize) 1962 } 1963 } 1964 1965 /* 1966 * We finished data, begin read-only data. 1967 * Not all systems support a separate read-only non-executable data section. 1968 * ELF and Windows PE systems do. 1969 * OS X and Plan 9 do not. 1970 * And if we're using external linking mode, the point is moot, 1971 * since it's not our decision; that code expects the sections in 1972 * segtext. 1973 */ 1974 var segro *sym.Segment 1975 if ctxt.IsELF && ctxt.LinkMode == LinkInternal { 1976 segro = &Segrodata 1977 } else if ctxt.HeadType == objabi.Hwindows { 1978 segro = &Segrodata 1979 } else { 1980 segro = &Segtext 1981 } 1982 1983 state.datsize = 0 1984 1985 /* read-only executable ELF, Mach-O sections */ 1986 if len(state.data[sym.STEXT]) != 0 { 1987 culprit := ldr.SymName(state.data[sym.STEXT][0]) 1988 Errorf(nil, "dodata found an sym.STEXT symbol: %s", culprit) 1989 } 1990 state.allocateSingleSymSections(&Segtext, sym.SELFRXSECT, sym.SRODATA, 05) 1991 state.allocateSingleSymSections(&Segtext, sym.SMACHOPLT, sym.SRODATA, 05) 1992 1993 /* read-only data */ 1994 sect = state.allocateNamedDataSection(segro, ".rodata", sym.ReadOnly, 04) 1995 ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.rodata", 0), sect) 1996 ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.erodata", 0), sect) 1997 if !ctxt.UseRelro() { 1998 ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.types", 0), sect) 1999 ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.etypes", 0), sect) 2000 } 2001 for _, symn := range sym.ReadOnly { 2002 symnStartValue := state.datsize 2003 if len(state.data[symn]) != 0 { 2004 symnStartValue = aligndatsize(state, symnStartValue, state.data[symn][0]) 2005 } 2006 state.assignToSection(sect, symn, sym.SRODATA) 2007 setCarrierSize(symn, state.datsize-symnStartValue) 2008 if ctxt.HeadType == objabi.Haix { 2009 // Read-only symbols might be wrapped inside their outer 2010 // symbol. 2011 // XCOFF symbol table needs to know the size of 2012 // these outer symbols. 2013 xcoffUpdateOuterSize(ctxt, state.datsize-symnStartValue, symn) 2014 } 2015 } 2016 2017 /* read-only ELF, Mach-O sections */ 2018 state.allocateSingleSymSections(segro, sym.SELFROSECT, sym.SRODATA, 04) 2019 2020 // There is some data that are conceptually read-only but are written to by 2021 // relocations. On GNU systems, we can arrange for the dynamic linker to 2022 // mprotect sections after relocations are applied by giving them write 2023 // permissions in the object file and calling them ".data.rel.ro.FOO". We 2024 // divide the .rodata section between actual .rodata and .data.rel.ro.rodata, 2025 // but for the other sections that this applies to, we just write a read-only 2026 // .FOO section or a read-write .data.rel.ro.FOO section depending on the 2027 // situation. 2028 // TODO(mwhudson): It would make sense to do this more widely, but it makes 2029 // the system linker segfault on darwin. 2030 const relroPerm = 06 2031 const fallbackPerm = 04 2032 relroSecPerm := fallbackPerm 2033 genrelrosecname := func(suffix string) string { 2034 if suffix == "" { 2035 return ".rodata" 2036 } 2037 return suffix 2038 } 2039 seg := segro 2040 2041 if ctxt.UseRelro() { 2042 segrelro := &Segrelrodata 2043 if ctxt.LinkMode == LinkExternal && !ctxt.IsAIX() && !ctxt.IsDarwin() { 2044 // Using a separate segment with an external 2045 // linker results in some programs moving 2046 // their data sections unexpectedly, which 2047 // corrupts the moduledata. So we use the 2048 // rodata segment and let the external linker 2049 // sort out a rel.ro segment. 2050 segrelro = segro 2051 } else { 2052 // Reset datsize for new segment. 2053 state.datsize = 0 2054 } 2055 2056 if !ctxt.IsDarwin() { // We don't need the special names on darwin. 2057 genrelrosecname = func(suffix string) string { 2058 return ".data.rel.ro" + suffix 2059 } 2060 } 2061 2062 relroReadOnly := []sym.SymKind{} 2063 for _, symnro := range sym.ReadOnly { 2064 symn := sym.RelROMap[symnro] 2065 relroReadOnly = append(relroReadOnly, symn) 2066 } 2067 seg = segrelro 2068 relroSecPerm = relroPerm 2069 2070 /* data only written by relocations */ 2071 sect = state.allocateNamedDataSection(segrelro, genrelrosecname(""), relroReadOnly, relroSecPerm) 2072 2073 ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.types", 0), sect) 2074 ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.etypes", 0), sect) 2075 2076 for i, symnro := range sym.ReadOnly { 2077 if i == 0 && symnro == sym.STYPE && ctxt.HeadType != objabi.Haix { 2078 // Skip forward so that no type 2079 // reference uses a zero offset. 2080 // This is unlikely but possible in small 2081 // programs with no other read-only data. 2082 state.datsize++ 2083 } 2084 2085 symn := sym.RelROMap[symnro] 2086 symnStartValue := state.datsize 2087 if len(state.data[symn]) != 0 { 2088 symnStartValue = aligndatsize(state, symnStartValue, state.data[symn][0]) 2089 } 2090 2091 for _, s := range state.data[symn] { 2092 outer := ldr.OuterSym(s) 2093 if s != 0 && ldr.SymSect(outer) != nil && ldr.SymSect(outer) != sect { 2094 ctxt.Errorf(s, "s.Outer (%s) in different section from s, %s != %s", ldr.SymName(outer), ldr.SymSect(outer).Name, sect.Name) 2095 } 2096 } 2097 state.assignToSection(sect, symn, sym.SRODATA) 2098 setCarrierSize(symn, state.datsize-symnStartValue) 2099 if ctxt.HeadType == objabi.Haix { 2100 // Read-only symbols might be wrapped inside their outer 2101 // symbol. 2102 // XCOFF symbol table needs to know the size of 2103 // these outer symbols. 2104 xcoffUpdateOuterSize(ctxt, state.datsize-symnStartValue, symn) 2105 } 2106 } 2107 2108 sect.Length = uint64(state.datsize) - sect.Vaddr 2109 } 2110 2111 /* typelink */ 2112 sect = state.allocateNamedDataSection(seg, genrelrosecname(".typelink"), []sym.SymKind{sym.STYPELINK}, relroSecPerm) 2113 2114 typelink := ldr.CreateSymForUpdate("runtime.typelink", 0) 2115 ldr.SetSymSect(typelink.Sym(), sect) 2116 typelink.SetType(sym.SRODATA) 2117 state.datsize += typelink.Size() 2118 state.checkdatsize(sym.STYPELINK) 2119 sect.Length = uint64(state.datsize) - sect.Vaddr 2120 2121 /* itablink */ 2122 sect = state.allocateNamedDataSection(seg, genrelrosecname(".itablink"), []sym.SymKind{sym.SITABLINK}, relroSecPerm) 2123 2124 itablink := ldr.CreateSymForUpdate("runtime.itablink", 0) 2125 ldr.SetSymSect(itablink.Sym(), sect) 2126 itablink.SetType(sym.SRODATA) 2127 state.datsize += itablink.Size() 2128 state.checkdatsize(sym.SITABLINK) 2129 sect.Length = uint64(state.datsize) - sect.Vaddr 2130 2131 /* gosymtab */ 2132 sect = state.allocateNamedSectionAndAssignSyms(seg, genrelrosecname(".gosymtab"), sym.SSYMTAB, sym.SRODATA, relroSecPerm) 2133 ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.symtab", 0), sect) 2134 ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.esymtab", 0), sect) 2135 2136 /* gopclntab */ 2137 sect = state.allocateNamedSectionAndAssignSyms(seg, genrelrosecname(".gopclntab"), sym.SPCLNTAB, sym.SRODATA, relroSecPerm) 2138 ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.pclntab", 0), sect) 2139 ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.pcheader", 0), sect) 2140 ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.funcnametab", 0), sect) 2141 ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.cutab", 0), sect) 2142 ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.filetab", 0), sect) 2143 ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.pctab", 0), sect) 2144 ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.functab", 0), sect) 2145 ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.epclntab", 0), sect) 2146 setCarrierSize(sym.SPCLNTAB, int64(sect.Length)) 2147 if ctxt.HeadType == objabi.Haix { 2148 xcoffUpdateOuterSize(ctxt, int64(sect.Length), sym.SPCLNTAB) 2149 } 2150 2151 // 6g uses 4-byte relocation offsets, so the entire segment must fit in 32 bits. 2152 if state.datsize != int64(uint32(state.datsize)) { 2153 Errorf(nil, "read-only data segment too large: %d", state.datsize) 2154 } 2155 2156 siz := 0 2157 for symn := sym.SELFRXSECT; symn < sym.SXREF; symn++ { 2158 siz += len(state.data[symn]) 2159 } 2160 ctxt.datap = make([]loader.Sym, 0, siz) 2161 for symn := sym.SELFRXSECT; symn < sym.SXREF; symn++ { 2162 ctxt.datap = append(ctxt.datap, state.data[symn]...) 2163 } 2164 } 2165 2166 // allocateDwarfSections allocates sym.Section objects for DWARF 2167 // symbols, and assigns symbols to sections. 2168 func (state *dodataState) allocateDwarfSections(ctxt *Link) { 2169 2170 alignOne := func(state *dodataState, datsize int64, s loader.Sym) int64 { return datsize } 2171 2172 ldr := ctxt.loader 2173 for i := 0; i < len(dwarfp); i++ { 2174 // First the section symbol. 2175 s := dwarfp[i].secSym() 2176 sect := state.allocateNamedDataSection(&Segdwarf, ldr.SymName(s), []sym.SymKind{}, 04) 2177 ldr.SetSymSect(s, sect) 2178 sect.Sym = sym.LoaderSym(s) 2179 curType := ldr.SymType(s) 2180 state.setSymType(s, sym.SRODATA) 2181 ldr.SetSymValue(s, int64(uint64(state.datsize)-sect.Vaddr)) 2182 state.datsize += ldr.SymSize(s) 2183 2184 // Then any sub-symbols for the section symbol. 2185 subSyms := dwarfp[i].subSyms() 2186 state.assignDsymsToSection(sect, subSyms, sym.SRODATA, alignOne) 2187 2188 for j := 0; j < len(subSyms); j++ { 2189 s := subSyms[j] 2190 if ctxt.HeadType == objabi.Haix && curType == sym.SDWARFLOC { 2191 // Update the size of .debug_loc for this symbol's 2192 // package. 2193 addDwsectCUSize(".debug_loc", ldr.SymPkg(s), uint64(ldr.SymSize(s))) 2194 } 2195 } 2196 sect.Length = uint64(state.datsize) - sect.Vaddr 2197 checkSectSize(sect) 2198 } 2199 } 2200 2201 // allocateSEHSections allocate a sym.Section object for SEH 2202 // symbols, and assigns symbols to sections. 2203 func (state *dodataState) allocateSEHSections(ctxt *Link) { 2204 if len(sehp.pdata) > 0 { 2205 sect := state.allocateNamedDataSection(&Segpdata, ".pdata", []sym.SymKind{}, 04) 2206 state.assignDsymsToSection(sect, sehp.pdata, sym.SRODATA, aligndatsize) 2207 state.checkdatsize(sym.SSEHSECT) 2208 } 2209 if len(sehp.xdata) > 0 { 2210 sect := state.allocateNamedDataSection(&Segxdata, ".xdata", []sym.SymKind{}, 04) 2211 state.assignDsymsToSection(sect, sehp.xdata, sym.SRODATA, aligndatsize) 2212 state.checkdatsize(sym.SSEHSECT) 2213 } 2214 } 2215 2216 type symNameSize struct { 2217 name string 2218 sz int64 2219 val int64 2220 sym loader.Sym 2221 } 2222 2223 func (state *dodataState) dodataSect(ctxt *Link, symn sym.SymKind, syms []loader.Sym) (result []loader.Sym, maxAlign int32) { 2224 var head, tail, zerobase loader.Sym 2225 ldr := ctxt.loader 2226 sl := make([]symNameSize, len(syms)) 2227 2228 // For ppc64, we want to interleave the .got and .toc sections 2229 // from input files. Both are type sym.SELFGOT, so in that case 2230 // we skip size comparison and do the name comparison instead 2231 // (conveniently, .got sorts before .toc). 2232 checkSize := symn != sym.SELFGOT 2233 2234 for k, s := range syms { 2235 ss := ldr.SymSize(s) 2236 sl[k] = symNameSize{sz: ss, sym: s} 2237 if !checkSize { 2238 sl[k].name = ldr.SymName(s) 2239 } 2240 ds := int64(len(ldr.Data(s))) 2241 switch { 2242 case ss < ds: 2243 ctxt.Errorf(s, "initialize bounds (%d < %d)", ss, ds) 2244 case ss < 0: 2245 ctxt.Errorf(s, "negative size (%d bytes)", ss) 2246 case ss > cutoff: 2247 ctxt.Errorf(s, "symbol too large (%d bytes)", ss) 2248 } 2249 2250 // If the usually-special section-marker symbols are being laid 2251 // out as regular symbols, put them either at the beginning or 2252 // end of their section. 2253 if (ctxt.DynlinkingGo() && ctxt.HeadType == objabi.Hdarwin) || (ctxt.HeadType == objabi.Haix && ctxt.LinkMode == LinkExternal) { 2254 switch ldr.SymName(s) { 2255 case "runtime.text", "runtime.bss", "runtime.data", "runtime.types", "runtime.rodata", 2256 "runtime.noptrdata", "runtime.noptrbss": 2257 head = s 2258 continue 2259 case "runtime.etext", "runtime.ebss", "runtime.edata", "runtime.etypes", "runtime.erodata", 2260 "runtime.enoptrdata", "runtime.enoptrbss": 2261 tail = s 2262 continue 2263 } 2264 } 2265 } 2266 zerobase = ldr.Lookup("runtime.zerobase", 0) 2267 2268 // Perform the sort. 2269 if symn != sym.SPCLNTAB { 2270 sort.Slice(sl, func(i, j int) bool { 2271 si, sj := sl[i].sym, sl[j].sym 2272 isz, jsz := sl[i].sz, sl[j].sz 2273 switch { 2274 case si == head, sj == tail: 2275 return true 2276 case sj == head, si == tail: 2277 return false 2278 // put zerobase right after all the zero-sized symbols, 2279 // so zero-sized symbols have the same address as zerobase. 2280 case si == zerobase: 2281 return jsz != 0 // zerobase < nonzero-sized 2282 case sj == zerobase: 2283 return isz == 0 // 0-sized < zerobase 2284 } 2285 if checkSize { 2286 if isz != jsz { 2287 return isz < jsz 2288 } 2289 } else { 2290 iname := sl[i].name 2291 jname := sl[j].name 2292 if iname != jname { 2293 return iname < jname 2294 } 2295 } 2296 return si < sj 2297 }) 2298 } else { 2299 // PCLNTAB was built internally, and already has the proper order. 2300 } 2301 2302 // Set alignment, construct result 2303 syms = syms[:0] 2304 for k := range sl { 2305 s := sl[k].sym 2306 if s != head && s != tail { 2307 align := symalign(ldr, s) 2308 if maxAlign < align { 2309 maxAlign = align 2310 } 2311 } 2312 syms = append(syms, s) 2313 } 2314 2315 return syms, maxAlign 2316 } 2317 2318 // Add buildid to beginning of text segment, on non-ELF systems. 2319 // Non-ELF binary formats are not always flexible enough to 2320 // give us a place to put the Go build ID. On those systems, we put it 2321 // at the very beginning of the text segment. 2322 // This “header” is read by cmd/go. 2323 func (ctxt *Link) textbuildid() { 2324 if ctxt.IsELF || *flagBuildid == "" { 2325 return 2326 } 2327 2328 ldr := ctxt.loader 2329 s := ldr.CreateSymForUpdate("go:buildid", 0) 2330 // The \xff is invalid UTF-8, meant to make it less likely 2331 // to find one of these accidentally. 2332 data := "\xff Go build ID: " + strconv.Quote(*flagBuildid) + "\n \xff" 2333 s.SetType(sym.STEXT) 2334 s.SetData([]byte(data)) 2335 s.SetSize(int64(len(data))) 2336 2337 ctxt.Textp = append(ctxt.Textp, 0) 2338 copy(ctxt.Textp[1:], ctxt.Textp) 2339 ctxt.Textp[0] = s.Sym() 2340 } 2341 2342 func (ctxt *Link) buildinfo() { 2343 // Write the buildinfo symbol, which go version looks for. 2344 // The code reading this data is in package debug/buildinfo. 2345 ldr := ctxt.loader 2346 s := ldr.CreateSymForUpdate("go:buildinfo", 0) 2347 s.SetType(sym.SBUILDINFO) 2348 s.SetAlign(16) 2349 // The \xff is invalid UTF-8, meant to make it less likely 2350 // to find one of these accidentally. 2351 const prefix = "\xff Go buildinf:" // 14 bytes, plus 2 data bytes filled in below 2352 data := make([]byte, 32) 2353 copy(data, prefix) 2354 data[len(prefix)] = byte(ctxt.Arch.PtrSize) 2355 data[len(prefix)+1] = 0 2356 if ctxt.Arch.ByteOrder == binary.BigEndian { 2357 data[len(prefix)+1] = 1 2358 } 2359 data[len(prefix)+1] |= 2 // signals new pointer-free format 2360 data = appendString(data, strdata["runtime.buildVersion"]) 2361 data = appendString(data, strdata["runtime.modinfo"]) 2362 // MacOS linker gets very upset if the size os not a multiple of alignment. 2363 for len(data)%16 != 0 { 2364 data = append(data, 0) 2365 } 2366 s.SetData(data) 2367 s.SetSize(int64(len(data))) 2368 2369 // Add reference to go:buildinfo from the rodata section, 2370 // so that external linking with -Wl,--gc-sections does not 2371 // delete the build info. 2372 sr := ldr.CreateSymForUpdate("go:buildinfo.ref", 0) 2373 sr.SetType(sym.SRODATA) 2374 sr.SetAlign(int32(ctxt.Arch.PtrSize)) 2375 sr.AddAddr(ctxt.Arch, s.Sym()) 2376 } 2377 2378 // appendString appends s to data, prefixed by its varint-encoded length. 2379 func appendString(data []byte, s string) []byte { 2380 var v [binary.MaxVarintLen64]byte 2381 n := binary.PutUvarint(v[:], uint64(len(s))) 2382 data = append(data, v[:n]...) 2383 data = append(data, s...) 2384 return data 2385 } 2386 2387 // assign addresses to text 2388 func (ctxt *Link) textaddress() { 2389 addsection(ctxt.loader, ctxt.Arch, &Segtext, ".text", 05) 2390 2391 // Assign PCs in text segment. 2392 // Could parallelize, by assigning to text 2393 // and then letting threads copy down, but probably not worth it. 2394 sect := Segtext.Sections[0] 2395 2396 sect.Align = int32(Funcalign) 2397 2398 ldr := ctxt.loader 2399 2400 text := ctxt.xdefine("runtime.text", sym.STEXT, 0) 2401 etext := ctxt.xdefine("runtime.etext", sym.STEXT, 0) 2402 ldr.SetSymSect(text, sect) 2403 if ctxt.IsAIX() && ctxt.IsExternal() { 2404 // Setting runtime.text has a real symbol prevents ld to 2405 // change its base address resulting in wrong offsets for 2406 // reflect methods. 2407 u := ldr.MakeSymbolUpdater(text) 2408 u.SetAlign(sect.Align) 2409 u.SetSize(8) 2410 } 2411 2412 if (ctxt.DynlinkingGo() && ctxt.IsDarwin()) || (ctxt.IsAIX() && ctxt.IsExternal()) { 2413 ldr.SetSymSect(etext, sect) 2414 ctxt.Textp = append(ctxt.Textp, etext, 0) 2415 copy(ctxt.Textp[1:], ctxt.Textp) 2416 ctxt.Textp[0] = text 2417 } 2418 2419 start := uint64(Rnd(*FlagTextAddr, int64(Funcalign))) 2420 va := start 2421 n := 1 2422 sect.Vaddr = va 2423 2424 limit := thearch.TrampLimit 2425 if limit == 0 { 2426 limit = 1 << 63 // unlimited 2427 } 2428 if *FlagDebugTextSize != 0 { 2429 limit = uint64(*FlagDebugTextSize) 2430 } 2431 if *FlagDebugTramp > 1 { 2432 limit = 1 // debug mode, force generating trampolines for everything 2433 } 2434 2435 if ctxt.IsAIX() && ctxt.IsExternal() { 2436 // On AIX, normally we won't generate direct calls to external symbols, 2437 // except in one test, cmd/go/testdata/script/link_syso_issue33139.txt. 2438 // That test doesn't make much sense, and I'm not sure it ever works. 2439 // Just generate trampoline for now (which will turn a direct call to 2440 // an indirect call, which at least builds). 2441 limit = 1 2442 } 2443 2444 // First pass: assign addresses assuming the program is small and will 2445 // not require trampoline generation. 2446 big := false 2447 for _, s := range ctxt.Textp { 2448 sect, n, va = assignAddress(ctxt, sect, n, s, va, false, big) 2449 if va-start >= limit { 2450 big = true 2451 break 2452 } 2453 } 2454 2455 // Second pass: only if it is too big, insert trampolines for too-far 2456 // jumps and targets with unknown addresses. 2457 if big { 2458 // reset addresses 2459 for _, s := range ctxt.Textp { 2460 if s != text { 2461 resetAddress(ctxt, s) 2462 } 2463 } 2464 va = start 2465 2466 ntramps := 0 2467 var curPkg string 2468 for i, s := range ctxt.Textp { 2469 // When we find the first symbol in a package, perform a 2470 // single iteration that assigns temporary addresses to all 2471 // of the text in the same package, using the maximum possible 2472 // number of trampolines. This allows for better decisions to 2473 // be made regarding reachability and the need for trampolines. 2474 if symPkg := ldr.SymPkg(s); symPkg != "" && curPkg != symPkg { 2475 curPkg = symPkg 2476 vaTmp := va 2477 for j := i; j < len(ctxt.Textp); j++ { 2478 curSym := ctxt.Textp[j] 2479 if symPkg := ldr.SymPkg(curSym); symPkg == "" || curPkg != symPkg { 2480 break 2481 } 2482 // We do not pass big to assignAddress here, as this 2483 // can result in side effects such as section splitting. 2484 sect, n, vaTmp = assignAddress(ctxt, sect, n, curSym, vaTmp, false, false) 2485 vaTmp += maxSizeTrampolines(ctxt, ldr, curSym, false) 2486 } 2487 } 2488 2489 // Reset address for current symbol. 2490 if s != text { 2491 resetAddress(ctxt, s) 2492 } 2493 2494 // Assign actual address for current symbol. 2495 sect, n, va = assignAddress(ctxt, sect, n, s, va, false, big) 2496 2497 // Resolve jumps, adding trampolines if they are needed. 2498 trampoline(ctxt, s) 2499 2500 // lay down trampolines after each function 2501 for ; ntramps < len(ctxt.tramps); ntramps++ { 2502 tramp := ctxt.tramps[ntramps] 2503 if ctxt.IsAIX() && strings.HasPrefix(ldr.SymName(tramp), "runtime.text.") { 2504 // Already set in assignAddress 2505 continue 2506 } 2507 sect, n, va = assignAddress(ctxt, sect, n, tramp, va, true, big) 2508 } 2509 } 2510 2511 // merge tramps into Textp, keeping Textp in address order 2512 if ntramps != 0 { 2513 newtextp := make([]loader.Sym, 0, len(ctxt.Textp)+ntramps) 2514 i := 0 2515 for _, s := range ctxt.Textp { 2516 for ; i < ntramps && ldr.SymValue(ctxt.tramps[i]) < ldr.SymValue(s); i++ { 2517 newtextp = append(newtextp, ctxt.tramps[i]) 2518 } 2519 newtextp = append(newtextp, s) 2520 } 2521 newtextp = append(newtextp, ctxt.tramps[i:ntramps]...) 2522 2523 ctxt.Textp = newtextp 2524 } 2525 } 2526 2527 // Add MinLC size after etext, so it won't collide with the next symbol 2528 // (which may confuse some symbolizer). 2529 sect.Length = va - sect.Vaddr + uint64(ctxt.Arch.MinLC) 2530 ldr.SetSymSect(etext, sect) 2531 if ldr.SymValue(etext) == 0 { 2532 // Set the address of the start/end symbols, if not already 2533 // (i.e. not darwin+dynlink or AIX+external, see above). 2534 ldr.SetSymValue(etext, int64(va)) 2535 ldr.SetSymValue(text, int64(Segtext.Sections[0].Vaddr)) 2536 } 2537 } 2538 2539 // assigns address for a text symbol, returns (possibly new) section, its number, and the address. 2540 func assignAddress(ctxt *Link, sect *sym.Section, n int, s loader.Sym, va uint64, isTramp, big bool) (*sym.Section, int, uint64) { 2541 ldr := ctxt.loader 2542 if thearch.AssignAddress != nil { 2543 return thearch.AssignAddress(ldr, sect, n, s, va, isTramp) 2544 } 2545 2546 ldr.SetSymSect(s, sect) 2547 if ldr.AttrSubSymbol(s) { 2548 return sect, n, va 2549 } 2550 2551 align := ldr.SymAlign(s) 2552 if align == 0 { 2553 align = int32(Funcalign) 2554 } 2555 va = uint64(Rnd(int64(va), int64(align))) 2556 if sect.Align < align { 2557 sect.Align = align 2558 } 2559 2560 funcsize := uint64(MINFUNC) // spacing required for findfunctab 2561 if ldr.SymSize(s) > MINFUNC { 2562 funcsize = uint64(ldr.SymSize(s)) 2563 } 2564 2565 // If we need to split text sections, and this function doesn't fit in the current 2566 // section, then create a new one. 2567 // 2568 // Only break at outermost syms. 2569 if big && splitTextSections(ctxt) && ldr.OuterSym(s) == 0 { 2570 // For debugging purposes, allow text size limit to be cranked down, 2571 // so as to stress test the code that handles multiple text sections. 2572 var textSizelimit uint64 = thearch.TrampLimit 2573 if *FlagDebugTextSize != 0 { 2574 textSizelimit = uint64(*FlagDebugTextSize) 2575 } 2576 2577 // Sanity check: make sure the limit is larger than any 2578 // individual text symbol. 2579 if funcsize > textSizelimit { 2580 panic(fmt.Sprintf("error: text size limit %d less than text symbol %s size of %d", textSizelimit, ldr.SymName(s), funcsize)) 2581 } 2582 2583 if va-sect.Vaddr+funcsize+maxSizeTrampolines(ctxt, ldr, s, isTramp) > textSizelimit { 2584 sectAlign := int32(thearch.Funcalign) 2585 if ctxt.IsPPC64() { 2586 // Align the next text section to the worst case function alignment likely 2587 // to be encountered when processing function symbols. The start address 2588 // is rounded against the final alignment of the text section later on in 2589 // (*Link).address. This may happen due to usage of PCALIGN directives 2590 // larger than Funcalign, or usage of ISA 3.1 prefixed instructions 2591 // (see ISA 3.1 Book I 1.9). 2592 const ppc64maxFuncalign = 64 2593 sectAlign = ppc64maxFuncalign 2594 va = uint64(Rnd(int64(va), ppc64maxFuncalign)) 2595 } 2596 2597 // Set the length for the previous text section 2598 sect.Length = va - sect.Vaddr 2599 2600 // Create new section, set the starting Vaddr 2601 sect = addsection(ctxt.loader, ctxt.Arch, &Segtext, ".text", 05) 2602 2603 sect.Vaddr = va 2604 sect.Align = sectAlign 2605 ldr.SetSymSect(s, sect) 2606 2607 // Create a symbol for the start of the secondary text sections 2608 ntext := ldr.CreateSymForUpdate(fmt.Sprintf("runtime.text.%d", n), 0) 2609 ntext.SetSect(sect) 2610 if ctxt.IsAIX() { 2611 // runtime.text.X must be a real symbol on AIX. 2612 // Assign its address directly in order to be the 2613 // first symbol of this new section. 2614 ntext.SetType(sym.STEXT) 2615 ntext.SetSize(int64(MINFUNC)) 2616 ntext.SetOnList(true) 2617 ntext.SetAlign(sectAlign) 2618 ctxt.tramps = append(ctxt.tramps, ntext.Sym()) 2619 2620 ntext.SetValue(int64(va)) 2621 va += uint64(ntext.Size()) 2622 2623 if align := ldr.SymAlign(s); align != 0 { 2624 va = uint64(Rnd(int64(va), int64(align))) 2625 } else { 2626 va = uint64(Rnd(int64(va), int64(Funcalign))) 2627 } 2628 } 2629 n++ 2630 } 2631 } 2632 2633 ldr.SetSymValue(s, 0) 2634 for sub := s; sub != 0; sub = ldr.SubSym(sub) { 2635 ldr.SetSymValue(sub, ldr.SymValue(sub)+int64(va)) 2636 if ctxt.Debugvlog > 2 { 2637 fmt.Println("assign text address:", ldr.SymName(sub), ldr.SymValue(sub)) 2638 } 2639 } 2640 2641 va += funcsize 2642 2643 return sect, n, va 2644 } 2645 2646 func resetAddress(ctxt *Link, s loader.Sym) { 2647 ldr := ctxt.loader 2648 if ldr.OuterSym(s) != 0 { 2649 return 2650 } 2651 oldv := ldr.SymValue(s) 2652 for sub := s; sub != 0; sub = ldr.SubSym(sub) { 2653 ldr.SetSymValue(sub, ldr.SymValue(sub)-oldv) 2654 } 2655 } 2656 2657 // Return whether we may need to split text sections. 2658 // 2659 // On PPC64x, when external linking, a text section should not be 2660 // larger than 2^25 bytes due to the size of call target offset field 2661 // in the 'bl' instruction. Splitting into smaller text sections 2662 // smaller than this limit allows the system linker to modify the long 2663 // calls appropriately. The limit allows for the space needed for 2664 // tables inserted by the linker. 2665 // 2666 // The same applies to Darwin/ARM64, with 2^27 byte threshold. 2667 // 2668 // Similarly for ARM, we split sections (at 2^25 bytes) to avoid 2669 // inconsistencies between the Go linker's reachability calculations 2670 // (e.g. will direct call from X to Y need a trampoline) and similar 2671 // machinery in the external linker; see #58425 for more on the 2672 // history here. 2673 func splitTextSections(ctxt *Link) bool { 2674 return (ctxt.IsARM() || ctxt.IsPPC64() || (ctxt.IsARM64() && ctxt.IsDarwin())) && ctxt.IsExternal() 2675 } 2676 2677 // On Wasm, we reserve 4096 bytes for zero page, then 8192 bytes for wasm_exec.js 2678 // to store command line args and environment variables. 2679 // Data sections starts from at least address 12288. 2680 // Keep in sync with wasm_exec.js. 2681 const wasmMinDataAddr = 4096 + 8192 2682 2683 // address assigns virtual addresses to all segments and sections and 2684 // returns all segments in file order. 2685 func (ctxt *Link) address() []*sym.Segment { 2686 var order []*sym.Segment // Layout order 2687 2688 va := uint64(*FlagTextAddr) 2689 order = append(order, &Segtext) 2690 Segtext.Rwx = 05 2691 Segtext.Vaddr = va 2692 for i, s := range Segtext.Sections { 2693 va = uint64(Rnd(int64(va), int64(s.Align))) 2694 s.Vaddr = va 2695 va += s.Length 2696 2697 if ctxt.IsWasm() && i == 0 && va < wasmMinDataAddr { 2698 va = wasmMinDataAddr 2699 } 2700 } 2701 2702 Segtext.Length = va - uint64(*FlagTextAddr) 2703 2704 if len(Segrodata.Sections) > 0 { 2705 // align to page boundary so as not to mix 2706 // rodata and executable text. 2707 // 2708 // Note: gold or GNU ld will reduce the size of the executable 2709 // file by arranging for the relro segment to end at a page 2710 // boundary, and overlap the end of the text segment with the 2711 // start of the relro segment in the file. The PT_LOAD segments 2712 // will be such that the last page of the text segment will be 2713 // mapped twice, once r-x and once starting out rw- and, after 2714 // relocation processing, changed to r--. 2715 // 2716 // Ideally the last page of the text segment would not be 2717 // writable even for this short period. 2718 va = uint64(Rnd(int64(va), *FlagRound)) 2719 2720 order = append(order, &Segrodata) 2721 Segrodata.Rwx = 04 2722 Segrodata.Vaddr = va 2723 for _, s := range Segrodata.Sections { 2724 va = uint64(Rnd(int64(va), int64(s.Align))) 2725 s.Vaddr = va 2726 va += s.Length 2727 } 2728 2729 Segrodata.Length = va - Segrodata.Vaddr 2730 } 2731 if len(Segrelrodata.Sections) > 0 { 2732 // align to page boundary so as not to mix 2733 // rodata, rel-ro data, and executable text. 2734 va = uint64(Rnd(int64(va), *FlagRound)) 2735 if ctxt.HeadType == objabi.Haix { 2736 // Relro data are inside data segment on AIX. 2737 va += uint64(XCOFFDATABASE) - uint64(XCOFFTEXTBASE) 2738 } 2739 2740 order = append(order, &Segrelrodata) 2741 Segrelrodata.Rwx = 06 2742 Segrelrodata.Vaddr = va 2743 for _, s := range Segrelrodata.Sections { 2744 va = uint64(Rnd(int64(va), int64(s.Align))) 2745 s.Vaddr = va 2746 va += s.Length 2747 } 2748 2749 Segrelrodata.Length = va - Segrelrodata.Vaddr 2750 } 2751 2752 va = uint64(Rnd(int64(va), *FlagRound)) 2753 if ctxt.HeadType == objabi.Haix && len(Segrelrodata.Sections) == 0 { 2754 // Data sections are moved to an unreachable segment 2755 // to ensure that they are position-independent. 2756 // Already done if relro sections exist. 2757 va += uint64(XCOFFDATABASE) - uint64(XCOFFTEXTBASE) 2758 } 2759 order = append(order, &Segdata) 2760 Segdata.Rwx = 06 2761 Segdata.Vaddr = va 2762 var data *sym.Section 2763 var noptr *sym.Section 2764 var bss *sym.Section 2765 var noptrbss *sym.Section 2766 var fuzzCounters *sym.Section 2767 for i, s := range Segdata.Sections { 2768 if (ctxt.IsELF || ctxt.HeadType == objabi.Haix) && s.Name == ".tbss" { 2769 continue 2770 } 2771 vlen := int64(s.Length) 2772 if i+1 < len(Segdata.Sections) && !((ctxt.IsELF || ctxt.HeadType == objabi.Haix) && Segdata.Sections[i+1].Name == ".tbss") { 2773 vlen = int64(Segdata.Sections[i+1].Vaddr - s.Vaddr) 2774 } 2775 s.Vaddr = va 2776 va += uint64(vlen) 2777 Segdata.Length = va - Segdata.Vaddr 2778 switch s.Name { 2779 case ".data": 2780 data = s 2781 case ".noptrdata": 2782 noptr = s 2783 case ".bss": 2784 bss = s 2785 case ".noptrbss": 2786 noptrbss = s 2787 case ".go.fuzzcntrs": 2788 fuzzCounters = s 2789 } 2790 } 2791 2792 // Assign Segdata's Filelen omitting the BSS. We do this here 2793 // simply because right now we know where the BSS starts. 2794 Segdata.Filelen = bss.Vaddr - Segdata.Vaddr 2795 2796 if len(Segpdata.Sections) > 0 { 2797 va = uint64(Rnd(int64(va), *FlagRound)) 2798 order = append(order, &Segpdata) 2799 Segpdata.Rwx = 04 2800 Segpdata.Vaddr = va 2801 // Segpdata.Sections is intended to contain just one section. 2802 // Loop through the slice anyway for consistency. 2803 for _, s := range Segpdata.Sections { 2804 va = uint64(Rnd(int64(va), int64(s.Align))) 2805 s.Vaddr = va 2806 va += s.Length 2807 } 2808 Segpdata.Length = va - Segpdata.Vaddr 2809 } 2810 2811 if len(Segxdata.Sections) > 0 { 2812 va = uint64(Rnd(int64(va), *FlagRound)) 2813 order = append(order, &Segxdata) 2814 Segxdata.Rwx = 04 2815 Segxdata.Vaddr = va 2816 // Segxdata.Sections is intended to contain just one section. 2817 // Loop through the slice anyway for consistency. 2818 for _, s := range Segxdata.Sections { 2819 va = uint64(Rnd(int64(va), int64(s.Align))) 2820 s.Vaddr = va 2821 va += s.Length 2822 } 2823 Segxdata.Length = va - Segxdata.Vaddr 2824 } 2825 2826 va = uint64(Rnd(int64(va), *FlagRound)) 2827 order = append(order, &Segdwarf) 2828 Segdwarf.Rwx = 06 2829 Segdwarf.Vaddr = va 2830 for i, s := range Segdwarf.Sections { 2831 vlen := int64(s.Length) 2832 if i+1 < len(Segdwarf.Sections) { 2833 vlen = int64(Segdwarf.Sections[i+1].Vaddr - s.Vaddr) 2834 } 2835 s.Vaddr = va 2836 va += uint64(vlen) 2837 if ctxt.HeadType == objabi.Hwindows { 2838 va = uint64(Rnd(int64(va), PEFILEALIGN)) 2839 } 2840 Segdwarf.Length = va - Segdwarf.Vaddr 2841 } 2842 2843 ldr := ctxt.loader 2844 var ( 2845 rodata = ldr.SymSect(ldr.LookupOrCreateSym("runtime.rodata", 0)) 2846 symtab = ldr.SymSect(ldr.LookupOrCreateSym("runtime.symtab", 0)) 2847 pclntab = ldr.SymSect(ldr.LookupOrCreateSym("runtime.pclntab", 0)) 2848 types = ldr.SymSect(ldr.LookupOrCreateSym("runtime.types", 0)) 2849 ) 2850 2851 for _, s := range ctxt.datap { 2852 if sect := ldr.SymSect(s); sect != nil { 2853 ldr.AddToSymValue(s, int64(sect.Vaddr)) 2854 } 2855 v := ldr.SymValue(s) 2856 for sub := ldr.SubSym(s); sub != 0; sub = ldr.SubSym(sub) { 2857 ldr.AddToSymValue(sub, v) 2858 } 2859 } 2860 2861 for _, si := range dwarfp { 2862 for _, s := range si.syms { 2863 if sect := ldr.SymSect(s); sect != nil { 2864 ldr.AddToSymValue(s, int64(sect.Vaddr)) 2865 } 2866 sub := ldr.SubSym(s) 2867 if sub != 0 { 2868 panic(fmt.Sprintf("unexpected sub-sym for %s %s", ldr.SymName(s), ldr.SymType(s).String())) 2869 } 2870 v := ldr.SymValue(s) 2871 for ; sub != 0; sub = ldr.SubSym(sub) { 2872 ldr.AddToSymValue(s, v) 2873 } 2874 } 2875 } 2876 2877 for _, s := range sehp.pdata { 2878 if sect := ldr.SymSect(s); sect != nil { 2879 ldr.AddToSymValue(s, int64(sect.Vaddr)) 2880 } 2881 } 2882 for _, s := range sehp.xdata { 2883 if sect := ldr.SymSect(s); sect != nil { 2884 ldr.AddToSymValue(s, int64(sect.Vaddr)) 2885 } 2886 } 2887 2888 if ctxt.BuildMode == BuildModeShared { 2889 s := ldr.LookupOrCreateSym("go:link.abihashbytes", 0) 2890 sect := ldr.SymSect(ldr.LookupOrCreateSym(".note.go.abihash", 0)) 2891 ldr.SetSymSect(s, sect) 2892 ldr.SetSymValue(s, int64(sect.Vaddr+16)) 2893 } 2894 2895 // If there are multiple text sections, create runtime.text.n for 2896 // their section Vaddr, using n for index 2897 n := 1 2898 for _, sect := range Segtext.Sections[1:] { 2899 if sect.Name != ".text" { 2900 break 2901 } 2902 symname := fmt.Sprintf("runtime.text.%d", n) 2903 if ctxt.HeadType != objabi.Haix || ctxt.LinkMode != LinkExternal { 2904 // Addresses are already set on AIX with external linker 2905 // because these symbols are part of their sections. 2906 ctxt.xdefine(symname, sym.STEXT, int64(sect.Vaddr)) 2907 } 2908 n++ 2909 } 2910 2911 ctxt.xdefine("runtime.rodata", sym.SRODATA, int64(rodata.Vaddr)) 2912 ctxt.xdefine("runtime.erodata", sym.SRODATA, int64(rodata.Vaddr+rodata.Length)) 2913 ctxt.xdefine("runtime.types", sym.SRODATA, int64(types.Vaddr)) 2914 ctxt.xdefine("runtime.etypes", sym.SRODATA, int64(types.Vaddr+types.Length)) 2915 2916 s := ldr.Lookup("runtime.gcdata", 0) 2917 ldr.SetAttrLocal(s, true) 2918 ctxt.xdefine("runtime.egcdata", sym.SRODATA, ldr.SymAddr(s)+ldr.SymSize(s)) 2919 ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.egcdata", 0), ldr.SymSect(s)) 2920 2921 s = ldr.LookupOrCreateSym("runtime.gcbss", 0) 2922 ldr.SetAttrLocal(s, true) 2923 ctxt.xdefine("runtime.egcbss", sym.SRODATA, ldr.SymAddr(s)+ldr.SymSize(s)) 2924 ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.egcbss", 0), ldr.SymSect(s)) 2925 2926 ctxt.xdefine("runtime.symtab", sym.SRODATA, int64(symtab.Vaddr)) 2927 ctxt.xdefine("runtime.esymtab", sym.SRODATA, int64(symtab.Vaddr+symtab.Length)) 2928 ctxt.xdefine("runtime.pclntab", sym.SRODATA, int64(pclntab.Vaddr)) 2929 ctxt.defineInternal("runtime.pcheader", sym.SRODATA) 2930 ctxt.defineInternal("runtime.funcnametab", sym.SRODATA) 2931 ctxt.defineInternal("runtime.cutab", sym.SRODATA) 2932 ctxt.defineInternal("runtime.filetab", sym.SRODATA) 2933 ctxt.defineInternal("runtime.pctab", sym.SRODATA) 2934 ctxt.defineInternal("runtime.functab", sym.SRODATA) 2935 ctxt.xdefine("runtime.epclntab", sym.SRODATA, int64(pclntab.Vaddr+pclntab.Length)) 2936 ctxt.xdefine("runtime.noptrdata", sym.SNOPTRDATA, int64(noptr.Vaddr)) 2937 ctxt.xdefine("runtime.enoptrdata", sym.SNOPTRDATA, int64(noptr.Vaddr+noptr.Length)) 2938 ctxt.xdefine("runtime.bss", sym.SBSS, int64(bss.Vaddr)) 2939 ctxt.xdefine("runtime.ebss", sym.SBSS, int64(bss.Vaddr+bss.Length)) 2940 ctxt.xdefine("runtime.data", sym.SDATA, int64(data.Vaddr)) 2941 ctxt.xdefine("runtime.edata", sym.SDATA, int64(data.Vaddr+data.Length)) 2942 ctxt.xdefine("runtime.noptrbss", sym.SNOPTRBSS, int64(noptrbss.Vaddr)) 2943 ctxt.xdefine("runtime.enoptrbss", sym.SNOPTRBSS, int64(noptrbss.Vaddr+noptrbss.Length)) 2944 ctxt.xdefine("runtime.covctrs", sym.SCOVERAGE_COUNTER, int64(noptrbss.Vaddr+covCounterDataStartOff)) 2945 ctxt.xdefine("runtime.ecovctrs", sym.SCOVERAGE_COUNTER, int64(noptrbss.Vaddr+covCounterDataStartOff+covCounterDataLen)) 2946 ctxt.xdefine("runtime.end", sym.SBSS, int64(Segdata.Vaddr+Segdata.Length)) 2947 2948 if fuzzCounters != nil { 2949 ctxt.xdefine("runtime.__start___sancov_cntrs", sym.SLIBFUZZER_8BIT_COUNTER, int64(fuzzCounters.Vaddr)) 2950 ctxt.xdefine("runtime.__stop___sancov_cntrs", sym.SLIBFUZZER_8BIT_COUNTER, int64(fuzzCounters.Vaddr+fuzzCounters.Length)) 2951 ctxt.xdefine("github.com/go-asm/go/fuzz._counters", sym.SLIBFUZZER_8BIT_COUNTER, int64(fuzzCounters.Vaddr)) 2952 ctxt.xdefine("github.com/go-asm/go/fuzz._ecounters", sym.SLIBFUZZER_8BIT_COUNTER, int64(fuzzCounters.Vaddr+fuzzCounters.Length)) 2953 } 2954 2955 if ctxt.IsSolaris() { 2956 // On Solaris, in the runtime it sets the external names of the 2957 // end symbols. Unset them and define separate symbols, so we 2958 // keep both. 2959 etext := ldr.Lookup("runtime.etext", 0) 2960 edata := ldr.Lookup("runtime.edata", 0) 2961 end := ldr.Lookup("runtime.end", 0) 2962 ldr.SetSymExtname(etext, "runtime.etext") 2963 ldr.SetSymExtname(edata, "runtime.edata") 2964 ldr.SetSymExtname(end, "runtime.end") 2965 ctxt.xdefine("_etext", ldr.SymType(etext), ldr.SymValue(etext)) 2966 ctxt.xdefine("_edata", ldr.SymType(edata), ldr.SymValue(edata)) 2967 ctxt.xdefine("_end", ldr.SymType(end), ldr.SymValue(end)) 2968 ldr.SetSymSect(ldr.Lookup("_etext", 0), ldr.SymSect(etext)) 2969 ldr.SetSymSect(ldr.Lookup("_edata", 0), ldr.SymSect(edata)) 2970 ldr.SetSymSect(ldr.Lookup("_end", 0), ldr.SymSect(end)) 2971 } 2972 2973 if ctxt.IsPPC64() && ctxt.IsElf() { 2974 // Resolve .TOC. symbols for all objects. Only one TOC region is supported. If a 2975 // GOT section is present, compute it as suggested by the ELFv2 ABI. Otherwise, 2976 // choose a similar offset from the start of the data segment. 2977 tocAddr := int64(Segdata.Vaddr) + 0x8000 2978 if gotAddr := ldr.SymValue(ctxt.GOT); gotAddr != 0 { 2979 tocAddr = gotAddr + 0x8000 2980 } 2981 for i := range ctxt.DotTOC { 2982 if i >= sym.SymVerABICount && i < sym.SymVerStatic { // these versions are not used currently 2983 continue 2984 } 2985 if toc := ldr.Lookup(".TOC.", i); toc != 0 { 2986 ldr.SetSymValue(toc, tocAddr) 2987 } 2988 } 2989 } 2990 2991 return order 2992 } 2993 2994 // layout assigns file offsets and lengths to the segments in order. 2995 // Returns the file size containing all the segments. 2996 func (ctxt *Link) layout(order []*sym.Segment) uint64 { 2997 var prev *sym.Segment 2998 for _, seg := range order { 2999 if prev == nil { 3000 seg.Fileoff = uint64(HEADR) 3001 } else { 3002 switch ctxt.HeadType { 3003 default: 3004 // Assuming the previous segment was 3005 // aligned, the following rounding 3006 // should ensure that this segment's 3007 // VA ≡ Fileoff mod FlagRound. 3008 seg.Fileoff = uint64(Rnd(int64(prev.Fileoff+prev.Filelen), *FlagRound)) 3009 if seg.Vaddr%uint64(*FlagRound) != seg.Fileoff%uint64(*FlagRound) { 3010 Exitf("bad segment rounding (Vaddr=%#x Fileoff=%#x FlagRound=%#x)", seg.Vaddr, seg.Fileoff, *FlagRound) 3011 } 3012 case objabi.Hwindows: 3013 seg.Fileoff = prev.Fileoff + uint64(Rnd(int64(prev.Filelen), PEFILEALIGN)) 3014 case objabi.Hplan9: 3015 seg.Fileoff = prev.Fileoff + prev.Filelen 3016 } 3017 } 3018 if seg != &Segdata { 3019 // Link.address already set Segdata.Filelen to 3020 // account for BSS. 3021 seg.Filelen = seg.Length 3022 } 3023 prev = seg 3024 } 3025 return prev.Fileoff + prev.Filelen 3026 } 3027 3028 // add a trampoline with symbol s (to be laid down after the current function) 3029 func (ctxt *Link) AddTramp(s *loader.SymbolBuilder) { 3030 s.SetType(sym.STEXT) 3031 s.SetReachable(true) 3032 s.SetOnList(true) 3033 ctxt.tramps = append(ctxt.tramps, s.Sym()) 3034 if *FlagDebugTramp > 0 && ctxt.Debugvlog > 0 { 3035 ctxt.Logf("trampoline %s inserted\n", s.Name()) 3036 } 3037 } 3038 3039 // compressSyms compresses syms and returns the contents of the 3040 // compressed section. If the section would get larger, it returns nil. 3041 func compressSyms(ctxt *Link, syms []loader.Sym) []byte { 3042 ldr := ctxt.loader 3043 var total int64 3044 for _, sym := range syms { 3045 total += ldr.SymSize(sym) 3046 } 3047 3048 var buf bytes.Buffer 3049 if ctxt.IsELF { 3050 switch ctxt.Arch.PtrSize { 3051 case 8: 3052 binary.Write(&buf, ctxt.Arch.ByteOrder, elf.Chdr64{ 3053 Type: uint32(elf.COMPRESS_ZLIB), 3054 Size: uint64(total), 3055 Addralign: uint64(ctxt.Arch.Alignment), 3056 }) 3057 case 4: 3058 binary.Write(&buf, ctxt.Arch.ByteOrder, elf.Chdr32{ 3059 Type: uint32(elf.COMPRESS_ZLIB), 3060 Size: uint32(total), 3061 Addralign: uint32(ctxt.Arch.Alignment), 3062 }) 3063 default: 3064 log.Fatalf("can't compress header size:%d", ctxt.Arch.PtrSize) 3065 } 3066 } else { 3067 buf.Write([]byte("ZLIB")) 3068 var sizeBytes [8]byte 3069 binary.BigEndian.PutUint64(sizeBytes[:], uint64(total)) 3070 buf.Write(sizeBytes[:]) 3071 } 3072 3073 var relocbuf []byte // temporary buffer for applying relocations 3074 3075 // Using zlib.BestSpeed achieves very nearly the same 3076 // compression levels of zlib.DefaultCompression, but takes 3077 // substantially less time. This is important because DWARF 3078 // compression can be a significant fraction of link time. 3079 z, err := zlib.NewWriterLevel(&buf, zlib.BestSpeed) 3080 if err != nil { 3081 log.Fatalf("NewWriterLevel failed: %s", err) 3082 } 3083 st := ctxt.makeRelocSymState() 3084 for _, s := range syms { 3085 // Symbol data may be read-only. Apply relocations in a 3086 // temporary buffer, and immediately write it out. 3087 P := ldr.Data(s) 3088 relocs := ldr.Relocs(s) 3089 if relocs.Count() != 0 { 3090 relocbuf = append(relocbuf[:0], P...) 3091 P = relocbuf 3092 st.relocsym(s, P) 3093 } 3094 if _, err := z.Write(P); err != nil { 3095 log.Fatalf("compression failed: %s", err) 3096 } 3097 for i := ldr.SymSize(s) - int64(len(P)); i > 0; { 3098 b := zeros[:] 3099 if i < int64(len(b)) { 3100 b = b[:i] 3101 } 3102 n, err := z.Write(b) 3103 if err != nil { 3104 log.Fatalf("compression failed: %s", err) 3105 } 3106 i -= int64(n) 3107 } 3108 } 3109 if err := z.Close(); err != nil { 3110 log.Fatalf("compression failed: %s", err) 3111 } 3112 if int64(buf.Len()) >= total { 3113 // Compression didn't save any space. 3114 return nil 3115 } 3116 return buf.Bytes() 3117 }