Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame^] | 1 | #include <config.h> |
| 2 | #include <inttypes.h> |
| 3 | #include <stdbool.h> |
| 4 | #include <obstack.h> |
| 5 | #include <stdio.h> |
| 6 | #include <stdlib.h> |
| 7 | #include <error.h> |
| 8 | #include <dwarf.h> |
| 9 | #include <libdw.h> |
| 10 | #include <assert.h> |
| 11 | |
| 12 | #define _(x) x |
| 13 | |
| 14 | #define STACK_TYPE "intptr_t" /* Must be the signed type. */ |
| 15 | #define UTYPE "uintptr_t" /* Must be the unsigned type. */ |
| 16 | #define SFORMAT "%" PRId64 "L" |
| 17 | #define UFORMAT "%" PRIu64 "UL" |
| 18 | #define AFORMAT "%#" PRIx64 "UL" |
| 19 | #define STACKFMT "s%u" |
| 20 | |
| 21 | struct location |
| 22 | { |
| 23 | struct location *next; |
| 24 | |
| 25 | const Dwarf_Loc *ops; |
| 26 | size_t nops; |
| 27 | |
| 28 | Dwarf_Word byte_size; |
| 29 | |
| 30 | enum { loc_address, loc_register, loc_noncontiguous, loc_final } type; |
| 31 | union |
| 32 | { |
| 33 | struct /* loc_address or loc_final */ |
| 34 | { |
| 35 | char *program; /* C fragment, leaves address in s0. */ |
| 36 | unsigned int stack_depth; /* Temporaries "s0..<N>" used by it. */ |
| 37 | struct location *frame_base; |
| 38 | bool used_deref; /* Program uses "deref" macro. */ |
| 39 | } address; |
| 40 | unsigned int regno; /* loc_register */ |
| 41 | struct location *pieces; /* loc_noncontiguous */ |
| 42 | }; |
| 43 | }; |
| 44 | |
| 45 | |
| 46 | static const char * |
| 47 | dwarf_diename_integrate (Dwarf_Die *die) |
| 48 | { |
| 49 | Dwarf_Attribute attr_mem; |
| 50 | return dwarf_formstring (dwarf_attr_integrate (die, DW_AT_name, &attr_mem)); |
| 51 | } |
| 52 | |
| 53 | /* Synthesize a new loc_address using the program on the obstack. */ |
| 54 | static struct location * |
| 55 | new_synthetic_loc (struct obstack *pool, struct location *origin, bool deref) |
| 56 | { |
| 57 | obstack_1grow (pool, '\0'); |
| 58 | char *program = obstack_finish (pool); |
| 59 | |
| 60 | struct location *loc = obstack_alloc (pool, sizeof *loc); |
| 61 | loc->next = NULL; |
| 62 | loc->byte_size = 0; |
| 63 | loc->type = loc_address; |
| 64 | loc->address.program = program; |
| 65 | loc->address.stack_depth = 0; |
| 66 | loc->address.frame_base = NULL; |
| 67 | loc->address.used_deref = deref; |
| 68 | |
| 69 | if (origin->type == loc_register) |
| 70 | { |
| 71 | loc->ops = origin->ops; |
| 72 | loc->nops = origin->nops; |
| 73 | } |
| 74 | else |
| 75 | { |
| 76 | loc->ops = NULL; |
| 77 | loc->nops = 0; |
| 78 | } |
| 79 | |
| 80 | return loc; |
| 81 | } |
| 82 | |
| 83 | |
| 84 | /* Die in the middle of an expression. */ |
| 85 | static struct location * |
| 86 | lose (const char *failure, const Dwarf_Loc *lexpr, size_t i) |
| 87 | { |
| 88 | error (2, 0, _("%s in DWARF expression [%Zu] at %" PRIu64 |
| 89 | " (%#x: %" PRId64 ", %" PRId64 ")"), |
| 90 | failure, i, lexpr[i].offset, |
| 91 | lexpr[i].atom, lexpr[i].number, lexpr[i].number2); |
| 92 | return NULL; |
| 93 | } |
| 94 | |
| 95 | /* Translate a (constrained) DWARF expression into C code |
| 96 | emitted to the obstack POOL. INDENT is the number of indentation levels. |
| 97 | ADDRBIAS is the difference between runtime and Dwarf info addresses. |
| 98 | INPUT is null or an expression to be initially pushed on the stack. |
| 99 | If NEED_FB is null, fail on DW_OP_fbreg, else set *NEED_FB to true |
| 100 | and emit "frame_base" for it. On success, set *MAX_STACK to the number |
| 101 | of stack slots required. On failure, set *LOSER to the index in EXPR |
| 102 | of the operation we could not handle. |
| 103 | |
| 104 | Returns a failure message or null for success. */ |
| 105 | |
| 106 | static const char * |
| 107 | translate (struct obstack *pool, int indent, Dwarf_Addr addrbias, |
| 108 | const Dwarf_Loc *expr, const size_t len, |
| 109 | struct location *input, |
| 110 | bool *need_fb, size_t *loser, |
| 111 | struct location *loc) |
| 112 | { |
| 113 | loc->ops = expr; |
| 114 | loc->nops = len; |
| 115 | |
| 116 | #define DIE(msg) return (*loser = i, _(msg)) |
| 117 | |
| 118 | #define emit(fmt, ...) obstack_printf (pool, fmt, ## __VA_ARGS__) |
| 119 | |
| 120 | unsigned int stack_depth = 0, max_stack = 0; |
| 121 | inline void deepen (void) |
| 122 | { |
| 123 | if (stack_depth == max_stack) |
| 124 | ++max_stack; |
| 125 | } |
| 126 | |
| 127 | #define POP(var) \ |
| 128 | if (stack_depth > 0) \ |
| 129 | --stack_depth; \ |
| 130 | else if (tos_register != -1) \ |
| 131 | fetch_tos_register (); \ |
| 132 | else \ |
| 133 | goto underflow; \ |
| 134 | int var = stack_depth |
| 135 | #define PUSH (deepen (), stack_depth++) |
| 136 | #define STACK(idx) (stack_depth - 1 - (idx)) |
| 137 | |
| 138 | /* Don't put stack operations in the arguments to this. */ |
| 139 | #define push(fmt, ...) \ |
| 140 | emit ("%*s" STACKFMT " = " fmt ";\n", indent * 2, "", PUSH, ## __VA_ARGS__) |
| 141 | |
| 142 | int tos_register = -1; |
| 143 | inline void fetch_tos_register (void) |
| 144 | { |
| 145 | deepen (); |
| 146 | emit ("%*s" STACKFMT " = fetch_register (%d);\n", |
| 147 | indent * 2, "", stack_depth, tos_register); |
| 148 | tos_register = -1; |
| 149 | } |
| 150 | |
| 151 | if (input != NULL) |
| 152 | switch (input->type) |
| 153 | { |
| 154 | case loc_address: |
| 155 | push ("addr"); |
| 156 | break; |
| 157 | |
| 158 | case loc_register: |
| 159 | tos_register = input->regno; |
| 160 | break; |
| 161 | |
| 162 | default: |
| 163 | abort (); |
| 164 | break; |
| 165 | } |
| 166 | |
| 167 | size_t i; |
| 168 | |
| 169 | inline const char *finish (struct location *piece) |
| 170 | { |
| 171 | if (stack_depth > 1) |
| 172 | DIE ("multiple values left on stack"); |
| 173 | if (stack_depth == 1) |
| 174 | { |
| 175 | obstack_1grow (pool, '\0'); |
| 176 | char *program = obstack_finish (pool); |
| 177 | piece->type = loc_address; |
| 178 | piece->address.program = program; |
| 179 | piece->address.stack_depth = max_stack; |
| 180 | piece->address.frame_base = NULL; |
| 181 | } |
| 182 | else if (tos_register == -1) |
| 183 | DIE ("stack underflow"); |
| 184 | else if (obstack_object_size (pool) != 0) |
| 185 | DIE ("register value must stand alone in location expression"); |
| 186 | else |
| 187 | { |
| 188 | piece->type = loc_register; |
| 189 | piece->regno = tos_register; |
| 190 | } |
| 191 | return NULL; |
| 192 | } |
| 193 | |
| 194 | struct location *pieces = NULL, **tailpiece = &pieces; |
| 195 | size_t piece_expr_start = 0; |
| 196 | for (i = 0; i < len; ++i) |
| 197 | { |
| 198 | unsigned int reg; |
| 199 | uint_fast8_t sp; |
| 200 | Dwarf_Word value; |
| 201 | |
| 202 | switch (expr[i].atom) |
| 203 | { |
| 204 | /* Basic stack operations. */ |
| 205 | case DW_OP_nop: |
| 206 | break; |
| 207 | |
| 208 | case DW_OP_dup: |
| 209 | if (stack_depth < 1) |
| 210 | goto underflow; |
| 211 | else |
| 212 | { |
| 213 | unsigned int tos = STACK (0); |
| 214 | push (STACKFMT, tos); |
| 215 | } |
| 216 | break; |
| 217 | |
| 218 | case DW_OP_drop: |
| 219 | POP (ignore); |
| 220 | emit ("%*s/* drop " STACKFMT "*/\n", indent * 2, "", ignore); |
| 221 | break; |
| 222 | |
| 223 | case DW_OP_pick: |
| 224 | sp = expr[i].number; |
| 225 | op_pick: |
| 226 | if (sp >= stack_depth) |
| 227 | goto underflow; |
| 228 | sp = STACK (sp); |
| 229 | push (STACKFMT, sp); |
| 230 | break; |
| 231 | |
| 232 | case DW_OP_over: |
| 233 | sp = 1; |
| 234 | goto op_pick; |
| 235 | |
| 236 | case DW_OP_swap: |
| 237 | if (stack_depth < 2) |
| 238 | goto underflow; |
| 239 | deepen (); /* Use a temporary slot. */ |
| 240 | emit ("%*s" |
| 241 | STACKFMT " = " STACKFMT ", " |
| 242 | STACKFMT " = " STACKFMT ", " |
| 243 | STACKFMT " = " STACKFMT ";\n", |
| 244 | indent * 2, "", |
| 245 | STACK (-1), STACK (0), |
| 246 | STACK (0), STACK (1), |
| 247 | STACK (1), STACK (-1)); |
| 248 | break; |
| 249 | |
| 250 | case DW_OP_rot: |
| 251 | if (stack_depth < 3) |
| 252 | goto underflow; |
| 253 | deepen (); /* Use a temporary slot. */ |
| 254 | emit ("%*s" |
| 255 | STACKFMT " = " STACKFMT ", " |
| 256 | STACKFMT " = " STACKFMT ", " |
| 257 | STACKFMT " = " STACKFMT ", " |
| 258 | STACKFMT " = " STACKFMT ";\n", |
| 259 | indent * 2, "", |
| 260 | STACK (-1), STACK (0), |
| 261 | STACK (0), STACK (1), |
| 262 | STACK (1), STACK (2), |
| 263 | STACK (3), STACK (-1)); |
| 264 | break; |
| 265 | |
| 266 | |
| 267 | /* Control flow operations. */ |
| 268 | case DW_OP_skip: |
| 269 | { |
| 270 | Dwarf_Off target = expr[i].offset + 3 + expr[i].number; |
| 271 | while (i + 1 < len && expr[i + 1].offset < target) |
| 272 | ++i; |
| 273 | if (expr[i + 1].offset != target) |
| 274 | DIE ("invalid skip target"); |
| 275 | break; |
| 276 | } |
| 277 | |
| 278 | case DW_OP_bra: |
| 279 | DIE ("conditional branches not supported"); |
| 280 | break; |
| 281 | |
| 282 | |
| 283 | /* Memory access. */ |
| 284 | case DW_OP_deref: |
| 285 | { |
| 286 | POP (addr); |
| 287 | push ("deref (sizeof (void *), " STACKFMT ")", addr); |
| 288 | loc->address.used_deref = true; |
| 289 | } |
| 290 | break; |
| 291 | |
| 292 | case DW_OP_deref_size: |
| 293 | { |
| 294 | POP (addr); |
| 295 | push ("deref (" UFORMAT ", " STACKFMT ")", |
| 296 | expr[i].number, addr); |
| 297 | loc->address.used_deref = true; |
| 298 | } |
| 299 | break; |
| 300 | |
| 301 | case DW_OP_xderef: |
| 302 | { |
| 303 | POP (addr); |
| 304 | POP (as); |
| 305 | push ("xderef (sizeof (void *), " STACKFMT ", " STACKFMT ")", |
| 306 | addr, as); |
| 307 | loc->address.used_deref = true; |
| 308 | } |
| 309 | break; |
| 310 | |
| 311 | case DW_OP_xderef_size: |
| 312 | { |
| 313 | POP (addr); |
| 314 | POP (as); |
| 315 | push ("xderef (" UFORMAT ", " STACKFMT ", " STACKFMT ")", |
| 316 | expr[i].number, addr, as); |
| 317 | loc->address.used_deref = true; |
| 318 | } |
| 319 | break; |
| 320 | |
| 321 | /* Constant-value operations. */ |
| 322 | |
| 323 | case DW_OP_addr: |
| 324 | push (AFORMAT, addrbias + expr[i].number); |
| 325 | break; |
| 326 | |
| 327 | case DW_OP_lit0 ... DW_OP_lit31: |
| 328 | value = expr[i].atom - DW_OP_lit0; |
| 329 | goto op_const; |
| 330 | |
| 331 | case DW_OP_const1u: |
| 332 | case DW_OP_const1s: |
| 333 | case DW_OP_const2u: |
| 334 | case DW_OP_const2s: |
| 335 | case DW_OP_const4u: |
| 336 | case DW_OP_const4s: |
| 337 | case DW_OP_const8u: |
| 338 | case DW_OP_const8s: |
| 339 | case DW_OP_constu: |
| 340 | case DW_OP_consts: |
| 341 | value = expr[i].number; |
| 342 | op_const: |
| 343 | push (SFORMAT, value); |
| 344 | break; |
| 345 | |
| 346 | /* Arithmetic operations. */ |
| 347 | #define UNOP(dw_op, c_op) \ |
| 348 | case DW_OP_##dw_op: \ |
| 349 | { \ |
| 350 | POP (tos); \ |
| 351 | push ("%s (" STACKFMT ")", #c_op, tos); \ |
| 352 | } \ |
| 353 | break |
| 354 | #define BINOP(dw_op, c_op) \ |
| 355 | case DW_OP_##dw_op: \ |
| 356 | { \ |
| 357 | POP (b); \ |
| 358 | POP (a); \ |
| 359 | push (STACKFMT " %s " STACKFMT, a, #c_op, b); \ |
| 360 | } \ |
| 361 | break |
| 362 | |
| 363 | UNOP (abs, op_abs); |
| 364 | BINOP (and, &); |
| 365 | BINOP (div, /); |
| 366 | BINOP (minus, -); |
| 367 | BINOP (mod, %); |
| 368 | BINOP (mul, *); |
| 369 | UNOP (neg, -); |
| 370 | UNOP (not, ~); |
| 371 | BINOP (or, |); |
| 372 | BINOP (plus, +); |
| 373 | BINOP (shl, <<); |
| 374 | BINOP (shra, >>); |
| 375 | BINOP (xor, ^); |
| 376 | |
| 377 | /* Comparisons are binary operators too. */ |
| 378 | BINOP (le, <=); |
| 379 | BINOP (ge, >=); |
| 380 | BINOP (eq, ==); |
| 381 | BINOP (lt, <); |
| 382 | BINOP (gt, >); |
| 383 | BINOP (ne, !=); |
| 384 | |
| 385 | #undef UNOP |
| 386 | #undef BINOP |
| 387 | |
| 388 | case DW_OP_shr: |
| 389 | { |
| 390 | POP (b); |
| 391 | POP (a); |
| 392 | push ("(%s) " STACKFMT " >> (%s)" STACKFMT, |
| 393 | UTYPE, a, UTYPE, b); |
| 394 | break; |
| 395 | } |
| 396 | |
| 397 | case DW_OP_plus_uconst: |
| 398 | { |
| 399 | POP (x); |
| 400 | push (STACKFMT " + " UFORMAT, x, expr[i].number); |
| 401 | } |
| 402 | break; |
| 403 | |
| 404 | |
| 405 | /* Register-relative addressing. */ |
| 406 | case DW_OP_breg0 ... DW_OP_breg31: |
| 407 | reg = expr[i].atom - DW_OP_breg0; |
| 408 | value = expr[i].number; |
| 409 | goto op_breg; |
| 410 | |
| 411 | case DW_OP_bregx: |
| 412 | reg = expr[i].number; |
| 413 | value = expr[i].number2; |
| 414 | op_breg: |
| 415 | push ("fetch_register (%u) + " SFORMAT, reg, value); |
| 416 | break; |
| 417 | |
| 418 | case DW_OP_fbreg: |
| 419 | if (need_fb == NULL) |
| 420 | DIE ("DW_OP_fbreg from DW_AT_frame_base"); |
| 421 | *need_fb = true; |
| 422 | push ("frame_base + " SFORMAT, expr[i].number); |
| 423 | break; |
| 424 | |
| 425 | /* Direct register contents. */ |
| 426 | case DW_OP_reg0 ... DW_OP_reg31: |
| 427 | reg = expr[i].atom - DW_OP_reg0; |
| 428 | goto op_reg; |
| 429 | |
| 430 | case DW_OP_regx: |
| 431 | reg = expr[i].number; |
| 432 | op_reg: |
| 433 | tos_register = reg; |
| 434 | break; |
| 435 | |
| 436 | /* Special magic. */ |
| 437 | case DW_OP_piece: |
| 438 | if (stack_depth > 1) |
| 439 | /* If this ever happens we could copy the program. */ |
| 440 | DIE ("DW_OP_piece left multiple values on stack"); |
| 441 | else |
| 442 | { |
| 443 | struct location *piece = obstack_alloc (pool, sizeof *piece); |
| 444 | const char *failure = finish (piece); |
| 445 | if (unlikely (failure != NULL)) |
| 446 | return failure; |
| 447 | |
| 448 | piece->ops = &expr[piece_expr_start]; |
| 449 | piece->nops = i - piece_expr_start; |
| 450 | piece_expr_start = i + 1; |
| 451 | |
| 452 | piece->byte_size = expr[i].number; |
| 453 | |
| 454 | *tailpiece = piece; |
| 455 | tailpiece = &piece->next; |
| 456 | piece->next = NULL; |
| 457 | } |
| 458 | break; |
| 459 | |
| 460 | case DW_OP_push_object_address: |
| 461 | DIE ("XXX DW_OP_push_object_address"); |
| 462 | break; |
| 463 | |
| 464 | default: |
| 465 | DIE ("unrecognized operation"); |
| 466 | break; |
| 467 | } |
| 468 | } |
| 469 | |
| 470 | if (likely (pieces == NULL)) |
| 471 | return finish (loc); |
| 472 | |
| 473 | if (piece_expr_start != i) |
| 474 | DIE ("extra operations after last DW_OP_piece"); |
| 475 | |
| 476 | loc->type = loc_noncontiguous; |
| 477 | loc->pieces = pieces; |
| 478 | |
| 479 | return NULL; |
| 480 | |
| 481 | underflow: |
| 482 | DIE ("stack underflow"); |
| 483 | |
| 484 | #undef emit |
| 485 | #undef push |
| 486 | #undef PUSH |
| 487 | #undef POP |
| 488 | #undef STACK |
| 489 | #undef DIE |
| 490 | } |
| 491 | |
| 492 | /* Translate a location starting from an address or nothing. */ |
| 493 | static struct location * |
| 494 | location_from_address (struct obstack *pool, |
| 495 | int indent, Dwarf_Addr dwbias, |
| 496 | const Dwarf_Loc *expr, size_t len, Dwarf_Addr address, |
| 497 | struct location **input, Dwarf_Attribute *fb_attr) |
| 498 | { |
| 499 | bool need_fb = false; |
| 500 | size_t loser; |
| 501 | struct location *loc = obstack_alloc (pool, sizeof *loc); |
| 502 | const char *failure = translate (pool, indent + 1, dwbias, expr, len, |
| 503 | *input, &need_fb, &loser, loc); |
| 504 | if (unlikely (failure != NULL)) |
| 505 | return lose (failure, expr, loser); |
| 506 | |
| 507 | loc->next = NULL; |
| 508 | if (need_fb) |
| 509 | { |
| 510 | /* The main expression uses DW_OP_fbreg, so we need to compute |
| 511 | the DW_AT_frame_base attribute expression's value first. */ |
| 512 | |
| 513 | Dwarf_Loc *fb_expr; |
| 514 | size_t fb_len; |
| 515 | switch (dwarf_addrloclists (fb_attr, address - dwbias, |
| 516 | &fb_expr, &fb_len, 1)) |
| 517 | { |
| 518 | case 1: /* Should always happen. */ |
| 519 | if (fb_len == 0) |
| 520 | goto fb_inaccessible; |
| 521 | break; |
| 522 | |
| 523 | default: /* Shouldn't happen. */ |
| 524 | case -1: |
| 525 | error (2, 0, "dwarf_addrloclists (form %#x): %s", |
| 526 | dwarf_whatform (fb_attr), dwarf_errmsg (-1)); |
| 527 | return NULL; |
| 528 | |
| 529 | case 0: /* Shouldn't happen. */ |
| 530 | fb_inaccessible: |
| 531 | error (2, 0, "DW_AT_frame_base not accessible at this address"); |
| 532 | return NULL; |
| 533 | } |
| 534 | |
| 535 | loc->address.frame_base = obstack_alloc (pool, sizeof *loc); |
| 536 | failure = translate (pool, indent + 1, dwbias, fb_expr, fb_len, NULL, |
| 537 | NULL, &loser, loc->address.frame_base); |
| 538 | if (unlikely (failure != NULL)) |
| 539 | return lose (failure, fb_expr, loser); |
| 540 | } |
| 541 | |
| 542 | if (*input != NULL) |
| 543 | (*input)->next = loc; |
| 544 | *input = loc; |
| 545 | |
| 546 | return loc; |
| 547 | } |
| 548 | |
| 549 | /* Translate a location starting from a non-address "on the top of the |
| 550 | stack". The *INPUT location is a register name or noncontiguous |
| 551 | object specification, and this expression wants to find the "address" |
| 552 | of an object relative to that "address". */ |
| 553 | |
| 554 | static struct location * |
| 555 | location_relative (struct obstack *pool, |
| 556 | int indent, Dwarf_Addr dwbias, |
| 557 | const Dwarf_Loc *expr, size_t len, Dwarf_Addr address, |
| 558 | struct location **input, Dwarf_Attribute *fb_attr) |
| 559 | { |
| 560 | Dwarf_Sword *stack; |
| 561 | unsigned int stack_depth = 0, max_stack = 0; |
| 562 | inline void deepen (void) |
| 563 | { |
| 564 | if (stack_depth == max_stack) |
| 565 | { |
| 566 | ++max_stack; |
| 567 | obstack_blank (pool, sizeof stack[0]); |
| 568 | stack = (void *) obstack_base (pool); |
| 569 | } |
| 570 | } |
| 571 | |
| 572 | #define POP(var) \ |
| 573 | if (stack_depth > 0) \ |
| 574 | --stack_depth; \ |
| 575 | else \ |
| 576 | goto underflow; \ |
| 577 | int var = stack_depth |
| 578 | #define PUSH (deepen (), stack_depth++) |
| 579 | #define STACK(idx) (stack_depth - 1 - (idx)) |
| 580 | #define STACKWORD(idx) stack[STACK (idx)] |
| 581 | |
| 582 | /* Don't put stack operations in the arguments to this. */ |
| 583 | #define push(value) (stack[PUSH] = (value)) |
| 584 | |
| 585 | const char *failure = NULL; |
| 586 | #define DIE(msg) do { failure = _(msg); goto fail; } while (0) |
| 587 | |
| 588 | struct location *head = NULL; |
| 589 | size_t i; |
| 590 | for (i = 0; i < len; ++i) |
| 591 | { |
| 592 | uint_fast8_t sp; |
| 593 | Dwarf_Word value; |
| 594 | |
| 595 | switch (expr[i].atom) |
| 596 | { |
| 597 | /* Basic stack operations. */ |
| 598 | case DW_OP_nop: |
| 599 | break; |
| 600 | |
| 601 | case DW_OP_dup: |
| 602 | if (stack_depth < 1) |
| 603 | goto underflow; |
| 604 | else |
| 605 | { |
| 606 | unsigned int tos = STACK (0); |
| 607 | push (stack[tos]); |
| 608 | } |
| 609 | break; |
| 610 | |
| 611 | case DW_OP_drop: |
| 612 | if (stack_depth > 0) |
| 613 | --stack_depth; |
| 614 | else if (*input != NULL) |
| 615 | /* Mark that we have consumed the input. */ |
| 616 | *input = NULL; |
| 617 | else |
| 618 | /* Hits if cleared above, or if we had no input at all. */ |
| 619 | goto underflow; |
| 620 | break; |
| 621 | |
| 622 | case DW_OP_pick: |
| 623 | sp = expr[i].number; |
| 624 | op_pick: |
| 625 | if (sp >= stack_depth) |
| 626 | goto underflow; |
| 627 | sp = STACK (sp); |
| 628 | push (stack[sp]); |
| 629 | break; |
| 630 | |
| 631 | case DW_OP_over: |
| 632 | sp = 1; |
| 633 | goto op_pick; |
| 634 | |
| 635 | case DW_OP_swap: |
| 636 | if (stack_depth < 2) |
| 637 | goto underflow; |
| 638 | deepen (); /* Use a temporary slot. */ |
| 639 | STACKWORD (-1) = STACKWORD (0); |
| 640 | STACKWORD (0) = STACKWORD (1); |
| 641 | STACKWORD (1) = STACKWORD (-1); |
| 642 | break; |
| 643 | |
| 644 | case DW_OP_rot: |
| 645 | if (stack_depth < 3) |
| 646 | goto underflow; |
| 647 | deepen (); /* Use a temporary slot. */ |
| 648 | STACKWORD (-1) = STACKWORD (0); |
| 649 | STACKWORD (0) = STACKWORD (1); |
| 650 | STACKWORD (2) = STACKWORD (2); |
| 651 | STACKWORD (2) = STACKWORD (-1); |
| 652 | break; |
| 653 | |
| 654 | |
| 655 | /* Control flow operations. */ |
| 656 | case DW_OP_bra: |
| 657 | { |
| 658 | POP (taken); |
| 659 | if (stack[taken] == 0) |
| 660 | break; |
| 661 | } |
| 662 | /*FALLTHROUGH*/ |
| 663 | |
| 664 | case DW_OP_skip: |
| 665 | { |
| 666 | Dwarf_Off target = expr[i].offset + 3 + expr[i].number; |
| 667 | while (i + 1 < len && expr[i + 1].offset < target) |
| 668 | ++i; |
| 669 | if (expr[i + 1].offset != target) |
| 670 | DIE ("invalid skip target"); |
| 671 | break; |
| 672 | } |
| 673 | |
| 674 | /* Memory access. */ |
| 675 | case DW_OP_deref: |
| 676 | case DW_OP_deref_size: |
| 677 | case DW_OP_xderef: |
| 678 | case DW_OP_xderef_size: |
| 679 | |
| 680 | /* Register-relative addressing. */ |
| 681 | case DW_OP_breg0 ... DW_OP_breg31: |
| 682 | case DW_OP_bregx: |
| 683 | case DW_OP_fbreg: |
| 684 | |
| 685 | /* This started from a register, but now it's following a pointer. |
| 686 | So we can do the translation starting from address here. */ |
| 687 | return location_from_address (pool, indent, dwbias, |
| 688 | expr, len, address, input, fb_attr); |
| 689 | |
| 690 | |
| 691 | /* Constant-value operations. */ |
| 692 | case DW_OP_addr: |
| 693 | push (dwbias + expr[i].number); |
| 694 | break; |
| 695 | |
| 696 | case DW_OP_lit0 ... DW_OP_lit31: |
| 697 | value = expr[i].atom - DW_OP_lit0; |
| 698 | goto op_const; |
| 699 | |
| 700 | case DW_OP_const1u: |
| 701 | case DW_OP_const1s: |
| 702 | case DW_OP_const2u: |
| 703 | case DW_OP_const2s: |
| 704 | case DW_OP_const4u: |
| 705 | case DW_OP_const4s: |
| 706 | case DW_OP_const8u: |
| 707 | case DW_OP_const8s: |
| 708 | case DW_OP_constu: |
| 709 | case DW_OP_consts: |
| 710 | value = expr[i].number; |
| 711 | op_const: |
| 712 | push (value); |
| 713 | break; |
| 714 | |
| 715 | /* Arithmetic operations. */ |
| 716 | #define UNOP(dw_op, c_op) \ |
| 717 | case DW_OP_##dw_op: \ |
| 718 | { \ |
| 719 | POP (tos); \ |
| 720 | push (c_op (stack[tos])); \ |
| 721 | } \ |
| 722 | break |
| 723 | #define BINOP(dw_op, c_op) \ |
| 724 | case DW_OP_##dw_op: \ |
| 725 | { \ |
| 726 | POP (b); \ |
| 727 | POP (a); \ |
| 728 | push (stack[a] c_op stack[b]); \ |
| 729 | } \ |
| 730 | break |
| 731 | |
| 732 | #define op_abs(x) (x < 0 ? -x : x) |
| 733 | UNOP (abs, op_abs); |
| 734 | BINOP (and, &); |
| 735 | BINOP (div, /); |
| 736 | BINOP (mod, %); |
| 737 | BINOP (mul, *); |
| 738 | UNOP (neg, -); |
| 739 | UNOP (not, ~); |
| 740 | BINOP (or, |); |
| 741 | BINOP (shl, <<); |
| 742 | BINOP (shra, >>); |
| 743 | BINOP (xor, ^); |
| 744 | |
| 745 | /* Comparisons are binary operators too. */ |
| 746 | BINOP (le, <=); |
| 747 | BINOP (ge, >=); |
| 748 | BINOP (eq, ==); |
| 749 | BINOP (lt, <); |
| 750 | BINOP (gt, >); |
| 751 | BINOP (ne, !=); |
| 752 | |
| 753 | #undef UNOP |
| 754 | #undef BINOP |
| 755 | |
| 756 | case DW_OP_shr: |
| 757 | { |
| 758 | POP (b); |
| 759 | POP (a); |
| 760 | push ((Dwarf_Word) stack[a] >> (Dwarf_Word) stack[b]); |
| 761 | break; |
| 762 | } |
| 763 | |
| 764 | /* Simple addition we may be able to handle relative to |
| 765 | the starting register name. */ |
| 766 | case DW_OP_minus: |
| 767 | { |
| 768 | POP (tos); |
| 769 | value = -stack[tos]; |
| 770 | goto plus; |
| 771 | } |
| 772 | case DW_OP_plus: |
| 773 | { |
| 774 | POP (tos); |
| 775 | value = stack[tos]; |
| 776 | goto plus; |
| 777 | } |
| 778 | case DW_OP_plus_uconst: |
| 779 | value = expr[i].number; |
| 780 | plus: |
| 781 | if (stack_depth > 0) |
| 782 | { |
| 783 | /* It's just private diddling after all. */ |
| 784 | POP (a); |
| 785 | push (stack[a] + value); |
| 786 | break; |
| 787 | } |
| 788 | if (*input == NULL) |
| 789 | goto underflow; |
| 790 | |
| 791 | /* This is the primary real-world case: the expression takes |
| 792 | the input address and adds a constant offset. */ |
| 793 | |
| 794 | while ((*input)->type == loc_noncontiguous) |
| 795 | { |
| 796 | /* We are starting from a noncontiguous object (DW_OP_piece). |
| 797 | Find the piece we want. */ |
| 798 | |
| 799 | struct location *piece = (*input)->pieces; |
| 800 | while (piece != NULL && value >= piece->byte_size) |
| 801 | { |
| 802 | value -= piece->byte_size; |
| 803 | piece = piece->next; |
| 804 | } |
| 805 | if (piece == NULL) |
| 806 | DIE ("offset outside available pieces"); |
| 807 | |
| 808 | *input = piece; |
| 809 | } |
| 810 | |
| 811 | switch ((*input)->type) |
| 812 | { |
| 813 | case loc_address: |
| 814 | { |
| 815 | /* The piece we want is actually in memory. Use the same |
| 816 | program to compute the address from the preceding input. */ |
| 817 | |
| 818 | struct location *loc = obstack_alloc (pool, sizeof *loc); |
| 819 | *loc = **input; |
| 820 | if (head == NULL) |
| 821 | head = loc; |
| 822 | (*input)->next = loc; |
| 823 | if (value == 0) |
| 824 | { |
| 825 | /* The piece addresses exactly where we want to go. */ |
| 826 | loc->next = NULL; |
| 827 | *input = loc; |
| 828 | } |
| 829 | else |
| 830 | { |
| 831 | /* Add a second fragment to offset the piece address. */ |
| 832 | obstack_printf (pool, "%*saddr += " SFORMAT "\n", |
| 833 | indent * 2, "", value); |
| 834 | *input = loc->next = new_synthetic_loc (pool, *input, |
| 835 | false); |
| 836 | } |
| 837 | |
| 838 | if (i + 1 < len) |
| 839 | { |
| 840 | /* This expression keeps going, but further |
| 841 | computations now have an address to start with. |
| 842 | So we can punt to the address computation generator. */ |
| 843 | loc = location_from_address (pool, indent, dwbias, |
| 844 | &expr[i + 1], len - i - 1, |
| 845 | address, input, fb_attr); |
| 846 | if (loc == NULL) |
| 847 | return NULL; |
| 848 | } |
| 849 | |
| 850 | /* That's all she wrote. */ |
| 851 | return head; |
| 852 | } |
| 853 | |
| 854 | case loc_register: |
| 855 | // XXX |
| 856 | |
| 857 | default: |
| 858 | abort (); |
| 859 | } |
| 860 | break; |
| 861 | |
| 862 | /* Direct register contents. */ |
| 863 | case DW_OP_reg0 ... DW_OP_reg31: |
| 864 | case DW_OP_regx: |
| 865 | DIE ("register"); |
| 866 | break; |
| 867 | |
| 868 | /* Special magic. */ |
| 869 | case DW_OP_piece: |
| 870 | DIE ("DW_OP_piece"); |
| 871 | break; |
| 872 | |
| 873 | case DW_OP_push_object_address: |
| 874 | DIE ("XXX DW_OP_push_object_address"); |
| 875 | break; |
| 876 | |
| 877 | default: |
| 878 | DIE ("unrecognized operation"); |
| 879 | break; |
| 880 | } |
| 881 | } |
| 882 | |
| 883 | if (stack_depth > 1) |
| 884 | DIE ("multiple values left on stack"); |
| 885 | |
| 886 | if (stack_depth > 0) /* stack_depth == 1 */ |
| 887 | { |
| 888 | if (*input != NULL) |
| 889 | DIE ("multiple values left on stack"); |
| 890 | |
| 891 | /* Could handle this if it ever actually happened. */ |
| 892 | DIE ("relative expression computed constant"); |
| 893 | } |
| 894 | |
| 895 | return head; |
| 896 | |
| 897 | underflow: |
| 898 | if (*input == NULL) |
| 899 | DIE ("stack underflow"); |
| 900 | else |
| 901 | DIE ("cannot handle location expression"); |
| 902 | |
| 903 | fail: |
| 904 | return lose (failure, expr, i); |
| 905 | } |
| 906 | |
| 907 | |
| 908 | /* Translate a C fragment for the location expression, using *INPUT |
| 909 | as the starting location, begin from scratch if *INPUT is null. |
| 910 | If DW_OP_fbreg is used, it may have a subfragment computing from |
| 911 | the FB_ATTR location expression. |
| 912 | |
| 913 | On errors, exit and never return (XXX ?). On success, return the |
| 914 | first fragment created, which is also chained onto (*INPUT)->next. |
| 915 | *INPUT is then updated with the new tail of that chain. */ |
| 916 | |
| 917 | struct location * |
| 918 | c_translate_location (struct obstack *pool, |
| 919 | int indent, Dwarf_Addr dwbias, |
| 920 | Dwarf_Attribute *loc_attr, Dwarf_Addr address, |
| 921 | struct location **input, Dwarf_Attribute *fb_attr) |
| 922 | { |
| 923 | Dwarf_Loc *expr; |
| 924 | size_t len; |
| 925 | switch (dwarf_addrloclists (loc_attr, address - dwbias, &expr, &len, 1)) |
| 926 | { |
| 927 | case 1: /* Should always happen. */ |
| 928 | if (len == 0) |
| 929 | goto inaccessible; |
| 930 | break; |
| 931 | |
| 932 | default: /* Shouldn't happen. */ |
| 933 | case -1: |
| 934 | error (2, 0, "dwarf_addrloclists (form %#x): %s", |
| 935 | dwarf_whatform (fb_attr), dwarf_errmsg (-1)); |
| 936 | return NULL; |
| 937 | |
| 938 | case 0: /* Shouldn't happen. */ |
| 939 | inaccessible: |
| 940 | error (2, 0, "not accessible at this address"); |
| 941 | return NULL; |
| 942 | } |
| 943 | |
| 944 | ++indent; |
| 945 | switch (*input == NULL ? loc_address : (*input)->type) |
| 946 | { |
| 947 | case loc_address: |
| 948 | /* We have a previous address computation. |
| 949 | This expression will compute starting with that on the stack. */ |
| 950 | return location_from_address (pool, indent, dwbias, expr, len, address, |
| 951 | input, fb_attr); |
| 952 | |
| 953 | case loc_noncontiguous: |
| 954 | case loc_register: |
| 955 | /* The starting point is not an address computation, but a |
| 956 | register. We can only handle limited computations from here. */ |
| 957 | return location_relative (pool, indent, dwbias, expr, len, address, |
| 958 | input, fb_attr); |
| 959 | |
| 960 | case loc_final: /* Bogus caller. */ |
| 961 | default: |
| 962 | abort (); |
| 963 | break; |
| 964 | } |
| 965 | |
| 966 | return NULL; |
| 967 | } |
| 968 | |
| 969 | |
| 970 | /* Emit "uintNN_t TARGET = ...;". */ |
| 971 | static bool |
| 972 | emit_base_fetch (struct obstack *pool, Dwarf_Word byte_size, |
| 973 | const char *target, bool decl, struct location *loc) |
| 974 | { |
| 975 | if (decl) |
| 976 | obstack_printf (pool, "uint%" PRIu64 "_t ", byte_size * 8); |
| 977 | |
| 978 | switch (loc->type) |
| 979 | { |
| 980 | case loc_address: |
| 981 | if (byte_size != 0 && byte_size != (Dwarf_Word) -1) |
| 982 | obstack_printf (pool, "%s = deref (%" PRIu64 ", addr); ", |
| 983 | target, byte_size); |
| 984 | else |
| 985 | obstack_printf (pool, "%s = deref (sizeof %s, addr); ", |
| 986 | target, target); |
| 987 | return true; |
| 988 | |
| 989 | case loc_register: |
| 990 | obstack_printf (pool, "%s = fetch_register (%u);", target, loc->regno); |
| 991 | break; |
| 992 | |
| 993 | case loc_noncontiguous: |
| 994 | /* Could be handled if it ever happened. */ |
| 995 | error (2, 0, _("noncontiguous locations not supported")); |
| 996 | break; |
| 997 | |
| 998 | default: |
| 999 | abort (); |
| 1000 | break; |
| 1001 | } |
| 1002 | |
| 1003 | return false; |
| 1004 | } |
| 1005 | |
| 1006 | /* Translate a fragment to dereference the given pointer type, |
| 1007 | where *INPUT is the location of the pointer with that type. |
| 1008 | |
| 1009 | We chain on a loc_address program that yields this pointer value |
| 1010 | (i.e. the location of what it points to). */ |
| 1011 | |
| 1012 | void |
| 1013 | c_translate_pointer (struct obstack *pool, int indent, |
| 1014 | Dwarf_Addr dwbias __attribute__ ((unused)), |
| 1015 | Dwarf_Die *typedie, struct location **input) |
| 1016 | { |
| 1017 | obstack_printf (pool, "%*s{ ", (indent + 2) * 2, ""); |
| 1018 | |
| 1019 | bool deref = false; |
| 1020 | Dwarf_Attribute attr_mem; |
| 1021 | Dwarf_Word byte_size; |
| 1022 | if (dwarf_attr_integrate (typedie, DW_AT_byte_size, &attr_mem) == NULL) |
| 1023 | { |
| 1024 | obstack_printf (pool, "uintptr_t "); |
| 1025 | emit_base_fetch (pool, 0, "tmp", false, *input); |
| 1026 | } |
| 1027 | else if (dwarf_formudata (&attr_mem, &byte_size) != 0) |
| 1028 | error (2, 0, |
| 1029 | _("cannot get byte_size attribute for type %s: %s"), |
| 1030 | dwarf_diename_integrate (typedie) ?: "<anonymous>", |
| 1031 | dwarf_errmsg (-1)); |
| 1032 | else |
| 1033 | deref = emit_base_fetch (pool, byte_size, "tmp", true, *input); |
| 1034 | |
| 1035 | obstack_printf (pool, " addr = tmp; }\n"); |
| 1036 | |
| 1037 | struct location *loc = new_synthetic_loc (pool, *input, deref); |
| 1038 | (*input)->next = loc; |
| 1039 | *input = loc; |
| 1040 | } |
| 1041 | |
| 1042 | /* Determine the byte size of a base type. */ |
| 1043 | static Dwarf_Word |
| 1044 | base_byte_size (Dwarf_Die *typedie) |
| 1045 | { |
| 1046 | Dwarf_Attribute attr_mem; |
| 1047 | Dwarf_Word size; |
| 1048 | if (dwarf_attr_integrate (typedie, DW_AT_byte_size, &attr_mem) != NULL |
| 1049 | && dwarf_formudata (&attr_mem, &size) == 0) |
| 1050 | return size; |
| 1051 | |
| 1052 | error (2, 0, |
| 1053 | _("cannot get byte_size attribute for type %s: %s"), |
| 1054 | dwarf_diename_integrate (typedie) ?: "<anonymous>", |
| 1055 | dwarf_errmsg (-1)); |
| 1056 | return -1; |
| 1057 | } |
| 1058 | |
| 1059 | /* Emit a code fragment like: |
| 1060 | { uintNN_t tmp = ...; S1 S2 S3, tmp, B0, Bn); } |
| 1061 | */ |
| 1062 | static void |
| 1063 | emit_bitfield (struct obstack *pool, int indent, |
| 1064 | Dwarf_Die *die, Dwarf_Word byte_size, struct location *loc, |
| 1065 | const char *s1, const char *s2, const char *s3) |
| 1066 | { |
| 1067 | Dwarf_Word bit_offset, bit_size; |
| 1068 | Dwarf_Attribute attr_mem; |
| 1069 | if (dwarf_attr_integrate (die, DW_AT_bit_offset, &attr_mem) == NULL |
| 1070 | || dwarf_formudata (&attr_mem, &bit_offset) != 0 |
| 1071 | || dwarf_attr_integrate (die, DW_AT_bit_size, &attr_mem) == NULL |
| 1072 | || dwarf_formudata (&attr_mem, &bit_size) != 0) |
| 1073 | error (2, 0, _("cannot get bit field parameters: %s"), |
| 1074 | dwarf_errmsg (-1)); |
| 1075 | |
| 1076 | /* Emit "{ uintNN_t tmp = ...;" to fetch the base type. */ |
| 1077 | |
| 1078 | obstack_printf (pool, "%*s{ ", indent * 2, ""); |
| 1079 | emit_base_fetch (pool, byte_size, "tmp", true, loc); |
| 1080 | |
| 1081 | obstack_printf (pool, "%s%s%s, tmp, %" PRIu64 ", %" PRIu64 "); }\n", |
| 1082 | s1, s2, s3, bit_offset, bit_size); |
| 1083 | } |
| 1084 | |
| 1085 | /* Translate a fragment to fetch the value of variable or member DIE |
| 1086 | at the *INPUT location and store it in variable TARGET. */ |
| 1087 | |
| 1088 | void |
| 1089 | c_translate_fetch (struct obstack *pool, int indent, |
| 1090 | Dwarf_Addr dwbias __attribute__ ((unused)), |
| 1091 | Dwarf_Die *die, Dwarf_Attribute *typeattr, |
| 1092 | struct location **input, const char *target) |
| 1093 | { |
| 1094 | ++indent; |
| 1095 | |
| 1096 | Dwarf_Attribute size_attr; |
| 1097 | Dwarf_Word byte_size; |
| 1098 | if (dwarf_attr_integrate (die, DW_AT_byte_size, &size_attr) == NULL |
| 1099 | || dwarf_formudata (&size_attr, &byte_size) != 0) |
| 1100 | { |
| 1101 | Dwarf_Die basedie; |
| 1102 | if (dwarf_formref_die (typeattr, &basedie) == NULL) |
| 1103 | error (2, 0, _("cannot get type of field: %s"), dwarf_errmsg (-1)); |
| 1104 | byte_size = base_byte_size (&basedie); |
| 1105 | } |
| 1106 | |
| 1107 | bool deref = false; |
| 1108 | if (dwarf_hasattr_integrate (die, DW_AT_bit_offset)) |
| 1109 | /* This is a bit field. */ |
| 1110 | emit_bitfield (pool, indent, die, byte_size, *input, |
| 1111 | "fetch_bitfield (", target, ""); |
| 1112 | else |
| 1113 | switch (byte_size) |
| 1114 | { |
| 1115 | case 1: |
| 1116 | case 2: |
| 1117 | case 4: |
| 1118 | case 8: |
| 1119 | obstack_printf (pool, "%*s", indent * 2, ""); |
| 1120 | deref = emit_base_fetch (pool, byte_size, target, false, *input); |
| 1121 | obstack_printf (pool, "\n"); |
| 1122 | break; |
| 1123 | |
| 1124 | default: |
| 1125 | /* Could handle this generating call to memcpy equivalent. */ |
| 1126 | error (2, 0, _("fetch is larger than base integer types")); |
| 1127 | break; |
| 1128 | } |
| 1129 | |
| 1130 | struct location *loc = new_synthetic_loc (pool, *input, deref); |
| 1131 | loc->type = loc_final; |
| 1132 | (*input)->next = loc; |
| 1133 | *input = loc; |
| 1134 | } |
| 1135 | |
| 1136 | void |
| 1137 | c_translate_addressof (struct obstack *pool, int indent, |
| 1138 | Dwarf_Addr dwbias __attribute__ ((unused)), |
| 1139 | Dwarf_Die *die, |
| 1140 | Dwarf_Attribute *typeattr __attribute__ ((unused)), |
| 1141 | struct location **input, const char *target) |
| 1142 | { |
| 1143 | ++indent; |
| 1144 | |
| 1145 | if (dwarf_hasattr_integrate (die, DW_AT_bit_offset)) |
| 1146 | error (2, 0, _("cannot take the address of a bit field")); |
| 1147 | |
| 1148 | switch ((*input)->type) |
| 1149 | { |
| 1150 | case loc_address: |
| 1151 | obstack_printf (pool, "%*s%s = addr;\n", indent * 2, "", target); |
| 1152 | (*input)->next = new_synthetic_loc (pool, *input, false); |
| 1153 | (*input)->next->type = loc_final; |
| 1154 | break; |
| 1155 | |
| 1156 | case loc_register: |
| 1157 | error (2, 0, _("cannot take address of object in register")); |
| 1158 | break; |
| 1159 | case loc_noncontiguous: |
| 1160 | error (2, 0, _("cannot take address of noncontiguous object")); |
| 1161 | break; |
| 1162 | |
| 1163 | default: |
| 1164 | abort(); |
| 1165 | break; |
| 1166 | } |
| 1167 | } |
| 1168 | |
| 1169 | |
| 1170 | /* Determine the element stride of an array type. */ |
| 1171 | static Dwarf_Word |
| 1172 | array_stride (Dwarf_Die *typedie) |
| 1173 | { |
| 1174 | Dwarf_Attribute attr_mem; |
| 1175 | if (dwarf_attr_integrate (typedie, DW_AT_stride_size, &attr_mem) != NULL) |
| 1176 | { |
| 1177 | Dwarf_Word stride; |
| 1178 | if (dwarf_formudata (&attr_mem, &stride) == 0) |
| 1179 | return stride; |
| 1180 | error (2, 0, _("cannot get stride_size attribute array type %s: %s"), |
| 1181 | dwarf_diename_integrate (typedie) ?: "<anonymous>", |
| 1182 | dwarf_errmsg (-1)); |
| 1183 | } |
| 1184 | |
| 1185 | Dwarf_Die die_mem; |
| 1186 | if (dwarf_attr_integrate (typedie, DW_AT_type, &attr_mem) == NULL |
| 1187 | || dwarf_formref_die (&attr_mem, &die_mem) == NULL) |
| 1188 | error (2, 0, _("cannot get element type of array type %s: %s"), |
| 1189 | dwarf_diename_integrate (typedie) ?: "<anonymous>", |
| 1190 | dwarf_errmsg (-1)); |
| 1191 | |
| 1192 | if (dwarf_attr_integrate (&die_mem, DW_AT_byte_size, &attr_mem) != NULL) |
| 1193 | { |
| 1194 | Dwarf_Word stride; |
| 1195 | if (dwarf_formudata (&attr_mem, &stride) == 0) |
| 1196 | return stride; |
| 1197 | error (2, 0, |
| 1198 | _("cannot get byte_size attribute for array element type %s: %s"), |
| 1199 | dwarf_diename_integrate (&die_mem) ?: "<anonymous>", |
| 1200 | dwarf_errmsg (-1)); |
| 1201 | } |
| 1202 | |
| 1203 | error (2, 0, _("confused about array element size")); |
| 1204 | return 0; |
| 1205 | } |
| 1206 | |
| 1207 | void |
| 1208 | c_translate_array (struct obstack *pool, int indent, |
| 1209 | Dwarf_Addr dwbias __attribute__ ((unused)), |
| 1210 | Dwarf_Die *typedie, struct location **input, |
| 1211 | const char *idx, Dwarf_Word const_idx) |
| 1212 | { |
| 1213 | ++indent; |
| 1214 | |
| 1215 | Dwarf_Word stride = array_stride (typedie); |
| 1216 | |
| 1217 | struct location *loc = *input; |
| 1218 | while (loc->type == loc_noncontiguous) |
| 1219 | { |
| 1220 | if (idx != NULL) |
| 1221 | error (2, 0, _("cannot dynamically index noncontiguous array")); |
| 1222 | else |
| 1223 | { |
| 1224 | Dwarf_Word offset = const_idx * stride; |
| 1225 | struct location *piece = loc->pieces; |
| 1226 | while (piece != NULL && offset >= piece->byte_size) |
| 1227 | { |
| 1228 | offset -= piece->byte_size; |
| 1229 | piece = piece->next; |
| 1230 | } |
| 1231 | if (piece == NULL) |
| 1232 | error (2, 0, _("constant index is outside noncontiguous array")); |
| 1233 | if (offset % stride != 0) |
| 1234 | error (2, 0, _("noncontiguous array splits elements")); |
| 1235 | const_idx = offset / stride; |
| 1236 | loc = piece; |
| 1237 | } |
| 1238 | } |
| 1239 | |
| 1240 | switch (loc->type) |
| 1241 | { |
| 1242 | case loc_address: |
| 1243 | ++indent; |
| 1244 | if (idx != NULL) |
| 1245 | obstack_printf (pool, "%*saddr += %s * " UFORMAT ";\n", |
| 1246 | indent * 2, "", idx, stride); |
| 1247 | else |
| 1248 | obstack_printf (pool, "%*saddr += " UFORMAT " * " UFORMAT ";\n", |
| 1249 | indent * 2, "", const_idx, stride); |
| 1250 | loc = new_synthetic_loc (pool, loc, false); |
| 1251 | break; |
| 1252 | |
| 1253 | case loc_register: |
| 1254 | error (2, 0, _("cannot index array stored in a register")); |
| 1255 | break; |
| 1256 | |
| 1257 | default: |
| 1258 | abort(); |
| 1259 | break; |
| 1260 | } |
| 1261 | |
| 1262 | (*input)->next = loc; |
| 1263 | *input = (*input)->next; |
| 1264 | } |
| 1265 | |
| 1266 | |
| 1267 | /* Emitting C code for finalized fragments. */ |
| 1268 | |
| 1269 | |
| 1270 | #define emit(fmt, ...) fprintf (out, fmt, ## __VA_ARGS__) |
| 1271 | |
| 1272 | /* Open a block with a comment giving the original DWARF expression. */ |
| 1273 | static void |
| 1274 | emit_header (FILE *out, struct location *loc, unsigned int hindent) |
| 1275 | { |
| 1276 | if (loc->ops == NULL) |
| 1277 | emit ("%*s{ // synthesized\n", hindent * 2, ""); |
| 1278 | else |
| 1279 | { |
| 1280 | emit ("%*s{ // DWARF expression:", hindent * 2, ""); |
| 1281 | for (size_t i = 0; i < loc->nops; ++i) |
| 1282 | { |
| 1283 | emit (" %#x", loc->ops[i].atom); |
| 1284 | if (loc->ops[i].number2 == 0) |
| 1285 | { |
| 1286 | if (loc->ops[i].number != 0) |
| 1287 | emit ("(%" PRId64 ")", loc->ops[i].number); |
| 1288 | } |
| 1289 | else |
| 1290 | emit ("(%" PRId64 ",%" PRId64 ")", |
| 1291 | loc->ops[i].number, loc->ops[i].number2); |
| 1292 | } |
| 1293 | emit ("\n"); |
| 1294 | } |
| 1295 | } |
| 1296 | |
| 1297 | /* Emit a code fragment to assign the target variable to a register value. */ |
| 1298 | static void |
| 1299 | emit_loc_register (FILE *out, struct location *loc, unsigned int indent, |
| 1300 | const char *target) |
| 1301 | { |
| 1302 | assert (loc->type == loc_register); |
| 1303 | |
| 1304 | emit ("%*s%s = fetch_register (%u);\n", |
| 1305 | indent * 2, "", target, loc->regno); |
| 1306 | } |
| 1307 | |
| 1308 | /* Emit a code fragment to assign the target variable to an address. */ |
| 1309 | static void |
| 1310 | emit_loc_address (FILE *out, struct location *loc, unsigned int indent, |
| 1311 | const char *target) |
| 1312 | { |
| 1313 | assert (loc->type == loc_address); |
| 1314 | |
| 1315 | if (loc->address.stack_depth == 0) |
| 1316 | /* Synthetic program. */ |
| 1317 | emit ("%s", loc->address.program); |
| 1318 | else |
| 1319 | { |
| 1320 | emit ("%*s%s " STACKFMT, (indent + 1) * 2, "", STACK_TYPE, 0); |
| 1321 | for (unsigned int i = 1; i < loc->address.stack_depth; ++i) |
| 1322 | emit (", " STACKFMT, i); |
| 1323 | emit (";\n"); |
| 1324 | |
| 1325 | emit ("%s%*s%s = " STACKFMT ";\n", loc->address.program, |
| 1326 | (indent + 1) * 2, "", target, 0); |
| 1327 | } |
| 1328 | } |
| 1329 | |
| 1330 | /* Emit a code fragment to declare the target variable and |
| 1331 | assign it to an address-sized value. */ |
| 1332 | static void |
| 1333 | emit_loc_value (FILE *out, struct location *loc, unsigned int indent, |
| 1334 | const char *target, bool declare) |
| 1335 | { |
| 1336 | if (declare) |
| 1337 | emit ("%*s%s %s;\n", indent * 2, "", STACK_TYPE, target); |
| 1338 | |
| 1339 | emit_header (out, loc, indent); |
| 1340 | |
| 1341 | switch (loc->type) |
| 1342 | { |
| 1343 | default: |
| 1344 | abort (); |
| 1345 | break; |
| 1346 | |
| 1347 | case loc_register: |
| 1348 | emit_loc_register (out, loc, indent, target); |
| 1349 | break; |
| 1350 | |
| 1351 | case loc_address: |
| 1352 | if (loc->address.frame_base != NULL) |
| 1353 | emit_loc_value (out, loc->address.frame_base, indent, |
| 1354 | "frame_base", true); |
| 1355 | emit_loc_address (out, loc, indent, target); |
| 1356 | break; |
| 1357 | } |
| 1358 | |
| 1359 | emit ("%*s}\n", indent * 2, ""); |
| 1360 | } |
| 1361 | |
| 1362 | bool |
| 1363 | c_emit_location (FILE *out, struct location *loc, int indent) |
| 1364 | { |
| 1365 | emit ("%*s{\n", indent * 2, ""); |
| 1366 | |
| 1367 | bool deref = false; |
| 1368 | for (bool declare_addr = true; loc->next != NULL; loc = loc->next) |
| 1369 | switch (loc->type) |
| 1370 | { |
| 1371 | case loc_address: |
| 1372 | /* Emit the program fragment to calculate the address. */ |
| 1373 | emit_loc_value (out, loc, indent + 1, "addr", declare_addr); |
| 1374 | declare_addr = false; |
| 1375 | deref = deref || loc->address.used_deref; |
| 1376 | break; |
| 1377 | |
| 1378 | case loc_register: |
| 1379 | case loc_noncontiguous: |
| 1380 | /* These don't produce any code directly. |
| 1381 | The next address/final record incorporates the value. */ |
| 1382 | break; |
| 1383 | |
| 1384 | case loc_final: /* Should be last in chain! */ |
| 1385 | default: |
| 1386 | abort (); |
| 1387 | break; |
| 1388 | } |
| 1389 | |
| 1390 | if (loc->type != loc_final) /* Unfinished chain. */ |
| 1391 | abort (); |
| 1392 | |
| 1393 | emit ("%s%*s}\n", loc->address.program, indent * 2, ""); |
| 1394 | |
| 1395 | return deref; |
| 1396 | } |
| 1397 | |
| 1398 | #undef emit |