-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypelink.go
69 lines (63 loc) · 1.75 KB
/
typelink.go
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
// Copyright 2016 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 ld
import (
"cmd/internal/objabi"
"cmd/link/internal/loader"
"cmd/link/internal/sym"
"slices"
"strings"
)
type typelinkSortKey struct {
TypeStr string
Type loader.Sym
}
// typelink generates the typelink table which is used by reflect.typelinks().
// Types that should be added to the typelinks table are marked with the
// MakeTypelink attribute by the compiler.
func (ctxt *Link) typelink() {
ldr := ctxt.loader
var typelinks []typelinkSortKey
var itabs []loader.Sym
for s := loader.Sym(1); s < loader.Sym(ldr.NSym()); s++ {
if !ldr.AttrReachable(s) {
continue
}
if ldr.IsTypelink(s) {
typelinks = append(typelinks, typelinkSortKey{decodetypeStr(ldr, ctxt.Arch, s), s})
} else if ldr.IsItab(s) {
itabs = append(itabs, s)
}
}
slices.SortFunc(typelinks, func(a, b typelinkSortKey) int {
return strings.Compare(a.TypeStr, b.TypeStr)
})
tl := ldr.CreateSymForUpdate("runtime.typelink", 0)
tl.SetType(sym.STYPELINK)
ldr.SetAttrLocal(tl.Sym(), true)
tl.SetSize(int64(4 * len(typelinks)))
tl.Grow(tl.Size())
relocs := tl.AddRelocs(len(typelinks))
for i, s := range typelinks {
r := relocs.At(i)
r.SetSym(s.Type)
r.SetOff(int32(i * 4))
r.SetSiz(4)
r.SetType(objabi.R_ADDROFF)
}
ptrsize := ctxt.Arch.PtrSize
il := ldr.CreateSymForUpdate("runtime.itablink", 0)
il.SetType(sym.SITABLINK)
ldr.SetAttrLocal(il.Sym(), true)
il.SetSize(int64(ptrsize * len(itabs)))
il.Grow(il.Size())
relocs = il.AddRelocs(len(itabs))
for i, s := range itabs {
r := relocs.At(i)
r.SetSym(s)
r.SetOff(int32(i * ptrsize))
r.SetSiz(uint8(ptrsize))
r.SetType(objabi.R_ADDR)
}
}