-
Notifications
You must be signed in to change notification settings - Fork 4
/
loc2c.c
2601 lines (2232 loc) · 67.3 KB
/
loc2c.c
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-c translator
// Copyright (C) 2005-2013 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 <inttypes.h>
#include <stdbool.h>
#include <obstack.h>
#include <stdio.h>
#include <stdlib.h>
#include <error.h>
#include <dwarf.h>
#include <elfutils/libdw.h>
#include <elfutils/version.h>
#include <assert.h>
#include "loc2c.h"
#include "config.h"
#define N_(x) x
/* NB: PR10601 may make one suspect that intptr_t and uintptr_t aren't
right, for example on a 64-bit kernel targeting a 32-bit userspace
process. At least these types are always at least as wide as
userspace (since 64-bit userspace doesn't run on a 32-bit kernel).
So as long as deref() and {fetch,store}_register() widen/narrow
their underlying values to these, there should be no problem. */
#define STACK_TYPE "intptr_t" /* Must be the signed type. */
#define UTYPE "uintptr_t" /* Must be the unsigned type. */
#define SFORMAT "%" PRId64 "L"
#define UFORMAT "%" PRIu64 "UL"
#define AFORMAT "%#" PRIx64 "UL"
#define STACKFMT "s%u"
struct location_context
{
struct obstack *pool;
void (*fail) (void *arg, const char *fmt, ...)
__attribute__ ((noreturn, format (printf, 2, 3)));
void *fail_arg;
void (*emit_address) (void *fail_arg, struct obstack *, Dwarf_Addr);
Dwarf_Attribute *attr;
Dwarf_Addr dwbias;
Dwarf_Addr pc;
Dwarf_Attribute *fb_attr;
const Dwarf_Op *cfa_ops;
};
struct location
{
struct location *next;
struct location_context *context;
const Dwarf_Op *ops;
size_t nops;
Dwarf_Word byte_size;
enum
{
loc_address, loc_register, loc_noncontiguous, loc_unavailable,
loc_value, loc_constant, loc_implicit_pointer,
loc_decl, loc_fragment, loc_final
} type;
struct location *frame_base;
union
{
struct /* loc_address, loc_value, loc_fragment, loc_final */
{
const char *declare; /* Temporary that needs declared. */
char *program; /* C fragment, leaves address in s0. */
unsigned int stack_depth; /* Temporaries "s0..<N>" used by it. */
bool used_deref; /* Program uses "deref" macro. */
} address;
struct /* loc_register */
{
unsigned int regno;
Dwarf_Word offset;
} reg;
struct location *pieces; /* loc_noncontiguous */
const void *constant_block; /* loc_constant */
struct /* loc_implicit_pointer */
{
struct location *target;
Dwarf_Sword offset;
} pointer;
};
};
/* Select the C type to use in the emitted code to represent slots in the
DWARF expression stack. For really proper semantics this should be the
target address size. */
static const char *
stack_slot_type (struct location *context __attribute__ ((unused)), bool sign)
{
return sign ? STACK_TYPE : UTYPE;
}
static struct location *
alloc_location (struct location_context *ctx)
{
struct location *loc = obstack_alloc (ctx->pool, sizeof *loc);
loc->context = ctx;
loc->byte_size = 0;
loc->frame_base = NULL;
loc->ops = NULL;
loc->nops = 0;
return loc;
}
#define FAIL(loc, fmt, ...) \
(*(loc)->context->fail) ((loc)->context->fail_arg, fmt, ## __VA_ARGS__)
static void
default_emit_address (void *fail_arg __attribute__ ((unused)),
struct obstack *pool, Dwarf_Addr address)
{
obstack_printf (pool, AFORMAT, address);
}
static struct location_context *
new_context (struct obstack *pool,
void (*fail) (void *arg, const char *fmt, ...)
__attribute__ ((noreturn, format (printf, 2, 3))),
void *fail_arg,
void (*emit_address) (void *fail_arg,
struct obstack *, Dwarf_Addr),
Dwarf_Addr dwbias, Dwarf_Addr pc_address,
Dwarf_Attribute *attr, Dwarf_Attribute *fb_attr,
const Dwarf_Op *cfa_ops)
{
struct location_context *ctx = obstack_alloc (pool, sizeof *ctx);
ctx->pool = pool;
ctx->fail = fail;
ctx->fail_arg = fail_arg;
ctx->emit_address = emit_address ?: &default_emit_address;
ctx->attr = attr;
ctx->dwbias = dwbias;
ctx->pc = pc_address;
ctx->fb_attr = fb_attr;
ctx->cfa_ops = cfa_ops;
return ctx;
}
/* Translate a DW_AT_const_value attribute as if it were a location of
constant-value flavor. */
static struct location *
translate_constant (struct location_context *ctx, int indent,
Dwarf_Attribute *attr)
{
indent += 2;
struct location *loc = obstack_alloc (ctx->pool, sizeof *loc);
loc->context = ctx;
loc->next = NULL;
loc->byte_size = 0;
loc->frame_base = NULL;
loc->address.stack_depth = 0;
loc->address.declare = NULL;
loc->address.used_deref = false;
loc->ops = NULL;
loc->nops = 0;
switch (dwarf_whatform (attr))
{
case DW_FORM_addr:
{
Dwarf_Addr addr;
if (dwarf_formaddr (attr, &addr) != 0)
{
//TRANSLATORS: failure to find an address that was a constant literal number
FAIL (loc, N_("cannot get constant address: %s"),
dwarf_errmsg (-1));
return NULL;
}
loc->type = loc_value;
obstack_printf (ctx->pool, "%*saddr = ", indent * 2, "");
(*ctx->emit_address) (ctx->fail_arg, ctx->pool, ctx->dwbias + addr);
obstack_grow (ctx->pool, ";\n", 3);
loc->address.program = obstack_finish (ctx->pool);
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)
{
FAIL (loc, N_("cannot get constant block: %s"), dwarf_errmsg (-1));
return NULL;
}
loc->type = 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)
{
FAIL (loc, N_("cannot get string constant: %s"), dwarf_errmsg (-1));
return NULL;
}
loc->type = loc_constant;
loc->byte_size = strlen (string) + 1;
loc->constant_block = string;
break;
}
default:
{
Dwarf_Sword value;
if (dwarf_formsdata (attr, &value) != 0)
{
FAIL (loc, N_("cannot get constant value: %s"), dwarf_errmsg (-1));
return NULL;
}
loc->type = loc_value;
obstack_printf (ctx->pool, "%*saddr = (" UTYPE ")%#" PRIx64 "ULL;\n",
indent * 2, "", value);
obstack_1grow (ctx->pool, '\0');
loc->address.program = obstack_finish (ctx->pool);
break;
}
}
return loc;
}
struct location *
c_translate_constant (struct obstack *pool,
void (*fail) (void *arg,
const char *fmt, ...)
__attribute__ ((noreturn,
format (printf, 2, 3))),
void *fail_arg,
void (*emit_address) (void *fail_arg,
struct obstack *,
Dwarf_Addr),
int indent, Dwarf_Addr dwbias, Dwarf_Attribute *attr)
{
return translate_constant (new_context (pool, fail, fail_arg, emit_address,
dwbias, 0, attr, NULL, NULL),
indent, attr);
}
#if _ELFUTILS_PREREQ (0, 149)
static struct location *location_from_attr (struct location_context *ctx,
int indent, Dwarf_Attribute *attr);
#endif
/* Synthesize a new loc_address using the program on the obstack. */
static struct location *
new_synthetic_loc (struct location *origin, bool deref)
{
obstack_1grow (origin->context->pool, '\0');
char *program = obstack_finish (origin->context->pool);
struct location *loc = alloc_location (origin->context);
loc->next = NULL;
loc->byte_size = 0;
loc->type = loc_address;
loc->address.program = program;
loc->address.stack_depth = 0;
loc->address.declare = NULL;
loc->address.used_deref = deref;
if (origin->type == loc_register)
{
loc->ops = origin->ops;
loc->nops = origin->nops;
}
else
{
loc->ops = NULL;
loc->nops = 0;
}
return loc;
}
/* Die in the middle of an expression. */
static struct location *
lose (struct location *loc, const Dwarf_Op *lexpr, size_t len,
const char *failure, size_t i)
{
if (lexpr == NULL || i >= len)
FAIL (loc, "%s", failure);
else if (i < len)
FAIL (loc, N_("%s in DWARF expression [%Zu] at %" PRIu64
" (%#x: %" PRId64 ", %" PRId64 ")"),
failure, i, lexpr[i].offset,
lexpr[i].atom, lexpr[i].number, lexpr[i].number2);
return NULL;
}
/* Translate a (constrained) DWARF expression into C code
emitted to the obstack POOL. INDENT is the number of indentation levels.
ADDRBIAS is the difference between runtime and Dwarf info addresses.
INPUT is null or an expression to be initially pushed on the stack.
If NEED_FB is null, fail on DW_OP_fbreg, else set *NEED_FB to true
and emit "frame_base" for it. On success, set *MAX_STACK to the number
of stack slots required. On failure, set *LOSER to the index in EXPR
of the operation we could not handle.
Returns a failure message or null for success. */
static const char *
translate (struct location_context *ctx, int indent,
const Dwarf_Op *expr, const size_t len,
struct location *input, bool *need_fb, size_t *loser,
struct location *loc)
{
loc->ops = expr;
loc->nops = len;
#define DIE(msg) return (*loser = i, N_(msg))
#define emit(fmt, ...) obstack_printf (ctx->pool, fmt, ## __VA_ARGS__)
unsigned int stack_depth;
unsigned int max_stack = 0;
inline void deepen (void)
{
if (stack_depth == max_stack)
++max_stack;
}
#define POP(var) \
if (stack_depth > 0) \
--stack_depth; \
else if (tos_register != -1) \
fetch_tos_register (); \
else \
goto underflow; \
int var = stack_depth
#define PUSH (deepen (), stack_depth++)
#define STACK(idx) ({int z = (stack_depth - 1 - (idx)); assert (z >= 0); z;})
/* Don't put stack operations in the arguments to this. */
#define push(fmt, ...) \
emit ("%*s" STACKFMT " = " fmt ";\n", indent * 2, "", PUSH, ## __VA_ARGS__)
int tos_register;
inline void fetch_tos_register (void)
{
deepen ();
emit ("%*s" STACKFMT " = fetch_register (%d);\n",
indent * 2, "", stack_depth, tos_register);
tos_register = -1;
}
bool tos_value;
Dwarf_Block implicit_value;
bool used_deref;
const Dwarf_Op *implicit_pointer;
/* Initialize our state for handling each new piece. */
inline void reset ()
{
stack_depth = 0;
tos_register = -1;
tos_value = false;
implicit_value.data = NULL;
implicit_pointer = NULL;
used_deref = false;
if (input != NULL)
switch (input->type)
{
case loc_address:
push ("addr");
break;
case loc_value:
push ("addr");
tos_value = true;
break;
case loc_register:
tos_register = input->reg.regno;
break;
default:
abort ();
break;
}
}
size_t i;
inline const char *finish (struct location *piece)
{
if (piece->nops == 0)
{
assert (stack_depth == 0);
assert (tos_register == -1);
assert (obstack_object_size (ctx->pool) == 0);
piece->type = loc_unavailable;
}
else if (stack_depth >= 1)
{
/* The top of stack has our value.
Other stack slots left don't matter. */
obstack_1grow (ctx->pool, '\0');
char *program = obstack_finish (ctx->pool);
if (implicit_pointer != NULL)
{
piece->type = loc_implicit_pointer;
piece->pointer.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 (ctx->attr,
implicit_pointer,
&target) != 0)
DIE ("invalid implicit pointer");
switch (dwarf_whatattr (&target))
{
case DW_AT_const_value:
piece->pointer.target = translate_constant (ctx, indent,
&target);
break;
case DW_AT_location:
piece->pointer.target = location_from_attr (ctx, indent,
&target);
break;
default:
DIE ("unexpected implicit pointer attribute!");
break;
}
#endif
}
else if (implicit_value.data == NULL)
{
piece->type = tos_value ? loc_value : loc_address;
piece->address.declare = NULL;
piece->address.program = program;
piece->address.stack_depth = max_stack;
piece->address.used_deref = used_deref;
}
else
{
piece->type = loc_constant;
piece->byte_size = implicit_value.length;
piece->constant_block = implicit_value.data;
}
}
else if (tos_register == -1)
DIE ("stack underflow");
else if (obstack_object_size (ctx->pool) != 0)
DIE ("register value must stand alone in location expression");
else
{
piece->type = loc_register;
piece->reg.regno = tos_register;
piece->reg.offset = 0;
}
return NULL;
}
reset ();
struct location *pieces = NULL, **tailpiece = &pieces;
size_t piece_expr_start = 0;
Dwarf_Word piece_total_bytes = 0;
for (i = 0; i < len; ++i)
{
unsigned int reg;
uint_fast8_t sp;
Dwarf_Word value;
inline bool more_ops (void)
{
return (expr[i].atom != DW_OP_nop
&& expr[i].atom != DW_OP_piece
&& expr[i].atom != DW_OP_bit_piece);
}
if (tos_value && more_ops ())
DIE ("operations follow DW_OP_stack_value");
if (implicit_value.data != NULL && more_ops ())
DIE ("operations follow DW_OP_implicit_value");
if (implicit_pointer != NULL && more_ops ())
DIE ("operations follow DW_OP_GNU_implicit_pointer");
switch (expr[i].atom)
{
/* Basic stack operations. */
case DW_OP_nop:
break;
case DW_OP_dup:
if (stack_depth < 1)
goto underflow;
else
{
unsigned int tos = STACK (0);
push (STACKFMT, tos);
}
break;
case DW_OP_drop:
{
POP (ignore);
emit ("%*s/* drop " STACKFMT "*/\n", indent * 2, "", ignore);
break;
}
case DW_OP_pick:
sp = expr[i].number;
op_pick:
if (sp >= stack_depth)
goto underflow;
sp = STACK (sp);
push (STACKFMT, sp);
break;
case DW_OP_over:
sp = 1;
goto op_pick;
case DW_OP_swap:
if (stack_depth < 2)
goto underflow;
deepen (); /* Use a temporary slot. */
emit ("%*s"
STACKFMT " = " STACKFMT ", "
STACKFMT " = " STACKFMT ", "
STACKFMT " = " STACKFMT ";\n",
indent * 2, "",
STACK (-1), STACK (0),
STACK (0), STACK (1),
STACK (1), STACK (-1));
break;
case DW_OP_rot:
if (stack_depth < 3)
goto underflow;
deepen (); /* Use a temporary slot. */
emit ("%*s"
STACKFMT " = " STACKFMT ", "
STACKFMT " = " STACKFMT ", "
STACKFMT " = " STACKFMT ", "
STACKFMT " = " STACKFMT ";\n",
indent * 2, "",
STACK (-1), STACK (0),
STACK (0), STACK (1),
STACK (1), STACK (2),
STACK (2), STACK (-1));
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:
DIE ("conditional branches not supported");
break;
/* Memory access. */
case DW_OP_deref:
{
POP (addr);
push ("deref (sizeof (void *), " STACKFMT ")", addr);
used_deref = true;
}
break;
case DW_OP_deref_size:
{
POP (addr);
push ("deref (" UFORMAT ", " STACKFMT ")",
expr[i].number, addr);
used_deref = true;
}
break;
case DW_OP_xderef:
{
POP (addr);
POP (as);
push ("xderef (sizeof (void *), " STACKFMT ", " STACKFMT ")",
addr, as);
used_deref = true;
}
break;
case DW_OP_xderef_size:
{
POP (addr);
POP (as);
push ("xderef (" UFORMAT ", " STACKFMT ", " STACKFMT ")",
expr[i].number, addr, as);
used_deref = true;
}
break;
/* Constant-value operations. */
case DW_OP_addr:
emit ("%*s" STACKFMT " = ", indent * 2, "", PUSH);
(*ctx->emit_address) (ctx->fail_arg, ctx->pool,
ctx->dwbias + expr[i].number);
emit (";\n");
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:
push (SFORMAT, value);
break;
/* Arithmetic operations. */
#define UNOP(dw_op, c_op) \
case DW_OP_##dw_op: \
{ \
POP (tos); \
push ("%s (" STACKFMT ")", #c_op, tos); \
} \
break
#define BINOP(dw_op, c_op) \
case DW_OP_##dw_op: \
{ \
POP (b); \
POP (a); \
push (STACKFMT " %s " STACKFMT, a, #c_op, b); \
} \
break
UNOP (abs, op_abs);
BINOP (and, &);
BINOP (minus, -);
BINOP (mul, *);
UNOP (neg, -);
UNOP (not, ~);
BINOP (or, |);
BINOP (plus, +);
BINOP (shl, <<);
BINOP (shr, >>);
BINOP (xor, ^);
/* Comparisons are binary operators too. */
BINOP (le, <=);
BINOP (ge, >=);
BINOP (eq, ==);
BINOP (lt, <);
BINOP (gt, >);
BINOP (ne, !=);
#undef UNOP
#undef BINOP
case DW_OP_shra:
{
POP (b);
POP (a);
push ("(%s) " STACKFMT " >> (%s)" STACKFMT,
stack_slot_type (loc, true), a,
stack_slot_type (loc, true), b);
break;
}
case DW_OP_div:
{
POP (b);
POP (a);
push ("dwarf_div_op((%s) " STACKFMT ", (%s) " STACKFMT ")",
stack_slot_type (loc, true), a,
stack_slot_type (loc, true), b);
used_deref = true;
break;
}
case DW_OP_mod:
{
POP (b);
POP (a);
push ("dwarf_mod_op((%s) " STACKFMT ", (%s) " STACKFMT ")",
stack_slot_type (loc, false), a,
stack_slot_type (loc, false), b);
used_deref = true;
break;
}
case DW_OP_plus_uconst:
{
POP (x);
push (STACKFMT " + " UFORMAT, x, 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 ("fetch_register (%u) + " SFORMAT, reg, value);
break;
case DW_OP_fbreg:
if (need_fb == NULL)
DIE ("DW_OP_fbreg from DW_AT_frame_base");
*need_fb = true;
push ("frame_base + " SFORMAT, 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:
tos_register = reg;
break;
/* Special magic. */
case DW_OP_piece:
if (stack_depth > 1)
/* If this ever happens we could copy the program. */
DIE ("DW_OP_piece left multiple values on stack");
else
{
/* The obstack has a pending program for loc_address,
so we must finish that piece off before we can
allocate again. */
struct location temp_piece =
{
.context = loc->context,
.frame_base = NULL,
.ops = &expr[piece_expr_start],
.nops = i - piece_expr_start,
};
const char *failure = finish (&temp_piece);
if (failure != NULL)
return failure;
struct location *piece = obstack_alloc (ctx->pool, sizeof *piece);
*piece = temp_piece;
piece_expr_start = i + 1;
piece_total_bytes += piece->byte_size = expr[i].number;
*tailpiece = piece;
tailpiece = &piece->next;
piece->next = NULL;
/* Reset default conditions for handling the next piece. */
reset ();
}
break;
case DW_OP_stack_value:
if (stack_depth > 1)
DIE ("DW_OP_stack_value left multiple values on stack");
else
{
/* Fetch a register to top of stack, or check for underflow.
Then mark the TOS as being a value. */
POP (tos);
assert (tos == 0);
PUSH;
tos_value = true;
}
break;
case DW_OP_implicit_value:
if (ctx->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_depth != 0)
DIE ("DW_OP_implicit_value follows stack operations");
#if _ELFUTILS_PREREQ (0, 143)
if (dwarf_getlocation_implicit_value (ctx->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;
break;
#endif
DIE ("DW_OP_implicit_value not supported");
break;
#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;
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 (need_fb == NULL)
DIE ("DW_OP_call_frame_cfa while processing frame base");
else
DIE ("DW_OP_call_frame_cfa not expected outside DW_AT_frame_base");
break;
case DW_OP_push_object_address:
DIE ("XXX DW_OP_push_object_address");
break;
default:
DIE ("unrecognized operation");
break;
}
}
if (pieces == NULL)
return finish (loc);
if (piece_expr_start != i)
DIE ("extra operations after last DW_OP_piece");
loc->type = loc_noncontiguous;
loc->pieces = pieces;
loc->byte_size = piece_total_bytes;
return NULL;
underflow:
DIE ("stack underflow");
#undef emit
#undef push
#undef PUSH
#undef POP
#undef STACK
#undef DIE
}
/* Translate a location starting from an address or nothing. */
static struct location *
location_from_address (struct location_context *ctx, int indent,
const Dwarf_Op *expr, size_t len,
struct location **input)
{
struct location *loc = obstack_alloc (ctx->pool, sizeof *loc);
loc->context = ctx;
loc->byte_size = 0;
loc->frame_base = NULL;
loc->ops = NULL;
loc->nops = 0;
bool need_fb = false;
size_t loser;
const char *failure = translate (ctx, indent + 1, expr, len,
*input, &need_fb, &loser, loc);
if (failure != NULL)
return lose (loc, expr, len, failure, loser);
loc->next = NULL;
if (need_fb)
{
/* The main expression uses DW_OP_fbreg, so we need to compute
the DW_AT_frame_base attribute expression's value first. */
if (ctx->fb_attr == NULL)
FAIL (loc, N_("required DW_AT_frame_base attribute not supplied"));
Dwarf_Op *fb_expr;
size_t fb_len;
switch (dwarf_getlocation_addr (ctx->fb_attr, ctx->pc,
&fb_expr, &fb_len, 1))
{
case 1: /* Should always happen. */
if (fb_len == 0)
goto fb_inaccessible;
break;
default: /* Shouldn't happen. */
case -1:
FAIL (loc, N_("dwarf_getlocation_addr (form %#x): %s"),
dwarf_whatform (ctx->fb_attr), dwarf_errmsg (-1));
return NULL;
case 0: /* Shouldn't happen. */
fb_inaccessible:
FAIL (loc, N_("DW_AT_frame_base not accessible at this address"));
return NULL;
}
// If it is DW_OP_call_frame_cfa then get cfi cfa ops.
const Dwarf_Op * fb_ops;
if (fb_len == 1 && fb_expr[0].atom == DW_OP_call_frame_cfa)
{
if (ctx->cfa_ops == NULL)
FAIL (loc, N_("No cfa_ops supplied, but needed by DW_OP_call_frame_cfa"));
fb_ops = ctx->cfa_ops;
}
else
fb_ops = fb_expr;
loc->frame_base = alloc_location (ctx);
failure = translate (ctx, indent + 1, fb_ops, fb_len, NULL,
NULL, &loser, loc->frame_base);
if (failure != NULL)
return lose (loc, fb_ops, fb_len, failure, loser);
}
if (*input != NULL)
(*input)->next = loc;
*input = loc;
return loc;
}
#if _ELFUTILS_PREREQ (0, 149)
static struct location *
location_from_attr (struct location_context *ctx, int indent,
Dwarf_Attribute *attr)
{
Dwarf_Op *expr;
size_t len;
switch (dwarf_getlocation_addr (attr, ctx->pc, &expr, &len, 1))
{
case 1: /* Should always happen. */
if (len > 0)
break;
/* Fall through. */
case 0: /* Shouldn't happen. */
(*ctx->fail) (ctx->fail_arg, N_("not accessible at this address (%#" PRIx64 ")"), ctx->pc);
return NULL;
default: /* Shouldn't happen. */
case -1:
(*ctx->fail) (ctx->fail_arg, "dwarf_getlocation_addr: %s",
dwarf_errmsg (-1));
return NULL;
}
struct location *input = NULL;
return location_from_address (ctx, indent, expr, len, &input);
}
#endif
static struct location *
translate_offset (int indent, const Dwarf_Op *expr, size_t len, size_t i,