summaryrefslogtreecommitdiffstats
path: root/src/cmd/compile/internal/noder/linker.go
blob: 2bc7f7c608ead06af328e49551dfb6f66e607791 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
// UNREVIEWED

// Copyright 2021 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package noder

import (
	"io"

	"cmd/compile/internal/base"
	"cmd/compile/internal/ir"
	"cmd/compile/internal/reflectdata"
	"cmd/compile/internal/types"
	"cmd/internal/goobj"
	"cmd/internal/obj"
)

// This file implements the unified IR linker, which combines the
// local package's stub data with imported package data to produce a
// complete export data file. It also rewrites the compiler's
// extension data sections based on the results of compilation (e.g.,
// the function inlining cost and linker symbol index assignments).
//
// TODO(mdempsky): Using the name "linker" here is confusing, because
// readers are likely to mistake references to it for cmd/link. But
// there's a shortage of good names for "something that combines
// multiple parts into a cohesive whole"... e.g., "assembler" and
// "compiler" are also already taken.

type linker struct {
	pw pkgEncoder

	pkgs  map[string]int
	decls map[*types.Sym]int
}

func (l *linker) relocAll(pr *pkgReader, relocs []relocEnt) []relocEnt {
	res := make([]relocEnt, len(relocs))
	for i, rent := range relocs {
		rent.idx = l.relocIdx(pr, rent.kind, rent.idx)
		res[i] = rent
	}
	return res
}

func (l *linker) relocIdx(pr *pkgReader, k reloc, idx int) int {
	assert(pr != nil)

	absIdx := pr.absIdx(k, idx)

	if newidx := pr.newindex[absIdx]; newidx != 0 {
		return ^newidx
	}

	var newidx int
	switch k {
	case relocString:
		newidx = l.relocString(pr, idx)
	case relocPkg:
		newidx = l.relocPkg(pr, idx)
	case relocObj:
		newidx = l.relocObj(pr, idx)

	default:
		// Generic relocations.
		//
		// TODO(mdempsky): Deduplicate more sections? In fact, I think
		// every section could be deduplicated. This would also be easier
		// if we do external relocations.

		w := l.pw.newEncoderRaw(k)
		l.relocCommon(pr, &w, k, idx)
		newidx = w.idx
	}

	pr.newindex[absIdx] = ^newidx

	return newidx
}

func (l *linker) relocString(pr *pkgReader, idx int) int {
	return l.pw.stringIdx(pr.stringIdx(idx))
}

func (l *linker) relocPkg(pr *pkgReader, idx int) int {
	path := pr.peekPkgPath(idx)

	if newidx, ok := l.pkgs[path]; ok {
		return newidx
	}

	r := pr.newDecoder(relocPkg, idx, syncPkgDef)
	w := l.pw.newEncoder(relocPkg, syncPkgDef)
	l.pkgs[path] = w.idx

	// TODO(mdempsky): We end up leaving an empty string reference here
	// from when the package was originally written as "". Probably not
	// a big deal, but a little annoying. Maybe relocating
	// cross-references in place is the way to go after all.
	w.relocs = l.relocAll(pr, r.relocs)

	_ = r.string() // original path
	w.string(path)

	io.Copy(&w.data, &r.data)

	return w.flush()
}

func (l *linker) relocObj(pr *pkgReader, idx int) int {
	path, name, tag := pr.peekObj(idx)
	sym := types.NewPkg(path, "").Lookup(name)

	if newidx, ok := l.decls[sym]; ok {
		return newidx
	}

	if tag == objStub && path != "builtin" && path != "unsafe" {
		pri, ok := objReader[sym]
		if !ok {
			base.Fatalf("missing reader for %q.%v", path, name)
		}
		assert(ok)

		pr = pri.pr
		idx = pri.idx

		path2, name2, tag2 := pr.peekObj(idx)
		sym2 := types.NewPkg(path2, "").Lookup(name2)
		assert(sym == sym2)
		assert(tag2 != objStub)
	}

	w := l.pw.newEncoderRaw(relocObj)
	wext := l.pw.newEncoderRaw(relocObjExt)
	wname := l.pw.newEncoderRaw(relocName)
	wdict := l.pw.newEncoderRaw(relocObjDict)

	l.decls[sym] = w.idx
	assert(wext.idx == w.idx)
	assert(wname.idx == w.idx)
	assert(wdict.idx == w.idx)

	l.relocCommon(pr, &w, relocObj, idx)
	l.relocCommon(pr, &wname, relocName, idx)
	l.relocCommon(pr, &wdict, relocObjDict, idx)

	var obj *ir.Name
	if path == "" {
		var ok bool
		obj, ok = sym.Def.(*ir.Name)

		// Generic types and functions and declared constraint types won't
		// have definitions.
		// For now, just generically copy their extension data.
		// TODO(mdempsky): Restore assertion.
		if !ok && false {
			base.Fatalf("missing definition for %v", sym)
		}
	}

	if obj != nil {
		wext.sync(syncObject1)
		switch tag {
		case objFunc:
			l.relocFuncExt(&wext, obj)
		case objType:
			l.relocTypeExt(&wext, obj)
		case objVar:
			l.relocVarExt(&wext, obj)
		}
		wext.flush()
	} else {
		l.relocCommon(pr, &wext, relocObjExt, idx)
	}

	return w.idx
}

