This repository has been archived by the owner on Feb 8, 2018. It is now read-only.
/
DwarfFrameReader.cs
505 lines (422 loc) · 11.2 KB
/
DwarfFrameReader.cs
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
using System;
using System.Collections;
namespace Mono.Debugger.Backend
{
internal class DwarfFrameReader
{
protected readonly Bfd bfd;
protected readonly TargetBlob blob;
protected readonly bool is_ehframe;
protected readonly long vma;
protected CIE cie_list;
public DwarfFrameReader (Bfd bfd, TargetBlob blob, long vma,
bool is_ehframe)
{
this.bfd = bfd;
this.blob = blob;
this.vma = vma;
this.is_ehframe = is_ehframe;
}
protected CIE find_cie (long offset)
{
for (CIE cie = cie_list; cie != null; cie = cie.Next) {
if (cie.Offset == offset)
return cie;
}
CIE new_cie = new CIE (this, offset, cie_list);
cie_list = new_cie;
return cie_list;
}
public StackFrame UnwindStack (StackFrame frame, TargetMemoryAccess target,
Architecture arch)
{
if (frame.TargetAddress.IsNull)
return null;
TargetAddress address = frame.TargetAddress;
DwarfBinaryReader reader = new DwarfBinaryReader (bfd, blob, false);
while (reader.Position < reader.Size) {
long length = reader.ReadInitialLength ();
if (length == 0)
break;
long end_pos = reader.Position + length;
long cie_pointer = reader.ReadOffset ();
bool is_cie;
if (is_ehframe)
is_cie = cie_pointer == 0;
else
is_cie = cie_pointer == -1;
if (is_cie) {
reader.Position = end_pos;
continue;
}
if (is_ehframe)
cie_pointer = reader.Position - cie_pointer -
target.TargetMemoryInfo.TargetAddressSize;
CIE cie = find_cie (cie_pointer);
long initial, range;
if (is_ehframe) {
initial = ReadEncodedValue (reader, cie.Encoding);
range = ReadEncodedValue (reader, cie.Encoding & 0x0f);
} else {
initial = reader.ReadAddress ();
range = reader.ReadAddress ();
}
TargetAddress start = new TargetAddress (target.AddressDomain, initial);
if ((address < start) || (address > start + range)) {
reader.Position = end_pos;
continue;
}
Entry fde = new Entry (cie, start, address);
fde.Read (reader, end_pos);
return fde.Unwind (frame, target, arch);
}
return null;
}
private long ReadEncodedValue (DwarfBinaryReader reader, int encoding)
{
long base_addr;
switch (encoding & 0x70) {
case 0:
base_addr = 0;
break;
case (byte) DW_EH_PE.pcrel:
base_addr = vma + reader.Position;
break;
default:
throw new DwarfException (
reader.Bfd, "Unknown encoding `{0:x}' in CIE",
encoding);
}
long value;
switch (encoding & 0x0f) {
case (byte) DW_EH_PE.udata4:
value = reader.ReadUInt32 ();
break;
case (byte) DW_EH_PE.sdata4:
value = reader.ReadInt32 ();
break;
default:
throw new DwarfException (
reader.Bfd, "Unknown encoding `{0:x}' in CIE",
encoding);
}
return base_addr + value;
}
protected enum DW_CFA : byte
{
// First byte
advance_loc = 0x01,
offset = 0x02,
restore = 0x03,
// Second byte
nop = 0x00,
set_loc = 0x01,
advance_loc1 = 0x02,
advance_loc2 = 0x03,
advance_loc4 = 0x04,
offset_extended = 0x05,
restore_extended = 0x06,
undefined = 0x07,
same_value = 0x08,
register = 0x09,
remember_state = 0x0a,
restore_state = 0x0b,
def_cfa = 0x0c,
def_cfa_register = 0x0d,
def_cfa_offset = 0x0e,
def_cfa_expression = 0x0f,
cfa_expression = 0x10,
offset_extended_sf = 0x11,
def_cfa_sf = 0x12,
def_cfa_offset_sf = 0x13,
// GNU extensions
gnu_args_size = 0x2e
}
[Flags]
protected enum DW_EH_PE : byte
{
absptr = 0x00,
omit = 0xff,
uleb128 = 0x01,
udata2 = 0x02,
udata4 = 0x03,
udata8 = 0x04,
sleb128 = 0x09,
sdata2 = 0x0a,
sdata4 = 0x0b,
sdata8 = 0x0c,
signed = 0x08,
pcrel = 0x10,
textrel = 0x20,
datarel = 0x30,
funcrel = 0x40,
aligned = 0x50,
indirect= 0x80
}
protected enum State
{
Undefined,
SameValue,
Offset,
Register
}
protected struct Column
{
public State State;
public int Register;
public int Offset;
public Column (State state)
{
this.State = state;
this.Register = 0;
this.Offset = 0;
}
public override string ToString ()
{
return String.Format ("[{0}:{1}:{2:x}]", State, Register, Offset);
}
}
protected class Entry
{
public readonly CIE cie;
protected TargetAddress current_address;
protected TargetAddress address;
Column[] columns;
public Entry (CIE cie)
{
this.cie = cie;
this.current_address = TargetAddress.Null;
this.address = TargetAddress.Null;
this.columns = cie.Columns;
}
public Entry (CIE cie, TargetAddress initial_location,
TargetAddress address)
{
this.cie = cie;
this.current_address = initial_location;
this.address = address;
this.columns = new Column [cie.Columns.Length];
cie.Columns.CopyTo (columns, 0);
}
public Column[] Columns {
get { return columns; }
}
public void Read (DwarfBinaryReader reader, long end_pos)
{
while (reader.Position < end_pos) {
byte first = reader.ReadByte ();
int opcode = first >> 6;
int low = first & 0x3f;
if (opcode == (int) DW_CFA.offset) {
int offset = reader.ReadLeb128 ();
offset *= cie.DataAlignment;
columns [low + 1].State = State.Offset;
columns [low + 1].Register = 0;
columns [low + 1].Offset = offset;
continue;
} else if (opcode == (int) DW_CFA.advance_loc) {
current_address += low;
if (current_address > address)
return;
continue;
} else if (opcode != 0) {
continue;
}
switch ((DW_CFA) low) {
case DW_CFA.nop:
break;
case DW_CFA.def_cfa:
columns [0].State = State.Register;
columns [0].Register = reader.ReadLeb128 ();
columns [0].Offset = reader.ReadLeb128 ();
break;
case DW_CFA.def_cfa_register:
columns [0].State = State.Register;
columns [0].Register = reader.ReadLeb128 ();
break;
case DW_CFA.def_cfa_offset:
columns [0].Offset = reader.ReadLeb128 ();
break;
case DW_CFA.gnu_args_size:
// Ignored.
reader.ReadLeb128 ();
break;
default:
break;
}
}
}
int GetArchRegister (int index)
{
return cie.Architecture.DwarfFrameRegisterMap [index];
}
Register GetRegister (Registers regs, int index)
{
return regs [GetArchRegister (index)];
}
long GetRegisterValue (Registers regs, int reg, Column column)
{
int index = GetArchRegister (column.Register + 3);
long value = regs [index].GetValue () + column.Offset;
regs [GetArchRegister (reg)].SetValue (TargetAddress.Null, value);
return value;
}
void GetValue (TargetMemoryAccess target, Registers regs,
TargetAddress cfa, int reg, Column column)
{
switch (column.State) {
case State.Register: {
GetRegisterValue (regs, reg, column);
break;
}
case State.SameValue:
regs [GetArchRegister (reg)].Valid = true;
break;
case State.Undefined:
break;
case State.Offset: {
TargetAddress addr = cfa + column.Offset;
long value = target.ReadAddress (addr).Address;
regs [GetArchRegister (reg)].SetValue (address, value);
break;
}
default:
throw new NotSupportedException ();
}
}
void SetRegisters (Registers regs, TargetMemoryAccess target,
Architecture arch, Column[] columns)
{
long cfa_addr = GetRegisterValue (regs, 1, columns [0]);
TargetAddress cfa = new TargetAddress (
target.AddressDomain, cfa_addr);
for (int i = 1; i < columns.Length; i++) {
GetValue (target, regs, cfa, i+2, columns [i]);
}
}
public StackFrame Unwind (StackFrame frame, TargetMemoryAccess target,
Architecture arch)
{
Registers old_regs = frame.Registers;
Registers regs = arch.CopyRegisters (old_regs);
SetRegisters (regs, target, arch, columns);
Register eip = GetRegister (regs, 0);
Register esp = GetRegister (regs, 1);
Register ebp = GetRegister (regs, 2);
if (!eip.Valid || !esp.Valid)
return null;
TargetAddress address = new TargetAddress (
target.AddressDomain, eip.Value);
TargetAddress stack = new TargetAddress (
target.AddressDomain, esp.Value);
TargetAddress frame_addr = TargetAddress.Null;
if (ebp.Valid)
frame_addr = new TargetAddress (
target.AddressDomain, ebp.Value);
return arch.CreateFrame (
frame.Thread, target, address, stack, frame_addr, regs);
}
}
protected class CIE
{
DwarfFrameReader frame;
long offset;
CIE next;
int code_alignment;
int data_alignment;
int return_register;
// bool has_z_augmentation;
byte encoding = (byte) DW_EH_PE.udata4;
Column[] columns;
public CIE (DwarfFrameReader frame, long offset, CIE next)
{
this.frame = frame;
this.offset = offset;
this.next = next;
DwarfBinaryReader reader = new DwarfBinaryReader (
frame.bfd, frame.blob, false);
read_cie (reader);
}
public CIE Next {
get { return next; }
}
public Architecture Architecture {
get { return frame.bfd.Architecture; }
}
public long Offset {
get { return offset; }
}
public int CodeAlignment {
get { return code_alignment; }
}
public int DataAlignment {
get { return data_alignment; }
}
public int ReturnRegister {
get { return return_register; }
}
public byte Encoding {
get { return encoding; }
}
public Column[] Columns {
get { return columns; }
}
void read_cie (DwarfBinaryReader reader)
{
long length = reader.ReadInitialLength ();
long end_pos = reader.Position + length;
int id = reader.ReadInt32 ();
bool is_cie;
if (frame.is_ehframe)
is_cie = id == 0;
else
is_cie = id == -1;
if (!is_cie)
throw new InvalidOperationException ();
int version = reader.ReadByte ();
if (version != 1)
throw new DwarfException (
reader.Bfd, "Unknown version {0} in CIE",
version);
string augmentation = reader.ReadString ();
if (augmentation.StartsWith ("eh")) {
reader.ReadAddress ();
augmentation = augmentation.Substring (2);
}
code_alignment = reader.ReadLeb128 ();
data_alignment = reader.ReadSLeb128 ();
return_register = reader.ReadByte ();
for (int pos = 0; pos < augmentation.Length; pos++) {
if (augmentation [pos] == 'z') {
reader.ReadLeb128 ();
// has_z_augmentation = true;
continue;
}
if (augmentation [pos] == 'L')
continue;
else if (augmentation [pos] == 'R') {
encoding = reader.ReadByte ();
continue;
}
else if (augmentation [pos] == 'P') {
continue;
}
throw new DwarfException (
reader.Bfd, "Unknown augmentation `{0}' in CIE",
augmentation[pos]);
}
columns = new Column [return_register + 2];
for (int i = 0; i < columns.Length; i++)
columns [i] = new Column (State.Undefined);
Entry entry = new Entry (this);
entry.Read (reader, end_pos);
reader.Position = end_pos;
}
}
public override string ToString ()
{
return String.Format ("DwarfFrameReader ({0}:{1}:{2})",
bfd.FileName, is_ehframe, blob.Size);
}
}
}