forked from brahma-adshonor/gohook
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathelf_helper.go
75 lines (59 loc) · 1.42 KB
/
elf_helper.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
package gohook
import (
"debug/elf"
"errors"
"os"
"path/filepath"
"sort"
)
var (
curExecutable, _ = filepath.Abs(os.Args[0])
)
type SymbolSlice []elf.Symbol
func (a SymbolSlice) Len() int { return len(a) }
func (a SymbolSlice) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a SymbolSlice) Less(i, j int) bool { return a[i].Value < a[j].Value }
type ElfInfo struct {
CurFile string
Symbol SymbolSlice
}
func NewElfInfo() (*ElfInfo, error) {
ei := &ElfInfo{CurFile: curExecutable}
err := ei.init()
if err != nil {
return nil, err
}
return ei, nil
}
func (ei *ElfInfo) init() error {
f, err := elf.Open(ei.CurFile)
if err != nil {
return err
}
defer f.Close()
var sym []elf.Symbol
sym, err = f.Symbols()
ei.Symbol = make(SymbolSlice, 0, len(sym))
for _, v := range sym {
if v.Size > 0 {
ei.Symbol = append(ei.Symbol, v)
}
}
if err != nil {
return err
}
sort.Sort(ei.Symbol)
return nil
}
func (ei *ElfInfo) GetFuncSize(addr uintptr) (uint32, error) {
if ei.Symbol == nil {
return 0, errors.New("no symbol")
}
i := sort.Search(len(ei.Symbol), func(i int) bool { return ei.Symbol[i].Value >= uint64(addr) })
if i < len(ei.Symbol) && ei.Symbol[i].Value == uint64(addr) {
//fmt.Printf("addr:0x%x,value:0x%x, sz:%d\n", addr, ei.Symbol[i].Value, ei.Symbol[i].Size)
return uint32(ei.Symbol[i].Size), nil
}
//fmt.Printf("not find elf\n")
return 0, errors.New("can not find func")
}