data.go 64.7 KB
Newer Older
1
// Derived from Inferno utils/6l/obj.c and utils/6l/span.c
2 3
// https://bitbucket.org/inferno-os/inferno-os/src/default/utils/6l/obj.c
// https://bitbucket.org/inferno-os/inferno-os/src/default/utils/6l/span.c
4 5 6 7 8 9 10 11
//
//	Copyright © 1994-1999 Lucent Technologies Inc.  All rights reserved.
//	Portions Copyright © 1995-1997 C H Forsyth (forsyth@terzarima.net)
//	Portions Copyright © 1997-1999 Vita Nuova Limited
//	Portions Copyright © 2000-2007 Vita Nuova Holdings Limited (www.vitanuova.com)
//	Portions Copyright © 2004,2006 Bruce Ellis
//	Portions Copyright © 2005-2007 C H Forsyth (forsyth@terzarima.net)
//	Revisions Copyright © 2000-2007 Lucent Technologies Inc. and others
12
//	Portions Copyright © 2009 The Go Authors. All rights reserved.
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package ld

import (
35
	"bytes"
36
	"cmd/internal/gcprog"
37
	"cmd/internal/objabi"
38
	"cmd/internal/sys"
39
	"cmd/link/internal/sym"
40 41
	"compress/zlib"
	"encoding/binary"
42 43
	"fmt"
	"log"
44
	"os"
45
	"sort"
46
	"strconv"
47
	"strings"
48
	"sync"
49 50
)

51 52 53 54
// isRuntimeDepPkg returns whether pkg is the runtime package or its dependency
func isRuntimeDepPkg(pkg string) bool {
	switch pkg {
	case "runtime",
55 56 57
		"sync/atomic",      // runtime may call to sync/atomic, due to go:linkname
		"internal/bytealg", // for IndexByte
		"internal/cpu":     // for cpu features
58 59 60 61 62
		return true
	}
	return strings.HasPrefix(pkg, "runtime/internal/") && !strings.HasSuffix(pkg, "_test")
}

63 64 65
// Estimate the max size needed to hold any new trampolines created for this function. This
// is used to determine when the section can be split if it becomes too large, to ensure that
// the trampolines are in the same section as the function that uses them.
66
func maxSizeTrampolinesPPC64(s *sym.Symbol, isTramp bool) uint64 {
67
	// If thearch.Trampoline is nil, then trampoline support is not available on this arch.
68
	// A trampoline does not need any dependent trampolines.
69
	if thearch.Trampoline == nil || isTramp {
70 71 72 73 74 75 76 77 78 79 80 81 82 83
		return 0
	}

	n := uint64(0)
	for ri := range s.R {
		r := &s.R[ri]
		if r.Type.IsDirectJump() {
			n++
		}
	}
	// Trampolines in ppc64 are 4 instructions.
	return n * 16
}

84
// detect too-far jumps in function s, and add trampolines if necessary
85 86 87
// ARM, PPC64 & PPC64LE support trampoline insertion for internal and external linking
// On PPC64 & PPC64LE the text sections might be split but will still insert trampolines
// where necessary.
88
func trampoline(ctxt *Link, s *sym.Symbol) {
89
	if thearch.Trampoline == nil {
90 91 92 93 94 95 96 97
		return // no need or no support of trampolines on this arch
	}

	for ri := range s.R {
		r := &s.R[ri]
		if !r.Type.IsDirectJump() {
			continue
		}
98
		if Symaddr(r.Sym) == 0 && r.Sym.Type != sym.SDYNIMPORT {
99 100
			if r.Sym.File != s.File {
				if !isRuntimeDepPkg(s.File) || !isRuntimeDepPkg(r.Sym.File) {
101
					ctxt.ErrorUnresolved(s, r)
102 103 104 105 106 107 108
				}
				// runtime and its dependent packages may call to each other.
				// they are fine, as they will be laid down together.
			}
			continue
		}

109
		thearch.Trampoline(ctxt, r, s)
110 111 112 113
	}

}

