From: Alex Brainman Date: Sat, 16 Jun 2018 06:23:52 +0000 (+1000) Subject: cmd/link: move .rel symbol from .rdata into .text X-Git-Tag: go1.12beta1~884 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=8256bcdae0da68644db1e33c0db86f5ac4395c4b;p=gostls13.git cmd/link: move .rel symbol from .rdata into .text .rel symbol type is sym.SELFROSECT, and that makes .rel written into .rdata section. But .rel stores code - jump table used for external C functions. So we have to mark whole .rdata section as executable (IMAGE_SCN_MEM_EXECUTE), because of .rel presence in it. Move .rel into .text section, and make .rdata section non executable. I also had to move code that adjusted the size of .rel symbol before calling textaddress, otherwise textaddress would not calculate size of .text section correctly. Fixes #25926 Change-Id: I4962f5de7b367410154c8709adfcd8472de9ac1a Reviewed-on: https://go-review.googlesource.com/c/125455 Run-TryBot: Alex Brainman TryBot-Result: Gobot Gobot Reviewed-by: Ian Lance Taylor --- diff --git a/src/cmd/link/internal/ld/data.go b/src/cmd/link/internal/ld/data.go index ee98aef20d..3cc9e294d2 100644 --- a/src/cmd/link/internal/ld/data.go +++ b/src/cmd/link/internal/ld/data.go @@ -529,11 +529,7 @@ func (ctxt *Link) reloc() { } } -func windynrelocsym(ctxt *Link, s *sym.Symbol) { - rel := ctxt.Syms.Lookup(".rel", 0) - if s == rel { - return - } +func windynrelocsym(ctxt *Link, rel, s *sym.Symbol) { for ri := range s.R { r := &s.R[ri] targ := r.Sym @@ -576,14 +572,31 @@ func windynrelocsym(ctxt *Link, s *sym.Symbol) { } } -func dynrelocsym(ctxt *Link, s *sym.Symbol) { - if ctxt.HeadType == objabi.Hwindows { - if ctxt.LinkMode == LinkInternal { - windynrelocsym(ctxt, s) - } +// windynrelocsyms generates jump table to C library functions that will be +// added later. windynrelocsyms writes the table into .rel symbol. +func (ctxt *Link) windynrelocsyms() { + if !(ctxt.HeadType == objabi.Hwindows && iscgo && ctxt.LinkMode == LinkInternal) { return } + if ctxt.Debugvlog != 0 { + ctxt.Logf("%5.2f windynrelocsyms\n", Cputime()) + } + + /* relocation table */ + rel := ctxt.Syms.Lookup(".rel", 0) + rel.Attr |= sym.AttrReachable + rel.Type = sym.STEXT + ctxt.Textp = append(ctxt.Textp, rel) + + for _, s := range ctxt.Textp { + if s == rel { + continue + } + windynrelocsym(ctxt, rel, s) + } +} +func dynrelocsym(ctxt *Link, s *sym.Symbol) { for ri := range s.R { r := &s.R[ri] if ctxt.BuildMode == BuildModePIE && ctxt.LinkMode == LinkInternal { @@ -605,9 +618,12 @@ func dynrelocsym(ctxt *Link, s *sym.Symbol) { } func dynreloc(ctxt *Link, data *[sym.SXREF][]*sym.Symbol) { + if ctxt.HeadType == objabi.Hwindows { + return + } // -d suppresses dynamic loader format, so we may as well not // compute these sections or mark their symbols as reachable. - if *FlagD && ctxt.HeadType != objabi.Hwindows { + if *FlagD { return } if ctxt.Debugvlog != 0 { diff --git a/src/cmd/link/internal/ld/main.go b/src/cmd/link/internal/ld/main.go index 905380a1db..2c5152f2e3 100644 --- a/src/cmd/link/internal/ld/main.go +++ b/src/cmd/link/internal/ld/main.go @@ -222,6 +222,7 @@ func Main(arch *sys.Arch, theArch Arch) { ctxt.dostkcheck() if ctxt.HeadType == objabi.Hwindows { ctxt.dope() + ctxt.windynrelocsyms() } ctxt.addexport() thearch.Gentext(ctxt) // trampolines, call stubs, etc. diff --git a/src/cmd/link/internal/ld/pe.go b/src/cmd/link/internal/ld/pe.go index db269c78e5..cf197f50b0 100644 --- a/src/cmd/link/internal/ld/pe.go +++ b/src/cmd/link/internal/ld/pe.go @@ -1461,12 +1461,6 @@ func addPEBaseReloc(ctxt *Link) { } func (ctxt *Link) dope() { - /* relocation table */ - rel := ctxt.Syms.Lookup(".rel", 0) - - rel.Attr |= sym.AttrReachable - rel.Type = sym.SELFROSECT - initdynimport(ctxt) initdynexport(ctxt) } @@ -1534,9 +1528,6 @@ func Asmbpe(ctxt *Link) { // some data symbols (e.g. masks) end up in the .rdata section, and they normally // expect larger alignment requirement than the default text section alignment. ro.characteristics |= IMAGE_SCN_ALIGN_32BYTES - } else { - // TODO(brainman): should not need IMAGE_SCN_MEM_EXECUTE, but I do not know why it carshes without it - ro.characteristics |= IMAGE_SCN_MEM_EXECUTE } ro.checkSegment(&Segrodata) pefile.rdataSect = ro