]>
Commit | Line | Data |
---|---|---|
d59ea8d4 | 1 | // dwarf location-list-to-c translator |
6c476b58 | 2 | // Copyright (C) 2005-2015 Red Hat Inc. |
d59ea8d4 FCE |
3 | // |
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 | |
7 | // later version. | |
8 | ||
9 | #include "config.h" | |
77de5e9e GH |
10 | #include <inttypes.h> |
11 | #include <stdbool.h> | |
12 | #include <obstack.h> | |
13 | #include <stdio.h> | |
14 | #include <stdlib.h> | |
15 | #include <error.h> | |
16 | #include <dwarf.h> | |
44049283 | 17 | #include <elfutils/libdw.h> |
e050d62f MW |
18 | #include <elfutils/version.h> |
19 | ||
77de5e9e | 20 | #include <assert.h> |
377b204c | 21 | #include "loc2c.h" |
77de5e9e | 22 | |
55d8c5d4 FCE |
23 | #include "config.h" |
24 | ||
1be70eb3 MW |
25 | #if ! _ELFUTILS_PREREQ(0, 153) |
26 | #define DW_OP_GNU_entry_value 0xf3 | |
27 | #endif | |
55d8c5d4 | 28 | |
e36387d7 | 29 | #define N_(x) x |
77de5e9e | 30 | |
a5a674fa FCE |
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. */ | |
37 | ||
77de5e9e GH |
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" | |
44 | ||
1220de76 | 45 | struct location_context |
77de5e9e | 46 | { |
1220de76 | 47 | struct obstack *pool; |
77de5e9e | 48 | |
e36387d7 RM |
49 | void (*fail) (void *arg, const char *fmt, ...) |
50 | __attribute__ ((noreturn, format (printf, 2, 3))); | |
51 | void *fail_arg; | |
4b1ad75e | 52 | void (*emit_address) (void *fail_arg, struct obstack *, Dwarf_Addr); |
e36387d7 | 53 | |
1220de76 RM |
54 | Dwarf_Attribute *attr; |
55 | Dwarf_Addr dwbias; | |
56 | Dwarf_Addr pc; | |
57 | Dwarf_Attribute *fb_attr; | |
58 | const Dwarf_Op *cfa_ops; | |
59 | }; | |
60 | ||
61 | struct location | |
62 | { | |
63 | struct location *next; | |
64 | ||
65 | struct location_context *context; | |
66 | ||
20e4a32c | 67 | const Dwarf_Op *ops; |
77de5e9e GH |
68 | size_t nops; |
69 | ||
70 | Dwarf_Word byte_size; | |
71 | ||
ce48dbc7 RM |
72 | enum |
73 | { | |
1220de76 RM |
74 | loc_address, loc_register, loc_noncontiguous, loc_unavailable, |
75 | loc_value, loc_constant, loc_implicit_pointer, | |
76 | loc_decl, loc_fragment, loc_final | |
ce48dbc7 | 77 | } type; |
9b9d9778 | 78 | struct location *frame_base; |
77de5e9e GH |
79 | union |
80 | { | |
45d7e325 | 81 | struct /* loc_address, loc_value, loc_fragment, loc_final */ |
77de5e9e | 82 | { |
ce48dbc7 | 83 | const char *declare; /* Temporary that needs declared. */ |
77de5e9e GH |
84 | char *program; /* C fragment, leaves address in s0. */ |
85 | unsigned int stack_depth; /* Temporaries "s0..<N>" used by it. */ | |
77de5e9e GH |
86 | bool used_deref; /* Program uses "deref" macro. */ |
87 | } address; | |
5ddc2ead RM |
88 | struct /* loc_register */ |
89 | { | |
90 | unsigned int regno; | |
91 | Dwarf_Word offset; | |
92 | } reg; | |
77de5e9e | 93 | struct location *pieces; /* loc_noncontiguous */ |
707e2bec | 94 | const void *constant_block; /* loc_constant */ |
1220de76 RM |
95 | struct /* loc_implicit_pointer */ |
96 | { | |
97 | struct location *target; | |
98 | Dwarf_Sword offset; | |
99 | } pointer; | |
77de5e9e GH |
100 | }; |
101 | }; | |
102 | ||
b25c01a1 RM |
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. */ | |
106 | static const char * | |
107 | stack_slot_type (struct location *context __attribute__ ((unused)), bool sign) | |
108 | { | |
109 | return sign ? STACK_TYPE : UTYPE; | |
110 | } | |
111 | ||
e36387d7 | 112 | static struct location * |
1220de76 | 113 | alloc_location (struct location_context *ctx) |
e36387d7 | 114 | { |
1220de76 RM |
115 | struct location *loc = obstack_alloc (ctx->pool, sizeof *loc); |
116 | loc->context = ctx; | |
ce48dbc7 | 117 | loc->byte_size = 0; |
9b9d9778 | 118 | loc->frame_base = NULL; |
8a77568a RM |
119 | loc->ops = NULL; |
120 | loc->nops = 0; | |
e36387d7 RM |
121 | return loc; |
122 | } | |
123 | ||
124 | #define FAIL(loc, fmt, ...) \ | |
1220de76 | 125 | (*(loc)->context->fail) ((loc)->context->fail_arg, fmt, ## __VA_ARGS__) |
4b1ad75e RM |
126 | |
127 | static void | |
128 | default_emit_address (void *fail_arg __attribute__ ((unused)), | |
129 | struct obstack *pool, Dwarf_Addr address) | |
130 | { | |
131 | obstack_printf (pool, AFORMAT, address); | |
132 | } | |
1220de76 RM |
133 | |
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))), | |
138 | void *fail_arg, | |
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) | |
144 | { | |
145 | struct location_context *ctx = obstack_alloc (pool, sizeof *ctx); | |
146 | ctx->pool = pool; | |
147 | ctx->fail = fail; | |
148 | ctx->fail_arg = fail_arg; | |
149 | ctx->emit_address = emit_address ?: &default_emit_address; | |
150 | ctx->attr = attr; | |
151 | ctx->dwbias = dwbias; | |
152 | ctx->pc = pc_address; | |
153 | ctx->fb_attr = fb_attr; | |
154 | ctx->cfa_ops = cfa_ops; | |
155 | return ctx; | |
156 | } | |
157 | \f | |
158 | /* Translate a DW_AT_const_value attribute as if it were a location of | |
159 | constant-value flavor. */ | |
160 | ||
161 | static struct location * | |
162 | translate_constant (struct location_context *ctx, int indent, | |
163 | Dwarf_Attribute *attr) | |
164 | { | |
165 | indent += 2; | |
166 | ||
167 | struct location *loc = obstack_alloc (ctx->pool, sizeof *loc); | |
168 | loc->context = ctx; | |
169 | loc->next = NULL; | |
170 | loc->byte_size = 0; | |
171 | loc->frame_base = NULL; | |
172 | loc->address.stack_depth = 0; | |
173 | loc->address.declare = NULL; | |
174 | loc->address.used_deref = false; | |
175 | loc->ops = NULL; | |
176 | loc->nops = 0; | |
177 | ||
178 | switch (dwarf_whatform (attr)) | |
179 | { | |
180 | case DW_FORM_addr: | |
181 | { | |
182 | Dwarf_Addr addr; | |
183 | if (dwarf_formaddr (attr, &addr) != 0) | |
184 | { | |
ce0f6648 | 185 | //TRANSLATORS: failure to find an address that was a constant literal number |
1220de76 RM |
186 | FAIL (loc, N_("cannot get constant address: %s"), |
187 | dwarf_errmsg (-1)); | |
188 | return NULL; | |
189 | } | |
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); | |
195 | break; | |
196 | } | |
197 | ||
198 | case DW_FORM_block: | |
199 | case DW_FORM_block1: | |
200 | case DW_FORM_block2: | |
201 | case DW_FORM_block4: | |
202 | { | |
203 | Dwarf_Block block; | |
204 | if (dwarf_formblock (attr, &block) != 0) | |
205 | { | |
206 | FAIL (loc, N_("cannot get constant block: %s"), dwarf_errmsg (-1)); | |
207 | return NULL; | |
208 | } | |
209 | loc->type = loc_constant; | |
210 | loc->byte_size = block.length; | |
211 | loc->constant_block = block.data; | |
212 | break; | |
213 | } | |
214 | ||
215 | case DW_FORM_string: | |
216 | case DW_FORM_strp: | |
217 | { | |
218 | const char *string = dwarf_formstring (attr); | |
219 | if (string == NULL) | |
220 | { | |
221 | FAIL (loc, N_("cannot get string constant: %s"), dwarf_errmsg (-1)); | |
222 | return NULL; | |
223 | } | |
224 | loc->type = loc_constant; | |
225 | loc->byte_size = strlen (string) + 1; | |
226 | loc->constant_block = string; | |
227 | break; | |
228 | } | |
229 | ||
230 | default: | |
231 | { | |
232 | Dwarf_Sword value; | |
233 | if (dwarf_formsdata (attr, &value) != 0) | |
234 | { | |
235 | FAIL (loc, N_("cannot get constant value: %s"), dwarf_errmsg (-1)); | |
236 | return NULL; | |
237 | } | |
238 | loc->type = loc_value; | |
f6d8e426 | 239 | obstack_printf (ctx->pool, "%*saddr = (" UTYPE ")%#" PRIx64 "ULL;\n", |
582840f4 | 240 | indent * 2, "", (uint64_t) value); |
1220de76 RM |
241 | obstack_1grow (ctx->pool, '\0'); |
242 | loc->address.program = obstack_finish (ctx->pool); | |
243 | break; | |
244 | } | |
245 | } | |
246 | ||
247 | return loc; | |
248 | } | |
249 | ||
250 | struct location * | |
251 | c_translate_constant (struct obstack *pool, | |
252 | void (*fail) (void *arg, | |
253 | const char *fmt, ...) | |
254 | __attribute__ ((noreturn, | |
255 | format (printf, 2, 3))), | |
256 | void *fail_arg, | |
257 | void (*emit_address) (void *fail_arg, | |
258 | struct obstack *, | |
259 | Dwarf_Addr), | |
260 | int indent, Dwarf_Addr dwbias, Dwarf_Attribute *attr) | |
261 | { | |
262 | return translate_constant (new_context (pool, fail, fail_arg, emit_address, | |
263 | dwbias, 0, attr, NULL, NULL), | |
264 | indent, attr); | |
265 | } | |
266 | ||
653a2c7b | 267 | #if _ELFUTILS_PREREQ (0, 149) |
1220de76 RM |
268 | static struct location *location_from_attr (struct location_context *ctx, |
269 | int indent, Dwarf_Attribute *attr); | |
653a2c7b MW |
270 | #endif |
271 | ||
77de5e9e | 272 | \f |
77de5e9e GH |
273 | /* Synthesize a new loc_address using the program on the obstack. */ |
274 | static struct location * | |
1220de76 | 275 | new_synthetic_loc (struct location *origin, bool deref) |
77de5e9e | 276 | { |
1220de76 RM |
277 | obstack_1grow (origin->context->pool, '\0'); |
278 | char *program = obstack_finish (origin->context->pool); | |
77de5e9e | 279 | |
1220de76 | 280 | struct location *loc = alloc_location (origin->context); |
77de5e9e GH |
281 | loc->next = NULL; |
282 | loc->byte_size = 0; | |
283 | loc->type = loc_address; | |
284 | loc->address.program = program; | |
285 | loc->address.stack_depth = 0; | |
ce48dbc7 | 286 | loc->address.declare = NULL; |
77de5e9e GH |
287 | loc->address.used_deref = deref; |
288 | ||
289 | if (origin->type == loc_register) | |
290 | { | |
291 | loc->ops = origin->ops; | |
292 | loc->nops = origin->nops; | |
293 | } | |
294 | else | |
295 | { | |
296 | loc->ops = NULL; | |
297 | loc->nops = 0; | |
298 | } | |
299 | ||
300 | return loc; | |
301 | } | |
302 | ||
303 | ||
304 | /* Die in the middle of an expression. */ | |
305 | static struct location * | |
1220de76 RM |
306 | lose (struct location *loc, const Dwarf_Op *lexpr, size_t len, |
307 | const char *failure, size_t i) | |
77de5e9e | 308 | { |
1220de76 RM |
309 | if (lexpr == NULL || i >= len) |
310 | FAIL (loc, "%s", failure); | |
311 | else if (i < len) | |
312 | FAIL (loc, N_("%s in DWARF expression [%Zu] at %" PRIu64 | |
f245e619 | 313 | " (%#x: %" PRIu64 ", %" PRIu64 ")"), |
1220de76 | 314 | failure, i, lexpr[i].offset, |
f245e619 | 315 | (unsigned) lexpr[i].atom, lexpr[i].number, lexpr[i].number2); |
77de5e9e GH |
316 | return NULL; |
317 | } | |
318 | ||
1220de76 | 319 | |
77de5e9e GH |
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. | |
328 | ||
329 | Returns a failure message or null for success. */ | |
330 | ||
331 | static const char * | |
1220de76 RM |
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, | |
77de5e9e GH |
335 | struct location *loc) |
336 | { | |
337 | loc->ops = expr; | |
338 | loc->nops = len; | |
339 | ||
e36387d7 | 340 | #define DIE(msg) return (*loser = i, N_(msg)) |
77de5e9e | 341 | |
1220de76 | 342 | #define emit(fmt, ...) obstack_printf (ctx->pool, fmt, ## __VA_ARGS__) |
77de5e9e | 343 | |
56cfcfc6 RM |
344 | unsigned int stack_depth; |
345 | unsigned int max_stack = 0; | |
77de5e9e GH |
346 | inline void deepen (void) |
347 | { | |
348 | if (stack_depth == max_stack) | |
349 | ++max_stack; | |
350 | } | |
351 | ||
352 | #define POP(var) \ | |
353 | if (stack_depth > 0) \ | |
354 | --stack_depth; \ | |
355 | else if (tos_register != -1) \ | |
356 | fetch_tos_register (); \ | |
357 | else \ | |
358 | goto underflow; \ | |
359 | int var = stack_depth | |
360 | #define PUSH (deepen (), stack_depth++) | |
d59ea8d4 | 361 | #define STACK(idx) ({int z = (stack_depth - 1 - (idx)); assert (z >= 0); z;}) |
77de5e9e GH |
362 | |
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__) | |
366 | ||
56cfcfc6 | 367 | int tos_register; |
77de5e9e GH |
368 | inline void fetch_tos_register (void) |
369 | { | |
370 | deepen (); | |
371 | emit ("%*s" STACKFMT " = fetch_register (%d);\n", | |
372 | indent * 2, "", stack_depth, tos_register); | |
373 | tos_register = -1; | |
374 | } | |
375 | ||
56cfcfc6 RM |
376 | bool tos_value; |
377 | Dwarf_Block implicit_value; | |
378 | bool used_deref; | |
1220de76 | 379 | const Dwarf_Op *implicit_pointer; |
0c254456 | 380 | |
56cfcfc6 RM |
381 | /* Initialize our state for handling each new piece. */ |
382 | inline void reset () | |
383 | { | |
384 | stack_depth = 0; | |
385 | tos_register = -1; | |
386 | tos_value = false; | |
387 | implicit_value.data = NULL; | |
1220de76 | 388 | implicit_pointer = NULL; |
56cfcfc6 | 389 | used_deref = false; |
77de5e9e | 390 | |
56cfcfc6 RM |
391 | if (input != NULL) |
392 | switch (input->type) | |
393 | { | |
394 | case loc_address: | |
395 | push ("addr"); | |
396 | break; | |
0c254456 | 397 | |
56cfcfc6 RM |
398 | case loc_value: |
399 | push ("addr"); | |
400 | tos_value = true; | |
401 | break; | |
77de5e9e | 402 | |
56cfcfc6 RM |
403 | case loc_register: |
404 | tos_register = input->reg.regno; | |
405 | break; | |
77de5e9e | 406 | |
56cfcfc6 RM |
407 | default: |
408 | abort (); | |
409 | break; | |
410 | } | |
411 | } | |
77de5e9e | 412 | |
56cfcfc6 | 413 | size_t i; |
77de5e9e GH |
414 | inline const char *finish (struct location *piece) |
415 | { | |
35bbf143 RM |
416 | if (piece->nops == 0) |
417 | { | |
418 | assert (stack_depth == 0); | |
419 | assert (tos_register == -1); | |
1220de76 | 420 | assert (obstack_object_size (ctx->pool) == 0); |
35bbf143 RM |
421 | piece->type = loc_unavailable; |
422 | } | |
423 | else if (stack_depth >= 1) | |
77de5e9e | 424 | { |
f1604314 RM |
425 | /* The top of stack has our value. |
426 | Other stack slots left don't matter. */ | |
1220de76 RM |
427 | obstack_1grow (ctx->pool, '\0'); |
428 | char *program = obstack_finish (ctx->pool); | |
429 | if (implicit_pointer != NULL) | |
430 | { | |
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? */ | |
435 | abort (); | |
436 | #else | |
437 | Dwarf_Attribute target; | |
438 | if (dwarf_getlocation_implicit_pointer (ctx->attr, | |
439 | implicit_pointer, | |
440 | &target) != 0) | |
441 | DIE ("invalid implicit pointer"); | |
442 | switch (dwarf_whatattr (&target)) | |
443 | { | |
444 | case DW_AT_const_value: | |
445 | piece->pointer.target = translate_constant (ctx, indent, | |
446 | &target); | |
447 | break; | |
448 | case DW_AT_location: | |
449 | piece->pointer.target = location_from_attr (ctx, indent, | |
450 | &target); | |
451 | break; | |
452 | default: | |
453 | DIE ("unexpected implicit pointer attribute!"); | |
454 | break; | |
455 | } | |
456 | #endif | |
457 | } | |
458 | else if (implicit_value.data == NULL) | |
707e2bec RM |
459 | { |
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; | |
465 | } | |
466 | else | |
467 | { | |
468 | piece->type = loc_constant; | |
469 | piece->byte_size = implicit_value.length; | |
470 | piece->constant_block = implicit_value.data; | |
471 | } | |
77de5e9e GH |
472 | } |
473 | else if (tos_register == -1) | |
474 | DIE ("stack underflow"); | |
1220de76 | 475 | else if (obstack_object_size (ctx->pool) != 0) |
77de5e9e GH |
476 | DIE ("register value must stand alone in location expression"); |
477 | else | |
478 | { | |
479 | piece->type = loc_register; | |
5ddc2ead RM |
480 | piece->reg.regno = tos_register; |
481 | piece->reg.offset = 0; | |
77de5e9e GH |
482 | } |
483 | return NULL; | |
484 | } | |
485 | ||
56cfcfc6 | 486 | reset (); |
77de5e9e GH |
487 | struct location *pieces = NULL, **tailpiece = &pieces; |
488 | size_t piece_expr_start = 0; | |
ce48dbc7 | 489 | Dwarf_Word piece_total_bytes = 0; |
77de5e9e GH |
490 | for (i = 0; i < len; ++i) |
491 | { | |
492 | unsigned int reg; | |
493 | uint_fast8_t sp; | |
494 | Dwarf_Word value; | |
495 | ||
707e2bec RM |
496 | inline bool more_ops (void) |
497 | { | |
498 | return (expr[i].atom != DW_OP_nop | |
499 | && expr[i].atom != DW_OP_piece | |
500 | && expr[i].atom != DW_OP_bit_piece); | |
501 | } | |
502 | ||
503 | if (tos_value && more_ops ()) | |
45d7e325 RM |
504 | DIE ("operations follow DW_OP_stack_value"); |
505 | ||
707e2bec RM |
506 | if (implicit_value.data != NULL && more_ops ()) |
507 | DIE ("operations follow DW_OP_implicit_value"); | |
508 | ||
1220de76 RM |
509 | if (implicit_pointer != NULL && more_ops ()) |
510 | DIE ("operations follow DW_OP_GNU_implicit_pointer"); | |
511 | ||
77de5e9e GH |
512 | switch (expr[i].atom) |
513 | { | |
514 | /* Basic stack operations. */ | |
515 | case DW_OP_nop: | |
516 | break; | |
517 | ||
518 | case DW_OP_dup: | |
519 | if (stack_depth < 1) | |
520 | goto underflow; | |
521 | else | |
522 | { | |
523 | unsigned int tos = STACK (0); | |
524 | push (STACKFMT, tos); | |
525 | } | |
526 | break; | |
527 | ||
528 | case DW_OP_drop: | |
5cc50568 MW |
529 | { |
530 | POP (ignore); | |
f245e619 | 531 | emit ("%*s/* drop " STACKFMT "*/\n", indent * 2, "", (unsigned)ignore); |
5cc50568 MW |
532 | break; |
533 | } | |
77de5e9e GH |
534 | |
535 | case DW_OP_pick: | |
536 | sp = expr[i].number; | |
537 | op_pick: | |
538 | if (sp >= stack_depth) | |
539 | goto underflow; | |
540 | sp = STACK (sp); | |
f245e619 | 541 | push (STACKFMT, (unsigned)sp); |
77de5e9e GH |
542 | break; |
543 | ||
544 | case DW_OP_over: | |
545 | sp = 1; | |
546 | goto op_pick; | |
547 | ||
548 | case DW_OP_swap: | |
549 | if (stack_depth < 2) | |
550 | goto underflow; | |
551 | deepen (); /* Use a temporary slot. */ | |
552 | emit ("%*s" | |
553 | STACKFMT " = " STACKFMT ", " | |
554 | STACKFMT " = " STACKFMT ", " | |
555 | STACKFMT " = " STACKFMT ";\n", | |
556 | indent * 2, "", | |
f245e619 FCE |
557 | (unsigned)STACK (-1), (unsigned)STACK (0), |
558 | (unsigned)STACK (0), (unsigned)STACK (1), | |
559 | (unsigned)STACK (1), (unsigned)STACK (-1)); | |
77de5e9e GH |
560 | break; |
561 | ||
562 | case DW_OP_rot: | |
563 | if (stack_depth < 3) | |
564 | goto underflow; | |
565 | deepen (); /* Use a temporary slot. */ | |
566 | emit ("%*s" | |
567 | STACKFMT " = " STACKFMT ", " | |
568 | STACKFMT " = " STACKFMT ", " | |
569 | STACKFMT " = " STACKFMT ", " | |
570 | STACKFMT " = " STACKFMT ";\n", | |
571 | indent * 2, "", | |
f245e619 FCE |
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)); | |
77de5e9e GH |
576 | break; |
577 | ||
578 | ||
579 | /* Control flow operations. */ | |
580 | case DW_OP_skip: | |
581 | { | |
582 | Dwarf_Off target = expr[i].offset + 3 + expr[i].number; | |
583 | while (i + 1 < len && expr[i + 1].offset < target) | |
584 | ++i; | |
585 | if (expr[i + 1].offset != target) | |
586 | DIE ("invalid skip target"); | |
587 | break; | |
588 | } | |
589 | ||
590 | case DW_OP_bra: | |
591 | DIE ("conditional branches not supported"); | |
592 | break; | |
593 | ||
594 | ||
595 | /* Memory access. */ | |
596 | case DW_OP_deref: | |
597 | { | |
598 | POP (addr); | |
f245e619 | 599 | push ("deref (sizeof (void *), " STACKFMT ")", (unsigned)addr); |
9b9d9778 | 600 | used_deref = true; |
77de5e9e GH |
601 | } |
602 | break; | |
603 | ||
604 | case DW_OP_deref_size: | |
605 | { | |
606 | POP (addr); | |
607 | push ("deref (" UFORMAT ", " STACKFMT ")", | |
f245e619 | 608 | expr[i].number, (unsigned)addr); |
9b9d9778 | 609 | used_deref = true; |
77de5e9e GH |
610 | } |
611 | break; | |
612 | ||
613 | case DW_OP_xderef: | |
614 | { | |
615 | POP (addr); | |
616 | POP (as); | |
617 | push ("xderef (sizeof (void *), " STACKFMT ", " STACKFMT ")", | |
f245e619 | 618 | (unsigned)addr, (unsigned)as); |
9b9d9778 | 619 | used_deref = true; |
77de5e9e GH |
620 | } |
621 | break; | |
622 | ||
623 | case DW_OP_xderef_size: | |
624 | { | |
625 | POP (addr); | |
626 | POP (as); | |
627 | push ("xderef (" UFORMAT ", " STACKFMT ", " STACKFMT ")", | |
f245e619 | 628 | expr[i].number, (unsigned)addr, (unsigned)as); |
9b9d9778 | 629 | used_deref = true; |
77de5e9e GH |
630 | } |
631 | break; | |
632 | ||
633 | /* Constant-value operations. */ | |
634 | ||
635 | case DW_OP_addr: | |
4b1ad75e | 636 | emit ("%*s" STACKFMT " = ", indent * 2, "", PUSH); |
1220de76 RM |
637 | (*ctx->emit_address) (ctx->fail_arg, ctx->pool, |
638 | ctx->dwbias + expr[i].number); | |
4b1ad75e | 639 | emit (";\n"); |
77de5e9e GH |
640 | break; |
641 | ||
642 | case DW_OP_lit0 ... DW_OP_lit31: | |
643 | value = expr[i].atom - DW_OP_lit0; | |
644 | goto op_const; | |
645 | ||
646 | case DW_OP_const1u: | |
647 | case DW_OP_const1s: | |
648 | case DW_OP_const2u: | |
649 | case DW_OP_const2s: | |
650 | case DW_OP_const4u: | |
651 | case DW_OP_const4s: | |
652 | case DW_OP_const8u: | |
653 | case DW_OP_const8s: | |
654 | case DW_OP_constu: | |
655 | case DW_OP_consts: | |
656 | value = expr[i].number; | |
657 | op_const: | |
582840f4 | 658 | push (SFORMAT, (int64_t)value); |
77de5e9e GH |
659 | break; |
660 | ||
661 | /* Arithmetic operations. */ | |
662 | #define UNOP(dw_op, c_op) \ | |
663 | case DW_OP_##dw_op: \ | |
664 | { \ | |
665 | POP (tos); \ | |
f245e619 | 666 | push ("%s (" STACKFMT ")", #c_op, (unsigned)tos); \ |
77de5e9e GH |
667 | } \ |
668 | break | |
669 | #define BINOP(dw_op, c_op) \ | |
670 | case DW_OP_##dw_op: \ | |
671 | { \ | |
672 | POP (b); \ | |
673 | POP (a); \ | |
f245e619 | 674 | push (STACKFMT " %s " STACKFMT, (unsigned)a, #c_op, (unsigned)b); \ |
77de5e9e GH |
675 | } \ |
676 | break | |
677 | ||
678 | UNOP (abs, op_abs); | |
679 | BINOP (and, &); | |
77de5e9e | 680 | BINOP (minus, -); |
77de5e9e GH |
681 | BINOP (mul, *); |
682 | UNOP (neg, -); | |
683 | UNOP (not, ~); | |
684 | BINOP (or, |); | |
685 | BINOP (plus, +); | |
686 | BINOP (shl, <<); | |
5d580c21 | 687 | BINOP (shr, >>); |
77de5e9e GH |
688 | BINOP (xor, ^); |
689 | ||
690 | /* Comparisons are binary operators too. */ | |
691 | BINOP (le, <=); | |
692 | BINOP (ge, >=); | |
693 | BINOP (eq, ==); | |
694 | BINOP (lt, <); | |
695 | BINOP (gt, >); | |
696 | BINOP (ne, !=); | |
697 | ||
698 | #undef UNOP | |
699 | #undef BINOP | |
700 | ||
5d580c21 | 701 | case DW_OP_shra: |
77de5e9e GH |
702 | { |
703 | POP (b); | |
704 | POP (a); | |
705 | push ("(%s) " STACKFMT " >> (%s)" STACKFMT, | |
f245e619 FCE |
706 | stack_slot_type (loc, true), (unsigned)a, |
707 | stack_slot_type (loc, true), (unsigned)b); | |
77de5e9e GH |
708 | break; |
709 | } | |
710 | ||
682d5372 RM |
711 | case DW_OP_div: |
712 | { | |
713 | POP (b); | |
714 | POP (a); | |
fa2e3415 | 715 | push ("dwarf_div_op((%s) " STACKFMT ", (%s) " STACKFMT ")", |
f245e619 FCE |
716 | stack_slot_type (loc, true), (unsigned)a, |
717 | stack_slot_type (loc, true), (unsigned)b); | |
fa2e3415 MW |
718 | used_deref = true; |
719 | break; | |
720 | } | |
721 | ||
722 | case DW_OP_mod: | |
723 | { | |
724 | POP (b); | |
725 | POP (a); | |
726 | push ("dwarf_mod_op((%s) " STACKFMT ", (%s) " STACKFMT ")", | |
f245e619 FCE |
727 | stack_slot_type (loc, false), (unsigned)a, |
728 | stack_slot_type (loc, false), (unsigned)b); | |
fa2e3415 | 729 | used_deref = true; |
682d5372 RM |
730 | break; |
731 | } | |
732 | ||
77de5e9e GH |
733 | case DW_OP_plus_uconst: |
734 | { | |
735 | POP (x); | |
f245e619 | 736 | push (STACKFMT " + " UFORMAT, (unsigned)x, expr[i].number); |
77de5e9e GH |
737 | } |
738 | break; | |
739 | ||
740 | ||
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; | |
745 | goto op_breg; | |
746 | ||
747 | case DW_OP_bregx: | |
748 | reg = expr[i].number; | |
749 | value = expr[i].number2; | |
750 | op_breg: | |
582840f4 | 751 | push ("fetch_register (%u) + " SFORMAT, reg, (int64_t)value); |
77de5e9e GH |
752 | break; |
753 | ||
754 | case DW_OP_fbreg: | |
755 | if (need_fb == NULL) | |
756 | DIE ("DW_OP_fbreg from DW_AT_frame_base"); | |
757 | *need_fb = true; | |
582840f4 | 758 | push ("frame_base + " SFORMAT, (int64_t)expr[i].number); |
77de5e9e GH |
759 | break; |
760 | ||
761 | /* Direct register contents. */ | |
762 | case DW_OP_reg0 ... DW_OP_reg31: | |
763 | reg = expr[i].atom - DW_OP_reg0; | |
764 | goto op_reg; | |
765 | ||
766 | case DW_OP_regx: | |
767 | reg = expr[i].number; | |
768 | op_reg: | |
769 | tos_register = reg; | |
770 | break; | |
771 | ||
772 | /* Special magic. */ | |
773 | case DW_OP_piece: | |
774 | if (stack_depth > 1) | |
775 | /* If this ever happens we could copy the program. */ | |
776 | DIE ("DW_OP_piece left multiple values on stack"); | |
777 | else | |
778 | { | |
9b9d9778 RM |
779 | /* The obstack has a pending program for loc_address, |
780 | so we must finish that piece off before we can | |
781 | allocate again. */ | |
782 | struct location temp_piece = | |
783 | { | |
1220de76 | 784 | .context = loc->context, |
9b9d9778 RM |
785 | .frame_base = NULL, |
786 | .ops = &expr[piece_expr_start], | |
787 | .nops = i - piece_expr_start, | |
788 | }; | |
789 | const char *failure = finish (&temp_piece); | |
77de5e9e GH |
790 | if (failure != NULL) |
791 | return failure; | |
792 | ||
1220de76 | 793 | struct location *piece = obstack_alloc (ctx->pool, sizeof *piece); |
9b9d9778 RM |
794 | *piece = temp_piece; |
795 | ||
77de5e9e GH |
796 | piece_expr_start = i + 1; |
797 | ||
ce48dbc7 | 798 | piece_total_bytes += piece->byte_size = expr[i].number; |
77de5e9e GH |
799 | |
800 | *tailpiece = piece; | |
801 | tailpiece = &piece->next; | |
802 | piece->next = NULL; | |
56cfcfc6 RM |
803 | |
804 | /* Reset default conditions for handling the next piece. */ | |
805 | reset (); | |
77de5e9e GH |
806 | } |
807 | break; | |
808 | ||
24c7957b | 809 | case DW_OP_stack_value: |
45d7e325 RM |
810 | if (stack_depth > 1) |
811 | DIE ("DW_OP_stack_value left multiple values on stack"); | |
812 | else | |
813 | { | |
814 | /* Fetch a register to top of stack, or check for underflow. | |
815 | Then mark the TOS as being a value. */ | |
816 | POP (tos); | |
817 | assert (tos == 0); | |
818 | PUSH; | |
819 | tos_value = true; | |
820 | } | |
24c7957b MW |
821 | break; |
822 | ||
823 | case DW_OP_implicit_value: | |
1220de76 | 824 | if (ctx->attr == NULL) |
707e2bec RM |
825 | DIE ("DW_OP_implicit_value used in invalid context" |
826 | " (no DWARF attribute, ABI return value location?)"); | |
827 | ||
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"); | |
bd39efb3 RM |
831 | |
832 | #if _ELFUTILS_PREREQ (0, 143) | |
1220de76 RM |
833 | if (dwarf_getlocation_implicit_value (ctx->attr, |
834 | (Dwarf_Op *) &expr[i], | |
707e2bec RM |
835 | &implicit_value) != 0) |
836 | DIE ("dwarf_getlocation_implicit_value failed"); | |
bd39efb3 | 837 | |
707e2bec RM |
838 | /* Fake top of stack: implicit_value being set marks it. */ |
839 | PUSH; | |
840 | break; | |
bd39efb3 | 841 | #endif |
707e2bec | 842 | |
bd39efb3 RM |
843 | DIE ("DW_OP_implicit_value not supported"); |
844 | break; | |
24c7957b | 845 | |
1220de76 RM |
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. */ | |
850 | PUSH; | |
851 | break; | |
852 | #endif | |
853 | ||
00b01a99 MW |
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. | |
857 | if (need_fb == NULL) | |
858 | DIE ("DW_OP_call_frame_cfa while processing frame base"); | |
859 | else | |
6c476b58 MW |
860 | { |
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. | |
864 | *need_fb = true; | |
865 | push ("frame_base"); | |
866 | } | |
00b01a99 MW |
867 | break; |
868 | ||
77de5e9e | 869 | case DW_OP_push_object_address: |
1be70eb3 MW |
870 | DIE ("unhandled DW_OP_push_object_address"); |
871 | break; | |
872 | ||
873 | case DW_OP_GNU_entry_value: | |
874 | DIE ("unhandled DW_OP_GNU_entry_value"); | |
77de5e9e GH |
875 | break; |
876 | ||
877 | default: | |
878 | DIE ("unrecognized operation"); | |
879 | break; | |
880 | } | |
881 | } | |
882 | ||
883 | if (pieces == NULL) | |
884 | return finish (loc); | |
885 | ||
886 | if (piece_expr_start != i) | |
887 | DIE ("extra operations after last DW_OP_piece"); | |
888 | ||
889 | loc->type = loc_noncontiguous; | |
890 | loc->pieces = pieces; | |
ce48dbc7 | 891 | loc->byte_size = piece_total_bytes; |
77de5e9e GH |
892 | |
893 | return NULL; | |
894 | ||
895 | underflow: | |
896 | DIE ("stack underflow"); | |
897 | ||
898 | #undef emit | |
899 | #undef push | |
900 | #undef PUSH | |
901 | #undef POP | |
902 | #undef STACK | |
903 | #undef DIE | |
904 | } | |
905 | ||
906 | /* Translate a location starting from an address or nothing. */ | |
907 | static struct location * | |
1220de76 RM |
908 | location_from_address (struct location_context *ctx, int indent, |
909 | const Dwarf_Op *expr, size_t len, | |
910 | struct location **input) | |
77de5e9e | 911 | { |
1220de76 RM |
912 | struct location *loc = obstack_alloc (ctx->pool, sizeof *loc); |
913 | loc->context = ctx; | |
ce48dbc7 | 914 | loc->byte_size = 0; |
9b9d9778 | 915 | loc->frame_base = NULL; |
8a77568a RM |
916 | loc->ops = NULL; |
917 | loc->nops = 0; | |
e36387d7 | 918 | |
77de5e9e GH |
919 | bool need_fb = false; |
920 | size_t loser; | |
1220de76 | 921 | const char *failure = translate (ctx, indent + 1, expr, len, |
77de5e9e GH |
922 | *input, &need_fb, &loser, loc); |
923 | if (failure != NULL) | |
1220de76 | 924 | return lose (loc, expr, len, failure, loser); |
77de5e9e GH |
925 | |
926 | loc->next = NULL; | |
927 | if (need_fb) | |
928 | { | |
929 | /* The main expression uses DW_OP_fbreg, so we need to compute | |
930 | the DW_AT_frame_base attribute expression's value first. */ | |
6c476b58 | 931 | static Dwarf_Op dummy_cfa_op = { .atom = DW_OP_call_frame_cfa }; |
b5bd7775 | 932 | |
20e4a32c | 933 | Dwarf_Op *fb_expr; |
77de5e9e | 934 | size_t fb_len; |
77de5e9e | 935 | |
6c476b58 MW |
936 | if (ctx->fb_attr == NULL) |
937 | { | |
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. | |
942 | fb_len = 1; | |
943 | fb_expr = &dummy_cfa_op; | |
944 | } | |
945 | else | |
946 | { | |
947 | switch (dwarf_getlocation_addr (ctx->fb_attr, ctx->pc, | |
948 | &fb_expr, &fb_len, 1)) | |
949 | { | |
950 | case 1: /* Should always happen. */ | |
951 | if (fb_len == 0) | |
952 | goto fb_inaccessible; | |
953 | break; | |
954 | ||
955 | default: /* Shouldn't happen. */ | |
956 | case -1: | |
957 | FAIL (loc, N_("dwarf_getlocation_addr (form %#x): %s"), | |
958 | dwarf_whatform (ctx->fb_attr), dwarf_errmsg (-1)); | |
959 | return NULL; | |
960 | ||
961 | case 0: /* Shouldn't happen. */ | |
962 | fb_inaccessible: | |
963 | FAIL (loc, N_("DW_AT_frame_base not accessible at this address")); | |
964 | return NULL; | |
965 | } | |
77de5e9e GH |
966 | } |
967 | ||
00b01a99 MW |
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) | |
971 | { | |
1220de76 | 972 | if (ctx->cfa_ops == NULL) |
00b01a99 | 973 | FAIL (loc, N_("No cfa_ops supplied, but needed by DW_OP_call_frame_cfa")); |
1220de76 | 974 | fb_ops = ctx->cfa_ops; |
00b01a99 MW |
975 | } |
976 | else | |
977 | fb_ops = fb_expr; | |
978 | ||
1220de76 RM |
979 | loc->frame_base = alloc_location (ctx); |
980 | failure = translate (ctx, indent + 1, fb_ops, fb_len, NULL, | |
9b9d9778 | 981 | NULL, &loser, loc->frame_base); |
77de5e9e | 982 | if (failure != NULL) |
1220de76 | 983 | return lose (loc, fb_ops, fb_len, failure, loser); |
77de5e9e GH |
984 | } |
985 | ||
986 | if (*input != NULL) | |
987 | (*input)->next = loc; | |
988 | *input = loc; | |
989 | ||
990 | return loc; | |
991 | } | |
992 | ||
653a2c7b | 993 | #if _ELFUTILS_PREREQ (0, 149) |
1220de76 RM |
994 | static struct location * |
995 | location_from_attr (struct location_context *ctx, int indent, | |
996 | Dwarf_Attribute *attr) | |
997 | { | |
998 | Dwarf_Op *expr; | |
999 | size_t len; | |
1000 | switch (dwarf_getlocation_addr (attr, ctx->pc, &expr, &len, 1)) | |
1001 | { | |
1002 | case 1: /* Should always happen. */ | |
1003 | if (len > 0) | |
1004 | break; | |
1005 | /* Fall through. */ | |
1006 | ||
1007 | case 0: /* Shouldn't happen. */ | |
5b7a351e | 1008 | (*ctx->fail) (ctx->fail_arg, N_("not accessible at this address (%#" PRIx64 ")"), ctx->pc); |
1220de76 RM |
1009 | return NULL; |
1010 | ||
1011 | default: /* Shouldn't happen. */ | |
1012 | case -1: | |
ce0f6648 | 1013 | (*ctx->fail) (ctx->fail_arg, "dwarf_getlocation_addr: %s", |
1220de76 RM |
1014 | dwarf_errmsg (-1)); |
1015 | return NULL; | |
1016 | } | |
1017 | ||
1018 | struct location *input = NULL; | |
1019 | return location_from_address (ctx, indent, expr, len, &input); | |
1020 | } | |
653a2c7b | 1021 | #endif |
1220de76 RM |
1022 | |
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, | |
1026 | Dwarf_Word offset) | |
1027 | { | |
1028 | struct location_context *const ctx = (*input)->context; | |
1029 | ||
1030 | #define DIE(msg) return lose (*input, expr, len, N_(msg), i) | |
1031 | ||
1032 | while ((*input)->type == loc_noncontiguous) | |
1033 | { | |
1034 | /* We are starting from a noncontiguous object (DW_OP_piece). | |
1035 | Find the piece we want. */ | |
1036 | ||
1037 | struct location *piece = (*input)->pieces; | |
1038 | while (piece != NULL && offset >= piece->byte_size) | |
1039 | { | |
1040 | offset -= piece->byte_size; | |
1041 | piece = piece->next; | |
1042 | } | |
1043 | if (piece == NULL) | |
1044 | DIE ("offset outside available pieces"); | |
1045 | ||
1046 | assert ((*input)->next == NULL); | |
1047 | (*input)->next = piece; | |
1048 | *input = piece; | |
1049 | } | |
1050 | ||
1051 | switch ((*input)->type) | |
1052 | { | |
1053 | case loc_address: | |
1054 | { | |
1055 | /* The piece we want is actually in memory. Use the same | |
1056 | program to compute the address from the preceding input. */ | |
1057 | ||
1058 | struct location *loc = obstack_alloc (ctx->pool, sizeof *loc); | |
1059 | *loc = **input; | |
1060 | if (head == NULL) | |
1061 | head = loc; | |
1062 | (*input)->next = loc; | |
1063 | if (offset == 0) | |
1064 | { | |
1065 | /* The piece addresses exactly where we want to go. */ | |
1066 | loc->next = NULL; | |
1067 | *input = loc; | |
1068 | } | |
1069 | else | |
1070 | { | |
1071 | /* Add a second fragment to offset the piece address. */ | |
1072 | obstack_printf (ctx->pool, "%*saddr += " SFORMAT "\n", | |
582840f4 | 1073 | indent * 2, "", (int64_t)offset); |
1220de76 RM |
1074 | *input = loc->next = new_synthetic_loc (*input, false); |
1075 | } | |
1076 | ||
1077 | /* That's all she wrote. */ | |
1078 | return head; | |
1079 | } | |
1080 | ||
1081 | case loc_register: | |
1082 | /* This piece (or the whole struct) fits in a register. */ | |
1083 | (*input)->reg.offset += offset; | |
1084 | return head ?: *input; | |
1085 | ||
1086 | case loc_constant: | |
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; | |
1093 | ||
1094 | case loc_implicit_pointer: | |
1095 | /* This piece is an implicit pointer. */ | |
1096 | (*input)->pointer.offset += offset; | |
1097 | return head ?: *input; | |
1098 | ||
1099 | case loc_unavailable: | |
1100 | /* Let it be diagnosed later. */ | |
1101 | return head ?: *input; | |
1102 | ||
1103 | case loc_value: | |
1104 | /* The piece we want is part of a computed offset. | |
1105 | If it's the whole thing, we are done. */ | |
1106 | if (offset == 0) | |
1107 | return head ?: *input; | |
1108 | DIE ("XXX extract partial rematerialized value"); | |
1109 | break; | |
1110 | ||
1111 | default: | |
1112 | abort (); | |
1113 | } | |
1114 | ||
1115 | #undef DIE | |
1116 | } | |
1117 | ||
1118 | ||
77de5e9e GH |
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" | |
24c7957b | 1122 | of an object (or the actual value) relative to that "address". */ |
77de5e9e GH |
1123 | |
1124 | static struct location * | |
1220de76 RM |
1125 | location_relative (struct location_context *ctx, int indent, |
1126 | const Dwarf_Op *expr, size_t len, | |
1127 | struct location **input) | |
77de5e9e | 1128 | { |
5cc50568 | 1129 | Dwarf_Sword *stack = NULL; |
77de5e9e GH |
1130 | unsigned int stack_depth = 0, max_stack = 0; |
1131 | inline void deepen (void) | |
1132 | { | |
1133 | if (stack_depth == max_stack) | |
1134 | { | |
1135 | ++max_stack; | |
1220de76 RM |
1136 | obstack_blank (ctx->pool, sizeof stack[0]); |
1137 | stack = (void *) obstack_base (ctx->pool); | |
77de5e9e GH |
1138 | } |
1139 | } | |
1140 | ||
1141 | #define POP(var) \ | |
1142 | if (stack_depth > 0) \ | |
1143 | --stack_depth; \ | |
1144 | else \ | |
1145 | goto underflow; \ | |
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)] | |
1150 | ||
1151 | /* Don't put stack operations in the arguments to this. */ | |
1152 | #define push(value) (stack[PUSH] = (value)) | |
1153 | ||
1154 | const char *failure = NULL; | |
e36387d7 | 1155 | #define DIE(msg) do { failure = N_(msg); goto fail; } while (0) |
77de5e9e GH |
1156 | |
1157 | struct location *head = NULL; | |
1158 | size_t i; | |
1159 | for (i = 0; i < len; ++i) | |
1160 | { | |
1161 | uint_fast8_t sp; | |
1162 | Dwarf_Word value; | |
1163 | ||
1164 | switch (expr[i].atom) | |
1165 | { | |
1166 | /* Basic stack operations. */ | |
1167 | case DW_OP_nop: | |
1168 | break; | |
1169 | ||
1170 | case DW_OP_dup: | |
1171 | if (stack_depth < 1) | |
1172 | goto underflow; | |
1173 | else | |
1174 | { | |
1175 | unsigned int tos = STACK (0); | |
1176 | push (stack[tos]); | |
1177 | } | |
1178 | break; | |
1179 | ||
1180 | case DW_OP_drop: | |
1181 | if (stack_depth > 0) | |
1182 | --stack_depth; | |
1183 | else if (*input != NULL) | |
1184 | /* Mark that we have consumed the input. */ | |
1185 | *input = NULL; | |
1186 | else | |
1187 | /* Hits if cleared above, or if we had no input at all. */ | |
1188 | goto underflow; | |
1189 | break; | |
1190 | ||
1191 | case DW_OP_pick: | |
1192 | sp = expr[i].number; | |
1193 | op_pick: | |
1194 | if (sp >= stack_depth) | |
1195 | goto underflow; | |
1196 | sp = STACK (sp); | |
1197 | push (stack[sp]); | |
1198 | break; | |
1199 | ||
1200 | case DW_OP_over: | |
1201 | sp = 1; | |
1202 | goto op_pick; | |
1203 | ||
1204 | case DW_OP_swap: | |
1205 | if (stack_depth < 2) | |
1206 | goto underflow; | |
1207 | deepen (); /* Use a temporary slot. */ | |
1208 | STACKWORD (-1) = STACKWORD (0); | |
1209 | STACKWORD (0) = STACKWORD (1); | |
1210 | STACKWORD (1) = STACKWORD (-1); | |
1211 | break; | |
1212 | ||
1213 | case DW_OP_rot: | |
1214 | if (stack_depth < 3) | |
1215 | goto underflow; | |
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); | |
1221 | break; | |
1222 | ||
1223 | ||
1224 | /* Control flow operations. */ | |
1225 | case DW_OP_bra: | |
1226 | { | |
1227 | POP (taken); | |
1228 | if (stack[taken] == 0) | |
1229 | break; | |
1230 | } | |
1231 | /*FALLTHROUGH*/ | |
1232 | ||
1233 | case DW_OP_skip: | |
1234 | { | |
1235 | Dwarf_Off target = expr[i].offset + 3 + expr[i].number; | |
1236 | while (i + 1 < len && expr[i + 1].offset < target) | |
1237 | ++i; | |
1238 | if (expr[i + 1].offset != target) | |
1239 | DIE ("invalid skip target"); | |
1240 | break; | |
1241 | } | |
1242 | ||
1243 | /* Memory access. */ | |
1244 | case DW_OP_deref: | |
1245 | case DW_OP_deref_size: | |
1246 | case DW_OP_xderef: | |
1247 | case DW_OP_xderef_size: | |
1248 | ||
1249 | /* Register-relative addressing. */ | |
1250 | case DW_OP_breg0 ... DW_OP_breg31: | |
1251 | case DW_OP_bregx: | |
1252 | case DW_OP_fbreg: | |
1253 | ||
1254 | /* This started from a register, but now it's following a pointer. | |
1255 | So we can do the translation starting from address here. */ | |
1220de76 | 1256 | return location_from_address (ctx, indent, expr, len, input); |
77de5e9e GH |
1257 | |
1258 | ||
1259 | /* Constant-value operations. */ | |
1260 | case DW_OP_addr: | |
4b1ad75e | 1261 | DIE ("static calculation depends on load-time address"); |
1220de76 | 1262 | push (ctx->dwbias + expr[i].number); |
77de5e9e GH |
1263 | break; |
1264 | ||
1265 | case DW_OP_lit0 ... DW_OP_lit31: | |
1266 | value = expr[i].atom - DW_OP_lit0; | |
1267 | goto op_const; | |
1268 | ||
1269 | case DW_OP_const1u: | |
1270 | case DW_OP_const1s: | |
1271 | case DW_OP_const2u: | |
1272 | case DW_OP_const2s: | |
1273 | case DW_OP_const4u: | |
1274 | case DW_OP_const4s: | |
1275 | case DW_OP_const8u: | |
1276 | case DW_OP_const8s: | |
1277 | case DW_OP_constu: | |
1278 | case DW_OP_consts: | |
1279 | value = expr[i].number; | |
1280 | op_const: | |
1281 | push (value); | |
1282 | break; | |
1283 | ||
1284 | /* Arithmetic operations. */ | |
1285 | #define UNOP(dw_op, c_op) \ | |
1286 | case DW_OP_##dw_op: \ | |
1287 | { \ | |
1288 | POP (tos); \ | |
1289 | push (c_op (stack[tos])); \ | |
1290 | } \ | |
1291 | break | |
1292 | #define BINOP(dw_op, c_op) \ | |
1293 | case DW_OP_##dw_op: \ | |
1294 | { \ | |
1295 | POP (b); \ | |
1296 | POP (a); \ | |
1297 | push (stack[a] c_op stack[b]); \ | |
1298 | } \ | |
1299 | break | |
1300 | ||
1301 | #define op_abs(x) (x < 0 ? -x : x) | |
1302 | UNOP (abs, op_abs); | |
1303 | BINOP (and, &); | |
1304 | BINOP (div, /); | |
1305 | BINOP (mod, %); | |
1306 | BINOP (mul, *); | |
1307 | UNOP (neg, -); | |
1308 | UNOP (not, ~); | |
1309 | BINOP (or, |); | |
1310 | BINOP (shl, <<); | |
1311 | BINOP (shra, >>); | |
1312 | BINOP (xor, ^); | |
1313 | ||
1314 | /* Comparisons are binary operators too. */ | |
1315 | BINOP (le, <=); | |
1316 | BINOP (ge, >=); | |
1317 | BINOP (eq, ==); | |
1318 | BINOP (lt, <); | |
1319 | BINOP (gt, >); | |
1320 | BINOP (ne, !=); | |
1321 | ||
1322 | #undef UNOP | |
1323 | #undef BINOP | |
1324 | ||
1325 | case DW_OP_shr: | |
1326 | { | |
1327 | POP (b); | |
1328 | POP (a); | |
1329 | push ((Dwarf_Word) stack[a] >> (Dwarf_Word) stack[b]); | |
1330 | break; | |
1331 | } | |
1332 | ||
1333 | /* Simple addition we may be able to handle relative to | |
1334 | the starting register name. */ | |
1335 | case DW_OP_minus: | |
1336 | { | |
1337 | POP (tos); | |
1338 | value = -stack[tos]; | |
1339 | goto plus; | |
1340 | } | |
1341 | case DW_OP_plus: | |
1342 | { | |
1343 | POP (tos); | |
1344 | value = stack[tos]; | |
1345 | goto plus; | |
1346 | } | |
1347 | case DW_OP_plus_uconst: | |
1348 | value = expr[i].number; | |
1349 | plus: | |
1350 | if (stack_depth > 0) | |
1351 | { | |
1352 | /* It's just private diddling after all. */ | |
1353 | POP (a); | |
1354 | push (stack[a] + value); | |
1355 | break; | |
1356 | } | |
1357 | if (*input == NULL) | |
1358 | goto underflow; | |
1359 | ||
1360 | /* This is the primary real-world case: the expression takes | |
1361 | the input address and adds a constant offset. */ | |
1362 | ||
1220de76 RM |
1363 | head = translate_offset (indent, expr, len, i, head, input, value); |
1364 | if (head != NULL && i + 1 < len) | |
77de5e9e | 1365 | { |
1220de76 RM |
1366 | if ((*input)->type != loc_address) |
1367 | DIE ("too much computation for non-address location"); | |
1368 | ||
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); | |
1374 | if (loc == NULL) | |
1375 | head = NULL; | |
77de5e9e | 1376 | } |
1220de76 | 1377 | return head; |
77de5e9e GH |
1378 | |
1379 | /* Direct register contents. */ | |
1380 | case DW_OP_reg0 ... DW_OP_reg31: | |
1381 | case DW_OP_regx: | |
1382 | DIE ("register"); | |
1383 | break; | |
1384 | ||
1385 | /* Special magic. */ | |
1386 | case DW_OP_piece: | |
1387 | DIE ("DW_OP_piece"); | |
1388 | break; | |
1389 | ||
1390 | case DW_OP_push_object_address: | |
1be70eb3 MW |
1391 | DIE ("unhandled DW_OP_push_object_address"); |
1392 | break; | |
1393 | ||
1394 | case DW_OP_GNU_entry_value: | |
1395 | DIE ("unhandled DW_OP_GNU_entry_value"); | |
77de5e9e GH |
1396 | break; |
1397 | ||
1398 | default: | |
1399 | DIE ("unrecognized operation"); | |
1400 | break; | |
1401 | } | |
1402 | } | |
1403 | ||
1404 | if (stack_depth > 1) | |
1405 | DIE ("multiple values left on stack"); | |
1406 | ||
1407 | if (stack_depth > 0) /* stack_depth == 1 */ | |
1408 | { | |
1409 | if (*input != NULL) | |
1410 | DIE ("multiple values left on stack"); | |
1411 | ||
1412 | /* Could handle this if it ever actually happened. */ | |
1413 | DIE ("relative expression computed constant"); | |
1414 | } | |
1415 | ||
1416 | return head; | |
1417 | ||
1418 | underflow: | |
1419 | if (*input == NULL) | |
1420 | DIE ("stack underflow"); | |
1421 | else | |
1422 | DIE ("cannot handle location expression"); | |
1423 | ||
1424 | fail: | |
1220de76 | 1425 | return lose (*input, expr, len, failure, i); |
77de5e9e GH |
1426 | } |
1427 | ||
77de5e9e GH |
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. | |
1432 | ||
ce48dbc7 | 1433 | On errors, call FAIL and never return. On success, return the |
77de5e9e GH |
1434 | first fragment created, which is also chained onto (*INPUT)->next. |
1435 | *INPUT is then updated with the new tail of that chain. */ | |
1436 | ||
1437 | struct location * | |
1438 | c_translate_location (struct obstack *pool, | |
e36387d7 RM |
1439 | void (*fail) (void *arg, const char *fmt, ...) |
1440 | __attribute__ ((noreturn, format (printf, 2, 3))), | |
1441 | void *fail_arg, | |
4b1ad75e RM |
1442 | void (*emit_address) (void *fail_arg, |
1443 | struct obstack *, Dwarf_Addr), | |
d1531387 | 1444 | int indent, Dwarf_Addr dwbias, Dwarf_Addr pc_address, |
24c7957b | 1445 | Dwarf_Attribute *attr, |
d1531387 | 1446 | const Dwarf_Op *expr, size_t len, |
00b01a99 MW |
1447 | struct location **input, Dwarf_Attribute *fb_attr, |
1448 | const Dwarf_Op *cfa_ops) | |
77de5e9e | 1449 | { |
d1531387 | 1450 | indent += 2; |
e141cc19 | 1451 | |
1220de76 RM |
1452 | struct location_context *ctx; |
1453 | if (*input == NULL) | |
1454 | ctx = new_context (pool, fail, fail_arg, emit_address, dwbias, pc_address, | |
1455 | attr, fb_attr, cfa_ops); | |
1456 | else | |
1457 | { | |
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; | |
11475197 FCE |
1464 | // PR15148: disable this assertion, in case the PR15123 address-retry logic |
1465 | // sent us this way | |
1466 | // assert (ctx->pc == pc_address); | |
1220de76 RM |
1467 | } |
1468 | ||
77de5e9e GH |
1469 | switch (*input == NULL ? loc_address : (*input)->type) |
1470 | { | |
1471 | case loc_address: | |
1472 | /* We have a previous address computation. | |
1473 | This expression will compute starting with that on the stack. */ | |
1220de76 | 1474 | return location_from_address (ctx, indent, expr, len, input); |
77de5e9e GH |
1475 | |
1476 | case loc_noncontiguous: | |
1477 | case loc_register: | |
45d7e325 | 1478 | case loc_value: |
707e2bec | 1479 | case loc_constant: |
35bbf143 | 1480 | case loc_unavailable: |
1220de76 | 1481 | case loc_implicit_pointer: |
77de5e9e | 1482 | /* The starting point is not an address computation, but a |
24c7957b MW |
1483 | register or implicit value. We can only handle limited |
1484 | computations from here. */ | |
1220de76 | 1485 | return location_relative (ctx, indent, expr, len, input); |
77de5e9e | 1486 | |
77de5e9e GH |
1487 | default: |
1488 | abort (); | |
1489 | break; | |
1490 | } | |
1491 | ||
1492 | return NULL; | |
1493 | } | |
1494 | ||
2e2a5d82 | 1495 | |
c4ce66a1 JS |
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. */ | |
1499 | struct location * | |
1500 | c_translate_argument (struct obstack *pool, | |
1501 | void (*fail) (void *arg, const char *fmt, ...) | |
1502 | __attribute__ ((noreturn, format (printf, 2, 3))), | |
1503 | void *fail_arg, | |
1504 | void (*emit_address) (void *fail_arg, | |
1505 | struct obstack *, Dwarf_Addr), | |
1506 | int indent, const char *value) | |
1507 | { | |
1508 | indent += 2; | |
1509 | ||
2e2a5d82 | 1510 | obstack_printf (pool, "%*saddr = %s;\n", indent * 2, "", value); |
c4ce66a1 JS |
1511 | obstack_1grow (pool, '\0'); |
1512 | char *program = obstack_finish (pool); | |
1513 | ||
1514 | struct location *loc = obstack_alloc (pool, sizeof *loc); | |
1220de76 RM |
1515 | loc->context = new_context (pool, fail, fail_arg, emit_address, 0, |
1516 | 0, NULL, NULL, NULL); | |
c4ce66a1 | 1517 | loc->next = NULL; |
c4ce66a1 JS |
1518 | loc->ops = NULL; |
1519 | loc->nops = 0; | |
1520 | loc->byte_size = 0; | |
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; | |
1527 | ||
1528 | return loc; | |
1529 | } | |
1530 | ||
1531 | ||
77de5e9e GH |
1532 | /* Emit "uintNN_t TARGET = ...;". */ |
1533 | static bool | |
1534 | emit_base_fetch (struct obstack *pool, Dwarf_Word byte_size, | |
e41b2484 | 1535 | bool signed_p, const char *target, struct location *loc) |
77de5e9e | 1536 | { |
83f1d2e5 | 1537 | bool deref = false; |
e41b2484 | 1538 | |
5ddc2ead | 1539 | /* Emit size/signed coercion. */ |
83f1d2e5 | 1540 | obstack_printf (pool, "{ "); |
5ddc2ead | 1541 | obstack_printf (pool, "%sint%u_t value = ", |
83f1d2e5 | 1542 | (signed_p ? "" : "u"), (unsigned)(byte_size * 8)); |
77de5e9e GH |
1543 | |
1544 | switch (loc->type) | |
1545 | { | |
45d7e325 RM |
1546 | case loc_value: |
1547 | obstack_printf (pool, "addr;"); | |
1548 | break; | |
1549 | ||
77de5e9e GH |
1550 | case loc_address: |
1551 | if (byte_size != 0 && byte_size != (Dwarf_Word) -1) | |
e41b2484 | 1552 | obstack_printf (pool, "deref (%" PRIu64 ", addr);", byte_size); |
77de5e9e | 1553 | else |
e41b2484 | 1554 | obstack_printf (pool, "deref (sizeof %s, addr);", target); |
83f1d2e5 FCE |
1555 | deref = true; |
1556 | break; | |
77de5e9e GH |
1557 | |
1558 | case loc_register: | |
5ddc2ead RM |
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); | |
77de5e9e GH |
1562 | break; |
1563 | ||
1564 | case loc_noncontiguous: | |
ce48dbc7 | 1565 | FAIL (loc, N_("noncontiguous location for base fetch")); |
77de5e9e GH |
1566 | break; |
1567 | ||
1220de76 RM |
1568 | case loc_implicit_pointer: |
1569 | FAIL (loc, N_("pointer has been optimized out")); | |
1570 | break; | |
1571 | ||
35bbf143 RM |
1572 | case loc_unavailable: |
1573 | FAIL (loc, N_("location not available")); | |
1574 | break; | |
1575 | ||
77de5e9e GH |
1576 | default: |
1577 | abort (); | |
1578 | break; | |
1579 | } | |
1580 | ||
56cfcfc6 | 1581 | obstack_printf (pool, " %s = value; }", target); |
83f1d2e5 | 1582 | return deref; |
77de5e9e GH |
1583 | } |
1584 | ||
e141cc19 RM |
1585 | /* Emit "... = RVALUE;". */ |
1586 | static bool | |
1587 | emit_base_store (struct obstack *pool, Dwarf_Word byte_size, | |
1588 | const char *rvalue, struct location *loc) | |
1589 | { | |
1590 | switch (loc->type) | |
1591 | { | |
1592 | case loc_address: | |
1593 | if (byte_size != 0 && byte_size != (Dwarf_Word) -1) | |
1594 | obstack_printf (pool, "store_deref (%" PRIu64 ", addr, %s); ", | |
1595 | byte_size, rvalue); | |
1596 | else | |
1597 | obstack_printf (pool, "store_deref (sizeof %s, addr, %s); ", | |
1598 | rvalue, rvalue); | |
1599 | return true; | |
1600 | ||
1601 | case loc_register: | |
5ddc2ead RM |
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); | |
e141cc19 RM |
1605 | break; |
1606 | ||
1607 | case loc_noncontiguous: | |
ce48dbc7 | 1608 | FAIL (loc, N_("noncontiguous location for base store")); |
e141cc19 RM |
1609 | break; |
1610 | ||
1220de76 RM |
1611 | case loc_implicit_pointer: |
1612 | FAIL (loc, N_("pointer has been optimized out")); | |
1613 | break; | |
1614 | ||
45d7e325 RM |
1615 | case loc_value: |
1616 | FAIL (loc, N_("location is computed value, cannot store")); | |
1617 | break; | |
1618 | ||
707e2bec RM |
1619 | case loc_constant: |
1620 | FAIL (loc, N_("location is constant value, cannot store")); | |
1621 | break; | |
1622 | ||
35bbf143 RM |
1623 | case loc_unavailable: |
1624 | FAIL (loc, N_("location is not available, cannot store")); | |
1625 | break; | |
1626 | ||
e141cc19 RM |
1627 | default: |
1628 | abort (); | |
1629 | break; | |
1630 | } | |
1631 | ||
1632 | return false; | |
1633 | } | |
1634 | ||
77de5e9e | 1635 | |
ce48dbc7 RM |
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 * | |
1220de76 | 1639 | discontiguify (struct location_context *ctx, int indent, struct location *loc, |
ce48dbc7 RM |
1640 | Dwarf_Word total_bytes, Dwarf_Word max_piece_bytes) |
1641 | { | |
1642 | inline bool pieces_small_enough (void) | |
1643 | { | |
1644 | if (loc->type != loc_noncontiguous) | |
315bfa79 | 1645 | return total_bytes <= max_piece_bytes; |
78f6bba6 FCE |
1646 | struct location *p; |
1647 | for (p = loc->pieces; p != NULL; p = p->next) | |
ce48dbc7 RM |
1648 | if (p->byte_size > max_piece_bytes) |
1649 | return false; | |
1650 | return true; | |
1651 | } | |
77de5e9e | 1652 | |
315bfa79 JS |
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) | |
1656 | { | |
1657 | if (loc->byte_size > total_bytes) | |
1658 | loc->byte_size = total_bytes; | |
1659 | return loc; | |
1660 | } | |
1661 | ||
ce48dbc7 RM |
1662 | if (pieces_small_enough ()) |
1663 | return loc; | |
1664 | ||
1220de76 | 1665 | struct location *noncontig = alloc_location (ctx); |
ce48dbc7 RM |
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) | |
1672 | { | |
1673 | *tailpiece = piece; | |
1674 | tailpiece = &piece->next; | |
1675 | } | |
1676 | ||
1677 | switch (loc->type) | |
1678 | { | |
1679 | case loc_address: | |
1680 | { | |
1681 | /* Synthesize a piece that sets "container_addr" to the computed | |
1682 | address of the whole object. Each piece will refer to this. */ | |
1220de76 | 1683 | obstack_printf (ctx->pool, "%*scontainer_addr = addr;\n", |
ce48dbc7 | 1684 | indent++ * 2, ""); |
1220de76 | 1685 | loc->next = new_synthetic_loc (loc, false); |
ce48dbc7 RM |
1686 | loc->next->byte_size = loc->byte_size; |
1687 | loc->next->type = loc_fragment; | |
1688 | loc->next->address.declare = "container_addr"; | |
1689 | loc = loc->next; | |
1690 | ||
1691 | /* Synthesize pieces that just compute "container_addr + N". */ | |
1692 | Dwarf_Word offset = 0; | |
1693 | while (total_bytes - offset > 0) | |
1694 | { | |
1695 | Dwarf_Word size = total_bytes - offset; | |
1696 | if (size > max_piece_bytes) | |
1697 | size = max_piece_bytes; | |
1698 | ||
1220de76 RM |
1699 | obstack_printf (ctx->pool, |
1700 | "%*saddr = container_addr + " UFORMAT ";\n", | |
ce48dbc7 | 1701 | indent * 2, "", offset); |
1220de76 | 1702 | struct location *piece = new_synthetic_loc (loc, false); |
ce48dbc7 RM |
1703 | piece->byte_size = size; |
1704 | add (piece); | |
1705 | ||
1706 | offset += size; | |
1707 | } | |
1708 | ||
1709 | --indent; | |
1710 | break; | |
1711 | } | |
1712 | ||
45d7e325 RM |
1713 | case loc_value: |
1714 | FAIL (loc, N_("stack value too big for fetch ???")); | |
1715 | break; | |
1716 | ||
ce48dbc7 RM |
1717 | case loc_register: |
1718 | FAIL (loc, N_("single register too big for fetch/store ???")); | |
1719 | break; | |
1720 | ||
1220de76 RM |
1721 | case loc_implicit_pointer: |
1722 | FAIL (loc, N_("implicit pointer too big for fetch/store ???")); | |
1723 | break; | |
1724 | ||
ce48dbc7 RM |
1725 | case loc_noncontiguous: |
1726 | /* Could be handled if it ever happened. */ | |
ce0f6648 | 1727 | FAIL (loc, N_("cannot support noncontiguous location")); |
ce48dbc7 RM |
1728 | break; |
1729 | ||
1730 | default: | |
1731 | abort (); | |
1732 | break; | |
1733 | } | |
1734 | ||
1735 | loc->next = noncontig; | |
1736 | return noncontig; | |
1737 | } | |
1738 | ||
1739 | /* Make a fragment that declares a union such as: | |
1740 | union { | |
1741 | char bytes[8]; | |
1742 | struct { | |
1743 | uint32_t p0; | |
1744 | uint32_t p4; | |
1745 | } pieces __attribute__ ((packed)); | |
1746 | uint64_t whole; | |
9fa29c52 | 1747 | } u_pieces<depth>; |
ce48dbc7 RM |
1748 | */ |
1749 | static void | |
1750 | declare_noncontig_union (struct obstack *pool, int indent, | |
9fa29c52 MW |
1751 | struct location **input, struct location *loc, |
1752 | int depth) | |
77de5e9e | 1753 | { |
9fa29c52 MW |
1754 | if (depth > 9) |
1755 | FAIL (loc, N_("declaring noncontig union for depth > 9, too many pieces")); | |
1756 | ||
02a966bc MW |
1757 | if (loc->byte_size > 8) |
1758 | FAIL (loc, N_("cannot create noncontig union type larger than 64 bits")); | |
1759 | ||
ce48dbc7 | 1760 | obstack_printf (pool, "%*sunion {\n", indent++ * 2, ""); |
377b204c | 1761 | |
ce48dbc7 RM |
1762 | obstack_printf (pool, "%*schar bytes[%" PRIu64 "];\n", |
1763 | indent * 2, "", loc->byte_size); | |
e141cc19 | 1764 | |
707e2bec | 1765 | if (loc->type == loc_noncontiguous) |
77de5e9e | 1766 | { |
707e2bec RM |
1767 | Dwarf_Word offset = 0; |
1768 | struct location *p; | |
5ec323b5 MW |
1769 | obstack_printf (pool, "%*sstruct {\n", indent++ * 2, ""); |
1770 | ||
707e2bec RM |
1771 | for (p = loc->pieces; p != NULL; p = p->next) |
1772 | { | |
1773 | obstack_printf (pool, "%*suint%" PRIu64 "_t p%" PRIu64 ";\n", | |
1774 | indent * 2, "", p->byte_size * 8, offset); | |
1775 | offset += p->byte_size; | |
1776 | } | |
77de5e9e | 1777 | |
707e2bec RM |
1778 | obstack_printf (pool, "%*s} pieces __attribute__ ((packed));\n", |
1779 | --indent * 2, ""); | |
1780 | } | |
77de5e9e | 1781 | |
ce48dbc7 RM |
1782 | obstack_printf (pool, "%*suint%" PRIu64 "_t whole;\n", |
1783 | indent * 2, "", loc->byte_size * 8); | |
1784 | ||
9fa29c52 | 1785 | obstack_printf (pool, "%*s} u_pieces%d;\n", --indent * 2, "", depth); |
ce48dbc7 | 1786 | |
1220de76 | 1787 | loc = new_synthetic_loc (*input, false); |
ce48dbc7 | 1788 | loc->type = loc_decl; |
77de5e9e GH |
1789 | (*input)->next = loc; |
1790 | *input = loc; | |
1791 | } | |
1792 | ||
1793 | /* Determine the byte size of a base type. */ | |
1794 | static Dwarf_Word | |
e36387d7 | 1795 | base_byte_size (Dwarf_Die *typedie, struct location *origin) |
77de5e9e | 1796 | { |
e7a012f0 GH |
1797 | assert (dwarf_tag (typedie) == DW_TAG_base_type || |
1798 | dwarf_tag (typedie) == DW_TAG_enumeration_type); | |
377b204c | 1799 | |
77de5e9e GH |
1800 | Dwarf_Attribute attr_mem; |
1801 | Dwarf_Word size; | |
1802 | if (dwarf_attr_integrate (typedie, DW_AT_byte_size, &attr_mem) != NULL | |
1803 | && dwarf_formudata (&attr_mem, &size) == 0) | |
1804 | return size; | |
1805 | ||
e36387d7 RM |
1806 | FAIL (origin, |
1807 | N_("cannot get byte_size attribute for type %s: %s"), | |
ce48dbc7 | 1808 | dwarf_diename (typedie) ?: "<anonymous>", |
77de5e9e GH |
1809 | dwarf_errmsg (-1)); |
1810 | return -1; | |
1811 | } | |
1812 | ||
83f1d2e5 FCE |
1813 | static Dwarf_Word |
1814 | base_encoding (Dwarf_Die *typedie, struct location *origin) | |
1815 | { | |
d6b38089 FCE |
1816 | if (! (dwarf_tag (typedie) == DW_TAG_base_type || |
1817 | dwarf_tag (typedie) == DW_TAG_enumeration_type)) | |
1818 | return -1; | |
83f1d2e5 FCE |
1819 | |
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) | |
1824 | return encoding; | |
1825 | ||
d6b38089 FCE |
1826 | (void) origin; |
1827 | /* | |
83f1d2e5 FCE |
1828 | FAIL (origin, |
1829 | N_("cannot get encoding attribute for type %s: %s"), | |
1830 | dwarf_diename (typedie) ?: "<anonymous>", | |
1831 | dwarf_errmsg (-1)); | |
d6b38089 | 1832 | */ |
83f1d2e5 FCE |
1833 | return -1; |
1834 | } | |
1835 | ||
1836 | ||
1837 | ||
ce48dbc7 RM |
1838 | /* Fetch the bitfield parameters. */ |
1839 | static void | |
1840 | get_bitfield (struct location *loc, | |
1841 | Dwarf_Die *die, Dwarf_Word *bit_offset, Dwarf_Word *bit_size) | |
77de5e9e | 1842 | { |
77de5e9e GH |
1843 | Dwarf_Attribute attr_mem; |
1844 | if (dwarf_attr_integrate (die, DW_AT_bit_offset, &attr_mem) == NULL | |
ce48dbc7 | 1845 | || dwarf_formudata (&attr_mem, bit_offset) != 0 |
77de5e9e | 1846 | || dwarf_attr_integrate (die, DW_AT_bit_size, &attr_mem) == NULL |
ce48dbc7 | 1847 | || dwarf_formudata (&attr_mem, bit_size) != 0) |
e36387d7 | 1848 | FAIL (loc, N_("cannot get bit field parameters: %s"), dwarf_errmsg (-1)); |
77de5e9e GH |
1849 | } |
1850 | ||
ce48dbc7 | 1851 | /* Translate a fragment to fetch the base-type value of BYTE_SIZE bytes |
e141cc19 | 1852 | at the *INPUT location and store it in lvalue TARGET. */ |
ce48dbc7 | 1853 | static void |
707e2bec RM |
1854 | translate_base_fetch (struct obstack *pool, int indent, |
1855 | Dwarf_Word byte_size, bool signed_p, | |
9fa29c52 MW |
1856 | struct location **input, const char *target, |
1857 | int depth) | |
77de5e9e | 1858 | { |
ce48dbc7 | 1859 | bool deref = false; |
77de5e9e | 1860 | |
ce48dbc7 RM |
1861 | if ((*input)->type == loc_noncontiguous) |
1862 | { | |
1863 | struct location *p = (*input)->pieces; | |
77de5e9e | 1864 | |
9fa29c52 | 1865 | declare_noncontig_union (pool, indent, input, *input, depth); |
ce48dbc7 RM |
1866 | |
1867 | Dwarf_Word offset = 0; | |
9fa29c52 MW |
1868 | char piece[sizeof "u_pieces?.pieces.p" + 20] = "u_pieces?.pieces.p"; |
1869 | piece[8] = (char) ('0' + depth); | |
1870 | int pdepth = depth + 1; | |
ce48dbc7 RM |
1871 | while (p != NULL) |
1872 | { | |
1873 | struct location *newp = obstack_alloc (pool, sizeof *newp); | |
1874 | *newp = *p; | |
1875 | newp->next = NULL; | |
1876 | (*input)->next = newp; | |
1877 | *input = newp; | |
1878 | ||
9fa29c52 | 1879 | snprintf (&piece[sizeof "u_pieces?.pieces.p" - 1], 20, |
73b5e906 | 1880 | "%" PRIu64, offset); |
e41b2484 | 1881 | translate_base_fetch (pool, indent, p->byte_size, signed_p /* ? */, |
9fa29c52 | 1882 | input, piece, pdepth); |
ce48dbc7 RM |
1883 | (*input)->type = loc_fragment; |
1884 | ||
1885 | offset += p->byte_size; | |
1886 | p = p->next; | |
9fa29c52 | 1887 | pdepth++; |
ce48dbc7 RM |
1888 | } |
1889 | ||
9fa29c52 MW |
1890 | obstack_printf (pool, "%*s%s = u_pieces%d.whole;\n", indent * 2, |
1891 | "", target, depth); | |
707e2bec RM |
1892 | } |
1893 | else if ((*input)->type == loc_constant) | |
1894 | { | |
1895 | const unsigned char *constant_block = (*input)->constant_block; | |
1896 | const size_t byte_size = (*input)->byte_size; | |
1897 | size_t i; | |
1898 | ||
9fa29c52 | 1899 | declare_noncontig_union (pool, indent, input, *input, depth); |
707e2bec RM |
1900 | |
1901 | for (i = 0; i < byte_size; ++i) | |
9fa29c52 | 1902 | obstack_printf (pool, "%*su_pieces%d.bytes[%zu] = %#x;\n", indent * 2, |
f245e619 | 1903 | "", depth, i, (unsigned)constant_block[i]); |
707e2bec | 1904 | |
9fa29c52 MW |
1905 | obstack_printf (pool, "%*s%s = u_pieces%d.whole;\n", indent * 2, |
1906 | "", target, depth); | |
ce48dbc7 | 1907 | } |
77de5e9e GH |
1908 | else |
1909 | switch (byte_size) | |
1910 | { | |
ce48dbc7 | 1911 | case 0: /* Special case, means address size. */ |
77de5e9e GH |
1912 | case 1: |
1913 | case 2: | |
1914 | case 4: | |
1915 | case 8: | |
1916 | obstack_printf (pool, "%*s", indent * 2, ""); | |
e41b2484 | 1917 | deref = emit_base_fetch (pool, byte_size, signed_p, target, *input); |
77de5e9e GH |
1918 | obstack_printf (pool, "\n"); |
1919 | break; | |
1920 | ||
1921 | default: | |
1922 | /* Could handle this generating call to memcpy equivalent. */ | |
e36387d7 | 1923 | FAIL (*input, N_("fetch is larger than base integer types")); |
77de5e9e GH |
1924 | break; |
1925 | } | |
1926 | ||
1220de76 | 1927 | struct location *loc = new_synthetic_loc (*input, deref); |
ce48dbc7 | 1928 | loc->byte_size = byte_size; |
77de5e9e GH |
1929 | loc->type = loc_final; |
1930 | (*input)->next = loc; | |
1931 | *input = loc; | |
1932 | } | |
1933 | ||
ce48dbc7 RM |
1934 | /* Determine the maximum size of a base type, from some DIE in the CU. */ |
1935 | static Dwarf_Word | |
1936 | max_fetch_size (struct location *loc, Dwarf_Die *die) | |
1937 | { | |
1938 | Dwarf_Die cu_mem; | |
1939 | uint8_t address_size; | |
ce48dbc7 RM |
1940 | Dwarf_Die *cu = dwarf_diecu (die, &cu_mem, &address_size, NULL); |
1941 | if (cu == NULL) | |
ce0f6648 | 1942 | //TRANSLATORS: CU stands for 'compilation unit' |
ce48dbc7 RM |
1943 | FAIL (loc, N_("cannot determine CU address size from %s: %s"), |
1944 | dwarf_diename (die), dwarf_errmsg (-1)); | |
1945 | ||
1946 | return address_size; | |
1947 | } | |
1948 | ||
1949 | /* Translate a fragment to fetch the value of variable or member DIE | |
1950 | at the *INPUT location and store it in lvalue TARGET. */ | |
1951 | void | |
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) | |
1956 | { | |
1220de76 RM |
1957 | struct location_context *const ctx = (*input)->context; |
1958 | assert (ctx->pool == pool); | |
1959 | ||
ce48dbc7 RM |
1960 | ++indent; |
1961 | ||
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); | |
1967 | ||
e41b2484 FCE |
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) | |
83f1d2e5 | 1972 | encoding = base_encoding (typedie, *input); |
707e2bec | 1973 | bool signed_p = encoding == DW_ATE_signed || encoding == DW_ATE_signed_char; |
e41b2484 | 1974 | |
1220de76 | 1975 | *input = discontiguify (ctx, indent, *input, byte_size, |
ce48dbc7 RM |
1976 | max_fetch_size (*input, die)); |
1977 | ||
1978 | if (dwarf_hasattr_integrate (die, DW_AT_bit_offset)) | |
1979 | { | |
1980 | /* This is a bit field. Fetch the containing base type into a | |
1981 | temporary variable. */ | |
1982 | ||
9fa29c52 | 1983 | translate_base_fetch (pool, indent, byte_size, signed_p, input, "tmp", 0); |
ce48dbc7 RM |
1984 | (*input)->type = loc_fragment; |
1985 | (*input)->address.declare = "tmp"; | |
1986 | ||
555ffd15 LB |
1987 | Dwarf_Word bit_offset = 0; |
1988 | Dwarf_Word bit_size = 0; | |
ce48dbc7 RM |
1989 | get_bitfield (*input, die, &bit_offset, &bit_size); |
1990 | ||
1991 | obstack_printf (pool, "%*s" | |
1992 | "fetch_bitfield (%s, tmp, %" PRIu64 ", %" PRIu64 ");\n", | |
1993 | indent *2, "", target, bit_offset, bit_size); | |
1994 | ||
1220de76 | 1995 | struct location *loc = new_synthetic_loc (*input, false); |
ce48dbc7 RM |
1996 | loc->type = loc_final; |
1997 | (*input)->next = loc; | |
1998 | *input = loc; | |
1999 | } | |
2000 | else | |
9fa29c52 | 2001 | translate_base_fetch (pool, indent, byte_size, signed_p, input, target, 0); |
ce48dbc7 RM |
2002 | } |
2003 | ||
2004 | /* Translate a fragment to store RVALUE into the base-type value of | |
2005 | BYTE_SIZE bytes at the *INPUT location. */ | |
2006 | static void | |
2007 | translate_base_store (struct obstack *pool, int indent, Dwarf_Word byte_size, | |
2008 | struct location **input, struct location *store_loc, | |
9fa29c52 | 2009 | const char *rvalue, int depth) |
ce48dbc7 RM |
2010 | { |
2011 | bool deref = false; | |
2012 | ||
2013 | if (store_loc->type == loc_noncontiguous) | |
2014 | { | |
9fa29c52 | 2015 | declare_noncontig_union (pool, indent, input, store_loc, depth); |
ce48dbc7 | 2016 | |
9fa29c52 MW |
2017 | obstack_printf (pool, "%*su_pieces%d.whole = %s;\n", indent * 2, |
2018 | "", depth, rvalue); | |
1220de76 | 2019 | struct location *loc = new_synthetic_loc (*input, deref); |
ce48dbc7 RM |
2020 | loc->type = loc_fragment; |
2021 | (*input)->next = loc; | |
2022 | *input = loc; | |
2023 | ||
2024 | Dwarf_Word offset = 0; | |
9fa29c52 MW |
2025 | char piece[sizeof "u_pieces?.pieces.p" + 20] = "u_pieces?.pieces.p"; |
2026 | piece[8] = (char) ('0' + depth); | |
78f6bba6 | 2027 | struct location *p; |
9fa29c52 | 2028 | int pdepth = depth + 1; |
78f6bba6 | 2029 | for (p = store_loc->pieces; p != NULL; p = p->next) |
b8da0ad1 | 2030 | { |
ce48dbc7 RM |
2031 | struct location *newp = obstack_alloc (pool, sizeof *newp); |
2032 | *newp = *p; | |
2033 | newp->next = NULL; | |
2034 | (*input)->next = newp; | |
2035 | *input = newp; | |
2036 | ||
9fa29c52 | 2037 | snprintf (&piece[sizeof "u_pieces?.pieces.p" - 1], 20, "%" PRIu64, |
73b5e906 | 2038 | offset); |
ce48dbc7 | 2039 | translate_base_store (pool, indent, |
9fa29c52 | 2040 | p->byte_size, input, *input, piece, pdepth++); |
ce48dbc7 RM |
2041 | (*input)->type = loc_fragment; |
2042 | ||
2043 | offset += p->byte_size; | |
2044 | } | |
2045 | ||
2046 | (*input)->type = loc_final; | |
2047 | } | |
2048 | else | |
2049 | { | |
2050 | switch (byte_size) | |
2051 | { | |
2052 | case 1: | |
2053 | case 2: | |
2054 | case 4: | |
2055 | case 8: | |
2056 | obstack_printf (pool, "%*s", indent * 2, ""); | |
2057 | deref = emit_base_store (pool, byte_size, rvalue, store_loc); | |
2058 | obstack_printf (pool, "\n"); | |
2059 | break; | |
2060 | ||
2061 | default: | |
2062 | /* Could handle this generating call to memcpy equivalent. */ | |
2063 | FAIL (*input, N_("store is larger than base integer types")); | |
2064 | break; | |
2065 | } | |
2066 | ||
1220de76 | 2067 | struct location *loc = new_synthetic_loc (*input, deref); |
ce48dbc7 RM |
2068 | loc->type = loc_final; |
2069 | (*input)->next = loc; | |
2070 | *input = loc; | |
2071 | } | |
2072 | } | |
2073 | ||
e141cc19 RM |
2074 | /* Translate a fragment to fetch the value of variable or member DIE |
2075 | at the *INPUT location and store it in rvalue RVALUE. */ | |
2076 | ||
2077 | void | |
2078 | c_translate_store (struct obstack *pool, int indent, | |
2079 | Dwarf_Addr dwbias __attribute__ ((unused)), | |
377b204c | 2080 | Dwarf_Die *die, Dwarf_Die *typedie, |
e141cc19 RM |
2081 | struct location **input, const char *rvalue) |
2082 | { | |
1220de76 RM |
2083 | struct location_context *const ctx = (*input)->context; |
2084 | assert (ctx->pool == pool); | |
2085 | ||
e141cc19 RM |
2086 | ++indent; |
2087 | ||
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) | |
e36387d7 | 2092 | byte_size = base_byte_size (typedie, *input); |
e141cc19 | 2093 | |
e41b2484 FCE |
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) | |
83f1d2e5 | 2098 | encoding = base_encoding (typedie, *input); |
5ddc2ead | 2099 | bool signed_p = (encoding == DW_ATE_signed |
83f1d2e5 | 2100 | || encoding == DW_ATE_signed_char); |
e41b2484 | 2101 | |
1220de76 | 2102 | *input = discontiguify (ctx, indent, *input, byte_size, |
ce48dbc7 RM |
2103 | max_fetch_size (*input, die)); |
2104 | ||
2105 | struct location *store_loc = *input; | |
2106 | ||
e141cc19 | 2107 | if (dwarf_hasattr_integrate (die, DW_AT_bit_offset)) |
ce48dbc7 RM |
2108 | { |
2109 | /* This is a bit field. Fetch the containing base type into a | |
2110 | temporary variable. */ | |
e141cc19 | 2111 | |
9fa29c52 | 2112 | translate_base_fetch (pool, indent, byte_size, signed_p, input, "tmp", 0); |
ce48dbc7 RM |
2113 | (*input)->type = loc_fragment; |
2114 | (*input)->address.declare = "tmp"; | |
e141cc19 | 2115 | |
555ffd15 LB |
2116 | Dwarf_Word bit_offset = 0; |
2117 | Dwarf_Word bit_size = 0; | |
ce48dbc7 RM |
2118 | get_bitfield (*input, die, &bit_offset, &bit_size); |
2119 | ||
2120 | obstack_printf (pool, "%*s" | |
2121 | "store_bitfield (tmp, %s, %" PRIu64 ", %" PRIu64 ");\n", | |
2122 | indent * 2, "", rvalue, bit_offset, bit_size); | |
2123 | ||
1220de76 | 2124 | struct location *loc = new_synthetic_loc (*input, false); |
ce48dbc7 RM |
2125 | loc->type = loc_fragment; |
2126 | (*input)->next = loc; | |
2127 | *input = loc; | |
2128 | ||
2129 | /* We have mixed RVALUE into the bits in "tmp". | |
2130 | Now we'll store "tmp" back whence we fetched it. */ | |
2131 | rvalue = "tmp"; | |
2132 | } | |
2133 | ||
9fa29c52 | 2134 | translate_base_store (pool, indent, byte_size, input, store_loc, rvalue, 0); |
ce48dbc7 RM |
2135 | } |
2136 | ||
2137 | /* Translate a fragment to dereference the given pointer type, | |
2138 | where *INPUT is the location of the pointer with that type. | |
2139 | ||
2140 | We chain on a loc_address program that yields this pointer value | |
2141 | (i.e. the location of what it points to). */ | |
2142 | ||
2143 | void | |
2144 | c_translate_pointer (struct obstack *pool, int indent, | |
2145 | Dwarf_Addr dwbias __attribute__ ((unused)), | |
2146 | Dwarf_Die *typedie, struct location **input) | |
2147 | { | |
9c119951 JS |
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); | |
ce48dbc7 RM |
2151 | |
2152 | Dwarf_Attribute attr_mem; | |
2153 | Dwarf_Word byte_size; | |
2154 | if (dwarf_attr_integrate (typedie, DW_AT_byte_size, &attr_mem) == NULL) | |
4bfa7779 | 2155 | byte_size = max_fetch_size (*input, typedie); /* Use CU address size. */ |
ce48dbc7 RM |
2156 | else if (dwarf_formudata (&attr_mem, &byte_size) != 0) |
2157 | FAIL (*input, | |
2158 | N_("cannot get byte_size attribute for type %s: %s"), | |
2159 | dwarf_diename (typedie) ?: "<anonymous>", | |
2160 | dwarf_errmsg (-1)); | |
2161 | ||
1220de76 RM |
2162 | if ((*input)->type == loc_implicit_pointer) |
2163 | { | |
2164 | struct location *const target = (*input)->pointer.target; | |
2165 | const Dwarf_Sword offset = (*input)->pointer.offset; | |
2166 | (*input)->next = target; | |
2167 | if (offset != 0) | |
2168 | translate_offset (indent, NULL, 0, 0, NULL, &(*input)->next, offset); | |
2169 | *input = (*input)->next; | |
2170 | } | |
2171 | else | |
2172 | { | |
2173 | bool signed_p = false; /* XXX: Does not matter? */ | |
e41b2484 | 2174 | |
1220de76 RM |
2175 | translate_base_fetch (pool, indent + 1, byte_size, signed_p, input, |
2176 | "addr", 0); | |
2177 | (*input)->type = loc_address; | |
2178 | } | |
e141cc19 RM |
2179 | } |
2180 | ||
2181 | ||
77de5e9e GH |
2182 | void |
2183 | c_translate_addressof (struct obstack *pool, int indent, | |
2184 | Dwarf_Addr dwbias __attribute__ ((unused)), | |
2185 | Dwarf_Die *die, | |
f0df19a2 | 2186 | Dwarf_Die *typedie __attribute__ ((unused)), |
77de5e9e GH |
2187 | struct location **input, const char *target) |
2188 | { | |
2189 | ++indent; | |
2190 | ||
1220de76 RM |
2191 | if (dwarf_hasattr_integrate (die, DW_AT_bit_offset) |
2192 | || dwarf_hasattr_integrate (die, DW_AT_data_bit_offset)) | |
e36387d7 | 2193 | FAIL (*input, N_("cannot take the address of a bit field")); |
77de5e9e GH |
2194 | |
2195 | switch ((*input)->type) | |
2196 | { | |
2197 | case loc_address: | |
2198 | obstack_printf (pool, "%*s%s = addr;\n", indent * 2, "", target); | |
1220de76 | 2199 | (*input)->next = new_synthetic_loc (*input, false); |
77de5e9e GH |
2200 | (*input)->next->type = loc_final; |
2201 | break; | |
2202 | ||
2203 | case loc_register: | |
e36387d7 | 2204 | FAIL (*input, N_("cannot take address of object in register")); |
77de5e9e GH |
2205 | break; |
2206 | case loc_noncontiguous: | |
e36387d7 | 2207 | FAIL (*input, N_("cannot take address of noncontiguous object")); |
77de5e9e | 2208 | break; |
45d7e325 RM |
2209 | case loc_value: |
2210 | FAIL (*input, N_("cannot take address of computed value")); | |
2211 | break; | |
707e2bec RM |
2212 | case loc_constant: |
2213 | FAIL (*input, N_("cannot take address of constant value")); | |
2214 | break; | |
35bbf143 RM |
2215 | case loc_unavailable: |
2216 | FAIL (*input, N_("cannot take address of unavailable value")); | |
2217 | break; | |
1220de76 RM |
2218 | case loc_implicit_pointer: |
2219 | FAIL (*input, N_("cannot take address of implicit pointer")); | |
2220 | break; | |
77de5e9e GH |
2221 | |
2222 | default: | |
1220de76 | 2223 | abort (); |
77de5e9e GH |
2224 | break; |
2225 | } | |
2226 | } | |
2227 | ||
2228 | ||
b0be9bdb FCE |
2229 | /* Translate a fragment to write the given pointer value, |
2230 | where *INPUT is the location of the pointer with that type. | |
2231 | */ | |
2232 | ||
2233 | void | |
2234 | c_translate_pointer_store (struct obstack *pool, int indent, | |
2235 | Dwarf_Addr dwbias __attribute__ ((unused)), | |
2236 | Dwarf_Die *typedie, struct location **input, | |
2237 | const char *rvalue) | |
2238 | { | |
2239 | assert (dwarf_tag (typedie) == DW_TAG_pointer_type); | |
2240 | ||
2241 | Dwarf_Attribute attr_mem; | |
2242 | Dwarf_Word byte_size; | |
2243 | if (dwarf_attr_integrate (typedie, DW_AT_byte_size, &attr_mem) == NULL) | |
4bfa7779 | 2244 | byte_size = max_fetch_size (*input, typedie); /* Use CU address size. */ |
b0be9bdb FCE |
2245 | else if (dwarf_formudata (&attr_mem, &byte_size) != 0) |
2246 | FAIL (*input, | |
2247 | N_("cannot get byte_size attribute for type %s: %s"), | |
2248 | dwarf_diename (typedie) ?: "<anonymous>", | |
2249 | dwarf_errmsg (-1)); | |
2250 | ||
2251 | translate_base_store (pool, indent + 1, byte_size, | |
9fa29c52 | 2252 | input, *input, rvalue, 0); |
b0be9bdb FCE |
2253 | |
2254 | // XXX: what about multiple-location lvalues? | |
2255 | } | |
2256 | ||
f3b5366d | 2257 | /* Determine the element stride of a pointer to a type. */ |
77de5e9e | 2258 | static Dwarf_Word |
f3b5366d | 2259 | pointer_stride (Dwarf_Die *typedie, struct location *origin) |
77de5e9e GH |
2260 | { |
2261 | Dwarf_Attribute attr_mem; | |
f3b5366d | 2262 | Dwarf_Die die_mem = *typedie; |
ddb02f50 | 2263 | int typetag = dwarf_tag (&die_mem); |
28ad563d | 2264 | while (typetag == DW_TAG_typedef || |
f3b5366d | 2265 | typetag == DW_TAG_const_type || |
c69af88f MW |
2266 | typetag == DW_TAG_volatile_type || |
2267 | typetag == DW_TAG_restrict_type) | |
28ad563d JS |
2268 | { |
2269 | if (dwarf_attr_integrate (&die_mem, DW_AT_type, &attr_mem) == NULL | |
f3b5366d | 2270 | || dwarf_formref_die (&attr_mem, &die_mem) == NULL) |
ce0f6648 | 2271 | //TRANSLATORS: This refers to the basic type, (stripped of const/volatile/etc.) |
f3b5366d JS |
2272 | FAIL (origin, N_("cannot get inner type of type %s: %s"), |
2273 | dwarf_diename (&die_mem) ?: "<anonymous>", | |
2274 | dwarf_errmsg (-1)); | |
ddb02f50 | 2275 | typetag = dwarf_tag (&die_mem); |
28ad563d JS |
2276 | } |
2277 | ||
77de5e9e GH |
2278 | if (dwarf_attr_integrate (&die_mem, DW_AT_byte_size, &attr_mem) != NULL) |
2279 | { | |
2280 | Dwarf_Word stride; | |
2281 | if (dwarf_formudata (&attr_mem, &stride) == 0) | |
2282 | return stride; | |
e36387d7 RM |
2283 | FAIL (origin, |
2284 | N_("cannot get byte_size attribute for array element type %s: %s"), | |
ce48dbc7 | 2285 | dwarf_diename (&die_mem) ?: "<anonymous>", |
e36387d7 | 2286 | dwarf_errmsg (-1)); |
77de5e9e GH |
2287 | } |
2288 | ||
e36387d7 | 2289 | FAIL (origin, N_("confused about array element size")); |
77de5e9e GH |
2290 | return 0; |
2291 | } | |
2292 | ||
f3b5366d JS |
2293 | /* Determine the element stride of an array type. */ |
2294 | static Dwarf_Word | |
2295 | array_stride (Dwarf_Die *typedie, struct location *origin) | |
2296 | { | |
2297 | Dwarf_Attribute attr_mem; | |
2298 | if (dwarf_attr_integrate (typedie, DW_AT_byte_stride, &attr_mem) != NULL) | |
2299 | { | |
2300 | Dwarf_Word stride; | |
2301 | if (dwarf_formudata (&attr_mem, &stride) == 0) | |
2302 | return stride; | |
2303 | FAIL (origin, N_("cannot get byte_stride attribute array type %s: %s"), | |
2304 | dwarf_diename (typedie) ?: "<anonymous>", | |
2305 | dwarf_errmsg (-1)); | |
2306 | } | |
2307 | ||
2308 | Dwarf_Die die_mem; | |
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>", | |
2313 | dwarf_errmsg (-1)); | |
2314 | ||
2315 | return pointer_stride (&die_mem, origin); | |
2316 | } | |
2317 | ||
ddb02f50 RM |
2318 | static void |
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) | |
77de5e9e GH |
2323 | { |
2324 | ++indent; | |
2325 | ||
77de5e9e GH |
2326 | struct location *loc = *input; |
2327 | while (loc->type == loc_noncontiguous) | |
2328 | { | |
2329 | if (idx != NULL) | |
e36387d7 | 2330 | FAIL (*input, N_("cannot dynamically index noncontiguous array")); |
77de5e9e GH |
2331 | else |
2332 | { | |
2333 | Dwarf_Word offset = const_idx * stride; | |
2334 | struct location *piece = loc->pieces; | |
2335 | while (piece != NULL && offset >= piece->byte_size) | |
2336 | { | |
2337 | offset -= piece->byte_size; | |
2338 | piece = piece->next; | |
2339 | } | |
2340 | if (piece == NULL) | |
ce0f6648 | 2341 | //TRANSLATORS: The index is constant |
e36387d7 | 2342 | FAIL (*input, N_("constant index is outside noncontiguous array")); |
ae90c066 | 2343 | if (offset % stride != 0 || piece->byte_size < stride) |
e36387d7 | 2344 | FAIL (*input, N_("noncontiguous array splits elements")); |
77de5e9e GH |
2345 | const_idx = offset / stride; |
2346 | loc = piece; | |
2347 | } | |
2348 | } | |
2349 | ||
2350 | switch (loc->type) | |
2351 | { | |
2352 | case loc_address: | |
2353 | ++indent; | |
2354 | if (idx != NULL) | |
2355 | obstack_printf (pool, "%*saddr += %s * " UFORMAT ";\n", | |
2356 | indent * 2, "", idx, stride); | |
2357 | else | |
2358 | obstack_printf (pool, "%*saddr += " UFORMAT " * " UFORMAT ";\n", | |
2359 | indent * 2, "", const_idx, stride); | |
1220de76 | 2360 | loc = new_synthetic_loc (loc, false); |
77de5e9e GH |
2361 | break; |
2362 | ||
2363 | case loc_register: | |
5ddc2ead RM |
2364 | if (idx != NULL) |
2365 | FAIL (*input, N_("cannot index array stored in a register")); | |
ddb02f50 | 2366 | else if (const_idx > max_fetch_size (loc, anydie) / stride) |
5ddc2ead RM |
2367 | FAIL (*input, N_("constant index is outside array held in register")); |
2368 | else | |
2369 | { | |
2370 | loc->reg.offset += const_idx * stride; | |
2371 | return; | |
2372 | } | |
77de5e9e GH |
2373 | break; |
2374 | ||
707e2bec RM |
2375 | case loc_constant: |
2376 | if (idx != NULL) | |
2377 | FAIL (*input, N_("cannot index into constant value")); | |
2378 | else if (const_idx > loc->byte_size / stride) | |
ce0f6648 | 2379 | //TRANSLATORS: The index is constant |
707e2bec RM |
2380 | FAIL (*input, N_("constant index is outside constant array value")); |
2381 | else | |
2382 | { | |
2383 | loc->byte_size = stride; | |
2384 | loc->constant_block += const_idx * stride; | |
2385 | return; | |
2386 | }; | |
2387 | break; | |
2388 | ||
1220de76 RM |
2389 | case loc_implicit_pointer: |
2390 | if (idx != NULL) | |
2391 | FAIL (*input, N_("cannot index into implicit pointer")); | |
2392 | else | |
2393 | loc->pointer.offset += const_idx * stride; | |
2394 | break; | |
2395 | ||
45d7e325 | 2396 | case loc_value: |
ddb02f50 RM |
2397 | if (idx != NULL || const_idx != 0) |
2398 | FAIL (*input, N_("cannot index into computed value")); | |
45d7e325 RM |
2399 | break; |
2400 | ||
35bbf143 | 2401 | case loc_unavailable: |
ddb02f50 RM |
2402 | if (idx != NULL || const_idx != 0) |
2403 | FAIL (*input, N_("cannot index into unavailable value")); | |
35bbf143 RM |
2404 | break; |
2405 | ||
77de5e9e | 2406 | default: |
ddb02f50 | 2407 | abort (); |
77de5e9e GH |
2408 | break; |
2409 | } | |
2410 | ||
2411 | (*input)->next = loc; | |
2412 | *input = (*input)->next; | |
2413 | } | |
2414 | ||
ddb02f50 RM |
2415 | void |
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) | |
2420 | { | |
2421 | assert (dwarf_tag (typedie) == DW_TAG_array_type || | |
2422 | dwarf_tag (typedie) == DW_TAG_pointer_type); | |
2423 | ||
2424 | return translate_array (pool, indent, typedie, | |
2425 | array_stride (typedie, *input), | |
2426 | input, idx, const_idx); | |
2427 | } | |
2428 | ||
f3b5366d JS |
2429 | void |
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) | |
2433 | { | |
ddb02f50 RM |
2434 | return translate_array (pool, indent, typedie, |
2435 | pointer_stride (typedie, *input), | |
2436 | input, idx, const_idx); | |
f3b5366d | 2437 | } |
77de5e9e GH |
2438 | \f |
2439 | /* Emitting C code for finalized fragments. */ | |
2440 | ||
77de5e9e GH |
2441 | #define emit(fmt, ...) fprintf (out, fmt, ## __VA_ARGS__) |
2442 | ||
2443 | /* Open a block with a comment giving the original DWARF expression. */ | |
2444 | static void | |
f245e619 | 2445 | emit_header (FILE *out, struct location *loc, int hindent) |
77de5e9e GH |
2446 | { |
2447 | if (loc->ops == NULL) | |
2448 | emit ("%*s{ // synthesized\n", hindent * 2, ""); | |
2449 | else | |
2450 | { | |
2451 | emit ("%*s{ // DWARF expression:", hindent * 2, ""); | |
78f6bba6 FCE |
2452 | size_t i; |
2453 | for (i = 0; i < loc->nops; ++i) | |
77de5e9e | 2454 | { |
f245e619 | 2455 | emit (" %#x", (unsigned)loc->ops[i].atom); |
77de5e9e GH |
2456 | if (loc->ops[i].number2 == 0) |
2457 | { | |
2458 | if (loc->ops[i].number != 0) | |
057f87fb | 2459 | emit ("(%" PRId64 ")", (int64_t)loc->ops[i].number); |
77de5e9e GH |
2460 | } |
2461 | else | |
2462 | emit ("(%" PRId64 ",%" PRId64 ")", | |
057f87fb | 2463 | (int64_t)loc->ops[i].number, (int64_t)loc->ops[i].number2); |
77de5e9e GH |
2464 | } |
2465 | emit ("\n"); | |
2466 | } | |
2467 | } | |
2468 | ||
2469 | /* Emit a code fragment to assign the target variable to a register value. */ | |
2470 | static void | |
f245e619 | 2471 | emit_loc_register (FILE *out, struct location *loc, int indent, |
77de5e9e GH |
2472 | const char *target) |
2473 | { | |
2474 | assert (loc->type == loc_register); | |
2475 | ||
5ddc2ead RM |
2476 | if (loc->reg.offset != 0) |
2477 | FAIL (loc, N_("cannot handle offset into register in fetch")); | |
2478 | ||
77de5e9e | 2479 | emit ("%*s%s = fetch_register (%u);\n", |
5ddc2ead | 2480 | indent * 2, "", target, loc->reg.regno); |
77de5e9e GH |
2481 | } |
2482 | ||
2483 | /* Emit a code fragment to assign the target variable to an address. */ | |
2484 | static void | |
f245e619 | 2485 | emit_loc_address (FILE *out, struct location *loc, int indent, |
77de5e9e GH |
2486 | const char *target) |
2487 | { | |
0c254456 | 2488 | assert (loc->type == loc_address || loc->type == loc_value); |
77de5e9e GH |
2489 | |
2490 | if (loc->address.stack_depth == 0) | |
2491 | /* Synthetic program. */ | |
2492 | emit ("%s", loc->address.program); | |
2493 | else | |
2494 | { | |
f1999557 | 2495 | emit ("%*s{\n", indent * 2, ""); |
b25c01a1 | 2496 | emit ("%*s%s " STACKFMT, (indent + 1) * 2, "", |
f245e619 | 2497 | stack_slot_type (loc, false), (unsigned)0); |
78f6bba6 FCE |
2498 | unsigned i; |
2499 | for (i = 1; i < loc->address.stack_depth; ++i) | |
77de5e9e GH |
2500 | emit (", " STACKFMT, i); |
2501 | emit (";\n"); | |
2502 | ||
2503 | emit ("%s%*s%s = " STACKFMT ";\n", loc->address.program, | |
f245e619 | 2504 | (indent + 1) * 2, "", target, (unsigned)0); |
f1999557 | 2505 | emit ("%*s}\n", indent * 2, ""); |
77de5e9e GH |
2506 | } |
2507 | } | |
2508 | ||
2509 | /* Emit a code fragment to declare the target variable and | |
2510 | assign it to an address-sized value. */ | |
2511 | static void | |
f245e619 | 2512 | emit_loc_value (FILE *out, struct location *loc, int indent, |
85dfc5c8 RM |
2513 | const char *target, bool declare, |
2514 | bool *used_deref, unsigned int *max_stack) | |
77de5e9e GH |
2515 | { |
2516 | if (declare) | |
b25c01a1 | 2517 | emit ("%*s%s %s;\n", indent * 2, "", stack_slot_type (loc, false), target); |
77de5e9e | 2518 | |
e141cc19 | 2519 | emit_header (out, loc, indent++); |
77de5e9e GH |
2520 | |
2521 | switch (loc->type) | |
2522 | { | |
2523 | default: | |
2524 | abort (); | |
2525 | break; | |
2526 | ||
2527 | case loc_register: | |
2528 | emit_loc_register (out, loc, indent, target); | |
2529 | break; | |
2530 | ||
2531 | case loc_address: | |
0c254456 | 2532 | case loc_value: |
77de5e9e | 2533 | emit_loc_address (out, loc, indent, target); |
85dfc5c8 RM |
2534 | *used_deref = *used_deref || loc->address.used_deref; |
2535 | if (loc->address.stack_depth > *max_stack) | |
2536 | *max_stack = loc->address.stack_depth; | |
77de5e9e GH |
2537 | break; |
2538 | } | |
2539 | ||
e141cc19 | 2540 | emit ("%*s}\n", --indent * 2, ""); |
77de5e9e GH |
2541 | } |
2542 | ||
2543 | bool | |
85dfc5c8 RM |
2544 | c_emit_location (FILE *out, struct location *loc, int indent, |
2545 | unsigned int *max_stack) | |
77de5e9e GH |
2546 | { |
2547 | emit ("%*s{\n", indent * 2, ""); | |
2548 | ||
ce48dbc7 | 2549 | bool declared_addr = false; |
78f6bba6 FCE |
2550 | struct location *l; |
2551 | for (l = loc; l != NULL; l = l->next) | |
ce48dbc7 RM |
2552 | switch (l->type) |
2553 | { | |
2554 | case loc_decl: | |
2555 | emit ("%s", l->address.program); | |
2556 | break; | |
2557 | ||
2558 | case loc_address: | |
0c254456 | 2559 | case loc_value: |
ce48dbc7 RM |
2560 | if (declared_addr) |
2561 | break; | |
2562 | declared_addr = true; | |
2563 | l->address.declare = "addr"; | |
2564 | case loc_fragment: | |
2565 | case loc_final: | |
2566 | if (l->address.declare != NULL) | |
2567 | { | |
2568 | if (l->byte_size == 0 || l->byte_size == (Dwarf_Word) -1) | |
2569 | emit ("%*s%s %s;\n", (indent + 1) * 2, "", | |
b25c01a1 | 2570 | stack_slot_type (l, false), l->address.declare); |
ce48dbc7 RM |
2571 | else |
2572 | emit ("%*suint%" PRIu64 "_t %s;\n", (indent + 1) * 2, "", | |
2573 | l->byte_size * 8, l->address.declare); | |
2574 | } | |
2575 | ||
2576 | default: | |
2577 | break; | |
2578 | } | |
2579 | ||
77de5e9e | 2580 | bool deref = false; |
85dfc5c8 | 2581 | *max_stack = 0; |
9b9d9778 | 2582 | |
b0b86028 JS |
2583 | for (l = loc; l != NULL; l = l->next) |
2584 | if (l->frame_base != NULL) | |
2585 | { | |
2586 | emit_loc_value (out, l->frame_base, indent, "frame_base", true, | |
2587 | &deref, max_stack); | |
2588 | break; | |
2589 | } | |
9b9d9778 | 2590 | |
ce48dbc7 | 2591 | for (; loc->next != NULL; loc = loc->next) |
77de5e9e GH |
2592 | switch (loc->type) |
2593 | { | |
2594 | case loc_address: | |
0c254456 | 2595 | case loc_value: |
77de5e9e | 2596 | /* Emit the program fragment to calculate the address. */ |
85dfc5c8 | 2597 | emit_loc_value (out, loc, indent + 1, "addr", false, &deref, max_stack); |
ce48dbc7 RM |
2598 | break; |
2599 | ||
2600 | case loc_fragment: | |
2601 | emit ("%s", loc->address.program); | |
77de5e9e GH |
2602 | deref = deref || loc->address.used_deref; |
2603 | break; | |
2604 | ||
ce48dbc7 | 2605 | case loc_decl: |
77de5e9e GH |
2606 | case loc_register: |
2607 | case loc_noncontiguous: | |
707e2bec | 2608 | case loc_constant: |
1220de76 | 2609 | case loc_implicit_pointer: |
77de5e9e GH |
2610 | /* These don't produce any code directly. |
2611 | The next address/final record incorporates the value. */ | |
2612 | break; | |
2613 | ||
2614 | case loc_final: /* Should be last in chain! */ | |
2615 | default: | |
2616 | abort (); | |
2617 | break; | |
2618 | } | |
2619 | ||
2620 | if (loc->type != loc_final) /* Unfinished chain. */ | |
2621 | abort (); | |
2622 | ||
2623 | emit ("%s%*s}\n", loc->address.program, indent * 2, ""); | |
2624 | ||
85dfc5c8 RM |
2625 | if (loc->address.stack_depth > *max_stack) |
2626 | *max_stack = loc->address.stack_depth; | |
2627 | ||
ce48dbc7 | 2628 | return deref || loc->address.used_deref; |
77de5e9e GH |
2629 | } |
2630 | ||
2631 | #undef emit | |
0c5f5812 JS |
2632 | |
2633 | /* vim: set sw=2 ts=8 cino=>4,n-2,{2,^-2,t0,(0,u0,w1,M1 : */ |