Source

gimli / dwarf-expr.c

Full commit
  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
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
/*
 * Copyright (c) 2009 Message Systems, Inc. All rights reserved
 * For licensing information, see:
 * https://bitbucket.org/wez/gimli/src/tip/LICENSE
 */
#include "impl.h"
#include "gimli_dwarf.h"
//#define debug 1

struct dw_stack_val {
  int is_signed;
  int is_stack;
  union {
    uint64_t u64;
    int64_t s64;
  } v;
};

struct dw_expr {
  int top;
  struct dw_stack_val stack[255];
  const uint8_t *ops, *end;
};

static int push(struct dw_expr *e, struct dw_stack_val *v)
{
  if (e->top > 0 && e->top >= sizeof(e->stack)/sizeof(e->stack[0])) {
    fprintf(stderr, "DWARF: expr: stack overflow (%d)\n", e->top);
    return 0;
  }
  e->stack[++e->top] = *v;
  if (debug) printf("push: sp=%d %" PRIx64 "\n", e->top, e->stack[e->top].v.u64);
  return 1;
}

static int pop(struct dw_expr *e, struct dw_stack_val *v)
{
  if (e->top < 0) {
    fprintf(stderr, "DWARF:expr: stack underflow\n");
    return 0;
  }
  *v = e->stack[e->top--];
  if (debug) printf("pop: sp=%d %" PRIx64 "\n", e->top, v->v.u64);
  return 1;
}

static int deref(gimli_proc_t proc, uint64_t ptr, uint64_t *resp, uint8_t opsize)
{
  uint8_t u8;
  uint16_t u16;
  uint32_t u32;
  uint64_t u64;
  int res;

  switch (opsize) {
    case 1:
      res = gimli_read_mem(proc, ptr, &u8, opsize);
      u64 = u8;
      break;
    case 2:
      res = gimli_read_mem(proc, ptr, &u16, opsize);
      u64 = u16;
      break;
    case 4:
      res = gimli_read_mem(proc, ptr, &u32, opsize);
      u64 = u32;
      break;
    case 8:
      res = gimli_read_mem(proc, ptr, &u64, opsize);
      break;
  }
  if (res != opsize) {
    fprintf(stderr, "DWARF: expr: unable to deref %d bytes from " PTRFMT "\n",
      opsize, ptr);
    return 0;
  }
  if (debug) printf("deref: addr=" PTRFMT " opsize=%d res=0x%x\n", ptr, opsize, res);
  *resp = res;
  return 1;
}

