1 // dwarf location-list-to-c translator
2 // Copyright (C) 2005-2013 Red Hat Inc.
4 // This file is part of systemtap, and is free software. You can
5 // redistribute it and/or modify it under the terms of the GNU General
6 // Public License (GPL); either version 2, or (at your option) any
17 #include <elfutils/libdw.h>
18 #include <elfutils/version.h>
25 #if ! _ELFUTILS_PREREQ(0, 153)
26 #define DW_OP_GNU_entry_value 0xf3
31 /* NB: PR10601 may make one suspect that intptr_t and uintptr_t aren't
32 right, for example on a 64-bit kernel targeting a 32-bit userspace
33 process. At least these types are always at least as wide as
34 userspace (since 64-bit userspace doesn't run on a 32-bit kernel).
35 So as long as deref() and {fetch,store}_register() widen/narrow
36 their underlying values to these, there should be no problem. */
38 #define STACK_TYPE "intptr_t" /* Must be the signed type. */
39 #define UTYPE "uintptr_t" /* Must be the unsigned type. */
40 #define SFORMAT "%" PRId64 "L"
41 #define UFORMAT "%" PRIu64 "UL"
42 #define AFORMAT "%#" PRIx64 "UL"
43 #define STACKFMT "s%u"
45 struct location_context
49 void (*fail
) (void *arg
, const char *fmt
, ...)
50 __attribute__ ((noreturn
, format (printf
, 2, 3)));
52 void (*emit_address
) (void *fail_arg
, struct obstack
*, Dwarf_Addr
);
54 Dwarf_Attribute
*attr
;
57 Dwarf_Attribute
*fb_attr
;
58 const Dwarf_Op
*cfa_ops
;
63 struct location
*next
;
65 struct location_context
*context
;
74 loc_address
, loc_register
, loc_noncontiguous
, loc_unavailable
,
75 loc_value
, loc_constant
, loc_implicit_pointer
,
76 loc_decl
, loc_fragment
, loc_final
78 struct location
*frame_base
;
81 struct /* loc_address, loc_value, loc_fragment, loc_final */
83 const char *declare
; /* Temporary that needs declared. */
84 char *program
; /* C fragment, leaves address in s0. */
85 unsigned int stack_depth
; /* Temporaries "s0..<N>" used by it. */
86 bool used_deref
; /* Program uses "deref" macro. */
88 struct /* loc_register */
93 struct location
*pieces
; /* loc_noncontiguous */
94 const void *constant_block
; /* loc_constant */
95 struct /* loc_implicit_pointer */
97 struct location
*target
;
103 /* Select the C type to use in the emitted code to represent slots in the
104 DWARF expression stack. For really proper semantics this should be the
105 target address size. */
107 stack_slot_type (struct location
*context
__attribute__ ((unused
)), bool sign
)
109 return sign
? STACK_TYPE
: UTYPE
;
112 static struct location
*
113 alloc_location (struct location_context
*ctx
)
115 struct location
*loc
= obstack_alloc (ctx
->pool
, sizeof *loc
);
118 loc
->frame_base
= NULL
;
124 #define FAIL(loc, fmt, ...) \
125 (*(loc)->context->fail) ((loc)->context->fail_arg, fmt, ## __VA_ARGS__)
128 default_emit_address (void *fail_arg
__attribute__ ((unused
)),
129 struct obstack
*pool
, Dwarf_Addr address
)
131 obstack_printf (pool
, AFORMAT
, address
);
134 static struct location_context
*
135 new_context (struct obstack
*pool
,
136 void (*fail
) (void *arg
, const char *fmt
, ...)
137 __attribute__ ((noreturn
, format (printf
, 2, 3))),
139 void (*emit_address
) (void *fail_arg
,
140 struct obstack
*, Dwarf_Addr
),
141 Dwarf_Addr dwbias
, Dwarf_Addr pc_address
,
142 Dwarf_Attribute
*attr
, Dwarf_Attribute
*fb_attr
,
143 const Dwarf_Op
*cfa_ops
)
145 struct location_context
*ctx
= obstack_alloc (pool
, sizeof *ctx
);
148 ctx
->fail_arg
= fail_arg
;
149 ctx
->emit_address
= emit_address
?: &default_emit_address
;
151 ctx
->dwbias
= dwbias
;
152 ctx
->pc
= pc_address
;
153 ctx
->fb_attr
= fb_attr
;
154 ctx
->cfa_ops
= cfa_ops
;
158 /* Translate a DW_AT_const_value attribute as if it were a location of
159 constant-value flavor. */
161 static struct location
*
162 translate_constant (struct location_context
*ctx
, int indent
,
163 Dwarf_Attribute
*attr
)
167 struct location
*loc
= obstack_alloc (ctx
->pool
, sizeof *loc
);
171 loc
->frame_base
= NULL
;
172 loc
->address
.stack_depth
= 0;
173 loc
->address
.declare
= NULL
;
174 loc
->address
.used_deref
= false;
178 switch (dwarf_whatform (attr
))
183 if (dwarf_formaddr (attr
, &addr
) != 0)
185 //TRANSLATORS: failure to find an address that was a constant literal number
186 FAIL (loc
, N_("cannot get constant address: %s"),
190 loc
->type
= loc_value
;
191 obstack_printf (ctx
->pool
, "%*saddr = ", indent
* 2, "");
192 (*ctx
->emit_address
) (ctx
->fail_arg
, ctx
->pool
, ctx
->dwbias
+ addr
);
193 obstack_grow (ctx
->pool
, ";\n", 3);
194 loc
->address
.program
= obstack_finish (ctx
->pool
);
204 if (dwarf_formblock (attr
, &block
) != 0)
206 FAIL (loc
, N_("cannot get constant block: %s"), dwarf_errmsg (-1));
209 loc
->type
= loc_constant
;
210 loc
->byte_size
= block
.length
;
211 loc
->constant_block
= block
.data
;
218 const char *string
= dwarf_formstring (attr
);
221 FAIL (loc
, N_("cannot get string constant: %s"), dwarf_errmsg (-1));
224 loc
->type
= loc_constant
;
225 loc
->byte_size
= strlen (string
) + 1;
226 loc
->constant_block
= string
;
233 if (dwarf_formsdata (attr
, &value
) != 0)
235 FAIL (loc
, N_("cannot get constant value: %s"), dwarf_errmsg (-1));
238 loc
->type
= loc_value
;
239 obstack_printf (ctx
->pool
, "%*saddr = (" UTYPE
")%#" PRIx64
"ULL;\n",
240 indent
* 2, "", value
);
241 obstack_1grow (ctx
->pool
, '\0');
242 loc
->address
.program
= obstack_finish (ctx
->pool
);
251 c_translate_constant (struct obstack
*pool
,
252 void (*fail
) (void *arg
,
253 const char *fmt
, ...)
254 __attribute__ ((noreturn
,
255 format (printf
, 2, 3))),
257 void (*emit_address
) (void *fail_arg
,
260 int indent
, Dwarf_Addr dwbias
, Dwarf_Attribute
*attr
)
262 return translate_constant (new_context (pool
, fail
, fail_arg
, emit_address
,
263 dwbias
, 0, attr
, NULL
, NULL
),
267 #if _ELFUTILS_PREREQ (0, 149)
268 static struct location
*location_from_attr (struct location_context
*ctx
,
269 int indent
, Dwarf_Attribute
*attr
);
273 /* Synthesize a new loc_address using the program on the obstack. */
274 static struct location
*
275 new_synthetic_loc (struct location
*origin
, bool deref
)
277 obstack_1grow (origin
->context
->pool
, '\0');
278 char *program
= obstack_finish (origin
->context
->pool
);
280 struct location
*loc
= alloc_location (origin
->context
);
283 loc
->type
= loc_address
;
284 loc
->address
.program
= program
;
285 loc
->address
.stack_depth
= 0;
286 loc
->address
.declare
= NULL
;
287 loc
->address
.used_deref
= deref
;
289 if (origin
->type
== loc_register
)
291 loc
->ops
= origin
->ops
;
292 loc
->nops
= origin
->nops
;
304 /* Die in the middle of an expression. */
305 static struct location
*
306 lose (struct location
*loc
, const Dwarf_Op
*lexpr
, size_t len
,
307 const char *failure
, size_t i
)
309 if (lexpr
== NULL
|| i
>= len
)
310 FAIL (loc
, "%s", failure
);
312 FAIL (loc
, N_("%s in DWARF expression [%Zu] at %" PRIu64
313 " (%#x: %" PRId64
", %" PRId64
")"),
314 failure
, i
, lexpr
[i
].offset
,
315 lexpr
[i
].atom
, lexpr
[i
].number
, lexpr
[i
].number2
);
320 /* Translate a (constrained) DWARF expression into C code
321 emitted to the obstack POOL. INDENT is the number of indentation levels.
322 ADDRBIAS is the difference between runtime and Dwarf info addresses.
323 INPUT is null or an expression to be initially pushed on the stack.
324 If NEED_FB is null, fail on DW_OP_fbreg, else set *NEED_FB to true
325 and emit "frame_base" for it. On success, set *MAX_STACK to the number
326 of stack slots required. On failure, set *LOSER to the index in EXPR
327 of the operation we could not handle.
329 Returns a failure message or null for success. */
332 translate (struct location_context
*ctx
, int indent
,
333 const Dwarf_Op
*expr
, const size_t len
,
334 struct location
*input
, bool *need_fb
, size_t *loser
,
335 struct location
*loc
)
340 #define DIE(msg) return (*loser = i, N_(msg))
342 #define emit(fmt, ...) obstack_printf (ctx->pool, fmt, ## __VA_ARGS__)
344 unsigned int stack_depth
;
345 unsigned int max_stack
= 0;
346 inline void deepen (void)
348 if (stack_depth
== max_stack
)
353 if (stack_depth > 0) \
355 else if (tos_register != -1) \
356 fetch_tos_register (); \
359 int var = stack_depth
360 #define PUSH (deepen (), stack_depth++)
361 #define STACK(idx) ({int z = (stack_depth - 1 - (idx)); assert (z >= 0); z;})
363 /* Don't put stack operations in the arguments to this. */
364 #define push(fmt, ...) \
365 emit ("%*s" STACKFMT " = " fmt ";\n", indent * 2, "", PUSH, ## __VA_ARGS__)
368 inline void fetch_tos_register (void)
371 emit ("%*s" STACKFMT
" = fetch_register (%d);\n",
372 indent
* 2, "", stack_depth
, tos_register
);
377 Dwarf_Block implicit_value
;
379 const Dwarf_Op
*implicit_pointer
;
381 /* Initialize our state for handling each new piece. */
387 implicit_value
.data
= NULL
;
388 implicit_pointer
= NULL
;
404 tos_register
= input
->reg
.regno
;
414 inline const char *finish (struct location
*piece
)
416 if (piece
->nops
== 0)
418 assert (stack_depth
== 0);
419 assert (tos_register
== -1);
420 assert (obstack_object_size (ctx
->pool
) == 0);
421 piece
->type
= loc_unavailable
;
423 else if (stack_depth
>= 1)
425 /* The top of stack has our value.
426 Other stack slots left don't matter. */
427 obstack_1grow (ctx
->pool
, '\0');
428 char *program
= obstack_finish (ctx
->pool
);
429 if (implicit_pointer
!= NULL
)
431 piece
->type
= loc_implicit_pointer
;
432 piece
->pointer
.offset
= implicit_pointer
->number2
;
433 #if !_ELFUTILS_PREREQ (0, 149)
434 /* Then how did we get here? */
437 Dwarf_Attribute target
;
438 if (dwarf_getlocation_implicit_pointer (ctx
->attr
,
441 DIE ("invalid implicit pointer");
442 switch (dwarf_whatattr (&target
))
444 case DW_AT_const_value
:
445 piece
->pointer
.target
= translate_constant (ctx
, indent
,
449 piece
->pointer
.target
= location_from_attr (ctx
, indent
,
453 DIE ("unexpected implicit pointer attribute!");
458 else if (implicit_value
.data
== NULL
)
460 piece
->type
= tos_value
? loc_value
: loc_address
;
461 piece
->address
.declare
= NULL
;
462 piece
->address
.program
= program
;
463 piece
->address
.stack_depth
= max_stack
;
464 piece
->address
.used_deref
= used_deref
;
468 piece
->type
= loc_constant
;
469 piece
->byte_size
= implicit_value
.length
;
470 piece
->constant_block
= implicit_value
.data
;
473 else if (tos_register
== -1)
474 DIE ("stack underflow");
475 else if (obstack_object_size (ctx
->pool
) != 0)
476 DIE ("register value must stand alone in location expression");
479 piece
->type
= loc_register
;
480 piece
->reg
.regno
= tos_register
;
481 piece
->reg
.offset
= 0;
487 struct location
*pieces
= NULL
, **tailpiece
= &pieces
;
488 size_t piece_expr_start
= 0;
489 Dwarf_Word piece_total_bytes
= 0;
490 for (i
= 0; i
< len
; ++i
)
496 inline bool more_ops (void)
498 return (expr
[i
].atom
!= DW_OP_nop
499 && expr
[i
].atom
!= DW_OP_piece
500 && expr
[i
].atom
!= DW_OP_bit_piece
);
503 if (tos_value
&& more_ops ())
504 DIE ("operations follow DW_OP_stack_value");
506 if (implicit_value
.data
!= NULL
&& more_ops ())
507 DIE ("operations follow DW_OP_implicit_value");
509 if (implicit_pointer
!= NULL
&& more_ops ())
510 DIE ("operations follow DW_OP_GNU_implicit_pointer");
512 switch (expr
[i
].atom
)
514 /* Basic stack operations. */
523 unsigned int tos
= STACK (0);
524 push (STACKFMT
, tos
);
531 emit ("%*s/* drop " STACKFMT
"*/\n", indent
* 2, "", ignore
);
538 if (sp
>= stack_depth
)
551 deepen (); /* Use a temporary slot. */
553 STACKFMT
" = " STACKFMT
", "
554 STACKFMT
" = " STACKFMT
", "
555 STACKFMT
" = " STACKFMT
";\n",
557 STACK (-1), STACK (0),
558 STACK (0), STACK (1),
559 STACK (1), STACK (-1));
565 deepen (); /* Use a temporary slot. */
567 STACKFMT
" = " STACKFMT
", "
568 STACKFMT
" = " STACKFMT
", "
569 STACKFMT
" = " STACKFMT
", "
570 STACKFMT
" = " STACKFMT
";\n",
572 STACK (-1), STACK (0),
573 STACK (0), STACK (1),
574 STACK (1), STACK (2),
575 STACK (2), STACK (-1));
579 /* Control flow operations. */
582 Dwarf_Off target
= expr
[i
].offset
+ 3 + expr
[i
].number
;
583 while (i
+ 1 < len
&& expr
[i
+ 1].offset
< target
)
585 if (expr
[i
+ 1].offset
!= target
)
586 DIE ("invalid skip target");
591 DIE ("conditional branches not supported");
599 push ("deref (sizeof (void *), " STACKFMT
")", addr
);
604 case DW_OP_deref_size
:
607 push ("deref (" UFORMAT
", " STACKFMT
")",
608 expr
[i
].number
, addr
);
617 push ("xderef (sizeof (void *), " STACKFMT
", " STACKFMT
")",
623 case DW_OP_xderef_size
:
627 push ("xderef (" UFORMAT
", " STACKFMT
", " STACKFMT
")",
628 expr
[i
].number
, addr
, as
);
633 /* Constant-value operations. */
636 emit ("%*s" STACKFMT
" = ", indent
* 2, "", PUSH
);
637 (*ctx
->emit_address
) (ctx
->fail_arg
, ctx
->pool
,
638 ctx
->dwbias
+ expr
[i
].number
);
642 case DW_OP_lit0
... DW_OP_lit31
:
643 value
= expr
[i
].atom
- DW_OP_lit0
;
656 value
= expr
[i
].number
;
658 push (SFORMAT
, value
);
661 /* Arithmetic operations. */
662 #define UNOP(dw_op, c_op) \
663 case DW_OP_##dw_op: \
666 push ("%s (" STACKFMT ")", #c_op, tos); \
669 #define BINOP(dw_op, c_op) \
670 case DW_OP_##dw_op: \
674 push (STACKFMT " %s " STACKFMT, a, #c_op, b); \
690 /* Comparisons are binary operators too. */
705 push ("(%s) " STACKFMT
" >> (%s)" STACKFMT
,
706 stack_slot_type (loc
, true), a
,
707 stack_slot_type (loc
, true), b
);
715 push ("dwarf_div_op((%s) " STACKFMT
", (%s) " STACKFMT
")",
716 stack_slot_type (loc
, true), a
,
717 stack_slot_type (loc
, true), b
);
726 push ("dwarf_mod_op((%s) " STACKFMT
", (%s) " STACKFMT
")",
727 stack_slot_type (loc
, false), a
,
728 stack_slot_type (loc
, false), b
);
733 case DW_OP_plus_uconst
:
736 push (STACKFMT
" + " UFORMAT
, x
, expr
[i
].number
);
741 /* Register-relative addressing. */
742 case DW_OP_breg0
... DW_OP_breg31
:
743 reg
= expr
[i
].atom
- DW_OP_breg0
;
744 value
= expr
[i
].number
;
748 reg
= expr
[i
].number
;
749 value
= expr
[i
].number2
;
751 push ("fetch_register (%u) + " SFORMAT
, reg
, value
);
756 DIE ("DW_OP_fbreg from DW_AT_frame_base");
758 push ("frame_base + " SFORMAT
, expr
[i
].number
);
761 /* Direct register contents. */
762 case DW_OP_reg0
... DW_OP_reg31
:
763 reg
= expr
[i
].atom
- DW_OP_reg0
;
767 reg
= expr
[i
].number
;
775 /* If this ever happens we could copy the program. */
776 DIE ("DW_OP_piece left multiple values on stack");
779 /* The obstack has a pending program for loc_address,
780 so we must finish that piece off before we can
782 struct location temp_piece
=
784 .context
= loc
->context
,
786 .ops
= &expr
[piece_expr_start
],
787 .nops
= i
- piece_expr_start
,
789 const char *failure
= finish (&temp_piece
);
793 struct location
*piece
= obstack_alloc (ctx
->pool
, sizeof *piece
);
796 piece_expr_start
= i
+ 1;
798 piece_total_bytes
+= piece
->byte_size
= expr
[i
].number
;
801 tailpiece
= &piece
->next
;
804 /* Reset default conditions for handling the next piece. */
809 case DW_OP_stack_value
:
811 DIE ("DW_OP_stack_value left multiple values on stack");
814 /* Fetch a register to top of stack, or check for underflow.
815 Then mark the TOS as being a value. */
823 case DW_OP_implicit_value
:
824 if (ctx
->attr
== NULL
)
825 DIE ("DW_OP_implicit_value used in invalid context"
826 " (no DWARF attribute, ABI return value location?)");
828 /* It's supposed to appear by itself, except for DW_OP_piece. */
829 if (stack_depth
!= 0)
830 DIE ("DW_OP_implicit_value follows stack operations");
832 #if _ELFUTILS_PREREQ (0, 143)
833 if (dwarf_getlocation_implicit_value (ctx
->attr
,
834 (Dwarf_Op
*) &expr
[i
],
835 &implicit_value
) != 0)
836 DIE ("dwarf_getlocation_implicit_value failed");
838 /* Fake top of stack: implicit_value being set marks it. */
843 DIE ("DW_OP_implicit_value not supported");
846 #if _ELFUTILS_PREREQ (0, 149)
847 case DW_OP_GNU_implicit_pointer
:
848 implicit_pointer
= &expr
[i
];
849 /* Fake top of stack: implicit_pointer being set marks it. */
854 case DW_OP_call_frame_cfa
:
855 // We pick this out when processing DW_AT_frame_base in
856 // so it really shouldn't turn up here.
858 DIE ("DW_OP_call_frame_cfa while processing frame base");
860 DIE ("DW_OP_call_frame_cfa not expected outside DW_AT_frame_base");
863 case DW_OP_push_object_address
:
864 DIE ("unhandled DW_OP_push_object_address");
867 case DW_OP_GNU_entry_value
:
868 DIE ("unhandled DW_OP_GNU_entry_value");
872 DIE ("unrecognized operation");
880 if (piece_expr_start
!= i
)
881 DIE ("extra operations after last DW_OP_piece");
883 loc
->type
= loc_noncontiguous
;
884 loc
->pieces
= pieces
;
885 loc
->byte_size
= piece_total_bytes
;
890 DIE ("stack underflow");
900 /* Translate a location starting from an address or nothing. */
901 static struct location
*
902 location_from_address (struct location_context
*ctx
, int indent
,
903 const Dwarf_Op
*expr
, size_t len
,
904 struct location
**input
)
906 struct location
*loc
= obstack_alloc (ctx
->pool
, sizeof *loc
);
909 loc
->frame_base
= NULL
;
913 bool need_fb
= false;
915 const char *failure
= translate (ctx
, indent
+ 1, expr
, len
,
916 *input
, &need_fb
, &loser
, loc
);
918 return lose (loc
, expr
, len
, failure
, loser
);
923 /* The main expression uses DW_OP_fbreg, so we need to compute
924 the DW_AT_frame_base attribute expression's value first. */
926 if (ctx
->fb_attr
== NULL
)
927 FAIL (loc
, N_("required DW_AT_frame_base attribute not supplied"));
931 switch (dwarf_getlocation_addr (ctx
->fb_attr
, ctx
->pc
,
932 &fb_expr
, &fb_len
, 1))
934 case 1: /* Should always happen. */
936 goto fb_inaccessible
;
939 default: /* Shouldn't happen. */
941 FAIL (loc
, N_("dwarf_getlocation_addr (form %#x): %s"),
942 dwarf_whatform (ctx
->fb_attr
), dwarf_errmsg (-1));
945 case 0: /* Shouldn't happen. */
947 FAIL (loc
, N_("DW_AT_frame_base not accessible at this address"));
951 // If it is DW_OP_call_frame_cfa then get cfi cfa ops.
952 const Dwarf_Op
* fb_ops
;
953 if (fb_len
== 1 && fb_expr
[0].atom
== DW_OP_call_frame_cfa
)
955 if (ctx
->cfa_ops
== NULL
)
956 FAIL (loc
, N_("No cfa_ops supplied, but needed by DW_OP_call_frame_cfa"));
957 fb_ops
= ctx
->cfa_ops
;
962 loc
->frame_base
= alloc_location (ctx
);
963 failure
= translate (ctx
, indent
+ 1, fb_ops
, fb_len
, NULL
,
964 NULL
, &loser
, loc
->frame_base
);
966 return lose (loc
, fb_ops
, fb_len
, failure
, loser
);
970 (*input
)->next
= loc
;
976 #if _ELFUTILS_PREREQ (0, 149)
977 static struct location
*
978 location_from_attr (struct location_context
*ctx
, int indent
,
979 Dwarf_Attribute
*attr
)
983 switch (dwarf_getlocation_addr (attr
, ctx
->pc
, &expr
, &len
, 1))
985 case 1: /* Should always happen. */
990 case 0: /* Shouldn't happen. */
991 (*ctx
->fail
) (ctx
->fail_arg
, N_("not accessible at this address (%#" PRIx64
")"), ctx
->pc
);
994 default: /* Shouldn't happen. */
996 (*ctx
->fail
) (ctx
->fail_arg
, "dwarf_getlocation_addr: %s",
1001 struct location
*input
= NULL
;
1002 return location_from_address (ctx
, indent
, expr
, len
, &input
);
1006 static struct location
*
1007 translate_offset (int indent
, const Dwarf_Op
*expr
, size_t len
, size_t i
,
1008 struct location
*head
, struct location
**input
,
1011 struct location_context
*const ctx
= (*input
)->context
;
1013 #define DIE(msg) return lose (*input, expr, len, N_(msg), i)
1015 while ((*input
)->type
== loc_noncontiguous
)
1017 /* We are starting from a noncontiguous object (DW_OP_piece).
1018 Find the piece we want. */
1020 struct location
*piece
= (*input
)->pieces
;
1021 while (piece
!= NULL
&& offset
>= piece
->byte_size
)
1023 offset
-= piece
->byte_size
;
1024 piece
= piece
->next
;
1027 DIE ("offset outside available pieces");
1029 assert ((*input
)->next
== NULL
);
1030 (*input
)->next
= piece
;
1034 switch ((*input
)->type
)
1038 /* The piece we want is actually in memory. Use the same
1039 program to compute the address from the preceding input. */
1041 struct location
*loc
= obstack_alloc (ctx
->pool
, sizeof *loc
);
1045 (*input
)->next
= loc
;
1048 /* The piece addresses exactly where we want to go. */
1054 /* Add a second fragment to offset the piece address. */
1055 obstack_printf (ctx
->pool
, "%*saddr += " SFORMAT
"\n",
1056 indent
* 2, "", offset
);
1057 *input
= loc
->next
= new_synthetic_loc (*input
, false);
1060 /* That's all she wrote. */
1065 /* This piece (or the whole struct) fits in a register. */
1066 (*input
)->reg
.offset
+= offset
;
1067 return head
?: *input
;
1070 /* This piece has a constant offset. */
1071 if (offset
>= (*input
)->byte_size
)
1072 DIE ("offset outside available constant block");
1073 (*input
)->constant_block
+= offset
;
1074 (*input
)->byte_size
-= offset
;
1075 return head
?: *input
;
1077 case loc_implicit_pointer
:
1078 /* This piece is an implicit pointer. */
1079 (*input
)->pointer
.offset
+= offset
;
1080 return head
?: *input
;
1082 case loc_unavailable
:
1083 /* Let it be diagnosed later. */
1084 return head
?: *input
;
1087 /* The piece we want is part of a computed offset.
1088 If it's the whole thing, we are done. */
1090 return head
?: *input
;
1091 DIE ("XXX extract partial rematerialized value");
1102 /* Translate a location starting from a non-address "on the top of the
1103 stack". The *INPUT location is a register name or noncontiguous
1104 object specification, and this expression wants to find the "address"
1105 of an object (or the actual value) relative to that "address". */
1107 static struct location
*
1108 location_relative (struct location_context
*ctx
, int indent
,
1109 const Dwarf_Op
*expr
, size_t len
,
1110 struct location
**input
)
1112 Dwarf_Sword
*stack
= NULL
;
1113 unsigned int stack_depth
= 0, max_stack
= 0;
1114 inline void deepen (void)
1116 if (stack_depth
== max_stack
)
1119 obstack_blank (ctx
->pool
, sizeof stack
[0]);
1120 stack
= (void *) obstack_base (ctx
->pool
);
1125 if (stack_depth > 0) \
1129 int var = stack_depth
1130 #define PUSH (deepen (), stack_depth++)
1131 #define STACK(idx) (stack_depth - 1 - (idx))
1132 #define STACKWORD(idx) stack[STACK (idx)]
1134 /* Don't put stack operations in the arguments to this. */
1135 #define push(value) (stack[PUSH] = (value))
1137 const char *failure
= NULL
;
1138 #define DIE(msg) do { failure = N_(msg); goto fail; } while (0)
1140 struct location
*head
= NULL
;
1142 for (i
= 0; i
< len
; ++i
)
1147 switch (expr
[i
].atom
)
1149 /* Basic stack operations. */
1154 if (stack_depth
< 1)
1158 unsigned int tos
= STACK (0);
1164 if (stack_depth
> 0)
1166 else if (*input
!= NULL
)
1167 /* Mark that we have consumed the input. */
1170 /* Hits if cleared above, or if we had no input at all. */
1175 sp
= expr
[i
].number
;
1177 if (sp
>= stack_depth
)
1188 if (stack_depth
< 2)
1190 deepen (); /* Use a temporary slot. */
1191 STACKWORD (-1) = STACKWORD (0);
1192 STACKWORD (0) = STACKWORD (1);
1193 STACKWORD (1) = STACKWORD (-1);
1197 if (stack_depth
< 3)
1199 deepen (); /* Use a temporary slot. */
1200 STACKWORD (-1) = STACKWORD (0);
1201 STACKWORD (0) = STACKWORD (1);
1202 STACKWORD (2) = STACKWORD (2);
1203 STACKWORD (2) = STACKWORD (-1);
1207 /* Control flow operations. */
1211 if (stack
[taken
] == 0)
1218 Dwarf_Off target
= expr
[i
].offset
+ 3 + expr
[i
].number
;
1219 while (i
+ 1 < len
&& expr
[i
+ 1].offset
< target
)
1221 if (expr
[i
+ 1].offset
!= target
)
1222 DIE ("invalid skip target");
1226 /* Memory access. */
1228 case DW_OP_deref_size
:
1230 case DW_OP_xderef_size
:
1232 /* Register-relative addressing. */
1233 case DW_OP_breg0
... DW_OP_breg31
:
1237 /* This started from a register, but now it's following a pointer.
1238 So we can do the translation starting from address here. */
1239 return location_from_address (ctx
, indent
, expr
, len
, input
);
1242 /* Constant-value operations. */
1244 DIE ("static calculation depends on load-time address");
1245 push (ctx
->dwbias
+ expr
[i
].number
);
1248 case DW_OP_lit0
... DW_OP_lit31
:
1249 value
= expr
[i
].atom
- DW_OP_lit0
;
1262 value
= expr
[i
].number
;
1267 /* Arithmetic operations. */
1268 #define UNOP(dw_op, c_op) \
1269 case DW_OP_##dw_op: \
1272 push (c_op (stack[tos])); \
1275 #define BINOP(dw_op, c_op) \
1276 case DW_OP_##dw_op: \
1280 push (stack[a] c_op stack[b]); \
1284 #define op_abs(x) (x < 0 ? -x : x)
1297 /* Comparisons are binary operators too. */
1312 push ((Dwarf_Word
) stack
[a
] >> (Dwarf_Word
) stack
[b
]);
1316 /* Simple addition we may be able to handle relative to
1317 the starting register name. */
1321 value
= -stack
[tos
];
1330 case DW_OP_plus_uconst
:
1331 value
= expr
[i
].number
;
1333 if (stack_depth
> 0)
1335 /* It's just private diddling after all. */
1337 push (stack
[a
] + value
);
1343 /* This is the primary real-world case: the expression takes
1344 the input address and adds a constant offset. */
1346 head
= translate_offset (indent
, expr
, len
, i
, head
, input
, value
);
1347 if (head
!= NULL
&& i
+ 1 < len
)
1349 if ((*input
)->type
!= loc_address
)
1350 DIE ("too much computation for non-address location");
1352 /* This expression keeps going, but further
1353 computations now have an address to start with.
1354 So we can punt to the address computation generator. */
1355 struct location
*loc
= location_from_address
1356 (ctx
, indent
, &expr
[i
+ 1], len
- i
- 1, input
);
1362 /* Direct register contents. */
1363 case DW_OP_reg0
... DW_OP_reg31
:
1368 /* Special magic. */
1370 DIE ("DW_OP_piece");
1373 case DW_OP_push_object_address
:
1374 DIE ("unhandled DW_OP_push_object_address");
1377 case DW_OP_GNU_entry_value
:
1378 DIE ("unhandled DW_OP_GNU_entry_value");
1382 DIE ("unrecognized operation");
1387 if (stack_depth
> 1)
1388 DIE ("multiple values left on stack");
1390 if (stack_depth
> 0) /* stack_depth == 1 */
1393 DIE ("multiple values left on stack");
1395 /* Could handle this if it ever actually happened. */
1396 DIE ("relative expression computed constant");
1403 DIE ("stack underflow");
1405 DIE ("cannot handle location expression");
1408 return lose (*input
, expr
, len
, failure
, i
);
1411 /* Translate a C fragment for the location expression, using *INPUT
1412 as the starting location, begin from scratch if *INPUT is null.
1413 If DW_OP_fbreg is used, it may have a subfragment computing from
1414 the FB_ATTR location expression.
1416 On errors, call FAIL and never return. On success, return the
1417 first fragment created, which is also chained onto (*INPUT)->next.
1418 *INPUT is then updated with the new tail of that chain. */
1421 c_translate_location (struct obstack
*pool
,
1422 void (*fail
) (void *arg
, const char *fmt
, ...)
1423 __attribute__ ((noreturn
, format (printf
, 2, 3))),
1425 void (*emit_address
) (void *fail_arg
,
1426 struct obstack
*, Dwarf_Addr
),
1427 int indent
, Dwarf_Addr dwbias
, Dwarf_Addr pc_address
,
1428 Dwarf_Attribute
*attr
,
1429 const Dwarf_Op
*expr
, size_t len
,
1430 struct location
**input
, Dwarf_Attribute
*fb_attr
,
1431 const Dwarf_Op
*cfa_ops
)
1435 struct location_context
*ctx
;
1437 ctx
= new_context (pool
, fail
, fail_arg
, emit_address
, dwbias
, pc_address
,
1438 attr
, fb_attr
, cfa_ops
);
1441 ctx
= (*input
)->context
;
1442 assert (ctx
->pool
== pool
);
1443 if (pc_address
== 0)
1444 pc_address
= ctx
->pc
;
1445 else if (ctx
->pc
== 0)
1446 ctx
->pc
= pc_address
;
1447 // PR15148: disable this assertion, in case the PR15123 address-retry logic
1449 // assert (ctx->pc == pc_address);
1452 switch (*input
== NULL
? loc_address
: (*input
)->type
)
1455 /* We have a previous address computation.
1456 This expression will compute starting with that on the stack. */
1457 return location_from_address (ctx
, indent
, expr
, len
, input
);
1459 case loc_noncontiguous
:
1463 case loc_unavailable
:
1464 case loc_implicit_pointer
:
1465 /* The starting point is not an address computation, but a
1466 register or implicit value. We can only handle limited
1467 computations from here. */
1468 return location_relative (ctx
, indent
, expr
, len
, input
);
1479 /* Translate a C fragment for a direct argument VALUE. On errors, call FAIL,
1480 which should not return. Any later errors will use FAIL and FAIL_ARG from
1481 this translate call. On success, return the fragment created. */
1483 c_translate_argument (struct obstack
*pool
,
1484 void (*fail
) (void *arg
, const char *fmt
, ...)
1485 __attribute__ ((noreturn
, format (printf
, 2, 3))),
1487 void (*emit_address
) (void *fail_arg
,
1488 struct obstack
*, Dwarf_Addr
),
1489 int indent
, const char *value
)
1493 obstack_printf (pool
, "%*saddr = %s;\n", indent
* 2, "", value
);
1494 obstack_1grow (pool
, '\0');
1495 char *program
= obstack_finish (pool
);
1497 struct location
*loc
= obstack_alloc (pool
, sizeof *loc
);
1498 loc
->context
= new_context (pool
, fail
, fail_arg
, emit_address
, 0,
1499 0, NULL
, NULL
, NULL
);
1504 loc
->type
= loc_address
;
1505 loc
->frame_base
= NULL
;
1506 loc
->address
.declare
= NULL
;
1507 loc
->address
.program
= program
;
1508 loc
->address
.stack_depth
= 0;
1509 loc
->address
.used_deref
= false;
1515 /* Emit "uintNN_t TARGET = ...;". */
1517 emit_base_fetch (struct obstack
*pool
, Dwarf_Word byte_size
,
1518 bool signed_p
, const char *target
, struct location
*loc
)
1522 /* Emit size/signed coercion. */
1523 obstack_printf (pool
, "{ ");
1524 obstack_printf (pool
, "%sint%u_t value = ",
1525 (signed_p
? "" : "u"), (unsigned)(byte_size
* 8));
1530 obstack_printf (pool
, "addr;");
1534 if (byte_size
!= 0 && byte_size
!= (Dwarf_Word
) -1)
1535 obstack_printf (pool
, "deref (%" PRIu64
", addr);", byte_size
);
1537 obstack_printf (pool
, "deref (sizeof %s, addr);", target
);
1542 if (loc
->reg
.offset
!= 0)
1543 FAIL (loc
, N_("cannot handle offset into register in fetch"));
1544 obstack_printf (pool
, "fetch_register (%u);", loc
->reg
.regno
);
1547 case loc_noncontiguous
:
1548 FAIL (loc
, N_("noncontiguous location for base fetch"));
1551 case loc_implicit_pointer
:
1552 FAIL (loc
, N_("pointer has been optimized out"));
1555 case loc_unavailable
:
1556 FAIL (loc
, N_("location not available"));
1564 obstack_printf (pool
, " %s = value; }", target
);
1568 /* Emit "... = RVALUE;". */
1570 emit_base_store (struct obstack
*pool
, Dwarf_Word byte_size
,
1571 const char *rvalue
, struct location
*loc
)
1576 if (byte_size
!= 0 && byte_size
!= (Dwarf_Word
) -1)
1577 obstack_printf (pool
, "store_deref (%" PRIu64
", addr, %s); ",
1580 obstack_printf (pool
, "store_deref (sizeof %s, addr, %s); ",
1585 if (loc
->reg
.offset
!= 0)
1586 FAIL (loc
, N_("cannot handle offset into register in store"));
1587 obstack_printf (pool
, "store_register (%u, %s);", loc
->reg
.regno
, rvalue
);
1590 case loc_noncontiguous
:
1591 FAIL (loc
, N_("noncontiguous location for base store"));
1594 case loc_implicit_pointer
:
1595 FAIL (loc
, N_("pointer has been optimized out"));
1599 FAIL (loc
, N_("location is computed value, cannot store"));
1603 FAIL (loc
, N_("location is constant value, cannot store"));
1606 case loc_unavailable
:
1607 FAIL (loc
, N_("location is not available, cannot store"));
1619 /* Slice up an object into pieces no larger than MAX_PIECE_BYTES,
1620 yielding a loc_noncontiguous location unless LOC is small enough. */
1621 static struct location
*
1622 discontiguify (struct location_context
*ctx
, int indent
, struct location
*loc
,
1623 Dwarf_Word total_bytes
, Dwarf_Word max_piece_bytes
)
1625 inline bool pieces_small_enough (void)
1627 if (loc
->type
!= loc_noncontiguous
)
1628 return total_bytes
<= max_piece_bytes
;
1630 for (p
= loc
->pieces
; p
!= NULL
; p
= p
->next
)
1631 if (p
->byte_size
> max_piece_bytes
)
1636 /* Constants are always copied byte-wise, but we may need to
1637 * truncate to the total_bytes requested here. */
1638 if (loc
->type
== loc_constant
)
1640 if (loc
->byte_size
> total_bytes
)
1641 loc
->byte_size
= total_bytes
;
1645 if (pieces_small_enough ())
1648 struct location
*noncontig
= alloc_location (ctx
);
1649 noncontig
->next
= NULL
;
1650 noncontig
->type
= loc_noncontiguous
;
1651 noncontig
->byte_size
= total_bytes
;
1652 noncontig
->pieces
= NULL
;
1653 struct location
**tailpiece
= &noncontig
->pieces
;
1654 inline void add (struct location
*piece
)
1657 tailpiece
= &piece
->next
;
1664 /* Synthesize a piece that sets "container_addr" to the computed
1665 address of the whole object. Each piece will refer to this. */
1666 obstack_printf (ctx
->pool
, "%*scontainer_addr = addr;\n",
1668 loc
->next
= new_synthetic_loc (loc
, false);
1669 loc
->next
->byte_size
= loc
->byte_size
;
1670 loc
->next
->type
= loc_fragment
;
1671 loc
->next
->address
.declare
= "container_addr";
1674 /* Synthesize pieces that just compute "container_addr + N". */
1675 Dwarf_Word offset
= 0;
1676 while (total_bytes
- offset
> 0)
1678 Dwarf_Word size
= total_bytes
- offset
;
1679 if (size
> max_piece_bytes
)
1680 size
= max_piece_bytes
;
1682 obstack_printf (ctx
->pool
,
1683 "%*saddr = container_addr + " UFORMAT
";\n",
1684 indent
* 2, "", offset
);
1685 struct location
*piece
= new_synthetic_loc (loc
, false);
1686 piece
->byte_size
= size
;
1697 FAIL (loc
, N_("stack value too big for fetch ???"));
1701 FAIL (loc
, N_("single register too big for fetch/store ???"));
1704 case loc_implicit_pointer
:
1705 FAIL (loc
, N_("implicit pointer too big for fetch/store ???"));
1708 case loc_noncontiguous
:
1709 /* Could be handled if it ever happened. */
1710 FAIL (loc
, N_("cannot support noncontiguous location"));
1718 loc
->next
= noncontig
;
1722 /* Make a fragment that declares a union such as:
1728 } pieces __attribute__ ((packed));
1733 declare_noncontig_union (struct obstack
*pool
, int indent
,
1734 struct location
**input
, struct location
*loc
,
1738 FAIL (loc
, N_("declaring noncontig union for depth > 9, too many pieces"));
1740 obstack_printf (pool
, "%*sunion {\n", indent
++ * 2, "");
1742 obstack_printf (pool
, "%*schar bytes[%" PRIu64
"];\n",
1743 indent
* 2, "", loc
->byte_size
);
1745 if (loc
->type
== loc_noncontiguous
)
1747 Dwarf_Word offset
= 0;
1749 obstack_printf (pool
, "%*sstruct {\n", indent
++ * 2, "");
1751 for (p
= loc
->pieces
; p
!= NULL
; p
= p
->next
)
1753 obstack_printf (pool
, "%*suint%" PRIu64
"_t p%" PRIu64
";\n",
1754 indent
* 2, "", p
->byte_size
* 8, offset
);
1755 offset
+= p
->byte_size
;
1758 obstack_printf (pool
, "%*s} pieces __attribute__ ((packed));\n",
1762 obstack_printf (pool
, "%*suint%" PRIu64
"_t whole;\n",
1763 indent
* 2, "", loc
->byte_size
* 8);
1765 obstack_printf (pool
, "%*s} u_pieces%d;\n", --indent
* 2, "", depth
);
1767 loc
= new_synthetic_loc (*input
, false);
1768 loc
->type
= loc_decl
;
1769 (*input
)->next
= loc
;
1773 /* Determine the byte size of a base type. */
1775 base_byte_size (Dwarf_Die
*typedie
, struct location
*origin
)
1777 assert (dwarf_tag (typedie
) == DW_TAG_base_type
||
1778 dwarf_tag (typedie
) == DW_TAG_enumeration_type
);
1780 Dwarf_Attribute attr_mem
;
1782 if (dwarf_attr_integrate (typedie
, DW_AT_byte_size
, &attr_mem
) != NULL
1783 && dwarf_formudata (&attr_mem
, &size
) == 0)
1787 N_("cannot get byte_size attribute for type %s: %s"),
1788 dwarf_diename (typedie
) ?: "<anonymous>",
1794 base_encoding (Dwarf_Die
*typedie
, struct location
*origin
)
1796 if (! (dwarf_tag (typedie
) == DW_TAG_base_type
||
1797 dwarf_tag (typedie
) == DW_TAG_enumeration_type
))
1800 Dwarf_Attribute attr_mem
;
1801 Dwarf_Word encoding
;
1802 if (dwarf_attr_integrate (typedie
, DW_AT_encoding
, &attr_mem
) != NULL
1803 && dwarf_formudata (&attr_mem
, &encoding
) == 0)
1809 N_("cannot get encoding attribute for type %s: %s"),
1810 dwarf_diename (typedie) ?: "<anonymous>",
1818 /* Fetch the bitfield parameters. */
1820 get_bitfield (struct location
*loc
,
1821 Dwarf_Die
*die
, Dwarf_Word
*bit_offset
, Dwarf_Word
*bit_size
)
1823 Dwarf_Attribute attr_mem
;
1824 if (dwarf_attr_integrate (die
, DW_AT_bit_offset
, &attr_mem
) == NULL
1825 || dwarf_formudata (&attr_mem
, bit_offset
) != 0
1826 || dwarf_attr_integrate (die
, DW_AT_bit_size
, &attr_mem
) == NULL
1827 || dwarf_formudata (&attr_mem
, bit_size
) != 0)
1828 FAIL (loc
, N_("cannot get bit field parameters: %s"), dwarf_errmsg (-1));
1831 /* Translate a fragment to fetch the base-type value of BYTE_SIZE bytes
1832 at the *INPUT location and store it in lvalue TARGET. */
1834 translate_base_fetch (struct obstack
*pool
, int indent
,
1835 Dwarf_Word byte_size
, bool signed_p
,
1836 struct location
**input
, const char *target
,
1841 if ((*input
)->type
== loc_noncontiguous
)
1843 struct location
*p
= (*input
)->pieces
;
1845 declare_noncontig_union (pool
, indent
, input
, *input
, depth
);
1847 Dwarf_Word offset
= 0;
1848 char piece
[sizeof "u_pieces?.pieces.p" + 20] = "u_pieces?.pieces.p";
1849 piece
[8] = (char) ('0' + depth
);
1850 int pdepth
= depth
+ 1;
1853 struct location
*newp
= obstack_alloc (pool
, sizeof *newp
);
1856 (*input
)->next
= newp
;
1859 snprintf (&piece
[sizeof "u_pieces?.pieces.p" - 1], 20,
1860 "%" PRIu64
, offset
);
1861 translate_base_fetch (pool
, indent
, p
->byte_size
, signed_p
/* ? */,
1862 input
, piece
, pdepth
);
1863 (*input
)->type
= loc_fragment
;
1865 offset
+= p
->byte_size
;
1870 obstack_printf (pool
, "%*s%s = u_pieces%d.whole;\n", indent
* 2,
1873 else if ((*input
)->type
== loc_constant
)
1875 const unsigned char *constant_block
= (*input
)->constant_block
;
1876 const size_t byte_size
= (*input
)->byte_size
;
1879 declare_noncontig_union (pool
, indent
, input
, *input
, depth
);
1881 for (i
= 0; i
< byte_size
; ++i
)
1882 obstack_printf (pool
, "%*su_pieces%d.bytes[%zu] = %#x;\n", indent
* 2,
1883 "", depth
, i
, constant_block
[i
]);
1885 obstack_printf (pool
, "%*s%s = u_pieces%d.whole;\n", indent
* 2,
1891 case 0: /* Special case, means address size. */
1896 obstack_printf (pool
, "%*s", indent
* 2, "");
1897 deref
= emit_base_fetch (pool
, byte_size
, signed_p
, target
, *input
);
1898 obstack_printf (pool
, "\n");
1902 /* Could handle this generating call to memcpy equivalent. */
1903 FAIL (*input
, N_("fetch is larger than base integer types"));
1907 struct location
*loc
= new_synthetic_loc (*input
, deref
);
1908 loc
->byte_size
= byte_size
;
1909 loc
->type
= loc_final
;
1910 (*input
)->next
= loc
;
1914 /* Determine the maximum size of a base type, from some DIE in the CU. */
1916 max_fetch_size (struct location
*loc
, Dwarf_Die
*die
)
1919 uint8_t address_size
;
1920 Dwarf_Die
*cu
= dwarf_diecu (die
, &cu_mem
, &address_size
, NULL
);
1922 //TRANSLATORS: CU stands for 'compilation unit'
1923 FAIL (loc
, N_("cannot determine CU address size from %s: %s"),
1924 dwarf_diename (die
), dwarf_errmsg (-1));
1926 return address_size
;
1929 /* Translate a fragment to fetch the value of variable or member DIE
1930 at the *INPUT location and store it in lvalue TARGET. */
1932 c_translate_fetch (struct obstack
*pool
, int indent
,
1933 Dwarf_Addr dwbias
__attribute__ ((unused
)),
1934 Dwarf_Die
*die
, Dwarf_Die
*typedie
,
1935 struct location
**input
, const char *target
)
1937 struct location_context
*const ctx
= (*input
)->context
;
1938 assert (ctx
->pool
== pool
);
1942 Dwarf_Attribute size_attr
;
1943 Dwarf_Word byte_size
;
1944 if (dwarf_attr_integrate (die
, DW_AT_byte_size
, &size_attr
) == NULL
1945 || dwarf_formudata (&size_attr
, &byte_size
) != 0)
1946 byte_size
= base_byte_size (typedie
, *input
);
1948 Dwarf_Attribute encoding_attr
;
1949 Dwarf_Word encoding
;
1950 if (dwarf_attr_integrate (die
, DW_AT_encoding
, &encoding_attr
) == NULL
1951 || dwarf_formudata (&encoding_attr
, &encoding
) != 0)
1952 encoding
= base_encoding (typedie
, *input
);
1953 bool signed_p
= encoding
== DW_ATE_signed
|| encoding
== DW_ATE_signed_char
;
1955 *input
= discontiguify (ctx
, indent
, *input
, byte_size
,
1956 max_fetch_size (*input
, die
));
1958 if (dwarf_hasattr_integrate (die
, DW_AT_bit_offset
))
1960 /* This is a bit field. Fetch the containing base type into a
1961 temporary variable. */
1963 translate_base_fetch (pool
, indent
, byte_size
, signed_p
, input
, "tmp", 0);
1964 (*input
)->type
= loc_fragment
;
1965 (*input
)->address
.declare
= "tmp";
1967 Dwarf_Word bit_offset
= 0;
1968 Dwarf_Word bit_size
= 0;
1969 get_bitfield (*input
, die
, &bit_offset
, &bit_size
);
1971 obstack_printf (pool
, "%*s"
1972 "fetch_bitfield (%s, tmp, %" PRIu64
", %" PRIu64
");\n",
1973 indent
*2, "", target
, bit_offset
, bit_size
);
1975 struct location
*loc
= new_synthetic_loc (*input
, false);
1976 loc
->type
= loc_final
;
1977 (*input
)->next
= loc
;
1981 translate_base_fetch (pool
, indent
, byte_size
, signed_p
, input
, target
, 0);
1984 /* Translate a fragment to store RVALUE into the base-type value of
1985 BYTE_SIZE bytes at the *INPUT location. */
1987 translate_base_store (struct obstack
*pool
, int indent
, Dwarf_Word byte_size
,
1988 struct location
**input
, struct location
*store_loc
,
1989 const char *rvalue
, int depth
)
1993 if (store_loc
->type
== loc_noncontiguous
)
1995 declare_noncontig_union (pool
, indent
, input
, store_loc
, depth
);
1997 obstack_printf (pool
, "%*su_pieces%d.whole = %s;\n", indent
* 2,
1999 struct location
*loc
= new_synthetic_loc (*input
, deref
);
2000 loc
->type
= loc_fragment
;
2001 (*input
)->next
= loc
;
2004 Dwarf_Word offset
= 0;
2005 char piece
[sizeof "u_pieces?.pieces.p" + 20] = "u_pieces?.pieces.p";
2006 piece
[8] = (char) ('0' + depth
);
2008 int pdepth
= depth
+ 1;
2009 for (p
= store_loc
->pieces
; p
!= NULL
; p
= p
->next
)
2011 struct location
*newp
= obstack_alloc (pool
, sizeof *newp
);
2014 (*input
)->next
= newp
;
2017 snprintf (&piece
[sizeof "u_pieces?.pieces.p" - 1], 20, "%" PRIu64
,
2019 translate_base_store (pool
, indent
,
2020 p
->byte_size
, input
, *input
, piece
, pdepth
++);
2021 (*input
)->type
= loc_fragment
;
2023 offset
+= p
->byte_size
;
2026 (*input
)->type
= loc_final
;
2036 obstack_printf (pool
, "%*s", indent
* 2, "");
2037 deref
= emit_base_store (pool
, byte_size
, rvalue
, store_loc
);
2038 obstack_printf (pool
, "\n");
2042 /* Could handle this generating call to memcpy equivalent. */
2043 FAIL (*input
, N_("store is larger than base integer types"));
2047 struct location
*loc
= new_synthetic_loc (*input
, deref
);
2048 loc
->type
= loc_final
;
2049 (*input
)->next
= loc
;
2054 /* Translate a fragment to fetch the value of variable or member DIE
2055 at the *INPUT location and store it in rvalue RVALUE. */
2058 c_translate_store (struct obstack
*pool
, int indent
,
2059 Dwarf_Addr dwbias
__attribute__ ((unused
)),
2060 Dwarf_Die
*die
, Dwarf_Die
*typedie
,
2061 struct location
**input
, const char *rvalue
)
2063 struct location_context
*const ctx
= (*input
)->context
;
2064 assert (ctx
->pool
== pool
);
2068 Dwarf_Attribute size_attr
;
2069 Dwarf_Word byte_size
;
2070 if (dwarf_attr_integrate (die
, DW_AT_byte_size
, &size_attr
) == NULL
2071 || dwarf_formudata (&size_attr
, &byte_size
) != 0)
2072 byte_size
= base_byte_size (typedie
, *input
);
2074 Dwarf_Attribute encoding_attr
;
2075 Dwarf_Word encoding
;
2076 if (dwarf_attr_integrate (die
, DW_AT_encoding
, &encoding_attr
) == NULL
2077 || dwarf_formudata (&encoding_attr
, &encoding
) != 0)
2078 encoding
= base_encoding (typedie
, *input
);
2079 bool signed_p
= (encoding
== DW_ATE_signed
2080 || encoding
== DW_ATE_signed_char
);
2082 *input
= discontiguify (ctx
, indent
, *input
, byte_size
,
2083 max_fetch_size (*input
, die
));
2085 struct location
*store_loc
= *input
;
2087 if (dwarf_hasattr_integrate (die
, DW_AT_bit_offset
))
2089 /* This is a bit field. Fetch the containing base type into a
2090 temporary variable. */
2092 translate_base_fetch (pool
, indent
, byte_size
, signed_p
, input
, "tmp", 0);
2093 (*input
)->type
= loc_fragment
;
2094 (*input
)->address
.declare
= "tmp";
2096 Dwarf_Word bit_offset
= 0;
2097 Dwarf_Word bit_size
= 0;
2098 get_bitfield (*input
, die
, &bit_offset
, &bit_size
);
2100 obstack_printf (pool
, "%*s"
2101 "store_bitfield (tmp, %s, %" PRIu64
", %" PRIu64
");\n",
2102 indent
* 2, "", rvalue
, bit_offset
, bit_size
);
2104 struct location
*loc
= new_synthetic_loc (*input
, false);
2105 loc
->type
= loc_fragment
;
2106 (*input
)->next
= loc
;
2109 /* We have mixed RVALUE into the bits in "tmp".
2110 Now we'll store "tmp" back whence we fetched it. */
2114 translate_base_store (pool
, indent
, byte_size
, input
, store_loc
, rvalue
, 0);
2117 /* Translate a fragment to dereference the given pointer type,
2118 where *INPUT is the location of the pointer with that type.
2120 We chain on a loc_address program that yields this pointer value
2121 (i.e. the location of what it points to). */
2124 c_translate_pointer (struct obstack
*pool
, int indent
,
2125 Dwarf_Addr dwbias
__attribute__ ((unused
)),
2126 Dwarf_Die
*typedie
, struct location
**input
)
2128 assert (dwarf_tag (typedie
) == DW_TAG_pointer_type
||
2129 dwarf_tag (typedie
) == DW_TAG_reference_type
||
2130 dwarf_tag (typedie
) == DW_TAG_rvalue_reference_type
);
2132 Dwarf_Attribute attr_mem
;
2133 Dwarf_Word byte_size
;
2134 if (dwarf_attr_integrate (typedie
, DW_AT_byte_size
, &attr_mem
) == NULL
)
2136 else if (dwarf_formudata (&attr_mem
, &byte_size
) != 0)
2138 N_("cannot get byte_size attribute for type %s: %s"),
2139 dwarf_diename (typedie
) ?: "<anonymous>",
2142 if ((*input
)->type
== loc_implicit_pointer
)
2144 struct location
*const target
= (*input
)->pointer
.target
;
2145 const Dwarf_Sword offset
= (*input
)->pointer
.offset
;
2146 (*input
)->next
= target
;
2148 translate_offset (indent
, NULL
, 0, 0, NULL
, &(*input
)->next
, offset
);
2149 *input
= (*input
)->next
;
2153 bool signed_p
= false; /* XXX: Does not matter? */
2155 translate_base_fetch (pool
, indent
+ 1, byte_size
, signed_p
, input
,
2157 (*input
)->type
= loc_address
;
2163 c_translate_addressof (struct obstack
*pool
, int indent
,
2164 Dwarf_Addr dwbias
__attribute__ ((unused
)),
2166 Dwarf_Die
*typedie
__attribute__ ((unused
)),
2167 struct location
**input
, const char *target
)
2171 if (dwarf_hasattr_integrate (die
, DW_AT_bit_offset
)
2172 || dwarf_hasattr_integrate (die
, DW_AT_data_bit_offset
))
2173 FAIL (*input
, N_("cannot take the address of a bit field"));
2175 switch ((*input
)->type
)
2178 obstack_printf (pool
, "%*s%s = addr;\n", indent
* 2, "", target
);
2179 (*input
)->next
= new_synthetic_loc (*input
, false);
2180 (*input
)->next
->type
= loc_final
;
2184 FAIL (*input
, N_("cannot take address of object in register"));
2186 case loc_noncontiguous
:
2187 FAIL (*input
, N_("cannot take address of noncontiguous object"));
2190 FAIL (*input
, N_("cannot take address of computed value"));
2193 FAIL (*input
, N_("cannot take address of constant value"));
2195 case loc_unavailable
:
2196 FAIL (*input
, N_("cannot take address of unavailable value"));
2198 case loc_implicit_pointer
:
2199 FAIL (*input
, N_("cannot take address of implicit pointer"));
2209 /* Translate a fragment to write the given pointer value,
2210 where *INPUT is the location of the pointer with that type.
2214 c_translate_pointer_store (struct obstack
*pool
, int indent
,
2215 Dwarf_Addr dwbias
__attribute__ ((unused
)),
2216 Dwarf_Die
*typedie
, struct location
**input
,
2219 assert (dwarf_tag (typedie
) == DW_TAG_pointer_type
);
2221 Dwarf_Attribute attr_mem
;
2222 Dwarf_Word byte_size
;
2223 if (dwarf_attr_integrate (typedie
, DW_AT_byte_size
, &attr_mem
) == NULL
)
2225 else if (dwarf_formudata (&attr_mem
, &byte_size
) != 0)
2227 N_("cannot get byte_size attribute for type %s: %s"),
2228 dwarf_diename (typedie
) ?: "<anonymous>",
2231 translate_base_store (pool
, indent
+ 1, byte_size
,
2232 input
, *input
, rvalue
, 0);
2234 // XXX: what about multiple-location lvalues?
2237 /* Determine the element stride of a pointer to a type. */
2239 pointer_stride (Dwarf_Die
*typedie
, struct location
*origin
)
2241 Dwarf_Attribute attr_mem
;
2242 Dwarf_Die die_mem
= *typedie
;
2243 int typetag
= dwarf_tag (&die_mem
);
2244 while (typetag
== DW_TAG_typedef
||
2245 typetag
== DW_TAG_const_type
||
2246 typetag
== DW_TAG_volatile_type
)
2248 if (dwarf_attr_integrate (&die_mem
, DW_AT_type
, &attr_mem
) == NULL
2249 || dwarf_formref_die (&attr_mem
, &die_mem
) == NULL
)
2250 //TRANSLATORS: This refers to the basic type, (stripped of const/volatile/etc.)
2251 FAIL (origin
, N_("cannot get inner type of type %s: %s"),
2252 dwarf_diename (&die_mem
) ?: "<anonymous>",
2254 typetag
= dwarf_tag (&die_mem
);
2257 if (dwarf_attr_integrate (&die_mem
, DW_AT_byte_size
, &attr_mem
) != NULL
)
2260 if (dwarf_formudata (&attr_mem
, &stride
) == 0)
2263 N_("cannot get byte_size attribute for array element type %s: %s"),
2264 dwarf_diename (&die_mem
) ?: "<anonymous>",
2268 FAIL (origin
, N_("confused about array element size"));
2272 /* Determine the element stride of an array type. */
2274 array_stride (Dwarf_Die
*typedie
, struct location
*origin
)
2276 Dwarf_Attribute attr_mem
;
2277 if (dwarf_attr_integrate (typedie
, DW_AT_byte_stride
, &attr_mem
) != NULL
)
2280 if (dwarf_formudata (&attr_mem
, &stride
) == 0)
2282 FAIL (origin
, N_("cannot get byte_stride attribute array type %s: %s"),
2283 dwarf_diename (typedie
) ?: "<anonymous>",
2288 if (dwarf_attr_integrate (typedie
, DW_AT_type
, &attr_mem
) == NULL
2289 || dwarf_formref_die (&attr_mem
, &die_mem
) == NULL
)
2290 FAIL (origin
, N_("cannot get element type of array type %s: %s"),
2291 dwarf_diename (typedie
) ?: "<anonymous>",
2294 return pointer_stride (&die_mem
, origin
);
2298 translate_array (struct obstack
*pool
, int indent
,
2299 Dwarf_Die
*anydie
, Dwarf_Word stride
,
2300 struct location
**input
,
2301 const char *idx
, Dwarf_Word const_idx
)
2305 struct location
*loc
= *input
;
2306 while (loc
->type
== loc_noncontiguous
)
2309 FAIL (*input
, N_("cannot dynamically index noncontiguous array"));
2312 Dwarf_Word offset
= const_idx
* stride
;
2313 struct location
*piece
= loc
->pieces
;
2314 while (piece
!= NULL
&& offset
>= piece
->byte_size
)
2316 offset
-= piece
->byte_size
;
2317 piece
= piece
->next
;
2320 //TRANSLATORS: The index is constant
2321 FAIL (*input
, N_("constant index is outside noncontiguous array"));
2322 if (offset
% stride
!= 0 || piece
->byte_size
< stride
)
2323 FAIL (*input
, N_("noncontiguous array splits elements"));
2324 const_idx
= offset
/ stride
;
2334 obstack_printf (pool
, "%*saddr += %s * " UFORMAT
";\n",
2335 indent
* 2, "", idx
, stride
);
2337 obstack_printf (pool
, "%*saddr += " UFORMAT
" * " UFORMAT
";\n",
2338 indent
* 2, "", const_idx
, stride
);
2339 loc
= new_synthetic_loc (loc
, false);
2344 FAIL (*input
, N_("cannot index array stored in a register"));
2345 else if (const_idx
> max_fetch_size (loc
, anydie
) / stride
)
2346 FAIL (*input
, N_("constant index is outside array held in register"));
2349 loc
->reg
.offset
+= const_idx
* stride
;
2356 FAIL (*input
, N_("cannot index into constant value"));
2357 else if (const_idx
> loc
->byte_size
/ stride
)
2358 //TRANSLATORS: The index is constant
2359 FAIL (*input
, N_("constant index is outside constant array value"));
2362 loc
->byte_size
= stride
;
2363 loc
->constant_block
+= const_idx
* stride
;
2368 case loc_implicit_pointer
:
2370 FAIL (*input
, N_("cannot index into implicit pointer"));
2372 loc
->pointer
.offset
+= const_idx
* stride
;
2376 if (idx
!= NULL
|| const_idx
!= 0)
2377 FAIL (*input
, N_("cannot index into computed value"));
2380 case loc_unavailable
:
2381 if (idx
!= NULL
|| const_idx
!= 0)
2382 FAIL (*input
, N_("cannot index into unavailable value"));
2390 (*input
)->next
= loc
;
2391 *input
= (*input
)->next
;
2395 c_translate_array (struct obstack
*pool
, int indent
,
2396 Dwarf_Addr dwbias
__attribute__ ((unused
)),
2397 Dwarf_Die
*typedie
, struct location
**input
,
2398 const char *idx
, Dwarf_Word const_idx
)
2400 assert (dwarf_tag (typedie
) == DW_TAG_array_type
||
2401 dwarf_tag (typedie
) == DW_TAG_pointer_type
);
2403 return translate_array (pool
, indent
, typedie
,
2404 array_stride (typedie
, *input
),
2405 input
, idx
, const_idx
);
2409 c_translate_array_pointer (struct obstack
*pool
, int indent
,
2410 Dwarf_Die
*typedie
, struct location
**input
,
2411 const char *idx
, Dwarf_Word const_idx
)
2413 return translate_array (pool
, indent
, typedie
,
2414 pointer_stride (typedie
, *input
),
2415 input
, idx
, const_idx
);
2418 /* Emitting C code for finalized fragments. */
2420 #define emit(fmt, ...) fprintf (out, fmt, ## __VA_ARGS__)
2422 /* Open a block with a comment giving the original DWARF expression. */
2424 emit_header (FILE *out
, struct location
*loc
, unsigned int hindent
)
2426 if (loc
->ops
== NULL
)
2427 emit ("%*s{ // synthesized\n", hindent
* 2, "");
2430 emit ("%*s{ // DWARF expression:", hindent
* 2, "");
2432 for (i
= 0; i
< loc
->nops
; ++i
)
2434 emit (" %#x", loc
->ops
[i
].atom
);
2435 if (loc
->ops
[i
].number2
== 0)
2437 if (loc
->ops
[i
].number
!= 0)
2438 emit ("(%" PRId64
")", loc
->ops
[i
].number
);
2441 emit ("(%" PRId64
",%" PRId64
")",
2442 loc
->ops
[i
].number
, loc
->ops
[i
].number2
);
2448 /* Emit a code fragment to assign the target variable to a register value. */
2450 emit_loc_register (FILE *out
, struct location
*loc
, unsigned int indent
,
2453 assert (loc
->type
== loc_register
);
2455 if (loc
->reg
.offset
!= 0)
2456 FAIL (loc
, N_("cannot handle offset into register in fetch"));
2458 emit ("%*s%s = fetch_register (%u);\n",
2459 indent
* 2, "", target
, loc
->reg
.regno
);
2462 /* Emit a code fragment to assign the target variable to an address. */
2464 emit_loc_address (FILE *out
, struct location
*loc
, unsigned int indent
,
2467 assert (loc
->type
== loc_address
|| loc
->type
== loc_value
);
2469 if (loc
->address
.stack_depth
== 0)
2470 /* Synthetic program. */
2471 emit ("%s", loc
->address
.program
);
2474 emit ("%*s{\n", indent
* 2, "");
2475 emit ("%*s%s " STACKFMT
, (indent
+ 1) * 2, "",
2476 stack_slot_type (loc
, false), 0);
2478 for (i
= 1; i
< loc
->address
.stack_depth
; ++i
)
2479 emit (", " STACKFMT
, i
);
2482 emit ("%s%*s%s = " STACKFMT
";\n", loc
->address
.program
,
2483 (indent
+ 1) * 2, "", target
, 0);
2484 emit ("%*s}\n", indent
* 2, "");
2488 /* Emit a code fragment to declare the target variable and
2489 assign it to an address-sized value. */
2491 emit_loc_value (FILE *out
, struct location
*loc
, unsigned int indent
,
2492 const char *target
, bool declare
,
2493 bool *used_deref
, unsigned int *max_stack
)
2496 emit ("%*s%s %s;\n", indent
* 2, "", stack_slot_type (loc
, false), target
);
2498 emit_header (out
, loc
, indent
++);
2507 emit_loc_register (out
, loc
, indent
, target
);
2512 emit_loc_address (out
, loc
, indent
, target
);
2513 *used_deref
= *used_deref
|| loc
->address
.used_deref
;
2514 if (loc
->address
.stack_depth
> *max_stack
)
2515 *max_stack
= loc
->address
.stack_depth
;
2519 emit ("%*s}\n", --indent
* 2, "");
2523 c_emit_location (FILE *out
, struct location
*loc
, int indent
,
2524 unsigned int *max_stack
)
2526 emit ("%*s{\n", indent
* 2, "");
2528 bool declared_addr
= false;
2530 for (l
= loc
; l
!= NULL
; l
= l
->next
)
2534 emit ("%s", l
->address
.program
);
2541 declared_addr
= true;
2542 l
->address
.declare
= "addr";
2545 if (l
->address
.declare
!= NULL
)
2547 if (l
->byte_size
== 0 || l
->byte_size
== (Dwarf_Word
) -1)
2548 emit ("%*s%s %s;\n", (indent
+ 1) * 2, "",
2549 stack_slot_type (l
, false), l
->address
.declare
);
2551 emit ("%*suint%" PRIu64
"_t %s;\n", (indent
+ 1) * 2, "",
2552 l
->byte_size
* 8, l
->address
.declare
);
2562 for (l
= loc
; l
!= NULL
; l
= l
->next
)
2563 if (l
->frame_base
!= NULL
)
2565 emit_loc_value (out
, l
->frame_base
, indent
, "frame_base", true,
2570 for (; loc
->next
!= NULL
; loc
= loc
->next
)
2575 /* Emit the program fragment to calculate the address. */
2576 emit_loc_value (out
, loc
, indent
+ 1, "addr", false, &deref
, max_stack
);
2580 emit ("%s", loc
->address
.program
);
2581 deref
= deref
|| loc
->address
.used_deref
;
2586 case loc_noncontiguous
:
2588 case loc_implicit_pointer
:
2589 /* These don't produce any code directly.
2590 The next address/final record incorporates the value. */
2593 case loc_final
: /* Should be last in chain! */
2599 if (loc
->type
!= loc_final
) /* Unfinished chain. */
2602 emit ("%s%*s}\n", loc
->address
.program
, indent
* 2, "");
2604 if (loc
->address
.stack_depth
> *max_stack
)
2605 *max_stack
= loc
->address
.stack_depth
;
2607 return deref
|| loc
->address
.used_deref
;
2612 /* vim: set sw=2 ts=8 cino=>4,n-2,{2,^-2,t0,(0,u0,w1,M1 : */