Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: c65261edf456763268011566d57b29bd196d1024 (plain) (blame)
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
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
/*******************************************************************************
 * Copyright (c) 2008 Wind River Systems, Inc. and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * and Eclipse Distribution License v1.0 which accompany this distribution.
 * The Eclipse Public License is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * and the Eclipse Distribution License is available at
 * http://www.eclipse.org/org/documents/edl-v10.php.
 *
 * Contributors:
 *     Wind River Systems - initial API and implementation
 *******************************************************************************/

/*
 * This module implements DWARF expressions evaluation.
 */
#include "mdep.h"
#include "config.h"

#if ENABLE_ELF

#include <assert.h>
#include <stdio.h>
#include "myalloc.h"
#include "dwarf.h"
#include "dwarfio.h"
#include "dwarfexpr.h"
#include "stacktrace.h"
#include "breakpoints.h"
#include "exceptions.h"
#include "errors.h"
#include "trace.h"

#define MD_READ     1
#define MD_WRITE    2
#define MD_ADDRESS  3
#define MD_FBASE    4

static int sMode;
static U1_T * sDataBuf;
static size_t sDataBufSize;
static int sDataDone;

static Context * sLocContext;
static ObjectInfo * sLocObject;
static int sLocFrame;
static ContextAddress sLocIP;
static ContextAddress sLocFP;

static U8_T * sExprStack = NULL;
static unsigned sExprStackLen = 0;
static unsigned sExprStackMax = 0;

#define check_e_stack(n) if (sExprStackLen < n) { errno = ERR_INV_DWARF; return -1; }

static ObjectInfo * get_parent_function(ObjectInfo * Info) {
    while (Info != NULL) {
        switch (Info->mTag) {
        case TAG_global_subroutine:
        case TAG_subroutine:
        case TAG_subprogram:
        case TAG_entry_point:
            return Info;
        }
        Info = Info->mParent;
    }
    return NULL;
}

/*
  I386 GNU DWARF register numbers:
   0: AX
   1: CX
   2: DX
   3: BX
   4: SP
   5: BP
   6: SI
   7: DI
   8: IP
   9: Flags
  11..18: I387 ST0..ST7
  21..28: XMM0..XMM7
  29..36: MM0..MM7
  37: FCTRL
  38: FSTAT
  39: MXCSR
  40: ES
  41: CS
  42: SS
  43: DS
  44: FS
  45: GS
*/
static int get_register(unsigned rg, U8_T * value) {
#if defined(__linux__) && defined(__i386__) || \
    defined(_WRS_KERNEL) && (CPU_FAMILY==SIMNT || CPU_FAMILY==I80X86)
    if (is_top_frame(sLocContext, sLocFrame)) {
        switch (rg) {
        case 0:
            *value = sLocContext->regs.eax;
            return 0;
        case 1:
            *value = sLocContext->regs.ecx;
            return 0;
        case 2:
            *value = sLocContext->regs.edx;
            return 0;
        case 3:
            *value = sLocContext->regs.ebx;
            return 0;
        case 4:
            *value = sLocContext->regs.esp;
            return 0;
        case 5:
            *value = sLocContext->regs.ebp;
            return 0;
        case 6:
            *value = sLocContext->regs.esi;
            return 0;
        case 7:
            *value = sLocContext->regs.edi;
            return 0;
        case 8:
#ifdef _WRS_KERNEL
            *value = (unsigned)sLocContext->regs.pc;
#else            
            *value = sLocContext->regs.eip;
#endif            
            return 0;
        case 9:
            *value = sLocContext->regs.eflags;
            return 0;
        }
    }
    if (sLocIP == 0 && get_frame_info(sLocContext, sLocFrame, &sLocIP, NULL, &sLocFP) < 0) return -1;
    switch (rg){
    case 5:
        *value = sLocFP;
        return 0;
    case 8:
        *value = sLocIP;
        return 0;
    }
    trace(LOG_ALWAYS, "get_register: Unsupported DWARF register number %d", rg); 
    errno = ERR_UNSUPPORTED;
    return -1;
#else
#error "Unknown DWARF registers mapping"
#endif
}

