Skip to content

Commit ad3ccd9

Browse files
ianlancetaylorgopherbot
authored andcommitted
cmd/link: move pclntab out of relro section
The .gopclntab section should not have any relocations. Move it out of relro to regular rodata. Note that this is tested by tests like TestNoTextrel in cmd/cgo/internal/testshared. The existing test TestMachoSectionsReadOnly looks for sections in a Mach-O file that are read-only after relocations are applied (this is marked by a segment with a flags field set to 0x10). We remove the __gopclntab section, as that section is now read-only at all times, not only after relocating. For #76038 Change-Id: I7f837e423bf1e802509277f5dc7fdd1ed0228e32 Reviewed-on: https://go-review.googlesource.com/c/go/+/718065 Reviewed-by: Russ Cox <[email protected]> Reviewed-by: Cherry Mui <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Auto-Submit: Ian Lance Taylor <[email protected]>
1 parent 43b91e7 commit ad3ccd9

File tree

6 files changed

+43
-42
lines changed

6 files changed

+43
-42
lines changed

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

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1581,7 +1581,7 @@ func (state *dodataState) makeRelroForSharedLib(target *Link) {
15811581
// the relro data.
15821582
isRelro = true
15831583
}
1584-
case sym.SGOFUNC:
1584+
case sym.SGOFUNC, sym.SPCLNTAB:
15851585
// The only SGOFUNC symbols that contain relocations are .stkobj,
15861586
// and their relocations are of type objabi.R_ADDROFF,
15871587
// which always get resolved during linking.
@@ -2119,6 +2119,21 @@ func (state *dodataState) allocateDataSections(ctxt *Link) {
21192119
}
21202120
}
21212121

2122+
/* gopclntab */
2123+
sect = state.allocateNamedSectionAndAssignSyms(segro, ".gopclntab", sym.SPCLNTAB, sym.SRODATA, 04)
2124+
ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.pclntab", 0), sect)
2125+
ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.pcheader", 0), sect)
2126+
ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.funcnametab", 0), sect)
2127+
ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.cutab", 0), sect)
2128+
ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.filetab", 0), sect)
2129+
ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.pctab", 0), sect)
2130+
ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.functab", 0), sect)
2131+
ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.epclntab", 0), sect)
2132+
setCarrierSize(sym.SPCLNTAB, int64(sect.Length))
2133+
if ctxt.HeadType == objabi.Haix {
2134+
xcoffUpdateOuterSize(ctxt, int64(sect.Length), sym.SPCLNTAB)
2135+
}
2136+
21222137
/* read-only ELF, Mach-O sections */
21232138
state.allocateSingleSymSections(segro, sym.SELFROSECT, sym.SRODATA, 04)
21242139