114 115 116 117 118 119 120 121 122 123 124 125 126 127
// relocsym resolve relocations in "s". The main loop walks through
// the list of relocations attached to "s" and resolves them where
// applicable. Relocations are often architecture-specific, requiring
// calls into the 'archreloc' and/or 'archrelocvariant' functions for
// the architecture. When external linking is in effect, it may not be
// possible to completely resolve the address/offset for a symbol, in
// which case the goal is to lay the groundwork for turning a given
// relocation into an external reloc (to be applied by the external
// linker). For more on how relocations work in general, see
//
//  "Linkers and Loaders", by John R. Levine (Morgan Kaufmann, 1999), ch. 7
//
// This is a performance-critical function for the linker; be careful
// to avoid introducing unnecessary allocations in the main loop.
128
func relocsym(ctxt *Link, s *sym.Symbol) {
Russ Cox's avatar
Russ Cox committed
129
	for ri := int32(0); ri < int32(len(s.R)); ri++ {
130
		r := &s.R[ri]
131 132 133 134
		if r.Done {
			// Relocation already processed by an earlier phase.
			continue
		}
Hiroshi Ioka's avatar
Hiroshi Ioka committed
135
		r.Done = true
136 137
		off := r.Off
		siz := int32(r.Siz)
138
		if off < 0 || off+siz > int32(len(s.P)) {
139 140 141 142 143
			rname := ""
			if r.Sym != nil {
				rname = r.Sym.Name
			}
			Errorf(s, "invalid relocation %s: %d+%d not in [%d,%d)", rname, off, siz, 0, len(s.P))
144 145 146
			continue
		}

147
		if r.Sym != nil && ((r.Sym.Type == sym.Sxxx && !r.Sym.Attr.VisibilityHidden()) || r.Sym.Type == sym.SXREF) {
148 149
			// When putting the runtime but not main into a shared library
			// these symbols are undefined and that's OK.
150
			if ctxt.BuildMode == BuildModeShared {
151
				if r.Sym.Name == "main.main" || r.Sym.Name == "main.init" {
152
					r.Sym.Type = sym.SDYNIMPORT
153 154 155 156 157
				} else if strings.HasPrefix(r.Sym.Name, "go.info.") {
					// Skip go.info symbols. They are only needed to communicate
					// DWARF info between the compiler and linker.
					continue
				}
158
			} else {
159
				ctxt.ErrorUnresolved(s, r)
160 161
				continue
			}
162 163 164 165 166 167 168 169
		}

		if r.Type >= 256 {
			continue
		}
		if r.Siz == 0 { // informational relocation - no work to do
			continue
		}
170 171 172 173 174 175
		if r.Type == objabi.R_DWARFFILEREF {
			// These should have been processed previously during
			// line table writing.
			Errorf(s, "orphan R_DWARFFILEREF reloc to %v", r.Sym.Name)
			continue
		}
176

177
		// We need to be able to reference dynimport symbols when linking against
178 179
		// shared libraries, and Solaris, Darwin and AIX need it always
		if ctxt.HeadType != objabi.Hsolaris && ctxt.HeadType != objabi.Hdarwin && ctxt.HeadType != objabi.Haix && r.Sym != nil && r.Sym.Type == sym.SDYNIMPORT && !ctxt.DynlinkingGo() && !r.Sym.Attr.SubSymbol() {
180
			if !(ctxt.Arch.Family == sys.PPC64 && ctxt.LinkMode == LinkExternal && r.Sym.Name == ".TOC.") {
181
				Errorf(s, "unhandled relocation for %s (type %d (%s) rtype %d (%s))", r.Sym.Name, r.Sym.Type, r.Sym.Type, r.Type, sym.RelocName(ctxt.Arch, r.Type))
182
			}
183
		}
184
		if r.Sym != nil && r.Sym.Type != sym.STLSBSS && r.Type != objabi.R_WEAKADDROFF && !r.Sym.Attr.Reachable() {
185
			Errorf(s, "unreachable sym in relocation: %s", r.Sym.Name)
186 187
		}

188
		// TODO(mundaym): remove this special case - see issue 14218.
189
		if ctxt.Arch.Family == sys.S390X {
190
			switch r.Type {
191 192
			case objabi.R_PCRELDBL:
				r.Type = objabi.R_PCREL
193
				r.Variant = sym.RV_390_DBL
194
			case objabi.R_CALL:
195
				r.Variant = sym.RV_390_DBL
196 197 198
			}
		}

199
		var o int64
200 201
		switch r.Type {
		default:
202 203
			switch siz {
			default:
204
				Errorf(s, "bad reloc size %#x for %s", uint32(siz), r.Sym.Name)
205 206 207
			case 1:
				o = int64(s.P[off])
			case 2:
208
				o = int64(ctxt.Arch.ByteOrder.Uint16(s.P[off:]))
209
			case 4:
210
				o = int64(ctxt.Arch.ByteOrder.Uint32(s.P[off:]))
211
			case 8:
212
				o = int64(ctxt.Arch.ByteOrder.Uint64(s.P[off:]))
213
			}
214 215 216
			if offset, ok := thearch.Archreloc(ctxt, r, s, o); ok {
				o = offset
			} else {
217
				Errorf(s, "unknown reloc to %v: %d (%s)", r.Sym.Name, r.Type, sym.RelocName(ctxt.Arch, r.Type))
218
			}
219
		case objabi.R_TLS_LE:
220
			isAndroidX86 := objabi.GOOS == "android" && (ctxt.Arch.InFamily(sys.AMD64, sys.I386))
221

222
			if ctxt.LinkMode == LinkExternal && ctxt.IsELF && !isAndroidX86 {
Hiroshi Ioka's avatar
Hiroshi Ioka committed
223
				r.Done = false
224
				if r.Sym == nil {
225
					r.Sym = ctxt.Tlsg
226 227
				}
				r.Xsym = r.Sym
228 229
				r.Xadd = r.Add
				o = 0
230
				if ctxt.Arch.Family != sys.AMD64 {
231 232 233 234 235
					o = r.Add
				}
				break
			}

236
			if ctxt.IsELF && ctxt.Arch.Family == sys.ARM {
237 238 239 240 241 242 243 244
				// On ELF ARM, the thread pointer is 8 bytes before
				// the start of the thread-local data block, so add 8
				// to the actual TLS offset (r->sym->value).
				// This 8 seems to be a fundamental constant of
				// ELF on ARM (or maybe Glibc on ARM); it is not
				// related to the fact that our own TLS storage happens
				// to take up 8 bytes.
				o = 8 + r.Sym.Value
245
			} else if ctxt.IsELF || ctxt.HeadType == objabi.Hplan9 || ctxt.HeadType == objabi.Hdarwin || isAndroidX86 {
246
				o = int64(ctxt.Tlsoffset) + r.Add
247
			} else if ctxt.HeadType == objabi.Hwindows {
248 249
				o = r.Add
			} else {
250
				log.Fatalf("unexpected R_TLS_LE relocation for %v", ctxt.HeadType)
251
			}
252
		case objabi.R_TLS_IE:
253
			isAndroidX86 := objabi.GOOS == "android" && (ctxt.Arch.InFamily(sys.AMD64, sys.I386))
254

255
			if ctxt.LinkMode == LinkExternal && ctxt.IsELF && !isAndroidX86 {
Hiroshi Ioka's avatar
Hiroshi Ioka committed
256
				r.Done = false
257
				if r.Sym == nil {
258
					r.Sym = ctxt.Tlsg
259 260
				}
				r.Xsym = r.Sym
261 262
				r.Xadd = r.Add
				o = 0
263
				if ctxt.Arch.Family != sys.AMD64 {
264 265 266 267
					o = r.Add
				}
				break
			}
268
			if ctxt.BuildMode == BuildModePIE && ctxt.IsELF {
269 270
				// We are linking the final executable, so we
				// can optimize any TLS IE relocation to LE.
271
				if thearch.TLSIEtoLE == nil {
272
					log.Fatalf("internal linking of TLS IE not supported on %v", ctxt.Arch.Family)
273
				}
274
				thearch.TLSIEtoLE(s, int(off), int(r.Siz))
275
				o = int64(ctxt.Tlsoffset)
276
				// TODO: o += r.Add when ctxt.Arch.Family != sys.AMD64?
277 278
				// Why do we treat r.Add differently on AMD64?
				// Is the external linker using Xadd at all?
279 280 281
			} else {
				log.Fatalf("cannot handle R_TLS_IE (sym %s) when linking internally", s.Name)
			}
282
		case objabi.R_ADDR:
283
			if ctxt.LinkMode == LinkExternal && r.Sym.Type != sym.SCONST {
Hiroshi Ioka's avatar
Hiroshi Ioka committed
284
				r.Done = false
285 286

				// set up addend for eventual relocation via outer symbol.
287
				rs := r.Sym
288 289 290

				r.Xadd = r.Add
				for rs.Outer != nil {
291
					r.Xadd += Symaddr(rs) - Symaddr(rs.Outer)
292 293 294
					rs = rs.Outer
				}

295
				if rs.Type != sym.SHOSTOBJ && rs.Type != sym.SDYNIMPORT && rs.Sect == nil {
296
					Errorf(s, "missing section for relocation target %s", rs.Name)
297 298 299 300
				}
				r.Xsym = rs

				o = r.Xadd
301
				if ctxt.IsELF {
302
					if ctxt.Arch.Family == sys.AMD64 {
303 304
						o = 0
					}
305
				} else if ctxt.HeadType == objabi.Hdarwin {
306
					if rs.Type != sym.SHOSTOBJ {
307
						o += Symaddr(rs)
308
					}
309
				} else if ctxt.HeadType == objabi.Hwindows {
310
					// nothing to do
311
				} else {
312
					Errorf(s, "unhandled pcrel relocation to %s on %v", rs.Name, ctxt.HeadType)
313 314 315 316
				}

				break
			}
317 318 319 320
			// On AIX, if a relocated symbol is in .data, a second relocation
			// must be done by the loader, as the section .data will be moved.
			// The "default" symbol address is still needed by the loader so
			// the current relocation can't be skipped.
321
			if ctxt.HeadType == objabi.Haix && r.Sym.Type != sym.SDYNIMPORT && r.Sym.Sect.Seg == &Segdata {
322 323 324
				// It's not possible to make a loader relocation to a DWARF section.
				// FIXME
				if s.Sect.Seg != &Segdwarf {
325
					Xcoffadddynrel(ctxt, s, r)
326 327
				}
			}
328

329
			o = Symaddr(r.Sym) + r.Add
330 331 332

			// On amd64, 4-byte offsets will be sign-extended, so it is impossible to
			// access more than 2GB of static data; fail at link time is better than
333
			// fail at runtime. See https://golang.org/issue/7980.
334 335
			// Instead of special casing only amd64, we treat this as an error on all
			// 64-bit architectures so as to be future-proof.
336
			if int32(o) < 0 && ctxt.Arch.PtrSize > 4 && siz == 4 {
337
				Errorf(s, "non-pc-relative relocation address for %s is too big: %#x (%#x + %#x)", r.Sym.Name, uint64(o), Symaddr(r.Sym), r.Add)
338
				errorexit()
339
			}
340
		case objabi.R_DWARFSECREF:
341
			if r.Sym.Sect == nil {
342
				Errorf(s, "missing DWARF section for relocation target %s", r.Sym.Name)
343
			}
344

345
			if ctxt.LinkMode == LinkExternal {
Hiroshi Ioka's avatar
Hiroshi Ioka committed
346
				r.Done = false
347 348 349 350 351 352

				// On most platforms, the external linker needs to adjust DWARF references
				// as it combines DWARF sections. However, on Darwin, dsymutil does the
				// DWARF linking, and it understands how to follow section offsets.
				// Leaving in the relocation records confuses it (see
				// https://golang.org/issue/22068) so drop them for Darwin.
353
				if ctxt.HeadType == objabi.Hdarwin {
354 355 356
					r.Done = true
				}

357
				// PE code emits IMAGE_REL_I386_SECREL and IMAGE_REL_AMD64_SECREL
358
				// for R_DWARFSECREF relocations, while R_ADDR is replaced with
359
				// IMAGE_REL_I386_DIR32, IMAGE_REL_AMD64_ADDR64 and IMAGE_REL_AMD64_ADDR32.
360
				// Do not replace R_DWARFSECREF with R_ADDR for windows -
361
				// let PE code emit correct relocations.
362
				if ctxt.HeadType != objabi.Hwindows {
363
					r.Type = objabi.R_ADDR
364
				}
365

366 367
				r.Xsym = ctxt.Syms.ROLookup(r.Sym.Sect.Name, 0)
				r.Xadd = r.Add + Symaddr(r.Sym) - int64(r.Sym.Sect.Vaddr)
368

369
				o = r.Xadd
370
				if ctxt.IsELF && ctxt.Arch.Family == sys.AMD64 {
371 372 373 374
					o = 0
				}
				break
			}
375
			o = Symaddr(r.Sym) + r.Add - int64(r.Sym.Sect.Vaddr)
376
		case objabi.R_WEAKADDROFF:
377 378 379 380
			if !r.Sym.Attr.Reachable() {
				continue
			}
			fallthrough
381
		case objabi.R_ADDROFF:
382 383 384
			// The method offset tables using this relocation expect the offset to be relative
			// to the start of the first text section, even if there are multiple.
			if r.Sym.Sect.Name == ".text" {
385
				o = Symaddr(r.Sym) - int64(Segtext.Sections[0].Vaddr) + r.Add
386 387 388
			} else {
				o = Symaddr(r.Sym) - int64(r.Sym.Sect.Vaddr) + r.Add
			}
389

390 391 392 393 394
		case objabi.R_ADDRCUOFF:
			// debug_range and debug_loc elements use this relocation type to get an
			// offset from the start of the compile unit.
			o = Symaddr(r.Sym) + r.Add - Symaddr(r.Sym.Lib.Textp[0])

395
			// r->sym can be null when CALL $(constant) is transformed from absolute PC to relative PC call.
396
		case objabi.R_GOTPCREL:
397
			if ctxt.DynlinkingGo() && ctxt.HeadType == objabi.Hdarwin && r.Sym != nil && r.Sym.Type != sym.SCONST {
Hiroshi Ioka's avatar
Hiroshi Ioka committed
398
				r.Done = false
399 400 401 402 403 404 405 406 407
				r.Xadd = r.Add
				r.Xadd -= int64(r.Siz) // relative to address after the relocated chunk
				r.Xsym = r.Sym

				o = r.Xadd
				o += int64(r.Siz)
				break
			}
			fallthrough
408
		case objabi.R_CALL, objabi.R_PCREL:
409
			if ctxt.LinkMode == LinkExternal && r.Sym != nil && r.Sym.Type != sym.SCONST && (r.Sym.Sect != s.Sect || r.Type == objabi.R_GOTPCREL) {
Hiroshi Ioka's avatar
Hiroshi Ioka committed
410
				r.Done = false
411 412

				// set up addend for eventual relocation via outer symbol.
413
				rs := r.Sym
414 415 416

				r.Xadd = r.Add
				for rs.Outer != nil {
417
					r.Xadd += Symaddr(rs) - Symaddr(rs.Outer)
418 419 420 421
					rs = rs.Outer
				}

				r.Xadd -= int64(r.Siz) // relative to address after the relocated chunk
422
				if rs.Type != sym.SHOSTOBJ && rs.Type != sym.SDYNIMPORT && rs.Sect == nil {
423
					Errorf(s, "missing section for relocation target %s", rs.Name)
424 425 426 427
				}
				r.Xsym = rs

				o = r.Xadd
428
				if ctxt.IsELF {
429
					if ctxt.Arch.Family == sys.AMD64 {
430 431
						o = 0
					}
432
				} else if ctxt.HeadType == objabi.Hdarwin {
433
					if r.Type == objabi.R_CALL {
434 435 436 437 438 439 440 441 442 443 444 445 446 447 448
						if ctxt.LinkMode == LinkExternal && rs.Type == sym.SDYNIMPORT {
							switch ctxt.Arch.Family {
							case sys.AMD64:
								// AMD64 dynamic relocations are relative to the end of the relocation.
								o += int64(r.Siz)
							case sys.I386:
								// I386 dynamic relocations are relative to the start of the section.
								o -= int64(r.Off)                         // offset in symbol
								o -= int64(s.Value - int64(s.Sect.Vaddr)) // offset of symbol in section
							}
						} else {
							if rs.Type != sym.SHOSTOBJ {
								o += int64(uint64(Symaddr(rs)) - rs.Sect.Vaddr)
							}
							o -= int64(r.Off) // relative to section offset, not symbol
449
						}
450
					} else if ctxt.Arch.Family == sys.ARM {
451
						// see ../arm/asm.go:/machoreloc1
452
						o += Symaddr(rs) - s.Value - int64(r.Off)
453 454 455
					} else {
						o += int64(r.Siz)
					}
456
				} else if ctxt.HeadType == objabi.Hwindows && ctxt.Arch.Family == sys.AMD64 { // only amd64 needs PCREL
457 458 459
					// PE/COFF's PC32 relocation uses the address after the relocated
					// bytes as the base. Compensate by skewing the addend.
					o += int64(r.Siz)
460
				} else {
461
					Errorf(s, "unhandled pcrel relocation to %s on %v", rs.Name, ctxt.HeadType)
462 463 464 465 466 467 468
				}

				break
			}

			o = 0
			if r.Sym != nil {
469
				o += Symaddr(r.Sym)
470 471
			}

472
			o += r.Add - (s.Value + int64(r.Off) + int64(r.Siz))
473
		case objabi.R_SIZE:
474 475 476
			o = r.Sym.Size + r.Add
		}

477
		if r.Variant != sym.RV_NONE {
478
			o = thearch.Archrelocvariant(ctxt, r, s, o)
479 480
		}

Russ Cox's avatar
Russ Cox committed
481 482
		if false {
			nam := "<nil>"
483
			var addr int64
Russ Cox's avatar
Russ Cox committed
484 485
			if r.Sym != nil {
				nam = r.Sym.Name
486 487 488 489 490
				addr = Symaddr(r.Sym)
			}
			xnam := "<nil>"
			if r.Xsym != nil {
				xnam = r.Xsym.Name
Russ Cox's avatar
Russ Cox committed
491
			}
492
			fmt.Printf("relocate %s %#x (%#x+%#x, size %d) => %s %#x +%#x (xsym: %s +%#x) [type %d (%s)/%d, %x]\n", s.Name, s.Value+int64(off), s.Value, r.Off, r.Siz, nam, addr, r.Add, xnam, r.Xadd, r.Type, sym.RelocName(ctxt.Arch, r.Type), r.Variant, o)
Russ Cox's avatar
Russ Cox committed
493
		}
494 495
		switch siz {
		default:
496
			Errorf(s, "bad reloc size %#x for %s", uint32(siz), r.Sym.Name)
497 498 499 500 501 502 503
			fallthrough

			// TODO(rsc): Remove.
		case 1:
			s.P[off] = byte(int8(o))
		case 2:
			if o != int64(int16(o)) {
504
				Errorf(s, "relocation address for %s is too big: %#x", r.Sym.Name, o)
505
			}
506
			i16 := int16(o)
507
			ctxt.Arch.ByteOrder.PutUint16(s.P[off:], uint16(i16))
508
		case 4:
509
			if r.Type == objabi.R_PCREL || r.Type == objabi.R_CALL {
510
				if o != int64(int32(o)) {
511
					Errorf(s, "pc-relative relocation address for %s is too big: %#x", r.Sym.Name, o)
512 513 514
				}
			} else {
				if o != int64(int32(o)) && o != int64(uint32(o)) {
515
					Errorf(s, "non-pc-relative relocation address for %s is too big: %#x", r.Sym.Name, uint64(o))
516 517 518
				}
			}

519
			fl := int32(o)
520
			ctxt.Arch.ByteOrder.PutUint32(s.P[off:], uint32(fl))
521
		case 8:
522
			ctxt.Arch.ByteOrder.PutUint64(s.P[off:], uint64(o))
523 524 525 526
		}
	}
}

527
func (ctxt *Link) reloc() {
528
	if ctxt.Debugvlog != 0 {
529
		ctxt.Logf("%5.2f reloc\n", Cputime())
530 531
	}

532 533
	for _, s := range ctxt.Textp {
		relocsym(ctxt, s)
534
	}
535 536
	for _, s := range datap {
		relocsym(ctxt, s)
537
	}
538
	for _, s := range dwarfp {
539
		relocsym(ctxt, s)
540
	}
541 542
}

