Skip to content

Commit df0ac45

Browse files
committed
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 <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: David Chase <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent 1de3131 commit df0ac45

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

src/cmd/link/elf_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,9 @@ func TestPIESize(t *testing.T) {
348348
// difference in size of the .got and .plt
349349
// sections if they exist.
350350
// We ignore unallocated sections.
351+
// There may be gaps between non-writeable and
352+
// writable PT_LOAD segments. We also skip those
353+
// gaps (see issue #36023).
351354

352355
textsize := func(ef *elf.File, name string) uint64 {
353356
for _, s := range ef.Sections {
@@ -383,11 +386,23 @@ func TestPIESize(t *testing.T) {
383386

384387
extrasize := func(ef *elf.File) uint64 {
385388
var ret uint64
389+
// skip unallocated sections
386390
for _, s := range ef.Sections {
387391
if s.Flags&elf.SHF_ALLOC == 0 {
388392
ret += s.Size
389393
}
390394
}
395+
// also skip gaps between PT_LOAD segments
396+
for i := range ef.Progs {
397+
if i == 0 {
398+
continue
399+
}
400+
p1 := ef.Progs[i-1]
401+
p2 := ef.Progs[i]
402+
if p1.Type == elf.PT_LOAD && p2.Type == elf.PT_LOAD {
403+
ret += p2.Off - p1.Off - p1.Filesz
404+
}
405+
}
391406
return ret
392407
}
393408

0 commit comments

Comments
 (0)