]> sourceware.org Git - systemtap.git/blob - loc2c.c
PR13313 cont'd, fix regexp for [+-]D[+-]D[+-]D(...) operand case
[systemtap.git] / loc2c.c
1 #include <config.h>
2 #include <inttypes.h>
3 #include <stdbool.h>
4 #include <obstack.h>
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <error.h>
8 #include <dwarf.h>
9 #include <elfutils/libdw.h>
10 #include <elfutils/version.h>
11
12 #include <assert.h>
13 #include "loc2c.h"
14
15 #include "config.h"
16
17
18 #define N_(x) x
19
20 /* NB: PR10601 may make one suspect that intptr_t and uintptr_t aren't
21 right, for example on a 64-bit kernel targeting a 32-bit userspace
22 process. At least these types are always at least as wide as
23 userspace (since 64-bit userspace doesn't run on a 32-bit kernel).
24 So as long as deref() and {fetch,store}_register() widen/narrow
25 their underlying values to these, there should be no problem. */
26
27 #define STACK_TYPE "intptr_t" /* Must be the signed type. */
28 #define UTYPE "uintptr_t" /* Must be the unsigned type. */
29 #define SFORMAT "%" PRId64 "L"
30 #define UFORMAT "%" PRIu64 "UL"
31 #define AFORMAT "%#" PRIx64 "UL"
32 #define STACKFMT "s%u"
33
34 struct location_context
35 {
36 struct obstack *pool;
37
38 void (*fail) (void *arg, const char *fmt, ...)
39 __attribute__ ((noreturn, format (printf, 2, 3)));
40 void *fail_arg;
41 void (*emit_address) (void *fail_arg, struct obstack *, Dwarf_Addr);
42
43 Dwarf_Attribute *attr;
44 Dwarf_Addr dwbias;
45 Dwarf_Addr pc;
46 Dwarf_Attribute *fb_attr;
47 const Dwarf_Op *cfa_ops;
48 };
49
50 struct location
51 {
52 struct location *next;
53
54 struct location_context *context;
55
56 const Dwarf_Op *ops;
57 size_t nops;
58
59 Dwarf_Word byte_size;
60
61 enum
62 {
63 loc_address, loc_register, loc_noncontiguous, loc_unavailable,
64 loc_value, loc_constant, loc_implicit_pointer,
65 loc_decl, loc_fragment, loc_final
66 } type;
67 struct location *frame_base;
68 union
69 {
70 struct /* loc_address, loc_value, loc_fragment, loc_final */
71 {
72 const char *declare; /* Temporary that needs declared. */
73 char *program; /* C fragment, leaves address in s0. */
74 unsigned int stack_depth; /* Temporaries "s0..<N>" used by it. */
75 bool used_deref; /* Program uses "deref" macro. */
76 } address;
77 struct /* loc_register */
78 {
79 unsigned int regno;
80 Dwarf_Word offset;
81 } reg;
82 struct location *pieces; /* loc_noncontiguous */
83 const void *constant_block; /* loc_constant */
84 struct /* loc_implicit_pointer */
85 {
86 struct location *target;
87 Dwarf_Sword offset;
88 } pointer;
89 };
90 };
91
92 /* Select the C type to use in the emitted code to represent slots in the
93 DWARF expression stack. For really proper semantics this should be the
94 target address size. */
95 static const char *
96 stack_slot_type (struct location *context __attribute__ ((unused)), bool sign)
97 {
98 return sign ? STACK_TYPE : UTYPE;
99 }
100
101 static struct location *
102 alloc_location (struct location_context *ctx)
103 {
104 struct location *loc = obstack_alloc (ctx->pool, sizeof *loc);
105 loc->context = ctx;
106 loc->byte_size = 0;
107 loc->frame_base = NULL;
108 loc->ops = NULL;
109 loc->nops = 0;
110 return loc;
111 }
112
113 #define FAIL(loc, fmt, ...) \
114 (*(loc)->context->fail) ((loc)->context->fail_arg, fmt, ## __VA_ARGS__)
115
116 static void
117 default_emit_address (void *fail_arg __attribute__ ((unused)),
118 struct obstack *pool, Dwarf_Addr address)
119 {
120 obstack_printf (pool, AFORMAT, address);
121 }
122
123 static struct location_context *
124 new_context (struct obstack *pool,
125 void (*fail) (void *arg, const char *fmt, ...)
126 __attribute__ ((noreturn, format (printf, 2, 3))),
127 void *fail_arg,
128 void (*emit_address) (void *fail_arg,
129 struct obstack *, Dwarf_Addr),
130 Dwarf_Addr dwbias, Dwarf_Addr pc_address,
131 Dwarf_Attribute *attr, Dwarf_Attribute *fb_attr,
132 const Dwarf_Op *cfa_ops)
133 {
134 struct location_context *ctx = obstack_alloc (pool, sizeof *ctx);
135 ctx->pool = pool;
136 ctx->fail = fail;
137 ctx->fail_arg = fail_arg;
138 ctx->emit_address = emit_address ?: &default_emit_address;
139 ctx->attr = attr;
140 ctx->dwbias = dwbias;
141 ctx->pc = pc_address;
142 ctx->fb_attr = fb_attr;
143 ctx->cfa_ops = cfa_ops;
144 return ctx;
145 }
146 \f
147 /* Translate a DW_AT_const_value attribute as if it were a location of
148 constant-value flavor. */
149
150 static struct location *
151 translate_constant (struct location_context *ctx, int indent,
152 Dwarf_Attribute *attr)
153 {
154 indent += 2;
155
156 struct location *loc = obstack_alloc (ctx->pool, sizeof *loc);
157 loc->context = ctx;
158 loc->next = NULL;
159 loc->byte_size = 0;
160 loc->frame_base = NULL;
161 loc->address.stack_depth = 0;
162 loc->address.declare = NULL;
163 loc->address.used_deref = false;
164 loc->ops = NULL;
165 loc->nops = 0;
166
167 switch (dwarf_whatform (attr))
168 {
169 case DW_FORM_addr:
170 {
171 Dwarf_Addr addr;
172 if (dwarf_formaddr (attr, &addr) != 0)
173 {
174 //TRANSLATORS: failure to find an address that was a constant literal number
175 FAIL (loc, N_("cannot get constant address: %s"),
176 dwarf_errmsg (-1));
177 return NULL;
178 }
179 loc->type = loc_value;
180 obstack_printf (ctx->pool, "%*saddr = ", indent * 2, "");
181 (*ctx->emit_address) (ctx->fail_arg, ctx->pool, ctx->dwbias + addr);
182 obstack_grow (ctx->pool, ";\n", 3);
183 loc->address.program = obstack_finish (ctx->pool);
184 break;
185 }
186
187 case DW_FORM_block:
188 case DW_FORM_block1:
189 case DW_FORM_block2:
190 case DW_FORM_block4:
191 {
192 Dwarf_Block block;
193 if (dwarf_formblock (attr, &block) != 0)
194 {
195 FAIL (loc, N_("cannot get constant block: %s"), dwarf_errmsg (-1));
196 return NULL;
197 }
198 loc->type = loc_constant;
199 loc->byte_size = block.length;
200 loc->constant_block = block.data;
201 break;
202 }
203
204 case DW_FORM_string:
205 case DW_FORM_strp:
206 {
207 const char *string = dwarf_formstring (attr);
208 if (string == NULL)
209 {
210 FAIL (loc, N_("cannot get string constant: %s"), dwarf_errmsg (-1));
211 return NULL;
212 }
213 loc->type = loc_constant;
214 loc->byte_size = strlen (string) + 1;
215 loc->constant_block = string;
216 break;
217 }
218
219 default:
220 {
221 Dwarf_Sword value;
222 if (dwarf_formsdata (attr, &value) != 0)
223 {
224 FAIL (loc, N_("cannot get constant value: %s"), dwarf_errmsg (-1));
225 return NULL;
226 }
227 loc->type = loc_value;
228 obstack_printf (ctx->pool, "%*saddr = %" PRId64 "L;\n",
229 indent * 2, "", value);
230 obstack_1grow (ctx->pool, '\0');
231 loc->address.program = obstack_finish (ctx->pool);
232 break;
233 }
234 }
235
236 return loc;
237 }
238
239 struct location *
240 c_translate_constant (struct obstack *pool,
241 void (*fail) (void *arg,
242 const char *fmt, ...)
243 __attribute__ ((noreturn,
244 format (printf, 2, 3))),
245 void *fail_arg,
246 void (*emit_address) (void *fail_arg,
247 struct obstack *,
248 Dwarf_Addr),
249 int indent, Dwarf_Addr dwbias, Dwarf_Attribute *attr)
250 {
251 return translate_constant (new_context (pool, fail, fail_arg, emit_address,
252 dwbias, 0, attr, NULL, NULL),
253 indent, attr);
254 }
255
256 #if _ELFUTILS_PREREQ (0, 149)
257 static struct location *location_from_attr (struct location_context *ctx,
258 int indent, Dwarf_Attribute *attr);
259 #endif
260
261 \f
262 /* Synthesize a new loc_address using the program on the obstack. */
263 static struct location *
264 new_synthetic_loc (struct location *origin, bool deref)
265 {
266 obstack_1grow (origin->context->pool, '\0');
267 char *program = obstack_finish (origin->context->pool);
268
269 struct location *loc = alloc_location (origin->context);
270 loc->next = NULL;
271 loc->byte_size = 0;
272 loc->type = loc_address;
273 loc->address.program = program;
274 loc->address.stack_depth = 0;
275 loc->address.declare = NULL;
276 loc->address.used_deref = deref;
277
278 if (origin->type == loc_register)
279 {
280 loc->ops = origin->ops;
281 loc->nops = origin->nops;
282 }
283 else
284 {
285 loc->ops = NULL;
286 loc->nops = 0;
287 }
288
289 return loc;
290 }
291
292
293 /* Die in the middle of an expression. */
294 static struct location *
295 lose (struct location *loc, const Dwarf_Op *lexpr, size_t len,
296 const char *failure, size_t i)
297 {
298 if (lexpr == NULL || i >= len)
299 FAIL (loc, "%s", failure);
300 else if (i < len)
301 FAIL (loc, N_("%s in DWARF expression [%Zu] at %" PRIu64
302 " (%#x: %" PRId64 ", %" PRId64 ")"),
303 failure, i, lexpr[i].offset,
304 lexpr[i].atom, lexpr[i].number, lexpr[i].number2);
305 return NULL;
306 }
307
308
309 /* Translate a (constrained) DWARF expression into C code
310 emitted to the obstack POOL. INDENT is the number of indentation levels.
311 ADDRBIAS is the difference between runtime and Dwarf info addresses.
312 INPUT is null or an expression to be initially pushed on the stack.
313 If NEED_FB is null, fail on DW_OP_fbreg, else set *NEED_FB to true
314 and emit "frame_base" for it. On success, set *MAX_STACK to the number
315 of stack slots required. On failure, set *LOSER to the index in EXPR
316 of the operation we could not handle.
317
318 Returns a failure message or null for success. */
319
320 static const char *
321 translate (struct location_context *ctx, int indent,
322 const Dwarf_Op *expr, const size_t len,
323 struct location *input, bool *need_fb, size_t *loser,
324 struct location *loc)
325 {
326 loc->ops = expr;
327 loc->nops = len;
328
329 #define DIE(msg) return (*loser = i, N_(msg))
330
331 #define emit(fmt, ...) obstack_printf (ctx->pool, fmt, ## __VA_ARGS__)
332
333 unsigned int stack_depth;
334 unsigned int max_stack = 0;
335 inline void deepen (void)
336 {
337 if (stack_depth == max_stack)
338 ++max_stack;
339 }
340
341 #define POP(var) \
342 if (stack_depth > 0) \
343 --stack_depth; \
344 else if (tos_register != -1) \
345 fetch_tos_register (); \
346 else \
347 goto underflow; \
348 int var = stack_depth
349 #define PUSH (deepen (), stack_depth++)
350 #define STACK(idx) (stack_depth - 1 - (idx))
351
352 /* Don't put stack operations in the arguments to this. */
353 #define push(fmt, ...) \
354 emit ("%*s" STACKFMT " = " fmt ";\n", indent * 2, "", PUSH, ## __VA_ARGS__)
355
356 int tos_register;
357 inline void fetch_tos_register (void)
358 {
359 deepen ();
360 emit ("%*s" STACKFMT " = fetch_register (%d);\n",
361 indent * 2, "", stack_depth, tos_register);
362 tos_register = -1;
363 }
364
365 bool tos_value;
366 Dwarf_Block implicit_value;
367 bool used_deref;
368 const Dwarf_Op *implicit_pointer;
369
370 /* Initialize our state for handling each new piece. */
371 inline void reset ()
372 {
373 stack_depth = 0;
374 tos_register = -1;
375 tos_value = false;
376 implicit_value.data = NULL;
377 implicit_pointer = NULL;
378 used_deref = false;
379
380 if (input != NULL)
381 switch (input->type)
382 {
383 case loc_address:
384 push ("addr");
385 break;
386
387 case loc_value:
388 push ("addr");
389 tos_value = true;
390 break;
391
392 case loc_register:
393 tos_register = input->reg.regno;
394 break;
395
396 default:
397 abort ();
398 break;
399 }
400 }
401
402 size_t i;
403 inline const char *finish (struct location *piece)
404 {
405 if (piece->nops == 0)
406 {
407 assert (stack_depth == 0);
408 assert (tos_register == -1);
409 assert (obstack_object_size (ctx->pool) == 0);
410 piece->type = loc_unavailable;
411 }
412 else if (stack_depth >= 1)
413 {
414 /* The top of stack has our value.
415 Other stack slots left don't matter. */
416 obstack_1grow (ctx->pool, '\0');
417 char *program = obstack_finish (ctx->pool);
418 if (implicit_pointer != NULL)
419 {
420 piece->type = loc_implicit_pointer;
421 piece->pointer.offset = implicit_pointer->number2;
422 #if !_ELFUTILS_PREREQ (0, 149)
423 /* Then how did we get here? */
424 abort ();
425 #else
426 Dwarf_Attribute target;
427 if (dwarf_getlocation_implicit_pointer (ctx->attr,
428 implicit_pointer,
429 &target) != 0)
430 DIE ("invalid implicit pointer");
431 switch (dwarf_whatattr (&target))
432 {
433 case DW_AT_const_value:
434 piece->pointer.target = translate_constant (ctx, indent,
435 &target);
436 break;
437 case DW_AT_location:
438 piece->pointer.target = location_from_attr (ctx, indent,
439 &target);
440 break;
441 default:
442 DIE ("unexpected implicit pointer attribute!");
443 break;
444 }
445 #endif
446 }
447 else if (implicit_value.data == NULL)
448 {
449 piece->type = tos_value ? loc_value : loc_address;
450 piece->address.declare = NULL;
451 piece->address.program = program;
452 piece->address.stack_depth = max_stack;
453 piece->address.used_deref = used_deref;
454 }
455 else
456 {
457 piece->type = loc_constant;
458 piece->byte_size = implicit_value.length;
459 piece->constant_block = implicit_value.data;
460 }
461 }
462 else if (tos_register == -1)
463 DIE ("stack underflow");
464 else if (obstack_object_size (ctx->pool) != 0)
465 DIE ("register value must stand alone in location expression");
466 else
467 {
468 piece->type = loc_register;
469 piece->reg.regno = tos_register;
470 piece->reg.offset = 0;
471 }
472 return NULL;
473 }
474
475 reset ();
476 struct location *pieces = NULL, **tailpiece = &pieces;
477 size_t piece_expr_start = 0;
478 Dwarf_Word piece_total_bytes = 0;
479 for (i = 0; i < len; ++i)
480 {
481 unsigned int reg;
482 uint_fast8_t sp;
483 Dwarf_Word value;
484
485 inline bool more_ops (void)
486 {
487 return (expr[i].atom != DW_OP_nop
488 && expr[i].atom != DW_OP_piece
489 && expr[i].atom != DW_OP_bit_piece);
490 }
491
492 if (tos_value && more_ops ())
493 DIE ("operations follow DW_OP_stack_value");
494
495 if (implicit_value.data != NULL && more_ops ())
496 DIE ("operations follow DW_OP_implicit_value");
497
498 if (implicit_pointer != NULL && more_ops ())
499 DIE ("operations follow DW_OP_GNU_implicit_pointer");
500
501 switch (expr[i].atom)
502 {
503 /* Basic stack operations. */
504 case DW_OP_nop:
505 break;
506
507 case DW_OP_dup:
508 if (stack_depth < 1)
509 goto underflow;
510 else
511 {
512 unsigned int tos = STACK (0);
513 push (STACKFMT, tos);
514 }
515 break;
516
517 case DW_OP_drop:
518 {
519 POP (ignore);
520 emit ("%*s/* drop " STACKFMT "*/\n", indent * 2, "", ignore);
521 break;
522 }
523
524 case DW_OP_pick:
525 sp = expr[i].number;
526 op_pick:
527 if (sp >= stack_depth)
528 goto underflow;
529 sp = STACK (sp);
530 push (STACKFMT, sp);
531 break;
532
533 case DW_OP_over:
534 sp = 1;
535 goto op_pick;
536
537 case DW_OP_swap:
538 if (stack_depth < 2)
539 goto underflow;
540 deepen (); /* Use a temporary slot. */
541 emit ("%*s"
542 STACKFMT " = " STACKFMT ", "
543 STACKFMT " = " STACKFMT ", "
544 STACKFMT " = " STACKFMT ";\n",
545 indent * 2, "",
546 STACK (-1), STACK (0),
547 STACK (0), STACK (1),
548 STACK (1), STACK (-1));
549 break;
550
551 case DW_OP_rot:
552 if (stack_depth < 3)
553 goto underflow;
554 deepen (); /* Use a temporary slot. */
555 emit ("%*s"
556 STACKFMT " = " STACKFMT ", "
557 STACKFMT " = " STACKFMT ", "
558 STACKFMT " = " STACKFMT ", "
559 STACKFMT " = " STACKFMT ";\n",
560 indent * 2, "",
561 STACK (-1), STACK (0),
562 STACK (0), STACK (1),
563 STACK (1), STACK (2),
564 STACK (3), STACK (-1));
565 break;
566
567
568 /* Control flow operations. */
569 case DW_OP_skip:
570 {
571 Dwarf_Off target = expr[i].offset + 3 + expr[i].number;
572 while (i + 1 < len && expr[i + 1].offset < target)
573 ++i;
574 if (expr[i + 1].offset != target)
575 DIE ("invalid skip target");
576 break;
577 }
578
579 case DW_OP_bra:
580 DIE ("conditional branches not supported");
581 break;
582
583
584 /* Memory access. */
585 case DW_OP_deref:
586 {
587 POP (addr);
588 push ("deref (sizeof (void *), " STACKFMT ")", addr);
589 used_deref = true;
590 }
591 break;
592
593 case DW_OP_deref_size:
594 {
595 POP (addr);
596 push ("deref (" UFORMAT ", " STACKFMT ")",
597 expr[i].number, addr);
598 used_deref = true;
599 }
600 break;
601
602 case DW_OP_xderef:
603 {
604 POP (addr);
605 POP (as);
606 push ("xderef (sizeof (void *), " STACKFMT ", " STACKFMT ")",
607 addr, as);
608 used_deref = true;
609 }
610 break;
611
612 case DW_OP_xderef_size:
613 {
614 POP (addr);
615 POP (as);
616 push ("xderef (" UFORMAT ", " STACKFMT ", " STACKFMT ")",
617 expr[i].number, addr, as);
618 used_deref = true;
619 }
620 break;
621
622 /* Constant-value operations. */
623
624 case DW_OP_addr:
625 emit ("%*s" STACKFMT " = ", indent * 2, "", PUSH);
626 (*ctx->emit_address) (ctx->fail_arg, ctx->pool,
627 ctx->dwbias + expr[i].number);
628 emit (";\n");
629 break;
630
631 case DW_OP_lit0 ... DW_OP_lit31:
632 value = expr[i].atom - DW_OP_lit0;
633 goto op_const;
634
635 case DW_OP_const1u:
636 case DW_OP_const1s:
637 case DW_OP_const2u:
638 case DW_OP_const2s:
639 case DW_OP_const4u:
640 case DW_OP_const4s:
641 case DW_OP_const8u:
642 case DW_OP_const8s:
643 case DW_OP_constu:
644 case DW_OP_consts:
645 value = expr[i].number;
646 op_const:
647 push (SFORMAT, value);
648 break;
649
650 /* Arithmetic operations. */
651 #define UNOP(dw_op, c_op) \
652 case DW_OP_##dw_op: \
653 { \
654 POP (tos); \
655 push ("%s (" STACKFMT ")", #c_op, tos); \
656 } \
657 break
658 #define BINOP(dw_op, c_op) \
659 case DW_OP_##dw_op: \
660 { \
661 POP (b); \
662 POP (a); \
663 push (STACKFMT " %s " STACKFMT, a, #c_op, b); \
664 } \
665 break
666
667 UNOP (abs, op_abs);
668 BINOP (and, &);
669 BINOP (minus, -);
670 BINOP (mul, *);
671 UNOP (neg, -);
672 UNOP (not, ~);
673 BINOP (or, |);
674 BINOP (plus, +);
675 BINOP (shl, <<);
676 BINOP (shr, >>);
677 BINOP (xor, ^);
678
679 /* Comparisons are binary operators too. */
680 BINOP (le, <=);
681 BINOP (ge, >=);
682 BINOP (eq, ==);
683 BINOP (lt, <);
684 BINOP (gt, >);
685 BINOP (ne, !=);
686
687 #undef UNOP
688 #undef BINOP
689
690 case DW_OP_shra:
691 {
692 POP (b);
693 POP (a);
694 push ("(%s) " STACKFMT " >> (%s)" STACKFMT,
695 stack_slot_type (loc, true), a,
696 stack_slot_type (loc, true), b);
697 break;
698 }
699
700 case DW_OP_div:
701 {
702 POP (b);
703 POP (a);
704 push ("dwarf_div_op((%s) " STACKFMT ", (%s) " STACKFMT ")",
705 stack_slot_type (loc, true), a,
706 stack_slot_type (loc, true), b);
707 used_deref = true;
708 break;
709 }
710
711 case DW_OP_mod:
712 {
713 POP (b);
714 POP (a);
715 push ("dwarf_mod_op((%s) " STACKFMT ", (%s) " STACKFMT ")",
716 stack_slot_type (loc, false), a,
717 stack_slot_type (loc, false), b);
718 used_deref = true;
719 break;
720 }
721
722 case DW_OP_plus_uconst:
723 {
724 POP (x);
725 push (STACKFMT " + " UFORMAT, x, expr[i].number);
726 }
727 break;
728
729
730 /* Register-relative addressing. */
731 case DW_OP_breg0 ... DW_OP_breg31:
732 reg = expr[i].atom - DW_OP_breg0;
733 value = expr[i].number;
734 goto op_breg;
735
736 case DW_OP_bregx:
737 reg = expr[i].number;
738 value = expr[i].number2;
739 op_breg:
740 push ("fetch_register (%u) + " SFORMAT, reg, value);
741 break;
742
743 case DW_OP_fbreg:
744 if (need_fb == NULL)
745 DIE ("DW_OP_fbreg from DW_AT_frame_base");
746 *need_fb = true;
747 push ("frame_base + " SFORMAT, expr[i].number);
748 break;
749
750 /* Direct register contents. */
751 case DW_OP_reg0 ... DW_OP_reg31:
752 reg = expr[i].atom - DW_OP_reg0;
753 goto op_reg;
754
755 case DW_OP_regx:
756 reg = expr[i].number;
757 op_reg:
758 tos_register = reg;
759 break;
760
761 /* Special magic. */
762 case DW_OP_piece:
763 if (stack_depth > 1)
764 /* If this ever happens we could copy the program. */
765 DIE ("DW_OP_piece left multiple values on stack");
766 else
767 {
768 /* The obstack has a pending program for loc_address,
769 so we must finish that piece off before we can
770 allocate again. */
771 struct location temp_piece =
772 {
773 .context = loc->context,
774 .frame_base = NULL,
775 .ops = &expr[piece_expr_start],
776 .nops = i - piece_expr_start,
777 };
778 const char *failure = finish (&temp_piece);
779 if (failure != NULL)
780 return failure;
781
782 struct location *piece = obstack_alloc (ctx->pool, sizeof *piece);
783 *piece = temp_piece;
784
785 piece_expr_start = i + 1;
786
787 piece_total_bytes += piece->byte_size = expr[i].number;
788
789 *tailpiece = piece;
790 tailpiece = &piece->next;
791 piece->next = NULL;
792
793 /* Reset default conditions for handling the next piece. */
794 reset ();
795 }
796 break;
797
798 case DW_OP_stack_value:
799 if (stack_depth > 1)
800 DIE ("DW_OP_stack_value left multiple values on stack");
801 else
802 {
803 /* Fetch a register to top of stack, or check for underflow.
804 Then mark the TOS as being a value. */
805 POP (tos);
806 assert (tos == 0);
807 PUSH;
808 tos_value = true;
809 }
810 break;
811
812 case DW_OP_implicit_value:
813 if (ctx->attr == NULL)
814 DIE ("DW_OP_implicit_value used in invalid context"
815 " (no DWARF attribute, ABI return value location?)");
816
817 /* It's supposed to appear by itself, except for DW_OP_piece. */
818 if (stack_depth != 0)
819 DIE ("DW_OP_implicit_value follows stack operations");
820
821 #if _ELFUTILS_PREREQ (0, 143)
822 if (dwarf_getlocation_implicit_value (ctx->attr,
823 (Dwarf_Op *) &expr[i],
824 &implicit_value) != 0)
825 DIE ("dwarf_getlocation_implicit_value failed");
826
827 /* Fake top of stack: implicit_value being set marks it. */
828 PUSH;
829 break;
830 #endif
831
832 DIE ("DW_OP_implicit_value not supported");
833 break;
834
835 #if _ELFUTILS_PREREQ (0, 149)
836 case DW_OP_GNU_implicit_pointer:
837 implicit_pointer = &expr[i];
838 /* Fake top of stack: implicit_pointer being set marks it. */
839 PUSH;
840 break;
841 #endif
842
843 case DW_OP_call_frame_cfa:
844 // We pick this out when processing DW_AT_frame_base in
845 // so it really shouldn't turn up here.
846 if (need_fb == NULL)
847 DIE ("DW_OP_call_frame_cfa while processing frame base");
848 else
849 DIE ("DW_OP_call_frame_cfa not expected outside DW_AT_frame_base");
850 break;
851
852 case DW_OP_push_object_address:
853 DIE ("XXX DW_OP_push_object_address");
854 break;
855
856 default:
857 DIE ("unrecognized operation");
858 break;
859 }
860 }
861
862 if (pieces == NULL)
863 return finish (loc);
864
865 if (piece_expr_start != i)
866 DIE ("extra operations after last DW_OP_piece");
867
868 loc->type = loc_noncontiguous;
869 loc->pieces = pieces;
870 loc->byte_size = piece_total_bytes;
871
872 return NULL;
873
874 underflow:
875 DIE ("stack underflow");
876
877 #undef emit
878 #undef push
879 #undef PUSH
880 #undef POP
881 #undef STACK
882 #undef DIE
883 }
884
885 /* Translate a location starting from an address or nothing. */
886 static struct location *
887 location_from_address (struct location_context *ctx, int indent,
888 const Dwarf_Op *expr, size_t len,
889 struct location **input)
890 {
891 struct location *loc = obstack_alloc (ctx->pool, sizeof *loc);
892 loc->context = ctx;
893 loc->byte_size = 0;
894 loc->frame_base = NULL;
895 loc->ops = NULL;
896 loc->nops = 0;
897
898 bool need_fb = false;
899 size_t loser;
900 const char *failure = translate (ctx, indent + 1, expr, len,
901 *input, &need_fb, &loser, loc);
902 if (failure != NULL)
903 return lose (loc, expr, len, failure, loser);
904
905 loc->next = NULL;
906 if (need_fb)
907 {
908 /* The main expression uses DW_OP_fbreg, so we need to compute
909 the DW_AT_frame_base attribute expression's value first. */
910
911 if (ctx->fb_attr == NULL)
912 FAIL (loc, N_("required DW_AT_frame_base attribute not supplied"));
913
914 Dwarf_Op *fb_expr;
915 size_t fb_len;
916 switch (dwarf_getlocation_addr (ctx->fb_attr, ctx->pc,
917 &fb_expr, &fb_len, 1))
918 {
919 case 1: /* Should always happen. */
920 if (fb_len == 0)
921 goto fb_inaccessible;
922 break;
923
924 default: /* Shouldn't happen. */
925 case -1:
926 FAIL (loc, N_("dwarf_getlocation_addr (form %#x): %s"),
927 dwarf_whatform (ctx->fb_attr), dwarf_errmsg (-1));
928 return NULL;
929
930 case 0: /* Shouldn't happen. */
931 fb_inaccessible:
932 FAIL (loc, N_("DW_AT_frame_base not accessible at this address"));
933 return NULL;
934 }
935
936 // If it is DW_OP_call_frame_cfa then get cfi cfa ops.
937 const Dwarf_Op * fb_ops;
938 if (fb_len == 1 && fb_expr[0].atom == DW_OP_call_frame_cfa)
939 {
940 if (ctx->cfa_ops == NULL)
941 FAIL (loc, N_("No cfa_ops supplied, but needed by DW_OP_call_frame_cfa"));
942 fb_ops = ctx->cfa_ops;
943 }
944 else
945 fb_ops = fb_expr;
946
947 loc->frame_base = alloc_location (ctx);
948 failure = translate (ctx, indent + 1, fb_ops, fb_len, NULL,
949 NULL, &loser, loc->frame_base);
950 if (failure != NULL)
951 return lose (loc, fb_ops, fb_len, failure, loser);
952 }
953
954 if (*input != NULL)
955 (*input)->next = loc;
956 *input = loc;
957
958 return loc;
959 }
960
961 #if _ELFUTILS_PREREQ (0, 149)
962 static struct location *
963 location_from_attr (struct location_context *ctx, int indent,
964 Dwarf_Attribute *attr)
965 {
966 Dwarf_Op *expr;
967 size_t len;
968 switch (dwarf_getlocation_addr (attr, ctx->pc, &expr, &len, 1))
969 {
970 case 1: /* Should always happen. */
971 if (len > 0)
972 break;
973 /* Fall through. */
974
975 case 0: /* Shouldn't happen. */
976 (*ctx->fail) (ctx->fail_arg, N_("not accessible at this address (%#" PRIx64 ")"), ctx->pc);
977 return NULL;
978
979 default: /* Shouldn't happen. */
980 case -1:
981 (*ctx->fail) (ctx->fail_arg, "dwarf_getlocation_addr: %s",
982 dwarf_errmsg (-1));
983 return NULL;
984 }
985
986 struct location *input = NULL;
987 return location_from_address (ctx, indent, expr, len, &input);
988 }
989 #endif
990
991 static struct location *
992 translate_offset (int indent, const Dwarf_Op *expr, size_t len, size_t i,
993 struct location *head, struct location **input,
994 Dwarf_Word offset)
995 {
996 struct location_context *const ctx = (*input)->context;
997
998 #define DIE(msg) return lose (*input, expr, len, N_(msg), i)
999
1000 while ((*input)->type == loc_noncontiguous)
1001 {
1002 /* We are starting from a noncontiguous object (DW_OP_piece).
1003 Find the piece we want. */
1004
1005 struct location *piece = (*input)->pieces;
1006 while (piece != NULL && offset >= piece->byte_size)
1007 {
1008 offset -= piece->byte_size;
1009 piece = piece->next;
1010 }
1011 if (piece == NULL)
1012 DIE ("offset outside available pieces");
1013
1014 assert ((*input)->next == NULL);
1015 (*input)->next = piece;
1016 *input = piece;
1017 }
1018
1019 switch ((*input)->type)
1020 {
1021 case loc_address:
1022 {
1023 /* The piece we want is actually in memory. Use the same
1024 program to compute the address from the preceding input. */
1025
1026 struct location *loc = obstack_alloc (ctx->pool, sizeof *loc);
1027 *loc = **input;
1028 if (head == NULL)
1029 head = loc;
1030 (*input)->next = loc;
1031 if (offset == 0)
1032 {
1033 /* The piece addresses exactly where we want to go. */
1034 loc->next = NULL;
1035 *input = loc;
1036 }
1037 else
1038 {
1039 /* Add a second fragment to offset the piece address. */
1040 obstack_printf (ctx->pool, "%*saddr += " SFORMAT "\n",
1041 indent * 2, "", offset);
1042 *input = loc->next = new_synthetic_loc (*input, false);
1043 }
1044
1045 /* That's all she wrote. */
1046 return head;
1047 }
1048
1049 case loc_register:
1050 /* This piece (or the whole struct) fits in a register. */
1051 (*input)->reg.offset += offset;
1052 return head ?: *input;
1053
1054 case loc_constant:
1055 /* This piece has a constant offset. */
1056 if (offset >= (*input)->byte_size)
1057 DIE ("offset outside available constant block");
1058 (*input)->constant_block += offset;
1059 (*input)->byte_size -= offset;
1060 return head ?: *input;
1061
1062 case loc_implicit_pointer:
1063 /* This piece is an implicit pointer. */
1064 (*input)->pointer.offset += offset;
1065 return head ?: *input;
1066
1067 case loc_unavailable:
1068 /* Let it be diagnosed later. */
1069 return head ?: *input;
1070
1071 case loc_value:
1072 /* The piece we want is part of a computed offset.
1073 If it's the whole thing, we are done. */
1074 if (offset == 0)
1075 return head ?: *input;
1076 DIE ("XXX extract partial rematerialized value");
1077 break;
1078
1079 default:
1080 abort ();
1081 }
1082
1083 #undef DIE
1084 }
1085
1086
1087 /* Translate a location starting from a non-address "on the top of the
1088 stack". The *INPUT location is a register name or noncontiguous
1089 object specification, and this expression wants to find the "address"
1090 of an object (or the actual value) relative to that "address". */
1091
1092 static struct location *
1093 location_relative (struct location_context *ctx, int indent,
1094 const Dwarf_Op *expr, size_t len,
1095 struct location **input)
1096 {
1097 Dwarf_Sword *stack = NULL;
1098 unsigned int stack_depth = 0, max_stack = 0;
1099 inline void deepen (void)
1100 {
1101 if (stack_depth == max_stack)
1102 {
1103 ++max_stack;
1104 obstack_blank (ctx->pool, sizeof stack[0]);
1105 stack = (void *) obstack_base (ctx->pool);
1106 }
1107 }
1108
1109 #define POP(var) \
1110 if (stack_depth > 0) \
1111 --stack_depth; \
1112 else \
1113 goto underflow; \
1114 int var = stack_depth
1115 #define PUSH (deepen (), stack_depth++)
1116 #define STACK(idx) (stack_depth - 1 - (idx))
1117 #define STACKWORD(idx) stack[STACK (idx)]
1118
1119 /* Don't put stack operations in the arguments to this. */
1120 #define push(value) (stack[PUSH] = (value))
1121
1122 const char *failure = NULL;
1123 #define DIE(msg) do { failure = N_(msg); goto fail; } while (0)
1124
1125 struct location *head = NULL;
1126 size_t i;
1127 for (i = 0; i < len; ++i)
1128 {
1129 uint_fast8_t sp;
1130 Dwarf_Word value;
1131
1132 switch (expr[i].atom)
1133 {
1134 /* Basic stack operations. */
1135 case DW_OP_nop:
1136 break;
1137
1138 case DW_OP_dup:
1139 if (stack_depth < 1)
1140 goto underflow;
1141 else
1142 {
1143 unsigned int tos = STACK (0);
1144 push (stack[tos]);
1145 }
1146 break;
1147
1148 case DW_OP_drop:
1149 if (stack_depth > 0)
1150 --stack_depth;
1151 else if (*input != NULL)
1152 /* Mark that we have consumed the input. */
1153 *input = NULL;
1154 else
1155 /* Hits if cleared above, or if we had no input at all. */
1156 goto underflow;
1157 break;
1158
1159 case DW_OP_pick:
1160 sp = expr[i].number;
1161 op_pick:
1162 if (sp >= stack_depth)
1163 goto underflow;
1164 sp = STACK (sp);
1165 push (stack[sp]);
1166 break;
1167
1168 case DW_OP_over:
1169 sp = 1;
1170 goto op_pick;
1171
1172 case DW_OP_swap:
1173 if (stack_depth < 2)
1174 goto underflow;
1175 deepen (); /* Use a temporary slot. */
1176 STACKWORD (-1) = STACKWORD (0);
1177 STACKWORD (0) = STACKWORD (1);
1178 STACKWORD (1) = STACKWORD (-1);
1179 break;
1180
1181 case DW_OP_rot:
1182 if (stack_depth < 3)
1183 goto underflow;
1184 deepen (); /* Use a temporary slot. */
1185 STACKWORD (-1) = STACKWORD (0);
1186 STACKWORD (0) = STACKWORD (1);
1187 STACKWORD (2) = STACKWORD (2);
1188 STACKWORD (2) = STACKWORD (-1);
1189 break;
1190
1191
1192 /* Control flow operations. */
1193 case DW_OP_bra:
1194 {
1195 POP (taken);
1196 if (stack[taken] == 0)
1197 break;
1198 }
1199 /*FALLTHROUGH*/
1200
1201 case DW_OP_skip:
1202 {
1203 Dwarf_Off target = expr[i].offset + 3 + expr[i].number;
1204 while (i + 1 < len && expr[i + 1].offset < target)
1205 ++i;
1206 if (expr[i + 1].offset != target)
1207 DIE ("invalid skip target");
1208 break;
1209 }
1210
1211 /* Memory access. */
1212 case DW_OP_deref:
1213 case DW_OP_deref_size:
1214 case DW_OP_xderef:
1215 case DW_OP_xderef_size:
1216
1217 /* Register-relative addressing. */
1218 case DW_OP_breg0 ... DW_OP_breg31:
1219 case DW_OP_bregx:
1220 case DW_OP_fbreg:
1221
1222 /* This started from a register, but now it's following a pointer.
1223 So we can do the translation starting from address here. */
1224 return location_from_address (ctx, indent, expr, len, input);
1225
1226
1227 /* Constant-value operations. */
1228 case DW_OP_addr:
1229 DIE ("static calculation depends on load-time address");
1230 push (ctx->dwbias + expr[i].number);
1231 break;
1232
1233 case DW_OP_lit0 ... DW_OP_lit31:
1234 value = expr[i].atom - DW_OP_lit0;
1235 goto op_const;
1236
1237 case DW_OP_const1u:
1238 case DW_OP_const1s:
1239 case DW_OP_const2u:
1240 case DW_OP_const2s:
1241 case DW_OP_const4u:
1242 case DW_OP_const4s:
1243 case DW_OP_const8u:
1244 case DW_OP_const8s:
1245 case DW_OP_constu:
1246 case DW_OP_consts:
1247 value = expr[i].number;
1248 op_const:
1249 push (value);
1250 break;
1251
1252 /* Arithmetic operations. */
1253 #define UNOP(dw_op, c_op) \
1254 case DW_OP_##dw_op: \
1255 { \
1256 POP (tos); \
1257 push (c_op (stack[tos])); \
1258 } \
1259 break
1260 #define BINOP(dw_op, c_op) \
1261 case DW_OP_##dw_op: \
1262 { \
1263 POP (b); \
1264 POP (a); \
1265 push (stack[a] c_op stack[b]); \
1266 } \
1267 break
1268
1269 #define op_abs(x) (x < 0 ? -x : x)
1270 UNOP (abs, op_abs);
1271 BINOP (and, &);
1272 BINOP (div, /);
1273 BINOP (mod, %);
1274 BINOP (mul, *);
1275 UNOP (neg, -);
1276 UNOP (not, ~);
1277 BINOP (or, |);
1278 BINOP (shl, <<);
1279 BINOP (shra, >>);
1280 BINOP (xor, ^);
1281
1282 /* Comparisons are binary operators too. */
1283 BINOP (le, <=);
1284 BINOP (ge, >=);
1285 BINOP (eq, ==);
1286 BINOP (lt, <);
1287 BINOP (gt, >);
1288 BINOP (ne, !=);
1289
1290 #undef UNOP
1291 #undef BINOP
1292
1293 case DW_OP_shr:
1294 {
1295 POP (b);
1296 POP (a);
1297 push ((Dwarf_Word) stack[a] >> (Dwarf_Word) stack[b]);
1298 break;
1299 }
1300
1301 /* Simple addition we may be able to handle relative to
1302 the starting register name. */
1303 case DW_OP_minus:
1304 {
1305 POP (tos);
1306 value = -stack[tos];
1307 goto plus;
1308 }
1309 case DW_OP_plus:
1310 {
1311 POP (tos);
1312 value = stack[tos];
1313 goto plus;
1314 }
1315 case DW_OP_plus_uconst:
1316 value = expr[i].number;
1317 plus:
1318 if (stack_depth > 0)
1319 {
1320 /* It's just private diddling after all. */
1321 POP (a);
1322 push (stack[a] + value);
1323 break;
1324 }
1325 if (*input == NULL)
1326 goto underflow;
1327
1328 /* This is the primary real-world case: the expression takes
1329 the input address and adds a constant offset. */
1330
1331 head = translate_offset (indent, expr, len, i, head, input, value);
1332 if (head != NULL && i + 1 < len)
1333 {
1334 if ((*input)->type != loc_address)
1335 DIE ("too much computation for non-address location");
1336
1337 /* This expression keeps going, but further
1338 computations now have an address to start with.
1339 So we can punt to the address computation generator. */
1340 struct location *loc = location_from_address
1341 (ctx, indent, &expr[i + 1], len - i - 1, input);
1342 if (loc == NULL)
1343 head = NULL;
1344 }
1345 return head;
1346
1347 /* Direct register contents. */
1348 case DW_OP_reg0 ... DW_OP_reg31:
1349 case DW_OP_regx:
1350 DIE ("register");
1351 break;
1352
1353 /* Special magic. */
1354 case DW_OP_piece:
1355 DIE ("DW_OP_piece");
1356 break;
1357
1358 case DW_OP_push_object_address:
1359 DIE ("XXX DW_OP_push_object_address");
1360 break;
1361
1362 default:
1363 DIE ("unrecognized operation");
1364 break;
1365 }
1366 }
1367
1368 if (stack_depth > 1)
1369 DIE ("multiple values left on stack");
1370
1371 if (stack_depth > 0) /* stack_depth == 1 */
1372 {
1373 if (*input != NULL)
1374 DIE ("multiple values left on stack");
1375
1376 /* Could handle this if it ever actually happened. */
1377 DIE ("relative expression computed constant");
1378 }
1379
1380 return head;
1381
1382 underflow:
1383 if (*input == NULL)
1384 DIE ("stack underflow");
1385 else
1386 DIE ("cannot handle location expression");
1387
1388 fail:
1389 return lose (*input, expr, len, failure, i);
1390 }
1391
1392 /* Translate a C fragment for the location expression, using *INPUT
1393 as the starting location, begin from scratch if *INPUT is null.
1394 If DW_OP_fbreg is used, it may have a subfragment computing from
1395 the FB_ATTR location expression.
1396
1397 On errors, call FAIL and never return. On success, return the
1398 first fragment created, which is also chained onto (*INPUT)->next.
1399 *INPUT is then updated with the new tail of that chain. */
1400
1401 struct location *
1402 c_translate_location (struct obstack *pool,
1403 void (*fail) (void *arg, const char *fmt, ...)
1404 __attribute__ ((noreturn, format (printf, 2, 3))),
1405 void *fail_arg,
1406 void (*emit_address) (void *fail_arg,
1407 struct obstack *, Dwarf_Addr),
1408 int indent, Dwarf_Addr dwbias, Dwarf_Addr pc_address,
1409 Dwarf_Attribute *attr,
1410 const Dwarf_Op *expr, size_t len,
1411 struct location **input, Dwarf_Attribute *fb_attr,
1412 const Dwarf_Op *cfa_ops)
1413 {
1414 indent += 2;
1415
1416 struct location_context *ctx;
1417 if (*input == NULL)
1418 ctx = new_context (pool, fail, fail_arg, emit_address, dwbias, pc_address,
1419 attr, fb_attr, cfa_ops);
1420 else
1421 {
1422 ctx = (*input)->context;
1423 assert (ctx->pool == pool);
1424 if (pc_address == 0)
1425 pc_address = ctx->pc;
1426 else if (ctx->pc == 0)
1427 ctx->pc = pc_address;
1428 assert (ctx->pc == pc_address);
1429 }
1430
1431 switch (*input == NULL ? loc_address : (*input)->type)
1432 {
1433 case loc_address:
1434 /* We have a previous address computation.
1435 This expression will compute starting with that on the stack. */
1436 return location_from_address (ctx, indent, expr, len, input);
1437
1438 case loc_noncontiguous:
1439 case loc_register:
1440 case loc_value:
1441 case loc_constant:
1442 case loc_unavailable:
1443 case loc_implicit_pointer:
1444 /* The starting point is not an address computation, but a
1445 register or implicit value. We can only handle limited
1446 computations from here. */
1447 return location_relative (ctx, indent, expr, len, input);
1448
1449 default:
1450 abort ();
1451 break;
1452 }
1453
1454 return NULL;
1455 }
1456
1457
1458 /* Translate a C fragment for a direct argument VALUE. On errors, call FAIL,
1459 which should not return. Any later errors will use FAIL and FAIL_ARG from
1460 this translate call. On success, return the fragment created. */
1461 struct location *
1462 c_translate_argument (struct obstack *pool,
1463 void (*fail) (void *arg, const char *fmt, ...)
1464 __attribute__ ((noreturn, format (printf, 2, 3))),
1465 void *fail_arg,
1466 void (*emit_address) (void *fail_arg,
1467 struct obstack *, Dwarf_Addr),
1468 int indent, const char *value)
1469 {
1470 indent += 2;
1471
1472 obstack_printf (pool, "%*saddr = %s;\n", indent * 2, "", value);
1473 obstack_1grow (pool, '\0');
1474 char *program = obstack_finish (pool);
1475
1476 struct location *loc = obstack_alloc (pool, sizeof *loc);
1477 loc->context = new_context (pool, fail, fail_arg, emit_address, 0,
1478 0, NULL, NULL, NULL);
1479 loc->next = NULL;
1480 loc->ops = NULL;
1481 loc->nops = 0;
1482 loc->byte_size = 0;
1483 loc->type = loc_address;
1484 loc->frame_base = NULL;
1485 loc->address.declare = NULL;
1486 loc->address.program = program;
1487 loc->address.stack_depth = 0;
1488 loc->address.used_deref = false;
1489
1490 return loc;
1491 }
1492
1493
1494 /* Emit "uintNN_t TARGET = ...;". */
1495 static bool
1496 emit_base_fetch (struct obstack *pool, Dwarf_Word byte_size,
1497 bool signed_p, const char *target, struct location *loc)
1498 {
1499 bool deref = false;
1500
1501 /* Emit size/signed coercion. */
1502 obstack_printf (pool, "{ ");
1503 obstack_printf (pool, "%sint%u_t value = ",
1504 (signed_p ? "" : "u"), (unsigned)(byte_size * 8));
1505
1506 switch (loc->type)
1507 {
1508 case loc_value:
1509 obstack_printf (pool, "addr;");
1510 break;
1511
1512 case loc_address:
1513 if (byte_size != 0 && byte_size != (Dwarf_Word) -1)
1514 obstack_printf (pool, "deref (%" PRIu64 ", addr);", byte_size);
1515 else
1516 obstack_printf (pool, "deref (sizeof %s, addr);", target);
1517 deref = true;
1518 break;
1519
1520 case loc_register:
1521 if (loc->reg.offset != 0)
1522 FAIL (loc, N_("cannot handle offset into register in fetch"));
1523 obstack_printf (pool, "fetch_register (%u);", loc->reg.regno);
1524 break;
1525
1526 case loc_noncontiguous:
1527 FAIL (loc, N_("noncontiguous location for base fetch"));
1528 break;
1529
1530 case loc_implicit_pointer:
1531 FAIL (loc, N_("pointer has been optimized out"));
1532 break;
1533
1534 case loc_unavailable:
1535 FAIL (loc, N_("location not available"));
1536 break;
1537
1538 default:
1539 abort ();
1540 break;
1541 }
1542
1543 obstack_printf (pool, " %s = value; }", target);
1544 return deref;
1545 }
1546
1547 /* Emit "... = RVALUE;". */
1548 static bool
1549 emit_base_store (struct obstack *pool, Dwarf_Word byte_size,
1550 const char *rvalue, struct location *loc)
1551 {
1552 switch (loc->type)
1553 {
1554 case loc_address:
1555 if (byte_size != 0 && byte_size != (Dwarf_Word) -1)
1556 obstack_printf (pool, "store_deref (%" PRIu64 ", addr, %s); ",
1557 byte_size, rvalue);
1558 else
1559 obstack_printf (pool, "store_deref (sizeof %s, addr, %s); ",
1560 rvalue, rvalue);
1561 return true;
1562
1563 case loc_register:
1564 if (loc->reg.offset != 0)
1565 FAIL (loc, N_("cannot handle offset into register in store"));
1566 obstack_printf (pool, "store_register (%u, %s);", loc->reg.regno, rvalue);
1567 break;
1568
1569 case loc_noncontiguous:
1570 FAIL (loc, N_("noncontiguous location for base store"));
1571 break;
1572
1573 case loc_implicit_pointer:
1574 FAIL (loc, N_("pointer has been optimized out"));
1575 break;
1576
1577 case loc_value:
1578 FAIL (loc, N_("location is computed value, cannot store"));
1579 break;
1580
1581 case loc_constant:
1582 FAIL (loc, N_("location is constant value, cannot store"));
1583 break;
1584
1585 case loc_unavailable:
1586 FAIL (loc, N_("location is not available, cannot store"));
1587 break;
1588
1589 default:
1590 abort ();
1591 break;
1592 }
1593
1594 return false;
1595 }
1596
1597
1598 /* Slice up an object into pieces no larger than MAX_PIECE_BYTES,
1599 yielding a loc_noncontiguous location unless LOC is small enough. */
1600 static struct location *
1601 discontiguify (struct location_context *ctx, int indent, struct location *loc,
1602 Dwarf_Word total_bytes, Dwarf_Word max_piece_bytes)
1603 {
1604 inline bool pieces_small_enough (void)
1605 {
1606 if (loc->type != loc_noncontiguous)
1607 return total_bytes <= max_piece_bytes;
1608 struct location *p;
1609 for (p = loc->pieces; p != NULL; p = p->next)
1610 if (p->byte_size > max_piece_bytes)
1611 return false;
1612 return true;
1613 }
1614
1615 /* Constants are always copied byte-wise, but we may need to
1616 * truncate to the total_bytes requested here. */
1617 if (loc->type == loc_constant)
1618 {
1619 if (loc->byte_size > total_bytes)
1620 loc->byte_size = total_bytes;
1621 return loc;
1622 }
1623
1624 if (pieces_small_enough ())
1625 return loc;
1626
1627 struct location *noncontig = alloc_location (ctx);
1628 noncontig->next = NULL;
1629 noncontig->type = loc_noncontiguous;
1630 noncontig->byte_size = total_bytes;
1631 noncontig->pieces = NULL;
1632 struct location **tailpiece = &noncontig->pieces;
1633 inline void add (struct location *piece)
1634 {
1635 *tailpiece = piece;
1636 tailpiece = &piece->next;
1637 }
1638
1639 switch (loc->type)
1640 {
1641 case loc_address:
1642 {
1643 /* Synthesize a piece that sets "container_addr" to the computed
1644 address of the whole object. Each piece will refer to this. */
1645 obstack_printf (ctx->pool, "%*scontainer_addr = addr;\n",
1646 indent++ * 2, "");
1647 loc->next = new_synthetic_loc (loc, false);
1648 loc->next->byte_size = loc->byte_size;
1649 loc->next->type = loc_fragment;
1650 loc->next->address.declare = "container_addr";
1651 loc = loc->next;
1652
1653 /* Synthesize pieces that just compute "container_addr + N". */
1654 Dwarf_Word offset = 0;
1655 while (total_bytes - offset > 0)
1656 {
1657 Dwarf_Word size = total_bytes - offset;
1658 if (size > max_piece_bytes)
1659 size = max_piece_bytes;
1660
1661 obstack_printf (ctx->pool,
1662 "%*saddr = container_addr + " UFORMAT ";\n",
1663 indent * 2, "", offset);
1664 struct location *piece = new_synthetic_loc (loc, false);
1665 piece->byte_size = size;
1666 add (piece);
1667
1668 offset += size;
1669 }
1670
1671 --indent;
1672 break;
1673 }
1674
1675 case loc_value:
1676 FAIL (loc, N_("stack value too big for fetch ???"));
1677 break;
1678
1679 case loc_register:
1680 FAIL (loc, N_("single register too big for fetch/store ???"));
1681 break;
1682
1683 case loc_implicit_pointer:
1684 FAIL (loc, N_("implicit pointer too big for fetch/store ???"));
1685 break;
1686
1687 case loc_noncontiguous:
1688 /* Could be handled if it ever happened. */
1689 FAIL (loc, N_("cannot support noncontiguous location"));
1690 break;
1691
1692 default:
1693 abort ();
1694 break;
1695 }
1696
1697 loc->next = noncontig;
1698 return noncontig;
1699 }
1700
1701 /* Make a fragment that declares a union such as:
1702 union {
1703 char bytes[8];
1704 struct {
1705 uint32_t p0;
1706 uint32_t p4;
1707 } pieces __attribute__ ((packed));
1708 uint64_t whole;
1709 } u_pieces<depth>;
1710 */
1711 static void
1712 declare_noncontig_union (struct obstack *pool, int indent,
1713 struct location **input, struct location *loc,
1714 int depth)
1715 {
1716 if (depth > 9)
1717 FAIL (loc, N_("declaring noncontig union for depth > 9, too many pieces"));
1718
1719 obstack_printf (pool, "%*sunion {\n", indent++ * 2, "");
1720
1721 obstack_printf (pool, "%*schar bytes[%" PRIu64 "];\n",
1722 indent * 2, "", loc->byte_size);
1723
1724 if (loc->type == loc_noncontiguous)
1725 {
1726 Dwarf_Word offset = 0;
1727 struct location *p;
1728 obstack_printf (pool, "%*sstruct {\n", indent++ * 2, "");
1729
1730 for (p = loc->pieces; p != NULL; p = p->next)
1731 {
1732 obstack_printf (pool, "%*suint%" PRIu64 "_t p%" PRIu64 ";\n",
1733 indent * 2, "", p->byte_size * 8, offset);
1734 offset += p->byte_size;
1735 }
1736
1737 obstack_printf (pool, "%*s} pieces __attribute__ ((packed));\n",
1738 --indent * 2, "");
1739 }
1740
1741 obstack_printf (pool, "%*suint%" PRIu64 "_t whole;\n",
1742 indent * 2, "", loc->byte_size * 8);
1743
1744 obstack_printf (pool, "%*s} u_pieces%d;\n", --indent * 2, "", depth);
1745
1746 loc = new_synthetic_loc (*input, false);
1747 loc->type = loc_decl;
1748 (*input)->next = loc;
1749 *input = loc;
1750 }
1751
1752 /* Determine the byte size of a base type. */
1753 static Dwarf_Word
1754 base_byte_size (Dwarf_Die *typedie, struct location *origin)
1755 {
1756 assert (dwarf_tag (typedie) == DW_TAG_base_type ||
1757 dwarf_tag (typedie) == DW_TAG_enumeration_type);
1758
1759 Dwarf_Attribute attr_mem;
1760 Dwarf_Word size;
1761 if (dwarf_attr_integrate (typedie, DW_AT_byte_size, &attr_mem) != NULL
1762 && dwarf_formudata (&attr_mem, &size) == 0)
1763 return size;
1764
1765 FAIL (origin,
1766 N_("cannot get byte_size attribute for type %s: %s"),
1767 dwarf_diename (typedie) ?: "<anonymous>",
1768 dwarf_errmsg (-1));
1769 return -1;
1770 }
1771
1772 static Dwarf_Word
1773 base_encoding (Dwarf_Die *typedie, struct location *origin)
1774 {
1775 if (! (dwarf_tag (typedie) == DW_TAG_base_type ||
1776 dwarf_tag (typedie) == DW_TAG_enumeration_type))
1777 return -1;
1778
1779 Dwarf_Attribute attr_mem;
1780 Dwarf_Word encoding;
1781 if (dwarf_attr_integrate (typedie, DW_AT_encoding, &attr_mem) != NULL
1782 && dwarf_formudata (&attr_mem, &encoding) == 0)
1783 return encoding;
1784
1785 (void) origin;
1786 /*
1787 FAIL (origin,
1788 N_("cannot get encoding attribute for type %s: %s"),
1789 dwarf_diename (typedie) ?: "<anonymous>",
1790 dwarf_errmsg (-1));
1791 */
1792 return -1;
1793 }
1794
1795
1796
1797 /* Fetch the bitfield parameters. */
1798 static void
1799 get_bitfield (struct location *loc,
1800 Dwarf_Die *die, Dwarf_Word *bit_offset, Dwarf_Word *bit_size)
1801 {
1802 Dwarf_Attribute attr_mem;
1803 if (dwarf_attr_integrate (die, DW_AT_bit_offset, &attr_mem) == NULL
1804 || dwarf_formudata (&attr_mem, bit_offset) != 0
1805 || dwarf_attr_integrate (die, DW_AT_bit_size, &attr_mem) == NULL
1806 || dwarf_formudata (&attr_mem, bit_size) != 0)
1807 FAIL (loc, N_("cannot get bit field parameters: %s"), dwarf_errmsg (-1));
1808 }
1809
1810 /* Translate a fragment to fetch the base-type value of BYTE_SIZE bytes
1811 at the *INPUT location and store it in lvalue TARGET. */
1812 static void
1813 translate_base_fetch (struct obstack *pool, int indent,
1814 Dwarf_Word byte_size, bool signed_p,
1815 struct location **input, const char *target,
1816 int depth)
1817 {
1818 bool deref = false;
1819
1820 if ((*input)->type == loc_noncontiguous)
1821 {
1822 struct location *p = (*input)->pieces;
1823
1824 declare_noncontig_union (pool, indent, input, *input, depth);
1825
1826 Dwarf_Word offset = 0;
1827 char piece[sizeof "u_pieces?.pieces.p" + 20] = "u_pieces?.pieces.p";
1828 piece[8] = (char) ('0' + depth);
1829 int pdepth = depth + 1;
1830 while (p != NULL)
1831 {
1832 struct location *newp = obstack_alloc (pool, sizeof *newp);
1833 *newp = *p;
1834 newp->next = NULL;
1835 (*input)->next = newp;
1836 *input = newp;
1837
1838 snprintf (&piece[sizeof "u_pieces?.pieces.p" - 1], 20,
1839 "%" PRIu64, offset);
1840 translate_base_fetch (pool, indent, p->byte_size, signed_p /* ? */,
1841 input, piece, pdepth);
1842 (*input)->type = loc_fragment;
1843
1844 offset += p->byte_size;
1845 p = p->next;
1846 pdepth++;
1847 }
1848
1849 obstack_printf (pool, "%*s%s = u_pieces%d.whole;\n", indent * 2,
1850 "", target, depth);
1851 }
1852 else if ((*input)->type == loc_constant)
1853 {
1854 const unsigned char *constant_block = (*input)->constant_block;
1855 const size_t byte_size = (*input)->byte_size;
1856 size_t i;
1857
1858 declare_noncontig_union (pool, indent, input, *input, depth);
1859
1860 for (i = 0; i < byte_size; ++i)
1861 obstack_printf (pool, "%*su_pieces%d.bytes[%zu] = %#x;\n", indent * 2,
1862 "", depth, i, constant_block[i]);
1863
1864 obstack_printf (pool, "%*s%s = u_pieces%d.whole;\n", indent * 2,
1865 "", target, depth);
1866 }
1867 else
1868 switch (byte_size)
1869 {
1870 case 0: /* Special case, means address size. */
1871 case 1:
1872 case 2:
1873 case 4:
1874 case 8:
1875 obstack_printf (pool, "%*s", indent * 2, "");
1876 deref = emit_base_fetch (pool, byte_size, signed_p, target, *input);
1877 obstack_printf (pool, "\n");
1878 break;
1879
1880 default:
1881 /* Could handle this generating call to memcpy equivalent. */
1882 FAIL (*input, N_("fetch is larger than base integer types"));
1883 break;
1884 }
1885
1886 struct location *loc = new_synthetic_loc (*input, deref);
1887 loc->byte_size = byte_size;
1888 loc->type = loc_final;
1889 (*input)->next = loc;
1890 *input = loc;
1891 }
1892
1893 /* Determine the maximum size of a base type, from some DIE in the CU. */
1894 static Dwarf_Word
1895 max_fetch_size (struct location *loc, Dwarf_Die *die)
1896 {
1897 Dwarf_Die cu_mem;
1898 uint8_t address_size;
1899 Dwarf_Die *cu = dwarf_diecu (die, &cu_mem, &address_size, NULL);
1900 if (cu == NULL)
1901 //TRANSLATORS: CU stands for 'compilation unit'
1902 FAIL (loc, N_("cannot determine CU address size from %s: %s"),
1903 dwarf_diename (die), dwarf_errmsg (-1));
1904
1905 return address_size;
1906 }
1907
1908 /* Translate a fragment to fetch the value of variable or member DIE
1909 at the *INPUT location and store it in lvalue TARGET. */
1910 void
1911 c_translate_fetch (struct obstack *pool, int indent,
1912 Dwarf_Addr dwbias __attribute__ ((unused)),
1913 Dwarf_Die *die, Dwarf_Die *typedie,
1914 struct location **input, const char *target)
1915 {
1916 struct location_context *const ctx = (*input)->context;
1917 assert (ctx->pool == pool);
1918
1919 ++indent;
1920
1921 Dwarf_Attribute size_attr;
1922 Dwarf_Word byte_size;
1923 if (dwarf_attr_integrate (die, DW_AT_byte_size, &size_attr) == NULL
1924 || dwarf_formudata (&size_attr, &byte_size) != 0)
1925 byte_size = base_byte_size (typedie, *input);
1926
1927 Dwarf_Attribute encoding_attr;
1928 Dwarf_Word encoding;
1929 if (dwarf_attr_integrate (die, DW_AT_encoding, &encoding_attr) == NULL
1930 || dwarf_formudata (&encoding_attr, &encoding) != 0)
1931 encoding = base_encoding (typedie, *input);
1932 bool signed_p = encoding == DW_ATE_signed || encoding == DW_ATE_signed_char;
1933
1934 *input = discontiguify (ctx, indent, *input, byte_size,
1935 max_fetch_size (*input, die));
1936
1937 if (dwarf_hasattr_integrate (die, DW_AT_bit_offset))
1938 {
1939 /* This is a bit field. Fetch the containing base type into a
1940 temporary variable. */
1941
1942 translate_base_fetch (pool, indent, byte_size, signed_p, input, "tmp", 0);
1943 (*input)->type = loc_fragment;
1944 (*input)->address.declare = "tmp";
1945
1946 Dwarf_Word bit_offset = 0;
1947 Dwarf_Word bit_size = 0;
1948 get_bitfield (*input, die, &bit_offset, &bit_size);
1949
1950 obstack_printf (pool, "%*s"
1951 "fetch_bitfield (%s, tmp, %" PRIu64 ", %" PRIu64 ");\n",
1952 indent *2, "", target, bit_offset, bit_size);
1953
1954 struct location *loc = new_synthetic_loc (*input, false);
1955 loc->type = loc_final;
1956 (*input)->next = loc;
1957 *input = loc;
1958 }
1959 else
1960 translate_base_fetch (pool, indent, byte_size, signed_p, input, target, 0);
1961 }
1962
1963 /* Translate a fragment to store RVALUE into the base-type value of
1964 BYTE_SIZE bytes at the *INPUT location. */
1965 static void
1966 translate_base_store (struct obstack *pool, int indent, Dwarf_Word byte_size,
1967 struct location **input, struct location *store_loc,
1968 const char *rvalue, int depth)
1969 {
1970 bool deref = false;
1971
1972 if (store_loc->type == loc_noncontiguous)
1973 {
1974 declare_noncontig_union (pool, indent, input, store_loc, depth);
1975
1976 obstack_printf (pool, "%*su_pieces%d.whole = %s;\n", indent * 2,
1977 "", depth, rvalue);
1978 struct location *loc = new_synthetic_loc (*input, deref);
1979 loc->type = loc_fragment;
1980 (*input)->next = loc;
1981 *input = loc;
1982
1983 Dwarf_Word offset = 0;
1984 char piece[sizeof "u_pieces?.pieces.p" + 20] = "u_pieces?.pieces.p";
1985 piece[8] = (char) ('0' + depth);
1986 struct location *p;
1987 int pdepth = depth + 1;
1988 for (p = store_loc->pieces; p != NULL; p = p->next)
1989 {
1990 struct location *newp = obstack_alloc (pool, sizeof *newp);
1991 *newp = *p;
1992 newp->next = NULL;
1993 (*input)->next = newp;
1994 *input = newp;
1995
1996 snprintf (&piece[sizeof "u_pieces?.pieces.p" - 1], 20, "%" PRIu64,
1997 offset);
1998 translate_base_store (pool, indent,
1999 p->byte_size, input, *input, piece, pdepth++);
2000 (*input)->type = loc_fragment;
2001
2002 offset += p->byte_size;
2003 }
2004
2005 (*input)->type = loc_final;
2006 }
2007 else
2008 {
2009 switch (byte_size)
2010 {
2011 case 1:
2012 case 2:
2013 case 4:
2014 case 8:
2015 obstack_printf (pool, "%*s", indent * 2, "");
2016 deref = emit_base_store (pool, byte_size, rvalue, store_loc);
2017 obstack_printf (pool, "\n");
2018 break;
2019
2020 default:
2021 /* Could handle this generating call to memcpy equivalent. */
2022 FAIL (*input, N_("store is larger than base integer types"));
2023 break;
2024 }
2025
2026 struct location *loc = new_synthetic_loc (*input, deref);
2027 loc->type = loc_final;
2028 (*input)->next = loc;
2029 *input = loc;
2030 }
2031 }
2032
2033 /* Translate a fragment to fetch the value of variable or member DIE
2034 at the *INPUT location and store it in rvalue RVALUE. */
2035
2036 void
2037 c_translate_store (struct obstack *pool, int indent,
2038 Dwarf_Addr dwbias __attribute__ ((unused)),
2039 Dwarf_Die *die, Dwarf_Die *typedie,
2040 struct location **input, const char *rvalue)
2041 {
2042 struct location_context *const ctx = (*input)->context;
2043 assert (ctx->pool == pool);
2044
2045 ++indent;
2046
2047 Dwarf_Attribute size_attr;
2048 Dwarf_Word byte_size;
2049 if (dwarf_attr_integrate (die, DW_AT_byte_size, &size_attr) == NULL
2050 || dwarf_formudata (&size_attr, &byte_size) != 0)
2051 byte_size = base_byte_size (typedie, *input);
2052
2053 Dwarf_Attribute encoding_attr;
2054 Dwarf_Word encoding;
2055 if (dwarf_attr_integrate (die, DW_AT_encoding, &encoding_attr) == NULL
2056 || dwarf_formudata (&encoding_attr, &encoding) != 0)
2057 encoding = base_encoding (typedie, *input);
2058 bool signed_p = (encoding == DW_ATE_signed
2059 || encoding == DW_ATE_signed_char);
2060
2061 *input = discontiguify (ctx, indent, *input, byte_size,
2062 max_fetch_size (*input, die));
2063
2064 struct location *store_loc = *input;
2065
2066 if (dwarf_hasattr_integrate (die, DW_AT_bit_offset))
2067 {
2068 /* This is a bit field. Fetch the containing base type into a
2069 temporary variable. */
2070
2071 translate_base_fetch (pool, indent, byte_size, signed_p, input, "tmp", 0);
2072 (*input)->type = loc_fragment;
2073 (*input)->address.declare = "tmp";
2074
2075 Dwarf_Word bit_offset = 0;
2076 Dwarf_Word bit_size = 0;
2077 get_bitfield (*input, die, &bit_offset, &bit_size);
2078
2079 obstack_printf (pool, "%*s"
2080 "store_bitfield (tmp, %s, %" PRIu64 ", %" PRIu64 ");\n",
2081 indent * 2, "", rvalue, bit_offset, bit_size);
2082
2083 struct location *loc = new_synthetic_loc (*input, false);
2084 loc->type = loc_fragment;
2085 (*input)->next = loc;
2086 *input = loc;
2087
2088 /* We have mixed RVALUE into the bits in "tmp".
2089 Now we'll store "tmp" back whence we fetched it. */
2090 rvalue = "tmp";
2091 }
2092
2093 translate_base_store (pool, indent, byte_size, input, store_loc, rvalue, 0);
2094 }
2095
2096 /* Translate a fragment to dereference the given pointer type,
2097 where *INPUT is the location of the pointer with that type.
2098
2099 We chain on a loc_address program that yields this pointer value
2100 (i.e. the location of what it points to). */
2101
2102 void
2103 c_translate_pointer (struct obstack *pool, int indent,
2104 Dwarf_Addr dwbias __attribute__ ((unused)),
2105 Dwarf_Die *typedie, struct location **input)
2106 {
2107 assert (dwarf_tag (typedie) == DW_TAG_pointer_type ||
2108 dwarf_tag (typedie) == DW_TAG_reference_type ||
2109 dwarf_tag (typedie) == DW_TAG_rvalue_reference_type);
2110
2111 Dwarf_Attribute attr_mem;
2112 Dwarf_Word byte_size;
2113 if (dwarf_attr_integrate (typedie, DW_AT_byte_size, &attr_mem) == NULL)
2114 byte_size = 0;
2115 else if (dwarf_formudata (&attr_mem, &byte_size) != 0)
2116 FAIL (*input,
2117 N_("cannot get byte_size attribute for type %s: %s"),
2118 dwarf_diename (typedie) ?: "<anonymous>",
2119 dwarf_errmsg (-1));
2120
2121 if ((*input)->type == loc_implicit_pointer)
2122 {
2123 struct location *const target = (*input)->pointer.target;
2124 const Dwarf_Sword offset = (*input)->pointer.offset;
2125 (*input)->next = target;
2126 if (offset != 0)
2127 translate_offset (indent, NULL, 0, 0, NULL, &(*input)->next, offset);
2128 *input = (*input)->next;
2129 }
2130 else
2131 {
2132 bool signed_p = false; /* XXX: Does not matter? */
2133
2134 translate_base_fetch (pool, indent + 1, byte_size, signed_p, input,
2135 "addr", 0);
2136 (*input)->type = loc_address;
2137 }
2138 }
2139
2140
2141 void
2142 c_translate_addressof (struct obstack *pool, int indent,
2143 Dwarf_Addr dwbias __attribute__ ((unused)),
2144 Dwarf_Die *die,
2145 Dwarf_Die *typedie __attribute__ ((unused)),
2146 struct location **input, const char *target)
2147 {
2148 ++indent;
2149
2150 if (dwarf_hasattr_integrate (die, DW_AT_bit_offset)
2151 || dwarf_hasattr_integrate (die, DW_AT_data_bit_offset))
2152 FAIL (*input, N_("cannot take the address of a bit field"));
2153
2154 switch ((*input)->type)
2155 {
2156 case loc_address:
2157 obstack_printf (pool, "%*s%s = addr;\n", indent * 2, "", target);
2158 (*input)->next = new_synthetic_loc (*input, false);
2159 (*input)->next->type = loc_final;
2160 break;
2161
2162 case loc_register:
2163 FAIL (*input, N_("cannot take address of object in register"));
2164 break;
2165 case loc_noncontiguous:
2166 FAIL (*input, N_("cannot take address of noncontiguous object"));
2167 break;
2168 case loc_value:
2169 FAIL (*input, N_("cannot take address of computed value"));
2170 break;
2171 case loc_constant:
2172 FAIL (*input, N_("cannot take address of constant value"));
2173 break;
2174 case loc_unavailable:
2175 FAIL (*input, N_("cannot take address of unavailable value"));
2176 break;
2177 case loc_implicit_pointer:
2178 FAIL (*input, N_("cannot take address of implicit pointer"));
2179 break;
2180
2181 default:
2182 abort ();
2183 break;
2184 }
2185 }
2186
2187
2188 /* Translate a fragment to write the given pointer value,
2189 where *INPUT is the location of the pointer with that type.
2190 */
2191
2192 void
2193 c_translate_pointer_store (struct obstack *pool, int indent,
2194 Dwarf_Addr dwbias __attribute__ ((unused)),
2195 Dwarf_Die *typedie, struct location **input,
2196 const char *rvalue)
2197 {
2198 assert (dwarf_tag (typedie) == DW_TAG_pointer_type);
2199
2200 Dwarf_Attribute attr_mem;
2201 Dwarf_Word byte_size;
2202 if (dwarf_attr_integrate (typedie, DW_AT_byte_size, &attr_mem) == NULL)
2203 byte_size = 0;
2204 else if (dwarf_formudata (&attr_mem, &byte_size) != 0)
2205 FAIL (*input,
2206 N_("cannot get byte_size attribute for type %s: %s"),
2207 dwarf_diename (typedie) ?: "<anonymous>",
2208 dwarf_errmsg (-1));
2209
2210 translate_base_store (pool, indent + 1, byte_size,
2211 input, *input, rvalue, 0);
2212
2213 // XXX: what about multiple-location lvalues?
2214 }
2215
2216 /* Determine the element stride of a pointer to a type. */
2217 static Dwarf_Word
2218 pointer_stride (Dwarf_Die *typedie, struct location *origin)
2219 {
2220 Dwarf_Attribute attr_mem;
2221 Dwarf_Die die_mem = *typedie;
2222 int typetag = dwarf_tag (&die_mem);
2223 while (typetag == DW_TAG_typedef ||
2224 typetag == DW_TAG_const_type ||
2225 typetag == DW_TAG_volatile_type)
2226 {
2227 if (dwarf_attr_integrate (&die_mem, DW_AT_type, &attr_mem) == NULL
2228 || dwarf_formref_die (&attr_mem, &die_mem) == NULL)
2229 //TRANSLATORS: This refers to the basic type, (stripped of const/volatile/etc.)
2230 FAIL (origin, N_("cannot get inner type of type %s: %s"),
2231 dwarf_diename (&die_mem) ?: "<anonymous>",
2232 dwarf_errmsg (-1));
2233 typetag = dwarf_tag (&die_mem);
2234 }
2235
2236 if (dwarf_attr_integrate (&die_mem, DW_AT_byte_size, &attr_mem) != NULL)
2237 {
2238 Dwarf_Word stride;
2239 if (dwarf_formudata (&attr_mem, &stride) == 0)
2240 return stride;
2241 FAIL (origin,
2242 N_("cannot get byte_size attribute for array element type %s: %s"),
2243 dwarf_diename (&die_mem) ?: "<anonymous>",
2244 dwarf_errmsg (-1));
2245 }
2246
2247 FAIL (origin, N_("confused about array element size"));
2248 return 0;
2249 }
2250
2251 /* Determine the element stride of an array type. */
2252 static Dwarf_Word
2253 array_stride (Dwarf_Die *typedie, struct location *origin)
2254 {
2255 Dwarf_Attribute attr_mem;
2256 if (dwarf_attr_integrate (typedie, DW_AT_byte_stride, &attr_mem) != NULL)
2257 {
2258 Dwarf_Word stride;
2259 if (dwarf_formudata (&attr_mem, &stride) == 0)
2260 return stride;
2261 FAIL (origin, N_("cannot get byte_stride attribute array type %s: %s"),
2262 dwarf_diename (typedie) ?: "<anonymous>",
2263 dwarf_errmsg (-1));
2264 }
2265
2266 Dwarf_Die die_mem;
2267 if (dwarf_attr_integrate (typedie, DW_AT_type, &attr_mem) == NULL
2268 || dwarf_formref_die (&attr_mem, &die_mem) == NULL)
2269 FAIL (origin, N_("cannot get element type of array type %s: %s"),
2270 dwarf_diename (typedie) ?: "<anonymous>",
2271 dwarf_errmsg (-1));
2272
2273 return pointer_stride (&die_mem, origin);
2274 }
2275
2276 static void
2277 translate_array (struct obstack *pool, int indent,
2278 Dwarf_Die *anydie, Dwarf_Word stride,
2279 struct location **input,
2280 const char *idx, Dwarf_Word const_idx)
2281 {
2282 ++indent;
2283
2284 struct location *loc = *input;
2285 while (loc->type == loc_noncontiguous)
2286 {
2287 if (idx != NULL)
2288 FAIL (*input, N_("cannot dynamically index noncontiguous array"));
2289 else
2290 {
2291 Dwarf_Word offset = const_idx * stride;
2292 struct location *piece = loc->pieces;
2293 while (piece != NULL && offset >= piece->byte_size)
2294 {
2295 offset -= piece->byte_size;
2296 piece = piece->next;
2297 }
2298 if (piece == NULL)
2299 //TRANSLATORS: The index is constant
2300 FAIL (*input, N_("constant index is outside noncontiguous array"));
2301 if (offset % stride != 0 || piece->byte_size < stride)
2302 FAIL (*input, N_("noncontiguous array splits elements"));
2303 const_idx = offset / stride;
2304 loc = piece;
2305 }
2306 }
2307
2308 switch (loc->type)
2309 {
2310 case loc_address:
2311 ++indent;
2312 if (idx != NULL)
2313 obstack_printf (pool, "%*saddr += %s * " UFORMAT ";\n",
2314 indent * 2, "", idx, stride);
2315 else
2316 obstack_printf (pool, "%*saddr += " UFORMAT " * " UFORMAT ";\n",
2317 indent * 2, "", const_idx, stride);
2318 loc = new_synthetic_loc (loc, false);
2319 break;
2320
2321 case loc_register:
2322 if (idx != NULL)
2323 FAIL (*input, N_("cannot index array stored in a register"));
2324 else if (const_idx > max_fetch_size (loc, anydie) / stride)
2325 FAIL (*input, N_("constant index is outside array held in register"));
2326 else
2327 {
2328 loc->reg.offset += const_idx * stride;
2329 return;
2330 }
2331 break;
2332
2333 case loc_constant:
2334 if (idx != NULL)
2335 FAIL (*input, N_("cannot index into constant value"));
2336 else if (const_idx > loc->byte_size / stride)
2337 //TRANSLATORS: The index is constant
2338 FAIL (*input, N_("constant index is outside constant array value"));
2339 else
2340 {
2341 loc->byte_size = stride;
2342 loc->constant_block += const_idx * stride;
2343 return;
2344 };
2345 break;
2346
2347 case loc_implicit_pointer:
2348 if (idx != NULL)
2349 FAIL (*input, N_("cannot index into implicit pointer"));
2350 else
2351 loc->pointer.offset += const_idx * stride;
2352 break;
2353
2354 case loc_value:
2355 if (idx != NULL || const_idx != 0)
2356 FAIL (*input, N_("cannot index into computed value"));
2357 break;
2358
2359 case loc_unavailable:
2360 if (idx != NULL || const_idx != 0)
2361 FAIL (*input, N_("cannot index into unavailable value"));
2362 break;
2363
2364 default:
2365 abort ();
2366 break;
2367 }
2368
2369 (*input)->next = loc;
2370 *input = (*input)->next;
2371 }
2372
2373 void
2374 c_translate_array (struct obstack *pool, int indent,
2375 Dwarf_Addr dwbias __attribute__ ((unused)),
2376 Dwarf_Die *typedie, struct location **input,
2377 const char *idx, Dwarf_Word const_idx)
2378 {
2379 assert (dwarf_tag (typedie) == DW_TAG_array_type ||
2380 dwarf_tag (typedie) == DW_TAG_pointer_type);
2381
2382 return translate_array (pool, indent, typedie,
2383 array_stride (typedie, *input),
2384 input, idx, const_idx);
2385 }
2386
2387 void
2388 c_translate_array_pointer (struct obstack *pool, int indent,
2389 Dwarf_Die *typedie, struct location **input,
2390 const char *idx, Dwarf_Word const_idx)
2391 {
2392 return translate_array (pool, indent, typedie,
2393 pointer_stride (typedie, *input),
2394 input, idx, const_idx);
2395 }
2396 \f
2397 /* Emitting C code for finalized fragments. */
2398
2399 #define emit(fmt, ...) fprintf (out, fmt, ## __VA_ARGS__)
2400
2401 /* Open a block with a comment giving the original DWARF expression. */
2402 static void
2403 emit_header (FILE *out, struct location *loc, unsigned int hindent)
2404 {
2405 if (loc->ops == NULL)
2406 emit ("%*s{ // synthesized\n", hindent * 2, "");
2407 else
2408 {
2409 emit ("%*s{ // DWARF expression:", hindent * 2, "");
2410 size_t i;
2411 for (i = 0; i < loc->nops; ++i)
2412 {
2413 emit (" %#x", loc->ops[i].atom);
2414 if (loc->ops[i].number2 == 0)
2415 {
2416 if (loc->ops[i].number != 0)
2417 emit ("(%" PRId64 ")", loc->ops[i].number);
2418 }
2419 else
2420 emit ("(%" PRId64 ",%" PRId64 ")",
2421 loc->ops[i].number, loc->ops[i].number2);
2422 }
2423 emit ("\n");
2424 }
2425 }
2426
2427 /* Emit a code fragment to assign the target variable to a register value. */
2428 static void
2429 emit_loc_register (FILE *out, struct location *loc, unsigned int indent,
2430 const char *target)
2431 {
2432 assert (loc->type == loc_register);
2433
2434 if (loc->reg.offset != 0)
2435 FAIL (loc, N_("cannot handle offset into register in fetch"));
2436
2437 emit ("%*s%s = fetch_register (%u);\n",
2438 indent * 2, "", target, loc->reg.regno);
2439 }
2440
2441 /* Emit a code fragment to assign the target variable to an address. */
2442 static void
2443 emit_loc_address (FILE *out, struct location *loc, unsigned int indent,
2444 const char *target)
2445 {
2446 assert (loc->type == loc_address || loc->type == loc_value);
2447
2448 if (loc->address.stack_depth == 0)
2449 /* Synthetic program. */
2450 emit ("%s", loc->address.program);
2451 else
2452 {
2453 emit ("%*s{\n", indent * 2, "");
2454 emit ("%*s%s " STACKFMT, (indent + 1) * 2, "",
2455 stack_slot_type (loc, false), 0);
2456 unsigned i;
2457 for (i = 1; i < loc->address.stack_depth; ++i)
2458 emit (", " STACKFMT, i);
2459 emit (";\n");
2460
2461 emit ("%s%*s%s = " STACKFMT ";\n", loc->address.program,
2462 (indent + 1) * 2, "", target, 0);
2463 emit ("%*s}\n", indent * 2, "");
2464 }
2465 }
2466
2467 /* Emit a code fragment to declare the target variable and
2468 assign it to an address-sized value. */
2469 static void
2470 emit_loc_value (FILE *out, struct location *loc, unsigned int indent,
2471 const char *target, bool declare,
2472 bool *used_deref, unsigned int *max_stack)
2473 {
2474 if (declare)
2475 emit ("%*s%s %s;\n", indent * 2, "", stack_slot_type (loc, false), target);
2476
2477 emit_header (out, loc, indent++);
2478
2479 switch (loc->type)
2480 {
2481 default:
2482 abort ();
2483 break;
2484
2485 case loc_register:
2486 emit_loc_register (out, loc, indent, target);
2487 break;
2488
2489 case loc_address:
2490 case loc_value:
2491 emit_loc_address (out, loc, indent, target);
2492 *used_deref = *used_deref || loc->address.used_deref;
2493 if (loc->address.stack_depth > *max_stack)
2494 *max_stack = loc->address.stack_depth;
2495 break;
2496 }
2497
2498 emit ("%*s}\n", --indent * 2, "");
2499 }
2500
2501 bool
2502 c_emit_location (FILE *out, struct location *loc, int indent,
2503 unsigned int *max_stack)
2504 {
2505 emit ("%*s{\n", indent * 2, "");
2506
2507 bool declared_addr = false;
2508 struct location *l;
2509 for (l = loc; l != NULL; l = l->next)
2510 switch (l->type)
2511 {
2512 case loc_decl:
2513 emit ("%s", l->address.program);
2514 break;
2515
2516 case loc_address:
2517 case loc_value:
2518 if (declared_addr)
2519 break;
2520 declared_addr = true;
2521 l->address.declare = "addr";
2522 case loc_fragment:
2523 case loc_final:
2524 if (l->address.declare != NULL)
2525 {
2526 if (l->byte_size == 0 || l->byte_size == (Dwarf_Word) -1)
2527 emit ("%*s%s %s;\n", (indent + 1) * 2, "",
2528 stack_slot_type (l, false), l->address.declare);
2529 else
2530 emit ("%*suint%" PRIu64 "_t %s;\n", (indent + 1) * 2, "",
2531 l->byte_size * 8, l->address.declare);
2532 }
2533
2534 default:
2535 break;
2536 }
2537
2538 bool deref = false;
2539 *max_stack = 0;
2540
2541 if (loc->frame_base != NULL)
2542 emit_loc_value (out, loc->frame_base, indent, "frame_base", true,
2543 &deref, max_stack);
2544
2545 for (; loc->next != NULL; loc = loc->next)
2546 switch (loc->type)
2547 {
2548 case loc_address:
2549 case loc_value:
2550 /* Emit the program fragment to calculate the address. */
2551 emit_loc_value (out, loc, indent + 1, "addr", false, &deref, max_stack);
2552 break;
2553
2554 case loc_fragment:
2555 emit ("%s", loc->address.program);
2556 deref = deref || loc->address.used_deref;
2557 break;
2558
2559 case loc_decl:
2560 case loc_register:
2561 case loc_noncontiguous:
2562 case loc_constant:
2563 case loc_implicit_pointer:
2564 /* These don't produce any code directly.
2565 The next address/final record incorporates the value. */
2566 break;
2567
2568 case loc_final: /* Should be last in chain! */
2569 default:
2570 abort ();
2571 break;
2572 }
2573
2574 if (loc->type != loc_final) /* Unfinished chain. */
2575 abort ();
2576
2577 emit ("%s%*s}\n", loc->address.program, indent * 2, "");
2578
2579 if (loc->address.stack_depth > *max_stack)
2580 *max_stack = loc->address.stack_depth;
2581
2582 return deref || loc->address.used_deref;
2583 }
2584
2585 #undef emit
2586
2587 /* vim: set sw=2 ts=8 cino=>4,n-2,{2,^-2,t0,(0,u0,w1,M1 : */
This page took 0.14528 seconds and 5 git commands to generate.