9 #include <elfutils/libdw.h>
15 #include <elfutils/libdw.h>
16 #ifdef HAVE_ELFUTILS_VERSION_H
17 #include <elfutils/version.h>
20 #if !defined(_ELFUTILS_PREREQ)
21 // make a dummy PREREQ check for elfutils < 0.138
22 #define _ELFUTILS_PREREQ(major, minor) (0 >= 1)
25 #if !_ELFUTILS_PREREQ (0,142)
26 #define DW_TAG_rvalue_reference_type 0x42
27 #define DW_OP_implicit_value 0x9e
28 #define DW_OP_stack_value 0x9f
33 /* NB: PR10601 may make one suspect that intptr_t and uintptr_t aren't
34 right, for example on a 64-bit kernel targeting a 32-bit userspace
35 process. At least these types are always at least as wide as
36 userspace (since 64-bit userspace doesn't run on a 32-bit kernel).
37 So as long as deref() and {fetch,store}_register() widen/narrow
38 their underlying values to these, there should be no problem. */
40 #define STACK_TYPE "intptr_t" /* Must be the signed type. */
41 #define UTYPE "uintptr_t" /* Must be the unsigned type. */
42 #define SFORMAT "%" PRId64 "L"
43 #define UFORMAT "%" PRIu64 "UL"
44 #define AFORMAT "%#" PRIx64 "UL"
45 #define STACKFMT "s%u"
49 struct location
*next
;
51 void (*fail
) (void *arg
, const char *fmt
, ...)
52 __attribute__ ((noreturn
, format (printf
, 2, 3)));
54 void (*emit_address
) (void *fail_arg
, struct obstack
*, Dwarf_Addr
);
63 loc_address
, loc_register
, loc_noncontiguous
, loc_value
,
64 loc_constant
, loc_decl
, loc_fragment
, loc_final
66 struct location
*frame_base
;
69 struct /* loc_address, loc_value, loc_fragment, loc_final */
71 const char *declare
; /* Temporary that needs declared. */
72 char *program
; /* C fragment, leaves address in s0. */
73 unsigned int stack_depth
; /* Temporaries "s0..<N>" used by it. */
74 bool used_deref
; /* Program uses "deref" macro. */
76 struct /* loc_register */
81 struct location
*pieces
; /* loc_noncontiguous */
82 const void *constant_block
; /* loc_constant */
86 /* Select the C type to use in the emitted code to represent slots in the
87 DWARF expression stack. For really proper semantics this should be the
88 target address size. */
90 stack_slot_type (struct location
*context
__attribute__ ((unused
)), bool sign
)
92 return sign
? STACK_TYPE
: UTYPE
;
95 static struct location
*
96 alloc_location (struct obstack
*pool
, struct location
*origin
)
98 struct location
*loc
= obstack_alloc (pool
, sizeof *loc
);
99 loc
->fail
= origin
->fail
;
100 loc
->fail_arg
= origin
->fail_arg
;
101 loc
->emit_address
= origin
->emit_address
;
103 loc
->frame_base
= NULL
;
109 #define FAIL(loc, fmt, ...) \
110 (*(loc)->fail) ((loc)->fail_arg, fmt, ## __VA_ARGS__)
113 default_emit_address (void *fail_arg
__attribute__ ((unused
)),
114 struct obstack
*pool
, Dwarf_Addr address
)
116 obstack_printf (pool
, AFORMAT
, address
);
119 /* Synthesize a new loc_address using the program on the obstack. */
120 static struct location
*
121 new_synthetic_loc (struct obstack
*pool
, struct location
*origin
, bool deref
)
123 obstack_1grow (pool
, '\0');
124 char *program
= obstack_finish (pool
);
126 struct location
*loc
= alloc_location (pool
, origin
);
129 loc
->type
= loc_address
;
130 loc
->address
.program
= program
;
131 loc
->address
.stack_depth
= 0;
132 loc
->address
.declare
= NULL
;
133 loc
->address
.used_deref
= deref
;
135 if (origin
->type
== loc_register
)
137 loc
->ops
= origin
->ops
;
138 loc
->nops
= origin
->nops
;
150 /* Die in the middle of an expression. */
151 static struct location
*
152 lose (struct location
*loc
,
153 const char *failure
, const Dwarf_Op
*lexpr
, size_t i
)
155 FAIL (loc
, N_("%s in DWARF expression [%Zu] at %" PRIu64
156 " (%#x: %" PRId64
", %" PRId64
")"),
157 failure
, i
, lexpr
[i
].offset
,
158 lexpr
[i
].atom
, lexpr
[i
].number
, lexpr
[i
].number2
);
162 /* Translate a (constrained) DWARF expression into C code
163 emitted to the obstack POOL. INDENT is the number of indentation levels.
164 ADDRBIAS is the difference between runtime and Dwarf info addresses.
165 INPUT is null or an expression to be initially pushed on the stack.
166 If NEED_FB is null, fail on DW_OP_fbreg, else set *NEED_FB to true
167 and emit "frame_base" for it. On success, set *MAX_STACK to the number
168 of stack slots required. On failure, set *LOSER to the index in EXPR
169 of the operation we could not handle.
171 Returns a failure message or null for success. */
174 translate (struct obstack
*pool
, int indent
, Dwarf_Addr addrbias
,
175 Dwarf_Attribute
*attr
, const Dwarf_Op
*expr
, const size_t len
,
176 struct location
*input
,
177 bool *need_fb
, size_t *loser
,
178 struct location
*loc
)
183 #define DIE(msg) return (*loser = i, N_(msg))
185 #define emit(fmt, ...) obstack_printf (pool, fmt, ## __VA_ARGS__)
187 unsigned int stack_depth
;
188 unsigned int max_stack
= 0;
189 inline void deepen (void)
191 if (stack_depth
== max_stack
)
196 if (stack_depth > 0) \
198 else if (tos_register != -1) \
199 fetch_tos_register (); \
202 int var = stack_depth
203 #define PUSH (deepen (), stack_depth++)
204 #define STACK(idx) (stack_depth - 1 - (idx))
206 /* Don't put stack operations in the arguments to this. */
207 #define push(fmt, ...) \
208 emit ("%*s" STACKFMT " = " fmt ";\n", indent * 2, "", PUSH, ## __VA_ARGS__)
211 inline void fetch_tos_register (void)
214 emit ("%*s" STACKFMT
" = fetch_register (%d);\n",
215 indent
* 2, "", stack_depth
, tos_register
);
220 Dwarf_Block implicit_value
;
223 /* Initialize our state for handling each new piece. */
229 implicit_value
.data
= NULL
;
245 tos_register
= input
->reg
.regno
;
255 inline const char *finish (struct location
*piece
)
258 DIE ("multiple values left on stack");
259 if (stack_depth
== 1)
261 obstack_1grow (pool
, '\0');
262 char *program
= obstack_finish (pool
);
263 if (implicit_value
.data
== NULL
)
265 piece
->type
= tos_value
? loc_value
: loc_address
;
266 piece
->address
.declare
= NULL
;
267 piece
->address
.program
= program
;
268 piece
->address
.stack_depth
= max_stack
;
269 piece
->address
.used_deref
= used_deref
;
273 piece
->type
= loc_constant
;
274 piece
->byte_size
= implicit_value
.length
;
275 piece
->constant_block
= implicit_value
.data
;
278 else if (tos_register
== -1)
279 DIE ("stack underflow");
280 else if (obstack_object_size (pool
) != 0)
281 DIE ("register value must stand alone in location expression");
284 piece
->type
= loc_register
;
285 piece
->reg
.regno
= tos_register
;
286 piece
->reg
.offset
= 0;
292 struct location
*pieces
= NULL
, **tailpiece
= &pieces
;
293 size_t piece_expr_start
= 0;
294 Dwarf_Word piece_total_bytes
= 0;
295 for (i
= 0; i
< len
; ++i
)
301 inline bool more_ops (void)
303 return (expr
[i
].atom
!= DW_OP_nop
304 && expr
[i
].atom
!= DW_OP_piece
305 && expr
[i
].atom
!= DW_OP_bit_piece
);
308 if (tos_value
&& more_ops ())
309 DIE ("operations follow DW_OP_stack_value");
311 if (implicit_value
.data
!= NULL
&& more_ops ())
312 DIE ("operations follow DW_OP_implicit_value");
314 switch (expr
[i
].atom
)
316 /* Basic stack operations. */
325 unsigned int tos
= STACK (0);
326 push (STACKFMT
, tos
);
333 emit ("%*s/* drop " STACKFMT
"*/\n", indent
* 2, "", ignore
);
340 if (sp
>= stack_depth
)
353 deepen (); /* Use a temporary slot. */
355 STACKFMT
" = " STACKFMT
", "
356 STACKFMT
" = " STACKFMT
", "
357 STACKFMT
" = " STACKFMT
";\n",
359 STACK (-1), STACK (0),
360 STACK (0), STACK (1),
361 STACK (1), STACK (-1));
367 deepen (); /* Use a temporary slot. */
369 STACKFMT
" = " STACKFMT
", "
370 STACKFMT
" = " STACKFMT
", "
371 STACKFMT
" = " STACKFMT
", "
372 STACKFMT
" = " STACKFMT
";\n",
374 STACK (-1), STACK (0),
375 STACK (0), STACK (1),
376 STACK (1), STACK (2),
377 STACK (3), STACK (-1));
381 /* Control flow operations. */
384 Dwarf_Off target
= expr
[i
].offset
+ 3 + expr
[i
].number
;
385 while (i
+ 1 < len
&& expr
[i
+ 1].offset
< target
)
387 if (expr
[i
+ 1].offset
!= target
)
388 DIE ("invalid skip target");
393 DIE ("conditional branches not supported");
401 push ("deref (sizeof (void *), " STACKFMT
")", addr
);
406 case DW_OP_deref_size
:
409 push ("deref (" UFORMAT
", " STACKFMT
")",
410 expr
[i
].number
, addr
);
419 push ("xderef (sizeof (void *), " STACKFMT
", " STACKFMT
")",
425 case DW_OP_xderef_size
:
429 push ("xderef (" UFORMAT
", " STACKFMT
", " STACKFMT
")",
430 expr
[i
].number
, addr
, as
);
435 /* Constant-value operations. */
438 emit ("%*s" STACKFMT
" = ", indent
* 2, "", PUSH
);
439 (*loc
->emit_address
) (loc
->fail_arg
, pool
,
440 addrbias
+ expr
[i
].number
);
444 case DW_OP_lit0
... DW_OP_lit31
:
445 value
= expr
[i
].atom
- DW_OP_lit0
;
458 value
= expr
[i
].number
;
460 push (SFORMAT
, value
);
463 /* Arithmetic operations. */
464 #define UNOP(dw_op, c_op) \
465 case DW_OP_##dw_op: \
468 push ("%s (" STACKFMT ")", #c_op, tos); \
471 #define BINOP(dw_op, c_op) \
472 case DW_OP_##dw_op: \
476 push (STACKFMT " %s " STACKFMT, a, #c_op, b); \
494 /* Comparisons are binary operators too. */
509 push ("(%s) " STACKFMT
" >> (%s)" STACKFMT
,
510 stack_slot_type (loc
, true), a
,
511 stack_slot_type (loc
, true), b
);
515 case DW_OP_plus_uconst
:
518 push (STACKFMT
" + " UFORMAT
, x
, expr
[i
].number
);
523 /* Register-relative addressing. */
524 case DW_OP_breg0
... DW_OP_breg31
:
525 reg
= expr
[i
].atom
- DW_OP_breg0
;
526 value
= expr
[i
].number
;
530 reg
= expr
[i
].number
;
531 value
= expr
[i
].number2
;
533 push ("fetch_register (%u) + " SFORMAT
, reg
, value
);
538 DIE ("DW_OP_fbreg from DW_AT_frame_base");
540 push ("frame_base + " SFORMAT
, expr
[i
].number
);
543 /* Direct register contents. */
544 case DW_OP_reg0
... DW_OP_reg31
:
545 reg
= expr
[i
].atom
- DW_OP_reg0
;
549 reg
= expr
[i
].number
;
557 /* If this ever happens we could copy the program. */
558 DIE ("DW_OP_piece left multiple values on stack");
561 /* The obstack has a pending program for loc_address,
562 so we must finish that piece off before we can
564 struct location temp_piece
=
567 .fail_arg
= loc
->fail_arg
,
568 .emit_address
= loc
->emit_address
,
570 .ops
= &expr
[piece_expr_start
],
571 .nops
= i
- piece_expr_start
,
573 const char *failure
= finish (&temp_piece
);
577 struct location
*piece
= obstack_alloc (pool
, sizeof *piece
);
580 piece_expr_start
= i
+ 1;
582 piece_total_bytes
+= piece
->byte_size
= expr
[i
].number
;
585 tailpiece
= &piece
->next
;
588 /* Reset default conditions for handling the next piece. */
593 case DW_OP_stack_value
:
595 DIE ("DW_OP_stack_value left multiple values on stack");
598 /* Fetch a register to top of stack, or check for underflow.
599 Then mark the TOS as being a value. */
607 case DW_OP_implicit_value
:
609 DIE ("DW_OP_implicit_value used in invalid context"
610 " (no DWARF attribute, ABI return value location?)");
612 /* It's supposed to appear by itself, except for DW_OP_piece. */
613 if (stack_depth
!= 0)
614 DIE ("DW_OP_implicit_value follows stack operations");
616 #if _ELFUTILS_PREREQ (0, 143)
617 if (dwarf_getlocation_implicit_value (attr
, (Dwarf_Op
*) &expr
[i
],
618 &implicit_value
) != 0)
619 DIE ("dwarf_getlocation_implicit_value failed");
621 /* Fake top of stack: implicit_value being set marks it. */
626 DIE ("DW_OP_implicit_value not supported");
629 case DW_OP_call_frame_cfa
:
630 // We pick this out when processing DW_AT_frame_base in
631 // so it really shouldn't turn up here.
633 DIE ("DW_OP_call_frame_cfa while processing frame base");
635 DIE ("DW_OP_call_frame_cfa not expected outside DW_AT_frame_base");
638 case DW_OP_push_object_address
:
639 DIE ("XXX DW_OP_push_object_address");
643 DIE ("unrecognized operation");
651 if (piece_expr_start
!= i
)
652 DIE ("extra operations after last DW_OP_piece");
654 loc
->type
= loc_noncontiguous
;
655 loc
->pieces
= pieces
;
656 loc
->byte_size
= piece_total_bytes
;
661 DIE ("stack underflow");
671 /* Translate a location starting from an address or nothing. */
672 static struct location
*
673 location_from_address (struct obstack
*pool
,
674 void (*fail
) (void *arg
, const char *fmt
, ...)
675 __attribute__ ((noreturn
, format (printf
, 2, 3))),
677 void (*emit_address
) (void *fail_arg
,
678 struct obstack
*, Dwarf_Addr
),
679 int indent
, Dwarf_Addr dwbias
,
680 Dwarf_Attribute
*attr
,
681 const Dwarf_Op
*expr
, size_t len
, Dwarf_Addr address
,
682 struct location
**input
, Dwarf_Attribute
*fb_attr
,
683 const Dwarf_Op
*cfa_ops
)
685 struct location
*loc
= obstack_alloc (pool
, sizeof *loc
);
686 loc
->fail
= *input
== NULL
? fail
: (*input
)->fail
;
687 loc
->fail_arg
= *input
== NULL
? fail_arg
: (*input
)->fail_arg
;
688 loc
->emit_address
= *input
== NULL
? emit_address
: (*input
)->emit_address
;
690 loc
->frame_base
= NULL
;
694 bool need_fb
= false;
696 const char *failure
= translate (pool
, indent
+ 1, dwbias
, attr
, expr
, len
,
697 *input
, &need_fb
, &loser
, loc
);
699 return lose (loc
, failure
, expr
, loser
);
704 /* The main expression uses DW_OP_fbreg, so we need to compute
705 the DW_AT_frame_base attribute expression's value first. */
708 FAIL (loc
, N_("required DW_AT_frame_base attribute not supplied"));
712 switch (dwarf_getlocation_addr (fb_attr
, address
- dwbias
,
713 &fb_expr
, &fb_len
, 1))
715 case 1: /* Should always happen. */
717 goto fb_inaccessible
;
720 default: /* Shouldn't happen. */
722 FAIL (loc
, N_("dwarf_getlocation_addr (form %#x): %s"),
723 dwarf_whatform (fb_attr
), dwarf_errmsg (-1));
726 case 0: /* Shouldn't happen. */
728 FAIL (loc
, N_("DW_AT_frame_base not accessible at this address"));
732 // If it is DW_OP_call_frame_cfa then get cfi cfa ops.
733 const Dwarf_Op
* fb_ops
;
734 if (fb_len
== 1 && fb_expr
[0].atom
== DW_OP_call_frame_cfa
)
737 FAIL (loc
, N_("No cfa_ops supplied, but needed by DW_OP_call_frame_cfa"));
743 loc
->frame_base
= alloc_location (pool
, loc
);
744 failure
= translate (pool
, indent
+ 1, dwbias
, attr
, fb_ops
, fb_len
, NULL
,
745 NULL
, &loser
, loc
->frame_base
);
747 return lose (loc
, failure
, fb_expr
, loser
);
751 (*input
)->next
= loc
;
757 /* Translate a location starting from a non-address "on the top of the
758 stack". The *INPUT location is a register name or noncontiguous
759 object specification, and this expression wants to find the "address"
760 of an object (or the actual value) relative to that "address". */
762 static struct location
*
763 location_relative (struct obstack
*pool
,
764 int indent
, Dwarf_Addr dwbias
, Dwarf_Attribute
*attr
,
765 const Dwarf_Op
*expr
, size_t len
, Dwarf_Addr address
,
766 struct location
**input
, Dwarf_Attribute
*fb_attr
,
767 const Dwarf_Op
*cfa_ops
)
769 Dwarf_Sword
*stack
= NULL
;
770 unsigned int stack_depth
= 0, max_stack
= 0;
771 inline void deepen (void)
773 if (stack_depth
== max_stack
)
776 obstack_blank (pool
, sizeof stack
[0]);
777 stack
= (void *) obstack_base (pool
);
782 if (stack_depth > 0) \
786 int var = stack_depth
787 #define PUSH (deepen (), stack_depth++)
788 #define STACK(idx) (stack_depth - 1 - (idx))
789 #define STACKWORD(idx) stack[STACK (idx)]
791 /* Don't put stack operations in the arguments to this. */
792 #define push(value) (stack[PUSH] = (value))
794 const char *failure
= NULL
;
795 #define DIE(msg) do { failure = N_(msg); goto fail; } while (0)
797 struct location
*head
= NULL
;
799 for (i
= 0; i
< len
; ++i
)
804 switch (expr
[i
].atom
)
806 /* Basic stack operations. */
815 unsigned int tos
= STACK (0);
823 else if (*input
!= NULL
)
824 /* Mark that we have consumed the input. */
827 /* Hits if cleared above, or if we had no input at all. */
834 if (sp
>= stack_depth
)
847 deepen (); /* Use a temporary slot. */
848 STACKWORD (-1) = STACKWORD (0);
849 STACKWORD (0) = STACKWORD (1);
850 STACKWORD (1) = STACKWORD (-1);
856 deepen (); /* Use a temporary slot. */
857 STACKWORD (-1) = STACKWORD (0);
858 STACKWORD (0) = STACKWORD (1);
859 STACKWORD (2) = STACKWORD (2);
860 STACKWORD (2) = STACKWORD (-1);
864 /* Control flow operations. */
868 if (stack
[taken
] == 0)
875 Dwarf_Off target
= expr
[i
].offset
+ 3 + expr
[i
].number
;
876 while (i
+ 1 < len
&& expr
[i
+ 1].offset
< target
)
878 if (expr
[i
+ 1].offset
!= target
)
879 DIE ("invalid skip target");
885 case DW_OP_deref_size
:
887 case DW_OP_xderef_size
:
889 /* Register-relative addressing. */
890 case DW_OP_breg0
... DW_OP_breg31
:
894 /* This started from a register, but now it's following a pointer.
895 So we can do the translation starting from address here. */
896 return location_from_address (pool
, NULL
, NULL
, NULL
, indent
, dwbias
,
897 attr
, expr
, len
, address
, input
, fb_attr
,
901 /* Constant-value operations. */
903 DIE ("static calculation depends on load-time address");
904 push (dwbias
+ expr
[i
].number
);
907 case DW_OP_lit0
... DW_OP_lit31
:
908 value
= expr
[i
].atom
- DW_OP_lit0
;
921 value
= expr
[i
].number
;
926 /* Arithmetic operations. */
927 #define UNOP(dw_op, c_op) \
928 case DW_OP_##dw_op: \
931 push (c_op (stack[tos])); \
934 #define BINOP(dw_op, c_op) \
935 case DW_OP_##dw_op: \
939 push (stack[a] c_op stack[b]); \
943 #define op_abs(x) (x < 0 ? -x : x)
956 /* Comparisons are binary operators too. */
971 push ((Dwarf_Word
) stack
[a
] >> (Dwarf_Word
) stack
[b
]);
975 /* Simple addition we may be able to handle relative to
976 the starting register name. */
989 case DW_OP_plus_uconst
:
990 value
= expr
[i
].number
;
994 /* It's just private diddling after all. */
996 push (stack
[a
] + value
);
1002 /* This is the primary real-world case: the expression takes
1003 the input address and adds a constant offset. */
1005 while ((*input
)->type
== loc_noncontiguous
)
1007 /* We are starting from a noncontiguous object (DW_OP_piece).
1008 Find the piece we want. */
1010 struct location
*piece
= (*input
)->pieces
;
1011 while (piece
!= NULL
&& value
>= piece
->byte_size
)
1013 value
-= piece
->byte_size
;
1014 piece
= piece
->next
;
1017 DIE ("offset outside available pieces");
1022 switch ((*input
)->type
)
1026 /* The piece we want is actually in memory. Use the same
1027 program to compute the address from the preceding input. */
1029 struct location
*loc
= alloc_location (pool
, *input
);
1033 (*input
)->next
= loc
;
1036 /* The piece addresses exactly where we want to go. */
1042 /* Add a second fragment to offset the piece address. */
1043 obstack_printf (pool
, "%*saddr += " SFORMAT
"\n",
1044 indent
* 2, "", value
);
1045 *input
= loc
->next
= new_synthetic_loc (pool
, *input
,
1051 /* This expression keeps going, but further
1052 computations now have an address to start with.
1053 So we can punt to the address computation generator. */
1054 loc
= location_from_address (pool
, NULL
, NULL
, NULL
,
1055 indent
, dwbias
, attr
,
1056 &expr
[i
+ 1], len
- i
- 1,
1057 address
, input
, fb_attr
,
1063 /* That's all she wrote. */
1068 /* This piece (or the whole struct) fits in a register. */
1069 (*input
)->reg
.offset
+= value
;
1070 return head
?: *input
;
1073 /* This piece has a constant value. */
1074 if (value
>= (*input
)->byte_size
)
1075 DIE ("offset outside available constant block");
1076 (*input
)->constant_block
+= value
;
1077 (*input
)->byte_size
-= value
;
1078 return head
?: *input
;
1081 /* The piece we want is part of a computed value! */
1082 /* XXX implement me! */
1089 /* Direct register contents. */
1090 case DW_OP_reg0
... DW_OP_reg31
:
1095 /* Special magic. */
1097 DIE ("DW_OP_piece");
1100 case DW_OP_push_object_address
:
1101 DIE ("XXX DW_OP_push_object_address");
1105 DIE ("unrecognized operation");
1110 if (stack_depth
> 1)
1111 DIE ("multiple values left on stack");
1113 if (stack_depth
> 0) /* stack_depth == 1 */
1116 DIE ("multiple values left on stack");
1118 /* Could handle this if it ever actually happened. */
1119 DIE ("relative expression computed constant");
1126 DIE ("stack underflow");
1128 DIE ("cannot handle location expression");
1131 return lose (*input
, failure
, expr
, i
);
1135 /* Translate a C fragment for the location expression, using *INPUT
1136 as the starting location, begin from scratch if *INPUT is null.
1137 If DW_OP_fbreg is used, it may have a subfragment computing from
1138 the FB_ATTR location expression.
1140 On errors, call FAIL and never return. On success, return the
1141 first fragment created, which is also chained onto (*INPUT)->next.
1142 *INPUT is then updated with the new tail of that chain. */
1145 c_translate_location (struct obstack
*pool
,
1146 void (*fail
) (void *arg
, const char *fmt
, ...)
1147 __attribute__ ((noreturn
, format (printf
, 2, 3))),
1149 void (*emit_address
) (void *fail_arg
,
1150 struct obstack
*, Dwarf_Addr
),
1151 int indent
, Dwarf_Addr dwbias
, Dwarf_Addr pc_address
,
1152 Dwarf_Attribute
*attr
,
1153 const Dwarf_Op
*expr
, size_t len
,
1154 struct location
**input
, Dwarf_Attribute
*fb_attr
,
1155 const Dwarf_Op
*cfa_ops
)
1159 switch (*input
== NULL
? loc_address
: (*input
)->type
)
1162 /* We have a previous address computation.
1163 This expression will compute starting with that on the stack. */
1164 return location_from_address (pool
, fail
, fail_arg
,
1165 emit_address
?: &default_emit_address
,
1166 indent
, dwbias
, attr
, expr
, len
, pc_address
,
1167 input
, fb_attr
, cfa_ops
);
1169 case loc_noncontiguous
:
1173 /* The starting point is not an address computation, but a
1174 register or implicit value. We can only handle limited
1175 computations from here. */
1176 return location_relative (pool
, indent
, dwbias
, attr
, expr
, len
,
1177 pc_address
, input
, fb_attr
, cfa_ops
);
1187 /* Translate a DW_AT_const_value attribute as if it were a location of
1188 constant-value flavor. */
1191 c_translate_constant (struct obstack
*pool
,
1192 void (*fail
) (void *arg
,
1193 const char *fmt
, ...)
1194 __attribute__ ((noreturn
,
1195 format (printf
, 2, 3))),
1197 void (*emit_address
) (void *fail_arg
,
1200 int indent
, Dwarf_Addr dwbias
, Dwarf_Attribute
*attr
)
1204 struct location
*loc
= obstack_alloc (pool
, sizeof *loc
);
1206 loc
->fail_arg
= fail_arg
;
1207 loc
->emit_address
= emit_address
?: &default_emit_address
;
1209 loc
->frame_base
= NULL
;
1210 loc
->address
.stack_depth
= 0;
1211 loc
->address
.declare
= NULL
;
1212 loc
->address
.used_deref
= false;
1216 switch (dwarf_whatform (attr
))
1221 if (dwarf_formaddr (attr
, &addr
) != 0)
1223 FAIL (loc
, N_("cannot get constant address: %s"),
1227 loc
->type
= loc_value
;
1228 obstack_printf (pool
, "%*saddr = ", indent
* 2, "");
1229 (*loc
->emit_address
) (loc
->fail_arg
, pool
, dwbias
+ addr
);
1230 obstack_grow (pool
, ";\n", 3);
1231 loc
->address
.program
= obstack_finish (pool
);
1236 case DW_FORM_block1
:
1237 case DW_FORM_block2
:
1238 case DW_FORM_block4
:
1241 if (dwarf_formblock (attr
, &block
) != 0)
1243 FAIL (loc
, N_("cannot get constant block: %s"), dwarf_errmsg (-1));
1246 loc
->type
= loc_constant
;
1247 loc
->byte_size
= block
.length
;
1248 loc
->constant_block
= block
.data
;
1252 case DW_FORM_string
:
1255 const char *string
= dwarf_formstring (attr
);
1258 FAIL (loc
, N_("cannot get string constant: %s"), dwarf_errmsg (-1));
1261 loc
->type
= loc_constant
;
1262 loc
->byte_size
= strlen (string
) + 1;
1263 loc
->constant_block
= string
;
1270 if (dwarf_formsdata (attr
, &value
) != 0)
1272 FAIL (loc
, N_("cannot get constant value: %s"), dwarf_errmsg (-1));
1275 loc
->type
= loc_value
;
1276 obstack_printf (pool
, "%*saddr = %" PRId64
"L;\n",
1277 indent
* 2, "", value
);
1278 obstack_1grow (pool
, '\0');
1279 loc
->address
.program
= obstack_finish (pool
);
1288 /* Translate a C fragment for a direct argument VALUE. On errors, call FAIL,
1289 which should not return. Any later errors will use FAIL and FAIL_ARG from
1290 this translate call. On success, return the fragment created. */
1292 c_translate_argument (struct obstack
*pool
,
1293 void (*fail
) (void *arg
, const char *fmt
, ...)
1294 __attribute__ ((noreturn
, format (printf
, 2, 3))),
1296 void (*emit_address
) (void *fail_arg
,
1297 struct obstack
*, Dwarf_Addr
),
1298 int indent
, const char *value
)
1302 obstack_printf (pool
, "%*saddr = %s;\n", indent
* 2, "", value
);
1303 obstack_1grow (pool
, '\0');
1304 char *program
= obstack_finish (pool
);
1306 struct location
*loc
= obstack_alloc (pool
, sizeof *loc
);
1309 loc
->fail_arg
= fail_arg
;
1310 loc
->emit_address
= emit_address
?: &default_emit_address
;
1314 loc
->type
= loc_address
;
1315 loc
->frame_base
= NULL
;
1316 loc
->address
.declare
= NULL
;
1317 loc
->address
.program
= program
;
1318 loc
->address
.stack_depth
= 0;
1319 loc
->address
.used_deref
= false;
1325 /* Emit "uintNN_t TARGET = ...;". */
1327 emit_base_fetch (struct obstack
*pool
, Dwarf_Word byte_size
,
1328 bool signed_p
, const char *target
, struct location
*loc
)
1332 /* Emit size/signed coercion. */
1333 obstack_printf (pool
, "{ ");
1334 obstack_printf (pool
, "%sint%u_t value = ",
1335 (signed_p
? "" : "u"), (unsigned)(byte_size
* 8));
1340 obstack_printf (pool
, "addr;");
1344 if (byte_size
!= 0 && byte_size
!= (Dwarf_Word
) -1)
1345 obstack_printf (pool
, "deref (%" PRIu64
", addr);", byte_size
);
1347 obstack_printf (pool
, "deref (sizeof %s, addr);", target
);
1352 if (loc
->reg
.offset
!= 0)
1353 FAIL (loc
, N_("cannot handle offset into register in fetch"));
1354 obstack_printf (pool
, "fetch_register (%u);", loc
->reg
.regno
);
1357 case loc_noncontiguous
:
1358 FAIL (loc
, N_("noncontiguous location for base fetch"));
1366 obstack_printf (pool
, " %s = value; }", target
);
1370 /* Emit "... = RVALUE;". */
1372 emit_base_store (struct obstack
*pool
, Dwarf_Word byte_size
,
1373 const char *rvalue
, struct location
*loc
)
1378 if (byte_size
!= 0 && byte_size
!= (Dwarf_Word
) -1)
1379 obstack_printf (pool
, "store_deref (%" PRIu64
", addr, %s); ",
1382 obstack_printf (pool
, "store_deref (sizeof %s, addr, %s); ",
1387 if (loc
->reg
.offset
!= 0)
1388 FAIL (loc
, N_("cannot handle offset into register in store"));
1389 obstack_printf (pool
, "store_register (%u, %s);", loc
->reg
.regno
, rvalue
);
1392 case loc_noncontiguous
:
1393 FAIL (loc
, N_("noncontiguous location for base store"));
1397 FAIL (loc
, N_("location is computed value, cannot store"));
1401 FAIL (loc
, N_("location is constant value, cannot store"));
1413 /* Slice up an object into pieces no larger than MAX_PIECE_BYTES,
1414 yielding a loc_noncontiguous location unless LOC is small enough. */
1415 static struct location
*
1416 discontiguify (struct obstack
*pool
, int indent
, struct location
*loc
,
1417 Dwarf_Word total_bytes
, Dwarf_Word max_piece_bytes
)
1419 inline bool pieces_small_enough (void)
1421 if (loc
->type
!= loc_noncontiguous
)
1422 return (loc
->byte_size
?: total_bytes
) <= max_piece_bytes
;
1424 for (p
= loc
->pieces
; p
!= NULL
; p
= p
->next
)
1425 if (p
->byte_size
> max_piece_bytes
)
1430 if (pieces_small_enough ())
1433 struct location
*noncontig
= alloc_location (pool
, loc
);
1434 noncontig
->next
= NULL
;
1435 noncontig
->type
= loc_noncontiguous
;
1436 noncontig
->byte_size
= total_bytes
;
1437 noncontig
->pieces
= NULL
;
1438 struct location
**tailpiece
= &noncontig
->pieces
;
1439 inline void add (struct location
*piece
)
1442 tailpiece
= &piece
->next
;
1449 /* Synthesize a piece that sets "container_addr" to the computed
1450 address of the whole object. Each piece will refer to this. */
1451 obstack_printf (pool
, "%*scontainer_addr = addr;\n",
1453 loc
->next
= new_synthetic_loc (pool
, loc
, false);
1454 loc
->next
->byte_size
= loc
->byte_size
;
1455 loc
->next
->type
= loc_fragment
;
1456 loc
->next
->address
.declare
= "container_addr";
1459 /* Synthesize pieces that just compute "container_addr + N". */
1460 Dwarf_Word offset
= 0;
1461 while (total_bytes
- offset
> 0)
1463 Dwarf_Word size
= total_bytes
- offset
;
1464 if (size
> max_piece_bytes
)
1465 size
= max_piece_bytes
;
1467 obstack_printf (pool
, "%*saddr = container_addr + " UFORMAT
";\n",
1468 indent
* 2, "", offset
);
1469 struct location
*piece
= new_synthetic_loc (pool
, loc
, false);
1470 piece
->byte_size
= size
;
1482 Dwarf_Word offset
= 0;
1483 while (total_bytes
- offset
> 0)
1485 Dwarf_Word size
= total_bytes
- offset
;
1486 if (size
> max_piece_bytes
)
1487 size
= max_piece_bytes
;
1489 struct location
*piece
= alloc_location (pool
, loc
);
1491 piece
->type
= loc_constant
;
1492 piece
->byte_size
= size
;
1493 piece
->constant_block
= loc
->constant_block
+ offset
;
1504 FAIL (loc
, N_("stack value too big for fetch ???"));
1508 FAIL (loc
, N_("single register too big for fetch/store ???"));
1511 case loc_noncontiguous
:
1512 /* Could be handled if it ever happened. */
1513 FAIL (loc
, N_("discontiguify of noncontiguous location not supported"));
1521 loc
->next
= noncontig
;
1525 /* Make a fragment that declares a union such as:
1531 } pieces __attribute__ ((packed));
1536 declare_noncontig_union (struct obstack
*pool
, int indent
,
1537 struct location
**input
, struct location
*loc
,
1541 FAIL (loc
, N_("declaring noncontig union for depth > 9, too many pieces"));
1543 obstack_printf (pool
, "%*sunion {\n", indent
++ * 2, "");
1545 obstack_printf (pool
, "%*schar bytes[%" PRIu64
"];\n",
1546 indent
* 2, "", loc
->byte_size
);
1548 if (loc
->type
== loc_noncontiguous
)
1550 Dwarf_Word offset
= 0;
1552 obstack_printf (pool
, "%*sstruct {\n", indent
++ * 2, "");
1554 for (p
= loc
->pieces
; p
!= NULL
; p
= p
->next
)
1556 obstack_printf (pool
, "%*suint%" PRIu64
"_t p%" PRIu64
";\n",
1557 indent
* 2, "", p
->byte_size
* 8, offset
);
1558 offset
+= p
->byte_size
;
1561 obstack_printf (pool
, "%*s} pieces __attribute__ ((packed));\n",
1565 obstack_printf (pool
, "%*suint%" PRIu64
"_t whole;\n",
1566 indent
* 2, "", loc
->byte_size
* 8);
1568 obstack_printf (pool
, "%*s} u_pieces%d;\n", --indent
* 2, "", depth
);
1570 loc
= new_synthetic_loc (pool
, *input
, false);
1571 loc
->type
= loc_decl
;
1572 (*input
)->next
= loc
;
1576 /* Determine the byte size of a base type. */
1578 base_byte_size (Dwarf_Die
*typedie
, struct location
*origin
)
1580 assert (dwarf_tag (typedie
) == DW_TAG_base_type
||
1581 dwarf_tag (typedie
) == DW_TAG_enumeration_type
);
1583 Dwarf_Attribute attr_mem
;
1585 if (dwarf_attr_integrate (typedie
, DW_AT_byte_size
, &attr_mem
) != NULL
1586 && dwarf_formudata (&attr_mem
, &size
) == 0)
1590 N_("cannot get byte_size attribute for type %s: %s"),
1591 dwarf_diename (typedie
) ?: "<anonymous>",
1597 base_encoding (Dwarf_Die
*typedie
, struct location
*origin
)
1599 if (! (dwarf_tag (typedie
) == DW_TAG_base_type
||
1600 dwarf_tag (typedie
) == DW_TAG_enumeration_type
))
1603 Dwarf_Attribute attr_mem
;
1604 Dwarf_Word encoding
;
1605 if (dwarf_attr_integrate (typedie
, DW_AT_encoding
, &attr_mem
) != NULL
1606 && dwarf_formudata (&attr_mem
, &encoding
) == 0)
1612 N_("cannot get encoding attribute for type %s: %s"),
1613 dwarf_diename (typedie) ?: "<anonymous>",
1621 /* Fetch the bitfield parameters. */
1623 get_bitfield (struct location
*loc
,
1624 Dwarf_Die
*die
, Dwarf_Word
*bit_offset
, Dwarf_Word
*bit_size
)
1626 Dwarf_Attribute attr_mem
;
1627 if (dwarf_attr_integrate (die
, DW_AT_bit_offset
, &attr_mem
) == NULL
1628 || dwarf_formudata (&attr_mem
, bit_offset
) != 0
1629 || dwarf_attr_integrate (die
, DW_AT_bit_size
, &attr_mem
) == NULL
1630 || dwarf_formudata (&attr_mem
, bit_size
) != 0)
1631 FAIL (loc
, N_("cannot get bit field parameters: %s"), dwarf_errmsg (-1));
1634 /* Translate a fragment to fetch the base-type value of BYTE_SIZE bytes
1635 at the *INPUT location and store it in lvalue TARGET. */
1637 translate_base_fetch (struct obstack
*pool
, int indent
,
1638 Dwarf_Word byte_size
, bool signed_p
,
1639 struct location
**input
, const char *target
,
1644 if ((*input
)->type
== loc_noncontiguous
)
1646 struct location
*p
= (*input
)->pieces
;
1648 declare_noncontig_union (pool
, indent
, input
, *input
, depth
);
1650 Dwarf_Word offset
= 0;
1651 char piece
[sizeof "u_pieces?.pieces.p" + 20] = "u_pieces?.pieces.p";
1652 piece
[8] = (char) ('0' + depth
);
1653 int pdepth
= depth
+ 1;
1656 struct location
*newp
= obstack_alloc (pool
, sizeof *newp
);
1659 (*input
)->next
= newp
;
1662 snprintf (&piece
[sizeof "u_pieces?.pieces.p" - 1], 20,
1663 "%" PRIu64
, offset
);
1664 translate_base_fetch (pool
, indent
, p
->byte_size
, signed_p
/* ? */,
1665 input
, piece
, pdepth
);
1666 (*input
)->type
= loc_fragment
;
1668 offset
+= p
->byte_size
;
1673 obstack_printf (pool
, "%*s%s = u_pieces%d.whole;\n", indent
* 2,
1676 else if ((*input
)->type
== loc_constant
)
1678 const unsigned char *constant_block
= (*input
)->constant_block
;
1679 const size_t byte_size
= (*input
)->byte_size
;
1682 declare_noncontig_union (pool
, indent
, input
, *input
, depth
);
1684 for (i
= 0; i
< byte_size
; ++i
)
1685 obstack_printf (pool
, "%*su_pieces%d.bytes[%zu] = %#x;\n", indent
* 2,
1686 "", depth
, i
, constant_block
[i
]);
1688 obstack_printf (pool
, "%*s%s = u_pieces%d.whole;\n", indent
* 2,
1694 case 0: /* Special case, means address size. */
1699 obstack_printf (pool
, "%*s", indent
* 2, "");
1700 deref
= emit_base_fetch (pool
, byte_size
, signed_p
, target
, *input
);
1701 obstack_printf (pool
, "\n");
1705 /* Could handle this generating call to memcpy equivalent. */
1706 FAIL (*input
, N_("fetch is larger than base integer types"));
1710 struct location
*loc
= new_synthetic_loc (pool
, *input
, deref
);
1711 loc
->byte_size
= byte_size
;
1712 loc
->type
= loc_final
;
1713 (*input
)->next
= loc
;
1717 /* Determine the maximum size of a base type, from some DIE in the CU. */
1719 max_fetch_size (struct location
*loc
, Dwarf_Die
*die
)
1722 uint8_t address_size
;
1723 Dwarf_Die
*cu
= dwarf_diecu (die
, &cu_mem
, &address_size
, NULL
);
1725 FAIL (loc
, N_("cannot determine CU address size from %s: %s"),
1726 dwarf_diename (die
), dwarf_errmsg (-1));
1728 return address_size
;
1731 /* Translate a fragment to fetch the value of variable or member DIE
1732 at the *INPUT location and store it in lvalue TARGET. */
1734 c_translate_fetch (struct obstack
*pool
, int indent
,
1735 Dwarf_Addr dwbias
__attribute__ ((unused
)),
1736 Dwarf_Die
*die
, Dwarf_Die
*typedie
,
1737 struct location
**input
, const char *target
)
1741 Dwarf_Attribute size_attr
;
1742 Dwarf_Word byte_size
;
1743 if (dwarf_attr_integrate (die
, DW_AT_byte_size
, &size_attr
) == NULL
1744 || dwarf_formudata (&size_attr
, &byte_size
) != 0)
1745 byte_size
= base_byte_size (typedie
, *input
);
1747 Dwarf_Attribute encoding_attr
;
1748 Dwarf_Word encoding
;
1749 if (dwarf_attr_integrate (die
, DW_AT_encoding
, &encoding_attr
) == NULL
1750 || dwarf_formudata (&encoding_attr
, &encoding
) != 0)
1751 encoding
= base_encoding (typedie
, *input
);
1752 bool signed_p
= encoding
== DW_ATE_signed
|| encoding
== DW_ATE_signed_char
;
1754 *input
= discontiguify (pool
, indent
, *input
, byte_size
,
1755 max_fetch_size (*input
, die
));
1757 if (dwarf_hasattr_integrate (die
, DW_AT_bit_offset
))
1759 /* This is a bit field. Fetch the containing base type into a
1760 temporary variable. */
1762 translate_base_fetch (pool
, indent
, byte_size
, signed_p
, input
, "tmp", 0);
1763 (*input
)->type
= loc_fragment
;
1764 (*input
)->address
.declare
= "tmp";
1766 Dwarf_Word bit_offset
, bit_size
;
1767 get_bitfield (*input
, die
, &bit_offset
, &bit_size
);
1769 obstack_printf (pool
, "%*s"
1770 "fetch_bitfield (%s, tmp, %" PRIu64
", %" PRIu64
");\n",
1771 indent
*2, "", target
, bit_offset
, bit_size
);
1773 struct location
*loc
= new_synthetic_loc (pool
, *input
, false);
1774 loc
->type
= loc_final
;
1775 (*input
)->next
= loc
;
1779 translate_base_fetch (pool
, indent
, byte_size
, signed_p
, input
, target
, 0);
1782 /* Translate a fragment to store RVALUE into the base-type value of
1783 BYTE_SIZE bytes at the *INPUT location. */
1785 translate_base_store (struct obstack
*pool
, int indent
, Dwarf_Word byte_size
,
1786 struct location
**input
, struct location
*store_loc
,
1787 const char *rvalue
, int depth
)
1791 if (store_loc
->type
== loc_noncontiguous
)
1793 declare_noncontig_union (pool
, indent
, input
, store_loc
, depth
);
1795 obstack_printf (pool
, "%*su_pieces%d.whole = %s;\n", indent
* 2,
1797 struct location
*loc
= new_synthetic_loc (pool
, *input
, deref
);
1798 loc
->type
= loc_fragment
;
1799 (*input
)->next
= loc
;
1802 Dwarf_Word offset
= 0;
1803 char piece
[sizeof "u_pieces?.pieces.p" + 20] = "u_pieces?.pieces.p";
1804 piece
[8] = (char) ('0' + depth
);
1806 int pdepth
= depth
+ 1;
1807 for (p
= store_loc
->pieces
; p
!= NULL
; p
= p
->next
)
1809 struct location
*newp
= obstack_alloc (pool
, sizeof *newp
);
1812 (*input
)->next
= newp
;
1815 snprintf (&piece
[sizeof "u_pieces?.pieces.p" - 1], 20, "%" PRIu64
,
1817 translate_base_store (pool
, indent
,
1818 p
->byte_size
, input
, *input
, piece
, pdepth
++);
1819 (*input
)->type
= loc_fragment
;
1821 offset
+= p
->byte_size
;
1824 (*input
)->type
= loc_final
;
1834 obstack_printf (pool
, "%*s", indent
* 2, "");
1835 deref
= emit_base_store (pool
, byte_size
, rvalue
, store_loc
);
1836 obstack_printf (pool
, "\n");
1840 /* Could handle this generating call to memcpy equivalent. */
1841 FAIL (*input
, N_("store is larger than base integer types"));
1845 struct location
*loc
= new_synthetic_loc (pool
, *input
, deref
);
1846 loc
->type
= loc_final
;
1847 (*input
)->next
= loc
;
1852 /* Translate a fragment to fetch the value of variable or member DIE
1853 at the *INPUT location and store it in rvalue RVALUE. */
1856 c_translate_store (struct obstack
*pool
, int indent
,
1857 Dwarf_Addr dwbias
__attribute__ ((unused
)),
1858 Dwarf_Die
*die
, Dwarf_Die
*typedie
,
1859 struct location
**input
, const char *rvalue
)
1863 Dwarf_Attribute size_attr
;
1864 Dwarf_Word byte_size
;
1865 if (dwarf_attr_integrate (die
, DW_AT_byte_size
, &size_attr
) == NULL
1866 || dwarf_formudata (&size_attr
, &byte_size
) != 0)
1867 byte_size
= base_byte_size (typedie
, *input
);
1869 Dwarf_Attribute encoding_attr
;
1870 Dwarf_Word encoding
;
1871 if (dwarf_attr_integrate (die
, DW_AT_encoding
, &encoding_attr
) == NULL
1872 || dwarf_formudata (&encoding_attr
, &encoding
) != 0)
1873 encoding
= base_encoding (typedie
, *input
);
1874 bool signed_p
= (encoding
== DW_ATE_signed
1875 || encoding
== DW_ATE_signed_char
);
1877 *input
= discontiguify (pool
, indent
, *input
, byte_size
,
1878 max_fetch_size (*input
, die
));
1880 struct location
*store_loc
= *input
;
1882 if (dwarf_hasattr_integrate (die
, DW_AT_bit_offset
))
1884 /* This is a bit field. Fetch the containing base type into a
1885 temporary variable. */
1887 translate_base_fetch (pool
, indent
, byte_size
, signed_p
, input
, "tmp", 0);
1888 (*input
)->type
= loc_fragment
;
1889 (*input
)->address
.declare
= "tmp";
1891 Dwarf_Word bit_offset
, bit_size
;
1892 get_bitfield (*input
, die
, &bit_offset
, &bit_size
);
1894 obstack_printf (pool
, "%*s"
1895 "store_bitfield (tmp, %s, %" PRIu64
", %" PRIu64
");\n",
1896 indent
* 2, "", rvalue
, bit_offset
, bit_size
);
1898 struct location
*loc
= new_synthetic_loc (pool
, *input
, false);
1899 loc
->type
= loc_fragment
;
1900 (*input
)->next
= loc
;
1903 /* We have mixed RVALUE into the bits in "tmp".
1904 Now we'll store "tmp" back whence we fetched it. */
1908 translate_base_store (pool
, indent
, byte_size
, input
, store_loc
, rvalue
, 0);
1911 /* Translate a fragment to dereference the given pointer type,
1912 where *INPUT is the location of the pointer with that type.
1914 We chain on a loc_address program that yields this pointer value
1915 (i.e. the location of what it points to). */
1918 c_translate_pointer (struct obstack
*pool
, int indent
,
1919 Dwarf_Addr dwbias
__attribute__ ((unused
)),
1920 Dwarf_Die
*typedie
, struct location
**input
)
1922 assert (dwarf_tag (typedie
) == DW_TAG_pointer_type
||
1923 dwarf_tag (typedie
) == DW_TAG_reference_type
||
1924 dwarf_tag (typedie
) == DW_TAG_rvalue_reference_type
);
1926 Dwarf_Attribute attr_mem
;
1927 Dwarf_Word byte_size
;
1928 if (dwarf_attr_integrate (typedie
, DW_AT_byte_size
, &attr_mem
) == NULL
)
1930 else if (dwarf_formudata (&attr_mem
, &byte_size
) != 0)
1932 N_("cannot get byte_size attribute for type %s: %s"),
1933 dwarf_diename (typedie
) ?: "<anonymous>",
1936 bool signed_p
= false; /* XXX: Does not matter? */
1938 translate_base_fetch (pool
, indent
+ 1, byte_size
, signed_p
, input
, "addr", 0);
1939 (*input
)->type
= loc_address
;
1944 c_translate_addressof (struct obstack
*pool
, int indent
,
1945 Dwarf_Addr dwbias
__attribute__ ((unused
)),
1947 Dwarf_Die
*typedie
__attribute__ ((unused
)),
1948 struct location
**input
, const char *target
)
1952 if (dwarf_hasattr_integrate (die
, DW_AT_bit_offset
))
1953 FAIL (*input
, N_("cannot take the address of a bit field"));
1955 switch ((*input
)->type
)
1958 obstack_printf (pool
, "%*s%s = addr;\n", indent
* 2, "", target
);
1959 (*input
)->next
= new_synthetic_loc (pool
, *input
, false);
1960 (*input
)->next
->type
= loc_final
;
1964 FAIL (*input
, N_("cannot take address of object in register"));
1966 case loc_noncontiguous
:
1967 FAIL (*input
, N_("cannot take address of noncontiguous object"));
1970 FAIL (*input
, N_("cannot take address of computed value"));
1973 FAIL (*input
, N_("cannot take address of constant value"));
1983 /* Translate a fragment to write the given pointer value,
1984 where *INPUT is the location of the pointer with that type.
1988 c_translate_pointer_store (struct obstack
*pool
, int indent
,
1989 Dwarf_Addr dwbias
__attribute__ ((unused
)),
1990 Dwarf_Die
*typedie
, struct location
**input
,
1993 assert (dwarf_tag (typedie
) == DW_TAG_pointer_type
);
1995 Dwarf_Attribute attr_mem
;
1996 Dwarf_Word byte_size
;
1997 if (dwarf_attr_integrate (typedie
, DW_AT_byte_size
, &attr_mem
) == NULL
)
1999 else if (dwarf_formudata (&attr_mem
, &byte_size
) != 0)
2001 N_("cannot get byte_size attribute for type %s: %s"),
2002 dwarf_diename (typedie
) ?: "<anonymous>",
2005 translate_base_store (pool
, indent
+ 1, byte_size
,
2006 input
, *input
, rvalue
, 0);
2008 // XXX: what about multiple-location lvalues?
2011 /* Determine the element stride of an array type. */
2013 array_stride (Dwarf_Die
*typedie
, struct location
*origin
)
2015 Dwarf_Attribute attr_mem
;
2016 if (dwarf_attr_integrate (typedie
, DW_AT_byte_stride
, &attr_mem
) != NULL
)
2019 if (dwarf_formudata (&attr_mem
, &stride
) == 0)
2021 FAIL (origin
, N_("cannot get byte_stride attribute array type %s: %s"),
2022 dwarf_diename (typedie
) ?: "<anonymous>",
2027 if (dwarf_attr_integrate (typedie
, DW_AT_type
, &attr_mem
) == NULL
2028 || dwarf_formref_die (&attr_mem
, &die_mem
) == NULL
)
2029 FAIL (origin
, N_("cannot get element type of array type %s: %s"),
2030 dwarf_diename (typedie
) ?: "<anonymous>",
2033 int typetag
= dwarf_tag(&die_mem
);
2034 while (typetag
== DW_TAG_typedef
||
2035 typetag
== DW_TAG_const_type
||
2036 typetag
== DW_TAG_volatile_type
)
2038 if (dwarf_attr_integrate (&die_mem
, DW_AT_type
, &attr_mem
) == NULL
2039 || dwarf_formref_die (&attr_mem
, &die_mem
) == NULL
)
2040 FAIL (origin
, N_("cannot get inner type of type %s: %s"),
2041 dwarf_diename (&die_mem
) ?: "<anonymous>",
2043 typetag
= dwarf_tag(&die_mem
);
2046 if (dwarf_attr_integrate (&die_mem
, DW_AT_byte_size
, &attr_mem
) != NULL
)
2049 if (dwarf_formudata (&attr_mem
, &stride
) == 0)
2052 N_("cannot get byte_size attribute for array element type %s: %s"),
2053 dwarf_diename (&die_mem
) ?: "<anonymous>",
2057 FAIL (origin
, N_("confused about array element size"));
2062 c_translate_array (struct obstack
*pool
, int indent
,
2063 Dwarf_Addr dwbias
__attribute__ ((unused
)),
2064 Dwarf_Die
*typedie
, struct location
**input
,
2065 const char *idx
, Dwarf_Word const_idx
)
2067 assert (dwarf_tag (typedie
) == DW_TAG_array_type
||
2068 dwarf_tag (typedie
) == DW_TAG_pointer_type
);
2072 Dwarf_Word stride
= array_stride (typedie
, *input
);
2074 struct location
*loc
= *input
;
2075 while (loc
->type
== loc_noncontiguous
)
2078 FAIL (*input
, N_("cannot dynamically index noncontiguous array"));
2081 Dwarf_Word offset
= const_idx
* stride
;
2082 struct location
*piece
= loc
->pieces
;
2083 while (piece
!= NULL
&& offset
>= piece
->byte_size
)
2085 offset
-= piece
->byte_size
;
2086 piece
= piece
->next
;
2089 FAIL (*input
, N_("constant index is outside noncontiguous array"));
2090 if (offset
% stride
!= 0)
2091 FAIL (*input
, N_("noncontiguous array splits elements"));
2092 const_idx
= offset
/ stride
;
2102 obstack_printf (pool
, "%*saddr += %s * " UFORMAT
";\n",
2103 indent
* 2, "", idx
, stride
);
2105 obstack_printf (pool
, "%*saddr += " UFORMAT
" * " UFORMAT
";\n",
2106 indent
* 2, "", const_idx
, stride
);
2107 loc
= new_synthetic_loc (pool
, loc
, false);
2112 FAIL (*input
, N_("cannot index array stored in a register"));
2113 else if (const_idx
> max_fetch_size (loc
, typedie
) / stride
)
2114 FAIL (*input
, N_("constant index is outside array held in register"));
2117 loc
->reg
.offset
+= const_idx
* stride
;
2124 FAIL (*input
, N_("cannot index into constant value"));
2125 else if (const_idx
> loc
->byte_size
/ stride
)
2126 FAIL (*input
, N_("constant index is outside constant array value"));
2129 loc
->byte_size
= stride
;
2130 loc
->constant_block
+= const_idx
* stride
;
2136 FAIL (*input
, N_("cannot index into computed value"));
2144 (*input
)->next
= loc
;
2145 *input
= (*input
)->next
;
2149 /* Emitting C code for finalized fragments. */
2152 #define emit(fmt, ...) fprintf (out, fmt, ## __VA_ARGS__)
2154 /* Open a block with a comment giving the original DWARF expression. */
2156 emit_header (FILE *out
, struct location
*loc
, unsigned int hindent
)
2158 if (loc
->ops
== NULL
)
2159 emit ("%*s{ // synthesized\n", hindent
* 2, "");
2162 emit ("%*s{ // DWARF expression:", hindent
* 2, "");
2164 for (i
= 0; i
< loc
->nops
; ++i
)
2166 emit (" %#x", loc
->ops
[i
].atom
);
2167 if (loc
->ops
[i
].number2
== 0)
2169 if (loc
->ops
[i
].number
!= 0)
2170 emit ("(%" PRId64
")", loc
->ops
[i
].number
);
2173 emit ("(%" PRId64
",%" PRId64
")",
2174 loc
->ops
[i
].number
, loc
->ops
[i
].number2
);
2180 /* Emit a code fragment to assign the target variable to a register value. */
2182 emit_loc_register (FILE *out
, struct location
*loc
, unsigned int indent
,
2185 assert (loc
->type
== loc_register
);
2187 if (loc
->reg
.offset
!= 0)
2188 FAIL (loc
, N_("cannot handle offset into register in fetch"));
2190 emit ("%*s%s = fetch_register (%u);\n",
2191 indent
* 2, "", target
, loc
->reg
.regno
);
2194 /* Emit a code fragment to assign the target variable to an address. */
2196 emit_loc_address (FILE *out
, struct location
*loc
, unsigned int indent
,
2199 assert (loc
->type
== loc_address
|| loc
->type
== loc_value
);
2201 if (loc
->address
.stack_depth
== 0)
2202 /* Synthetic program. */
2203 emit ("%s", loc
->address
.program
);
2206 emit ("%*s{\n", indent
* 2, "");
2207 emit ("%*s%s " STACKFMT
, (indent
+ 1) * 2, "",
2208 stack_slot_type (loc
, false), 0);
2210 for (i
= 1; i
< loc
->address
.stack_depth
; ++i
)
2211 emit (", " STACKFMT
, i
);
2214 emit ("%s%*s%s = " STACKFMT
";\n", loc
->address
.program
,
2215 (indent
+ 1) * 2, "", target
, 0);
2216 emit ("%*s}\n", indent
* 2, "");
2220 /* Emit a code fragment to declare the target variable and
2221 assign it to an address-sized value. */
2223 emit_loc_value (FILE *out
, struct location
*loc
, unsigned int indent
,
2224 const char *target
, bool declare
,
2225 bool *used_deref
, unsigned int *max_stack
)
2228 emit ("%*s%s %s;\n", indent
* 2, "", stack_slot_type (loc
, false), target
);
2230 emit_header (out
, loc
, indent
++);
2239 emit_loc_register (out
, loc
, indent
, target
);
2244 emit_loc_address (out
, loc
, indent
, target
);
2245 *used_deref
= *used_deref
|| loc
->address
.used_deref
;
2246 if (loc
->address
.stack_depth
> *max_stack
)
2247 *max_stack
= loc
->address
.stack_depth
;
2251 emit ("%*s}\n", --indent
* 2, "");
2255 c_emit_location (FILE *out
, struct location
*loc
, int indent
,
2256 unsigned int *max_stack
)
2258 emit ("%*s{\n", indent
* 2, "");
2260 bool declared_addr
= false;
2262 for (l
= loc
; l
!= NULL
; l
= l
->next
)
2266 emit ("%s", l
->address
.program
);
2273 declared_addr
= true;
2274 l
->address
.declare
= "addr";
2277 if (l
->address
.declare
!= NULL
)
2279 if (l
->byte_size
== 0 || l
->byte_size
== (Dwarf_Word
) -1)
2280 emit ("%*s%s %s;\n", (indent
+ 1) * 2, "",
2281 stack_slot_type (l
, false), l
->address
.declare
);
2283 emit ("%*suint%" PRIu64
"_t %s;\n", (indent
+ 1) * 2, "",
2284 l
->byte_size
* 8, l
->address
.declare
);
2294 if (loc
->frame_base
!= NULL
)
2295 emit_loc_value (out
, loc
->frame_base
, indent
, "frame_base", true,
2298 for (; loc
->next
!= NULL
; loc
= loc
->next
)
2303 /* Emit the program fragment to calculate the address. */
2304 emit_loc_value (out
, loc
, indent
+ 1, "addr", false, &deref
, max_stack
);
2308 emit ("%s", loc
->address
.program
);
2309 deref
= deref
|| loc
->address
.used_deref
;
2314 case loc_noncontiguous
:
2316 /* These don't produce any code directly.
2317 The next address/final record incorporates the value. */
2320 case loc_final
: /* Should be last in chain! */
2326 if (loc
->type
!= loc_final
) /* Unfinished chain. */
2329 emit ("%s%*s}\n", loc
->address
.program
, indent
* 2, "");
2331 if (loc
->address
.stack_depth
> *max_stack
)
2332 *max_stack
= loc
->address
.stack_depth
;
2334 return deref
|| loc
->address
.used_deref
;
2339 /* vim: set sw=2 ts=8 cino=>4,n-2,{2,^-2,t0,(0,u0,w1,M1 : */