-
Notifications
You must be signed in to change notification settings - Fork 7
/
file.go
193 lines (175 loc) · 4.44 KB
/
file.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
// Package coff provides COFF(Common Object File Format)-related
// functionalities.
package coff
import (
"encoding/binary"
"errors"
"fmt"
"io"
"github.com/hallazzang/syso/pkg/common"
)
// common errors
var (
ErrSectionExists = errors.New("section with given name already exists")
ErrSectionNotFound = errors.New("section not found")
)
type rawFileHeader struct {
Machine uint16
NumberOfSections uint16
TimeDateStamp uint32
PointerToSymbolTable uint32
NumberOfSymbols uint32
SizeOfOptionalHeader uint16
Characteristics uint16
}
// File is a COFF file.
type File struct {
sections []*section
symbolsOffset uint32
strings []*_string
stringTable map[string]*_string
stringTableSize uint32
}
// New returns newly created COFF file.
func New() *File {
return &File{
stringTable: make(map[string]*_string),
}
}
// AddSection adds section s to file.
func (f *File) AddSection(s Section) error {
for _, sec := range f.sections {
if sec.Name() == s.Name() {
return ErrSectionExists
}
}
f.sections = append(f.sections, §ion{
Section: s,
})
if len(s.Name()) > 8 {
if _, ok := f.stringTable[s.Name()]; !ok {
str := &_string{
b: []byte(s.Name() + "\x00"), // null-terminated UTF-8 encoded string
}
f.strings = append(f.strings, str)
f.stringTable[s.Name()] = str
}
}
return nil
}
// Section finds a section by its name and returns it if found.
func (f *File) Section(name string) (Section, error) {
for _, s := range f.sections {
if s.Name() == name {
return s.Section, nil
}
}
return nil, ErrSectionNotFound
}
func (f *File) freeze() {
offset := uint32(binary.Size(&rawFileHeader{}))
offset += uint32(binary.Size(&rawSectionHeader{}) * len(f.sections))
for _, s := range f.sections {
s.dataOffset = offset
offset += uint32(s.Size())
}
for _, s := range f.sections {
s.relocationsOffset = offset
offset += uint32(binary.Size(&rawRelocation{}) * len(s.Relocations()))
}
f.symbolsOffset = offset
offset += uint32(binary.Size(&rawSymbol{}) * len(f.sections))
offset += 4 // string table size
so := offset // start offset of string table
for _, s := range f.strings {
s.offset = offset
offset += uint32(len(s.b))
}
f.stringTableSize = offset - so + 4
}
// WriteTo writes COFF file data to w.
func (f *File) WriteTo(w io.Writer) (int64, error) {
var written int64
f.freeze()
n, err := common.BinaryWriteTo(w, &rawFileHeader{
Machine: 0x14c, // IMAGE_FILE_MACHINE_I386
NumberOfSections: uint16(len(f.sections)),
PointerToSymbolTable: f.symbolsOffset,
NumberOfSymbols: uint32(len(f.sections)),
Characteristics: 0x0100, // IMAGE_FILE_32BIT_MACHINE
})
if err != nil {
return written, err
}
written += n
for _, s := range f.sections {
var name [8]byte
if len(s.Name()) > 8 {
copy(name[:], fmt.Sprintf("/%d", f.stringTable[s.Name()].offset))
} else {
copy(name[:], s.Name())
}
n, err := common.BinaryWriteTo(w, &rawSectionHeader{
Name: name,
SizeOfRawData: uint32(s.Size()),
PointerToRawData: s.dataOffset,
PointerToRelocations: s.relocationsOffset,
NumberOfRelocations: uint16(len(s.Relocations())),
Characteristics: 0x40000040, // IMAGE_SCN_MEM_READ|IMAGE_SCN_CNT_INITIALIZED_DATA
})
if err != nil {
return written, err
}
written += n
}
for _, s := range f.sections {
n, err := s.WriteTo(w)
if err != nil {
return written, err
}
written += n
}
for i, s := range f.sections {
for _, r := range s.Relocations() {
n, err := common.BinaryWriteTo(w, &rawRelocation{
VirtualAddress: r.VirtualAddress(),
SymbolTableIndex: uint32(i),
Type: 0x0007, // IMAGE_REL_I386_DIR32NB
})
if err != nil {
return written, err
}
written += n
}
}
for i, s := range f.sections {
var name [8]byte
if len(s.Name()) > 8 {
binary.LittleEndian.PutUint32(name[4:], f.stringTable[s.Name()].offset)
} else {
copy(name[:], s.Name())
}
n, err := common.BinaryWriteTo(w, &rawSymbol{
Name: name,
SectionNumber: uint16(i) + 1,
StorageClass: 3, // IMAGE_SYM_CLASS_STATIC
})
if err != nil {
return written, err
}
written += n
}
n, err = common.BinaryWriteTo(w, f.stringTableSize)
if err != nil {
return written, err
}
written += n
for _, s := range f.strings {
n, err := common.BinaryWriteTo(w, s.b)
if err != nil {
return written, err
}
written += n
}
return written, nil
}