-
Notifications
You must be signed in to change notification settings - Fork 14
/
elf.go
294 lines (273 loc) · 6.14 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
/*
* ELF tree - Tree viewer for ELF library dependency
*
* Copyright (C) 2017-2018 Namhyung Kim <[email protected]>
*
* Released under MIT license.
*/
package main
import (
"debug/elf"
"fmt"
str "strings"
)
const (
GNU_EH_FRAME = elf.PT_LOOS + 74769744
GNU_STACK = elf.PT_LOOS + 74769745
GNU_RELRO = elf.PT_LOOS + 74769746
)
func progHdrString(phdr *elf.Prog) string {
var typeStr string
var flagStr string
switch phdr.Type {
case GNU_EH_FRAME:
typeStr = "GNU_EH_FRAME"
case GNU_STACK:
typeStr = "GNU_STACK"
case GNU_RELRO:
typeStr = "GNU_RELRO"
default:
typeStr = phdr.Type.String()[3:]
}
switch phdr.Flags {
case elf.PF_X:
flagStr = "__X"
case elf.PF_W:
flagStr = "_W_"
case elf.PF_R:
flagStr = "R__"
case elf.PF_R | elf.PF_W:
flagStr = "RW_"
case elf.PF_R | elf.PF_X:
flagStr = "R_X"
case elf.PF_R | elf.PF_W | elf.PF_X:
flagStr = "RWX"
default:
flagStr = "???"
}
return fmt.Sprintf("%-16s %s %#18x %#10x %#8x", typeStr, flagStr, phdr.Vaddr, phdr.Memsz, phdr.Align)
}
const (
DT_GNU_HASH = elf.DT_HIOS + 3829
DT_RELACOUNT = elf.DT_VERSYM + 9
DT_RELCOUNT = elf.DT_VERSYM + 10
DT_FLAGS_1 = elf.DT_VERSYM + 11
DT_VERDEF = elf.DT_VERSYM + 12
DT_VERDEFNUM = elf.DT_VERSYM + 13
DT_VERNEED = elf.DT_VERSYM + 14
DT_VERNEEDNUM = elf.DT_VERSYM + 15
)
// convert DT_FLAGS
func strFlags(val uint64) string {
var ret []string
if (val & 0x1) != 0 {
ret = append(ret, "ORIGIN")
}
if (val & 0x2) != 0 {
ret = append(ret, "SYMBOLIC")
}
if (val & 0x4) != 0 {
ret = append(ret, "TEXTREL")
}
if (val & 0x8) != 0 {
ret = append(ret, "BIND_NOW")
}
if (val & 0x10) != 0 {
ret = append(ret, "STATIC_TLS")
}
return str.Join(ret, "|")
}
// convert DT_FLAGS_1
func strFlags1(val uint64) string {
var ret []string
if (val & 0x1) != 0 {
ret = append(ret, "NOW")
}
if (val & 0x2) != 0 {
ret = append(ret, "GLOBAL")
}
if (val & 0x4) != 0 {
ret = append(ret, "GROUP")
}
if (val & 0x8) != 0 {
ret = append(ret, "NODELETE")
}
if (val & 0x10) != 0 {
ret = append(ret, "LOADFLTR")
}
if (val & 0x20) != 0 {
ret = append(ret, "INITFIRST")
}
if (val & 0x40) != 0 {
ret = append(ret, "NOOPEN")
}
if (val & 0x80) != 0 {
ret = append(ret, "ORIGIN")
}
if (val & 0x100) != 0 {
ret = append(ret, "DIRECT")
}
if (val & 0x200) != 0 {
ret = append(ret, "TRANS")
}
if (val & 0x400) != 0 {
ret = append(ret, "INTERPOSE")
}
if (val & 0x800) != 0 {
ret = append(ret, "NODEFLIB")
}
if (val & 0x1000) != 0 {
ret = append(ret, "NODUMP")
}
if (val & 0x2000) != 0 {
ret = append(ret, "CONFLAT")
}
if (val & 0x4000) != 0 {
ret = append(ret, "ENDFILTEE")
}
if (val & 0x8000) != 0 {
ret = append(ret, "DISPRELDNE")
}
if (val & 0x10000) != 0 {
ret = append(ret, "DISPRELPND")
}
if (val & 0x20000) != 0 {
ret = append(ret, "NODIRECT")
}
if (val & 0x40000) != 0 {
ret = append(ret, "IGNMULDEF")
}
if (val & 0x80000) != 0 {
ret = append(ret, "NOKSYMS")
}
if (val & 0x100000) != 0 {
ret = append(ret, "NOHDR")
}
if (val & 0x200000) != 0 {
ret = append(ret, "EDITED")
}
if (val & 0x400000) != 0 {
ret = append(ret, "NORELOC")
}
if (val & 0x800000) != 0 {
ret = append(ret, "SYMINTPOSE")
}
if (val & 0x1000000) != 0 {
ret = append(ret, "GLOBAUDIT")
}
if (val & 0x2000000) != 0 {
ret = append(ret, "SINGLETON")
}
return str.Join(ret, "|")
}
func makeDynamicStrings(info *DepsInfo) []string {
// dynamic attributes
var dyns []string
for _, v := range info.dyns {
switch v.tag {
case elf.DT_NEEDED:
fallthrough
case elf.DT_RPATH:
fallthrough
case elf.DT_RUNPATH:
fallthrough
case elf.DT_SONAME:
dyns = append(dyns, fmt.Sprintf(" %-16s %s", v.tag, v.val.(string)))
case DT_GNU_HASH:
dyns = append(dyns, fmt.Sprintf(" %-16s %x", "DT_GNU_HASH", v.val))
case DT_RELACOUNT:
dyns = append(dyns, fmt.Sprintf(" %-16s %v", "DT_RELACOUNT", v.val))
case DT_RELCOUNT:
dyns = append(dyns, fmt.Sprintf(" %-16s %v", "DT_RELCOUNT", v.val))
case elf.DT_FLAGS:
dyns = append(dyns, fmt.Sprintf(" %-16s %s", "DT_FLAGS", strFlags(v.val.(uint64))))
case DT_FLAGS_1:
dyns = append(dyns, fmt.Sprintf(" %-16s %s", "DT_FLAGS_1", strFlags1(v.val.(uint64))))
case DT_VERDEF:
dyns = append(dyns, fmt.Sprintf(" %-16s %x", "DT_VERDEF", v.val))
case DT_VERDEFNUM:
dyns = append(dyns, fmt.Sprintf(" %-16s %v", "DT_VERDEFNUM", v.val))
case DT_VERNEED:
dyns = append(dyns, fmt.Sprintf(" %-16s %x", "DT_VERNEED", v.val))
case DT_VERNEEDNUM:
dyns = append(dyns, fmt.Sprintf(" %-16s %v", "DT_VERNEEDNUM", v.val))
default:
dyns = append(dyns, fmt.Sprintf(" %-16s %x", v.tag, v.val))
}
}
return dyns
}
func makeSymbolString(sym elf.Symbol) string {
var t string
switch elf.ST_TYPE(sym.Info) {
case elf.STT_NOTYPE:
t = "NON"
case elf.STT_OBJECT:
t = "OBJ"
case elf.STT_FUNC:
t = "FUN"
case elf.STT_SECTION:
t = "SEC"
case elf.STT_FILE:
t = "FIL"
case elf.STT_COMMON:
t = "COM"
case elf.STT_TLS:
t = "TLS"
default:
t = "XXX"
}
var b string
switch elf.ST_BIND(sym.Info) {
case elf.STB_LOCAL:
b = "L"
case elf.STB_GLOBAL:
b = "G"
case elf.STB_WEAK:
b = "W"
default:
b = "X"
}
return fmt.Sprintf(" %8x %s %s %s", sym.Value, t, b, sym.Name)
}
func makeSectionString(idx int, sec *elf.Section) string {
var flag []string
val := sec.Flags
if (val & 0x1) != 0 {
flag = append(flag, "W") // write
}
if (val & 0x2) != 0 {
flag = append(flag, "A") // alloc
}
if (val & 0x4) != 0 {
flag = append(flag, "X") // execute
}
if (val & 0x10) != 0 {
flag = append(flag, "M") // merge
}
if (val & 0x20) != 0 {
flag = append(flag, "S") // string
}
if (val & 0x40) != 0 {
flag = append(flag, "I") // info link
}
if (val & 0x80) != 0 {
flag = append(flag, "L") // link order
}
if (val & 0x100) != 0 {
flag = append(flag, "O") // OS non-conforming
}
if (val & 0x200) != 0 {
flag = append(flag, "G") // group
}
if (val & 0x400) != 0 {
flag = append(flag, "T") // TLS
}
if (val & 0x800) != 0 {
flag = append(flag, "C") // compressed
}
f := str.Join(flag, "")
t := sec.Type.String()
return fmt.Sprintf(" [%2d] %-24s %-12s %8x %8x %4s",
idx, sec.Name, t[4:len(t)], sec.Offset, sec.Size, f)
}