-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathelf_interface.go
522 lines (435 loc) · 12.9 KB
/
elf_interface.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
package elf_reader
// This file contains the definition for an ELF file interface that can be used
// to read either 32- or 64-bit ELF files. Boilerplate wrappers for
// implementing this interface are also kept in this file.
import (
"fmt"
)
// This is a 32- or 64-bit agnostic way of reading an ELF file. If needed, one
// can use type assertions to convert instances of this interface into either
// instances of *ELF64File or *ELF32File.
type ELFFile interface {
// Returns the value specified in the ELF header of whether the ELF file is
// an executable, relocatable, shared, or core file.
GetFileType() ELFFileType
// Returns the value specified in the ELF header of the type of machine
// this file targets.
GetMachineType() MachineType
// Returns the number of sections defined in the ELF file.
GetSectionCount() uint16
// Returns the number of segments (program headers) defined in the ELF
// file.
GetSegmentCount() uint16
// Returns the name of the section at the given index.
GetSectionName(index uint16) (string, error)
// Returns the content of the section at the given index.
GetSectionContent(index uint16) ([]byte, error)
// Returns the content of the segment at the given index.
GetSegmentContent(index uint16) ([]byte, error)
// Returns an interface that can be used to access the header metadata for
// the section at the given index.
GetSectionHeader(index uint16) (ELFSectionHeader, error)
// Returns an interface that can be used to access the header metadata for
// the program header (segment) at the given index.
GetProgramHeader(index uint16) (ELFProgramHeader, error)
// Returns true if the section at the given index is a string table.
IsStringTable(index uint16) bool
// Returns a slice of strings from the string table in the given section
// index.
GetStringTable(index uint16) ([]string, error)
// Returns true if the section at the given index is a symbol table.
IsSymbolTable(index uint16) bool
// Parses the symbol table in the section at the given index, and returns
// a slice of symbols in it. The slice of strings is the list of symbol
// names, in the same order as the symbols themselves.
GetSymbols(index uint16) ([]ELFSymbol, []string, error)
// Returns true if the section at the given index is a relocation table.
IsRelocationTable(index uint16) bool
// Parses the relocation table in the section at the given index, and
// returns a slice of the relocations contained in it.
GetRelocations(index uint16) ([]ELFRelocation, error)
// Returns true if the section at the given index is a dynamic table.
IsDynamicSection(index uint16) bool
// Parses and returns the dynamic linking table at the given section index.
// This may return entries past the end of the actual table, depending on
// the section size, so callers must check for the terminating null entry
// when referring to the returned slice.
DynamicEntries(intex uint16) ([]ELFDynamicEntry, error)
}
func (f *ELF64File) GetFileType() ELFFileType {
return f.Header.Type
}
func (f *ELF32File) GetFileType() ELFFileType {
return f.Header.Type
}
func (f *ELF64File) GetMachineType() MachineType {
return f.Header.Machine
}
func (f *ELF32File) GetMachineType() MachineType {
return f.Header.Machine
}
func (f *ELF64File) GetSectionCount() uint16 {
return f.Header.SectionHeaderEntries
}
func (f *ELF32File) GetSectionCount() uint16 {
return f.Header.SectionHeaderEntries
}
func (f *ELF64File) GetSegmentCount() uint16 {
return f.Header.ProgramHeaderEntries
}
func (f *ELF32File) GetSegmentCount() uint16 {
return f.Header.ProgramHeaderEntries
}
func (f *ELF64File) GetSectionHeader(index uint16) (ELFSectionHeader, error) {
if int(index) >= len(f.Sections) {
return nil, fmt.Errorf("Invalid section index: %d", index)
}
return &(f.Sections[index]), nil
}
func (f *ELF32File) GetSectionHeader(index uint16) (ELFSectionHeader, error) {
if int(index) >= len(f.Sections) {
return nil, fmt.Errorf("Invalid section index: %d", index)
}
return &(f.Sections[index]), nil
}
func (f *ELF64File) GetProgramHeader(index uint16) (ELFProgramHeader, error) {
if int(index) >= len(f.Segments) {
return nil, fmt.Errorf("Invalid segment index: %d", index)
}
return &(f.Segments[index]), nil
}
func (f *ELF32File) GetProgramHeader(index uint16) (ELFProgramHeader, error) {
if int(index) >= len(f.Segments) {
return nil, fmt.Errorf("Invalid segment index: %d", index)
}
return &(f.Segments[index]), nil
}
func (f *ELF64File) GetSymbols(index uint16) ([]ELFSymbol, []string, error) {
table, names, e := f.GetSymbolTable(index)
if e != nil {
return nil, nil, e
}
// We need to convert the table into a list of pointers to satisfy the
// ELFSymbol interface.
toReturn := make([]ELFSymbol, len(table))
for i := range table {
toReturn[i] = &(table[i])
}
return toReturn, names, nil
}
func (f *ELF32File) GetSymbols(index uint16) ([]ELFSymbol, []string, error) {
table, names, e := f.GetSymbolTable(index)
if e != nil {
return nil, nil, e
}
toReturn := make([]ELFSymbol, len(table))
for i := range table {
toReturn[i] = &(table[i])
}
return toReturn, names, nil
}
func (f *ELF64File) GetRelocations(index uint16) ([]ELFRelocation, error) {
// The 64-bit ELF relocation table already satisfies the ELFRelocation
// interface.
values, e := f.GetRelocationTable(index)
if e != nil {
return nil, e
}
toReturn := make([]ELFRelocation, len(values))
for i := range values {
toReturn[i] = values[i]
}
return toReturn, nil
}
func (f *ELF32File) GetRelocations(index uint16) ([]ELFRelocation, error) {
// We need to convert this table into the 64-bit format...
table32, e := f.GetRelocationTable(index)
if e != nil {
return nil, e
}
toReturn := make([]ELFRelocation, len(table32))
for i := range table32 {
original := table32[i]
relocationType := original.Type()
symbolIndex := original.SymbolIndex()
newInfo := ELF64RelocationInfo(relocationType)
newInfo |= ELF64RelocationInfo(symbolIndex) << 32
toReturn[i] = &ELF64Rela{
Address: uint64(original.Offset()),
RelocationInfo: newInfo,
AddendValue: int64(original.Addend()),
}
}
return toReturn, nil
}
func (f *ELF64File) DynamicEntries(index uint16) ([]ELFDynamicEntry, error) {
table, e := f.GetDynamicTable(index)
if e != nil {
return nil, e
}
// Same story as with GetSymbols... we need a slice of interfaces here.
toReturn := make([]ELFDynamicEntry, len(table))
for i := range table {
toReturn[i] = &(table[i])
}
return toReturn, nil
}
func (f *ELF32File) DynamicEntries(index uint16) ([]ELFDynamicEntry, error) {
table, e := f.GetDynamicTable(index)
if e != nil {
return nil, e
}
// Same story as with GetSymbols... we need a slice of interfaces here.
toReturn := make([]ELFDynamicEntry, len(table))
for i := range table {
toReturn[i] = &(table[i])
}
return toReturn, nil
}
// This is a 32- or 64-bit agnostic interface for accessing an ELF section's
// flags. Can be converted using type assertions into either
// SectionHeaderFlags64 or SectionHeaderFlags32 values.
type ELFSectionFlags interface {
Executable() bool
Allocated() bool
Writable() bool
String() string
}
func (f SectionHeaderFlags32) Executable() bool {
return (f & 4) != 0
}
func (f SectionHeaderFlags32) Allocated() bool {
return (f & 2) != 0
}
func (f SectionHeaderFlags32) Writable() bool {
return (f & 1) != 0
}
func (f SectionHeaderFlags64) Executable() bool {
return (f & 4) != 0
}
func (f SectionHeaderFlags64) Allocated() bool {
return (f & 2) != 0
}
func (f SectionHeaderFlags64) Writable() bool {
return (f & 1) != 0
}
// This is a 32- or 64-bit agnostic way of accessing an ELF section header.
type ELFSectionHeader interface {
GetType() SectionHeaderType
GetFlags() ELFSectionFlags
GetVirtualAddress() uint64
GetFileOffset() uint64
GetSize() uint64
GetLinkedIndex() uint32
GetInfo() uint32
GetAlignment() uint64
GetEntrySize() uint64
String() string
}
func (h *ELF64SectionHeader) GetType() SectionHeaderType {
return h.Type
}
func (h *ELF64SectionHeader) GetFlags() ELFSectionFlags {
return h.Flags
}
func (h *ELF64SectionHeader) GetVirtualAddress() uint64 {
return h.VirtualAddress
}
func (h *ELF64SectionHeader) GetFileOffset() uint64 {
return h.FileOffset
}
func (h *ELF64SectionHeader) GetSize() uint64 {
return h.Size
}
func (h *ELF64SectionHeader) GetLinkedIndex() uint32 {
return h.LinkedIndex
}
func (h *ELF64SectionHeader) GetInfo() uint32 {
return h.Info
}
func (h *ELF64SectionHeader) GetAlignment() uint64 {
return h.Align
}
func (h *ELF64SectionHeader) GetEntrySize() uint64 {
return h.EntrySize
}
func (h *ELF32SectionHeader) GetType() SectionHeaderType {
return h.Type
}
func (h *ELF32SectionHeader) GetFlags() ELFSectionFlags {
return h.Flags
}
func (h *ELF32SectionHeader) GetVirtualAddress() uint64 {
return uint64(h.VirtualAddress)
}
func (h *ELF32SectionHeader) GetFileOffset() uint64 {
return uint64(h.FileOffset)
}
func (h *ELF32SectionHeader) GetSize() uint64 {
return uint64(h.Size)
}
func (h *ELF32SectionHeader) GetLinkedIndex() uint32 {
return h.LinkedIndex
}
func (h *ELF32SectionHeader) GetInfo() uint32 {
return h.Info
}
func (h *ELF32SectionHeader) GetAlignment() uint64 {
return uint64(h.Align)
}
func (h *ELF32SectionHeader) GetEntrySize() uint64 {
return uint64(h.EntrySize)
}
// This is a 32- or 64-bit agnostic way of accessing an ELF program header.
type ELFProgramHeader interface {
GetType() ProgramHeaderType
GetFlags() ProgramHeaderFlags
GetFileOffset() uint64
GetVirtualAddress() uint64
GetPhysicalAddress() uint64
GetFileSize() uint64
GetMemorySize() uint64
GetAlignment() uint64
String() string
}
func (h *ELF64ProgramHeader) GetType() ProgramHeaderType {
return h.Type
}
func (h *ELF64ProgramHeader) GetFlags() ProgramHeaderFlags {
return h.Flags
}
func (h *ELF64ProgramHeader) GetFileOffset() uint64 {
return h.FileOffset
}
func (h *ELF64ProgramHeader) GetVirtualAddress() uint64 {
return h.VirtualAddress
}
func (h *ELF64ProgramHeader) GetPhysicalAddress() uint64 {
return h.PhysicalAddress
}
func (h *ELF64ProgramHeader) GetFileSize() uint64 {
return h.FileSize
}
func (h *ELF64ProgramHeader) GetMemorySize() uint64 {
return h.MemorySize
}
func (h *ELF64ProgramHeader) GetAlignment() uint64 {
return h.Align
}
func (h *ELF32ProgramHeader) GetType() ProgramHeaderType {
return h.Type
}
func (h *ELF32ProgramHeader) GetFlags() ProgramHeaderFlags {
return h.Flags
}
func (h *ELF32ProgramHeader) GetFileOffset() uint64 {
return uint64(h.FileOffset)
}
func (h *ELF32ProgramHeader) GetVirtualAddress() uint64 {
return uint64(h.VirtualAddress)
}
func (h *ELF32ProgramHeader) GetPhysicalAddress() uint64 {
return uint64(h.PhysicalAddress)
}
func (h *ELF32ProgramHeader) GetFileSize() uint64 {
return uint64(h.FileSize)
}
func (h *ELF32ProgramHeader) GetMemorySize() uint64 {
return uint64(h.MemorySize)
}
func (h *ELF32ProgramHeader) GetAlignment() uint64 {
return uint64(h.Align)
}
// This is an interface used to access either 64- or 32-bit ELF symbol table
// entries.
type ELFSymbol interface {
GetName() uint32
GetInfo() ELFSymbolInfo
GetOther() uint8
GetSectionIndex() uint16
GetValue() uint64
GetSize() uint64
String() string
}
func (s *ELF64Symbol) GetName() uint32 {
return s.Name
}
func (s *ELF64Symbol) GetInfo() ELFSymbolInfo {
return s.Info
}
func (s *ELF64Symbol) GetOther() uint8 {
return s.Other
}
func (s *ELF64Symbol) GetSectionIndex() uint16 {
return s.SectionIndex
}
func (s *ELF64Symbol) GetValue() uint64 {
return s.Value
}
func (s *ELF64Symbol) GetSize() uint64 {
return s.Size
}
func (s *ELF32Symbol) GetName() uint32 {
return s.Name
}
func (s *ELF32Symbol) GetInfo() ELFSymbolInfo {
return s.Info
}
func (s *ELF32Symbol) GetOther() uint8 {
return s.Other
}
func (s *ELF32Symbol) GetSectionIndex() uint16 {
return s.SectionIndex
}
func (s *ELF32Symbol) GetValue() uint64 {
return uint64(s.Value)
}
func (s *ELF32Symbol) GetSize() uint64 {
return uint64(s.Size)
}
// This holds a generic entry in a relocation table for either a 32- or 64-bit
// ELF file.
type ELFRelocation interface {
Offset() uint64
Type() uint32
SymbolIndex() uint32
Addend() int64
String() string
}
type ELFDynamicTag interface {
GetValue() int64
String() string
}
func (t ELF64DynamicTag) GetValue() int64 {
return int64(t)
}
func (t ELF32DynamicTag) GetValue() int64 {
return int64(t)
}
type ELFDynamicEntry interface {
GetTag() ELFDynamicTag
GetValue() uint64
}
func (n *ELF64DynamicEntry) GetTag() ELFDynamicTag {
return n.Tag
}
func (n *ELF32DynamicEntry) GetTag() ELFDynamicTag {
return n.Tag
}
func (n *ELF64DynamicEntry) GetValue() uint64 {
return n.Value
}
func (n *ELF32DynamicEntry) GetValue() uint64 {
return uint64(n.Value)
}
// This function parses any ELF file and returns an instance of the ELFFile
// interface if no errors occur.
func ParseELFFile(raw []byte) (ELFFile, error) {
if len(raw) < 5 {
return nil, fmt.Errorf("Invalid ELF file: is only %d bytes", len(raw))
}
if raw[4] == 2 {
return ParseELF64File(raw)
}
return ParseELF32File(raw)
}