Commit ab5a2173 authored by David Crawshaw's avatar David Crawshaw

cmd/link: limit darwin dynlink symbol exports

The pclntable contains pointers to functions. If the function symbol
is exported in a plugin, and there is a matching symbol in the host
binary, then the pclntable of a plugin ends up pointing at the
function in the host module.

This doesn't work because the traceback code expects the pointer to
be in the same module space as the PC value.

So don't export functions that might overlap with the host binary.
This way the pointer stays in its module.

Updates #18190

Change-Id: Ifb77605b35fb0a1e7edeecfd22b1e335ed4bb392
Reviewed-on: https://go-review.googlesource.com/34196
Run-TryBot: David Crawshaw <crawshaw@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: 's avatarIan Lance Taylor <iant@golang.org>
parent 4c71af71
...@@ -9,7 +9,10 @@ import "C" ...@@ -9,7 +9,10 @@ import "C"
import "common" import "common"
func F() int { return 3 } func F() int {
_ = make([]byte, 1<<21) // trigger stack unwind, Issue #18190.
return 3
}
func ReadCommonX() int { func ReadCommonX() int {
return common.X return common.X
......
...@@ -684,6 +684,20 @@ func machosymorder(ctxt *Link) { ...@@ -684,6 +684,20 @@ func machosymorder(ctxt *Link) {
} }
} }
// machoShouldExport reports whether a symbol needs to be exported.
//
// When dynamically linking, all non-local variables and plugin-exported
// symbols need to be exported.
func machoShouldExport(ctxt *Link, s *Symbol) bool {
if !ctxt.DynlinkingGo() || s.Attr.Local() {
return false
}
if Buildmode == BuildmodePlugin && strings.HasPrefix(s.Extname, *flagPluginPath) {
return true
}
return s.Type != obj.STEXT
}
func machosymtab(ctxt *Link) { func machosymtab(ctxt *Link) {
symtab := ctxt.Syms.Lookup(".machosymtab", 0) symtab := ctxt.Syms.Lookup(".machosymtab", 0)
symstr := ctxt.Syms.Lookup(".machosymstr", 0) symstr := ctxt.Syms.Lookup(".machosymstr", 0)
...@@ -692,13 +706,11 @@ func machosymtab(ctxt *Link) { ...@@ -692,13 +706,11 @@ func machosymtab(ctxt *Link) {
s := sortsym[i] s := sortsym[i]
Adduint32(ctxt, symtab, uint32(symstr.Size)) Adduint32(ctxt, symtab, uint32(symstr.Size))
export := machoShouldExport(ctxt, s)
// In normal buildmodes, only add _ to C symbols, as // In normal buildmodes, only add _ to C symbols, as
// Go symbols have dot in the name. // Go symbols have dot in the name.
// if !strings.Contains(s.Extname, ".") || export {
// When dynamically linking, prefix all non-local
// symbols with _ as dlsym on darwin requires it to
// resolve any symbol.
if !strings.Contains(s.Extname, ".") || (ctxt.DynlinkingGo() && !s.Attr.Local()) {
Adduint8(ctxt, symstr, '_') Adduint8(ctxt, symstr, '_')
} }
...@@ -711,7 +723,7 @@ func machosymtab(ctxt *Link) { ...@@ -711,7 +723,7 @@ func machosymtab(ctxt *Link) {
Adduint16(ctxt, symtab, 0) // desc Adduint16(ctxt, symtab, 0) // desc
adduintxx(ctxt, symtab, 0, SysArch.PtrSize) // no value adduintxx(ctxt, symtab, 0, SysArch.PtrSize) // no value
} else { } else {
if s.Attr.CgoExport() || (ctxt.DynlinkingGo() && !s.Attr.Local()) { if s.Attr.CgoExport() || export {
Adduint8(ctxt, symtab, 0x0f) Adduint8(ctxt, symtab, 0x0f)
} else { } else {
Adduint8(ctxt, symtab, 0x0e) Adduint8(ctxt, symtab, 0x0e)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment