Commit 285a1843 authored by Alex Brainman's avatar Alex Brainman

debug/pe: pretty section.go code

Introduce (*SectionHeader32).fullName and add documentation comments.

Updates #15345

Change-Id: I8f3b8ab9492642d62e7aad010c91c68daea3f14b
Reviewed-on: https://go-review.googlesource.com/22301Reviewed-by: default avatarIan Lance Taylor <iant@golang.org>
Run-TryBot: Alex Brainman <alex.brainman@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
parent 75b886ab
...@@ -12,7 +12,6 @@ import ( ...@@ -12,7 +12,6 @@ import (
"fmt" "fmt"
"io" "io"
"os" "os"
"strconv"
) )
// A File represents an open PE file. // A File represents an open PE file.
...@@ -172,12 +171,9 @@ func NewFile(r io.ReaderAt) (*File, error) { ...@@ -172,12 +171,9 @@ func NewFile(r io.ReaderAt) (*File, error) {
if err := binary.Read(sr, binary.LittleEndian, sh); err != nil { if err := binary.Read(sr, binary.LittleEndian, sh); err != nil {
return nil, err return nil, err
} }
var name string name, err := sh.fullName(f.StringTable)
if sh.Name[0] == '\x2F' { if err != nil {
si, _ := strconv.Atoi(cstring(sh.Name[1:])) return nil, err
name, _ = getString(ss, si)
} else {
name = cstring(sh.Name[0:])
} }
s := new(Section) s := new(Section)
s.SectionHeader = SectionHeader{ s.SectionHeader = SectionHeader{
......
...@@ -6,8 +6,10 @@ package pe ...@@ -6,8 +6,10 @@ package pe
import ( import (
"io" "io"
"strconv"
) )
// SectionHeader32 represents real PE COFF section header.
type SectionHeader32 struct { type SectionHeader32 struct {
Name [8]uint8 Name [8]uint8
VirtualSize uint32 VirtualSize uint32
...@@ -21,6 +23,22 @@ type SectionHeader32 struct { ...@@ -21,6 +23,22 @@ type SectionHeader32 struct {
Characteristics uint32 Characteristics uint32
} }
// fullName finds real name of section sh. Normally name is stored
// in sh.Name, but if it is longer then 8 characters, it is stored
// in COFF string table st instead.
func (sh *SectionHeader32) fullName(st StringTable) (string, error) {
if sh.Name[0] != '/' {
return cstring(sh.Name[:]), nil
}
i, err := strconv.Atoi(cstring(sh.Name[1:]))
if err != nil {
return "", err
}
return st.String(uint32(i))
}
// SectionHeader is similar to SectionHeader32 with Name
// field replaced by Go string.
type SectionHeader struct { type SectionHeader struct {
Name string Name string
VirtualSize uint32 VirtualSize uint32
...@@ -34,6 +52,7 @@ type SectionHeader struct { ...@@ -34,6 +52,7 @@ type SectionHeader struct {
Characteristics uint32 Characteristics uint32
} }
// Section provides access to PE COFF section.
type Section struct { type Section struct {
SectionHeader SectionHeader
...@@ -47,7 +66,7 @@ type Section struct { ...@@ -47,7 +66,7 @@ type Section struct {
sr *io.SectionReader sr *io.SectionReader
} }
// Data reads and returns the contents of the PE section. // Data reads and returns the contents of the PE section s.
func (s *Section) Data() ([]byte, error) { func (s *Section) Data() ([]byte, error) {
dat := make([]byte, s.sr.Size()) dat := make([]byte, s.sr.Size())
n, err := s.sr.ReadAt(dat, 0) n, err := s.sr.ReadAt(dat, 0)
...@@ -57,5 +76,7 @@ func (s *Section) Data() ([]byte, error) { ...@@ -57,5 +76,7 @@ func (s *Section) Data() ([]byte, error) {
return dat[0:n], err return dat[0:n], err
} }
// Open returns a new ReadSeeker reading the PE section. // Open returns a new ReadSeeker reading the PE section s.
func (s *Section) Open() io.ReadSeeker { return io.NewSectionReader(s.sr, 0, 1<<63-1) } func (s *Section) Open() io.ReadSeeker {
return io.NewSectionReader(s.sr, 0, 1<<63-1)
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment