-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtls_gen.go
113 lines (102 loc) · 1.97 KB
/
tls_gen.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
//+build ignore
package main
import (
"bufio"
"debug/dwarf"
"debug/elf"
"os"
"path/filepath"
"strconv"
)
func main() {
f, err := os.Open(filepath.Join("/", "proc", "self", "stat"))
if err != nil {
panic(err)
}
defer f.Close()
var columns []string
s := bufio.NewScanner(f)
s.Split(bufio.ScanWords)
for s.Scan() {
columns = append(columns, s.Text())
}
if err := s.Err(); err != nil {
panic(err)
}
parent := columns[3]
path, err := os.Readlink(filepath.Join("/", "proc", parent, "exe"))
if err != nil {
panic(err)
}
cf, err := elf.Open(path)
if err != nil {
panic(err)
}
defer cf.Close()
d, err := cf.DWARF()
if err != nil {
panic(err)
}
r := d.Reader()
types:
for {
e, err := r.Next()
if err != nil {
panic(err)
}
if e == nil && err == nil {
break
}
if len(e.Field) == 0 {
continue
}
var name bool
for _, a := range e.Field {
if a.Attr == dwarf.AttrName && a.Val == "g" {
name = true
}
if name && a.Attr == dwarf.AttrType && a.Class == dwarf.ClassReference {
t, err := d.Type(a.Val.(dwarf.Offset))
if err != nil {
panic(err)
}
if p, ok := t.(*dwarf.PtrType); ok {
t = p.Type
}
if t.String() != "runtime.g" {
continue
}
if d, ok := t.(*dwarf.TypedefType); ok {
t = d.Type
}
if s, ok := t.(*dwarf.StructType); ok {
if t.String() != "struct runtime.g" {
continue
}
for _, sf := range s.Field {
if sf.Name == "goid" && sf.Type.String() == "int64" {
tf, err := os.Create("tls.s")
if err != nil {
panic(err)
}
defer tf.Close()
_, err = tf.WriteString(`#include "textflag.h"
// This has been automatically generated by running: go run tls_gen.go
TEXT ·goid(SB),NOSPLIT,$-8
MOVQ TLS, AX
MOVQ 0(AX)(TLS*1), AX
MOVQ ` + strconv.FormatInt(sf.ByteOffset, 10) + `(AX), AX
MOVQ AX, ret+0(FP)
RET
`)
if err != nil {
panic(err)
}
break types
}
}
}
}
}
}
}