* minsyms.h (struct bound_minimal_symbol): New.
[platform/upstream/binutils.git] / gdb / dwarf2loc.c
1 /* DWARF 2 location expression support for GDB.
2
3    Copyright (C) 2003-2013 Free Software Foundation, Inc.
4
5    Contributed by Daniel Jacobowitz, MontaVista Software, Inc.
6
7    This file is part of GDB.
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
21
22 #include "defs.h"
23 #include "ui-out.h"
24 #include "value.h"
25 #include "frame.h"
26 #include "gdbcore.h"
27 #include "target.h"
28 #include "inferior.h"
29 #include "ax.h"
30 #include "ax-gdb.h"
31 #include "regcache.h"
32 #include "objfiles.h"
33 #include "exceptions.h"
34 #include "block.h"
35 #include "gdbcmd.h"
36
37 #include "dwarf2.h"
38 #include "dwarf2expr.h"
39 #include "dwarf2loc.h"
40 #include "dwarf2-frame.h"
41
42 #include "gdb_string.h"
43 #include "gdb_assert.h"
44
45 DEF_VEC_I(int);
46
47 extern int dwarf2_always_disassemble;
48
49 static void dwarf_expr_frame_base_1 (struct symbol *framefunc, CORE_ADDR pc,
50                                      const gdb_byte **start, size_t *length);
51
52 static const struct dwarf_expr_context_funcs dwarf_expr_ctx_funcs;
53
54 static struct value *dwarf2_evaluate_loc_desc_full (struct type *type,
55                                                     struct frame_info *frame,
56                                                     const gdb_byte *data,
57                                                     size_t size,
58                                                     struct dwarf2_per_cu_data *per_cu,
59                                                     LONGEST byte_offset);
60
61 /* Until these have formal names, we define these here.
62    ref: http://gcc.gnu.org/wiki/DebugFission
63    Each entry in .debug_loc.dwo begins with a byte that describes the entry,
64    and is then followed by data specific to that entry.  */
65
66 enum debug_loc_kind
67 {
68   /* Indicates the end of the list of entries.  */
69   DEBUG_LOC_END_OF_LIST = 0,
70
71   /* This is followed by an unsigned LEB128 number that is an index into
72      .debug_addr and specifies the base address for all following entries.  */
73   DEBUG_LOC_BASE_ADDRESS = 1,
74
75   /* This is followed by two unsigned LEB128 numbers that are indices into
76      .debug_addr and specify the beginning and ending addresses, and then
77      a normal location expression as in .debug_loc.  */
78   DEBUG_LOC_START_END = 2,
79
80   /* This is followed by an unsigned LEB128 number that is an index into
81      .debug_addr and specifies the beginning address, and a 4 byte unsigned
82      number that specifies the length, and then a normal location expression
83      as in .debug_loc.  */
84   DEBUG_LOC_START_LENGTH = 3,
85
86   /* An internal value indicating there is insufficient data.  */
87   DEBUG_LOC_BUFFER_OVERFLOW = -1,
88
89   /* An internal value indicating an invalid kind of entry was found.  */
90   DEBUG_LOC_INVALID_ENTRY = -2
91 };
92
93 /* Decode the addresses in a non-dwo .debug_loc entry.
94    A pointer to the next byte to examine is returned in *NEW_PTR.
95    The encoded low,high addresses are return in *LOW,*HIGH.
96    The result indicates the kind of entry found.  */
97
98 static enum debug_loc_kind
99 decode_debug_loc_addresses (const gdb_byte *loc_ptr, const gdb_byte *buf_end,
100                             const gdb_byte **new_ptr,
101                             CORE_ADDR *low, CORE_ADDR *high,
102                             enum bfd_endian byte_order,
103                             unsigned int addr_size,
104                             int signed_addr_p)
105 {
106   CORE_ADDR base_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
107
108   if (buf_end - loc_ptr < 2 * addr_size)
109     return DEBUG_LOC_BUFFER_OVERFLOW;
110
111   if (signed_addr_p)
112     *low = extract_signed_integer (loc_ptr, addr_size, byte_order);
113   else
114     *low = extract_unsigned_integer (loc_ptr, addr_size, byte_order);
115   loc_ptr += addr_size;
116
117   if (signed_addr_p)
118     *high = extract_signed_integer (loc_ptr, addr_size, byte_order);
119   else
120     *high = extract_unsigned_integer (loc_ptr, addr_size, byte_order);
121   loc_ptr += addr_size;
122
123   *new_ptr = loc_ptr;
124
125   /* A base-address-selection entry.  */
126   if ((*low & base_mask) == base_mask)
127     return DEBUG_LOC_BASE_ADDRESS;
128
129   /* An end-of-list entry.  */
130   if (*low == 0 && *high == 0)
131     return DEBUG_LOC_END_OF_LIST;
132
133   return DEBUG_LOC_START_END;
134 }
135
136 /* Decode the addresses in .debug_loc.dwo entry.
137    A pointer to the next byte to examine is returned in *NEW_PTR.
138    The encoded low,high addresses are return in *LOW,*HIGH.
139    The result indicates the kind of entry found.  */
140
141 static enum debug_loc_kind
142 decode_debug_loc_dwo_addresses (struct dwarf2_per_cu_data *per_cu,
143                                 const gdb_byte *loc_ptr,
144                                 const gdb_byte *buf_end,
145                                 const gdb_byte **new_ptr,
146                                 CORE_ADDR *low, CORE_ADDR *high,
147                                 enum bfd_endian byte_order)
148 {
149   uint64_t low_index, high_index;
150
151   if (loc_ptr == buf_end)
152     return DEBUG_LOC_BUFFER_OVERFLOW;
153
154   switch (*loc_ptr++)
155     {
156     case DEBUG_LOC_END_OF_LIST:
157       *new_ptr = loc_ptr;
158       return DEBUG_LOC_END_OF_LIST;
159     case DEBUG_LOC_BASE_ADDRESS:
160       *low = 0;
161       loc_ptr = gdb_read_uleb128 (loc_ptr, buf_end, &high_index);
162       if (loc_ptr == NULL)
163         return DEBUG_LOC_BUFFER_OVERFLOW;
164       *high = dwarf2_read_addr_index (per_cu, high_index);
165       *new_ptr = loc_ptr;
166       return DEBUG_LOC_BASE_ADDRESS;
167     case DEBUG_LOC_START_END:
168       loc_ptr = gdb_read_uleb128 (loc_ptr, buf_end, &low_index);
169       if (loc_ptr == NULL)
170         return DEBUG_LOC_BUFFER_OVERFLOW;
171       *low = dwarf2_read_addr_index (per_cu, low_index);
172       loc_ptr = gdb_read_uleb128 (loc_ptr, buf_end, &high_index);
173       if (loc_ptr == NULL)
174         return DEBUG_LOC_BUFFER_OVERFLOW;
175       *high = dwarf2_read_addr_index (per_cu, high_index);
176       *new_ptr = loc_ptr;
177       return DEBUG_LOC_START_END;
178     case DEBUG_LOC_START_LENGTH:
179       loc_ptr = gdb_read_uleb128 (loc_ptr, buf_end, &low_index);
180       if (loc_ptr == NULL)
181         return DEBUG_LOC_BUFFER_OVERFLOW;
182       *low = dwarf2_read_addr_index (per_cu, low_index);
183       if (loc_ptr + 4 > buf_end)
184         return DEBUG_LOC_BUFFER_OVERFLOW;
185       *high = *low;
186       *high += extract_unsigned_integer (loc_ptr, 4, byte_order);
187       *new_ptr = loc_ptr + 4;
188       return DEBUG_LOC_START_LENGTH;
189     default:
190       return DEBUG_LOC_INVALID_ENTRY;
191     }
192 }
193
194 /* A function for dealing with location lists.  Given a
195    symbol baton (BATON) and a pc value (PC), find the appropriate
196    location expression, set *LOCEXPR_LENGTH, and return a pointer
197    to the beginning of the expression.  Returns NULL on failure.
198
199    For now, only return the first matching location expression; there
200    can be more than one in the list.  */
201
202 const gdb_byte *
203 dwarf2_find_location_expression (struct dwarf2_loclist_baton *baton,
204                                  size_t *locexpr_length, CORE_ADDR pc)
205 {
206   struct objfile *objfile = dwarf2_per_cu_objfile (baton->per_cu);
207   struct gdbarch *gdbarch = get_objfile_arch (objfile);
208   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
209   unsigned int addr_size = dwarf2_per_cu_addr_size (baton->per_cu);
210   int signed_addr_p = bfd_get_sign_extend_vma (objfile->obfd);
211   /* Adjust base_address for relocatable objects.  */
212   CORE_ADDR base_offset = dwarf2_per_cu_text_offset (baton->per_cu);
213   CORE_ADDR base_address = baton->base_address + base_offset;
214   const gdb_byte *loc_ptr, *buf_end;
215
216   loc_ptr = baton->data;
217   buf_end = baton->data + baton->size;
218
219   while (1)
220     {
221       CORE_ADDR low = 0, high = 0; /* init for gcc -Wall */
222       int length;
223       enum debug_loc_kind kind;
224       const gdb_byte *new_ptr = NULL; /* init for gcc -Wall */
225
226       if (baton->from_dwo)
227         kind = decode_debug_loc_dwo_addresses (baton->per_cu,
228                                                loc_ptr, buf_end, &new_ptr,
229                                                &low, &high, byte_order);
230       else
231         kind = decode_debug_loc_addresses (loc_ptr, buf_end, &new_ptr,
232                                            &low, &high,
233                                            byte_order, addr_size,
234                                            signed_addr_p);
235       loc_ptr = new_ptr;
236       switch (kind)
237         {
238         case DEBUG_LOC_END_OF_LIST:
239           *locexpr_length = 0;
240           return NULL;
241         case DEBUG_LOC_BASE_ADDRESS:
242           base_address = high + base_offset;
243           continue;
244         case DEBUG_LOC_START_END:
245         case DEBUG_LOC_START_LENGTH:
246           break;
247         case DEBUG_LOC_BUFFER_OVERFLOW:
248         case DEBUG_LOC_INVALID_ENTRY:
249           error (_("dwarf2_find_location_expression: "
250                    "Corrupted DWARF expression."));
251         default:
252           gdb_assert_not_reached ("bad debug_loc_kind");
253         }
254
255       /* Otherwise, a location expression entry.
256          If the entry is from a DWO, don't add base address: the entry is
257          from .debug_addr which has absolute addresses.  */
258       if (! baton->from_dwo)
259         {
260           low += base_address;
261           high += base_address;
262         }
263
264       length = extract_unsigned_integer (loc_ptr, 2, byte_order);
265       loc_ptr += 2;
266
267       if (low == high && pc == low)
268         {
269           /* This is entry PC record present only at entry point
270              of a function.  Verify it is really the function entry point.  */
271
272           struct block *pc_block = block_for_pc (pc);
273           struct symbol *pc_func = NULL;
274
275           if (pc_block)
276             pc_func = block_linkage_function (pc_block);
277
278           if (pc_func && pc == BLOCK_START (SYMBOL_BLOCK_VALUE (pc_func)))
279             {
280               *locexpr_length = length;
281               return loc_ptr;
282             }
283         }
284
285       if (pc >= low && pc < high)
286         {
287           *locexpr_length = length;
288           return loc_ptr;
289         }
290
291       loc_ptr += length;
292     }
293 }
294
295 /* This is the baton used when performing dwarf2 expression
296    evaluation.  */
297 struct dwarf_expr_baton
298 {
299   struct frame_info *frame;
300   struct dwarf2_per_cu_data *per_cu;
301 };
302
303 /* Helper functions for dwarf2_evaluate_loc_desc.  */
304
305 /* Using the frame specified in BATON, return the value of register
306    REGNUM, treated as a pointer.  */
307 static CORE_ADDR
308 dwarf_expr_read_reg (void *baton, int dwarf_regnum)
309 {
310   struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
311   struct gdbarch *gdbarch = get_frame_arch (debaton->frame);
312   CORE_ADDR result;
313   int regnum;
314
315   regnum = gdbarch_dwarf2_reg_to_regnum (gdbarch, dwarf_regnum);
316   result = address_from_register (builtin_type (gdbarch)->builtin_data_ptr,
317                                   regnum, debaton->frame);
318   return result;
319 }
320
321 /* Read memory at ADDR (length LEN) into BUF.  */
322
323 static void
324 dwarf_expr_read_mem (void *baton, gdb_byte *buf, CORE_ADDR addr, size_t len)
325 {
326   read_memory (addr, buf, len);
327 }
328
329 /* Using the frame specified in BATON, find the location expression
330    describing the frame base.  Return a pointer to it in START and
331    its length in LENGTH.  */
332 static void
333 dwarf_expr_frame_base (void *baton, const gdb_byte **start, size_t * length)
334 {
335   /* FIXME: cagney/2003-03-26: This code should be using
336      get_frame_base_address(), and then implement a dwarf2 specific
337      this_base method.  */
338   struct symbol *framefunc;
339   struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
340   struct block *bl = get_frame_block (debaton->frame, NULL);
341
342   if (bl == NULL)
343     error (_("frame address is not available."));
344
345   /* Use block_linkage_function, which returns a real (not inlined)
346      function, instead of get_frame_function, which may return an
347      inlined function.  */
348   framefunc = block_linkage_function (bl);
349
350   /* If we found a frame-relative symbol then it was certainly within
351      some function associated with a frame. If we can't find the frame,
352      something has gone wrong.  */
353   gdb_assert (framefunc != NULL);
354
355   dwarf_expr_frame_base_1 (framefunc,
356                            get_frame_address_in_block (debaton->frame),
357                            start, length);
358 }
359
360 /* Implement find_frame_base_location method for LOC_BLOCK functions using
361    DWARF expression for its DW_AT_frame_base.  */
362
363 static void
364 locexpr_find_frame_base_location (struct symbol *framefunc, CORE_ADDR pc,
365                                   const gdb_byte **start, size_t *length)
366 {
367   struct dwarf2_locexpr_baton *symbaton = SYMBOL_LOCATION_BATON (framefunc);
368
369   *length = symbaton->size;
370   *start = symbaton->data;
371 }
372
373 /* Vector for inferior functions as represented by LOC_BLOCK, if the inferior
374    function uses DWARF expression for its DW_AT_frame_base.  */
375
376 const struct symbol_block_ops dwarf2_block_frame_base_locexpr_funcs =
377 {
378   locexpr_find_frame_base_location
379 };
380
381 /* Implement find_frame_base_location method for LOC_BLOCK functions using
382    DWARF location list for its DW_AT_frame_base.  */
383
384 static void
385 loclist_find_frame_base_location (struct symbol *framefunc, CORE_ADDR pc,
386                                   const gdb_byte **start, size_t *length)
387 {
388   struct dwarf2_loclist_baton *symbaton = SYMBOL_LOCATION_BATON (framefunc);
389
390   *start = dwarf2_find_location_expression (symbaton, length, pc);
391 }
392
393 /* Vector for inferior functions as represented by LOC_BLOCK, if the inferior
394    function uses DWARF location list for its DW_AT_frame_base.  */
395
396 const struct symbol_block_ops dwarf2_block_frame_base_loclist_funcs =
397 {
398   loclist_find_frame_base_location
399 };
400
401 static void
402 dwarf_expr_frame_base_1 (struct symbol *framefunc, CORE_ADDR pc,
403                          const gdb_byte **start, size_t *length)
404 {
405   if (SYMBOL_BLOCK_OPS (framefunc) != NULL)
406     {
407       const struct symbol_block_ops *ops_block = SYMBOL_BLOCK_OPS (framefunc);
408
409       ops_block->find_frame_base_location (framefunc, pc, start, length);
410     }
411   else
412     *length = 0;
413
414   if (*length == 0)
415     error (_("Could not find the frame base for \"%s\"."),
416            SYMBOL_NATURAL_NAME (framefunc));
417 }
418
419 /* Helper function for dwarf2_evaluate_loc_desc.  Computes the CFA for
420    the frame in BATON.  */
421
422 static CORE_ADDR
423 dwarf_expr_frame_cfa (void *baton)
424 {
425   struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
426
427   return dwarf2_frame_cfa (debaton->frame);
428 }
429
430 /* Helper function for dwarf2_evaluate_loc_desc.  Computes the PC for
431    the frame in BATON.  */
432
433 static CORE_ADDR
434 dwarf_expr_frame_pc (void *baton)
435 {
436   struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
437
438   return get_frame_address_in_block (debaton->frame);
439 }
440
441 /* Using the objfile specified in BATON, find the address for the
442    current thread's thread-local storage with offset OFFSET.  */
443 static CORE_ADDR
444 dwarf_expr_tls_address (void *baton, CORE_ADDR offset)
445 {
446   struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
447   struct objfile *objfile = dwarf2_per_cu_objfile (debaton->per_cu);
448
449   return target_translate_tls_address (objfile, offset);
450 }
451
452 /* Call DWARF subroutine from DW_AT_location of DIE at DIE_OFFSET in
453    current CU (as is PER_CU).  State of the CTX is not affected by the
454    call and return.  */
455
456 static void
457 per_cu_dwarf_call (struct dwarf_expr_context *ctx, cu_offset die_offset,
458                    struct dwarf2_per_cu_data *per_cu,
459                    CORE_ADDR (*get_frame_pc) (void *baton),
460                    void *baton)
461 {
462   struct dwarf2_locexpr_baton block;
463
464   block = dwarf2_fetch_die_loc_cu_off (die_offset, per_cu, get_frame_pc, baton);
465
466   /* DW_OP_call_ref is currently not supported.  */
467   gdb_assert (block.per_cu == per_cu);
468
469   dwarf_expr_eval (ctx, block.data, block.size);
470 }
471
472 /* Helper interface of per_cu_dwarf_call for dwarf2_evaluate_loc_desc.  */
473
474 static void
475 dwarf_expr_dwarf_call (struct dwarf_expr_context *ctx, cu_offset die_offset)
476 {
477   struct dwarf_expr_baton *debaton = ctx->baton;
478
479   per_cu_dwarf_call (ctx, die_offset, debaton->per_cu,
480                      ctx->funcs->get_frame_pc, ctx->baton);
481 }
482
483 /* Callback function for dwarf2_evaluate_loc_desc.  */
484
485 static struct type *
486 dwarf_expr_get_base_type (struct dwarf_expr_context *ctx,
487                           cu_offset die_offset)
488 {
489   struct dwarf_expr_baton *debaton = ctx->baton;
490
491   return dwarf2_get_die_type (die_offset, debaton->per_cu);
492 }
493
494 /* See dwarf2loc.h.  */
495
496 unsigned int entry_values_debug = 0;
497
498 /* Helper to set entry_values_debug.  */
499
500 static void
501 show_entry_values_debug (struct ui_file *file, int from_tty,
502                          struct cmd_list_element *c, const char *value)
503 {
504   fprintf_filtered (file,
505                     _("Entry values and tail call frames debugging is %s.\n"),
506                     value);
507 }
508
509 /* Find DW_TAG_GNU_call_site's DW_AT_GNU_call_site_target address.
510    CALLER_FRAME (for registers) can be NULL if it is not known.  This function
511    always returns valid address or it throws NO_ENTRY_VALUE_ERROR.  */
512
513 static CORE_ADDR
514 call_site_to_target_addr (struct gdbarch *call_site_gdbarch,
515                           struct call_site *call_site,
516                           struct frame_info *caller_frame)
517 {
518   switch (FIELD_LOC_KIND (call_site->target))
519     {
520     case FIELD_LOC_KIND_DWARF_BLOCK:
521       {
522         struct dwarf2_locexpr_baton *dwarf_block;
523         struct value *val;
524         struct type *caller_core_addr_type;
525         struct gdbarch *caller_arch;
526
527         dwarf_block = FIELD_DWARF_BLOCK (call_site->target);
528         if (dwarf_block == NULL)
529           {
530             struct bound_minimal_symbol msym;
531             
532             msym = lookup_minimal_symbol_by_pc (call_site->pc - 1);
533             throw_error (NO_ENTRY_VALUE_ERROR,
534                          _("DW_AT_GNU_call_site_target is not specified "
535                            "at %s in %s"),
536                          paddress (call_site_gdbarch, call_site->pc),
537                          (msym.minsym == NULL ? "???"
538                           : SYMBOL_PRINT_NAME (msym.minsym)));
539                         
540           }
541         if (caller_frame == NULL)
542           {
543             struct bound_minimal_symbol msym;
544             
545             msym = lookup_minimal_symbol_by_pc (call_site->pc - 1);
546             throw_error (NO_ENTRY_VALUE_ERROR,
547                          _("DW_AT_GNU_call_site_target DWARF block resolving "
548                            "requires known frame which is currently not "
549                            "available at %s in %s"),
550                          paddress (call_site_gdbarch, call_site->pc),
551                          (msym.minsym == NULL ? "???"
552                           : SYMBOL_PRINT_NAME (msym.minsym)));
553                         
554           }
555         caller_arch = get_frame_arch (caller_frame);
556         caller_core_addr_type = builtin_type (caller_arch)->builtin_func_ptr;
557         val = dwarf2_evaluate_loc_desc (caller_core_addr_type, caller_frame,
558                                         dwarf_block->data, dwarf_block->size,
559                                         dwarf_block->per_cu);
560         /* DW_AT_GNU_call_site_target is a DWARF expression, not a DWARF
561            location.  */
562         if (VALUE_LVAL (val) == lval_memory)
563           return value_address (val);
564         else
565           return value_as_address (val);
566       }
567
568     case FIELD_LOC_KIND_PHYSNAME:
569       {
570         const char *physname;
571         struct minimal_symbol *msym;
572
573         physname = FIELD_STATIC_PHYSNAME (call_site->target);
574
575         /* Handle both the mangled and demangled PHYSNAME.  */
576         msym = lookup_minimal_symbol (physname, NULL, NULL);
577         if (msym == NULL)
578           {
579             msym = lookup_minimal_symbol_by_pc (call_site->pc - 1).minsym;
580             throw_error (NO_ENTRY_VALUE_ERROR,
581                          _("Cannot find function \"%s\" for a call site target "
582                            "at %s in %s"),
583                          physname, paddress (call_site_gdbarch, call_site->pc),
584                          msym == NULL ? "???" : SYMBOL_PRINT_NAME (msym));
585                         
586           }
587         return SYMBOL_VALUE_ADDRESS (msym);
588       }
589
590     case FIELD_LOC_KIND_PHYSADDR:
591       return FIELD_STATIC_PHYSADDR (call_site->target);
592
593     default:
594       internal_error (__FILE__, __LINE__, _("invalid call site target kind"));
595     }
596 }
597
598 /* Convert function entry point exact address ADDR to the function which is
599    compliant with TAIL_CALL_LIST_COMPLETE condition.  Throw
600    NO_ENTRY_VALUE_ERROR otherwise.  */
601
602 static struct symbol *
603 func_addr_to_tail_call_list (struct gdbarch *gdbarch, CORE_ADDR addr)
604 {
605   struct symbol *sym = find_pc_function (addr);
606   struct type *type;
607
608   if (sym == NULL || BLOCK_START (SYMBOL_BLOCK_VALUE (sym)) != addr)
609     throw_error (NO_ENTRY_VALUE_ERROR,
610                  _("DW_TAG_GNU_call_site resolving failed to find function "
611                    "name for address %s"),
612                  paddress (gdbarch, addr));
613
614   type = SYMBOL_TYPE (sym);
615   gdb_assert (TYPE_CODE (type) == TYPE_CODE_FUNC);
616   gdb_assert (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_FUNC);
617
618   return sym;
619 }
620
621 /* Verify function with entry point exact address ADDR can never call itself
622    via its tail calls (incl. transitively).  Throw NO_ENTRY_VALUE_ERROR if it
623    can call itself via tail calls.
624
625    If a funtion can tail call itself its entry value based parameters are
626    unreliable.  There is no verification whether the value of some/all
627    parameters is unchanged through the self tail call, we expect if there is
628    a self tail call all the parameters can be modified.  */
629
630 static void
631 func_verify_no_selftailcall (struct gdbarch *gdbarch, CORE_ADDR verify_addr)
632 {
633   struct obstack addr_obstack;
634   struct cleanup *old_chain;
635   CORE_ADDR addr;
636
637   /* Track here CORE_ADDRs which were already visited.  */
638   htab_t addr_hash;
639
640   /* The verification is completely unordered.  Track here function addresses
641      which still need to be iterated.  */
642   VEC (CORE_ADDR) *todo = NULL;
643
644   obstack_init (&addr_obstack);
645   old_chain = make_cleanup_obstack_free (&addr_obstack);   
646   addr_hash = htab_create_alloc_ex (64, core_addr_hash, core_addr_eq, NULL,
647                                     &addr_obstack, hashtab_obstack_allocate,
648                                     NULL);
649   make_cleanup_htab_delete (addr_hash);
650
651   make_cleanup (VEC_cleanup (CORE_ADDR), &todo);
652
653   VEC_safe_push (CORE_ADDR, todo, verify_addr);
654   while (!VEC_empty (CORE_ADDR, todo))
655     {
656       struct symbol *func_sym;
657       struct call_site *call_site;
658
659       addr = VEC_pop (CORE_ADDR, todo);
660
661       func_sym = func_addr_to_tail_call_list (gdbarch, addr);
662
663       for (call_site = TYPE_TAIL_CALL_LIST (SYMBOL_TYPE (func_sym));
664            call_site; call_site = call_site->tail_call_next)
665         {
666           CORE_ADDR target_addr;
667           void **slot;
668
669           /* CALLER_FRAME with registers is not available for tail-call jumped
670              frames.  */
671           target_addr = call_site_to_target_addr (gdbarch, call_site, NULL);
672
673           if (target_addr == verify_addr)
674             {
675               struct bound_minimal_symbol msym;
676               
677               msym = lookup_minimal_symbol_by_pc (verify_addr);
678               throw_error (NO_ENTRY_VALUE_ERROR,
679                            _("DW_OP_GNU_entry_value resolving has found "
680                              "function \"%s\" at %s can call itself via tail "
681                              "calls"),
682                            (msym.minsym == NULL ? "???"
683                             : SYMBOL_PRINT_NAME (msym.minsym)),
684                            paddress (gdbarch, verify_addr));
685             }
686
687           slot = htab_find_slot (addr_hash, &target_addr, INSERT);
688           if (*slot == NULL)
689             {
690               *slot = obstack_copy (&addr_obstack, &target_addr,
691                                     sizeof (target_addr));
692               VEC_safe_push (CORE_ADDR, todo, target_addr);
693             }
694         }
695     }
696
697   do_cleanups (old_chain);
698 }
699
700 /* Print user readable form of CALL_SITE->PC to gdb_stdlog.  Used only for
701    ENTRY_VALUES_DEBUG.  */
702
703 static void
704 tailcall_dump (struct gdbarch *gdbarch, const struct call_site *call_site)
705 {
706   CORE_ADDR addr = call_site->pc;
707   struct bound_minimal_symbol msym = lookup_minimal_symbol_by_pc (addr - 1);
708
709   fprintf_unfiltered (gdb_stdlog, " %s(%s)", paddress (gdbarch, addr),
710                       (msym.minsym == NULL ? "???"
711                        : SYMBOL_PRINT_NAME (msym.minsym)));
712
713 }
714
715 /* vec.h needs single word type name, typedef it.  */
716 typedef struct call_site *call_sitep;
717
718 /* Define VEC (call_sitep) functions.  */
719 DEF_VEC_P (call_sitep);
720
721 /* Intersect RESULTP with CHAIN to keep RESULTP unambiguous, keep in RESULTP
722    only top callers and bottom callees which are present in both.  GDBARCH is
723    used only for ENTRY_VALUES_DEBUG.  RESULTP is NULL after return if there are
724    no remaining possibilities to provide unambiguous non-trivial result.
725    RESULTP should point to NULL on the first (initialization) call.  Caller is
726    responsible for xfree of any RESULTP data.  */
727
728 static void
729 chain_candidate (struct gdbarch *gdbarch, struct call_site_chain **resultp,
730                  VEC (call_sitep) *chain)
731 {
732   struct call_site_chain *result = *resultp;
733   long length = VEC_length (call_sitep, chain);
734   int callers, callees, idx;
735
736   if (result == NULL)
737     {
738       /* Create the initial chain containing all the passed PCs.  */
739
740       result = xmalloc (sizeof (*result) + sizeof (*result->call_site)
741                                            * (length - 1));
742       result->length = length;
743       result->callers = result->callees = length;
744       memcpy (result->call_site, VEC_address (call_sitep, chain),
745               sizeof (*result->call_site) * length);
746       *resultp = result;
747
748       if (entry_values_debug)
749         {
750           fprintf_unfiltered (gdb_stdlog, "tailcall: initial:");
751           for (idx = 0; idx < length; idx++)
752             tailcall_dump (gdbarch, result->call_site[idx]);
753           fputc_unfiltered ('\n', gdb_stdlog);
754         }
755
756       return;
757     }
758
759   if (entry_values_debug)
760     {
761       fprintf_unfiltered (gdb_stdlog, "tailcall: compare:");
762       for (idx = 0; idx < length; idx++)
763         tailcall_dump (gdbarch, VEC_index (call_sitep, chain, idx));
764       fputc_unfiltered ('\n', gdb_stdlog);
765     }
766
767   /* Intersect callers.  */
768
769   callers = min (result->callers, length);
770   for (idx = 0; idx < callers; idx++)
771     if (result->call_site[idx] != VEC_index (call_sitep, chain, idx))
772       {
773         result->callers = idx;
774         break;
775       }
776
777   /* Intersect callees.  */
778
779   callees = min (result->callees, length);
780   for (idx = 0; idx < callees; idx++)
781     if (result->call_site[result->length - 1 - idx]
782         != VEC_index (call_sitep, chain, length - 1 - idx))
783       {
784         result->callees = idx;
785         break;
786       }
787
788   if (entry_values_debug)
789     {
790       fprintf_unfiltered (gdb_stdlog, "tailcall: reduced:");
791       for (idx = 0; idx < result->callers; idx++)
792         tailcall_dump (gdbarch, result->call_site[idx]);
793       fputs_unfiltered (" |", gdb_stdlog);
794       for (idx = 0; idx < result->callees; idx++)
795         tailcall_dump (gdbarch, result->call_site[result->length
796                                                   - result->callees + idx]);
797       fputc_unfiltered ('\n', gdb_stdlog);
798     }
799
800   if (result->callers == 0 && result->callees == 0)
801     {
802       /* There are no common callers or callees.  It could be also a direct
803          call (which has length 0) with ambiguous possibility of an indirect
804          call - CALLERS == CALLEES == 0 is valid during the first allocation
805          but any subsequence processing of such entry means ambiguity.  */
806       xfree (result);
807       *resultp = NULL;
808       return;
809     }
810
811   /* See call_site_find_chain_1 why there is no way to reach the bottom callee
812      PC again.  In such case there must be two different code paths to reach
813      it, therefore some of the former determined intermediate PCs must differ
814      and the unambiguous chain gets shortened.  */
815   gdb_assert (result->callers + result->callees < result->length);
816 }
817
818 /* Create and return call_site_chain for CALLER_PC and CALLEE_PC.  All the
819    assumed frames between them use GDBARCH.  Use depth first search so we can
820    keep single CHAIN of call_site's back to CALLER_PC.  Function recursion
821    would have needless GDB stack overhead.  Caller is responsible for xfree of
822    the returned result.  Any unreliability results in thrown
823    NO_ENTRY_VALUE_ERROR.  */
824
825 static struct call_site_chain *
826 call_site_find_chain_1 (struct gdbarch *gdbarch, CORE_ADDR caller_pc,
827                         CORE_ADDR callee_pc)
828 {
829   CORE_ADDR save_callee_pc = callee_pc;
830   struct obstack addr_obstack;
831   struct cleanup *back_to_retval, *back_to_workdata;
832   struct call_site_chain *retval = NULL;
833   struct call_site *call_site;
834
835   /* Mark CALL_SITEs so we do not visit the same ones twice.  */
836   htab_t addr_hash;
837
838   /* CHAIN contains only the intermediate CALL_SITEs.  Neither CALLER_PC's
839      call_site nor any possible call_site at CALLEE_PC's function is there.
840      Any CALL_SITE in CHAIN will be iterated to its siblings - via
841      TAIL_CALL_NEXT.  This is inappropriate for CALLER_PC's call_site.  */
842   VEC (call_sitep) *chain = NULL;
843
844   /* We are not interested in the specific PC inside the callee function.  */
845   callee_pc = get_pc_function_start (callee_pc);
846   if (callee_pc == 0)
847     throw_error (NO_ENTRY_VALUE_ERROR, _("Unable to find function for PC %s"),
848                  paddress (gdbarch, save_callee_pc));
849
850   back_to_retval = make_cleanup (free_current_contents, &retval);
851
852   obstack_init (&addr_obstack);
853   back_to_workdata = make_cleanup_obstack_free (&addr_obstack);   
854   addr_hash = htab_create_alloc_ex (64, core_addr_hash, core_addr_eq, NULL,
855                                     &addr_obstack, hashtab_obstack_allocate,
856                                     NULL);
857   make_cleanup_htab_delete (addr_hash);
858
859   make_cleanup (VEC_cleanup (call_sitep), &chain);
860
861   /* Do not push CALL_SITE to CHAIN.  Push there only the first tail call site
862      at the target's function.  All the possible tail call sites in the
863      target's function will get iterated as already pushed into CHAIN via their
864      TAIL_CALL_NEXT.  */
865   call_site = call_site_for_pc (gdbarch, caller_pc);
866
867   while (call_site)
868     {
869       CORE_ADDR target_func_addr;
870       struct call_site *target_call_site;
871
872       /* CALLER_FRAME with registers is not available for tail-call jumped
873          frames.  */
874       target_func_addr = call_site_to_target_addr (gdbarch, call_site, NULL);
875
876       if (target_func_addr == callee_pc)
877         {
878           chain_candidate (gdbarch, &retval, chain);
879           if (retval == NULL)
880             break;
881
882           /* There is no way to reach CALLEE_PC again as we would prevent
883              entering it twice as being already marked in ADDR_HASH.  */
884           target_call_site = NULL;
885         }
886       else
887         {
888           struct symbol *target_func;
889
890           target_func = func_addr_to_tail_call_list (gdbarch, target_func_addr);
891           target_call_site = TYPE_TAIL_CALL_LIST (SYMBOL_TYPE (target_func));
892         }
893
894       do
895         {
896           /* Attempt to visit TARGET_CALL_SITE.  */
897
898           if (target_call_site)
899             {
900               void **slot;
901
902               slot = htab_find_slot (addr_hash, &target_call_site->pc, INSERT);
903               if (*slot == NULL)
904                 {
905                   /* Successfully entered TARGET_CALL_SITE.  */
906
907                   *slot = &target_call_site->pc;
908                   VEC_safe_push (call_sitep, chain, target_call_site);
909                   break;
910                 }
911             }
912
913           /* Backtrack (without revisiting the originating call_site).  Try the
914              callers's sibling; if there isn't any try the callers's callers's
915              sibling etc.  */
916
917           target_call_site = NULL;
918           while (!VEC_empty (call_sitep, chain))
919             {
920               call_site = VEC_pop (call_sitep, chain);
921
922               gdb_assert (htab_find_slot (addr_hash, &call_site->pc,
923                                           NO_INSERT) != NULL);
924               htab_remove_elt (addr_hash, &call_site->pc);
925
926               target_call_site = call_site->tail_call_next;
927               if (target_call_site)
928                 break;
929             }
930         }
931       while (target_call_site);
932
933       if (VEC_empty (call_sitep, chain))
934         call_site = NULL;
935       else
936         call_site = VEC_last (call_sitep, chain);
937     }
938
939   if (retval == NULL)
940     {
941       struct bound_minimal_symbol msym_caller, msym_callee;
942       
943       msym_caller = lookup_minimal_symbol_by_pc (caller_pc);
944       msym_callee = lookup_minimal_symbol_by_pc (callee_pc);
945       throw_error (NO_ENTRY_VALUE_ERROR,
946                    _("There are no unambiguously determinable intermediate "
947                      "callers or callees between caller function \"%s\" at %s "
948                      "and callee function \"%s\" at %s"),
949                    (msym_caller.minsym == NULL
950                     ? "???" : SYMBOL_PRINT_NAME (msym_caller.minsym)),
951                    paddress (gdbarch, caller_pc),
952                    (msym_callee.minsym == NULL
953                     ? "???" : SYMBOL_PRINT_NAME (msym_callee.minsym)),
954                    paddress (gdbarch, callee_pc));
955     }
956
957   do_cleanups (back_to_workdata);
958   discard_cleanups (back_to_retval);
959   return retval;
960 }
961
962 /* Create and return call_site_chain for CALLER_PC and CALLEE_PC.  All the
963    assumed frames between them use GDBARCH.  If valid call_site_chain cannot be
964    constructed return NULL.  Caller is responsible for xfree of the returned
965    result.  */
966
967 struct call_site_chain *
968 call_site_find_chain (struct gdbarch *gdbarch, CORE_ADDR caller_pc,
969                       CORE_ADDR callee_pc)
970 {
971   volatile struct gdb_exception e;
972   struct call_site_chain *retval = NULL;
973
974   TRY_CATCH (e, RETURN_MASK_ERROR)
975     {
976       retval = call_site_find_chain_1 (gdbarch, caller_pc, callee_pc);
977     }
978   if (e.reason < 0)
979     {
980       if (e.error == NO_ENTRY_VALUE_ERROR)
981         {
982           if (entry_values_debug)
983             exception_print (gdb_stdout, e);
984
985           return NULL;
986         }
987       else
988         throw_exception (e);
989     }
990   return retval;
991 }
992
993 /* Return 1 if KIND and KIND_U match PARAMETER.  Return 0 otherwise.  */
994
995 static int
996 call_site_parameter_matches (struct call_site_parameter *parameter,
997                              enum call_site_parameter_kind kind,
998                              union call_site_parameter_u kind_u)
999 {
1000   if (kind == parameter->kind)
1001     switch (kind)
1002       {
1003       case CALL_SITE_PARAMETER_DWARF_REG:
1004         return kind_u.dwarf_reg == parameter->u.dwarf_reg;
1005       case CALL_SITE_PARAMETER_FB_OFFSET:
1006         return kind_u.fb_offset == parameter->u.fb_offset;
1007       case CALL_SITE_PARAMETER_PARAM_OFFSET:
1008         return kind_u.param_offset.cu_off == parameter->u.param_offset.cu_off;
1009       }
1010   return 0;
1011 }
1012
1013 /* Fetch call_site_parameter from caller matching KIND and KIND_U.
1014    FRAME is for callee.
1015
1016    Function always returns non-NULL, it throws NO_ENTRY_VALUE_ERROR
1017    otherwise.  */
1018
1019 static struct call_site_parameter *
1020 dwarf_expr_reg_to_entry_parameter (struct frame_info *frame,
1021                                    enum call_site_parameter_kind kind,
1022                                    union call_site_parameter_u kind_u,
1023                                    struct dwarf2_per_cu_data **per_cu_return)
1024 {
1025   CORE_ADDR func_addr, caller_pc;
1026   struct gdbarch *gdbarch;
1027   struct frame_info *caller_frame;
1028   struct call_site *call_site;
1029   int iparams;
1030   /* Initialize it just to avoid a GCC false warning.  */
1031   struct call_site_parameter *parameter = NULL;
1032   CORE_ADDR target_addr;
1033
1034   while (get_frame_type (frame) == INLINE_FRAME)
1035     {
1036       frame = get_prev_frame (frame);
1037       gdb_assert (frame != NULL);
1038     }
1039
1040   func_addr = get_frame_func (frame);
1041   gdbarch = get_frame_arch (frame);
1042   caller_frame = get_prev_frame (frame);
1043   if (gdbarch != frame_unwind_arch (frame))
1044     {
1045       struct bound_minimal_symbol msym
1046         = lookup_minimal_symbol_by_pc (func_addr);
1047       struct gdbarch *caller_gdbarch = frame_unwind_arch (frame);
1048
1049       throw_error (NO_ENTRY_VALUE_ERROR,
1050                    _("DW_OP_GNU_entry_value resolving callee gdbarch %s "
1051                      "(of %s (%s)) does not match caller gdbarch %s"),
1052                    gdbarch_bfd_arch_info (gdbarch)->printable_name,
1053                    paddress (gdbarch, func_addr),
1054                    (msym.minsym == NULL ? "???"
1055                     : SYMBOL_PRINT_NAME (msym.minsym)),
1056                    gdbarch_bfd_arch_info (caller_gdbarch)->printable_name);
1057     }
1058
1059   if (caller_frame == NULL)
1060     {
1061       struct bound_minimal_symbol msym
1062         = lookup_minimal_symbol_by_pc (func_addr);
1063
1064       throw_error (NO_ENTRY_VALUE_ERROR, _("DW_OP_GNU_entry_value resolving "
1065                                            "requires caller of %s (%s)"),
1066                    paddress (gdbarch, func_addr),
1067                    (msym.minsym == NULL ? "???"
1068                     : SYMBOL_PRINT_NAME (msym.minsym)));
1069     }
1070   caller_pc = get_frame_pc (caller_frame);
1071   call_site = call_site_for_pc (gdbarch, caller_pc);
1072
1073   target_addr = call_site_to_target_addr (gdbarch, call_site, caller_frame);
1074   if (target_addr != func_addr)
1075     {
1076       struct minimal_symbol *target_msym, *func_msym;
1077
1078       target_msym = lookup_minimal_symbol_by_pc (target_addr).minsym;
1079       func_msym = lookup_minimal_symbol_by_pc (func_addr).minsym;
1080       throw_error (NO_ENTRY_VALUE_ERROR,
1081                    _("DW_OP_GNU_entry_value resolving expects callee %s at %s "
1082                      "but the called frame is for %s at %s"),
1083                    (target_msym == NULL ? "???"
1084                                         : SYMBOL_PRINT_NAME (target_msym)),
1085                    paddress (gdbarch, target_addr),
1086                    func_msym == NULL ? "???" : SYMBOL_PRINT_NAME (func_msym),
1087                    paddress (gdbarch, func_addr));
1088     }
1089
1090   /* No entry value based parameters would be reliable if this function can
1091      call itself via tail calls.  */
1092   func_verify_no_selftailcall (gdbarch, func_addr);
1093
1094   for (iparams = 0; iparams < call_site->parameter_count; iparams++)
1095     {
1096       parameter = &call_site->parameter[iparams];
1097       if (call_site_parameter_matches (parameter, kind, kind_u))
1098         break;
1099     }
1100   if (iparams == call_site->parameter_count)
1101     {
1102       struct minimal_symbol *msym
1103         = lookup_minimal_symbol_by_pc (caller_pc).minsym;
1104
1105       /* DW_TAG_GNU_call_site_parameter will be missing just if GCC could not
1106          determine its value.  */
1107       throw_error (NO_ENTRY_VALUE_ERROR, _("Cannot find matching parameter "
1108                                            "at DW_TAG_GNU_call_site %s at %s"),
1109                    paddress (gdbarch, caller_pc),
1110                    msym == NULL ? "???" : SYMBOL_PRINT_NAME (msym)); 
1111     }
1112
1113   *per_cu_return = call_site->per_cu;
1114   return parameter;
1115 }
1116
1117 /* Return value for PARAMETER matching DEREF_SIZE.  If DEREF_SIZE is -1, return
1118    the normal DW_AT_GNU_call_site_value block.  Otherwise return the
1119    DW_AT_GNU_call_site_data_value (dereferenced) block.
1120
1121    TYPE and CALLER_FRAME specify how to evaluate the DWARF block into returned
1122    struct value.
1123
1124    Function always returns non-NULL, non-optimized out value.  It throws
1125    NO_ENTRY_VALUE_ERROR if it cannot resolve the value for any reason.  */
1126
1127 static struct value *
1128 dwarf_entry_parameter_to_value (struct call_site_parameter *parameter,
1129                                 CORE_ADDR deref_size, struct type *type,
1130                                 struct frame_info *caller_frame,
1131                                 struct dwarf2_per_cu_data *per_cu)
1132 {
1133   const gdb_byte *data_src;
1134   gdb_byte *data;
1135   size_t size;
1136
1137   data_src = deref_size == -1 ? parameter->value : parameter->data_value;
1138   size = deref_size == -1 ? parameter->value_size : parameter->data_value_size;
1139
1140   /* DEREF_SIZE size is not verified here.  */
1141   if (data_src == NULL)
1142     throw_error (NO_ENTRY_VALUE_ERROR,
1143                  _("Cannot resolve DW_AT_GNU_call_site_data_value"));
1144
1145   /* DW_AT_GNU_call_site_value is a DWARF expression, not a DWARF
1146      location.  Postprocessing of DWARF_VALUE_MEMORY would lose the type from
1147      DWARF block.  */
1148   data = alloca (size + 1);
1149   memcpy (data, data_src, size);
1150   data[size] = DW_OP_stack_value;
1151
1152   return dwarf2_evaluate_loc_desc (type, caller_frame, data, size + 1, per_cu);
1153 }
1154
1155 /* Execute DWARF block of call_site_parameter which matches KIND and KIND_U.
1156    Choose DEREF_SIZE value of that parameter.  Search caller of the CTX's
1157    frame.  CTX must be of dwarf_expr_ctx_funcs kind.
1158
1159    The CTX caller can be from a different CU - per_cu_dwarf_call implementation
1160    can be more simple as it does not support cross-CU DWARF executions.  */
1161
1162 static void
1163 dwarf_expr_push_dwarf_reg_entry_value (struct dwarf_expr_context *ctx,
1164                                        enum call_site_parameter_kind kind,
1165                                        union call_site_parameter_u kind_u,
1166                                        int deref_size)
1167 {
1168   struct dwarf_expr_baton *debaton;
1169   struct frame_info *frame, *caller_frame;
1170   struct dwarf2_per_cu_data *caller_per_cu;
1171   struct dwarf_expr_baton baton_local;
1172   struct dwarf_expr_context saved_ctx;
1173   struct call_site_parameter *parameter;
1174   const gdb_byte *data_src;
1175   size_t size;
1176
1177   gdb_assert (ctx->funcs == &dwarf_expr_ctx_funcs);
1178   debaton = ctx->baton;
1179   frame = debaton->frame;
1180   caller_frame = get_prev_frame (frame);
1181
1182   parameter = dwarf_expr_reg_to_entry_parameter (frame, kind, kind_u,
1183                                                  &caller_per_cu);
1184   data_src = deref_size == -1 ? parameter->value : parameter->data_value;
1185   size = deref_size == -1 ? parameter->value_size : parameter->data_value_size;
1186
1187   /* DEREF_SIZE size is not verified here.  */
1188   if (data_src == NULL)
1189     throw_error (NO_ENTRY_VALUE_ERROR,
1190                  _("Cannot resolve DW_AT_GNU_call_site_data_value"));
1191
1192   baton_local.frame = caller_frame;
1193   baton_local.per_cu = caller_per_cu;
1194
1195   saved_ctx.gdbarch = ctx->gdbarch;
1196   saved_ctx.addr_size = ctx->addr_size;
1197   saved_ctx.offset = ctx->offset;
1198   saved_ctx.baton = ctx->baton;
1199   ctx->gdbarch = get_objfile_arch (dwarf2_per_cu_objfile (baton_local.per_cu));
1200   ctx->addr_size = dwarf2_per_cu_addr_size (baton_local.per_cu);
1201   ctx->offset = dwarf2_per_cu_text_offset (baton_local.per_cu);
1202   ctx->baton = &baton_local;
1203
1204   dwarf_expr_eval (ctx, data_src, size);
1205
1206   ctx->gdbarch = saved_ctx.gdbarch;
1207   ctx->addr_size = saved_ctx.addr_size;
1208   ctx->offset = saved_ctx.offset;
1209   ctx->baton = saved_ctx.baton;
1210 }
1211
1212 /* Callback function for dwarf2_evaluate_loc_desc.
1213    Fetch the address indexed by DW_OP_GNU_addr_index.  */
1214
1215 static CORE_ADDR
1216 dwarf_expr_get_addr_index (void *baton, unsigned int index)
1217 {
1218   struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
1219
1220   return dwarf2_read_addr_index (debaton->per_cu, index);
1221 }
1222
1223 /* VALUE must be of type lval_computed with entry_data_value_funcs.  Perform
1224    the indirect method on it, that is use its stored target value, the sole
1225    purpose of entry_data_value_funcs..  */
1226
1227 static struct value *
1228 entry_data_value_coerce_ref (const struct value *value)
1229 {
1230   struct type *checked_type = check_typedef (value_type (value));
1231   struct value *target_val;
1232
1233   if (TYPE_CODE (checked_type) != TYPE_CODE_REF)
1234     return NULL;
1235
1236   target_val = value_computed_closure (value);
1237   value_incref (target_val);
1238   return target_val;
1239 }
1240
1241 /* Implement copy_closure.  */
1242
1243 static void *
1244 entry_data_value_copy_closure (const struct value *v)
1245 {
1246   struct value *target_val = value_computed_closure (v);
1247
1248   value_incref (target_val);
1249   return target_val;
1250 }
1251
1252 /* Implement free_closure.  */
1253
1254 static void
1255 entry_data_value_free_closure (struct value *v)
1256 {
1257   struct value *target_val = value_computed_closure (v);
1258
1259   value_free (target_val);
1260 }
1261
1262 /* Vector for methods for an entry value reference where the referenced value
1263    is stored in the caller.  On the first dereference use
1264    DW_AT_GNU_call_site_data_value in the caller.  */
1265
1266 static const struct lval_funcs entry_data_value_funcs =
1267 {
1268   NULL, /* read */
1269   NULL, /* write */
1270   NULL, /* check_validity */
1271   NULL, /* check_any_valid */
1272   NULL, /* indirect */
1273   entry_data_value_coerce_ref,
1274   NULL, /* check_synthetic_pointer */
1275   entry_data_value_copy_closure,
1276   entry_data_value_free_closure
1277 };
1278
1279 /* Read parameter of TYPE at (callee) FRAME's function entry.  KIND and KIND_U
1280    are used to match DW_AT_location at the caller's
1281    DW_TAG_GNU_call_site_parameter.
1282
1283    Function always returns non-NULL value.  It throws NO_ENTRY_VALUE_ERROR if it
1284    cannot resolve the parameter for any reason.  */
1285
1286 static struct value *
1287 value_of_dwarf_reg_entry (struct type *type, struct frame_info *frame,
1288                           enum call_site_parameter_kind kind,
1289                           union call_site_parameter_u kind_u)
1290 {
1291   struct type *checked_type = check_typedef (type);
1292   struct type *target_type = TYPE_TARGET_TYPE (checked_type);
1293   struct frame_info *caller_frame = get_prev_frame (frame);
1294   struct value *outer_val, *target_val, *val;
1295   struct call_site_parameter *parameter;
1296   struct dwarf2_per_cu_data *caller_per_cu;
1297   CORE_ADDR addr;
1298
1299   parameter = dwarf_expr_reg_to_entry_parameter (frame, kind, kind_u,
1300                                                  &caller_per_cu);
1301
1302   outer_val = dwarf_entry_parameter_to_value (parameter, -1 /* deref_size */,
1303                                               type, caller_frame,
1304                                               caller_per_cu);
1305
1306   /* Check if DW_AT_GNU_call_site_data_value cannot be used.  If it should be
1307      used and it is not available do not fall back to OUTER_VAL - dereferencing
1308      TYPE_CODE_REF with non-entry data value would give current value - not the
1309      entry value.  */
1310
1311   if (TYPE_CODE (checked_type) != TYPE_CODE_REF
1312       || TYPE_TARGET_TYPE (checked_type) == NULL)
1313     return outer_val;
1314
1315   target_val = dwarf_entry_parameter_to_value (parameter,
1316                                                TYPE_LENGTH (target_type),
1317                                                target_type, caller_frame,
1318                                                caller_per_cu);
1319
1320   /* value_as_address dereferences TYPE_CODE_REF.  */
1321   addr = extract_typed_address (value_contents (outer_val), checked_type);
1322
1323   /* The target entry value has artificial address of the entry value
1324      reference.  */
1325   VALUE_LVAL (target_val) = lval_memory;
1326   set_value_address (target_val, addr);
1327
1328   release_value (target_val);
1329   val = allocate_computed_value (type, &entry_data_value_funcs,
1330                                  target_val /* closure */);
1331
1332   /* Copy the referencing pointer to the new computed value.  */
1333   memcpy (value_contents_raw (val), value_contents_raw (outer_val),
1334           TYPE_LENGTH (checked_type));
1335   set_value_lazy (val, 0);
1336
1337   return val;
1338 }
1339
1340 /* Read parameter of TYPE at (callee) FRAME's function entry.  DATA and
1341    SIZE are DWARF block used to match DW_AT_location at the caller's
1342    DW_TAG_GNU_call_site_parameter.
1343
1344    Function always returns non-NULL value.  It throws NO_ENTRY_VALUE_ERROR if it
1345    cannot resolve the parameter for any reason.  */
1346
1347 static struct value *
1348 value_of_dwarf_block_entry (struct type *type, struct frame_info *frame,
1349                             const gdb_byte *block, size_t block_len)
1350 {
1351   union call_site_parameter_u kind_u;
1352
1353   kind_u.dwarf_reg = dwarf_block_to_dwarf_reg (block, block + block_len);
1354   if (kind_u.dwarf_reg != -1)
1355     return value_of_dwarf_reg_entry (type, frame, CALL_SITE_PARAMETER_DWARF_REG,
1356                                      kind_u);
1357
1358   if (dwarf_block_to_fb_offset (block, block + block_len, &kind_u.fb_offset))
1359     return value_of_dwarf_reg_entry (type, frame, CALL_SITE_PARAMETER_FB_OFFSET,
1360                                      kind_u);
1361
1362   /* This can normally happen - throw NO_ENTRY_VALUE_ERROR to get the message
1363      suppressed during normal operation.  The expression can be arbitrary if
1364      there is no caller-callee entry value binding expected.  */
1365   throw_error (NO_ENTRY_VALUE_ERROR,
1366                _("DWARF-2 expression error: DW_OP_GNU_entry_value is supported "
1367                  "only for single DW_OP_reg* or for DW_OP_fbreg(*)"));
1368 }
1369
1370 struct piece_closure
1371 {
1372   /* Reference count.  */
1373   int refc;
1374
1375   /* The CU from which this closure's expression came.  */
1376   struct dwarf2_per_cu_data *per_cu;
1377
1378   /* The number of pieces used to describe this variable.  */
1379   int n_pieces;
1380
1381   /* The target address size, used only for DWARF_VALUE_STACK.  */
1382   int addr_size;
1383
1384   /* The pieces themselves.  */
1385   struct dwarf_expr_piece *pieces;
1386 };
1387
1388 /* Allocate a closure for a value formed from separately-described
1389    PIECES.  */
1390
1391 static struct piece_closure *
1392 allocate_piece_closure (struct dwarf2_per_cu_data *per_cu,
1393                         int n_pieces, struct dwarf_expr_piece *pieces,
1394                         int addr_size)
1395 {
1396   struct piece_closure *c = XZALLOC (struct piece_closure);
1397   int i;
1398
1399   c->refc = 1;
1400   c->per_cu = per_cu;
1401   c->n_pieces = n_pieces;
1402   c->addr_size = addr_size;
1403   c->pieces = XCALLOC (n_pieces, struct dwarf_expr_piece);
1404
1405   memcpy (c->pieces, pieces, n_pieces * sizeof (struct dwarf_expr_piece));
1406   for (i = 0; i < n_pieces; ++i)
1407     if (c->pieces[i].location == DWARF_VALUE_STACK)
1408       value_incref (c->pieces[i].v.value);
1409
1410   return c;
1411 }
1412
1413 /* The lowest-level function to extract bits from a byte buffer.
1414    SOURCE is the buffer.  It is updated if we read to the end of a
1415    byte.
1416    SOURCE_OFFSET_BITS is the offset of the first bit to read.  It is
1417    updated to reflect the number of bits actually read.
1418    NBITS is the number of bits we want to read.  It is updated to
1419    reflect the number of bits actually read.  This function may read
1420    fewer bits.
1421    BITS_BIG_ENDIAN is taken directly from gdbarch.
1422    This function returns the extracted bits.  */
1423
1424 static unsigned int
1425 extract_bits_primitive (const gdb_byte **source,
1426                         unsigned int *source_offset_bits,
1427                         int *nbits, int bits_big_endian)
1428 {
1429   unsigned int avail, mask, datum;
1430
1431   gdb_assert (*source_offset_bits < 8);
1432
1433   avail = 8 - *source_offset_bits;
1434   if (avail > *nbits)
1435     avail = *nbits;
1436
1437   mask = (1 << avail) - 1;
1438   datum = **source;
1439   if (bits_big_endian)
1440     datum >>= 8 - (*source_offset_bits + *nbits);
1441   else
1442     datum >>= *source_offset_bits;
1443   datum &= mask;
1444
1445   *nbits -= avail;
1446   *source_offset_bits += avail;
1447   if (*source_offset_bits >= 8)
1448     {
1449       *source_offset_bits -= 8;
1450       ++*source;
1451     }
1452
1453   return datum;
1454 }
1455
1456 /* Extract some bits from a source buffer and move forward in the
1457    buffer.
1458    
1459    SOURCE is the source buffer.  It is updated as bytes are read.
1460    SOURCE_OFFSET_BITS is the offset into SOURCE.  It is updated as
1461    bits are read.
1462    NBITS is the number of bits to read.
1463    BITS_BIG_ENDIAN is taken directly from gdbarch.
1464    
1465    This function returns the bits that were read.  */
1466
1467 static unsigned int
1468 extract_bits (const gdb_byte **source, unsigned int *source_offset_bits,
1469               int nbits, int bits_big_endian)
1470 {
1471   unsigned int datum;
1472
1473   gdb_assert (nbits > 0 && nbits <= 8);
1474
1475   datum = extract_bits_primitive (source, source_offset_bits, &nbits,
1476                                   bits_big_endian);
1477   if (nbits > 0)
1478     {
1479       unsigned int more;
1480
1481       more = extract_bits_primitive (source, source_offset_bits, &nbits,
1482                                      bits_big_endian);
1483       if (bits_big_endian)
1484         datum <<= nbits;
1485       else
1486         more <<= nbits;
1487       datum |= more;
1488     }
1489
1490   return datum;
1491 }
1492
1493 /* Write some bits into a buffer and move forward in the buffer.
1494    
1495    DATUM is the bits to write.  The low-order bits of DATUM are used.
1496    DEST is the destination buffer.  It is updated as bytes are
1497    written.
1498    DEST_OFFSET_BITS is the bit offset in DEST at which writing is
1499    done.
1500    NBITS is the number of valid bits in DATUM.
1501    BITS_BIG_ENDIAN is taken directly from gdbarch.  */
1502
1503 static void
1504 insert_bits (unsigned int datum,
1505              gdb_byte *dest, unsigned int dest_offset_bits,
1506              int nbits, int bits_big_endian)
1507 {
1508   unsigned int mask;
1509
1510   gdb_assert (dest_offset_bits + nbits <= 8);
1511
1512   mask = (1 << nbits) - 1;
1513   if (bits_big_endian)
1514     {
1515       datum <<= 8 - (dest_offset_bits + nbits);
1516       mask <<= 8 - (dest_offset_bits + nbits);
1517     }
1518   else
1519     {
1520       datum <<= dest_offset_bits;
1521       mask <<= dest_offset_bits;
1522     }
1523
1524   gdb_assert ((datum & ~mask) == 0);
1525
1526   *dest = (*dest & ~mask) | datum;
1527 }
1528
1529 /* Copy bits from a source to a destination.
1530    
1531    DEST is where the bits should be written.
1532    DEST_OFFSET_BITS is the bit offset into DEST.
1533    SOURCE is the source of bits.
1534    SOURCE_OFFSET_BITS is the bit offset into SOURCE.
1535    BIT_COUNT is the number of bits to copy.
1536    BITS_BIG_ENDIAN is taken directly from gdbarch.  */
1537
1538 static void
1539 copy_bitwise (gdb_byte *dest, unsigned int dest_offset_bits,
1540               const gdb_byte *source, unsigned int source_offset_bits,
1541               unsigned int bit_count,
1542               int bits_big_endian)
1543 {
1544   unsigned int dest_avail;
1545   int datum;
1546
1547   /* Reduce everything to byte-size pieces.  */
1548   dest += dest_offset_bits / 8;
1549   dest_offset_bits %= 8;
1550   source += source_offset_bits / 8;
1551   source_offset_bits %= 8;
1552
1553   dest_avail = 8 - dest_offset_bits % 8;
1554
1555   /* See if we can fill the first destination byte.  */
1556   if (dest_avail < bit_count)
1557     {
1558       datum = extract_bits (&source, &source_offset_bits, dest_avail,
1559                             bits_big_endian);
1560       insert_bits (datum, dest, dest_offset_bits, dest_avail, bits_big_endian);
1561       ++dest;
1562       dest_offset_bits = 0;
1563       bit_count -= dest_avail;
1564     }
1565
1566   /* Now, either DEST_OFFSET_BITS is byte-aligned, or we have fewer
1567      than 8 bits remaining.  */
1568   gdb_assert (dest_offset_bits % 8 == 0 || bit_count < 8);
1569   for (; bit_count >= 8; bit_count -= 8)
1570     {
1571       datum = extract_bits (&source, &source_offset_bits, 8, bits_big_endian);
1572       *dest++ = (gdb_byte) datum;
1573     }
1574
1575   /* Finally, we may have a few leftover bits.  */
1576   gdb_assert (bit_count <= 8 - dest_offset_bits % 8);
1577   if (bit_count > 0)
1578     {
1579       datum = extract_bits (&source, &source_offset_bits, bit_count,
1580                             bits_big_endian);
1581       insert_bits (datum, dest, dest_offset_bits, bit_count, bits_big_endian);
1582     }
1583 }
1584
1585 static void
1586 read_pieced_value (struct value *v)
1587 {
1588   int i;
1589   long offset = 0;
1590   ULONGEST bits_to_skip;
1591   gdb_byte *contents;
1592   struct piece_closure *c
1593     = (struct piece_closure *) value_computed_closure (v);
1594   struct frame_info *frame = frame_find_by_id (VALUE_FRAME_ID (v));
1595   size_t type_len;
1596   size_t buffer_size = 0;
1597   char *buffer = NULL;
1598   struct cleanup *cleanup;
1599   int bits_big_endian
1600     = gdbarch_bits_big_endian (get_type_arch (value_type (v)));
1601
1602   if (value_type (v) != value_enclosing_type (v))
1603     internal_error (__FILE__, __LINE__,
1604                     _("Should not be able to create a lazy value with "
1605                       "an enclosing type"));
1606
1607   cleanup = make_cleanup (free_current_contents, &buffer);
1608
1609   contents = value_contents_raw (v);
1610   bits_to_skip = 8 * value_offset (v);
1611   if (value_bitsize (v))
1612     {
1613       bits_to_skip += value_bitpos (v);
1614       type_len = value_bitsize (v);
1615     }
1616   else
1617     type_len = 8 * TYPE_LENGTH (value_type (v));
1618
1619   for (i = 0; i < c->n_pieces && offset < type_len; i++)
1620     {
1621       struct dwarf_expr_piece *p = &c->pieces[i];
1622       size_t this_size, this_size_bits;
1623       long dest_offset_bits, source_offset_bits, source_offset;
1624       const gdb_byte *intermediate_buffer;
1625
1626       /* Compute size, source, and destination offsets for copying, in
1627          bits.  */
1628       this_size_bits = p->size;
1629       if (bits_to_skip > 0 && bits_to_skip >= this_size_bits)
1630         {
1631           bits_to_skip -= this_size_bits;
1632           continue;
1633         }
1634       if (this_size_bits > type_len - offset)
1635         this_size_bits = type_len - offset;
1636       if (bits_to_skip > 0)
1637         {
1638           dest_offset_bits = 0;
1639           source_offset_bits = bits_to_skip;
1640           this_size_bits -= bits_to_skip;
1641           bits_to_skip = 0;
1642         }
1643       else
1644         {
1645           dest_offset_bits = offset;
1646           source_offset_bits = 0;
1647         }
1648
1649       this_size = (this_size_bits + source_offset_bits % 8 + 7) / 8;
1650       source_offset = source_offset_bits / 8;
1651       if (buffer_size < this_size)
1652         {
1653           buffer_size = this_size;
1654           buffer = xrealloc (buffer, buffer_size);
1655         }
1656       intermediate_buffer = buffer;
1657
1658       /* Copy from the source to DEST_BUFFER.  */
1659       switch (p->location)
1660         {
1661         case DWARF_VALUE_REGISTER:
1662           {
1663             struct gdbarch *arch = get_frame_arch (frame);
1664             int gdb_regnum = gdbarch_dwarf2_reg_to_regnum (arch, p->v.regno);
1665             int reg_offset = source_offset;
1666
1667             if (gdbarch_byte_order (arch) == BFD_ENDIAN_BIG
1668                 && this_size < register_size (arch, gdb_regnum))
1669               {
1670                 /* Big-endian, and we want less than full size.  */
1671                 reg_offset = register_size (arch, gdb_regnum) - this_size;
1672                 /* We want the lower-order THIS_SIZE_BITS of the bytes
1673                    we extract from the register.  */
1674                 source_offset_bits += 8 * this_size - this_size_bits;
1675               }
1676
1677             if (gdb_regnum != -1)
1678               {
1679                 int optim, unavail;
1680
1681                 if (!get_frame_register_bytes (frame, gdb_regnum, reg_offset,
1682                                                this_size, buffer,
1683                                                &optim, &unavail))
1684                   {
1685                     /* Just so garbage doesn't ever shine through.  */
1686                     memset (buffer, 0, this_size);
1687
1688                     if (optim)
1689                       set_value_optimized_out (v, 1);
1690                     if (unavail)
1691                       mark_value_bytes_unavailable (v, offset, this_size);
1692                   }
1693               }
1694             else
1695               {
1696                 error (_("Unable to access DWARF register number %s"),
1697                        paddress (arch, p->v.regno));
1698               }
1699           }
1700           break;
1701
1702         case DWARF_VALUE_MEMORY:
1703           read_value_memory (v, offset,
1704                              p->v.mem.in_stack_memory,
1705                              p->v.mem.addr + source_offset,
1706                              buffer, this_size);
1707           break;
1708
1709         case DWARF_VALUE_STACK:
1710           {
1711             size_t n = this_size;
1712
1713             if (n > c->addr_size - source_offset)
1714               n = (c->addr_size >= source_offset
1715                    ? c->addr_size - source_offset
1716                    : 0);
1717             if (n == 0)
1718               {
1719                 /* Nothing.  */
1720               }
1721             else
1722               {
1723                 const gdb_byte *val_bytes = value_contents_all (p->v.value);
1724
1725                 intermediate_buffer = val_bytes + source_offset;
1726               }
1727           }
1728           break;
1729
1730         case DWARF_VALUE_LITERAL:
1731           {
1732             size_t n = this_size;
1733
1734             if (n > p->v.literal.length - source_offset)
1735               n = (p->v.literal.length >= source_offset
1736                    ? p->v.literal.length - source_offset
1737                    : 0);
1738             if (n != 0)
1739               intermediate_buffer = p->v.literal.data + source_offset;
1740           }
1741           break;
1742
1743           /* These bits show up as zeros -- but do not cause the value
1744              to be considered optimized-out.  */
1745         case DWARF_VALUE_IMPLICIT_POINTER:
1746           break;
1747
1748         case DWARF_VALUE_OPTIMIZED_OUT:
1749           set_value_optimized_out (v, 1);
1750           break;
1751
1752         default:
1753           internal_error (__FILE__, __LINE__, _("invalid location type"));
1754         }
1755
1756       if (p->location != DWARF_VALUE_OPTIMIZED_OUT
1757           && p->location != DWARF_VALUE_IMPLICIT_POINTER)
1758         copy_bitwise (contents, dest_offset_bits,
1759                       intermediate_buffer, source_offset_bits % 8,
1760                       this_size_bits, bits_big_endian);
1761
1762       offset += this_size_bits;
1763     }
1764
1765   do_cleanups (cleanup);
1766 }
1767
1768 static void
1769 write_pieced_value (struct value *to, struct value *from)
1770 {
1771   int i;
1772   long offset = 0;
1773   ULONGEST bits_to_skip;
1774   const gdb_byte *contents;
1775   struct piece_closure *c
1776     = (struct piece_closure *) value_computed_closure (to);
1777   struct frame_info *frame = frame_find_by_id (VALUE_FRAME_ID (to));
1778   size_t type_len;
1779   size_t buffer_size = 0;
1780   char *buffer = NULL;
1781   struct cleanup *cleanup;
1782   int bits_big_endian
1783     = gdbarch_bits_big_endian (get_type_arch (value_type (to)));
1784
1785   if (frame == NULL)
1786     {
1787       set_value_optimized_out (to, 1);
1788       return;
1789     }
1790
1791   cleanup = make_cleanup (free_current_contents, &buffer);
1792
1793   contents = value_contents (from);
1794   bits_to_skip = 8 * value_offset (to);
1795   if (value_bitsize (to))
1796     {
1797       bits_to_skip += value_bitpos (to);
1798       type_len = value_bitsize (to);
1799     }
1800   else
1801     type_len = 8 * TYPE_LENGTH (value_type (to));
1802
1803   for (i = 0; i < c->n_pieces && offset < type_len; i++)
1804     {
1805       struct dwarf_expr_piece *p = &c->pieces[i];
1806       size_t this_size_bits, this_size;
1807       long dest_offset_bits, source_offset_bits, dest_offset, source_offset;
1808       int need_bitwise;
1809       const gdb_byte *source_buffer;
1810
1811       this_size_bits = p->size;
1812       if (bits_to_skip > 0 && bits_to_skip >= this_size_bits)
1813         {
1814           bits_to_skip -= this_size_bits;
1815           continue;
1816         }
1817       if (this_size_bits > type_len - offset)
1818         this_size_bits = type_len - offset;
1819       if (bits_to_skip > 0)
1820         {
1821           dest_offset_bits = bits_to_skip;
1822           source_offset_bits = 0;
1823           this_size_bits -= bits_to_skip;
1824           bits_to_skip = 0;
1825         }
1826       else
1827         {
1828           dest_offset_bits = 0;
1829           source_offset_bits = offset;
1830         }
1831
1832       this_size = (this_size_bits + source_offset_bits % 8 + 7) / 8;
1833       source_offset = source_offset_bits / 8;
1834       dest_offset = dest_offset_bits / 8;
1835       if (dest_offset_bits % 8 == 0 && source_offset_bits % 8 == 0)
1836         {
1837           source_buffer = contents + source_offset;
1838           need_bitwise = 0;
1839         }
1840       else
1841         {
1842           if (buffer_size < this_size)
1843             {
1844               buffer_size = this_size;
1845               buffer = xrealloc (buffer, buffer_size);
1846             }
1847           source_buffer = buffer;
1848           need_bitwise = 1;
1849         }
1850
1851       switch (p->location)
1852         {
1853         case DWARF_VALUE_REGISTER:
1854           {
1855             struct gdbarch *arch = get_frame_arch (frame);
1856             int gdb_regnum = gdbarch_dwarf2_reg_to_regnum (arch, p->v.regno);
1857             int reg_offset = dest_offset;
1858
1859             if (gdbarch_byte_order (arch) == BFD_ENDIAN_BIG
1860                 && this_size <= register_size (arch, gdb_regnum))
1861               /* Big-endian, and we want less than full size.  */
1862               reg_offset = register_size (arch, gdb_regnum) - this_size;
1863
1864             if (gdb_regnum != -1)
1865               {
1866                 if (need_bitwise)
1867                   {
1868                     int optim, unavail;
1869
1870                     if (!get_frame_register_bytes (frame, gdb_regnum, reg_offset,
1871                                                    this_size, buffer,
1872                                                    &optim, &unavail))
1873                       {
1874                         if (optim)
1875                           error (_("Can't do read-modify-write to "
1876                                    "update bitfield; containing word has been "
1877                                    "optimized out"));
1878                         if (unavail)
1879                           throw_error (NOT_AVAILABLE_ERROR,
1880                                        _("Can't do read-modify-write to update "
1881                                          "bitfield; containing word "
1882                                          "is unavailable"));
1883                       }
1884                     copy_bitwise (buffer, dest_offset_bits,
1885                                   contents, source_offset_bits,
1886                                   this_size_bits,
1887                                   bits_big_endian);
1888                   }
1889
1890                 put_frame_register_bytes (frame, gdb_regnum, reg_offset, 
1891                                           this_size, source_buffer);
1892               }
1893             else
1894               {
1895                 error (_("Unable to write to DWARF register number %s"),
1896                        paddress (arch, p->v.regno));
1897               }
1898           }
1899           break;
1900         case DWARF_VALUE_MEMORY:
1901           if (need_bitwise)
1902             {
1903               /* Only the first and last bytes can possibly have any
1904                  bits reused.  */
1905               read_memory (p->v.mem.addr + dest_offset, buffer, 1);
1906               read_memory (p->v.mem.addr + dest_offset + this_size - 1,
1907                            buffer + this_size - 1, 1);
1908               copy_bitwise (buffer, dest_offset_bits,
1909                             contents, source_offset_bits,
1910                             this_size_bits,
1911                             bits_big_endian);
1912             }
1913
1914           write_memory (p->v.mem.addr + dest_offset,
1915                         source_buffer, this_size);
1916           break;
1917         default:
1918           set_value_optimized_out (to, 1);
1919           break;
1920         }
1921       offset += this_size_bits;
1922     }
1923
1924   do_cleanups (cleanup);
1925 }
1926
1927 /* A helper function that checks bit validity in a pieced value.
1928    CHECK_FOR indicates the kind of validity checking.
1929    DWARF_VALUE_MEMORY means to check whether any bit is valid.
1930    DWARF_VALUE_OPTIMIZED_OUT means to check whether any bit is
1931    optimized out.
1932    DWARF_VALUE_IMPLICIT_POINTER means to check whether the bits are an
1933    implicit pointer.  */
1934
1935 static int
1936 check_pieced_value_bits (const struct value *value, int bit_offset,
1937                          int bit_length,
1938                          enum dwarf_value_location check_for)
1939 {
1940   struct piece_closure *c
1941     = (struct piece_closure *) value_computed_closure (value);
1942   int i;
1943   int validity = (check_for == DWARF_VALUE_MEMORY
1944                   || check_for == DWARF_VALUE_IMPLICIT_POINTER);
1945
1946   bit_offset += 8 * value_offset (value);
1947   if (value_bitsize (value))
1948     bit_offset += value_bitpos (value);
1949
1950   for (i = 0; i < c->n_pieces && bit_length > 0; i++)
1951     {
1952       struct dwarf_expr_piece *p = &c->pieces[i];
1953       size_t this_size_bits = p->size;
1954
1955       if (bit_offset > 0)
1956         {
1957           if (bit_offset >= this_size_bits)
1958             {
1959               bit_offset -= this_size_bits;
1960               continue;
1961             }
1962
1963           bit_length -= this_size_bits - bit_offset;
1964           bit_offset = 0;
1965         }
1966       else
1967         bit_length -= this_size_bits;
1968
1969       if (check_for == DWARF_VALUE_IMPLICIT_POINTER)
1970         {
1971           if (p->location != DWARF_VALUE_IMPLICIT_POINTER)
1972             return 0;
1973         }
1974       else if (p->location == DWARF_VALUE_OPTIMIZED_OUT
1975                || p->location == DWARF_VALUE_IMPLICIT_POINTER)
1976         {
1977           if (validity)
1978             return 0;
1979         }
1980       else
1981         {
1982           if (!validity)
1983             return 1;
1984         }
1985     }
1986
1987   return validity;
1988 }
1989
1990 static int
1991 check_pieced_value_validity (const struct value *value, int bit_offset,
1992                              int bit_length)
1993 {
1994   return check_pieced_value_bits (value, bit_offset, bit_length,
1995                                   DWARF_VALUE_MEMORY);
1996 }
1997
1998 static int
1999 check_pieced_value_invalid (const struct value *value)
2000 {
2001   return check_pieced_value_bits (value, 0,
2002                                   8 * TYPE_LENGTH (value_type (value)),
2003                                   DWARF_VALUE_OPTIMIZED_OUT);
2004 }
2005
2006 /* An implementation of an lval_funcs method to see whether a value is
2007    a synthetic pointer.  */
2008
2009 static int
2010 check_pieced_synthetic_pointer (const struct value *value, int bit_offset,
2011                                 int bit_length)
2012 {
2013   return check_pieced_value_bits (value, bit_offset, bit_length,
2014                                   DWARF_VALUE_IMPLICIT_POINTER);
2015 }
2016
2017 /* A wrapper function for get_frame_address_in_block.  */
2018
2019 static CORE_ADDR
2020 get_frame_address_in_block_wrapper (void *baton)
2021 {
2022   return get_frame_address_in_block (baton);
2023 }
2024
2025 /* An implementation of an lval_funcs method to indirect through a
2026    pointer.  This handles the synthetic pointer case when needed.  */
2027
2028 static struct value *
2029 indirect_pieced_value (struct value *value)
2030 {
2031   struct piece_closure *c
2032     = (struct piece_closure *) value_computed_closure (value);
2033   struct type *type;
2034   struct frame_info *frame;
2035   struct dwarf2_locexpr_baton baton;
2036   int i, bit_offset, bit_length;
2037   struct dwarf_expr_piece *piece = NULL;
2038   LONGEST byte_offset;
2039
2040   type = check_typedef (value_type (value));
2041   if (TYPE_CODE (type) != TYPE_CODE_PTR)
2042     return NULL;
2043
2044   bit_length = 8 * TYPE_LENGTH (type);
2045   bit_offset = 8 * value_offset (value);
2046   if (value_bitsize (value))
2047     bit_offset += value_bitpos (value);
2048
2049   for (i = 0; i < c->n_pieces && bit_length > 0; i++)
2050     {
2051       struct dwarf_expr_piece *p = &c->pieces[i];
2052       size_t this_size_bits = p->size;
2053
2054       if (bit_offset > 0)
2055         {
2056           if (bit_offset >= this_size_bits)
2057             {
2058               bit_offset -= this_size_bits;
2059               continue;
2060             }
2061
2062           bit_length -= this_size_bits - bit_offset;
2063           bit_offset = 0;
2064         }
2065       else
2066         bit_length -= this_size_bits;
2067
2068       if (p->location != DWARF_VALUE_IMPLICIT_POINTER)
2069         return NULL;
2070
2071       if (bit_length != 0)
2072         error (_("Invalid use of DW_OP_GNU_implicit_pointer"));
2073
2074       piece = p;
2075       break;
2076     }
2077
2078   frame = get_selected_frame (_("No frame selected."));
2079
2080   /* This is an offset requested by GDB, such as value subcripts.  */
2081   byte_offset = value_as_address (value);
2082
2083   gdb_assert (piece);
2084   baton
2085     = dwarf2_fetch_die_loc_sect_off (piece->v.ptr.die, c->per_cu,
2086                                      get_frame_address_in_block_wrapper,
2087                                      frame);
2088
2089   return dwarf2_evaluate_loc_desc_full (TYPE_TARGET_TYPE (type), frame,
2090                                         baton.data, baton.size, baton.per_cu,
2091                                         piece->v.ptr.offset + byte_offset);
2092 }
2093
2094 static void *
2095 copy_pieced_value_closure (const struct value *v)
2096 {
2097   struct piece_closure *c
2098     = (struct piece_closure *) value_computed_closure (v);
2099   
2100   ++c->refc;
2101   return c;
2102 }
2103
2104 static void
2105 free_pieced_value_closure (struct value *v)
2106 {
2107   struct piece_closure *c
2108     = (struct piece_closure *) value_computed_closure (v);
2109
2110   --c->refc;
2111   if (c->refc == 0)
2112     {
2113       int i;
2114
2115       for (i = 0; i < c->n_pieces; ++i)
2116         if (c->pieces[i].location == DWARF_VALUE_STACK)
2117           value_free (c->pieces[i].v.value);
2118
2119       xfree (c->pieces);
2120       xfree (c);
2121     }
2122 }
2123
2124 /* Functions for accessing a variable described by DW_OP_piece.  */
2125 static const struct lval_funcs pieced_value_funcs = {
2126   read_pieced_value,
2127   write_pieced_value,
2128   check_pieced_value_validity,
2129   check_pieced_value_invalid,
2130   indirect_pieced_value,
2131   NULL, /* coerce_ref */
2132   check_pieced_synthetic_pointer,
2133   copy_pieced_value_closure,
2134   free_pieced_value_closure
2135 };
2136
2137 /* Helper function which throws an error if a synthetic pointer is
2138    invalid.  */
2139
2140 static void
2141 invalid_synthetic_pointer (void)
2142 {
2143   error (_("access outside bounds of object "
2144            "referenced via synthetic pointer"));
2145 }
2146
2147 /* Virtual method table for dwarf2_evaluate_loc_desc_full below.  */
2148
2149 static const struct dwarf_expr_context_funcs dwarf_expr_ctx_funcs =
2150 {
2151   dwarf_expr_read_reg,
2152   dwarf_expr_read_mem,
2153   dwarf_expr_frame_base,
2154   dwarf_expr_frame_cfa,
2155   dwarf_expr_frame_pc,
2156   dwarf_expr_tls_address,
2157   dwarf_expr_dwarf_call,
2158   dwarf_expr_get_base_type,
2159   dwarf_expr_push_dwarf_reg_entry_value,
2160   dwarf_expr_get_addr_index
2161 };
2162
2163 /* Evaluate a location description, starting at DATA and with length
2164    SIZE, to find the current location of variable of TYPE in the
2165    context of FRAME.  BYTE_OFFSET is applied after the contents are
2166    computed.  */
2167
2168 static struct value *
2169 dwarf2_evaluate_loc_desc_full (struct type *type, struct frame_info *frame,
2170                                const gdb_byte *data, size_t size,
2171                                struct dwarf2_per_cu_data *per_cu,
2172                                LONGEST byte_offset)
2173 {
2174   struct value *retval;
2175   struct dwarf_expr_baton baton;
2176   struct dwarf_expr_context *ctx;
2177   struct cleanup *old_chain, *value_chain;
2178   struct objfile *objfile = dwarf2_per_cu_objfile (per_cu);
2179   volatile struct gdb_exception ex;
2180
2181   if (byte_offset < 0)
2182     invalid_synthetic_pointer ();
2183
2184   if (size == 0)
2185     return allocate_optimized_out_value (type);
2186
2187   baton.frame = frame;
2188   baton.per_cu = per_cu;
2189
2190   ctx = new_dwarf_expr_context ();
2191   old_chain = make_cleanup_free_dwarf_expr_context (ctx);
2192   value_chain = make_cleanup_value_free_to_mark (value_mark ());
2193
2194   ctx->gdbarch = get_objfile_arch (objfile);
2195   ctx->addr_size = dwarf2_per_cu_addr_size (per_cu);
2196   ctx->ref_addr_size = dwarf2_per_cu_ref_addr_size (per_cu);
2197   ctx->offset = dwarf2_per_cu_text_offset (per_cu);
2198   ctx->baton = &baton;
2199   ctx->funcs = &dwarf_expr_ctx_funcs;
2200
2201   TRY_CATCH (ex, RETURN_MASK_ERROR)
2202     {
2203       dwarf_expr_eval (ctx, data, size);
2204     }
2205   if (ex.reason < 0)
2206     {
2207       if (ex.error == NOT_AVAILABLE_ERROR)
2208         {
2209           do_cleanups (old_chain);
2210           retval = allocate_value (type);
2211           mark_value_bytes_unavailable (retval, 0, TYPE_LENGTH (type));
2212           return retval;
2213         }
2214       else if (ex.error == NO_ENTRY_VALUE_ERROR)
2215         {
2216           if (entry_values_debug)
2217             exception_print (gdb_stdout, ex);
2218           do_cleanups (old_chain);
2219           return allocate_optimized_out_value (type);
2220         }
2221       else
2222         throw_exception (ex);
2223     }
2224
2225   if (ctx->num_pieces > 0)
2226     {
2227       struct piece_closure *c;
2228       struct frame_id frame_id = get_frame_id (frame);
2229       ULONGEST bit_size = 0;
2230       int i;
2231
2232       for (i = 0; i < ctx->num_pieces; ++i)
2233         bit_size += ctx->pieces[i].size;
2234       if (8 * (byte_offset + TYPE_LENGTH (type)) > bit_size)
2235         invalid_synthetic_pointer ();
2236
2237       c = allocate_piece_closure (per_cu, ctx->num_pieces, ctx->pieces,
2238                                   ctx->addr_size);
2239       /* We must clean up the value chain after creating the piece
2240          closure but before allocating the result.  */
2241       do_cleanups (value_chain);
2242       retval = allocate_computed_value (type, &pieced_value_funcs, c);
2243       VALUE_FRAME_ID (retval) = frame_id;
2244       set_value_offset (retval, byte_offset);
2245     }
2246   else
2247     {
2248       switch (ctx->location)
2249         {
2250         case DWARF_VALUE_REGISTER:
2251           {
2252             struct gdbarch *arch = get_frame_arch (frame);
2253             ULONGEST dwarf_regnum = value_as_long (dwarf_expr_fetch (ctx, 0));
2254             int gdb_regnum = gdbarch_dwarf2_reg_to_regnum (arch, dwarf_regnum);
2255
2256             if (byte_offset != 0)
2257               error (_("cannot use offset on synthetic pointer to register"));
2258             do_cleanups (value_chain);
2259             if (gdb_regnum != -1)
2260               retval = value_from_register (type, gdb_regnum, frame);
2261             else
2262               error (_("Unable to access DWARF register number %s"),
2263                      paddress (arch, dwarf_regnum));
2264           }
2265           break;
2266
2267         case DWARF_VALUE_MEMORY:
2268           {
2269             CORE_ADDR address = dwarf_expr_fetch_address (ctx, 0);
2270             int in_stack_memory = dwarf_expr_fetch_in_stack_memory (ctx, 0);
2271
2272             do_cleanups (value_chain);
2273             retval = allocate_value_lazy (type);
2274             VALUE_LVAL (retval) = lval_memory;
2275             if (in_stack_memory)
2276               set_value_stack (retval, 1);
2277             set_value_address (retval, address + byte_offset);
2278           }
2279           break;
2280
2281         case DWARF_VALUE_STACK:
2282           {
2283             struct value *value = dwarf_expr_fetch (ctx, 0);
2284             gdb_byte *contents;
2285             const gdb_byte *val_bytes;
2286             size_t n = TYPE_LENGTH (value_type (value));
2287
2288             if (byte_offset + TYPE_LENGTH (type) > n)
2289               invalid_synthetic_pointer ();
2290
2291             val_bytes = value_contents_all (value);
2292             val_bytes += byte_offset;
2293             n -= byte_offset;
2294
2295             /* Preserve VALUE because we are going to free values back
2296                to the mark, but we still need the value contents
2297                below.  */
2298             value_incref (value);
2299             do_cleanups (value_chain);
2300             make_cleanup_value_free (value);
2301
2302             retval = allocate_value (type);
2303             contents = value_contents_raw (retval);
2304             if (n > TYPE_LENGTH (type))
2305               {
2306                 struct gdbarch *objfile_gdbarch = get_objfile_arch (objfile);
2307
2308                 if (gdbarch_byte_order (objfile_gdbarch) == BFD_ENDIAN_BIG)
2309                   val_bytes += n - TYPE_LENGTH (type);
2310                 n = TYPE_LENGTH (type);
2311               }
2312             memcpy (contents, val_bytes, n);
2313           }
2314           break;
2315
2316         case DWARF_VALUE_LITERAL:
2317           {
2318             bfd_byte *contents;
2319             const bfd_byte *ldata;
2320             size_t n = ctx->len;
2321
2322             if (byte_offset + TYPE_LENGTH (type) > n)
2323               invalid_synthetic_pointer ();
2324
2325             do_cleanups (value_chain);
2326             retval = allocate_value (type);
2327             contents = value_contents_raw (retval);
2328
2329             ldata = ctx->data + byte_offset;
2330             n -= byte_offset;
2331
2332             if (n > TYPE_LENGTH (type))
2333               {
2334                 struct gdbarch *objfile_gdbarch = get_objfile_arch (objfile);
2335
2336                 if (gdbarch_byte_order (objfile_gdbarch) == BFD_ENDIAN_BIG)
2337                   ldata += n - TYPE_LENGTH (type);
2338                 n = TYPE_LENGTH (type);
2339               }
2340             memcpy (contents, ldata, n);
2341           }
2342           break;
2343
2344         case DWARF_VALUE_OPTIMIZED_OUT:
2345           do_cleanups (value_chain);
2346           retval = allocate_optimized_out_value (type);
2347           break;
2348
2349           /* DWARF_VALUE_IMPLICIT_POINTER was converted to a pieced
2350              operation by execute_stack_op.  */
2351         case DWARF_VALUE_IMPLICIT_POINTER:
2352           /* DWARF_VALUE_OPTIMIZED_OUT can't occur in this context --
2353              it can only be encountered when making a piece.  */
2354         default:
2355           internal_error (__FILE__, __LINE__, _("invalid location type"));
2356         }
2357     }
2358
2359   set_value_initialized (retval, ctx->initialized);
2360
2361   do_cleanups (old_chain);
2362
2363   return retval;
2364 }
2365
2366 /* The exported interface to dwarf2_evaluate_loc_desc_full; it always
2367    passes 0 as the byte_offset.  */
2368
2369 struct value *
2370 dwarf2_evaluate_loc_desc (struct type *type, struct frame_info *frame,
2371                           const gdb_byte *data, size_t size,
2372                           struct dwarf2_per_cu_data *per_cu)
2373 {
2374   return dwarf2_evaluate_loc_desc_full (type, frame, data, size, per_cu, 0);
2375 }
2376
2377 \f
2378 /* Helper functions and baton for dwarf2_loc_desc_needs_frame.  */
2379
2380 struct needs_frame_baton
2381 {
2382   int needs_frame;
2383   struct dwarf2_per_cu_data *per_cu;
2384 };
2385
2386 /* Reads from registers do require a frame.  */
2387 static CORE_ADDR
2388 needs_frame_read_reg (void *baton, int regnum)
2389 {
2390   struct needs_frame_baton *nf_baton = baton;
2391
2392   nf_baton->needs_frame = 1;
2393   return 1;
2394 }
2395
2396 /* Reads from memory do not require a frame.  */
2397 static void
2398 needs_frame_read_mem (void *baton, gdb_byte *buf, CORE_ADDR addr, size_t len)
2399 {
2400   memset (buf, 0, len);
2401 }
2402
2403 /* Frame-relative accesses do require a frame.  */
2404 static void
2405 needs_frame_frame_base (void *baton, const gdb_byte **start, size_t * length)
2406 {
2407   static gdb_byte lit0 = DW_OP_lit0;
2408   struct needs_frame_baton *nf_baton = baton;
2409
2410   *start = &lit0;
2411   *length = 1;
2412
2413   nf_baton->needs_frame = 1;
2414 }
2415
2416 /* CFA accesses require a frame.  */
2417
2418 static CORE_ADDR
2419 needs_frame_frame_cfa (void *baton)
2420 {
2421   struct needs_frame_baton *nf_baton = baton;
2422
2423   nf_baton->needs_frame = 1;
2424   return 1;
2425 }
2426
2427 /* Thread-local accesses do require a frame.  */
2428 static CORE_ADDR
2429 needs_frame_tls_address (void *baton, CORE_ADDR offset)
2430 {
2431   struct needs_frame_baton *nf_baton = baton;
2432
2433   nf_baton->needs_frame = 1;
2434   return 1;
2435 }
2436
2437 /* Helper interface of per_cu_dwarf_call for dwarf2_loc_desc_needs_frame.  */
2438
2439 static void
2440 needs_frame_dwarf_call (struct dwarf_expr_context *ctx, cu_offset die_offset)
2441 {
2442   struct needs_frame_baton *nf_baton = ctx->baton;
2443
2444   per_cu_dwarf_call (ctx, die_offset, nf_baton->per_cu,
2445                      ctx->funcs->get_frame_pc, ctx->baton);
2446 }
2447
2448 /* DW_OP_GNU_entry_value accesses require a caller, therefore a frame.  */
2449
2450 static void
2451 needs_dwarf_reg_entry_value (struct dwarf_expr_context *ctx,
2452                              enum call_site_parameter_kind kind,
2453                              union call_site_parameter_u kind_u, int deref_size)
2454 {
2455   struct needs_frame_baton *nf_baton = ctx->baton;
2456
2457   nf_baton->needs_frame = 1;
2458
2459   /* The expression may require some stub values on DWARF stack.  */
2460   dwarf_expr_push_address (ctx, 0, 0);
2461 }
2462
2463 /* DW_OP_GNU_addr_index doesn't require a frame.  */
2464
2465 static CORE_ADDR
2466 needs_get_addr_index (void *baton, unsigned int index)
2467 {
2468   /* Nothing to do.  */
2469   return 1;
2470 }
2471
2472 /* Virtual method table for dwarf2_loc_desc_needs_frame below.  */
2473
2474 static const struct dwarf_expr_context_funcs needs_frame_ctx_funcs =
2475 {
2476   needs_frame_read_reg,
2477   needs_frame_read_mem,
2478   needs_frame_frame_base,
2479   needs_frame_frame_cfa,
2480   needs_frame_frame_cfa,        /* get_frame_pc */
2481   needs_frame_tls_address,
2482   needs_frame_dwarf_call,
2483   NULL,                         /* get_base_type */
2484   needs_dwarf_reg_entry_value,
2485   needs_get_addr_index
2486 };
2487
2488 /* Return non-zero iff the location expression at DATA (length SIZE)
2489    requires a frame to evaluate.  */
2490
2491 static int
2492 dwarf2_loc_desc_needs_frame (const gdb_byte *data, size_t size,
2493                              struct dwarf2_per_cu_data *per_cu)
2494 {
2495   struct needs_frame_baton baton;
2496   struct dwarf_expr_context *ctx;
2497   int in_reg;
2498   struct cleanup *old_chain;
2499   struct objfile *objfile = dwarf2_per_cu_objfile (per_cu);
2500
2501   baton.needs_frame = 0;
2502   baton.per_cu = per_cu;
2503
2504   ctx = new_dwarf_expr_context ();
2505   old_chain = make_cleanup_free_dwarf_expr_context (ctx);
2506   make_cleanup_value_free_to_mark (value_mark ());
2507
2508   ctx->gdbarch = get_objfile_arch (objfile);
2509   ctx->addr_size = dwarf2_per_cu_addr_size (per_cu);
2510   ctx->ref_addr_size = dwarf2_per_cu_ref_addr_size (per_cu);
2511   ctx->offset = dwarf2_per_cu_text_offset (per_cu);
2512   ctx->baton = &baton;
2513   ctx->funcs = &needs_frame_ctx_funcs;
2514
2515   dwarf_expr_eval (ctx, data, size);
2516
2517   in_reg = ctx->location == DWARF_VALUE_REGISTER;
2518
2519   if (ctx->num_pieces > 0)
2520     {
2521       int i;
2522
2523       /* If the location has several pieces, and any of them are in
2524          registers, then we will need a frame to fetch them from.  */
2525       for (i = 0; i < ctx->num_pieces; i++)
2526         if (ctx->pieces[i].location == DWARF_VALUE_REGISTER)
2527           in_reg = 1;
2528     }
2529
2530   do_cleanups (old_chain);
2531
2532   return baton.needs_frame || in_reg;
2533 }
2534
2535 /* A helper function that throws an unimplemented error mentioning a
2536    given DWARF operator.  */
2537
2538 static void
2539 unimplemented (unsigned int op)
2540 {
2541   const char *name = get_DW_OP_name (op);
2542
2543   if (name)
2544     error (_("DWARF operator %s cannot be translated to an agent expression"),
2545            name);
2546   else
2547     error (_("Unknown DWARF operator 0x%02x cannot be translated "
2548              "to an agent expression"),
2549            op);
2550 }
2551
2552 /* A helper function to convert a DWARF register to an arch register.
2553    ARCH is the architecture.
2554    DWARF_REG is the register.
2555    This will throw an exception if the DWARF register cannot be
2556    translated to an architecture register.  */
2557
2558 static int
2559 translate_register (struct gdbarch *arch, int dwarf_reg)
2560 {
2561   int reg = gdbarch_dwarf2_reg_to_regnum (arch, dwarf_reg);
2562   if (reg == -1)
2563     error (_("Unable to access DWARF register number %d"), dwarf_reg);
2564   return reg;
2565 }
2566
2567 /* A helper function that emits an access to memory.  ARCH is the
2568    target architecture.  EXPR is the expression which we are building.
2569    NBITS is the number of bits we want to read.  This emits the
2570    opcodes needed to read the memory and then extract the desired
2571    bits.  */
2572
2573 static void
2574 access_memory (struct gdbarch *arch, struct agent_expr *expr, ULONGEST nbits)
2575 {
2576   ULONGEST nbytes = (nbits + 7) / 8;
2577
2578   gdb_assert (nbytes > 0 && nbytes <= sizeof (LONGEST));
2579
2580   if (expr->tracing)
2581     ax_trace_quick (expr, nbytes);
2582
2583   if (nbits <= 8)
2584     ax_simple (expr, aop_ref8);
2585   else if (nbits <= 16)
2586     ax_simple (expr, aop_ref16);
2587   else if (nbits <= 32)
2588     ax_simple (expr, aop_ref32);
2589   else
2590     ax_simple (expr, aop_ref64);
2591
2592   /* If we read exactly the number of bytes we wanted, we're done.  */
2593   if (8 * nbytes == nbits)
2594     return;
2595
2596   if (gdbarch_bits_big_endian (arch))
2597     {
2598       /* On a bits-big-endian machine, we want the high-order
2599          NBITS.  */
2600       ax_const_l (expr, 8 * nbytes - nbits);
2601       ax_simple (expr, aop_rsh_unsigned);
2602     }
2603   else
2604     {
2605       /* On a bits-little-endian box, we want the low-order NBITS.  */
2606       ax_zero_ext (expr, nbits);
2607     }
2608 }
2609
2610 /* A helper function to return the frame's PC.  */
2611
2612 static CORE_ADDR
2613 get_ax_pc (void *baton)
2614 {
2615   struct agent_expr *expr = baton;
2616
2617   return expr->scope;
2618 }
2619
2620 /* Compile a DWARF location expression to an agent expression.
2621    
2622    EXPR is the agent expression we are building.
2623    LOC is the agent value we modify.
2624    ARCH is the architecture.
2625    ADDR_SIZE is the size of addresses, in bytes.
2626    OP_PTR is the start of the location expression.
2627    OP_END is one past the last byte of the location expression.
2628    
2629    This will throw an exception for various kinds of errors -- for
2630    example, if the expression cannot be compiled, or if the expression
2631    is invalid.  */
2632
2633 void
2634 dwarf2_compile_expr_to_ax (struct agent_expr *expr, struct axs_value *loc,
2635                            struct gdbarch *arch, unsigned int addr_size,
2636                            const gdb_byte *op_ptr, const gdb_byte *op_end,
2637                            struct dwarf2_per_cu_data *per_cu)
2638 {
2639   struct cleanup *cleanups;
2640   int i, *offsets;
2641   VEC(int) *dw_labels = NULL, *patches = NULL;
2642   const gdb_byte * const base = op_ptr;
2643   const gdb_byte *previous_piece = op_ptr;
2644   enum bfd_endian byte_order = gdbarch_byte_order (arch);
2645   ULONGEST bits_collected = 0;
2646   unsigned int addr_size_bits = 8 * addr_size;
2647   int bits_big_endian = gdbarch_bits_big_endian (arch);
2648
2649   offsets = xmalloc ((op_end - op_ptr) * sizeof (int));
2650   cleanups = make_cleanup (xfree, offsets);
2651
2652   for (i = 0; i < op_end - op_ptr; ++i)
2653     offsets[i] = -1;
2654
2655   make_cleanup (VEC_cleanup (int), &dw_labels);
2656   make_cleanup (VEC_cleanup (int), &patches);
2657
2658   /* By default we are making an address.  */
2659   loc->kind = axs_lvalue_memory;
2660
2661   while (op_ptr < op_end)
2662     {
2663       enum dwarf_location_atom op = *op_ptr;
2664       uint64_t uoffset, reg;
2665       int64_t offset;
2666       int i;
2667
2668       offsets[op_ptr - base] = expr->len;
2669       ++op_ptr;
2670
2671       /* Our basic approach to code generation is to map DWARF
2672          operations directly to AX operations.  However, there are
2673          some differences.
2674
2675          First, DWARF works on address-sized units, but AX always uses
2676          LONGEST.  For most operations we simply ignore this
2677          difference; instead we generate sign extensions as needed
2678          before division and comparison operations.  It would be nice
2679          to omit the sign extensions, but there is no way to determine
2680          the size of the target's LONGEST.  (This code uses the size
2681          of the host LONGEST in some cases -- that is a bug but it is
2682          difficult to fix.)
2683
2684          Second, some DWARF operations cannot be translated to AX.
2685          For these we simply fail.  See
2686          http://sourceware.org/bugzilla/show_bug.cgi?id=11662.  */
2687       switch (op)
2688         {
2689         case DW_OP_lit0:
2690         case DW_OP_lit1:
2691         case DW_OP_lit2:
2692         case DW_OP_lit3:
2693         case DW_OP_lit4:
2694         case DW_OP_lit5:
2695         case DW_OP_lit6:
2696         case DW_OP_lit7:
2697         case DW_OP_lit8:
2698         case DW_OP_lit9:
2699         case DW_OP_lit10:
2700         case DW_OP_lit11:
2701         case DW_OP_lit12:
2702         case DW_OP_lit13:
2703         case DW_OP_lit14:
2704         case DW_OP_lit15:
2705         case DW_OP_lit16:
2706         case DW_OP_lit17:
2707         case DW_OP_lit18:
2708         case DW_OP_lit19:
2709         case DW_OP_lit20:
2710         case DW_OP_lit21:
2711         case DW_OP_lit22:
2712         case DW_OP_lit23:
2713         case DW_OP_lit24:
2714         case DW_OP_lit25:
2715         case DW_OP_lit26:
2716         case DW_OP_lit27:
2717         case DW_OP_lit28:
2718         case DW_OP_lit29:
2719         case DW_OP_lit30:
2720         case DW_OP_lit31:
2721           ax_const_l (expr, op - DW_OP_lit0);
2722           break;
2723
2724         case DW_OP_addr:
2725           uoffset = extract_unsigned_integer (op_ptr, addr_size, byte_order);
2726           op_ptr += addr_size;
2727           /* Some versions of GCC emit DW_OP_addr before
2728              DW_OP_GNU_push_tls_address.  In this case the value is an
2729              index, not an address.  We don't support things like
2730              branching between the address and the TLS op.  */
2731           if (op_ptr >= op_end || *op_ptr != DW_OP_GNU_push_tls_address)
2732             uoffset += dwarf2_per_cu_text_offset (per_cu);
2733           ax_const_l (expr, uoffset);
2734           break;
2735
2736         case DW_OP_const1u:
2737           ax_const_l (expr, extract_unsigned_integer (op_ptr, 1, byte_order));
2738           op_ptr += 1;
2739           break;
2740         case DW_OP_const1s:
2741           ax_const_l (expr, extract_signed_integer (op_ptr, 1, byte_order));
2742           op_ptr += 1;
2743           break;
2744         case DW_OP_const2u:
2745           ax_const_l (expr, extract_unsigned_integer (op_ptr, 2, byte_order));
2746           op_ptr += 2;
2747           break;
2748         case DW_OP_const2s:
2749           ax_const_l (expr, extract_signed_integer (op_ptr, 2, byte_order));
2750           op_ptr += 2;
2751           break;
2752         case DW_OP_const4u:
2753           ax_const_l (expr, extract_unsigned_integer (op_ptr, 4, byte_order));
2754           op_ptr += 4;
2755           break;
2756         case DW_OP_const4s:
2757           ax_const_l (expr, extract_signed_integer (op_ptr, 4, byte_order));
2758           op_ptr += 4;
2759           break;
2760         case DW_OP_const8u:
2761           ax_const_l (expr, extract_unsigned_integer (op_ptr, 8, byte_order));
2762           op_ptr += 8;
2763           break;
2764         case DW_OP_const8s:
2765           ax_const_l (expr, extract_signed_integer (op_ptr, 8, byte_order));
2766           op_ptr += 8;
2767           break;
2768         case DW_OP_constu:
2769           op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
2770           ax_const_l (expr, uoffset);
2771           break;
2772         case DW_OP_consts:
2773           op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
2774           ax_const_l (expr, offset);
2775           break;
2776
2777         case DW_OP_reg0:
2778         case DW_OP_reg1:
2779         case DW_OP_reg2:
2780         case DW_OP_reg3:
2781         case DW_OP_reg4:
2782         case DW_OP_reg5:
2783         case DW_OP_reg6:
2784         case DW_OP_reg7:
2785         case DW_OP_reg8:
2786         case DW_OP_reg9:
2787         case DW_OP_reg10:
2788         case DW_OP_reg11:
2789         case DW_OP_reg12:
2790         case DW_OP_reg13:
2791         case DW_OP_reg14:
2792         case DW_OP_reg15:
2793         case DW_OP_reg16:
2794         case DW_OP_reg17:
2795         case DW_OP_reg18:
2796         case DW_OP_reg19:
2797         case DW_OP_reg20:
2798         case DW_OP_reg21:
2799         case DW_OP_reg22:
2800         case DW_OP_reg23:
2801         case DW_OP_reg24:
2802         case DW_OP_reg25:
2803         case DW_OP_reg26:
2804         case DW_OP_reg27:
2805         case DW_OP_reg28:
2806         case DW_OP_reg29:
2807         case DW_OP_reg30:
2808         case DW_OP_reg31:
2809           dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_regx");
2810           loc->u.reg = translate_register (arch, op - DW_OP_reg0);
2811           loc->kind = axs_lvalue_register;
2812           break;
2813
2814         case DW_OP_regx:
2815           op_ptr = safe_read_uleb128 (op_ptr, op_end, &reg);
2816           dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_regx");
2817           loc->u.reg = translate_register (arch, reg);
2818           loc->kind = axs_lvalue_register;
2819           break;
2820
2821         case DW_OP_implicit_value:
2822           {
2823             uint64_t len;
2824
2825             op_ptr = safe_read_uleb128 (op_ptr, op_end, &len);
2826             if (op_ptr + len > op_end)
2827               error (_("DW_OP_implicit_value: too few bytes available."));
2828             if (len > sizeof (ULONGEST))
2829               error (_("Cannot translate DW_OP_implicit_value of %d bytes"),
2830                      (int) len);
2831
2832             ax_const_l (expr, extract_unsigned_integer (op_ptr, len,
2833                                                         byte_order));
2834             op_ptr += len;
2835             dwarf_expr_require_composition (op_ptr, op_end,
2836                                             "DW_OP_implicit_value");
2837
2838             loc->kind = axs_rvalue;
2839           }
2840           break;
2841
2842         case DW_OP_stack_value:
2843           dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_stack_value");
2844           loc->kind = axs_rvalue;
2845           break;
2846
2847         case DW_OP_breg0:
2848         case DW_OP_breg1:
2849         case DW_OP_breg2:
2850         case DW_OP_breg3:
2851         case DW_OP_breg4:
2852         case DW_OP_breg5:
2853         case DW_OP_breg6:
2854         case DW_OP_breg7:
2855         case DW_OP_breg8:
2856         case DW_OP_breg9:
2857         case DW_OP_breg10:
2858         case DW_OP_breg11:
2859         case DW_OP_breg12:
2860         case DW_OP_breg13:
2861         case DW_OP_breg14:
2862         case DW_OP_breg15:
2863         case DW_OP_breg16:
2864         case DW_OP_breg17:
2865         case DW_OP_breg18:
2866         case DW_OP_breg19:
2867         case DW_OP_breg20:
2868         case DW_OP_breg21:
2869         case DW_OP_breg22:
2870         case DW_OP_breg23:
2871         case DW_OP_breg24:
2872         case DW_OP_breg25:
2873         case DW_OP_breg26:
2874         case DW_OP_breg27:
2875         case DW_OP_breg28:
2876         case DW_OP_breg29:
2877         case DW_OP_breg30:
2878         case DW_OP_breg31:
2879           op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
2880           i = translate_register (arch, op - DW_OP_breg0);
2881           ax_reg (expr, i);
2882           if (offset != 0)
2883             {
2884               ax_const_l (expr, offset);
2885               ax_simple (expr, aop_add);
2886             }
2887           break;
2888         case DW_OP_bregx:
2889           {
2890             op_ptr = safe_read_uleb128 (op_ptr, op_end, &reg);
2891             op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
2892             i = translate_register (arch, reg);
2893             ax_reg (expr, i);
2894             if (offset != 0)
2895               {
2896                 ax_const_l (expr, offset);
2897                 ax_simple (expr, aop_add);
2898               }
2899           }
2900           break;
2901         case DW_OP_fbreg:
2902           {
2903             const gdb_byte *datastart;
2904             size_t datalen;
2905             struct block *b;
2906             struct symbol *framefunc;
2907
2908             b = block_for_pc (expr->scope);
2909
2910             if (!b)
2911               error (_("No block found for address"));
2912
2913             framefunc = block_linkage_function (b);
2914
2915             if (!framefunc)
2916               error (_("No function found for block"));
2917
2918             dwarf_expr_frame_base_1 (framefunc, expr->scope,
2919                                      &datastart, &datalen);
2920
2921             op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
2922             dwarf2_compile_expr_to_ax (expr, loc, arch, addr_size, datastart,
2923                                        datastart + datalen, per_cu);
2924             if (loc->kind == axs_lvalue_register)
2925               require_rvalue (expr, loc);
2926
2927             if (offset != 0)
2928               {
2929                 ax_const_l (expr, offset);
2930                 ax_simple (expr, aop_add);
2931               }
2932
2933             loc->kind = axs_lvalue_memory;
2934           }
2935           break;
2936
2937         case DW_OP_dup:
2938           ax_simple (expr, aop_dup);
2939           break;
2940
2941         case DW_OP_drop:
2942           ax_simple (expr, aop_pop);
2943           break;
2944
2945         case DW_OP_pick:
2946           offset = *op_ptr++;
2947           ax_pick (expr, offset);
2948           break;
2949           
2950         case DW_OP_swap:
2951           ax_simple (expr, aop_swap);
2952           break;
2953
2954         case DW_OP_over:
2955           ax_pick (expr, 1);
2956           break;
2957
2958         case DW_OP_rot:
2959           ax_simple (expr, aop_rot);
2960           break;
2961
2962         case DW_OP_deref:
2963         case DW_OP_deref_size:
2964           {
2965             int size;
2966
2967             if (op == DW_OP_deref_size)
2968               size = *op_ptr++;
2969             else
2970               size = addr_size;
2971
2972             if (size != 1 && size != 2 && size != 4 && size != 8)
2973               error (_("Unsupported size %d in %s"),
2974                      size, get_DW_OP_name (op));
2975             access_memory (arch, expr, size * TARGET_CHAR_BIT);
2976           }
2977           break;
2978
2979         case DW_OP_abs:
2980           /* Sign extend the operand.  */
2981           ax_ext (expr, addr_size_bits);
2982           ax_simple (expr, aop_dup);
2983           ax_const_l (expr, 0);
2984           ax_simple (expr, aop_less_signed);
2985           ax_simple (expr, aop_log_not);
2986           i = ax_goto (expr, aop_if_goto);
2987           /* We have to emit 0 - X.  */
2988           ax_const_l (expr, 0);
2989           ax_simple (expr, aop_swap);
2990           ax_simple (expr, aop_sub);
2991           ax_label (expr, i, expr->len);
2992           break;
2993
2994         case DW_OP_neg:
2995           /* No need to sign extend here.  */
2996           ax_const_l (expr, 0);
2997           ax_simple (expr, aop_swap);
2998           ax_simple (expr, aop_sub);
2999           break;
3000
3001         case DW_OP_not:
3002           /* Sign extend the operand.  */
3003           ax_ext (expr, addr_size_bits);
3004           ax_simple (expr, aop_bit_not);
3005           break;
3006
3007         case DW_OP_plus_uconst:
3008           op_ptr = safe_read_uleb128 (op_ptr, op_end, &reg);
3009           /* It would be really weird to emit `DW_OP_plus_uconst 0',
3010              but we micro-optimize anyhow.  */
3011           if (reg != 0)
3012             {
3013               ax_const_l (expr, reg);
3014               ax_simple (expr, aop_add);
3015             }
3016           break;
3017
3018         case DW_OP_and:
3019           ax_simple (expr, aop_bit_and);
3020           break;
3021
3022         case DW_OP_div:
3023           /* Sign extend the operands.  */
3024           ax_ext (expr, addr_size_bits);
3025           ax_simple (expr, aop_swap);
3026           ax_ext (expr, addr_size_bits);
3027           ax_simple (expr, aop_swap);
3028           ax_simple (expr, aop_div_signed);
3029           break;
3030
3031         case DW_OP_minus:
3032           ax_simple (expr, aop_sub);
3033           break;
3034
3035         case DW_OP_mod:
3036           ax_simple (expr, aop_rem_unsigned);
3037           break;
3038
3039         case DW_OP_mul:
3040           ax_simple (expr, aop_mul);
3041           break;
3042
3043         case DW_OP_or:
3044           ax_simple (expr, aop_bit_or);
3045           break;
3046
3047         case DW_OP_plus:
3048           ax_simple (expr, aop_add);
3049           break;
3050
3051         case DW_OP_shl:
3052           ax_simple (expr, aop_lsh);
3053           break;
3054
3055         case DW_OP_shr:
3056           ax_simple (expr, aop_rsh_unsigned);
3057           break;
3058
3059         case DW_OP_shra:
3060           ax_simple (expr, aop_rsh_signed);
3061           break;
3062
3063         case DW_OP_xor:
3064           ax_simple (expr, aop_bit_xor);
3065           break;
3066
3067         case DW_OP_le:
3068           /* Sign extend the operands.  */
3069           ax_ext (expr, addr_size_bits);
3070           ax_simple (expr, aop_swap);
3071           ax_ext (expr, addr_size_bits);
3072           /* Note no swap here: A <= B is !(B < A).  */
3073           ax_simple (expr, aop_less_signed);
3074           ax_simple (expr, aop_log_not);
3075           break;
3076
3077         case DW_OP_ge:
3078           /* Sign extend the operands.  */
3079           ax_ext (expr, addr_size_bits);
3080           ax_simple (expr, aop_swap);
3081           ax_ext (expr, addr_size_bits);
3082           ax_simple (expr, aop_swap);
3083           /* A >= B is !(A < B).  */
3084           ax_simple (expr, aop_less_signed);
3085           ax_simple (expr, aop_log_not);
3086           break;
3087
3088         case DW_OP_eq:
3089           /* Sign extend the operands.  */
3090           ax_ext (expr, addr_size_bits);
3091           ax_simple (expr, aop_swap);
3092           ax_ext (expr, addr_size_bits);
3093           /* No need for a second swap here.  */
3094           ax_simple (expr, aop_equal);
3095           break;
3096
3097         case DW_OP_lt:
3098           /* Sign extend the operands.  */
3099           ax_ext (expr, addr_size_bits);
3100           ax_simple (expr, aop_swap);
3101           ax_ext (expr, addr_size_bits);
3102           ax_simple (expr, aop_swap);
3103           ax_simple (expr, aop_less_signed);
3104           break;
3105
3106         case DW_OP_gt:
3107           /* Sign extend the operands.  */
3108           ax_ext (expr, addr_size_bits);
3109           ax_simple (expr, aop_swap);
3110           ax_ext (expr, addr_size_bits);
3111           /* Note no swap here: A > B is B < A.  */
3112           ax_simple (expr, aop_less_signed);
3113           break;
3114
3115         case DW_OP_ne:
3116           /* Sign extend the operands.  */
3117           ax_ext (expr, addr_size_bits);
3118           ax_simple (expr, aop_swap);
3119           ax_ext (expr, addr_size_bits);
3120           /* No need for a swap here.  */
3121           ax_simple (expr, aop_equal);
3122           ax_simple (expr, aop_log_not);
3123           break;
3124
3125         case DW_OP_call_frame_cfa:
3126           dwarf2_compile_cfa_to_ax (expr, loc, arch, expr->scope, per_cu);
3127           loc->kind = axs_lvalue_memory;
3128           break;
3129
3130         case DW_OP_GNU_push_tls_address:
3131           unimplemented (op);
3132           break;
3133
3134         case DW_OP_skip:
3135           offset = extract_signed_integer (op_ptr, 2, byte_order);
3136           op_ptr += 2;
3137           i = ax_goto (expr, aop_goto);
3138           VEC_safe_push (int, dw_labels, op_ptr + offset - base);
3139           VEC_safe_push (int, patches, i);
3140           break;
3141
3142         case DW_OP_bra:
3143           offset = extract_signed_integer (op_ptr, 2, byte_order);
3144           op_ptr += 2;
3145           /* Zero extend the operand.  */
3146           ax_zero_ext (expr, addr_size_bits);
3147           i = ax_goto (expr, aop_if_goto);
3148           VEC_safe_push (int, dw_labels, op_ptr + offset - base);
3149           VEC_safe_push (int, patches, i);
3150           break;
3151
3152         case DW_OP_nop:
3153           break;
3154
3155         case DW_OP_piece:
3156         case DW_OP_bit_piece:
3157           {
3158             uint64_t size, offset;
3159
3160             if (op_ptr - 1 == previous_piece)
3161               error (_("Cannot translate empty pieces to agent expressions"));
3162             previous_piece = op_ptr - 1;
3163
3164             op_ptr = safe_read_uleb128 (op_ptr, op_end, &size);
3165             if (op == DW_OP_piece)
3166               {
3167                 size *= 8;
3168                 offset = 0;
3169               }
3170             else
3171               op_ptr = safe_read_uleb128 (op_ptr, op_end, &offset);
3172
3173             if (bits_collected + size > 8 * sizeof (LONGEST))
3174               error (_("Expression pieces exceed word size"));
3175
3176             /* Access the bits.  */
3177             switch (loc->kind)
3178               {
3179               case axs_lvalue_register:
3180                 ax_reg (expr, loc->u.reg);
3181                 break;
3182
3183               case axs_lvalue_memory:
3184                 /* Offset the pointer, if needed.  */
3185                 if (offset > 8)
3186                   {
3187                     ax_const_l (expr, offset / 8);
3188                     ax_simple (expr, aop_add);
3189                     offset %= 8;
3190                   }
3191                 access_memory (arch, expr, size);
3192                 break;
3193               }
3194
3195             /* For a bits-big-endian target, shift up what we already
3196                have.  For a bits-little-endian target, shift up the
3197                new data.  Note that there is a potential bug here if
3198                the DWARF expression leaves multiple values on the
3199                stack.  */
3200             if (bits_collected > 0)
3201               {
3202                 if (bits_big_endian)
3203                   {
3204                     ax_simple (expr, aop_swap);
3205                     ax_const_l (expr, size);
3206                     ax_simple (expr, aop_lsh);
3207                     /* We don't need a second swap here, because
3208                        aop_bit_or is symmetric.  */
3209                   }
3210                 else
3211                   {
3212                     ax_const_l (expr, size);
3213                     ax_simple (expr, aop_lsh);
3214                   }
3215                 ax_simple (expr, aop_bit_or);
3216               }
3217
3218             bits_collected += size;
3219             loc->kind = axs_rvalue;
3220           }
3221           break;
3222
3223         case DW_OP_GNU_uninit:
3224           unimplemented (op);
3225
3226         case DW_OP_call2:
3227         case DW_OP_call4:
3228           {
3229             struct dwarf2_locexpr_baton block;
3230             int size = (op == DW_OP_call2 ? 2 : 4);
3231             cu_offset offset;
3232
3233             uoffset = extract_unsigned_integer (op_ptr, size, byte_order);
3234             op_ptr += size;
3235
3236             offset.cu_off = uoffset;
3237             block = dwarf2_fetch_die_loc_cu_off (offset, per_cu,
3238                                                  get_ax_pc, expr);
3239
3240             /* DW_OP_call_ref is currently not supported.  */
3241             gdb_assert (block.per_cu == per_cu);
3242
3243             dwarf2_compile_expr_to_ax (expr, loc, arch, addr_size,
3244                                        block.data, block.data + block.size,
3245                                        per_cu);
3246           }
3247           break;
3248
3249         case DW_OP_call_ref:
3250           unimplemented (op);
3251
3252         default:
3253           unimplemented (op);
3254         }
3255     }
3256
3257   /* Patch all the branches we emitted.  */
3258   for (i = 0; i < VEC_length (int, patches); ++i)
3259     {
3260       int targ = offsets[VEC_index (int, dw_labels, i)];
3261       if (targ == -1)
3262         internal_error (__FILE__, __LINE__, _("invalid label"));
3263       ax_label (expr, VEC_index (int, patches, i), targ);
3264     }
3265
3266   do_cleanups (cleanups);
3267 }
3268
3269 \f
3270 /* Return the value of SYMBOL in FRAME using the DWARF-2 expression
3271    evaluator to calculate the location.  */
3272 static struct value *
3273 locexpr_read_variable (struct symbol *symbol, struct frame_info *frame)
3274 {
3275   struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
3276   struct value *val;
3277
3278   val = dwarf2_evaluate_loc_desc (SYMBOL_TYPE (symbol), frame, dlbaton->data,
3279                                   dlbaton->size, dlbaton->per_cu);
3280
3281   return val;
3282 }
3283
3284 /* Return the value of SYMBOL in FRAME at (callee) FRAME's function
3285    entry.  SYMBOL should be a function parameter, otherwise NO_ENTRY_VALUE_ERROR
3286    will be thrown.  */
3287
3288 static struct value *
3289 locexpr_read_variable_at_entry (struct symbol *symbol, struct frame_info *frame)
3290 {
3291   struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
3292
3293   return value_of_dwarf_block_entry (SYMBOL_TYPE (symbol), frame, dlbaton->data,
3294                                      dlbaton->size);
3295 }
3296
3297 /* Return non-zero iff we need a frame to evaluate SYMBOL.  */
3298 static int
3299 locexpr_read_needs_frame (struct symbol *symbol)
3300 {
3301   struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
3302
3303   return dwarf2_loc_desc_needs_frame (dlbaton->data, dlbaton->size,
3304                                       dlbaton->per_cu);
3305 }
3306
3307 /* Return true if DATA points to the end of a piece.  END is one past
3308    the last byte in the expression.  */
3309
3310 static int
3311 piece_end_p (const gdb_byte *data, const gdb_byte *end)
3312 {
3313   return data == end || data[0] == DW_OP_piece || data[0] == DW_OP_bit_piece;
3314 }
3315
3316 /* Helper for locexpr_describe_location_piece that finds the name of a
3317    DWARF register.  */
3318
3319 static const char *
3320 locexpr_regname (struct gdbarch *gdbarch, int dwarf_regnum)
3321 {
3322   int regnum;
3323
3324   regnum = gdbarch_dwarf2_reg_to_regnum (gdbarch, dwarf_regnum);
3325   return gdbarch_register_name (gdbarch, regnum);
3326 }
3327
3328 /* Nicely describe a single piece of a location, returning an updated
3329    position in the bytecode sequence.  This function cannot recognize
3330    all locations; if a location is not recognized, it simply returns
3331    DATA.  If there is an error during reading, e.g. we run off the end
3332    of the buffer, an error is thrown.  */
3333
3334 static const gdb_byte *
3335 locexpr_describe_location_piece (struct symbol *symbol, struct ui_file *stream,
3336                                  CORE_ADDR addr, struct objfile *objfile,
3337                                  struct dwarf2_per_cu_data *per_cu,
3338                                  const gdb_byte *data, const gdb_byte *end,
3339                                  unsigned int addr_size)
3340 {
3341   struct gdbarch *gdbarch = get_objfile_arch (objfile);
3342   size_t leb128_size;
3343
3344   if (data[0] >= DW_OP_reg0 && data[0] <= DW_OP_reg31)
3345     {
3346       fprintf_filtered (stream, _("a variable in $%s"),
3347                         locexpr_regname (gdbarch, data[0] - DW_OP_reg0));
3348       data += 1;
3349     }
3350   else if (data[0] == DW_OP_regx)
3351     {
3352       uint64_t reg;
3353
3354       data = safe_read_uleb128 (data + 1, end, &reg);
3355       fprintf_filtered (stream, _("a variable in $%s"),
3356                         locexpr_regname (gdbarch, reg));
3357     }
3358   else if (data[0] == DW_OP_fbreg)
3359     {
3360       struct block *b;
3361       struct symbol *framefunc;
3362       int frame_reg = 0;
3363       int64_t frame_offset;
3364       const gdb_byte *base_data, *new_data, *save_data = data;
3365       size_t base_size;
3366       int64_t base_offset = 0;
3367
3368       new_data = safe_read_sleb128 (data + 1, end, &frame_offset);
3369       if (!piece_end_p (new_data, end))
3370         return data;
3371       data = new_data;
3372
3373       b = block_for_pc (addr);
3374
3375       if (!b)
3376         error (_("No block found for address for symbol \"%s\"."),
3377                SYMBOL_PRINT_NAME (symbol));
3378
3379       framefunc = block_linkage_function (b);
3380
3381       if (!framefunc)
3382         error (_("No function found for block for symbol \"%s\"."),
3383                SYMBOL_PRINT_NAME (symbol));
3384
3385       dwarf_expr_frame_base_1 (framefunc, addr, &base_data, &base_size);
3386
3387       if (base_data[0] >= DW_OP_breg0 && base_data[0] <= DW_OP_breg31)
3388         {
3389           const gdb_byte *buf_end;
3390           
3391           frame_reg = base_data[0] - DW_OP_breg0;
3392           buf_end = safe_read_sleb128 (base_data + 1, base_data + base_size,
3393                                        &base_offset);
3394           if (buf_end != base_data + base_size)
3395             error (_("Unexpected opcode after "
3396                      "DW_OP_breg%u for symbol \"%s\"."),
3397                    frame_reg, SYMBOL_PRINT_NAME (symbol));
3398         }
3399       else if (base_data[0] >= DW_OP_reg0 && base_data[0] <= DW_OP_reg31)
3400         {
3401           /* The frame base is just the register, with no offset.  */
3402           frame_reg = base_data[0] - DW_OP_reg0;
3403           base_offset = 0;
3404         }
3405       else
3406         {
3407           /* We don't know what to do with the frame base expression,
3408              so we can't trace this variable; give up.  */
3409           return save_data;
3410         }
3411
3412       fprintf_filtered (stream,
3413                         _("a variable at frame base reg $%s offset %s+%s"),
3414                         locexpr_regname (gdbarch, frame_reg),
3415                         plongest (base_offset), plongest (frame_offset));
3416     }
3417   else if (data[0] >= DW_OP_breg0 && data[0] <= DW_OP_breg31
3418            && piece_end_p (data, end))
3419     {
3420       int64_t offset;
3421
3422       data = safe_read_sleb128 (data + 1, end, &offset);
3423
3424       fprintf_filtered (stream,
3425                         _("a variable at offset %s from base reg $%s"),
3426                         plongest (offset),
3427                         locexpr_regname (gdbarch, data[0] - DW_OP_breg0));
3428     }
3429
3430   /* The location expression for a TLS variable looks like this (on a
3431      64-bit LE machine):
3432
3433      DW_AT_location    : 10 byte block: 3 4 0 0 0 0 0 0 0 e0
3434                         (DW_OP_addr: 4; DW_OP_GNU_push_tls_address)
3435
3436      0x3 is the encoding for DW_OP_addr, which has an operand as long
3437      as the size of an address on the target machine (here is 8
3438      bytes).  Note that more recent version of GCC emit DW_OP_const4u
3439      or DW_OP_const8u, depending on address size, rather than
3440      DW_OP_addr.  0xe0 is the encoding for DW_OP_GNU_push_tls_address.
3441      The operand represents the offset at which the variable is within
3442      the thread local storage.  */
3443
3444   else if (data + 1 + addr_size < end
3445            && (data[0] == DW_OP_addr
3446                || (addr_size == 4 && data[0] == DW_OP_const4u)
3447                || (addr_size == 8 && data[0] == DW_OP_const8u))
3448            && data[1 + addr_size] == DW_OP_GNU_push_tls_address
3449            && piece_end_p (data + 2 + addr_size, end))
3450     {
3451       ULONGEST offset;
3452       offset = extract_unsigned_integer (data + 1, addr_size,
3453                                          gdbarch_byte_order (gdbarch));
3454
3455       fprintf_filtered (stream, 
3456                         _("a thread-local variable at offset 0x%s "
3457                           "in the thread-local storage for `%s'"),
3458                         phex_nz (offset, addr_size), objfile->name);
3459
3460       data += 1 + addr_size + 1;
3461     }
3462
3463   /* With -gsplit-dwarf a TLS variable can also look like this:
3464      DW_AT_location    : 3 byte block: fc 4 e0
3465                         (DW_OP_GNU_const_index: 4;
3466                          DW_OP_GNU_push_tls_address)  */
3467   else if (data + 3 <= end
3468            && data + 1 + (leb128_size = skip_leb128 (data + 1, end)) < end
3469            && data[0] == DW_OP_GNU_const_index
3470            && leb128_size > 0
3471            && data[1 + leb128_size] == DW_OP_GNU_push_tls_address
3472            && piece_end_p (data + 2 + leb128_size, end))
3473     {
3474       uint64_t offset;
3475
3476       data = safe_read_uleb128 (data + 1, end, &offset);
3477       offset = dwarf2_read_addr_index (per_cu, offset);
3478       fprintf_filtered (stream, 
3479                         _("a thread-local variable at offset 0x%s "
3480                           "in the thread-local storage for `%s'"),
3481                         phex_nz (offset, addr_size), objfile->name);
3482       ++data;
3483     }
3484
3485   else if (data[0] >= DW_OP_lit0
3486            && data[0] <= DW_OP_lit31
3487            && data + 1 < end
3488            && data[1] == DW_OP_stack_value)
3489     {
3490       fprintf_filtered (stream, _("the constant %d"), data[0] - DW_OP_lit0);
3491       data += 2;
3492     }
3493
3494   return data;
3495 }
3496
3497 /* Disassemble an expression, stopping at the end of a piece or at the
3498    end of the expression.  Returns a pointer to the next unread byte
3499    in the input expression.  If ALL is nonzero, then this function
3500    will keep going until it reaches the end of the expression.
3501    If there is an error during reading, e.g. we run off the end
3502    of the buffer, an error is thrown.  */
3503
3504 static const gdb_byte *
3505 disassemble_dwarf_expression (struct ui_file *stream,
3506                               struct gdbarch *arch, unsigned int addr_size,
3507                               int offset_size, const gdb_byte *start,
3508                               const gdb_byte *data, const gdb_byte *end,
3509                               int indent, int all,
3510                               struct dwarf2_per_cu_data *per_cu)
3511 {
3512   while (data < end
3513          && (all
3514              || (data[0] != DW_OP_piece && data[0] != DW_OP_bit_piece)))
3515     {
3516       enum dwarf_location_atom op = *data++;
3517       uint64_t ul;
3518       int64_t l;
3519       const char *name;
3520
3521       name = get_DW_OP_name (op);
3522
3523       if (!name)
3524         error (_("Unrecognized DWARF opcode 0x%02x at %ld"),
3525                op, (long) (data - 1 - start));
3526       fprintf_filtered (stream, "  %*ld: %s", indent + 4,
3527                         (long) (data - 1 - start), name);
3528
3529       switch (op)
3530         {
3531         case DW_OP_addr:
3532           ul = extract_unsigned_integer (data, addr_size,
3533                                          gdbarch_byte_order (arch));
3534           data += addr_size;
3535           fprintf_filtered (stream, " 0x%s", phex_nz (ul, addr_size));
3536           break;
3537
3538         case DW_OP_const1u:
3539           ul = extract_unsigned_integer (data, 1, gdbarch_byte_order (arch));
3540           data += 1;
3541           fprintf_filtered (stream, " %s", pulongest (ul));
3542           break;
3543         case DW_OP_const1s:
3544           l = extract_signed_integer (data, 1, gdbarch_byte_order (arch));
3545           data += 1;
3546           fprintf_filtered (stream, " %s", plongest (l));
3547           break;
3548         case DW_OP_const2u:
3549           ul = extract_unsigned_integer (data, 2, gdbarch_byte_order (arch));
3550           data += 2;
3551           fprintf_filtered (stream, " %s", pulongest (ul));
3552           break;
3553         case DW_OP_const2s:
3554           l = extract_signed_integer (data, 2, gdbarch_byte_order (arch));
3555           data += 2;
3556           fprintf_filtered (stream, " %s", plongest (l));
3557           break;
3558         case DW_OP_const4u:
3559           ul = extract_unsigned_integer (data, 4, gdbarch_byte_order (arch));
3560           data += 4;
3561           fprintf_filtered (stream, " %s", pulongest (ul));
3562           break;
3563         case DW_OP_const4s:
3564           l = extract_signed_integer (data, 4, gdbarch_byte_order (arch));
3565           data += 4;
3566           fprintf_filtered (stream, " %s", plongest (l));
3567           break;
3568         case DW_OP_const8u:
3569           ul = extract_unsigned_integer (data, 8, gdbarch_byte_order (arch));
3570           data += 8;
3571           fprintf_filtered (stream, " %s", pulongest (ul));
3572           break;
3573         case DW_OP_const8s:
3574           l = extract_signed_integer (data, 8, gdbarch_byte_order (arch));
3575           data += 8;
3576           fprintf_filtered (stream, " %s", plongest (l));
3577           break;
3578         case DW_OP_constu:
3579           data = safe_read_uleb128 (data, end, &ul);
3580           fprintf_filtered (stream, " %s", pulongest (ul));
3581           break;
3582         case DW_OP_consts:
3583           data = safe_read_sleb128 (data, end, &l);
3584           fprintf_filtered (stream, " %s", plongest (l));
3585           break;
3586
3587         case DW_OP_reg0:
3588         case DW_OP_reg1:
3589         case DW_OP_reg2:
3590         case DW_OP_reg3:
3591         case DW_OP_reg4:
3592         case DW_OP_reg5:
3593         case DW_OP_reg6:
3594         case DW_OP_reg7:
3595         case DW_OP_reg8:
3596         case DW_OP_reg9:
3597         case DW_OP_reg10:
3598         case DW_OP_reg11:
3599         case DW_OP_reg12:
3600         case DW_OP_reg13:
3601         case DW_OP_reg14:
3602         case DW_OP_reg15:
3603         case DW_OP_reg16:
3604         case DW_OP_reg17:
3605         case DW_OP_reg18:
3606         case DW_OP_reg19:
3607         case DW_OP_reg20:
3608         case DW_OP_reg21:
3609         case DW_OP_reg22:
3610         case DW_OP_reg23:
3611         case DW_OP_reg24:
3612         case DW_OP_reg25:
3613         case DW_OP_reg26:
3614         case DW_OP_reg27:
3615         case DW_OP_reg28:
3616         case DW_OP_reg29:
3617         case DW_OP_reg30:
3618         case DW_OP_reg31:
3619           fprintf_filtered (stream, " [$%s]",
3620                             locexpr_regname (arch, op - DW_OP_reg0));
3621           break;
3622
3623         case DW_OP_regx:
3624           data = safe_read_uleb128 (data, end, &ul);
3625           fprintf_filtered (stream, " %s [$%s]", pulongest (ul),
3626                             locexpr_regname (arch, (int) ul));
3627           break;
3628
3629         case DW_OP_implicit_value:
3630           data = safe_read_uleb128 (data, end, &ul);
3631           data += ul;
3632           fprintf_filtered (stream, " %s", pulongest (ul));
3633           break;
3634
3635         case DW_OP_breg0:
3636         case DW_OP_breg1:
3637         case DW_OP_breg2:
3638         case DW_OP_breg3:
3639         case DW_OP_breg4:
3640         case DW_OP_breg5:
3641         case DW_OP_breg6:
3642         case DW_OP_breg7:
3643         case DW_OP_breg8:
3644         case DW_OP_breg9:
3645         case DW_OP_breg10:
3646         case DW_OP_breg11:
3647         case DW_OP_breg12:
3648         case DW_OP_breg13:
3649         case DW_OP_breg14:
3650         case DW_OP_breg15:
3651         case DW_OP_breg16:
3652         case DW_OP_breg17:
3653         case DW_OP_breg18:
3654         case DW_OP_breg19:
3655         case DW_OP_breg20:
3656         case DW_OP_breg21:
3657         case DW_OP_breg22:
3658         case DW_OP_breg23:
3659         case DW_OP_breg24:
3660         case DW_OP_breg25:
3661         case DW_OP_breg26:
3662         case DW_OP_breg27:
3663         case DW_OP_breg28:
3664         case DW_OP_breg29:
3665         case DW_OP_breg30:
3666         case DW_OP_breg31:
3667           data = safe_read_sleb128 (data, end, &l);
3668           fprintf_filtered (stream, " %s [$%s]", plongest (l),
3669                             locexpr_regname (arch, op - DW_OP_breg0));
3670           break;
3671
3672         case DW_OP_bregx:
3673           data = safe_read_uleb128 (data, end, &ul);
3674           data = safe_read_sleb128 (data, end, &l);
3675           fprintf_filtered (stream, " register %s [$%s] offset %s",
3676                             pulongest (ul),
3677                             locexpr_regname (arch, (int) ul),
3678                             plongest (l));
3679           break;
3680
3681         case DW_OP_fbreg:
3682           data = safe_read_sleb128 (data, end, &l);
3683           fprintf_filtered (stream, " %s", plongest (l));
3684           break;
3685
3686         case DW_OP_xderef_size:
3687         case DW_OP_deref_size:
3688         case DW_OP_pick:
3689           fprintf_filtered (stream, " %d", *data);
3690           ++data;
3691           break;
3692
3693         case DW_OP_plus_uconst:
3694           data = safe_read_uleb128 (data, end, &ul);
3695           fprintf_filtered (stream, " %s", pulongest (ul));
3696           break;
3697
3698         case DW_OP_skip:
3699           l = extract_signed_integer (data, 2, gdbarch_byte_order (arch));
3700           data += 2;
3701           fprintf_filtered (stream, " to %ld",
3702                             (long) (data + l - start));
3703           break;
3704
3705         case DW_OP_bra:
3706           l = extract_signed_integer (data, 2, gdbarch_byte_order (arch));
3707           data += 2;
3708           fprintf_filtered (stream, " %ld",
3709                             (long) (data + l - start));
3710           break;
3711
3712         case DW_OP_call2:
3713           ul = extract_unsigned_integer (data, 2, gdbarch_byte_order (arch));
3714           data += 2;
3715           fprintf_filtered (stream, " offset %s", phex_nz (ul, 2));
3716           break;
3717
3718         case DW_OP_call4:
3719           ul = extract_unsigned_integer (data, 4, gdbarch_byte_order (arch));
3720           data += 4;
3721           fprintf_filtered (stream, " offset %s", phex_nz (ul, 4));
3722           break;
3723
3724         case DW_OP_call_ref:
3725           ul = extract_unsigned_integer (data, offset_size,
3726                                          gdbarch_byte_order (arch));
3727           data += offset_size;
3728           fprintf_filtered (stream, " offset %s", phex_nz (ul, offset_size));
3729           break;
3730
3731         case DW_OP_piece:
3732           data = safe_read_uleb128 (data, end, &ul);
3733           fprintf_filtered (stream, " %s (bytes)", pulongest (ul));
3734           break;
3735
3736         case DW_OP_bit_piece:
3737           {
3738             uint64_t offset;
3739
3740             data = safe_read_uleb128 (data, end, &ul);
3741             data = safe_read_uleb128 (data, end, &offset);
3742             fprintf_filtered (stream, " size %s offset %s (bits)",
3743                               pulongest (ul), pulongest (offset));
3744           }
3745           break;
3746
3747         case DW_OP_GNU_implicit_pointer:
3748           {
3749             ul = extract_unsigned_integer (data, offset_size,
3750                                            gdbarch_byte_order (arch));
3751             data += offset_size;
3752
3753             data = safe_read_sleb128 (data, end, &l);
3754
3755             fprintf_filtered (stream, " DIE %s offset %s",
3756                               phex_nz (ul, offset_size),
3757                               plongest (l));
3758           }
3759           break;
3760
3761         case DW_OP_GNU_deref_type:
3762           {
3763             int addr_size = *data++;
3764             cu_offset offset;
3765             struct type *type;
3766
3767             data = safe_read_uleb128 (data, end, &ul);
3768             offset.cu_off = ul;
3769             type = dwarf2_get_die_type (offset, per_cu);
3770             fprintf_filtered (stream, "<");
3771             type_print (type, "", stream, -1);
3772             fprintf_filtered (stream, " [0x%s]> %d", phex_nz (offset.cu_off, 0),
3773                               addr_size);
3774           }
3775           break;
3776
3777         case DW_OP_GNU_const_type:
3778           {
3779             cu_offset type_die;
3780             struct type *type;
3781
3782             data = safe_read_uleb128 (data, end, &ul);
3783             type_die.cu_off = ul;
3784             type = dwarf2_get_die_type (type_die, per_cu);
3785             fprintf_filtered (stream, "<");
3786             type_print (type, "", stream, -1);
3787             fprintf_filtered (stream, " [0x%s]>", phex_nz (type_die.cu_off, 0));
3788           }
3789           break;
3790
3791         case DW_OP_GNU_regval_type:
3792           {
3793             uint64_t reg;
3794             cu_offset type_die;
3795             struct type *type;
3796
3797             data = safe_read_uleb128 (data, end, &reg);
3798             data = safe_read_uleb128 (data, end, &ul);
3799             type_die.cu_off = ul;
3800
3801             type = dwarf2_get_die_type (type_die, per_cu);
3802             fprintf_filtered (stream, "<");
3803             type_print (type, "", stream, -1);
3804             fprintf_filtered (stream, " [0x%s]> [$%s]",
3805                               phex_nz (type_die.cu_off, 0),
3806                               locexpr_regname (arch, reg));
3807           }
3808           break;
3809
3810         case DW_OP_GNU_convert:
3811         case DW_OP_GNU_reinterpret:
3812           {
3813             cu_offset type_die;
3814
3815             data = safe_read_uleb128 (data, end, &ul);
3816             type_die.cu_off = ul;
3817
3818             if (type_die.cu_off == 0)
3819               fprintf_filtered (stream, "<0>");
3820             else
3821               {
3822                 struct type *type;
3823
3824                 type = dwarf2_get_die_type (type_die, per_cu);
3825                 fprintf_filtered (stream, "<");
3826                 type_print (type, "", stream, -1);
3827                 fprintf_filtered (stream, " [0x%s]>", phex_nz (type_die.cu_off, 0));
3828               }
3829           }
3830           break;
3831
3832         case DW_OP_GNU_entry_value:
3833           data = safe_read_uleb128 (data, end, &ul);
3834           fputc_filtered ('\n', stream);
3835           disassemble_dwarf_expression (stream, arch, addr_size, offset_size,
3836                                         start, data, data + ul, indent + 2,
3837                                         all, per_cu);
3838           data += ul;
3839           continue;
3840
3841         case DW_OP_GNU_parameter_ref:
3842           ul = extract_unsigned_integer (data, 4, gdbarch_byte_order (arch));
3843           data += 4;
3844           fprintf_filtered (stream, " offset %s", phex_nz (ul, 4));
3845           break;
3846
3847         case DW_OP_GNU_addr_index:
3848           data = safe_read_uleb128 (data, end, &ul);
3849           ul = dwarf2_read_addr_index (per_cu, ul);
3850           fprintf_filtered (stream, " 0x%s", phex_nz (ul, addr_size));
3851           break;
3852         case DW_OP_GNU_const_index:
3853           data = safe_read_uleb128 (data, end, &ul);
3854           ul = dwarf2_read_addr_index (per_cu, ul);
3855           fprintf_filtered (stream, " %s", pulongest (ul));
3856           break;
3857         }
3858
3859       fprintf_filtered (stream, "\n");
3860     }
3861
3862   return data;
3863 }
3864
3865 /* Describe a single location, which may in turn consist of multiple
3866    pieces.  */
3867
3868 static void
3869 locexpr_describe_location_1 (struct symbol *symbol, CORE_ADDR addr,
3870                              struct ui_file *stream,
3871                              const gdb_byte *data, size_t size,
3872                              struct objfile *objfile, unsigned int addr_size,
3873                              int offset_size, struct dwarf2_per_cu_data *per_cu)
3874 {
3875   const gdb_byte *end = data + size;
3876   int first_piece = 1, bad = 0;
3877
3878   while (data < end)
3879     {
3880       const gdb_byte *here = data;
3881       int disassemble = 1;
3882
3883       if (first_piece)
3884         first_piece = 0;
3885       else
3886         fprintf_filtered (stream, _(", and "));
3887
3888       if (!dwarf2_always_disassemble)
3889         {
3890           data = locexpr_describe_location_piece (symbol, stream,
3891                                                   addr, objfile, per_cu,
3892                                                   data, end, addr_size);
3893           /* If we printed anything, or if we have an empty piece,
3894              then don't disassemble.  */
3895           if (data != here
3896               || data[0] == DW_OP_piece
3897               || data[0] == DW_OP_bit_piece)
3898             disassemble = 0;
3899         }
3900       if (disassemble)
3901         {
3902           fprintf_filtered (stream, _("a complex DWARF expression:\n"));
3903           data = disassemble_dwarf_expression (stream,
3904                                                get_objfile_arch (objfile),
3905                                                addr_size, offset_size, data,
3906                                                data, end, 0,
3907                                                dwarf2_always_disassemble,
3908                                                per_cu);
3909         }
3910
3911       if (data < end)
3912         {
3913           int empty = data == here;
3914               
3915           if (disassemble)
3916             fprintf_filtered (stream, "   ");
3917           if (data[0] == DW_OP_piece)
3918             {
3919               uint64_t bytes;
3920
3921               data = safe_read_uleb128 (data + 1, end, &bytes);
3922
3923               if (empty)
3924                 fprintf_filtered (stream, _("an empty %s-byte piece"),
3925                                   pulongest (bytes));
3926               else
3927                 fprintf_filtered (stream, _(" [%s-byte piece]"),
3928                                   pulongest (bytes));
3929             }
3930           else if (data[0] == DW_OP_bit_piece)
3931             {
3932               uint64_t bits, offset;
3933
3934               data = safe_read_uleb128 (data + 1, end, &bits);
3935               data = safe_read_uleb128 (data, end, &offset);
3936
3937               if (empty)
3938                 fprintf_filtered (stream,
3939                                   _("an empty %s-bit piece"),
3940                                   pulongest (bits));
3941               else
3942                 fprintf_filtered (stream,
3943                                   _(" [%s-bit piece, offset %s bits]"),
3944                                   pulongest (bits), pulongest (offset));
3945             }
3946           else
3947             {
3948               bad = 1;
3949               break;
3950             }
3951         }
3952     }
3953
3954   if (bad || data > end)
3955     error (_("Corrupted DWARF2 expression for \"%s\"."),
3956            SYMBOL_PRINT_NAME (symbol));
3957 }
3958
3959 /* Print a natural-language description of SYMBOL to STREAM.  This
3960    version is for a symbol with a single location.  */
3961
3962 static void
3963 locexpr_describe_location (struct symbol *symbol, CORE_ADDR addr,
3964                            struct ui_file *stream)
3965 {
3966   struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
3967   struct objfile *objfile = dwarf2_per_cu_objfile (dlbaton->per_cu);
3968   unsigned int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);
3969   int offset_size = dwarf2_per_cu_offset_size (dlbaton->per_cu);
3970
3971   locexpr_describe_location_1 (symbol, addr, stream,
3972                                dlbaton->data, dlbaton->size,
3973                                objfile, addr_size, offset_size,
3974                                dlbaton->per_cu);
3975 }
3976
3977 /* Describe the location of SYMBOL as an agent value in VALUE, generating
3978    any necessary bytecode in AX.  */
3979
3980 static void
3981 locexpr_tracepoint_var_ref (struct symbol *symbol, struct gdbarch *gdbarch,
3982                             struct agent_expr *ax, struct axs_value *value)
3983 {
3984   struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
3985   unsigned int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);
3986
3987   if (dlbaton->size == 0)
3988     value->optimized_out = 1;
3989   else
3990     dwarf2_compile_expr_to_ax (ax, value, gdbarch, addr_size,
3991                                dlbaton->data, dlbaton->data + dlbaton->size,
3992                                dlbaton->per_cu);
3993 }
3994
3995 /* The set of location functions used with the DWARF-2 expression
3996    evaluator.  */
3997 const struct symbol_computed_ops dwarf2_locexpr_funcs = {
3998   locexpr_read_variable,
3999   locexpr_read_variable_at_entry,
4000   locexpr_read_needs_frame,
4001   locexpr_describe_location,
4002   0,    /* location_has_loclist */
4003   locexpr_tracepoint_var_ref
4004 };
4005
4006
4007 /* Wrapper functions for location lists.  These generally find
4008    the appropriate location expression and call something above.  */
4009
4010 /* Return the value of SYMBOL in FRAME using the DWARF-2 expression
4011    evaluator to calculate the location.  */
4012 static struct value *
4013 loclist_read_variable (struct symbol *symbol, struct frame_info *frame)
4014 {
4015   struct dwarf2_loclist_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
4016   struct value *val;
4017   const gdb_byte *data;
4018   size_t size;
4019   CORE_ADDR pc = frame ? get_frame_address_in_block (frame) : 0;
4020
4021   data = dwarf2_find_location_expression (dlbaton, &size, pc);
4022   val = dwarf2_evaluate_loc_desc (SYMBOL_TYPE (symbol), frame, data, size,
4023                                   dlbaton->per_cu);
4024
4025   return val;
4026 }
4027
4028 /* Read variable SYMBOL like loclist_read_variable at (callee) FRAME's function
4029    entry.  SYMBOL should be a function parameter, otherwise NO_ENTRY_VALUE_ERROR
4030    will be thrown.
4031
4032    Function always returns non-NULL value, it may be marked optimized out if
4033    inferior frame information is not available.  It throws NO_ENTRY_VALUE_ERROR
4034    if it cannot resolve the parameter for any reason.  */
4035
4036 static struct value *
4037 loclist_read_variable_at_entry (struct symbol *symbol, struct frame_info *frame)
4038 {
4039   struct dwarf2_loclist_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
4040   const gdb_byte *data;
4041   size_t size;
4042   CORE_ADDR pc;
4043
4044   if (frame == NULL || !get_frame_func_if_available (frame, &pc))
4045     return allocate_optimized_out_value (SYMBOL_TYPE (symbol));
4046
4047   data = dwarf2_find_location_expression (dlbaton, &size, pc);
4048   if (data == NULL)
4049     return allocate_optimized_out_value (SYMBOL_TYPE (symbol));
4050
4051   return value_of_dwarf_block_entry (SYMBOL_TYPE (symbol), frame, data, size);
4052 }
4053
4054 /* Return non-zero iff we need a frame to evaluate SYMBOL.  */
4055 static int
4056 loclist_read_needs_frame (struct symbol *symbol)
4057 {
4058   /* If there's a location list, then assume we need to have a frame
4059      to choose the appropriate location expression.  With tracking of
4060      global variables this is not necessarily true, but such tracking
4061      is disabled in GCC at the moment until we figure out how to
4062      represent it.  */
4063
4064   return 1;
4065 }
4066
4067 /* Print a natural-language description of SYMBOL to STREAM.  This
4068    version applies when there is a list of different locations, each
4069    with a specified address range.  */
4070
4071 static void
4072 loclist_describe_location (struct symbol *symbol, CORE_ADDR addr,
4073                            struct ui_file *stream)
4074 {
4075   struct dwarf2_loclist_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
4076   const gdb_byte *loc_ptr, *buf_end;
4077   struct objfile *objfile = dwarf2_per_cu_objfile (dlbaton->per_cu);
4078   struct gdbarch *gdbarch = get_objfile_arch (objfile);
4079   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
4080   unsigned int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);
4081   int offset_size = dwarf2_per_cu_offset_size (dlbaton->per_cu);
4082   int signed_addr_p = bfd_get_sign_extend_vma (objfile->obfd);
4083   /* Adjust base_address for relocatable objects.  */
4084   CORE_ADDR base_offset = dwarf2_per_cu_text_offset (dlbaton->per_cu);
4085   CORE_ADDR base_address = dlbaton->base_address + base_offset;
4086   int done = 0;
4087
4088   loc_ptr = dlbaton->data;
4089   buf_end = dlbaton->data + dlbaton->size;
4090
4091   fprintf_filtered (stream, _("multi-location:\n"));
4092
4093   /* Iterate through locations until we run out.  */
4094   while (!done)
4095     {
4096       CORE_ADDR low = 0, high = 0; /* init for gcc -Wall */
4097       int length;
4098       enum debug_loc_kind kind;
4099       const gdb_byte *new_ptr = NULL; /* init for gcc -Wall */
4100
4101       if (dlbaton->from_dwo)
4102         kind = decode_debug_loc_dwo_addresses (dlbaton->per_cu,
4103                                                loc_ptr, buf_end, &new_ptr,
4104                                                &low, &high, byte_order);
4105       else
4106         kind = decode_debug_loc_addresses (loc_ptr, buf_end, &new_ptr,
4107                                            &low, &high,
4108                                            byte_order, addr_size,
4109                                            signed_addr_p);
4110       loc_ptr = new_ptr;
4111       switch (kind)
4112         {
4113         case DEBUG_LOC_END_OF_LIST:
4114           done = 1;
4115           continue;
4116         case DEBUG_LOC_BASE_ADDRESS:
4117           base_address = high + base_offset;
4118           fprintf_filtered (stream, _("  Base address %s"),
4119                             paddress (gdbarch, base_address));
4120           continue;
4121         case DEBUG_LOC_START_END:
4122         case DEBUG_LOC_START_LENGTH:
4123           break;
4124         case DEBUG_LOC_BUFFER_OVERFLOW:
4125         case DEBUG_LOC_INVALID_ENTRY:
4126           error (_("Corrupted DWARF expression for symbol \"%s\"."),
4127                  SYMBOL_PRINT_NAME (symbol));
4128         default:
4129           gdb_assert_not_reached ("bad debug_loc_kind");
4130         }
4131
4132       /* Otherwise, a location expression entry.  */
4133       low += base_address;
4134       high += base_address;
4135
4136       length = extract_unsigned_integer (loc_ptr, 2, byte_order);
4137       loc_ptr += 2;
4138
4139       /* (It would improve readability to print only the minimum
4140          necessary digits of the second number of the range.)  */
4141       fprintf_filtered (stream, _("  Range %s-%s: "),
4142                         paddress (gdbarch, low), paddress (gdbarch, high));
4143
4144       /* Now describe this particular location.  */
4145       locexpr_describe_location_1 (symbol, low, stream, loc_ptr, length,
4146                                    objfile, addr_size, offset_size,
4147                                    dlbaton->per_cu);
4148
4149       fprintf_filtered (stream, "\n");
4150
4151       loc_ptr += length;
4152     }
4153 }
4154
4155 /* Describe the location of SYMBOL as an agent value in VALUE, generating
4156    any necessary bytecode in AX.  */
4157 static void
4158 loclist_tracepoint_var_ref (struct symbol *symbol, struct gdbarch *gdbarch,
4159                             struct agent_expr *ax, struct axs_value *value)
4160 {
4161   struct dwarf2_loclist_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
4162   const gdb_byte *data;
4163   size_t size;
4164   unsigned int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);
4165
4166   data = dwarf2_find_location_expression (dlbaton, &size, ax->scope);
4167   if (size == 0)
4168     value->optimized_out = 1;
4169   else
4170     dwarf2_compile_expr_to_ax (ax, value, gdbarch, addr_size, data, data + size,
4171                                dlbaton->per_cu);
4172 }
4173
4174 /* The set of location functions used with the DWARF-2 expression
4175    evaluator and location lists.  */
4176 const struct symbol_computed_ops dwarf2_loclist_funcs = {
4177   loclist_read_variable,
4178   loclist_read_variable_at_entry,
4179   loclist_read_needs_frame,
4180   loclist_describe_location,
4181   1,    /* location_has_loclist */
4182   loclist_tracepoint_var_ref
4183 };
4184
4185 /* Provide a prototype to silence -Wmissing-prototypes.  */
4186 extern initialize_file_ftype _initialize_dwarf2loc;
4187
4188 void
4189 _initialize_dwarf2loc (void)
4190 {
4191   add_setshow_zuinteger_cmd ("entry-values", class_maintenance,
4192                              &entry_values_debug,
4193                              _("Set entry values and tail call frames "
4194                                "debugging."),
4195                              _("Show entry values and tail call frames "
4196                                "debugging."),
4197                              _("When non-zero, the process of determining "
4198                                "parameter values from function entry point "
4199                                "and tail call frames will be printed."),
4200                              NULL,
4201                              show_entry_values_debug,
4202                              &setdebuglist, &showdebuglist);
4203 }