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