Skip to content

Commit 8256bcd

Browse files
committed
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 <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent 48e22da commit 8256bcd

File tree

3 files changed

+28
-20
lines changed

3 files changed

+28
-20
lines changed

src/cmd/link/internal/ld/data.go

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -529,11 +529,7 @@ func (ctxt *Link) reloc() {
529529
}
530530
}
531531

532-
func windynrelocsym(ctxt *Link, s *sym.Symbol) {
533-
rel := ctxt.Syms.Lookup(".rel", 0)
534-
if s == rel {
535-
return
536-
}
532+
func windynrelocsym(ctxt *Link, rel, s *sym.Symbol) {
537533
for ri := range s.R {
538534
r := &s.R[ri]
539535
targ := r.Sym
@@ -576,14 +572,31 @@ func windynrelocsym(ctxt *Link, s *sym.Symbol) {
576572
}
577573
}
578574

579-
func dynrelocsym(ctxt *Link, s *sym.Symbol) {
580-
if ctxt.HeadType == objabi.Hwindows {
581-
if ctxt.LinkMode == LinkInternal {
582-
windynrelocsym(ctxt, s)
583-
}
575+
// windynrelocsyms generates jump table to C library functions that will be
576+
// added later. windynrelocsyms writes the table into .rel symbol.
577+
func (ctxt *Link) windynrelocsyms() {
578+
if !(ctxt.HeadType == objabi.Hwindows && iscgo && ctxt.LinkMode == LinkInternal) {
584579
return
585580
}
581+
if ctxt.Debugvlog != 0 {
582+
ctxt.Logf("%5.2f windynrelocsyms\n", Cputime())
583+
}
584+
585+
/* relocation table */
586+
rel := ctxt.Syms.Lookup(".rel", 0)
587+
rel.Attr |= sym.AttrReachable
588+
rel.Type = sym.STEXT
589+
ctxt.Textp = append(ctxt.Textp, rel)
590+
591+
for _, s := range ctxt.Textp {
592+
if s == rel {
593+
continue
594+
}
595+
windynrelocsym(ctxt, rel, s)
596+
}
597+
}
586598

599+
func dynrelocsym(ctxt *Link, s *sym.Symbol) {
587600
for ri := range s.R {
588601
r := &s.R[ri]
589602
if ctxt.BuildMode == BuildModePIE && ctxt.LinkMode == LinkInternal {
@@ -605,9 +618,12 @@ func dynrelocsym(ctxt *Link, s *sym.Symbol) {
605618
}
606619

607620
func dynreloc(ctxt *Link, data *[sym.SXREF][]*sym.Symbol) {
621+
if ctxt.HeadType == objabi.Hwindows {
622+
return
623+
}
608624
// -d suppresses dynamic loader format, so we may as well not
609625
// compute these sections or mark their symbols as reachable.
610-
if *FlagD && ctxt.HeadType != objabi.Hwindows {
626+
if *FlagD {
611627
return
612628
}
613629
if ctxt.Debugvlog != 0 {

src/cmd/link/internal/ld/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ func Main(arch *sys.Arch, theArch Arch) {
222222
ctxt.dostkcheck()
223223
if ctxt.HeadType == objabi.Hwindows {
224224
ctxt.dope()
225+
ctxt.windynrelocsyms()
225226
}
226227
ctxt.addexport()
227228
thearch.Gentext(ctxt) // trampolines, call stubs, etc.

src/cmd/link/internal/ld/pe.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1461,12 +1461,6 @@ func addPEBaseReloc(ctxt *Link) {
14611461
}
14621462

14631463
func (ctxt *Link) dope() {
1464-
/* relocation table */
1465-
rel := ctxt.Syms.Lookup(".rel", 0)
1466-
1467-
rel.Attr |= sym.AttrReachable
1468-
rel.Type = sym.SELFROSECT
1469-
14701464
initdynimport(ctxt)
14711465
initdynexport(ctxt)
14721466
}
@@ -1534,9 +1528,6 @@ func Asmbpe(ctxt *Link) {
15341528
// some data symbols (e.g. masks) end up in the .rdata section, and they normally
15351529
// expect larger alignment requirement than the default text section alignment.
15361530
ro.characteristics |= IMAGE_SCN_ALIGN_32BYTES
1537-
} else {
1538-
// TODO(brainman): should not need IMAGE_SCN_MEM_EXECUTE, but I do not know why it carshes without it
1539-
ro.characteristics |= IMAGE_SCN_MEM_EXECUTE
15401531
}
15411532
ro.checkSegment(&Segrodata)
15421533
pefile.rdataSect = ro

0 commit comments

Comments
 (0)