static int set_register(unsigned rg, U8_T value) {
#if defined(__linux__) && defined(__i386__) || \
    defined(_WRS_KERNEL) && (CPU_FAMILY==SIMNT || CPU_FAMILY==I80X86)
    if (is_top_frame(sLocContext, sLocFrame)) {
        switch (rg) {
        case 0:
            sLocContext->regs.eax = (unsigned long)value;
            return 0;
        case 1:
            sLocContext->regs.ecx = (unsigned long)value;
            return 0;
        case 2:
            sLocContext->regs.edx = (unsigned long)value;
            return 0;
        case 3:
            sLocContext->regs.ebx = (unsigned long)value;
            return 0;
        case 4:
            sLocContext->regs.esp = (unsigned long)value;
            return 0;
        case 5:
            sLocContext->regs.ebp = (unsigned long)value;
            return 0;
        case 6:
            sLocContext->regs.esi = (unsigned long)value;
            return 0;
        case 7:
            sLocContext->regs.edi = (unsigned long)value;
            return 0;
        case 8:
#ifdef _WRS_KERNEL
            sLocContext->regs.pc = (void *)(unsigned long)value;
#else            
            sLocContext->regs.eip = (unsigned long)value;
#endif            
            return 0;
        case 9:
            sLocContext->regs.eflags = (unsigned long)value;
            return 0;
        }
    }
    trace(LOG_ALWAYS, "set_register: Unsupported DWARF register number %d", rg); 
    errno = ERR_UNSUPPORTED;
    return -1;

#else
#error "Unknown DWARF registers mapping"
#endif
}

static int register_access_expression(unsigned rg) {
    U8_T Data = 0;

    assert(!sDataDone);
    switch (sMode) {
    case MD_FBASE:
        if (get_register(rg, &Data) < 0) return -1;
        sExprStack[sExprStackLen++] = Data;
        return 0;
    case MD_READ:
        if (get_register(rg, &Data) < 0) return -1;
        switch (sDataBufSize) {
        case 1:
            *sDataBuf = (U1_T)Data;
            break;
        case 2:
            *(U2_T *)sDataBuf = (U2_T)Data;
            break;
        case 4:
            *(U4_T *)sDataBuf = (U4_T)Data;
            break;
        case 8:
            *(U8_T *)sDataBuf = Data;
            break;
        default:
            errno = ERR_INV_DWARF;
            return -1;
        }
        break;
    case MD_WRITE:
        switch (sDataBufSize) {
        case 1:
            Data = *sDataBuf;
            break;
        case 2:
            Data = *(U2_T *)sDataBuf;
            break;
        case 4:
            Data = *(U4_T *)sDataBuf;
            break;
        case 8:
            Data = *(U8_T *)sDataBuf;
            break;
        default:
            errno = ERR_INV_DWARF;
            return -1;
        }
        set_register(rg, Data);
        break;
    default:
        errno = ERR_INV_DWARF;
        return -1;
    }
    sDataDone = 1;
    return 0;
}

static void enter_section(CompUnit * Unit, U1_T * Data, U8_T Offset, U8_T Size) {
    dio_EnterSectionData(Unit->mFile, Data, Offset, Size);
    dio_gVersion = Unit->mVersion;
    dio_g64bit = Unit->mFile->elf64;
    dio_gAddressSize = Unit->mAddressSize;
}

static int evaluate_expression(U1_T * Buf, size_t Size);

static int evaluate_location(const LocationInfo * Loc) {
    if (Loc->mList) {
        U1_T * Addr = NULL;
        U8_T Offset = 0;
        U8_T Base = 0;
        U8_T BaseMark = 0;
        DWARFCache * Cache = (DWARFCache *)sLocObject->mCompUnit->mFile->dwarf_dt_cache;
        assert(Cache->magic == SYM_CACHE_MAGIC);
        if (Cache->mDebugLoc == NULL) {
            errno = ERR_INV_DWARF;
            return -1;
        }
        enter_section(sLocObject->mCompUnit, Loc->mAddr, 0, Loc->mSize);
        switch (Loc->mSize) {
        case 4:
            Offset = dio_ReadU4();
            BaseMark = ~(U4_T)0;
            break;
        case 8:
            Offset = dio_ReadU8();
            BaseMark = ~(U8_T)0;
            break;
        default:
            errno = ERR_INV_DWARF;
            return -1;
        }
        if (elf_load(Cache->mDebugLoc) < 0) return -1;
        Addr = Cache->mDebugLoc->data;
        Base = sLocObject->mCompUnit->mLowPC;
        enter_section(sLocObject->mCompUnit, Addr, Offset, Cache->mDebugLoc->size);
        while (1) {
            U8_T Addr0 = dio_ReadAddress();
            U8_T Addr1 = dio_ReadAddress();
            if (Addr0 == BaseMark) {
                Base = Addr1;
            }
            else if (Addr0 == 0 && Addr1 == 0) {
                break;
            }
            else {
                U2_T Size = dio_ReadU2();
                if (sLocIP == 0 && get_frame_info(sLocContext, sLocFrame, &sLocIP, NULL, &sLocFP) < 0) return -1;
                if (sLocIP >= Base + Addr0 && sLocIP < Base + Addr1) {
                    return evaluate_expression(Addr + dio_GetPos(), Size);
                }
                dio_Skip(Size);
            }
        }
        errno = ERR_INV_ADDRESS;
        return -1;
    }
    return evaluate_expression(Loc->mAddr, Loc->mSize);
}

