GDB (API)
/home/stan/gdb/src/gdb/dwarf2loc.c
Go to the documentation of this file.
00001 /* DWARF 2 location expression support for GDB.
00002 
00003    Copyright (C) 2003-2013 Free Software Foundation, Inc.
00004 
00005    Contributed by Daniel Jacobowitz, MontaVista Software, Inc.
00006 
00007    This file is part of GDB.
00008 
00009    This program is free software; you can redistribute it and/or modify
00010    it under the terms of the GNU General Public License as published by
00011    the Free Software Foundation; either version 3 of the License, or
00012    (at your option) any later version.
00013 
00014    This program is distributed in the hope that it will be useful,
00015    but WITHOUT ANY WARRANTY; without even the implied warranty of
00016    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017    GNU General Public License for more details.
00018 
00019    You should have received a copy of the GNU General Public License
00020    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
00021 
00022 #include "defs.h"
00023 #include "ui-out.h"
00024 #include "value.h"
00025 #include "frame.h"
00026 #include "gdbcore.h"
00027 #include "target.h"
00028 #include "inferior.h"
00029 #include "ax.h"
00030 #include "ax-gdb.h"
00031 #include "regcache.h"
00032 #include "objfiles.h"
00033 #include "exceptions.h"
00034 #include "block.h"
00035 #include "gdbcmd.h"
00036 
00037 #include "dwarf2.h"
00038 #include "dwarf2expr.h"
00039 #include "dwarf2loc.h"
00040 #include "dwarf2-frame.h"
00041 
00042 #include "gdb_string.h"
00043 #include "gdb_assert.h"
00044 
00045 extern int dwarf2_always_disassemble;
00046 
00047 static void dwarf_expr_frame_base_1 (struct symbol *framefunc, CORE_ADDR pc,
00048                                      const gdb_byte **start, size_t *length);
00049 
00050 static const struct dwarf_expr_context_funcs dwarf_expr_ctx_funcs;
00051 
00052 static struct value *dwarf2_evaluate_loc_desc_full (struct type *type,
00053                                                     struct frame_info *frame,
00054                                                     const gdb_byte *data,
00055                                                     size_t size,
00056                                                     struct dwarf2_per_cu_data *per_cu,
00057                                                     LONGEST byte_offset);
00058 
00059 /* Until these have formal names, we define these here.
00060    ref: http://gcc.gnu.org/wiki/DebugFission
00061    Each entry in .debug_loc.dwo begins with a byte that describes the entry,
00062    and is then followed by data specific to that entry.  */
00063 
00064 enum debug_loc_kind
00065 {
00066   /* Indicates the end of the list of entries.  */
00067   DEBUG_LOC_END_OF_LIST = 0,
00068 
00069   /* This is followed by an unsigned LEB128 number that is an index into
00070      .debug_addr and specifies the base address for all following entries.  */
00071   DEBUG_LOC_BASE_ADDRESS = 1,
00072 
00073   /* This is followed by two unsigned LEB128 numbers that are indices into
00074      .debug_addr and specify the beginning and ending addresses, and then
00075      a normal location expression as in .debug_loc.  */
00076   DEBUG_LOC_START_END = 2,
00077 
00078   /* This is followed by an unsigned LEB128 number that is an index into
00079      .debug_addr and specifies the beginning address, and a 4 byte unsigned
00080      number that specifies the length, and then a normal location expression
00081      as in .debug_loc.  */
00082   DEBUG_LOC_START_LENGTH = 3,
00083 
00084   /* An internal value indicating there is insufficient data.  */
00085   DEBUG_LOC_BUFFER_OVERFLOW = -1,
00086 
00087   /* An internal value indicating an invalid kind of entry was found.  */
00088   DEBUG_LOC_INVALID_ENTRY = -2
00089 };
00090 
00091 /* Helper function which throws an error if a synthetic pointer is
00092    invalid.  */
00093 
00094 static void
00095 invalid_synthetic_pointer (void)
00096 {
00097   error (_("access outside bounds of object "
00098            "referenced via synthetic pointer"));
00099 }
00100 
00101 /* Decode the addresses in a non-dwo .debug_loc entry.
00102    A pointer to the next byte to examine is returned in *NEW_PTR.
00103    The encoded low,high addresses are return in *LOW,*HIGH.
00104    The result indicates the kind of entry found.  */
00105 
00106 static enum debug_loc_kind
00107 decode_debug_loc_addresses (const gdb_byte *loc_ptr, const gdb_byte *buf_end,
00108                             const gdb_byte **new_ptr,
00109                             CORE_ADDR *low, CORE_ADDR *high,
00110                             enum bfd_endian byte_order,
00111                             unsigned int addr_size,
00112                             int signed_addr_p)
00113 {
00114   CORE_ADDR base_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
00115 
00116   if (buf_end - loc_ptr < 2 * addr_size)
00117     return DEBUG_LOC_BUFFER_OVERFLOW;
00118 
00119   if (signed_addr_p)
00120     *low = extract_signed_integer (loc_ptr, addr_size, byte_order);
00121   else
00122     *low = extract_unsigned_integer (loc_ptr, addr_size, byte_order);
00123   loc_ptr += addr_size;
00124 
00125   if (signed_addr_p)
00126     *high = extract_signed_integer (loc_ptr, addr_size, byte_order);
00127   else
00128     *high = extract_unsigned_integer (loc_ptr, addr_size, byte_order);
00129   loc_ptr += addr_size;
00130 
00131   *new_ptr = loc_ptr;
00132 
00133   /* A base-address-selection entry.  */
00134   if ((*low & base_mask) == base_mask)
00135     return DEBUG_LOC_BASE_ADDRESS;
00136 
00137   /* An end-of-list entry.  */
00138   if (*low == 0 && *high == 0)
00139     return DEBUG_LOC_END_OF_LIST;
00140 
00141   return DEBUG_LOC_START_END;
00142 }
00143 
00144 /* Decode the addresses in .debug_loc.dwo entry.
00145    A pointer to the next byte to examine is returned in *NEW_PTR.
00146    The encoded low,high addresses are return in *LOW,*HIGH.
00147    The result indicates the kind of entry found.  */
00148 
00149 static enum debug_loc_kind
00150 decode_debug_loc_dwo_addresses (struct dwarf2_per_cu_data *per_cu,
00151                                 const gdb_byte *loc_ptr,
00152                                 const gdb_byte *buf_end,
00153                                 const gdb_byte **new_ptr,
00154                                 CORE_ADDR *low, CORE_ADDR *high,
00155                                 enum bfd_endian byte_order)
00156 {
00157   uint64_t low_index, high_index;
00158 
00159   if (loc_ptr == buf_end)
00160     return DEBUG_LOC_BUFFER_OVERFLOW;
00161 
00162   switch (*loc_ptr++)
00163     {
00164     case DEBUG_LOC_END_OF_LIST:
00165       *new_ptr = loc_ptr;
00166       return DEBUG_LOC_END_OF_LIST;
00167     case DEBUG_LOC_BASE_ADDRESS:
00168       *low = 0;
00169       loc_ptr = gdb_read_uleb128 (loc_ptr, buf_end, &high_index);
00170       if (loc_ptr == NULL)
00171         return DEBUG_LOC_BUFFER_OVERFLOW;
00172       *high = dwarf2_read_addr_index (per_cu, high_index);
00173       *new_ptr = loc_ptr;
00174       return DEBUG_LOC_BASE_ADDRESS;
00175     case DEBUG_LOC_START_END:
00176       loc_ptr = gdb_read_uleb128 (loc_ptr, buf_end, &low_index);
00177       if (loc_ptr == NULL)
00178         return DEBUG_LOC_BUFFER_OVERFLOW;
00179       *low = dwarf2_read_addr_index (per_cu, low_index);
00180       loc_ptr = gdb_read_uleb128 (loc_ptr, buf_end, &high_index);
00181       if (loc_ptr == NULL)
00182         return DEBUG_LOC_BUFFER_OVERFLOW;
00183       *high = dwarf2_read_addr_index (per_cu, high_index);
00184       *new_ptr = loc_ptr;
00185       return DEBUG_LOC_START_END;
00186     case DEBUG_LOC_START_LENGTH:
00187       loc_ptr = gdb_read_uleb128 (loc_ptr, buf_end, &low_index);
00188       if (loc_ptr == NULL)
00189         return DEBUG_LOC_BUFFER_OVERFLOW;
00190       *low = dwarf2_read_addr_index (per_cu, low_index);
00191       if (loc_ptr + 4 > buf_end)
00192         return DEBUG_LOC_BUFFER_OVERFLOW;
00193       *high = *low;
00194       *high += extract_unsigned_integer (loc_ptr, 4, byte_order);
00195       *new_ptr = loc_ptr + 4;
00196       return DEBUG_LOC_START_LENGTH;
00197     default:
00198       return DEBUG_LOC_INVALID_ENTRY;
00199     }
00200 }
00201 
00202 /* A function for dealing with location lists.  Given a
00203    symbol baton (BATON) and a pc value (PC), find the appropriate
00204    location expression, set *LOCEXPR_LENGTH, and return a pointer
00205    to the beginning of the expression.  Returns NULL on failure.
00206 
00207    For now, only return the first matching location expression; there
00208    can be more than one in the list.  */
00209 
00210 const gdb_byte *
00211 dwarf2_find_location_expression (struct dwarf2_loclist_baton *baton,
00212                                  size_t *locexpr_length, CORE_ADDR pc)
00213 {
00214   struct objfile *objfile = dwarf2_per_cu_objfile (baton->per_cu);
00215   struct gdbarch *gdbarch = get_objfile_arch (objfile);
00216   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
00217   unsigned int addr_size = dwarf2_per_cu_addr_size (baton->per_cu);
00218   int signed_addr_p = bfd_get_sign_extend_vma (objfile->obfd);
00219   /* Adjust base_address for relocatable objects.  */
00220   CORE_ADDR base_offset = dwarf2_per_cu_text_offset (baton->per_cu);
00221   CORE_ADDR base_address = baton->base_address + base_offset;
00222   const gdb_byte *loc_ptr, *buf_end;
00223 
00224   loc_ptr = baton->data;
00225   buf_end = baton->data + baton->size;
00226 
00227   while (1)
00228     {
00229       CORE_ADDR low = 0, high = 0; /* init for gcc -Wall */
00230       int length;
00231       enum debug_loc_kind kind;
00232       const gdb_byte *new_ptr = NULL; /* init for gcc -Wall */
00233 
00234       if (baton->from_dwo)
00235         kind = decode_debug_loc_dwo_addresses (baton->per_cu,
00236                                                loc_ptr, buf_end, &new_ptr,
00237                                                &low, &high, byte_order);
00238       else
00239         kind = decode_debug_loc_addresses (loc_ptr, buf_end, &new_ptr,
00240                                            &low, &high,
00241                                            byte_order, addr_size,
00242                                            signed_addr_p);
00243       loc_ptr = new_ptr;
00244       switch (kind)
00245         {
00246         case DEBUG_LOC_END_OF_LIST:
00247           *locexpr_length = 0;
00248           return NULL;
00249         case DEBUG_LOC_BASE_ADDRESS:
00250           base_address = high + base_offset;
00251           continue;
00252         case DEBUG_LOC_START_END:
00253         case DEBUG_LOC_START_LENGTH:
00254           break;
00255         case DEBUG_LOC_BUFFER_OVERFLOW:
00256         case DEBUG_LOC_INVALID_ENTRY:
00257           error (_("dwarf2_find_location_expression: "
00258                    "Corrupted DWARF expression."));
00259         default:
00260           gdb_assert_not_reached ("bad debug_loc_kind");
00261         }
00262 
00263       /* Otherwise, a location expression entry.
00264          If the entry is from a DWO, don't add base address: the entry is
00265          from .debug_addr which has absolute addresses.  */
00266       if (! baton->from_dwo)
00267         {
00268           low += base_address;
00269           high += base_address;
00270         }
00271 
00272       length = extract_unsigned_integer (loc_ptr, 2, byte_order);
00273       loc_ptr += 2;
00274 
00275       if (low == high && pc == low)
00276         {
00277           /* This is entry PC record present only at entry point
00278              of a function.  Verify it is really the function entry point.  */
00279 
00280           struct block *pc_block = block_for_pc (pc);
00281           struct symbol *pc_func = NULL;
00282 
00283           if (pc_block)
00284             pc_func = block_linkage_function (pc_block);
00285 
00286           if (pc_func && pc == BLOCK_START (SYMBOL_BLOCK_VALUE (pc_func)))
00287             {
00288               *locexpr_length = length;
00289               return loc_ptr;
00290             }
00291         }
00292 
00293       if (pc >= low && pc < high)
00294         {
00295           *locexpr_length = length;
00296           return loc_ptr;
00297         }
00298 
00299       loc_ptr += length;
00300     }
00301 }
00302 
00303 /* This is the baton used when performing dwarf2 expression
00304    evaluation.  */
00305 struct dwarf_expr_baton
00306 {
00307   struct frame_info *frame;
00308   struct dwarf2_per_cu_data *per_cu;
00309 };
00310 
00311 /* Helper functions for dwarf2_evaluate_loc_desc.  */
00312 
00313 /* Using the frame specified in BATON, return the value of register
00314    REGNUM, treated as a pointer.  */
00315 static CORE_ADDR
00316 dwarf_expr_read_reg (void *baton, int dwarf_regnum)
00317 {
00318   struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
00319   struct gdbarch *gdbarch = get_frame_arch (debaton->frame);
00320   CORE_ADDR result;
00321   int regnum;
00322 
00323   regnum = gdbarch_dwarf2_reg_to_regnum (gdbarch, dwarf_regnum);
00324   result = address_from_register (builtin_type (gdbarch)->builtin_data_ptr,
00325                                   regnum, debaton->frame);
00326   return result;
00327 }
00328 
00329 /* Read memory at ADDR (length LEN) into BUF.  */
00330 
00331 static void
00332 dwarf_expr_read_mem (void *baton, gdb_byte *buf, CORE_ADDR addr, size_t len)
00333 {
00334   read_memory (addr, buf, len);
00335 }
00336 
00337 /* Using the frame specified in BATON, find the location expression
00338    describing the frame base.  Return a pointer to it in START and
00339    its length in LENGTH.  */
00340 static void
00341 dwarf_expr_frame_base (void *baton, const gdb_byte **start, size_t * length)
00342 {
00343   /* FIXME: cagney/2003-03-26: This code should be using
00344      get_frame_base_address(), and then implement a dwarf2 specific
00345      this_base method.  */
00346   struct symbol *framefunc;
00347   struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
00348   struct block *bl = get_frame_block (debaton->frame, NULL);
00349 
00350   if (bl == NULL)
00351     error (_("frame address is not available."));
00352 
00353   /* Use block_linkage_function, which returns a real (not inlined)
00354      function, instead of get_frame_function, which may return an
00355      inlined function.  */
00356   framefunc = block_linkage_function (bl);
00357 
00358   /* If we found a frame-relative symbol then it was certainly within
00359      some function associated with a frame. If we can't find the frame,
00360      something has gone wrong.  */
00361   gdb_assert (framefunc != NULL);
00362 
00363   dwarf_expr_frame_base_1 (framefunc,
00364                            get_frame_address_in_block (debaton->frame),
00365                            start, length);
00366 }
00367 
00368 /* Implement find_frame_base_location method for LOC_BLOCK functions using
00369    DWARF expression for its DW_AT_frame_base.  */
00370 
00371 static void
00372 locexpr_find_frame_base_location (struct symbol *framefunc, CORE_ADDR pc,
00373                                   const gdb_byte **start, size_t *length)
00374 {
00375   struct dwarf2_locexpr_baton *symbaton = SYMBOL_LOCATION_BATON (framefunc);
00376 
00377   *length = symbaton->size;
00378   *start = symbaton->data;
00379 }
00380 
00381 /* Vector for inferior functions as represented by LOC_BLOCK, if the inferior
00382    function uses DWARF expression for its DW_AT_frame_base.  */
00383 
00384 const struct symbol_block_ops dwarf2_block_frame_base_locexpr_funcs =
00385 {
00386   locexpr_find_frame_base_location
00387 };
00388 
00389 /* Implement find_frame_base_location method for LOC_BLOCK functions using
00390    DWARF location list for its DW_AT_frame_base.  */
00391 
00392 static void
00393 loclist_find_frame_base_location (struct symbol *framefunc, CORE_ADDR pc,
00394                                   const gdb_byte **start, size_t *length)
00395 {
00396   struct dwarf2_loclist_baton *symbaton = SYMBOL_LOCATION_BATON (framefunc);
00397 
00398   *start = dwarf2_find_location_expression (symbaton, length, pc);
00399 }
00400 
00401 /* Vector for inferior functions as represented by LOC_BLOCK, if the inferior
00402    function uses DWARF location list for its DW_AT_frame_base.  */
00403 
00404 const struct symbol_block_ops dwarf2_block_frame_base_loclist_funcs =
00405 {
00406   loclist_find_frame_base_location
00407 };
00408 
00409 static void
00410 dwarf_expr_frame_base_1 (struct symbol *framefunc, CORE_ADDR pc,
00411                          const gdb_byte **start, size_t *length)
00412 {
00413   if (SYMBOL_BLOCK_OPS (framefunc) != NULL)
00414     {
00415       const struct symbol_block_ops *ops_block = SYMBOL_BLOCK_OPS (framefunc);
00416 
00417       ops_block->find_frame_base_location (framefunc, pc, start, length);
00418     }
00419   else
00420     *length = 0;
00421 
00422   if (*length == 0)
00423     error (_("Could not find the frame base for \"%s\"."),
00424            SYMBOL_NATURAL_NAME (framefunc));
00425 }
00426 
00427 /* Helper function for dwarf2_evaluate_loc_desc.  Computes the CFA for
00428    the frame in BATON.  */
00429 
00430 static CORE_ADDR
00431 dwarf_expr_frame_cfa (void *baton)
00432 {
00433   struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
00434 
00435   return dwarf2_frame_cfa (debaton->frame);
00436 }
00437 
00438 /* Helper function for dwarf2_evaluate_loc_desc.  Computes the PC for
00439    the frame in BATON.  */
00440 
00441 static CORE_ADDR
00442 dwarf_expr_frame_pc (void *baton)
00443 {
00444   struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
00445 
00446   return get_frame_address_in_block (debaton->frame);
00447 }
00448 
00449 /* Using the objfile specified in BATON, find the address for the
00450    current thread's thread-local storage with offset OFFSET.  */
00451 static CORE_ADDR
00452 dwarf_expr_tls_address (void *baton, CORE_ADDR offset)
00453 {
00454   struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
00455   struct objfile *objfile = dwarf2_per_cu_objfile (debaton->per_cu);
00456 
00457   return target_translate_tls_address (objfile, offset);
00458 }
00459 
00460 /* Call DWARF subroutine from DW_AT_location of DIE at DIE_OFFSET in
00461    current CU (as is PER_CU).  State of the CTX is not affected by the
00462    call and return.  */
00463 
00464 static void
00465 per_cu_dwarf_call (struct dwarf_expr_context *ctx, cu_offset die_offset,
00466                    struct dwarf2_per_cu_data *per_cu,
00467                    CORE_ADDR (*get_frame_pc) (void *baton),
00468                    void *baton)
00469 {
00470   struct dwarf2_locexpr_baton block;
00471 
00472   block = dwarf2_fetch_die_loc_cu_off (die_offset, per_cu, get_frame_pc, baton);
00473 
00474   /* DW_OP_call_ref is currently not supported.  */
00475   gdb_assert (block.per_cu == per_cu);
00476 
00477   dwarf_expr_eval (ctx, block.data, block.size);
00478 }
00479 
00480 /* Helper interface of per_cu_dwarf_call for dwarf2_evaluate_loc_desc.  */
00481 
00482 static void
00483 dwarf_expr_dwarf_call (struct dwarf_expr_context *ctx, cu_offset die_offset)
00484 {
00485   struct dwarf_expr_baton *debaton = ctx->baton;
00486 
00487   per_cu_dwarf_call (ctx, die_offset, debaton->per_cu,
00488                      ctx->funcs->get_frame_pc, ctx->baton);
00489 }
00490 
00491 /* Callback function for dwarf2_evaluate_loc_desc.  */
00492 
00493 static struct type *
00494 dwarf_expr_get_base_type (struct dwarf_expr_context *ctx,
00495                           cu_offset die_offset)
00496 {
00497   struct dwarf_expr_baton *debaton = ctx->baton;
00498 
00499   return dwarf2_get_die_type (die_offset, debaton->per_cu);
00500 }
00501 
00502 /* See dwarf2loc.h.  */
00503 
00504 unsigned int entry_values_debug = 0;
00505 
00506 /* Helper to set entry_values_debug.  */
00507 
00508 static void
00509 show_entry_values_debug (struct ui_file *file, int from_tty,
00510                          struct cmd_list_element *c, const char *value)
00511 {
00512   fprintf_filtered (file,
00513                     _("Entry values and tail call frames debugging is %s.\n"),
00514                     value);
00515 }
00516 
00517 /* Find DW_TAG_GNU_call_site's DW_AT_GNU_call_site_target address.
00518    CALLER_FRAME (for registers) can be NULL if it is not known.  This function
00519    always returns valid address or it throws NO_ENTRY_VALUE_ERROR.  */
00520 
00521 static CORE_ADDR
00522 call_site_to_target_addr (struct gdbarch *call_site_gdbarch,
00523                           struct call_site *call_site,
00524                           struct frame_info *caller_frame)
00525 {
00526   switch (FIELD_LOC_KIND (call_site->target))
00527     {
00528     case FIELD_LOC_KIND_DWARF_BLOCK:
00529       {
00530         struct dwarf2_locexpr_baton *dwarf_block;
00531         struct value *val;
00532         struct type *caller_core_addr_type;
00533         struct gdbarch *caller_arch;
00534 
00535         dwarf_block = FIELD_DWARF_BLOCK (call_site->target);
00536         if (dwarf_block == NULL)
00537           {
00538             struct bound_minimal_symbol msym;
00539             
00540             msym = lookup_minimal_symbol_by_pc (call_site->pc - 1);
00541             throw_error (NO_ENTRY_VALUE_ERROR,
00542                          _("DW_AT_GNU_call_site_target is not specified "
00543                            "at %s in %s"),
00544                          paddress (call_site_gdbarch, call_site->pc),
00545                          (msym.minsym == NULL ? "???"
00546                           : SYMBOL_PRINT_NAME (msym.minsym)));
00547                         
00548           }
00549         if (caller_frame == NULL)
00550           {
00551             struct bound_minimal_symbol msym;
00552             
00553             msym = lookup_minimal_symbol_by_pc (call_site->pc - 1);
00554             throw_error (NO_ENTRY_VALUE_ERROR,
00555                          _("DW_AT_GNU_call_site_target DWARF block resolving "
00556                            "requires known frame which is currently not "
00557                            "available at %s in %s"),
00558                          paddress (call_site_gdbarch, call_site->pc),
00559                          (msym.minsym == NULL ? "???"
00560                           : SYMBOL_PRINT_NAME (msym.minsym)));
00561                         
00562           }
00563         caller_arch = get_frame_arch (caller_frame);
00564         caller_core_addr_type = builtin_type (caller_arch)->builtin_func_ptr;
00565         val = dwarf2_evaluate_loc_desc (caller_core_addr_type, caller_frame,
00566                                         dwarf_block->data, dwarf_block->size,
00567                                         dwarf_block->per_cu);
00568         /* DW_AT_GNU_call_site_target is a DWARF expression, not a DWARF
00569            location.  */
00570         if (VALUE_LVAL (val) == lval_memory)
00571           return value_address (val);
00572         else
00573           return value_as_address (val);
00574       }
00575 
00576     case FIELD_LOC_KIND_PHYSNAME:
00577       {
00578         const char *physname;
00579         struct minimal_symbol *msym;
00580 
00581         physname = FIELD_STATIC_PHYSNAME (call_site->target);
00582 
00583         /* Handle both the mangled and demangled PHYSNAME.  */
00584         msym = lookup_minimal_symbol (physname, NULL, NULL);
00585         if (msym == NULL)
00586           {
00587             msym = lookup_minimal_symbol_by_pc (call_site->pc - 1).minsym;
00588             throw_error (NO_ENTRY_VALUE_ERROR,
00589                          _("Cannot find function \"%s\" for a call site target "
00590                            "at %s in %s"),
00591                          physname, paddress (call_site_gdbarch, call_site->pc),
00592                          msym == NULL ? "???" : SYMBOL_PRINT_NAME (msym));
00593                         
00594           }
00595         return SYMBOL_VALUE_ADDRESS (msym);
00596       }
00597 
00598     case FIELD_LOC_KIND_PHYSADDR:
00599       return FIELD_STATIC_PHYSADDR (call_site->target);
00600 
00601     default:
00602       internal_error (__FILE__, __LINE__, _("invalid call site target kind"));
00603     }
00604 }
00605 
00606 /* Convert function entry point exact address ADDR to the function which is
00607    compliant with TAIL_CALL_LIST_COMPLETE condition.  Throw
00608    NO_ENTRY_VALUE_ERROR otherwise.  */
00609 
00610 static struct symbol *
00611 func_addr_to_tail_call_list (struct gdbarch *gdbarch, CORE_ADDR addr)
00612 {
00613   struct symbol *sym = find_pc_function (addr);
00614   struct type *type;
00615 
00616   if (sym == NULL || BLOCK_START (SYMBOL_BLOCK_VALUE (sym)) != addr)
00617     throw_error (NO_ENTRY_VALUE_ERROR,
00618                  _("DW_TAG_GNU_call_site resolving failed to find function "
00619                    "name for address %s"),
00620                  paddress (gdbarch, addr));
00621 
00622   type = SYMBOL_TYPE (sym);
00623   gdb_assert (TYPE_CODE (type) == TYPE_CODE_FUNC);
00624   gdb_assert (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_FUNC);
00625 
00626   return sym;
00627 }
00628 
00629 /* Verify function with entry point exact address ADDR can never call itself
00630    via its tail calls (incl. transitively).  Throw NO_ENTRY_VALUE_ERROR if it
00631    can call itself via tail calls.
00632 
00633    If a funtion can tail call itself its entry value based parameters are
00634    unreliable.  There is no verification whether the value of some/all
00635    parameters is unchanged through the self tail call, we expect if there is
00636    a self tail call all the parameters can be modified.  */
00637 
00638 static void
00639 func_verify_no_selftailcall (struct gdbarch *gdbarch, CORE_ADDR verify_addr)
00640 {
00641   struct obstack addr_obstack;
00642   struct cleanup *old_chain;
00643   CORE_ADDR addr;
00644 
00645   /* Track here CORE_ADDRs which were already visited.  */
00646   htab_t addr_hash;
00647 
00648   /* The verification is completely unordered.  Track here function addresses
00649      which still need to be iterated.  */
00650   VEC (CORE_ADDR) *todo = NULL;
00651 
00652   obstack_init (&addr_obstack);
00653   old_chain = make_cleanup_obstack_free (&addr_obstack);   
00654   addr_hash = htab_create_alloc_ex (64, core_addr_hash, core_addr_eq, NULL,
00655                                     &addr_obstack, hashtab_obstack_allocate,
00656                                     NULL);
00657   make_cleanup_htab_delete (addr_hash);
00658 
00659   make_cleanup (VEC_cleanup (CORE_ADDR), &todo);
00660 
00661   VEC_safe_push (CORE_ADDR, todo, verify_addr);
00662   while (!VEC_empty (CORE_ADDR, todo))
00663     {
00664       struct symbol *func_sym;
00665       struct call_site *call_site;
00666 
00667       addr = VEC_pop (CORE_ADDR, todo);
00668 
00669       func_sym = func_addr_to_tail_call_list (gdbarch, addr);
00670 
00671       for (call_site = TYPE_TAIL_CALL_LIST (SYMBOL_TYPE (func_sym));
00672            call_site; call_site = call_site->tail_call_next)
00673         {
00674           CORE_ADDR target_addr;
00675           void **slot;
00676 
00677           /* CALLER_FRAME with registers is not available for tail-call jumped
00678              frames.  */
00679           target_addr = call_site_to_target_addr (gdbarch, call_site, NULL);
00680 
00681           if (target_addr == verify_addr)
00682             {
00683               struct bound_minimal_symbol msym;
00684               
00685               msym = lookup_minimal_symbol_by_pc (verify_addr);
00686               throw_error (NO_ENTRY_VALUE_ERROR,
00687                            _("DW_OP_GNU_entry_value resolving has found "
00688                              "function \"%s\" at %s can call itself via tail "
00689                              "calls"),
00690                            (msym.minsym == NULL ? "???"
00691                             : SYMBOL_PRINT_NAME (msym.minsym)),
00692                            paddress (gdbarch, verify_addr));
00693             }
00694 
00695           slot = htab_find_slot (addr_hash, &target_addr, INSERT);
00696           if (*slot == NULL)
00697             {
00698               *slot = obstack_copy (&addr_obstack, &target_addr,
00699                                     sizeof (target_addr));
00700               VEC_safe_push (CORE_ADDR, todo, target_addr);
00701             }
00702         }
00703     }
00704 
00705   do_cleanups (old_chain);
00706 }
00707 
00708 /* Print user readable form of CALL_SITE->PC to gdb_stdlog.  Used only for
00709    ENTRY_VALUES_DEBUG.  */
00710 
00711 static void
00712 tailcall_dump (struct gdbarch *gdbarch, const struct call_site *call_site)
00713 {
00714   CORE_ADDR addr = call_site->pc;
00715   struct bound_minimal_symbol msym = lookup_minimal_symbol_by_pc (addr - 1);
00716 
00717   fprintf_unfiltered (gdb_stdlog, " %s(%s)", paddress (gdbarch, addr),
00718                       (msym.minsym == NULL ? "???"
00719                        : SYMBOL_PRINT_NAME (msym.minsym)));
00720 
00721 }
00722 
00723 /* vec.h needs single word type name, typedef it.  */
00724 typedef struct call_site *call_sitep;
00725 
00726 /* Define VEC (call_sitep) functions.  */
00727 DEF_VEC_P (call_sitep);
00728 
00729 /* Intersect RESULTP with CHAIN to keep RESULTP unambiguous, keep in RESULTP
00730    only top callers and bottom callees which are present in both.  GDBARCH is
00731    used only for ENTRY_VALUES_DEBUG.  RESULTP is NULL after return if there are
00732    no remaining possibilities to provide unambiguous non-trivial result.
00733    RESULTP should point to NULL on the first (initialization) call.  Caller is
00734    responsible for xfree of any RESULTP data.  */
00735 
00736 static void
00737 chain_candidate (struct gdbarch *gdbarch, struct call_site_chain **resultp,
00738                  VEC (call_sitep) *chain)
00739 {
00740   struct call_site_chain *result = *resultp;
00741   long length = VEC_length (call_sitep, chain);
00742   int callers, callees, idx;
00743 
00744   if (result == NULL)
00745     {
00746       /* Create the initial chain containing all the passed PCs.  */
00747 
00748       result = xmalloc (sizeof (*result) + sizeof (*result->call_site)
00749                                            * (length - 1));
00750       result->length = length;
00751       result->callers = result->callees = length;
00752       memcpy (result->call_site, VEC_address (call_sitep, chain),
00753               sizeof (*result->call_site) * length);
00754       *resultp = result;
00755 
00756       if (entry_values_debug)
00757         {
00758           fprintf_unfiltered (gdb_stdlog, "tailcall: initial:");
00759           for (idx = 0; idx < length; idx++)
00760             tailcall_dump (gdbarch, result->call_site[idx]);
00761           fputc_unfiltered ('\n', gdb_stdlog);
00762         }
00763 
00764       return;
00765     }
00766 
00767   if (entry_values_debug)
00768     {
00769       fprintf_unfiltered (gdb_stdlog, "tailcall: compare:");
00770       for (idx = 0; idx < length; idx++)
00771         tailcall_dump (gdbarch, VEC_index (call_sitep, chain, idx));
00772       fputc_unfiltered ('\n', gdb_stdlog);
00773     }
00774 
00775   /* Intersect callers.  */
00776 
00777   callers = min (result->callers, length);
00778   for (idx = 0; idx < callers; idx++)
00779     if (result->call_site[idx] != VEC_index (call_sitep, chain, idx))
00780       {
00781         result->callers = idx;
00782         break;
00783       }
00784 
00785   /* Intersect callees.  */
00786 
00787   callees = min (result->callees, length);
00788   for (idx = 0; idx < callees; idx++)
00789     if (result->call_site[result->length - 1 - idx]
00790         != VEC_index (call_sitep, chain, length - 1 - idx))
00791       {
00792         result->callees = idx;
00793         break;
00794       }
00795 
00796   if (entry_values_debug)
00797     {
00798       fprintf_unfiltered (gdb_stdlog, "tailcall: reduced:");
00799       for (idx = 0; idx < result->callers; idx++)
00800         tailcall_dump (gdbarch, result->call_site[idx]);
00801       fputs_unfiltered (" |", gdb_stdlog);
00802       for (idx = 0; idx < result->callees; idx++)
00803         tailcall_dump (gdbarch, result->call_site[result->length
00804                                                   - result->callees + idx]);
00805       fputc_unfiltered ('\n', gdb_stdlog);
00806     }
00807 
00808   if (result->callers == 0 && result->callees == 0)
00809     {
00810       /* There are no common callers or callees.  It could be also a direct
00811          call (which has length 0) with ambiguous possibility of an indirect
00812          call - CALLERS == CALLEES == 0 is valid during the first allocation
00813          but any subsequence processing of such entry means ambiguity.  */
00814       xfree (result);
00815       *resultp = NULL;
00816       return;
00817     }
00818 
00819   /* See call_site_find_chain_1 why there is no way to reach the bottom callee
00820      PC again.  In such case there must be two different code paths to reach
00821      it, therefore some of the former determined intermediate PCs must differ
00822      and the unambiguous chain gets shortened.  */
00823   gdb_assert (result->callers + result->callees < result->length);
00824 }
00825 
00826 /* Create and return call_site_chain for CALLER_PC and CALLEE_PC.  All the
00827    assumed frames between them use GDBARCH.  Use depth first search so we can
00828    keep single CHAIN of call_site's back to CALLER_PC.  Function recursion
00829    would have needless GDB stack overhead.  Caller is responsible for xfree of
00830    the returned result.  Any unreliability results in thrown
00831    NO_ENTRY_VALUE_ERROR.  */
00832 
00833 static struct call_site_chain *
00834 call_site_find_chain_1 (struct gdbarch *gdbarch, CORE_ADDR caller_pc,
00835                         CORE_ADDR callee_pc)
00836 {
00837   CORE_ADDR save_callee_pc = callee_pc;
00838   struct obstack addr_obstack;
00839   struct cleanup *back_to_retval, *back_to_workdata;
00840   struct call_site_chain *retval = NULL;
00841   struct call_site *call_site;
00842 
00843   /* Mark CALL_SITEs so we do not visit the same ones twice.  */
00844   htab_t addr_hash;
00845 
00846   /* CHAIN contains only the intermediate CALL_SITEs.  Neither CALLER_PC's
00847      call_site nor any possible call_site at CALLEE_PC's function is there.
00848      Any CALL_SITE in CHAIN will be iterated to its siblings - via
00849      TAIL_CALL_NEXT.  This is inappropriate for CALLER_PC's call_site.  */
00850   VEC (call_sitep) *chain = NULL;
00851 
00852   /* We are not interested in the specific PC inside the callee function.  */
00853   callee_pc = get_pc_function_start (callee_pc);
00854   if (callee_pc == 0)
00855     throw_error (NO_ENTRY_VALUE_ERROR, _("Unable to find function for PC %s"),
00856                  paddress (gdbarch, save_callee_pc));
00857 
00858   back_to_retval = make_cleanup (free_current_contents, &retval);
00859 
00860   obstack_init (&addr_obstack);
00861   back_to_workdata = make_cleanup_obstack_free (&addr_obstack);   
00862   addr_hash = htab_create_alloc_ex (64, core_addr_hash, core_addr_eq, NULL,
00863                                     &addr_obstack, hashtab_obstack_allocate,
00864                                     NULL);
00865   make_cleanup_htab_delete (addr_hash);
00866 
00867   make_cleanup (VEC_cleanup (call_sitep), &chain);
00868 
00869   /* Do not push CALL_SITE to CHAIN.  Push there only the first tail call site
00870      at the target's function.  All the possible tail call sites in the
00871      target's function will get iterated as already pushed into CHAIN via their
00872      TAIL_CALL_NEXT.  */
00873   call_site = call_site_for_pc (gdbarch, caller_pc);
00874 
00875   while (call_site)
00876     {
00877       CORE_ADDR target_func_addr;
00878       struct call_site *target_call_site;
00879 
00880       /* CALLER_FRAME with registers is not available for tail-call jumped
00881          frames.  */
00882       target_func_addr = call_site_to_target_addr (gdbarch, call_site, NULL);
00883 
00884       if (target_func_addr == callee_pc)
00885         {
00886           chain_candidate (gdbarch, &retval, chain);
00887           if (retval == NULL)
00888             break;
00889 
00890           /* There is no way to reach CALLEE_PC again as we would prevent
00891              entering it twice as being already marked in ADDR_HASH.  */
00892           target_call_site = NULL;
00893         }
00894       else
00895         {
00896           struct symbol *target_func;
00897 
00898           target_func = func_addr_to_tail_call_list (gdbarch, target_func_addr);
00899           target_call_site = TYPE_TAIL_CALL_LIST (SYMBOL_TYPE (target_func));
00900         }
00901 
00902       do
00903         {
00904           /* Attempt to visit TARGET_CALL_SITE.  */
00905 
00906           if (target_call_site)
00907             {
00908               void **slot;
00909 
00910               slot = htab_find_slot (addr_hash, &target_call_site->pc, INSERT);
00911               if (*slot == NULL)
00912                 {
00913                   /* Successfully entered TARGET_CALL_SITE.  */
00914 
00915                   *slot = &target_call_site->pc;
00916                   VEC_safe_push (call_sitep, chain, target_call_site);
00917                   break;
00918                 }
00919             }
00920 
00921           /* Backtrack (without revisiting the originating call_site).  Try the
00922              callers's sibling; if there isn't any try the callers's callers's
00923              sibling etc.  */
00924 
00925           target_call_site = NULL;
00926           while (!VEC_empty (call_sitep, chain))
00927             {
00928               call_site = VEC_pop (call_sitep, chain);
00929 
00930               gdb_assert (htab_find_slot (addr_hash, &call_site->pc,
00931                                           NO_INSERT) != NULL);
00932               htab_remove_elt (addr_hash, &call_site->pc);
00933 
00934               target_call_site = call_site->tail_call_next;
00935               if (target_call_site)
00936                 break;
00937             }
00938         }
00939       while (target_call_site);
00940 
00941       if (VEC_empty (call_sitep, chain))
00942         call_site = NULL;
00943       else
00944         call_site = VEC_last (call_sitep, chain);
00945     }
00946 
00947   if (retval == NULL)
00948     {
00949       struct bound_minimal_symbol msym_caller, msym_callee;
00950       
00951       msym_caller = lookup_minimal_symbol_by_pc (caller_pc);
00952       msym_callee = lookup_minimal_symbol_by_pc (callee_pc);
00953       throw_error (NO_ENTRY_VALUE_ERROR,
00954                    _("There are no unambiguously determinable intermediate "
00955                      "callers or callees between caller function \"%s\" at %s "
00956                      "and callee function \"%s\" at %s"),
00957                    (msym_caller.minsym == NULL
00958                     ? "???" : SYMBOL_PRINT_NAME (msym_caller.minsym)),
00959                    paddress (gdbarch, caller_pc),
00960                    (msym_callee.minsym == NULL
00961                     ? "???" : SYMBOL_PRINT_NAME (msym_callee.minsym)),
00962                    paddress (gdbarch, callee_pc));
00963     }
00964 
00965   do_cleanups (back_to_workdata);
00966   discard_cleanups (back_to_retval);
00967   return retval;
00968 }
00969 
00970 /* Create and return call_site_chain for CALLER_PC and CALLEE_PC.  All the
00971    assumed frames between them use GDBARCH.  If valid call_site_chain cannot be
00972    constructed return NULL.  Caller is responsible for xfree of the returned
00973    result.  */
00974 
00975 struct call_site_chain *
00976 call_site_find_chain (struct gdbarch *gdbarch, CORE_ADDR caller_pc,
00977                       CORE_ADDR callee_pc)
00978 {
00979   volatile struct gdb_exception e;
00980   struct call_site_chain *retval = NULL;
00981 
00982   TRY_CATCH (e, RETURN_MASK_ERROR)
00983     {
00984       retval = call_site_find_chain_1 (gdbarch, caller_pc, callee_pc);
00985     }
00986   if (e.reason < 0)
00987     {
00988       if (e.error == NO_ENTRY_VALUE_ERROR)
00989         {
00990           if (entry_values_debug)
00991             exception_print (gdb_stdout, e);
00992 
00993           return NULL;
00994         }
00995       else
00996         throw_exception (e);
00997     }
00998   return retval;
00999 }
01000 
01001 /* Return 1 if KIND and KIND_U match PARAMETER.  Return 0 otherwise.  */
01002 
01003 static int
01004 call_site_parameter_matches (struct call_site_parameter *parameter,
01005                              enum call_site_parameter_kind kind,
01006                              union call_site_parameter_u kind_u)
01007 {
01008   if (kind == parameter->kind)
01009     switch (kind)
01010       {
01011       case CALL_SITE_PARAMETER_DWARF_REG:
01012         return kind_u.dwarf_reg == parameter->u.dwarf_reg;
01013       case CALL_SITE_PARAMETER_FB_OFFSET:
01014         return kind_u.fb_offset == parameter->u.fb_offset;
01015       case CALL_SITE_PARAMETER_PARAM_OFFSET:
01016         return kind_u.param_offset.cu_off == parameter->u.param_offset.cu_off;
01017       }
01018   return 0;
01019 }
01020 
01021 /* Fetch call_site_parameter from caller matching KIND and KIND_U.
01022    FRAME is for callee.
01023 
01024    Function always returns non-NULL, it throws NO_ENTRY_VALUE_ERROR
01025    otherwise.  */
01026 
01027 static struct call_site_parameter *
01028 dwarf_expr_reg_to_entry_parameter (struct frame_info *frame,
01029                                    enum call_site_parameter_kind kind,
01030                                    union call_site_parameter_u kind_u,
01031                                    struct dwarf2_per_cu_data **per_cu_return)
01032 {
01033   CORE_ADDR func_addr, caller_pc;
01034   struct gdbarch *gdbarch;
01035   struct frame_info *caller_frame;
01036   struct call_site *call_site;
01037   int iparams;
01038   /* Initialize it just to avoid a GCC false warning.  */
01039   struct call_site_parameter *parameter = NULL;
01040   CORE_ADDR target_addr;
01041 
01042   while (get_frame_type (frame) == INLINE_FRAME)
01043     {
01044       frame = get_prev_frame (frame);
01045       gdb_assert (frame != NULL);
01046     }
01047 
01048   func_addr = get_frame_func (frame);
01049   gdbarch = get_frame_arch (frame);
01050   caller_frame = get_prev_frame (frame);
01051   if (gdbarch != frame_unwind_arch (frame))
01052     {
01053       struct bound_minimal_symbol msym
01054         = lookup_minimal_symbol_by_pc (func_addr);
01055       struct gdbarch *caller_gdbarch = frame_unwind_arch (frame);
01056 
01057       throw_error (NO_ENTRY_VALUE_ERROR,
01058                    _("DW_OP_GNU_entry_value resolving callee gdbarch %s "
01059                      "(of %s (%s)) does not match caller gdbarch %s"),
01060                    gdbarch_bfd_arch_info (gdbarch)->printable_name,
01061                    paddress (gdbarch, func_addr),
01062                    (msym.minsym == NULL ? "???"
01063                     : SYMBOL_PRINT_NAME (msym.minsym)),
01064                    gdbarch_bfd_arch_info (caller_gdbarch)->printable_name);
01065     }
01066 
01067   if (caller_frame == NULL)
01068     {
01069       struct bound_minimal_symbol msym
01070         = lookup_minimal_symbol_by_pc (func_addr);
01071 
01072       throw_error (NO_ENTRY_VALUE_ERROR, _("DW_OP_GNU_entry_value resolving "
01073                                            "requires caller of %s (%s)"),
01074                    paddress (gdbarch, func_addr),
01075                    (msym.minsym == NULL ? "???"
01076                     : SYMBOL_PRINT_NAME (msym.minsym)));
01077     }
01078   caller_pc = get_frame_pc (caller_frame);
01079   call_site = call_site_for_pc (gdbarch, caller_pc);
01080 
01081   target_addr = call_site_to_target_addr (gdbarch, call_site, caller_frame);
01082   if (target_addr != func_addr)
01083     {
01084       struct minimal_symbol *target_msym, *func_msym;
01085 
01086       target_msym = lookup_minimal_symbol_by_pc (target_addr).minsym;
01087       func_msym = lookup_minimal_symbol_by_pc (func_addr).minsym;
01088       throw_error (NO_ENTRY_VALUE_ERROR,
01089                    _("DW_OP_GNU_entry_value resolving expects callee %s at %s "
01090                      "but the called frame is for %s at %s"),
01091                    (target_msym == NULL ? "???"
01092                                         : SYMBOL_PRINT_NAME (target_msym)),
01093                    paddress (gdbarch, target_addr),
01094                    func_msym == NULL ? "???" : SYMBOL_PRINT_NAME (func_msym),
01095                    paddress (gdbarch, func_addr));
01096     }
01097 
01098   /* No entry value based parameters would be reliable if this function can
01099      call itself via tail calls.  */
01100   func_verify_no_selftailcall (gdbarch, func_addr);
01101 
01102   for (iparams = 0; iparams < call_site->parameter_count; iparams++)
01103     {
01104       parameter = &call_site->parameter[iparams];
01105       if (call_site_parameter_matches (parameter, kind, kind_u))
01106         break;
01107     }
01108   if (iparams == call_site->parameter_count)
01109     {
01110       struct minimal_symbol *msym
01111         = lookup_minimal_symbol_by_pc (caller_pc).minsym;
01112 
01113       /* DW_TAG_GNU_call_site_parameter will be missing just if GCC could not
01114          determine its value.  */
01115       throw_error (NO_ENTRY_VALUE_ERROR, _("Cannot find matching parameter "
01116                                            "at DW_TAG_GNU_call_site %s at %s"),
01117                    paddress (gdbarch, caller_pc),
01118                    msym == NULL ? "???" : SYMBOL_PRINT_NAME (msym)); 
01119     }
01120 
01121   *per_cu_return = call_site->per_cu;
01122   return parameter;
01123 }
01124 
01125 /* Return value for PARAMETER matching DEREF_SIZE.  If DEREF_SIZE is -1, return
01126    the normal DW_AT_GNU_call_site_value block.  Otherwise return the
01127    DW_AT_GNU_call_site_data_value (dereferenced) block.
01128 
01129    TYPE and CALLER_FRAME specify how to evaluate the DWARF block into returned
01130    struct value.
01131 
01132    Function always returns non-NULL, non-optimized out value.  It throws
01133    NO_ENTRY_VALUE_ERROR if it cannot resolve the value for any reason.  */
01134 
01135 static struct value *
01136 dwarf_entry_parameter_to_value (struct call_site_parameter *parameter,
01137                                 CORE_ADDR deref_size, struct type *type,
01138                                 struct frame_info *caller_frame,
01139                                 struct dwarf2_per_cu_data *per_cu)
01140 {
01141   const gdb_byte *data_src;
01142   gdb_byte *data;
01143   size_t size;
01144 
01145   data_src = deref_size == -1 ? parameter->value : parameter->data_value;
01146   size = deref_size == -1 ? parameter->value_size : parameter->data_value_size;
01147 
01148   /* DEREF_SIZE size is not verified here.  */
01149   if (data_src == NULL)
01150     throw_error (NO_ENTRY_VALUE_ERROR,
01151                  _("Cannot resolve DW_AT_GNU_call_site_data_value"));
01152 
01153   /* DW_AT_GNU_call_site_value is a DWARF expression, not a DWARF
01154      location.  Postprocessing of DWARF_VALUE_MEMORY would lose the type from
01155      DWARF block.  */
01156   data = alloca (size + 1);
01157   memcpy (data, data_src, size);
01158   data[size] = DW_OP_stack_value;
01159 
01160   return dwarf2_evaluate_loc_desc (type, caller_frame, data, size + 1, per_cu);
01161 }
01162 
01163 /* Execute DWARF block of call_site_parameter which matches KIND and KIND_U.
01164    Choose DEREF_SIZE value of that parameter.  Search caller of the CTX's
01165    frame.  CTX must be of dwarf_expr_ctx_funcs kind.
01166 
01167    The CTX caller can be from a different CU - per_cu_dwarf_call implementation
01168    can be more simple as it does not support cross-CU DWARF executions.  */
01169 
01170 static void
01171 dwarf_expr_push_dwarf_reg_entry_value (struct dwarf_expr_context *ctx,
01172                                        enum call_site_parameter_kind kind,
01173                                        union call_site_parameter_u kind_u,
01174                                        int deref_size)
01175 {
01176   struct dwarf_expr_baton *debaton;
01177   struct frame_info *frame, *caller_frame;
01178   struct dwarf2_per_cu_data *caller_per_cu;
01179   struct dwarf_expr_baton baton_local;
01180   struct dwarf_expr_context saved_ctx;
01181   struct call_site_parameter *parameter;
01182   const gdb_byte *data_src;
01183   size_t size;
01184 
01185   gdb_assert (ctx->funcs == &dwarf_expr_ctx_funcs);
01186   debaton = ctx->baton;
01187   frame = debaton->frame;
01188   caller_frame = get_prev_frame (frame);
01189 
01190   parameter = dwarf_expr_reg_to_entry_parameter (frame, kind, kind_u,
01191                                                  &caller_per_cu);
01192   data_src = deref_size == -1 ? parameter->value : parameter->data_value;
01193   size = deref_size == -1 ? parameter->value_size : parameter->data_value_size;
01194 
01195   /* DEREF_SIZE size is not verified here.  */
01196   if (data_src == NULL)
01197     throw_error (NO_ENTRY_VALUE_ERROR,
01198                  _("Cannot resolve DW_AT_GNU_call_site_data_value"));
01199 
01200   baton_local.frame = caller_frame;
01201   baton_local.per_cu = caller_per_cu;
01202 
01203   saved_ctx.gdbarch = ctx->gdbarch;
01204   saved_ctx.addr_size = ctx->addr_size;
01205   saved_ctx.offset = ctx->offset;
01206   saved_ctx.baton = ctx->baton;
01207   ctx->gdbarch = get_objfile_arch (dwarf2_per_cu_objfile (baton_local.per_cu));
01208   ctx->addr_size = dwarf2_per_cu_addr_size (baton_local.per_cu);
01209   ctx->offset = dwarf2_per_cu_text_offset (baton_local.per_cu);
01210   ctx->baton = &baton_local;
01211 
01212   dwarf_expr_eval (ctx, data_src, size);
01213 
01214   ctx->gdbarch = saved_ctx.gdbarch;
01215   ctx->addr_size = saved_ctx.addr_size;
01216   ctx->offset = saved_ctx.offset;
01217   ctx->baton = saved_ctx.baton;
01218 }
01219 
01220 /* Callback function for dwarf2_evaluate_loc_desc.
01221    Fetch the address indexed by DW_OP_GNU_addr_index.  */
01222 
01223 static CORE_ADDR
01224 dwarf_expr_get_addr_index (void *baton, unsigned int index)
01225 {
01226   struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
01227 
01228   return dwarf2_read_addr_index (debaton->per_cu, index);
01229 }
01230 
01231 /* VALUE must be of type lval_computed with entry_data_value_funcs.  Perform
01232    the indirect method on it, that is use its stored target value, the sole
01233    purpose of entry_data_value_funcs..  */
01234 
01235 static struct value *
01236 entry_data_value_coerce_ref (const struct value *value)
01237 {
01238   struct type *checked_type = check_typedef (value_type (value));
01239   struct value *target_val;
01240 
01241   if (TYPE_CODE (checked_type) != TYPE_CODE_REF)
01242     return NULL;
01243 
01244   target_val = value_computed_closure (value);
01245   value_incref (target_val);
01246   return target_val;
01247 }
01248 
01249 /* Implement copy_closure.  */
01250 
01251 static void *
01252 entry_data_value_copy_closure (const struct value *v)
01253 {
01254   struct value *target_val = value_computed_closure (v);
01255 
01256   value_incref (target_val);
01257   return target_val;
01258 }
01259 
01260 /* Implement free_closure.  */
01261 
01262 static void
01263 entry_data_value_free_closure (struct value *v)
01264 {
01265   struct value *target_val = value_computed_closure (v);
01266 
01267   value_free (target_val);
01268 }
01269 
01270 /* Vector for methods for an entry value reference where the referenced value
01271    is stored in the caller.  On the first dereference use
01272    DW_AT_GNU_call_site_data_value in the caller.  */
01273 
01274 static const struct lval_funcs entry_data_value_funcs =
01275 {
01276   NULL, /* read */
01277   NULL, /* write */
01278   NULL, /* check_validity */
01279   NULL, /* check_any_valid */
01280   NULL, /* indirect */
01281   entry_data_value_coerce_ref,
01282   NULL, /* check_synthetic_pointer */
01283   entry_data_value_copy_closure,
01284   entry_data_value_free_closure
01285 };
01286 
01287 /* Read parameter of TYPE at (callee) FRAME's function entry.  KIND and KIND_U
01288    are used to match DW_AT_location at the caller's
01289    DW_TAG_GNU_call_site_parameter.
01290 
01291    Function always returns non-NULL value.  It throws NO_ENTRY_VALUE_ERROR if it
01292    cannot resolve the parameter for any reason.  */
01293 
01294 static struct value *
01295 value_of_dwarf_reg_entry (struct type *type, struct frame_info *frame,
01296                           enum call_site_parameter_kind kind,
01297                           union call_site_parameter_u kind_u)
01298 {
01299   struct type *checked_type = check_typedef (type);
01300   struct type *target_type = TYPE_TARGET_TYPE (checked_type);
01301   struct frame_info *caller_frame = get_prev_frame (frame);
01302   struct value *outer_val, *target_val, *val;
01303   struct call_site_parameter *parameter;
01304   struct dwarf2_per_cu_data *caller_per_cu;
01305   CORE_ADDR addr;
01306 
01307   parameter = dwarf_expr_reg_to_entry_parameter (frame, kind, kind_u,
01308                                                  &caller_per_cu);
01309 
01310   outer_val = dwarf_entry_parameter_to_value (parameter, -1 /* deref_size */,
01311                                               type, caller_frame,
01312                                               caller_per_cu);
01313 
01314   /* Check if DW_AT_GNU_call_site_data_value cannot be used.  If it should be
01315      used and it is not available do not fall back to OUTER_VAL - dereferencing
01316      TYPE_CODE_REF with non-entry data value would give current value - not the
01317      entry value.  */
01318 
01319   if (TYPE_CODE (checked_type) != TYPE_CODE_REF
01320       || TYPE_TARGET_TYPE (checked_type) == NULL)
01321     return outer_val;
01322 
01323   target_val = dwarf_entry_parameter_to_value (parameter,
01324                                                TYPE_LENGTH (target_type),
01325                                                target_type, caller_frame,
01326                                                caller_per_cu);
01327 
01328   /* value_as_address dereferences TYPE_CODE_REF.  */
01329   addr = extract_typed_address (value_contents (outer_val), checked_type);
01330 
01331   /* The target entry value has artificial address of the entry value
01332      reference.  */
01333   VALUE_LVAL (target_val) = lval_memory;
01334   set_value_address (target_val, addr);
01335 
01336   release_value (target_val);
01337   val = allocate_computed_value (type, &entry_data_value_funcs,
01338                                  target_val /* closure */);
01339 
01340   /* Copy the referencing pointer to the new computed value.  */
01341   memcpy (value_contents_raw (val), value_contents_raw (outer_val),
01342           TYPE_LENGTH (checked_type));
01343   set_value_lazy (val, 0);
01344 
01345   return val;
01346 }
01347 
01348 /* Read parameter of TYPE at (callee) FRAME's function entry.  DATA and
01349    SIZE are DWARF block used to match DW_AT_location at the caller's
01350    DW_TAG_GNU_call_site_parameter.
01351 
01352    Function always returns non-NULL value.  It throws NO_ENTRY_VALUE_ERROR if it
01353    cannot resolve the parameter for any reason.  */
01354 
01355 static struct value *
01356 value_of_dwarf_block_entry (struct type *type, struct frame_info *frame,
01357                             const gdb_byte *block, size_t block_len)
01358 {
01359   union call_site_parameter_u kind_u;
01360 
01361   kind_u.dwarf_reg = dwarf_block_to_dwarf_reg (block, block + block_len);
01362   if (kind_u.dwarf_reg != -1)
01363     return value_of_dwarf_reg_entry (type, frame, CALL_SITE_PARAMETER_DWARF_REG,
01364                                      kind_u);
01365 
01366   if (dwarf_block_to_fb_offset (block, block + block_len, &kind_u.fb_offset))
01367     return value_of_dwarf_reg_entry (type, frame, CALL_SITE_PARAMETER_FB_OFFSET,
01368                                      kind_u);
01369 
01370   /* This can normally happen - throw NO_ENTRY_VALUE_ERROR to get the message
01371      suppressed during normal operation.  The expression can be arbitrary if
01372      there is no caller-callee entry value binding expected.  */
01373   throw_error (NO_ENTRY_VALUE_ERROR,
01374                _("DWARF-2 expression error: DW_OP_GNU_entry_value is supported "
01375                  "only for single DW_OP_reg* or for DW_OP_fbreg(*)"));
01376 }
01377 
01378 struct piece_closure
01379 {
01380   /* Reference count.  */
01381   int refc;
01382 
01383   /* The CU from which this closure's expression came.  */
01384   struct dwarf2_per_cu_data *per_cu;
01385 
01386   /* The number of pieces used to describe this variable.  */
01387   int n_pieces;
01388 
01389   /* The target address size, used only for DWARF_VALUE_STACK.  */
01390   int addr_size;
01391 
01392   /* The pieces themselves.  */
01393   struct dwarf_expr_piece *pieces;
01394 };
01395 
01396 /* Allocate a closure for a value formed from separately-described
01397    PIECES.  */
01398 
01399 static struct piece_closure *
01400 allocate_piece_closure (struct dwarf2_per_cu_data *per_cu,
01401                         int n_pieces, struct dwarf_expr_piece *pieces,
01402                         int addr_size)
01403 {
01404   struct piece_closure *c = XZALLOC (struct piece_closure);
01405   int i;
01406 
01407   c->refc = 1;
01408   c->per_cu = per_cu;
01409   c->n_pieces = n_pieces;
01410   c->addr_size = addr_size;
01411   c->pieces = XCALLOC (n_pieces, struct dwarf_expr_piece);
01412 
01413   memcpy (c->pieces, pieces, n_pieces * sizeof (struct dwarf_expr_piece));
01414   for (i = 0; i < n_pieces; ++i)
01415     if (c->pieces[i].location == DWARF_VALUE_STACK)
01416       value_incref (c->pieces[i].v.value);
01417 
01418   return c;
01419 }
01420 
01421 /* The lowest-level function to extract bits from a byte buffer.
01422    SOURCE is the buffer.  It is updated if we read to the end of a
01423    byte.
01424    SOURCE_OFFSET_BITS is the offset of the first bit to read.  It is
01425    updated to reflect the number of bits actually read.
01426    NBITS is the number of bits we want to read.  It is updated to
01427    reflect the number of bits actually read.  This function may read
01428    fewer bits.
01429    BITS_BIG_ENDIAN is taken directly from gdbarch.
01430    This function returns the extracted bits.  */
01431 
01432 static unsigned int
01433 extract_bits_primitive (const gdb_byte **source,
01434                         unsigned int *source_offset_bits,
01435                         int *nbits, int bits_big_endian)
01436 {
01437   unsigned int avail, mask, datum;
01438 
01439   gdb_assert (*source_offset_bits < 8);
01440 
01441   avail = 8 - *source_offset_bits;
01442   if (avail > *nbits)
01443     avail = *nbits;
01444 
01445   mask = (1 << avail) - 1;
01446   datum = **source;
01447   if (bits_big_endian)
01448     datum >>= 8 - (*source_offset_bits + *nbits);
01449   else
01450     datum >>= *source_offset_bits;
01451   datum &= mask;
01452 
01453   *nbits -= avail;
01454   *source_offset_bits += avail;
01455   if (*source_offset_bits >= 8)
01456     {
01457       *source_offset_bits -= 8;
01458       ++*source;
01459     }
01460 
01461   return datum;
01462 }
01463 
01464 /* Extract some bits from a source buffer and move forward in the
01465    buffer.
01466    
01467    SOURCE is the source buffer.  It is updated as bytes are read.
01468    SOURCE_OFFSET_BITS is the offset into SOURCE.  It is updated as
01469    bits are read.
01470    NBITS is the number of bits to read.
01471    BITS_BIG_ENDIAN is taken directly from gdbarch.
01472    
01473    This function returns the bits that were read.  */
01474 
01475 static unsigned int
01476 extract_bits (const gdb_byte **source, unsigned int *source_offset_bits,
01477               int nbits, int bits_big_endian)
01478 {
01479   unsigned int datum;
01480 
01481   gdb_assert (nbits > 0 && nbits <= 8);
01482 
01483   datum = extract_bits_primitive (source, source_offset_bits, &nbits,
01484                                   bits_big_endian);
01485   if (nbits > 0)
01486     {
01487       unsigned int more;
01488 
01489       more = extract_bits_primitive (source, source_offset_bits, &nbits,
01490                                      bits_big_endian);
01491       if (bits_big_endian)
01492         datum <<= nbits;
01493       else
01494         more <<= nbits;
01495       datum |= more;
01496     }
01497 
01498   return datum;
01499 }
01500 
01501 /* Write some bits into a buffer and move forward in the buffer.
01502    
01503    DATUM is the bits to write.  The low-order bits of DATUM are used.
01504    DEST is the destination buffer.  It is updated as bytes are
01505    written.
01506    DEST_OFFSET_BITS is the bit offset in DEST at which writing is
01507    done.
01508    NBITS is the number of valid bits in DATUM.
01509    BITS_BIG_ENDIAN is taken directly from gdbarch.  */
01510 
01511 static void
01512 insert_bits (unsigned int datum,
01513              gdb_byte *dest, unsigned int dest_offset_bits,
01514              int nbits, int bits_big_endian)
01515 {
01516   unsigned int mask;
01517 
01518   gdb_assert (dest_offset_bits + nbits <= 8);
01519 
01520   mask = (1 << nbits) - 1;
01521   if (bits_big_endian)
01522     {
01523       datum <<= 8 - (dest_offset_bits + nbits);
01524       mask <<= 8 - (dest_offset_bits + nbits);
01525     }
01526   else
01527     {
01528       datum <<= dest_offset_bits;
01529       mask <<= dest_offset_bits;
01530     }
01531 
01532   gdb_assert ((datum & ~mask) == 0);
01533 
01534   *dest = (*dest & ~mask) | datum;
01535 }
01536 
01537 /* Copy bits from a source to a destination.
01538    
01539    DEST is where the bits should be written.
01540    DEST_OFFSET_BITS is the bit offset into DEST.
01541    SOURCE is the source of bits.
01542    SOURCE_OFFSET_BITS is the bit offset into SOURCE.
01543    BIT_COUNT is the number of bits to copy.
01544    BITS_BIG_ENDIAN is taken directly from gdbarch.  */
01545 
01546 static void
01547 copy_bitwise (gdb_byte *dest, unsigned int dest_offset_bits,
01548               const gdb_byte *source, unsigned int source_offset_bits,
01549               unsigned int bit_count,
01550               int bits_big_endian)
01551 {
01552   unsigned int dest_avail;
01553   int datum;
01554 
01555   /* Reduce everything to byte-size pieces.  */
01556   dest += dest_offset_bits / 8;
01557   dest_offset_bits %= 8;
01558   source += source_offset_bits / 8;
01559   source_offset_bits %= 8;
01560 
01561   dest_avail = 8 - dest_offset_bits % 8;
01562 
01563   /* See if we can fill the first destination byte.  */
01564   if (dest_avail < bit_count)
01565     {
01566       datum = extract_bits (&source, &source_offset_bits, dest_avail,
01567                             bits_big_endian);
01568       insert_bits (datum, dest, dest_offset_bits, dest_avail, bits_big_endian);
01569       ++dest;
01570       dest_offset_bits = 0;
01571       bit_count -= dest_avail;
01572     }
01573 
01574   /* Now, either DEST_OFFSET_BITS is byte-aligned, or we have fewer
01575      than 8 bits remaining.  */
01576   gdb_assert (dest_offset_bits % 8 == 0 || bit_count < 8);
01577   for (; bit_count >= 8; bit_count -= 8)
01578     {
01579       datum = extract_bits (&source, &source_offset_bits, 8, bits_big_endian);
01580       *dest++ = (gdb_byte) datum;
01581     }
01582 
01583   /* Finally, we may have a few leftover bits.  */
01584   gdb_assert (bit_count <= 8 - dest_offset_bits % 8);
01585   if (bit_count > 0)
01586     {
01587       datum = extract_bits (&source, &source_offset_bits, bit_count,
01588                             bits_big_endian);
01589       insert_bits (datum, dest, dest_offset_bits, bit_count, bits_big_endian);
01590     }
01591 }
01592 
01593 static void
01594 read_pieced_value (struct value *v)
01595 {
01596   int i;
01597   long offset = 0;
01598   ULONGEST bits_to_skip;
01599   gdb_byte *contents;
01600   struct piece_closure *c
01601     = (struct piece_closure *) value_computed_closure (v);
01602   struct frame_info *frame = frame_find_by_id (VALUE_FRAME_ID (v));
01603   size_t type_len;
01604   size_t buffer_size = 0;
01605   gdb_byte *buffer = NULL;
01606   struct cleanup *cleanup;
01607   int bits_big_endian
01608     = gdbarch_bits_big_endian (get_type_arch (value_type (v)));
01609 
01610   if (value_type (v) != value_enclosing_type (v))
01611     internal_error (__FILE__, __LINE__,
01612                     _("Should not be able to create a lazy value with "
01613                       "an enclosing type"));
01614 
01615   cleanup = make_cleanup (free_current_contents, &buffer);
01616 
01617   contents = value_contents_raw (v);
01618   bits_to_skip = 8 * value_offset (v);
01619   if (value_bitsize (v))
01620     {
01621       bits_to_skip += value_bitpos (v);
01622       type_len = value_bitsize (v);
01623     }
01624   else
01625     type_len = 8 * TYPE_LENGTH (value_type (v));
01626 
01627   for (i = 0; i < c->n_pieces && offset < type_len; i++)
01628     {
01629       struct dwarf_expr_piece *p = &c->pieces[i];
01630       size_t this_size, this_size_bits;
01631       long dest_offset_bits, source_offset_bits, source_offset;
01632       const gdb_byte *intermediate_buffer;
01633 
01634       /* Compute size, source, and destination offsets for copying, in
01635          bits.  */
01636       this_size_bits = p->size;
01637       if (bits_to_skip > 0 && bits_to_skip >= this_size_bits)
01638         {
01639           bits_to_skip -= this_size_bits;
01640           continue;
01641         }
01642       if (bits_to_skip > 0)
01643         {
01644           dest_offset_bits = 0;
01645           source_offset_bits = bits_to_skip;
01646           this_size_bits -= bits_to_skip;
01647           bits_to_skip = 0;
01648         }
01649       else
01650         {
01651           dest_offset_bits = offset;
01652           source_offset_bits = 0;
01653         }
01654       if (this_size_bits > type_len - offset)
01655         this_size_bits = type_len - offset;
01656 
01657       this_size = (this_size_bits + source_offset_bits % 8 + 7) / 8;
01658       source_offset = source_offset_bits / 8;
01659       if (buffer_size < this_size)
01660         {
01661           buffer_size = this_size;
01662           buffer = xrealloc (buffer, buffer_size);
01663         }
01664       intermediate_buffer = buffer;
01665 
01666       /* Copy from the source to DEST_BUFFER.  */
01667       switch (p->location)
01668         {
01669         case DWARF_VALUE_REGISTER:
01670           {
01671             struct gdbarch *arch = get_frame_arch (frame);
01672             int gdb_regnum = gdbarch_dwarf2_reg_to_regnum (arch, p->v.regno);
01673             int reg_offset = source_offset;
01674 
01675             if (gdbarch_byte_order (arch) == BFD_ENDIAN_BIG
01676                 && this_size < register_size (arch, gdb_regnum))
01677               {
01678                 /* Big-endian, and we want less than full size.  */
01679                 reg_offset = register_size (arch, gdb_regnum) - this_size;
01680                 /* We want the lower-order THIS_SIZE_BITS of the bytes
01681                    we extract from the register.  */
01682                 source_offset_bits += 8 * this_size - this_size_bits;
01683               }
01684 
01685             if (gdb_regnum != -1)
01686               {
01687                 int optim, unavail;
01688 
01689                 if (!get_frame_register_bytes (frame, gdb_regnum, reg_offset,
01690                                                this_size, buffer,
01691                                                &optim, &unavail))
01692                   {
01693                     /* Just so garbage doesn't ever shine through.  */
01694                     memset (buffer, 0, this_size);
01695 
01696                     if (optim)
01697                       set_value_optimized_out (v, 1);
01698                     if (unavail)
01699                       mark_value_bytes_unavailable (v, offset, this_size);
01700                   }
01701               }
01702             else
01703               {
01704                 error (_("Unable to access DWARF register number %s"),
01705                        paddress (arch, p->v.regno));
01706               }
01707           }
01708           break;
01709 
01710         case DWARF_VALUE_MEMORY:
01711           read_value_memory (v, offset,
01712                              p->v.mem.in_stack_memory,
01713                              p->v.mem.addr + source_offset,
01714                              buffer, this_size);
01715           break;
01716 
01717         case DWARF_VALUE_STACK:
01718           {
01719             size_t n = this_size;
01720 
01721             if (n > c->addr_size - source_offset)
01722               n = (c->addr_size >= source_offset
01723                    ? c->addr_size - source_offset
01724                    : 0);
01725             if (n == 0)
01726               {
01727                 /* Nothing.  */
01728               }
01729             else
01730               {
01731                 const gdb_byte *val_bytes = value_contents_all (p->v.value);
01732 
01733                 intermediate_buffer = val_bytes + source_offset;
01734               }
01735           }
01736           break;
01737 
01738         case DWARF_VALUE_LITERAL:
01739           {
01740             size_t n = this_size;
01741 
01742             if (n > p->v.literal.length - source_offset)
01743               n = (p->v.literal.length >= source_offset
01744                    ? p->v.literal.length - source_offset
01745                    : 0);
01746             if (n != 0)
01747               intermediate_buffer = p->v.literal.data + source_offset;
01748           }
01749           break;
01750 
01751           /* These bits show up as zeros -- but do not cause the value
01752              to be considered optimized-out.  */
01753         case DWARF_VALUE_IMPLICIT_POINTER:
01754           break;
01755 
01756         case DWARF_VALUE_OPTIMIZED_OUT:
01757           set_value_optimized_out (v, 1);
01758           break;
01759 
01760         default:
01761           internal_error (__FILE__, __LINE__, _("invalid location type"));
01762         }
01763 
01764       if (p->location != DWARF_VALUE_OPTIMIZED_OUT
01765           && p->location != DWARF_VALUE_IMPLICIT_POINTER)
01766         copy_bitwise (contents, dest_offset_bits,
01767                       intermediate_buffer, source_offset_bits % 8,
01768                       this_size_bits, bits_big_endian);
01769 
01770       offset += this_size_bits;
01771     }
01772 
01773   do_cleanups (cleanup);
01774 }
01775 
01776 static void
01777 write_pieced_value (struct value *to, struct value *from)
01778 {
01779   int i;
01780   long offset = 0;
01781   ULONGEST bits_to_skip;
01782   const gdb_byte *contents;
01783   struct piece_closure *c
01784     = (struct piece_closure *) value_computed_closure (to);
01785   struct frame_info *frame = frame_find_by_id (VALUE_FRAME_ID (to));
01786   size_t type_len;
01787   size_t buffer_size = 0;
01788   gdb_byte *buffer = NULL;
01789   struct cleanup *cleanup;
01790   int bits_big_endian
01791     = gdbarch_bits_big_endian (get_type_arch (value_type (to)));
01792 
01793   if (frame == NULL)
01794     {
01795       set_value_optimized_out (to, 1);
01796       return;
01797     }
01798 
01799   cleanup = make_cleanup (free_current_contents, &buffer);
01800 
01801   contents = value_contents (from);
01802   bits_to_skip = 8 * value_offset (to);
01803   if (value_bitsize (to))
01804     {
01805       bits_to_skip += value_bitpos (to);
01806       type_len = value_bitsize (to);
01807     }
01808   else
01809     type_len = 8 * TYPE_LENGTH (value_type (to));
01810 
01811   for (i = 0; i < c->n_pieces && offset < type_len; i++)
01812     {
01813       struct dwarf_expr_piece *p = &c->pieces[i];
01814       size_t this_size_bits, this_size;
01815       long dest_offset_bits, source_offset_bits, dest_offset, source_offset;
01816       int need_bitwise;
01817       const gdb_byte *source_buffer;
01818 
01819       this_size_bits = p->size;
01820       if (bits_to_skip > 0 && bits_to_skip >= this_size_bits)
01821         {
01822           bits_to_skip -= this_size_bits;
01823           continue;
01824         }
01825       if (this_size_bits > type_len - offset)
01826         this_size_bits = type_len - offset;
01827       if (bits_to_skip > 0)
01828         {
01829           dest_offset_bits = bits_to_skip;
01830           source_offset_bits = 0;
01831           this_size_bits -= bits_to_skip;
01832           bits_to_skip = 0;
01833         }
01834       else
01835         {
01836           dest_offset_bits = 0;
01837           source_offset_bits = offset;
01838         }
01839 
01840       this_size = (this_size_bits + source_offset_bits % 8 + 7) / 8;
01841       source_offset = source_offset_bits / 8;
01842       dest_offset = dest_offset_bits / 8;
01843       if (dest_offset_bits % 8 == 0 && source_offset_bits % 8 == 0)
01844         {
01845           source_buffer = contents + source_offset;
01846           need_bitwise = 0;
01847         }
01848       else
01849         {
01850           if (buffer_size < this_size)
01851             {
01852               buffer_size = this_size;
01853               buffer = xrealloc (buffer, buffer_size);
01854             }
01855           source_buffer = buffer;
01856           need_bitwise = 1;
01857         }
01858 
01859       switch (p->location)
01860         {
01861         case DWARF_VALUE_REGISTER:
01862           {
01863             struct gdbarch *arch = get_frame_arch (frame);
01864             int gdb_regnum = gdbarch_dwarf2_reg_to_regnum (arch, p->v.regno);
01865             int reg_offset = dest_offset;
01866 
01867             if (gdbarch_byte_order (arch) == BFD_ENDIAN_BIG
01868                 && this_size <= register_size (arch, gdb_regnum))
01869               /* Big-endian, and we want less than full size.  */
01870               reg_offset = register_size (arch, gdb_regnum) - this_size;
01871 
01872             if (gdb_regnum != -1)
01873               {
01874                 if (need_bitwise)
01875                   {
01876                     int optim, unavail;
01877 
01878                     if (!get_frame_register_bytes (frame, gdb_regnum, reg_offset,
01879                                                    this_size, buffer,
01880                                                    &optim, &unavail))
01881                       {
01882                         if (optim)
01883                           error (_("Can't do read-modify-write to "
01884                                    "update bitfield; containing word has been "
01885                                    "optimized out"));
01886                         if (unavail)
01887                           throw_error (NOT_AVAILABLE_ERROR,
01888                                        _("Can't do read-modify-write to update "
01889                                          "bitfield; containing word "
01890                                          "is unavailable"));
01891                       }
01892                     copy_bitwise (buffer, dest_offset_bits,
01893                                   contents, source_offset_bits,
01894                                   this_size_bits,
01895                                   bits_big_endian);
01896                   }
01897 
01898                 put_frame_register_bytes (frame, gdb_regnum, reg_offset, 
01899                                           this_size, source_buffer);
01900               }
01901             else
01902               {
01903                 error (_("Unable to write to DWARF register number %s"),
01904                        paddress (arch, p->v.regno));
01905               }
01906           }
01907           break;
01908         case DWARF_VALUE_MEMORY:
01909           if (need_bitwise)
01910             {
01911               /* Only the first and last bytes can possibly have any
01912                  bits reused.  */
01913               read_memory (p->v.mem.addr + dest_offset, buffer, 1);
01914               read_memory (p->v.mem.addr + dest_offset + this_size - 1,
01915                            buffer + this_size - 1, 1);
01916               copy_bitwise (buffer, dest_offset_bits,
01917                             contents, source_offset_bits,
01918                             this_size_bits,
01919                             bits_big_endian);
01920             }
01921 
01922           write_memory (p->v.mem.addr + dest_offset,
01923                         source_buffer, this_size);
01924           break;
01925         default:
01926           set_value_optimized_out (to, 1);
01927           break;
01928         }
01929       offset += this_size_bits;
01930     }
01931 
01932   do_cleanups (cleanup);
01933 }
01934 
01935 /* A helper function that checks bit validity in a pieced value.
01936    CHECK_FOR indicates the kind of validity checking.
01937    DWARF_VALUE_MEMORY means to check whether any bit is valid.
01938    DWARF_VALUE_OPTIMIZED_OUT means to check whether any bit is
01939    optimized out.
01940    DWARF_VALUE_IMPLICIT_POINTER means to check whether the bits are an
01941    implicit pointer.  */
01942 
01943 static int
01944 check_pieced_value_bits (const struct value *value, int bit_offset,
01945                          int bit_length,
01946                          enum dwarf_value_location check_for)
01947 {
01948   struct piece_closure *c
01949     = (struct piece_closure *) value_computed_closure (value);
01950   int i;
01951   int validity = (check_for == DWARF_VALUE_MEMORY
01952                   || check_for == DWARF_VALUE_IMPLICIT_POINTER);
01953 
01954   bit_offset += 8 * value_offset (value);
01955   if (value_bitsize (value))
01956     bit_offset += value_bitpos (value);
01957 
01958   for (i = 0; i < c->n_pieces && bit_length > 0; i++)
01959     {
01960       struct dwarf_expr_piece *p = &c->pieces[i];
01961       size_t this_size_bits = p->size;
01962 
01963       if (bit_offset > 0)
01964         {
01965           if (bit_offset >= this_size_bits)
01966             {
01967               bit_offset -= this_size_bits;
01968               continue;
01969             }
01970 
01971           bit_length -= this_size_bits - bit_offset;
01972           bit_offset = 0;
01973         }
01974       else
01975         bit_length -= this_size_bits;
01976 
01977       if (check_for == DWARF_VALUE_IMPLICIT_POINTER)
01978         {
01979           if (p->location != DWARF_VALUE_IMPLICIT_POINTER)
01980             return 0;
01981         }
01982       else if (p->location == DWARF_VALUE_OPTIMIZED_OUT
01983                || p->location == DWARF_VALUE_IMPLICIT_POINTER)
01984         {
01985           if (validity)
01986             return 0;
01987         }
01988       else
01989         {
01990           if (!validity)
01991             return 1;
01992         }
01993     }
01994 
01995   return validity;
01996 }
01997 
01998 static int
01999 check_pieced_value_validity (const struct value *value, int bit_offset,
02000                              int bit_length)
02001 {
02002   return check_pieced_value_bits (value, bit_offset, bit_length,
02003                                   DWARF_VALUE_MEMORY);
02004 }
02005 
02006 static int
02007 check_pieced_value_invalid (const struct value *value)
02008 {
02009   return check_pieced_value_bits (value, 0,
02010                                   8 * TYPE_LENGTH (value_type (value)),
02011                                   DWARF_VALUE_OPTIMIZED_OUT);
02012 }
02013 
02014 /* An implementation of an lval_funcs method to see whether a value is
02015    a synthetic pointer.  */
02016 
02017 static int
02018 check_pieced_synthetic_pointer (const struct value *value, int bit_offset,
02019                                 int bit_length)
02020 {
02021   return check_pieced_value_bits (value, bit_offset, bit_length,
02022                                   DWARF_VALUE_IMPLICIT_POINTER);
02023 }
02024 
02025 /* A wrapper function for get_frame_address_in_block.  */
02026 
02027 static CORE_ADDR
02028 get_frame_address_in_block_wrapper (void *baton)
02029 {
02030   return get_frame_address_in_block (baton);
02031 }
02032 
02033 /* An implementation of an lval_funcs method to indirect through a
02034    pointer.  This handles the synthetic pointer case when needed.  */
02035 
02036 static struct value *
02037 indirect_pieced_value (struct value *value)
02038 {
02039   struct piece_closure *c
02040     = (struct piece_closure *) value_computed_closure (value);
02041   struct type *type;
02042   struct frame_info *frame;
02043   struct dwarf2_locexpr_baton baton;
02044   int i, bit_offset, bit_length;
02045   struct dwarf_expr_piece *piece = NULL;
02046   LONGEST byte_offset;
02047 
02048   type = check_typedef (value_type (value));
02049   if (TYPE_CODE (type) != TYPE_CODE_PTR)
02050     return NULL;
02051 
02052   bit_length = 8 * TYPE_LENGTH (type);
02053   bit_offset = 8 * value_offset (value);
02054   if (value_bitsize (value))
02055     bit_offset += value_bitpos (value);
02056 
02057   for (i = 0; i < c->n_pieces && bit_length > 0; i++)
02058     {
02059       struct dwarf_expr_piece *p = &c->pieces[i];
02060       size_t this_size_bits = p->size;
02061 
02062       if (bit_offset > 0)
02063         {
02064           if (bit_offset >= this_size_bits)
02065             {
02066               bit_offset -= this_size_bits;
02067               continue;
02068             }
02069 
02070           bit_length -= this_size_bits - bit_offset;
02071           bit_offset = 0;
02072         }
02073       else
02074         bit_length -= this_size_bits;
02075 
02076       if (p->location != DWARF_VALUE_IMPLICIT_POINTER)
02077         return NULL;
02078 
02079       if (bit_length != 0)
02080         error (_("Invalid use of DW_OP_GNU_implicit_pointer"));
02081 
02082       piece = p;
02083       break;
02084     }
02085 
02086   frame = get_selected_frame (_("No frame selected."));
02087 
02088   /* This is an offset requested by GDB, such as value subscripts.
02089      However, due to how synthetic pointers are implemented, this is
02090      always presented to us as a pointer type.  This means we have to
02091      sign-extend it manually as appropriate.  */
02092   byte_offset = value_as_address (value);
02093   if (TYPE_LENGTH (value_type (value)) < sizeof (LONGEST))
02094     byte_offset = gdb_sign_extend (byte_offset,
02095                                    8 * TYPE_LENGTH (value_type (value)));
02096   byte_offset += piece->v.ptr.offset;
02097 
02098   gdb_assert (piece);
02099   baton
02100     = dwarf2_fetch_die_loc_sect_off (piece->v.ptr.die, c->per_cu,
02101                                      get_frame_address_in_block_wrapper,
02102                                      frame);
02103 
02104   if (baton.data != NULL)
02105     return dwarf2_evaluate_loc_desc_full (TYPE_TARGET_TYPE (type), frame,
02106                                           baton.data, baton.size, baton.per_cu,
02107                                           byte_offset);
02108 
02109   {
02110     struct obstack temp_obstack;
02111     struct cleanup *cleanup;
02112     const gdb_byte *bytes;
02113     LONGEST len;
02114     struct value *result;
02115 
02116     obstack_init (&temp_obstack);
02117     cleanup = make_cleanup_obstack_free (&temp_obstack);
02118 
02119     bytes = dwarf2_fetch_constant_bytes (piece->v.ptr.die, c->per_cu,
02120                                          &temp_obstack, &len);
02121     if (bytes == NULL)
02122       result = allocate_optimized_out_value (TYPE_TARGET_TYPE (type));
02123     else
02124       {
02125         if (byte_offset < 0
02126             || byte_offset + TYPE_LENGTH (TYPE_TARGET_TYPE (type)) > len)
02127           invalid_synthetic_pointer ();
02128         bytes += byte_offset;
02129         result = value_from_contents (TYPE_TARGET_TYPE (type), bytes);
02130       }
02131 
02132     do_cleanups (cleanup);
02133     return result;
02134   }
02135 }
02136 
02137 static void *
02138 copy_pieced_value_closure (const struct value *v)
02139 {
02140   struct piece_closure *c
02141     = (struct piece_closure *) value_computed_closure (v);
02142   
02143   ++c->refc;
02144   return c;
02145 }
02146 
02147 static void
02148 free_pieced_value_closure (struct value *v)
02149 {
02150   struct piece_closure *c
02151     = (struct piece_closure *) value_computed_closure (v);
02152 
02153   --c->refc;
02154   if (c->refc == 0)
02155     {
02156       int i;
02157 
02158       for (i = 0; i < c->n_pieces; ++i)
02159         if (c->pieces[i].location == DWARF_VALUE_STACK)
02160           value_free (c->pieces[i].v.value);
02161 
02162       xfree (c->pieces);
02163       xfree (c);
02164     }
02165 }
02166 
02167 /* Functions for accessing a variable described by DW_OP_piece.  */
02168 static const struct lval_funcs pieced_value_funcs = {
02169   read_pieced_value,
02170   write_pieced_value,
02171   check_pieced_value_validity,
02172   check_pieced_value_invalid,
02173   indirect_pieced_value,
02174   NULL, /* coerce_ref */
02175   check_pieced_synthetic_pointer,
02176   copy_pieced_value_closure,
02177   free_pieced_value_closure
02178 };
02179 
02180 /* Virtual method table for dwarf2_evaluate_loc_desc_full below.  */
02181 
02182 static const struct dwarf_expr_context_funcs dwarf_expr_ctx_funcs =
02183 {
02184   dwarf_expr_read_reg,
02185   dwarf_expr_read_mem,
02186   dwarf_expr_frame_base,
02187   dwarf_expr_frame_cfa,
02188   dwarf_expr_frame_pc,
02189   dwarf_expr_tls_address,
02190   dwarf_expr_dwarf_call,
02191   dwarf_expr_get_base_type,
02192   dwarf_expr_push_dwarf_reg_entry_value,
02193   dwarf_expr_get_addr_index
02194 };
02195 
02196 /* Evaluate a location description, starting at DATA and with length
02197    SIZE, to find the current location of variable of TYPE in the
02198    context of FRAME.  BYTE_OFFSET is applied after the contents are
02199    computed.  */
02200 
02201 static struct value *
02202 dwarf2_evaluate_loc_desc_full (struct type *type, struct frame_info *frame,
02203                                const gdb_byte *data, size_t size,
02204                                struct dwarf2_per_cu_data *per_cu,
02205                                LONGEST byte_offset)
02206 {
02207   struct value *retval;
02208   struct dwarf_expr_baton baton;
02209   struct dwarf_expr_context *ctx;
02210   struct cleanup *old_chain, *value_chain;
02211   struct objfile *objfile = dwarf2_per_cu_objfile (per_cu);
02212   volatile struct gdb_exception ex;
02213 
02214   if (byte_offset < 0)
02215     invalid_synthetic_pointer ();
02216 
02217   if (size == 0)
02218     return allocate_optimized_out_value (type);
02219 
02220   baton.frame = frame;
02221   baton.per_cu = per_cu;
02222 
02223   ctx = new_dwarf_expr_context ();
02224   old_chain = make_cleanup_free_dwarf_expr_context (ctx);
02225   value_chain = make_cleanup_value_free_to_mark (value_mark ());
02226 
02227   ctx->gdbarch = get_objfile_arch (objfile);
02228   ctx->addr_size = dwarf2_per_cu_addr_size (per_cu);
02229   ctx->ref_addr_size = dwarf2_per_cu_ref_addr_size (per_cu);
02230   ctx->offset = dwarf2_per_cu_text_offset (per_cu);
02231   ctx->baton = &baton;
02232   ctx->funcs = &dwarf_expr_ctx_funcs;
02233 
02234   TRY_CATCH (ex, RETURN_MASK_ERROR)
02235     {
02236       dwarf_expr_eval (ctx, data, size);
02237     }
02238   if (ex.reason < 0)
02239     {
02240       if (ex.error == NOT_AVAILABLE_ERROR)
02241         {
02242           do_cleanups (old_chain);
02243           retval = allocate_value (type);
02244           mark_value_bytes_unavailable (retval, 0, TYPE_LENGTH (type));
02245           return retval;
02246         }
02247       else if (ex.error == NO_ENTRY_VALUE_ERROR)
02248         {
02249           if (entry_values_debug)
02250             exception_print (gdb_stdout, ex);
02251           do_cleanups (old_chain);
02252           return allocate_optimized_out_value (type);
02253         }
02254       else
02255         throw_exception (ex);
02256     }
02257 
02258   if (ctx->num_pieces > 0)
02259     {
02260       struct piece_closure *c;
02261       struct frame_id frame_id = get_frame_id (frame);
02262       ULONGEST bit_size = 0;
02263       int i;
02264 
02265       for (i = 0; i < ctx->num_pieces; ++i)
02266         bit_size += ctx->pieces[i].size;
02267       if (8 * (byte_offset + TYPE_LENGTH (type)) > bit_size)
02268         invalid_synthetic_pointer ();
02269 
02270       c = allocate_piece_closure (per_cu, ctx->num_pieces, ctx->pieces,
02271                                   ctx->addr_size);
02272       /* We must clean up the value chain after creating the piece
02273          closure but before allocating the result.  */
02274       do_cleanups (value_chain);
02275       retval = allocate_computed_value (type, &pieced_value_funcs, c);
02276       VALUE_FRAME_ID (retval) = frame_id;
02277       set_value_offset (retval, byte_offset);
02278     }
02279   else
02280     {
02281       switch (ctx->location)
02282         {
02283         case DWARF_VALUE_REGISTER:
02284           {
02285             struct gdbarch *arch = get_frame_arch (frame);
02286             int dwarf_regnum
02287               = longest_to_int (value_as_long (dwarf_expr_fetch (ctx, 0)));
02288             int gdb_regnum = gdbarch_dwarf2_reg_to_regnum (arch, dwarf_regnum);
02289 
02290             if (byte_offset != 0)
02291               error (_("cannot use offset on synthetic pointer to register"));
02292             do_cleanups (value_chain);
02293            if (gdb_regnum == -1)
02294               error (_("Unable to access DWARF register number %d"),
02295                      dwarf_regnum);
02296            retval = value_from_register (type, gdb_regnum, frame);
02297            if (value_optimized_out (retval))
02298              {
02299                /* This means the register has undefined value / was
02300                   not saved.  As we're computing the location of some
02301                   variable etc. in the program, not a value for
02302                   inspecting a register ($pc, $sp, etc.), return a
02303                   generic optimized out value instead, so that we show
02304                   <optimized out> instead of <not saved>.  */
02305                do_cleanups (value_chain);
02306                retval = allocate_optimized_out_value (type);
02307              }
02308           }
02309           break;
02310 
02311         case DWARF_VALUE_MEMORY:
02312           {
02313             CORE_ADDR address = dwarf_expr_fetch_address (ctx, 0);
02314             int in_stack_memory = dwarf_expr_fetch_in_stack_memory (ctx, 0);
02315 
02316             do_cleanups (value_chain);
02317             retval = value_at_lazy (type, address + byte_offset);
02318             if (in_stack_memory)
02319               set_value_stack (retval, 1);
02320           }
02321           break;
02322 
02323         case DWARF_VALUE_STACK:
02324           {
02325             struct value *value = dwarf_expr_fetch (ctx, 0);
02326             gdb_byte *contents;
02327             const gdb_byte *val_bytes;
02328             size_t n = TYPE_LENGTH (value_type (value));
02329 
02330             if (byte_offset + TYPE_LENGTH (type) > n)
02331               invalid_synthetic_pointer ();
02332 
02333             val_bytes = value_contents_all (value);
02334             val_bytes += byte_offset;
02335             n -= byte_offset;
02336 
02337             /* Preserve VALUE because we are going to free values back
02338                to the mark, but we still need the value contents
02339                below.  */
02340             value_incref (value);
02341             do_cleanups (value_chain);
02342             make_cleanup_value_free (value);
02343 
02344             retval = allocate_value (type);
02345             contents = value_contents_raw (retval);
02346             if (n > TYPE_LENGTH (type))
02347               {
02348                 struct gdbarch *objfile_gdbarch = get_objfile_arch (objfile);
02349 
02350                 if (gdbarch_byte_order (objfile_gdbarch) == BFD_ENDIAN_BIG)
02351                   val_bytes += n - TYPE_LENGTH (type);
02352                 n = TYPE_LENGTH (type);
02353               }
02354             memcpy (contents, val_bytes, n);
02355           }
02356           break;
02357 
02358         case DWARF_VALUE_LITERAL:
02359           {
02360             bfd_byte *contents;
02361             const bfd_byte *ldata;
02362             size_t n = ctx->len;
02363 
02364             if (byte_offset + TYPE_LENGTH (type) > n)
02365               invalid_synthetic_pointer ();
02366 
02367             do_cleanups (value_chain);
02368             retval = allocate_value (type);
02369             contents = value_contents_raw (retval);
02370 
02371             ldata = ctx->data + byte_offset;
02372             n -= byte_offset;
02373 
02374             if (n > TYPE_LENGTH (type))
02375               {
02376                 struct gdbarch *objfile_gdbarch = get_objfile_arch (objfile);
02377 
02378                 if (gdbarch_byte_order (objfile_gdbarch) == BFD_ENDIAN_BIG)
02379                   ldata += n - TYPE_LENGTH (type);
02380                 n = TYPE_LENGTH (type);
02381               }
02382             memcpy (contents, ldata, n);
02383           }
02384           break;
02385 
02386         case DWARF_VALUE_OPTIMIZED_OUT:
02387           do_cleanups (value_chain);
02388           retval = allocate_optimized_out_value (type);
02389           break;
02390 
02391           /* DWARF_VALUE_IMPLICIT_POINTER was converted to a pieced
02392              operation by execute_stack_op.  */
02393         case DWARF_VALUE_IMPLICIT_POINTER:
02394           /* DWARF_VALUE_OPTIMIZED_OUT can't occur in this context --
02395              it can only be encountered when making a piece.  */
02396         default:
02397           internal_error (__FILE__, __LINE__, _("invalid location type"));
02398         }
02399     }
02400 
02401   set_value_initialized (retval, ctx->initialized);
02402 
02403   do_cleanups (old_chain);
02404 
02405   return retval;
02406 }
02407 
02408 /* The exported interface to dwarf2_evaluate_loc_desc_full; it always
02409    passes 0 as the byte_offset.  */
02410 
02411 struct value *
02412 dwarf2_evaluate_loc_desc (struct type *type, struct frame_info *frame,
02413                           const gdb_byte *data, size_t size,
02414                           struct dwarf2_per_cu_data *per_cu)
02415 {
02416   return dwarf2_evaluate_loc_desc_full (type, frame, data, size, per_cu, 0);
02417 }
02418 
02419 
02420 /* Helper functions and baton for dwarf2_loc_desc_needs_frame.  */
02421 
02422 struct needs_frame_baton
02423 {
02424   int needs_frame;
02425   struct dwarf2_per_cu_data *per_cu;
02426 };
02427 
02428 /* Reads from registers do require a frame.  */
02429 static CORE_ADDR
02430 needs_frame_read_reg (void *baton, int regnum)
02431 {
02432   struct needs_frame_baton *nf_baton = baton;
02433 
02434   nf_baton->needs_frame = 1;
02435   return 1;
02436 }
02437 
02438 /* Reads from memory do not require a frame.  */
02439 static void
02440 needs_frame_read_mem (void *baton, gdb_byte *buf, CORE_ADDR addr, size_t len)
02441 {
02442   memset (buf, 0, len);
02443 }
02444 
02445 /* Frame-relative accesses do require a frame.  */
02446 static void
02447 needs_frame_frame_base (void *baton, const gdb_byte **start, size_t * length)
02448 {
02449   static gdb_byte lit0 = DW_OP_lit0;
02450   struct needs_frame_baton *nf_baton = baton;
02451 
02452   *start = &lit0;
02453   *length = 1;
02454 
02455   nf_baton->needs_frame = 1;
02456 }
02457 
02458 /* CFA accesses require a frame.  */
02459 
02460 static CORE_ADDR
02461 needs_frame_frame_cfa (void *baton)
02462 {
02463   struct needs_frame_baton *nf_baton = baton;
02464 
02465   nf_baton->needs_frame = 1;
02466   return 1;
02467 }
02468 
02469 /* Thread-local accesses do require a frame.  */
02470 static CORE_ADDR
02471 needs_frame_tls_address (void *baton, CORE_ADDR offset)
02472 {
02473   struct needs_frame_baton *nf_baton = baton;
02474 
02475   nf_baton->needs_frame = 1;
02476   return 1;
02477 }
02478 
02479 /* Helper interface of per_cu_dwarf_call for dwarf2_loc_desc_needs_frame.  */
02480 
02481 static void
02482 needs_frame_dwarf_call (struct dwarf_expr_context *ctx, cu_offset die_offset)
02483 {
02484   struct needs_frame_baton *nf_baton = ctx->baton;
02485 
02486   per_cu_dwarf_call (ctx, die_offset, nf_baton->per_cu,
02487                      ctx->funcs->get_frame_pc, ctx->baton);
02488 }
02489 
02490 /* DW_OP_GNU_entry_value accesses require a caller, therefore a frame.  */
02491 
02492 static void
02493 needs_dwarf_reg_entry_value (struct dwarf_expr_context *ctx,
02494                              enum call_site_parameter_kind kind,
02495                              union call_site_parameter_u kind_u, int deref_size)
02496 {
02497   struct needs_frame_baton *nf_baton = ctx->baton;
02498 
02499   nf_baton->needs_frame = 1;
02500 
02501   /* The expression may require some stub values on DWARF stack.  */
02502   dwarf_expr_push_address (ctx, 0, 0);
02503 }
02504 
02505 /* DW_OP_GNU_addr_index doesn't require a frame.  */
02506 
02507 static CORE_ADDR
02508 needs_get_addr_index (void *baton, unsigned int index)
02509 {
02510   /* Nothing to do.  */
02511   return 1;
02512 }
02513 
02514 /* Virtual method table for dwarf2_loc_desc_needs_frame below.  */
02515 
02516 static const struct dwarf_expr_context_funcs needs_frame_ctx_funcs =
02517 {
02518   needs_frame_read_reg,
02519   needs_frame_read_mem,
02520   needs_frame_frame_base,
02521   needs_frame_frame_cfa,
02522   needs_frame_frame_cfa,        /* get_frame_pc */
02523   needs_frame_tls_address,
02524   needs_frame_dwarf_call,
02525   NULL,                         /* get_base_type */
02526   needs_dwarf_reg_entry_value,
02527   needs_get_addr_index
02528 };
02529 
02530 /* Return non-zero iff the location expression at DATA (length SIZE)
02531    requires a frame to evaluate.  */
02532 
02533 static int
02534 dwarf2_loc_desc_needs_frame (const gdb_byte *data, size_t size,
02535                              struct dwarf2_per_cu_data *per_cu)
02536 {
02537   struct needs_frame_baton baton;
02538   struct dwarf_expr_context *ctx;
02539   int in_reg;
02540   struct cleanup *old_chain;
02541   struct objfile *objfile = dwarf2_per_cu_objfile (per_cu);
02542 
02543   baton.needs_frame = 0;
02544   baton.per_cu = per_cu;
02545 
02546   ctx = new_dwarf_expr_context ();
02547   old_chain = make_cleanup_free_dwarf_expr_context (ctx);
02548   make_cleanup_value_free_to_mark (value_mark ());
02549 
02550   ctx->gdbarch = get_objfile_arch (objfile);
02551   ctx->addr_size = dwarf2_per_cu_addr_size (per_cu);
02552   ctx->ref_addr_size = dwarf2_per_cu_ref_addr_size (per_cu);
02553   ctx->offset = dwarf2_per_cu_text_offset (per_cu);
02554   ctx->baton = &baton;
02555   ctx->funcs = &needs_frame_ctx_funcs;
02556 
02557   dwarf_expr_eval (ctx, data, size);
02558 
02559   in_reg = ctx->location == DWARF_VALUE_REGISTER;
02560 
02561   if (ctx->num_pieces > 0)
02562     {
02563       int i;
02564 
02565       /* If the location has several pieces, and any of them are in
02566          registers, then we will need a frame to fetch them from.  */
02567       for (i = 0; i < ctx->num_pieces; i++)
02568         if (ctx->pieces[i].location == DWARF_VALUE_REGISTER)
02569           in_reg = 1;
02570     }
02571 
02572   do_cleanups (old_chain);
02573 
02574   return baton.needs_frame || in_reg;
02575 }
02576 
02577 /* A helper function that throws an unimplemented error mentioning a
02578    given DWARF operator.  */
02579 
02580 static void
02581 unimplemented (unsigned int op)
02582 {
02583   const char *name = get_DW_OP_name (op);
02584 
02585   if (name)
02586     error (_("DWARF operator %s cannot be translated to an agent expression"),
02587            name);
02588   else
02589     error (_("Unknown DWARF operator 0x%02x cannot be translated "
02590              "to an agent expression"),
02591            op);
02592 }
02593 
02594 /* A helper function to convert a DWARF register to an arch register.
02595    ARCH is the architecture.
02596    DWARF_REG is the register.
02597    This will throw an exception if the DWARF register cannot be
02598    translated to an architecture register.  */
02599 
02600 static int
02601 translate_register (struct gdbarch *arch, int dwarf_reg)
02602 {
02603   int reg = gdbarch_dwarf2_reg_to_regnum (arch, dwarf_reg);
02604   if (reg == -1)
02605     error (_("Unable to access DWARF register number %d"), dwarf_reg);
02606   return reg;
02607 }
02608 
02609 /* A helper function that emits an access to memory.  ARCH is the
02610    target architecture.  EXPR is the expression which we are building.
02611    NBITS is the number of bits we want to read.  This emits the
02612    opcodes needed to read the memory and then extract the desired
02613    bits.  */
02614 
02615 static void
02616 access_memory (struct gdbarch *arch, struct agent_expr *expr, ULONGEST nbits)
02617 {
02618   ULONGEST nbytes = (nbits + 7) / 8;
02619 
02620   gdb_assert (nbytes > 0 && nbytes <= sizeof (LONGEST));
02621 
02622   if (expr->tracing)
02623     ax_trace_quick (expr, nbytes);
02624 
02625   if (nbits <= 8)
02626     ax_simple (expr, aop_ref8);
02627   else if (nbits <= 16)
02628     ax_simple (expr, aop_ref16);
02629   else if (nbits <= 32)
02630     ax_simple (expr, aop_ref32);
02631   else
02632     ax_simple (expr, aop_ref64);
02633 
02634   /* If we read exactly the number of bytes we wanted, we're done.  */
02635   if (8 * nbytes == nbits)
02636     return;
02637 
02638   if (gdbarch_bits_big_endian (arch))
02639     {
02640       /* On a bits-big-endian machine, we want the high-order
02641          NBITS.  */
02642       ax_const_l (expr, 8 * nbytes - nbits);
02643       ax_simple (expr, aop_rsh_unsigned);
02644     }
02645   else
02646     {
02647       /* On a bits-little-endian box, we want the low-order NBITS.  */
02648       ax_zero_ext (expr, nbits);
02649     }
02650 }
02651 
02652 /* A helper function to return the frame's PC.  */
02653 
02654 static CORE_ADDR
02655 get_ax_pc (void *baton)
02656 {
02657   struct agent_expr *expr = baton;
02658 
02659   return expr->scope;
02660 }
02661 
02662 /* Compile a DWARF location expression to an agent expression.
02663    
02664    EXPR is the agent expression we are building.
02665    LOC is the agent value we modify.
02666    ARCH is the architecture.
02667    ADDR_SIZE is the size of addresses, in bytes.
02668    OP_PTR is the start of the location expression.
02669    OP_END is one past the last byte of the location expression.
02670    
02671    This will throw an exception for various kinds of errors -- for
02672    example, if the expression cannot be compiled, or if the expression
02673    is invalid.  */
02674 
02675 void
02676 dwarf2_compile_expr_to_ax (struct agent_expr *expr, struct axs_value *loc,
02677                            struct gdbarch *arch, unsigned int addr_size,
02678                            const gdb_byte *op_ptr, const gdb_byte *op_end,
02679                            struct dwarf2_per_cu_data *per_cu)
02680 {
02681   struct cleanup *cleanups;
02682   int i, *offsets;
02683   VEC(int) *dw_labels = NULL, *patches = NULL;
02684   const gdb_byte * const base = op_ptr;
02685   const gdb_byte *previous_piece = op_ptr;
02686   enum bfd_endian byte_order = gdbarch_byte_order (arch);
02687   ULONGEST bits_collected = 0;
02688   unsigned int addr_size_bits = 8 * addr_size;
02689   int bits_big_endian = gdbarch_bits_big_endian (arch);
02690 
02691   offsets = xmalloc ((op_end - op_ptr) * sizeof (int));
02692   cleanups = make_cleanup (xfree, offsets);
02693 
02694   for (i = 0; i < op_end - op_ptr; ++i)
02695     offsets[i] = -1;
02696 
02697   make_cleanup (VEC_cleanup (int), &dw_labels);
02698   make_cleanup (VEC_cleanup (int), &patches);
02699 
02700   /* By default we are making an address.  */
02701   loc->kind = axs_lvalue_memory;
02702 
02703   while (op_ptr < op_end)
02704     {
02705       enum dwarf_location_atom op = *op_ptr;
02706       uint64_t uoffset, reg;
02707       int64_t offset;
02708       int i;
02709 
02710       offsets[op_ptr - base] = expr->len;
02711       ++op_ptr;
02712 
02713       /* Our basic approach to code generation is to map DWARF
02714          operations directly to AX operations.  However, there are
02715          some differences.
02716 
02717          First, DWARF works on address-sized units, but AX always uses
02718          LONGEST.  For most operations we simply ignore this
02719          difference; instead we generate sign extensions as needed
02720          before division and comparison operations.  It would be nice
02721          to omit the sign extensions, but there is no way to determine
02722          the size of the target's LONGEST.  (This code uses the size
02723          of the host LONGEST in some cases -- that is a bug but it is
02724          difficult to fix.)
02725 
02726          Second, some DWARF operations cannot be translated to AX.
02727          For these we simply fail.  See
02728          http://sourceware.org/bugzilla/show_bug.cgi?id=11662.  */
02729       switch (op)
02730         {
02731         case DW_OP_lit0:
02732         case DW_OP_lit1:
02733         case DW_OP_lit2:
02734         case DW_OP_lit3:
02735         case DW_OP_lit4:
02736         case DW_OP_lit5:
02737         case DW_OP_lit6:
02738         case DW_OP_lit7:
02739         case DW_OP_lit8:
02740         case DW_OP_lit9:
02741         case DW_OP_lit10:
02742         case DW_OP_lit11:
02743         case DW_OP_lit12:
02744         case DW_OP_lit13:
02745         case DW_OP_lit14:
02746         case DW_OP_lit15:
02747         case DW_OP_lit16:
02748         case DW_OP_lit17:
02749         case DW_OP_lit18:
02750         case DW_OP_lit19:
02751         case DW_OP_lit20:
02752         case DW_OP_lit21:
02753         case DW_OP_lit22:
02754         case DW_OP_lit23:
02755         case DW_OP_lit24:
02756         case DW_OP_lit25:
02757         case DW_OP_lit26:
02758         case DW_OP_lit27:
02759         case DW_OP_lit28:
02760         case DW_OP_lit29:
02761         case DW_OP_lit30:
02762         case DW_OP_lit31:
02763           ax_const_l (expr, op - DW_OP_lit0);
02764           break;
02765 
02766         case DW_OP_addr:
02767           uoffset = extract_unsigned_integer (op_ptr, addr_size, byte_order);
02768           op_ptr += addr_size;
02769           /* Some versions of GCC emit DW_OP_addr before
02770              DW_OP_GNU_push_tls_address.  In this case the value is an
02771              index, not an address.  We don't support things like
02772              branching between the address and the TLS op.  */
02773           if (op_ptr >= op_end || *op_ptr != DW_OP_GNU_push_tls_address)
02774             uoffset += dwarf2_per_cu_text_offset (per_cu);
02775           ax_const_l (expr, uoffset);
02776           break;
02777 
02778         case DW_OP_const1u:
02779           ax_const_l (expr, extract_unsigned_integer (op_ptr, 1, byte_order));
02780           op_ptr += 1;
02781           break;
02782         case DW_OP_const1s:
02783           ax_const_l (expr, extract_signed_integer (op_ptr, 1, byte_order));
02784           op_ptr += 1;
02785           break;
02786         case DW_OP_const2u:
02787           ax_const_l (expr, extract_unsigned_integer (op_ptr, 2, byte_order));
02788           op_ptr += 2;
02789           break;
02790         case DW_OP_const2s:
02791           ax_const_l (expr, extract_signed_integer (op_ptr, 2, byte_order));
02792           op_ptr += 2;
02793           break;
02794         case DW_OP_const4u:
02795           ax_const_l (expr, extract_unsigned_integer (op_ptr, 4, byte_order));
02796           op_ptr += 4;
02797           break;
02798         case DW_OP_const4s:
02799           ax_const_l (expr, extract_signed_integer (op_ptr, 4, byte_order));
02800           op_ptr += 4;
02801           break;
02802         case DW_OP_const8u:
02803           ax_const_l (expr, extract_unsigned_integer (op_ptr, 8, byte_order));
02804           op_ptr += 8;
02805           break;
02806         case DW_OP_const8s:
02807           ax_const_l (expr, extract_signed_integer (op_ptr, 8, byte_order));
02808           op_ptr += 8;
02809           break;
02810         case DW_OP_constu:
02811           op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
02812           ax_const_l (expr, uoffset);
02813           break;
02814         case DW_OP_consts:
02815           op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
02816           ax_const_l (expr, offset);
02817           break;
02818 
02819         case DW_OP_reg0:
02820         case DW_OP_reg1:
02821         case DW_OP_reg2:
02822         case DW_OP_reg3:
02823         case DW_OP_reg4:
02824         case DW_OP_reg5:
02825         case DW_OP_reg6:
02826         case DW_OP_reg7:
02827         case DW_OP_reg8:
02828         case DW_OP_reg9:
02829         case DW_OP_reg10:
02830         case DW_OP_reg11:
02831         case DW_OP_reg12:
02832         case DW_OP_reg13:
02833         case DW_OP_reg14:
02834         case DW_OP_reg15:
02835         case DW_OP_reg16:
02836         case DW_OP_reg17:
02837         case DW_OP_reg18:
02838         case DW_OP_reg19:
02839         case DW_OP_reg20:
02840         case DW_OP_reg21:
02841         case DW_OP_reg22:
02842         case DW_OP_reg23:
02843         case DW_OP_reg24:
02844         case DW_OP_reg25:
02845         case DW_OP_reg26:
02846         case DW_OP_reg27:
02847         case DW_OP_reg28:
02848         case DW_OP_reg29:
02849         case DW_OP_reg30:
02850         case DW_OP_reg31:
02851           dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_regx");
02852           loc->u.reg = translate_register (arch, op - DW_OP_reg0);
02853           loc->kind = axs_lvalue_register;
02854           break;
02855 
02856         case DW_OP_regx:
02857           op_ptr = safe_read_uleb128 (op_ptr, op_end, &reg);
02858           dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_regx");
02859           loc->u.reg = translate_register (arch, reg);
02860           loc->kind = axs_lvalue_register;
02861           break;
02862 
02863         case DW_OP_implicit_value:
02864           {
02865             uint64_t len;
02866 
02867             op_ptr = safe_read_uleb128 (op_ptr, op_end, &len);
02868             if (op_ptr + len > op_end)
02869               error (_("DW_OP_implicit_value: too few bytes available."));
02870             if (len > sizeof (ULONGEST))
02871               error (_("Cannot translate DW_OP_implicit_value of %d bytes"),
02872                      (int) len);
02873 
02874             ax_const_l (expr, extract_unsigned_integer (op_ptr, len,
02875                                                         byte_order));
02876             op_ptr += len;
02877             dwarf_expr_require_composition (op_ptr, op_end,
02878                                             "DW_OP_implicit_value");
02879 
02880             loc->kind = axs_rvalue;
02881           }
02882           break;
02883 
02884         case DW_OP_stack_value:
02885           dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_stack_value");
02886           loc->kind = axs_rvalue;
02887           break;
02888 
02889         case DW_OP_breg0:
02890         case DW_OP_breg1:
02891         case DW_OP_breg2:
02892         case DW_OP_breg3:
02893         case DW_OP_breg4:
02894         case DW_OP_breg5:
02895         case DW_OP_breg6:
02896         case DW_OP_breg7:
02897         case DW_OP_breg8:
02898         case DW_OP_breg9:
02899         case DW_OP_breg10:
02900         case DW_OP_breg11:
02901         case DW_OP_breg12:
02902         case DW_OP_breg13:
02903         case DW_OP_breg14:
02904         case DW_OP_breg15:
02905         case DW_OP_breg16:
02906         case DW_OP_breg17:
02907         case DW_OP_breg18:
02908         case DW_OP_breg19:
02909         case DW_OP_breg20:
02910         case DW_OP_breg21:
02911         case DW_OP_breg22:
02912         case DW_OP_breg23:
02913         case DW_OP_breg24:
02914         case DW_OP_breg25:
02915         case DW_OP_breg26:
02916         case DW_OP_breg27:
02917         case DW_OP_breg28:
02918         case DW_OP_breg29:
02919         case DW_OP_breg30:
02920         case DW_OP_breg31:
02921           op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
02922           i = translate_register (arch, op - DW_OP_breg0);
02923           ax_reg (expr, i);
02924           if (offset != 0)
02925             {
02926               ax_const_l (expr, offset);
02927               ax_simple (expr, aop_add);
02928             }
02929           break;
02930         case DW_OP_bregx:
02931           {
02932             op_ptr = safe_read_uleb128 (op_ptr, op_end, &reg);
02933             op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
02934             i = translate_register (arch, reg);
02935             ax_reg (expr, i);
02936             if (offset != 0)
02937               {
02938                 ax_const_l (expr, offset);
02939                 ax_simple (expr, aop_add);
02940               }
02941           }
02942           break;
02943         case DW_OP_fbreg:
02944           {
02945             const gdb_byte *datastart;
02946             size_t datalen;
02947             struct block *b;
02948             struct symbol *framefunc;
02949 
02950             b = block_for_pc (expr->scope);
02951 
02952             if (!b)
02953               error (_("No block found for address"));
02954 
02955             framefunc = block_linkage_function (b);
02956 
02957             if (!framefunc)
02958               error (_("No function found for block"));
02959 
02960             dwarf_expr_frame_base_1 (framefunc, expr->scope,
02961                                      &datastart, &datalen);
02962 
02963             op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
02964             dwarf2_compile_expr_to_ax (expr, loc, arch, addr_size, datastart,
02965                                        datastart + datalen, per_cu);
02966             if (loc->kind == axs_lvalue_register)
02967               require_rvalue (expr, loc);
02968 
02969             if (offset != 0)
02970               {
02971                 ax_const_l (expr, offset);
02972                 ax_simple (expr, aop_add);
02973               }
02974 
02975             loc->kind = axs_lvalue_memory;
02976           }
02977           break;
02978 
02979         case DW_OP_dup:
02980           ax_simple (expr, aop_dup);
02981           break;
02982 
02983         case DW_OP_drop:
02984           ax_simple (expr, aop_pop);
02985           break;
02986 
02987         case DW_OP_pick:
02988           offset = *op_ptr++;
02989           ax_pick (expr, offset);
02990           break;
02991           
02992         case DW_OP_swap:
02993           ax_simple (expr, aop_swap);
02994           break;
02995 
02996         case DW_OP_over:
02997           ax_pick (expr, 1);
02998           break;
02999 
03000         case DW_OP_rot:
03001           ax_simple (expr, aop_rot);
03002           break;
03003 
03004         case DW_OP_deref:
03005         case DW_OP_deref_size:
03006           {
03007             int size;
03008 
03009             if (op == DW_OP_deref_size)
03010               size = *op_ptr++;
03011             else
03012               size = addr_size;
03013 
03014             if (size != 1 && size != 2 && size != 4 && size != 8)
03015               error (_("Unsupported size %d in %s"),
03016                      size, get_DW_OP_name (op));
03017             access_memory (arch, expr, size * TARGET_CHAR_BIT);
03018           }
03019           break;
03020 
03021         case DW_OP_abs:
03022           /* Sign extend the operand.  */
03023           ax_ext (expr, addr_size_bits);
03024           ax_simple (expr, aop_dup);
03025           ax_const_l (expr, 0);
03026           ax_simple (expr, aop_less_signed);
03027           ax_simple (expr, aop_log_not);
03028           i = ax_goto (expr, aop_if_goto);
03029           /* We have to emit 0 - X.  */
03030           ax_const_l (expr, 0);
03031           ax_simple (expr, aop_swap);
03032           ax_simple (expr, aop_sub);
03033           ax_label (expr, i, expr->len);
03034           break;
03035 
03036         case DW_OP_neg:
03037           /* No need to sign extend here.  */
03038           ax_const_l (expr, 0);
03039           ax_simple (expr, aop_swap);
03040           ax_simple (expr, aop_sub);
03041           break;
03042 
03043         case DW_OP_not:
03044           /* Sign extend the operand.  */
03045           ax_ext (expr, addr_size_bits);
03046           ax_simple (expr, aop_bit_not);
03047           break;
03048 
03049         case DW_OP_plus_uconst:
03050           op_ptr = safe_read_uleb128 (op_ptr, op_end, &reg);
03051           /* It would be really weird to emit `DW_OP_plus_uconst 0',
03052              but we micro-optimize anyhow.  */
03053           if (reg != 0)
03054             {
03055               ax_const_l (expr, reg);
03056               ax_simple (expr, aop_add);
03057             }
03058           break;
03059 
03060         case DW_OP_and:
03061           ax_simple (expr, aop_bit_and);
03062           break;
03063 
03064         case DW_OP_div:
03065           /* Sign extend the operands.  */
03066           ax_ext (expr, addr_size_bits);
03067           ax_simple (expr, aop_swap);
03068           ax_ext (expr, addr_size_bits);
03069           ax_simple (expr, aop_swap);
03070           ax_simple (expr, aop_div_signed);
03071           break;
03072 
03073         case DW_OP_minus:
03074           ax_simple (expr, aop_sub);
03075           break;
03076 
03077         case DW_OP_mod:
03078           ax_simple (expr, aop_rem_unsigned);
03079           break;
03080 
03081         case DW_OP_mul:
03082           ax_simple (expr, aop_mul);
03083           break;
03084 
03085         case DW_OP_or:
03086           ax_simple (expr, aop_bit_or);
03087           break;
03088 
03089         case DW_OP_plus:
03090           ax_simple (expr, aop_add);
03091           break;
03092 
03093         case DW_OP_shl:
03094           ax_simple (expr, aop_lsh);
03095           break;
03096 
03097         case DW_OP_shr:
03098           ax_simple (expr, aop_rsh_unsigned);
03099           break;
03100 
03101         case DW_OP_shra:
03102           ax_simple (expr, aop_rsh_signed);
03103           break;
03104 
03105         case DW_OP_xor:
03106           ax_simple (expr, aop_bit_xor);
03107           break;
03108 
03109         case DW_OP_le:
03110           /* Sign extend the operands.  */
03111           ax_ext (expr, addr_size_bits);
03112           ax_simple (expr, aop_swap);
03113           ax_ext (expr, addr_size_bits);
03114           /* Note no swap here: A <= B is !(B < A).  */
03115           ax_simple (expr, aop_less_signed);
03116           ax_simple (expr, aop_log_not);
03117           break;
03118 
03119         case DW_OP_ge:
03120           /* Sign extend the operands.  */
03121           ax_ext (expr, addr_size_bits);
03122           ax_simple (expr, aop_swap);
03123           ax_ext (expr, addr_size_bits);
03124           ax_simple (expr, aop_swap);
03125           /* A >= B is !(A < B).  */
03126           ax_simple (expr, aop_less_signed);
03127           ax_simple (expr, aop_log_not);
03128           break;
03129 
03130         case DW_OP_eq:
03131           /* Sign extend the operands.  */
03132           ax_ext (expr, addr_size_bits);
03133           ax_simple (expr, aop_swap);
03134           ax_ext (expr, addr_size_bits);
03135           /* No need for a second swap here.  */
03136           ax_simple (expr, aop_equal);
03137           break;
03138 
03139         case DW_OP_lt:
03140           /* Sign extend the operands.  */
03141           ax_ext (expr, addr_size_bits);
03142           ax_simple (expr, aop_swap);
03143           ax_ext (expr, addr_size_bits);
03144           ax_simple (expr, aop_swap);
03145           ax_simple (expr, aop_less_signed);
03146           break;
03147 
03148         case DW_OP_gt:
03149           /* Sign extend the operands.  */
03150           ax_ext (expr, addr_size_bits);
03151           ax_simple (expr, aop_swap);
03152           ax_ext (expr, addr_size_bits);
03153           /* Note no swap here: A > B is B < A.  */
03154           ax_simple (expr, aop_less_signed);
03155           break;
03156 
03157         case DW_OP_ne:
03158           /* Sign extend the operands.  */
03159           ax_ext (expr, addr_size_bits);
03160           ax_simple (expr, aop_swap);
03161           ax_ext (expr, addr_size_bits);
03162           /* No need for a swap here.  */
03163           ax_simple (expr, aop_equal);
03164           ax_simple (expr, aop_log_not);
03165           break;
03166 
03167         case DW_OP_call_frame_cfa:
03168           dwarf2_compile_cfa_to_ax (expr, loc, arch, expr->scope, per_cu);
03169           loc->kind = axs_lvalue_memory;
03170           break;
03171 
03172         case DW_OP_GNU_push_tls_address:
03173           unimplemented (op);
03174           break;
03175 
03176         case DW_OP_skip:
03177           offset = extract_signed_integer (op_ptr, 2, byte_order);
03178           op_ptr += 2;
03179           i = ax_goto (expr, aop_goto);
03180           VEC_safe_push (int, dw_labels, op_ptr + offset - base);
03181           VEC_safe_push (int, patches, i);
03182           break;
03183 
03184         case DW_OP_bra:
03185           offset = extract_signed_integer (op_ptr, 2, byte_order);
03186           op_ptr += 2;
03187           /* Zero extend the operand.  */
03188           ax_zero_ext (expr, addr_size_bits);
03189           i = ax_goto (expr, aop_if_goto);
03190           VEC_safe_push (int, dw_labels, op_ptr + offset - base);
03191           VEC_safe_push (int, patches, i);
03192           break;
03193 
03194         case DW_OP_nop:
03195           break;
03196 
03197         case DW_OP_piece:
03198         case DW_OP_bit_piece:
03199           {
03200             uint64_t size, offset;
03201 
03202             if (op_ptr - 1 == previous_piece)
03203               error (_("Cannot translate empty pieces to agent expressions"));
03204             previous_piece = op_ptr - 1;
03205 
03206             op_ptr = safe_read_uleb128 (op_ptr, op_end, &size);
03207             if (op == DW_OP_piece)
03208               {
03209                 size *= 8;
03210                 offset = 0;
03211               }
03212             else
03213               op_ptr = safe_read_uleb128 (op_ptr, op_end, &offset);
03214 
03215             if (bits_collected + size > 8 * sizeof (LONGEST))
03216               error (_("Expression pieces exceed word size"));
03217 
03218             /* Access the bits.  */
03219             switch (loc->kind)
03220               {
03221               case axs_lvalue_register:
03222                 ax_reg (expr, loc->u.reg);
03223                 break;
03224 
03225               case axs_lvalue_memory:
03226                 /* Offset the pointer, if needed.  */
03227                 if (offset > 8)
03228                   {
03229                     ax_const_l (expr, offset / 8);
03230                     ax_simple (expr, aop_add);
03231                     offset %= 8;
03232                   }
03233                 access_memory (arch, expr, size);
03234                 break;
03235               }
03236 
03237             /* For a bits-big-endian target, shift up what we already
03238                have.  For a bits-little-endian target, shift up the
03239                new data.  Note that there is a potential bug here if
03240                the DWARF expression leaves multiple values on the
03241                stack.  */
03242             if (bits_collected > 0)
03243               {
03244                 if (bits_big_endian)
03245                   {
03246                     ax_simple (expr, aop_swap);
03247                     ax_const_l (expr, size);
03248                     ax_simple (expr, aop_lsh);
03249                     /* We don't need a second swap here, because
03250                        aop_bit_or is symmetric.  */
03251                   }
03252                 else
03253                   {
03254                     ax_const_l (expr, size);
03255                     ax_simple (expr, aop_lsh);
03256                   }
03257                 ax_simple (expr, aop_bit_or);
03258               }
03259 
03260             bits_collected += size;
03261             loc->kind = axs_rvalue;
03262           }
03263           break;
03264 
03265         case DW_OP_GNU_uninit:
03266           unimplemented (op);
03267 
03268         case DW_OP_call2:
03269         case DW_OP_call4:
03270           {
03271             struct dwarf2_locexpr_baton block;
03272             int size = (op == DW_OP_call2 ? 2 : 4);
03273             cu_offset offset;
03274 
03275             uoffset = extract_unsigned_integer (op_ptr, size, byte_order);
03276             op_ptr += size;
03277 
03278             offset.cu_off = uoffset;
03279             block = dwarf2_fetch_die_loc_cu_off (offset, per_cu,
03280                                                  get_ax_pc, expr);
03281 
03282             /* DW_OP_call_ref is currently not supported.  */
03283             gdb_assert (block.per_cu == per_cu);
03284 
03285             dwarf2_compile_expr_to_ax (expr, loc, arch, addr_size,
03286                                        block.data, block.data + block.size,
03287                                        per_cu);
03288           }
03289           break;
03290 
03291         case DW_OP_call_ref:
03292           unimplemented (op);
03293 
03294         default:
03295           unimplemented (op);
03296         }
03297     }
03298 
03299   /* Patch all the branches we emitted.  */
03300   for (i = 0; i < VEC_length (int, patches); ++i)
03301     {
03302       int targ = offsets[VEC_index (int, dw_labels, i)];
03303       if (targ == -1)
03304         internal_error (__FILE__, __LINE__, _("invalid label"));
03305       ax_label (expr, VEC_index (int, patches, i), targ);
03306     }
03307 
03308   do_cleanups (cleanups);
03309 }
03310 
03311 
03312 /* Return the value of SYMBOL in FRAME using the DWARF-2 expression
03313    evaluator to calculate the location.  */
03314 static struct value *
03315 locexpr_read_variable (struct symbol *symbol, struct frame_info *frame)
03316 {
03317   struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
03318   struct value *val;
03319 
03320   val = dwarf2_evaluate_loc_desc (SYMBOL_TYPE (symbol), frame, dlbaton->data,
03321                                   dlbaton->size, dlbaton->per_cu);
03322 
03323   return val;
03324 }
03325 
03326 /* Return the value of SYMBOL in FRAME at (callee) FRAME's function
03327    entry.  SYMBOL should be a function parameter, otherwise NO_ENTRY_VALUE_ERROR
03328    will be thrown.  */
03329 
03330 static struct value *
03331 locexpr_read_variable_at_entry (struct symbol *symbol, struct frame_info *frame)
03332 {
03333   struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
03334 
03335   return value_of_dwarf_block_entry (SYMBOL_TYPE (symbol), frame, dlbaton->data,
03336                                      dlbaton->size);
03337 }
03338 
03339 /* Return non-zero iff we need a frame to evaluate SYMBOL.  */
03340 static int
03341 locexpr_read_needs_frame (struct symbol *symbol)
03342 {
03343   struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
03344 
03345   return dwarf2_loc_desc_needs_frame (dlbaton->data, dlbaton->size,
03346                                       dlbaton->per_cu);
03347 }
03348 
03349 /* Return true if DATA points to the end of a piece.  END is one past
03350    the last byte in the expression.  */
03351 
03352 static int
03353 piece_end_p (const gdb_byte *data, const gdb_byte *end)
03354 {
03355   return data == end || data[0] == DW_OP_piece || data[0] == DW_OP_bit_piece;
03356 }
03357 
03358 /* Helper for locexpr_describe_location_piece that finds the name of a
03359    DWARF register.  */
03360 
03361 static const char *
03362 locexpr_regname (struct gdbarch *gdbarch, int dwarf_regnum)
03363 {
03364   int regnum;
03365 
03366   regnum = gdbarch_dwarf2_reg_to_regnum (gdbarch, dwarf_regnum);
03367   return gdbarch_register_name (gdbarch, regnum);
03368 }
03369 
03370 /* Nicely describe a single piece of a location, returning an updated
03371    position in the bytecode sequence.  This function cannot recognize
03372    all locations; if a location is not recognized, it simply returns
03373    DATA.  If there is an error during reading, e.g. we run off the end
03374    of the buffer, an error is thrown.  */
03375 
03376 static const gdb_byte *
03377 locexpr_describe_location_piece (struct symbol *symbol, struct ui_file *stream,
03378                                  CORE_ADDR addr, struct objfile *objfile,
03379                                  struct dwarf2_per_cu_data *per_cu,
03380                                  const gdb_byte *data, const gdb_byte *end,
03381                                  unsigned int addr_size)
03382 {
03383   struct gdbarch *gdbarch = get_objfile_arch (objfile);
03384   size_t leb128_size;
03385 
03386   if (data[0] >= DW_OP_reg0 && data[0] <= DW_OP_reg31)
03387     {
03388       fprintf_filtered (stream, _("a variable in $%s"),
03389                         locexpr_regname (gdbarch, data[0] - DW_OP_reg0));
03390       data += 1;
03391     }
03392   else if (data[0] == DW_OP_regx)
03393     {
03394       uint64_t reg;
03395 
03396       data = safe_read_uleb128 (data + 1, end, &reg);
03397       fprintf_filtered (stream, _("a variable in $%s"),
03398                         locexpr_regname (gdbarch, reg));
03399     }
03400   else if (data[0] == DW_OP_fbreg)
03401     {
03402       struct block *b;
03403       struct symbol *framefunc;
03404       int frame_reg = 0;
03405       int64_t frame_offset;
03406       const gdb_byte *base_data, *new_data, *save_data = data;
03407       size_t base_size;
03408       int64_t base_offset = 0;
03409 
03410       new_data = safe_read_sleb128 (data + 1, end, &frame_offset);
03411       if (!piece_end_p (new_data, end))
03412         return data;
03413       data = new_data;
03414 
03415       b = block_for_pc (addr);
03416 
03417       if (!b)
03418         error (_("No block found for address for symbol \"%s\"."),
03419                SYMBOL_PRINT_NAME (symbol));
03420 
03421       framefunc = block_linkage_function (b);
03422 
03423       if (!framefunc)
03424         error (_("No function found for block for symbol \"%s\"."),
03425                SYMBOL_PRINT_NAME (symbol));
03426 
03427       dwarf_expr_frame_base_1 (framefunc, addr, &base_data, &base_size);
03428 
03429       if (base_data[0] >= DW_OP_breg0 && base_data[0] <= DW_OP_breg31)
03430         {
03431           const gdb_byte *buf_end;
03432           
03433           frame_reg = base_data[0] - DW_OP_breg0;
03434           buf_end = safe_read_sleb128 (base_data + 1, base_data + base_size,
03435                                        &base_offset);
03436           if (buf_end != base_data + base_size)
03437             error (_("Unexpected opcode after "
03438                      "DW_OP_breg%u for symbol \"%s\"."),
03439                    frame_reg, SYMBOL_PRINT_NAME (symbol));
03440         }
03441       else if (base_data[0] >= DW_OP_reg0 && base_data[0] <= DW_OP_reg31)
03442         {
03443           /* The frame base is just the register, with no offset.  */
03444           frame_reg = base_data[0] - DW_OP_reg0;
03445           base_offset = 0;
03446         }
03447       else
03448         {
03449           /* We don't know what to do with the frame base expression,
03450              so we can't trace this variable; give up.  */
03451           return save_data;
03452         }
03453 
03454       fprintf_filtered (stream,
03455                         _("a variable at frame base reg $%s offset %s+%s"),
03456                         locexpr_regname (gdbarch, frame_reg),
03457                         plongest (base_offset), plongest (frame_offset));
03458     }
03459   else if (data[0] >= DW_OP_breg0 && data[0] <= DW_OP_breg31
03460            && piece_end_p (data, end))
03461     {
03462       int64_t offset;
03463 
03464       data = safe_read_sleb128 (data + 1, end, &offset);
03465 
03466       fprintf_filtered (stream,
03467                         _("a variable at offset %s from base reg $%s"),
03468                         plongest (offset),
03469                         locexpr_regname (gdbarch, data[0] - DW_OP_breg0));
03470     }
03471 
03472   /* The location expression for a TLS variable looks like this (on a
03473      64-bit LE machine):
03474 
03475      DW_AT_location    : 10 byte block: 3 4 0 0 0 0 0 0 0 e0
03476                         (DW_OP_addr: 4; DW_OP_GNU_push_tls_address)
03477 
03478      0x3 is the encoding for DW_OP_addr, which has an operand as long
03479      as the size of an address on the target machine (here is 8
03480      bytes).  Note that more recent version of GCC emit DW_OP_const4u
03481      or DW_OP_const8u, depending on address size, rather than
03482      DW_OP_addr.  0xe0 is the encoding for DW_OP_GNU_push_tls_address.
03483      The operand represents the offset at which the variable is within
03484      the thread local storage.  */
03485 
03486   else if (data + 1 + addr_size < end
03487            && (data[0] == DW_OP_addr
03488                || (addr_size == 4 && data[0] == DW_OP_const4u)
03489                || (addr_size == 8 && data[0] == DW_OP_const8u))
03490            && data[1 + addr_size] == DW_OP_GNU_push_tls_address
03491            && piece_end_p (data + 2 + addr_size, end))
03492     {
03493       ULONGEST offset;
03494       offset = extract_unsigned_integer (data + 1, addr_size,
03495                                          gdbarch_byte_order (gdbarch));
03496 
03497       fprintf_filtered (stream, 
03498                         _("a thread-local variable at offset 0x%s "
03499                           "in the thread-local storage for `%s'"),
03500                         phex_nz (offset, addr_size), objfile_name (objfile));
03501 
03502       data += 1 + addr_size + 1;
03503     }
03504 
03505   /* With -gsplit-dwarf a TLS variable can also look like this:
03506      DW_AT_location    : 3 byte block: fc 4 e0
03507                         (DW_OP_GNU_const_index: 4;
03508                          DW_OP_GNU_push_tls_address)  */
03509   else if (data + 3 <= end
03510            && data + 1 + (leb128_size = skip_leb128 (data + 1, end)) < end
03511            && data[0] == DW_OP_GNU_const_index
03512            && leb128_size > 0
03513            && data[1 + leb128_size] == DW_OP_GNU_push_tls_address
03514            && piece_end_p (data + 2 + leb128_size, end))
03515     {
03516       uint64_t offset;
03517 
03518       data = safe_read_uleb128 (data + 1, end, &offset);
03519       offset = dwarf2_read_addr_index (per_cu, offset);
03520       fprintf_filtered (stream, 
03521                         _("a thread-local variable at offset 0x%s "
03522                           "in the thread-local storage for `%s'"),
03523                         phex_nz (offset, addr_size), objfile_name (objfile));
03524       ++data;
03525     }
03526 
03527   else if (data[0] >= DW_OP_lit0
03528            && data[0] <= DW_OP_lit31
03529            && data + 1 < end
03530            && data[1] == DW_OP_stack_value)
03531     {
03532       fprintf_filtered (stream, _("the constant %d"), data[0] - DW_OP_lit0);
03533       data += 2;
03534     }
03535 
03536   return data;
03537 }
03538 
03539 /* Disassemble an expression, stopping at the end of a piece or at the
03540    end of the expression.  Returns a pointer to the next unread byte
03541    in the input expression.  If ALL is nonzero, then this function
03542    will keep going until it reaches the end of the expression.
03543    If there is an error during reading, e.g. we run off the end
03544    of the buffer, an error is thrown.  */
03545 
03546 static const gdb_byte *
03547 disassemble_dwarf_expression (struct ui_file *stream,
03548                               struct gdbarch *arch, unsigned int addr_size,
03549                               int offset_size, const gdb_byte *start,
03550                               const gdb_byte *data, const gdb_byte *end,
03551                               int indent, int all,
03552                               struct dwarf2_per_cu_data *per_cu)
03553 {
03554   while (data < end
03555          && (all
03556              || (data[0] != DW_OP_piece && data[0] != DW_OP_bit_piece)))
03557     {
03558       enum dwarf_location_atom op = *data++;
03559       uint64_t ul;
03560       int64_t l;
03561       const char *name;
03562 
03563       name = get_DW_OP_name (op);
03564 
03565       if (!name)
03566         error (_("Unrecognized DWARF opcode 0x%02x at %ld"),
03567                op, (long) (data - 1 - start));
03568       fprintf_filtered (stream, "  %*ld: %s", indent + 4,
03569                         (long) (data - 1 - start), name);
03570 
03571       switch (op)
03572         {
03573         case DW_OP_addr:
03574           ul = extract_unsigned_integer (data, addr_size,
03575                                          gdbarch_byte_order (arch));
03576           data += addr_size;
03577           fprintf_filtered (stream, " 0x%s", phex_nz (ul, addr_size));
03578           break;
03579 
03580         case DW_OP_const1u:
03581           ul = extract_unsigned_integer (data, 1, gdbarch_byte_order (arch));
03582           data += 1;
03583           fprintf_filtered (stream, " %s", pulongest (ul));
03584           break;
03585         case DW_OP_const1s:
03586           l = extract_signed_integer (data, 1, gdbarch_byte_order (arch));
03587           data += 1;
03588           fprintf_filtered (stream, " %s", plongest (l));
03589           break;
03590         case DW_OP_const2u:
03591           ul = extract_unsigned_integer (data, 2, gdbarch_byte_order (arch));
03592           data += 2;
03593           fprintf_filtered (stream, " %s", pulongest (ul));
03594           break;
03595         case DW_OP_const2s:
03596           l = extract_signed_integer (data, 2, gdbarch_byte_order (arch));
03597           data += 2;
03598           fprintf_filtered (stream, " %s", plongest (l));
03599           break;
03600         case DW_OP_const4u:
03601           ul = extract_unsigned_integer (data, 4, gdbarch_byte_order (arch));
03602           data += 4;
03603           fprintf_filtered (stream, " %s", pulongest (ul));
03604           break;
03605         case DW_OP_const4s:
03606           l = extract_signed_integer (data, 4, gdbarch_byte_order (arch));
03607           data += 4;
03608           fprintf_filtered (stream, " %s", plongest (l));
03609           break;
03610         case DW_OP_const8u:
03611           ul = extract_unsigned_integer (data, 8, gdbarch_byte_order (arch));
03612           data += 8;
03613           fprintf_filtered (stream, " %s", pulongest (ul));
03614           break;
03615         case DW_OP_const8s:
03616           l = extract_signed_integer (data, 8, gdbarch_byte_order (arch));
03617           data += 8;
03618           fprintf_filtered (stream, " %s", plongest (l));
03619           break;
03620         case DW_OP_constu:
03621           data = safe_read_uleb128 (data, end, &ul);
03622           fprintf_filtered (stream, " %s", pulongest (ul));
03623           break;
03624         case DW_OP_consts:
03625           data = safe_read_sleb128 (data, end, &l);
03626           fprintf_filtered (stream, " %s", plongest (l));
03627           break;
03628 
03629         case DW_OP_reg0:
03630         case DW_OP_reg1:
03631         case DW_OP_reg2:
03632         case DW_OP_reg3:
03633         case DW_OP_reg4:
03634         case DW_OP_reg5:
03635         case DW_OP_reg6:
03636         case DW_OP_reg7:
03637         case DW_OP_reg8:
03638         case DW_OP_reg9:
03639         case DW_OP_reg10:
03640         case DW_OP_reg11:
03641         case DW_OP_reg12:
03642         case DW_OP_reg13:
03643         case DW_OP_reg14:
03644         case DW_OP_reg15:
03645         case DW_OP_reg16:
03646         case DW_OP_reg17:
03647         case DW_OP_reg18:
03648         case DW_OP_reg19:
03649         case DW_OP_reg20:
03650         case DW_OP_reg21:
03651         case DW_OP_reg22:
03652         case DW_OP_reg23:
03653         case DW_OP_reg24:
03654         case DW_OP_reg25:
03655         case DW_OP_reg26:
03656         case DW_OP_reg27:
03657         case DW_OP_reg28:
03658         case DW_OP_reg29:
03659         case DW_OP_reg30:
03660         case DW_OP_reg31:
03661           fprintf_filtered (stream, " [$%s]",
03662                             locexpr_regname (arch, op - DW_OP_reg0));
03663           break;
03664 
03665         case DW_OP_regx:
03666           data = safe_read_uleb128 (data, end, &ul);
03667           fprintf_filtered (stream, " %s [$%s]", pulongest (ul),
03668                             locexpr_regname (arch, (int) ul));
03669           break;
03670 
03671         case DW_OP_implicit_value:
03672           data = safe_read_uleb128 (data, end, &ul);
03673           data += ul;
03674           fprintf_filtered (stream, " %s", pulongest (ul));
03675           break;
03676 
03677         case DW_OP_breg0:
03678         case DW_OP_breg1:
03679         case DW_OP_breg2:
03680         case DW_OP_breg3:
03681         case DW_OP_breg4:
03682         case DW_OP_breg5:
03683         case DW_OP_breg6:
03684         case DW_OP_breg7:
03685         case DW_OP_breg8:
03686         case DW_OP_breg9:
03687         case DW_OP_breg10:
03688         case DW_OP_breg11:
03689         case DW_OP_breg12:
03690         case DW_OP_breg13:
03691         case DW_OP_breg14:
03692         case DW_OP_breg15:
03693         case DW_OP_breg16:
03694         case DW_OP_breg17:
03695         case DW_OP_breg18:
03696         case DW_OP_breg19:
03697         case DW_OP_breg20:
03698         case DW_OP_breg21:
03699         case DW_OP_breg22:
03700         case DW_OP_breg23:
03701         case DW_OP_breg24:
03702         case DW_OP_breg25:
03703         case DW_OP_breg26:
03704         case DW_OP_breg27:
03705         case DW_OP_breg28:
03706         case DW_OP_breg29:
03707         case DW_OP_breg30:
03708         case DW_OP_breg31:
03709           data = safe_read_sleb128 (data, end, &l);
03710           fprintf_filtered (stream, " %s [$%s]", plongest (l),
03711                             locexpr_regname (arch, op - DW_OP_breg0));
03712           break;
03713 
03714         case DW_OP_bregx:
03715           data = safe_read_uleb128 (data, end, &ul);
03716           data = safe_read_sleb128 (data, end, &l);
03717           fprintf_filtered (stream, " register %s [$%s] offset %s",
03718                             pulongest (ul),
03719                             locexpr_regname (arch, (int) ul),
03720                             plongest (l));
03721           break;
03722 
03723         case DW_OP_fbreg:
03724           data = safe_read_sleb128 (data, end, &l);
03725           fprintf_filtered (stream, " %s", plongest (l));
03726           break;
03727 
03728         case DW_OP_xderef_size:
03729         case DW_OP_deref_size:
03730         case DW_OP_pick:
03731           fprintf_filtered (stream, " %d", *data);
03732           ++data;
03733           break;
03734 
03735         case DW_OP_plus_uconst:
03736           data = safe_read_uleb128 (data, end, &ul);
03737           fprintf_filtered (stream, " %s", pulongest (ul));
03738           break;
03739 
03740         case DW_OP_skip:
03741           l = extract_signed_integer (data, 2, gdbarch_byte_order (arch));
03742           data += 2;
03743           fprintf_filtered (stream, " to %ld",
03744                             (long) (data + l - start));
03745           break;
03746 
03747         case DW_OP_bra:
03748           l = extract_signed_integer (data, 2, gdbarch_byte_order (arch));
03749           data += 2;
03750           fprintf_filtered (stream, " %ld",
03751                             (long) (data + l - start));
03752           break;
03753 
03754         case DW_OP_call2:
03755           ul = extract_unsigned_integer (data, 2, gdbarch_byte_order (arch));
03756           data += 2;
03757           fprintf_filtered (stream, " offset %s", phex_nz (ul, 2));
03758           break;
03759 
03760         case DW_OP_call4:
03761           ul = extract_unsigned_integer (data, 4, gdbarch_byte_order (arch));
03762           data += 4;
03763           fprintf_filtered (stream, " offset %s", phex_nz (ul, 4));
03764           break;
03765 
03766         case DW_OP_call_ref:
03767           ul = extract_unsigned_integer (data, offset_size,
03768                                          gdbarch_byte_order (arch));
03769           data += offset_size;
03770           fprintf_filtered (stream, " offset %s", phex_nz (ul, offset_size));
03771           break;
03772 
03773         case DW_OP_piece:
03774           data = safe_read_uleb128 (data, end, &ul);
03775           fprintf_filtered (stream, " %s (bytes)", pulongest (ul));
03776           break;
03777 
03778         case DW_OP_bit_piece:
03779           {
03780             uint64_t offset;
03781 
03782             data = safe_read_uleb128 (data, end, &ul);
03783             data = safe_read_uleb128 (data, end, &offset);
03784             fprintf_filtered (stream, " size %s offset %s (bits)",
03785                               pulongest (ul), pulongest (offset));
03786           }
03787           break;
03788 
03789         case DW_OP_GNU_implicit_pointer:
03790           {
03791             ul = extract_unsigned_integer (data, offset_size,
03792                                            gdbarch_byte_order (arch));
03793             data += offset_size;
03794 
03795             data = safe_read_sleb128 (data, end, &l);
03796 
03797             fprintf_filtered (stream, " DIE %s offset %s",
03798                               phex_nz (ul, offset_size),
03799                               plongest (l));
03800           }
03801           break;
03802 
03803         case DW_OP_GNU_deref_type:
03804           {
03805             int addr_size = *data++;
03806             cu_offset offset;
03807             struct type *type;
03808 
03809             data = safe_read_uleb128 (data, end, &ul);
03810             offset.cu_off = ul;
03811             type = dwarf2_get_die_type (offset, per_cu);
03812             fprintf_filtered (stream, "<");
03813             type_print (type, "", stream, -1);
03814             fprintf_filtered (stream, " [0x%s]> %d", phex_nz (offset.cu_off, 0),
03815                               addr_size);
03816           }
03817           break;
03818 
03819         case DW_OP_GNU_const_type:
03820           {
03821             cu_offset type_die;
03822             struct type *type;
03823 
03824             data = safe_read_uleb128 (data, end, &ul);
03825             type_die.cu_off = ul;
03826             type = dwarf2_get_die_type (type_die, per_cu);
03827             fprintf_filtered (stream, "<");
03828             type_print (type, "", stream, -1);
03829             fprintf_filtered (stream, " [0x%s]>", phex_nz (type_die.cu_off, 0));
03830           }
03831           break;
03832 
03833         case DW_OP_GNU_regval_type:
03834           {
03835             uint64_t reg;
03836             cu_offset type_die;
03837             struct type *type;
03838 
03839             data = safe_read_uleb128 (data, end, &reg);
03840             data = safe_read_uleb128 (data, end, &ul);
03841             type_die.cu_off = ul;
03842 
03843             type = dwarf2_get_die_type (type_die, per_cu);
03844             fprintf_filtered (stream, "<");
03845             type_print (type, "", stream, -1);
03846             fprintf_filtered (stream, " [0x%s]> [$%s]",
03847                               phex_nz (type_die.cu_off, 0),
03848                               locexpr_regname (arch, reg));
03849           }
03850           break;
03851 
03852         case DW_OP_GNU_convert:
03853         case DW_OP_GNU_reinterpret:
03854           {
03855             cu_offset type_die;
03856 
03857             data = safe_read_uleb128 (data, end, &ul);
03858             type_die.cu_off = ul;
03859 
03860             if (type_die.cu_off == 0)
03861               fprintf_filtered (stream, "<0>");
03862             else
03863               {
03864                 struct type *type;
03865 
03866                 type = dwarf2_get_die_type (type_die, per_cu);
03867                 fprintf_filtered (stream, "<");
03868                 type_print (type, "", stream, -1);
03869                 fprintf_filtered (stream, " [0x%s]>", phex_nz (type_die.cu_off, 0));
03870               }
03871           }
03872           break;
03873 
03874         case DW_OP_GNU_entry_value:
03875           data = safe_read_uleb128 (data, end, &ul);
03876           fputc_filtered ('\n', stream);
03877           disassemble_dwarf_expression (stream, arch, addr_size, offset_size,
03878                                         start, data, data + ul, indent + 2,
03879                                         all, per_cu);
03880           data += ul;
03881           continue;
03882 
03883         case DW_OP_GNU_parameter_ref:
03884           ul = extract_unsigned_integer (data, 4, gdbarch_byte_order (arch));
03885           data += 4;
03886           fprintf_filtered (stream, " offset %s", phex_nz (ul, 4));
03887           break;
03888 
03889         case DW_OP_GNU_addr_index:
03890           data = safe_read_uleb128 (data, end, &ul);
03891           ul = dwarf2_read_addr_index (per_cu, ul);
03892           fprintf_filtered (stream, " 0x%s", phex_nz (ul, addr_size));
03893           break;
03894         case DW_OP_GNU_const_index:
03895           data = safe_read_uleb128 (data, end, &ul);
03896           ul = dwarf2_read_addr_index (per_cu, ul);
03897           fprintf_filtered (stream, " %s", pulongest (ul));
03898           break;
03899         }
03900 
03901       fprintf_filtered (stream, "\n");
03902     }
03903 
03904   return data;
03905 }
03906 
03907 /* Describe a single location, which may in turn consist of multiple
03908    pieces.  */
03909 
03910 static void
03911 locexpr_describe_location_1 (struct symbol *symbol, CORE_ADDR addr,
03912                              struct ui_file *stream,
03913                              const gdb_byte *data, size_t size,
03914                              struct objfile *objfile, unsigned int addr_size,
03915                              int offset_size, struct dwarf2_per_cu_data *per_cu)
03916 {
03917   const gdb_byte *end = data + size;
03918   int first_piece = 1, bad = 0;
03919 
03920   while (data < end)
03921     {
03922       const gdb_byte *here = data;
03923       int disassemble = 1;
03924 
03925       if (first_piece)
03926         first_piece = 0;
03927       else
03928         fprintf_filtered (stream, _(", and "));
03929 
03930       if (!dwarf2_always_disassemble)
03931         {
03932           data = locexpr_describe_location_piece (symbol, stream,
03933                                                   addr, objfile, per_cu,
03934                                                   data, end, addr_size);
03935           /* If we printed anything, or if we have an empty piece,
03936              then don't disassemble.  */
03937           if (data != here
03938               || data[0] == DW_OP_piece
03939               || data[0] == DW_OP_bit_piece)
03940             disassemble = 0;
03941         }
03942       if (disassemble)
03943         {
03944           fprintf_filtered (stream, _("a complex DWARF expression:\n"));
03945           data = disassemble_dwarf_expression (stream,
03946                                                get_objfile_arch (objfile),
03947                                                addr_size, offset_size, data,
03948                                                data, end, 0,
03949                                                dwarf2_always_disassemble,
03950                                                per_cu);
03951         }
03952 
03953       if (data < end)
03954         {
03955           int empty = data == here;
03956               
03957           if (disassemble)
03958             fprintf_filtered (stream, "   ");
03959           if (data[0] == DW_OP_piece)
03960             {
03961               uint64_t bytes;
03962 
03963               data = safe_read_uleb128 (data + 1, end, &bytes);
03964 
03965               if (empty)
03966                 fprintf_filtered (stream, _("an empty %s-byte piece"),
03967                                   pulongest (bytes));
03968               else
03969                 fprintf_filtered (stream, _(" [%s-byte piece]"),
03970                                   pulongest (bytes));
03971             }
03972           else if (data[0] == DW_OP_bit_piece)
03973             {
03974               uint64_t bits, offset;
03975 
03976               data = safe_read_uleb128 (data + 1, end, &bits);
03977               data = safe_read_uleb128 (data, end, &offset);
03978 
03979               if (empty)
03980                 fprintf_filtered (stream,
03981                                   _("an empty %s-bit piece"),
03982                                   pulongest (bits));
03983               else
03984                 fprintf_filtered (stream,
03985                                   _(" [%s-bit piece, offset %s bits]"),
03986                                   pulongest (bits), pulongest (offset));
03987             }
03988           else
03989             {
03990               bad = 1;
03991               break;
03992             }
03993         }
03994     }
03995 
03996   if (bad || data > end)
03997     error (_("Corrupted DWARF2 expression for \"%s\"."),
03998            SYMBOL_PRINT_NAME (symbol));
03999 }
04000 
04001 /* Print a natural-language description of SYMBOL to STREAM.  This
04002    version is for a symbol with a single location.  */
04003 
04004 static void
04005 locexpr_describe_location (struct symbol *symbol, CORE_ADDR addr,
04006                            struct ui_file *stream)
04007 {
04008   struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
04009   struct objfile *objfile = dwarf2_per_cu_objfile (dlbaton->per_cu);
04010   unsigned int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);
04011   int offset_size = dwarf2_per_cu_offset_size (dlbaton->per_cu);
04012 
04013   locexpr_describe_location_1 (symbol, addr, stream,
04014                                dlbaton->data, dlbaton->size,
04015                                objfile, addr_size, offset_size,
04016                                dlbaton->per_cu);
04017 }
04018 
04019 /* Describe the location of SYMBOL as an agent value in VALUE, generating
04020    any necessary bytecode in AX.  */
04021 
04022 static void
04023 locexpr_tracepoint_var_ref (struct symbol *symbol, struct gdbarch *gdbarch,
04024                             struct agent_expr *ax, struct axs_value *value)
04025 {
04026   struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
04027   unsigned int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);
04028 
04029   if (dlbaton->size == 0)
04030     value->optimized_out = 1;
04031   else
04032     dwarf2_compile_expr_to_ax (ax, value, gdbarch, addr_size,
04033                                dlbaton->data, dlbaton->data + dlbaton->size,
04034                                dlbaton->per_cu);
04035 }
04036 
04037 /* The set of location functions used with the DWARF-2 expression
04038    evaluator.  */
04039 const struct symbol_computed_ops dwarf2_locexpr_funcs = {
04040   locexpr_read_variable,
04041   locexpr_read_variable_at_entry,
04042   locexpr_read_needs_frame,
04043   locexpr_describe_location,
04044   0,    /* location_has_loclist */
04045   locexpr_tracepoint_var_ref
04046 };
04047 
04048 
04049 /* Wrapper functions for location lists.  These generally find
04050    the appropriate location expression and call something above.  */
04051 
04052 /* Return the value of SYMBOL in FRAME using the DWARF-2 expression
04053    evaluator to calculate the location.  */
04054 static struct value *
04055 loclist_read_variable (struct symbol *symbol, struct frame_info *frame)
04056 {
04057   struct dwarf2_loclist_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
04058   struct value *val;
04059   const gdb_byte *data;
04060   size_t size;
04061   CORE_ADDR pc = frame ? get_frame_address_in_block (frame) : 0;
04062 
04063   data = dwarf2_find_location_expression (dlbaton, &size, pc);
04064   val = dwarf2_evaluate_loc_desc (SYMBOL_TYPE (symbol), frame, data, size,
04065                                   dlbaton->per_cu);
04066 
04067   return val;
04068 }
04069 
04070 /* Read variable SYMBOL like loclist_read_variable at (callee) FRAME's function
04071    entry.  SYMBOL should be a function parameter, otherwise NO_ENTRY_VALUE_ERROR
04072    will be thrown.
04073 
04074    Function always returns non-NULL value, it may be marked optimized out if
04075    inferior frame information is not available.  It throws NO_ENTRY_VALUE_ERROR
04076    if it cannot resolve the parameter for any reason.  */
04077 
04078 static struct value *
04079 loclist_read_variable_at_entry (struct symbol *symbol, struct frame_info *frame)
04080 {
04081   struct dwarf2_loclist_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
04082   const gdb_byte *data;
04083   size_t size;
04084   CORE_ADDR pc;
04085 
04086   if (frame == NULL || !get_frame_func_if_available (frame, &pc))
04087     return allocate_optimized_out_value (SYMBOL_TYPE (symbol));
04088 
04089   data = dwarf2_find_location_expression (dlbaton, &size, pc);
04090   if (data == NULL)
04091     return allocate_optimized_out_value (SYMBOL_TYPE (symbol));
04092 
04093   return value_of_dwarf_block_entry (SYMBOL_TYPE (symbol), frame, data, size);
04094 }
04095 
04096 /* Return non-zero iff we need a frame to evaluate SYMBOL.  */
04097 static int
04098 loclist_read_needs_frame (struct symbol *symbol)
04099 {
04100   /* If there's a location list, then assume we need to have a frame
04101      to choose the appropriate location expression.  With tracking of
04102      global variables this is not necessarily true, but such tracking
04103      is disabled in GCC at the moment until we figure out how to
04104      represent it.  */
04105 
04106   return 1;
04107 }
04108 
04109 /* Print a natural-language description of SYMBOL to STREAM.  This
04110    version applies when there is a list of different locations, each
04111    with a specified address range.  */
04112 
04113 static void
04114 loclist_describe_location (struct symbol *symbol, CORE_ADDR addr,
04115                            struct ui_file *stream)
04116 {
04117   struct dwarf2_loclist_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
04118   const gdb_byte *loc_ptr, *buf_end;
04119   struct objfile *objfile = dwarf2_per_cu_objfile (dlbaton->per_cu);
04120   struct gdbarch *gdbarch = get_objfile_arch (objfile);
04121   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
04122   unsigned int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);
04123   int offset_size = dwarf2_per_cu_offset_size (dlbaton->per_cu);
04124   int signed_addr_p = bfd_get_sign_extend_vma (objfile->obfd);
04125   /* Adjust base_address for relocatable objects.  */
04126   CORE_ADDR base_offset = dwarf2_per_cu_text_offset (dlbaton->per_cu);
04127   CORE_ADDR base_address = dlbaton->base_address + base_offset;
04128   int done = 0;
04129 
04130   loc_ptr = dlbaton->data;
04131   buf_end = dlbaton->data + dlbaton->size;
04132 
04133   fprintf_filtered (stream, _("multi-location:\n"));
04134 
04135   /* Iterate through locations until we run out.  */
04136   while (!done)
04137     {
04138       CORE_ADDR low = 0, high = 0; /* init for gcc -Wall */
04139       int length;
04140       enum debug_loc_kind kind;
04141       const gdb_byte *new_ptr = NULL; /* init for gcc -Wall */
04142 
04143       if (dlbaton->from_dwo)
04144         kind = decode_debug_loc_dwo_addresses (dlbaton->per_cu,
04145                                                loc_ptr, buf_end, &new_ptr,
04146                                                &low, &high, byte_order);
04147       else
04148         kind = decode_debug_loc_addresses (loc_ptr, buf_end, &new_ptr,
04149                                            &low, &high,
04150                                            byte_order, addr_size,
04151                                            signed_addr_p);
04152       loc_ptr = new_ptr;
04153       switch (kind)
04154         {
04155         case DEBUG_LOC_END_OF_LIST:
04156           done = 1;
04157           continue;
04158         case DEBUG_LOC_BASE_ADDRESS:
04159           base_address = high + base_offset;
04160           fprintf_filtered (stream, _("  Base address %s"),
04161                             paddress (gdbarch, base_address));
04162           continue;
04163         case DEBUG_LOC_START_END:
04164         case DEBUG_LOC_START_LENGTH:
04165           break;
04166         case DEBUG_LOC_BUFFER_OVERFLOW:
04167         case DEBUG_LOC_INVALID_ENTRY:
04168           error (_("Corrupted DWARF expression for symbol \"%s\"."),
04169                  SYMBOL_PRINT_NAME (symbol));
04170         default:
04171           gdb_assert_not_reached ("bad debug_loc_kind");
04172         }
04173 
04174       /* Otherwise, a location expression entry.  */
04175       low += base_address;
04176       high += base_address;
04177 
04178       length = extract_unsigned_integer (loc_ptr, 2, byte_order);
04179       loc_ptr += 2;
04180 
04181       /* (It would improve readability to print only the minimum
04182          necessary digits of the second number of the range.)  */
04183       fprintf_filtered (stream, _("  Range %s-%s: "),
04184                         paddress (gdbarch, low), paddress (gdbarch, high));
04185 
04186       /* Now describe this particular location.  */
04187       locexpr_describe_location_1 (symbol, low, stream, loc_ptr, length,
04188                                    objfile, addr_size, offset_size,
04189                                    dlbaton->per_cu);
04190 
04191       fprintf_filtered (stream, "\n");
04192 
04193       loc_ptr += length;
04194     }
04195 }
04196 
04197 /* Describe the location of SYMBOL as an agent value in VALUE, generating
04198    any necessary bytecode in AX.  */
04199 static void
04200 loclist_tracepoint_var_ref (struct symbol *symbol, struct gdbarch *gdbarch,
04201                             struct agent_expr *ax, struct axs_value *value)
04202 {
04203   struct dwarf2_loclist_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
04204   const gdb_byte *data;
04205   size_t size;
04206   unsigned int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);
04207 
04208   data = dwarf2_find_location_expression (dlbaton, &size, ax->scope);
04209   if (size == 0)
04210     value->optimized_out = 1;
04211   else
04212     dwarf2_compile_expr_to_ax (ax, value, gdbarch, addr_size, data, data + size,
04213                                dlbaton->per_cu);
04214 }
04215 
04216 /* The set of location functions used with the DWARF-2 expression
04217    evaluator and location lists.  */
04218 const struct symbol_computed_ops dwarf2_loclist_funcs = {
04219   loclist_read_variable,
04220   loclist_read_variable_at_entry,
04221   loclist_read_needs_frame,
04222   loclist_describe_location,
04223   1,    /* location_has_loclist */
04224   loclist_tracepoint_var_ref
04225 };
04226 
04227 /* Provide a prototype to silence -Wmissing-prototypes.  */
04228 extern initialize_file_ftype _initialize_dwarf2loc;
04229 
04230 void
04231 _initialize_dwarf2loc (void)
04232 {
04233   add_setshow_zuinteger_cmd ("entry-values", class_maintenance,
04234                              &entry_values_debug,
04235                              _("Set entry values and tail call frames "
04236                                "debugging."),
04237                              _("Show entry values and tail call frames "
04238                                "debugging."),
04239                              _("When non-zero, the process of determining "
04240                                "parameter values from function entry point "
04241                                "and tail call frames will be printed."),
04242                              NULL,
04243                              show_entry_values_debug,
04244                              &setdebuglist, &showdebuglist);
04245 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines