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