This repository has been archived by the owner on Mar 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathelf.go
406 lines (357 loc) · 11.3 KB
/
elf.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
package ebpf
import (
"bytes"
"debug/elf"
"encoding/binary"
"io"
"strings"
"github.com/newtools/ebpf/asm"
"github.com/pkg/errors"
)
type elfCode struct {
*elf.File
symtab *symtab
}
// LoadCollectionSpecFromReader parses an io.ReaderAt that represents an ELF layout
// into a CollectionSpec.
func LoadCollectionSpecFromReader(code io.ReaderAt) (*CollectionSpec, error) {
f, err := elf.NewFile(code)
if err != nil {
return nil, err
}
defer f.Close()
symbols, err := f.Symbols()
if err != nil {
return nil, errors.Wrap(err, "load symbols")
}
ec := &elfCode{f, newSymtab(symbols)}
var licenseSection, versionSection *elf.Section
progSections := make(map[int]*elf.Section)
relSections := make(map[int]*elf.Section)
mapSections := make(map[int]*elf.Section)
for i, sec := range ec.Sections {
switch {
case strings.HasPrefix(sec.Name, "license"):
licenseSection = sec
case strings.HasPrefix(sec.Name, "version"):
versionSection = sec
case strings.HasPrefix(sec.Name, "maps"):
mapSections[i] = sec
case sec.Type == elf.SHT_REL:
if int(sec.Info) >= len(ec.Sections) {
return nil, errors.Errorf("found relocation section %v for missing section %v", i, sec.Info)
}
// Store relocations under the section index of the target
idx := int(sec.Info)
if relSections[idx] != nil {
return nil, errors.Errorf("section %d has multiple relocation sections", idx)
}
relSections[idx] = sec
case sec.Type == elf.SHT_PROGBITS && (sec.Flags&elf.SHF_EXECINSTR) != 0 && sec.Size > 0:
progSections[i] = sec
}
}
license, err := loadLicense(licenseSection)
if err != nil {
return nil, errors.Wrap(err, "load license")
}
version, err := loadVersion(versionSection, ec.ByteOrder)
if err != nil {
return nil, errors.Wrap(err, "load version")
}
maps, err := ec.loadMaps(mapSections)
if err != nil {
return nil, errors.Wrap(err, "load maps")
}
progs, libs, err := ec.loadPrograms(progSections, relSections, license, version)
if err != nil {
return nil, errors.Wrap(err, "load programs")
}
if len(libs) > 0 {
for name, prog := range progs {
editor := Edit(&prog.Instructions)
if err := editor.Link(libs...); err != nil {
return nil, errors.Wrapf(err, "program %s", name)
}
}
}
return &CollectionSpec{maps, progs}, nil
}
func loadLicense(sec *elf.Section) (string, error) {
if sec == nil {
return "", errors.Errorf("missing license section")
}
data, err := sec.Data()
if err != nil {
return "", errors.Wrapf(err, "section %s", sec.Name)
}
return string(bytes.TrimRight(data, "\000")), nil
}
func loadVersion(sec *elf.Section, bo binary.ByteOrder) (uint32, error) {
if sec == nil {
return 0, nil
}
var version uint32
err := binary.Read(sec.Open(), bo, &version)
return version, errors.Wrapf(err, "section %s", sec.Name)
}
func (ec *elfCode) loadPrograms(progSections, relSections map[int]*elf.Section, license string, version uint32) (map[string]*ProgramSpec, []asm.Instructions, error) {
progs := make(map[string]*ProgramSpec)
var libs []asm.Instructions
for idx, prog := range progSections {
funcSym := ec.symtab.forSectionOffset(idx, 0)
if funcSym == nil {
return nil, nil, errors.Errorf("section %v: no label at start", prog.Name)
}
var insns asm.Instructions
offsets, err := insns.Unmarshal(prog.Open(), ec.ByteOrder)
if err != nil {
return nil, nil, errors.Wrapf(err, "program %s", funcSym.Name)
}
err = assignSymbols(ec.symtab.forSection(idx), offsets, insns)
if err != nil {
return nil, nil, errors.Wrapf(err, "program %s", funcSym.Name)
}
if rels := relSections[idx]; rels != nil {
err = ec.applyRelocations(insns, rels, offsets)
if err != nil {
return nil, nil, errors.Wrapf(err, "program %s: section %s", funcSym.Name, rels.Name)
}
}
if progType, attachType := getProgType(prog.Name); progType == Unrecognized {
// There is no single name we can use for "library" sections,
// since they may contain multiple functions. We'll decode the
// labels they contain later on, and then link sections that way.
libs = append(libs, insns)
} else {
progs[funcSym.Name] = &ProgramSpec{
Name: funcSym.Name,
Type: progType,
AttachType: attachType,
License: license,
KernelVersion: version,
Instructions: insns,
}
}
}
return progs, libs, nil
}
func (ec *elfCode) loadMaps(mapSections map[int]*elf.Section) (map[string]*MapSpec, error) {
maps := make(map[string]*MapSpec)
for idx, sec := range mapSections {
// TODO: Iterate symbols
n := len(ec.symtab.forSection(idx))
if n == 0 {
return nil, errors.Errorf("section %v: no symbols", sec.Name)
}
data, err := sec.Data()
if err != nil {
return nil, err
}
if len(data)%n != 0 {
return nil, errors.Errorf("map descriptors are not of equal size")
}
size := len(data) / n
var ordered []*MapSpec
for i := 0; i < n; i++ {
rd := bytes.NewReader(data[i*size : i*size+size])
mapSym := ec.symtab.forSectionOffset(idx, uint64(i*size))
if mapSym == nil {
return nil, errors.Errorf("section %s: missing symbol for map #%d", sec.Name, i)
}
name := mapSym.Name
if maps[name] != nil {
return nil, errors.Errorf("section %v: map %v already exists", sec.Name, name)
}
var spec MapSpec
var inner uint32
switch {
case binary.Read(rd, ec.ByteOrder, &spec.Type) != nil:
return nil, errors.Errorf("map %v: missing type", name)
case binary.Read(rd, ec.ByteOrder, &spec.KeySize) != nil:
return nil, errors.Errorf("map %v: missing key size", name)
case binary.Read(rd, ec.ByteOrder, &spec.ValueSize) != nil:
return nil, errors.Errorf("map %v: missing value size", name)
case binary.Read(rd, ec.ByteOrder, &spec.MaxEntries) != nil:
return nil, errors.Errorf("map %v: missing max entries", name)
case binary.Read(rd, ec.ByteOrder, &spec.Flags) != nil:
return nil, errors.Errorf("map %v: missing flags", name)
case rd.Len() > 0 && binary.Read(rd, ec.ByteOrder, &inner) != nil:
return nil, errors.Errorf("map %v: can't read inner map index", name)
}
for rd.Len() > 0 {
b, err := rd.ReadByte()
if err != nil {
return nil, err
}
if b != 0 {
return nil, errors.Errorf("map %v: unknown and non-zero fields in definition", name)
}
}
if spec.Type == ArrayOfMaps || spec.Type == HashOfMaps {
if int(inner) > len(ordered) {
return nil, errors.Errorf("map %v: invalid inner map index %d", name, inner)
}
innerSpec := ordered[int(inner)]
if innerSpec.InnerMap != nil {
return nil, errors.Errorf("map %v: can't nest map of map", name)
}
spec.InnerMap = innerSpec.Copy()
}
maps[name] = &spec
ordered = append(ordered, &spec)
}
}
return maps, nil
}
func getProgType(v string) (ProgType, AttachType) {
types := map[string]ProgType{
// From https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/tools/lib/bpf/libbpf.c#n3568
"socket": SocketFilter,
"seccomp": SocketFilter,
"kprobe/": Kprobe,
"kretprobe/": Kprobe,
"tracepoint/": TracePoint,
"xdp": XDP,
"perf_event": PerfEvent,
"sockops": SockOps,
"sk_skb": SkSKB,
"sk_msg": SkMsg,
"lirc_mode2": LircMode2,
"flow_dissector": FlowDissector,
"cgroup_skb/": CGroupSKB,
"cgroup/dev": CGroupDevice,
"cgroup/skb": CGroupSKB,
"cgroup/sock": CGroupSock,
"cgroup/post_bind": CGroupSock,
"cgroup/bind": CGroupSockAddr,
"cgroup/connect": CGroupSockAddr,
"cgroup/sendmsg": CGroupSockAddr,
"cgroup/recvmsg": CGroupSockAddr,
"cgroup/sysctl": CGroupSysctl,
"cgroup/getsockopt": CGroupSockopt,
"cgroup/setsockopt": CGroupSockopt,
"classifier": SchedCLS,
"action": SchedACT,
}
attachTypes := map[string]AttachType{
"cgroup_skb/ingress": AttachCGroupInetIngress,
"cgroup_skb/egress": AttachCGroupInetEgress,
"cgroup/sock": AttachCGroupInetSockCreate,
"cgroup/post_bind4": AttachCGroupInet4PostBind,
"cgroup/post_bind6": AttachCGroupInet6PostBind,
"cgroup/dev": AttachCGroupDevice,
"sockops": AttachCGroupSockOps,
"sk_skb/stream_parser": AttachSkSKBStreamParser,
"sk_skb/stream_verdict": AttachSkSKBStreamVerdict,
"sk_msg": AttachSkSKBStreamVerdict,
"lirc_mode2": AttachLircMode2,
"flow_dissector": AttachFlowDissector,
"cgroup/bind4": AttachCGroupInet4Bind,
"cgroup/bind6": AttachCGroupInet6Bind,
"cgroup/connect4": AttachCGroupInet4Connect,
"cgroup/connect6": AttachCGroupInet6Connect,
"cgroup/sendmsg4": AttachCGroupUDP4Sendmsg,
"cgroup/sendmsg6": AttachCGroupUDP6Sendmsg,
"cgroup/recvmsg4": AttachCGroupUDP4Recvmsg,
"cgroup/recvmsg6": AttachCGroupUDP6Recvmsg,
"cgroup/sysctl": AttachCGroupSysctl,
"cgroup/getsockopt": AttachCGroupGetsockopt,
"cgroup/setsockopt": AttachCGroupSetsockopt,
}
attachType := AttachNone
for k, t := range attachTypes {
if strings.HasPrefix(v, k) {
attachType = t
}
}
for k, t := range types {
if strings.HasPrefix(v, k) {
return t, attachType
}
}
return Unrecognized, AttachNone
}
func assignSymbols(symbolOffsets map[uint64]*elf.Symbol, insOffsets map[uint64]int, insns asm.Instructions) error {
for offset, sym := range symbolOffsets {
i, ok := insOffsets[offset]
if !ok {
return errors.Errorf("symbol %s: no instruction at offset %d", sym.Name, offset)
}
insns[i].Symbol = sym.Name
}
return nil
}
func (ec *elfCode) applyRelocations(insns asm.Instructions, sec *elf.Section, offsets map[uint64]int) error {
if sec.Entsize < 16 {
return errors.New("rls are less than 16 bytes")
}
r := sec.Open()
for off := uint64(0); off < sec.Size; off += sec.Entsize {
ent := io.LimitReader(r, int64(sec.Entsize))
var rel elf.Rel64
if binary.Read(ent, ec.ByteOrder, &rel) != nil {
return errors.Errorf("can't parse relocation at offset %v", off)
}
sym, err := ec.symtab.forRelocation(rel)
if err != nil {
return errors.Wrapf(err, "relocation at offset %v", off)
}
idx, ok := offsets[rel.Off]
if !ok {
return errors.Errorf("symbol %v: invalid instruction offset %x", sym, rel.Off)
}
insns[idx].Reference = sym.Name
}
return nil
}
type symtab struct {
Symbols []elf.Symbol
index map[int]map[uint64]*elf.Symbol
}
func newSymtab(symbols []elf.Symbol) *symtab {
index := make(map[int]map[uint64]*elf.Symbol)
for i, sym := range symbols {
switch elf.ST_TYPE(sym.Info) {
case elf.STT_NOTYPE:
// Older versions of LLVM doesn't tag
// symbols correctly.
break
case elf.STT_OBJECT:
break
case elf.STT_FUNC:
break
default:
continue
}
if sym.Name == "" {
continue
}
idx := int(sym.Section)
if _, ok := index[idx]; !ok {
index[idx] = make(map[uint64]*elf.Symbol)
}
index[idx][sym.Value] = &symbols[i]
}
return &symtab{
symbols,
index,
}
}
func (st *symtab) forSection(sec int) map[uint64]*elf.Symbol {
return st.index[sec]
}
func (st *symtab) forSectionOffset(sec int, offset uint64) *elf.Symbol {
offsets := st.index[sec]
if offsets == nil {
return nil
}
return offsets[offset]
}
func (st *symtab) forRelocation(rel elf.Rel64) (*elf.Symbol, error) {
symNo := int(elf.R_SYM64(rel.Info) - 1)
if symNo >= len(st.Symbols) {
return nil, errors.Errorf("symbol %v doesnt exist", symNo)
}
return &st.Symbols[symNo], nil
}