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); \
493 /* Comparisons are binary operators too. */
508 push ("(%s) " STACKFMT
" >> (%s)" STACKFMT
,
509 stack_slot_type (loc
, true), a
,
510 stack_slot_type (loc
, true), b
);
518 push ("(%s) " STACKFMT
" / (%s)" STACKFMT
,
519 stack_slot_type (loc
, true), a
,
520 stack_slot_type (loc
, true), b
);
524 case DW_OP_plus_uconst
:
527 push (STACKFMT
" + " UFORMAT
, x
, expr
[i
].number
);
532 /* Register-relative addressing. */
533 case DW_OP_breg0
... DW_OP_breg31
:
534 reg
= expr
[i
].atom
- DW_OP_breg0
;
535 value
= expr
[i
].number
;
539 reg
= expr
[i
].number
;
540 value
= expr
[i
].number2
;
542 push ("fetch_register (%u) + " SFORMAT
, reg
, value
);
547 DIE ("DW_OP_fbreg from DW_AT_frame_base");
549 push ("frame_base + " SFORMAT
, expr
[i
].number
);
552 /* Direct register contents. */
553 case DW_OP_reg0
... DW_OP_reg31
:
554 reg
= expr
[i
].atom
- DW_OP_reg0
;
558 reg
= expr
[i
].number
;
566 /* If this ever happens we could copy the program. */
567 DIE ("DW_OP_piece left multiple values on stack");
570 /* The obstack has a pending program for loc_address,
571 so we must finish that piece off before we can
573 struct location temp_piece
=
576 .fail_arg
= loc
->fail_arg
,
577 .emit_address
= loc
->emit_address
,
579 .ops
= &expr
[piece_expr_start
],
580 .nops
= i
- piece_expr_start
,
582 const char *failure
= finish (&temp_piece
);
586 struct location
*piece
= obstack_alloc (pool
, sizeof *piece
);
589 piece_expr_start
= i
+ 1;
591 piece_total_bytes
+= piece
->byte_size
= expr
[i
].number
;
594 tailpiece
= &piece
->next
;
597 /* Reset default conditions for handling the next piece. */
602 case DW_OP_stack_value
:
604 DIE ("DW_OP_stack_value left multiple values on stack");
607 /* Fetch a register to top of stack, or check for underflow.
608 Then mark the TOS as being a value. */
616 case DW_OP_implicit_value
:
618 DIE ("DW_OP_implicit_value used in invalid context"
619 " (no DWARF attribute, ABI return value location?)");
621 /* It's supposed to appear by itself, except for DW_OP_piece. */
622 if (stack_depth
!= 0)
623 DIE ("DW_OP_implicit_value follows stack operations");
625 #if _ELFUTILS_PREREQ (0, 143)
626 if (dwarf_getlocation_implicit_value (attr
, (Dwarf_Op
*) &expr
[i
],
627 &implicit_value
) != 0)
628 DIE ("dwarf_getlocation_implicit_value failed");
630 /* Fake top of stack: implicit_value being set marks it. */
635 DIE ("DW_OP_implicit_value not supported");
638 case DW_OP_call_frame_cfa
:
639 // We pick this out when processing DW_AT_frame_base in
640 // so it really shouldn't turn up here.
642 DIE ("DW_OP_call_frame_cfa while processing frame base");
644 DIE ("DW_OP_call_frame_cfa not expected outside DW_AT_frame_base");
647 case DW_OP_push_object_address
:
648 DIE ("XXX DW_OP_push_object_address");
652 DIE ("unrecognized operation");
660 if (piece_expr_start
!= i
)
661 DIE ("extra operations after last DW_OP_piece");
663 loc
->type
= loc_noncontiguous
;
664 loc
->pieces
= pieces
;
665 loc
->byte_size
= piece_total_bytes
;
670 DIE ("stack underflow");
680 /* Translate a location starting from an address or nothing. */
681 static struct location
*
682 location_from_address (struct obstack
*pool
,
683 void (*fail
) (void *arg
, const char *fmt
, ...)
684 __attribute__ ((noreturn
, format (printf
, 2, 3))),
686 void (*emit_address
) (void *fail_arg
,
687 struct obstack
*, Dwarf_Addr
),
688 int indent
, Dwarf_Addr dwbias
,
689 Dwarf_Attribute
*attr
,
690 const Dwarf_Op
*expr
, size_t len
, Dwarf_Addr address
,
691 struct location
**input
, Dwarf_Attribute
*fb_attr
,
692 const Dwarf_Op
*cfa_ops
)
694 struct location
*loc
= obstack_alloc (pool
, sizeof *loc
);
695 loc
->fail
= *input
== NULL
? fail
: (*input
)->fail
;
696 loc
->fail_arg
= *input
== NULL
? fail_arg
: (*input
)->fail_arg
;
697 loc
->emit_address
= *input
== NULL
? emit_address
: (*input
)->emit_address
;
699 loc
->frame_base
= NULL
;
703 bool need_fb
= false;
705 const char *failure
= translate (pool
, indent
+ 1, dwbias
, attr
, expr
, len
,
706 *input
, &need_fb
, &loser
, loc
);
708 return lose (loc
, failure
, expr
, loser
);
713 /* The main expression uses DW_OP_fbreg, so we need to compute
714 the DW_AT_frame_base attribute expression's value first. */
717 FAIL (loc
, N_("required DW_AT_frame_base attribute not supplied"));
721 switch (dwarf_getlocation_addr (fb_attr
, address
- dwbias
,
722 &fb_expr
, &fb_len
, 1))
724 case 1: /* Should always happen. */
726 goto fb_inaccessible
;
729 default: /* Shouldn't happen. */
731 FAIL (loc
, N_("dwarf_getlocation_addr (form %#x): %s"),
732 dwarf_whatform (fb_attr
), dwarf_errmsg (-1));
735 case 0: /* Shouldn't happen. */
737 FAIL (loc
, N_("DW_AT_frame_base not accessible at this address"));
741 // If it is DW_OP_call_frame_cfa then get cfi cfa ops.
742 const Dwarf_Op
* fb_ops
;
743 if (fb_len
== 1 && fb_expr
[0].atom
== DW_OP_call_frame_cfa
)
746 FAIL (loc
, N_("No cfa_ops supplied, but needed by DW_OP_call_frame_cfa"));
752 loc
->frame_base
= alloc_location (pool
, loc
);
753 failure
= translate (pool
, indent
+ 1, dwbias
, attr
, fb_ops
, fb_len
, NULL
,
754 NULL
, &loser
, loc
->frame_base
);
756 return lose (loc
, failure
, fb_expr
, loser
);
760 (*input
)->next
= loc
;
766 /* Translate a location starting from a non-address "on the top of the
767 stack". The *INPUT location is a register name or noncontiguous
768 object specification, and this expression wants to find the "address"
769 of an object (or the actual value) relative to that "address". */
771 static struct location
*
772 location_relative (struct obstack
*pool
,
773 int indent
, Dwarf_Addr dwbias
, Dwarf_Attribute
*attr
,
774 const Dwarf_Op
*expr
, size_t len
, Dwarf_Addr address
,
775 struct location
**input
, Dwarf_Attribute
*fb_attr
,
776 const Dwarf_Op
*cfa_ops
)
778 Dwarf_Sword
*stack
= NULL
;
779 unsigned int stack_depth
= 0, max_stack
= 0;
780 inline void deepen (void)
782 if (stack_depth
== max_stack
)
785 obstack_blank (pool
, sizeof stack
[0]);
786 stack
= (void *) obstack_base (pool
);
791 if (stack_depth > 0) \
795 int var = stack_depth
796 #define PUSH (deepen (), stack_depth++)
797 #define STACK(idx) (stack_depth - 1 - (idx))
798 #define STACKWORD(idx) stack[STACK (idx)]
800 /* Don't put stack operations in the arguments to this. */
801 #define push(value) (stack[PUSH] = (value))
803 const char *failure
= NULL
;
804 #define DIE(msg) do { failure = N_(msg); goto fail; } while (0)
806 struct location
*head
= NULL
;
808 for (i
= 0; i
< len
; ++i
)
813 switch (expr
[i
].atom
)
815 /* Basic stack operations. */
824 unsigned int tos
= STACK (0);
832 else if (*input
!= NULL
)
833 /* Mark that we have consumed the input. */
836 /* Hits if cleared above, or if we had no input at all. */
843 if (sp
>= stack_depth
)
856 deepen (); /* Use a temporary slot. */
857 STACKWORD (-1) = STACKWORD (0);
858 STACKWORD (0) = STACKWORD (1);
859 STACKWORD (1) = STACKWORD (-1);
865 deepen (); /* Use a temporary slot. */
866 STACKWORD (-1) = STACKWORD (0);
867 STACKWORD (0) = STACKWORD (1);
868 STACKWORD (2) = STACKWORD (2);
869 STACKWORD (2) = STACKWORD (-1);
873 /* Control flow operations. */
877 if (stack
[taken
] == 0)
884 Dwarf_Off target
= expr
[i
].offset
+ 3 + expr
[i
].number
;
885 while (i
+ 1 < len
&& expr
[i
+ 1].offset
< target
)
887 if (expr
[i
+ 1].offset
!= target
)
888 DIE ("invalid skip target");
894 case DW_OP_deref_size
:
896 case DW_OP_xderef_size
:
898 /* Register-relative addressing. */
899 case DW_OP_breg0
... DW_OP_breg31
:
903 /* This started from a register, but now it's following a pointer.
904 So we can do the translation starting from address here. */
905 return location_from_address (pool
, NULL
, NULL
, NULL
, indent
, dwbias
,
906 attr
, expr
, len
, address
, input
, fb_attr
,
910 /* Constant-value operations. */
912 DIE ("static calculation depends on load-time address");
913 push (dwbias
+ expr
[i
].number
);
916 case DW_OP_lit0
... DW_OP_lit31
:
917 value
= expr
[i
].atom
- DW_OP_lit0
;
930 value
= expr
[i
].number
;
935 /* Arithmetic operations. */
936 #define UNOP(dw_op, c_op) \
937 case DW_OP_##dw_op: \
940 push (c_op (stack[tos])); \
943 #define BINOP(dw_op, c_op) \
944 case DW_OP_##dw_op: \
948 push (stack[a] c_op stack[b]); \
952 #define op_abs(x) (x < 0 ? -x : x)
965 /* Comparisons are binary operators too. */
980 push ((Dwarf_Word
) stack
[a
] >> (Dwarf_Word
) stack
[b
]);
984 /* Simple addition we may be able to handle relative to
985 the starting register name. */
998 case DW_OP_plus_uconst
:
999 value
= expr
[i
].number
;
1001 if (stack_depth
> 0)
1003 /* It's just private diddling after all. */
1005 push (stack
[a
] + value
);
1011 /* This is the primary real-world case: the expression takes
1012 the input address and adds a constant offset. */
1014 while ((*input
)->type
== loc_noncontiguous
)
1016 /* We are starting from a noncontiguous object (DW_OP_piece).
1017 Find the piece we want. */
1019 struct location
*piece
= (*input
)->pieces
;
1020 while (piece
!= NULL
&& value
>= piece
->byte_size
)
1022 value
-= piece
->byte_size
;
1023 piece
= piece
->next
;
1026 DIE ("offset outside available pieces");
1031 switch ((*input
)->type
)
1035 /* The piece we want is actually in memory. Use the same
1036 program to compute the address from the preceding input. */
1038 struct location
*loc
= alloc_location (pool
, *input
);
1042 (*input
)->next
= loc
;
1045 /* The piece addresses exactly where we want to go. */
1051 /* Add a second fragment to offset the piece address. */
1052 obstack_printf (pool
, "%*saddr += " SFORMAT
"\n",
1053 indent
* 2, "", value
);
1054 *input
= loc
->next
= new_synthetic_loc (pool
, *input
,
1060 /* This expression keeps going, but further
1061 computations now have an address to start with.
1062 So we can punt to the address computation generator. */
1063 loc
= location_from_address (pool
, NULL
, NULL
, NULL
,
1064 indent
, dwbias
, attr
,
1065 &expr
[i
+ 1], len
- i
- 1,
1066 address
, input
, fb_attr
,
1072 /* That's all she wrote. */
1077 /* This piece (or the whole struct) fits in a register. */
1078 (*input
)->reg
.offset
+= value
;
1079 return head
?: *input
;
1082 /* This piece has a constant value. */
1083 if (value
>= (*input
)->byte_size
)
1084 DIE ("offset outside available constant block");
1085 (*input
)->constant_block
+= value
;
1086 (*input
)->byte_size
-= value
;
1087 return head
?: *input
;
1090 /* The piece we want is part of a computed value! */
1091 /* XXX implement me! */
1098 /* Direct register contents. */
1099 case DW_OP_reg0
... DW_OP_reg31
:
1104 /* Special magic. */
1106 DIE ("DW_OP_piece");
1109 case DW_OP_push_object_address
:
1110 DIE ("XXX DW_OP_push_object_address");
1114 DIE ("unrecognized operation");
1119 if (stack_depth
> 1)
1120 DIE ("multiple values left on stack");
1122 if (stack_depth
> 0) /* stack_depth == 1 */
1125 DIE ("multiple values left on stack");
1127 /* Could handle this if it ever actually happened. */
1128 DIE ("relative expression computed constant");
1135 DIE ("stack underflow");
1137 DIE ("cannot handle location expression");
1140 return lose (*input
, failure
, expr
, i
);
1144 /* Translate a C fragment for the location expression, using *INPUT
1145 as the starting location, begin from scratch if *INPUT is null.
1146 If DW_OP_fbreg is used, it may have a subfragment computing from
1147 the FB_ATTR location expression.
1149 On errors, call FAIL and never return. On success, return the
1150 first fragment created, which is also chained onto (*INPUT)->next.
1151 *INPUT is then updated with the new tail of that chain. */
1154 c_translate_location (struct obstack
*pool
,
1155 void (*fail
) (void *arg
, const char *fmt
, ...)
1156 __attribute__ ((noreturn
, format (printf
, 2, 3))),
1158 void (*emit_address
) (void *fail_arg
,
1159 struct obstack
*, Dwarf_Addr
),
1160 int indent
, Dwarf_Addr dwbias
, Dwarf_Addr pc_address
,
1161 Dwarf_Attribute
*attr
,
1162 const Dwarf_Op
*expr
, size_t len
,
1163 struct location
**input
, Dwarf_Attribute
*fb_attr
,
1164 const Dwarf_Op
*cfa_ops
)
1168 switch (*input
== NULL
? loc_address
: (*input
)->type
)
1171 /* We have a previous address computation.
1172 This expression will compute starting with that on the stack. */
1173 return location_from_address (pool
, fail
, fail_arg
,
1174 emit_address
?: &default_emit_address
,
1175 indent
, dwbias
, attr
, expr
, len
, pc_address
,
1176 input
, fb_attr
, cfa_ops
);
1178 case loc_noncontiguous
:
1182 /* The starting point is not an address computation, but a
1183 register or implicit value. We can only handle limited
1184 computations from here. */
1185 return location_relative (pool
, indent
, dwbias
, attr
, expr
, len
,
1186 pc_address
, input
, fb_attr
, cfa_ops
);
1196 /* Translate a DW_AT_const_value attribute as if it were a location of
1197 constant-value flavor. */
1200 c_translate_constant (struct obstack
*pool
,
1201 void (*fail
) (void *arg
,
1202 const char *fmt
, ...)
1203 __attribute__ ((noreturn
,
1204 format (printf
, 2, 3))),
1206 void (*emit_address
) (void *fail_arg
,
1209 int indent
, Dwarf_Addr dwbias
, Dwarf_Attribute
*attr
)
1213 struct location
*loc
= obstack_alloc (pool
, sizeof *loc
);
1215 loc
->fail_arg
= fail_arg
;
1216 loc
->emit_address
= emit_address
?: &default_emit_address
;
1218 loc
->frame_base
= NULL
;
1219 loc
->address
.stack_depth
= 0;
1220 loc
->address
.declare
= NULL
;
1221 loc
->address
.used_deref
= false;
1225 switch (dwarf_whatform (attr
))
1230 if (dwarf_formaddr (attr
, &addr
) != 0)
1232 FAIL (loc
, N_("cannot get constant address: %s"),
1236 loc
->type
= loc_value
;
1237 obstack_printf (pool
, "%*saddr = ", indent
* 2, "");
1238 (*loc
->emit_address
) (loc
->fail_arg
, pool
, dwbias
+ addr
);
1239 obstack_grow (pool
, ";\n", 3);
1240 loc
->address
.program
= obstack_finish (pool
);
1245 case DW_FORM_block1
:
1246 case DW_FORM_block2
:
1247 case DW_FORM_block4
:
1250 if (dwarf_formblock (attr
, &block
) != 0)
1252 FAIL (loc
, N_("cannot get constant block: %s"), dwarf_errmsg (-1));
1255 loc
->type
= loc_constant
;
1256 loc
->byte_size
= block
.length
;
1257 loc
->constant_block
= block
.data
;
1261 case DW_FORM_string
:
1264 const char *string
= dwarf_formstring (attr
);
1267 FAIL (loc
, N_("cannot get string constant: %s"), dwarf_errmsg (-1));
1270 loc
->type
= loc_constant
;
1271 loc
->byte_size
= strlen (string
) + 1;
1272 loc
->constant_block
= string
;
1279 if (dwarf_formsdata (attr
, &value
) != 0)
1281 FAIL (loc
, N_("cannot get constant value: %s"), dwarf_errmsg (-1));
1284 loc
->type
= loc_value
;
1285 obstack_printf (pool
, "%*saddr = %" PRId64
"L;\n",
1286 indent
* 2, "", value
);
1287 obstack_1grow (pool
, '\0');
1288 loc
->address
.program
= obstack_finish (pool
);
1297 /* Translate a C fragment for a direct argument VALUE. On errors, call FAIL,
1298 which should not return. Any later errors will use FAIL and FAIL_ARG from
1299 this translate call. On success, return the fragment created. */
1301 c_translate_argument (struct obstack
*pool
,
1302 void (*fail
) (void *arg
, const char *fmt
, ...)
1303 __attribute__ ((noreturn
, format (printf
, 2, 3))),
1305 void (*emit_address
) (void *fail_arg
,
1306 struct obstack
*, Dwarf_Addr
),
1307 int indent
, const char *value
)
1311 obstack_printf (pool
, "%*saddr = %s;\n", indent
* 2, "", value
);
1312 obstack_1grow (pool
, '\0');
1313 char *program
= obstack_finish (pool
);
1315 struct location
*loc
= obstack_alloc (pool
, sizeof *loc
);
1318 loc
->fail_arg
= fail_arg
;
1319 loc
->emit_address
= emit_address
?: &default_emit_address
;
1323 loc
->type
= loc_address
;
1324 loc
->frame_base
= NULL
;
1325 loc
->address
.declare
= NULL
;
1326 loc
->address
.program
= program
;
1327 loc
->address
.stack_depth
= 0;
1328 loc
->address
.used_deref
= false;
1334 /* Emit "uintNN_t TARGET = ...;". */
1336 emit_base_fetch (struct obstack
*pool
, Dwarf_Word byte_size
,
1337 bool signed_p
, const char *target
, struct location
*loc
)
1341 /* Emit size/signed coercion. */
1342 obstack_printf (pool
, "{ ");
1343 obstack_printf (pool
, "%sint%u_t value = ",
1344 (signed_p
? "" : "u"), (unsigned)(byte_size
* 8));
1349 obstack_printf (pool
, "addr;");
1353 if (byte_size
!= 0 && byte_size
!= (Dwarf_Word
) -1)
1354 obstack_printf (pool
, "deref (%" PRIu64
", addr);", byte_size
);
1356 obstack_printf (pool
, "deref (sizeof %s, addr);", target
);
1361 if (loc
->reg
.offset
!= 0)
1362 FAIL (loc
, N_("cannot handle offset into register in fetch"));
1363 obstack_printf (pool
, "fetch_register (%u);", loc
->reg
.regno
);
1366 case loc_noncontiguous
:
1367 FAIL (loc
, N_("noncontiguous location for base fetch"));
1375 obstack_printf (pool
, " %s = value; }", target
);
1379 /* Emit "... = RVALUE;". */
1381 emit_base_store (struct obstack
*pool
, Dwarf_Word byte_size
,
1382 const char *rvalue
, struct location
*loc
)
1387 if (byte_size
!= 0 && byte_size
!= (Dwarf_Word
) -1)
1388 obstack_printf (pool
, "store_deref (%" PRIu64
", addr, %s); ",
1391 obstack_printf (pool
, "store_deref (sizeof %s, addr, %s); ",
1396 if (loc
->reg
.offset
!= 0)
1397 FAIL (loc
, N_("cannot handle offset into register in store"));
1398 obstack_printf (pool
, "store_register (%u, %s);", loc
->reg
.regno
, rvalue
);
1401 case loc_noncontiguous
:
1402 FAIL (loc
, N_("noncontiguous location for base store"));
1406 FAIL (loc
, N_("location is computed value, cannot store"));
1410 FAIL (loc
, N_("location is constant value, cannot store"));
1422 /* Slice up an object into pieces no larger than MAX_PIECE_BYTES,
1423 yielding a loc_noncontiguous location unless LOC is small enough. */
1424 static struct location
*
1425 discontiguify (struct obstack
*pool
, int indent
, struct location
*loc
,
1426 Dwarf_Word total_bytes
, Dwarf_Word max_piece_bytes
)
1428 inline bool pieces_small_enough (void)
1430 if (loc
->type
!= loc_noncontiguous
)
1431 return (loc
->byte_size
?: total_bytes
) <= max_piece_bytes
;
1433 for (p
= loc
->pieces
; p
!= NULL
; p
= p
->next
)
1434 if (p
->byte_size
> max_piece_bytes
)
1439 if (pieces_small_enough ())
1442 struct location
*noncontig
= alloc_location (pool
, loc
);
1443 noncontig
->next
= NULL
;
1444 noncontig
->type
= loc_noncontiguous
;
1445 noncontig
->byte_size
= total_bytes
;
1446 noncontig
->pieces
= NULL
;
1447 struct location
**tailpiece
= &noncontig
->pieces
;
1448 inline void add (struct location
*piece
)
1451 tailpiece
= &piece
->next
;
1458 /* Synthesize a piece that sets "container_addr" to the computed
1459 address of the whole object. Each piece will refer to this. */
1460 obstack_printf (pool
, "%*scontainer_addr = addr;\n",
1462 loc
->next
= new_synthetic_loc (pool
, loc
, false);
1463 loc
->next
->byte_size
= loc
->byte_size
;
1464 loc
->next
->type
= loc_fragment
;
1465 loc
->next
->address
.declare
= "container_addr";
1468 /* Synthesize pieces that just compute "container_addr + N". */
1469 Dwarf_Word offset
= 0;
1470 while (total_bytes
- offset
> 0)
1472 Dwarf_Word size
= total_bytes
- offset
;
1473 if (size
> max_piece_bytes
)
1474 size
= max_piece_bytes
;
1476 obstack_printf (pool
, "%*saddr = container_addr + " UFORMAT
";\n",
1477 indent
* 2, "", offset
);
1478 struct location
*piece
= new_synthetic_loc (pool
, loc
, false);
1479 piece
->byte_size
= size
;
1491 Dwarf_Word offset
= 0;
1492 while (total_bytes
- offset
> 0)
1494 Dwarf_Word size
= total_bytes
- offset
;
1495 if (size
> max_piece_bytes
)
1496 size
= max_piece_bytes
;
1498 struct location
*piece
= alloc_location (pool
, loc
);
1500 piece
->type
= loc_constant
;
1501 piece
->byte_size
= size
;
1502 piece
->constant_block
= loc
->constant_block
+ offset
;
1513 FAIL (loc
, N_("stack value too big for fetch ???"));
1517 FAIL (loc
, N_("single register too big for fetch/store ???"));
1520 case loc_noncontiguous
:
1521 /* Could be handled if it ever happened. */
1522 FAIL (loc
, N_("discontiguify of noncontiguous location not supported"));
1530 loc
->next
= noncontig
;
1534 /* Make a fragment that declares a union such as:
1540 } pieces __attribute__ ((packed));
1545 declare_noncontig_union (struct obstack
*pool
, int indent
,
1546 struct location
**input
, struct location
*loc
,
1550 FAIL (loc
, N_("declaring noncontig union for depth > 9, too many pieces"));
1552 obstack_printf (pool
, "%*sunion {\n", indent
++ * 2, "");
1554 obstack_printf (pool
, "%*schar bytes[%" PRIu64
"];\n",
1555 indent
* 2, "", loc
->byte_size
);
1557 if (loc
->type
== loc_noncontiguous
)
1559 Dwarf_Word offset
= 0;
1561 obstack_printf (pool
, "%*sstruct {\n", indent
++ * 2, "");
1563 for (p
= loc
->pieces
; p
!= NULL
; p
= p
->next
)
1565 obstack_printf (pool
, "%*suint%" PRIu64
"_t p%" PRIu64
";\n",
1566 indent
* 2, "", p
->byte_size
* 8, offset
);
1567 offset
+= p
->byte_size
;
1570 obstack_printf (pool
, "%*s} pieces __attribute__ ((packed));\n",
1574 obstack_printf (pool
, "%*suint%" PRIu64
"_t whole;\n",
1575 indent
* 2, "", loc
->byte_size
* 8);
1577 obstack_printf (pool
, "%*s} u_pieces%d;\n", --indent
* 2, "", depth
);
1579 loc
= new_synthetic_loc (pool
, *input
, false);
1580 loc
->type
= loc_decl
;
1581 (*input
)->next
= loc
;
1585 /* Determine the byte size of a base type. */
1587 base_byte_size (Dwarf_Die
*typedie
, struct location
*origin
)
1589 assert (dwarf_tag (typedie
) == DW_TAG_base_type
||
1590 dwarf_tag (typedie
) == DW_TAG_enumeration_type
);
1592 Dwarf_Attribute attr_mem
;
1594 if (dwarf_attr_integrate (typedie
, DW_AT_byte_size
, &attr_mem
) != NULL
1595 && dwarf_formudata (&attr_mem
, &size
) == 0)
1599 N_("cannot get byte_size attribute for type %s: %s"),
1600 dwarf_diename (typedie
) ?: "<anonymous>",
1606 base_encoding (Dwarf_Die
*typedie
, struct location
*origin
)
1608 if (! (dwarf_tag (typedie
) == DW_TAG_base_type
||
1609 dwarf_tag (typedie
) == DW_TAG_enumeration_type
))
1612 Dwarf_Attribute attr_mem
;
1613 Dwarf_Word encoding
;
1614 if (dwarf_attr_integrate (typedie
, DW_AT_encoding
, &attr_mem
) != NULL
1615 && dwarf_formudata (&attr_mem
, &encoding
) == 0)
1621 N_("cannot get encoding attribute for type %s: %s"),
1622 dwarf_diename (typedie) ?: "<anonymous>",
1630 /* Fetch the bitfield parameters. */
1632 get_bitfield (struct location
*loc
,
1633 Dwarf_Die
*die
, Dwarf_Word
*bit_offset
, Dwarf_Word
*bit_size
)
1635 Dwarf_Attribute attr_mem
;
1636 if (dwarf_attr_integrate (die
, DW_AT_bit_offset
, &attr_mem
) == NULL
1637 || dwarf_formudata (&attr_mem
, bit_offset
) != 0
1638 || dwarf_attr_integrate (die
, DW_AT_bit_size
, &attr_mem
) == NULL
1639 || dwarf_formudata (&attr_mem
, bit_size
) != 0)
1640 FAIL (loc
, N_("cannot get bit field parameters: %s"), dwarf_errmsg (-1));
1643 /* Translate a fragment to fetch the base-type value of BYTE_SIZE bytes
1644 at the *INPUT location and store it in lvalue TARGET. */
1646 translate_base_fetch (struct obstack
*pool
, int indent
,
1647 Dwarf_Word byte_size
, bool signed_p
,
1648 struct location
**input
, const char *target
,
1653 if ((*input
)->type
== loc_noncontiguous
)
1655 struct location
*p
= (*input
)->pieces
;
1657 declare_noncontig_union (pool
, indent
, input
, *input
, depth
);
1659 Dwarf_Word offset
= 0;
1660 char piece
[sizeof "u_pieces?.pieces.p" + 20] = "u_pieces?.pieces.p";
1661 piece
[8] = (char) ('0' + depth
);
1662 int pdepth
= depth
+ 1;
1665 struct location
*newp
= obstack_alloc (pool
, sizeof *newp
);
1668 (*input
)->next
= newp
;
1671 snprintf (&piece
[sizeof "u_pieces?.pieces.p" - 1], 20,
1672 "%" PRIu64
, offset
);
1673 translate_base_fetch (pool
, indent
, p
->byte_size
, signed_p
/* ? */,
1674 input
, piece
, pdepth
);
1675 (*input
)->type
= loc_fragment
;
1677 offset
+= p
->byte_size
;
1682 obstack_printf (pool
, "%*s%s = u_pieces%d.whole;\n", indent
* 2,
1685 else if ((*input
)->type
== loc_constant
)
1687 const unsigned char *constant_block
= (*input
)->constant_block
;
1688 const size_t byte_size
= (*input
)->byte_size
;
1691 declare_noncontig_union (pool
, indent
, input
, *input
, depth
);
1693 for (i
= 0; i
< byte_size
; ++i
)
1694 obstack_printf (pool
, "%*su_pieces%d.bytes[%zu] = %#x;\n", indent
* 2,
1695 "", depth
, i
, constant_block
[i
]);
1697 obstack_printf (pool
, "%*s%s = u_pieces%d.whole;\n", indent
* 2,
1703 case 0: /* Special case, means address size. */
1708 obstack_printf (pool
, "%*s", indent
* 2, "");
1709 deref
= emit_base_fetch (pool
, byte_size
, signed_p
, target
, *input
);
1710 obstack_printf (pool
, "\n");
1714 /* Could handle this generating call to memcpy equivalent. */
1715 FAIL (*input
, N_("fetch is larger than base integer types"));
1719 struct location
*loc
= new_synthetic_loc (pool
, *input
, deref
);
1720 loc
->byte_size
= byte_size
;
1721 loc
->type
= loc_final
;
1722 (*input
)->next
= loc
;
1726 /* Determine the maximum size of a base type, from some DIE in the CU. */
1728 max_fetch_size (struct location
*loc
, Dwarf_Die
*die
)
1731 uint8_t address_size
;
1732 Dwarf_Die
*cu
= dwarf_diecu (die
, &cu_mem
, &address_size
, NULL
);
1734 FAIL (loc
, N_("cannot determine CU address size from %s: %s"),
1735 dwarf_diename (die
), dwarf_errmsg (-1));
1737 return address_size
;
1740 /* Translate a fragment to fetch the value of variable or member DIE
1741 at the *INPUT location and store it in lvalue TARGET. */
1743 c_translate_fetch (struct obstack
*pool
, int indent
,
1744 Dwarf_Addr dwbias
__attribute__ ((unused
)),
1745 Dwarf_Die
*die
, Dwarf_Die
*typedie
,
1746 struct location
**input
, const char *target
)
1750 Dwarf_Attribute size_attr
;
1751 Dwarf_Word byte_size
;
1752 if (dwarf_attr_integrate (die
, DW_AT_byte_size
, &size_attr
) == NULL
1753 || dwarf_formudata (&size_attr
, &byte_size
) != 0)
1754 byte_size
= base_byte_size (typedie
, *input
);
1756 Dwarf_Attribute encoding_attr
;
1757 Dwarf_Word encoding
;
1758 if (dwarf_attr_integrate (die
, DW_AT_encoding
, &encoding_attr
) == NULL
1759 || dwarf_formudata (&encoding_attr
, &encoding
) != 0)
1760 encoding
= base_encoding (typedie
, *input
);
1761 bool signed_p
= encoding
== DW_ATE_signed
|| encoding
== DW_ATE_signed_char
;
1763 *input
= discontiguify (pool
, indent
, *input
, byte_size
,
1764 max_fetch_size (*input
, die
));
1766 if (dwarf_hasattr_integrate (die
, DW_AT_bit_offset
))
1768 /* This is a bit field. Fetch the containing base type into a
1769 temporary variable. */
1771 translate_base_fetch (pool
, indent
, byte_size
, signed_p
, input
, "tmp", 0);
1772 (*input
)->type
= loc_fragment
;
1773 (*input
)->address
.declare
= "tmp";
1775 Dwarf_Word bit_offset
, bit_size
;
1776 get_bitfield (*input
, die
, &bit_offset
, &bit_size
);
1778 obstack_printf (pool
, "%*s"
1779 "fetch_bitfield (%s, tmp, %" PRIu64
", %" PRIu64
");\n",
1780 indent
*2, "", target
, bit_offset
, bit_size
);
1782 struct location
*loc
= new_synthetic_loc (pool
, *input
, false);
1783 loc
->type
= loc_final
;
1784 (*input
)->next
= loc
;
1788 translate_base_fetch (pool
, indent
, byte_size
, signed_p
, input
, target
, 0);
1791 /* Translate a fragment to store RVALUE into the base-type value of
1792 BYTE_SIZE bytes at the *INPUT location. */
1794 translate_base_store (struct obstack
*pool
, int indent
, Dwarf_Word byte_size
,
1795 struct location
**input
, struct location
*store_loc
,
1796 const char *rvalue
, int depth
)
1800 if (store_loc
->type
== loc_noncontiguous
)
1802 declare_noncontig_union (pool
, indent
, input
, store_loc
, depth
);
1804 obstack_printf (pool
, "%*su_pieces%d.whole = %s;\n", indent
* 2,
1806 struct location
*loc
= new_synthetic_loc (pool
, *input
, deref
);
1807 loc
->type
= loc_fragment
;
1808 (*input
)->next
= loc
;
1811 Dwarf_Word offset
= 0;
1812 char piece
[sizeof "u_pieces?.pieces.p" + 20] = "u_pieces?.pieces.p";
1813 piece
[8] = (char) ('0' + depth
);
1815 int pdepth
= depth
+ 1;
1816 for (p
= store_loc
->pieces
; p
!= NULL
; p
= p
->next
)
1818 struct location
*newp
= obstack_alloc (pool
, sizeof *newp
);
1821 (*input
)->next
= newp
;
1824 snprintf (&piece
[sizeof "u_pieces?.pieces.p" - 1], 20, "%" PRIu64
,
1826 translate_base_store (pool
, indent
,
1827 p
->byte_size
, input
, *input
, piece
, pdepth
++);
1828 (*input
)->type
= loc_fragment
;
1830 offset
+= p
->byte_size
;
1833 (*input
)->type
= loc_final
;
1843 obstack_printf (pool
, "%*s", indent
* 2, "");
1844 deref
= emit_base_store (pool
, byte_size
, rvalue
, store_loc
);
1845 obstack_printf (pool
, "\n");
1849 /* Could handle this generating call to memcpy equivalent. */
1850 FAIL (*input
, N_("store is larger than base integer types"));
1854 struct location
*loc
= new_synthetic_loc (pool
, *input
, deref
);
1855 loc
->type
= loc_final
;
1856 (*input
)->next
= loc
;
1861 /* Translate a fragment to fetch the value of variable or member DIE
1862 at the *INPUT location and store it in rvalue RVALUE. */
1865 c_translate_store (struct obstack
*pool
, int indent
,
1866 Dwarf_Addr dwbias
__attribute__ ((unused
)),
1867 Dwarf_Die
*die
, Dwarf_Die
*typedie
,
1868 struct location
**input
, const char *rvalue
)
1872 Dwarf_Attribute size_attr
;
1873 Dwarf_Word byte_size
;
1874 if (dwarf_attr_integrate (die
, DW_AT_byte_size
, &size_attr
) == NULL
1875 || dwarf_formudata (&size_attr
, &byte_size
) != 0)
1876 byte_size
= base_byte_size (typedie
, *input
);
1878 Dwarf_Attribute encoding_attr
;
1879 Dwarf_Word encoding
;
1880 if (dwarf_attr_integrate (die
, DW_AT_encoding
, &encoding_attr
) == NULL
1881 || dwarf_formudata (&encoding_attr
, &encoding
) != 0)
1882 encoding
= base_encoding (typedie
, *input
);
1883 bool signed_p
= (encoding
== DW_ATE_signed
1884 || encoding
== DW_ATE_signed_char
);
1886 *input
= discontiguify (pool
, indent
, *input
, byte_size
,
1887 max_fetch_size (*input
, die
));
1889 struct location
*store_loc
= *input
;
1891 if (dwarf_hasattr_integrate (die
, DW_AT_bit_offset
))
1893 /* This is a bit field. Fetch the containing base type into a
1894 temporary variable. */
1896 translate_base_fetch (pool
, indent
, byte_size
, signed_p
, input
, "tmp", 0);
1897 (*input
)->type
= loc_fragment
;
1898 (*input
)->address
.declare
= "tmp";
1900 Dwarf_Word bit_offset
, bit_size
;
1901 get_bitfield (*input
, die
, &bit_offset
, &bit_size
);
1903 obstack_printf (pool
, "%*s"
1904 "store_bitfield (tmp, %s, %" PRIu64
", %" PRIu64
");\n",
1905 indent
* 2, "", rvalue
, bit_offset
, bit_size
);
1907 struct location
*loc
= new_synthetic_loc (pool
, *input
, false);
1908 loc
->type
= loc_fragment
;
1909 (*input
)->next
= loc
;
1912 /* We have mixed RVALUE into the bits in "tmp".
1913 Now we'll store "tmp" back whence we fetched it. */
1917 translate_base_store (pool
, indent
, byte_size
, input
, store_loc
, rvalue
, 0);
1920 /* Translate a fragment to dereference the given pointer type,
1921 where *INPUT is the location of the pointer with that type.
1923 We chain on a loc_address program that yields this pointer value
1924 (i.e. the location of what it points to). */
1927 c_translate_pointer (struct obstack
*pool
, int indent
,
1928 Dwarf_Addr dwbias
__attribute__ ((unused
)),
1929 Dwarf_Die
*typedie
, struct location
**input
)
1931 assert (dwarf_tag (typedie
) == DW_TAG_pointer_type
||
1932 dwarf_tag (typedie
) == DW_TAG_reference_type
||
1933 dwarf_tag (typedie
) == DW_TAG_rvalue_reference_type
);
1935 Dwarf_Attribute attr_mem
;
1936 Dwarf_Word byte_size
;
1937 if (dwarf_attr_integrate (typedie
, DW_AT_byte_size
, &attr_mem
) == NULL
)
1939 else if (dwarf_formudata (&attr_mem
, &byte_size
) != 0)
1941 N_("cannot get byte_size attribute for type %s: %s"),
1942 dwarf_diename (typedie
) ?: "<anonymous>",
1945 bool signed_p
= false; /* XXX: Does not matter? */
1947 translate_base_fetch (pool
, indent
+ 1, byte_size
, signed_p
, input
, "addr", 0);
1948 (*input
)->type
= loc_address
;
1953 c_translate_addressof (struct obstack
*pool
, int indent
,
1954 Dwarf_Addr dwbias
__attribute__ ((unused
)),
1956 Dwarf_Die
*typedie
__attribute__ ((unused
)),
1957 struct location
**input
, const char *target
)
1961 if (dwarf_hasattr_integrate (die
, DW_AT_bit_offset
))
1962 FAIL (*input
, N_("cannot take the address of a bit field"));
1964 switch ((*input
)->type
)
1967 obstack_printf (pool
, "%*s%s = addr;\n", indent
* 2, "", target
);
1968 (*input
)->next
= new_synthetic_loc (pool
, *input
, false);
1969 (*input
)->next
->type
= loc_final
;
1973 FAIL (*input
, N_("cannot take address of object in register"));
1975 case loc_noncontiguous
:
1976 FAIL (*input
, N_("cannot take address of noncontiguous object"));
1979 FAIL (*input
, N_("cannot take address of computed value"));
1982 FAIL (*input
, N_("cannot take address of constant value"));
1992 /* Translate a fragment to write the given pointer value,
1993 where *INPUT is the location of the pointer with that type.
1997 c_translate_pointer_store (struct obstack
*pool
, int indent
,
1998 Dwarf_Addr dwbias
__attribute__ ((unused
)),
1999 Dwarf_Die
*typedie
, struct location
**input
,
2002 assert (dwarf_tag (typedie
) == DW_TAG_pointer_type
);
2004 Dwarf_Attribute attr_mem
;
2005 Dwarf_Word byte_size
;
2006 if (dwarf_attr_integrate (typedie
, DW_AT_byte_size
, &attr_mem
) == NULL
)
2008 else if (dwarf_formudata (&attr_mem
, &byte_size
) != 0)
2010 N_("cannot get byte_size attribute for type %s: %s"),
2011 dwarf_diename (typedie
) ?: "<anonymous>",
2014 translate_base_store (pool
, indent
+ 1, byte_size
,
2015 input
, *input
, rvalue
, 0);
2017 // XXX: what about multiple-location lvalues?
2020 /* Determine the element stride of an array type. */
2022 array_stride (Dwarf_Die
*typedie
, struct location
*origin
)
2024 Dwarf_Attribute attr_mem
;
2025 if (dwarf_attr_integrate (typedie
, DW_AT_byte_stride
, &attr_mem
) != NULL
)
2028 if (dwarf_formudata (&attr_mem
, &stride
) == 0)
2030 FAIL (origin
, N_("cannot get byte_stride attribute array type %s: %s"),
2031 dwarf_diename (typedie
) ?: "<anonymous>",
2036 if (dwarf_attr_integrate (typedie
, DW_AT_type
, &attr_mem
) == NULL
2037 || dwarf_formref_die (&attr_mem
, &die_mem
) == NULL
)
2038 FAIL (origin
, N_("cannot get element type of array type %s: %s"),
2039 dwarf_diename (typedie
) ?: "<anonymous>",
2042 int typetag
= dwarf_tag(&die_mem
);
2043 while (typetag
== DW_TAG_typedef
||
2044 typetag
== DW_TAG_const_type
||
2045 typetag
== DW_TAG_volatile_type
)
2047 if (dwarf_attr_integrate (&die_mem
, DW_AT_type
, &attr_mem
) == NULL
2048 || dwarf_formref_die (&attr_mem
, &die_mem
) == NULL
)
2049 FAIL (origin
, N_("cannot get inner type of type %s: %s"),
2050 dwarf_diename (&die_mem
) ?: "<anonymous>",
2052 typetag
= dwarf_tag(&die_mem
);
2055 if (dwarf_attr_integrate (&die_mem
, DW_AT_byte_size
, &attr_mem
) != NULL
)
2058 if (dwarf_formudata (&attr_mem
, &stride
) == 0)
2061 N_("cannot get byte_size attribute for array element type %s: %s"),
2062 dwarf_diename (&die_mem
) ?: "<anonymous>",
2066 FAIL (origin
, N_("confused about array element size"));
2071 c_translate_array (struct obstack
*pool
, int indent
,
2072 Dwarf_Addr dwbias
__attribute__ ((unused
)),
2073 Dwarf_Die
*typedie
, struct location
**input
,
2074 const char *idx
, Dwarf_Word const_idx
)
2076 assert (dwarf_tag (typedie
) == DW_TAG_array_type
||
2077 dwarf_tag (typedie
) == DW_TAG_pointer_type
);
2081 Dwarf_Word stride
= array_stride (typedie
, *input
);
2083 struct location
*loc
= *input
;
2084 while (loc
->type
== loc_noncontiguous
)
2087 FAIL (*input
, N_("cannot dynamically index noncontiguous array"));
2090 Dwarf_Word offset
= const_idx
* stride
;
2091 struct location
*piece
= loc
->pieces
;
2092 while (piece
!= NULL
&& offset
>= piece
->byte_size
)
2094 offset
-= piece
->byte_size
;
2095 piece
= piece
->next
;
2098 FAIL (*input
, N_("constant index is outside noncontiguous array"));
2099 if (offset
% stride
!= 0)
2100 FAIL (*input
, N_("noncontiguous array splits elements"));
2101 const_idx
= offset
/ stride
;
2111 obstack_printf (pool
, "%*saddr += %s * " UFORMAT
";\n",
2112 indent
* 2, "", idx
, stride
);
2114 obstack_printf (pool
, "%*saddr += " UFORMAT
" * " UFORMAT
";\n",
2115 indent
* 2, "", const_idx
, stride
);
2116 loc
= new_synthetic_loc (pool
, loc
, false);
2121 FAIL (*input
, N_("cannot index array stored in a register"));
2122 else if (const_idx
> max_fetch_size (loc
, typedie
) / stride
)
2123 FAIL (*input
, N_("constant index is outside array held in register"));
2126 loc
->reg
.offset
+= const_idx
* stride
;
2133 FAIL (*input
, N_("cannot index into constant value"));
2134 else if (const_idx
> loc
->byte_size
/ stride
)
2135 FAIL (*input
, N_("constant index is outside constant array value"));
2138 loc
->byte_size
= stride
;
2139 loc
->constant_block
+= const_idx
* stride
;
2145 FAIL (*input
, N_("cannot index into computed value"));
2153 (*input
)->next
= loc
;
2154 *input
= (*input
)->next
;
2158 /* Emitting C code for finalized fragments. */
2161 #define emit(fmt, ...) fprintf (out, fmt, ## __VA_ARGS__)
2163 /* Open a block with a comment giving the original DWARF expression. */
2165 emit_header (FILE *out
, struct location
*loc
, unsigned int hindent
)
2167 if (loc
->ops
== NULL
)
2168 emit ("%*s{ // synthesized\n", hindent
* 2, "");
2171 emit ("%*s{ // DWARF expression:", hindent
* 2, "");
2173 for (i
= 0; i
< loc
->nops
; ++i
)
2175 emit (" %#x", loc
->ops
[i
].atom
);
2176 if (loc
->ops
[i
].number2
== 0)
2178 if (loc
->ops
[i
].number
!= 0)
2179 emit ("(%" PRId64
")", loc
->ops
[i
].number
);
2182 emit ("(%" PRId64
",%" PRId64
")",
2183 loc
->ops
[i
].number
, loc
->ops
[i
].number2
);
2189 /* Emit a code fragment to assign the target variable to a register value. */
2191 emit_loc_register (FILE *out
, struct location
*loc
, unsigned int indent
,
2194 assert (loc
->type
== loc_register
);
2196 if (loc
->reg
.offset
!= 0)
2197 FAIL (loc
, N_("cannot handle offset into register in fetch"));
2199 emit ("%*s%s = fetch_register (%u);\n",
2200 indent
* 2, "", target
, loc
->reg
.regno
);
2203 /* Emit a code fragment to assign the target variable to an address. */
2205 emit_loc_address (FILE *out
, struct location
*loc
, unsigned int indent
,
2208 assert (loc
->type
== loc_address
|| loc
->type
== loc_value
);
2210 if (loc
->address
.stack_depth
== 0)
2211 /* Synthetic program. */
2212 emit ("%s", loc
->address
.program
);
2215 emit ("%*s{\n", indent
* 2, "");
2216 emit ("%*s%s " STACKFMT
, (indent
+ 1) * 2, "",
2217 stack_slot_type (loc
, false), 0);
2219 for (i
= 1; i
< loc
->address
.stack_depth
; ++i
)
2220 emit (", " STACKFMT
, i
);
2223 emit ("%s%*s%s = " STACKFMT
";\n", loc
->address
.program
,
2224 (indent
+ 1) * 2, "", target
, 0);
2225 emit ("%*s}\n", indent
* 2, "");
2229 /* Emit a code fragment to declare the target variable and
2230 assign it to an address-sized value. */
2232 emit_loc_value (FILE *out
, struct location
*loc
, unsigned int indent
,
2233 const char *target
, bool declare
,
2234 bool *used_deref
, unsigned int *max_stack
)
2237 emit ("%*s%s %s;\n", indent
* 2, "", stack_slot_type (loc
, false), target
);
2239 emit_header (out
, loc
, indent
++);
2248 emit_loc_register (out
, loc
, indent
, target
);
2253 emit_loc_address (out
, loc
, indent
, target
);
2254 *used_deref
= *used_deref
|| loc
->address
.used_deref
;
2255 if (loc
->address
.stack_depth
> *max_stack
)
2256 *max_stack
= loc
->address
.stack_depth
;
2260 emit ("%*s}\n", --indent
* 2, "");
2264 c_emit_location (FILE *out
, struct location
*loc
, int indent
,
2265 unsigned int *max_stack
)
2267 emit ("%*s{\n", indent
* 2, "");
2269 bool declared_addr
= false;
2271 for (l
= loc
; l
!= NULL
; l
= l
->next
)
2275 emit ("%s", l
->address
.program
);
2282 declared_addr
= true;
2283 l
->address
.declare
= "addr";
2286 if (l
->address
.declare
!= NULL
)
2288 if (l
->byte_size
== 0 || l
->byte_size
== (Dwarf_Word
) -1)
2289 emit ("%*s%s %s;\n", (indent
+ 1) * 2, "",
2290 stack_slot_type (l
, false), l
->address
.declare
);
2292 emit ("%*suint%" PRIu64
"_t %s;\n", (indent
+ 1) * 2, "",
2293 l
->byte_size
* 8, l
->address
.declare
);
2303 if (loc
->frame_base
!= NULL
)
2304 emit_loc_value (out
, loc
->frame_base
, indent
, "frame_base", true,
2307 for (; loc
->next
!= NULL
; loc
= loc
->next
)
2312 /* Emit the program fragment to calculate the address. */
2313 emit_loc_value (out
, loc
, indent
+ 1, "addr", false, &deref
, max_stack
);
2317 emit ("%s", loc
->address
.program
);
2318 deref
= deref
|| loc
->address
.used_deref
;
2323 case loc_noncontiguous
:
2325 /* These don't produce any code directly.
2326 The next address/final record incorporates the value. */
2329 case loc_final
: /* Should be last in chain! */
2335 if (loc
->type
!= loc_final
) /* Unfinished chain. */
2338 emit ("%s%*s}\n", loc
->address
.program
, indent
* 2, "");
2340 if (loc
->address
.stack_depth
> *max_stack
)
2341 *max_stack
= loc
->address
.stack_depth
;
2343 return deref
|| loc
->address
.used_deref
;
2348 /* vim: set sw=2 ts=8 cino=>4,n-2,{2,^-2,t0,(0,u0,w1,M1 : */