static int evaluate_expression(U1_T * Buf, size_t Size) {
    enter_section(sLocObject->mCompUnit, Buf, 0, Size);
    while (dio_GetPos() < Size) {
        U1_T Op = dio_ReadU1();
        U8_T Data = 0;

        if (sExprStackLen >= sExprStackMax) {
            sExprStackMax = sExprStackMax == 0 ? 8 : sExprStackMax * 2;
            sExprStack = loc_realloc(sExprStack, sizeof(U8_T) * sExprStackMax);
        }
        switch (Op) {
        case OP_addr:
            sExprStack[sExprStackLen++] = dio_ReadAddress();
            break;
        case OP_deref:
            check_e_stack(1);
            {
                U1_T Tmp[8];
                ContextAddress Addr = (ContextAddress)sExprStack[sExprStackLen - 1];
                if (context_read_mem(sLocContext, Addr, Tmp, dio_gAddressSize) < 0) return -1;
                check_breakpoints_on_memory_read(sLocContext, Addr, Tmp, dio_gAddressSize);
                switch (dio_gAddressSize)  {
                case 1: Data = *Tmp; break;
                case 2: Data = *(U2_T *)Tmp; break;
                case 4: Data = *(U4_T *)Tmp; break;
                case 8: Data = *(U8_T *)Tmp; break;
                default: assert(0);
                }
                sExprStack[sExprStackLen - 1] = Data;
            }            
            break;
        case OP_const1u:
            sExprStack[sExprStackLen++] = dio_ReadU1();
            break;
        case OP_const1s:
            sExprStack[sExprStackLen++] = (I1_T)dio_ReadU1();
            break;
        case OP_const2u:
            sExprStack[sExprStackLen++] = dio_ReadU2();
            break;
        case OP_const2s:
            sExprStack[sExprStackLen++] = (I2_T)dio_ReadU2();
            break;
        case OP_const4u:
            sExprStack[sExprStackLen++] = dio_ReadU4();
            break;
        case OP_const4s:
            sExprStack[sExprStackLen++] = (I4_T)dio_ReadU4();
            break;
        case OP_const8u:
            sExprStack[sExprStackLen++] = dio_ReadU8();
            break;
        case OP_const8s:
            sExprStack[sExprStackLen++] = (I8_T)dio_ReadU8();
            break;
        case OP_constu:
            sExprStack[sExprStackLen++] = dio_ReadU8LEB128();
            break;
        case OP_consts:
            sExprStack[sExprStackLen++] = dio_ReadS8LEB128();
            break;
        case OP_dup:
            check_e_stack(1);
            sExprStack[sExprStackLen] = sExprStack[sExprStackLen - 1];
            sExprStackLen++;
            break;
        case OP_drop:
            check_e_stack(1);
            sExprStackLen--;
            break;
        case OP_over:
            check_e_stack(2);
            sExprStack[sExprStackLen] = sExprStack[sExprStackLen - 2];
            sExprStackLen++;
            break;
        case OP_pick:
            {
                unsigned n = dio_ReadU1();
                check_e_stack(n + 1);
                sExprStack[sExprStackLen] = sExprStack[sExprStackLen - n - 1];
                sExprStackLen++;
            }
            break;
        case OP_swap:
            check_e_stack(2);
            Data = sExprStack[sExprStackLen - 1];
            sExprStack[sExprStackLen - 1] = sExprStack[sExprStackLen - 2];
            sExprStack[sExprStackLen - 2] = Data;
            break;
        case OP_rot:
            check_e_stack(3);
            Data = sExprStack[sExprStackLen - 1];
            sExprStack[sExprStackLen - 1] = sExprStack[sExprStackLen - 2];
            sExprStack[sExprStackLen - 2] = sExprStack[sExprStackLen - 3];
            sExprStack[sExprStackLen - 3] = Data;
            break;
        case OP_xderef:
            check_e_stack(2);
            {
                U1_T Tmp[8];
                ContextAddress Addr = (ContextAddress)sExprStack[sExprStackLen - 1];
                if (context_read_mem(sLocContext, Addr, Tmp, dio_gAddressSize) < 0) return -1;
                check_breakpoints_on_memory_read(sLocContext, Addr, Tmp, dio_gAddressSize);
                switch (dio_gAddressSize)  {
                case 1: Data = *Tmp; break;
                case 2: Data = *(U2_T *)Tmp; break;
                case 4: Data = *(U4_T *)Tmp; break;
                case 8: Data = *(U8_T *)Tmp; break;
                default: assert(0);
                }
                sExprStack[sExprStackLen - 2] = Data;
                sExprStackLen--;
            }            
            break;
        case OP_abs:
            check_e_stack(1);
            if ((I8_T)sExprStack[sExprStackLen - 1] < 0) {
                sExprStack[sExprStackLen - 1] = ~sExprStack[sExprStackLen - 1] + 1;
            }
            break;
        case OP_and:
            check_e_stack(2);
            sExprStackLen--;
            sExprStack[sExprStackLen - 1] = sExprStack[sExprStackLen - 1] & sExprStack[sExprStackLen];
            break;
        case OP_div:
            check_e_stack(2);
            sExprStackLen--;
            sExprStack[sExprStackLen - 1] /= sExprStack[sExprStackLen];
            break;
        case OP_minus:
            check_e_stack(2);
            sExprStackLen--;
            sExprStack[sExprStackLen - 1] -= sExprStack[sExprStackLen];
            break;
        case OP_mod:
            check_e_stack(2);
            sExprStackLen--;
            sExprStack[sExprStackLen - 1] %= sExprStack[sExprStackLen];
            break;
        case OP_mul:
            check_e_stack(2);
            sExprStackLen--;
            sExprStack[sExprStackLen - 1] *= sExprStack[sExprStackLen];
            break;
        case OP_neg:
            check_e_stack(1);
            sExprStack[sExprStackLen - 1] = ~sExprStack[sExprStackLen - 1] + 1;
            break;
        case OP_not:
            check_e_stack(1);
            sExprStack[sExprStackLen - 1] = ~sExprStack[sExprStackLen - 1];
            break;
        case OP_or:
            check_e_stack(2);
            sExprStackLen--;
            sExprStack[sExprStackLen - 1] = sExprStack[sExprStackLen - 1] | sExprStack[sExprStackLen];
            break;
        case OP_plus:
            check_e_stack(2);
            sExprStackLen--;
            sExprStack[sExprStackLen - 1] += sExprStack[sExprStackLen];
            break;
        case OP_plus_uconst:
            check_e_stack(1);
            sExprStack[sExprStackLen - 1] += dio_ReadU8LEB128();
            break;
        case OP_shl:
            check_e_stack(2);
            sExprStackLen--;
            sExprStack[sExprStackLen - 1] <<= sExprStack[sExprStackLen];
            break;
        case OP_shr:
            check_e_stack(2);
            sExprStackLen--;
            sExprStack[sExprStackLen - 1] >>= sExprStack[sExprStackLen];
            break;
        case OP_shra:
            {   
                U8_T Cnt;
                check_e_stack(2);
                Data = sExprStack[sExprStackLen - 2];
                Cnt = sExprStack[sExprStackLen - 1];
                while (Cnt > 0) {
                    int s = (Data & ((U8_T)1 << 63)) != 0;
                    Data >>= 1;
                    if (s) Data |= (U8_T)1 << 63;
                    Cnt--;
                }
                sExprStack[sExprStackLen - 2] = Data;
                sExprStackLen--;
            }
            break;
        case OP_xor:
            check_e_stack(2);
            sExprStackLen--;
            sExprStack[sExprStackLen - 1] = sExprStack[sExprStackLen - 1] ^ sExprStack[sExprStackLen];
            break;
        case OP_bra:
            {
                I2_T Offs = (I2_T)dio_ReadU2();
                check_e_stack(1);
                if (sExprStack[sExprStackLen - 1]) dio_Skip(Offs);
                sExprStackLen--;
            }
            break;
        case OP_eq:
            check_e_stack(2);
            sExprStackLen--;
            sExprStack[sExprStackLen - 1] = sExprStack[sExprStackLen - 1] == sExprStack[sExprStackLen];
            break;
        case OP_ge:
            check_e_stack(2);
            sExprStackLen--;
            sExprStack[sExprStackLen - 1] = sExprStack[sExprStackLen - 1] >= sExprStack[sExprStackLen];
            break;
        case OP_gt:
            check_e_stack(2);
            sExprStackLen--;
            sExprStack[sExprStackLen - 1] = sExprStack[sExprStackLen - 1] > sExprStack[sExprStackLen];
            break;
        case OP_le:
            check_e_stack(2);
            sExprStackLen--;
            sExprStack[sExprStackLen - 1] = sExprStack[sExprStackLen - 1] <= sExprStack[sExprStackLen];
            break;
        case OP_lt:
            check_e_stack(2);
            sExprStackLen--;
            sExprStack[sExprStackLen - 1] = sExprStack[sExprStackLen - 1] < sExprStack[sExprStackLen];
            break;
        case OP_ne:
            check_e_stack(2);
            sExprStackLen--;
            sExprStack[sExprStackLen - 1] = sExprStack[sExprStackLen - 1] != sExprStack[sExprStackLen];
            break;
        case OP_skip:
            dio_Skip((I2_T)dio_ReadU2());
            break;
        case OP_lit0:
        case OP_lit1:
        case OP_lit2:
        case OP_lit3:
        case OP_lit4:
        case OP_lit5:
        case OP_lit6:
        case OP_lit7:
        case OP_lit8:
        case OP_lit9:
        case OP_lit10:
        case OP_lit11:
        case OP_lit12:
        case OP_lit13:
        case OP_lit14:
        case OP_lit15:
        case OP_lit16:
        case OP_lit17:
        case OP_lit18:
        case OP_lit19:
        case OP_lit20:
        case OP_lit21:
        case OP_lit22:
        case OP_lit23:
        case OP_lit24:
        case OP_lit25:
        case OP_lit26:
        case OP_lit27:
        case OP_lit28:
        case OP_lit29:
        case OP_lit30:
        case OP_lit31:
            sExprStack[sExprStackLen++] = Op - OP_lit0;
            break;
        case OP_reg0:
        case OP_reg1:
        case OP_reg2:
        case OP_reg3:
        case OP_reg4:
        case OP_reg5:
        case OP_reg6:
        case OP_reg7:
        case OP_reg8:
        case OP_reg9:
        case OP_reg10:
        case OP_reg11:
        case OP_reg12:
        case OP_reg13:
        case OP_reg14:
        case OP_reg15:
        case OP_reg16:
        case OP_reg17:
        case OP_reg18:
        case OP_reg19:
        case OP_reg20:
        case OP_reg21:
        case OP_reg22:
        case OP_reg23:
        case OP_reg24:
        case OP_reg25:
        case OP_reg26:
        case OP_reg27:
        case OP_reg28:
        case OP_reg29:
        case OP_reg30:
        case OP_reg31:
            {
                unsigned n = Op - OP_reg0;
                if (sExprStackLen != 0 || dio_GetPos() < Size) {
                    errno = ERR_INV_DWARF;
                    return -1;
                }
                if (register_access_expression(n) < 0) return -1;
            }
            break;
        case OP_regx:
            {
                unsigned n = dio_ReadULEB128();
                if (sExprStackLen != 0 || dio_GetPos() < Size) {
                    errno = ERR_INV_DWARF;
                    return -1;
                }
                if (register_access_expression(n) < 0) return -1;
            }
            break;
        case OP_breg0:
        case OP_breg1:
        case OP_breg2:
        case OP_breg3:
        case OP_breg4:
        case OP_breg5:
        case OP_breg6:
        case OP_breg7:
        case OP_breg8:
        case OP_breg9:
        case OP_breg10:
        case OP_breg11:
        case OP_breg12:
        case OP_breg13:
        case OP_breg14:
        case OP_breg15:
        case OP_breg16:
        case OP_breg17:
        case OP_breg18:
        case OP_breg19:
        case OP_breg20:
        case OP_breg21:
        case OP_breg22:
        case OP_breg23:
        case OP_breg24:
        case OP_breg25:
        case OP_breg26:
        case OP_breg27:
        case OP_breg28:
        case OP_breg29:
        case OP_breg30:
        case OP_breg31:
            if (get_register(Op - OP_breg0, sExprStack + sExprStackLen) < 0) return -1;
            sExprStack[sExprStackLen++] += dio_ReadS8LEB128();
            break;
        case OP_fbreg:
            {
                U8_T Pos = dio_GetPos();
                ObjectInfo * Obj = sLocObject;
                int Mode = sMode;
                sMode = MD_FBASE;
                sLocObject = get_parent_function(sLocObject);
                if (sLocObject == NULL || sLocObject->mFrameBase.mAddr == NULL) {
                    errno = ERR_INV_DWARF;
                    return -1;
                }
                if (evaluate_location(&sLocObject->mFrameBase) < 0) return -1;
                sLocObject = Obj;
                sMode = Mode;
                enter_section(sLocObject->mCompUnit, Buf, Pos, Size);
                assert(sExprStackLen > 0);
                sExprStack[sExprStackLen - 1] += dio_ReadS8LEB128();
            }
            break;
        case OP_bregx:
            if (get_register(dio_ReadULEB128(), sExprStack + sExprStackLen) < 0) return -1;
            sExprStack[sExprStackLen++] += dio_ReadS8LEB128();
            break;
        case OP_piece:
        case OP_deref_size:
        case OP_xderef_size:
        case OP_nop:
        case OP_push_object_address:
        case OP_call2:
        case OP_call4:
        case OP_call_ref:
        case OP_bit_piece:
        case OP_reg:
        case OP_basereg:
        case OP_const:
        case OP_deref2:
        case OP_add:
        default:
            trace(LOG_ALWAYS, "Unsupported DWARF expression op 0x%02x", Op); 
            errno = ERR_UNSUPPORTED;
            return -1;
        }
    }
    dio_ExitSection();
    return 0;
}

