Skip to content

Commit 5923df1

Browse files
committed
cmd/compile: generate table of main symbol types
For each exported symbol in package main, add its name and type to go.plugin.tabs symbol. This is used by the runtime when loading a plugin to return a typed interface{} value. Change-Id: I23c39583e57180acb8f7a74d218dae4368614f46 Reviewed-on: https://go-review.googlesource.com/27818 Run-TryBot: David Crawshaw <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent 6e703ae commit 5923df1

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

src/cmd/compile/internal/gc/obj.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ func dumpobj1(outfile string, mode int) {
130130
externs := len(externdcl)
131131

132132
dumpglobls()
133+
dumpptabs()
133134
dumptypestructs()
134135

135136
// Dump extra globals.
@@ -163,6 +164,35 @@ func dumpobj1(outfile string, mode int) {
163164
bout.Close()
164165
}
165166

167+
func dumpptabs() {
168+
if !Ctxt.Flag_dynlink || localpkg.Name != "main" {
169+
return
170+
}
171+
for _, exportn := range exportlist {
172+
s := exportn.Sym
173+
n := s.Def
174+
if n == nil {
175+
continue
176+
}
177+
if n.Op != ONAME {
178+
continue
179+
}
180+
if !exportname(s.Name) {
181+
continue
182+
}
183+
if s.Pkg.Name != "main" {
184+
continue
185+
}
186+
if n.Type.Etype == TFUNC && n.Class == PFUNC {
187+
// function
188+
ptabs = append(ptabs, ptabEntry{s: s, t: s.Def.Type})
189+
} else {
190+
// variable
191+
ptabs = append(ptabs, ptabEntry{s: s, t: typPtr(s.Def.Type)})
192+
}
193+
}
194+
}
195+
166196
func dumpglobls() {
167197
// add globals
168198
for _, n := range externdcl {

src/cmd/compile/internal/gc/reflect.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,15 @@ type itabEntry struct {
1818
sym *Sym
1919
}
2020

21+
type ptabEntry struct {
22+
s *Sym
23+
t *Type
24+
}
25+
2126
// runtime interface and reflection data structures
2227
var signatlist []*Node
2328
var itabs []itabEntry
29+
var ptabs []ptabEntry
2430

2531
type Sig struct {
2632
name string
@@ -1405,6 +1411,31 @@ func dumptypestructs() {
14051411
ggloblsym(ilink, int32(Widthptr), int16(obj.DUPOK|obj.RODATA))
14061412
}
14071413

1414+
// process ptabs
1415+
if localpkg.Name == "main" && len(ptabs) > 0 {
1416+
ot := 0
1417+
s := obj.Linklookup(Ctxt, "go.plugin.tabs", 0)
1418+
for _, p := range ptabs {
1419+
// Dump ptab symbol into go.pluginsym package.
1420+
//
1421+
// type ptab struct {
1422+
// name nameOff
1423+
// typ typeOff // pointer to symbol
1424+
// }
1425+
nsym := dname(p.s.Name, "", nil, true)
1426+
ot = dsymptrOffLSym(s, ot, nsym, 0)
1427+
ot = dsymptrOffLSym(s, ot, Linksym(typesym(p.t)), 0)
1428+
}
1429+
ggloblLSym(s, int32(ot), int16(obj.RODATA))
1430+
1431+
ot = 0
1432+
s = obj.Linklookup(Ctxt, "go.plugin.exports", 0)
1433+
for _, p := range ptabs {
1434+
ot = dsymptrLSym(s, ot, Linksym(p.s), 0)
1435+
}
1436+
ggloblLSym(s, int32(ot), int16(obj.RODATA))
1437+
}
1438+
14081439
// generate import strings for imported packages
14091440
if forceObjFileStability {
14101441
// Sorting the packages is not necessary but to compare binaries created

0 commit comments

Comments
 (0)