-
Notifications
You must be signed in to change notification settings - Fork 1
/
checker.go
364 lines (328 loc) · 7.76 KB
/
checker.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
package main
// TODO: rewrite this code to operate just within the scope of
// individual compilation units.
import (
"debug/dwarf"
"debug/elf"
"debug/macho"
"debug/pe"
"encoding/binary"
"encoding/hex"
"fmt"
"io"
"os"
"sort"
"strings"
"github.com/thanm/dwarf-check/dwexaminer"
)
type readLineMode int
const (
noReadLine readLineMode = 0
silentReadLine readLineMode = 1
dumpReadLine readLineMode = 2
)
type dumpTypesMode int
const (
noDumpTypes dumpTypesMode = 0
yesDumpTypes dumpTypesMode = 1
)
type dumpBuildIdMode int
const (
noDumpBuildId dumpBuildIdMode = 0
yesDumpBuildId dumpBuildIdMode = 1
)
type doAbsChecksMode int
const (
noDoAbsChecks doAbsChecksMode = 0
yesDoAbsChecks doAbsChecksMode = 1
)
type dumpSizeMode int
const (
noDumpSize dumpSizeMode = 0
yesDumpSize dumpSizeMode = 1
detailDumpSize dumpSizeMode = 2
)
type options struct {
db dumpBuildIdMode
dt dumpTypesMode
rl readLineMode
sz dumpSizeMode
dc doAbsChecksMode
}
func readAligned4(r io.Reader, sz int32) ([]byte, error) {
full := (sz + 3) &^ 3
data := make([]byte, full)
_, err := io.ReadFull(r, data)
if err != nil {
return nil, err
}
data = data[:sz]
return data, nil
}
func dumpBuildIdsInNoteSection(ef *elf.File, sect *elf.Section) error {
const wantNameSize = 4 // GNU\0
var wantNoteName = [...]byte{'G', 'N', 'U', 0}
const wantNoteType = 3 // NT_GNU_BUILD_ID
r := sect.Open()
for {
var namesize, descsize, noteType int32
if err := binary.Read(r, ef.ByteOrder, &namesize); err != nil {
if err == io.EOF {
return nil
}
return err
}
if namesize != wantNameSize {
continue
}
if err := binary.Read(r, ef.ByteOrder, &descsize); err != nil {
return err
}
if err := binary.Read(r, ef.ByteOrder, ¬eType); err != nil {
return err
}
if noteType != wantNoteType {
continue
}
noteName, err := readAligned4(r, namesize)
if err != nil {
continue
}
if wantNoteName[0] != noteName[0] ||
wantNoteName[1] != noteName[1] ||
wantNoteName[2] != noteName[2] ||
wantNoteName[3] != noteName[3] {
continue
}
desc, err := readAligned4(r, descsize)
if err != nil {
return err
}
descStr := hex.EncodeToString(desc)
fmt.Fprintf(os.Stderr, "found build id '%s' in section `%s` notename `%s`\n", descStr, sect.Name, string(noteName))
}
}
func dumpBuildId(ef *elf.File) {
for _, sect := range ef.Sections {
if sect.Type != elf.SHT_NOTE {
continue
}
dumpBuildIdsInNoteSection(ef, sect)
}
}
// isDebugSect returns TRUE if this section contains DWARF/debug info.
// Here we include ".eh_frame" and related, since they are basically DWARF
// under the covers.
func isDwarfSect(secName string) bool {
return strings.HasPrefix(secName, ".debug_") ||
strings.HasPrefix(secName, ".zdebug_") ||
strings.HasPrefix(secName, ".eh_frame")
}
func dumpSizes(ef *elf.File, mode dumpSizeMode) {
totDw := uint64(0)
totExe := uint64(0)
for _, sect := range ef.Sections {
totExe += sect.FileSize
if isDwarfSect(sect.Name) {
totDw += sect.FileSize
}
}
perc := func(v, tot uint64) string {
if v == 0 || tot == 0 {
return "0%"
}
fv := float64(v)
ft := float64(tot)
return fmt.Sprintf("%2.2f%%", (fv/ft)*100.0)
}
if mode == detailDumpSize {
for _, sect := range ef.Sections {
if isDwarfSect(sect.Name) {
fmt.Printf("section %15s: %10d bytes, %s of DWARF, %s of exe\n",
sect.Name, sect.FileSize,
perc(sect.FileSize, totDw),
perc(sect.FileSize, totExe))
} else { // if sect.Name == ".text" {
fmt.Printf("section %15s: %10d bytes, %s of exe\n",
sect.Name, sect.FileSize,
perc(sect.FileSize, totExe))
}
}
}
if mode == detailDumpSize {
fmt.Printf("DWARF size total: %d bytes, %s of exe\n", totDw, perc(totDw, totExe))
fmt.Printf("Exe size total: %d bytes\n", totExe)
} else {
fmt.Printf("DWARF size total: %d bytes\n", totDw)
}
}
func examineFile(filename string, o options) bool {
var d *dwarf.Data
var derr error
tries := []struct {
opener func(exe string) (*dwarf.Data, error)
flav string
}{
{flav: "ELF",
opener: func(exe string) (*dwarf.Data, error) {
f, err := elf.Open(exe)
if err != nil {
return nil, err
}
rv, err := f.DWARF()
if o.sz != noDumpSize {
dumpSizes(f, o.sz)
}
if o.db == yesDumpBuildId {
dumpBuildId(f)
}
return rv, err
},
},
{
flav: "Macho",
opener: func(exe string) (*dwarf.Data, error) {
f, err := macho.Open(exe)
if err != nil {
return nil, err
}
return f.DWARF()
},
},
{
flav: "PE",
opener: func(exe string) (*dwarf.Data, error) {
f, err := pe.Open(exe)
if err != nil {
return nil, err
}
return f.DWARF()
},
},
}
for _, try := range tries {
verb(1, "loading %s for %s", try.flav, filename)
d, derr = try.opener(filename)
if derr != nil {
warn("unable to open %s as %s: %v",
filename, try.flav, derr)
continue
}
break
}
if d == nil {
return false
}
// Initialize state
verb(1, "examining DWARF for %s", filename)
rdr := d.Reader()
var ds *dwexaminer.DwExaminer
if o.dc != noDoAbsChecks || o.dt != noDumpTypes {
var err error
ds, err = dwexaminer.NewDwExaminer(rdr)
if err != nil {
warn("error initializing dwarf state examiner: %v", err)
return false
}
}
dcount := 0
absocount := 0
typeNames := make(map[string]struct{})
isTypeTag := func(t dwarf.Tag) bool {
switch t {
case dwarf.TagSubrangeType, dwarf.TagSubroutineType, dwarf.TagArrayType, dwarf.TagBaseType, dwarf.TagPointerType, dwarf.TagStructType, dwarf.TagTypedef:
return true
}
return false
}
if o.dc != noDoAbsChecks || o.dt != noDumpTypes {
// Walk DIEs
dieOffsets := ds.DieOffsets()
for idx, off := range dieOffsets {
verb(3, "examining DIE at offset 0x%x", off)
die, err := ds.LoadEntryByOffset(off)
dcount++
if err != nil {
warn("error examining DWARF: %v", err)
return false
}
if o.dt != noDumpTypes {
if isTypeTag(die.Tag) {
if name, ok := die.Val(dwarf.AttrName).(string); ok {
typeNames[name] = struct{}{}
}
}
}
// Does it have an abstract origin?
ooff, originOK := die.Val(dwarf.AttrAbstractOrigin).(dwarf.Offset)
if !originOK {
continue
}
absocount++
// All abstract origin references should be resolvable.
var entry *dwarf.Entry
entry, err = ds.LoadEntryByOffset(ooff)
if err != nil || entry == nil {
warn("unresolved abstract origin ref from DIE %d at offset 0x%x to bad offset 0x%x\n", idx, off, ooff)
err := ds.DumpEntry(idx, false, true, 0)
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
}
return false
}
}
verb(1, "read %d DIEs, processed %d abstract origin refs",
dcount, absocount)
}
if o.dt != noDumpTypes {
fmt.Println("Types:")
sl := make([]string, 0, len(typeNames))
for k := range typeNames {
sl = append(sl, k)
}
sort.Strings(sl)
for i := range sl {
fmt.Println(i, sl[i])
}
}
if o.rl != noReadLine {
dr := d.Reader()
for {
ent, err := dr.Next()
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
return false
} else if ent == nil {
break
}
if ent.Tag != dwarf.TagCompileUnit {
dr.SkipChildren()
continue
}
// Decode CU's line table.
lr, err := d.LineReader(ent)
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
return false
} else if lr == nil {
continue
}
for {
var line dwarf.LineEntry
err := lr.Next(&line)
if err != nil {
if err == io.EOF {
break
}
fmt.Fprintf(os.Stderr, "%v\n", err)
return false
}
if o.rl == dumpReadLine {
fmt.Printf("Address: %x File: %s Line: %d IsStmt: %v PrologueEnd: %v\n", line.Address, line.File.Name, line.Line, line.IsStmt, line.PrologueEnd)
}
}
}
}
verb(1, "true return from examineFile")
return true
}