static int evaluate(Context * Ctx, int Frame, ObjectInfo * Info) {
    Trap trap;
    int error = 0;

    sDataDone = 0;
    sExprStackLen = 0;
    sLocContext = Ctx;
    sLocFrame = Frame;
    sLocObject = Info;
    sLocIP = 0;
    sLocFP = 0;
    
    if (set_trap(&trap)) {
        if (evaluate_location(&Info->mLocation) < 0) error = errno;
        clear_trap(&trap);
    }
    else {
        error = trap.error;
    }

    switch (sMode) {
    case MD_ADDRESS:
        if (!error) {
            assert(sExprStackLen == 1);
            assert(*sExprStack != 0);
            assert(sDataDone == 0);
            assert(sDataBufSize == sizeof(U8_T));
            *(U8_T *)sDataBuf = *sExprStack;
        }
        break;
    case MD_READ:
        if (!error && !sDataDone) {
            ContextAddress Addr = 0;
            assert(sExprStackLen == 1);
            assert(*sExprStack != 0);
            Addr = (ContextAddress)*sExprStack;
            if (context_read_mem(sLocContext, Addr, sDataBuf, sDataBufSize) < 0) error = errno;
            if (error == 0) check_breakpoints_on_memory_read(sLocContext, Addr, sDataBuf, sDataBufSize);
        }
        break;
    case MD_WRITE:
        if (!error && !sDataDone) {
            ContextAddress Addr = 0;
            assert(sExprStackLen == 1);
            assert(*sExprStack != 0);
            Addr = (ContextAddress)*sExprStack;
            check_breakpoints_on_memory_write(sLocContext, Addr, sDataBuf, sDataBufSize);
            if (context_write_mem(sLocContext, Addr, sDataBuf, sDataBufSize) < 0) error = errno;
        }
        break;
    }
    
    sExprStackLen = 0;
    sLocContext = NULL;
    sLocFrame = 0;
    sLocObject = NULL;
    sLocIP = 0;
    sLocFP = 0;

    errno = error;
    return error ? -1 : 0;
}

U8_T dwarf_expression_addr(Context * Ctx, int Frame, ObjectInfo * Info) {
    U8_T Res = 0;

    if (Info->mLowPC != 0) return Info->mLowPC;

    sMode = MD_ADDRESS;
    sDataBuf = (U1_T *)&Res;
    sDataBufSize = sizeof(Res);

    if (evaluate(Ctx, Frame, Info) < 0) return 0;
    return Res;
}

int dwarf_expression_read(Context * Ctx, int Frame, ObjectInfo * Info, U1_T * Buf, size_t Size) {
    sMode = MD_READ;
    sDataBuf = Buf;
    sDataBufSize = Size;
    return evaluate(Ctx, Frame, Info);
}

int dwarf_expression_write(Context * Ctx, int Frame, ObjectInfo * Info, U1_T * Buf, size_t Size) {
    sMode = MD_WRITE;
    sDataBuf = Buf;
    sDataBufSize = Size;
    return evaluate(Ctx, Frame, Info);
}

#endif

Back to the top