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