Skip to content

Commit 285a184

Browse files
committed
debug/pe: pretty section.go code
Introduce (*SectionHeader32).fullName and add documentation comments. Updates golang#15345 Change-Id: I8f3b8ab9492642d62e7aad010c91c68daea3f14b Reviewed-on: https://go-review.googlesource.com/22301 Reviewed-by: Ian Lance Taylor <iant@golang.org> Run-TryBot: Alex Brainman <alex.brainman@gmail.com> TryBot-Result: Gobot Gobot <gobot@golang.org>
1 parent 75b886a commit 285a184

File tree

2 files changed

+27
-10
lines changed

2 files changed

+27
-10
lines changed

src/debug/pe/file.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
"fmt"
1313
"io"
1414
"os"
15-
"strconv"
1615
)
1716

1817
// A File represents an open PE file.
@@ -172,12 +171,9 @@ func NewFile(r io.ReaderAt) (*File, error) {
172171
if err := binary.Read(sr, binary.LittleEndian, sh); err != nil {
173172
return nil, err
174173
}
175-
var name string
176-
if sh.Name[0] == '\x2F' {
177-
si, _ := strconv.Atoi(cstring(sh.Name[1:]))
178-
name, _ = getString(ss, si)
179-
} else {
180-
name = cstring(sh.Name[0:])
174+
name, err := sh.fullName(f.StringTable)
175+
if err != nil {
176+
return nil, err
181177
}
182178
s := new(Section)
183179
s.SectionHeader = SectionHeader{

src/debug/pe/section.go

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ package pe
66

77
import (
88
"io"
9+
"strconv"
910
)
1011

12+
// SectionHeader32 represents real PE COFF section header.
1113
type SectionHeader32 struct {
1214
Name [8]uint8
1315
VirtualSize uint32
@@ -21,6 +23,22 @@ type SectionHeader32 struct {
2123
Characteristics uint32
2224
}
2325

26+
// fullName finds real name of section sh. Normally name is stored
27+
// in sh.Name, but if it is longer then 8 characters, it is stored
28+
// in COFF string table st instead.
29+
func (sh *SectionHeader32) fullName(st StringTable) (string, error) {
30+
if sh.Name[0] != '/' {
31+
return cstring(sh.Name[:]), nil
32+
}
33+
i, err := strconv.Atoi(cstring(sh.Name[1:]))
34+
if err != nil {
35+
return "", err
36+
}
37+
return st.String(uint32(i))
38+
}
39+
40+
// SectionHeader is similar to SectionHeader32 with Name
41+
// field replaced by Go string.
2442
type SectionHeader struct {
2543
Name string
2644
VirtualSize uint32
@@ -34,6 +52,7 @@ type SectionHeader struct {
3452
Characteristics uint32
3553
}
3654

55+
// Section provides access to PE COFF section.
3756
type Section struct {
3857
SectionHeader
3958

@@ -47,7 +66,7 @@ type Section struct {
4766
sr *io.SectionReader
4867
}
4968

50-
// Data reads and returns the contents of the PE section.
69+
// Data reads and returns the contents of the PE section s.
5170
func (s *Section) Data() ([]byte, error) {
5271
dat := make([]byte, s.sr.Size())
5372
n, err := s.sr.ReadAt(dat, 0)
@@ -57,5 +76,7 @@ func (s *Section) Data() ([]byte, error) {
5776
return dat[0:n], err
5877
}
5978

60-
// Open returns a new ReadSeeker reading the PE section.
61-
func (s *Section) Open() io.ReadSeeker { return io.NewSectionReader(s.sr, 0, 1<<63-1) }
79+
// Open returns a new ReadSeeker reading the PE section s.
80+
func (s *Section) Open() io.ReadSeeker {
81+
return io.NewSectionReader(s.sr, 0, 1<<63-1)
82+
}

0 commit comments

Comments
 (0)