Commit 8256bcda authored by Alex Brainman's avatar Alex Brainman

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 <alex.brainman@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: default avatarIan Lance Taylor <iant@golang.org>
parent 48e22da1
......@@ -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 {
......
......@@ -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.
......
......@@ -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
......
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