-
Notifications
You must be signed in to change notification settings - Fork 0
/
loader.go
273 lines (244 loc) · 6.01 KB
/
loader.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
// +build linux,amd64 !cgo
package plugin
import (
"debug/elf"
"errors"
"fmt"
"io"
"strings"
)
const (
pkgSep string = `.`
pluginPrefix string = `plugin/`
pkgNamePrefix string = `go.link.pkghashbytes.plugin/`
)
var ()
type elf64SharedObject struct {
pkgName string
hdr elf.Header64
mms []mm
//the symtabs and stringtabs are just references to the mapped regions
// when we close they point to invalid memory
symtab mappedSection
stringtabs []mappedSection
sections []elf.Section64
}
// elf64load loads an ELF shared library file into memory and performs validation on the headers
// we will perform relocations and set the permission bits appriopriately on each section
// This implementation of DLoad WILL NOT link imports in the library, if the library has any imports
// we will return an error. Staticly compiled libraries ONLY
func elf64load(p string) (s elf64SharedObject, err error) {
var f fileMap
var elfio *elf.File
if f, err = newFileMap(p); err != nil {
return
}
if elfio, err = elf.NewFile(f); err != nil {
f.close()
return
}
//validate that the ELF file meets our expectations
if err = checkArch(elfio); err != nil {
elfio.Close()
f.close()
return
} else if err = checkForImports(elfio); err != nil {
elfio.Close()
f.close()
return
} else if err = checkSections(elfio); err != nil {
elfio.Close()
f.close()
return
}
if err = checkGoVersion(elfio); err != nil {
elfio.Close()
f.close()
return
}
if err = elfio.Close(); err != nil {
f.close()
return
}
//load and validate our elf64 header
if s.hdr, err = loadHeader64(f.buff); err != nil {
f.close()
return
}
//load the program regions
if s.mms, err = loadProgramSections(f.buff, s.hdr); err != nil {
f.close()
return
}
if s.sections, err = loadSections(s.mms, s.hdr, f.buff); err != nil {
f.close()
s.Close()
return
}
//Close our file map, it is no longer needed
if err = f.close(); err != nil {
return
}
//get the symbol table
var tabs []mappedSection
if tabs, err = mapSections(s.mms, s.sections, uint32(elf.SHT_DYNSYM)); err == nil {
if len(tabs) != 1 {
err = errors.New("multiple symbol tables")
} else {
s.symtab = tabs[0]
}
}
if err != nil {
s.Close()
f.close()
return
}
//get the symbol table
if s.stringtabs, err = mapSections(s.mms, s.sections, uint32(elf.SHT_STRTAB)); err != nil {
s.Close()
f.close()
return
}
//get the relocation tables
var relocs []mappedSection
if relocs, err = mapSections(s.mms, s.sections, uint32(elf.SHT_RELA), uint32(elf.SHT_REL)); err != nil {
s.Close()
f.close()
return
}
//perform relocations
if err = s.performRelocations(relocs); err != nil {
s.Close()
return
}
//location package name via the symbol table
if s.pkgName, err = s.identifyPluginName(); err != nil {
s.Close()
f.close()
return
}
//set the permissions on the memory regions
if err = setPermissions(s.mms); err != nil {
s.Close()
return
}
return
}
func (s *elf64SharedObject) lookup(v string) (ret elf.Sym64, err error) {
//tgtName := pluginPrefix+s.pkgName+pkgSep+v
tgtName := v
err = s.symbolWalk(func(nm string, sym elf.Sym64, sect elf.Section64) (lerr error) {
if nm == tgtName {
lerr = io.EOF //return EOF to get the walk to stop
ret = sym
}
return
})
if err == nil {
err = errors.New("Symbol not found")
} else if err == io.EOF {
err = nil
}
return
}
func (s *elf64SharedObject) identifyPluginName() (r string, err error) {
var found bool
err = s.symbolWalk(func(nm string, sym elf.Sym64, sect elf.Section64) (lerr error) {
if elf.ST_TYPE(sym.Info) != elf.STT_OBJECT {
return
}
if ((sym.Value - sect.Addr) + sym.Size) > sect.Size {
fmt.Printf("%s symbol points outside section %d %+v\n", nm, sect.Size, sym)
lerr = errors.New("symbol points outside section")
} else if strings.HasPrefix(nm, pkgNamePrefix) {
r = strings.TrimPrefix(nm, pkgNamePrefix)
found = true
lerr = io.EOF
}
return
})
if err == io.EOF {
err = nil
} else if err == nil && !found {
err = errors.New("packagename not found")
}
return
}
type symbolCallback func(string, elf.Sym64, elf.Section64) error
func (s *elf64SharedObject) symbolWalk(cb symbolCallback) (err error) {
if s.mms == nil {
err = errors.New("elf64SharedObject not loaded")
return
} else if cb == nil {
err = errors.New("Invalid callback")
return
}
strtab, ok := s.linkedStringTable(s.symtab.idx)
if !ok {
err = errors.New("no string table linked to symbol table")
return
}
//swing through the symbol table looking looking for strings that match
var sym elf.Sym64
var str string
for off := 0; (off + symSize) <= len(s.symtab.buff); off += symSize {
if sym, err = readSymbol(s.symtab.buff[off:]); err != nil {
return
} else if sym.Shndx == 0 {
continue
} else if int(sym.Shndx) >= len(s.sections) {
err = errors.New("symbol points outside our section table")
return
} else if str, err = s.getString(strtab, sym.Name); err != nil {
return
}
if err = cb(str, sym, s.sections[sym.Shndx]); err != nil {
return
}
}
return
}
func (s *elf64SharedObject) lookupSymbolName(sym elf.Sym64) (n string, err error) {
strtab, ok := s.linkedStringTable(s.symtab.idx)
if ok {
n, err = s.getString(strtab, sym.Name)
} else {
err = errors.New("no string table linked to symbol table")
}
return
}
func (s *elf64SharedObject) linkedStringTable(idx int) (ms mappedSection, ok bool) {
for _, tab := range s.stringtabs {
if tab.idx-1 == idx {
ms = tab
ok = true
}
}
return
}
func (s *elf64SharedObject) getString(ms mappedSection, stroff uint32) (r string, err error) {
//now get the string
if stroff >= uint32(len(ms.buff)) {
err = errors.New("INvalid string offset")
return
}
var e uint32
for e = stroff; e < uint32(len(ms.buff)); e++ {
if ms.buff[e] == 0 {
break
}
}
r = string(ms.buff[stroff:e])
return
}
func (s *elf64SharedObject) Close() (err error) {
if s == nil {
return errors.New("not open")
}
for _, mm := range s.mms {
if lerr := mm.close(); lerr != nil {
err = lerr
}
}
return
}