Source file
src/debug/pe/section.go
1
2
3
4
5 package pe
6
7 import (
8 "encoding/binary"
9 "fmt"
10 "io"
11 "strconv"
12 )
13
14
15 type SectionHeader32 struct {
16 Name [8]uint8
17 VirtualSize uint32
18 VirtualAddress uint32
19 SizeOfRawData uint32
20 PointerToRawData uint32
21 PointerToRelocations uint32
22 PointerToLineNumbers uint32
23 NumberOfRelocations uint16
24 NumberOfLineNumbers uint16
25 Characteristics uint32
26 }
27
28
29
30
31 func (sh *SectionHeader32) fullName(st StringTable) (string, error) {
32 if sh.Name[0] != '/' {
33 return cstring(sh.Name[:]), nil
34 }
35 i, err := strconv.Atoi(cstring(sh.Name[1:]))
36 if err != nil {
37 return "", err
38 }
39 return st.String(uint32(i))
40 }
41
42
43
44
45
46 type Reloc struct {
47 VirtualAddress uint32
48 SymbolTableIndex uint32
49 Type uint16
50 }
51
52 func readRelocs(sh *SectionHeader, r io.ReadSeeker) ([]Reloc, error) {
53 if sh.NumberOfRelocations <= 0 {
54 return nil, nil
55 }
56 _, err := r.Seek(int64(sh.PointerToRelocations), seekStart)
57 if err != nil {
58 return nil, fmt.Errorf("fail to seek to %q section relocations: %v", sh.Name, err)
59 }
60 relocs := make([]Reloc, sh.NumberOfRelocations)
61 err = binary.Read(r, binary.LittleEndian, relocs)
62 if err != nil {
63 return nil, fmt.Errorf("fail to read section relocations: %v", err)
64 }
65 return relocs, nil
66 }
67
68
69
70 type SectionHeader struct {
71 Name string
72 VirtualSize uint32
73 VirtualAddress uint32
74 Size uint32
75 Offset uint32
76 PointerToRelocations uint32
77 PointerToLineNumbers uint32
78 NumberOfRelocations uint16
79 NumberOfLineNumbers uint16
80 Characteristics uint32
81 }
82
83
84 type Section struct {
85 SectionHeader
86 Relocs []Reloc
87
88
89
90
91
92
93
94 io.ReaderAt
95 sr *io.SectionReader
96 }
97
98
99 func (s *Section) Data() ([]byte, error) {
100 dat := make([]byte, s.sr.Size())
101 n, err := s.sr.ReadAt(dat, 0)
102 if n == len(dat) {
103 err = nil
104 }
105 return dat[0:n], err
106 }
107
108
109 func (s *Section) Open() io.ReadSeeker {
110 return io.NewSectionReader(s.sr, 0, 1<<63-1)
111 }
112
View as plain text