gdb/dwarf2expr.c - gdb

Global variables defined

Data types defined

Functions defined

Source code

  1. /* DWARF 2 Expression Evaluator.

  2.    Copyright (C) 2001-2015 Free Software Foundation, Inc.

  3.    Contributed by Daniel Berlin (dan@dberlin.org)

  4.    This file is part of GDB.

  5.    This program is free software; you can redistribute it and/or modify
  6.    it under the terms of the GNU General Public License as published by
  7.    the Free Software Foundation; either version 3 of the License, or
  8.    (at your option) any later version.

  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.

  13.    You should have received a copy of the GNU General Public License
  14.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

  15. #include "defs.h"
  16. #include "symtab.h"
  17. #include "gdbtypes.h"
  18. #include "value.h"
  19. #include "gdbcore.h"
  20. #include "dwarf2.h"
  21. #include "dwarf2expr.h"

  22. /* Local prototypes.  */

  23. static void execute_stack_op (struct dwarf_expr_context *,
  24.                               const gdb_byte *, const gdb_byte *);

  25. /* Cookie for gdbarch data.  */

  26. static struct gdbarch_data *dwarf_arch_cookie;

  27. /* This holds gdbarch-specific types used by the DWARF expression
  28.    evaluator.  See comments in execute_stack_op.  */

  29. struct dwarf_gdbarch_types
  30. {
  31.   struct type *dw_types[3];
  32. };

  33. /* Allocate and fill in dwarf_gdbarch_types for an arch.  */

  34. static void *
  35. dwarf_gdbarch_types_init (struct gdbarch *gdbarch)
  36. {
  37.   struct dwarf_gdbarch_types *types
  38.     = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct dwarf_gdbarch_types);

  39.   /* The types themselves are lazily initialized.  */

  40.   return types;
  41. }

  42. /* Return the type used for DWARF operations where the type is
  43.    unspecified in the DWARF spec.  Only certain sizes are
  44.    supported.  */

  45. static struct type *
  46. dwarf_expr_address_type (struct dwarf_expr_context *ctx)
  47. {
  48.   struct dwarf_gdbarch_types *types = gdbarch_data (ctx->gdbarch,
  49.                                                     dwarf_arch_cookie);
  50.   int ndx;

  51.   if (ctx->addr_size == 2)
  52.     ndx = 0;
  53.   else if (ctx->addr_size == 4)
  54.     ndx = 1;
  55.   else if (ctx->addr_size == 8)
  56.     ndx = 2;
  57.   else
  58.     error (_("Unsupported address size in DWARF expressions: %d bits"),
  59.            8 * ctx->addr_size);

  60.   if (types->dw_types[ndx] == NULL)
  61.     types->dw_types[ndx]
  62.       = arch_integer_type (ctx->gdbarch,
  63.                            8 * ctx->addr_size,
  64.                            0, "<signed DWARF address type>");

  65.   return types->dw_types[ndx];
  66. }

  67. /* Create a new context for the expression evaluator.  */

  68. struct dwarf_expr_context *
  69. new_dwarf_expr_context (void)
  70. {
  71.   struct dwarf_expr_context *retval;

  72.   retval = xcalloc (1, sizeof (struct dwarf_expr_context));
  73.   retval->stack_len = 0;
  74.   retval->stack_allocated = 10;
  75.   retval->stack = xmalloc (retval->stack_allocated
  76.                            * sizeof (struct dwarf_stack_value));
  77.   retval->num_pieces = 0;
  78.   retval->pieces = 0;
  79.   retval->max_recursion_depth = 0x100;
  80.   return retval;
  81. }

  82. /* Release the memory allocated to CTX.  */

  83. void
  84. free_dwarf_expr_context (struct dwarf_expr_context *ctx)
  85. {
  86.   xfree (ctx->stack);
  87.   xfree (ctx->pieces);
  88.   xfree (ctx);
  89. }

  90. /* Helper for make_cleanup_free_dwarf_expr_context.  */

  91. static void
  92. free_dwarf_expr_context_cleanup (void *arg)
  93. {
  94.   free_dwarf_expr_context (arg);
  95. }

  96. /* Return a cleanup that calls free_dwarf_expr_context.  */

  97. struct cleanup *
  98. make_cleanup_free_dwarf_expr_context (struct dwarf_expr_context *ctx)
  99. {
  100.   return make_cleanup (free_dwarf_expr_context_cleanup, ctx);
  101. }

  102. /* Expand the memory allocated to CTX's stack to contain at least
  103.    NEED more elements than are currently used.  */

  104. static void
  105. dwarf_expr_grow_stack (struct dwarf_expr_context *ctx, size_t need)
  106. {
  107.   if (ctx->stack_len + need > ctx->stack_allocated)
  108.     {
  109.       size_t newlen = ctx->stack_len + need + 10;

  110.       ctx->stack = xrealloc (ctx->stack,
  111.                              newlen * sizeof (struct dwarf_stack_value));
  112.       ctx->stack_allocated = newlen;
  113.     }
  114. }

  115. /* Push VALUE onto CTX's stack.  */

  116. static void
  117. dwarf_expr_push (struct dwarf_expr_context *ctx, struct value *value,
  118.                  int in_stack_memory)
  119. {
  120.   struct dwarf_stack_value *v;

  121.   dwarf_expr_grow_stack (ctx, 1);
  122.   v = &ctx->stack[ctx->stack_len++];
  123.   v->value = value;
  124.   v->in_stack_memory = in_stack_memory;
  125. }

  126. /* Push VALUE onto CTX's stack.  */

  127. void
  128. dwarf_expr_push_address (struct dwarf_expr_context *ctx, CORE_ADDR value,
  129.                          int in_stack_memory)
  130. {
  131.   dwarf_expr_push (ctx,
  132.                    value_from_ulongest (dwarf_expr_address_type (ctx), value),
  133.                    in_stack_memory);
  134. }

  135. /* Pop the top item off of CTX's stack.  */

  136. static void
  137. dwarf_expr_pop (struct dwarf_expr_context *ctx)
  138. {
  139.   if (ctx->stack_len <= 0)
  140.     error (_("dwarf expression stack underflow"));
  141.   ctx->stack_len--;
  142. }

  143. /* Retrieve the N'th item on CTX's stack.  */

  144. struct value *
  145. dwarf_expr_fetch (struct dwarf_expr_context *ctx, int n)
  146. {
  147.   if (ctx->stack_len <= n)
  148.      error (_("Asked for position %d of stack, "
  149.               "stack only has %d elements on it."),
  150.             n, ctx->stack_len);
  151.   return ctx->stack[ctx->stack_len - (1 + n)].value;
  152. }

  153. /* Require that TYPE be an integral type; throw an exception if not.  */

  154. static void
  155. dwarf_require_integral (struct type *type)
  156. {
  157.   if (TYPE_CODE (type) != TYPE_CODE_INT
  158.       && TYPE_CODE (type) != TYPE_CODE_CHAR
  159.       && TYPE_CODE (type) != TYPE_CODE_BOOL)
  160.     error (_("integral type expected in DWARF expression"));
  161. }

  162. /* Return the unsigned form of TYPETYPE is necessarily an integral
  163.    type.  */

  164. static struct type *
  165. get_unsigned_type (struct gdbarch *gdbarch, struct type *type)
  166. {
  167.   switch (TYPE_LENGTH (type))
  168.     {
  169.     case 1:
  170.       return builtin_type (gdbarch)->builtin_uint8;
  171.     case 2:
  172.       return builtin_type (gdbarch)->builtin_uint16;
  173.     case 4:
  174.       return builtin_type (gdbarch)->builtin_uint32;
  175.     case 8:
  176.       return builtin_type (gdbarch)->builtin_uint64;
  177.     default:
  178.       error (_("no unsigned variant found for type, while evaluating "
  179.                "DWARF expression"));
  180.     }
  181. }

  182. /* Return the signed form of TYPETYPE is necessarily an integral
  183.    type.  */

  184. static struct type *
  185. get_signed_type (struct gdbarch *gdbarch, struct type *type)
  186. {
  187.   switch (TYPE_LENGTH (type))
  188.     {
  189.     case 1:
  190.       return builtin_type (gdbarch)->builtin_int8;
  191.     case 2:
  192.       return builtin_type (gdbarch)->builtin_int16;
  193.     case 4:
  194.       return builtin_type (gdbarch)->builtin_int32;
  195.     case 8:
  196.       return builtin_type (gdbarch)->builtin_int64;
  197.     default:
  198.       error (_("no signed variant found for type, while evaluating "
  199.                "DWARF expression"));
  200.     }
  201. }

  202. /* Retrieve the N'th item on CTX's stack, converted to an address.  */

  203. CORE_ADDR
  204. dwarf_expr_fetch_address (struct dwarf_expr_context *ctx, int n)
  205. {
  206.   struct value *result_val = dwarf_expr_fetch (ctx, n);
  207.   enum bfd_endian byte_order = gdbarch_byte_order (ctx->gdbarch);
  208.   ULONGEST result;

  209.   dwarf_require_integral (value_type (result_val));
  210.   result = extract_unsigned_integer (value_contents (result_val),
  211.                                      TYPE_LENGTH (value_type (result_val)),
  212.                                      byte_order);

  213.   /* For most architectures, calling extract_unsigned_integer() alone
  214.      is sufficient for extracting an address.  However, some
  215.      architectures (e.g. MIPS) use signed addresses and using
  216.      extract_unsigned_integer() will not produce a correct
  217.      result.  Make sure we invoke gdbarch_integer_to_address()
  218.      for those architectures which require it.  */
  219.   if (gdbarch_integer_to_address_p (ctx->gdbarch))
  220.     {
  221.       gdb_byte *buf = alloca (ctx->addr_size);
  222.       struct type *int_type = get_unsigned_type (ctx->gdbarch,
  223.                                                  value_type (result_val));

  224.       store_unsigned_integer (buf, ctx->addr_size, byte_order, result);
  225.       return gdbarch_integer_to_address (ctx->gdbarch, int_type, buf);
  226.     }

  227.   return (CORE_ADDR) result;
  228. }

  229. /* Retrieve the in_stack_memory flag of the N'th item on CTX's stack.  */

  230. int
  231. dwarf_expr_fetch_in_stack_memory (struct dwarf_expr_context *ctx, int n)
  232. {
  233.   if (ctx->stack_len <= n)
  234.      error (_("Asked for position %d of stack, "
  235.               "stack only has %d elements on it."),
  236.             n, ctx->stack_len);
  237.   return ctx->stack[ctx->stack_len - (1 + n)].in_stack_memory;
  238. }

  239. /* Return true if the expression stack is empty.  */

  240. static int
  241. dwarf_expr_stack_empty_p (struct dwarf_expr_context *ctx)
  242. {
  243.   return ctx->stack_len == 0;
  244. }

  245. /* Add a new piece to CTX's piece list.  */
  246. static void
  247. add_piece (struct dwarf_expr_context *ctx, ULONGEST size, ULONGEST offset)
  248. {
  249.   struct dwarf_expr_piece *p;

  250.   ctx->num_pieces++;

  251.   ctx->pieces = xrealloc (ctx->pieces,
  252.                           (ctx->num_pieces
  253.                            * sizeof (struct dwarf_expr_piece)));

  254.   p = &ctx->pieces[ctx->num_pieces - 1];
  255.   p->location = ctx->location;
  256.   p->size = size;
  257.   p->offset = offset;

  258.   if (p->location == DWARF_VALUE_LITERAL)
  259.     {
  260.       p->v.literal.data = ctx->data;
  261.       p->v.literal.length = ctx->len;
  262.     }
  263.   else if (dwarf_expr_stack_empty_p (ctx))
  264.     {
  265.       p->location = DWARF_VALUE_OPTIMIZED_OUT;
  266.       /* Also reset the context's location, for our callers.  This is
  267.          a somewhat strange approach, but this lets us avoid setting
  268.          the location to DWARF_VALUE_MEMORY in all the individual
  269.          cases in the evaluator.  */
  270.       ctx->location = DWARF_VALUE_OPTIMIZED_OUT;
  271.     }
  272.   else if (p->location == DWARF_VALUE_MEMORY)
  273.     {
  274.       p->v.mem.addr = dwarf_expr_fetch_address (ctx, 0);
  275.       p->v.mem.in_stack_memory = dwarf_expr_fetch_in_stack_memory (ctx, 0);
  276.     }
  277.   else if (p->location == DWARF_VALUE_IMPLICIT_POINTER)
  278.     {
  279.       p->v.ptr.die.sect_off = ctx->len;
  280.       p->v.ptr.offset = value_as_long (dwarf_expr_fetch (ctx, 0));
  281.     }
  282.   else if (p->location == DWARF_VALUE_REGISTER)
  283.     p->v.regno = value_as_long (dwarf_expr_fetch (ctx, 0));
  284.   else
  285.     {
  286.       p->v.value = dwarf_expr_fetch (ctx, 0);
  287.     }
  288. }

  289. /* Evaluate the expression at ADDR (LEN bytes long) using the context
  290.    CTX.  */

  291. void
  292. dwarf_expr_eval (struct dwarf_expr_context *ctx, const gdb_byte *addr,
  293.                  size_t len)
  294. {
  295.   int old_recursion_depth = ctx->recursion_depth;

  296.   execute_stack_op (ctx, addr, addr + len);

  297.   /* CTX RECURSION_DEPTH becomes invalid if an exception was thrown here.  */

  298.   gdb_assert (ctx->recursion_depth == old_recursion_depth);
  299. }

  300. /* Helper to read a uleb128 value or throw an error.  */

  301. const gdb_byte *
  302. safe_read_uleb128 (const gdb_byte *buf, const gdb_byte *buf_end,
  303.                    uint64_t *r)
  304. {
  305.   buf = gdb_read_uleb128 (buf, buf_end, r);
  306.   if (buf == NULL)
  307.     error (_("DWARF expression error: ran off end of buffer reading uleb128 value"));
  308.   return buf;
  309. }

  310. /* Helper to read a sleb128 value or throw an error.  */

  311. const gdb_byte *
  312. safe_read_sleb128 (const gdb_byte *buf, const gdb_byte *buf_end,
  313.                    int64_t *r)
  314. {
  315.   buf = gdb_read_sleb128 (buf, buf_end, r);
  316.   if (buf == NULL)
  317.     error (_("DWARF expression error: ran off end of buffer reading sleb128 value"));
  318.   return buf;
  319. }

  320. const gdb_byte *
  321. safe_skip_leb128 (const gdb_byte *buf, const gdb_byte *buf_end)
  322. {
  323.   buf = gdb_skip_leb128 (buf, buf_end);
  324.   if (buf == NULL)
  325.     error (_("DWARF expression error: ran off end of buffer reading leb128 value"));
  326.   return buf;
  327. }


  328. /* Check that the current operator is either at the end of an
  329.    expression, or that it is followed by a composition operator.  */

  330. void
  331. dwarf_expr_require_composition (const gdb_byte *op_ptr, const gdb_byte *op_end,
  332.                                 const char *op_name)
  333. {
  334.   /* It seems like DW_OP_GNU_uninit should be handled here.  However,
  335.      it doesn't seem to make sense for DW_OP_*_value, and it was not
  336.      checked at the other place that this function is called.  */
  337.   if (op_ptr != op_end && *op_ptr != DW_OP_piece && *op_ptr != DW_OP_bit_piece)
  338.     error (_("DWARF-2 expression error: `%s' operations must be "
  339.              "used either alone or in conjunction with DW_OP_piece "
  340.              "or DW_OP_bit_piece."),
  341.            op_name);
  342. }

  343. /* Return true iff the types T1 and T2 are "the same".  This only does
  344.    checks that might reasonably be needed to compare DWARF base
  345.    types.  */

  346. static int
  347. base_types_equal_p (struct type *t1, struct type *t2)
  348. {
  349.   if (TYPE_CODE (t1) != TYPE_CODE (t2))
  350.     return 0;
  351.   if (TYPE_UNSIGNED (t1) != TYPE_UNSIGNED (t2))
  352.     return 0;
  353.   return TYPE_LENGTH (t1) == TYPE_LENGTH (t2);
  354. }

  355. /* A convenience function to call get_base_type on CTX and return the
  356.    result.  DIE is the DIE whose type we need.  SIZE is non-zero if
  357.    this function should verify that the resulting type has the correct
  358.    size.  */

  359. static struct type *
  360. dwarf_get_base_type (struct dwarf_expr_context *ctx, cu_offset die, int size)
  361. {
  362.   struct type *result;

  363.   if (ctx->funcs->get_base_type)
  364.     {
  365.       result = ctx->funcs->get_base_type (ctx, die);
  366.       if (result == NULL)
  367.         error (_("Could not find type for DW_OP_GNU_const_type"));
  368.       if (size != 0 && TYPE_LENGTH (result) != size)
  369.         error (_("DW_OP_GNU_const_type has different sizes for type and data"));
  370.     }
  371.   else
  372.     /* Anything will do.  */
  373.     result = builtin_type (ctx->gdbarch)->builtin_int;

  374.   return result;
  375. }

  376. /* If <BUF..BUF_END] contains DW_FORM_block* with single DW_OP_reg* return the
  377.    DWARF register number.  Otherwise return -1.  */

  378. int
  379. dwarf_block_to_dwarf_reg (const gdb_byte *buf, const gdb_byte *buf_end)
  380. {
  381.   uint64_t dwarf_reg;

  382.   if (buf_end <= buf)
  383.     return -1;
  384.   if (*buf >= DW_OP_reg0 && *buf <= DW_OP_reg31)
  385.     {
  386.       if (buf_end - buf != 1)
  387.         return -1;
  388.       return *buf - DW_OP_reg0;
  389.     }

  390.   if (*buf == DW_OP_GNU_regval_type)
  391.     {
  392.       buf++;
  393.       buf = gdb_read_uleb128 (buf, buf_end, &dwarf_reg);
  394.       if (buf == NULL)
  395.         return -1;
  396.       buf = gdb_skip_leb128 (buf, buf_end);
  397.       if (buf == NULL)
  398.         return -1;
  399.     }
  400.   else if (*buf == DW_OP_regx)
  401.     {
  402.       buf++;
  403.       buf = gdb_read_uleb128 (buf, buf_end, &dwarf_reg);
  404.       if (buf == NULL)
  405.         return -1;
  406.     }
  407.   else
  408.     return -1;
  409.   if (buf != buf_end || (int) dwarf_reg != dwarf_reg)
  410.     return -1;
  411.   return dwarf_reg;
  412. }

  413. /* If <BUF..BUF_END] contains DW_FORM_block* with just DW_OP_breg*(0) and
  414.    DW_OP_deref* return the DWARF register number.  Otherwise return -1.
  415.    DEREF_SIZE_RETURN contains -1 for DW_OP_deref; otherwise it contains the
  416.    size from DW_OP_deref_size.  */

  417. int
  418. dwarf_block_to_dwarf_reg_deref (const gdb_byte *buf, const gdb_byte *buf_end,
  419.                                 CORE_ADDR *deref_size_return)
  420. {
  421.   uint64_t dwarf_reg;
  422.   int64_t offset;

  423.   if (buf_end <= buf)
  424.     return -1;

  425.   if (*buf >= DW_OP_breg0 && *buf <= DW_OP_breg31)
  426.     {
  427.       dwarf_reg = *buf - DW_OP_breg0;
  428.       buf++;
  429.       if (buf >= buf_end)
  430.         return -1;
  431.     }
  432.   else if (*buf == DW_OP_bregx)
  433.     {
  434.       buf++;
  435.       buf = gdb_read_uleb128 (buf, buf_end, &dwarf_reg);
  436.       if (buf == NULL)
  437.         return -1;
  438.       if ((int) dwarf_reg != dwarf_reg)
  439.        return -1;
  440.     }
  441.   else
  442.     return -1;

  443.   buf = gdb_read_sleb128 (buf, buf_end, &offset);
  444.   if (buf == NULL)
  445.     return -1;
  446.   if (offset != 0)
  447.     return -1;

  448.   if (*buf == DW_OP_deref)
  449.     {
  450.       buf++;
  451.       *deref_size_return = -1;
  452.     }
  453.   else if (*buf == DW_OP_deref_size)
  454.     {
  455.       buf++;
  456.       if (buf >= buf_end)
  457.        return -1;
  458.       *deref_size_return = *buf++;
  459.     }
  460.   else
  461.     return -1;

  462.   if (buf != buf_end)
  463.     return -1;

  464.   return dwarf_reg;
  465. }

  466. /* If <BUF..BUF_END] contains DW_FORM_block* with single DW_OP_fbreg(X) fill
  467.    in FB_OFFSET_RETURN with the X offset and return 1.  Otherwise return 0.  */

  468. int
  469. dwarf_block_to_fb_offset (const gdb_byte *buf, const gdb_byte *buf_end,
  470.                           CORE_ADDR *fb_offset_return)
  471. {
  472.   int64_t fb_offset;

  473.   if (buf_end <= buf)
  474.     return 0;

  475.   if (*buf != DW_OP_fbreg)
  476.     return 0;
  477.   buf++;

  478.   buf = gdb_read_sleb128 (buf, buf_end, &fb_offset);
  479.   if (buf == NULL)
  480.     return 0;
  481.   *fb_offset_return = fb_offset;
  482.   if (buf != buf_end || fb_offset != (LONGEST) *fb_offset_return)
  483.     return 0;

  484.   return 1;
  485. }

  486. /* If <BUF..BUF_END] contains DW_FORM_block* with single DW_OP_bregSP(X) fill
  487.    in SP_OFFSET_RETURN with the X offset and return 1.  Otherwise return 0.
  488.    The matched SP register number depends on GDBARCH.  */

  489. int
  490. dwarf_block_to_sp_offset (struct gdbarch *gdbarch, const gdb_byte *buf,
  491.                           const gdb_byte *buf_end, CORE_ADDR *sp_offset_return)
  492. {
  493.   uint64_t dwarf_reg;
  494.   int64_t sp_offset;

  495.   if (buf_end <= buf)
  496.     return 0;
  497.   if (*buf >= DW_OP_breg0 && *buf <= DW_OP_breg31)
  498.     {
  499.       dwarf_reg = *buf - DW_OP_breg0;
  500.       buf++;
  501.     }
  502.   else
  503.     {
  504.       if (*buf != DW_OP_bregx)
  505.        return 0;
  506.       buf++;
  507.       buf = gdb_read_uleb128 (buf, buf_end, &dwarf_reg);
  508.       if (buf == NULL)
  509.         return 0;
  510.     }

  511.   if (gdbarch_dwarf2_reg_to_regnum (gdbarch, dwarf_reg)
  512.       != gdbarch_sp_regnum (gdbarch))
  513.     return 0;

  514.   buf = gdb_read_sleb128 (buf, buf_end, &sp_offset);
  515.   if (buf == NULL)
  516.     return 0;
  517.   *sp_offset_return = sp_offset;
  518.   if (buf != buf_end || sp_offset != (LONGEST) *sp_offset_return)
  519.     return 0;

  520.   return 1;
  521. }

  522. /* The engine for the expression evaluator.  Using the context in CTX,
  523.    evaluate the expression between OP_PTR and OP_END.  */

  524. static void
  525. execute_stack_op (struct dwarf_expr_context *ctx,
  526.                   const gdb_byte *op_ptr, const gdb_byte *op_end)
  527. {
  528.   enum bfd_endian byte_order = gdbarch_byte_order (ctx->gdbarch);
  529.   /* Old-style "untyped" DWARF values need special treatment in a
  530.      couple of places, specifically DW_OP_mod and DW_OP_shr.  We need
  531.      a special type for these values so we can distinguish them from
  532.      values that have an explicit type, because explicitly-typed
  533.      values do not need special treatment.  This special type must be
  534.      different (in the `==' sense) from any base type coming from the
  535.      CU.  */
  536.   struct type *address_type = dwarf_expr_address_type (ctx);

  537.   ctx->location = DWARF_VALUE_MEMORY;
  538.   ctx->initialized = 1/* Default is initialized.  */

  539.   if (ctx->recursion_depth > ctx->max_recursion_depth)
  540.     error (_("DWARF-2 expression error: Loop detected (%d)."),
  541.            ctx->recursion_depth);
  542.   ctx->recursion_depth++;

  543.   while (op_ptr < op_end)
  544.     {
  545.       enum dwarf_location_atom op = *op_ptr++;
  546.       ULONGEST result;
  547.       /* Assume the value is not in stack memory.
  548.          Code that knows otherwise sets this to 1.
  549.          Some arithmetic on stack addresses can probably be assumed to still
  550.          be a stack address, but we skip this complication for now.
  551.          This is just an optimization, so it's always ok to punt
  552.          and leave this as 0.  */
  553.       int in_stack_memory = 0;
  554.       uint64_t uoffset, reg;
  555.       int64_t offset;
  556.       struct value *result_val = NULL;

  557.       /* The DWARF expression might have a bug causing an infinite
  558.          loop.  In that case, quitting is the only way out.  */
  559.       QUIT;

  560.       switch (op)
  561.         {
  562.         case DW_OP_lit0:
  563.         case DW_OP_lit1:
  564.         case DW_OP_lit2:
  565.         case DW_OP_lit3:
  566.         case DW_OP_lit4:
  567.         case DW_OP_lit5:
  568.         case DW_OP_lit6:
  569.         case DW_OP_lit7:
  570.         case DW_OP_lit8:
  571.         case DW_OP_lit9:
  572.         case DW_OP_lit10:
  573.         case DW_OP_lit11:
  574.         case DW_OP_lit12:
  575.         case DW_OP_lit13:
  576.         case DW_OP_lit14:
  577.         case DW_OP_lit15:
  578.         case DW_OP_lit16:
  579.         case DW_OP_lit17:
  580.         case DW_OP_lit18:
  581.         case DW_OP_lit19:
  582.         case DW_OP_lit20:
  583.         case DW_OP_lit21:
  584.         case DW_OP_lit22:
  585.         case DW_OP_lit23:
  586.         case DW_OP_lit24:
  587.         case DW_OP_lit25:
  588.         case DW_OP_lit26:
  589.         case DW_OP_lit27:
  590.         case DW_OP_lit28:
  591.         case DW_OP_lit29:
  592.         case DW_OP_lit30:
  593.         case DW_OP_lit31:
  594.           result = op - DW_OP_lit0;
  595.           result_val = value_from_ulongest (address_type, result);
  596.           break;

  597.         case DW_OP_addr:
  598.           result = extract_unsigned_integer (op_ptr,
  599.                                              ctx->addr_size, byte_order);
  600.           op_ptr += ctx->addr_size;
  601.           /* Some versions of GCC emit DW_OP_addr before
  602.              DW_OP_GNU_push_tls_address.  In this case the value is an
  603.              index, not an address.  We don't support things like
  604.              branching between the address and the TLS op.  */
  605.           if (op_ptr >= op_end || *op_ptr != DW_OP_GNU_push_tls_address)
  606.             result += ctx->offset;
  607.           result_val = value_from_ulongest (address_type, result);
  608.           break;

  609.         case DW_OP_GNU_addr_index:
  610.           op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
  611.           result = (ctx->funcs->get_addr_index) (ctx->baton, uoffset);
  612.           result += ctx->offset;
  613.           result_val = value_from_ulongest (address_type, result);
  614.           break;
  615.         case DW_OP_GNU_const_index:
  616.           op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
  617.           result = (ctx->funcs->get_addr_index) (ctx->baton, uoffset);
  618.           result_val = value_from_ulongest (address_type, result);
  619.           break;

  620.         case DW_OP_const1u:
  621.           result = extract_unsigned_integer (op_ptr, 1, byte_order);
  622.           result_val = value_from_ulongest (address_type, result);
  623.           op_ptr += 1;
  624.           break;
  625.         case DW_OP_const1s:
  626.           result = extract_signed_integer (op_ptr, 1, byte_order);
  627.           result_val = value_from_ulongest (address_type, result);
  628.           op_ptr += 1;
  629.           break;
  630.         case DW_OP_const2u:
  631.           result = extract_unsigned_integer (op_ptr, 2, byte_order);
  632.           result_val = value_from_ulongest (address_type, result);
  633.           op_ptr += 2;
  634.           break;
  635.         case DW_OP_const2s:
  636.           result = extract_signed_integer (op_ptr, 2, byte_order);
  637.           result_val = value_from_ulongest (address_type, result);
  638.           op_ptr += 2;
  639.           break;
  640.         case DW_OP_const4u:
  641.           result = extract_unsigned_integer (op_ptr, 4, byte_order);
  642.           result_val = value_from_ulongest (address_type, result);
  643.           op_ptr += 4;
  644.           break;
  645.         case DW_OP_const4s:
  646.           result = extract_signed_integer (op_ptr, 4, byte_order);
  647.           result_val = value_from_ulongest (address_type, result);
  648.           op_ptr += 4;
  649.           break;
  650.         case DW_OP_const8u:
  651.           result = extract_unsigned_integer (op_ptr, 8, byte_order);
  652.           result_val = value_from_ulongest (address_type, result);
  653.           op_ptr += 8;
  654.           break;
  655.         case DW_OP_const8s:
  656.           result = extract_signed_integer (op_ptr, 8, byte_order);
  657.           result_val = value_from_ulongest (address_type, result);
  658.           op_ptr += 8;
  659.           break;
  660.         case DW_OP_constu:
  661.           op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
  662.           result = uoffset;
  663.           result_val = value_from_ulongest (address_type, result);
  664.           break;
  665.         case DW_OP_consts:
  666.           op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
  667.           result = offset;
  668.           result_val = value_from_ulongest (address_type, result);
  669.           break;

  670.         /* The DW_OP_reg operations are required to occur alone in
  671.            location expressions.  */
  672.         case DW_OP_reg0:
  673.         case DW_OP_reg1:
  674.         case DW_OP_reg2:
  675.         case DW_OP_reg3:
  676.         case DW_OP_reg4:
  677.         case DW_OP_reg5:
  678.         case DW_OP_reg6:
  679.         case DW_OP_reg7:
  680.         case DW_OP_reg8:
  681.         case DW_OP_reg9:
  682.         case DW_OP_reg10:
  683.         case DW_OP_reg11:
  684.         case DW_OP_reg12:
  685.         case DW_OP_reg13:
  686.         case DW_OP_reg14:
  687.         case DW_OP_reg15:
  688.         case DW_OP_reg16:
  689.         case DW_OP_reg17:
  690.         case DW_OP_reg18:
  691.         case DW_OP_reg19:
  692.         case DW_OP_reg20:
  693.         case DW_OP_reg21:
  694.         case DW_OP_reg22:
  695.         case DW_OP_reg23:
  696.         case DW_OP_reg24:
  697.         case DW_OP_reg25:
  698.         case DW_OP_reg26:
  699.         case DW_OP_reg27:
  700.         case DW_OP_reg28:
  701.         case DW_OP_reg29:
  702.         case DW_OP_reg30:
  703.         case DW_OP_reg31:
  704.           if (op_ptr != op_end
  705.               && *op_ptr != DW_OP_piece
  706.               && *op_ptr != DW_OP_bit_piece
  707.               && *op_ptr != DW_OP_GNU_uninit)
  708.             error (_("DWARF-2 expression error: DW_OP_reg operations must be "
  709.                      "used either alone or in conjunction with DW_OP_piece "
  710.                      "or DW_OP_bit_piece."));

  711.           result = op - DW_OP_reg0;
  712.           result_val = value_from_ulongest (address_type, result);
  713.           ctx->location = DWARF_VALUE_REGISTER;
  714.           break;

  715.         case DW_OP_regx:
  716.           op_ptr = safe_read_uleb128 (op_ptr, op_end, &reg);
  717.           dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_regx");

  718.           result = reg;
  719.           result_val = value_from_ulongest (address_type, result);
  720.           ctx->location = DWARF_VALUE_REGISTER;
  721.           break;

  722.         case DW_OP_implicit_value:
  723.           {
  724.             uint64_t len;

  725.             op_ptr = safe_read_uleb128 (op_ptr, op_end, &len);
  726.             if (op_ptr + len > op_end)
  727.               error (_("DW_OP_implicit_value: too few bytes available."));
  728.             ctx->len = len;
  729.             ctx->data = op_ptr;
  730.             ctx->location = DWARF_VALUE_LITERAL;
  731.             op_ptr += len;
  732.             dwarf_expr_require_composition (op_ptr, op_end,
  733.                                             "DW_OP_implicit_value");
  734.           }
  735.           goto no_push;

  736.         case DW_OP_stack_value:
  737.           ctx->location = DWARF_VALUE_STACK;
  738.           dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_stack_value");
  739.           goto no_push;

  740.         case DW_OP_GNU_implicit_pointer:
  741.           {
  742.             int64_t len;

  743.             if (ctx->ref_addr_size == -1)
  744.               error (_("DWARF-2 expression error: DW_OP_GNU_implicit_pointer "
  745.                        "is not allowed in frame context"));

  746.             /* The referred-to DIE of sect_offset kind.  */
  747.             ctx->len = extract_unsigned_integer (op_ptr, ctx->ref_addr_size,
  748.                                                  byte_order);
  749.             op_ptr += ctx->ref_addr_size;

  750.             /* The byte offset into the data.  */
  751.             op_ptr = safe_read_sleb128 (op_ptr, op_end, &len);
  752.             result = (ULONGEST) len;
  753.             result_val = value_from_ulongest (address_type, result);

  754.             ctx->location = DWARF_VALUE_IMPLICIT_POINTER;
  755.             dwarf_expr_require_composition (op_ptr, op_end,
  756.                                             "DW_OP_GNU_implicit_pointer");
  757.           }
  758.           break;

  759.         case DW_OP_breg0:
  760.         case DW_OP_breg1:
  761.         case DW_OP_breg2:
  762.         case DW_OP_breg3:
  763.         case DW_OP_breg4:
  764.         case DW_OP_breg5:
  765.         case DW_OP_breg6:
  766.         case DW_OP_breg7:
  767.         case DW_OP_breg8:
  768.         case DW_OP_breg9:
  769.         case DW_OP_breg10:
  770.         case DW_OP_breg11:
  771.         case DW_OP_breg12:
  772.         case DW_OP_breg13:
  773.         case DW_OP_breg14:
  774.         case DW_OP_breg15:
  775.         case DW_OP_breg16:
  776.         case DW_OP_breg17:
  777.         case DW_OP_breg18:
  778.         case DW_OP_breg19:
  779.         case DW_OP_breg20:
  780.         case DW_OP_breg21:
  781.         case DW_OP_breg22:
  782.         case DW_OP_breg23:
  783.         case DW_OP_breg24:
  784.         case DW_OP_breg25:
  785.         case DW_OP_breg26:
  786.         case DW_OP_breg27:
  787.         case DW_OP_breg28:
  788.         case DW_OP_breg29:
  789.         case DW_OP_breg30:
  790.         case DW_OP_breg31:
  791.           {
  792.             op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
  793.             result = (ctx->funcs->read_addr_from_reg) (ctx->baton,
  794.                                                        op - DW_OP_breg0);
  795.             result += offset;
  796.             result_val = value_from_ulongest (address_type, result);
  797.           }
  798.           break;
  799.         case DW_OP_bregx:
  800.           {
  801.             op_ptr = safe_read_uleb128 (op_ptr, op_end, &reg);
  802.             op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
  803.             result = (ctx->funcs->read_addr_from_reg) (ctx->baton, reg);
  804.             result += offset;
  805.             result_val = value_from_ulongest (address_type, result);
  806.           }
  807.           break;
  808.         case DW_OP_fbreg:
  809.           {
  810.             const gdb_byte *datastart;
  811.             size_t datalen;
  812.             unsigned int before_stack_len;

  813.             op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
  814.             /* Rather than create a whole new context, we simply
  815.                record the stack length before execution, then reset it
  816.                afterwards, effectively erasing whatever the recursive
  817.                call put there.  */
  818.             before_stack_len = ctx->stack_len;
  819.             /* FIXME: cagney/2003-03-26: This code should be using
  820.                get_frame_base_address(), and then implement a dwarf2
  821.                specific this_base method.  */
  822.             (ctx->funcs->get_frame_base) (ctx->baton, &datastart, &datalen);
  823.             dwarf_expr_eval (ctx, datastart, datalen);
  824.             if (ctx->location == DWARF_VALUE_MEMORY)
  825.               result = dwarf_expr_fetch_address (ctx, 0);
  826.             else if (ctx->location == DWARF_VALUE_REGISTER)
  827.               result = (ctx->funcs->read_addr_from_reg)
  828.                           (ctx->baton,
  829.                            value_as_long (dwarf_expr_fetch (ctx, 0)));
  830.             else
  831.               error (_("Not implemented: computing frame "
  832.                        "base using explicit value operator"));
  833.             result = result + offset;
  834.             result_val = value_from_ulongest (address_type, result);
  835.             in_stack_memory = 1;
  836.             ctx->stack_len = before_stack_len;
  837.             ctx->location = DWARF_VALUE_MEMORY;
  838.           }
  839.           break;

  840.         case DW_OP_dup:
  841.           result_val = dwarf_expr_fetch (ctx, 0);
  842.           in_stack_memory = dwarf_expr_fetch_in_stack_memory (ctx, 0);
  843.           break;

  844.         case DW_OP_drop:
  845.           dwarf_expr_pop (ctx);
  846.           goto no_push;

  847.         case DW_OP_pick:
  848.           offset = *op_ptr++;
  849.           result_val = dwarf_expr_fetch (ctx, offset);
  850.           in_stack_memory = dwarf_expr_fetch_in_stack_memory (ctx, offset);
  851.           break;

  852.         case DW_OP_swap:
  853.           {
  854.             struct dwarf_stack_value t1, t2;

  855.             if (ctx->stack_len < 2)
  856.                error (_("Not enough elements for "
  857.                         "DW_OP_swap.  Need 2, have %d."),
  858.                       ctx->stack_len);
  859.             t1 = ctx->stack[ctx->stack_len - 1];
  860.             t2 = ctx->stack[ctx->stack_len - 2];
  861.             ctx->stack[ctx->stack_len - 1] = t2;
  862.             ctx->stack[ctx->stack_len - 2] = t1;
  863.             goto no_push;
  864.           }

  865.         case DW_OP_over:
  866.           result_val = dwarf_expr_fetch (ctx, 1);
  867.           in_stack_memory = dwarf_expr_fetch_in_stack_memory (ctx, 1);
  868.           break;

  869.         case DW_OP_rot:
  870.           {
  871.             struct dwarf_stack_value t1, t2, t3;

  872.             if (ctx->stack_len < 3)
  873.                error (_("Not enough elements for "
  874.                         "DW_OP_rot.  Need 3, have %d."),
  875.                       ctx->stack_len);
  876.             t1 = ctx->stack[ctx->stack_len - 1];
  877.             t2 = ctx->stack[ctx->stack_len - 2];
  878.             t3 = ctx->stack[ctx->stack_len - 3];
  879.             ctx->stack[ctx->stack_len - 1] = t2;
  880.             ctx->stack[ctx->stack_len - 2] = t3;
  881.             ctx->stack[ctx->stack_len - 3] = t1;
  882.             goto no_push;
  883.           }

  884.         case DW_OP_deref:
  885.         case DW_OP_deref_size:
  886.         case DW_OP_GNU_deref_type:
  887.           {
  888.             int addr_size = (op == DW_OP_deref ? ctx->addr_size : *op_ptr++);
  889.             gdb_byte *buf = alloca (addr_size);
  890.             CORE_ADDR addr = dwarf_expr_fetch_address (ctx, 0);
  891.             struct type *type;

  892.             dwarf_expr_pop (ctx);

  893.             if (op == DW_OP_GNU_deref_type)
  894.               {
  895.                 cu_offset type_die;

  896.                 op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
  897.                 type_die.cu_off = uoffset;
  898.                 type = dwarf_get_base_type (ctx, type_die, 0);
  899.               }
  900.             else
  901.               type = address_type;

  902.             (ctx->funcs->read_mem) (ctx->baton, buf, addr, addr_size);

  903.             /* If the size of the object read from memory is different
  904.                from the type length, we need to zero-extend it.  */
  905.             if (TYPE_LENGTH (type) != addr_size)
  906.               {
  907.                 ULONGEST result =
  908.                   extract_unsigned_integer (buf, addr_size, byte_order);

  909.                 buf = alloca (TYPE_LENGTH (type));
  910.                 store_unsigned_integer (buf, TYPE_LENGTH (type),
  911.                                         byte_order, result);
  912.               }

  913.             result_val = value_from_contents_and_address (type, buf, addr);
  914.             break;
  915.           }

  916.         case DW_OP_abs:
  917.         case DW_OP_neg:
  918.         case DW_OP_not:
  919.         case DW_OP_plus_uconst:
  920.           {
  921.             /* Unary operations.  */
  922.             result_val = dwarf_expr_fetch (ctx, 0);
  923.             dwarf_expr_pop (ctx);

  924.             switch (op)
  925.               {
  926.               case DW_OP_abs:
  927.                 if (value_less (result_val,
  928.                                 value_zero (value_type (result_val), not_lval)))
  929.                   result_val = value_neg (result_val);
  930.                 break;
  931.               case DW_OP_neg:
  932.                 result_val = value_neg (result_val);
  933.                 break;
  934.               case DW_OP_not:
  935.                 dwarf_require_integral (value_type (result_val));
  936.                 result_val = value_complement (result_val);
  937.                 break;
  938.               case DW_OP_plus_uconst:
  939.                 dwarf_require_integral (value_type (result_val));
  940.                 result = value_as_long (result_val);
  941.                 op_ptr = safe_read_uleb128 (op_ptr, op_end, &reg);
  942.                 result += reg;
  943.                 result_val = value_from_ulongest (address_type, result);
  944.                 break;
  945.               }
  946.           }
  947.           break;

  948.         case DW_OP_and:
  949.         case DW_OP_div:
  950.         case DW_OP_minus:
  951.         case DW_OP_mod:
  952.         case DW_OP_mul:
  953.         case DW_OP_or:
  954.         case DW_OP_plus:
  955.         case DW_OP_shl:
  956.         case DW_OP_shr:
  957.         case DW_OP_shra:
  958.         case DW_OP_xor:
  959.         case DW_OP_le:
  960.         case DW_OP_ge:
  961.         case DW_OP_eq:
  962.         case DW_OP_lt:
  963.         case DW_OP_gt:
  964.         case DW_OP_ne:
  965.           {
  966.             /* Binary operations.  */
  967.             struct value *first, *second;

  968.             second = dwarf_expr_fetch (ctx, 0);
  969.             dwarf_expr_pop (ctx);

  970.             first = dwarf_expr_fetch (ctx, 0);
  971.             dwarf_expr_pop (ctx);

  972.             if (! base_types_equal_p (value_type (first), value_type (second)))
  973.               error (_("Incompatible types on DWARF stack"));

  974.             switch (op)
  975.               {
  976.               case DW_OP_and:
  977.                 dwarf_require_integral (value_type (first));
  978.                 dwarf_require_integral (value_type (second));
  979.                 result_val = value_binop (first, second, BINOP_BITWISE_AND);
  980.                 break;
  981.               case DW_OP_div:
  982.                 result_val = value_binop (first, second, BINOP_DIV);
  983.                 break;
  984.               case DW_OP_minus:
  985.                 result_val = value_binop (first, second, BINOP_SUB);
  986.                 break;
  987.               case DW_OP_mod:
  988.                 {
  989.                   int cast_back = 0;
  990.                   struct type *orig_type = value_type (first);

  991.                   /* We have to special-case "old-style" untyped values
  992.                      -- these must have mod computed using unsigned
  993.                      math.  */
  994.                   if (orig_type == address_type)
  995.                     {
  996.                       struct type *utype
  997.                         = get_unsigned_type (ctx->gdbarch, orig_type);

  998.                       cast_back = 1;
  999.                       first = value_cast (utype, first);
  1000.                       second = value_cast (utype, second);
  1001.                     }
  1002.                   /* Note that value_binop doesn't handle float or
  1003.                      decimal float here.  This seems unimportant.  */
  1004.                   result_val = value_binop (first, second, BINOP_MOD);
  1005.                   if (cast_back)
  1006.                     result_val = value_cast (orig_type, result_val);
  1007.                 }
  1008.                 break;
  1009.               case DW_OP_mul:
  1010.                 result_val = value_binop (first, second, BINOP_MUL);
  1011.                 break;
  1012.               case DW_OP_or:
  1013.                 dwarf_require_integral (value_type (first));
  1014.                 dwarf_require_integral (value_type (second));
  1015.                 result_val = value_binop (first, second, BINOP_BITWISE_IOR);
  1016.                 break;
  1017.               case DW_OP_plus:
  1018.                 result_val = value_binop (first, second, BINOP_ADD);
  1019.                 break;
  1020.               case DW_OP_shl:
  1021.                 dwarf_require_integral (value_type (first));
  1022.                 dwarf_require_integral (value_type (second));
  1023.                 result_val = value_binop (first, second, BINOP_LSH);
  1024.                 break;
  1025.               case DW_OP_shr:
  1026.                 dwarf_require_integral (value_type (first));
  1027.                 dwarf_require_integral (value_type (second));
  1028.                 if (!TYPE_UNSIGNED (value_type (first)))
  1029.                   {
  1030.                     struct type *utype
  1031.                       = get_unsigned_type (ctx->gdbarch, value_type (first));

  1032.                     first = value_cast (utype, first);
  1033.                   }

  1034.                 result_val = value_binop (first, second, BINOP_RSH);
  1035.                 /* Make sure we wind up with the same type we started
  1036.                    with.  */
  1037.                 if (value_type (result_val) != value_type (second))
  1038.                   result_val = value_cast (value_type (second), result_val);
  1039.                 break;
  1040.               case DW_OP_shra:
  1041.                 dwarf_require_integral (value_type (first));
  1042.                 dwarf_require_integral (value_type (second));
  1043.                 if (TYPE_UNSIGNED (value_type (first)))
  1044.                   {
  1045.                     struct type *stype
  1046.                       = get_signed_type (ctx->gdbarch, value_type (first));

  1047.                     first = value_cast (stype, first);
  1048.                   }

  1049.                 result_val = value_binop (first, second, BINOP_RSH);
  1050.                 /* Make sure we wind up with the same type we started
  1051.                    with.  */
  1052.                 if (value_type (result_val) != value_type (second))
  1053.                   result_val = value_cast (value_type (second), result_val);
  1054.                 break;
  1055.               case DW_OP_xor:
  1056.                 dwarf_require_integral (value_type (first));
  1057.                 dwarf_require_integral (value_type (second));
  1058.                 result_val = value_binop (first, second, BINOP_BITWISE_XOR);
  1059.                 break;
  1060.               case DW_OP_le:
  1061.                 /* A <= B is !(B < A).  */
  1062.                 result = ! value_less (second, first);
  1063.                 result_val = value_from_ulongest (address_type, result);
  1064.                 break;
  1065.               case DW_OP_ge:
  1066.                 /* A >= B is !(A < B).  */
  1067.                 result = ! value_less (first, second);
  1068.                 result_val = value_from_ulongest (address_type, result);
  1069.                 break;
  1070.               case DW_OP_eq:
  1071.                 result = value_equal (first, second);
  1072.                 result_val = value_from_ulongest (address_type, result);
  1073.                 break;
  1074.               case DW_OP_lt:
  1075.                 result = value_less (first, second);
  1076.                 result_val = value_from_ulongest (address_type, result);
  1077.                 break;
  1078.               case DW_OP_gt:
  1079.                 /* A > B is B < A.  */
  1080.                 result = value_less (second, first);
  1081.                 result_val = value_from_ulongest (address_type, result);
  1082.                 break;
  1083.               case DW_OP_ne:
  1084.                 result = ! value_equal (first, second);
  1085.                 result_val = value_from_ulongest (address_type, result);
  1086.                 break;
  1087.               default:
  1088.                 internal_error (__FILE__, __LINE__,
  1089.                                 _("Can't be reached."));
  1090.               }
  1091.           }
  1092.           break;

  1093.         case DW_OP_call_frame_cfa:
  1094.           result = (ctx->funcs->get_frame_cfa) (ctx->baton);
  1095.           result_val = value_from_ulongest (address_type, result);
  1096.           in_stack_memory = 1;
  1097.           break;

  1098.         case DW_OP_GNU_push_tls_address:
  1099.           /* Variable is at a constant offset in the thread-local
  1100.           storage block into the objfile for the current thread and
  1101.           the dynamic linker module containing this expression.  Here
  1102.           we return returns the offset from that base.  The top of the
  1103.           stack has the offset from the beginning of the thread
  1104.           control block at which the variable is located.  Nothing
  1105.           should follow this operator, so the top of stack would be
  1106.           returned.  */
  1107.           result = value_as_long (dwarf_expr_fetch (ctx, 0));
  1108.           dwarf_expr_pop (ctx);
  1109.           result = (ctx->funcs->get_tls_address) (ctx->baton, result);
  1110.           result_val = value_from_ulongest (address_type, result);
  1111.           break;

  1112.         case DW_OP_skip:
  1113.           offset = extract_signed_integer (op_ptr, 2, byte_order);
  1114.           op_ptr += 2;
  1115.           op_ptr += offset;
  1116.           goto no_push;

  1117.         case DW_OP_bra:
  1118.           {
  1119.             struct value *val;

  1120.             offset = extract_signed_integer (op_ptr, 2, byte_order);
  1121.             op_ptr += 2;
  1122.             val = dwarf_expr_fetch (ctx, 0);
  1123.             dwarf_require_integral (value_type (val));
  1124.             if (value_as_long (val) != 0)
  1125.               op_ptr += offset;
  1126.             dwarf_expr_pop (ctx);
  1127.           }
  1128.           goto no_push;

  1129.         case DW_OP_nop:
  1130.           goto no_push;

  1131.         case DW_OP_piece:
  1132.           {
  1133.             uint64_t size;

  1134.             /* Record the piece.  */
  1135.             op_ptr = safe_read_uleb128 (op_ptr, op_end, &size);
  1136.             add_piece (ctx, 8 * size, 0);

  1137.             /* Pop off the address/regnum, and reset the location
  1138.                type.  */
  1139.             if (ctx->location != DWARF_VALUE_LITERAL
  1140.                 && ctx->location != DWARF_VALUE_OPTIMIZED_OUT)
  1141.               dwarf_expr_pop (ctx);
  1142.             ctx->location = DWARF_VALUE_MEMORY;
  1143.           }
  1144.           goto no_push;

  1145.         case DW_OP_bit_piece:
  1146.           {
  1147.             uint64_t size, offset;

  1148.             /* Record the piece.  */
  1149.             op_ptr = safe_read_uleb128 (op_ptr, op_end, &size);
  1150.             op_ptr = safe_read_uleb128 (op_ptr, op_end, &offset);
  1151.             add_piece (ctx, size, offset);

  1152.             /* Pop off the address/regnum, and reset the location
  1153.                type.  */
  1154.             if (ctx->location != DWARF_VALUE_LITERAL
  1155.                 && ctx->location != DWARF_VALUE_OPTIMIZED_OUT)
  1156.               dwarf_expr_pop (ctx);
  1157.             ctx->location = DWARF_VALUE_MEMORY;
  1158.           }
  1159.           goto no_push;

  1160.         case DW_OP_GNU_uninit:
  1161.           if (op_ptr != op_end)
  1162.             error (_("DWARF-2 expression error: DW_OP_GNU_uninit must always "
  1163.                    "be the very last op."));

  1164.           ctx->initialized = 0;
  1165.           goto no_push;

  1166.         case DW_OP_call2:
  1167.           {
  1168.             cu_offset offset;

  1169.             offset.cu_off = extract_unsigned_integer (op_ptr, 2, byte_order);
  1170.             op_ptr += 2;
  1171.             ctx->funcs->dwarf_call (ctx, offset);
  1172.           }
  1173.           goto no_push;

  1174.         case DW_OP_call4:
  1175.           {
  1176.             cu_offset offset;

  1177.             offset.cu_off = extract_unsigned_integer (op_ptr, 4, byte_order);
  1178.             op_ptr += 4;
  1179.             ctx->funcs->dwarf_call (ctx, offset);
  1180.           }
  1181.           goto no_push;

  1182.         case DW_OP_GNU_entry_value:
  1183.           {
  1184.             uint64_t len;
  1185.             CORE_ADDR deref_size;
  1186.             union call_site_parameter_u kind_u;

  1187.             op_ptr = safe_read_uleb128 (op_ptr, op_end, &len);
  1188.             if (op_ptr + len > op_end)
  1189.               error (_("DW_OP_GNU_entry_value: too few bytes available."));

  1190.             kind_u.dwarf_reg = dwarf_block_to_dwarf_reg (op_ptr, op_ptr + len);
  1191.             if (kind_u.dwarf_reg != -1)
  1192.               {
  1193.                 op_ptr += len;
  1194.                 ctx->funcs->push_dwarf_reg_entry_value (ctx,
  1195.                                                   CALL_SITE_PARAMETER_DWARF_REG,
  1196.                                                         kind_u,
  1197.                                                         -1 /* deref_size */);
  1198.                 goto no_push;
  1199.               }

  1200.             kind_u.dwarf_reg = dwarf_block_to_dwarf_reg_deref (op_ptr,
  1201.                                                                op_ptr + len,
  1202.                                                                &deref_size);
  1203.             if (kind_u.dwarf_reg != -1)
  1204.               {
  1205.                 if (deref_size == -1)
  1206.                   deref_size = ctx->addr_size;
  1207.                 op_ptr += len;
  1208.                 ctx->funcs->push_dwarf_reg_entry_value (ctx,
  1209.                                                   CALL_SITE_PARAMETER_DWARF_REG,
  1210.                                                         kind_u, deref_size);
  1211.                 goto no_push;
  1212.               }

  1213.             error (_("DWARF-2 expression error: DW_OP_GNU_entry_value is "
  1214.                      "supported only for single DW_OP_reg* "
  1215.                      "or for DW_OP_breg*(0)+DW_OP_deref*"));
  1216.           }

  1217.         case DW_OP_GNU_parameter_ref:
  1218.           {
  1219.             union call_site_parameter_u kind_u;

  1220.             kind_u.param_offset.cu_off = extract_unsigned_integer (op_ptr, 4,
  1221.                                                                    byte_order);
  1222.             op_ptr += 4;
  1223.             ctx->funcs->push_dwarf_reg_entry_value (ctx,
  1224.                                                CALL_SITE_PARAMETER_PARAM_OFFSET,
  1225.                                                     kind_u,
  1226.                                                     -1 /* deref_size */);
  1227.           }
  1228.           goto no_push;

  1229.         case DW_OP_GNU_const_type:
  1230.           {
  1231.             cu_offset type_die;
  1232.             int n;
  1233.             const gdb_byte *data;
  1234.             struct type *type;

  1235.             op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
  1236.             type_die.cu_off = uoffset;
  1237.             n = *op_ptr++;
  1238.             data = op_ptr;
  1239.             op_ptr += n;

  1240.             type = dwarf_get_base_type (ctx, type_die, n);
  1241.             result_val = value_from_contents (type, data);
  1242.           }
  1243.           break;

  1244.         case DW_OP_GNU_regval_type:
  1245.           {
  1246.             cu_offset type_die;
  1247.             struct type *type;

  1248.             op_ptr = safe_read_uleb128 (op_ptr, op_end, &reg);
  1249.             op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
  1250.             type_die.cu_off = uoffset;

  1251.             type = dwarf_get_base_type (ctx, type_die, 0);
  1252.             result_val = ctx->funcs->get_reg_value (ctx->baton, type, reg);
  1253.           }
  1254.           break;

  1255.         case DW_OP_GNU_convert:
  1256.         case DW_OP_GNU_reinterpret:
  1257.           {
  1258.             cu_offset type_die;
  1259.             struct type *type;

  1260.             op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
  1261.             type_die.cu_off = uoffset;

  1262.             if (type_die.cu_off == 0)
  1263.               type = address_type;
  1264.             else
  1265.               type = dwarf_get_base_type (ctx, type_die, 0);

  1266.             result_val = dwarf_expr_fetch (ctx, 0);
  1267.             dwarf_expr_pop (ctx);

  1268.             if (op == DW_OP_GNU_convert)
  1269.               result_val = value_cast (type, result_val);
  1270.             else if (type == value_type (result_val))
  1271.               {
  1272.                 /* Nothing.  */
  1273.               }
  1274.             else if (TYPE_LENGTH (type)
  1275.                      != TYPE_LENGTH (value_type (result_val)))
  1276.               error (_("DW_OP_GNU_reinterpret has wrong size"));
  1277.             else
  1278.               result_val
  1279.                 = value_from_contents (type,
  1280.                                        value_contents_all (result_val));
  1281.           }
  1282.           break;

  1283.         case DW_OP_push_object_address:
  1284.           /* Return the address of the object we are currently observing.  */
  1285.           result = (ctx->funcs->get_object_address) (ctx->baton);
  1286.           result_val = value_from_ulongest (address_type, result);
  1287.           break;

  1288.         default:
  1289.           error (_("Unhandled dwarf expression opcode 0x%x"), op);
  1290.         }

  1291.       /* Most things push a result value.  */
  1292.       gdb_assert (result_val != NULL);
  1293.       dwarf_expr_push (ctx, result_val, in_stack_memory);
  1294.     no_push:
  1295.       ;
  1296.     }

  1297.   /* To simplify our main caller, if the result is an implicit
  1298.      pointer, then make a pieced value.  This is ok because we can't
  1299.      have implicit pointers in contexts where pieces are invalid.  */
  1300.   if (ctx->location == DWARF_VALUE_IMPLICIT_POINTER)
  1301.     add_piece (ctx, 8 * ctx->addr_size, 0);

  1302. abort_expression:
  1303.   ctx->recursion_depth--;
  1304.   gdb_assert (ctx->recursion_depth >= 0);
  1305. }

  1306. /* Stub dwarf_expr_context_funcs.get_frame_base implementation.  */

  1307. void
  1308. ctx_no_get_frame_base (void *baton, const gdb_byte **start, size_t *length)
  1309. {
  1310.   error (_("%s is invalid in this context"), "DW_OP_fbreg");
  1311. }

  1312. /* Stub dwarf_expr_context_funcs.get_frame_cfa implementation.  */

  1313. CORE_ADDR
  1314. ctx_no_get_frame_cfa (void *baton)
  1315. {
  1316.   error (_("%s is invalid in this context"), "DW_OP_call_frame_cfa");
  1317. }

  1318. /* Stub dwarf_expr_context_funcs.get_frame_pc implementation.  */

  1319. CORE_ADDR
  1320. ctx_no_get_frame_pc (void *baton)
  1321. {
  1322.   error (_("%s is invalid in this context"), "DW_OP_GNU_implicit_pointer");
  1323. }

  1324. /* Stub dwarf_expr_context_funcs.get_tls_address implementation.  */

  1325. CORE_ADDR
  1326. ctx_no_get_tls_address (void *baton, CORE_ADDR offset)
  1327. {
  1328.   error (_("%s is invalid in this context"), "DW_OP_GNU_push_tls_address");
  1329. }

  1330. /* Stub dwarf_expr_context_funcs.dwarf_call implementation.  */

  1331. void
  1332. ctx_no_dwarf_call (struct dwarf_expr_context *ctx, cu_offset die_offset)
  1333. {
  1334.   error (_("%s is invalid in this context"), "DW_OP_call*");
  1335. }

  1336. /* Stub dwarf_expr_context_funcs.get_base_type implementation.  */

  1337. struct type *
  1338. ctx_no_get_base_type (struct dwarf_expr_context *ctx, cu_offset die)
  1339. {
  1340.   error (_("Support for typed DWARF is not supported in this context"));
  1341. }

  1342. /* Stub dwarf_expr_context_funcs.push_dwarf_block_entry_value
  1343.    implementation.  */

  1344. void
  1345. ctx_no_push_dwarf_reg_entry_value (struct dwarf_expr_context *ctx,
  1346.                                    enum call_site_parameter_kind kind,
  1347.                                    union call_site_parameter_u kind_u,
  1348.                                    int deref_size)
  1349. {
  1350.   internal_error (__FILE__, __LINE__,
  1351.                   _("Support for DW_OP_GNU_entry_value is unimplemented"));
  1352. }

  1353. /* Stub dwarf_expr_context_funcs.get_addr_index implementation.  */

  1354. CORE_ADDR
  1355. ctx_no_get_addr_index (void *baton, unsigned int index)
  1356. {
  1357.   error (_("%s is invalid in this context"), "DW_OP_GNU_addr_index");
  1358. }

  1359. /* Provide a prototype to silence -Wmissing-prototypes.  */
  1360. extern initialize_file_ftype _initialize_dwarf2expr;

  1361. void
  1362. _initialize_dwarf2expr (void)
  1363. {
  1364.   dwarf_arch_cookie
  1365.     = gdbarch_data_register_post_init (dwarf_gdbarch_types_init);
  1366. }