-
Notifications
You must be signed in to change notification settings - Fork 1
/
file.go
133 lines (116 loc) · 2.6 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
package bre
import (
"debug/dwarf"
"debug/elf"
"debug/macho"
"debug/pe"
"io"
"github.com/ArtemKulyabin/bre/binaryx"
"github.com/ArtemKulyabin/bre/binaryx/elfx"
"github.com/ArtemKulyabin/bre/binaryx/machox"
"github.com/ArtemKulyabin/bre/binaryx/pex"
)
func NewFile(r io.ReaderAt) (binaryx.File, error) {
elfBinary, err := elf.NewFile(r)
if err == nil {
return newFile(&elfx.File{elfBinary})
}
machoBinary, err := macho.NewFile(r)
if err == nil {
return newFile(&machox.File{machoBinary})
}
peBinary, err := pe.NewFile(r)
if err == nil {
return newFile(&pex.File{peBinary})
}
return nil, err
}
func Open(name string) (binaryx.File, error) {
elfBinary, err := elf.Open(name)
if err == nil {
return newFile(&elfx.File{elfBinary})
}
machoBinary, err := macho.Open(name)
if err == nil {
return newFile(&machox.File{machoBinary})
}
peBinary, err := pe.Open(name)
if err == nil {
return newFile(&pex.File{peBinary})
}
return nil, err
}
type file struct {
arch string
os string
importedLibraries []string
importedSymbols []string
dWARF *dwarf.Data
sections []binaryx.Section
ftype binaryx.FileType
format binaryx.Format
entry uint64
binaryFile binaryx.File
}
func newFile(binaryFile binaryx.File) (binaryx.File, error) {
f := &file{}
f.arch = binaryFile.Arch()
f.os = binaryFile.Os()
f.sections = binaryFile.Sections()
f.ftype = binaryFile.Type()
f.format = binaryFile.Format()
f.entry = binaryFile.Entry()
f.binaryFile = binaryFile
return f, nil
}
func (f *file) Arch() string {
return f.arch
}
func (f *file) Os() string {
return f.os
}
func (f *file) ImportedLibraries() ([]string, error) {
var err error
if f.importedLibraries == nil {
f.importedLibraries, err = f.binaryFile.ImportedLibraries()
if err != nil {
return nil, err
}
}
return f.importedLibraries, nil
}
func (f *file) ImportedSymbols() ([]string, error) {
var err error
if f.importedSymbols == nil {
f.importedSymbols, err = f.binaryFile.ImportedSymbols()
if err != nil {
return nil, err
}
}
return f.importedSymbols, nil
}
func (f *file) DWARF() (*dwarf.Data, error) {
var err error
if f.dWARF == nil {
f.dWARF, err = f.binaryFile.DWARF()
if err != nil {
return nil, err
}
}
return f.dWARF, nil
}
func (f *file) Sections() []binaryx.Section {
return f.sections
}
func (f *file) Type() binaryx.FileType {
return f.ftype
}
func (f *file) Format() binaryx.Format {
return f.format
}
func (f *file) Entry() uint64 {
return f.entry
}
func (f *file) Close() error {
return f.binaryFile.Close()
}