9 #include <elfutils/libdw.h>
10 #include <elfutils/version.h>
20 /* NB: PR10601 may make one suspect that intptr_t and uintptr_t aren't
21 right, for example on a 64-bit kernel targeting a 32-bit userspace
22 process. At least these types are always at least as wide as
23 userspace (since 64-bit userspace doesn't run on a 32-bit kernel).
24 So as long as deref() and {fetch,store}_register() widen/narrow
25 their underlying values to these, there should be no problem. */
27 #define STACK_TYPE "intptr_t" /* Must be the signed type. */
28 #define UTYPE "uintptr_t" /* Must be the unsigned type. */
29 #define SFORMAT "%" PRId64 "L"
30 #define UFORMAT "%" PRIu64 "UL"
31 #define AFORMAT "%#" PRIx64 "UL"
32 #define STACKFMT "s%u"
34 struct location_context
38 void (*fail
) (void *arg
, const char *fmt
, ...)
39 __attribute__ ((noreturn
, format (printf
, 2, 3)));
41 void (*emit_address
) (void *fail_arg
, struct obstack
*, Dwarf_Addr
);
43 Dwarf_Attribute
*attr
;
46 Dwarf_Attribute
*fb_attr
;
47 const Dwarf_Op
*cfa_ops
;
52 struct location
*next
;
54 struct location_context
*context
;
63 loc_address
, loc_register
, loc_noncontiguous
, loc_unavailable
,
64 loc_value
, loc_constant
, loc_implicit_pointer
,
65 loc_decl
, loc_fragment
, loc_final
67 struct location
*frame_base
;
70 struct /* loc_address, loc_value, loc_fragment, loc_final */
72 const char *declare
; /* Temporary that needs declared. */
73 char *program
; /* C fragment, leaves address in s0. */
74 unsigned int stack_depth
; /* Temporaries "s0..<N>" used by it. */
75 bool used_deref
; /* Program uses "deref" macro. */
77 struct /* loc_register */
82 struct location
*pieces
; /* loc_noncontiguous */
83 const void *constant_block
; /* loc_constant */
84 struct /* loc_implicit_pointer */
86 struct location
*target
;
92 /* Select the C type to use in the emitted code to represent slots in the
93 DWARF expression stack. For really proper semantics this should be the
94 target address size. */
96 stack_slot_type (struct location
*context
__attribute__ ((unused
)), bool sign
)
98 return sign
? STACK_TYPE
: UTYPE
;
101 static struct location
*
102 alloc_location (struct location_context
*ctx
)
104 struct location
*loc
= obstack_alloc (ctx
->pool
, sizeof *loc
);
107 loc
->frame_base
= NULL
;
113 #define FAIL(loc, fmt, ...) \
114 (*(loc)->context->fail) ((loc)->context->fail_arg, fmt, ## __VA_ARGS__)
117 default_emit_address (void *fail_arg
__attribute__ ((unused
)),
118 struct obstack
*pool
, Dwarf_Addr address
)
120 obstack_printf (pool
, AFORMAT
, address
);
123 static struct location_context
*
124 new_context (struct obstack
*pool
,
125 void (*fail
) (void *arg
, const char *fmt
, ...)
126 __attribute__ ((noreturn
, format (printf
, 2, 3))),
128 void (*emit_address
) (void *fail_arg
,
129 struct obstack
*, Dwarf_Addr
),
130 Dwarf_Addr dwbias
, Dwarf_Addr pc_address
,
131 Dwarf_Attribute
*attr
, Dwarf_Attribute
*fb_attr
,
132 const Dwarf_Op
*cfa_ops
)
134 struct location_context
*ctx
= obstack_alloc (pool
, sizeof *ctx
);
137 ctx
->fail_arg
= fail_arg
;
138 ctx
->emit_address
= emit_address
?: &default_emit_address
;
140 ctx
->dwbias
= dwbias
;
141 ctx
->pc
= pc_address
;
142 ctx
->fb_attr
= fb_attr
;
143 ctx
->cfa_ops
= cfa_ops
;
147 /* Translate a DW_AT_const_value attribute as if it were a location of
148 constant-value flavor. */
150 static struct location
*
151 translate_constant (struct location_context
*ctx
, int indent
,
152 Dwarf_Attribute
*attr
)
156 struct location
*loc
= obstack_alloc (ctx
->pool
, sizeof *loc
);
160 loc
->frame_base
= NULL
;
161 loc
->address
.stack_depth
= 0;
162 loc
->address
.declare
= NULL
;
163 loc
->address
.used_deref
= false;
167 switch (dwarf_whatform (attr
))
172 if (dwarf_formaddr (attr
, &addr
) != 0)
174 //TRANSLATORS: failure to find an address that was a constant literal number
175 FAIL (loc
, N_("cannot get constant address: %s"),
179 loc
->type
= loc_value
;
180 obstack_printf (ctx
->pool
, "%*saddr = ", indent
* 2, "");
181 (*ctx
->emit_address
) (ctx
->fail_arg
, ctx
->pool
, ctx
->dwbias
+ addr
);
182 obstack_grow (ctx
->pool
, ";\n", 3);
183 loc
->address
.program
= obstack_finish (ctx
->pool
);
193 if (dwarf_formblock (attr
, &block
) != 0)
195 FAIL (loc
, N_("cannot get constant block: %s"), dwarf_errmsg (-1));
198 loc
->type
= loc_constant
;
199 loc
->byte_size
= block
.length
;
200 loc
->constant_block
= block
.data
;
207 const char *string
= dwarf_formstring (attr
);
210 FAIL (loc
, N_("cannot get string constant: %s"), dwarf_errmsg (-1));
213 loc
->type
= loc_constant
;
214 loc
->byte_size
= strlen (string
) + 1;
215 loc
->constant_block
= string
;
222 if (dwarf_formsdata (attr
, &value
) != 0)
224 FAIL (loc
, N_("cannot get constant value: %s"), dwarf_errmsg (-1));
227 loc
->type
= loc_value
;
228 obstack_printf (ctx
->pool
, "%*saddr = %" PRId64
"L;\n",
229 indent
* 2, "", value
);
230 obstack_1grow (ctx
->pool
, '\0');
231 loc
->address
.program
= obstack_finish (ctx
->pool
);
240 c_translate_constant (struct obstack
*pool
,
241 void (*fail
) (void *arg
,
242 const char *fmt
, ...)
243 __attribute__ ((noreturn
,
244 format (printf
, 2, 3))),
246 void (*emit_address
) (void *fail_arg
,
249 int indent
, Dwarf_Addr dwbias
, Dwarf_Attribute
*attr
)
251 return translate_constant (new_context (pool
, fail
, fail_arg
, emit_address
,
252 dwbias
, 0, attr
, NULL
, NULL
),
256 #if _ELFUTILS_PREREQ (0, 149)
257 static struct location
*location_from_attr (struct location_context
*ctx
,
258 int indent
, Dwarf_Attribute
*attr
);
262 /* Synthesize a new loc_address using the program on the obstack. */
263 static struct location
*
264 new_synthetic_loc (struct location
*origin
, bool deref
)
266 obstack_1grow (origin
->context
->pool
, '\0');
267 char *program
= obstack_finish (origin
->context
->pool
);
269 struct location
*loc
= alloc_location (origin
->context
);
272 loc
->type
= loc_address
;
273 loc
->address
.program
= program
;
274 loc
->address
.stack_depth
= 0;
275 loc
->address
.declare
= NULL
;
276 loc
->address
.used_deref
= deref
;
278 if (origin
->type
== loc_register
)
280 loc
->ops
= origin
->ops
;
281 loc
->nops
= origin
->nops
;
293 /* Die in the middle of an expression. */
294 static struct location
*
295 lose (struct location
*loc
, const Dwarf_Op
*lexpr
, size_t len
,
296 const char *failure
, size_t i
)
298 if (lexpr
== NULL
|| i
>= len
)
299 FAIL (loc
, "%s", failure
);
301 FAIL (loc
, N_("%s in DWARF expression [%Zu] at %" PRIu64
302 " (%#x: %" PRId64
", %" PRId64
")"),
303 failure
, i
, lexpr
[i
].offset
,
304 lexpr
[i
].atom
, lexpr
[i
].number
, lexpr
[i
].number2
);
309 /* Translate a (constrained) DWARF expression into C code
310 emitted to the obstack POOL. INDENT is the number of indentation levels.
311 ADDRBIAS is the difference between runtime and Dwarf info addresses.
312 INPUT is null or an expression to be initially pushed on the stack.
313 If NEED_FB is null, fail on DW_OP_fbreg, else set *NEED_FB to true
314 and emit "frame_base" for it. On success, set *MAX_STACK to the number
315 of stack slots required. On failure, set *LOSER to the index in EXPR
316 of the operation we could not handle.
318 Returns a failure message or null for success. */
321 translate (struct location_context
*ctx
, int indent
,
322 const Dwarf_Op
*expr
, const size_t len
,
323 struct location
*input
, bool *need_fb
, size_t *loser
,
324 struct location
*loc
)
329 #define DIE(msg) return (*loser = i, N_(msg))
331 #define emit(fmt, ...) obstack_printf (ctx->pool, fmt, ## __VA_ARGS__)
333 unsigned int stack_depth
;
334 unsigned int max_stack
= 0;
335 inline void deepen (void)
337 if (stack_depth
== max_stack
)
342 if (stack_depth > 0) \
344 else if (tos_register != -1) \
345 fetch_tos_register (); \
348 int var = stack_depth
349 #define PUSH (deepen (), stack_depth++)
350 #define STACK(idx) (stack_depth - 1 - (idx))
352 /* Don't put stack operations in the arguments to this. */
353 #define push(fmt, ...) \
354 emit ("%*s" STACKFMT " = " fmt ";\n", indent * 2, "", PUSH, ## __VA_ARGS__)
357 inline void fetch_tos_register (void)
360 emit ("%*s" STACKFMT
" = fetch_register (%d);\n",
361 indent
* 2, "", stack_depth
, tos_register
);
366 Dwarf_Block implicit_value
;
368 const Dwarf_Op
*implicit_pointer
;
370 /* Initialize our state for handling each new piece. */
376 implicit_value
.data
= NULL
;
377 implicit_pointer
= NULL
;
393 tos_register
= input
->reg
.regno
;
403 inline const char *finish (struct location
*piece
)
405 if (piece
->nops
== 0)
407 assert (stack_depth
== 0);
408 assert (tos_register
== -1);
409 assert (obstack_object_size (ctx
->pool
) == 0);
410 piece
->type
= loc_unavailable
;
412 else if (stack_depth
>= 1)
414 /* The top of stack has our value.
415 Other stack slots left don't matter. */
416 obstack_1grow (ctx
->pool
, '\0');
417 char *program
= obstack_finish (ctx
->pool
);
418 if (implicit_pointer
!= NULL
)
420 piece
->type
= loc_implicit_pointer
;
421 piece
->pointer
.offset
= implicit_pointer
->number2
;
422 #if !_ELFUTILS_PREREQ (0, 149)
423 /* Then how did we get here? */
426 Dwarf_Attribute target
;
427 if (dwarf_getlocation_implicit_pointer (ctx
->attr
,
430 DIE ("invalid implicit pointer");
431 switch (dwarf_whatattr (&target
))
433 case DW_AT_const_value
:
434 piece
->pointer
.target
= translate_constant (ctx
, indent
,
438 piece
->pointer
.target
= location_from_attr (ctx
, indent
,
442 DIE ("unexpected implicit pointer attribute!");
447 else if (implicit_value
.data
== NULL
)
449 piece
->type
= tos_value
? loc_value
: loc_address
;
450 piece
->address
.declare
= NULL
;
451 piece
->address
.program
= program
;
452 piece
->address
.stack_depth
= max_stack
;
453 piece
->address
.used_deref
= used_deref
;
457 piece
->type
= loc_constant
;
458 piece
->byte_size
= implicit_value
.length
;
459 piece
->constant_block
= implicit_value
.data
;
462 else if (tos_register
== -1)
463 DIE ("stack underflow");
464 else if (obstack_object_size (ctx
->pool
) != 0)
465 DIE ("register value must stand alone in location expression");
468 piece
->type
= loc_register
;
469 piece
->reg
.regno
= tos_register
;
470 piece
->reg
.offset
= 0;
476 struct location
*pieces
= NULL
, **tailpiece
= &pieces
;
477 size_t piece_expr_start
= 0;
478 Dwarf_Word piece_total_bytes
= 0;
479 for (i
= 0; i
< len
; ++i
)
485 inline bool more_ops (void)
487 return (expr
[i
].atom
!= DW_OP_nop
488 && expr
[i
].atom
!= DW_OP_piece
489 && expr
[i
].atom
!= DW_OP_bit_piece
);
492 if (tos_value
&& more_ops ())
493 DIE ("operations follow DW_OP_stack_value");
495 if (implicit_value
.data
!= NULL
&& more_ops ())
496 DIE ("operations follow DW_OP_implicit_value");
498 if (implicit_pointer
!= NULL
&& more_ops ())
499 DIE ("operations follow DW_OP_GNU_implicit_pointer");
501 switch (expr
[i
].atom
)
503 /* Basic stack operations. */
512 unsigned int tos
= STACK (0);
513 push (STACKFMT
, tos
);
520 emit ("%*s/* drop " STACKFMT
"*/\n", indent
* 2, "", ignore
);
527 if (sp
>= stack_depth
)
540 deepen (); /* Use a temporary slot. */
542 STACKFMT
" = " STACKFMT
", "
543 STACKFMT
" = " STACKFMT
", "
544 STACKFMT
" = " STACKFMT
";\n",
546 STACK (-1), STACK (0),
547 STACK (0), STACK (1),
548 STACK (1), STACK (-1));
554 deepen (); /* Use a temporary slot. */
556 STACKFMT
" = " STACKFMT
", "
557 STACKFMT
" = " STACKFMT
", "
558 STACKFMT
" = " STACKFMT
", "
559 STACKFMT
" = " STACKFMT
";\n",
561 STACK (-1), STACK (0),
562 STACK (0), STACK (1),
563 STACK (1), STACK (2),
564 STACK (3), STACK (-1));
568 /* Control flow operations. */
571 Dwarf_Off target
= expr
[i
].offset
+ 3 + expr
[i
].number
;
572 while (i
+ 1 < len
&& expr
[i
+ 1].offset
< target
)
574 if (expr
[i
+ 1].offset
!= target
)
575 DIE ("invalid skip target");
580 DIE ("conditional branches not supported");
588 push ("deref (sizeof (void *), " STACKFMT
")", addr
);
593 case DW_OP_deref_size
:
596 push ("deref (" UFORMAT
", " STACKFMT
")",
597 expr
[i
].number
, addr
);
606 push ("xderef (sizeof (void *), " STACKFMT
", " STACKFMT
")",
612 case DW_OP_xderef_size
:
616 push ("xderef (" UFORMAT
", " STACKFMT
", " STACKFMT
")",
617 expr
[i
].number
, addr
, as
);
622 /* Constant-value operations. */
625 emit ("%*s" STACKFMT
" = ", indent
* 2, "", PUSH
);
626 (*ctx
->emit_address
) (ctx
->fail_arg
, ctx
->pool
,
627 ctx
->dwbias
+ expr
[i
].number
);
631 case DW_OP_lit0
... DW_OP_lit31
:
632 value
= expr
[i
].atom
- DW_OP_lit0
;
645 value
= expr
[i
].number
;
647 push (SFORMAT
, value
);
650 /* Arithmetic operations. */
651 #define UNOP(dw_op, c_op) \
652 case DW_OP_##dw_op: \
655 push ("%s (" STACKFMT ")", #c_op, tos); \
658 #define BINOP(dw_op, c_op) \
659 case DW_OP_##dw_op: \
663 push (STACKFMT " %s " STACKFMT, a, #c_op, b); \
679 /* Comparisons are binary operators too. */
694 push ("(%s) " STACKFMT
" >> (%s)" STACKFMT
,
695 stack_slot_type (loc
, true), a
,
696 stack_slot_type (loc
, true), b
);
704 push ("dwarf_div_op((%s) " STACKFMT
", (%s) " STACKFMT
")",
705 stack_slot_type (loc
, true), a
,
706 stack_slot_type (loc
, true), b
);
715 push ("dwarf_mod_op((%s) " STACKFMT
", (%s) " STACKFMT
")",
716 stack_slot_type (loc
, false), a
,
717 stack_slot_type (loc
, false), b
);
722 case DW_OP_plus_uconst
:
725 push (STACKFMT
" + " UFORMAT
, x
, expr
[i
].number
);
730 /* Register-relative addressing. */
731 case DW_OP_breg0
... DW_OP_breg31
:
732 reg
= expr
[i
].atom
- DW_OP_breg0
;
733 value
= expr
[i
].number
;
737 reg
= expr
[i
].number
;
738 value
= expr
[i
].number2
;
740 push ("fetch_register (%u) + " SFORMAT
, reg
, value
);
745 DIE ("DW_OP_fbreg from DW_AT_frame_base");
747 push ("frame_base + " SFORMAT
, expr
[i
].number
);
750 /* Direct register contents. */
751 case DW_OP_reg0
... DW_OP_reg31
:
752 reg
= expr
[i
].atom
- DW_OP_reg0
;
756 reg
= expr
[i
].number
;
764 /* If this ever happens we could copy the program. */
765 DIE ("DW_OP_piece left multiple values on stack");
768 /* The obstack has a pending program for loc_address,
769 so we must finish that piece off before we can
771 struct location temp_piece
=
773 .context
= loc
->context
,
775 .ops
= &expr
[piece_expr_start
],
776 .nops
= i
- piece_expr_start
,
778 const char *failure
= finish (&temp_piece
);
782 struct location
*piece
= obstack_alloc (ctx
->pool
, sizeof *piece
);
785 piece_expr_start
= i
+ 1;
787 piece_total_bytes
+= piece
->byte_size
= expr
[i
].number
;
790 tailpiece
= &piece
->next
;
793 /* Reset default conditions for handling the next piece. */
798 case DW_OP_stack_value
:
800 DIE ("DW_OP_stack_value left multiple values on stack");
803 /* Fetch a register to top of stack, or check for underflow.
804 Then mark the TOS as being a value. */
812 case DW_OP_implicit_value
:
813 if (ctx
->attr
== NULL
)
814 DIE ("DW_OP_implicit_value used in invalid context"
815 " (no DWARF attribute, ABI return value location?)");
817 /* It's supposed to appear by itself, except for DW_OP_piece. */
818 if (stack_depth
!= 0)
819 DIE ("DW_OP_implicit_value follows stack operations");
821 #if _ELFUTILS_PREREQ (0, 143)
822 if (dwarf_getlocation_implicit_value (ctx
->attr
,
823 (Dwarf_Op
*) &expr
[i
],
824 &implicit_value
) != 0)
825 DIE ("dwarf_getlocation_implicit_value failed");
827 /* Fake top of stack: implicit_value being set marks it. */
832 DIE ("DW_OP_implicit_value not supported");
835 #if _ELFUTILS_PREREQ (0, 149)
836 case DW_OP_GNU_implicit_pointer
:
837 implicit_pointer
= &expr
[i
];
838 /* Fake top of stack: implicit_pointer being set marks it. */
843 case DW_OP_call_frame_cfa
:
844 // We pick this out when processing DW_AT_frame_base in
845 // so it really shouldn't turn up here.
847 DIE ("DW_OP_call_frame_cfa while processing frame base");
849 DIE ("DW_OP_call_frame_cfa not expected outside DW_AT_frame_base");
852 case DW_OP_push_object_address
:
853 DIE ("XXX DW_OP_push_object_address");
857 DIE ("unrecognized operation");
865 if (piece_expr_start
!= i
)
866 DIE ("extra operations after last DW_OP_piece");
868 loc
->type
= loc_noncontiguous
;
869 loc
->pieces
= pieces
;
870 loc
->byte_size
= piece_total_bytes
;
875 DIE ("stack underflow");
885 /* Translate a location starting from an address or nothing. */
886 static struct location
*
887 location_from_address (struct location_context
*ctx
, int indent
,
888 const Dwarf_Op
*expr
, size_t len
,
889 struct location
**input
)
891 struct location
*loc
= obstack_alloc (ctx
->pool
, sizeof *loc
);
894 loc
->frame_base
= NULL
;
898 bool need_fb
= false;
900 const char *failure
= translate (ctx
, indent
+ 1, expr
, len
,
901 *input
, &need_fb
, &loser
, loc
);
903 return lose (loc
, expr
, len
, failure
, loser
);
908 /* The main expression uses DW_OP_fbreg, so we need to compute
909 the DW_AT_frame_base attribute expression's value first. */
911 if (ctx
->fb_attr
== NULL
)
912 FAIL (loc
, N_("required DW_AT_frame_base attribute not supplied"));
916 switch (dwarf_getlocation_addr (ctx
->fb_attr
, ctx
->pc
,
917 &fb_expr
, &fb_len
, 1))
919 case 1: /* Should always happen. */
921 goto fb_inaccessible
;
924 default: /* Shouldn't happen. */
926 FAIL (loc
, N_("dwarf_getlocation_addr (form %#x): %s"),
927 dwarf_whatform (ctx
->fb_attr
), dwarf_errmsg (-1));
930 case 0: /* Shouldn't happen. */
932 FAIL (loc
, N_("DW_AT_frame_base not accessible at this address"));
936 // If it is DW_OP_call_frame_cfa then get cfi cfa ops.
937 const Dwarf_Op
* fb_ops
;
938 if (fb_len
== 1 && fb_expr
[0].atom
== DW_OP_call_frame_cfa
)
940 if (ctx
->cfa_ops
== NULL
)
941 FAIL (loc
, N_("No cfa_ops supplied, but needed by DW_OP_call_frame_cfa"));
942 fb_ops
= ctx
->cfa_ops
;
947 loc
->frame_base
= alloc_location (ctx
);
948 failure
= translate (ctx
, indent
+ 1, fb_ops
, fb_len
, NULL
,
949 NULL
, &loser
, loc
->frame_base
);
951 return lose (loc
, fb_ops
, fb_len
, failure
, loser
);
955 (*input
)->next
= loc
;
961 #if _ELFUTILS_PREREQ (0, 149)
962 static struct location
*
963 location_from_attr (struct location_context
*ctx
, int indent
,
964 Dwarf_Attribute
*attr
)
968 switch (dwarf_getlocation_addr (attr
, ctx
->pc
, &expr
, &len
, 1))
970 case 1: /* Should always happen. */
975 case 0: /* Shouldn't happen. */
976 (*ctx
->fail
) (ctx
->fail_arg
, N_("not accessible at this address (%#" PRIx64
")"), ctx
->pc
);
979 default: /* Shouldn't happen. */
981 (*ctx
->fail
) (ctx
->fail_arg
, "dwarf_getlocation_addr: %s",
986 struct location
*input
= NULL
;
987 return location_from_address (ctx
, indent
, expr
, len
, &input
);
991 static struct location
*
992 translate_offset (int indent
, const Dwarf_Op
*expr
, size_t len
, size_t i
,
993 struct location
*head
, struct location
**input
,
996 struct location_context
*const ctx
= (*input
)->context
;
998 #define DIE(msg) return lose (*input, expr, len, N_(msg), i)
1000 while ((*input
)->type
== loc_noncontiguous
)
1002 /* We are starting from a noncontiguous object (DW_OP_piece).
1003 Find the piece we want. */
1005 struct location
*piece
= (*input
)->pieces
;
1006 while (piece
!= NULL
&& offset
>= piece
->byte_size
)
1008 offset
-= piece
->byte_size
;
1009 piece
= piece
->next
;
1012 DIE ("offset outside available pieces");
1014 assert ((*input
)->next
== NULL
);
1015 (*input
)->next
= piece
;
1019 switch ((*input
)->type
)
1023 /* The piece we want is actually in memory. Use the same
1024 program to compute the address from the preceding input. */
1026 struct location
*loc
= obstack_alloc (ctx
->pool
, sizeof *loc
);
1030 (*input
)->next
= loc
;
1033 /* The piece addresses exactly where we want to go. */
1039 /* Add a second fragment to offset the piece address. */
1040 obstack_printf (ctx
->pool
, "%*saddr += " SFORMAT
"\n",
1041 indent
* 2, "", offset
);
1042 *input
= loc
->next
= new_synthetic_loc (*input
, false);
1045 /* That's all she wrote. */
1050 /* This piece (or the whole struct) fits in a register. */
1051 (*input
)->reg
.offset
+= offset
;
1052 return head
?: *input
;
1055 /* This piece has a constant offset. */
1056 if (offset
>= (*input
)->byte_size
)
1057 DIE ("offset outside available constant block");
1058 (*input
)->constant_block
+= offset
;
1059 (*input
)->byte_size
-= offset
;
1060 return head
?: *input
;
1062 case loc_implicit_pointer
:
1063 /* This piece is an implicit pointer. */
1064 (*input
)->pointer
.offset
+= offset
;
1065 return head
?: *input
;
1067 case loc_unavailable
:
1068 /* Let it be diagnosed later. */
1069 return head
?: *input
;
1072 /* The piece we want is part of a computed offset.
1073 If it's the whole thing, we are done. */
1075 return head
?: *input
;
1076 DIE ("XXX extract partial rematerialized value");
1087 /* Translate a location starting from a non-address "on the top of the
1088 stack". The *INPUT location is a register name or noncontiguous
1089 object specification, and this expression wants to find the "address"
1090 of an object (or the actual value) relative to that "address". */
1092 static struct location
*
1093 location_relative (struct location_context
*ctx
, int indent
,
1094 const Dwarf_Op
*expr
, size_t len
,
1095 struct location
**input
)
1097 Dwarf_Sword
*stack
= NULL
;
1098 unsigned int stack_depth
= 0, max_stack
= 0;
1099 inline void deepen (void)
1101 if (stack_depth
== max_stack
)
1104 obstack_blank (ctx
->pool
, sizeof stack
[0]);
1105 stack
= (void *) obstack_base (ctx
->pool
);
1110 if (stack_depth > 0) \
1114 int var = stack_depth
1115 #define PUSH (deepen (), stack_depth++)
1116 #define STACK(idx) (stack_depth - 1 - (idx))
1117 #define STACKWORD(idx) stack[STACK (idx)]
1119 /* Don't put stack operations in the arguments to this. */
1120 #define push(value) (stack[PUSH] = (value))
1122 const char *failure
= NULL
;
1123 #define DIE(msg) do { failure = N_(msg); goto fail; } while (0)
1125 struct location
*head
= NULL
;
1127 for (i
= 0; i
< len
; ++i
)
1132 switch (expr
[i
].atom
)
1134 /* Basic stack operations. */
1139 if (stack_depth
< 1)
1143 unsigned int tos
= STACK (0);
1149 if (stack_depth
> 0)
1151 else if (*input
!= NULL
)
1152 /* Mark that we have consumed the input. */
1155 /* Hits if cleared above, or if we had no input at all. */
1160 sp
= expr
[i
].number
;
1162 if (sp
>= stack_depth
)
1173 if (stack_depth
< 2)
1175 deepen (); /* Use a temporary slot. */
1176 STACKWORD (-1) = STACKWORD (0);
1177 STACKWORD (0) = STACKWORD (1);
1178 STACKWORD (1) = STACKWORD (-1);
1182 if (stack_depth
< 3)
1184 deepen (); /* Use a temporary slot. */
1185 STACKWORD (-1) = STACKWORD (0);
1186 STACKWORD (0) = STACKWORD (1);
1187 STACKWORD (2) = STACKWORD (2);
1188 STACKWORD (2) = STACKWORD (-1);
1192 /* Control flow operations. */
1196 if (stack
[taken
] == 0)
1203 Dwarf_Off target
= expr
[i
].offset
+ 3 + expr
[i
].number
;
1204 while (i
+ 1 < len
&& expr
[i
+ 1].offset
< target
)
1206 if (expr
[i
+ 1].offset
!= target
)
1207 DIE ("invalid skip target");
1211 /* Memory access. */
1213 case DW_OP_deref_size
:
1215 case DW_OP_xderef_size
:
1217 /* Register-relative addressing. */
1218 case DW_OP_breg0
... DW_OP_breg31
:
1222 /* This started from a register, but now it's following a pointer.
1223 So we can do the translation starting from address here. */
1224 return location_from_address (ctx
, indent
, expr
, len
, input
);
1227 /* Constant-value operations. */
1229 DIE ("static calculation depends on load-time address");
1230 push (ctx
->dwbias
+ expr
[i
].number
);
1233 case DW_OP_lit0
... DW_OP_lit31
:
1234 value
= expr
[i
].atom
- DW_OP_lit0
;
1247 value
= expr
[i
].number
;
1252 /* Arithmetic operations. */
1253 #define UNOP(dw_op, c_op) \
1254 case DW_OP_##dw_op: \
1257 push (c_op (stack[tos])); \
1260 #define BINOP(dw_op, c_op) \
1261 case DW_OP_##dw_op: \
1265 push (stack[a] c_op stack[b]); \
1269 #define op_abs(x) (x < 0 ? -x : x)
1282 /* Comparisons are binary operators too. */
1297 push ((Dwarf_Word
) stack
[a
] >> (Dwarf_Word
) stack
[b
]);
1301 /* Simple addition we may be able to handle relative to
1302 the starting register name. */
1306 value
= -stack
[tos
];
1315 case DW_OP_plus_uconst
:
1316 value
= expr
[i
].number
;
1318 if (stack_depth
> 0)
1320 /* It's just private diddling after all. */
1322 push (stack
[a
] + value
);
1328 /* This is the primary real-world case: the expression takes
1329 the input address and adds a constant offset. */
1331 head
= translate_offset (indent
, expr
, len
, i
, head
, input
, value
);
1332 if (head
!= NULL
&& i
+ 1 < len
)
1334 if ((*input
)->type
!= loc_address
)
1335 DIE ("too much computation for non-address location");
1337 /* This expression keeps going, but further
1338 computations now have an address to start with.
1339 So we can punt to the address computation generator. */
1340 struct location
*loc
= location_from_address
1341 (ctx
, indent
, &expr
[i
+ 1], len
- i
- 1, input
);
1347 /* Direct register contents. */
1348 case DW_OP_reg0
... DW_OP_reg31
:
1353 /* Special magic. */
1355 DIE ("DW_OP_piece");
1358 case DW_OP_push_object_address
:
1359 DIE ("XXX DW_OP_push_object_address");
1363 DIE ("unrecognized operation");
1368 if (stack_depth
> 1)
1369 DIE ("multiple values left on stack");
1371 if (stack_depth
> 0) /* stack_depth == 1 */
1374 DIE ("multiple values left on stack");
1376 /* Could handle this if it ever actually happened. */
1377 DIE ("relative expression computed constant");
1384 DIE ("stack underflow");
1386 DIE ("cannot handle location expression");
1389 return lose (*input
, expr
, len
, failure
, i
);
1392 /* Translate a C fragment for the location expression, using *INPUT
1393 as the starting location, begin from scratch if *INPUT is null.
1394 If DW_OP_fbreg is used, it may have a subfragment computing from
1395 the FB_ATTR location expression.
1397 On errors, call FAIL and never return. On success, return the
1398 first fragment created, which is also chained onto (*INPUT)->next.
1399 *INPUT is then updated with the new tail of that chain. */
1402 c_translate_location (struct obstack
*pool
,
1403 void (*fail
) (void *arg
, const char *fmt
, ...)
1404 __attribute__ ((noreturn
, format (printf
, 2, 3))),
1406 void (*emit_address
) (void *fail_arg
,
1407 struct obstack
*, Dwarf_Addr
),
1408 int indent
, Dwarf_Addr dwbias
, Dwarf_Addr pc_address
,
1409 Dwarf_Attribute
*attr
,
1410 const Dwarf_Op
*expr
, size_t len
,
1411 struct location
**input
, Dwarf_Attribute
*fb_attr
,
1412 const Dwarf_Op
*cfa_ops
)
1416 struct location_context
*ctx
;
1418 ctx
= new_context (pool
, fail
, fail_arg
, emit_address
, dwbias
, pc_address
,
1419 attr
, fb_attr
, cfa_ops
);
1422 ctx
= (*input
)->context
;
1423 assert (ctx
->pool
== pool
);
1424 if (pc_address
== 0)
1425 pc_address
= ctx
->pc
;
1426 else if (ctx
->pc
== 0)
1427 ctx
->pc
= pc_address
;
1428 assert (ctx
->pc
== pc_address
);
1431 switch (*input
== NULL
? loc_address
: (*input
)->type
)
1434 /* We have a previous address computation.
1435 This expression will compute starting with that on the stack. */
1436 return location_from_address (ctx
, indent
, expr
, len
, input
);
1438 case loc_noncontiguous
:
1442 case loc_unavailable
:
1443 case loc_implicit_pointer
:
1444 /* The starting point is not an address computation, but a
1445 register or implicit value. We can only handle limited
1446 computations from here. */
1447 return location_relative (ctx
, indent
, expr
, len
, input
);
1458 /* Translate a C fragment for a direct argument VALUE. On errors, call FAIL,
1459 which should not return. Any later errors will use FAIL and FAIL_ARG from
1460 this translate call. On success, return the fragment created. */
1462 c_translate_argument (struct obstack
*pool
,
1463 void (*fail
) (void *arg
, const char *fmt
, ...)
1464 __attribute__ ((noreturn
, format (printf
, 2, 3))),
1466 void (*emit_address
) (void *fail_arg
,
1467 struct obstack
*, Dwarf_Addr
),
1468 int indent
, const char *value
)
1472 obstack_printf (pool
, "%*saddr = %s;\n", indent
* 2, "", value
);
1473 obstack_1grow (pool
, '\0');
1474 char *program
= obstack_finish (pool
);
1476 struct location
*loc
= obstack_alloc (pool
, sizeof *loc
);
1477 loc
->context
= new_context (pool
, fail
, fail_arg
, emit_address
, 0,
1478 0, NULL
, NULL
, NULL
);
1483 loc
->type
= loc_address
;
1484 loc
->frame_base
= NULL
;
1485 loc
->address
.declare
= NULL
;
1486 loc
->address
.program
= program
;
1487 loc
->address
.stack_depth
= 0;
1488 loc
->address
.used_deref
= false;
1494 /* Emit "uintNN_t TARGET = ...;". */
1496 emit_base_fetch (struct obstack
*pool
, Dwarf_Word byte_size
,
1497 bool signed_p
, const char *target
, struct location
*loc
)
1501 /* Emit size/signed coercion. */
1502 obstack_printf (pool
, "{ ");
1503 obstack_printf (pool
, "%sint%u_t value = ",
1504 (signed_p
? "" : "u"), (unsigned)(byte_size
* 8));
1509 obstack_printf (pool
, "addr;");
1513 if (byte_size
!= 0 && byte_size
!= (Dwarf_Word
) -1)
1514 obstack_printf (pool
, "deref (%" PRIu64
", addr);", byte_size
);
1516 obstack_printf (pool
, "deref (sizeof %s, addr);", target
);
1521 if (loc
->reg
.offset
!= 0)
1522 FAIL (loc
, N_("cannot handle offset into register in fetch"));
1523 obstack_printf (pool
, "fetch_register (%u);", loc
->reg
.regno
);
1526 case loc_noncontiguous
:
1527 FAIL (loc
, N_("noncontiguous location for base fetch"));
1530 case loc_implicit_pointer
:
1531 FAIL (loc
, N_("pointer has been optimized out"));
1534 case loc_unavailable
:
1535 FAIL (loc
, N_("location not available"));
1543 obstack_printf (pool
, " %s = value; }", target
);
1547 /* Emit "... = RVALUE;". */
1549 emit_base_store (struct obstack
*pool
, Dwarf_Word byte_size
,
1550 const char *rvalue
, struct location
*loc
)
1555 if (byte_size
!= 0 && byte_size
!= (Dwarf_Word
) -1)
1556 obstack_printf (pool
, "store_deref (%" PRIu64
", addr, %s); ",
1559 obstack_printf (pool
, "store_deref (sizeof %s, addr, %s); ",
1564 if (loc
->reg
.offset
!= 0)
1565 FAIL (loc
, N_("cannot handle offset into register in store"));
1566 obstack_printf (pool
, "store_register (%u, %s);", loc
->reg
.regno
, rvalue
);
1569 case loc_noncontiguous
:
1570 FAIL (loc
, N_("noncontiguous location for base store"));
1573 case loc_implicit_pointer
:
1574 FAIL (loc
, N_("pointer has been optimized out"));
1578 FAIL (loc
, N_("location is computed value, cannot store"));
1582 FAIL (loc
, N_("location is constant value, cannot store"));
1585 case loc_unavailable
:
1586 FAIL (loc
, N_("location is not available, cannot store"));
1598 /* Slice up an object into pieces no larger than MAX_PIECE_BYTES,
1599 yielding a loc_noncontiguous location unless LOC is small enough. */
1600 static struct location
*
1601 discontiguify (struct location_context
*ctx
, int indent
, struct location
*loc
,
1602 Dwarf_Word total_bytes
, Dwarf_Word max_piece_bytes
)
1604 inline bool pieces_small_enough (void)
1606 if (loc
->type
!= loc_noncontiguous
)
1607 return total_bytes
<= max_piece_bytes
;
1609 for (p
= loc
->pieces
; p
!= NULL
; p
= p
->next
)
1610 if (p
->byte_size
> max_piece_bytes
)
1615 /* Constants are always copied byte-wise, but we may need to
1616 * truncate to the total_bytes requested here. */
1617 if (loc
->type
== loc_constant
)
1619 if (loc
->byte_size
> total_bytes
)
1620 loc
->byte_size
= total_bytes
;
1624 if (pieces_small_enough ())
1627 struct location
*noncontig
= alloc_location (ctx
);
1628 noncontig
->next
= NULL
;
1629 noncontig
->type
= loc_noncontiguous
;
1630 noncontig
->byte_size
= total_bytes
;
1631 noncontig
->pieces
= NULL
;
1632 struct location
**tailpiece
= &noncontig
->pieces
;
1633 inline void add (struct location
*piece
)
1636 tailpiece
= &piece
->next
;
1643 /* Synthesize a piece that sets "container_addr" to the computed
1644 address of the whole object. Each piece will refer to this. */
1645 obstack_printf (ctx
->pool
, "%*scontainer_addr = addr;\n",
1647 loc
->next
= new_synthetic_loc (loc
, false);
1648 loc
->next
->byte_size
= loc
->byte_size
;
1649 loc
->next
->type
= loc_fragment
;
1650 loc
->next
->address
.declare
= "container_addr";
1653 /* Synthesize pieces that just compute "container_addr + N". */
1654 Dwarf_Word offset
= 0;
1655 while (total_bytes
- offset
> 0)
1657 Dwarf_Word size
= total_bytes
- offset
;
1658 if (size
> max_piece_bytes
)
1659 size
= max_piece_bytes
;
1661 obstack_printf (ctx
->pool
,
1662 "%*saddr = container_addr + " UFORMAT
";\n",
1663 indent
* 2, "", offset
);
1664 struct location
*piece
= new_synthetic_loc (loc
, false);
1665 piece
->byte_size
= size
;
1676 FAIL (loc
, N_("stack value too big for fetch ???"));
1680 FAIL (loc
, N_("single register too big for fetch/store ???"));
1683 case loc_implicit_pointer
:
1684 FAIL (loc
, N_("implicit pointer too big for fetch/store ???"));
1687 case loc_noncontiguous
:
1688 /* Could be handled if it ever happened. */
1689 FAIL (loc
, N_("cannot support noncontiguous location"));
1697 loc
->next
= noncontig
;
1701 /* Make a fragment that declares a union such as:
1707 } pieces __attribute__ ((packed));
1712 declare_noncontig_union (struct obstack
*pool
, int indent
,
1713 struct location
**input
, struct location
*loc
,
1717 FAIL (loc
, N_("declaring noncontig union for depth > 9, too many pieces"));
1719 obstack_printf (pool
, "%*sunion {\n", indent
++ * 2, "");
1721 obstack_printf (pool
, "%*schar bytes[%" PRIu64
"];\n",
1722 indent
* 2, "", loc
->byte_size
);
1724 if (loc
->type
== loc_noncontiguous
)
1726 Dwarf_Word offset
= 0;
1728 obstack_printf (pool
, "%*sstruct {\n", indent
++ * 2, "");
1730 for (p
= loc
->pieces
; p
!= NULL
; p
= p
->next
)
1732 obstack_printf (pool
, "%*suint%" PRIu64
"_t p%" PRIu64
";\n",
1733 indent
* 2, "", p
->byte_size
* 8, offset
);
1734 offset
+= p
->byte_size
;
1737 obstack_printf (pool
, "%*s} pieces __attribute__ ((packed));\n",
1741 obstack_printf (pool
, "%*suint%" PRIu64
"_t whole;\n",
1742 indent
* 2, "", loc
->byte_size
* 8);
1744 obstack_printf (pool
, "%*s} u_pieces%d;\n", --indent
* 2, "", depth
);
1746 loc
= new_synthetic_loc (*input
, false);
1747 loc
->type
= loc_decl
;
1748 (*input
)->next
= loc
;
1752 /* Determine the byte size of a base type. */
1754 base_byte_size (Dwarf_Die
*typedie
, struct location
*origin
)
1756 assert (dwarf_tag (typedie
) == DW_TAG_base_type
||
1757 dwarf_tag (typedie
) == DW_TAG_enumeration_type
);
1759 Dwarf_Attribute attr_mem
;
1761 if (dwarf_attr_integrate (typedie
, DW_AT_byte_size
, &attr_mem
) != NULL
1762 && dwarf_formudata (&attr_mem
, &size
) == 0)
1766 N_("cannot get byte_size attribute for type %s: %s"),
1767 dwarf_diename (typedie
) ?: "<anonymous>",
1773 base_encoding (Dwarf_Die
*typedie
, struct location
*origin
)
1775 if (! (dwarf_tag (typedie
) == DW_TAG_base_type
||
1776 dwarf_tag (typedie
) == DW_TAG_enumeration_type
))
1779 Dwarf_Attribute attr_mem
;
1780 Dwarf_Word encoding
;
1781 if (dwarf_attr_integrate (typedie
, DW_AT_encoding
, &attr_mem
) != NULL
1782 && dwarf_formudata (&attr_mem
, &encoding
) == 0)
1788 N_("cannot get encoding attribute for type %s: %s"),
1789 dwarf_diename (typedie) ?: "<anonymous>",
1797 /* Fetch the bitfield parameters. */
1799 get_bitfield (struct location
*loc
,
1800 Dwarf_Die
*die
, Dwarf_Word
*bit_offset
, Dwarf_Word
*bit_size
)
1802 Dwarf_Attribute attr_mem
;
1803 if (dwarf_attr_integrate (die
, DW_AT_bit_offset
, &attr_mem
) == NULL
1804 || dwarf_formudata (&attr_mem
, bit_offset
) != 0
1805 || dwarf_attr_integrate (die
, DW_AT_bit_size
, &attr_mem
) == NULL
1806 || dwarf_formudata (&attr_mem
, bit_size
) != 0)
1807 FAIL (loc
, N_("cannot get bit field parameters: %s"), dwarf_errmsg (-1));
1810 /* Translate a fragment to fetch the base-type value of BYTE_SIZE bytes
1811 at the *INPUT location and store it in lvalue TARGET. */
1813 translate_base_fetch (struct obstack
*pool
, int indent
,
1814 Dwarf_Word byte_size
, bool signed_p
,
1815 struct location
**input
, const char *target
,
1820 if ((*input
)->type
== loc_noncontiguous
)
1822 struct location
*p
= (*input
)->pieces
;
1824 declare_noncontig_union (pool
, indent
, input
, *input
, depth
);
1826 Dwarf_Word offset
= 0;
1827 char piece
[sizeof "u_pieces?.pieces.p" + 20] = "u_pieces?.pieces.p";
1828 piece
[8] = (char) ('0' + depth
);
1829 int pdepth
= depth
+ 1;
1832 struct location
*newp
= obstack_alloc (pool
, sizeof *newp
);
1835 (*input
)->next
= newp
;
1838 snprintf (&piece
[sizeof "u_pieces?.pieces.p" - 1], 20,
1839 "%" PRIu64
, offset
);
1840 translate_base_fetch (pool
, indent
, p
->byte_size
, signed_p
/* ? */,
1841 input
, piece
, pdepth
);
1842 (*input
)->type
= loc_fragment
;
1844 offset
+= p
->byte_size
;
1849 obstack_printf (pool
, "%*s%s = u_pieces%d.whole;\n", indent
* 2,
1852 else if ((*input
)->type
== loc_constant
)
1854 const unsigned char *constant_block
= (*input
)->constant_block
;
1855 const size_t byte_size
= (*input
)->byte_size
;
1858 declare_noncontig_union (pool
, indent
, input
, *input
, depth
);
1860 for (i
= 0; i
< byte_size
; ++i
)
1861 obstack_printf (pool
, "%*su_pieces%d.bytes[%zu] = %#x;\n", indent
* 2,
1862 "", depth
, i
, constant_block
[i
]);
1864 obstack_printf (pool
, "%*s%s = u_pieces%d.whole;\n", indent
* 2,
1870 case 0: /* Special case, means address size. */
1875 obstack_printf (pool
, "%*s", indent
* 2, "");
1876 deref
= emit_base_fetch (pool
, byte_size
, signed_p
, target
, *input
);
1877 obstack_printf (pool
, "\n");
1881 /* Could handle this generating call to memcpy equivalent. */
1882 FAIL (*input
, N_("fetch is larger than base integer types"));
1886 struct location
*loc
= new_synthetic_loc (*input
, deref
);
1887 loc
->byte_size
= byte_size
;
1888 loc
->type
= loc_final
;
1889 (*input
)->next
= loc
;
1893 /* Determine the maximum size of a base type, from some DIE in the CU. */
1895 max_fetch_size (struct location
*loc
, Dwarf_Die
*die
)
1898 uint8_t address_size
;
1899 Dwarf_Die
*cu
= dwarf_diecu (die
, &cu_mem
, &address_size
, NULL
);
1901 //TRANSLATORS: CU stands for 'compilation unit'
1902 FAIL (loc
, N_("cannot determine CU address size from %s: %s"),
1903 dwarf_diename (die
), dwarf_errmsg (-1));
1905 return address_size
;
1908 /* Translate a fragment to fetch the value of variable or member DIE
1909 at the *INPUT location and store it in lvalue TARGET. */
1911 c_translate_fetch (struct obstack
*pool
, int indent
,
1912 Dwarf_Addr dwbias
__attribute__ ((unused
)),
1913 Dwarf_Die
*die
, Dwarf_Die
*typedie
,
1914 struct location
**input
, const char *target
)
1916 struct location_context
*const ctx
= (*input
)->context
;
1917 assert (ctx
->pool
== pool
);
1921 Dwarf_Attribute size_attr
;
1922 Dwarf_Word byte_size
;
1923 if (dwarf_attr_integrate (die
, DW_AT_byte_size
, &size_attr
) == NULL
1924 || dwarf_formudata (&size_attr
, &byte_size
) != 0)
1925 byte_size
= base_byte_size (typedie
, *input
);
1927 Dwarf_Attribute encoding_attr
;
1928 Dwarf_Word encoding
;
1929 if (dwarf_attr_integrate (die
, DW_AT_encoding
, &encoding_attr
) == NULL
1930 || dwarf_formudata (&encoding_attr
, &encoding
) != 0)
1931 encoding
= base_encoding (typedie
, *input
);
1932 bool signed_p
= encoding
== DW_ATE_signed
|| encoding
== DW_ATE_signed_char
;
1934 *input
= discontiguify (ctx
, indent
, *input
, byte_size
,
1935 max_fetch_size (*input
, die
));
1937 if (dwarf_hasattr_integrate (die
, DW_AT_bit_offset
))
1939 /* This is a bit field. Fetch the containing base type into a
1940 temporary variable. */
1942 translate_base_fetch (pool
, indent
, byte_size
, signed_p
, input
, "tmp", 0);
1943 (*input
)->type
= loc_fragment
;
1944 (*input
)->address
.declare
= "tmp";
1946 Dwarf_Word bit_offset
= 0;
1947 Dwarf_Word bit_size
= 0;
1948 get_bitfield (*input
, die
, &bit_offset
, &bit_size
);
1950 obstack_printf (pool
, "%*s"
1951 "fetch_bitfield (%s, tmp, %" PRIu64
", %" PRIu64
");\n",
1952 indent
*2, "", target
, bit_offset
, bit_size
);
1954 struct location
*loc
= new_synthetic_loc (*input
, false);
1955 loc
->type
= loc_final
;
1956 (*input
)->next
= loc
;
1960 translate_base_fetch (pool
, indent
, byte_size
, signed_p
, input
, target
, 0);
1963 /* Translate a fragment to store RVALUE into the base-type value of
1964 BYTE_SIZE bytes at the *INPUT location. */
1966 translate_base_store (struct obstack
*pool
, int indent
, Dwarf_Word byte_size
,
1967 struct location
**input
, struct location
*store_loc
,
1968 const char *rvalue
, int depth
)
1972 if (store_loc
->type
== loc_noncontiguous
)
1974 declare_noncontig_union (pool
, indent
, input
, store_loc
, depth
);
1976 obstack_printf (pool
, "%*su_pieces%d.whole = %s;\n", indent
* 2,
1978 struct location
*loc
= new_synthetic_loc (*input
, deref
);
1979 loc
->type
= loc_fragment
;
1980 (*input
)->next
= loc
;
1983 Dwarf_Word offset
= 0;
1984 char piece
[sizeof "u_pieces?.pieces.p" + 20] = "u_pieces?.pieces.p";
1985 piece
[8] = (char) ('0' + depth
);
1987 int pdepth
= depth
+ 1;
1988 for (p
= store_loc
->pieces
; p
!= NULL
; p
= p
->next
)
1990 struct location
*newp
= obstack_alloc (pool
, sizeof *newp
);
1993 (*input
)->next
= newp
;
1996 snprintf (&piece
[sizeof "u_pieces?.pieces.p" - 1], 20, "%" PRIu64
,
1998 translate_base_store (pool
, indent
,
1999 p
->byte_size
, input
, *input
, piece
, pdepth
++);
2000 (*input
)->type
= loc_fragment
;
2002 offset
+= p
->byte_size
;
2005 (*input
)->type
= loc_final
;
2015 obstack_printf (pool
, "%*s", indent
* 2, "");
2016 deref
= emit_base_store (pool
, byte_size
, rvalue
, store_loc
);
2017 obstack_printf (pool
, "\n");
2021 /* Could handle this generating call to memcpy equivalent. */
2022 FAIL (*input
, N_("store is larger than base integer types"));
2026 struct location
*loc
= new_synthetic_loc (*input
, deref
);
2027 loc
->type
= loc_final
;
2028 (*input
)->next
= loc
;
2033 /* Translate a fragment to fetch the value of variable or member DIE
2034 at the *INPUT location and store it in rvalue RVALUE. */
2037 c_translate_store (struct obstack
*pool
, int indent
,
2038 Dwarf_Addr dwbias
__attribute__ ((unused
)),
2039 Dwarf_Die
*die
, Dwarf_Die
*typedie
,
2040 struct location
**input
, const char *rvalue
)
2042 struct location_context
*const ctx
= (*input
)->context
;
2043 assert (ctx
->pool
== pool
);
2047 Dwarf_Attribute size_attr
;
2048 Dwarf_Word byte_size
;
2049 if (dwarf_attr_integrate (die
, DW_AT_byte_size
, &size_attr
) == NULL
2050 || dwarf_formudata (&size_attr
, &byte_size
) != 0)
2051 byte_size
= base_byte_size (typedie
, *input
);
2053 Dwarf_Attribute encoding_attr
;
2054 Dwarf_Word encoding
;
2055 if (dwarf_attr_integrate (die
, DW_AT_encoding
, &encoding_attr
) == NULL
2056 || dwarf_formudata (&encoding_attr
, &encoding
) != 0)
2057 encoding
= base_encoding (typedie
, *input
);
2058 bool signed_p
= (encoding
== DW_ATE_signed
2059 || encoding
== DW_ATE_signed_char
);
2061 *input
= discontiguify (ctx
, indent
, *input
, byte_size
,
2062 max_fetch_size (*input
, die
));
2064 struct location
*store_loc
= *input
;
2066 if (dwarf_hasattr_integrate (die
, DW_AT_bit_offset
))
2068 /* This is a bit field. Fetch the containing base type into a
2069 temporary variable. */
2071 translate_base_fetch (pool
, indent
, byte_size
, signed_p
, input
, "tmp", 0);
2072 (*input
)->type
= loc_fragment
;
2073 (*input
)->address
.declare
= "tmp";
2075 Dwarf_Word bit_offset
= 0;
2076 Dwarf_Word bit_size
= 0;
2077 get_bitfield (*input
, die
, &bit_offset
, &bit_size
);
2079 obstack_printf (pool
, "%*s"
2080 "store_bitfield (tmp, %s, %" PRIu64
", %" PRIu64
");\n",
2081 indent
* 2, "", rvalue
, bit_offset
, bit_size
);
2083 struct location
*loc
= new_synthetic_loc (*input
, false);
2084 loc
->type
= loc_fragment
;
2085 (*input
)->next
= loc
;
2088 /* We have mixed RVALUE into the bits in "tmp".
2089 Now we'll store "tmp" back whence we fetched it. */
2093 translate_base_store (pool
, indent
, byte_size
, input
, store_loc
, rvalue
, 0);
2096 /* Translate a fragment to dereference the given pointer type,
2097 where *INPUT is the location of the pointer with that type.
2099 We chain on a loc_address program that yields this pointer value
2100 (i.e. the location of what it points to). */
2103 c_translate_pointer (struct obstack
*pool
, int indent
,
2104 Dwarf_Addr dwbias
__attribute__ ((unused
)),
2105 Dwarf_Die
*typedie
, struct location
**input
)
2107 assert (dwarf_tag (typedie
) == DW_TAG_pointer_type
||
2108 dwarf_tag (typedie
) == DW_TAG_reference_type
||
2109 dwarf_tag (typedie
) == DW_TAG_rvalue_reference_type
);
2111 Dwarf_Attribute attr_mem
;
2112 Dwarf_Word byte_size
;
2113 if (dwarf_attr_integrate (typedie
, DW_AT_byte_size
, &attr_mem
) == NULL
)
2115 else if (dwarf_formudata (&attr_mem
, &byte_size
) != 0)
2117 N_("cannot get byte_size attribute for type %s: %s"),
2118 dwarf_diename (typedie
) ?: "<anonymous>",
2121 if ((*input
)->type
== loc_implicit_pointer
)
2123 struct location
*const target
= (*input
)->pointer
.target
;
2124 const Dwarf_Sword offset
= (*input
)->pointer
.offset
;
2125 (*input
)->next
= target
;
2127 translate_offset (indent
, NULL
, 0, 0, NULL
, &(*input
)->next
, offset
);
2128 *input
= (*input
)->next
;
2132 bool signed_p
= false; /* XXX: Does not matter? */
2134 translate_base_fetch (pool
, indent
+ 1, byte_size
, signed_p
, input
,
2136 (*input
)->type
= loc_address
;
2142 c_translate_addressof (struct obstack
*pool
, int indent
,
2143 Dwarf_Addr dwbias
__attribute__ ((unused
)),
2145 Dwarf_Die
*typedie
__attribute__ ((unused
)),
2146 struct location
**input
, const char *target
)
2150 if (dwarf_hasattr_integrate (die
, DW_AT_bit_offset
)
2151 || dwarf_hasattr_integrate (die
, DW_AT_data_bit_offset
))
2152 FAIL (*input
, N_("cannot take the address of a bit field"));
2154 switch ((*input
)->type
)
2157 obstack_printf (pool
, "%*s%s = addr;\n", indent
* 2, "", target
);
2158 (*input
)->next
= new_synthetic_loc (*input
, false);
2159 (*input
)->next
->type
= loc_final
;
2163 FAIL (*input
, N_("cannot take address of object in register"));
2165 case loc_noncontiguous
:
2166 FAIL (*input
, N_("cannot take address of noncontiguous object"));
2169 FAIL (*input
, N_("cannot take address of computed value"));
2172 FAIL (*input
, N_("cannot take address of constant value"));
2174 case loc_unavailable
:
2175 FAIL (*input
, N_("cannot take address of unavailable value"));
2177 case loc_implicit_pointer
:
2178 FAIL (*input
, N_("cannot take address of implicit pointer"));
2188 /* Translate a fragment to write the given pointer value,
2189 where *INPUT is the location of the pointer with that type.
2193 c_translate_pointer_store (struct obstack
*pool
, int indent
,
2194 Dwarf_Addr dwbias
__attribute__ ((unused
)),
2195 Dwarf_Die
*typedie
, struct location
**input
,
2198 assert (dwarf_tag (typedie
) == DW_TAG_pointer_type
);
2200 Dwarf_Attribute attr_mem
;
2201 Dwarf_Word byte_size
;
2202 if (dwarf_attr_integrate (typedie
, DW_AT_byte_size
, &attr_mem
) == NULL
)
2204 else if (dwarf_formudata (&attr_mem
, &byte_size
) != 0)
2206 N_("cannot get byte_size attribute for type %s: %s"),
2207 dwarf_diename (typedie
) ?: "<anonymous>",
2210 translate_base_store (pool
, indent
+ 1, byte_size
,
2211 input
, *input
, rvalue
, 0);
2213 // XXX: what about multiple-location lvalues?
2216 /* Determine the element stride of a pointer to a type. */
2218 pointer_stride (Dwarf_Die
*typedie
, struct location
*origin
)
2220 Dwarf_Attribute attr_mem
;
2221 Dwarf_Die die_mem
= *typedie
;
2222 int typetag
= dwarf_tag (&die_mem
);
2223 while (typetag
== DW_TAG_typedef
||
2224 typetag
== DW_TAG_const_type
||
2225 typetag
== DW_TAG_volatile_type
)
2227 if (dwarf_attr_integrate (&die_mem
, DW_AT_type
, &attr_mem
) == NULL
2228 || dwarf_formref_die (&attr_mem
, &die_mem
) == NULL
)
2229 //TRANSLATORS: This refers to the basic type, (stripped of const/volatile/etc.)
2230 FAIL (origin
, N_("cannot get inner type of type %s: %s"),
2231 dwarf_diename (&die_mem
) ?: "<anonymous>",
2233 typetag
= dwarf_tag (&die_mem
);
2236 if (dwarf_attr_integrate (&die_mem
, DW_AT_byte_size
, &attr_mem
) != NULL
)
2239 if (dwarf_formudata (&attr_mem
, &stride
) == 0)
2242 N_("cannot get byte_size attribute for array element type %s: %s"),
2243 dwarf_diename (&die_mem
) ?: "<anonymous>",
2247 FAIL (origin
, N_("confused about array element size"));
2251 /* Determine the element stride of an array type. */
2253 array_stride (Dwarf_Die
*typedie
, struct location
*origin
)
2255 Dwarf_Attribute attr_mem
;
2256 if (dwarf_attr_integrate (typedie
, DW_AT_byte_stride
, &attr_mem
) != NULL
)
2259 if (dwarf_formudata (&attr_mem
, &stride
) == 0)
2261 FAIL (origin
, N_("cannot get byte_stride attribute array type %s: %s"),
2262 dwarf_diename (typedie
) ?: "<anonymous>",
2267 if (dwarf_attr_integrate (typedie
, DW_AT_type
, &attr_mem
) == NULL
2268 || dwarf_formref_die (&attr_mem
, &die_mem
) == NULL
)
2269 FAIL (origin
, N_("cannot get element type of array type %s: %s"),
2270 dwarf_diename (typedie
) ?: "<anonymous>",
2273 return pointer_stride (&die_mem
, origin
);
2277 translate_array (struct obstack
*pool
, int indent
,
2278 Dwarf_Die
*anydie
, Dwarf_Word stride
,
2279 struct location
**input
,
2280 const char *idx
, Dwarf_Word const_idx
)
2284 struct location
*loc
= *input
;
2285 while (loc
->type
== loc_noncontiguous
)
2288 FAIL (*input
, N_("cannot dynamically index noncontiguous array"));
2291 Dwarf_Word offset
= const_idx
* stride
;
2292 struct location
*piece
= loc
->pieces
;
2293 while (piece
!= NULL
&& offset
>= piece
->byte_size
)
2295 offset
-= piece
->byte_size
;
2296 piece
= piece
->next
;
2299 //TRANSLATORS: The index is constant
2300 FAIL (*input
, N_("constant index is outside noncontiguous array"));
2301 if (offset
% stride
!= 0 || piece
->byte_size
< stride
)
2302 FAIL (*input
, N_("noncontiguous array splits elements"));
2303 const_idx
= offset
/ stride
;
2313 obstack_printf (pool
, "%*saddr += %s * " UFORMAT
";\n",
2314 indent
* 2, "", idx
, stride
);
2316 obstack_printf (pool
, "%*saddr += " UFORMAT
" * " UFORMAT
";\n",
2317 indent
* 2, "", const_idx
, stride
);
2318 loc
= new_synthetic_loc (loc
, false);
2323 FAIL (*input
, N_("cannot index array stored in a register"));
2324 else if (const_idx
> max_fetch_size (loc
, anydie
) / stride
)
2325 FAIL (*input
, N_("constant index is outside array held in register"));
2328 loc
->reg
.offset
+= const_idx
* stride
;
2335 FAIL (*input
, N_("cannot index into constant value"));
2336 else if (const_idx
> loc
->byte_size
/ stride
)
2337 //TRANSLATORS: The index is constant
2338 FAIL (*input
, N_("constant index is outside constant array value"));
2341 loc
->byte_size
= stride
;
2342 loc
->constant_block
+= const_idx
* stride
;
2347 case loc_implicit_pointer
:
2349 FAIL (*input
, N_("cannot index into implicit pointer"));
2351 loc
->pointer
.offset
+= const_idx
* stride
;
2355 if (idx
!= NULL
|| const_idx
!= 0)
2356 FAIL (*input
, N_("cannot index into computed value"));
2359 case loc_unavailable
:
2360 if (idx
!= NULL
|| const_idx
!= 0)
2361 FAIL (*input
, N_("cannot index into unavailable value"));
2369 (*input
)->next
= loc
;
2370 *input
= (*input
)->next
;
2374 c_translate_array (struct obstack
*pool
, int indent
,
2375 Dwarf_Addr dwbias
__attribute__ ((unused
)),
2376 Dwarf_Die
*typedie
, struct location
**input
,
2377 const char *idx
, Dwarf_Word const_idx
)
2379 assert (dwarf_tag (typedie
) == DW_TAG_array_type
||
2380 dwarf_tag (typedie
) == DW_TAG_pointer_type
);
2382 return translate_array (pool
, indent
, typedie
,
2383 array_stride (typedie
, *input
),
2384 input
, idx
, const_idx
);
2388 c_translate_array_pointer (struct obstack
*pool
, int indent
,
2389 Dwarf_Die
*typedie
, struct location
**input
,
2390 const char *idx
, Dwarf_Word const_idx
)
2392 return translate_array (pool
, indent
, typedie
,
2393 pointer_stride (typedie
, *input
),
2394 input
, idx
, const_idx
);
2397 /* Emitting C code for finalized fragments. */
2399 #define emit(fmt, ...) fprintf (out, fmt, ## __VA_ARGS__)
2401 /* Open a block with a comment giving the original DWARF expression. */
2403 emit_header (FILE *out
, struct location
*loc
, unsigned int hindent
)
2405 if (loc
->ops
== NULL
)
2406 emit ("%*s{ // synthesized\n", hindent
* 2, "");
2409 emit ("%*s{ // DWARF expression:", hindent
* 2, "");
2411 for (i
= 0; i
< loc
->nops
; ++i
)
2413 emit (" %#x", loc
->ops
[i
].atom
);
2414 if (loc
->ops
[i
].number2
== 0)
2416 if (loc
->ops
[i
].number
!= 0)
2417 emit ("(%" PRId64
")", loc
->ops
[i
].number
);
2420 emit ("(%" PRId64
",%" PRId64
")",
2421 loc
->ops
[i
].number
, loc
->ops
[i
].number2
);
2427 /* Emit a code fragment to assign the target variable to a register value. */
2429 emit_loc_register (FILE *out
, struct location
*loc
, unsigned int indent
,
2432 assert (loc
->type
== loc_register
);
2434 if (loc
->reg
.offset
!= 0)
2435 FAIL (loc
, N_("cannot handle offset into register in fetch"));
2437 emit ("%*s%s = fetch_register (%u);\n",
2438 indent
* 2, "", target
, loc
->reg
.regno
);
2441 /* Emit a code fragment to assign the target variable to an address. */
2443 emit_loc_address (FILE *out
, struct location
*loc
, unsigned int indent
,
2446 assert (loc
->type
== loc_address
|| loc
->type
== loc_value
);
2448 if (loc
->address
.stack_depth
== 0)
2449 /* Synthetic program. */
2450 emit ("%s", loc
->address
.program
);
2453 emit ("%*s{\n", indent
* 2, "");
2454 emit ("%*s%s " STACKFMT
, (indent
+ 1) * 2, "",
2455 stack_slot_type (loc
, false), 0);
2457 for (i
= 1; i
< loc
->address
.stack_depth
; ++i
)
2458 emit (", " STACKFMT
, i
);
2461 emit ("%s%*s%s = " STACKFMT
";\n", loc
->address
.program
,
2462 (indent
+ 1) * 2, "", target
, 0);
2463 emit ("%*s}\n", indent
* 2, "");
2467 /* Emit a code fragment to declare the target variable and
2468 assign it to an address-sized value. */
2470 emit_loc_value (FILE *out
, struct location
*loc
, unsigned int indent
,
2471 const char *target
, bool declare
,
2472 bool *used_deref
, unsigned int *max_stack
)
2475 emit ("%*s%s %s;\n", indent
* 2, "", stack_slot_type (loc
, false), target
);
2477 emit_header (out
, loc
, indent
++);
2486 emit_loc_register (out
, loc
, indent
, target
);
2491 emit_loc_address (out
, loc
, indent
, target
);
2492 *used_deref
= *used_deref
|| loc
->address
.used_deref
;
2493 if (loc
->address
.stack_depth
> *max_stack
)
2494 *max_stack
= loc
->address
.stack_depth
;
2498 emit ("%*s}\n", --indent
* 2, "");
2502 c_emit_location (FILE *out
, struct location
*loc
, int indent
,
2503 unsigned int *max_stack
)
2505 emit ("%*s{\n", indent
* 2, "");
2507 bool declared_addr
= false;
2509 for (l
= loc
; l
!= NULL
; l
= l
->next
)
2513 emit ("%s", l
->address
.program
);
2520 declared_addr
= true;
2521 l
->address
.declare
= "addr";
2524 if (l
->address
.declare
!= NULL
)
2526 if (l
->byte_size
== 0 || l
->byte_size
== (Dwarf_Word
) -1)
2527 emit ("%*s%s %s;\n", (indent
+ 1) * 2, "",
2528 stack_slot_type (l
, false), l
->address
.declare
);
2530 emit ("%*suint%" PRIu64
"_t %s;\n", (indent
+ 1) * 2, "",
2531 l
->byte_size
* 8, l
->address
.declare
);
2541 if (loc
->frame_base
!= NULL
)
2542 emit_loc_value (out
, loc
->frame_base
, indent
, "frame_base", true,
2545 for (; loc
->next
!= NULL
; loc
= loc
->next
)
2550 /* Emit the program fragment to calculate the address. */
2551 emit_loc_value (out
, loc
, indent
+ 1, "addr", false, &deref
, max_stack
);
2555 emit ("%s", loc
->address
.program
);
2556 deref
= deref
|| loc
->address
.used_deref
;
2561 case loc_noncontiguous
:
2563 case loc_implicit_pointer
:
2564 /* These don't produce any code directly.
2565 The next address/final record incorporates the value. */
2568 case loc_final
: /* Should be last in chain! */
2574 if (loc
->type
!= loc_final
) /* Unfinished chain. */
2577 emit ("%s%*s}\n", loc
->address
.program
, indent
* 2, "");
2579 if (loc
->address
.stack_depth
> *max_stack
)
2580 *max_stack
= loc
->address
.stack_depth
;
2582 return deref
|| loc
->address
.used_deref
;
2587 /* vim: set sw=2 ts=8 cino=>4,n-2,{2,^-2,t0,(0,u0,w1,M1 : */