github.com/dannin/go@v0.0.0-20161031215817-d35dfd405eaa/src/runtime/plugin.go (about) 1 // Copyright 2016 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package runtime 6 7 import "unsafe" 8 9 //go:linkname plugin_lastmoduleinit plugin.lastmoduleinit 10 func plugin_lastmoduleinit() (path string, syms map[string]interface{}) { 11 md := firstmoduledata.next 12 if md == nil { 13 throw("runtime: no plugin module data") 14 } 15 for md.next != nil { 16 md = md.next 17 } 18 if md.typemap != nil { 19 throw("runtime: plugin already initialized") 20 } 21 22 for pmd := &firstmoduledata; pmd != md; pmd = pmd.next { 23 if pmd.pluginpath == md.pluginpath { 24 println("plugin: plugin", md.pluginpath, "already loaded") 25 throw("plugin: plugin already loaded") 26 } 27 28 if inRange(pmd.text, pmd.etext, md.text, md.etext) || 29 inRange(pmd.bss, pmd.ebss, md.bss, md.ebss) || 30 inRange(pmd.data, pmd.edata, md.data, md.edata) || 31 inRange(pmd.types, pmd.etypes, md.types, md.etypes) { 32 println("plugin: new module data overlaps with previous moduledata") 33 println("\tpmd.text-etext=", hex(pmd.text), "-", hex(pmd.etext)) 34 println("\tpmd.bss-ebss=", hex(pmd.bss), "-", hex(pmd.ebss)) 35 println("\tpmd.data-edata=", hex(pmd.data), "-", hex(pmd.edata)) 36 println("\tpmd.types-etypes=", hex(pmd.types), "-", hex(pmd.etypes)) 37 println("\tmd.text-etext=", hex(md.text), "-", hex(md.etext)) 38 println("\tmd.bss-ebss=", hex(md.bss), "-", hex(md.ebss)) 39 println("\tmd.data-edata=", hex(md.data), "-", hex(md.edata)) 40 println("\tmd.types-etypes=", hex(md.types), "-", hex(md.etypes)) 41 throw("plugin: new module data overlaps with previous moduledata") 42 } 43 } 44 45 // Initialize the freshly loaded module. 46 typelinksinit() 47 md.gcdatamask = progToPointerMask((*byte)(unsafe.Pointer(md.gcdata)), md.edata-md.data) 48 md.gcbssmask = progToPointerMask((*byte)(unsafe.Pointer(md.gcbss)), md.ebss-md.bss) 49 50 lock(&ifaceLock) 51 for _, i := range md.itablinks { 52 additab(i, true, false) 53 } 54 unlock(&ifaceLock) 55 56 // Build a map of symbol names to symbols. Here in the runtime 57 // we fill out the first word of the interface, the type. We 58 // pass these zero value interfaces to the plugin package, 59 // where the symbol value is filled in (usually via cgo). 60 // 61 // Because functions are handled specially in the plugin package, 62 // function symbol names are prefixed here with '.' to avoid 63 // a dependency on the reflect package. 64 syms = make(map[string]interface{}, len(md.ptab)) 65 for _, ptab := range md.ptab { 66 symName := resolveNameOff(unsafe.Pointer(md.types), ptab.name) 67 t := (*_type)(unsafe.Pointer(md.types)).typeOff(ptab.typ) 68 var val interface{} 69 valp := (*[2]unsafe.Pointer)(unsafe.Pointer(&val)) 70 (*valp)[0] = unsafe.Pointer(t) 71 72 name := symName.name() 73 if t.kind&kindMask == kindFunc { 74 name = "." + name 75 } 76 syms[name] = val 77 } 78 return md.pluginpath, syms 79 } 80 81 // inRange reports whether v0 or v1 are in the range [r0, r1]. 82 func inRange(r0, r1, v0, v1 uintptr) bool { 83 return (v0 >= r0 && v0 <= r1) || (v1 >= r0 && v1 <= r1) 84 } 85 86 // A ptabEntry is generated by the compiler for each exported function 87 // and global variable in the main package of a plugin. It is used to 88 // initialize the plugin module's symbol map. 89 type ptabEntry struct { 90 name nameOff 91 typ typeOff 92 }