Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  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
589
590
//===-------------------------- DwarfInstructions.hpp ---------------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//
//  Processor specific interpretation of DWARF unwind info.
//
//===----------------------------------------------------------------------===//

#ifndef __DWARF_INSTRUCTIONS_HPP__
#define __DWARF_INSTRUCTIONS_HPP__

#include <cstdint>
#include <cstdlib>

#include "dwarf2.h"
#include "AddressSpace.hpp"
#include "Registers.hpp"
#include "DwarfParser.hpp"

namespace _Unwind {

enum step_result {
  UNW_STEP_SUCCESS,
  UNW_STEP_END,
  UNW_STEP_FAILED
};

/// DwarfInstructions maps abtract dwarf unwind instructions to a particular
/// architecture
template <typename A, typename R> class DwarfInstructions {
public:
  typedef typename A::pint_t pint_t;
  typedef typename A::sint_t sint_t;

  static step_result stepWithDwarf(A &, pint_t, pint_t, R &, unw_proc_info_t *);

private:
  static pint_t evaluateExpression(pint_t, A &, const R &, pint_t);
  static pint_t
  getSavedRegister(A &, const R &, pint_t,
                   const typename CFI_Parser<A, R>::RegisterLocation &);
  static pint_t
  computeRegisterLocation(A &, const R &, pint_t,
                          const typename CFI_Parser<A, R>::RegisterLocation &);

  static int lastRestoreReg(const R &) { return R::LAST_RESTORE_REG; }

  static pint_t getCFA(A &addressSpace,
                       const typename CFI_Parser<A, R>::PrologInfo &prolog,
                       const R &registers) {
    if (prolog.cfaRegister != 0)
      return registers.getRegister(prolog.cfaRegister) +
             prolog.cfaRegisterOffset;
    if (prolog.cfaExpression != 0)
      return evaluateExpression(prolog.cfaExpression, addressSpace, registers,
                                0);
    assert(0 && "getCFA(): unknown location");
    __builtin_unreachable();
  }
};

template <typename A, typename R>
typename A::pint_t DwarfInstructions<A, R>::getSavedRegister(
    A &addressSpace, const R &registers, pint_t cfa,
    const typename CFI_Parser<A, R>::RegisterLocation &savedReg) {
  switch (savedReg.location) {
  case CFI_Parser<A, R>::kRegisterInCFA:
    return addressSpace.getP(cfa + savedReg.value);

  case CFI_Parser<A, R>::kRegisterAtExpression:
    return addressSpace.getP(
        evaluateExpression(savedReg.value, addressSpace, registers, cfa));

  case CFI_Parser<A, R>::kRegisterIsExpression:
    return evaluateExpression(savedReg.value, addressSpace, registers, cfa);

  case CFI_Parser<A, R>::kRegisterInRegister:
    return registers.getRegister(savedReg.value);

  case CFI_Parser<A, R>::kRegisterUnused:
  case CFI_Parser<A, R>::kRegisterOffsetFromCFA:
    assert(0 && "unsupported restore location for register");
  }
  __builtin_unreachable();
}

template <typename A, typename R>
typename DwarfInstructions<A, R>::pint_t
DwarfInstructions<A, R>::computeRegisterLocation(
    A &addressSpace, const R &registers, pint_t cfa,
    const typename CFI_Parser<A, R>::RegisterLocation &savedReg) {
  switch (savedReg.location) {
  case CFI_Parser<A, R>::kRegisterInCFA:
    return cfa + savedReg.value;

  case CFI_Parser<A, R>::kRegisterAtExpression:
    return evaluateExpression(savedReg.value, addressSpace, registers, cfa);

  case CFI_Parser<A, R>::kRegisterIsExpression:
  case CFI_Parser<A, R>::kRegisterUnused:
  case CFI_Parser<A, R>::kRegisterOffsetFromCFA:
  case CFI_Parser<A, R>::kRegisterInRegister:
    assert(0 && "unsupported restore location for float/vector register");
  }
  __builtin_unreachable();
}

template <typename A, typename R>
step_result DwarfInstructions<A, R>::stepWithDwarf(A &addressSpace, pint_t pc,
                                                   pint_t fdeStart,
                                                   R &registers,
                                                   unw_proc_info_t *ctx) {
  typename CFI_Parser<A, R>::FDE_Info fdeInfo;
  typename CFI_Parser<A, R>::CIE_Info cieInfo;
  if (!CFI_Parser<A, R>::decodeFDE(addressSpace, fdeStart, &fdeInfo, &cieInfo,
                                   ctx))
    return UNW_STEP_FAILED;

  typename CFI_Parser<A, R>::PrologInfo prolog;
  if (!CFI_Parser<A, R>::parseFDEInstructions(addressSpace, fdeInfo, cieInfo,
                                              pc, &prolog, ctx))
    return UNW_STEP_FAILED;

  // Create working copy of the register set.
  R newRegisters = registers;

  // Get pointer to CFA by the architecture-specific code.
  pint_t cfa = getCFA(addressSpace, prolog, registers);

  // Restore registers according to DWARF instructions
  pint_t returnAddress = 0;
  for (int i = 0; i <= lastRestoreReg(newRegisters); ++i) {
    if (prolog.savedRegisters[i].location == CFI_Parser<A, R>::kRegisterUnused)
      continue;
    if (i == (int)cieInfo.returnAddressRegister)
      returnAddress = getSavedRegister(addressSpace, registers, cfa,
                                       prolog.savedRegisters[i]);
    else if (registers.validRegister(i))
      newRegisters.setRegister(i, getSavedRegister(addressSpace, registers, cfa,
                                                   prolog.savedRegisters[i]));
    else if (registers.validFloatVectorRegister(i))
      newRegisters.copyFloatVectorRegister(
          i, computeRegisterLocation(addressSpace, registers, cfa,
                                     prolog.savedRegisters[i]));
    else
      return UNW_STEP_FAILED;
  }

  // The CFA is defined as the stack pointer at the call site.
  // Therefore the SP is restored by setting it to the CFA.
  newRegisters.setSP(cfa);
  returnAddress += R::RETURN_OFFSET;
  returnAddress &= ~R::RETURN_MASK;
  newRegisters.setIP(returnAddress);

  // Now replace register set with the working copy.
  registers = newRegisters;

  return UNW_STEP_SUCCESS;
}

template <typename A, typename R>
typename A::pint_t
DwarfInstructions<A, R>::evaluateExpression(pint_t expression, A &addressSpace,
                                            const R &registers,
                                            pint_t initialStackValue) {
  pint_t p = expression;
  pint_t expressionEnd = expression + 20; // Rough estimate
  uint64_t length = addressSpace.getULEB128(p, expressionEnd);
  expressionEnd = p + length;
  pint_t stack[100];
  pint_t *sp = stack;
  *(++sp) = initialStackValue;

  while (p < expressionEnd) {
    uint8_t opcode = addressSpace.get8(p++);
    sint_t svalue;
    pint_t value;
    uint32_t reg;
    switch (opcode) {
    case DW_OP_addr:
      // push immediate address sized value
      value = addressSpace.getP(p);
      p += sizeof(pint_t);
      *(++sp) = value;
      break;

    case DW_OP_deref:
      // pop stack, dereference, push result
      value = *sp--;
      *(++sp) = addressSpace.getP(value);
      break;

    case DW_OP_const1u:
      // push immediate 1 byte value
      value = addressSpace.get8(p);
      p += 1;
      *(++sp) = value;
      break;

    case DW_OP_const1s:
      // push immediate 1 byte signed value
      svalue = (int8_t)addressSpace.get8(p);
      p += 1;
      *(++sp) = svalue;
      break;

    case DW_OP_const2u:
      // push immediate 2 byte value
      value = addressSpace.get16(p);
      p += 2;
      *(++sp) = value;
      break;

    case DW_OP_const2s:
      // push immediate 2 byte signed value
      svalue = (int16_t)addressSpace.get16(p);
      p += 2;
      *(++sp) = svalue;
      break;

    case DW_OP_const4u:
      // push immediate 4 byte value
      value = addressSpace.get32(p);
      p += 4;
      *(++sp) = value;
      break;

    case DW_OP_const4s:
      // push immediate 4 byte signed value
      svalue = (int32_t)addressSpace.get32(p);
      p += 4;
      *(++sp) = svalue;
      break;

    case DW_OP_const8u:
      // push immediate 8 byte value
      value = addressSpace.get64(p);
      p += 8;
      *(++sp) = value;
      break;

    case DW_OP_const8s:
      // push immediate 8 byte signed value
      value = (int32_t)addressSpace.get64(p);
      p += 8;
      *(++sp) = value;
      break;

    case DW_OP_constu:
      // push immediate ULEB128 value
      value = addressSpace.getULEB128(p, expressionEnd);
      *(++sp) = value;
      break;

    case DW_OP_consts:
      // push immediate SLEB128 value
      svalue = addressSpace.getSLEB128(p, expressionEnd);
      *(++sp) = svalue;
      break;

    case DW_OP_dup:
      // push top of stack
      value = *sp;
      *(++sp) = value;
      break;

    case DW_OP_drop:
      // pop
      --sp;
      break;

    case DW_OP_over:
      // dup second
      value = sp[-1];
      *(++sp) = value;
      break;

    case DW_OP_pick:
      // pick from
      reg = addressSpace.get8(p);
      p += 1;
      value = sp[-reg];
      *(++sp) = value;
      break;

    case DW_OP_swap:
      // swap top two
      value = sp[0];
      sp[0] = sp[-1];
      sp[-1] = value;
      break;

    case DW_OP_rot:
      // rotate top three
      value = sp[0];
      sp[0] = sp[-1];
      sp[-1] = sp[-2];
      sp[-2] = value;
      break;

    case DW_OP_xderef:
      // pop stack, dereference, push result
      value = *sp--;
      *sp = *((uint64_t *)value);
      break;

    case DW_OP_abs:
      svalue = *sp;
      if (svalue < 0)
        *sp = -svalue;
      break;

    case DW_OP_and:
      value = *sp--;
      *sp &= value;
      break;

    case DW_OP_div:
      svalue = *sp--;
      *sp = *sp / svalue;
      break;

    case DW_OP_minus:
      svalue = *sp--;
      *sp = *sp - svalue;
      break;

    case DW_OP_mod:
      svalue = *sp--;
      *sp = *sp % svalue;
      break;

    case DW_OP_mul:
      svalue = *sp--;
      *sp = *sp * svalue;
      break;

    case DW_OP_neg:
      *sp = 0 - *sp;
      break;

    case DW_OP_not:
      svalue = *sp;
      *sp = ~svalue;
      break;

    case DW_OP_or:
      value = *sp--;
      *sp |= value;
      break;

    case DW_OP_plus:
      value = *sp--;
      *sp += value;
      break;

    case DW_OP_plus_uconst:
      // pop stack, add uelb128 constant, push result
      *sp += addressSpace.getULEB128(p, expressionEnd);
      break;

    case DW_OP_shl:
      value = *sp--;
      *sp = *sp << value;
      break;

    case DW_OP_shr:
      value = *sp--;
      *sp = *sp >> value;
      break;

    case DW_OP_shra:
      value = *sp--;
      svalue = *sp;
      *sp = svalue >> value;
      break;

    case DW_OP_xor:
      value = *sp--;
      *sp ^= value;
      break;

    case DW_OP_skip:
      svalue = (int16_t)addressSpace.get16(p);
      p += 2;
      p += svalue;
      break;

    case DW_OP_bra:
      svalue = (int16_t)addressSpace.get16(p);
      p += 2;
      if (*sp--)
        p += svalue;
      break;

    case DW_OP_eq:
      value = *sp--;
      *sp = (*sp == value);
      break;

    case DW_OP_ge:
      value = *sp--;
      *sp = (*sp >= value);
      break;

    case DW_OP_gt:
      value = *sp--;
      *sp = (*sp > value);
      break;

    case DW_OP_le:
      value = *sp--;
      *sp = (*sp <= value);
      break;

    case DW_OP_lt:
      value = *sp--;
      *sp = (*sp < value);
      break;

    case DW_OP_ne:
      value = *sp--;
      *sp = (*sp != value);
      break;

    case DW_OP_lit0:
    case DW_OP_lit1:
    case DW_OP_lit2:
    case DW_OP_lit3:
    case DW_OP_lit4:
    case DW_OP_lit5:
    case DW_OP_lit6:
    case DW_OP_lit7:
    case DW_OP_lit8:
    case DW_OP_lit9:
    case DW_OP_lit10:
    case DW_OP_lit11:
    case DW_OP_lit12:
    case DW_OP_lit13:
    case DW_OP_lit14:
    case DW_OP_lit15:
    case DW_OP_lit16:
    case DW_OP_lit17:
    case DW_OP_lit18:
    case DW_OP_lit19:
    case DW_OP_lit20:
    case DW_OP_lit21:
    case DW_OP_lit22:
    case DW_OP_lit23:
    case DW_OP_lit24:
    case DW_OP_lit25:
    case DW_OP_lit26:
    case DW_OP_lit27:
    case DW_OP_lit28:
    case DW_OP_lit29:
    case DW_OP_lit30:
    case DW_OP_lit31:
      value = opcode - DW_OP_lit0;
      *(++sp) = value;
      break;

    case DW_OP_reg0:
    case DW_OP_reg1:
    case DW_OP_reg2:
    case DW_OP_reg3:
    case DW_OP_reg4:
    case DW_OP_reg5:
    case DW_OP_reg6:
    case DW_OP_reg7:
    case DW_OP_reg8:
    case DW_OP_reg9:
    case DW_OP_reg10:
    case DW_OP_reg11:
    case DW_OP_reg12:
    case DW_OP_reg13:
    case DW_OP_reg14:
    case DW_OP_reg15:
    case DW_OP_reg16:
    case DW_OP_reg17:
    case DW_OP_reg18:
    case DW_OP_reg19:
    case DW_OP_reg20:
    case DW_OP_reg21:
    case DW_OP_reg22:
    case DW_OP_reg23:
    case DW_OP_reg24:
    case DW_OP_reg25:
    case DW_OP_reg26:
    case DW_OP_reg27:
    case DW_OP_reg28:
    case DW_OP_reg29:
    case DW_OP_reg30:
    case DW_OP_reg31:
      reg = opcode - DW_OP_reg0;
      *(++sp) = registers.getRegister(reg);
      break;

    case DW_OP_regx:
      reg = addressSpace.getULEB128(p, expressionEnd);
      *(++sp) = registers.getRegister(reg);
      break;

    case DW_OP_breg0:
    case DW_OP_breg1:
    case DW_OP_breg2:
    case DW_OP_breg3:
    case DW_OP_breg4:
    case DW_OP_breg5:
    case DW_OP_breg6:
    case DW_OP_breg7:
    case DW_OP_breg8:
    case DW_OP_breg9:
    case DW_OP_breg10:
    case DW_OP_breg11:
    case DW_OP_breg12:
    case DW_OP_breg13:
    case DW_OP_breg14:
    case DW_OP_breg15:
    case DW_OP_breg16:
    case DW_OP_breg17:
    case DW_OP_breg18:
    case DW_OP_breg19:
    case DW_OP_breg20:
    case DW_OP_breg21:
    case DW_OP_breg22:
    case DW_OP_breg23:
    case DW_OP_breg24:
    case DW_OP_breg25:
    case DW_OP_breg26:
    case DW_OP_breg27:
    case DW_OP_breg28:
    case DW_OP_breg29:
    case DW_OP_breg30:
    case DW_OP_breg31:
      reg = opcode - DW_OP_breg0;
      svalue = addressSpace.getSLEB128(p, expressionEnd);
      *(++sp) = registers.getRegister(reg) + svalue;
      break;

    case DW_OP_bregx:
      reg = addressSpace.getULEB128(p, expressionEnd);
      svalue = addressSpace.getSLEB128(p, expressionEnd);
      *(++sp) = registers.getRegister(reg) + svalue;
      break;

    case DW_OP_deref_size:
      // pop stack, dereference, push result
      value = *sp--;
      switch (addressSpace.get8(p++)) {
      case 1:
        value = addressSpace.get8(value);
        break;
      case 2:
        value = addressSpace.get16(value);
        break;
      case 4:
        value = addressSpace.get32(value);
        break;
      case 8:
        value = addressSpace.get64(value);
        break;
      default:
        assert(0 && "DW_OP_deref_size with bad size");
      }
      *(++sp) = value;
      break;

    case DW_OP_fbreg:
    case DW_OP_piece:
    case DW_OP_xderef_size:
    case DW_OP_nop:
    case DW_OP_push_object_addres:
    case DW_OP_call2:
    case DW_OP_call4:
    case DW_OP_call_ref:
    default:
      assert(0 && "dwarf opcode not implemented");
    }
  }
  return *sp;
}

} // namespace _Unwind

#endif // __DWARF_INSTRUCTIONS_HPP__