Skip to content

Commit e4a6b84

Browse files
aarzilliianlancetaylor
authored andcommitted
debug/elf: do not read unrelated bytes for SHT_NOBITS sections
SHT_NOBITS sections do not occupy space in the file and their offset is "conceptual", reading their data should return all zeroes instead of reading bytes from the section that follows them. Change-Id: Iaa9634792c1909c3e87dab841dd646cd6dcf9027 Reviewed-on: https://go-review.googlesource.com/c/go/+/375216 Reviewed-by: Emmanuel Odeke <[email protected]> Run-TryBot: Emmanuel Odeke <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent 9c6ecc4 commit e4a6b84

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

src/debug/elf/elf_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,25 @@ func TestNames(t *testing.T) {
4747
}
4848
}
4949
}
50+
51+
func TestNobitsSection(t *testing.T) {
52+
const testdata = "testdata/gcc-amd64-linux-exec"
53+
f, err := Open(testdata)
54+
if err != nil {
55+
t.Fatalf("could not read %s: %v", testdata, err)
56+
}
57+
defer f.Close()
58+
bss := f.Section(".bss")
59+
bssData, err := bss.Data()
60+
if err != nil {
61+
t.Fatalf("error reading .bss section: %v", err)
62+
}
63+
if g, w := uint64(len(bssData)), bss.Size; g != w {
64+
t.Errorf(".bss section length mismatch: got %d, want %d", g, w)
65+
}
66+
for i := range bssData {
67+
if bssData[i] != 0 {
68+
t.Fatalf("unexpected non-zero byte at offset %d: %#x", i, bssData[i])
69+
}
70+
}
71+
}

src/debug/elf/file.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,9 @@ func (f *File) stringTable(link uint32) ([]byte, error) {
120120
// Even if the section is stored compressed in the ELF file,
121121
// the ReadSeeker reads uncompressed data.
122122
func (s *Section) Open() io.ReadSeeker {
123+
if s.Type == SHT_NOBITS {
124+
return io.NewSectionReader(&zeroReader{}, 0, int64(s.Size))
125+
}
123126
if s.Flags&SHF_COMPRESSED == 0 {
124127
return io.NewSectionReader(s.sr, 0, 1<<63-1)
125128
}
@@ -1453,3 +1456,12 @@ func (f *File) DynString(tag DynTag) ([]string, error) {
14531456
}
14541457
return all, nil
14551458
}
1459+
1460+
type zeroReader struct{}
1461+
1462+
func (*zeroReader) ReadAt(p []byte, off int64) (n int, err error) {
1463+
for i := range p {
1464+
p[i] = 0
1465+
}
1466+
return len(p), nil
1467+
}

0 commit comments

Comments
 (0)