-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloc2stap.cxx
1573 lines (1364 loc) · 38 KB
/
loc2stap.cxx
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
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// dwarf location-list-to-staptree translator
// Copyright (C) 2016 Red Hat Inc.
//
// This file is part of systemtap, and is free software. You can
// redistribute it and/or modify it under the terms of the GNU General
// Public License (GPL); either version 2, or (at your option) any
// later version.
#include "config.h"
#include <cinttypes>
#include <cassert>
#include <cstdlib>
#include <dwarf.h>
#include <elfutils/libdw.h>
#include <elfutils/version.h>
#include "loc2stap.h"
#include "dwflpp.h"
#if ! _ELFUTILS_PREREQ(0, 153)
#define DW_OP_GNU_entry_value 0xf3
#endif
#define N_(x) x
location_context::location_context(target_symbol *ee, expression *pp)
: e_orig(ee), e(deep_copy_visitor::deep_copy(ee)), pointer(0), value(0),
attr(0), dwbias(0), pc(0), fb_attr(0), cfa_ops(0),
dw(0), frame_base(0)
{
// If this code snippet uses a precomputed pointer, create an
// parameter variable to hold it.
if (pp)
{
vardecl *v = new vardecl;
v->type = pe_long;
v->name = "pointer";
v->tok = ee->tok;
this->pointer = v;
}
// Any non-literal indexes need to have parameter variables too.
for (unsigned i = 0; i < ee->components.size(); ++i)
if (ee->components[i].type == target_symbol::comp_expression_array_index)
{
vardecl *v = new vardecl;
v->type = pe_long;
v->name = "index" + lex_cast(i);
v->tok = e->tok;
this->indicies.push_back(v);
// substitute the [$exprN]->indexN in the target_symbol* copy,
// which will be used for expansion to kderef() etc.
symbol *s = new symbol;
s->type = pe_long;
s->name = v->name;
s->tok = v->tok;
e->components[i].expr_index = s;
}
}
expression *
location_context::translate_address(Dwarf_Addr addr)
{
if (dw == NULL)
return new literal_number(addr);
int n = dwfl_module_relocations (dw->module);
Dwarf_Addr reloc_addr = addr;
const char *secname = "";
if (n > 1)
{
int i = dwfl_module_relocate_address (dw->module, &reloc_addr);
secname = dwfl_module_relocation_info (dw->module, i, NULL);
}
if (n > 0 && !(n == 1 && secname == NULL))
{
std::string c;
if (n > 1 || secname[0] != 0)
{
// This gives us the module name and section name within the
// module, for a kernel module.
c = "({ unsigned long addr = 0; "
"addr = _stp_kmodule_relocate (\""
+ dw->module_name + "\", \"" + secname + "\", "
+ lex_cast_hex (reloc_addr)
+ "); addr; })";
}
else if (n == 1 && dw->module_name == "kernel" && secname[0] == 0)
{
// elfutils way of telling us that this is a relocatable kernel
// address, which we need to treat the same way here as
// dwarf_query::add_probe_point does.
c = "({ unsigned long addr = 0; "
"addr = _stp_kmodule_relocate (\"kernel\", \"_stext\", "
+ lex_cast_hex (addr - dw->sess.sym_stext)
+ "); addr; })";
}
else
{
c = "/* pragma:vma */ "
"({ unsigned long addr = 0; "
"addr = _stp_umodule_relocate (\""
+ resolve_path(dw->module_name.c_str()) + "\", "
+ lex_cast_hex (addr)
+ ", current); addr; })";
}
embedded_expr *r = new embedded_expr;
r->tok = e->tok;
r->code = c;
return r;
}
else
return new literal_number(addr);
}
location *
location_context::translate_constant(Dwarf_Attribute *attr)
{
location *loc;
switch (dwarf_whatform (attr))
{
case DW_FORM_addr:
{
Dwarf_Addr addr;
if (dwarf_formaddr (attr, &addr) != 0)
throw SEMANTIC_ERROR(std::string("cannot get constant address: ")
+ dwarf_errmsg (-1));
loc = new_location(loc_value);
loc->program = translate_address(this->dwbias + addr);
}
break;
case DW_FORM_block:
case DW_FORM_block1:
case DW_FORM_block2:
case DW_FORM_block4:
{
Dwarf_Block block;
if (dwarf_formblock (attr, &block) != 0)
throw SEMANTIC_ERROR(std::string("cannot get constant block :")
+ dwarf_errmsg (-1));
loc = new_location(loc_constant);
loc->byte_size = block.length;
loc->constant_block = block.data;
}
break;
case DW_FORM_string:
case DW_FORM_strp:
{
const char *string = dwarf_formstring (attr);
if (string == NULL)
throw SEMANTIC_ERROR(std::string("cannot get constant string:")
+ dwarf_errmsg (-1));
loc = new_location(loc_constant);
loc->byte_size = strlen(string) + 1;
loc->constant_block = string;
}
break;
default:
{
Dwarf_Sword value;
if (dwarf_formsdata (attr, &value) != 0)
throw SEMANTIC_ERROR (std::string("cannot get constant value: ")
+ dwarf_errmsg (-1));
loc = new_location(loc_value);
loc->program = new literal_number(this->dwbias + value);
}
break;
}
return loc;
}
/* Die in the middle of an expression. */
static void __attribute__((noreturn))
lose (const Dwarf_Op *lexpr, size_t len, const char *failure, size_t i)
{
if (lexpr == NULL || i >= len)
throw SEMANTIC_ERROR(failure);
else
throw SEMANTIC_ERROR(std::string(failure)
+ " in DWARF expression ["
+ lex_cast(i)
+ "] at " + lex_cast(lexpr[i].offset)
+ " (" + lex_cast(lexpr[i].atom)
+ ": " + lex_cast(lexpr[i].number)
+ ", " + lex_cast(lexpr[i].number2) + ")");
}
location *
location_context::new_location(location_type t)
{
location *n = new location(t);
this->locations.push_back(n);
return n;
}
location *
location_context::new_location(const location &o)
{
location *n = new location(o);
this->locations.push_back(n);
return n;
}
symbol *
location_context::new_symref(vardecl *var)
{
symbol *sym = new symbol;
sym->name = var->name;
// sym->referent = var;
return sym;
}
symbol *
location_context::new_local(const char *namebase)
{
static int counter;
vardecl *var = new vardecl;
var->name = std::string(namebase) + lex_cast(counter++);
var->type = pe_long;
var->arity = 0;
var->synthetic = true;
this->locals.push_back(var);
return new_symref(var);
}
expression *
location_context::new_target_reg(unsigned int regno)
{
target_register *reg = new target_register;
reg->tok = e->tok;
reg->regno = regno;
reg->userspace_p = this->userspace_p;
return reg;
}
expression *
location_context::new_plus_const(expression *l, int64_t r)
{
if (r == 0)
return l;
binary_expression *val = new binary_expression;
val->tok = e->tok;
val->op = "+";
val->left = l;
val->right = new literal_number(r);
return val;
}
// If VAL is cheaply copied, return it.
// Otherwise compute the expression to a temp.
expression *
location_context::save_expression(expression *val)
{
if (dynamic_cast<literal_number *>(val) || dynamic_cast<symbol *>(val))
return val;
symbol *sym = new_local("_tmp_");
assignment *set = new assignment;
set->tok = e->tok;
set->op = "=";
set->left = sym;
set->right = val;
this->evals.push_back(set);
return sym;
}
/* Translate a (constrained) DWARF expression into STAP trees.
If NEED_FB is null, fail on DW_OP_fbreg, else set *NEED_FB to the
variable that should be initialized with frame_base. */
location *
location_context::translate (const Dwarf_Op *expr, const size_t len,
const size_t piece_expr_start,
location *input, bool may_use_fb,
bool computing_value_orig)
{
#define DIE(msg) lose(expr, len, N_(msg), i)
#define POP(VAR) if (stack.empty()) goto underflow; \
expression *VAR = stack.back(); \
stack.pop_back(); \
tos_register = false;
#define PUSH(VAL) stack.push_back(VAL); \
tos_register = false;
bool saw_stack_value = false;
bool tos_register = false;
bool computing_value = computing_value_orig;
Dwarf_Word piece_size = 0;
Dwarf_Block implicit_value = Dwarf_Block();
const Dwarf_Op *implicit_pointer = NULL;
location temp_piece;
size_t i;
{
std::vector<expression *> stack;
if (input != NULL)
switch (input->type)
{
case loc_value:
assert(computing_value);
/* FALLTHRU */
case loc_address:
PUSH(input->program);
break;
case loc_register:
assert(computing_value);
PUSH(new_target_reg(input->regno));
tos_register = true;
break;
default:
abort();
}
for (i = piece_expr_start; i < len; ++i)
{
unsigned int reg;
uint_fast8_t sp;
Dwarf_Word value;
if (expr[i].atom != DW_OP_nop
&& expr[i].atom != DW_OP_piece
&& expr[i].atom != DW_OP_bit_piece)
{
if (saw_stack_value)
DIE("operations follow DW_OP_stack_value");
if (implicit_value.data != NULL)
DIE ("operations follow DW_OP_implicit_value");
if (implicit_pointer != NULL)
DIE ("operations follow DW_OP_GNU_implicit_pointer");
}
switch (expr[i].atom)
{
/* Basic stack operations. */
case DW_OP_nop:
break;
case DW_OP_drop:
{
POP(unused);
(void)unused;
}
break;
case DW_OP_dup:
sp = 0;
goto op_pick;
break;
case DW_OP_over:
sp = 1;
goto op_pick;
case DW_OP_pick:
sp = expr[i].number;
op_pick:
{
size_t stack_size = stack.size();
if (sp >= stack_size)
goto underflow;
expression *val = stack[stack_size - 1 - sp];
PUSH(save_expression(val));
}
break;
case DW_OP_swap:
{
POP(a);
POP(b);
PUSH(a);
PUSH(b);
}
break;
case DW_OP_rot:
{
POP(a);
POP(b);
POP(c);
PUSH(a);
PUSH(c);
PUSH(b);
}
break;
/* Control flow operations. */
case DW_OP_skip:
{
Dwarf_Off target = expr[i].offset + 3 + expr[i].number;
while (i + 1 < len && expr[i + 1].offset < target)
++i;
if (expr[i + 1].offset != target)
DIE ("invalid skip target");
break;
}
case DW_OP_bra:
// ??? Could copy the stack, recurse, and place both
// traces in arms of a ternary_expression. No point
// in doing that if this is never used, however.
DIE ("conditional branches not supported");
break;
/* Memory access. */
case DW_OP_deref:
// ??? Target sizeof, not host sizeof.
sp = sizeof(void *);
goto op_deref;
case DW_OP_deref_size:
sp = expr[i].number;
op_deref:
{
POP(addr);
target_deref *val = new target_deref;
val->addr = addr;
val->size = sp;
val->userspace_p = this->userspace_p;
val->tok = e->tok;
PUSH(val);
}
break;
case DW_OP_xderef:
case DW_OP_xderef_size:
// not yet
// POP (addr);
// POP (as);
// push (xderef expr[i].number, (unsigned)addr, (unsigned)as);
DIE ("address spaces not supported");
/* Constant-value operations. */
case DW_OP_addr:
PUSH(translate_address(this->dwbias + expr[i].number));
break;
case DW_OP_lit0 ... DW_OP_lit31:
value = expr[i].atom - DW_OP_lit0;
goto op_const;
case DW_OP_const1u:
case DW_OP_const1s:
case DW_OP_const2u:
case DW_OP_const2s:
case DW_OP_const4u:
case DW_OP_const4s:
case DW_OP_const8u:
case DW_OP_const8s:
case DW_OP_constu:
case DW_OP_consts:
value = expr[i].number;
op_const:
{
literal_number *ln = new literal_number(value);
ln->tok = e->tok;
PUSH(ln);
}
break;
/* Arithmetic operations. */
#define UNOP(dw_op, stap_op) \
case DW_OP_##dw_op: \
{ \
POP(arg); \
unary_expression *val = new unary_expression; \
val->tok = e->tok; \
val->op = #stap_op; \
val->operand = arg; \
PUSH(val); \
} \
break
UNOP (neg, -);
UNOP (not, ~);
#undef UNOP
#define BINOP(dw_op, type, stap_op) \
case DW_OP_##dw_op: \
{ \
POP(b); \
POP(a); \
type *val = new type; \
val->op = #stap_op; \
val->left = a; \
val->right = b; \
val->tok = e->tok; \
PUSH(val); \
} \
break
BINOP (and, binary_expression, &);
BINOP (minus, binary_expression, -);
BINOP (mul, binary_expression, *);
BINOP (or, binary_expression, |);
BINOP (plus, binary_expression, +);
BINOP (shl, binary_expression, <<);
BINOP (shr, binary_expression, >>>);
BINOP (shra, binary_expression, >>);
BINOP (xor, binary_expression, ^);
BINOP (div, binary_expression, /);
BINOP (mod, binary_expression, %);
/* Comparisons are binary operators too. */
BINOP (le, comparison, <=);
BINOP (ge, comparison, >=);
BINOP (eq, comparison, ==);
BINOP (lt, comparison, <);
BINOP (gt, comparison, >);
BINOP (ne, comparison, !=);
#undef BINOP
case DW_OP_abs:
{
// Form (arg < 0 ? -arg : arg).
POP(arg);
comparison *cmp = new comparison;
cmp->op = "<";
cmp->left = arg;
cmp->right = new literal_number(0);
unary_expression *neg = new unary_expression;
neg->op = "-";
neg->operand = arg;
ternary_expression *val = new ternary_expression;
val->cond = cmp;
val->truevalue = neg;
val->falsevalue = arg;
PUSH(val);
}
break;
case DW_OP_plus_uconst:
{
POP(arg);
PUSH(new_plus_const(arg, expr[i].number));
}
break;
/* Register-relative addressing. */
case DW_OP_breg0 ... DW_OP_breg31:
reg = expr[i].atom - DW_OP_breg0;
value = expr[i].number;
goto op_breg;
case DW_OP_bregx:
reg = expr[i].number;
value = expr[i].number2;
op_breg:
PUSH(new_plus_const(new_target_reg(reg), value));
break;
case DW_OP_fbreg:
if (!may_use_fb)
DIE ("DW_OP_fbreg from DW_AT_frame_base");
PUSH(new_plus_const(frame_location(), expr[i].number));
break;
/* Direct register contents. */
case DW_OP_reg0 ... DW_OP_reg31:
reg = expr[i].atom - DW_OP_reg0;
goto op_reg;
case DW_OP_regx:
reg = expr[i].number;
op_reg:
PUSH(new_target_reg(reg));
tos_register = true;
break;
/* Special magic. */
case DW_OP_piece:
if (stack.size() > 1)
// If this ever happens we could copy the program.
DIE ("DW_OP_piece left multiple values on stack");
piece_size = expr[i].number;
goto end_piece;
case DW_OP_stack_value:
if (stack.empty())
goto underflow;
if (stack.size() > 1)
DIE ("DW_OP_stack_value left multiple values on stack");
saw_stack_value = true;
computing_value = true;
break;
case DW_OP_implicit_value:
if (this->attr == NULL)
DIE ("DW_OP_implicit_value used in invalid context"
" (no DWARF attribute, ABI return value location?)");
/* It's supposed to appear by itself, except for DW_OP_piece. */
if (!stack.empty())
DIE ("DW_OP_implicit_value follows stack operations");
#if _ELFUTILS_PREREQ (0, 143)
if (dwarf_getlocation_implicit_value (this->attr,
(Dwarf_Op *) &expr[i],
&implicit_value) != 0)
DIE ("dwarf_getlocation_implicit_value failed");
/* Fake top of stack: implicit_value being set marks it. */
PUSH(NULL);
break;
#else
DIE ("DW_OP_implicit_value not supported");
break;
#endif
#if _ELFUTILS_PREREQ (0, 149)
case DW_OP_GNU_implicit_pointer:
implicit_pointer = &expr[i];
/* Fake top of stack: implicit_pointer being set marks it. */
PUSH(NULL);
break;
#endif
case DW_OP_call_frame_cfa:
// We pick this out when processing DW_AT_frame_base in
// so it really shouldn't turn up here.
if (!may_use_fb)
DIE ("DW_OP_call_frame_cfa while processing frame base");
// This is slightly weird/inefficient, but golang is known
// to produce DW_OP_call_frame_cfa; DW_OP_consts: 8; DW_OP_plus
// instead of a simple DW_OP_fbreg 8.
PUSH(frame_location());
break;
case DW_OP_push_object_address:
DIE ("unhandled DW_OP_push_object_address");
break;
case DW_OP_GNU_entry_value:
DIE ("unhandled DW_OP_GNU_entry_value");
break;
default:
DIE ("unhandled DW_OP operation");
break;
}
}
end_piece:
// Finish recognizing exceptions before allocating memory.
if (i == piece_expr_start)
{
assert(stack.empty());
assert(!tos_register);
temp_piece.type = loc_unavailable;
}
else if (stack.empty())
goto underflow;
else if (implicit_pointer != NULL)
{
temp_piece.type = loc_implicit_pointer;
temp_piece.offset = implicit_pointer->number2;
#if !_ELFUTILS_PREREQ (0, 149)
/* Then how did we get here? */
abort ();
#else
Dwarf_Attribute target;
if (dwarf_getlocation_implicit_pointer (this->attr, implicit_pointer,
&target) != 0)
DIE ("invalid implicit pointer");
switch (dwarf_whatattr (&target))
{
case DW_AT_const_value:
temp_piece.target = translate_constant(&target);
break;
case DW_AT_location:
{
Dwarf_Op *expr;
size_t len;
switch (dwarf_getlocation_addr(&target, pc, &expr, &len, 1))
{
case 1: /* Should always happen */
if (len > 0)
break;
/* fallthru */
case 0: /* Should never happen */
throw SEMANTIC_ERROR("not accessible at this address: "
+ lex_cast(pc));
default: /* Should never happen */
throw SEMANTIC_ERROR(std::string("dwarf_getlocation_addr: ")
+ dwarf_errmsg(-1));
}
temp_piece.target = location_from_address(expr, len, NULL);
}
break;
default:
DIE ("unexpected implicit pointer attribute!");
}
#endif
}
else if (implicit_value.data != NULL)
{
temp_piece.type = loc_constant;
temp_piece.byte_size = implicit_value.length;
temp_piece.constant_block = implicit_value.data;
}
else
{
expression *val = stack.back();
if (computing_value || tos_register)
{
if (target_register *reg = dynamic_cast<target_register *>(val))
{
temp_piece.type = loc_register;
temp_piece.regno = reg->regno;
}
else
temp_piece.type = loc_value;
}
else
temp_piece.type = loc_address;
temp_piece.program = val;
}
// stack goes out of scope here
}
if (piece_size != 0)
{
temp_piece.byte_size = piece_size;
temp_piece.piece_next = translate(expr, len, i + 1, input, may_use_fb,
computing_value_orig);
// For the first DW_OP_piece, create the loc_noncontiguous container.
// For subsequent pieces, fall through for normal location return.
if (piece_expr_start == 0)
{
location *pieces = new_location(temp_piece);
location *loc = new_location(loc_noncontiguous);
loc->pieces = pieces;
size_t total_size = 0;
for (location *p = pieces; p != NULL; p = p->piece_next)
total_size += p->byte_size;
loc->byte_size = total_size;
return loc;
}
}
else if (piece_expr_start && piece_expr_start != i)
DIE ("extra operations after last DW_OP_piece");
return new_location(temp_piece);
underflow:
DIE ("stack underflow");
#undef DIE
#undef PUSH
#undef POP
}
symbol *
location_context::frame_location()
{
if (this->frame_base == NULL)
{
// The main expression uses DW_OP_fbreg, so we need to compute
// the DW_AT_frame_base attribute expression's value first.
const Dwarf_Op *fb_ops;
Dwarf_Op *fb_expr;
size_t fb_len;
if (this->fb_attr == NULL)
{
// Lets just assume we want DW_OP_call_frame_cfa.
// Some (buggy golang) DWARF producers use that directly in
// location descriptions. And at least we should have a chance
// to get an actual call frame address that way.
goto use_cfa_ops;
}
else
{
switch (dwarf_getlocation_addr (this->fb_attr, this->pc,
&fb_expr, &fb_len, 1))
{
case 1: /* Should always happen. */
if (fb_len != 0)
break;
/* fallthru */
case 0: /* Shouldn't happen. */
throw SEMANTIC_ERROR("DW_AT_frame_base not accessible "
"at this address");
default: /* Shouldn't happen. */
case -1:
throw SEMANTIC_ERROR
("dwarf_getlocation_addr (form "
+ lex_cast(dwarf_whatform (this->fb_attr))
+ "): " + dwarf_errmsg (-1));
}
}
// If it is DW_OP_call_frame_cfa then get cfi cfa ops.
if (fb_len == 1 && fb_expr[0].atom == DW_OP_call_frame_cfa)
{
use_cfa_ops:
if (this->cfa_ops == NULL)
throw SEMANTIC_ERROR("No cfa_ops supplied, "
"but needed by DW_OP_call_frame_cfa");
fb_ops = this->cfa_ops;
}
else
fb_ops = fb_expr;
location *fb_loc = translate (fb_ops, fb_len, 0, NULL, false, false);
assert(fb_loc->type == loc_address);
this->frame_base = new_local("_fb_");
assignment *set = new assignment;
set->op = "=";
set->left = this->frame_base;
set->right = fb_loc->program;
this->evals.push_back(set);
}
return this->frame_base;
}
/* Translate a location starting from an address or nothing. */
location *
location_context::location_from_address (const Dwarf_Op *expr, size_t len,
struct location *input)
{
return translate (expr, len, 0, input, true, false);
}
location *
location_context::translate_offset (const Dwarf_Op *expr, size_t len,
size_t i, location *input,
Dwarf_Word offset)
{
#define DIE(msg) lose (expr, len, N_(msg), i)
while (input->type == loc_noncontiguous)
{
/* We are starting from a noncontiguous object (DW_OP_piece).
Find the piece we want. */
struct location *piece = input->pieces;
while (piece != NULL && offset >= piece->byte_size)
{
offset -= piece->byte_size;
piece = piece->piece_next;
}
if (piece == NULL)
DIE ("offset outside available pieces");
input = piece;
}
location *loc = new_location(*input);
switch (loc->type)
{
case loc_address:
/* The piece we want is actually in memory. Use the same
program to compute the address from the preceding input. */
loc->program = new_plus_const(loc->program, offset);
break;
case loc_register:
case loc_implicit_pointer:
loc->offset += offset;
break;
case loc_constant:
/* This piece has a constant offset. */
if (offset >= loc->byte_size)
DIE ("offset outside available constant block");
loc->constant_block = (void *)((char *)loc->constant_block + offset);
loc->byte_size -= offset;
break;
case loc_unavailable:
/* Let it be diagnosed later. */
break;
case loc_value:
/* The piece we want is part of a computed offset.
If it's the whole thing, we are done. */
if (offset == 0)
break;
DIE ("extract partial rematerialized value");
default:
abort ();
}
return loc;
#undef DIE
}
/* Translate a location starting from a non-address "on the top of the
stack". The *INPUT location is a register name or noncontiguous
object specification, and this expression wants to find the "address"
of an object (or the actual value) relative to that "address". */
location *
location_context::location_relative (const Dwarf_Op *expr, size_t len,
location *input)
{
#define DIE(msg) lose(expr, len, N_(msg), i)
#define POP(VAR) if (stack.empty()) \
goto underflow; \
Dwarf_Sword VAR = stack.back(); \
stack.pop_back()
#define PUSH(VAL) stack.push_back(VAL)
std::vector<Dwarf_Sword> stack;
size_t i;
for (i = 0; i < len; ++i)
{
uint_fast8_t sp;
Dwarf_Word value;
switch (expr[i].atom)
{
/* Basic stack operations. */
case DW_OP_nop:
break;
case DW_OP_drop:
if (stack.empty())
{
if (input == NULL)
goto underflow;
/* Mark that we have consumed the input. */
input = NULL;
}
else
stack.pop_back();
break;
case DW_OP_dup:
sp = 0;
goto op_pick;
case DW_OP_over:
sp = 1;
goto op_pick;
case DW_OP_pick:
sp = expr[i].number;
op_pick:
{
size_t stack_size = stack.size();
if (sp < stack_size)
PUSH(stack[stack_size - 1 - sp]);
else if (sp == stack_size)
goto underflow;
else
goto real_underflow;
}
break;
case DW_OP_swap:
{
POP(a);