func (l *linker) relocCommon(pr *pkgReader, w *encoder, k reloc, idx int) {
	r := pr.newDecoderRaw(k, idx)
	w.relocs = l.relocAll(pr, r.relocs)
	io.Copy(&w.data, &r.data)
	w.flush()
}

func (l *linker) pragmaFlag(w *encoder, pragma ir.PragmaFlag) {
	w.sync(syncPragma)
	w.int(int(pragma))
}

func (l *linker) relocFuncExt(w *encoder, name *ir.Name) {
	w.sync(syncFuncExt)

	l.pragmaFlag(w, name.Func.Pragma)
	l.linkname(w, name)

	// Relocated extension data.
	w.bool(true)

	// Record definition ABI so cross-ABI calls can be direct.
	// This is important for the performance of calling some
	// common functions implemented in assembly (e.g., bytealg).
	w.uint64(uint64(name.Func.ABI))

	// Escape analysis.
	for _, fs := range &types.RecvsParams {
		for _, f := range fs(name.Type()).FieldSlice() {
			w.string(f.Note)
		}
	}

	if inl := name.Func.Inl; w.bool(inl != nil) {
		w.len(int(inl.Cost))
		w.bool(inl.CanDelayResults)

		pri, ok := bodyReader[name.Func]
		assert(ok)
		w.reloc(relocBody, l.relocIdx(pri.pr, relocBody, pri.idx))
	}

	w.sync(syncEOF)
}

func (l *linker) relocTypeExt(w *encoder, name *ir.Name) {
	w.sync(syncTypeExt)

	typ := name.Type()

	l.pragmaFlag(w, name.Pragma())

	// For type T, export the index of type descriptor symbols of T and *T.
	l.lsymIdx(w, "", reflectdata.TypeLinksym(typ))
	l.lsymIdx(w, "", reflectdata.TypeLinksym(typ.PtrTo()))

	if typ.Kind() != types.TINTER {
		for _, method := range typ.Methods().Slice() {
			l.relocFuncExt(w, method.Nname.(*ir.Name))
		}
	}
}

func (l *linker) relocVarExt(w *encoder, name *ir.Name) {
	w.sync(syncVarExt)
	l.linkname(w, name)
}

func (l *linker) linkname(w *encoder, name *ir.Name) {
	w.sync(syncLinkname)

	linkname := name.Sym().Linkname
	if !l.lsymIdx(w, linkname, name.Linksym()) {
		w.string(linkname)
	}
}

func (l *linker) lsymIdx(w *encoder, linkname string, lsym *obj.LSym) bool {
	if lsym.PkgIdx > goobj.PkgIdxSelf || (lsym.PkgIdx == goobj.PkgIdxInvalid && !lsym.Indexed()) || linkname != "" {
		w.int64(-1)
		return false
	}

	// For a defined symbol, export its index.
	// For re-exporting an imported symbol, pass its index through.
	w.int64(int64(lsym.SymIdx))
	return true
}

// @@@ Helpers

// TODO(mdempsky): These should probably be removed. I think they're a
// smell that the export data format is not yet quite right.

func (pr *pkgDecoder) peekPkgPath(idx int) string {
	r := pr.newDecoder(relocPkg, idx, syncPkgDef)
	path := r.string()
	if path == "" {
		path = pr.pkgPath
	}
	return path
}

func (pr *pkgDecoder) peekObj(idx int) (string, string, codeObj) {
	r := pr.newDecoder(relocName, idx, syncObject1)
	r.sync(syncSym)
	r.sync(syncPkg)
	path := pr.peekPkgPath(r.reloc(relocPkg))
	name := r.string()
	assert(name != "")

	tag := codeObj(r.code(syncCodeObj))

	return path, name, tag
}