]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/link: move .rel symbol from .rdata into .text
authorAlex Brainman <alex.brainman@gmail.com>
Sat, 16 Jun 2018 06:23:52 +0000 (16:23 +1000)
committerAlex Brainman <alex.brainman@gmail.com>
Fri, 5 Oct 2018 08:54:24 +0000 (08:54 +0000)
.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 <alex.brainman@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
src/cmd/link/internal/ld/data.go
src/cmd/link/internal/ld/main.go
src/cmd/link/internal/ld/pe.go

index ee98aef20dc1997ab5fec55f13d211f90bf49a1f..3cc9e294d29fe3de5b2e5706411e6e6a55f97b30 100644 (file)
@@ -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 {
index 905380a1dbfda2d17450f09c9ca7fcfc7e53d12b..2c5152f2e3d63cb4fa0916374b3e4ba9af40c280 100644 (file)
@@ -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.
index db269c78e5313c14a43a2b902a60a44bc67db3d8..cf197f50b06d7e48dd574deba1c66fad5c97cf59 100644 (file)
@@ -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