543
func windynrelocsym(ctxt *Link, rel, s *sym.Symbol) {
544
	for ri := range s.R {
545 546 547 548
		r := &s.R[ri]
		targ := r.Sym
		if targ == nil {
			continue
549
		}
550 551
		if !targ.Attr.Reachable() {
			if r.Type == objabi.R_WEAKADDROFF {
552 553
				continue
			}
554 555
			Errorf(s, "dynamic relocation to unreachable symbol %s", targ.Name)
		}
556 557
		if r.Sym.Plt() == -2 && r.Sym.Got() != -2 { // make dynimport JMP table for PE object files.
			targ.SetPlt(int32(rel.Size))
558
			r.Sym = rel
559
			r.Add = int64(targ.Plt())
560 561

			// jmp *addr
562 563 564 565 566
			switch ctxt.Arch.Family {
			default:
				Errorf(s, "unsupported arch %v", ctxt.Arch.Family)
				return
			case sys.I386:
567 568 569 570 571
				rel.AddUint8(0xff)
				rel.AddUint8(0x25)
				rel.AddAddr(ctxt.Arch, targ)
				rel.AddUint8(0x90)
				rel.AddUint8(0x90)
572
			case sys.AMD64:
573 574 575 576 577
				rel.AddUint8(0xff)
				rel.AddUint8(0x24)
				rel.AddUint8(0x25)
				rel.AddAddrPlus4(targ, 0)
				rel.AddUint8(0x90)
578
			}
579
		} else if r.Sym.Plt() >= 0 {
580
			r.Sym = rel
581
			r.Add = int64(targ.Plt())
582
		}
583 584
	}
}
585

586 587 588 589
// windynrelocsyms generates jump table to C library functions that will be
// added later. windynrelocsyms writes the table into .rel symbol.
func (ctxt *Link) windynrelocsyms() {
	if !(ctxt.HeadType == objabi.Hwindows && iscgo && ctxt.LinkMode == LinkInternal) {
590 591
		return
	}
592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608
	if ctxt.Debugvlog != 0 {
		ctxt.Logf("%5.2f windynrelocsyms\n", Cputime())
	}

	/* relocation table */
	rel := ctxt.Syms.Lookup(".rel", 0)
	rel.Attr |= sym.AttrReachable
	rel.Type = sym.STEXT
	ctxt.Textp = append(ctxt.Textp, rel)

	for _, s := range ctxt.Textp {
		if s == rel {
			continue
		}
		windynrelocsym(ctxt, rel, s)
	}
}
609

610
func dynrelocsym(ctxt *Link, s *sym.Symbol) {
611
	for ri := range s.R {
612
		r := &s.R[ri]
613
		if ctxt.BuildMode == BuildModePIE && ctxt.LinkMode == LinkInternal {
614 615 616
			// It's expected that some relocations will be done
			// later by relocsym (R_TLS_LE, R_ADDROFF), so
			// don't worry if Adddynrel returns false.
617
			thearch.Adddynrel(ctxt, s, r)
618 619
			continue
		}
620

621
		if r.Sym != nil && r.Sym.Type == sym.SDYNIMPORT || r.Type >= 256 {
622
			if r.Sym != nil && !r.Sym.Attr.Reachable() {
623
				Errorf(s, "dynamic relocation to unreachable symbol %s", r.Sym.Name)
624
			}
625
			if !thearch.Adddynrel(ctxt, s, r) {
626
				Errorf(s, "unsupported dynamic relocation for symbol %s (type=%d (%s) stype=%d (%s))", r.Sym.Name, r.Type, sym.RelocName(ctxt.Arch, r.Type), r.Sym.Type, r.Sym.Type)
627
			}
628 629 630 631
		}
	}
}

632
func dynreloc(ctxt *Link, data *[sym.SXREF][]*sym.Symbol) {
633 634 635
	if ctxt.HeadType == objabi.Hwindows {
		return
	}
636 637
	// -d suppresses dynamic loader format, so we may as well not
	// compute these sections or mark their symbols as reachable.
638
	if *FlagD {
639 640
		return
	}
641
	if ctxt.Debugvlog != 0 {
Hiroshi Ioka's avatar
Hiroshi Ioka committed
642
		ctxt.Logf("%5.2f dynreloc\n", Cputime())
643 644
	}

645 646
	for _, s := range ctxt.Textp {
		dynrelocsym(ctxt, s)
647
	}
648
	for _, syms := range data {
649 650
		for _, s := range syms {
			dynrelocsym(ctxt, s)
651
		}
652
	}
653
	if ctxt.IsELF {
654
		elfdynhash(ctxt)
655 656 657
	}
}

658 659
func Codeblk(ctxt *Link, addr int64, size int64) {
	CodeblkPad(ctxt, addr, size, zeros[:])
660
}
661
func CodeblkPad(ctxt *Link, addr int64, size int64, pad []byte) {
662
	if *flagA {
663
		ctxt.Logf("codeblk [%#x,%#x) at offset %#x\n", addr, addr+size, ctxt.Out.Offset())
664 665
	}

666
	blk(ctxt, ctxt.Textp, addr, size, pad)
667 668

	/* again for printing */
669
	if !*flagA {
670 671 672
		return
	}

673
	syms := ctxt.Textp
674 675
	for i, s := range syms {
		if !s.Attr.Reachable() {
676 677
			continue
		}
678
		if s.Value >= addr {
679
			syms = syms[i:]
680 681 682 683
			break
		}
	}

Russ Cox's avatar
Russ Cox committed
684
	eaddr := addr + size
685 686
	for _, s := range syms {
		if !s.Attr.Reachable() {
687 688
			continue
		}
689
		if s.Value >= eaddr {
690 691 692
			break
		}

693
		if addr < s.Value {
694
			ctxt.Logf("%-20s %.8x|", "_", uint64(addr))
695
			for ; addr < s.Value; addr++ {
696
				ctxt.Logf(" %.2x", 0)
697
			}
698
			ctxt.Logf("\n")
699 700
		}

701
		ctxt.Logf("%.6x\t%-20s\n", uint64(addr), s.Name)
702
		q := s.P
703

704
		for len(q) >= 16 {
705
			ctxt.Logf("%.6x\t% x\n", uint64(addr), q[:16])
706 707 708 709
			addr += 16
			q = q[16:]
		}

710
		if len(q) > 0 {
711
			ctxt.Logf("%.6x\t% x\n", uint64(addr), q)
712
			addr += int64(len(q))
713 714 715 716
		}
	}

	if addr < eaddr {
717
		ctxt.Logf("%-20s %.8x|", "_", uint64(addr))
718
		for ; addr < eaddr; addr++ {
719
			ctxt.Logf(" %.2x", 0)
720 721 722 723
		}
	}
}

724
func blk(ctxt *Link, syms []*sym.Symbol, addr, size int64, pad []byte) {
725
	for i, s := range syms {
726
		if !s.Attr.SubSymbol() && s.Value >= addr {
727 728 729 730 731
			syms = syms[i:]
			break
		}
	}

732 733 734 735
	// This doesn't distinguish the memory size from the file
	// size, and it lays out the file based on Symbol.Value, which
	// is the virtual address. DWARF compression changes file sizes,
	// so dwarfcompress will fix this up later if necessary.
736 737
	eaddr := addr + size
	for _, s := range syms {
738
		if s.Attr.SubSymbol() {
739 740 741 742 743 744
			continue
		}
		if s.Value >= eaddr {
			break
		}
		if s.Value < addr {
745
			Errorf(s, "phase error: addr=%#x but sym=%#x type=%d", addr, s.Value, s.Type)
746 747 748
			errorexit()
		}
		if addr < s.Value {
749
			ctxt.Out.WriteStringPad("", int(s.Value-addr), pad)
750 751
			addr = s.Value
		}
752
		ctxt.Out.Write(s.P)
753 754
		addr += int64(len(s.P))
		if addr < s.Value+s.Size {
755
			ctxt.Out.WriteStringPad("", int(s.Value+s.Size-addr), pad)
756 757 758
			addr = s.Value + s.Size
		}
		if addr != s.Value+s.Size {
759
			Errorf(s, "phase error: addr=%#x value+size=%#x", addr, s.Value+s.Size)
760 761 762 763 764 765 766 767
			errorexit()
		}
		if s.Value+s.Size >= eaddr {
			break
		}
	}

	if addr < eaddr {
768
		ctxt.Out.WriteStringPad("", int(eaddr-addr), pad)
769
	}
770
	ctxt.Out.Flush()
771 772
}

773
func Datblk(ctxt *Link, addr int64, size int64) {
774
	if *flagA {
775
		ctxt.Logf("datblk [%#x,%#x) at offset %#x\n", addr, addr+size, ctxt.Out.Offset())
776 777
	}

778
	blk(ctxt, datap, addr, size, zeros[:])
779 780

	/* again for printing */
781
	if !*flagA {
782 783 784
		return
	}

785 786
	syms := datap
	for i, sym := range syms {
787
		if sym.Value >= addr {
788
			syms = syms[i:]
789 790 791 792
			break
		}
	}

Russ Cox's avatar
Russ Cox committed
793
	eaddr := addr + size
794
	for _, sym := range syms {
795 796 797 798
		if sym.Value >= eaddr {
			break
		}
		if addr < sym.Value {
799
			ctxt.Logf("\t%.8x| 00 ...\n", uint64(addr))
800 801 802
			addr = sym.Value
		}

803
		ctxt.Logf("%s\n\t%.8x|", sym.Name, uint64(addr))
804 805
		for i, b := range sym.P {
			if i > 0 && i%16 == 0 {
806
				ctxt.Logf("\n\t%.8x|", uint64(addr)+uint64(i))
807
			}
808
			ctxt.Logf(" %.2x", b)
809 810 811 812
		}

		addr += int64(len(sym.P))
		for ; addr < sym.Value+sym.Size; addr++ {
813
			ctxt.Logf(" %.2x", 0)
814
		}
815
		ctxt.Logf("\n")
816

817
		if ctxt.LinkMode != LinkExternal {
818 819
			continue
		}
820
		for i := range sym.R {
821
			r := &sym.R[i] // Copying sym.Reloc has measurable impact on performance
822 823 824 825 826 827
			rsname := ""
			if r.Sym != nil {
				rsname = r.Sym.Name
			}
			typ := "?"
			switch r.Type {
828
			case objabi.R_ADDR:
829
				typ = "addr"
830
			case objabi.R_PCREL:
831
				typ = "pcrel"
832
			case objabi.R_CALL:
833
				typ = "call"
834
			}
835
			ctxt.Logf("\treloc %.8x/%d %s %s+%#x [%#x]\n", uint(sym.Value+int64(r.Off)), r.Siz, typ, rsname, r.Add, r.Sym.Value+r.Add)
836 837 838 839
		}
	}

	if addr < eaddr {
840
		ctxt.Logf("\t%.8x| 00 ...\n", uint(addr))
841
	}
842
	ctxt.Logf("\t%.8x|\n", uint(eaddr))
843 844
}

845
func Dwarfblk(ctxt *Link, addr int64, size int64) {
846
	if *flagA {
847
		ctxt.Logf("dwarfblk [%#x,%#x) at offset %#x\n", addr, addr+size, ctxt.Out.Offset())
848 849
	}

850
	blk(ctxt, dwarfp, addr, size, zeros[:])
851 852
}

853
var zeros [512]byte
854

855 856 857 858
var (
	strdata  = make(map[string]string)
	strnames []string
)
859

860
func addstrdata1(ctxt *Link, arg string) {
861
	eq := strings.Index(arg, "=")
862 863
	dot := strings.LastIndex(arg[:eq+1], ".")
	if eq < 0 || dot < 0 {
864
		Exitf("-X flag requires argument of the form importpath.name=value")
865
	}
866
	pkg := arg[:dot]
867
	if ctxt.BuildMode == BuildModePlugin && pkg == "main" {
868 869
		pkg = *flagPluginPath
	}
870
	pkg = objabi.PathToPrefix(pkg)
871 872 873 874 875 876
	name := pkg + arg[dot:eq]
	value := arg[eq+1:]
	if _, ok := strdata[name]; !ok {
		strnames = append(strnames, name)
	}
	strdata[name] = value
877 878
}

