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