gdb/dwarf2loc.c - gdb

Global variables defined

Data types defined

Functions defined

Source code

  1. /* DWARF 2 location expression support for GDB.

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

  3.    Contributed by Daniel Jacobowitz, MontaVista Software, Inc.

  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 "ui-out.h"
  17. #include "value.h"
  18. #include "frame.h"
  19. #include "gdbcore.h"
  20. #include "target.h"
  21. #include "inferior.h"
  22. #include "ax.h"
  23. #include "ax-gdb.h"
  24. #include "regcache.h"
  25. #include "objfiles.h"
  26. #include "block.h"
  27. #include "gdbcmd.h"

  28. #include "dwarf2.h"
  29. #include "dwarf2expr.h"
  30. #include "dwarf2loc.h"
  31. #include "dwarf2-frame.h"
  32. #include "compile/compile.h"

  33. extern int dwarf2_always_disassemble;

  34. static const struct dwarf_expr_context_funcs dwarf_expr_ctx_funcs;

  35. static struct value *dwarf2_evaluate_loc_desc_full (struct type *type,
  36.                                                     struct frame_info *frame,
  37.                                                     const gdb_byte *data,
  38.                                                     size_t size,
  39.                                                     struct dwarf2_per_cu_data *per_cu,
  40.                                                     LONGEST byte_offset);

  41. /* Until these have formal names, we define these here.
  42.    ref: http://gcc.gnu.org/wiki/DebugFission
  43.    Each entry in .debug_loc.dwo begins with a byte that describes the entry,
  44.    and is then followed by data specific to that entry.  */

  45. enum debug_loc_kind
  46. {
  47.   /* Indicates the end of the list of entries.  */
  48.   DEBUG_LOC_END_OF_LIST = 0,

  49.   /* This is followed by an unsigned LEB128 number that is an index into
  50.      .debug_addr and specifies the base address for all following entries.  */
  51.   DEBUG_LOC_BASE_ADDRESS = 1,

  52.   /* This is followed by two unsigned LEB128 numbers that are indices into
  53.      .debug_addr and specify the beginning and ending addresses, and then
  54.      a normal location expression as in .debug_loc.  */
  55.   DEBUG_LOC_START_END = 2,

  56.   /* This is followed by an unsigned LEB128 number that is an index into
  57.      .debug_addr and specifies the beginning address, and a 4 byte unsigned
  58.      number that specifies the length, and then a normal location expression
  59.      as in .debug_loc.  */
  60.   DEBUG_LOC_START_LENGTH = 3,

  61.   /* An internal value indicating there is insufficient data.  */
  62.   DEBUG_LOC_BUFFER_OVERFLOW = -1,

  63.   /* An internal value indicating an invalid kind of entry was found.  */
  64.   DEBUG_LOC_INVALID_ENTRY = -2
  65. };

  66. /* Helper function which throws an error if a synthetic pointer is
  67.    invalid.  */

  68. static void
  69. invalid_synthetic_pointer (void)
  70. {
  71.   error (_("access outside bounds of object "
  72.            "referenced via synthetic pointer"));
  73. }

  74. /* Decode the addresses in a non-dwo .debug_loc entry.
  75.    A pointer to the next byte to examine is returned in *NEW_PTR.
  76.    The encoded low,high addresses are return in *LOW,*HIGH.
  77.    The result indicates the kind of entry found.  */

  78. static enum debug_loc_kind
  79. decode_debug_loc_addresses (const gdb_byte *loc_ptr, const gdb_byte *buf_end,
  80.                             const gdb_byte **new_ptr,
  81.                             CORE_ADDR *low, CORE_ADDR *high,
  82.                             enum bfd_endian byte_order,
  83.                             unsigned int addr_size,
  84.                             int signed_addr_p)
  85. {
  86.   CORE_ADDR base_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));

  87.   if (buf_end - loc_ptr < 2 * addr_size)
  88.     return DEBUG_LOC_BUFFER_OVERFLOW;

  89.   if (signed_addr_p)
  90.     *low = extract_signed_integer (loc_ptr, addr_size, byte_order);
  91.   else
  92.     *low = extract_unsigned_integer (loc_ptr, addr_size, byte_order);
  93.   loc_ptr += addr_size;

  94.   if (signed_addr_p)
  95.     *high = extract_signed_integer (loc_ptr, addr_size, byte_order);
  96.   else
  97.     *high = extract_unsigned_integer (loc_ptr, addr_size, byte_order);
  98.   loc_ptr += addr_size;

  99.   *new_ptr = loc_ptr;

  100.   /* A base-address-selection entry.  */
  101.   if ((*low & base_mask) == base_mask)
  102.     return DEBUG_LOC_BASE_ADDRESS;

  103.   /* An end-of-list entry.  */
  104.   if (*low == 0 && *high == 0)
  105.     return DEBUG_LOC_END_OF_LIST;

  106.   return DEBUG_LOC_START_END;
  107. }

  108. /* Decode the addresses in .debug_loc.dwo entry.
  109.    A pointer to the next byte to examine is returned in *NEW_PTR.
  110.    The encoded low,high addresses are return in *LOW,*HIGH.
  111.    The result indicates the kind of entry found.  */

  112. static enum debug_loc_kind
  113. decode_debug_loc_dwo_addresses (struct dwarf2_per_cu_data *per_cu,
  114.                                 const gdb_byte *loc_ptr,
  115.                                 const gdb_byte *buf_end,
  116.                                 const gdb_byte **new_ptr,
  117.                                 CORE_ADDR *low, CORE_ADDR *high,
  118.                                 enum bfd_endian byte_order)
  119. {
  120.   uint64_t low_index, high_index;

  121.   if (loc_ptr == buf_end)
  122.     return DEBUG_LOC_BUFFER_OVERFLOW;

  123.   switch (*loc_ptr++)
  124.     {
  125.     case DEBUG_LOC_END_OF_LIST:
  126.       *new_ptr = loc_ptr;
  127.       return DEBUG_LOC_END_OF_LIST;
  128.     case DEBUG_LOC_BASE_ADDRESS:
  129.       *low = 0;
  130.       loc_ptr = gdb_read_uleb128 (loc_ptr, buf_end, &high_index);
  131.       if (loc_ptr == NULL)
  132.         return DEBUG_LOC_BUFFER_OVERFLOW;
  133.       *high = dwarf2_read_addr_index (per_cu, high_index);
  134.       *new_ptr = loc_ptr;
  135.       return DEBUG_LOC_BASE_ADDRESS;
  136.     case DEBUG_LOC_START_END:
  137.       loc_ptr = gdb_read_uleb128 (loc_ptr, buf_end, &low_index);
  138.       if (loc_ptr == NULL)
  139.         return DEBUG_LOC_BUFFER_OVERFLOW;
  140.       *low = dwarf2_read_addr_index (per_cu, low_index);
  141.       loc_ptr = gdb_read_uleb128 (loc_ptr, buf_end, &high_index);
  142.       if (loc_ptr == NULL)
  143.         return DEBUG_LOC_BUFFER_OVERFLOW;
  144.       *high = dwarf2_read_addr_index (per_cu, high_index);
  145.       *new_ptr = loc_ptr;
  146.       return DEBUG_LOC_START_END;
  147.     case DEBUG_LOC_START_LENGTH:
  148.       loc_ptr = gdb_read_uleb128 (loc_ptr, buf_end, &low_index);
  149.       if (loc_ptr == NULL)
  150.         return DEBUG_LOC_BUFFER_OVERFLOW;
  151.       *low = dwarf2_read_addr_index (per_cu, low_index);
  152.       if (loc_ptr + 4 > buf_end)
  153.         return DEBUG_LOC_BUFFER_OVERFLOW;
  154.       *high = *low;
  155.       *high += extract_unsigned_integer (loc_ptr, 4, byte_order);
  156.       *new_ptr = loc_ptr + 4;
  157.       return DEBUG_LOC_START_LENGTH;
  158.     default:
  159.       return DEBUG_LOC_INVALID_ENTRY;
  160.     }
  161. }

  162. /* A function for dealing with location lists.  Given a
  163.    symbol baton (BATON) and a pc value (PC), find the appropriate
  164.    location expression, set *LOCEXPR_LENGTH, and return a pointer
  165.    to the beginning of the expression.  Returns NULL on failure.

  166.    For now, only return the first matching location expression; there
  167.    can be more than one in the list.  */

  168. const gdb_byte *
  169. dwarf2_find_location_expression (struct dwarf2_loclist_baton *baton,
  170.                                  size_t *locexpr_length, CORE_ADDR pc)
  171. {
  172.   struct objfile *objfile = dwarf2_per_cu_objfile (baton->per_cu);
  173.   struct gdbarch *gdbarch = get_objfile_arch (objfile);
  174.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  175.   unsigned int addr_size = dwarf2_per_cu_addr_size (baton->per_cu);
  176.   int signed_addr_p = bfd_get_sign_extend_vma (objfile->obfd);
  177.   /* Adjust base_address for relocatable objects.  */
  178.   CORE_ADDR base_offset = dwarf2_per_cu_text_offset (baton->per_cu);
  179.   CORE_ADDR base_address = baton->base_address + base_offset;
  180.   const gdb_byte *loc_ptr, *buf_end;

  181.   loc_ptr = baton->data;
  182.   buf_end = baton->data + baton->size;

  183.   while (1)
  184.     {
  185.       CORE_ADDR low = 0, high = 0; /* init for gcc -Wall */
  186.       int length;
  187.       enum debug_loc_kind kind;
  188.       const gdb_byte *new_ptr = NULL; /* init for gcc -Wall */

  189.       if (baton->from_dwo)
  190.         kind = decode_debug_loc_dwo_addresses (baton->per_cu,
  191.                                                loc_ptr, buf_end, &new_ptr,
  192.                                                &low, &high, byte_order);
  193.       else
  194.         kind = decode_debug_loc_addresses (loc_ptr, buf_end, &new_ptr,
  195.                                            &low, &high,
  196.                                            byte_order, addr_size,
  197.                                            signed_addr_p);
  198.       loc_ptr = new_ptr;
  199.       switch (kind)
  200.         {
  201.         case DEBUG_LOC_END_OF_LIST:
  202.           *locexpr_length = 0;
  203.           return NULL;
  204.         case DEBUG_LOC_BASE_ADDRESS:
  205.           base_address = high + base_offset;
  206.           continue;
  207.         case DEBUG_LOC_START_END:
  208.         case DEBUG_LOC_START_LENGTH:
  209.           break;
  210.         case DEBUG_LOC_BUFFER_OVERFLOW:
  211.         case DEBUG_LOC_INVALID_ENTRY:
  212.           error (_("dwarf2_find_location_expression: "
  213.                    "Corrupted DWARF expression."));
  214.         default:
  215.           gdb_assert_not_reached ("bad debug_loc_kind");
  216.         }

  217.       /* Otherwise, a location expression entry.
  218.          If the entry is from a DWO, don't add base address: the entry is
  219.          from .debug_addr which has absolute addresses.  */
  220.       if (! baton->from_dwo)
  221.         {
  222.           low += base_address;
  223.           high += base_address;
  224.         }

  225.       length = extract_unsigned_integer (loc_ptr, 2, byte_order);
  226.       loc_ptr += 2;

  227.       if (low == high && pc == low)
  228.         {
  229.           /* This is entry PC record present only at entry point
  230.              of a function.  Verify it is really the function entry point.  */

  231.           const struct block *pc_block = block_for_pc (pc);
  232.           struct symbol *pc_func = NULL;

  233.           if (pc_block)
  234.             pc_func = block_linkage_function (pc_block);

  235.           if (pc_func && pc == BLOCK_START (SYMBOL_BLOCK_VALUE (pc_func)))
  236.             {
  237.               *locexpr_length = length;
  238.               return loc_ptr;
  239.             }
  240.         }

  241.       if (pc >= low && pc < high)
  242.         {
  243.           *locexpr_length = length;
  244.           return loc_ptr;
  245.         }

  246.       loc_ptr += length;
  247.     }
  248. }

  249. /* This is the baton used when performing dwarf2 expression
  250.    evaluation.  */
  251. struct dwarf_expr_baton
  252. {
  253.   struct frame_info *frame;
  254.   struct dwarf2_per_cu_data *per_cu;
  255.   CORE_ADDR obj_address;
  256. };

  257. /* Helper functions for dwarf2_evaluate_loc_desc.  */

  258. /* Using the frame specified in BATON, return the value of register
  259.    REGNUM, treated as a pointer.  */
  260. static CORE_ADDR
  261. dwarf_expr_read_addr_from_reg (void *baton, int dwarf_regnum)
  262. {
  263.   struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
  264.   struct gdbarch *gdbarch = get_frame_arch (debaton->frame);
  265.   int regnum = gdbarch_dwarf2_reg_to_regnum (gdbarch, dwarf_regnum);

  266.   return address_from_register (regnum, debaton->frame);
  267. }

  268. /* Implement struct dwarf_expr_context_funcs' "get_reg_value" callback.  */

  269. static struct value *
  270. dwarf_expr_get_reg_value (void *baton, struct type *type, int dwarf_regnum)
  271. {
  272.   struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
  273.   struct gdbarch *gdbarch = get_frame_arch (debaton->frame);
  274.   int regnum = gdbarch_dwarf2_reg_to_regnum (gdbarch, dwarf_regnum);

  275.   return value_from_register (type, regnum, debaton->frame);
  276. }

  277. /* Read memory at ADDR (length LEN) into BUF.  */

  278. static void
  279. dwarf_expr_read_mem (void *baton, gdb_byte *buf, CORE_ADDR addr, size_t len)
  280. {
  281.   read_memory (addr, buf, len);
  282. }

  283. /* Using the frame specified in BATON, find the location expression
  284.    describing the frame base.  Return a pointer to it in START and
  285.    its length in LENGTH.  */
  286. static void
  287. dwarf_expr_frame_base (void *baton, const gdb_byte **start, size_t * length)
  288. {
  289.   /* FIXME: cagney/2003-03-26: This code should be using
  290.      get_frame_base_address(), and then implement a dwarf2 specific
  291.      this_base method.  */
  292.   struct symbol *framefunc;
  293.   struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
  294.   const struct block *bl = get_frame_block (debaton->frame, NULL);

  295.   if (bl == NULL)
  296.     error (_("frame address is not available."));

  297.   /* Use block_linkage_function, which returns a real (not inlined)
  298.      function, instead of get_frame_function, which may return an
  299.      inlined function.  */
  300.   framefunc = block_linkage_function (bl);

  301.   /* If we found a frame-relative symbol then it was certainly within
  302.      some function associated with a frame. If we can't find the frame,
  303.      something has gone wrong.  */
  304.   gdb_assert (framefunc != NULL);

  305.   func_get_frame_base_dwarf_block (framefunc,
  306.                                    get_frame_address_in_block (debaton->frame),
  307.                                    start, length);
  308. }

  309. /* Implement find_frame_base_location method for LOC_BLOCK functions using
  310.    DWARF expression for its DW_AT_frame_base.  */

  311. static void
  312. locexpr_find_frame_base_location (struct symbol *framefunc, CORE_ADDR pc,
  313.                                   const gdb_byte **start, size_t *length)
  314. {
  315.   struct dwarf2_locexpr_baton *symbaton = SYMBOL_LOCATION_BATON (framefunc);

  316.   *length = symbaton->size;
  317.   *start = symbaton->data;
  318. }

  319. /* Vector for inferior functions as represented by LOC_BLOCK, if the inferior
  320.    function uses DWARF expression for its DW_AT_frame_base.  */

  321. const struct symbol_block_ops dwarf2_block_frame_base_locexpr_funcs =
  322. {
  323.   locexpr_find_frame_base_location
  324. };

  325. /* Implement find_frame_base_location method for LOC_BLOCK functions using
  326.    DWARF location list for its DW_AT_frame_base.  */

  327. static void
  328. loclist_find_frame_base_location (struct symbol *framefunc, CORE_ADDR pc,
  329.                                   const gdb_byte **start, size_t *length)
  330. {
  331.   struct dwarf2_loclist_baton *symbaton = SYMBOL_LOCATION_BATON (framefunc);

  332.   *start = dwarf2_find_location_expression (symbaton, length, pc);
  333. }

  334. /* Vector for inferior functions as represented by LOC_BLOCK, if the inferior
  335.    function uses DWARF location list for its DW_AT_frame_base.  */

  336. const struct symbol_block_ops dwarf2_block_frame_base_loclist_funcs =
  337. {
  338.   loclist_find_frame_base_location
  339. };

  340. /* See dwarf2loc.h.  */

  341. void
  342. func_get_frame_base_dwarf_block (struct symbol *framefunc, CORE_ADDR pc,
  343.                                  const gdb_byte **start, size_t *length)
  344. {
  345.   if (SYMBOL_BLOCK_OPS (framefunc) != NULL)
  346.     {
  347.       const struct symbol_block_ops *ops_block = SYMBOL_BLOCK_OPS (framefunc);

  348.       ops_block->find_frame_base_location (framefunc, pc, start, length);
  349.     }
  350.   else
  351.     *length = 0;

  352.   if (*length == 0)
  353.     error (_("Could not find the frame base for \"%s\"."),
  354.            SYMBOL_NATURAL_NAME (framefunc));
  355. }

  356. /* Helper function for dwarf2_evaluate_loc_desc.  Computes the CFA for
  357.    the frame in BATON.  */

  358. static CORE_ADDR
  359. dwarf_expr_frame_cfa (void *baton)
  360. {
  361.   struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;

  362.   return dwarf2_frame_cfa (debaton->frame);
  363. }

  364. /* Helper function for dwarf2_evaluate_loc_desc.  Computes the PC for
  365.    the frame in BATON.  */

  366. static CORE_ADDR
  367. dwarf_expr_frame_pc (void *baton)
  368. {
  369.   struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;

  370.   return get_frame_address_in_block (debaton->frame);
  371. }

  372. /* Using the objfile specified in BATON, find the address for the
  373.    current thread's thread-local storage with offset OFFSET.  */
  374. static CORE_ADDR
  375. dwarf_expr_tls_address (void *baton, CORE_ADDR offset)
  376. {
  377.   struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
  378.   struct objfile *objfile = dwarf2_per_cu_objfile (debaton->per_cu);

  379.   return target_translate_tls_address (objfile, offset);
  380. }

  381. /* Call DWARF subroutine from DW_AT_location of DIE at DIE_OFFSET in
  382.    current CU (as is PER_CU).  State of the CTX is not affected by the
  383.    call and return.  */

  384. static void
  385. per_cu_dwarf_call (struct dwarf_expr_context *ctx, cu_offset die_offset,
  386.                    struct dwarf2_per_cu_data *per_cu,
  387.                    CORE_ADDR (*get_frame_pc) (void *baton),
  388.                    void *baton)
  389. {
  390.   struct dwarf2_locexpr_baton block;

  391.   block = dwarf2_fetch_die_loc_cu_off (die_offset, per_cu, get_frame_pc, baton);

  392.   /* DW_OP_call_ref is currently not supported.  */
  393.   gdb_assert (block.per_cu == per_cu);

  394.   dwarf_expr_eval (ctx, block.data, block.size);
  395. }

  396. /* Helper interface of per_cu_dwarf_call for dwarf2_evaluate_loc_desc.  */

  397. static void
  398. dwarf_expr_dwarf_call (struct dwarf_expr_context *ctx, cu_offset die_offset)
  399. {
  400.   struct dwarf_expr_baton *debaton = ctx->baton;

  401.   per_cu_dwarf_call (ctx, die_offset, debaton->per_cu,
  402.                      ctx->funcs->get_frame_pc, ctx->baton);
  403. }

  404. /* Callback function for dwarf2_evaluate_loc_desc.  */

  405. static struct type *
  406. dwarf_expr_get_base_type (struct dwarf_expr_context *ctx,
  407.                           cu_offset die_offset)
  408. {
  409.   struct dwarf_expr_baton *debaton = ctx->baton;

  410.   return dwarf2_get_die_type (die_offset, debaton->per_cu);
  411. }

  412. /* See dwarf2loc.h.  */

  413. unsigned int entry_values_debug = 0;

  414. /* Helper to set entry_values_debug.  */

  415. static void
  416. show_entry_values_debug (struct ui_file *file, int from_tty,
  417.                          struct cmd_list_element *c, const char *value)
  418. {
  419.   fprintf_filtered (file,
  420.                     _("Entry values and tail call frames debugging is %s.\n"),
  421.                     value);
  422. }

  423. /* Find DW_TAG_GNU_call_site's DW_AT_GNU_call_site_target address.
  424.    CALLER_FRAME (for registers) can be NULL if it is not known.  This function
  425.    always returns valid address or it throws NO_ENTRY_VALUE_ERROR.  */

  426. static CORE_ADDR
  427. call_site_to_target_addr (struct gdbarch *call_site_gdbarch,
  428.                           struct call_site *call_site,
  429.                           struct frame_info *caller_frame)
  430. {
  431.   switch (FIELD_LOC_KIND (call_site->target))
  432.     {
  433.     case FIELD_LOC_KIND_DWARF_BLOCK:
  434.       {
  435.         struct dwarf2_locexpr_baton *dwarf_block;
  436.         struct value *val;
  437.         struct type *caller_core_addr_type;
  438.         struct gdbarch *caller_arch;

  439.         dwarf_block = FIELD_DWARF_BLOCK (call_site->target);
  440.         if (dwarf_block == NULL)
  441.           {
  442.             struct bound_minimal_symbol msym;

  443.             msym = lookup_minimal_symbol_by_pc (call_site->pc - 1);
  444.             throw_error (NO_ENTRY_VALUE_ERROR,
  445.                          _("DW_AT_GNU_call_site_target is not specified "
  446.                            "at %s in %s"),
  447.                          paddress (call_site_gdbarch, call_site->pc),
  448.                          (msym.minsym == NULL ? "???"
  449.                           : MSYMBOL_PRINT_NAME (msym.minsym)));

  450.           }
  451.         if (caller_frame == NULL)
  452.           {
  453.             struct bound_minimal_symbol msym;

  454.             msym = lookup_minimal_symbol_by_pc (call_site->pc - 1);
  455.             throw_error (NO_ENTRY_VALUE_ERROR,
  456.                          _("DW_AT_GNU_call_site_target DWARF block resolving "
  457.                            "requires known frame which is currently not "
  458.                            "available at %s in %s"),
  459.                          paddress (call_site_gdbarch, call_site->pc),
  460.                          (msym.minsym == NULL ? "???"
  461.                           : MSYMBOL_PRINT_NAME (msym.minsym)));

  462.           }
  463.         caller_arch = get_frame_arch (caller_frame);
  464.         caller_core_addr_type = builtin_type (caller_arch)->builtin_func_ptr;
  465.         val = dwarf2_evaluate_loc_desc (caller_core_addr_type, caller_frame,
  466.                                         dwarf_block->data, dwarf_block->size,
  467.                                         dwarf_block->per_cu);
  468.         /* DW_AT_GNU_call_site_target is a DWARF expression, not a DWARF
  469.            location.  */
  470.         if (VALUE_LVAL (val) == lval_memory)
  471.           return value_address (val);
  472.         else
  473.           return value_as_address (val);
  474.       }

  475.     case FIELD_LOC_KIND_PHYSNAME:
  476.       {
  477.         const char *physname;
  478.         struct bound_minimal_symbol msym;

  479.         physname = FIELD_STATIC_PHYSNAME (call_site->target);

  480.         /* Handle both the mangled and demangled PHYSNAME.  */
  481.         msym = lookup_minimal_symbol (physname, NULL, NULL);
  482.         if (msym.minsym == NULL)
  483.           {
  484.             msym = lookup_minimal_symbol_by_pc (call_site->pc - 1);
  485.             throw_error (NO_ENTRY_VALUE_ERROR,
  486.                          _("Cannot find function \"%s\" for a call site target "
  487.                            "at %s in %s"),
  488.                          physname, paddress (call_site_gdbarch, call_site->pc),
  489.                          (msym.minsym == NULL ? "???"
  490.                           : MSYMBOL_PRINT_NAME (msym.minsym)));

  491.           }
  492.         return BMSYMBOL_VALUE_ADDRESS (msym);
  493.       }

  494.     case FIELD_LOC_KIND_PHYSADDR:
  495.       return FIELD_STATIC_PHYSADDR (call_site->target);

  496.     default:
  497.       internal_error (__FILE__, __LINE__, _("invalid call site target kind"));
  498.     }
  499. }

  500. /* Convert function entry point exact address ADDR to the function which is
  501.    compliant with TAIL_CALL_LIST_COMPLETE condition.  Throw
  502.    NO_ENTRY_VALUE_ERROR otherwise.  */

  503. static struct symbol *
  504. func_addr_to_tail_call_list (struct gdbarch *gdbarch, CORE_ADDR addr)
  505. {
  506.   struct symbol *sym = find_pc_function (addr);
  507.   struct type *type;

  508.   if (sym == NULL || BLOCK_START (SYMBOL_BLOCK_VALUE (sym)) != addr)
  509.     throw_error (NO_ENTRY_VALUE_ERROR,
  510.                  _("DW_TAG_GNU_call_site resolving failed to find function "
  511.                    "name for address %s"),
  512.                  paddress (gdbarch, addr));

  513.   type = SYMBOL_TYPE (sym);
  514.   gdb_assert (TYPE_CODE (type) == TYPE_CODE_FUNC);
  515.   gdb_assert (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_FUNC);

  516.   return sym;
  517. }

  518. /* Verify function with entry point exact address ADDR can never call itself
  519.    via its tail calls (incl. transitively).  Throw NO_ENTRY_VALUE_ERROR if it
  520.    can call itself via tail calls.

  521.    If a funtion can tail call itself its entry value based parameters are
  522.    unreliable.  There is no verification whether the value of some/all
  523.    parameters is unchanged through the self tail call, we expect if there is
  524.    a self tail call all the parameters can be modified.  */

  525. static void
  526. func_verify_no_selftailcall (struct gdbarch *gdbarch, CORE_ADDR verify_addr)
  527. {
  528.   struct obstack addr_obstack;
  529.   struct cleanup *old_chain;
  530.   CORE_ADDR addr;

  531.   /* Track here CORE_ADDRs which were already visited.  */
  532.   htab_t addr_hash;

  533.   /* The verification is completely unordered.  Track here function addresses
  534.      which still need to be iterated.  */
  535.   VEC (CORE_ADDR) *todo = NULL;

  536.   obstack_init (&addr_obstack);
  537.   old_chain = make_cleanup_obstack_free (&addr_obstack);
  538.   addr_hash = htab_create_alloc_ex (64, core_addr_hash, core_addr_eq, NULL,
  539.                                     &addr_obstack, hashtab_obstack_allocate,
  540.                                     NULL);
  541.   make_cleanup_htab_delete (addr_hash);

  542.   make_cleanup (VEC_cleanup (CORE_ADDR), &todo);

  543.   VEC_safe_push (CORE_ADDR, todo, verify_addr);
  544.   while (!VEC_empty (CORE_ADDR, todo))
  545.     {
  546.       struct symbol *func_sym;
  547.       struct call_site *call_site;

  548.       addr = VEC_pop (CORE_ADDR, todo);

  549.       func_sym = func_addr_to_tail_call_list (gdbarch, addr);

  550.       for (call_site = TYPE_TAIL_CALL_LIST (SYMBOL_TYPE (func_sym));
  551.            call_site; call_site = call_site->tail_call_next)
  552.         {
  553.           CORE_ADDR target_addr;
  554.           void **slot;

  555.           /* CALLER_FRAME with registers is not available for tail-call jumped
  556.              frames.  */
  557.           target_addr = call_site_to_target_addr (gdbarch, call_site, NULL);

  558.           if (target_addr == verify_addr)
  559.             {
  560.               struct bound_minimal_symbol msym;

  561.               msym = lookup_minimal_symbol_by_pc (verify_addr);
  562.               throw_error (NO_ENTRY_VALUE_ERROR,
  563.                            _("DW_OP_GNU_entry_value resolving has found "
  564.                              "function \"%s\" at %s can call itself via tail "
  565.                              "calls"),
  566.                            (msym.minsym == NULL ? "???"
  567.                             : MSYMBOL_PRINT_NAME (msym.minsym)),
  568.                            paddress (gdbarch, verify_addr));
  569.             }

  570.           slot = htab_find_slot (addr_hash, &target_addr, INSERT);
  571.           if (*slot == NULL)
  572.             {
  573.               *slot = obstack_copy (&addr_obstack, &target_addr,
  574.                                     sizeof (target_addr));
  575.               VEC_safe_push (CORE_ADDR, todo, target_addr);
  576.             }
  577.         }
  578.     }

  579.   do_cleanups (old_chain);
  580. }

  581. /* Print user readable form of CALL_SITE->PC to gdb_stdlog.  Used only for
  582.    ENTRY_VALUES_DEBUG.  */

  583. static void
  584. tailcall_dump (struct gdbarch *gdbarch, const struct call_site *call_site)
  585. {
  586.   CORE_ADDR addr = call_site->pc;
  587.   struct bound_minimal_symbol msym = lookup_minimal_symbol_by_pc (addr - 1);

  588.   fprintf_unfiltered (gdb_stdlog, " %s(%s)", paddress (gdbarch, addr),
  589.                       (msym.minsym == NULL ? "???"
  590.                        : MSYMBOL_PRINT_NAME (msym.minsym)));

  591. }

  592. /* vec.h needs single word type name, typedef it.  */
  593. typedef struct call_site *call_sitep;

  594. /* Define VEC (call_sitep) functions.  */
  595. DEF_VEC_P (call_sitep);

  596. /* Intersect RESULTP with CHAIN to keep RESULTP unambiguous, keep in RESULTP
  597.    only top callers and bottom callees which are present in both.  GDBARCH is
  598.    used only for ENTRY_VALUES_DEBUG.  RESULTP is NULL after return if there are
  599.    no remaining possibilities to provide unambiguous non-trivial result.
  600.    RESULTP should point to NULL on the first (initialization) call.  Caller is
  601.    responsible for xfree of any RESULTP data.  */

  602. static void
  603. chain_candidate (struct gdbarch *gdbarch, struct call_site_chain **resultp,
  604.                  VEC (call_sitep) *chain)
  605. {
  606.   struct call_site_chain *result = *resultp;
  607.   long length = VEC_length (call_sitep, chain);
  608.   int callers, callees, idx;

  609.   if (result == NULL)
  610.     {
  611.       /* Create the initial chain containing all the passed PCs.  */

  612.       result = xmalloc (sizeof (*result) + sizeof (*result->call_site)
  613.                                            * (length - 1));
  614.       result->length = length;
  615.       result->callers = result->callees = length;
  616.       if (!VEC_empty (call_sitep, chain))
  617.         memcpy (result->call_site, VEC_address (call_sitep, chain),
  618.                 sizeof (*result->call_site) * length);
  619.       *resultp = result;

  620.       if (entry_values_debug)
  621.         {
  622.           fprintf_unfiltered (gdb_stdlog, "tailcall: initial:");
  623.           for (idx = 0; idx < length; idx++)
  624.             tailcall_dump (gdbarch, result->call_site[idx]);
  625.           fputc_unfiltered ('\n', gdb_stdlog);
  626.         }

  627.       return;
  628.     }

  629.   if (entry_values_debug)
  630.     {
  631.       fprintf_unfiltered (gdb_stdlog, "tailcall: compare:");
  632.       for (idx = 0; idx < length; idx++)
  633.         tailcall_dump (gdbarch, VEC_index (call_sitep, chain, idx));
  634.       fputc_unfiltered ('\n', gdb_stdlog);
  635.     }

  636.   /* Intersect callers.  */

  637.   callers = min (result->callers, length);
  638.   for (idx = 0; idx < callers; idx++)
  639.     if (result->call_site[idx] != VEC_index (call_sitep, chain, idx))
  640.       {
  641.         result->callers = idx;
  642.         break;
  643.       }

  644.   /* Intersect callees.  */

  645.   callees = min (result->callees, length);
  646.   for (idx = 0; idx < callees; idx++)
  647.     if (result->call_site[result->length - 1 - idx]
  648.         != VEC_index (call_sitep, chain, length - 1 - idx))
  649.       {
  650.         result->callees = idx;
  651.         break;
  652.       }

  653.   if (entry_values_debug)
  654.     {
  655.       fprintf_unfiltered (gdb_stdlog, "tailcall: reduced:");
  656.       for (idx = 0; idx < result->callers; idx++)
  657.         tailcall_dump (gdbarch, result->call_site[idx]);
  658.       fputs_unfiltered (" |", gdb_stdlog);
  659.       for (idx = 0; idx < result->callees; idx++)
  660.         tailcall_dump (gdbarch, result->call_site[result->length
  661.                                                   - result->callees + idx]);
  662.       fputc_unfiltered ('\n', gdb_stdlog);
  663.     }

  664.   if (result->callers == 0 && result->callees == 0)
  665.     {
  666.       /* There are no common callers or callees.  It could be also a direct
  667.          call (which has length 0) with ambiguous possibility of an indirect
  668.          call - CALLERS == CALLEES == 0 is valid during the first allocation
  669.          but any subsequence processing of such entry means ambiguity.  */
  670.       xfree (result);
  671.       *resultp = NULL;
  672.       return;
  673.     }

  674.   /* See call_site_find_chain_1 why there is no way to reach the bottom callee
  675.      PC again.  In such case there must be two different code paths to reach
  676.      it, therefore some of the former determined intermediate PCs must differ
  677.      and the unambiguous chain gets shortened.  */
  678.   gdb_assert (result->callers + result->callees < result->length);
  679. }

  680. /* Create and return call_site_chain for CALLER_PC and CALLEE_PC.  All the
  681.    assumed frames between them use GDBARCH.  Use depth first search so we can
  682.    keep single CHAIN of call_site's back to CALLER_PC.  Function recursion
  683.    would have needless GDB stack overhead.  Caller is responsible for xfree of
  684.    the returned result.  Any unreliability results in thrown
  685.    NO_ENTRY_VALUE_ERROR.  */

  686. static struct call_site_chain *
  687. call_site_find_chain_1 (struct gdbarch *gdbarch, CORE_ADDR caller_pc,
  688.                         CORE_ADDR callee_pc)
  689. {
  690.   CORE_ADDR save_callee_pc = callee_pc;
  691.   struct obstack addr_obstack;
  692.   struct cleanup *back_to_retval, *back_to_workdata;
  693.   struct call_site_chain *retval = NULL;
  694.   struct call_site *call_site;

  695.   /* Mark CALL_SITEs so we do not visit the same ones twice.  */
  696.   htab_t addr_hash;

  697.   /* CHAIN contains only the intermediate CALL_SITEs.  Neither CALLER_PC's
  698.      call_site nor any possible call_site at CALLEE_PC's function is there.
  699.      Any CALL_SITE in CHAIN will be iterated to its siblings - via
  700.      TAIL_CALL_NEXT.  This is inappropriate for CALLER_PC's call_site.  */
  701.   VEC (call_sitep) *chain = NULL;

  702.   /* We are not interested in the specific PC inside the callee function.  */
  703.   callee_pc = get_pc_function_start (callee_pc);
  704.   if (callee_pc == 0)
  705.     throw_error (NO_ENTRY_VALUE_ERROR, _("Unable to find function for PC %s"),
  706.                  paddress (gdbarch, save_callee_pc));

  707.   back_to_retval = make_cleanup (free_current_contents, &retval);

  708.   obstack_init (&addr_obstack);
  709.   back_to_workdata = make_cleanup_obstack_free (&addr_obstack);
  710.   addr_hash = htab_create_alloc_ex (64, core_addr_hash, core_addr_eq, NULL,
  711.                                     &addr_obstack, hashtab_obstack_allocate,
  712.                                     NULL);
  713.   make_cleanup_htab_delete (addr_hash);

  714.   make_cleanup (VEC_cleanup (call_sitep), &chain);

  715.   /* Do not push CALL_SITE to CHAIN.  Push there only the first tail call site
  716.      at the target's function.  All the possible tail call sites in the
  717.      target's function will get iterated as already pushed into CHAIN via their
  718.      TAIL_CALL_NEXT.  */
  719.   call_site = call_site_for_pc (gdbarch, caller_pc);

  720.   while (call_site)
  721.     {
  722.       CORE_ADDR target_func_addr;
  723.       struct call_site *target_call_site;

  724.       /* CALLER_FRAME with registers is not available for tail-call jumped
  725.          frames.  */
  726.       target_func_addr = call_site_to_target_addr (gdbarch, call_site, NULL);

  727.       if (target_func_addr == callee_pc)
  728.         {
  729.           chain_candidate (gdbarch, &retval, chain);
  730.           if (retval == NULL)
  731.             break;

  732.           /* There is no way to reach CALLEE_PC again as we would prevent
  733.              entering it twice as being already marked in ADDR_HASH.  */
  734.           target_call_site = NULL;
  735.         }
  736.       else
  737.         {
  738.           struct symbol *target_func;

  739.           target_func = func_addr_to_tail_call_list (gdbarch, target_func_addr);
  740.           target_call_site = TYPE_TAIL_CALL_LIST (SYMBOL_TYPE (target_func));
  741.         }

  742.       do
  743.         {
  744.           /* Attempt to visit TARGET_CALL_SITE.  */

  745.           if (target_call_site)
  746.             {
  747.               void **slot;

  748.               slot = htab_find_slot (addr_hash, &target_call_site->pc, INSERT);
  749.               if (*slot == NULL)
  750.                 {
  751.                   /* Successfully entered TARGET_CALL_SITE.  */

  752.                   *slot = &target_call_site->pc;
  753.                   VEC_safe_push (call_sitep, chain, target_call_site);
  754.                   break;
  755.                 }
  756.             }

  757.           /* Backtrack (without revisiting the originating call_site).  Try the
  758.              callers's sibling; if there isn't any try the callers's callers's
  759.              sibling etc.  */

  760.           target_call_site = NULL;
  761.           while (!VEC_empty (call_sitep, chain))
  762.             {
  763.               call_site = VEC_pop (call_sitep, chain);

  764.               gdb_assert (htab_find_slot (addr_hash, &call_site->pc,
  765.                                           NO_INSERT) != NULL);
  766.               htab_remove_elt (addr_hash, &call_site->pc);

  767.               target_call_site = call_site->tail_call_next;
  768.               if (target_call_site)
  769.                 break;
  770.             }
  771.         }
  772.       while (target_call_site);

  773.       if (VEC_empty (call_sitep, chain))
  774.         call_site = NULL;
  775.       else
  776.         call_site = VEC_last (call_sitep, chain);
  777.     }

  778.   if (retval == NULL)
  779.     {
  780.       struct bound_minimal_symbol msym_caller, msym_callee;

  781.       msym_caller = lookup_minimal_symbol_by_pc (caller_pc);
  782.       msym_callee = lookup_minimal_symbol_by_pc (callee_pc);
  783.       throw_error (NO_ENTRY_VALUE_ERROR,
  784.                    _("There are no unambiguously determinable intermediate "
  785.                      "callers or callees between caller function \"%s\" at %s "
  786.                      "and callee function \"%s\" at %s"),
  787.                    (msym_caller.minsym == NULL
  788.                     ? "???" : MSYMBOL_PRINT_NAME (msym_caller.minsym)),
  789.                    paddress (gdbarch, caller_pc),
  790.                    (msym_callee.minsym == NULL
  791.                     ? "???" : MSYMBOL_PRINT_NAME (msym_callee.minsym)),
  792.                    paddress (gdbarch, callee_pc));
  793.     }

  794.   do_cleanups (back_to_workdata);
  795.   discard_cleanups (back_to_retval);
  796.   return retval;
  797. }

  798. /* Create and return call_site_chain for CALLER_PC and CALLEE_PC.  All the
  799.    assumed frames between them use GDBARCH.  If valid call_site_chain cannot be
  800.    constructed return NULL.  Caller is responsible for xfree of the returned
  801.    result.  */

  802. struct call_site_chain *
  803. call_site_find_chain (struct gdbarch *gdbarch, CORE_ADDR caller_pc,
  804.                       CORE_ADDR callee_pc)
  805. {
  806.   volatile struct gdb_exception e;
  807.   struct call_site_chain *retval = NULL;

  808.   TRY_CATCH (e, RETURN_MASK_ERROR)
  809.     {
  810.       retval = call_site_find_chain_1 (gdbarch, caller_pc, callee_pc);
  811.     }
  812.   if (e.reason < 0)
  813.     {
  814.       if (e.error == NO_ENTRY_VALUE_ERROR)
  815.         {
  816.           if (entry_values_debug)
  817.             exception_print (gdb_stdout, e);

  818.           return NULL;
  819.         }
  820.       else
  821.         throw_exception (e);
  822.     }
  823.   return retval;
  824. }

  825. /* Return 1 if KIND and KIND_U match PARAMETER.  Return 0 otherwise.  */

  826. static int
  827. call_site_parameter_matches (struct call_site_parameter *parameter,
  828.                              enum call_site_parameter_kind kind,
  829.                              union call_site_parameter_u kind_u)
  830. {
  831.   if (kind == parameter->kind)
  832.     switch (kind)
  833.       {
  834.       case CALL_SITE_PARAMETER_DWARF_REG:
  835.         return kind_u.dwarf_reg == parameter->u.dwarf_reg;
  836.       case CALL_SITE_PARAMETER_FB_OFFSET:
  837.         return kind_u.fb_offset == parameter->u.fb_offset;
  838.       case CALL_SITE_PARAMETER_PARAM_OFFSET:
  839.         return kind_u.param_offset.cu_off == parameter->u.param_offset.cu_off;
  840.       }
  841.   return 0;
  842. }

  843. /* Fetch call_site_parameter from caller matching KIND and KIND_U.
  844.    FRAME is for callee.

  845.    Function always returns non-NULL, it throws NO_ENTRY_VALUE_ERROR
  846.    otherwise.  */

  847. static struct call_site_parameter *
  848. dwarf_expr_reg_to_entry_parameter (struct frame_info *frame,
  849.                                    enum call_site_parameter_kind kind,
  850.                                    union call_site_parameter_u kind_u,
  851.                                    struct dwarf2_per_cu_data **per_cu_return)
  852. {
  853.   CORE_ADDR func_addr, caller_pc;
  854.   struct gdbarch *gdbarch;
  855.   struct frame_info *caller_frame;
  856.   struct call_site *call_site;
  857.   int iparams;
  858.   /* Initialize it just to avoid a GCC false warning.  */
  859.   struct call_site_parameter *parameter = NULL;
  860.   CORE_ADDR target_addr;

  861.   while (get_frame_type (frame) == INLINE_FRAME)
  862.     {
  863.       frame = get_prev_frame (frame);
  864.       gdb_assert (frame != NULL);
  865.     }

  866.   func_addr = get_frame_func (frame);
  867.   gdbarch = get_frame_arch (frame);
  868.   caller_frame = get_prev_frame (frame);
  869.   if (gdbarch != frame_unwind_arch (frame))
  870.     {
  871.       struct bound_minimal_symbol msym
  872.         = lookup_minimal_symbol_by_pc (func_addr);
  873.       struct gdbarch *caller_gdbarch = frame_unwind_arch (frame);

  874.       throw_error (NO_ENTRY_VALUE_ERROR,
  875.                    _("DW_OP_GNU_entry_value resolving callee gdbarch %s "
  876.                      "(of %s (%s)) does not match caller gdbarch %s"),
  877.                    gdbarch_bfd_arch_info (gdbarch)->printable_name,
  878.                    paddress (gdbarch, func_addr),
  879.                    (msym.minsym == NULL ? "???"
  880.                     : MSYMBOL_PRINT_NAME (msym.minsym)),
  881.                    gdbarch_bfd_arch_info (caller_gdbarch)->printable_name);
  882.     }

  883.   if (caller_frame == NULL)
  884.     {
  885.       struct bound_minimal_symbol msym
  886.         = lookup_minimal_symbol_by_pc (func_addr);

  887.       throw_error (NO_ENTRY_VALUE_ERROR, _("DW_OP_GNU_entry_value resolving "
  888.                                            "requires caller of %s (%s)"),
  889.                    paddress (gdbarch, func_addr),
  890.                    (msym.minsym == NULL ? "???"
  891.                     : MSYMBOL_PRINT_NAME (msym.minsym)));
  892.     }
  893.   caller_pc = get_frame_pc (caller_frame);
  894.   call_site = call_site_for_pc (gdbarch, caller_pc);

  895.   target_addr = call_site_to_target_addr (gdbarch, call_site, caller_frame);
  896.   if (target_addr != func_addr)
  897.     {
  898.       struct minimal_symbol *target_msym, *func_msym;

  899.       target_msym = lookup_minimal_symbol_by_pc (target_addr).minsym;
  900.       func_msym = lookup_minimal_symbol_by_pc (func_addr).minsym;
  901.       throw_error (NO_ENTRY_VALUE_ERROR,
  902.                    _("DW_OP_GNU_entry_value resolving expects callee %s at %s "
  903.                      "but the called frame is for %s at %s"),
  904.                    (target_msym == NULL ? "???"
  905.                                         : MSYMBOL_PRINT_NAME (target_msym)),
  906.                    paddress (gdbarch, target_addr),
  907.                    func_msym == NULL ? "???" : MSYMBOL_PRINT_NAME (func_msym),
  908.                    paddress (gdbarch, func_addr));
  909.     }

  910.   /* No entry value based parameters would be reliable if this function can
  911.      call itself via tail calls.  */
  912.   func_verify_no_selftailcall (gdbarch, func_addr);

  913.   for (iparams = 0; iparams < call_site->parameter_count; iparams++)
  914.     {
  915.       parameter = &call_site->parameter[iparams];
  916.       if (call_site_parameter_matches (parameter, kind, kind_u))
  917.         break;
  918.     }
  919.   if (iparams == call_site->parameter_count)
  920.     {
  921.       struct minimal_symbol *msym
  922.         = lookup_minimal_symbol_by_pc (caller_pc).minsym;

  923.       /* DW_TAG_GNU_call_site_parameter will be missing just if GCC could not
  924.          determine its value.  */
  925.       throw_error (NO_ENTRY_VALUE_ERROR, _("Cannot find matching parameter "
  926.                                            "at DW_TAG_GNU_call_site %s at %s"),
  927.                    paddress (gdbarch, caller_pc),
  928.                    msym == NULL ? "???" : MSYMBOL_PRINT_NAME (msym));
  929.     }

  930.   *per_cu_return = call_site->per_cu;
  931.   return parameter;
  932. }

  933. /* Return value for PARAMETER matching DEREF_SIZE.  If DEREF_SIZE is -1, return
  934.    the normal DW_AT_GNU_call_site_value block.  Otherwise return the
  935.    DW_AT_GNU_call_site_data_value (dereferenced) block.

  936.    TYPE and CALLER_FRAME specify how to evaluate the DWARF block into returned
  937.    struct value.

  938.    Function always returns non-NULL, non-optimized out value.  It throws
  939.    NO_ENTRY_VALUE_ERROR if it cannot resolve the value for any reason.  */

  940. static struct value *
  941. dwarf_entry_parameter_to_value (struct call_site_parameter *parameter,
  942.                                 CORE_ADDR deref_size, struct type *type,
  943.                                 struct frame_info *caller_frame,
  944.                                 struct dwarf2_per_cu_data *per_cu)
  945. {
  946.   const gdb_byte *data_src;
  947.   gdb_byte *data;
  948.   size_t size;

  949.   data_src = deref_size == -1 ? parameter->value : parameter->data_value;
  950.   size = deref_size == -1 ? parameter->value_size : parameter->data_value_size;

  951.   /* DEREF_SIZE size is not verified here.  */
  952.   if (data_src == NULL)
  953.     throw_error (NO_ENTRY_VALUE_ERROR,
  954.                  _("Cannot resolve DW_AT_GNU_call_site_data_value"));

  955.   /* DW_AT_GNU_call_site_value is a DWARF expression, not a DWARF
  956.      location.  Postprocessing of DWARF_VALUE_MEMORY would lose the type from
  957.      DWARF block.  */
  958.   data = alloca (size + 1);
  959.   memcpy (data, data_src, size);
  960.   data[size] = DW_OP_stack_value;

  961.   return dwarf2_evaluate_loc_desc (type, caller_frame, data, size + 1, per_cu);
  962. }

  963. /* Execute DWARF block of call_site_parameter which matches KIND and KIND_U.
  964.    Choose DEREF_SIZE value of that parameter.  Search caller of the CTX's
  965.    frame.  CTX must be of dwarf_expr_ctx_funcs kind.

  966.    The CTX caller can be from a different CU - per_cu_dwarf_call implementation
  967.    can be more simple as it does not support cross-CU DWARF executions.  */

  968. static void
  969. dwarf_expr_push_dwarf_reg_entry_value (struct dwarf_expr_context *ctx,
  970.                                        enum call_site_parameter_kind kind,
  971.                                        union call_site_parameter_u kind_u,
  972.                                        int deref_size)
  973. {
  974.   struct dwarf_expr_baton *debaton;
  975.   struct frame_info *frame, *caller_frame;
  976.   struct dwarf2_per_cu_data *caller_per_cu;
  977.   struct dwarf_expr_baton baton_local;
  978.   struct dwarf_expr_context saved_ctx;
  979.   struct call_site_parameter *parameter;
  980.   const gdb_byte *data_src;
  981.   size_t size;

  982.   gdb_assert (ctx->funcs == &dwarf_expr_ctx_funcs);
  983.   debaton = ctx->baton;
  984.   frame = debaton->frame;
  985.   caller_frame = get_prev_frame (frame);

  986.   parameter = dwarf_expr_reg_to_entry_parameter (frame, kind, kind_u,
  987.                                                  &caller_per_cu);
  988.   data_src = deref_size == -1 ? parameter->value : parameter->data_value;
  989.   size = deref_size == -1 ? parameter->value_size : parameter->data_value_size;

  990.   /* DEREF_SIZE size is not verified here.  */
  991.   if (data_src == NULL)
  992.     throw_error (NO_ENTRY_VALUE_ERROR,
  993.                  _("Cannot resolve DW_AT_GNU_call_site_data_value"));

  994.   baton_local.frame = caller_frame;
  995.   baton_local.per_cu = caller_per_cu;
  996.   baton_local.obj_address = 0;

  997.   saved_ctx.gdbarch = ctx->gdbarch;
  998.   saved_ctx.addr_size = ctx->addr_size;
  999.   saved_ctx.offset = ctx->offset;
  1000.   saved_ctx.baton = ctx->baton;
  1001.   ctx->gdbarch = get_objfile_arch (dwarf2_per_cu_objfile (baton_local.per_cu));
  1002.   ctx->addr_size = dwarf2_per_cu_addr_size (baton_local.per_cu);
  1003.   ctx->offset = dwarf2_per_cu_text_offset (baton_local.per_cu);
  1004.   ctx->baton = &baton_local;

  1005.   dwarf_expr_eval (ctx, data_src, size);

  1006.   ctx->gdbarch = saved_ctx.gdbarch;
  1007.   ctx->addr_size = saved_ctx.addr_size;
  1008.   ctx->offset = saved_ctx.offset;
  1009.   ctx->baton = saved_ctx.baton;
  1010. }

  1011. /* Callback function for dwarf2_evaluate_loc_desc.
  1012.    Fetch the address indexed by DW_OP_GNU_addr_index.  */

  1013. static CORE_ADDR
  1014. dwarf_expr_get_addr_index (void *baton, unsigned int index)
  1015. {
  1016.   struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;

  1017.   return dwarf2_read_addr_index (debaton->per_cu, index);
  1018. }

  1019. /* Callback function for get_object_address. Return the address of the VLA
  1020.    object.  */

  1021. static CORE_ADDR
  1022. dwarf_expr_get_obj_addr (void *baton)
  1023. {
  1024.   struct dwarf_expr_baton *debaton = baton;

  1025.   gdb_assert (debaton != NULL);

  1026.   if (debaton->obj_address == 0)
  1027.     error (_("Location address is not set."));

  1028.   return debaton->obj_address;
  1029. }

  1030. /* VALUE must be of type lval_computed with entry_data_value_funcs.  Perform
  1031.    the indirect method on it, that is use its stored target value, the sole
  1032.    purpose of entry_data_value_funcs..  */

  1033. static struct value *
  1034. entry_data_value_coerce_ref (const struct value *value)
  1035. {
  1036.   struct type *checked_type = check_typedef (value_type (value));
  1037.   struct value *target_val;

  1038.   if (TYPE_CODE (checked_type) != TYPE_CODE_REF)
  1039.     return NULL;

  1040.   target_val = value_computed_closure (value);
  1041.   value_incref (target_val);
  1042.   return target_val;
  1043. }

  1044. /* Implement copy_closure.  */

  1045. static void *
  1046. entry_data_value_copy_closure (const struct value *v)
  1047. {
  1048.   struct value *target_val = value_computed_closure (v);

  1049.   value_incref (target_val);
  1050.   return target_val;
  1051. }

  1052. /* Implement free_closure.  */

  1053. static void
  1054. entry_data_value_free_closure (struct value *v)
  1055. {
  1056.   struct value *target_val = value_computed_closure (v);

  1057.   value_free (target_val);
  1058. }

  1059. /* Vector for methods for an entry value reference where the referenced value
  1060.    is stored in the caller.  On the first dereference use
  1061.    DW_AT_GNU_call_site_data_value in the caller.  */

  1062. static const struct lval_funcs entry_data_value_funcs =
  1063. {
  1064.   NULL,        /* read */
  1065.   NULL,        /* write */
  1066.   NULL,        /* indirect */
  1067.   entry_data_value_coerce_ref,
  1068.   NULL,        /* check_synthetic_pointer */
  1069.   entry_data_value_copy_closure,
  1070.   entry_data_value_free_closure
  1071. };

  1072. /* Read parameter of TYPE at (callee) FRAME's function entry.  KIND and KIND_U
  1073.    are used to match DW_AT_location at the caller's
  1074.    DW_TAG_GNU_call_site_parameter.

  1075.    Function always returns non-NULL value.  It throws NO_ENTRY_VALUE_ERROR if it
  1076.    cannot resolve the parameter for any reason.  */

  1077. static struct value *
  1078. value_of_dwarf_reg_entry (struct type *type, struct frame_info *frame,
  1079.                           enum call_site_parameter_kind kind,
  1080.                           union call_site_parameter_u kind_u)
  1081. {
  1082.   struct type *checked_type = check_typedef (type);
  1083.   struct type *target_type = TYPE_TARGET_TYPE (checked_type);
  1084.   struct frame_info *caller_frame = get_prev_frame (frame);
  1085.   struct value *outer_val, *target_val, *val;
  1086.   struct call_site_parameter *parameter;
  1087.   struct dwarf2_per_cu_data *caller_per_cu;

  1088.   parameter = dwarf_expr_reg_to_entry_parameter (frame, kind, kind_u,
  1089.                                                  &caller_per_cu);

  1090.   outer_val = dwarf_entry_parameter_to_value (parameter, -1 /* deref_size */,
  1091.                                               type, caller_frame,
  1092.                                               caller_per_cu);

  1093.   /* Check if DW_AT_GNU_call_site_data_value cannot be used.  If it should be
  1094.      used and it is not available do not fall back to OUTER_VAL - dereferencing
  1095.      TYPE_CODE_REF with non-entry data value would give current value - not the
  1096.      entry value.  */

  1097.   if (TYPE_CODE (checked_type) != TYPE_CODE_REF
  1098.       || TYPE_TARGET_TYPE (checked_type) == NULL)
  1099.     return outer_val;

  1100.   target_val = dwarf_entry_parameter_to_value (parameter,
  1101.                                                TYPE_LENGTH (target_type),
  1102.                                                target_type, caller_frame,
  1103.                                                caller_per_cu);

  1104.   release_value (target_val);
  1105.   val = allocate_computed_value (type, &entry_data_value_funcs,
  1106.                                  target_val /* closure */);

  1107.   /* Copy the referencing pointer to the new computed value.  */
  1108.   memcpy (value_contents_raw (val), value_contents_raw (outer_val),
  1109.           TYPE_LENGTH (checked_type));
  1110.   set_value_lazy (val, 0);

  1111.   return val;
  1112. }

  1113. /* Read parameter of TYPE at (callee) FRAME's function entry.  DATA and
  1114.    SIZE are DWARF block used to match DW_AT_location at the caller's
  1115.    DW_TAG_GNU_call_site_parameter.

  1116.    Function always returns non-NULL value.  It throws NO_ENTRY_VALUE_ERROR if it
  1117.    cannot resolve the parameter for any reason.  */

  1118. static struct value *
  1119. value_of_dwarf_block_entry (struct type *type, struct frame_info *frame,
  1120.                             const gdb_byte *block, size_t block_len)
  1121. {
  1122.   union call_site_parameter_u kind_u;

  1123.   kind_u.dwarf_reg = dwarf_block_to_dwarf_reg (block, block + block_len);
  1124.   if (kind_u.dwarf_reg != -1)
  1125.     return value_of_dwarf_reg_entry (type, frame, CALL_SITE_PARAMETER_DWARF_REG,
  1126.                                      kind_u);

  1127.   if (dwarf_block_to_fb_offset (block, block + block_len, &kind_u.fb_offset))
  1128.     return value_of_dwarf_reg_entry (type, frame, CALL_SITE_PARAMETER_FB_OFFSET,
  1129.                                      kind_u);

  1130.   /* This can normally happen - throw NO_ENTRY_VALUE_ERROR to get the message
  1131.      suppressed during normal operation.  The expression can be arbitrary if
  1132.      there is no caller-callee entry value binding expected.  */
  1133.   throw_error (NO_ENTRY_VALUE_ERROR,
  1134.                _("DWARF-2 expression error: DW_OP_GNU_entry_value is supported "
  1135.                  "only for single DW_OP_reg* or for DW_OP_fbreg(*)"));
  1136. }

  1137. struct piece_closure
  1138. {
  1139.   /* Reference count.  */
  1140.   int refc;

  1141.   /* The CU from which this closure's expression came.  */
  1142.   struct dwarf2_per_cu_data *per_cu;

  1143.   /* The number of pieces used to describe this variable.  */
  1144.   int n_pieces;

  1145.   /* The target address size, used only for DWARF_VALUE_STACK.  */
  1146.   int addr_size;

  1147.   /* The pieces themselves.  */
  1148.   struct dwarf_expr_piece *pieces;
  1149. };

  1150. /* Allocate a closure for a value formed from separately-described
  1151.    PIECES.  */

  1152. static struct piece_closure *
  1153. allocate_piece_closure (struct dwarf2_per_cu_data *per_cu,
  1154.                         int n_pieces, struct dwarf_expr_piece *pieces,
  1155.                         int addr_size)
  1156. {
  1157.   struct piece_closure *c = XCNEW (struct piece_closure);
  1158.   int i;

  1159.   c->refc = 1;
  1160.   c->per_cu = per_cu;
  1161.   c->n_pieces = n_pieces;
  1162.   c->addr_size = addr_size;
  1163.   c->pieces = XCNEWVEC (struct dwarf_expr_piece, n_pieces);

  1164.   memcpy (c->pieces, pieces, n_pieces * sizeof (struct dwarf_expr_piece));
  1165.   for (i = 0; i < n_pieces; ++i)
  1166.     if (c->pieces[i].location == DWARF_VALUE_STACK)
  1167.       value_incref (c->pieces[i].v.value);

  1168.   return c;
  1169. }

  1170. /* The lowest-level function to extract bits from a byte buffer.
  1171.    SOURCE is the buffer.  It is updated if we read to the end of a
  1172.    byte.
  1173.    SOURCE_OFFSET_BITS is the offset of the first bit to read.  It is
  1174.    updated to reflect the number of bits actually read.
  1175.    NBITS is the number of bits we want to read.  It is updated to
  1176.    reflect the number of bits actually read.  This function may read
  1177.    fewer bits.
  1178.    BITS_BIG_ENDIAN is taken directly from gdbarch.
  1179.    This function returns the extracted bits.  */

  1180. static unsigned int
  1181. extract_bits_primitive (const gdb_byte **source,
  1182.                         unsigned int *source_offset_bits,
  1183.                         int *nbits, int bits_big_endian)
  1184. {
  1185.   unsigned int avail, mask, datum;

  1186.   gdb_assert (*source_offset_bits < 8);

  1187.   avail = 8 - *source_offset_bits;
  1188.   if (avail > *nbits)
  1189.     avail = *nbits;

  1190.   mask = (1 << avail) - 1;
  1191.   datum = **source;
  1192.   if (bits_big_endian)
  1193.     datum >>= 8 - (*source_offset_bits + *nbits);
  1194.   else
  1195.     datum >>= *source_offset_bits;
  1196.   datum &= mask;

  1197.   *nbits -= avail;
  1198.   *source_offset_bits += avail;
  1199.   if (*source_offset_bits >= 8)
  1200.     {
  1201.       *source_offset_bits -= 8;
  1202.       ++*source;
  1203.     }

  1204.   return datum;
  1205. }

  1206. /* Extract some bits from a source buffer and move forward in the
  1207.    buffer.

  1208.    SOURCE is the source buffer.  It is updated as bytes are read.
  1209.    SOURCE_OFFSET_BITS is the offset into SOURCE.  It is updated as
  1210.    bits are read.
  1211.    NBITS is the number of bits to read.
  1212.    BITS_BIG_ENDIAN is taken directly from gdbarch.

  1213.    This function returns the bits that were read.  */

  1214. static unsigned int
  1215. extract_bits (const gdb_byte **source, unsigned int *source_offset_bits,
  1216.               int nbits, int bits_big_endian)
  1217. {
  1218.   unsigned int datum;

  1219.   gdb_assert (nbits > 0 && nbits <= 8);

  1220.   datum = extract_bits_primitive (source, source_offset_bits, &nbits,
  1221.                                   bits_big_endian);
  1222.   if (nbits > 0)
  1223.     {
  1224.       unsigned int more;

  1225.       more = extract_bits_primitive (source, source_offset_bits, &nbits,
  1226.                                      bits_big_endian);
  1227.       if (bits_big_endian)
  1228.         datum <<= nbits;
  1229.       else
  1230.         more <<= nbits;
  1231.       datum |= more;
  1232.     }

  1233.   return datum;
  1234. }

  1235. /* Write some bits into a buffer and move forward in the buffer.

  1236.    DATUM is the bits to write.  The low-order bits of DATUM are used.
  1237.    DEST is the destination buffer.  It is updated as bytes are
  1238.    written.
  1239.    DEST_OFFSET_BITS is the bit offset in DEST at which writing is
  1240.    done.
  1241.    NBITS is the number of valid bits in DATUM.
  1242.    BITS_BIG_ENDIAN is taken directly from gdbarch.  */

  1243. static void
  1244. insert_bits (unsigned int datum,
  1245.              gdb_byte *dest, unsigned int dest_offset_bits,
  1246.              int nbits, int bits_big_endian)
  1247. {
  1248.   unsigned int mask;

  1249.   gdb_assert (dest_offset_bits + nbits <= 8);

  1250.   mask = (1 << nbits) - 1;
  1251.   if (bits_big_endian)
  1252.     {
  1253.       datum <<= 8 - (dest_offset_bits + nbits);
  1254.       mask <<= 8 - (dest_offset_bits + nbits);
  1255.     }
  1256.   else
  1257.     {
  1258.       datum <<= dest_offset_bits;
  1259.       mask <<= dest_offset_bits;
  1260.     }

  1261.   gdb_assert ((datum & ~mask) == 0);

  1262.   *dest = (*dest & ~mask) | datum;
  1263. }

  1264. /* Copy bits from a source to a destination.

  1265.    DEST is where the bits should be written.
  1266.    DEST_OFFSET_BITS is the bit offset into DEST.
  1267.    SOURCE is the source of bits.
  1268.    SOURCE_OFFSET_BITS is the bit offset into SOURCE.
  1269.    BIT_COUNT is the number of bits to copy.
  1270.    BITS_BIG_ENDIAN is taken directly from gdbarch.  */

  1271. static void
  1272. copy_bitwise (gdb_byte *dest, unsigned int dest_offset_bits,
  1273.               const gdb_byte *source, unsigned int source_offset_bits,
  1274.               unsigned int bit_count,
  1275.               int bits_big_endian)
  1276. {
  1277.   unsigned int dest_avail;
  1278.   int datum;

  1279.   /* Reduce everything to byte-size pieces.  */
  1280.   dest += dest_offset_bits / 8;
  1281.   dest_offset_bits %= 8;
  1282.   source += source_offset_bits / 8;
  1283.   source_offset_bits %= 8;

  1284.   dest_avail = 8 - dest_offset_bits % 8;

  1285.   /* See if we can fill the first destination byte.  */
  1286.   if (dest_avail < bit_count)
  1287.     {
  1288.       datum = extract_bits (&source, &source_offset_bits, dest_avail,
  1289.                             bits_big_endian);
  1290.       insert_bits (datum, dest, dest_offset_bits, dest_avail, bits_big_endian);
  1291.       ++dest;
  1292.       dest_offset_bits = 0;
  1293.       bit_count -= dest_avail;
  1294.     }

  1295.   /* Now, either DEST_OFFSET_BITS is byte-aligned, or we have fewer
  1296.      than 8 bits remaining.  */
  1297.   gdb_assert (dest_offset_bits % 8 == 0 || bit_count < 8);
  1298.   for (; bit_count >= 8; bit_count -= 8)
  1299.     {
  1300.       datum = extract_bits (&source, &source_offset_bits, 8, bits_big_endian);
  1301.       *dest++ = (gdb_byte) datum;
  1302.     }

  1303.   /* Finally, we may have a few leftover bits.  */
  1304.   gdb_assert (bit_count <= 8 - dest_offset_bits % 8);
  1305.   if (bit_count > 0)
  1306.     {
  1307.       datum = extract_bits (&source, &source_offset_bits, bit_count,
  1308.                             bits_big_endian);
  1309.       insert_bits (datum, dest, dest_offset_bits, bit_count, bits_big_endian);
  1310.     }
  1311. }

  1312. static void
  1313. read_pieced_value (struct value *v)
  1314. {
  1315.   int i;
  1316.   long offset = 0;
  1317.   ULONGEST bits_to_skip;
  1318.   gdb_byte *contents;
  1319.   struct piece_closure *c
  1320.     = (struct piece_closure *) value_computed_closure (v);
  1321.   struct frame_info *frame = frame_find_by_id (VALUE_FRAME_ID (v));
  1322.   size_t type_len;
  1323.   size_t buffer_size = 0;
  1324.   gdb_byte *buffer = NULL;
  1325.   struct cleanup *cleanup;
  1326.   int bits_big_endian
  1327.     = gdbarch_bits_big_endian (get_type_arch (value_type (v)));

  1328.   if (value_type (v) != value_enclosing_type (v))
  1329.     internal_error (__FILE__, __LINE__,
  1330.                     _("Should not be able to create a lazy value with "
  1331.                       "an enclosing type"));

  1332.   cleanup = make_cleanup (free_current_contents, &buffer);

  1333.   contents = value_contents_raw (v);
  1334.   bits_to_skip = 8 * value_offset (v);
  1335.   if (value_bitsize (v))
  1336.     {
  1337.       bits_to_skip += value_bitpos (v);
  1338.       type_len = value_bitsize (v);
  1339.     }
  1340.   else
  1341.     type_len = 8 * TYPE_LENGTH (value_type (v));

  1342.   for (i = 0; i < c->n_pieces && offset < type_len; i++)
  1343.     {
  1344.       struct dwarf_expr_piece *p = &c->pieces[i];
  1345.       size_t this_size, this_size_bits;
  1346.       long dest_offset_bits, source_offset_bits, source_offset;
  1347.       const gdb_byte *intermediate_buffer;

  1348.       /* Compute size, source, and destination offsets for copying, in
  1349.          bits.  */
  1350.       this_size_bits = p->size;
  1351.       if (bits_to_skip > 0 && bits_to_skip >= this_size_bits)
  1352.         {
  1353.           bits_to_skip -= this_size_bits;
  1354.           continue;
  1355.         }
  1356.       if (bits_to_skip > 0)
  1357.         {
  1358.           dest_offset_bits = 0;
  1359.           source_offset_bits = bits_to_skip;
  1360.           this_size_bits -= bits_to_skip;
  1361.           bits_to_skip = 0;
  1362.         }
  1363.       else
  1364.         {
  1365.           dest_offset_bits = offset;
  1366.           source_offset_bits = 0;
  1367.         }
  1368.       if (this_size_bits > type_len - offset)
  1369.         this_size_bits = type_len - offset;

  1370.       this_size = (this_size_bits + source_offset_bits % 8 + 7) / 8;
  1371.       source_offset = source_offset_bits / 8;
  1372.       if (buffer_size < this_size)
  1373.         {
  1374.           buffer_size = this_size;
  1375.           buffer = xrealloc (buffer, buffer_size);
  1376.         }
  1377.       intermediate_buffer = buffer;

  1378.       /* Copy from the source to DEST_BUFFER.  */
  1379.       switch (p->location)
  1380.         {
  1381.         case DWARF_VALUE_REGISTER:
  1382.           {
  1383.             struct gdbarch *arch = get_frame_arch (frame);
  1384.             int gdb_regnum = gdbarch_dwarf2_reg_to_regnum (arch, p->v.regno);

  1385.             if (gdb_regnum != -1)
  1386.               {
  1387.                 int optim, unavail;
  1388.                 int reg_offset = source_offset;

  1389.                 if (gdbarch_byte_order (arch) == BFD_ENDIAN_BIG
  1390.                     && this_size < register_size (arch, gdb_regnum))
  1391.                   {
  1392.                     /* Big-endian, and we want less than full size.  */
  1393.                     reg_offset = register_size (arch, gdb_regnum) - this_size;
  1394.                     /* We want the lower-order THIS_SIZE_BITS of the bytes
  1395.                        we extract from the register.  */
  1396.                     source_offset_bits += 8 * this_size - this_size_bits;
  1397.                  }

  1398.                 if (!get_frame_register_bytes (frame, gdb_regnum, reg_offset,
  1399.                                                this_size, buffer,
  1400.                                                &optim, &unavail))
  1401.                   {
  1402.                     /* Just so garbage doesn't ever shine through.  */
  1403.                     memset (buffer, 0, this_size);

  1404.                     if (optim)
  1405.                       mark_value_bits_optimized_out (v, offset, this_size_bits);
  1406.                     if (unavail)
  1407.                       mark_value_bits_unavailable (v, offset, this_size_bits);
  1408.                   }
  1409.               }
  1410.             else
  1411.               {
  1412.                 error (_("Unable to access DWARF register number %s"),
  1413.                        paddress (arch, p->v.regno));
  1414.               }
  1415.           }
  1416.           break;

  1417.         case DWARF_VALUE_MEMORY:
  1418.           read_value_memory (v, offset,
  1419.                              p->v.mem.in_stack_memory,
  1420.                              p->v.mem.addr + source_offset,
  1421.                              buffer, this_size);
  1422.           break;

  1423.         case DWARF_VALUE_STACK:
  1424.           {
  1425.             size_t n = this_size;

  1426.             if (n > c->addr_size - source_offset)
  1427.               n = (c->addr_size >= source_offset
  1428.                    ? c->addr_size - source_offset
  1429.                    : 0);
  1430.             if (n == 0)
  1431.               {
  1432.                 /* Nothing.  */
  1433.               }
  1434.             else
  1435.               {
  1436.                 const gdb_byte *val_bytes = value_contents_all (p->v.value);

  1437.                 intermediate_buffer = val_bytes + source_offset;
  1438.               }
  1439.           }
  1440.           break;

  1441.         case DWARF_VALUE_LITERAL:
  1442.           {
  1443.             size_t n = this_size;

  1444.             if (n > p->v.literal.length - source_offset)
  1445.               n = (p->v.literal.length >= source_offset
  1446.                    ? p->v.literal.length - source_offset
  1447.                    : 0);
  1448.             if (n != 0)
  1449.               intermediate_buffer = p->v.literal.data + source_offset;
  1450.           }
  1451.           break;

  1452.           /* These bits show up as zeros -- but do not cause the value
  1453.              to be considered optimized-out.  */
  1454.         case DWARF_VALUE_IMPLICIT_POINTER:
  1455.           break;

  1456.         case DWARF_VALUE_OPTIMIZED_OUT:
  1457.           mark_value_bits_optimized_out (v, offset, this_size_bits);
  1458.           break;

  1459.         default:
  1460.           internal_error (__FILE__, __LINE__, _("invalid location type"));
  1461.         }

  1462.       if (p->location != DWARF_VALUE_OPTIMIZED_OUT
  1463.           && p->location != DWARF_VALUE_IMPLICIT_POINTER)
  1464.         copy_bitwise (contents, dest_offset_bits,
  1465.                       intermediate_buffer, source_offset_bits % 8,
  1466.                       this_size_bits, bits_big_endian);

  1467.       offset += this_size_bits;
  1468.     }

  1469.   do_cleanups (cleanup);
  1470. }

  1471. static void
  1472. write_pieced_value (struct value *to, struct value *from)
  1473. {
  1474.   int i;
  1475.   long offset = 0;
  1476.   ULONGEST bits_to_skip;
  1477.   const gdb_byte *contents;
  1478.   struct piece_closure *c
  1479.     = (struct piece_closure *) value_computed_closure (to);
  1480.   struct frame_info *frame = frame_find_by_id (VALUE_FRAME_ID (to));
  1481.   size_t type_len;
  1482.   size_t buffer_size = 0;
  1483.   gdb_byte *buffer = NULL;
  1484.   struct cleanup *cleanup;
  1485.   int bits_big_endian
  1486.     = gdbarch_bits_big_endian (get_type_arch (value_type (to)));

  1487.   if (frame == NULL)
  1488.     {
  1489.       mark_value_bytes_optimized_out (to, 0, TYPE_LENGTH (value_type (to)));
  1490.       return;
  1491.     }

  1492.   cleanup = make_cleanup (free_current_contents, &buffer);

  1493.   contents = value_contents (from);
  1494.   bits_to_skip = 8 * value_offset (to);
  1495.   if (value_bitsize (to))
  1496.     {
  1497.       bits_to_skip += value_bitpos (to);
  1498.       type_len = value_bitsize (to);
  1499.     }
  1500.   else
  1501.     type_len = 8 * TYPE_LENGTH (value_type (to));

  1502.   for (i = 0; i < c->n_pieces && offset < type_len; i++)
  1503.     {
  1504.       struct dwarf_expr_piece *p = &c->pieces[i];
  1505.       size_t this_size_bits, this_size;
  1506.       long dest_offset_bits, source_offset_bits, dest_offset, source_offset;
  1507.       int need_bitwise;
  1508.       const gdb_byte *source_buffer;

  1509.       this_size_bits = p->size;
  1510.       if (bits_to_skip > 0 && bits_to_skip >= this_size_bits)
  1511.         {
  1512.           bits_to_skip -= this_size_bits;
  1513.           continue;
  1514.         }
  1515.       if (this_size_bits > type_len - offset)
  1516.         this_size_bits = type_len - offset;
  1517.       if (bits_to_skip > 0)
  1518.         {
  1519.           dest_offset_bits = bits_to_skip;
  1520.           source_offset_bits = 0;
  1521.           this_size_bits -= bits_to_skip;
  1522.           bits_to_skip = 0;
  1523.         }
  1524.       else
  1525.         {
  1526.           dest_offset_bits = 0;
  1527.           source_offset_bits = offset;
  1528.         }

  1529.       this_size = (this_size_bits + source_offset_bits % 8 + 7) / 8;
  1530.       source_offset = source_offset_bits / 8;
  1531.       dest_offset = dest_offset_bits / 8;
  1532.       if (dest_offset_bits % 8 == 0 && source_offset_bits % 8 == 0)
  1533.         {
  1534.           source_buffer = contents + source_offset;
  1535.           need_bitwise = 0;
  1536.         }
  1537.       else
  1538.         {
  1539.           if (buffer_size < this_size)
  1540.             {
  1541.               buffer_size = this_size;
  1542.               buffer = xrealloc (buffer, buffer_size);
  1543.             }
  1544.           source_buffer = buffer;
  1545.           need_bitwise = 1;
  1546.         }

  1547.       switch (p->location)
  1548.         {
  1549.         case DWARF_VALUE_REGISTER:
  1550.           {
  1551.             struct gdbarch *arch = get_frame_arch (frame);
  1552.             int gdb_regnum = gdbarch_dwarf2_reg_to_regnum (arch, p->v.regno);

  1553.             if (gdb_regnum != -1)
  1554.               {
  1555.                 int reg_offset = dest_offset;

  1556.                 if (gdbarch_byte_order (arch) == BFD_ENDIAN_BIG
  1557.                     && this_size <= register_size (arch, gdb_regnum))
  1558.                   {
  1559.                     /* Big-endian, and we want less than full size.  */
  1560.                     reg_offset = register_size (arch, gdb_regnum) - this_size;
  1561.                   }

  1562.                 if (need_bitwise)
  1563.                   {
  1564.                     int optim, unavail;

  1565.                     if (!get_frame_register_bytes (frame, gdb_regnum, reg_offset,
  1566.                                                    this_size, buffer,
  1567.                                                    &optim, &unavail))
  1568.                       {
  1569.                         if (optim)
  1570.                           throw_error (OPTIMIZED_OUT_ERROR,
  1571.                                        _("Can't do read-modify-write to "
  1572.                                          "update bitfield; containing word "
  1573.                                          "has been optimized out"));
  1574.                         if (unavail)
  1575.                           throw_error (NOT_AVAILABLE_ERROR,
  1576.                                        _("Can't do read-modify-write to update "
  1577.                                          "bitfield; containing word "
  1578.                                          "is unavailable"));
  1579.                       }
  1580.                     copy_bitwise (buffer, dest_offset_bits,
  1581.                                   contents, source_offset_bits,
  1582.                                   this_size_bits,
  1583.                                   bits_big_endian);
  1584.                   }

  1585.                 put_frame_register_bytes (frame, gdb_regnum, reg_offset,
  1586.                                           this_size, source_buffer);
  1587.               }
  1588.             else
  1589.               {
  1590.                 error (_("Unable to write to DWARF register number %s"),
  1591.                        paddress (arch, p->v.regno));
  1592.               }
  1593.           }
  1594.           break;
  1595.         case DWARF_VALUE_MEMORY:
  1596.           if (need_bitwise)
  1597.             {
  1598.               /* Only the first and last bytes can possibly have any
  1599.                  bits reused.  */
  1600.               read_memory (p->v.mem.addr + dest_offset, buffer, 1);
  1601.               read_memory (p->v.mem.addr + dest_offset + this_size - 1,
  1602.                            buffer + this_size - 1, 1);
  1603.               copy_bitwise (buffer, dest_offset_bits,
  1604.                             contents, source_offset_bits,
  1605.                             this_size_bits,
  1606.                             bits_big_endian);
  1607.             }

  1608.           write_memory (p->v.mem.addr + dest_offset,
  1609.                         source_buffer, this_size);
  1610.           break;
  1611.         default:
  1612.           mark_value_bytes_optimized_out (to, 0, TYPE_LENGTH (value_type (to)));
  1613.           break;
  1614.         }
  1615.       offset += this_size_bits;
  1616.     }

  1617.   do_cleanups (cleanup);
  1618. }

  1619. /* An implementation of an lval_funcs method to see whether a value is
  1620.    a synthetic pointer.  */

  1621. static int
  1622. check_pieced_synthetic_pointer (const struct value *value, int bit_offset,
  1623.                                 int bit_length)
  1624. {
  1625.   struct piece_closure *c
  1626.     = (struct piece_closure *) value_computed_closure (value);
  1627.   int i;

  1628.   bit_offset += 8 * value_offset (value);
  1629.   if (value_bitsize (value))
  1630.     bit_offset += value_bitpos (value);

  1631.   for (i = 0; i < c->n_pieces && bit_length > 0; i++)
  1632.     {
  1633.       struct dwarf_expr_piece *p = &c->pieces[i];
  1634.       size_t this_size_bits = p->size;

  1635.       if (bit_offset > 0)
  1636.         {
  1637.           if (bit_offset >= this_size_bits)
  1638.             {
  1639.               bit_offset -= this_size_bits;
  1640.               continue;
  1641.             }

  1642.           bit_length -= this_size_bits - bit_offset;
  1643.           bit_offset = 0;
  1644.         }
  1645.       else
  1646.         bit_length -= this_size_bits;

  1647.       if (p->location != DWARF_VALUE_IMPLICIT_POINTER)
  1648.         return 0;
  1649.     }

  1650.   return 1;
  1651. }

  1652. /* A wrapper function for get_frame_address_in_block.  */

  1653. static CORE_ADDR
  1654. get_frame_address_in_block_wrapper (void *baton)
  1655. {
  1656.   return get_frame_address_in_block (baton);
  1657. }

  1658. /* An implementation of an lval_funcs method to indirect through a
  1659.    pointer.  This handles the synthetic pointer case when needed.  */

  1660. static struct value *
  1661. indirect_pieced_value (struct value *value)
  1662. {
  1663.   struct piece_closure *c
  1664.     = (struct piece_closure *) value_computed_closure (value);
  1665.   struct type *type;
  1666.   struct frame_info *frame;
  1667.   struct dwarf2_locexpr_baton baton;
  1668.   int i, bit_offset, bit_length;
  1669.   struct dwarf_expr_piece *piece = NULL;
  1670.   LONGEST byte_offset;
  1671.   enum bfd_endian byte_order;

  1672.   type = check_typedef (value_type (value));
  1673.   if (TYPE_CODE (type) != TYPE_CODE_PTR)
  1674.     return NULL;

  1675.   bit_length = 8 * TYPE_LENGTH (type);
  1676.   bit_offset = 8 * value_offset (value);
  1677.   if (value_bitsize (value))
  1678.     bit_offset += value_bitpos (value);

  1679.   for (i = 0; i < c->n_pieces && bit_length > 0; i++)
  1680.     {
  1681.       struct dwarf_expr_piece *p = &c->pieces[i];
  1682.       size_t this_size_bits = p->size;

  1683.       if (bit_offset > 0)
  1684.         {
  1685.           if (bit_offset >= this_size_bits)
  1686.             {
  1687.               bit_offset -= this_size_bits;
  1688.               continue;
  1689.             }

  1690.           bit_length -= this_size_bits - bit_offset;
  1691.           bit_offset = 0;
  1692.         }
  1693.       else
  1694.         bit_length -= this_size_bits;

  1695.       if (p->location != DWARF_VALUE_IMPLICIT_POINTER)
  1696.         return NULL;

  1697.       if (bit_length != 0)
  1698.         error (_("Invalid use of DW_OP_GNU_implicit_pointer"));

  1699.       piece = p;
  1700.       break;
  1701.     }

  1702.   frame = get_selected_frame (_("No frame selected."));

  1703.   /* This is an offset requested by GDB, such as value subscripts.
  1704.      However, due to how synthetic pointers are implemented, this is
  1705.      always presented to us as a pointer type.  This means we have to
  1706.      sign-extend it manually as appropriate.  Use raw
  1707.      extract_signed_integer directly rather than value_as_address and
  1708.      sign extend afterwards on architectures that would need it
  1709.      (mostly everywhere except MIPS, which has signed addresses) as
  1710.      the later would go through gdbarch_pointer_to_address and thus
  1711.      return a CORE_ADDR with high bits set on architectures that
  1712.      encode address spaces and other things in CORE_ADDR.  */
  1713.   byte_order = gdbarch_byte_order (get_frame_arch (frame));
  1714.   byte_offset = extract_signed_integer (value_contents (value),
  1715.                                         TYPE_LENGTH (type), byte_order);
  1716.   byte_offset += piece->v.ptr.offset;

  1717.   gdb_assert (piece);
  1718.   baton
  1719.     = dwarf2_fetch_die_loc_sect_off (piece->v.ptr.die, c->per_cu,
  1720.                                      get_frame_address_in_block_wrapper,
  1721.                                      frame);

  1722.   if (baton.data != NULL)
  1723.     return dwarf2_evaluate_loc_desc_full (TYPE_TARGET_TYPE (type), frame,
  1724.                                           baton.data, baton.size, baton.per_cu,
  1725.                                           byte_offset);

  1726.   {
  1727.     struct obstack temp_obstack;
  1728.     struct cleanup *cleanup;
  1729.     const gdb_byte *bytes;
  1730.     LONGEST len;
  1731.     struct value *result;

  1732.     obstack_init (&temp_obstack);
  1733.     cleanup = make_cleanup_obstack_free (&temp_obstack);

  1734.     bytes = dwarf2_fetch_constant_bytes (piece->v.ptr.die, c->per_cu,
  1735.                                          &temp_obstack, &len);
  1736.     if (bytes == NULL)
  1737.       result = allocate_optimized_out_value (TYPE_TARGET_TYPE (type));
  1738.     else
  1739.       {
  1740.         if (byte_offset < 0
  1741.             || byte_offset + TYPE_LENGTH (TYPE_TARGET_TYPE (type)) > len)
  1742.           invalid_synthetic_pointer ();
  1743.         bytes += byte_offset;
  1744.         result = value_from_contents (TYPE_TARGET_TYPE (type), bytes);
  1745.       }

  1746.     do_cleanups (cleanup);
  1747.     return result;
  1748.   }
  1749. }

  1750. static void *
  1751. copy_pieced_value_closure (const struct value *v)
  1752. {
  1753.   struct piece_closure *c
  1754.     = (struct piece_closure *) value_computed_closure (v);

  1755.   ++c->refc;
  1756.   return c;
  1757. }

  1758. static void
  1759. free_pieced_value_closure (struct value *v)
  1760. {
  1761.   struct piece_closure *c
  1762.     = (struct piece_closure *) value_computed_closure (v);

  1763.   --c->refc;
  1764.   if (c->refc == 0)
  1765.     {
  1766.       int i;

  1767.       for (i = 0; i < c->n_pieces; ++i)
  1768.         if (c->pieces[i].location == DWARF_VALUE_STACK)
  1769.           value_free (c->pieces[i].v.value);

  1770.       xfree (c->pieces);
  1771.       xfree (c);
  1772.     }
  1773. }

  1774. /* Functions for accessing a variable described by DW_OP_piece.  */
  1775. static const struct lval_funcs pieced_value_funcs = {
  1776.   read_pieced_value,
  1777.   write_pieced_value,
  1778.   indirect_pieced_value,
  1779.   NULL,        /* coerce_ref */
  1780.   check_pieced_synthetic_pointer,
  1781.   copy_pieced_value_closure,
  1782.   free_pieced_value_closure
  1783. };

  1784. /* Virtual method table for dwarf2_evaluate_loc_desc_full below.  */

  1785. static const struct dwarf_expr_context_funcs dwarf_expr_ctx_funcs =
  1786. {
  1787.   dwarf_expr_read_addr_from_reg,
  1788.   dwarf_expr_get_reg_value,
  1789.   dwarf_expr_read_mem,
  1790.   dwarf_expr_frame_base,
  1791.   dwarf_expr_frame_cfa,
  1792.   dwarf_expr_frame_pc,
  1793.   dwarf_expr_tls_address,
  1794.   dwarf_expr_dwarf_call,
  1795.   dwarf_expr_get_base_type,
  1796.   dwarf_expr_push_dwarf_reg_entry_value,
  1797.   dwarf_expr_get_addr_index,
  1798.   dwarf_expr_get_obj_addr
  1799. };

  1800. /* Evaluate a location description, starting at DATA and with length
  1801.    SIZE, to find the current location of variable of TYPE in the
  1802.    context of FRAME.  BYTE_OFFSET is applied after the contents are
  1803.    computed.  */

  1804. static struct value *
  1805. dwarf2_evaluate_loc_desc_full (struct type *type, struct frame_info *frame,
  1806.                                const gdb_byte *data, size_t size,
  1807.                                struct dwarf2_per_cu_data *per_cu,
  1808.                                LONGEST byte_offset)
  1809. {
  1810.   struct value *retval;
  1811.   struct dwarf_expr_baton baton;
  1812.   struct dwarf_expr_context *ctx;
  1813.   struct cleanup *old_chain, *value_chain;
  1814.   struct objfile *objfile = dwarf2_per_cu_objfile (per_cu);
  1815.   volatile struct gdb_exception ex;

  1816.   if (byte_offset < 0)
  1817.     invalid_synthetic_pointer ();

  1818.   if (size == 0)
  1819.     return allocate_optimized_out_value (type);

  1820.   baton.frame = frame;
  1821.   baton.per_cu = per_cu;
  1822.   baton.obj_address = 0;

  1823.   ctx = new_dwarf_expr_context ();
  1824.   old_chain = make_cleanup_free_dwarf_expr_context (ctx);
  1825.   value_chain = make_cleanup_value_free_to_mark (value_mark ());

  1826.   ctx->gdbarch = get_objfile_arch (objfile);
  1827.   ctx->addr_size = dwarf2_per_cu_addr_size (per_cu);
  1828.   ctx->ref_addr_size = dwarf2_per_cu_ref_addr_size (per_cu);
  1829.   ctx->offset = dwarf2_per_cu_text_offset (per_cu);
  1830.   ctx->baton = &baton;
  1831.   ctx->funcs = &dwarf_expr_ctx_funcs;

  1832.   TRY_CATCH (ex, RETURN_MASK_ERROR)
  1833.     {
  1834.       dwarf_expr_eval (ctx, data, size);
  1835.     }
  1836.   if (ex.reason < 0)
  1837.     {
  1838.       if (ex.error == NOT_AVAILABLE_ERROR)
  1839.         {
  1840.           do_cleanups (old_chain);
  1841.           retval = allocate_value (type);
  1842.           mark_value_bytes_unavailable (retval, 0, TYPE_LENGTH (type));
  1843.           return retval;
  1844.         }
  1845.       else if (ex.error == NO_ENTRY_VALUE_ERROR)
  1846.         {
  1847.           if (entry_values_debug)
  1848.             exception_print (gdb_stdout, ex);
  1849.           do_cleanups (old_chain);
  1850.           return allocate_optimized_out_value (type);
  1851.         }
  1852.       else
  1853.         throw_exception (ex);
  1854.     }

  1855.   if (ctx->num_pieces > 0)
  1856.     {
  1857.       struct piece_closure *c;
  1858.       struct frame_id frame_id = get_frame_id (frame);
  1859.       ULONGEST bit_size = 0;
  1860.       int i;

  1861.       for (i = 0; i < ctx->num_pieces; ++i)
  1862.         bit_size += ctx->pieces[i].size;
  1863.       if (8 * (byte_offset + TYPE_LENGTH (type)) > bit_size)
  1864.         invalid_synthetic_pointer ();

  1865.       c = allocate_piece_closure (per_cu, ctx->num_pieces, ctx->pieces,
  1866.                                   ctx->addr_size);
  1867.       /* We must clean up the value chain after creating the piece
  1868.          closure but before allocating the result.  */
  1869.       do_cleanups (value_chain);
  1870.       retval = allocate_computed_value (type, &pieced_value_funcs, c);
  1871.       VALUE_FRAME_ID (retval) = frame_id;
  1872.       set_value_offset (retval, byte_offset);
  1873.     }
  1874.   else
  1875.     {
  1876.       switch (ctx->location)
  1877.         {
  1878.         case DWARF_VALUE_REGISTER:
  1879.           {
  1880.             struct gdbarch *arch = get_frame_arch (frame);
  1881.             int dwarf_regnum
  1882.               = longest_to_int (value_as_long (dwarf_expr_fetch (ctx, 0)));
  1883.             int gdb_regnum = gdbarch_dwarf2_reg_to_regnum (arch, dwarf_regnum);

  1884.             if (byte_offset != 0)
  1885.               error (_("cannot use offset on synthetic pointer to register"));
  1886.             do_cleanups (value_chain);
  1887.            if (gdb_regnum == -1)
  1888.               error (_("Unable to access DWARF register number %d"),
  1889.                      dwarf_regnum);
  1890.            retval = value_from_register (type, gdb_regnum, frame);
  1891.            if (value_optimized_out (retval))
  1892.              {
  1893.                struct value *tmp;

  1894.                /* This means the register has undefined value / was
  1895.                   not saved.  As we're computing the location of some
  1896.                   variable etc. in the program, not a value for
  1897.                   inspecting a register ($pc, $sp, etc.), return a
  1898.                   generic optimized out value instead, so that we show
  1899.                   <optimized out> instead of <not saved>.  */
  1900.                do_cleanups (value_chain);
  1901.                tmp = allocate_value (type);
  1902.                value_contents_copy (tmp, 0, retval, 0, TYPE_LENGTH (type));
  1903.                retval = tmp;
  1904.              }
  1905.           }
  1906.           break;

  1907.         case DWARF_VALUE_MEMORY:
  1908.           {
  1909.             CORE_ADDR address = dwarf_expr_fetch_address (ctx, 0);
  1910.             int in_stack_memory = dwarf_expr_fetch_in_stack_memory (ctx, 0);

  1911.             do_cleanups (value_chain);
  1912.             retval = value_at_lazy (type, address + byte_offset);
  1913.             if (in_stack_memory)
  1914.               set_value_stack (retval, 1);
  1915.           }
  1916.           break;

  1917.         case DWARF_VALUE_STACK:
  1918.           {
  1919.             struct value *value = dwarf_expr_fetch (ctx, 0);
  1920.             gdb_byte *contents;
  1921.             const gdb_byte *val_bytes;
  1922.             size_t n = TYPE_LENGTH (value_type (value));

  1923.             if (byte_offset + TYPE_LENGTH (type) > n)
  1924.               invalid_synthetic_pointer ();

  1925.             val_bytes = value_contents_all (value);
  1926.             val_bytes += byte_offset;
  1927.             n -= byte_offset;

  1928.             /* Preserve VALUE because we are going to free values back
  1929.                to the mark, but we still need the value contents
  1930.                below.  */
  1931.             value_incref (value);
  1932.             do_cleanups (value_chain);
  1933.             make_cleanup_value_free (value);

  1934.             retval = allocate_value (type);
  1935.             contents = value_contents_raw (retval);
  1936.             if (n > TYPE_LENGTH (type))
  1937.               {
  1938.                 struct gdbarch *objfile_gdbarch = get_objfile_arch (objfile);

  1939.                 if (gdbarch_byte_order (objfile_gdbarch) == BFD_ENDIAN_BIG)
  1940.                   val_bytes += n - TYPE_LENGTH (type);
  1941.                 n = TYPE_LENGTH (type);
  1942.               }
  1943.             memcpy (contents, val_bytes, n);
  1944.           }
  1945.           break;

  1946.         case DWARF_VALUE_LITERAL:
  1947.           {
  1948.             bfd_byte *contents;
  1949.             const bfd_byte *ldata;
  1950.             size_t n = ctx->len;

  1951.             if (byte_offset + TYPE_LENGTH (type) > n)
  1952.               invalid_synthetic_pointer ();

  1953.             do_cleanups (value_chain);
  1954.             retval = allocate_value (type);
  1955.             contents = value_contents_raw (retval);

  1956.             ldata = ctx->data + byte_offset;
  1957.             n -= byte_offset;

  1958.             if (n > TYPE_LENGTH (type))
  1959.               {
  1960.                 struct gdbarch *objfile_gdbarch = get_objfile_arch (objfile);

  1961.                 if (gdbarch_byte_order (objfile_gdbarch) == BFD_ENDIAN_BIG)
  1962.                   ldata += n - TYPE_LENGTH (type);
  1963.                 n = TYPE_LENGTH (type);
  1964.               }
  1965.             memcpy (contents, ldata, n);
  1966.           }
  1967.           break;

  1968.         case DWARF_VALUE_OPTIMIZED_OUT:
  1969.           do_cleanups (value_chain);
  1970.           retval = allocate_optimized_out_value (type);
  1971.           break;

  1972.           /* DWARF_VALUE_IMPLICIT_POINTER was converted to a pieced
  1973.              operation by execute_stack_op.  */
  1974.         case DWARF_VALUE_IMPLICIT_POINTER:
  1975.           /* DWARF_VALUE_OPTIMIZED_OUT can't occur in this context --
  1976.              it can only be encountered when making a piece.  */
  1977.         default:
  1978.           internal_error (__FILE__, __LINE__, _("invalid location type"));
  1979.         }
  1980.     }

  1981.   set_value_initialized (retval, ctx->initialized);

  1982.   do_cleanups (old_chain);

  1983.   return retval;
  1984. }

  1985. /* The exported interface to dwarf2_evaluate_loc_desc_full; it always
  1986.    passes 0 as the byte_offset.  */

  1987. struct value *
  1988. dwarf2_evaluate_loc_desc (struct type *type, struct frame_info *frame,
  1989.                           const gdb_byte *data, size_t size,
  1990.                           struct dwarf2_per_cu_data *per_cu)
  1991. {
  1992.   return dwarf2_evaluate_loc_desc_full (type, frame, data, size, per_cu, 0);
  1993. }

  1994. /* Evaluates a dwarf expression and stores the result in VAL, expecting
  1995.    that the dwarf expression only produces a single CORE_ADDR.  ADDR is a
  1996.    context (location of a variable) and might be needed to evaluate the
  1997.    location expression.
  1998.    Returns 1 on success, 0 otherwise.   */

  1999. static int
  2000. dwarf2_locexpr_baton_eval (const struct dwarf2_locexpr_baton *dlbaton,
  2001.                            CORE_ADDR addr,
  2002.                            CORE_ADDR *valp)
  2003. {
  2004.   struct dwarf_expr_context *ctx;
  2005.   struct dwarf_expr_baton baton;
  2006.   struct objfile *objfile;
  2007.   struct cleanup *cleanup;

  2008.   if (dlbaton == NULL || dlbaton->size == 0)
  2009.     return 0;

  2010.   ctx = new_dwarf_expr_context ();
  2011.   cleanup = make_cleanup_free_dwarf_expr_context (ctx);

  2012.   baton.frame = get_selected_frame (NULL);
  2013.   baton.per_cu = dlbaton->per_cu;
  2014.   baton.obj_address = addr;

  2015.   objfile = dwarf2_per_cu_objfile (dlbaton->per_cu);

  2016.   ctx->gdbarch = get_objfile_arch (objfile);
  2017.   ctx->addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);
  2018.   ctx->ref_addr_size = dwarf2_per_cu_ref_addr_size (dlbaton->per_cu);
  2019.   ctx->offset = dwarf2_per_cu_text_offset (dlbaton->per_cu);
  2020.   ctx->funcs = &dwarf_expr_ctx_funcs;
  2021.   ctx->baton = &baton;

  2022.   dwarf_expr_eval (ctx, dlbaton->data, dlbaton->size);

  2023.   switch (ctx->location)
  2024.     {
  2025.     case DWARF_VALUE_REGISTER:
  2026.     case DWARF_VALUE_MEMORY:
  2027.     case DWARF_VALUE_STACK:
  2028.       *valp = dwarf_expr_fetch_address (ctx, 0);
  2029.       if (ctx->location == DWARF_VALUE_REGISTER)
  2030.         *valp = dwarf_expr_read_addr_from_reg (&baton, *valp);
  2031.       do_cleanups (cleanup);
  2032.       return 1;
  2033.     case DWARF_VALUE_LITERAL:
  2034.       *valp = extract_signed_integer (ctx->data, ctx->len,
  2035.                                       gdbarch_byte_order (ctx->gdbarch));
  2036.       do_cleanups (cleanup);
  2037.       return 1;
  2038.       /* Unsupported dwarf values.  */
  2039.     case DWARF_VALUE_OPTIMIZED_OUT:
  2040.     case DWARF_VALUE_IMPLICIT_POINTER:
  2041.       break;
  2042.     }

  2043.   do_cleanups (cleanup);
  2044.   return 0;
  2045. }

  2046. /* See dwarf2loc.h.  */

  2047. int
  2048. dwarf2_evaluate_property (const struct dynamic_prop *prop,
  2049.                           CORE_ADDR address, CORE_ADDR *value)
  2050. {
  2051.   if (prop == NULL)
  2052.     return 0;

  2053.   switch (prop->kind)
  2054.     {
  2055.     case PROP_LOCEXPR:
  2056.       {
  2057.         const struct dwarf2_property_baton *baton = prop->data.baton;

  2058.         if (dwarf2_locexpr_baton_eval (&baton->locexpr, address, value))
  2059.           {
  2060.             if (baton->referenced_type)
  2061.               {
  2062.                 struct value *val = value_at (baton->referenced_type, *value);

  2063.                 *value = value_as_address (val);
  2064.               }
  2065.             return 1;
  2066.           }
  2067.       }
  2068.       break;

  2069.     case PROP_LOCLIST:
  2070.       {
  2071.         struct dwarf2_property_baton *baton = prop->data.baton;
  2072.         struct frame_info *frame = get_selected_frame (NULL);
  2073.         CORE_ADDR pc = get_frame_address_in_block (frame);
  2074.         const gdb_byte *data;
  2075.         struct value *val;
  2076.         size_t size;

  2077.         data = dwarf2_find_location_expression (&baton->loclist, &size, pc);
  2078.         if (data != NULL)
  2079.           {
  2080.             val = dwarf2_evaluate_loc_desc (baton->referenced_type, frame, data,
  2081.                                             size, baton->loclist.per_cu);
  2082.             if (!value_optimized_out (val))
  2083.               {
  2084.                 *value = value_as_address (val);
  2085.                 return 1;
  2086.               }
  2087.           }
  2088.       }
  2089.       break;

  2090.     case PROP_CONST:
  2091.       *value = prop->data.const_val;
  2092.       return 1;
  2093.     }

  2094.   return 0;
  2095. }

  2096. /* See dwarf2loc.h.  */

  2097. void
  2098. dwarf2_compile_property_to_c (struct ui_file *stream,
  2099.                               const char *result_name,
  2100.                               struct gdbarch *gdbarch,
  2101.                               unsigned char *registers_used,
  2102.                               const struct dynamic_prop *prop,
  2103.                               CORE_ADDR pc,
  2104.                               struct symbol *sym)
  2105. {
  2106.   struct dwarf2_property_baton *baton = prop->data.baton;
  2107.   const gdb_byte *data;
  2108.   size_t size;
  2109.   struct dwarf2_per_cu_data *per_cu;

  2110.   if (prop->kind == PROP_LOCEXPR)
  2111.     {
  2112.       data = baton->locexpr.data;
  2113.       size = baton->locexpr.size;
  2114.       per_cu = baton->locexpr.per_cu;
  2115.     }
  2116.   else
  2117.     {
  2118.       gdb_assert (prop->kind == PROP_LOCLIST);

  2119.       data = dwarf2_find_location_expression (&baton->loclist, &size, pc);
  2120.       per_cu = baton->loclist.per_cu;
  2121.     }

  2122.   compile_dwarf_bounds_to_c (stream, result_name, prop, sym, pc,
  2123.                              gdbarch, registers_used,
  2124.                              dwarf2_per_cu_addr_size (per_cu),
  2125.                              data, data + size, per_cu);
  2126. }


  2127. /* Helper functions and baton for dwarf2_loc_desc_needs_frame.  */

  2128. struct needs_frame_baton
  2129. {
  2130.   int needs_frame;
  2131.   struct dwarf2_per_cu_data *per_cu;
  2132. };

  2133. /* Reads from registers do require a frame.  */
  2134. static CORE_ADDR
  2135. needs_frame_read_addr_from_reg (void *baton, int regnum)
  2136. {
  2137.   struct needs_frame_baton *nf_baton = baton;

  2138.   nf_baton->needs_frame = 1;
  2139.   return 1;
  2140. }

  2141. /* struct dwarf_expr_context_funcs' "get_reg_value" callback:
  2142.    Reads from registers do require a frame.  */

  2143. static struct value *
  2144. needs_frame_get_reg_value (void *baton, struct type *type, int regnum)
  2145. {
  2146.   struct needs_frame_baton *nf_baton = baton;

  2147.   nf_baton->needs_frame = 1;
  2148.   return value_zero (type, not_lval);
  2149. }

  2150. /* Reads from memory do not require a frame.  */
  2151. static void
  2152. needs_frame_read_mem (void *baton, gdb_byte *buf, CORE_ADDR addr, size_t len)
  2153. {
  2154.   memset (buf, 0, len);
  2155. }

  2156. /* Frame-relative accesses do require a frame.  */
  2157. static void
  2158. needs_frame_frame_base (void *baton, const gdb_byte **start, size_t * length)
  2159. {
  2160.   static gdb_byte lit0 = DW_OP_lit0;
  2161.   struct needs_frame_baton *nf_baton = baton;

  2162.   *start = &lit0;
  2163.   *length = 1;

  2164.   nf_baton->needs_frame = 1;
  2165. }

  2166. /* CFA accesses require a frame.  */

  2167. static CORE_ADDR
  2168. needs_frame_frame_cfa (void *baton)
  2169. {
  2170.   struct needs_frame_baton *nf_baton = baton;

  2171.   nf_baton->needs_frame = 1;
  2172.   return 1;
  2173. }

  2174. /* Thread-local accesses do require a frame.  */
  2175. static CORE_ADDR
  2176. needs_frame_tls_address (void *baton, CORE_ADDR offset)
  2177. {
  2178.   struct needs_frame_baton *nf_baton = baton;

  2179.   nf_baton->needs_frame = 1;
  2180.   return 1;
  2181. }

  2182. /* Helper interface of per_cu_dwarf_call for dwarf2_loc_desc_needs_frame.  */

  2183. static void
  2184. needs_frame_dwarf_call (struct dwarf_expr_context *ctx, cu_offset die_offset)
  2185. {
  2186.   struct needs_frame_baton *nf_baton = ctx->baton;

  2187.   per_cu_dwarf_call (ctx, die_offset, nf_baton->per_cu,
  2188.                      ctx->funcs->get_frame_pc, ctx->baton);
  2189. }

  2190. /* DW_OP_GNU_entry_value accesses require a caller, therefore a frame.  */

  2191. static void
  2192. needs_dwarf_reg_entry_value (struct dwarf_expr_context *ctx,
  2193.                              enum call_site_parameter_kind kind,
  2194.                              union call_site_parameter_u kind_u, int deref_size)
  2195. {
  2196.   struct needs_frame_baton *nf_baton = ctx->baton;

  2197.   nf_baton->needs_frame = 1;

  2198.   /* The expression may require some stub values on DWARF stack.  */
  2199.   dwarf_expr_push_address (ctx, 0, 0);
  2200. }

  2201. /* DW_OP_GNU_addr_index doesn't require a frame.  */

  2202. static CORE_ADDR
  2203. needs_get_addr_index (void *baton, unsigned int index)
  2204. {
  2205.   /* Nothing to do.  */
  2206.   return 1;
  2207. }

  2208. /* DW_OP_push_object_address has a frame already passed through.  */

  2209. static CORE_ADDR
  2210. needs_get_obj_addr (void *baton)
  2211. {
  2212.   /* Nothing to do.  */
  2213.   return 1;
  2214. }

  2215. /* Virtual method table for dwarf2_loc_desc_needs_frame below.  */

  2216. static const struct dwarf_expr_context_funcs needs_frame_ctx_funcs =
  2217. {
  2218.   needs_frame_read_addr_from_reg,
  2219.   needs_frame_get_reg_value,
  2220.   needs_frame_read_mem,
  2221.   needs_frame_frame_base,
  2222.   needs_frame_frame_cfa,
  2223.   needs_frame_frame_cfa,        /* get_frame_pc */
  2224.   needs_frame_tls_address,
  2225.   needs_frame_dwarf_call,
  2226.   NULL,                                /* get_base_type */
  2227.   needs_dwarf_reg_entry_value,
  2228.   needs_get_addr_index,
  2229.   needs_get_obj_addr
  2230. };

  2231. /* Return non-zero iff the location expression at DATA (length SIZE)
  2232.    requires a frame to evaluate.  */

  2233. static int
  2234. dwarf2_loc_desc_needs_frame (const gdb_byte *data, size_t size,
  2235.                              struct dwarf2_per_cu_data *per_cu)
  2236. {
  2237.   struct needs_frame_baton baton;
  2238.   struct dwarf_expr_context *ctx;
  2239.   int in_reg;
  2240.   struct cleanup *old_chain;
  2241.   struct objfile *objfile = dwarf2_per_cu_objfile (per_cu);

  2242.   baton.needs_frame = 0;
  2243.   baton.per_cu = per_cu;

  2244.   ctx = new_dwarf_expr_context ();
  2245.   old_chain = make_cleanup_free_dwarf_expr_context (ctx);
  2246.   make_cleanup_value_free_to_mark (value_mark ());

  2247.   ctx->gdbarch = get_objfile_arch (objfile);
  2248.   ctx->addr_size = dwarf2_per_cu_addr_size (per_cu);
  2249.   ctx->ref_addr_size = dwarf2_per_cu_ref_addr_size (per_cu);
  2250.   ctx->offset = dwarf2_per_cu_text_offset (per_cu);
  2251.   ctx->baton = &baton;
  2252.   ctx->funcs = &needs_frame_ctx_funcs;

  2253.   dwarf_expr_eval (ctx, data, size);

  2254.   in_reg = ctx->location == DWARF_VALUE_REGISTER;

  2255.   if (ctx->num_pieces > 0)
  2256.     {
  2257.       int i;

  2258.       /* If the location has several pieces, and any of them are in
  2259.          registers, then we will need a frame to fetch them from.  */
  2260.       for (i = 0; i < ctx->num_pieces; i++)
  2261.         if (ctx->pieces[i].location == DWARF_VALUE_REGISTER)
  2262.           in_reg = 1;
  2263.     }

  2264.   do_cleanups (old_chain);

  2265.   return baton.needs_frame || in_reg;
  2266. }

  2267. /* A helper function that throws an unimplemented error mentioning a
  2268.    given DWARF operator.  */

  2269. static void
  2270. unimplemented (unsigned int op)
  2271. {
  2272.   const char *name = get_DW_OP_name (op);

  2273.   if (name)
  2274.     error (_("DWARF operator %s cannot be translated to an agent expression"),
  2275.            name);
  2276.   else
  2277.     error (_("Unknown DWARF operator 0x%02x cannot be translated "
  2278.              "to an agent expression"),
  2279.            op);
  2280. }

  2281. /* See dwarf2loc.h.  */

  2282. int
  2283. dwarf2_reg_to_regnum_or_error (struct gdbarch *arch, int dwarf_reg)
  2284. {
  2285.   int reg = gdbarch_dwarf2_reg_to_regnum (arch, dwarf_reg);
  2286.   if (reg == -1)
  2287.     error (_("Unable to access DWARF register number %d"), dwarf_reg);
  2288.   return reg;
  2289. }

  2290. /* A helper function that emits an access to memory.  ARCH is the
  2291.    target architecture.  EXPR is the expression which we are building.
  2292.    NBITS is the number of bits we want to read.  This emits the
  2293.    opcodes needed to read the memory and then extract the desired
  2294.    bits.  */

  2295. static void
  2296. access_memory (struct gdbarch *arch, struct agent_expr *expr, ULONGEST nbits)
  2297. {
  2298.   ULONGEST nbytes = (nbits + 7) / 8;

  2299.   gdb_assert (nbytes > 0 && nbytes <= sizeof (LONGEST));

  2300.   if (expr->tracing)
  2301.     ax_trace_quick (expr, nbytes);

  2302.   if (nbits <= 8)
  2303.     ax_simple (expr, aop_ref8);
  2304.   else if (nbits <= 16)
  2305.     ax_simple (expr, aop_ref16);
  2306.   else if (nbits <= 32)
  2307.     ax_simple (expr, aop_ref32);
  2308.   else
  2309.     ax_simple (expr, aop_ref64);

  2310.   /* If we read exactly the number of bytes we wanted, we're done.  */
  2311.   if (8 * nbytes == nbits)
  2312.     return;

  2313.   if (gdbarch_bits_big_endian (arch))
  2314.     {
  2315.       /* On a bits-big-endian machine, we want the high-order
  2316.          NBITS.  */
  2317.       ax_const_l (expr, 8 * nbytes - nbits);
  2318.       ax_simple (expr, aop_rsh_unsigned);
  2319.     }
  2320.   else
  2321.     {
  2322.       /* On a bits-little-endian box, we want the low-order NBITS.  */
  2323.       ax_zero_ext (expr, nbits);
  2324.     }
  2325. }

  2326. /* A helper function to return the frame's PC.  */

  2327. static CORE_ADDR
  2328. get_ax_pc (void *baton)
  2329. {
  2330.   struct agent_expr *expr = baton;

  2331.   return expr->scope;
  2332. }

  2333. /* Compile a DWARF location expression to an agent expression.

  2334.    EXPR is the agent expression we are building.
  2335.    LOC is the agent value we modify.
  2336.    ARCH is the architecture.
  2337.    ADDR_SIZE is the size of addresses, in bytes.
  2338.    OP_PTR is the start of the location expression.
  2339.    OP_END is one past the last byte of the location expression.

  2340.    This will throw an exception for various kinds of errors -- for
  2341.    example, if the expression cannot be compiled, or if the expression
  2342.    is invalid.  */

  2343. void
  2344. dwarf2_compile_expr_to_ax (struct agent_expr *expr, struct axs_value *loc,
  2345.                            struct gdbarch *arch, unsigned int addr_size,
  2346.                            const gdb_byte *op_ptr, const gdb_byte *op_end,
  2347.                            struct dwarf2_per_cu_data *per_cu)
  2348. {
  2349.   struct cleanup *cleanups;
  2350.   int i, *offsets;
  2351.   VEC(int) *dw_labels = NULL, *patches = NULL;
  2352.   const gdb_byte * const base = op_ptr;
  2353.   const gdb_byte *previous_piece = op_ptr;
  2354.   enum bfd_endian byte_order = gdbarch_byte_order (arch);
  2355.   ULONGEST bits_collected = 0;
  2356.   unsigned int addr_size_bits = 8 * addr_size;
  2357.   int bits_big_endian = gdbarch_bits_big_endian (arch);

  2358.   offsets = xmalloc ((op_end - op_ptr) * sizeof (int));
  2359.   cleanups = make_cleanup (xfree, offsets);

  2360.   for (i = 0; i < op_end - op_ptr; ++i)
  2361.     offsets[i] = -1;

  2362.   make_cleanup (VEC_cleanup (int), &dw_labels);
  2363.   make_cleanup (VEC_cleanup (int), &patches);

  2364.   /* By default we are making an address.  */
  2365.   loc->kind = axs_lvalue_memory;

  2366.   while (op_ptr < op_end)
  2367.     {
  2368.       enum dwarf_location_atom op = *op_ptr;
  2369.       uint64_t uoffset, reg;
  2370.       int64_t offset;
  2371.       int i;

  2372.       offsets[op_ptr - base] = expr->len;
  2373.       ++op_ptr;

  2374.       /* Our basic approach to code generation is to map DWARF
  2375.          operations directly to AX operations.  However, there are
  2376.          some differences.

  2377.          First, DWARF works on address-sized units, but AX always uses
  2378.          LONGEST.  For most operations we simply ignore this
  2379.          difference; instead we generate sign extensions as needed
  2380.          before division and comparison operations.  It would be nice
  2381.          to omit the sign extensions, but there is no way to determine
  2382.          the size of the target's LONGEST.  (This code uses the size
  2383.          of the host LONGEST in some cases -- that is a bug but it is
  2384.          difficult to fix.)

  2385.          Second, some DWARF operations cannot be translated to AX.
  2386.          For these we simply fail.  See
  2387.          http://sourceware.org/bugzilla/show_bug.cgi?id=11662.  */
  2388.       switch (op)
  2389.         {
  2390.         case DW_OP_lit0:
  2391.         case DW_OP_lit1:
  2392.         case DW_OP_lit2:
  2393.         case DW_OP_lit3:
  2394.         case DW_OP_lit4:
  2395.         case DW_OP_lit5:
  2396.         case DW_OP_lit6:
  2397.         case DW_OP_lit7:
  2398.         case DW_OP_lit8:
  2399.         case DW_OP_lit9:
  2400.         case DW_OP_lit10:
  2401.         case DW_OP_lit11:
  2402.         case DW_OP_lit12:
  2403.         case DW_OP_lit13:
  2404.         case DW_OP_lit14:
  2405.         case DW_OP_lit15:
  2406.         case DW_OP_lit16:
  2407.         case DW_OP_lit17:
  2408.         case DW_OP_lit18:
  2409.         case DW_OP_lit19:
  2410.         case DW_OP_lit20:
  2411.         case DW_OP_lit21:
  2412.         case DW_OP_lit22:
  2413.         case DW_OP_lit23:
  2414.         case DW_OP_lit24:
  2415.         case DW_OP_lit25:
  2416.         case DW_OP_lit26:
  2417.         case DW_OP_lit27:
  2418.         case DW_OP_lit28:
  2419.         case DW_OP_lit29:
  2420.         case DW_OP_lit30:
  2421.         case DW_OP_lit31:
  2422.           ax_const_l (expr, op - DW_OP_lit0);
  2423.           break;

  2424.         case DW_OP_addr:
  2425.           uoffset = extract_unsigned_integer (op_ptr, addr_size, byte_order);
  2426.           op_ptr += addr_size;
  2427.           /* Some versions of GCC emit DW_OP_addr before
  2428.              DW_OP_GNU_push_tls_address.  In this case the value is an
  2429.              index, not an address.  We don't support things like
  2430.              branching between the address and the TLS op.  */
  2431.           if (op_ptr >= op_end || *op_ptr != DW_OP_GNU_push_tls_address)
  2432.             uoffset += dwarf2_per_cu_text_offset (per_cu);
  2433.           ax_const_l (expr, uoffset);
  2434.           break;

  2435.         case DW_OP_const1u:
  2436.           ax_const_l (expr, extract_unsigned_integer (op_ptr, 1, byte_order));
  2437.           op_ptr += 1;
  2438.           break;
  2439.         case DW_OP_const1s:
  2440.           ax_const_l (expr, extract_signed_integer (op_ptr, 1, byte_order));
  2441.           op_ptr += 1;
  2442.           break;
  2443.         case DW_OP_const2u:
  2444.           ax_const_l (expr, extract_unsigned_integer (op_ptr, 2, byte_order));
  2445.           op_ptr += 2;
  2446.           break;
  2447.         case DW_OP_const2s:
  2448.           ax_const_l (expr, extract_signed_integer (op_ptr, 2, byte_order));
  2449.           op_ptr += 2;
  2450.           break;
  2451.         case DW_OP_const4u:
  2452.           ax_const_l (expr, extract_unsigned_integer (op_ptr, 4, byte_order));
  2453.           op_ptr += 4;
  2454.           break;
  2455.         case DW_OP_const4s:
  2456.           ax_const_l (expr, extract_signed_integer (op_ptr, 4, byte_order));
  2457.           op_ptr += 4;
  2458.           break;
  2459.         case DW_OP_const8u:
  2460.           ax_const_l (expr, extract_unsigned_integer (op_ptr, 8, byte_order));
  2461.           op_ptr += 8;
  2462.           break;
  2463.         case DW_OP_const8s:
  2464.           ax_const_l (expr, extract_signed_integer (op_ptr, 8, byte_order));
  2465.           op_ptr += 8;
  2466.           break;
  2467.         case DW_OP_constu:
  2468.           op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
  2469.           ax_const_l (expr, uoffset);
  2470.           break;
  2471.         case DW_OP_consts:
  2472.           op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
  2473.           ax_const_l (expr, offset);
  2474.           break;

  2475.         case DW_OP_reg0:
  2476.         case DW_OP_reg1:
  2477.         case DW_OP_reg2:
  2478.         case DW_OP_reg3:
  2479.         case DW_OP_reg4:
  2480.         case DW_OP_reg5:
  2481.         case DW_OP_reg6:
  2482.         case DW_OP_reg7:
  2483.         case DW_OP_reg8:
  2484.         case DW_OP_reg9:
  2485.         case DW_OP_reg10:
  2486.         case DW_OP_reg11:
  2487.         case DW_OP_reg12:
  2488.         case DW_OP_reg13:
  2489.         case DW_OP_reg14:
  2490.         case DW_OP_reg15:
  2491.         case DW_OP_reg16:
  2492.         case DW_OP_reg17:
  2493.         case DW_OP_reg18:
  2494.         case DW_OP_reg19:
  2495.         case DW_OP_reg20:
  2496.         case DW_OP_reg21:
  2497.         case DW_OP_reg22:
  2498.         case DW_OP_reg23:
  2499.         case DW_OP_reg24:
  2500.         case DW_OP_reg25:
  2501.         case DW_OP_reg26:
  2502.         case DW_OP_reg27:
  2503.         case DW_OP_reg28:
  2504.         case DW_OP_reg29:
  2505.         case DW_OP_reg30:
  2506.         case DW_OP_reg31:
  2507.           dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_regx");
  2508.           loc->u.reg = dwarf2_reg_to_regnum_or_error (arch, op - DW_OP_reg0);
  2509.           loc->kind = axs_lvalue_register;
  2510.           break;

  2511.         case DW_OP_regx:
  2512.           op_ptr = safe_read_uleb128 (op_ptr, op_end, &reg);
  2513.           dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_regx");
  2514.           loc->u.reg = dwarf2_reg_to_regnum_or_error (arch, reg);
  2515.           loc->kind = axs_lvalue_register;
  2516.           break;

  2517.         case DW_OP_implicit_value:
  2518.           {
  2519.             uint64_t len;

  2520.             op_ptr = safe_read_uleb128 (op_ptr, op_end, &len);
  2521.             if (op_ptr + len > op_end)
  2522.               error (_("DW_OP_implicit_value: too few bytes available."));
  2523.             if (len > sizeof (ULONGEST))
  2524.               error (_("Cannot translate DW_OP_implicit_value of %d bytes"),
  2525.                      (int) len);

  2526.             ax_const_l (expr, extract_unsigned_integer (op_ptr, len,
  2527.                                                         byte_order));
  2528.             op_ptr += len;
  2529.             dwarf_expr_require_composition (op_ptr, op_end,
  2530.                                             "DW_OP_implicit_value");

  2531.             loc->kind = axs_rvalue;
  2532.           }
  2533.           break;

  2534.         case DW_OP_stack_value:
  2535.           dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_stack_value");
  2536.           loc->kind = axs_rvalue;
  2537.           break;

  2538.         case DW_OP_breg0:
  2539.         case DW_OP_breg1:
  2540.         case DW_OP_breg2:
  2541.         case DW_OP_breg3:
  2542.         case DW_OP_breg4:
  2543.         case DW_OP_breg5:
  2544.         case DW_OP_breg6:
  2545.         case DW_OP_breg7:
  2546.         case DW_OP_breg8:
  2547.         case DW_OP_breg9:
  2548.         case DW_OP_breg10:
  2549.         case DW_OP_breg11:
  2550.         case DW_OP_breg12:
  2551.         case DW_OP_breg13:
  2552.         case DW_OP_breg14:
  2553.         case DW_OP_breg15:
  2554.         case DW_OP_breg16:
  2555.         case DW_OP_breg17:
  2556.         case DW_OP_breg18:
  2557.         case DW_OP_breg19:
  2558.         case DW_OP_breg20:
  2559.         case DW_OP_breg21:
  2560.         case DW_OP_breg22:
  2561.         case DW_OP_breg23:
  2562.         case DW_OP_breg24:
  2563.         case DW_OP_breg25:
  2564.         case DW_OP_breg26:
  2565.         case DW_OP_breg27:
  2566.         case DW_OP_breg28:
  2567.         case DW_OP_breg29:
  2568.         case DW_OP_breg30:
  2569.         case DW_OP_breg31:
  2570.           op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
  2571.           i = dwarf2_reg_to_regnum_or_error (arch, op - DW_OP_breg0);
  2572.           ax_reg (expr, i);
  2573.           if (offset != 0)
  2574.             {
  2575.               ax_const_l (expr, offset);
  2576.               ax_simple (expr, aop_add);
  2577.             }
  2578.           break;
  2579.         case DW_OP_bregx:
  2580.           {
  2581.             op_ptr = safe_read_uleb128 (op_ptr, op_end, &reg);
  2582.             op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
  2583.             i = dwarf2_reg_to_regnum_or_error (arch, reg);
  2584.             ax_reg (expr, i);
  2585.             if (offset != 0)
  2586.               {
  2587.                 ax_const_l (expr, offset);
  2588.                 ax_simple (expr, aop_add);
  2589.               }
  2590.           }
  2591.           break;
  2592.         case DW_OP_fbreg:
  2593.           {
  2594.             const gdb_byte *datastart;
  2595.             size_t datalen;
  2596.             const struct block *b;
  2597.             struct symbol *framefunc;

  2598.             b = block_for_pc (expr->scope);

  2599.             if (!b)
  2600.               error (_("No block found for address"));

  2601.             framefunc = block_linkage_function (b);

  2602.             if (!framefunc)
  2603.               error (_("No function found for block"));

  2604.             func_get_frame_base_dwarf_block (framefunc, expr->scope,
  2605.                                              &datastart, &datalen);

  2606.             op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
  2607.             dwarf2_compile_expr_to_ax (expr, loc, arch, addr_size, datastart,
  2608.                                        datastart + datalen, per_cu);
  2609.             if (loc->kind == axs_lvalue_register)
  2610.               require_rvalue (expr, loc);

  2611.             if (offset != 0)
  2612.               {
  2613.                 ax_const_l (expr, offset);
  2614.                 ax_simple (expr, aop_add);
  2615.               }

  2616.             loc->kind = axs_lvalue_memory;
  2617.           }
  2618.           break;

  2619.         case DW_OP_dup:
  2620.           ax_simple (expr, aop_dup);
  2621.           break;

  2622.         case DW_OP_drop:
  2623.           ax_simple (expr, aop_pop);
  2624.           break;

  2625.         case DW_OP_pick:
  2626.           offset = *op_ptr++;
  2627.           ax_pick (expr, offset);
  2628.           break;

  2629.         case DW_OP_swap:
  2630.           ax_simple (expr, aop_swap);
  2631.           break;

  2632.         case DW_OP_over:
  2633.           ax_pick (expr, 1);
  2634.           break;

  2635.         case DW_OP_rot:
  2636.           ax_simple (expr, aop_rot);
  2637.           break;

  2638.         case DW_OP_deref:
  2639.         case DW_OP_deref_size:
  2640.           {
  2641.             int size;

  2642.             if (op == DW_OP_deref_size)
  2643.               size = *op_ptr++;
  2644.             else
  2645.               size = addr_size;

  2646.             if (size != 1 && size != 2 && size != 4 && size != 8)
  2647.               error (_("Unsupported size %d in %s"),
  2648.                      size, get_DW_OP_name (op));
  2649.             access_memory (arch, expr, size * TARGET_CHAR_BIT);
  2650.           }
  2651.           break;

  2652.         case DW_OP_abs:
  2653.           /* Sign extend the operand.  */
  2654.           ax_ext (expr, addr_size_bits);
  2655.           ax_simple (expr, aop_dup);
  2656.           ax_const_l (expr, 0);
  2657.           ax_simple (expr, aop_less_signed);
  2658.           ax_simple (expr, aop_log_not);
  2659.           i = ax_goto (expr, aop_if_goto);
  2660.           /* We have to emit 0 - X.  */
  2661.           ax_const_l (expr, 0);
  2662.           ax_simple (expr, aop_swap);
  2663.           ax_simple (expr, aop_sub);
  2664.           ax_label (expr, i, expr->len);
  2665.           break;

  2666.         case DW_OP_neg:
  2667.           /* No need to sign extend here.  */
  2668.           ax_const_l (expr, 0);
  2669.           ax_simple (expr, aop_swap);
  2670.           ax_simple (expr, aop_sub);
  2671.           break;

  2672.         case DW_OP_not:
  2673.           /* Sign extend the operand.  */
  2674.           ax_ext (expr, addr_size_bits);
  2675.           ax_simple (expr, aop_bit_not);
  2676.           break;

  2677.         case DW_OP_plus_uconst:
  2678.           op_ptr = safe_read_uleb128 (op_ptr, op_end, &reg);
  2679.           /* It would be really weird to emit `DW_OP_plus_uconst 0',
  2680.              but we micro-optimize anyhow.  */
  2681.           if (reg != 0)
  2682.             {
  2683.               ax_const_l (expr, reg);
  2684.               ax_simple (expr, aop_add);
  2685.             }
  2686.           break;

  2687.         case DW_OP_and:
  2688.           ax_simple (expr, aop_bit_and);
  2689.           break;

  2690.         case DW_OP_div:
  2691.           /* Sign extend the operands.  */
  2692.           ax_ext (expr, addr_size_bits);
  2693.           ax_simple (expr, aop_swap);
  2694.           ax_ext (expr, addr_size_bits);
  2695.           ax_simple (expr, aop_swap);
  2696.           ax_simple (expr, aop_div_signed);
  2697.           break;

  2698.         case DW_OP_minus:
  2699.           ax_simple (expr, aop_sub);
  2700.           break;

  2701.         case DW_OP_mod:
  2702.           ax_simple (expr, aop_rem_unsigned);
  2703.           break;

  2704.         case DW_OP_mul:
  2705.           ax_simple (expr, aop_mul);
  2706.           break;

  2707.         case DW_OP_or:
  2708.           ax_simple (expr, aop_bit_or);
  2709.           break;

  2710.         case DW_OP_plus:
  2711.           ax_simple (expr, aop_add);
  2712.           break;

  2713.         case DW_OP_shl:
  2714.           ax_simple (expr, aop_lsh);
  2715.           break;

  2716.         case DW_OP_shr:
  2717.           ax_simple (expr, aop_rsh_unsigned);
  2718.           break;

  2719.         case DW_OP_shra:
  2720.           ax_simple (expr, aop_rsh_signed);
  2721.           break;

  2722.         case DW_OP_xor:
  2723.           ax_simple (expr, aop_bit_xor);
  2724.           break;

  2725.         case DW_OP_le:
  2726.           /* Sign extend the operands.  */
  2727.           ax_ext (expr, addr_size_bits);
  2728.           ax_simple (expr, aop_swap);
  2729.           ax_ext (expr, addr_size_bits);
  2730.           /* Note no swap here: A <= B is !(B < A).  */
  2731.           ax_simple (expr, aop_less_signed);
  2732.           ax_simple (expr, aop_log_not);
  2733.           break;

  2734.         case DW_OP_ge:
  2735.           /* Sign extend the operands.  */
  2736.           ax_ext (expr, addr_size_bits);
  2737.           ax_simple (expr, aop_swap);
  2738.           ax_ext (expr, addr_size_bits);
  2739.           ax_simple (expr, aop_swap);
  2740.           /* A >= B is !(A < B).  */
  2741.           ax_simple (expr, aop_less_signed);
  2742.           ax_simple (expr, aop_log_not);
  2743.           break;

  2744.         case DW_OP_eq:
  2745.           /* Sign extend the operands.  */
  2746.           ax_ext (expr, addr_size_bits);
  2747.           ax_simple (expr, aop_swap);
  2748.           ax_ext (expr, addr_size_bits);
  2749.           /* No need for a second swap here.  */
  2750.           ax_simple (expr, aop_equal);
  2751.           break;

  2752.         case DW_OP_lt:
  2753.           /* Sign extend the operands.  */
  2754.           ax_ext (expr, addr_size_bits);
  2755.           ax_simple (expr, aop_swap);
  2756.           ax_ext (expr, addr_size_bits);
  2757.           ax_simple (expr, aop_swap);
  2758.           ax_simple (expr, aop_less_signed);
  2759.           break;

  2760.         case DW_OP_gt:
  2761.           /* Sign extend the operands.  */
  2762.           ax_ext (expr, addr_size_bits);
  2763.           ax_simple (expr, aop_swap);
  2764.           ax_ext (expr, addr_size_bits);
  2765.           /* Note no swap here: A > B is B < A.  */
  2766.           ax_simple (expr, aop_less_signed);
  2767.           break;

  2768.         case DW_OP_ne:
  2769.           /* Sign extend the operands.  */
  2770.           ax_ext (expr, addr_size_bits);
  2771.           ax_simple (expr, aop_swap);
  2772.           ax_ext (expr, addr_size_bits);
  2773.           /* No need for a swap here.  */
  2774.           ax_simple (expr, aop_equal);
  2775.           ax_simple (expr, aop_log_not);
  2776.           break;

  2777.         case DW_OP_call_frame_cfa:
  2778.           {
  2779.             int regnum;
  2780.             CORE_ADDR text_offset;
  2781.             LONGEST off;
  2782.             const gdb_byte *cfa_start, *cfa_end;

  2783.             if (dwarf2_fetch_cfa_info (arch, expr->scope, per_cu,
  2784.                                        &regnum, &off,
  2785.                                        &text_offset, &cfa_start, &cfa_end))
  2786.               {
  2787.                 /* Register.  */
  2788.                 ax_reg (expr, regnum);
  2789.                 if (off != 0)
  2790.                   {
  2791.                     ax_const_l (expr, off);
  2792.                     ax_simple (expr, aop_add);
  2793.                   }
  2794.               }
  2795.             else
  2796.               {
  2797.                 /* Another expression.  */
  2798.                 ax_const_l (expr, text_offset);
  2799.                 dwarf2_compile_expr_to_ax (expr, loc, arch, addr_size,
  2800.                                            cfa_start, cfa_end, per_cu);
  2801.               }

  2802.             loc->kind = axs_lvalue_memory;
  2803.           }
  2804.           break;

  2805.         case DW_OP_GNU_push_tls_address:
  2806.           unimplemented (op);
  2807.           break;

  2808.         case DW_OP_push_object_address:
  2809.           unimplemented (op);
  2810.           break;

  2811.         case DW_OP_skip:
  2812.           offset = extract_signed_integer (op_ptr, 2, byte_order);
  2813.           op_ptr += 2;
  2814.           i = ax_goto (expr, aop_goto);
  2815.           VEC_safe_push (int, dw_labels, op_ptr + offset - base);
  2816.           VEC_safe_push (int, patches, i);
  2817.           break;

  2818.         case DW_OP_bra:
  2819.           offset = extract_signed_integer (op_ptr, 2, byte_order);
  2820.           op_ptr += 2;
  2821.           /* Zero extend the operand.  */
  2822.           ax_zero_ext (expr, addr_size_bits);
  2823.           i = ax_goto (expr, aop_if_goto);
  2824.           VEC_safe_push (int, dw_labels, op_ptr + offset - base);
  2825.           VEC_safe_push (int, patches, i);
  2826.           break;

  2827.         case DW_OP_nop:
  2828.           break;

  2829.         case DW_OP_piece:
  2830.         case DW_OP_bit_piece:
  2831.           {
  2832.             uint64_t size, offset;

  2833.             if (op_ptr - 1 == previous_piece)
  2834.               error (_("Cannot translate empty pieces to agent expressions"));
  2835.             previous_piece = op_ptr - 1;

  2836.             op_ptr = safe_read_uleb128 (op_ptr, op_end, &size);
  2837.             if (op == DW_OP_piece)
  2838.               {
  2839.                 size *= 8;
  2840.                 offset = 0;
  2841.               }
  2842.             else
  2843.               op_ptr = safe_read_uleb128 (op_ptr, op_end, &offset);

  2844.             if (bits_collected + size > 8 * sizeof (LONGEST))
  2845.               error (_("Expression pieces exceed word size"));

  2846.             /* Access the bits.  */
  2847.             switch (loc->kind)
  2848.               {
  2849.               case axs_lvalue_register:
  2850.                 ax_reg (expr, loc->u.reg);
  2851.                 break;

  2852.               case axs_lvalue_memory:
  2853.                 /* Offset the pointer, if needed.  */
  2854.                 if (offset > 8)
  2855.                   {
  2856.                     ax_const_l (expr, offset / 8);
  2857.                     ax_simple (expr, aop_add);
  2858.                     offset %= 8;
  2859.                   }
  2860.                 access_memory (arch, expr, size);
  2861.                 break;
  2862.               }

  2863.             /* For a bits-big-endian target, shift up what we already
  2864.                have.  For a bits-little-endian target, shift up the
  2865.                new data.  Note that there is a potential bug here if
  2866.                the DWARF expression leaves multiple values on the
  2867.                stack.  */
  2868.             if (bits_collected > 0)
  2869.               {
  2870.                 if (bits_big_endian)
  2871.                   {
  2872.                     ax_simple (expr, aop_swap);
  2873.                     ax_const_l (expr, size);
  2874.                     ax_simple (expr, aop_lsh);
  2875.                     /* We don't need a second swap here, because
  2876.                        aop_bit_or is symmetric.  */
  2877.                   }
  2878.                 else
  2879.                   {
  2880.                     ax_const_l (expr, size);
  2881.                     ax_simple (expr, aop_lsh);
  2882.                   }
  2883.                 ax_simple (expr, aop_bit_or);
  2884.               }

  2885.             bits_collected += size;
  2886.             loc->kind = axs_rvalue;
  2887.           }
  2888.           break;

  2889.         case DW_OP_GNU_uninit:
  2890.           unimplemented (op);

  2891.         case DW_OP_call2:
  2892.         case DW_OP_call4:
  2893.           {
  2894.             struct dwarf2_locexpr_baton block;
  2895.             int size = (op == DW_OP_call2 ? 2 : 4);
  2896.             cu_offset offset;

  2897.             uoffset = extract_unsigned_integer (op_ptr, size, byte_order);
  2898.             op_ptr += size;

  2899.             offset.cu_off = uoffset;
  2900.             block = dwarf2_fetch_die_loc_cu_off (offset, per_cu,
  2901.                                                  get_ax_pc, expr);

  2902.             /* DW_OP_call_ref is currently not supported.  */
  2903.             gdb_assert (block.per_cu == per_cu);

  2904.             dwarf2_compile_expr_to_ax (expr, loc, arch, addr_size,
  2905.                                        block.data, block.data + block.size,
  2906.                                        per_cu);
  2907.           }
  2908.           break;

  2909.         case DW_OP_call_ref:
  2910.           unimplemented (op);

  2911.         default:
  2912.           unimplemented (op);
  2913.         }
  2914.     }

  2915.   /* Patch all the branches we emitted.  */
  2916.   for (i = 0; i < VEC_length (int, patches); ++i)
  2917.     {
  2918.       int targ = offsets[VEC_index (int, dw_labels, i)];
  2919.       if (targ == -1)
  2920.         internal_error (__FILE__, __LINE__, _("invalid label"));
  2921.       ax_label (expr, VEC_index (int, patches, i), targ);
  2922.     }

  2923.   do_cleanups (cleanups);
  2924. }


  2925. /* Return the value of SYMBOL in FRAME using the DWARF-2 expression
  2926.    evaluator to calculate the location.  */
  2927. static struct value *
  2928. locexpr_read_variable (struct symbol *symbol, struct frame_info *frame)
  2929. {
  2930.   struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
  2931.   struct value *val;

  2932.   val = dwarf2_evaluate_loc_desc (SYMBOL_TYPE (symbol), frame, dlbaton->data,
  2933.                                   dlbaton->size, dlbaton->per_cu);

  2934.   return val;
  2935. }

  2936. /* Return the value of SYMBOL in FRAME at (callee) FRAME's function
  2937.    entrySYMBOL should be a function parameter, otherwise NO_ENTRY_VALUE_ERROR
  2938.    will be thrown.  */

  2939. static struct value *
  2940. locexpr_read_variable_at_entry (struct symbol *symbol, struct frame_info *frame)
  2941. {
  2942.   struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);

  2943.   return value_of_dwarf_block_entry (SYMBOL_TYPE (symbol), frame, dlbaton->data,
  2944.                                      dlbaton->size);
  2945. }

  2946. /* Return non-zero iff we need a frame to evaluate SYMBOL.  */
  2947. static int
  2948. locexpr_read_needs_frame (struct symbol *symbol)
  2949. {
  2950.   struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);

  2951.   return dwarf2_loc_desc_needs_frame (dlbaton->data, dlbaton->size,
  2952.                                       dlbaton->per_cu);
  2953. }

  2954. /* Return true if DATA points to the end of a piece.  END is one past
  2955.    the last byte in the expression.  */

  2956. static int
  2957. piece_end_p (const gdb_byte *data, const gdb_byte *end)
  2958. {
  2959.   return data == end || data[0] == DW_OP_piece || data[0] == DW_OP_bit_piece;
  2960. }

  2961. /* Helper for locexpr_describe_location_piece that finds the name of a
  2962.    DWARF register.  */

  2963. static const char *
  2964. locexpr_regname (struct gdbarch *gdbarch, int dwarf_regnum)
  2965. {
  2966.   int regnum;

  2967.   regnum = gdbarch_dwarf2_reg_to_regnum (gdbarch, dwarf_regnum);
  2968.   return gdbarch_register_name (gdbarch, regnum);
  2969. }

  2970. /* Nicely describe a single piece of a location, returning an updated
  2971.    position in the bytecode sequence.  This function cannot recognize
  2972.    all locations; if a location is not recognized, it simply returns
  2973.    DATA.  If there is an error during reading, e.g. we run off the end
  2974.    of the buffer, an error is thrown.  */

  2975. static const gdb_byte *
  2976. locexpr_describe_location_piece (struct symbol *symbol, struct ui_file *stream,
  2977.                                  CORE_ADDR addr, struct objfile *objfile,
  2978.                                  struct dwarf2_per_cu_data *per_cu,
  2979.                                  const gdb_byte *data, const gdb_byte *end,
  2980.                                  unsigned int addr_size)
  2981. {
  2982.   struct gdbarch *gdbarch = get_objfile_arch (objfile);
  2983.   size_t leb128_size;

  2984.   if (data[0] >= DW_OP_reg0 && data[0] <= DW_OP_reg31)
  2985.     {
  2986.       fprintf_filtered (stream, _("a variable in $%s"),
  2987.                         locexpr_regname (gdbarch, data[0] - DW_OP_reg0));
  2988.       data += 1;
  2989.     }
  2990.   else if (data[0] == DW_OP_regx)
  2991.     {
  2992.       uint64_t reg;

  2993.       data = safe_read_uleb128 (data + 1, end, &reg);
  2994.       fprintf_filtered (stream, _("a variable in $%s"),
  2995.                         locexpr_regname (gdbarch, reg));
  2996.     }
  2997.   else if (data[0] == DW_OP_fbreg)
  2998.     {
  2999.       const struct block *b;
  3000.       struct symbol *framefunc;
  3001.       int frame_reg = 0;
  3002.       int64_t frame_offset;
  3003.       const gdb_byte *base_data, *new_data, *save_data = data;
  3004.       size_t base_size;
  3005.       int64_t base_offset = 0;

  3006.       new_data = safe_read_sleb128 (data + 1, end, &frame_offset);
  3007.       if (!piece_end_p (new_data, end))
  3008.         return data;
  3009.       data = new_data;

  3010.       b = block_for_pc (addr);

  3011.       if (!b)
  3012.         error (_("No block found for address for symbol \"%s\"."),
  3013.                SYMBOL_PRINT_NAME (symbol));

  3014.       framefunc = block_linkage_function (b);

  3015.       if (!framefunc)
  3016.         error (_("No function found for block for symbol \"%s\"."),
  3017.                SYMBOL_PRINT_NAME (symbol));

  3018.       func_get_frame_base_dwarf_block (framefunc, addr, &base_data, &base_size);

  3019.       if (base_data[0] >= DW_OP_breg0 && base_data[0] <= DW_OP_breg31)
  3020.         {
  3021.           const gdb_byte *buf_end;

  3022.           frame_reg = base_data[0] - DW_OP_breg0;
  3023.           buf_end = safe_read_sleb128 (base_data + 1, base_data + base_size,
  3024.                                        &base_offset);
  3025.           if (buf_end != base_data + base_size)
  3026.             error (_("Unexpected opcode after "
  3027.                      "DW_OP_breg%u for symbol \"%s\"."),
  3028.                    frame_reg, SYMBOL_PRINT_NAME (symbol));
  3029.         }
  3030.       else if (base_data[0] >= DW_OP_reg0 && base_data[0] <= DW_OP_reg31)
  3031.         {
  3032.           /* The frame base is just the register, with no offset.  */
  3033.           frame_reg = base_data[0] - DW_OP_reg0;
  3034.           base_offset = 0;
  3035.         }
  3036.       else
  3037.         {
  3038.           /* We don't know what to do with the frame base expression,
  3039.              so we can't trace this variable; give up.  */
  3040.           return save_data;
  3041.         }

  3042.       fprintf_filtered (stream,
  3043.                         _("a variable at frame base reg $%s offset %s+%s"),
  3044.                         locexpr_regname (gdbarch, frame_reg),
  3045.                         plongest (base_offset), plongest (frame_offset));
  3046.     }
  3047.   else if (data[0] >= DW_OP_breg0 && data[0] <= DW_OP_breg31
  3048.            && piece_end_p (data, end))
  3049.     {
  3050.       int64_t offset;

  3051.       data = safe_read_sleb128 (data + 1, end, &offset);

  3052.       fprintf_filtered (stream,
  3053.                         _("a variable at offset %s from base reg $%s"),
  3054.                         plongest (offset),
  3055.                         locexpr_regname (gdbarch, data[0] - DW_OP_breg0));
  3056.     }

  3057.   /* The location expression for a TLS variable looks like this (on a
  3058.      64-bit LE machine):

  3059.      DW_AT_location    : 10 byte block: 3 4 0 0 0 0 0 0 0 e0
  3060.                         (DW_OP_addr: 4; DW_OP_GNU_push_tls_address)

  3061.      0x3 is the encoding for DW_OP_addr, which has an operand as long
  3062.      as the size of an address on the target machine (here is 8
  3063.      bytes).  Note that more recent version of GCC emit DW_OP_const4u
  3064.      or DW_OP_const8u, depending on address size, rather than
  3065.      DW_OP_addr.  0xe0 is the encoding for DW_OP_GNU_push_tls_address.
  3066.      The operand represents the offset at which the variable is within
  3067.      the thread local storage.  */

  3068.   else if (data + 1 + addr_size < end
  3069.            && (data[0] == DW_OP_addr
  3070.                || (addr_size == 4 && data[0] == DW_OP_const4u)
  3071.                || (addr_size == 8 && data[0] == DW_OP_const8u))
  3072.            && data[1 + addr_size] == DW_OP_GNU_push_tls_address
  3073.            && piece_end_p (data + 2 + addr_size, end))
  3074.     {
  3075.       ULONGEST offset;
  3076.       offset = extract_unsigned_integer (data + 1, addr_size,
  3077.                                          gdbarch_byte_order (gdbarch));

  3078.       fprintf_filtered (stream,
  3079.                         _("a thread-local variable at offset 0x%s "
  3080.                           "in the thread-local storage for `%s'"),
  3081.                         phex_nz (offset, addr_size), objfile_name (objfile));

  3082.       data += 1 + addr_size + 1;
  3083.     }

  3084.   /* With -gsplit-dwarf a TLS variable can also look like this:
  3085.      DW_AT_location    : 3 byte block: fc 4 e0
  3086.                         (DW_OP_GNU_const_index: 4;
  3087.                          DW_OP_GNU_push_tls_address)  */
  3088.   else if (data + 3 <= end
  3089.            && data + 1 + (leb128_size = skip_leb128 (data + 1, end)) < end
  3090.            && data[0] == DW_OP_GNU_const_index
  3091.            && leb128_size > 0
  3092.            && data[1 + leb128_size] == DW_OP_GNU_push_tls_address
  3093.            && piece_end_p (data + 2 + leb128_size, end))
  3094.     {
  3095.       uint64_t offset;

  3096.       data = safe_read_uleb128 (data + 1, end, &offset);
  3097.       offset = dwarf2_read_addr_index (per_cu, offset);
  3098.       fprintf_filtered (stream,
  3099.                         _("a thread-local variable at offset 0x%s "
  3100.                           "in the thread-local storage for `%s'"),
  3101.                         phex_nz (offset, addr_size), objfile_name (objfile));
  3102.       ++data;
  3103.     }

  3104.   else if (data[0] >= DW_OP_lit0
  3105.            && data[0] <= DW_OP_lit31
  3106.            && data + 1 < end
  3107.            && data[1] == DW_OP_stack_value)
  3108.     {
  3109.       fprintf_filtered (stream, _("the constant %d"), data[0] - DW_OP_lit0);
  3110.       data += 2;
  3111.     }

  3112.   return data;
  3113. }

  3114. /* Disassemble an expression, stopping at the end of a piece or at the
  3115.    end of the expression.  Returns a pointer to the next unread byte
  3116.    in the input expression.  If ALL is nonzero, then this function
  3117.    will keep going until it reaches the end of the expression.
  3118.    If there is an error during reading, e.g. we run off the end
  3119.    of the buffer, an error is thrown.  */

  3120. static const gdb_byte *
  3121. disassemble_dwarf_expression (struct ui_file *stream,
  3122.                               struct gdbarch *arch, unsigned int addr_size,
  3123.                               int offset_size, const gdb_byte *start,
  3124.                               const gdb_byte *data, const gdb_byte *end,
  3125.                               int indent, int all,
  3126.                               struct dwarf2_per_cu_data *per_cu)
  3127. {
  3128.   while (data < end
  3129.          && (all
  3130.              || (data[0] != DW_OP_piece && data[0] != DW_OP_bit_piece)))
  3131.     {
  3132.       enum dwarf_location_atom op = *data++;
  3133.       uint64_t ul;
  3134.       int64_t l;
  3135.       const char *name;

  3136.       name = get_DW_OP_name (op);

  3137.       if (!name)
  3138.         error (_("Unrecognized DWARF opcode 0x%02x at %ld"),
  3139.                op, (long) (data - 1 - start));
  3140.       fprintf_filtered (stream, %*ld: %s", indent + 4,
  3141.                         (long) (data - 1 - start), name);

  3142.       switch (op)
  3143.         {
  3144.         case DW_OP_addr:
  3145.           ul = extract_unsigned_integer (data, addr_size,
  3146.                                          gdbarch_byte_order (arch));
  3147.           data += addr_size;
  3148.           fprintf_filtered (stream, " 0x%s", phex_nz (ul, addr_size));
  3149.           break;

  3150.         case DW_OP_const1u:
  3151.           ul = extract_unsigned_integer (data, 1, gdbarch_byte_order (arch));
  3152.           data += 1;
  3153.           fprintf_filtered (stream, " %s", pulongest (ul));
  3154.           break;
  3155.         case DW_OP_const1s:
  3156.           l = extract_signed_integer (data, 1, gdbarch_byte_order (arch));
  3157.           data += 1;
  3158.           fprintf_filtered (stream, " %s", plongest (l));
  3159.           break;
  3160.         case DW_OP_const2u:
  3161.           ul = extract_unsigned_integer (data, 2, gdbarch_byte_order (arch));
  3162.           data += 2;
  3163.           fprintf_filtered (stream, " %s", pulongest (ul));
  3164.           break;
  3165.         case DW_OP_const2s:
  3166.           l = extract_signed_integer (data, 2, gdbarch_byte_order (arch));
  3167.           data += 2;
  3168.           fprintf_filtered (stream, " %s", plongest (l));
  3169.           break;
  3170.         case DW_OP_const4u:
  3171.           ul = extract_unsigned_integer (data, 4, gdbarch_byte_order (arch));
  3172.           data += 4;
  3173.           fprintf_filtered (stream, " %s", pulongest (ul));
  3174.           break;
  3175.         case DW_OP_const4s:
  3176.           l = extract_signed_integer (data, 4, gdbarch_byte_order (arch));
  3177.           data += 4;
  3178.           fprintf_filtered (stream, " %s", plongest (l));
  3179.           break;
  3180.         case DW_OP_const8u:
  3181.           ul = extract_unsigned_integer (data, 8, gdbarch_byte_order (arch));
  3182.           data += 8;
  3183.           fprintf_filtered (stream, " %s", pulongest (ul));
  3184.           break;
  3185.         case DW_OP_const8s:
  3186.           l = extract_signed_integer (data, 8, gdbarch_byte_order (arch));
  3187.           data += 8;
  3188.           fprintf_filtered (stream, " %s", plongest (l));
  3189.           break;
  3190.         case DW_OP_constu:
  3191.           data = safe_read_uleb128 (data, end, &ul);
  3192.           fprintf_filtered (stream, " %s", pulongest (ul));
  3193.           break;
  3194.         case DW_OP_consts:
  3195.           data = safe_read_sleb128 (data, end, &l);
  3196.           fprintf_filtered (stream, " %s", plongest (l));
  3197.           break;

  3198.         case DW_OP_reg0:
  3199.         case DW_OP_reg1:
  3200.         case DW_OP_reg2:
  3201.         case DW_OP_reg3:
  3202.         case DW_OP_reg4:
  3203.         case DW_OP_reg5:
  3204.         case DW_OP_reg6:
  3205.         case DW_OP_reg7:
  3206.         case DW_OP_reg8:
  3207.         case DW_OP_reg9:
  3208.         case DW_OP_reg10:
  3209.         case DW_OP_reg11:
  3210.         case DW_OP_reg12:
  3211.         case DW_OP_reg13:
  3212.         case DW_OP_reg14:
  3213.         case DW_OP_reg15:
  3214.         case DW_OP_reg16:
  3215.         case DW_OP_reg17:
  3216.         case DW_OP_reg18:
  3217.         case DW_OP_reg19:
  3218.         case DW_OP_reg20:
  3219.         case DW_OP_reg21:
  3220.         case DW_OP_reg22:
  3221.         case DW_OP_reg23:
  3222.         case DW_OP_reg24:
  3223.         case DW_OP_reg25:
  3224.         case DW_OP_reg26:
  3225.         case DW_OP_reg27:
  3226.         case DW_OP_reg28:
  3227.         case DW_OP_reg29:
  3228.         case DW_OP_reg30:
  3229.         case DW_OP_reg31:
  3230.           fprintf_filtered (stream, " [$%s]",
  3231.                             locexpr_regname (arch, op - DW_OP_reg0));
  3232.           break;

  3233.         case DW_OP_regx:
  3234.           data = safe_read_uleb128 (data, end, &ul);
  3235.           fprintf_filtered (stream, " %s [$%s]", pulongest (ul),
  3236.                             locexpr_regname (arch, (int) ul));
  3237.           break;

  3238.         case DW_OP_implicit_value:
  3239.           data = safe_read_uleb128 (data, end, &ul);
  3240.           data += ul;
  3241.           fprintf_filtered (stream, " %s", pulongest (ul));
  3242.           break;

  3243.         case DW_OP_breg0:
  3244.         case DW_OP_breg1:
  3245.         case DW_OP_breg2:
  3246.         case DW_OP_breg3:
  3247.         case DW_OP_breg4:
  3248.         case DW_OP_breg5:
  3249.         case DW_OP_breg6:
  3250.         case DW_OP_breg7:
  3251.         case DW_OP_breg8:
  3252.         case DW_OP_breg9:
  3253.         case DW_OP_breg10:
  3254.         case DW_OP_breg11:
  3255.         case DW_OP_breg12:
  3256.         case DW_OP_breg13:
  3257.         case DW_OP_breg14:
  3258.         case DW_OP_breg15:
  3259.         case DW_OP_breg16:
  3260.         case DW_OP_breg17:
  3261.         case DW_OP_breg18:
  3262.         case DW_OP_breg19:
  3263.         case DW_OP_breg20:
  3264.         case DW_OP_breg21:
  3265.         case DW_OP_breg22:
  3266.         case DW_OP_breg23:
  3267.         case DW_OP_breg24:
  3268.         case DW_OP_breg25:
  3269.         case DW_OP_breg26:
  3270.         case DW_OP_breg27:
  3271.         case DW_OP_breg28:
  3272.         case DW_OP_breg29:
  3273.         case DW_OP_breg30:
  3274.         case DW_OP_breg31:
  3275.           data = safe_read_sleb128 (data, end, &l);
  3276.           fprintf_filtered (stream, " %s [$%s]", plongest (l),
  3277.                             locexpr_regname (arch, op - DW_OP_breg0));
  3278.           break;

  3279.         case DW_OP_bregx:
  3280.           data = safe_read_uleb128 (data, end, &ul);
  3281.           data = safe_read_sleb128 (data, end, &l);
  3282.           fprintf_filtered (stream, " register %s [$%s] offset %s",
  3283.                             pulongest (ul),
  3284.                             locexpr_regname (arch, (int) ul),
  3285.                             plongest (l));
  3286.           break;

  3287.         case DW_OP_fbreg:
  3288.           data = safe_read_sleb128 (data, end, &l);
  3289.           fprintf_filtered (stream, " %s", plongest (l));
  3290.           break;

  3291.         case DW_OP_xderef_size:
  3292.         case DW_OP_deref_size:
  3293.         case DW_OP_pick:
  3294.           fprintf_filtered (stream, " %d", *data);
  3295.           ++data;
  3296.           break;

  3297.         case DW_OP_plus_uconst:
  3298.           data = safe_read_uleb128 (data, end, &ul);
  3299.           fprintf_filtered (stream, " %s", pulongest (ul));
  3300.           break;

  3301.         case DW_OP_skip:
  3302.           l = extract_signed_integer (data, 2, gdbarch_byte_order (arch));
  3303.           data += 2;
  3304.           fprintf_filtered (stream, " to %ld",
  3305.                             (long) (data + l - start));
  3306.           break;

  3307.         case DW_OP_bra:
  3308.           l = extract_signed_integer (data, 2, gdbarch_byte_order (arch));
  3309.           data += 2;
  3310.           fprintf_filtered (stream, " %ld",
  3311.                             (long) (data + l - start));
  3312.           break;

  3313.         case DW_OP_call2:
  3314.           ul = extract_unsigned_integer (data, 2, gdbarch_byte_order (arch));
  3315.           data += 2;
  3316.           fprintf_filtered (stream, " offset %s", phex_nz (ul, 2));
  3317.           break;

  3318.         case DW_OP_call4:
  3319.           ul = extract_unsigned_integer (data, 4, gdbarch_byte_order (arch));
  3320.           data += 4;
  3321.           fprintf_filtered (stream, " offset %s", phex_nz (ul, 4));
  3322.           break;

  3323.         case DW_OP_call_ref:
  3324.           ul = extract_unsigned_integer (data, offset_size,
  3325.                                          gdbarch_byte_order (arch));
  3326.           data += offset_size;
  3327.           fprintf_filtered (stream, " offset %s", phex_nz (ul, offset_size));
  3328.           break;

  3329.         case DW_OP_piece:
  3330.           data = safe_read_uleb128 (data, end, &ul);
  3331.           fprintf_filtered (stream, " %s (bytes)", pulongest (ul));
  3332.           break;

  3333.         case DW_OP_bit_piece:
  3334.           {
  3335.             uint64_t offset;

  3336.             data = safe_read_uleb128 (data, end, &ul);
  3337.             data = safe_read_uleb128 (data, end, &offset);
  3338.             fprintf_filtered (stream, " size %s offset %s (bits)",
  3339.                               pulongest (ul), pulongest (offset));
  3340.           }
  3341.           break;

  3342.         case DW_OP_GNU_implicit_pointer:
  3343.           {
  3344.             ul = extract_unsigned_integer (data, offset_size,
  3345.                                            gdbarch_byte_order (arch));
  3346.             data += offset_size;

  3347.             data = safe_read_sleb128 (data, end, &l);

  3348.             fprintf_filtered (stream, " DIE %s offset %s",
  3349.                               phex_nz (ul, offset_size),
  3350.                               plongest (l));
  3351.           }
  3352.           break;

  3353.         case DW_OP_GNU_deref_type:
  3354.           {
  3355.             int addr_size = *data++;
  3356.             cu_offset offset;
  3357.             struct type *type;

  3358.             data = safe_read_uleb128 (data, end, &ul);
  3359.             offset.cu_off = ul;
  3360.             type = dwarf2_get_die_type (offset, per_cu);
  3361.             fprintf_filtered (stream, "<");
  3362.             type_print (type, "", stream, -1);
  3363.             fprintf_filtered (stream, " [0x%s]> %d", phex_nz (offset.cu_off, 0),
  3364.                               addr_size);
  3365.           }
  3366.           break;

  3367.         case DW_OP_GNU_const_type:
  3368.           {
  3369.             cu_offset type_die;
  3370.             struct type *type;

  3371.             data = safe_read_uleb128 (data, end, &ul);
  3372.             type_die.cu_off = ul;
  3373.             type = dwarf2_get_die_type (type_die, per_cu);
  3374.             fprintf_filtered (stream, "<");
  3375.             type_print (type, "", stream, -1);
  3376.             fprintf_filtered (stream, " [0x%s]>", phex_nz (type_die.cu_off, 0));
  3377.           }
  3378.           break;

  3379.         case DW_OP_GNU_regval_type:
  3380.           {
  3381.             uint64_t reg;
  3382.             cu_offset type_die;
  3383.             struct type *type;

  3384.             data = safe_read_uleb128 (data, end, &reg);
  3385.             data = safe_read_uleb128 (data, end, &ul);
  3386.             type_die.cu_off = ul;

  3387.             type = dwarf2_get_die_type (type_die, per_cu);
  3388.             fprintf_filtered (stream, "<");
  3389.             type_print (type, "", stream, -1);
  3390.             fprintf_filtered (stream, " [0x%s]> [$%s]",
  3391.                               phex_nz (type_die.cu_off, 0),
  3392.                               locexpr_regname (arch, reg));
  3393.           }
  3394.           break;

  3395.         case DW_OP_GNU_convert:
  3396.         case DW_OP_GNU_reinterpret:
  3397.           {
  3398.             cu_offset type_die;

  3399.             data = safe_read_uleb128 (data, end, &ul);
  3400.             type_die.cu_off = ul;

  3401.             if (type_die.cu_off == 0)
  3402.               fprintf_filtered (stream, "<0>");
  3403.             else
  3404.               {
  3405.                 struct type *type;

  3406.                 type = dwarf2_get_die_type (type_die, per_cu);
  3407.                 fprintf_filtered (stream, "<");
  3408.                 type_print (type, "", stream, -1);
  3409.                 fprintf_filtered (stream, " [0x%s]>", phex_nz (type_die.cu_off, 0));
  3410.               }
  3411.           }
  3412.           break;

  3413.         case DW_OP_GNU_entry_value:
  3414.           data = safe_read_uleb128 (data, end, &ul);
  3415.           fputc_filtered ('\n', stream);
  3416.           disassemble_dwarf_expression (stream, arch, addr_size, offset_size,
  3417.                                         start, data, data + ul, indent + 2,
  3418.                                         all, per_cu);
  3419.           data += ul;
  3420.           continue;

  3421.         case DW_OP_GNU_parameter_ref:
  3422.           ul = extract_unsigned_integer (data, 4, gdbarch_byte_order (arch));
  3423.           data += 4;
  3424.           fprintf_filtered (stream, " offset %s", phex_nz (ul, 4));
  3425.           break;

  3426.         case DW_OP_GNU_addr_index:
  3427.           data = safe_read_uleb128 (data, end, &ul);
  3428.           ul = dwarf2_read_addr_index (per_cu, ul);
  3429.           fprintf_filtered (stream, " 0x%s", phex_nz (ul, addr_size));
  3430.           break;
  3431.         case DW_OP_GNU_const_index:
  3432.           data = safe_read_uleb128 (data, end, &ul);
  3433.           ul = dwarf2_read_addr_index (per_cu, ul);
  3434.           fprintf_filtered (stream, " %s", pulongest (ul));
  3435.           break;
  3436.         }

  3437.       fprintf_filtered (stream, "\n");
  3438.     }

  3439.   return data;
  3440. }

  3441. /* Describe a single location, which may in turn consist of multiple
  3442.    pieces.  */

  3443. static void
  3444. locexpr_describe_location_1 (struct symbol *symbol, CORE_ADDR addr,
  3445.                              struct ui_file *stream,
  3446.                              const gdb_byte *data, size_t size,
  3447.                              struct objfile *objfile, unsigned int addr_size,
  3448.                              int offset_size, struct dwarf2_per_cu_data *per_cu)
  3449. {
  3450.   const gdb_byte *end = data + size;
  3451.   int first_piece = 1, bad = 0;

  3452.   while (data < end)
  3453.     {
  3454.       const gdb_byte *here = data;
  3455.       int disassemble = 1;

  3456.       if (first_piece)
  3457.         first_piece = 0;
  3458.       else
  3459.         fprintf_filtered (stream, _(", and "));

  3460.       if (!dwarf2_always_disassemble)
  3461.         {
  3462.           data = locexpr_describe_location_piece (symbol, stream,
  3463.                                                   addr, objfile, per_cu,
  3464.                                                   data, end, addr_size);
  3465.           /* If we printed anything, or if we have an empty piece,
  3466.              then don't disassemble.  */
  3467.           if (data != here
  3468.               || data[0] == DW_OP_piece
  3469.               || data[0] == DW_OP_bit_piece)
  3470.             disassemble = 0;
  3471.         }
  3472.       if (disassemble)
  3473.         {
  3474.           fprintf_filtered (stream, _("a complex DWARF expression:\n"));
  3475.           data = disassemble_dwarf_expression (stream,
  3476.                                                get_objfile_arch (objfile),
  3477.                                                addr_size, offset_size, data,
  3478.                                                data, end, 0,
  3479.                                                dwarf2_always_disassemble,
  3480.                                                per_cu);
  3481.         }

  3482.       if (data < end)
  3483.         {
  3484.           int empty = data == here;

  3485.           if (disassemble)
  3486.             fprintf_filtered (stream, "   ");
  3487.           if (data[0] == DW_OP_piece)
  3488.             {
  3489.               uint64_t bytes;

  3490.               data = safe_read_uleb128 (data + 1, end, &bytes);

  3491.               if (empty)
  3492.                 fprintf_filtered (stream, _("an empty %s-byte piece"),
  3493.                                   pulongest (bytes));
  3494.               else
  3495.                 fprintf_filtered (stream, _(" [%s-byte piece]"),
  3496.                                   pulongest (bytes));
  3497.             }
  3498.           else if (data[0] == DW_OP_bit_piece)
  3499.             {
  3500.               uint64_t bits, offset;

  3501.               data = safe_read_uleb128 (data + 1, end, &bits);
  3502.               data = safe_read_uleb128 (data, end, &offset);

  3503.               if (empty)
  3504.                 fprintf_filtered (stream,
  3505.                                   _("an empty %s-bit piece"),
  3506.                                   pulongest (bits));
  3507.               else
  3508.                 fprintf_filtered (stream,
  3509.                                   _(" [%s-bit piece, offset %s bits]"),
  3510.                                   pulongest (bits), pulongest (offset));
  3511.             }
  3512.           else
  3513.             {
  3514.               bad = 1;
  3515.               break;
  3516.             }
  3517.         }
  3518.     }

  3519.   if (bad || data > end)
  3520.     error (_("Corrupted DWARF2 expression for \"%s\"."),
  3521.            SYMBOL_PRINT_NAME (symbol));
  3522. }

  3523. /* Print a natural-language description of SYMBOL to STREAM.  This
  3524.    version is for a symbol with a single location.  */

  3525. static void
  3526. locexpr_describe_location (struct symbol *symbol, CORE_ADDR addr,
  3527.                            struct ui_file *stream)
  3528. {
  3529.   struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
  3530.   struct objfile *objfile = dwarf2_per_cu_objfile (dlbaton->per_cu);
  3531.   unsigned int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);
  3532.   int offset_size = dwarf2_per_cu_offset_size (dlbaton->per_cu);

  3533.   locexpr_describe_location_1 (symbol, addr, stream,
  3534.                                dlbaton->data, dlbaton->size,
  3535.                                objfile, addr_size, offset_size,
  3536.                                dlbaton->per_cu);
  3537. }

  3538. /* Describe the location of SYMBOL as an agent value in VALUE, generating
  3539.    any necessary bytecode in AX.  */

  3540. static void
  3541. locexpr_tracepoint_var_ref (struct symbol *symbol, struct gdbarch *gdbarch,
  3542.                             struct agent_expr *ax, struct axs_value *value)
  3543. {
  3544.   struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
  3545.   unsigned int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);

  3546.   if (dlbaton->size == 0)
  3547.     value->optimized_out = 1;
  3548.   else
  3549.     dwarf2_compile_expr_to_ax (ax, value, gdbarch, addr_size,
  3550.                                dlbaton->data, dlbaton->data + dlbaton->size,
  3551.                                dlbaton->per_cu);
  3552. }

  3553. /* symbol_computed_ops 'generate_c_location' method.  */

  3554. static void
  3555. locexpr_generate_c_location (struct symbol *sym, struct ui_file *stream,
  3556.                              struct gdbarch *gdbarch,
  3557.                              unsigned char *registers_used,
  3558.                              CORE_ADDR pc, const char *result_name)
  3559. {
  3560.   struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (sym);
  3561.   unsigned int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);

  3562.   if (dlbaton->size == 0)
  3563.     error (_("symbol \"%s\" is optimized out"), SYMBOL_NATURAL_NAME (sym));

  3564.   compile_dwarf_expr_to_c (stream, result_name,
  3565.                            sym, pc, gdbarch, registers_used, addr_size,
  3566.                            dlbaton->data, dlbaton->data + dlbaton->size,
  3567.                            dlbaton->per_cu);
  3568. }

  3569. /* The set of location functions used with the DWARF-2 expression
  3570.    evaluator.  */
  3571. const struct symbol_computed_ops dwarf2_locexpr_funcs = {
  3572.   locexpr_read_variable,
  3573.   locexpr_read_variable_at_entry,
  3574.   locexpr_read_needs_frame,
  3575.   locexpr_describe_location,
  3576.   0,        /* location_has_loclist */
  3577.   locexpr_tracepoint_var_ref,
  3578.   locexpr_generate_c_location
  3579. };


  3580. /* Wrapper functions for location lists.  These generally find
  3581.    the appropriate location expression and call something above.  */

  3582. /* Return the value of SYMBOL in FRAME using the DWARF-2 expression
  3583.    evaluator to calculate the location.  */
  3584. static struct value *
  3585. loclist_read_variable (struct symbol *symbol, struct frame_info *frame)
  3586. {
  3587.   struct dwarf2_loclist_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
  3588.   struct value *val;
  3589.   const gdb_byte *data;
  3590.   size_t size;
  3591.   CORE_ADDR pc = frame ? get_frame_address_in_block (frame) : 0;

  3592.   data = dwarf2_find_location_expression (dlbaton, &size, pc);
  3593.   val = dwarf2_evaluate_loc_desc (SYMBOL_TYPE (symbol), frame, data, size,
  3594.                                   dlbaton->per_cu);

  3595.   return val;
  3596. }

  3597. /* Read variable SYMBOL like loclist_read_variable at (callee) FRAME's function
  3598.    entrySYMBOL should be a function parameter, otherwise NO_ENTRY_VALUE_ERROR
  3599.    will be thrown.

  3600.    Function always returns non-NULL value, it may be marked optimized out if
  3601.    inferior frame information is not available.  It throws NO_ENTRY_VALUE_ERROR
  3602.    if it cannot resolve the parameter for any reason.  */

  3603. static struct value *
  3604. loclist_read_variable_at_entry (struct symbol *symbol, struct frame_info *frame)
  3605. {
  3606.   struct dwarf2_loclist_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
  3607.   const gdb_byte *data;
  3608.   size_t size;
  3609.   CORE_ADDR pc;

  3610.   if (frame == NULL || !get_frame_func_if_available (frame, &pc))
  3611.     return allocate_optimized_out_value (SYMBOL_TYPE (symbol));

  3612.   data = dwarf2_find_location_expression (dlbaton, &size, pc);
  3613.   if (data == NULL)
  3614.     return allocate_optimized_out_value (SYMBOL_TYPE (symbol));

  3615.   return value_of_dwarf_block_entry (SYMBOL_TYPE (symbol), frame, data, size);
  3616. }

  3617. /* Return non-zero iff we need a frame to evaluate SYMBOL.  */
  3618. static int
  3619. loclist_read_needs_frame (struct symbol *symbol)
  3620. {
  3621.   /* If there's a location list, then assume we need to have a frame
  3622.      to choose the appropriate location expression.  With tracking of
  3623.      global variables this is not necessarily true, but such tracking
  3624.      is disabled in GCC at the moment until we figure out how to
  3625.      represent it.  */

  3626.   return 1;
  3627. }

  3628. /* Print a natural-language description of SYMBOL to STREAM.  This
  3629.    version applies when there is a list of different locations, each
  3630.    with a specified address range.  */

  3631. static void
  3632. loclist_describe_location (struct symbol *symbol, CORE_ADDR addr,
  3633.                            struct ui_file *stream)
  3634. {
  3635.   struct dwarf2_loclist_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
  3636.   const gdb_byte *loc_ptr, *buf_end;
  3637.   struct objfile *objfile = dwarf2_per_cu_objfile (dlbaton->per_cu);
  3638.   struct gdbarch *gdbarch = get_objfile_arch (objfile);
  3639.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  3640.   unsigned int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);
  3641.   int offset_size = dwarf2_per_cu_offset_size (dlbaton->per_cu);
  3642.   int signed_addr_p = bfd_get_sign_extend_vma (objfile->obfd);
  3643.   /* Adjust base_address for relocatable objects.  */
  3644.   CORE_ADDR base_offset = dwarf2_per_cu_text_offset (dlbaton->per_cu);
  3645.   CORE_ADDR base_address = dlbaton->base_address + base_offset;
  3646.   int done = 0;

  3647.   loc_ptr = dlbaton->data;
  3648.   buf_end = dlbaton->data + dlbaton->size;

  3649.   fprintf_filtered (stream, _("multi-location:\n"));

  3650.   /* Iterate through locations until we run out.  */
  3651.   while (!done)
  3652.     {
  3653.       CORE_ADDR low = 0, high = 0; /* init for gcc -Wall */
  3654.       int length;
  3655.       enum debug_loc_kind kind;
  3656.       const gdb_byte *new_ptr = NULL; /* init for gcc -Wall */

  3657.       if (dlbaton->from_dwo)
  3658.         kind = decode_debug_loc_dwo_addresses (dlbaton->per_cu,
  3659.                                                loc_ptr, buf_end, &new_ptr,
  3660.                                                &low, &high, byte_order);
  3661.       else
  3662.         kind = decode_debug_loc_addresses (loc_ptr, buf_end, &new_ptr,
  3663.                                            &low, &high,
  3664.                                            byte_order, addr_size,
  3665.                                            signed_addr_p);
  3666.       loc_ptr = new_ptr;
  3667.       switch (kind)
  3668.         {
  3669.         case DEBUG_LOC_END_OF_LIST:
  3670.           done = 1;
  3671.           continue;
  3672.         case DEBUG_LOC_BASE_ADDRESS:
  3673.           base_address = high + base_offset;
  3674.           fprintf_filtered (stream, _(Base address %s"),
  3675.                             paddress (gdbarch, base_address));
  3676.           continue;
  3677.         case DEBUG_LOC_START_END:
  3678.         case DEBUG_LOC_START_LENGTH:
  3679.           break;
  3680.         case DEBUG_LOC_BUFFER_OVERFLOW:
  3681.         case DEBUG_LOC_INVALID_ENTRY:
  3682.           error (_("Corrupted DWARF expression for symbol \"%s\"."),
  3683.                  SYMBOL_PRINT_NAME (symbol));
  3684.         default:
  3685.           gdb_assert_not_reached ("bad debug_loc_kind");
  3686.         }

  3687.       /* Otherwise, a location expression entry.  */
  3688.       low += base_address;
  3689.       high += base_address;

  3690.       low = gdbarch_adjust_dwarf2_addr (gdbarch, low);
  3691.       high = gdbarch_adjust_dwarf2_addr (gdbarch, high);

  3692.       length = extract_unsigned_integer (loc_ptr, 2, byte_order);
  3693.       loc_ptr += 2;

  3694.       /* (It would improve readability to print only the minimum
  3695.          necessary digits of the second number of the range.)  */
  3696.       fprintf_filtered (stream, _("  Range %s-%s: "),
  3697.                         paddress (gdbarch, low), paddress (gdbarch, high));

  3698.       /* Now describe this particular location.  */
  3699.       locexpr_describe_location_1 (symbol, low, stream, loc_ptr, length,
  3700.                                    objfile, addr_size, offset_size,
  3701.                                    dlbaton->per_cu);

  3702.       fprintf_filtered (stream, "\n");

  3703.       loc_ptr += length;
  3704.     }
  3705. }

  3706. /* Describe the location of SYMBOL as an agent value in VALUE, generating
  3707.    any necessary bytecode in AX.  */
  3708. static void
  3709. loclist_tracepoint_var_ref (struct symbol *symbol, struct gdbarch *gdbarch,
  3710.                             struct agent_expr *ax, struct axs_value *value)
  3711. {
  3712.   struct dwarf2_loclist_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
  3713.   const gdb_byte *data;
  3714.   size_t size;
  3715.   unsigned int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);

  3716.   data = dwarf2_find_location_expression (dlbaton, &size, ax->scope);
  3717.   if (size == 0)
  3718.     value->optimized_out = 1;
  3719.   else
  3720.     dwarf2_compile_expr_to_ax (ax, value, gdbarch, addr_size, data, data + size,
  3721.                                dlbaton->per_cu);
  3722. }

  3723. /* symbol_computed_ops 'generate_c_location' method.  */

  3724. static void
  3725. loclist_generate_c_location (struct symbol *sym, struct ui_file *stream,
  3726.                              struct gdbarch *gdbarch,
  3727.                              unsigned char *registers_used,
  3728.                              CORE_ADDR pc, const char *result_name)
  3729. {
  3730.   struct dwarf2_loclist_baton *dlbaton = SYMBOL_LOCATION_BATON (sym);
  3731.   unsigned int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);
  3732.   const gdb_byte *data;
  3733.   size_t size;

  3734.   data = dwarf2_find_location_expression (dlbaton, &size, pc);
  3735.   if (size == 0)
  3736.     error (_("symbol \"%s\" is optimized out"), SYMBOL_NATURAL_NAME (sym));

  3737.   compile_dwarf_expr_to_c (stream, result_name,
  3738.                            sym, pc, gdbarch, registers_used, addr_size,
  3739.                            data, data + size,
  3740.                            dlbaton->per_cu);
  3741. }

  3742. /* The set of location functions used with the DWARF-2 expression
  3743.    evaluator and location lists.  */
  3744. const struct symbol_computed_ops dwarf2_loclist_funcs = {
  3745.   loclist_read_variable,
  3746.   loclist_read_variable_at_entry,
  3747.   loclist_read_needs_frame,
  3748.   loclist_describe_location,
  3749.   1,        /* location_has_loclist */
  3750.   loclist_tracepoint_var_ref,
  3751.   loclist_generate_c_location
  3752. };

  3753. /* Provide a prototype to silence -Wmissing-prototypes.  */
  3754. extern initialize_file_ftype _initialize_dwarf2loc;

  3755. void
  3756. _initialize_dwarf2loc (void)
  3757. {
  3758.   add_setshow_zuinteger_cmd ("entry-values", class_maintenance,
  3759.                              &entry_values_debug,
  3760.                              _("Set entry values and tail call frames "
  3761.                                "debugging."),
  3762.                              _("Show entry values and tail call frames "
  3763.                                "debugging."),
  3764.                              _("When non-zero, the process of determining "
  3765.                                "parameter values from function entry point "
  3766.                                "and tail call frames will be printed."),
  3767.                              NULL,
  3768.                              show_entry_values_debug,
  3769.                              &setdebuglist, &showdebuglist);
  3770. }