879
// addstrdata sets the initial value of the string variable name to value.
880 881 882 883 884 885 886 887 888 889 890 891 892 893 894
func addstrdata(ctxt *Link, name, value string) {
	s := ctxt.Syms.ROLookup(name, 0)
	if s == nil || s.Gotype == nil {
		// Not defined in the loaded packages.
		return
	}
	if s.Gotype.Name != "type.string" {
		Errorf(s, "cannot set with -X: not a var of type string (%s)", s.Gotype.Name)
		return
	}
	if s.Type == sym.SBSS {
		s.Type = sym.SDATA
	}

	p := fmt.Sprintf("%s.str", s.Name)
895
	sp := ctxt.Syms.Lookup(p, 0)
896

897
	Addstring(sp, value)
898
	sp.Type = sym.SRODATA
899 900

	s.Size = 0
901 902
	s.P = s.P[:0]
	s.R = s.R[:0]
903
	reachable := s.Attr.Reachable()
904 905
	s.AddAddr(ctxt.Arch, sp)
	s.AddUint(ctxt.Arch, uint64(len(value)))
906 907 908 909

	// addstring, addaddr, etc., mark the symbols as reachable.
	// In this case that is not necessarily true, so stick to what
	// we know before entering this function.
910
	s.Attr.Set(sym.AttrReachable, reachable)
911

912
	sp.Attr.Set(sym.AttrReachable, reachable)
913 914
}

915 916 917
func (ctxt *Link) dostrdata() {
	for _, name := range strnames {
		addstrdata(ctxt, name, strdata[name])
918 919 920
	}
}

921
func Addstring(s *sym.Symbol, str string) int64 {
922
	if s.Type == 0 {
923
		s.Type = sym.SNOPTRDATA
924
	}
925
	s.Attr |= sym.AttrReachable
926
	r := s.Size
927
	if s.Name == ".shstrtab" {
928
		elfsetstring(s, str, int(r))
929
	}
930 931 932 933
	s.P = append(s.P, str...)
	s.P = append(s.P, 0)
	s.Size = int64(len(s.P))
	return r
934 935
}

936 937
// addgostring adds str, as a Go string value, to s. symname is the name of the
// symbol used to define the string data and must be unique per linked object.
938 939 940
func addgostring(ctxt *Link, s *sym.Symbol, symname, str string) {
	sdata := ctxt.Syms.Lookup(symname, 0)
	if sdata.Type != sym.Sxxx {
941
		Errorf(s, "duplicate symname in addgostring: %s", symname)
942
	}
943 944 945 946 947 948
	sdata.Attr |= sym.AttrReachable
	sdata.Attr |= sym.AttrLocal
	sdata.Type = sym.SRODATA
	sdata.Size = int64(len(str))
	sdata.P = []byte(str)
	s.AddAddr(ctxt.Arch, sdata)
949
	s.AddUint(ctxt.Arch, uint64(len(str)))
950 951
}

952
func addinitarrdata(ctxt *Link, s *sym.Symbol) {
953
	p := s.Name + ".ptr"
954
	sp := ctxt.Syms.Lookup(p, 0)
955
	sp.Type = sym.SINITARR
956
	sp.Size = 0
957
	sp.Attr |= sym.AttrDuplicateOK
958
	sp.AddAddr(ctxt.Arch, s)
959 960
}

961
func dosymtype(ctxt *Link) {
962 963
	switch ctxt.BuildMode {
	case BuildModeCArchive, BuildModeCShared:
964 965 966
		for _, s := range ctxt.Syms.Allsym {
			// Create a new entry in the .init_array section that points to the
			// library initializer function.
967 968
			if s.Name == *flagEntrySymbol {
				addinitarrdata(ctxt, s)
969
			}
970
		}
971 972 973
	}
}

974
// symalign returns the required alignment for the given symbol s.
975
func symalign(s *sym.Symbol) int32 {
976
	min := int32(thearch.Minalign)
977
	if s.Align >= min {
978
		return s.Align
979 980
	} else if s.Align != 0 {
		return min
981
	}
982
	if strings.HasPrefix(s.Name, "go.string.") || strings.HasPrefix(s.Name, "type..namedata.") {
983 984
		// String data is just bytes.
		// If we align it, we waste a lot of space to padding.
985
		return min
986
	}
987
	align := int32(thearch.Maxalign)
988
	for int64(align) > s.Size && align > min {
989 990 991 992 993
		align >>= 1
	}
	return align
}

994
func aligndatsize(datsize int64, s *sym.Symbol) int64 {
995 996 997
	return Rnd(datsize, int64(symalign(s)))
}

998
const debugGCProg = false
999

1000
type GCProg struct {
1001
	ctxt *Link
1002
	sym  *sym.Symbol
1003
	w    gcprog.Writer
1004 1005
}

1006 1007
func (p *GCProg) Init(ctxt *Link, name string) {
	p.ctxt = ctxt
1008
	p.sym = ctxt.Syms.Lookup(name, 0)
1009
	p.w.Init(p.writeByte(ctxt))
1010 1011 1012
	if debugGCProg {
		fmt.Fprintf(os.Stderr, "ld: start GCProg %s\n", name)
		p.w.Debug(os.Stderr)
1013 1014 1015
	}
}

1016 1017
func (p *GCProg) writeByte(ctxt *Link) func(x byte) {
	return func(x byte) {
1018
		p.sym.AddUint8(x)
1019
	}
1020 1021
}

1022
func (p *GCProg) End(size int64) {
1023
	p.w.ZeroUntil(size / int64(p.ctxt.Arch.PtrSize))
1024 1025 1026
	p.w.End()
	if debugGCProg {
		fmt.Fprintf(os.Stderr, "ld: end GCProg\n")
1027 1028 1029
	}
}

1030
func (p *GCProg) AddSym(s *sym.Symbol) {
1031
	typ := s.Gotype
1032
	// Things without pointers should be in sym.SNOPTRDATA or sym.SNOPTRBSS;
1033 1034
	// everything we see should have pointers and should therefore have a type.
	if typ == nil {
1035 1036 1037 1038 1039 1040 1041
		switch s.Name {
		case "runtime.data", "runtime.edata", "runtime.bss", "runtime.ebss":
			// Ignore special symbols that are sometimes laid out
			// as real symbols. See comment about dyld on darwin in
			// the address function.
			return
		}
1042
		Errorf(s, "missing Go type information for global symbol: size %d", s.Size)
1043 1044 1045
		return
	}

1046
	ptrsize := int64(p.ctxt.Arch.PtrSize)
1047
	nptr := decodetypePtrdata(p.ctxt.Arch, typ) / ptrsize
1048

1049 1050
	if debugGCProg {
		fmt.Fprintf(os.Stderr, "gcprog sym: %s at %d (ptr=%d+%d)\n", s.Name, s.Value, s.Value/ptrsize, nptr)
1051
	}
1052

1053
	if decodetypeUsegcprog(p.ctxt.Arch, typ) == 0 {
1054
		// Copy pointers from mask into program.
1055
		mask := decodetypeGcmask(p.ctxt, typ)
1056 1057 1058
		for i := int64(0); i < nptr; i++ {
			if (mask[i/8]>>uint(i%8))&1 != 0 {
				p.w.Ptr(s.Value/ptrsize + i)
1059 1060
			}
		}
1061
		return
1062
	}
1063 1064

	// Copy program.
1065
	prog := decodetypeGcprog(p.ctxt, typ)
1066
	p.w.ZeroUntil(s.Value / ptrsize)
1067
	p.w.Append(prog[4:], nptr)
1068 1069
}

1070
// dataSortKey is used to sort a slice of data symbol *sym.Symbol pointers.
1071
// The sort keys are kept inline to improve cache behavior while sorting.
1072
type dataSortKey struct {
1073 1074
	size int64
	name string
1075
	sym  *sym.Symbol
1076 1077
}

1078
type bySizeAndName []dataSortKey
1079

1080 1081 1082 1083 1084 1085
func (d bySizeAndName) Len() int      { return len(d) }
func (d bySizeAndName) Swap(i, j int) { d[i], d[j] = d[j], d[i] }
func (d bySizeAndName) Less(i, j int) bool {
	s1, s2 := d[i], d[j]
	if s1.size != s2.size {
		return s1.size < s2.size
1086
	}
1087
	return s1.name < s2.name
1088 1089
}

1090 1091 1092
// cutoff is the maximum data section size permitted by the linker
// (see issue #9862).
const cutoff = 2e9 // 2 GB (or so; looks better in errors than 2^31)
1093

1094
func checkdatsize(ctxt *Link, datsize int64, symn sym.SymKind) {
1095
	if datsize > cutoff {
1096
		Errorf(nil, "too much data in section %v (over %v bytes)", symn, cutoff)
1097
	}
1098 1099
}

1100 1101
// datap is a collection of reachable data symbols in address order.
// Generated by dodata.
1102
var datap []*sym.Symbol
1103

