1 // dwarf location-list-to-c translator
2 // Copyright (C) 2005-2015 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, "", (uint64_t) 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: %" PRIu64
", %" PRIu64
")"),
314 failure
, i
, lexpr
[i
].offset
,
315 (unsigned) 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, "", (unsigned)ignore
);
538 if (sp
>= stack_depth
)
541 push (STACKFMT
, (unsigned)sp
);
551 deepen (); /* Use a temporary slot. */
553 STACKFMT
" = " STACKFMT
", "
554 STACKFMT
" = " STACKFMT
", "
555 STACKFMT
" = " STACKFMT
";\n",
557 (unsigned)STACK (-1), (unsigned)STACK (0),
558 (unsigned)STACK (0), (unsigned)STACK (1),
559 (unsigned)STACK (1), (unsigned)STACK (-1));
565 deepen (); /* Use a temporary slot. */
567 STACKFMT
" = " STACKFMT
", "
568 STACKFMT
" = " STACKFMT
", "
569 STACKFMT
" = " STACKFMT
", "
570 STACKFMT
" = " STACKFMT
";\n",
572 (unsigned)STACK (-1), (unsigned)STACK (0),
573 (unsigned)STACK (0), (unsigned)STACK (1),
574 (unsigned)STACK (1), (unsigned)STACK (2),
575 (unsigned)STACK (2), (unsigned)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
")", (unsigned)addr
);
604 case DW_OP_deref_size
:
607 push ("deref (" UFORMAT
", " STACKFMT
")",
608 expr
[i
].number
, (unsigned)addr
);
617 push ("xderef (sizeof (void *), " STACKFMT
", " STACKFMT
")",
618 (unsigned)addr
, (unsigned)as
);
623 case DW_OP_xderef_size
:
627 push ("xderef (" UFORMAT
", " STACKFMT
", " STACKFMT
")",
628 expr
[i
].number
, (unsigned)addr
, (unsigned)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
, (int64_t)value
);
661 /* Arithmetic operations. */
662 #define UNOP(dw_op, c_op) \
663 case DW_OP_##dw_op: \
666 push ("%s (" STACKFMT ")", #c_op, (unsigned)tos); \
669 #define BINOP(dw_op, c_op) \
670 case DW_OP_##dw_op: \
674 push (STACKFMT " %s " STACKFMT, (unsigned)a, #c_op, (unsigned)b); \
690 /* Comparisons are binary operators too. */
705 push ("(%s) " STACKFMT
" >> (%s)" STACKFMT
,
706 stack_slot_type (loc
, true), (unsigned)a
,
707 stack_slot_type (loc
, true), (unsigned)b
);
715 push ("dwarf_div_op((%s) " STACKFMT
", (%s) " STACKFMT
")",
716 stack_slot_type (loc
, true), (unsigned)a
,
717 stack_slot_type (loc
, true), (unsigned)b
);
726 push ("dwarf_mod_op((%s) " STACKFMT
", (%s) " STACKFMT
")",
727 stack_slot_type (loc
, false), (unsigned)a
,
728 stack_slot_type (loc
, false), (unsigned)b
);
733 case DW_OP_plus_uconst
:
736 push (STACKFMT
" + " UFORMAT
, (unsigned)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
, (int64_t)value
);
756 DIE ("DW_OP_fbreg from DW_AT_frame_base");
758 push ("frame_base + " SFORMAT
, (int64_t)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");
861 // This is slightly weird/inefficient, but golang is known
862 // to produce DW_OP_call_frame_cfa; DW_OP_consts: 8; DW_OP_plus
863 // instead of a simple DW_OP_fbreg 8.
869 case DW_OP_push_object_address
:
870 DIE ("unhandled DW_OP_push_object_address");
873 case DW_OP_GNU_entry_value
:
874 DIE ("unhandled DW_OP_GNU_entry_value");
878 DIE ("unrecognized operation");
886 if (piece_expr_start
!= i
)
887 DIE ("extra operations after last DW_OP_piece");
889 loc
->type
= loc_noncontiguous
;
890 loc
->pieces
= pieces
;
891 loc
->byte_size
= piece_total_bytes
;
896 DIE ("stack underflow");
906 /* Translate a location starting from an address or nothing. */
907 static struct location
*
908 location_from_address (struct location_context
*ctx
, int indent
,
909 const Dwarf_Op
*expr
, size_t len
,
910 struct location
**input
)
912 struct location
*loc
= obstack_alloc (ctx
->pool
, sizeof *loc
);
915 loc
->frame_base
= NULL
;
919 bool need_fb
= false;
921 const char *failure
= translate (ctx
, indent
+ 1, expr
, len
,
922 *input
, &need_fb
, &loser
, loc
);
924 return lose (loc
, expr
, len
, failure
, loser
);
929 /* The main expression uses DW_OP_fbreg, so we need to compute
930 the DW_AT_frame_base attribute expression's value first. */
931 static Dwarf_Op dummy_cfa_op
= { .atom
= DW_OP_call_frame_cfa
};
936 if (ctx
->fb_attr
== NULL
)
938 // Lets just assume we want DW_OP_call_frame_cfa.
939 // Some (buggy golang) DWARF producers use that directly in
940 // location descriptions. And at least we should have a chance
941 // to get an actual call frame address that way.
943 fb_expr
= &dummy_cfa_op
;
947 switch (dwarf_getlocation_addr (ctx
->fb_attr
, ctx
->pc
,
948 &fb_expr
, &fb_len
, 1))
950 case 1: /* Should always happen. */
952 goto fb_inaccessible
;
955 default: /* Shouldn't happen. */
957 FAIL (loc
, N_("dwarf_getlocation_addr (form %#x): %s"),
958 dwarf_whatform (ctx
->fb_attr
), dwarf_errmsg (-1));
961 case 0: /* Shouldn't happen. */
963 FAIL (loc
, N_("DW_AT_frame_base not accessible at this address"));
968 // If it is DW_OP_call_frame_cfa then get cfi cfa ops.
969 const Dwarf_Op
* fb_ops
;
970 if (fb_len
== 1 && fb_expr
[0].atom
== DW_OP_call_frame_cfa
)
972 if (ctx
->cfa_ops
== NULL
)
973 FAIL (loc
, N_("No cfa_ops supplied, but needed by DW_OP_call_frame_cfa"));
974 fb_ops
= ctx
->cfa_ops
;
979 loc
->frame_base
= alloc_location (ctx
);
980 failure
= translate (ctx
, indent
+ 1, fb_ops
, fb_len
, NULL
,
981 NULL
, &loser
, loc
->frame_base
);
983 return lose (loc
, fb_ops
, fb_len
, failure
, loser
);
987 (*input
)->next
= loc
;
993 #if _ELFUTILS_PREREQ (0, 149)
994 static struct location
*
995 location_from_attr (struct location_context
*ctx
, int indent
,
996 Dwarf_Attribute
*attr
)
1000 switch (dwarf_getlocation_addr (attr
, ctx
->pc
, &expr
, &len
, 1))
1002 case 1: /* Should always happen. */
1007 case 0: /* Shouldn't happen. */
1008 (*ctx
->fail
) (ctx
->fail_arg
, N_("not accessible at this address (%#" PRIx64
")"), ctx
->pc
);
1011 default: /* Shouldn't happen. */
1013 (*ctx
->fail
) (ctx
->fail_arg
, "dwarf_getlocation_addr: %s",
1018 struct location
*input
= NULL
;
1019 return location_from_address (ctx
, indent
, expr
, len
, &input
);
1023 static struct location
*
1024 translate_offset (int indent
, const Dwarf_Op
*expr
, size_t len
, size_t i
,
1025 struct location
*head
, struct location
**input
,
1028 struct location_context
*const ctx
= (*input
)->context
;
1030 #define DIE(msg) return lose (*input, expr, len, N_(msg), i)
1032 while ((*input
)->type
== loc_noncontiguous
)
1034 /* We are starting from a noncontiguous object (DW_OP_piece).
1035 Find the piece we want. */
1037 struct location
*piece
= (*input
)->pieces
;
1038 while (piece
!= NULL
&& offset
>= piece
->byte_size
)
1040 offset
-= piece
->byte_size
;
1041 piece
= piece
->next
;
1044 DIE ("offset outside available pieces");
1046 assert ((*input
)->next
== NULL
);
1047 (*input
)->next
= piece
;
1051 switch ((*input
)->type
)
1055 /* The piece we want is actually in memory. Use the same
1056 program to compute the address from the preceding input. */
1058 struct location
*loc
= obstack_alloc (ctx
->pool
, sizeof *loc
);
1062 (*input
)->next
= loc
;
1065 /* The piece addresses exactly where we want to go. */
1071 /* Add a second fragment to offset the piece address. */
1072 obstack_printf (ctx
->pool
, "%*saddr += " SFORMAT
"\n",
1073 indent
* 2, "", (int64_t)offset
);
1074 *input
= loc
->next
= new_synthetic_loc (*input
, false);
1077 /* That's all she wrote. */
1082 /* This piece (or the whole struct) fits in a register. */
1083 (*input
)->reg
.offset
+= offset
;
1084 return head
?: *input
;
1087 /* This piece has a constant offset. */
1088 if (offset
>= (*input
)->byte_size
)
1089 DIE ("offset outside available constant block");
1090 (*input
)->constant_block
+= offset
;
1091 (*input
)->byte_size
-= offset
;
1092 return head
?: *input
;
1094 case loc_implicit_pointer
:
1095 /* This piece is an implicit pointer. */
1096 (*input
)->pointer
.offset
+= offset
;
1097 return head
?: *input
;
1099 case loc_unavailable
:
1100 /* Let it be diagnosed later. */
1101 return head
?: *input
;
1104 /* The piece we want is part of a computed offset.
1105 If it's the whole thing, we are done. */
1107 return head
?: *input
;
1108 DIE ("XXX extract partial rematerialized value");
1119 /* Translate a location starting from a non-address "on the top of the
1120 stack". The *INPUT location is a register name or noncontiguous
1121 object specification, and this expression wants to find the "address"
1122 of an object (or the actual value) relative to that "address". */
1124 static struct location
*
1125 location_relative (struct location_context
*ctx
, int indent
,
1126 const Dwarf_Op
*expr
, size_t len
,
1127 struct location
**input
)
1129 Dwarf_Sword
*stack
= NULL
;
1130 unsigned int stack_depth
= 0, max_stack
= 0;
1131 inline void deepen (void)
1133 if (stack_depth
== max_stack
)
1136 obstack_blank (ctx
->pool
, sizeof stack
[0]);
1137 stack
= (void *) obstack_base (ctx
->pool
);
1142 if (stack_depth > 0) \
1146 int var = stack_depth
1147 #define PUSH (deepen (), stack_depth++)
1148 #define STACK(idx) (stack_depth - 1 - (idx))
1149 #define STACKWORD(idx) stack[STACK (idx)]
1151 /* Don't put stack operations in the arguments to this. */
1152 #define push(value) (stack[PUSH] = (value))
1154 const char *failure
= NULL
;
1155 #define DIE(msg) do { failure = N_(msg); goto fail; } while (0)
1157 struct location
*head
= NULL
;
1159 for (i
= 0; i
< len
; ++i
)
1164 switch (expr
[i
].atom
)
1166 /* Basic stack operations. */
1171 if (stack_depth
< 1)
1175 unsigned int tos
= STACK (0);
1181 if (stack_depth
> 0)
1183 else if (*input
!= NULL
)
1184 /* Mark that we have consumed the input. */
1187 /* Hits if cleared above, or if we had no input at all. */
1192 sp
= expr
[i
].number
;
1194 if (sp
>= stack_depth
)
1205 if (stack_depth
< 2)
1207 deepen (); /* Use a temporary slot. */
1208 STACKWORD (-1) = STACKWORD (0);
1209 STACKWORD (0) = STACKWORD (1);
1210 STACKWORD (1) = STACKWORD (-1);
1214 if (stack_depth
< 3)
1216 deepen (); /* Use a temporary slot. */
1217 STACKWORD (-1) = STACKWORD (0);
1218 STACKWORD (0) = STACKWORD (1);
1219 STACKWORD (2) = STACKWORD (2);
1220 STACKWORD (2) = STACKWORD (-1);
1224 /* Control flow operations. */
1228 if (stack
[taken
] == 0)
1235 Dwarf_Off target
= expr
[i
].offset
+ 3 + expr
[i
].number
;
1236 while (i
+ 1 < len
&& expr
[i
+ 1].offset
< target
)
1238 if (expr
[i
+ 1].offset
!= target
)
1239 DIE ("invalid skip target");
1243 /* Memory access. */
1245 case DW_OP_deref_size
:
1247 case DW_OP_xderef_size
:
1249 /* Register-relative addressing. */
1250 case DW_OP_breg0
... DW_OP_breg31
:
1254 /* This started from a register, but now it's following a pointer.
1255 So we can do the translation starting from address here. */
1256 return location_from_address (ctx
, indent
, expr
, len
, input
);
1259 /* Constant-value operations. */
1261 DIE ("static calculation depends on load-time address");
1262 push (ctx
->dwbias
+ expr
[i
].number
);
1265 case DW_OP_lit0
... DW_OP_lit31
:
1266 value
= expr
[i
].atom
- DW_OP_lit0
;
1279 value
= expr
[i
].number
;
1284 /* Arithmetic operations. */
1285 #define UNOP(dw_op, c_op) \
1286 case DW_OP_##dw_op: \
1289 push (c_op (stack[tos])); \
1292 #define BINOP(dw_op, c_op) \
1293 case DW_OP_##dw_op: \
1297 push (stack[a] c_op stack[b]); \
1301 #define op_abs(x) (x < 0 ? -x : x)
1314 /* Comparisons are binary operators too. */
1329 push ((Dwarf_Word
) stack
[a
] >> (Dwarf_Word
) stack
[b
]);
1333 /* Simple addition we may be able to handle relative to
1334 the starting register name. */
1338 value
= -stack
[tos
];
1347 case DW_OP_plus_uconst
:
1348 value
= expr
[i
].number
;
1350 if (stack_depth
> 0)
1352 /* It's just private diddling after all. */
1354 push (stack
[a
] + value
);
1360 /* This is the primary real-world case: the expression takes
1361 the input address and adds a constant offset. */
1363 head
= translate_offset (indent
, expr
, len
, i
, head
, input
, value
);
1364 if (head
!= NULL
&& i
+ 1 < len
)
1366 if ((*input
)->type
!= loc_address
)
1367 DIE ("too much computation for non-address location");
1369 /* This expression keeps going, but further
1370 computations now have an address to start with.
1371 So we can punt to the address computation generator. */
1372 struct location
*loc
= location_from_address
1373 (ctx
, indent
, &expr
[i
+ 1], len
- i
- 1, input
);
1379 /* Direct register contents. */
1380 case DW_OP_reg0
... DW_OP_reg31
:
1385 /* Special magic. */
1387 DIE ("DW_OP_piece");
1390 case DW_OP_push_object_address
:
1391 DIE ("unhandled DW_OP_push_object_address");
1394 case DW_OP_GNU_entry_value
:
1395 DIE ("unhandled DW_OP_GNU_entry_value");
1399 DIE ("unrecognized operation");
1404 if (stack_depth
> 1)
1405 DIE ("multiple values left on stack");
1407 if (stack_depth
> 0) /* stack_depth == 1 */
1410 DIE ("multiple values left on stack");
1412 /* Could handle this if it ever actually happened. */
1413 DIE ("relative expression computed constant");
1420 DIE ("stack underflow");
1422 DIE ("cannot handle location expression");
1425 return lose (*input
, expr
, len
, failure
, i
);
1428 /* Translate a C fragment for the location expression, using *INPUT
1429 as the starting location, begin from scratch if *INPUT is null.
1430 If DW_OP_fbreg is used, it may have a subfragment computing from
1431 the FB_ATTR location expression.
1433 On errors, call FAIL and never return. On success, return the
1434 first fragment created, which is also chained onto (*INPUT)->next.
1435 *INPUT is then updated with the new tail of that chain. */
1438 c_translate_location (struct obstack
*pool
,
1439 void (*fail
) (void *arg
, const char *fmt
, ...)
1440 __attribute__ ((noreturn
, format (printf
, 2, 3))),
1442 void (*emit_address
) (void *fail_arg
,
1443 struct obstack
*, Dwarf_Addr
),
1444 int indent
, Dwarf_Addr dwbias
, Dwarf_Addr pc_address
,
1445 Dwarf_Attribute
*attr
,
1446 const Dwarf_Op
*expr
, size_t len
,
1447 struct location
**input
, Dwarf_Attribute
*fb_attr
,
1448 const Dwarf_Op
*cfa_ops
)
1452 struct location_context
*ctx
;
1454 ctx
= new_context (pool
, fail
, fail_arg
, emit_address
, dwbias
, pc_address
,
1455 attr
, fb_attr
, cfa_ops
);
1458 ctx
= (*input
)->context
;
1459 assert (ctx
->pool
== pool
);
1460 if (pc_address
== 0)
1461 pc_address
= ctx
->pc
;
1462 else if (ctx
->pc
== 0)
1463 ctx
->pc
= pc_address
;
1464 // PR15148: disable this assertion, in case the PR15123 address-retry logic
1466 // assert (ctx->pc == pc_address);
1469 switch (*input
== NULL
? loc_address
: (*input
)->type
)
1472 /* We have a previous address computation.
1473 This expression will compute starting with that on the stack. */
1474 return location_from_address (ctx
, indent
, expr
, len
, input
);
1476 case loc_noncontiguous
:
1480 case loc_unavailable
:
1481 case loc_implicit_pointer
:
1482 /* The starting point is not an address computation, but a
1483 register or implicit value. We can only handle limited
1484 computations from here. */
1485 return location_relative (ctx
, indent
, expr
, len
, input
);
1496 /* Translate a C fragment for a direct argument VALUE. On errors, call FAIL,
1497 which should not return. Any later errors will use FAIL and FAIL_ARG from
1498 this translate call. On success, return the fragment created. */
1500 c_translate_argument (struct obstack
*pool
,
1501 void (*fail
) (void *arg
, const char *fmt
, ...)
1502 __attribute__ ((noreturn
, format (printf
, 2, 3))),
1504 void (*emit_address
) (void *fail_arg
,
1505 struct obstack
*, Dwarf_Addr
),
1506 int indent
, const char *value
)
1510 obstack_printf (pool
, "%*saddr = %s;\n", indent
* 2, "", value
);
1511 obstack_1grow (pool
, '\0');
1512 char *program
= obstack_finish (pool
);
1514 struct location
*loc
= obstack_alloc (pool
, sizeof *loc
);
1515 loc
->context
= new_context (pool
, fail
, fail_arg
, emit_address
, 0,
1516 0, NULL
, NULL
, NULL
);
1521 loc
->type
= loc_address
;
1522 loc
->frame_base
= NULL
;
1523 loc
->address
.declare
= NULL
;
1524 loc
->address
.program
= program
;
1525 loc
->address
.stack_depth
= 0;
1526 loc
->address
.used_deref
= false;
1532 /* Emit "uintNN_t TARGET = ...;". */
1534 emit_base_fetch (struct obstack
*pool
, Dwarf_Word byte_size
,
1535 bool signed_p
, const char *target
, struct location
*loc
)
1539 /* Emit size/signed coercion. */
1540 obstack_printf (pool
, "{ ");
1541 obstack_printf (pool
, "%sint%u_t value = ",
1542 (signed_p
? "" : "u"), (unsigned)(byte_size
* 8));
1547 obstack_printf (pool
, "addr;");
1551 if (byte_size
!= 0 && byte_size
!= (Dwarf_Word
) -1)
1552 obstack_printf (pool
, "deref (%" PRIu64
", addr);", byte_size
);
1554 obstack_printf (pool
, "deref (sizeof %s, addr);", target
);
1559 if (loc
->reg
.offset
!= 0)
1560 FAIL (loc
, N_("cannot handle offset into register in fetch"));
1561 obstack_printf (pool
, "fetch_register (%u);", loc
->reg
.regno
);
1564 case loc_noncontiguous
:
1565 FAIL (loc
, N_("noncontiguous location for base fetch"));
1568 case loc_implicit_pointer
:
1569 FAIL (loc
, N_("pointer has been optimized out"));
1572 case loc_unavailable
:
1573 FAIL (loc
, N_("location not available"));
1581 obstack_printf (pool
, " %s = value; }", target
);
1585 /* Emit "... = RVALUE;". */
1587 emit_base_store (struct obstack
*pool
, Dwarf_Word byte_size
,
1588 const char *rvalue
, struct location
*loc
)
1593 if (byte_size
!= 0 && byte_size
!= (Dwarf_Word
) -1)
1594 obstack_printf (pool
, "store_deref (%" PRIu64
", addr, %s); ",
1597 obstack_printf (pool
, "store_deref (sizeof %s, addr, %s); ",
1602 if (loc
->reg
.offset
!= 0)
1603 FAIL (loc
, N_("cannot handle offset into register in store"));
1604 obstack_printf (pool
, "store_register (%u, %s);", loc
->reg
.regno
, rvalue
);
1607 case loc_noncontiguous
:
1608 FAIL (loc
, N_("noncontiguous location for base store"));
1611 case loc_implicit_pointer
:
1612 FAIL (loc
, N_("pointer has been optimized out"));
1616 FAIL (loc
, N_("location is computed value, cannot store"));
1620 FAIL (loc
, N_("location is constant value, cannot store"));
1623 case loc_unavailable
:
1624 FAIL (loc
, N_("location is not available, cannot store"));
1636 /* Slice up an object into pieces no larger than MAX_PIECE_BYTES,
1637 yielding a loc_noncontiguous location unless LOC is small enough. */
1638 static struct location
*
1639 discontiguify (struct location_context
*ctx
, int indent
, struct location
*loc
,
1640 Dwarf_Word total_bytes
, Dwarf_Word max_piece_bytes
)
1642 inline bool pieces_small_enough (void)
1644 if (loc
->type
!= loc_noncontiguous
)
1645 return total_bytes
<= max_piece_bytes
;
1647 for (p
= loc
->pieces
; p
!= NULL
; p
= p
->next
)
1648 if (p
->byte_size
> max_piece_bytes
)
1653 /* Constants are always copied byte-wise, but we may need to
1654 * truncate to the total_bytes requested here. */
1655 if (loc
->type
== loc_constant
)
1657 if (loc
->byte_size
> total_bytes
)
1658 loc
->byte_size
= total_bytes
;
1662 if (pieces_small_enough ())
1665 struct location
*noncontig
= alloc_location (ctx
);
1666 noncontig
->next
= NULL
;
1667 noncontig
->type
= loc_noncontiguous
;
1668 noncontig
->byte_size
= total_bytes
;
1669 noncontig
->pieces
= NULL
;
1670 struct location
**tailpiece
= &noncontig
->pieces
;
1671 inline void add (struct location
*piece
)
1674 tailpiece
= &piece
->next
;
1681 /* Synthesize a piece that sets "container_addr" to the computed
1682 address of the whole object. Each piece will refer to this. */
1683 obstack_printf (ctx
->pool
, "%*scontainer_addr = addr;\n",
1685 loc
->next
= new_synthetic_loc (loc
, false);
1686 loc
->next
->byte_size
= loc
->byte_size
;
1687 loc
->next
->type
= loc_fragment
;
1688 loc
->next
->address
.declare
= "container_addr";
1691 /* Synthesize pieces that just compute "container_addr + N". */
1692 Dwarf_Word offset
= 0;
1693 while (total_bytes
- offset
> 0)
1695 Dwarf_Word size
= total_bytes
- offset
;
1696 if (size
> max_piece_bytes
)
1697 size
= max_piece_bytes
;
1699 obstack_printf (ctx
->pool
,
1700 "%*saddr = container_addr + " UFORMAT
";\n",
1701 indent
* 2, "", offset
);
1702 struct location
*piece
= new_synthetic_loc (loc
, false);
1703 piece
->byte_size
= size
;
1714 FAIL (loc
, N_("stack value too big for fetch ???"));
1718 FAIL (loc
, N_("single register too big for fetch/store ???"));
1721 case loc_implicit_pointer
:
1722 FAIL (loc
, N_("implicit pointer too big for fetch/store ???"));
1725 case loc_noncontiguous
:
1726 /* Could be handled if it ever happened. */
1727 FAIL (loc
, N_("cannot support noncontiguous location"));
1735 loc
->next
= noncontig
;
1739 /* Make a fragment that declares a union such as:
1745 } pieces __attribute__ ((packed));
1750 declare_noncontig_union (struct obstack
*pool
, int indent
,
1751 struct location
**input
, struct location
*loc
,
1755 FAIL (loc
, N_("declaring noncontig union for depth > 9, too many pieces"));
1757 if (loc
->byte_size
> 8)
1758 FAIL (loc
, N_("cannot create noncontig union type larger than 64 bits"));
1760 obstack_printf (pool
, "%*sunion {\n", indent
++ * 2, "");
1762 obstack_printf (pool
, "%*schar bytes[%" PRIu64
"];\n",
1763 indent
* 2, "", loc
->byte_size
);
1765 if (loc
->type
== loc_noncontiguous
)
1767 Dwarf_Word offset
= 0;
1769 obstack_printf (pool
, "%*sstruct {\n", indent
++ * 2, "");
1771 for (p
= loc
->pieces
; p
!= NULL
; p
= p
->next
)
1773 obstack_printf (pool
, "%*suint%" PRIu64
"_t p%" PRIu64
";\n",
1774 indent
* 2, "", p
->byte_size
* 8, offset
);
1775 offset
+= p
->byte_size
;
1778 obstack_printf (pool
, "%*s} pieces __attribute__ ((packed));\n",
1782 obstack_printf (pool
, "%*suint%" PRIu64
"_t whole;\n",
1783 indent
* 2, "", loc
->byte_size
* 8);
1785 obstack_printf (pool
, "%*s} u_pieces%d;\n", --indent
* 2, "", depth
);
1787 loc
= new_synthetic_loc (*input
, false);
1788 loc
->type
= loc_decl
;
1789 (*input
)->next
= loc
;
1793 /* Determine the byte size of a base type. */
1795 base_byte_size (Dwarf_Die
*typedie
, struct location
*origin
)
1797 assert (dwarf_tag (typedie
) == DW_TAG_base_type
||
1798 dwarf_tag (typedie
) == DW_TAG_enumeration_type
);
1800 Dwarf_Attribute attr_mem
;
1802 if (dwarf_attr_integrate (typedie
, DW_AT_byte_size
, &attr_mem
) != NULL
1803 && dwarf_formudata (&attr_mem
, &size
) == 0)
1807 N_("cannot get byte_size attribute for type %s: %s"),
1808 dwarf_diename (typedie
) ?: "<anonymous>",
1814 base_encoding (Dwarf_Die
*typedie
, struct location
*origin
)
1816 if (! (dwarf_tag (typedie
) == DW_TAG_base_type
||
1817 dwarf_tag (typedie
) == DW_TAG_enumeration_type
))
1820 Dwarf_Attribute attr_mem
;
1821 Dwarf_Word encoding
;
1822 if (dwarf_attr_integrate (typedie
, DW_AT_encoding
, &attr_mem
) != NULL
1823 && dwarf_formudata (&attr_mem
, &encoding
) == 0)
1829 N_("cannot get encoding attribute for type %s: %s"),
1830 dwarf_diename (typedie) ?: "<anonymous>",
1838 /* Fetch the bitfield parameters. */
1840 get_bitfield (struct location
*loc
,
1841 Dwarf_Die
*die
, Dwarf_Word
*bit_offset
, Dwarf_Word
*bit_size
)
1843 Dwarf_Attribute attr_mem
;
1844 if (dwarf_attr_integrate (die
, DW_AT_bit_offset
, &attr_mem
) == NULL
1845 || dwarf_formudata (&attr_mem
, bit_offset
) != 0
1846 || dwarf_attr_integrate (die
, DW_AT_bit_size
, &attr_mem
) == NULL
1847 || dwarf_formudata (&attr_mem
, bit_size
) != 0)
1848 FAIL (loc
, N_("cannot get bit field parameters: %s"), dwarf_errmsg (-1));
1851 /* Translate a fragment to fetch the base-type value of BYTE_SIZE bytes
1852 at the *INPUT location and store it in lvalue TARGET. */
1854 translate_base_fetch (struct obstack
*pool
, int indent
,
1855 Dwarf_Word byte_size
, bool signed_p
,
1856 struct location
**input
, const char *target
,
1861 if ((*input
)->type
== loc_noncontiguous
)
1863 struct location
*p
= (*input
)->pieces
;
1865 declare_noncontig_union (pool
, indent
, input
, *input
, depth
);
1867 Dwarf_Word offset
= 0;
1868 char piece
[sizeof "u_pieces?.pieces.p" + 20] = "u_pieces?.pieces.p";
1869 piece
[8] = (char) ('0' + depth
);
1870 int pdepth
= depth
+ 1;
1873 struct location
*newp
= obstack_alloc (pool
, sizeof *newp
);
1876 (*input
)->next
= newp
;
1879 snprintf (&piece
[sizeof "u_pieces?.pieces.p" - 1], 20,
1880 "%" PRIu64
, offset
);
1881 translate_base_fetch (pool
, indent
, p
->byte_size
, signed_p
/* ? */,
1882 input
, piece
, pdepth
);
1883 (*input
)->type
= loc_fragment
;
1885 offset
+= p
->byte_size
;
1890 obstack_printf (pool
, "%*s%s = u_pieces%d.whole;\n", indent
* 2,
1893 else if ((*input
)->type
== loc_constant
)
1895 const unsigned char *constant_block
= (*input
)->constant_block
;
1896 const size_t byte_size
= (*input
)->byte_size
;
1899 declare_noncontig_union (pool
, indent
, input
, *input
, depth
);
1901 for (i
= 0; i
< byte_size
; ++i
)
1902 obstack_printf (pool
, "%*su_pieces%d.bytes[%zu] = %#x;\n", indent
* 2,
1903 "", depth
, i
, (unsigned)constant_block
[i
]);
1905 obstack_printf (pool
, "%*s%s = u_pieces%d.whole;\n", indent
* 2,
1911 case 0: /* Special case, means address size. */
1916 obstack_printf (pool
, "%*s", indent
* 2, "");
1917 deref
= emit_base_fetch (pool
, byte_size
, signed_p
, target
, *input
);
1918 obstack_printf (pool
, "\n");
1922 /* Could handle this generating call to memcpy equivalent. */
1923 FAIL (*input
, N_("fetch is larger than base integer types"));
1927 struct location
*loc
= new_synthetic_loc (*input
, deref
);
1928 loc
->byte_size
= byte_size
;
1929 loc
->type
= loc_final
;
1930 (*input
)->next
= loc
;
1934 /* Determine the maximum size of a base type, from some DIE in the CU. */
1936 max_fetch_size (struct location
*loc
, Dwarf_Die
*die
)
1939 uint8_t address_size
;
1940 Dwarf_Die
*cu
= dwarf_diecu (die
, &cu_mem
, &address_size
, NULL
);
1942 //TRANSLATORS: CU stands for 'compilation unit'
1943 FAIL (loc
, N_("cannot determine CU address size from %s: %s"),
1944 dwarf_diename (die
), dwarf_errmsg (-1));
1946 return address_size
;
1949 /* Translate a fragment to fetch the value of variable or member DIE
1950 at the *INPUT location and store it in lvalue TARGET. */
1952 c_translate_fetch (struct obstack
*pool
, int indent
,
1953 Dwarf_Addr dwbias
__attribute__ ((unused
)),
1954 Dwarf_Die
*die
, Dwarf_Die
*typedie
,
1955 struct location
**input
, const char *target
)
1957 struct location_context
*const ctx
= (*input
)->context
;
1958 assert (ctx
->pool
== pool
);
1962 Dwarf_Attribute size_attr
;
1963 Dwarf_Word byte_size
;
1964 if (dwarf_attr_integrate (die
, DW_AT_byte_size
, &size_attr
) == NULL
1965 || dwarf_formudata (&size_attr
, &byte_size
) != 0)
1966 byte_size
= base_byte_size (typedie
, *input
);
1968 Dwarf_Attribute encoding_attr
;
1969 Dwarf_Word encoding
;
1970 if (dwarf_attr_integrate (die
, DW_AT_encoding
, &encoding_attr
) == NULL
1971 || dwarf_formudata (&encoding_attr
, &encoding
) != 0)
1972 encoding
= base_encoding (typedie
, *input
);
1973 bool signed_p
= encoding
== DW_ATE_signed
|| encoding
== DW_ATE_signed_char
;
1975 *input
= discontiguify (ctx
, indent
, *input
, byte_size
,
1976 max_fetch_size (*input
, die
));
1978 if (dwarf_hasattr_integrate (die
, DW_AT_bit_offset
))
1980 /* This is a bit field. Fetch the containing base type into a
1981 temporary variable. */
1983 translate_base_fetch (pool
, indent
, byte_size
, signed_p
, input
, "tmp", 0);
1984 (*input
)->type
= loc_fragment
;
1985 (*input
)->address
.declare
= "tmp";
1987 Dwarf_Word bit_offset
= 0;
1988 Dwarf_Word bit_size
= 0;
1989 get_bitfield (*input
, die
, &bit_offset
, &bit_size
);
1991 obstack_printf (pool
, "%*s"
1992 "fetch_bitfield (%s, tmp, %" PRIu64
", %" PRIu64
");\n",
1993 indent
*2, "", target
, bit_offset
, bit_size
);
1995 struct location
*loc
= new_synthetic_loc (*input
, false);
1996 loc
->type
= loc_final
;
1997 (*input
)->next
= loc
;
2001 translate_base_fetch (pool
, indent
, byte_size
, signed_p
, input
, target
, 0);
2004 /* Translate a fragment to store RVALUE into the base-type value of
2005 BYTE_SIZE bytes at the *INPUT location. */
2007 translate_base_store (struct obstack
*pool
, int indent
, Dwarf_Word byte_size
,
2008 struct location
**input
, struct location
*store_loc
,
2009 const char *rvalue
, int depth
)
2013 if (store_loc
->type
== loc_noncontiguous
)
2015 declare_noncontig_union (pool
, indent
, input
, store_loc
, depth
);
2017 obstack_printf (pool
, "%*su_pieces%d.whole = %s;\n", indent
* 2,
2019 struct location
*loc
= new_synthetic_loc (*input
, deref
);
2020 loc
->type
= loc_fragment
;
2021 (*input
)->next
= loc
;
2024 Dwarf_Word offset
= 0;
2025 char piece
[sizeof "u_pieces?.pieces.p" + 20] = "u_pieces?.pieces.p";
2026 piece
[8] = (char) ('0' + depth
);
2028 int pdepth
= depth
+ 1;
2029 for (p
= store_loc
->pieces
; p
!= NULL
; p
= p
->next
)
2031 struct location
*newp
= obstack_alloc (pool
, sizeof *newp
);
2034 (*input
)->next
= newp
;
2037 snprintf (&piece
[sizeof "u_pieces?.pieces.p" - 1], 20, "%" PRIu64
,
2039 translate_base_store (pool
, indent
,
2040 p
->byte_size
, input
, *input
, piece
, pdepth
++);
2041 (*input
)->type
= loc_fragment
;
2043 offset
+= p
->byte_size
;
2046 (*input
)->type
= loc_final
;
2056 obstack_printf (pool
, "%*s", indent
* 2, "");
2057 deref
= emit_base_store (pool
, byte_size
, rvalue
, store_loc
);
2058 obstack_printf (pool
, "\n");
2062 /* Could handle this generating call to memcpy equivalent. */
2063 FAIL (*input
, N_("store is larger than base integer types"));
2067 struct location
*loc
= new_synthetic_loc (*input
, deref
);
2068 loc
->type
= loc_final
;
2069 (*input
)->next
= loc
;
2074 /* Translate a fragment to fetch the value of variable or member DIE
2075 at the *INPUT location and store it in rvalue RVALUE. */
2078 c_translate_store (struct obstack
*pool
, int indent
,
2079 Dwarf_Addr dwbias
__attribute__ ((unused
)),
2080 Dwarf_Die
*die
, Dwarf_Die
*typedie
,
2081 struct location
**input
, const char *rvalue
)
2083 struct location_context
*const ctx
= (*input
)->context
;
2084 assert (ctx
->pool
== pool
);
2088 Dwarf_Attribute size_attr
;
2089 Dwarf_Word byte_size
;
2090 if (dwarf_attr_integrate (die
, DW_AT_byte_size
, &size_attr
) == NULL
2091 || dwarf_formudata (&size_attr
, &byte_size
) != 0)
2092 byte_size
= base_byte_size (typedie
, *input
);
2094 Dwarf_Attribute encoding_attr
;
2095 Dwarf_Word encoding
;
2096 if (dwarf_attr_integrate (die
, DW_AT_encoding
, &encoding_attr
) == NULL
2097 || dwarf_formudata (&encoding_attr
, &encoding
) != 0)
2098 encoding
= base_encoding (typedie
, *input
);
2099 bool signed_p
= (encoding
== DW_ATE_signed
2100 || encoding
== DW_ATE_signed_char
);
2102 *input
= discontiguify (ctx
, indent
, *input
, byte_size
,
2103 max_fetch_size (*input
, die
));
2105 struct location
*store_loc
= *input
;
2107 if (dwarf_hasattr_integrate (die
, DW_AT_bit_offset
))
2109 /* This is a bit field. Fetch the containing base type into a
2110 temporary variable. */
2112 translate_base_fetch (pool
, indent
, byte_size
, signed_p
, input
, "tmp", 0);
2113 (*input
)->type
= loc_fragment
;
2114 (*input
)->address
.declare
= "tmp";
2116 Dwarf_Word bit_offset
= 0;
2117 Dwarf_Word bit_size
= 0;
2118 get_bitfield (*input
, die
, &bit_offset
, &bit_size
);
2120 obstack_printf (pool
, "%*s"
2121 "store_bitfield (tmp, %s, %" PRIu64
", %" PRIu64
");\n",
2122 indent
* 2, "", rvalue
, bit_offset
, bit_size
);
2124 struct location
*loc
= new_synthetic_loc (*input
, false);
2125 loc
->type
= loc_fragment
;
2126 (*input
)->next
= loc
;
2129 /* We have mixed RVALUE into the bits in "tmp".
2130 Now we'll store "tmp" back whence we fetched it. */
2134 translate_base_store (pool
, indent
, byte_size
, input
, store_loc
, rvalue
, 0);
2137 /* Translate a fragment to dereference the given pointer type,
2138 where *INPUT is the location of the pointer with that type.
2140 We chain on a loc_address program that yields this pointer value
2141 (i.e. the location of what it points to). */
2144 c_translate_pointer (struct obstack
*pool
, int indent
,
2145 Dwarf_Addr dwbias
__attribute__ ((unused
)),
2146 Dwarf_Die
*typedie
, struct location
**input
)
2148 assert (dwarf_tag (typedie
) == DW_TAG_pointer_type
||
2149 dwarf_tag (typedie
) == DW_TAG_reference_type
||
2150 dwarf_tag (typedie
) == DW_TAG_rvalue_reference_type
);
2152 Dwarf_Attribute attr_mem
;
2153 Dwarf_Word byte_size
;
2154 if (dwarf_attr_integrate (typedie
, DW_AT_byte_size
, &attr_mem
) == NULL
)
2155 byte_size
= max_fetch_size (*input
, typedie
); /* Use CU address size. */
2156 else if (dwarf_formudata (&attr_mem
, &byte_size
) != 0)
2158 N_("cannot get byte_size attribute for type %s: %s"),
2159 dwarf_diename (typedie
) ?: "<anonymous>",
2162 if ((*input
)->type
== loc_implicit_pointer
)
2164 struct location
*const target
= (*input
)->pointer
.target
;
2165 const Dwarf_Sword offset
= (*input
)->pointer
.offset
;
2166 (*input
)->next
= target
;
2168 translate_offset (indent
, NULL
, 0, 0, NULL
, &(*input
)->next
, offset
);
2169 *input
= (*input
)->next
;
2173 bool signed_p
= false; /* XXX: Does not matter? */
2175 translate_base_fetch (pool
, indent
+ 1, byte_size
, signed_p
, input
,
2177 (*input
)->type
= loc_address
;
2183 c_translate_addressof (struct obstack
*pool
, int indent
,
2184 Dwarf_Addr dwbias
__attribute__ ((unused
)),
2186 Dwarf_Die
*typedie
__attribute__ ((unused
)),
2187 struct location
**input
, const char *target
)
2191 if (dwarf_hasattr_integrate (die
, DW_AT_bit_offset
)
2192 || dwarf_hasattr_integrate (die
, DW_AT_data_bit_offset
))
2193 FAIL (*input
, N_("cannot take the address of a bit field"));
2195 switch ((*input
)->type
)
2198 obstack_printf (pool
, "%*s%s = addr;\n", indent
* 2, "", target
);
2199 (*input
)->next
= new_synthetic_loc (*input
, false);
2200 (*input
)->next
->type
= loc_final
;
2204 FAIL (*input
, N_("cannot take address of object in register"));
2206 case loc_noncontiguous
:
2207 FAIL (*input
, N_("cannot take address of noncontiguous object"));
2210 FAIL (*input
, N_("cannot take address of computed value"));
2213 FAIL (*input
, N_("cannot take address of constant value"));
2215 case loc_unavailable
:
2216 FAIL (*input
, N_("cannot take address of unavailable value"));
2218 case loc_implicit_pointer
:
2219 FAIL (*input
, N_("cannot take address of implicit pointer"));
2229 /* Translate a fragment to write the given pointer value,
2230 where *INPUT is the location of the pointer with that type.
2234 c_translate_pointer_store (struct obstack
*pool
, int indent
,
2235 Dwarf_Addr dwbias
__attribute__ ((unused
)),
2236 Dwarf_Die
*typedie
, struct location
**input
,
2239 assert (dwarf_tag (typedie
) == DW_TAG_pointer_type
);
2241 Dwarf_Attribute attr_mem
;
2242 Dwarf_Word byte_size
;
2243 if (dwarf_attr_integrate (typedie
, DW_AT_byte_size
, &attr_mem
) == NULL
)
2244 byte_size
= max_fetch_size (*input
, typedie
); /* Use CU address size. */
2245 else if (dwarf_formudata (&attr_mem
, &byte_size
) != 0)
2247 N_("cannot get byte_size attribute for type %s: %s"),
2248 dwarf_diename (typedie
) ?: "<anonymous>",
2251 translate_base_store (pool
, indent
+ 1, byte_size
,
2252 input
, *input
, rvalue
, 0);
2254 // XXX: what about multiple-location lvalues?
2257 /* Determine the element stride of a pointer to a type. */
2259 pointer_stride (Dwarf_Die
*typedie
, struct location
*origin
)
2261 Dwarf_Attribute attr_mem
;
2262 Dwarf_Die die_mem
= *typedie
;
2263 int typetag
= dwarf_tag (&die_mem
);
2264 while (typetag
== DW_TAG_typedef
||
2265 typetag
== DW_TAG_const_type
||
2266 typetag
== DW_TAG_volatile_type
||
2267 typetag
== DW_TAG_restrict_type
)
2269 if (dwarf_attr_integrate (&die_mem
, DW_AT_type
, &attr_mem
) == NULL
2270 || dwarf_formref_die (&attr_mem
, &die_mem
) == NULL
)
2271 //TRANSLATORS: This refers to the basic type, (stripped of const/volatile/etc.)
2272 FAIL (origin
, N_("cannot get inner type of type %s: %s"),
2273 dwarf_diename (&die_mem
) ?: "<anonymous>",
2275 typetag
= dwarf_tag (&die_mem
);
2278 if (dwarf_attr_integrate (&die_mem
, DW_AT_byte_size
, &attr_mem
) != NULL
)
2281 if (dwarf_formudata (&attr_mem
, &stride
) == 0)
2284 N_("cannot get byte_size attribute for array element type %s: %s"),
2285 dwarf_diename (&die_mem
) ?: "<anonymous>",
2289 FAIL (origin
, N_("confused about array element size"));
2293 /* Determine the element stride of an array type. */
2295 array_stride (Dwarf_Die
*typedie
, struct location
*origin
)
2297 Dwarf_Attribute attr_mem
;
2298 if (dwarf_attr_integrate (typedie
, DW_AT_byte_stride
, &attr_mem
) != NULL
)
2301 if (dwarf_formudata (&attr_mem
, &stride
) == 0)
2303 FAIL (origin
, N_("cannot get byte_stride attribute array type %s: %s"),
2304 dwarf_diename (typedie
) ?: "<anonymous>",
2309 if (dwarf_attr_integrate (typedie
, DW_AT_type
, &attr_mem
) == NULL
2310 || dwarf_formref_die (&attr_mem
, &die_mem
) == NULL
)
2311 FAIL (origin
, N_("cannot get element type of array type %s: %s"),
2312 dwarf_diename (typedie
) ?: "<anonymous>",
2315 return pointer_stride (&die_mem
, origin
);
2319 translate_array (struct obstack
*pool
, int indent
,
2320 Dwarf_Die
*anydie
, Dwarf_Word stride
,
2321 struct location
**input
,
2322 const char *idx
, Dwarf_Word const_idx
)
2326 struct location
*loc
= *input
;
2327 while (loc
->type
== loc_noncontiguous
)
2330 FAIL (*input
, N_("cannot dynamically index noncontiguous array"));
2333 Dwarf_Word offset
= const_idx
* stride
;
2334 struct location
*piece
= loc
->pieces
;
2335 while (piece
!= NULL
&& offset
>= piece
->byte_size
)
2337 offset
-= piece
->byte_size
;
2338 piece
= piece
->next
;
2341 //TRANSLATORS: The index is constant
2342 FAIL (*input
, N_("constant index is outside noncontiguous array"));
2343 if (offset
% stride
!= 0 || piece
->byte_size
< stride
)
2344 FAIL (*input
, N_("noncontiguous array splits elements"));
2345 const_idx
= offset
/ stride
;
2355 obstack_printf (pool
, "%*saddr += %s * " UFORMAT
";\n",
2356 indent
* 2, "", idx
, stride
);
2358 obstack_printf (pool
, "%*saddr += " UFORMAT
" * " UFORMAT
";\n",
2359 indent
* 2, "", const_idx
, stride
);
2360 loc
= new_synthetic_loc (loc
, false);
2365 FAIL (*input
, N_("cannot index array stored in a register"));
2366 else if (const_idx
> max_fetch_size (loc
, anydie
) / stride
)
2367 FAIL (*input
, N_("constant index is outside array held in register"));
2370 loc
->reg
.offset
+= const_idx
* stride
;
2377 FAIL (*input
, N_("cannot index into constant value"));
2378 else if (const_idx
> loc
->byte_size
/ stride
)
2379 //TRANSLATORS: The index is constant
2380 FAIL (*input
, N_("constant index is outside constant array value"));
2383 loc
->byte_size
= stride
;
2384 loc
->constant_block
+= const_idx
* stride
;
2389 case loc_implicit_pointer
:
2391 FAIL (*input
, N_("cannot index into implicit pointer"));
2393 loc
->pointer
.offset
+= const_idx
* stride
;
2397 if (idx
!= NULL
|| const_idx
!= 0)
2398 FAIL (*input
, N_("cannot index into computed value"));
2401 case loc_unavailable
:
2402 if (idx
!= NULL
|| const_idx
!= 0)
2403 FAIL (*input
, N_("cannot index into unavailable value"));
2411 (*input
)->next
= loc
;
2412 *input
= (*input
)->next
;
2416 c_translate_array (struct obstack
*pool
, int indent
,
2417 Dwarf_Addr dwbias
__attribute__ ((unused
)),
2418 Dwarf_Die
*typedie
, struct location
**input
,
2419 const char *idx
, Dwarf_Word const_idx
)
2421 assert (dwarf_tag (typedie
) == DW_TAG_array_type
||
2422 dwarf_tag (typedie
) == DW_TAG_pointer_type
);
2424 return translate_array (pool
, indent
, typedie
,
2425 array_stride (typedie
, *input
),
2426 input
, idx
, const_idx
);
2430 c_translate_array_pointer (struct obstack
*pool
, int indent
,
2431 Dwarf_Die
*typedie
, struct location
**input
,
2432 const char *idx
, Dwarf_Word const_idx
)
2434 return translate_array (pool
, indent
, typedie
,
2435 pointer_stride (typedie
, *input
),
2436 input
, idx
, const_idx
);
2439 /* Emitting C code for finalized fragments. */
2441 #define emit(fmt, ...) fprintf (out, fmt, ## __VA_ARGS__)
2443 /* Open a block with a comment giving the original DWARF expression. */
2445 emit_header (FILE *out
, struct location
*loc
, int hindent
)
2447 if (loc
->ops
== NULL
)
2448 emit ("%*s{ // synthesized\n", hindent
* 2, "");
2451 emit ("%*s{ // DWARF expression:", hindent
* 2, "");
2453 for (i
= 0; i
< loc
->nops
; ++i
)
2455 emit (" %#x", (unsigned)loc
->ops
[i
].atom
);
2456 if (loc
->ops
[i
].number2
== 0)
2458 if (loc
->ops
[i
].number
!= 0)
2459 emit ("(%" PRId64
")", (int64_t)loc
->ops
[i
].number
);
2462 emit ("(%" PRId64
",%" PRId64
")",
2463 (int64_t)loc
->ops
[i
].number
, (int64_t)loc
->ops
[i
].number2
);
2469 /* Emit a code fragment to assign the target variable to a register value. */
2471 emit_loc_register (FILE *out
, struct location
*loc
, int indent
,
2474 assert (loc
->type
== loc_register
);
2476 if (loc
->reg
.offset
!= 0)
2477 FAIL (loc
, N_("cannot handle offset into register in fetch"));
2479 emit ("%*s%s = fetch_register (%u);\n",
2480 indent
* 2, "", target
, loc
->reg
.regno
);
2483 /* Emit a code fragment to assign the target variable to an address. */
2485 emit_loc_address (FILE *out
, struct location
*loc
, int indent
,
2488 assert (loc
->type
== loc_address
|| loc
->type
== loc_value
);
2490 if (loc
->address
.stack_depth
== 0)
2491 /* Synthetic program. */
2492 emit ("%s", loc
->address
.program
);
2495 emit ("%*s{\n", indent
* 2, "");
2496 emit ("%*s%s " STACKFMT
, (indent
+ 1) * 2, "",
2497 stack_slot_type (loc
, false), (unsigned)0);
2499 for (i
= 1; i
< loc
->address
.stack_depth
; ++i
)
2500 emit (", " STACKFMT
, i
);
2503 emit ("%s%*s%s = " STACKFMT
";\n", loc
->address
.program
,
2504 (indent
+ 1) * 2, "", target
, (unsigned)0);
2505 emit ("%*s}\n", indent
* 2, "");
2509 /* Emit a code fragment to declare the target variable and
2510 assign it to an address-sized value. */
2512 emit_loc_value (FILE *out
, struct location
*loc
, int indent
,
2513 const char *target
, bool declare
,
2514 bool *used_deref
, unsigned int *max_stack
)
2517 emit ("%*s%s %s;\n", indent
* 2, "", stack_slot_type (loc
, false), target
);
2519 emit_header (out
, loc
, indent
++);
2528 emit_loc_register (out
, loc
, indent
, target
);
2533 emit_loc_address (out
, loc
, indent
, target
);
2534 *used_deref
= *used_deref
|| loc
->address
.used_deref
;
2535 if (loc
->address
.stack_depth
> *max_stack
)
2536 *max_stack
= loc
->address
.stack_depth
;
2540 emit ("%*s}\n", --indent
* 2, "");
2544 c_emit_location (FILE *out
, struct location
*loc
, int indent
,
2545 unsigned int *max_stack
)
2547 emit ("%*s{\n", indent
* 2, "");
2549 bool declared_addr
= false;
2551 for (l
= loc
; l
!= NULL
; l
= l
->next
)
2555 emit ("%s", l
->address
.program
);
2562 declared_addr
= true;
2563 l
->address
.declare
= "addr";
2566 if (l
->address
.declare
!= NULL
)
2568 if (l
->byte_size
== 0 || l
->byte_size
== (Dwarf_Word
) -1)
2569 emit ("%*s%s %s;\n", (indent
+ 1) * 2, "",
2570 stack_slot_type (l
, false), l
->address
.declare
);
2572 emit ("%*suint%" PRIu64
"_t %s;\n", (indent
+ 1) * 2, "",
2573 l
->byte_size
* 8, l
->address
.declare
);
2583 for (l
= loc
; l
!= NULL
; l
= l
->next
)
2584 if (l
->frame_base
!= NULL
)
2586 emit_loc_value (out
, l
->frame_base
, indent
, "frame_base", true,
2591 for (; loc
->next
!= NULL
; loc
= loc
->next
)
2596 /* Emit the program fragment to calculate the address. */
2597 emit_loc_value (out
, loc
, indent
+ 1, "addr", false, &deref
, max_stack
);
2601 emit ("%s", loc
->address
.program
);
2602 deref
= deref
|| loc
->address
.used_deref
;
2607 case loc_noncontiguous
:
2609 case loc_implicit_pointer
:
2610 /* These don't produce any code directly.
2611 The next address/final record incorporates the value. */
2614 case loc_final
: /* Should be last in chain! */
2620 if (loc
->type
!= loc_final
) /* Unfinished chain. */
2623 emit ("%s%*s}\n", loc
->address
.program
, indent
* 2, "");
2625 if (loc
->address
.stack_depth
> *max_stack
)
2626 *max_stack
= loc
->address
.stack_depth
;
2628 return deref
|| loc
->address
.used_deref
;
2633 /* vim: set sw=2 ts=8 cino=>4,n-2,{2,^-2,t0,(0,u0,w1,M1 : */