int dw_eval_expr(struct gimli_unwind_cursor *cur, const uint8_t *ops,
  uint64_t oplen,
  uint64_t frame_base, uint64_t *result, uint64_t *prepopulate,
  int *is_stack)
{
  struct dw_expr e;
  uint32_t u32;
  int32_t s32;
  uint8_t u8;
  int8_t s8;
  uint16_t u16;
  int16_t s16;
  int64_t s64;
  uint64_t u64;
  struct dw_stack_val val, val2;
  int i;

  memset(&e, 0, sizeof(e));
  e.ops = ops;
  e.end = ops + oplen;
  e.top = -1;

  if (prepopulate) {
    val.is_signed = 0;
    val.is_stack = is_stack ? *is_stack : 0;
    val.v.u64 = *prepopulate;
    push(&e, &val);
  }

  memset(&val, 0, sizeof(val));
  memset(&val2, 0, sizeof(val2));

  while (e.ops < e.end) {
    uint8_t op = (uint8_t)*e.ops;

    if (debug) printf("OP: %02x\n", op);
    e.ops++;

    /* literal encodings: all these operations push a value onto the stack */
    if (op >= DW_OP_lit0 && op <= DW_OP_lit31) {
      val.is_signed = 0;
      val.is_stack = 0;
      val.v.u64 = op - DW_OP_lit0;
      if (debug) printf("OP_lit%d\n", op - DW_OP_lit0);
      if (!push(&e, &val)) return 0;
      continue;
    }
    if (op >= DW_OP_breg0 && op <= DW_OP_breg31) {
      s64 = dw_read_leb128(&e.ops, e.end);
      val.is_signed = 0;
      val.is_stack = 1;
      if (!gimli_reg_get(cur, op - DW_OP_breg0, &val.v.u64)) return 0;
      if (debug) printf("OP_breg%d val=%" PRIx64 "\n", op - DW_OP_breg0, val.v.u64);
      val.v.u64 += s64;
      if (!push(&e, &val)) return 0;
      continue;
    }
    if (op >= DW_OP_reg0 && op <= DW_OP_reg31) {
      val.is_signed = 0;
      val.is_stack = 0;
      if (!gimli_reg_get(cur, op - DW_OP_reg0, &val.v.u64)) return 0;
      if (debug) printf("OP_reg%d -> %" PRIx64 "\n", op - DW_OP_reg0, val.v.u64);
      if (!push(&e, &val)) return 0;
      continue;
    }

    switch (op) {
      case DW_OP_addr:
        val.is_signed = 0;
        val.is_stack = 1;
        if (sizeof(void*) == 4) {
          memcpy(&u32, e.ops, sizeof(u32));
          e.ops += sizeof(u32);
          val.v.u64 = u32;
        } else {
          memcpy(&val.v.u64, e.ops, sizeof(val.v.u64));
          e.ops += sizeof(val.v.u64);
        }
        if (debug) printf("OP_addr: %" PRIx64 "\n", val.v.u64);
        if (!push(&e, &val)) return 0;
        continue;

      case DW_OP_const1u:
        memcpy(&u8, e.ops, sizeof(u8));
        val.is_signed = 0;
        val.is_stack = 1;
        val.v.u64 = u8;
        if (debug) printf("OP_const1u: %" PRIx64 "\n", val.v.u64);
        if (!push(&e, &val)) return 0;
        continue;

      case DW_OP_const1s:
        memcpy(&s8, e.ops, sizeof(s8));
        val.is_signed = 1;
        val.is_stack = 1;
        val.v.s64 = s8;
        if (debug) printf("OP_const1s: %" PRIx64 "\n", val.v.u64);
        if (!push(&e, &val)) return 0;
        continue;

      case DW_OP_const2u:
        memcpy(&u16, e.ops, sizeof(u16));
        val.is_signed = 0;
        val.is_stack = 1;
        val.v.u64 = u16;
        if (debug) printf("OP_const2u: %" PRIx64 "\n", val.v.u64);
        if (!push(&e, &val)) return 0;
        continue;

      case DW_OP_const2s:
        memcpy(&s16, e.ops, sizeof(s16));
        val.is_signed = 1;
        val.is_stack = 1;
        val.v.s64 = s16;
        if (debug) printf("OP_const2s: %" PRIx64 "\n", val.v.u64);
        if (!push(&e, &val)) return 0;
        continue;

      case DW_OP_const4u:
        memcpy(&u32, e.ops, sizeof(u32));
        val.is_signed = 0;
        val.is_stack = 1;
        val.v.u64 = u32;
        if (debug) printf("OP_const4u: %" PRIx64 "\n", val.v.u64);
        if (!push(&e, &val)) return 0;
        continue;

      case DW_OP_const4s:
        memcpy(&s32, e.ops, sizeof(s32));
        val.is_signed = 1;
        val.is_stack = 1;
        val.v.s64 = s32;
        if (debug) printf("OP_const4s: %" PRIx64 "\n", val.v.u64);
        if (!push(&e, &val)) return 0;
        continue;

      case DW_OP_const8u:
        memcpy(&val.v.u64, e.ops, sizeof(val.v.u64));
        val.is_signed = 0;
        val.is_stack = 1;
        if (debug) printf("OP_const8u: %" PRIx64 "\n", val.v.u64);
        if (!push(&e, &val)) return 0;
        continue;

      case DW_OP_const8s:
        memcpy(&val.v.s64, e.ops, sizeof(val.v.s64));
        val.is_signed = 1;
        val.is_stack = 1;
        if (debug) printf("OP_const8s: %" PRIx64 "\n", val.v.u64);
        if (!push(&e, &val)) return 0;
        continue;

      case DW_OP_constu:
        val.v.u64 = dw_read_uleb128(&e.ops, e.end);
        val.is_signed = 0;
        val.is_stack = 1;
        if (debug) printf("OP_constu: %" PRIx64 "\n", val.v.u64);
        if (!push(&e, &val)) return 0;
        continue;

      case DW_OP_consts:
        val.v.s64 = dw_read_leb128(&e.ops, e.end);
        val.is_signed = 1;
        val.is_stack = 1;
        if (debug) printf("OP_consts: %" PRIx64 "\n", val.v.u64);
        if (!push(&e, &val)) return 0;
        continue;

      case DW_OP_fbreg:
        s64 = dw_read_leb128(&e.ops, e.end);
        val.is_signed = 0;
        val.is_stack = 1;
        val.v.u64 = frame_base + s64;
        if (debug) printf("OP_fbreg: %" PRIx64 "\n", val.v.u64);
        if (!push(&e, &val)) return 0;
        continue;

      case DW_OP_bregx:
        u64 = dw_read_uleb128(&e.ops, e.end);
        s64 = dw_read_leb128(&e.ops, e.end);
        val.is_signed = 0;
        val.is_stack = 1;
        if (!gimli_reg_get(cur, u64, &val.v.u64)) return 0;
        val.v.u64 += s64;
        if (debug) printf("OP_breg%" PRId64 ": %" PRIx64 "\n", u64, val.v.u64);
        if (!push(&e, &val)) return 0;
        continue;

      case DW_OP_regx:
        u64 = dw_read_uleb128(&e.ops, e.end);
        val.is_signed = 0;
        val.is_stack = 0;
        if (!gimli_reg_get(cur, u64, &val.v.u64)) return 0;
        if (debug) printf("OP_reg%" PRId64 "\n", u64);
        if (!push(&e, &val)) return 0;
        continue;

      case DW_OP_dup:
        val = e.stack[e.top];
        if (debug) printf("OP_dup\n");
        if (!push(&e, &val)) return 0;
        continue;

      case DW_OP_drop:
        if (!pop(&e, &val)) return 0;
        if (debug) printf("OP_drop\n");
        continue;

      case DW_OP_pick:
        memcpy(&u8, e.ops, sizeof(u8));
        e.ops += sizeof(u8);
        if (debug) printf("OP_pick %" PRIu8 "\n", u8);
        if (e.top - u8 < 0) {
          fprintf(stderr, "DWARF:expr: DW_OP_pick(%" PRIu8 "): stack underflow\n", u8);
          return 0;
        }
        val = e.stack[e.top - u8];
        if (!push(&e, &val)) return 0;
        continue;

      case DW_OP_over:
        if (e.top - 1 < 0) {
          fprintf(stderr, "DWARF:expr: DW_OP_over: stack underflow\n");
          return 0;
        }
        val = e.stack[e.top - 1];
        if (!push(&e, &val)) return 0;
        continue;

      case DW_OP_swap:
        if (e.top < 1) {
          fprintf(stderr, "DWARF: expr: DW_OP_swap: stack underflow\n");
          return 0;
        }

        val = e.stack[e.top - 1];
        e.stack[e.top - 1] = e.stack[e.top];
        e.stack[e.top] = val;
        continue;

      case DW_OP_rot:
        if (e.top < 2) {
          fprintf(stderr, "DWARF: expr: DW_OP_rot: stack underflow\n");
          return 0;
        }

        val = e.stack[e.top];
        e.stack[e.top] = e.stack[e.top - 2];
        e.stack[e.top - 2] = e.stack[e.top - 1];
        e.stack[e.top - 1] = val;
        continue;

      case DW_OP_deref:
        if (!pop(&e, &val)) return 0;
        if (!deref(cur->proc, val.v.u64, &u64, sizeof(void*))) return 0;
        val.is_signed = 0;
        val.is_stack = 1;
        if (!push(&e, &val)) return 0;
        continue;

      case DW_OP_deref_size:
        memcpy(&u8, e.ops, sizeof(u8));
        if (!pop(&e, &val)) return 0;
        if (!deref(cur->proc, val.v.u64, &u64, u8)) return 0;
        val.is_signed = 0;
        val.is_stack = 1;
        if (!push(&e, &val)) return 0;
        continue;

      case DW_OP_call_frame_cfa:
        val.is_signed = 0;
        val.is_stack = 0;
        val.v.u64 = (uint64_t)(intptr_t)cur->st.fp;
        if (!push(&e, &val)) return 0;
        continue;

      case DW_OP_abs:
        if (!pop(&e, &val)) return 0;
        if (val.v.s64 < 0) {
          u64 = -val.v.s64;
        } else {
          u64 = val.v.s64;
        }
        val.v.u64 = u64;
        val.is_stack = 1;
        if (!push(&e, &val)) return 0;
        continue;

      case DW_OP_and:
        if (!pop(&e, &val)) return 0;
        if (!pop(&e, &val2)) return 0;
        val.v.u64 &= val2.v.u64;
        val.is_stack = 1;
        if (!push(&e, &val)) return 0;
        continue;

      case DW_OP_div:
        if (!pop(&e, &val)) return 0;
        if (!pop(&e, &val2)) return 0;
        val.v.u64 = val2.v.u64 / val.v.u64;
        val.is_stack = 1;
        if (!push(&e, &val)) return 0;
        continue;

      case DW_OP_minus:
        if (!pop(&e, &val)) return 0;
        if (!pop(&e, &val2)) return 0;
        val.v.u64 = val2.v.u64 - val.v.u64;
        val.is_stack = 1;
        if (!push(&e, &val)) return 0;
        continue;

      case DW_OP_mod:
        if (!pop(&e, &val)) return 0;
        if (!pop(&e, &val2)) return 0;
        val.v.u64 = val2.v.u64 % val.v.u64;
        val.is_stack = 1;
        if (!push(&e, &val)) return 0;
        continue;

      case DW_OP_mul:
        if (!pop(&e, &val)) return 0;
        if (!pop(&e, &val2)) return 0;
        val.v.u64 = val2.v.u64 * val.v.u64;
        val.is_stack = 1;
        if (!push(&e, &val)) return 0;
        continue;

      case DW_OP_neg:
        if (!pop(&e, &val)) return 0;
        val.v.u64 = ~val.v.u64;
        val.is_stack = 1;
        if (!push(&e, &val)) return 0;
        continue;

      case DW_OP_not:
        if (!pop(&e, &val)) return 0;
        val.v.u64 = !val.v.u64;
        val.is_stack = 1;
        if (!push(&e, &val)) return 0;
        continue;

      case DW_OP_or:
        if (!pop(&e, &val)) return 0;
        if (!pop(&e, &val2)) return 0;
        val.v.u64 |= val2.v.u64;
        val.is_stack = 1;
        if (!push(&e, &val)) return 0;
        continue;

      case DW_OP_plus:
        if (!pop(&e, &val)) return 0;
        if (!pop(&e, &val2)) return 0;
        val.v.u64 += val2.v.u64;
        val.is_stack = 1;
        if (!push(&e, &val)) return 0;
        continue;

      case DW_OP_plus_uconst:
        if (!pop(&e, &val)) return 0;
        val.v.u64 += dw_read_uleb128(&e.ops, e.end);
        val.is_stack = 1;
        if (!push(&e, &val)) return 0;
        continue;

      case DW_OP_shl:
        if (!pop(&e, &val)) return 0;
        if (!pop(&e, &val2)) return 0;
        val.v.u64 = val2.v.u64 << val.v.u64;
        val.is_stack = 1;
        if (!push(&e, &val)) return 0;
        continue;

      case DW_OP_shr:
        if (!pop(&e, &val)) return 0;
        if (!pop(&e, &val2)) return 0;
        val.v.u64 = val2.v.u64 >> val.v.u64;
        val.is_stack = 1;
        if (!push(&e, &val)) return 0;
        continue;

      case DW_OP_shra:
        if (!pop(&e, &val)) return 0;
        if (!pop(&e, &val2)) return 0;
        if (val2.is_signed) {
          for (i = 0; i < val.v.u64; i++) {
            val2.v.s64 /= 2;
          }
        } else {
          val2.v.u64 = val2.v.u64 >> val.v.u64;
        }
        val2.is_stack = 1;
        if (!push(&e, &val2)) return 0;
        continue;

      case DW_OP_xor:
        if (!pop(&e, &val)) return 0;
        if (!pop(&e, &val2)) return 0;
        val.v.u64 = val2.v.u64 ^ val.v.u64;
        val.is_stack = 1;
        if (!push(&e, &val)) return 0;
        continue;

      case DW_OP_le:
        if (!pop(&e, &val)) return 0;
        if (!pop(&e, &val2)) return 0;
        val.v.u64 = (val2.v.s64 <= val.v.s64) ? 1 : 0;
        val.is_signed = 0;
        val.is_stack = 1;
        if (!push(&e, &val)) return 0;
        continue;

      case DW_OP_lt:
        if (!pop(&e, &val)) return 0;
        if (!pop(&e, &val2)) return 0;
        val.v.u64 = (val2.v.s64 < val.v.s64) ? 1 : 0;
        val.is_signed = 0;
        val.is_stack = 1;
        if (!push(&e, &val)) return 0;
        continue;

      case DW_OP_ge:
        if (!pop(&e, &val)) return 0;
        if (!pop(&e, &val2)) return 0;
        val.v.u64 = (val2.v.s64 >= val.v.s64) ? 1 : 0;
        val.is_signed = 0;
        val.is_stack = 1;
        if (!push(&e, &val)) return 0;
        continue;

      case DW_OP_gt:
        if (!pop(&e, &val)) return 0;
        if (!pop(&e, &val2)) return 0;
        val.v.u64 = (val2.v.s64 > val.v.s64) ? 1 : 0;
        val.is_signed = 0;
        val.is_stack = 1;
        if (!push(&e, &val)) return 0;
        continue;

      case DW_OP_eq:
        if (!pop(&e, &val)) return 0;
        if (!pop(&e, &val2)) return 0;
        val.v.u64 = (val2.v.s64 == val.v.s64) ? 1 : 0;
        val.is_signed = 0;
        val.is_stack = 1;
        if (!push(&e, &val)) return 0;
        continue;

      case DW_OP_ne:
        if (!pop(&e, &val)) return 0;
        if (!pop(&e, &val2)) return 0;
        val.v.u64 = (val2.v.s64 != val.v.s64) ? 1 : 0;
        val.is_signed = 0;
        val.is_stack = 1;
        if (!push(&e, &val)) return 0;
        continue;

      case DW_OP_skip:
        memcpy(&s16, e.ops, sizeof(s16));
        e.ops += sizeof(s16);
        e.ops += s16;
        continue;

      case DW_OP_bra:
        memcpy(&s16, e.ops, sizeof(s16));
        e.ops += sizeof(s16);
        if (!pop(&e, &val)) return 0;
        if (val.v.u64) {
          e.ops += s16;
        }
        continue;

      case DW_OP_nop:
        continue;

      case DW_OP_GNU_entry_value:
      case DW_OP_implicit_value:
      case DW_OP_stack_value:
      case DW_OP_piece:
        /* The DW_OP_stack_value operation specifies that the object does not
         * exist in memory but its value is nonetheless known and is at the top
         * of the DWARF expression stack. In this form of location description,
         * the DWARF expression represents the actual value of the object,
         * rather than its location. The DW_OP_stack_value operation terminates
         * the expression.
         *
         * Unfortunately, we're focused around expressions returning
         * addresses rather than values, so while it is simple to
         * break here to obtain the desired value, we have no way to convey
         * that result back to the caller at this time.
         */
        return 0;

      default:
        fprintf(stderr, "DWARF: expr: unhandled op 0x%02x\n", op);
        return 0;
    }
  }

  if (is_stack) *is_stack = e.stack[e.top].is_stack;
  *result = e.stack[e.top].v.u64;
  if (debug) printf("eval expr: result=%" PRIx64 "\n", *result);
  return 1;
}

/* vim:ts=2:sw=2:et:
 */