1104
func (ctxt *Link) dodata() {
1105
	if ctxt.Debugvlog != 0 {
1106
		ctxt.Logf("%5.2f dodata\n", Cputime())
1107 1108
	}

1109
	if ctxt.DynlinkingGo() && ctxt.HeadType == objabi.Hdarwin {
1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128
		// The values in moduledata are filled out by relocations
		// pointing to the addresses of these special symbols.
		// Typically these symbols have no size and are not laid
		// out with their matching section.
		//
		// However on darwin, dyld will find the special symbol
		// in the first loaded module, even though it is local.
		//
		// (An hypothesis, formed without looking in the dyld sources:
		// these special symbols have no size, so their address
		// matches a real symbol. The dynamic linker assumes we
		// want the normal symbol with the same address and finds
		// it in the other module.)
		//
		// To work around this we lay out the symbls whose
		// addresses are vital for multi-module programs to work
		// as normal symbols, and give them a little size.
		bss := ctxt.Syms.Lookup("runtime.bss", 0)
		bss.Size = 8
1129
		bss.Attr.Set(sym.AttrSpecial, false)
1130

1131
		ctxt.Syms.Lookup("runtime.ebss", 0).Attr.Set(sym.AttrSpecial, false)
1132 1133 1134

		data := ctxt.Syms.Lookup("runtime.data", 0)
		data.Size = 8
1135
		data.Attr.Set(sym.AttrSpecial, false)
1136

1137
		ctxt.Syms.Lookup("runtime.edata", 0).Attr.Set(sym.AttrSpecial, false)
1138 1139

		types := ctxt.Syms.Lookup("runtime.types", 0)
1140
		types.Type = sym.STYPE
1141
		types.Size = 8
1142
		types.Attr.Set(sym.AttrSpecial, false)
1143 1144

		etypes := ctxt.Syms.Lookup("runtime.etypes", 0)
1145 1146
		etypes.Type = sym.SFUNCTAB
		etypes.Attr.Set(sym.AttrSpecial, false)
1147 1148
	}

1149
	// Collect data symbols by type into data.
1150
	var data [sym.SXREF][]*sym.Symbol
1151
	for _, s := range ctxt.Syms.Allsym {
1152
		if !s.Attr.Reachable() || s.Attr.Special() || s.Attr.SubSymbol() {
1153 1154
			continue
		}
1155
		if s.Type <= sym.STEXT || s.Type >= sym.SXREF {
1156
			continue
1157
		}
1158
		data[s.Type] = append(data[s.Type], s)
1159 1160
	}

1161 1162 1163 1164 1165 1166
	// Now that we have the data symbols, but before we start
	// to assign addresses, record all the necessary
	// dynamic relocations. These will grow the relocation
	// symbol, which is itself data.
	//
	// On darwin, we need the symbol table numbers for dynreloc.
1167
	if ctxt.HeadType == objabi.Hdarwin {
1168
		machosymorder(ctxt)
1169
	}
1170
	dynreloc(ctxt, &data)
1171

1172
	if ctxt.UseRelro() {
1173 1174 1175
		// "read only" data with relocations needs to go in its own section
		// when building a shared library. We do this by boosting objects of
		// type SXXX with relocations to type SXXXRELRO.
1176 1177
		for _, symnro := range sym.ReadOnly {
			symnrelro := sym.RelROMap[symnro]
1178

1179
			ro := []*sym.Symbol{}
1180 1181 1182 1183 1184
			relro := data[symnrelro]

			for _, s := range data[symnro] {
				isRelro := len(s.R) > 0
				switch s.Type {
1185
				case sym.STYPE, sym.STYPERELRO, sym.SGOFUNCRELRO:
1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198
					// Symbols are not sorted yet, so it is possible
					// that an Outer symbol has been changed to a
					// relro Type before it reaches here.
					isRelro = true
				}
				if isRelro {
					s.Type = symnrelro
					if s.Outer != nil {
						s.Outer.Type = s.Type
					}
					relro = append(relro, s)
				} else {
					ro = append(ro, s)
1199 1200 1201
				}
			}

1202 1203 1204 1205 1206 1207
			// Check that we haven't made two symbols with the same .Outer into
			// different types (because references two symbols with non-nil Outer
			// become references to the outer symbol + offset it's vital that the
			// symbol and the outer end up in the same section).
			for _, s := range relro {
				if s.Outer != nil && s.Outer.Type != s.Type {
1208 1209
					Errorf(s, "inconsistent types for symbol and its Outer %s (%v != %v)",
						s.Outer.Name, s.Type, s.Outer.Type)
1210 1211
				}
			}
1212 1213 1214

			data[symnro] = ro
			data[symnrelro] = relro
1215 1216 1217
		}
	}

1218
	// Sort symbols.
1219
	var dataMaxAlign [sym.SXREF]int32
1220 1221
	var wg sync.WaitGroup
	for symn := range data {
1222
		symn := sym.SymKind(symn)
1223 1224
		wg.Add(1)
		go func() {
1225
			data[symn], dataMaxAlign[symn] = dodataSect(ctxt, symn, data[symn])
1226 1227
			wg.Done()
		}()
1228
	}
1229
	wg.Wait()
1230

1231 1232 1233 1234
	// Allocate sections.
	// Data is processed before segtext, because we need
	// to see all symbols in the .data and .bss sections in order
	// to generate garbage collection information.
Russ Cox's avatar
Russ Cox committed
1235
	datsize := int64(0)
1236

1237
	// Writable data sections that do not need any specialized handling.
1238 1239 1240 1241 1242
	writable := []sym.SymKind{
		sym.SELFSECT,
		sym.SMACHO,
		sym.SMACHOGOT,
		sym.SWINDOWS,
1243
	}
1244
	for _, symn := range writable {
1245
		for _, s := range data[symn] {
1246
			sect := addsection(ctxt.Arch, &Segdata, s.Name, 06)
1247 1248 1249 1250
			sect.Align = symalign(s)
			datsize = Rnd(datsize, int64(sect.Align))
			sect.Vaddr = uint64(datsize)
			s.Sect = sect
1251
			s.Type = sym.SDATA
1252
			s.Value = int64(uint64(datsize) - sect.Vaddr)
1253
			datsize += s.Size
1254 1255
			sect.Length = uint64(datsize) - sect.Vaddr
		}
1256
		checkdatsize(ctxt, datsize, symn)
1257 1258
	}

1259
	// .got (and .toc on ppc64)
1260
	if len(data[sym.SELFGOT]) > 0 {
1261
		sect := addsection(ctxt.Arch, &Segdata, ".got", 06)
1262
		sect.Align = dataMaxAlign[sym.SELFGOT]
1263 1264
		datsize = Rnd(datsize, int64(sect.Align))
		sect.Vaddr = uint64(datsize)
1265
		for _, s := range data[sym.SELFGOT] {
1266 1267
			datsize = aligndatsize(datsize, s)
			s.Sect = sect
1268
			s.Type = sym.SDATA
1269 1270 1271
			s.Value = int64(uint64(datsize) - sect.Vaddr)

			// Resolve .TOC. symbol for this object file (ppc64)
1272
			toc := ctxt.Syms.ROLookup(".TOC.", int(s.Version))
1273 1274 1275 1276 1277 1278 1279 1280 1281
			if toc != nil {
				toc.Sect = sect
				toc.Outer = s
				toc.Sub = s.Sub
				s.Sub = toc

				toc.Value = 0x8000
			}

1282
			datsize += s.Size
1283
		}
1284
		checkdatsize(ctxt, datsize, sym.SELFGOT)
1285 1286 1287 1288
		sect.Length = uint64(datsize) - sect.Vaddr
	}

	/* pointer-free data */
1289
	sect := addsection(ctxt.Arch, &Segdata, ".noptrdata", 06)
1290
	sect.Align = dataMaxAlign[sym.SNOPTRDATA]
1291 1292
	datsize = Rnd(datsize, int64(sect.Align))
	sect.Vaddr = uint64(datsize)
1293 1294
	ctxt.Syms.Lookup("runtime.noptrdata", 0).Sect = sect
	ctxt.Syms.Lookup("runtime.enoptrdata", 0).Sect = sect
1295
	for _, s := range data[sym.SNOPTRDATA] {
1296 1297
		datsize = aligndatsize(datsize, s)
		s.Sect = sect
1298
		s.Type = sym.SDATA
1299
		s.Value = int64(uint64(datsize) - sect.Vaddr)
1300
		datsize += s.Size
1301
	}
1302
	checkdatsize(ctxt, datsize, sym.SNOPTRDATA)
1303 1304
	sect.Length = uint64(datsize) - sect.Vaddr

1305
	hasinitarr := ctxt.linkShared
1306

1307
	/* shared library initializer */
1308 1309
	switch ctxt.BuildMode {
	case BuildModeCArchive, BuildModeCShared, BuildModeShared, BuildModePlugin:
1310 1311 1312
		hasinitarr = true
	}
	if hasinitarr {
1313
		sect := addsection(ctxt.Arch, &Segdata, ".init_array", 06)
1314
		sect.Align = dataMaxAlign[sym.SINITARR]
1315 1316
		datsize = Rnd(datsize, int64(sect.Align))
		sect.Vaddr = uint64(datsize)
1317
		for _, s := range data[sym.SINITARR] {
1318 1319 1320
			datsize = aligndatsize(datsize, s)
			s.Sect = sect
			s.Value = int64(uint64(datsize) - sect.Vaddr)
1321
			datsize += s.Size
1322 1323
		}
		sect.Length = uint64(datsize) - sect.Vaddr
1324
		checkdatsize(ctxt, datsize, sym.SINITARR)
1325 1326 1327
	}

	/* data */
1328
	sect = addsection(ctxt.Arch, &Segdata, ".data", 06)
1329
	sect.Align = dataMaxAlign[sym.SDATA]
1330 1331
	datsize = Rnd(datsize, int64(sect.Align))
	sect.Vaddr = uint64(datsize)
1332 1333
	ctxt.Syms.Lookup("runtime.data", 0).Sect = sect
	ctxt.Syms.Lookup("runtime.edata", 0).Sect = sect
1334
	var gc GCProg
1335
	gc.Init(ctxt, "runtime.gcdata")
1336
	for _, s := range data[sym.SDATA] {
1337
		s.Sect = sect
1338
		s.Type = sym.SDATA
1339 1340
		datsize = aligndatsize(datsize, s)
		s.Value = int64(uint64(datsize) - sect.Vaddr)
1341
		gc.AddSym(s)
1342
		datsize += s.Size
1343
	}
1344 1345 1346 1347 1348 1349 1350 1351
	// On AIX, TOC entries must be the last of .data
	for _, s := range data[sym.SXCOFFTOC] {
		s.Sect = sect
		s.Type = sym.SDATA
		datsize = aligndatsize(datsize, s)
		s.Value = int64(uint64(datsize) - sect.Vaddr)
		datsize += s.Size
	}
1352
	checkdatsize(ctxt, datsize, sym.SDATA)
1353
	sect.Length = uint64(datsize) - sect.Vaddr
1354
	gc.End(int64(sect.Length))
1355 1356

	/* bss */
1357
	sect = addsection(ctxt.Arch, &Segdata, ".bss", 06)
1358
	sect.Align = dataMaxAlign[sym.SBSS]
1359 1360
	datsize = Rnd(datsize, int64(sect.Align))
	sect.Vaddr = uint64(datsize)
1361 1362
	ctxt.Syms.Lookup("runtime.bss", 0).Sect = sect
	ctxt.Syms.Lookup("runtime.ebss", 0).Sect = sect
1363
	gc = GCProg{}
1364
	gc.Init(ctxt, "runtime.gcbss")
1365
	for _, s := range data[sym.SBSS] {
1366 1367 1368
		s.Sect = sect
		datsize = aligndatsize(datsize, s)
		s.Value = int64(uint64(datsize) - sect.Vaddr)
1369
		gc.AddSym(s)
1370
		datsize += s.Size
1371
	}
1372
	checkdatsize(ctxt, datsize, sym.SBSS)
1373
	sect.Length = uint64(datsize) - sect.Vaddr
1374
	gc.End(int64(sect.Length))
1375 1376

	/* pointer-free bss */
1377
	sect = addsection(ctxt.Arch, &Segdata, ".noptrbss", 06)
1378
	sect.Align = dataMaxAlign[sym.SNOPTRBSS]
1379 1380
	datsize = Rnd(datsize, int64(sect.Align))
	sect.Vaddr = uint64(datsize)
1381 1382
	ctxt.Syms.Lookup("runtime.noptrbss", 0).Sect = sect
	ctxt.Syms.Lookup("runtime.enoptrbss", 0).Sect = sect
1383
	for _, s := range data[sym.SNOPTRBSS] {
1384 1385 1386
		datsize = aligndatsize(datsize, s)
		s.Sect = sect
		s.Value = int64(uint64(datsize) - sect.Vaddr)
1387
		datsize += s.Size
1388 1389 1390
	}

	sect.Length = uint64(datsize) - sect.Vaddr
1391
	ctxt.Syms.Lookup("runtime.end", 0).Sect = sect
1392
	checkdatsize(ctxt, datsize, sym.SNOPTRBSS)
1393

1394 1395
	if len(data[sym.STLSBSS]) > 0 {
		var sect *sym.Section
1396
		if ctxt.IsELF && (ctxt.LinkMode == LinkExternal || !*FlagD) {
1397 1398
			sect = addsection(ctxt.Arch, &Segdata, ".tbss", 06)
			sect.Align = int32(ctxt.Arch.PtrSize)
1399 1400
			sect.Vaddr = 0
		}
1401
		datsize = 0
1402

1403
		for _, s := range data[sym.STLSBSS] {
1404 1405
			datsize = aligndatsize(datsize, s)
			s.Sect = sect
1406
			s.Value = datsize
1407
			datsize += s.Size
1408
		}
1409
		checkdatsize(ctxt, datsize, sym.STLSBSS)
1410

1411 1412
		if sect != nil {
			sect.Length = uint64(datsize)
1413 1414 1415 1416 1417 1418
		}
	}

	/*
	 * We finished data, begin read-only data.
	 * Not all systems support a separate read-only non-executable data section.
1419
	 * ELF and Windows PE systems do.
1420 1421 1422 1423 1424
	 * OS X and Plan 9 do not.
	 * And if we're using external linking mode, the point is moot,
	 * since it's not our decision; that code expects the sections in
	 * segtext.
	 */
1425
	var segro *sym.Segment
1426
	if ctxt.IsELF && ctxt.LinkMode == LinkInternal {
1427
		segro = &Segrodata
1428 1429
	} else if ctxt.HeadType == objabi.Hwindows {
		segro = &Segrodata
1430 1431 1432 1433 1434 1435 1436
	} else {
		segro = &Segtext
	}

	datsize = 0

	/* read-only executable ELF, Mach-O sections */
1437 1438
	if len(data[sym.STEXT]) != 0 {
		Errorf(nil, "dodata found an sym.STEXT symbol: %s", data[sym.STEXT][0].Name)
1439
	}
1440
	for _, s := range data[sym.SELFRXSECT] {
1441
		sect := addsection(ctxt.Arch, &Segtext, s.Name, 04)
1442 1443 1444 1445
		sect.Align = symalign(s)
		datsize = Rnd(datsize, int64(sect.Align))
		sect.Vaddr = uint64(datsize)
		s.Sect = sect
1446
		s.Type = sym.SRODATA
1447
		s.Value = int64(uint64(datsize) - sect.Vaddr)
1448
		datsize += s.Size
1449
		sect.Length = uint64(datsize) - sect.Vaddr
1450
		checkdatsize(ctxt, datsize, sym.SELFRXSECT)
1451 1452 1453
	}

	/* read-only data */
1454
	sect = addsection(ctxt.Arch, segro, ".rodata", 04)
1455 1456

	sect.Vaddr = 0
1457 1458
	ctxt.Syms.Lookup("runtime.rodata", 0).Sect = sect
	ctxt.Syms.Lookup("runtime.erodata", 0).Sect = sect
1459
	if !ctxt.UseRelro() {
1460 1461
		ctxt.Syms.Lookup("runtime.types", 0).Sect = sect
		ctxt.Syms.Lookup("runtime.etypes", 0).Sect = sect
1462
	}
1463
	for _, symn := range sym.ReadOnly {
1464
		align := dataMaxAlign[symn]
1465 1466 1467 1468 1469
		if sect.Align < align {
			sect.Align = align
		}
	}
	datsize = Rnd(datsize, int64(sect.Align))
1470
	for _, symn := range sym.ReadOnly {
1471 1472 1473
		for _, s := range data[symn] {
			datsize = aligndatsize(datsize, s)
			s.Sect = sect
1474
			s.Type = sym.SRODATA
1475
			s.Value = int64(uint64(datsize) - sect.Vaddr)
1476
			datsize += s.Size
1477
		}
1478
		checkdatsize(ctxt, datsize, symn)
1479 1480 1481
	}
	sect.Length = uint64(datsize) - sect.Vaddr

1482
	/* read-only ELF, Mach-O sections */
1483
	for _, s := range data[sym.SELFROSECT] {
1484
		sect = addsection(ctxt.Arch, segro, s.Name, 04)
1485 1486 1487 1488
		sect.Align = symalign(s)
		datsize = Rnd(datsize, int64(sect.Align))
		sect.Vaddr = uint64(datsize)
		s.Sect = sect
1489
		s.Type = sym.SRODATA
1490 1491 1492 1493
		s.Value = int64(uint64(datsize) - sect.Vaddr)
		datsize += s.Size
		sect.Length = uint64(datsize) - sect.Vaddr
	}
1494
	checkdatsize(ctxt, datsize, sym.SELFROSECT)
1495

1496
	for _, s := range data[sym.SMACHOPLT] {
1497
		sect = addsection(ctxt.Arch, segro, s.Name, 04)
1498 1499 1500 1501
		sect.Align = symalign(s)
		datsize = Rnd(datsize, int64(sect.Align))
		sect.Vaddr = uint64(datsize)
		s.Sect = sect
1502
		s.Type = sym.SRODATA
1503 1504 1505 1506
		s.Value = int64(uint64(datsize) - sect.Vaddr)
		datsize += s.Size
		sect.Length = uint64(datsize) - sect.Vaddr
	}
1507
	checkdatsize(ctxt, datsize, sym.SMACHOPLT)
1508

1509 1510 1511 1512 1513 1514 1515 1516 1517 1518
	// There is some data that are conceptually read-only but are written to by
	// relocations. On GNU systems, we can arrange for the dynamic linker to
	// mprotect sections after relocations are applied by giving them write
	// permissions in the object file and calling them ".data.rel.ro.FOO". We
	// divide the .rodata section between actual .rodata and .data.rel.ro.rodata,
	// but for the other sections that this applies to, we just write a read-only
	// .FOO section or a read-write .data.rel.ro.FOO section depending on the
	// situation.
	// TODO(mwhudson): It would make sense to do this more widely, but it makes
	// the system linker segfault on darwin.
1519
	addrelrosection := func(suffix string) *sym.Section {
1520
		return addsection(ctxt.Arch, segro, suffix, 04)
1521
	}
1522

1523
	if ctxt.UseRelro() {
1524
		addrelrosection = func(suffix string) *sym.Section {
1525
			seg := &Segrelrodata
1526
			if ctxt.LinkMode == LinkExternal {
1527 1528 1529 1530 1531 1532 1533 1534
				// Using a separate segment with an external
				// linker results in some programs moving
				// their data sections unexpectedly, which
				// corrupts the moduledata. So we use the
				// rodata segment and let the external linker
				// sort out a rel.ro segment.
				seg = &Segrodata
			}
1535
			return addsection(ctxt.Arch, seg, ".data.rel.ro"+suffix, 06)
1536
		}
1537
		/* data only written by relocations */
1538
		sect = addrelrosection("")
1539 1540

		sect.Vaddr = 0
1541 1542
		ctxt.Syms.Lookup("runtime.types", 0).Sect = sect
		ctxt.Syms.Lookup("runtime.etypes", 0).Sect = sect
1543 1544
		for _, symnro := range sym.ReadOnly {
			symn := sym.RelROMap[symnro]
1545
			align := dataMaxAlign[symn]
1546 1547 1548 1549 1550
			if sect.Align < align {
				sect.Align = align
			}
		}
		datsize = Rnd(datsize, int64(sect.Align))
1551 1552
		for _, symnro := range sym.ReadOnly {
			symn := sym.RelROMap[symnro]
1553 1554 1555
			for _, s := range data[symn] {
				datsize = aligndatsize(datsize, s)
				if s.Outer != nil && s.Outer.Sect != nil && s.Outer.Sect != sect {
1556
					Errorf(s, "s.Outer (%s) in different section from s, %s != %s", s.Outer.Name, s.Outer.Sect.Name, sect.Name)
1557 1558
				}
				s.Sect = sect
1559
				s.Type = sym.SRODATA
1560
				s.Value = int64(uint64(datsize) - sect.Vaddr)
1561
				datsize += s.Size
1562
			}
1563
			checkdatsize(ctxt, datsize, symn)
1564 1565 1566 1567 1568
		}

		sect.Length = uint64(datsize) - sect.Vaddr
	}

1569
	/* typelink */
1570
	sect = addrelrosection(".typelink")
1571
	sect.Align = dataMaxAlign[sym.STYPELINK]
1572 1573
	datsize = Rnd(datsize, int64(sect.Align))
	sect.Vaddr = uint64(datsize)
1574 1575
	typelink := ctxt.Syms.Lookup("runtime.typelink", 0)
	typelink.Sect = sect
1576
	typelink.Type = sym.SRODATA
1577
	datsize += typelink.Size
1578
	checkdatsize(ctxt, datsize, sym.STYPELINK)
1579 1580
	sect.Length = uint64(datsize) - sect.Vaddr

1581
	/* itablink */
1582
	sect = addrelrosection(".itablink")
1583
	sect.Align = dataMaxAlign[sym.SITABLINK]
1584 1585
	datsize = Rnd(datsize, int64(sect.Align))
	sect.Vaddr = uint64(datsize)
1586 1587
	ctxt.Syms.Lookup("runtime.itablink", 0).Sect = sect
	ctxt.Syms.Lookup("runtime.eitablink", 0).Sect = sect
1588
	for _, s := range data[sym.SITABLINK] {
1589 1590
		datsize = aligndatsize(datsize, s)
		s.Sect = sect
1591
		s.Type = sym.SRODATA
1592
		s.Value = int64(uint64(datsize) - sect.Vaddr)
1593
		datsize += s.Size
1594
	}
1595
	checkdatsize(ctxt, datsize, sym.SITABLINK)
1596 1597
	sect.Length = uint64(datsize) - sect.Vaddr

1598
	/* gosymtab */
1599
	sect = addrelrosection(".gosymtab")
1600
	sect.Align = dataMaxAlign[sym.SSYMTAB]
1601 1602
	datsize = Rnd(datsize, int64(sect.Align))
	sect.Vaddr = uint64(datsize)
1603 1604
	ctxt.Syms.Lookup("runtime.symtab", 0).Sect = sect
	ctxt.Syms.Lookup("runtime.esymtab", 0).Sect = sect
1605
	for _, s := range data[sym.SSYMTAB] {
1606 1607
		datsize = aligndatsize(datsize, s)
		s.Sect = sect
1608
		s.Type = sym.SRODATA
1609
		s.Value = int64(uint64(datsize) - sect.Vaddr)
1610
		datsize += s.Size
1611
	}
1612
	checkdatsize(ctxt, datsize, sym.SSYMTAB)
1613 1614 1615
	sect.Length = uint64(datsize) - sect.Vaddr

	/* gopclntab */
1616
	sect = addrelrosection(".gopclntab")
1617
	sect.Align = dataMaxAlign[sym.SPCLNTAB]
1618 1619
	datsize = Rnd(datsize, int64(sect.Align))
	sect.Vaddr = uint64(datsize)
1620 1621
	ctxt.Syms.Lookup("runtime.pclntab", 0).Sect = sect
	ctxt.Syms.Lookup("runtime.epclntab", 0).Sect = sect
1622
	for _, s := range data[sym.SPCLNTAB] {
1623 1624
		datsize = aligndatsize(datsize, s)
		s.Sect = sect
1625
		s.Type = sym.SRODATA
1626
		s.Value = int64(uint64(datsize) - sect.Vaddr)
1627
		datsize += s.Size
1628
	}
1629
	checkdatsize(ctxt, datsize, sym.SRODATA)
1630 1631 1632 1633
	sect.Length = uint64(datsize) - sect.Vaddr

	// 6g uses 4-byte relocation offsets, so the entire segment must fit in 32 bits.
	if datsize != int64(uint32(datsize)) {
1634
		Errorf(nil, "read-only data segment too large: %d", datsize)
1635 1636
	}

1637
	for symn := sym.SELFRXSECT; symn < sym.SXREF; symn++ {
1638 1639 1640
		datap = append(datap, data[symn]...)
	}

1641
	dwarfGenerateDebugSyms(ctxt)
1642

1643
	var i int
1644 1645
	for ; i < len(dwarfp); i++ {
		s := dwarfp[i]
1646
		if s.Type != sym.SDWARFSECT {
1647 1648
			break
		}
1649

1650
		sect = addsection(ctxt.Arch, &Segdwarf, s.Name, 04)
1651 1652 1653 1654
		sect.Align = 1
		datsize = Rnd(datsize, int64(sect.Align))
		sect.Vaddr = uint64(datsize)
		s.Sect = sect
1655
		s.Type = sym.SRODATA
1656
		s.Value = int64(uint64(datsize) - sect.Vaddr)
1657
		datsize += s.Size
1658 1659
		sect.Length = uint64(datsize) - sect.Vaddr
	}
1660
	checkdatsize(ctxt, datsize, sym.SDWARFSECT)
1661

1662 1663
	for i < len(dwarfp) {
		curType := dwarfp[i].Type
1664
		var sect *sym.Section
1665
		switch curType {
1666
		case sym.SDWARFINFO:
1667
			sect = addsection(ctxt.Arch, &Segdwarf, ".debug_info", 04)
1668
		case sym.SDWARFRANGE:
1669
			sect = addsection(ctxt.Arch, &Segdwarf, ".debug_ranges", 04)
1670
		case sym.SDWARFLOC:
1671
			sect = addsection(ctxt.Arch, &Segdwarf, ".debug_loc", 04)
1672 1673 1674 1675
		default:
			Errorf(dwarfp[i], "unknown DWARF section %v", curType)
		}

1676 1677 1678
		sect.Align = 1
		datsize = Rnd(datsize, int64(sect.Align))
		sect.Vaddr = uint64(datsize)
1679 1680 1681
		for ; i < len(dwarfp); i++ {
			s := dwarfp[i]
			if s.Type != curType {
1682 1683
				break
			}
1684
			s.Sect = sect
1685
			s.Type = sym.SRODATA
1686
			s.Value = int64(uint64(datsize) - sect.Vaddr)
1687
			s.Attr |= sym.AttrLocal
1688
			datsize += s.Size
1689 1690
		}
		sect.Length = uint64(datsize) - sect.Vaddr
1691
		checkdatsize(ctxt, datsize, curType)
1692 1693
	}

1694
	/* number the sections */
Russ Cox's avatar
Russ Cox committed
1695
	n := int32(1)
1696

1697
	for _, sect := range Segtext.Sections {
1698 1699 1700
		sect.Extnum = int16(n)
		n++
	}
1701
	for _, sect := range Segrodata.Sections {
1702 1703 1704
		sect.Extnum = int16(n)
		n++
	}
1705
	for _, sect := range Segrelrodata.Sections {
1706 1707 1708
		sect.Extnum = int16(n)
		n++
	}
1709
	for _, sect := range Segdata.Sections {
1710 1711 1712
		sect.Extnum = int16(n)
		n++
	}
1713
	for _, sect := range Segdwarf.Sections {
1714 1715 1716
		sect.Extnum = int16(n)
		n++
	}
1717
}
1718

1719
func dodataSect(ctxt *Link, symn sym.SymKind, syms []*sym.Symbol) (result []*sym.Symbol, maxAlign int32) {
1720
	if ctxt.HeadType == objabi.Hdarwin {
1721 1722
		// Some symbols may no longer belong in syms
		// due to movement in machosymorder.
1723
		newSyms := make([]*sym.Symbol, 0, len(syms))
1724
		for _, s := range syms {
1725
			if s.Type == symn {
1726 1727 1728 1729 1730 1731
				newSyms = append(newSyms, s)
			}
		}
		syms = newSyms
	}

1732
	var head, tail *sym.Symbol
1733 1734
	symsSort := make([]dataSortKey, 0, len(syms))
	for _, s := range syms {
1735 1736 1737
		if s.Attr.OnList() {
			log.Fatalf("symbol %s listed multiple times", s.Name)
		}
1738
		s.Attr |= sym.AttrOnList
1739 1740
		switch {
		case s.Size < int64(len(s.P)):
1741
			Errorf(s, "initialize bounds (%d < %d)", s.Size, len(s.P))
1742
		case s.Size < 0:
1743
			Errorf(s, "negative size (%d bytes)", s.Size)
1744
		case s.Size > cutoff:
1745
			Errorf(s, "symbol too large (%d bytes)", s.Size)
1746 1747
		}

1748 1749 1750
		// If the usually-special section-marker symbols are being laid
		// out as regular symbols, put them either at the beginning or
		// end of their section.
1751
		if ctxt.DynlinkingGo() && ctxt.HeadType == objabi.Hdarwin {
1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762
			switch s.Name {
			case "runtime.text", "runtime.bss", "runtime.data", "runtime.types":
				head = s
				continue
			case "runtime.etext", "runtime.ebss", "runtime.edata", "runtime.etypes":
				tail = s
				continue
			}
		}

		key := dataSortKey{
1763 1764
			size: s.Size,
			name: s.Name,
1765
			sym:  s,
1766 1767 1768
		}

		switch s.Type {
1769
		case sym.SELFGOT:
1770
			// For ppc64, we want to interleave the .got and .toc sections
1771
			// from input files. Both are type sym.SELFGOT, so in that case
1772 1773
			// we skip size comparison and fall through to the name
			// comparison (conveniently, .got sorts before .toc).
1774
			key.size = 0
1775
		}
1776 1777

		symsSort = append(symsSort, key)
1778 1779 1780 1781
	}

	sort.Sort(bySizeAndName(symsSort))

1782 1783 1784 1785 1786
	off := 0
	if head != nil {
		syms[0] = head
		off++
	}
1787
	for i, symSort := range symsSort {
1788
		syms[i+off] = symSort.sym
1789
		align := symalign(symSort.sym)
1790 1791 1792
		if maxAlign < align {
			maxAlign = align
		}
1793
	}
1794 1795 1796
	if tail != nil {
		syms[len(syms)-1] = tail
	}
1797

1798
	if ctxt.IsELF && symn == sym.SELFROSECT {
1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810
		// Make .rela and .rela.plt contiguous, the ELF ABI requires this
		// and Solaris actually cares.
		reli, plti := -1, -1
		for i, s := range syms {
			switch s.Name {
			case ".rel.plt", ".rela.plt":
				plti = i
			case ".rel", ".rela":
				reli = i
			}
		}
		if reli >= 0 && plti >= 0 && plti != reli+1 {
1811 1812 1813 1814 1815
			var first, second int
			if plti > reli {
				first, second = reli, plti
			} else {
				first, second = plti, reli
1816
			}
1817 1818 1819 1820
			rel, plt := syms[reli], syms[plti]
			copy(syms[first+2:], syms[first+1:second])
			syms[first+0] = rel
			syms[first+1] = plt
1821 1822 1823 1824 1825

			// Make sure alignment doesn't introduce a gap.
			// Setting the alignment explicitly prevents
			// symalign from basing it on the size and
			// getting it wrong.
1826 1827
			rel.Align = int32(ctxt.Arch.RegSize)
			plt.Align = int32(ctxt.Arch.RegSize)
1828 1829 1830
		}
	}

1831
	return syms, maxAlign
1832 1833
}

