cmd/internal/ld: put read-only relocated data into .data.rel.ro when making a shared object

Currently Go produces shared libraries that cannot be shared between processes
because they have relocations against the text segment (not text section). This
fixes this by moving some data to sections with magic names recognized by the
static linker.

Fixes #10914
Updates #9210

Change-Id: I7178daadc0ae87953d5a084aa3d580f4e3b46d47
Reviewed-on: https://go-review.googlesource.com/10300
Run-TryBot: Michael Hudson-Doyle <michael.hudson@canonical.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
This commit is contained in:
Michael Hudson-Doyle 2015-05-21 13:07:19 +12:00
parent eaea5ade2b
commit 2c2cbb69c8
13 changed files with 206 additions and 18 deletions

View file

@ -174,6 +174,12 @@ func DynlinkingGo() bool {
return Buildmode == BuildmodeShared || Linkshared
}
// UseRelro returns whether to make use of "read only relocations" aka
// relro.
func UseRelro() bool {
return (Buildmode == BuildmodeCShared || Buildmode == BuildmodeShared) && Iself
}
var (
Thestring string
Thelinkarch *LinkArch
@ -980,6 +986,9 @@ func hostlink() {
argv = append(argv, "-dynamiclib")
} else {
argv = append(argv, "-Wl,-Bsymbolic")
if UseRelro() {
argv = append(argv, "-Wl,-z,relro")
}
argv = append(argv, "-shared")
}
case BuildmodeShared:
@ -991,7 +1000,10 @@ func hostlink() {
// think we may well end up wanting to use -Bsymbolic here
// anyway.
argv = append(argv, "-Wl,-Bsymbolic-functions")
argv = append(argv, "-shared")
if UseRelro() {
argv = append(argv, "-shared")
}
argv = append(argv, "-Wl,-z,relro")
}
if Linkshared && Iself {
@ -1771,6 +1783,12 @@ func genasmsym(put func(*LSym, string, int, int64, int64, int, *LSym)) {
obj.SGOSTRING,
obj.SGOFUNC,
obj.SGCBITS,
obj.STYPERELRO,
obj.SSTRINGRELRO,
obj.SGOSTRINGRELRO,
obj.SGOFUNCRELRO,
obj.SGCBITSRELRO,
obj.SRODATARELRO,
obj.SWINDOWS:
if !s.Reachable {
continue