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