1834 1835 1836 1837 1838
// Add buildid to beginning of text segment, on non-ELF systems.
// Non-ELF binary formats are not always flexible enough to
// give us a place to put the Go build ID. On those systems, we put it
// at the very beginning of the text segment.
// This ``header'' is read by cmd/go.
1839
func (ctxt *Link) textbuildid() {
1840
	if ctxt.IsELF || ctxt.BuildMode == BuildModePlugin || *flagBuildid == "" {
1841 1842 1843
		return
	}

1844 1845
	s := ctxt.Syms.Lookup("go.buildid", 0)
	s.Attr |= sym.AttrReachable
1846 1847
	// The \xff is invalid UTF-8, meant to make it less likely
	// to find one of these accidentally.
1848
	data := "\xff Go build ID: " + strconv.Quote(*flagBuildid) + "\n \xff"
1849 1850 1851
	s.Type = sym.STEXT
	s.P = []byte(data)
	s.Size = int64(len(s.P))
1852

1853 1854
	ctxt.Textp = append(ctxt.Textp, nil)
	copy(ctxt.Textp[1:], ctxt.Textp)
1855
	ctxt.Textp[0] = s
1856
}
1857 1858

// assign addresses to text
1859
func (ctxt *Link) textaddress() {
1860
	addsection(ctxt.Arch, &Segtext, ".text", 05)
1861 1862 1863 1864

	// Assign PCs in text segment.
	// Could parallelize, by assigning to text
	// and then letting threads copy down, but probably not worth it.
1865
	sect := Segtext.Sections[0]
1866 1867

	sect.Align = int32(Funcalign)
1868 1869 1870 1871

	text := ctxt.Syms.Lookup("runtime.text", 0)
	text.Sect = sect

1872
	if ctxt.DynlinkingGo() && ctxt.HeadType == objabi.Hdarwin {
1873 1874 1875 1876 1877 1878 1879 1880
		etext := ctxt.Syms.Lookup("runtime.etext", 0)
		etext.Sect = sect

		ctxt.Textp = append(ctxt.Textp, etext, nil)
		copy(ctxt.Textp[1:], ctxt.Textp)
		ctxt.Textp[0] = text
	}

1881
	va := uint64(*FlagTextAddr)
1882
	n := 1
1883
	sect.Vaddr = va
1884
	ntramps := 0
1885 1886
	for _, s := range ctxt.Textp {
		sect, n, va = assignAddress(ctxt, sect, n, s, va, false)
1887

1888
		trampoline(ctxt, s) // resolve jumps, may add trampolines if jump too far
1889 1890 1891 1892

		// lay down trampolines after each function
		for ; ntramps < len(ctxt.tramps); ntramps++ {
			tramp := ctxt.tramps[ntramps]
1893
			sect, n, va = assignAddress(ctxt, sect, n, tramp, va, true)
1894
		}
1895 1896 1897 1898 1899 1900 1901
	}

	sect.Length = va - sect.Vaddr
	ctxt.Syms.Lookup("runtime.etext", 0).Sect = sect

	// merge tramps into Textp, keeping Textp in address order
	if ntramps != 0 {
1902
		newtextp := make([]*sym.Symbol, 0, len(ctxt.Textp)+ntramps)
1903
		i := 0
1904 1905
		for _, s := range ctxt.Textp {
			for ; i < ntramps && ctxt.tramps[i].Value < s.Value; i++ {
1906 1907
				newtextp = append(newtextp, ctxt.tramps[i])
			}
1908
			newtextp = append(newtextp, s)
1909
		}
1910 1911 1912 1913 1914 1915 1916 1917 1918
		newtextp = append(newtextp, ctxt.tramps[i:ntramps]...)

		ctxt.Textp = newtextp
	}
}

// assigns address for a text symbol, returns (possibly new) section, its number, and the address
// Note: once we have trampoline insertion support for external linking, this function
// will not need to create new text sections, and so no need to return sect and n.
1919
func assignAddress(ctxt *Link, sect *sym.Section, n int, s *sym.Symbol, va uint64, isTramp bool) (*sym.Section, int, uint64) {
1920 1921 1922 1923
	if thearch.AssignAddress != nil {
		return thearch.AssignAddress(ctxt, sect, n, s, va, isTramp)
	}

1924
	s.Sect = sect
1925
	if s.Attr.SubSymbol() {
1926 1927
		return sect, n, va
	}
1928 1929
	if s.Align != 0 {
		va = uint64(Rnd(int64(va), int64(s.Align)))
1930 1931 1932
	} else {
		va = uint64(Rnd(int64(va), int64(Funcalign)))
	}
1933 1934
	s.Value = 0
	for sub := s; sub != nil; sub = sub.Sub {
1935 1936 1937 1938
		sub.Value += int64(va)
	}

	funcsize := uint64(MINFUNC) // spacing required for findfunctab
1939 1940
	if s.Size > MINFUNC {
		funcsize = uint64(s.Size)
1941
	}
1942

1943 1944 1945 1946
	// On ppc64x a text section should not be larger than 2^26 bytes due to the size of
	// call target offset field in the bl instruction.  Splitting into smaller text
	// sections smaller than this limit allows the GNU linker to modify the long calls
	// appropriately.  The limit allows for the space needed for tables inserted by the linker.
1947

1948
	// If this function doesn't fit in the current text section, then create a new one.
1949

1950
	// Only break at outermost syms.
1951

1952
	if ctxt.Arch.InFamily(sys.PPC64) && s.Outer == nil && ctxt.IsELF && ctxt.LinkMode == LinkExternal && va-sect.Vaddr+funcsize+maxSizeTrampolinesPPC64(s, isTramp) > 0x1c00000 {
1953 1954
		// Set the length for the previous text section
		sect.Length = va - sect.Vaddr
1955

1956
		// Create new section, set the starting Vaddr
1957
		sect = addsection(ctxt.Arch, &Segtext, ".text", 05)
1958
		sect.Vaddr = va
1959
		s.Sect = sect
1960

1961 1962 1963
		// Create a symbol for the start of the secondary text sections
		ctxt.Syms.Lookup(fmt.Sprintf("runtime.text.%d", n), 0).Sect = sect
		n++
1964
	}
1965
	va += funcsize
1966

1967
	return sect, n, va
1968 1969
}

