Skip to content

Commit 1ca31ea

Browse files
committed
cmd/link/internal/ld: revised bindnow/relro test for ELF
This patch re-enables the portion of the TestElfBindNow test that verifies that selected sections are in a read-only segment. Turns out we can't always check for read-only ".got" on all architectures (on ppc64le for example ".got" will only turn up if there is CGO use), so always look for readonly ".dynamic", but only look for readonly ".got" if the section is present. Updates #45681. Change-Id: I4687ae3cf9a81818268925e17700170ba34204a7 Reviewed-on: https://go-review.googlesource.com/c/go/+/581115 Reviewed-by: Cherry Mui <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent 5702600 commit 1ca31ea

File tree

1 file changed

+22
-9
lines changed

1 file changed

+22
-9
lines changed

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

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -194,11 +194,19 @@ func TestElfBindNow(t *testing.T) {
194194
progC = `package main; import "C"; func main() {}`
195195
)
196196

197+
// Note: for linux/amd64 and linux/arm64, for relro we'll always see
198+
// a .got section when building with -buildmode=pie (in addition
199+
// to .dynamic); for some other less mainstream archs (ppc64le,
200+
// s390) this is not the case (on ppc64le for example we only see
201+
// got refs from C objects). Hence we put ".dynamic" in the 'want RO'
202+
// list below and ".got" in the 'want RO if present".
203+
197204
tests := []struct {
198205
name string
199206
args []string
200207
prog string
201208
wantSecsRO []string
209+
wantSecsROIfPresent []string
202210
mustHaveBuildModePIE bool
203211
mustHaveCGO bool
204212
mustInternalLink bool
@@ -214,7 +222,8 @@ func TestElfBindNow(t *testing.T) {
214222
mustHaveBuildModePIE: true,
215223
mustInternalLink: true,
216224
wantDf1Pie: true,
217-
wantSecsRO: []string{".dynamic", ".got"},
225+
wantSecsRO: []string{".dynamic"},
226+
wantSecsROIfPresent: []string{".got"},
218227
},
219228
{
220229
name: "bindnow-linkmode-internal",
@@ -234,7 +243,8 @@ func TestElfBindNow(t *testing.T) {
234243
wantDfBindNow: true,
235244
wantDf1Now: true,
236245
wantDf1Pie: true,
237-
wantSecsRO: []string{".dynamic", ".got", ".got.plt"},
246+
wantSecsRO: []string{".dynamic"},
247+
wantSecsROIfPresent: []string{".got", ".got.plt"},
238248
},
239249
{
240250
name: "bindnow-pie-linkmode-external",
@@ -245,8 +255,9 @@ func TestElfBindNow(t *testing.T) {
245255
wantDfBindNow: true,
246256
wantDf1Now: true,
247257
wantDf1Pie: true,
258+
wantSecsRO: []string{".dynamic"},
248259
// NB: external linker produces .plt.got, not .got.plt
249-
wantSecsRO: []string{".dynamic", ".got"},
260+
wantSecsROIfPresent: []string{".got", ".got.plt"},
250261
},
251262
}
252263

@@ -339,10 +350,9 @@ func TestElfBindNow(t *testing.T) {
339350
t.Fatalf("DT_FLAGS_1 DF_1_PIE got: %v, want: %v", gotDf1Pie, test.wantDf1Pie)
340351
}
341352

342-
// Skipping this newer portion of the test temporarily pending resolution of problems on ppc64le, loonpg64, possibly others.
343-
if false {
344-
345-
for _, wsroname := range test.wantSecsRO {
353+
wsrolists := [][]string{test.wantSecsRO, test.wantSecsROIfPresent}
354+
for k, wsrolist := range wsrolists {
355+
for _, wsroname := range wsrolist {
346356
// Locate section of interest.
347357
var wsro *elf.Section
348358
for _, s := range elfFile.Sections {
@@ -352,8 +362,11 @@ func TestElfBindNow(t *testing.T) {
352362
}
353363
}
354364
if wsro == nil {
355-
t.Fatalf("test %s: can't locate %q section",
356-
test.name, wsroname)
365+
if k == 0 {
366+
t.Fatalf("test %s: can't locate %q section",
367+
test.name, wsroname)
368+
}
369+
continue
357370
}
358371

359372
// Now walk the program headers. Section should be part of

0 commit comments

Comments
 (0)