@@ -2238,21 +2253,6 @@ func (state *dodataState) allocateDataSections(ctxt *Link) {
22382253
state.checkdatsize(sym.SITABLINK)
22392254
sect.Length = uint64(state.datsize) - sect.Vaddr
22402255

2241-
/* gopclntab */
2242-
sect = state.allocateNamedSectionAndAssignSyms(seg, genrelrosecname(".gopclntab"), sym.SPCLNTAB, sym.SRODATA, relroSecPerm)
2243-
ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.pclntab", 0), sect)
2244-
ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.pcheader", 0), sect)
2245-
ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.funcnametab", 0), sect)
2246-
ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.cutab", 0), sect)
2247-
ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.filetab", 0), sect)
2248-
ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.pctab", 0), sect)
2249-
ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.functab", 0), sect)
2250-
ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.epclntab", 0), sect)
2251-
setCarrierSize(sym.SPCLNTAB, int64(sect.Length))
2252-
if ctxt.HeadType == objabi.Haix {
2253-
xcoffUpdateOuterSize(ctxt, int64(sect.Length), sym.SPCLNTAB)
2254-
}
2255-
22562256
// 6g uses 4-byte relocation offsets, so the entire segment must fit in 32 bits.
22572257
if state.datsize != int64(uint32(state.datsize)) {
22582258
Errorf("read-only data segment too large: %d", state.datsize)

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1250,11 +1250,12 @@ func elfshbits(linkmode LinkMode, sect *sym.Section) *ElfShdr {
12501250

12511251
func elfshreloc(arch *sys.Arch, sect *sym.Section) *ElfShdr {
12521252
// If main section is SHT_NOBITS, nothing to relocate.
1253-
// Also nothing to relocate in .shstrtab or notes.
1253+
// Also nothing to relocate in .shstrtab or notes or .gopclntab.
12541254
if sect.Vaddr >= sect.Seg.Vaddr+sect.Seg.Filelen {
12551255
return nil
12561256
}
1257-
if sect.Name == ".shstrtab" || sect.Name == ".tbss" {
1257+
switch sect.Name {
1258+
case ".shstrtab", ".tbss", ".gopclntab":
12581259
return nil
12591260
}
12601261
if sect.Elfsect.(*ElfShdr).Type == uint32(elf.SHT_NOTE) {
@@ -1469,6 +1470,7 @@ func (ctxt *Link) doelf() {
14691470
}
14701471
shstrtabAddstring(".elfdata")
14711472
shstrtabAddstring(".rodata")
1473+
shstrtabAddstring(".gopclntab")
14721474
// See the comment about data.rel.ro.FOO section names in data.go.
14731475
relro_prefix := ""
14741476
if ctxt.UseRelro() {
@@ -1477,7 +1479,6 @@ func (ctxt *Link) doelf() {
14771479
}
14781480
shstrtabAddstring(relro_prefix + ".typelink")
14791481
shstrtabAddstring(relro_prefix + ".itablink")
1480-
shstrtabAddstring(relro_prefix + ".gopclntab")
14811482

14821483
if ctxt.IsExternal() {
14831484
*FlagD = true
@@ -1486,7 +1487,6 @@ func (ctxt *Link) doelf() {
14861487
shstrtabAddstring(elfRelType + ".rodata")
14871488
shstrtabAddstring(elfRelType + relro_prefix + ".typelink")
14881489
shstrtabAddstring(elfRelType + relro_prefix + ".itablink")
1489-
shstrtabAddstring(elfRelType + relro_prefix + ".gopclntab")
14901490
shstrtabAddstring(elfRelType + ".noptrdata")
14911491
shstrtabAddstring(elfRelType + ".data")
14921492
if ctxt.UseRelro() {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ func TestElfBindNow(t *testing.T) {
408408
}
409409

410410
// This program is intended to be just big/complicated enough that
411-
// we wind up with decent-sized .data.rel.ro.{typelink,itablink,gopclntab}
411+
// we wind up with decent-sized .data.rel.ro.{typelink,itablink}
412412
// sections.
413413
const ifacecallsProg = `
414414
package main

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,29 +37,29 @@ func TestMachoSectionsReadOnly(t *testing.T) {
3737
args: []string{"-ldflags", "-linkmode=internal"},
3838
prog: prog,
3939
mustInternalLink: true,
40-
wantSecsRO: []string{"__got", "__rodata", "__itablink", "__typelink", "__gopclntab"},
40+
wantSecsRO: []string{"__got", "__rodata", "__itablink", "__typelink"},
4141
},
4242
{
4343
name: "linkmode-external",
4444
args: []string{"-ldflags", "-linkmode=external"},
4545
prog: prog,
4646
mustHaveCGO: true,
47-
wantSecsRO: []string{"__got", "__rodata", "__itablink", "__typelink", "__gopclntab"},
47+
wantSecsRO: []string{"__got", "__rodata", "__itablink", "__typelink"},
4848
},
4949
{
5050
name: "cgo-linkmode-internal",
5151
args: []string{"-ldflags", "-linkmode=external"},
5252
prog: progC,
5353
mustHaveCGO: true,
5454
mustInternalLink: true,
55-
wantSecsRO: []string{"__got", "__rodata", "__itablink", "__typelink", "__gopclntab"},
55+
wantSecsRO: []string{"__got", "__rodata", "__itablink", "__typelink"},
5656
},
5757
{
5858
name: "cgo-linkmode-external",
5959
args: []string{"-ldflags", "-linkmode=external"},
6060
prog: progC,
6161
mustHaveCGO: true,
62-
wantSecsRO: []string{"__got", "__rodata", "__itablink", "__typelink", "__gopclntab"},
62+
wantSecsRO: []string{"__got", "__rodata", "__itablink", "__typelink"},
6363
},
6464
}
6565

@@ -111,7 +111,8 @@ func TestMachoSectionsReadOnly(t *testing.T) {
111111

112112
for _, wsroname := range test.wantSecsRO {
113113
// Now walk the sections. Section should be part of
114-
// some segment that is readonly.
114+
// some segment that is made readonly after
115+
// relocations are appied.
115116
var wsro *macho.Section
116117
foundRO := false
117118
for _, s := range machoFile.Sections {

src/cmd/link/internal/sym/symkind.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ const (
6464
SRODATAFIPSEND // End of FIPS read-only data.
6565
SRODATAEND // End of read-only data.
6666
SFUNCTAB // Appears to be unused, except for runtime.etypes.
67+
SPCLNTAB // Pclntab data.
6768
SELFROSECT // ELF read-only data: relocs, dynamic linking info.
6869

6970
// Read-only, non-executable, dynamically relocatable segment.
@@ -91,7 +92,6 @@ const (
9192

9293
STYPELINK // Type links.
9394
SITABLINK // Itab links.
94-
SPCLNTAB // Pclntab data.
9595

9696
// Allocated writable segment.
9797
SFirstWritable

src/cmd/link/internal/sym/symkind_string.go

Lines changed: 15 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)