1970 1971 1972 1973 1974
// address assigns virtual addresses to all segments and sections and
// returns all segments in file order.
func (ctxt *Link) address() []*sym.Segment {
	var order []*sym.Segment // Layout order

1975
	va := uint64(*FlagTextAddr)
1976
	order = append(order, &Segtext)
1977 1978
	Segtext.Rwx = 05
	Segtext.Vaddr = va
1979
	for _, s := range Segtext.Sections {
1980 1981 1982 1983 1984
		va = uint64(Rnd(int64(va), int64(s.Align)))
		s.Vaddr = va
		va += s.Length
	}

1985
	Segtext.Length = va - uint64(*FlagTextAddr)
1986
	if ctxt.HeadType == objabi.Hnacl {
1987 1988 1989
		va += 32 // room for the "halt sled"
	}

1990
	if len(Segrodata.Sections) > 0 {
1991 1992
		// align to page boundary so as not to mix
		// rodata and executable text.
1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003
		//
		// Note: gold or GNU ld will reduce the size of the executable
		// file by arranging for the relro segment to end at a page
		// boundary, and overlap the end of the text segment with the
		// start of the relro segment in the file.  The PT_LOAD segments
		// will be such that the last page of the text segment will be
		// mapped twice, once r-x and once starting out rw- and, after
		// relocation processing, changed to r--.
		//
		// Ideally the last page of the text segment would not be
		// writable even for this short period.
2004
		va = uint64(Rnd(int64(va), int64(*FlagRound)))
2005

2006
		order = append(order, &Segrodata)
2007 2008
		Segrodata.Rwx = 04
		Segrodata.Vaddr = va
2009
		for _, s := range Segrodata.Sections {
2010 2011 2012 2013 2014 2015 2016
			va = uint64(Rnd(int64(va), int64(s.Align)))
			s.Vaddr = va
			va += s.Length
		}

		Segrodata.Length = va - Segrodata.Vaddr
	}
2017
	if len(Segrelrodata.Sections) > 0 {
2018 2019 2020 2021
		// align to page boundary so as not to mix
		// rodata, rel-ro data, and executable text.
		va = uint64(Rnd(int64(va), int64(*FlagRound)))

2022
		order = append(order, &Segrelrodata)
2023 2024
		Segrelrodata.Rwx = 06
		Segrelrodata.Vaddr = va
2025
		for _, s := range Segrelrodata.Sections {
2026 2027 2028 2029 2030 2031 2032
			va = uint64(Rnd(int64(va), int64(s.Align)))
			s.Vaddr = va
			va += s.Length
		}

		Segrelrodata.Length = va - Segrelrodata.Vaddr
	}
2033

2034
	va = uint64(Rnd(int64(va), int64(*FlagRound)))
2035
	order = append(order, &Segdata)
2036 2037
	Segdata.Rwx = 06
	Segdata.Vaddr = va
2038 2039 2040 2041
	var data *sym.Section
	var noptr *sym.Section
	var bss *sym.Section
	var noptrbss *sym.Section
2042
	for i, s := range Segdata.Sections {
2043
		if ctxt.IsELF && s.Name == ".tbss" {
2044 2045
			continue
		}
2046
		vlen := int64(s.Length)
2047
		if i+1 < len(Segdata.Sections) && !(ctxt.IsELF && Segdata.Sections[i+1].Name == ".tbss") {
2048
			vlen = int64(Segdata.Sections[i+1].Vaddr - s.Vaddr)
2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066
		}
		s.Vaddr = va
		va += uint64(vlen)
		Segdata.Length = va - Segdata.Vaddr
		if s.Name == ".data" {
			data = s
		}
		if s.Name == ".noptrdata" {
			noptr = s
		}
		if s.Name == ".bss" {
			bss = s
		}
		if s.Name == ".noptrbss" {
			noptrbss = s
		}
	}

2067 2068
	// Assign Segdata's Filelen omitting the BSS. We do this here
	// simply because right now we know where the BSS starts.
2069 2070
	Segdata.Filelen = bss.Vaddr - Segdata.Vaddr

2071
	va = uint64(Rnd(int64(va), int64(*FlagRound)))
2072
	order = append(order, &Segdwarf)
2073 2074
	Segdwarf.Rwx = 06
	Segdwarf.Vaddr = va
2075
	for i, s := range Segdwarf.Sections {
2076
		vlen := int64(s.Length)
2077 2078
		if i+1 < len(Segdwarf.Sections) {
			vlen = int64(Segdwarf.Sections[i+1].Vaddr - s.Vaddr)
2079 2080 2081
		}
		s.Vaddr = va
		va += uint64(vlen)
2082
		if ctxt.HeadType == objabi.Hwindows {
2083 2084 2085 2086 2087
			va = uint64(Rnd(int64(va), PEFILEALIGN))
		}
		Segdwarf.Length = va - Segdwarf.Vaddr
	}

2088
	var (
2089
		text     = Segtext.Sections[0]
2090 2091 2092 2093 2094
		rodata   = ctxt.Syms.Lookup("runtime.rodata", 0).Sect
		itablink = ctxt.Syms.Lookup("runtime.itablink", 0).Sect
		symtab   = ctxt.Syms.Lookup("runtime.symtab", 0).Sect
		pclntab  = ctxt.Syms.Lookup("runtime.pclntab", 0).Sect
		types    = ctxt.Syms.Lookup("runtime.types", 0).Sect
2095
	)
2096 2097
	lasttext := text
	// Could be multiple .text sections
2098 2099 2100 2101
	for _, sect := range Segtext.Sections {
		if sect.Name == ".text" {
			lasttext = sect
		}
2102
	}
2103

2104 2105 2106
	for _, s := range datap {
		if s.Sect != nil {
			s.Value += int64(s.Sect.Vaddr)
2107
		}
2108 2109
		for sub := s.Sub; sub != nil; sub = sub.Sub {
			sub.Value += s.Value
2110 2111
		}
	}
2112

2113 2114 2115
	for _, s := range dwarfp {
		if s.Sect != nil {
			s.Value += int64(s.Sect.Vaddr)
2116
		}
2117 2118
		for sub := s.Sub; sub != nil; sub = sub.Sub {
			sub.Value += s.Value
2119 2120
		}
	}
2121

2122
	if ctxt.BuildMode == BuildModeShared {
2123 2124
		s := ctxt.Syms.Lookup("go.link.abihashbytes", 0)
		sectSym := ctxt.Syms.Lookup(".note.go.abihash", 0)
2125 2126 2127 2128
		s.Sect = sectSym.Sect
		s.Value = int64(sectSym.Sect.Vaddr + 16)
	}

2129 2130
	ctxt.xdefine("runtime.text", sym.STEXT, int64(text.Vaddr))
	ctxt.xdefine("runtime.etext", sym.STEXT, int64(lasttext.Vaddr+lasttext.Length))
2131 2132 2133 2134

	// If there are multiple text sections, create runtime.text.n for
	// their section Vaddr, using n for index
	n := 1
2135
	for _, sect := range Segtext.Sections[1:] {
2136
		if sect.Name != ".text" {
2137 2138
			break
		}
2139 2140 2141
		symname := fmt.Sprintf("runtime.text.%d", n)
		ctxt.xdefine(symname, sym.STEXT, int64(sect.Vaddr))
		n++
2142 2143
	}

2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173
	ctxt.xdefine("runtime.rodata", sym.SRODATA, int64(rodata.Vaddr))
	ctxt.xdefine("runtime.erodata", sym.SRODATA, int64(rodata.Vaddr+rodata.Length))
	ctxt.xdefine("runtime.types", sym.SRODATA, int64(types.Vaddr))
	ctxt.xdefine("runtime.etypes", sym.SRODATA, int64(types.Vaddr+types.Length))
	ctxt.xdefine("runtime.itablink", sym.SRODATA, int64(itablink.Vaddr))
	ctxt.xdefine("runtime.eitablink", sym.SRODATA, int64(itablink.Vaddr+itablink.Length))

	s := ctxt.Syms.Lookup("runtime.gcdata", 0)
	s.Attr |= sym.AttrLocal
	ctxt.xdefine("runtime.egcdata", sym.SRODATA, Symaddr(s)+s.Size)
	ctxt.Syms.Lookup("runtime.egcdata", 0).Sect = s.Sect

	s = ctxt.Syms.Lookup("runtime.gcbss", 0)
	s.Attr |= sym.AttrLocal
	ctxt.xdefine("runtime.egcbss", sym.SRODATA, Symaddr(s)+s.Size)
	ctxt.Syms.Lookup("runtime.egcbss", 0).Sect = s.Sect

	ctxt.xdefine("runtime.symtab", sym.SRODATA, int64(symtab.Vaddr))
	ctxt.xdefine("runtime.esymtab", sym.SRODATA, int64(symtab.Vaddr+symtab.Length))
	ctxt.xdefine("runtime.pclntab", sym.SRODATA, int64(pclntab.Vaddr))
	ctxt.xdefine("runtime.epclntab", sym.SRODATA, int64(pclntab.Vaddr+pclntab.Length))
	ctxt.xdefine("runtime.noptrdata", sym.SNOPTRDATA, int64(noptr.Vaddr))
	ctxt.xdefine("runtime.enoptrdata", sym.SNOPTRDATA, int64(noptr.Vaddr+noptr.Length))
	ctxt.xdefine("runtime.bss", sym.SBSS, int64(bss.Vaddr))
	ctxt.xdefine("runtime.ebss", sym.SBSS, int64(bss.Vaddr+bss.Length))
	ctxt.xdefine("runtime.data", sym.SDATA, int64(data.Vaddr))
	ctxt.xdefine("runtime.edata", sym.SDATA, int64(data.Vaddr+data.Length))
	ctxt.xdefine("runtime.noptrbss", sym.SNOPTRBSS, int64(noptrbss.Vaddr))
	ctxt.xdefine("runtime.enoptrbss", sym.SNOPTRBSS, int64(noptrbss.Vaddr+noptrbss.Length))
	ctxt.xdefine("runtime.end", sym.SBSS, int64(Segdata.Vaddr+Segdata.Length))
2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208

	return order
}

// layout assigns file offsets and lengths to the segments in order.
func (ctxt *Link) layout(order []*sym.Segment) {
	var prev *sym.Segment
	for _, seg := range order {
		if prev == nil {
			seg.Fileoff = uint64(HEADR)
		} else {
			switch ctxt.HeadType {
			default:
				// Assuming the previous segment was
				// aligned, the following rounding
				// should ensure that this segment's
				// VA ≡ Fileoff mod FlagRound.
				seg.Fileoff = uint64(Rnd(int64(prev.Fileoff+prev.Filelen), int64(*FlagRound)))
				if seg.Vaddr%uint64(*FlagRound) != seg.Fileoff%uint64(*FlagRound) {
					Exitf("bad segment rounding (Vaddr=%#x Fileoff=%#x FlagRound=%#x)", seg.Vaddr, seg.Fileoff, *FlagRound)
				}
			case objabi.Hwindows:
				seg.Fileoff = prev.Fileoff + uint64(Rnd(int64(prev.Filelen), PEFILEALIGN))
			case objabi.Hplan9:
				seg.Fileoff = prev.Fileoff + prev.Filelen
			}
		}
		if seg != &Segdata {
			// Link.address already set Segdata.Filelen to
			// account for BSS.
			seg.Filelen = seg.Length
		}
		prev = seg
	}

2209
}
2210 2211

// add a trampoline with symbol s (to be laid down after the current function)
2212 2213 2214 2215
func (ctxt *Link) AddTramp(s *sym.Symbol) {
	s.Type = sym.STEXT
	s.Attr |= sym.AttrReachable
	s.Attr |= sym.AttrOnList
2216 2217 2218 2219 2220
	ctxt.tramps = append(ctxt.tramps, s)
	if *FlagDebugTramp > 0 && ctxt.Debugvlog > 0 {
		ctxt.Logf("trampoline %s inserted\n", s)
	}
}
2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235

// compressSyms compresses syms and returns the contents of the
// compressed section. If the section would get larger, it returns nil.
func compressSyms(ctxt *Link, syms []*sym.Symbol) []byte {
	var total int64
	for _, sym := range syms {
		total += sym.Size
	}

	var buf bytes.Buffer
	buf.Write([]byte("ZLIB"))
	var sizeBytes [8]byte
	binary.BigEndian.PutUint64(sizeBytes[:], uint64(total))
	buf.Write(sizeBytes[:])

2236 2237 2238 2239 2240 2241 2242 2243
	// Using zlib.BestSpeed achieves very nearly the same
	// compression levels of zlib.DefaultCompression, but takes
	// substantially less time. This is important because DWARF
	// compression can be a significant fraction of link time.
	z, err := zlib.NewWriterLevel(&buf, zlib.BestSpeed)
	if err != nil {
		log.Fatalf("NewWriterLevel failed: %s", err)
	}
2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268
	for _, sym := range syms {
		if _, err := z.Write(sym.P); err != nil {
			log.Fatalf("compression failed: %s", err)
		}
		for i := sym.Size - int64(len(sym.P)); i > 0; {
			b := zeros[:]
			if i < int64(len(b)) {
				b = b[:i]
			}
			n, err := z.Write(b)
			if err != nil {
				log.Fatalf("compression failed: %s", err)
			}
			i -= int64(n)
		}
	}
	if err := z.Close(); err != nil {
		log.Fatalf("compression failed: %s", err)
	}
	if int64(buf.Len()) >= total {
		// Compression didn't save any space.
		return nil
	}
	return buf.Bytes()
}