]> Cypherpunks repositories - gostls13.git/commitdiff
debug/elf: do not read unrelated bytes for SHT_NOBITS sections
authorAlessandro Arzilli <alessandro.arzilli@gmail.com>
Tue, 4 Jan 2022 14:47:02 +0000 (15:47 +0100)
committerIan Lance Taylor <iant@golang.org>
Fri, 14 Jan 2022 18:20:45 +0000 (18:20 +0000)
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 <emmanuel@orijtech.com>
Run-TryBot: Emmanuel Odeke <emmanuel@orijtech.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
src/debug/elf/elf_test.go
src/debug/elf/file.go

index a61b491090d173a24235ca66becd05afed23cb86..b8c310dba55f9e3687d0243c567467c6b16e37ae 100644 (file)
@@ -47,3 +47,25 @@ func TestNames(t *testing.T) {
                }
        }
 }
+
+func TestNobitsSection(t *testing.T) {
+       const testdata = "testdata/gcc-amd64-linux-exec"
+       f, err := Open(testdata)
+       if err != nil {
+               t.Fatalf("could not read %s: %v", testdata, err)
+       }
+       defer f.Close()
+       bss := f.Section(".bss")
+       bssData, err := bss.Data()
+       if err != nil {
+               t.Fatalf("error reading .bss section: %v", err)
+       }
+       if g, w := uint64(len(bssData)), bss.Size; g != w {
+               t.Errorf(".bss section length mismatch: got %d, want %d", g, w)
+       }
+       for i := range bssData {
+               if bssData[i] != 0 {
+                       t.Fatalf("unexpected non-zero byte at offset %d: %#x", i, bssData[i])
+               }
+       }
+}
index eefcaab8d6994e6d17148e599444008415550e7a..8c84661c5f7de6e5dffba4b06d6a0e5bbb662dac 100644 (file)
@@ -120,6 +120,9 @@ func (f *File) stringTable(link uint32) ([]byte, error) {
 // Even if the section is stored compressed in the ELF file,
 // the ReadSeeker reads uncompressed data.
 func (s *Section) Open() io.ReadSeeker {
+       if s.Type == SHT_NOBITS {
+               return io.NewSectionReader(&zeroReader{}, 0, int64(s.Size))
+       }
        if s.Flags&SHF_COMPRESSED == 0 {
                return io.NewSectionReader(s.sr, 0, 1<<63-1)
        }
@@ -1453,3 +1456,12 @@ func (f *File) DynString(tag DynTag) ([]string, error) {
        }
        return all, nil
 }
+
+type zeroReader struct{}
+
+func (*zeroReader) ReadAt(p []byte, off int64) (n int, err error) {
+       for i := range p {
+               p[i] = 0
+       }
+       return len(p), nil
+}