From df0ac450022076631280569e2a157f016dcdab80 Mon Sep 17 00:00:00 2001 From: Cherry Zhang Date: Fri, 6 Dec 2019 16:52:53 -0500 Subject: [PATCH] cmd/link: skip gaps between PT_LOAD segments in TestPIESize There may be gaps between non-writeable and writeable PT_LOAD segments, and the gaps may be large as the segments may have large alignment. Don't count those gaps in file size comparison. Fixes #36023. Change-Id: I68582bdd0f385ac5c6f87d485d476d06bc96db19 Reviewed-on: https://go-review.googlesource.com/c/go/+/210180 Run-TryBot: Cherry Zhang TryBot-Result: Gobot Gobot Reviewed-by: David Chase Reviewed-by: Ian Lance Taylor --- src/cmd/link/elf_test.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/cmd/link/elf_test.go b/src/cmd/link/elf_test.go index f0c7872236..11a7730796 100644 --- a/src/cmd/link/elf_test.go +++ b/src/cmd/link/elf_test.go @@ -348,6 +348,9 @@ func TestPIESize(t *testing.T) { // difference in size of the .got and .plt // sections if they exist. // We ignore unallocated sections. + // There may be gaps between non-writeable and + // writable PT_LOAD segments. We also skip those + // gaps (see issue #36023). textsize := func(ef *elf.File, name string) uint64 { for _, s := range ef.Sections { @@ -383,11 +386,23 @@ func TestPIESize(t *testing.T) { extrasize := func(ef *elf.File) uint64 { var ret uint64 + // skip unallocated sections for _, s := range ef.Sections { if s.Flags&elf.SHF_ALLOC == 0 { ret += s.Size } } + // also skip gaps between PT_LOAD segments + for i := range ef.Progs { + if i == 0 { + continue + } + p1 := ef.Progs[i-1] + p2 := ef.Progs[i] + if p1.Type == elf.PT_LOAD && p2.Type == elf.PT_LOAD { + ret += p2.Off - p1.Off - p1.Filesz + } + } return ret } -- 2.50.0