Automatic date update in version.in
[external/binutils.git] / gdb / dwarf2loc.c
1 /* DWARF 2 location expression support for GDB.
2
3    Copyright (C) 2003-2018 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_ENTRY_PC (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_ENTRY_PC (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_decref (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).release ();
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 = 0;
1483
1484   /* The CU from which this closure's expression came.  */
1485   struct dwarf2_per_cu_data *per_cu = NULL;
1486
1487   /* The pieces describing this variable.  */
1488   std::vector<dwarf_expr_piece> pieces;
1489
1490   /* Frame ID of frame to which a register value is relative, used
1491      only by DWARF_VALUE_REGISTER.  */
1492   struct frame_id frame_id;
1493 };
1494
1495 /* Allocate a closure for a value formed from separately-described
1496    PIECES.  */
1497
1498 static struct piece_closure *
1499 allocate_piece_closure (struct dwarf2_per_cu_data *per_cu,
1500                         std::vector<dwarf_expr_piece> &&pieces,
1501                         struct frame_info *frame)
1502 {
1503   struct piece_closure *c = new piece_closure;
1504
1505   c->refc = 1;
1506   c->per_cu = per_cu;
1507   c->pieces = std::move (pieces);
1508   if (frame == NULL)
1509     c->frame_id = null_frame_id;
1510   else
1511     c->frame_id = get_frame_id (frame);
1512
1513   for (dwarf_expr_piece &piece : c->pieces)
1514     if (piece.location == DWARF_VALUE_STACK)
1515       value_incref (piece.v.value);
1516
1517   return c;
1518 }
1519
1520 /* Copy NBITS bits from SOURCE to DEST starting at the given bit
1521    offsets.  Use the bit order as specified by BITS_BIG_ENDIAN.
1522    Source and destination buffers must not overlap.  */
1523
1524 static void
1525 copy_bitwise (gdb_byte *dest, ULONGEST dest_offset,
1526               const gdb_byte *source, ULONGEST source_offset,
1527               ULONGEST nbits, int bits_big_endian)
1528 {
1529   unsigned int buf, avail;
1530
1531   if (nbits == 0)
1532     return;
1533
1534   if (bits_big_endian)
1535     {
1536       /* Start from the end, then work backwards.  */
1537       dest_offset += nbits - 1;
1538       dest += dest_offset / 8;
1539       dest_offset = 7 - dest_offset % 8;
1540       source_offset += nbits - 1;
1541       source += source_offset / 8;
1542       source_offset = 7 - source_offset % 8;
1543     }
1544   else
1545     {
1546       dest += dest_offset / 8;
1547       dest_offset %= 8;
1548       source += source_offset / 8;
1549       source_offset %= 8;
1550     }
1551
1552   /* Fill BUF with DEST_OFFSET bits from the destination and 8 -
1553      SOURCE_OFFSET bits from the source.  */
1554   buf = *(bits_big_endian ? source-- : source++) >> source_offset;
1555   buf <<= dest_offset;
1556   buf |= *dest & ((1 << dest_offset) - 1);
1557
1558   /* NBITS: bits yet to be written; AVAIL: BUF's fill level.  */
1559   nbits += dest_offset;
1560   avail = dest_offset + 8 - source_offset;
1561
1562   /* Flush 8 bits from BUF, if appropriate.  */
1563   if (nbits >= 8 && avail >= 8)
1564     {
1565       *(bits_big_endian ? dest-- : dest++) = buf;
1566       buf >>= 8;
1567       avail -= 8;
1568       nbits -= 8;
1569     }
1570
1571   /* Copy the middle part.  */
1572   if (nbits >= 8)
1573     {
1574       size_t len = nbits / 8;
1575
1576       /* Use a faster method for byte-aligned copies.  */
1577       if (avail == 0)
1578         {
1579           if (bits_big_endian)
1580             {
1581               dest -= len;
1582               source -= len;
1583               memcpy (dest + 1, source + 1, len);
1584             }
1585           else
1586             {
1587               memcpy (dest, source, len);
1588               dest += len;
1589               source += len;
1590             }
1591         }
1592       else
1593         {
1594           while (len--)
1595             {
1596               buf |= *(bits_big_endian ? source-- : source++) << avail;
1597               *(bits_big_endian ? dest-- : dest++) = buf;
1598               buf >>= 8;
1599             }
1600         }
1601       nbits %= 8;
1602     }
1603
1604   /* Write the last byte.  */
1605   if (nbits)
1606     {
1607       if (avail < nbits)
1608         buf |= *source << avail;
1609
1610       buf &= (1 << nbits) - 1;
1611       *dest = (*dest & (~0 << nbits)) | buf;
1612     }
1613 }
1614
1615 #if GDB_SELF_TEST
1616
1617 namespace selftests {
1618
1619 /* Helper function for the unit test of copy_bitwise.  Convert NBITS bits
1620    out of BITS, starting at OFFS, to the respective '0'/'1'-string.  MSB0
1621    specifies whether to assume big endian bit numbering.  Store the
1622    resulting (not null-terminated) string at STR.  */
1623
1624 static void
1625 bits_to_str (char *str, const gdb_byte *bits, ULONGEST offs,
1626              ULONGEST nbits, int msb0)
1627 {
1628   unsigned int j;
1629   size_t i;
1630
1631   for (i = offs / 8, j = offs % 8; nbits; i++, j = 0)
1632     {
1633       unsigned int ch = bits[i];
1634       for (; j < 8 && nbits; j++, nbits--)
1635         *str++ = (ch & (msb0 ? (1 << (7 - j)) : (1 << j))) ? '1' : '0';
1636     }
1637 }
1638
1639 /* Check one invocation of copy_bitwise with the given parameters.  */
1640
1641 static void
1642 check_copy_bitwise (const gdb_byte *dest, unsigned int dest_offset,
1643                     const gdb_byte *source, unsigned int source_offset,
1644                     unsigned int nbits, int msb0)
1645 {
1646   size_t len = align_up (dest_offset + nbits, 8);
1647   char *expected = (char *) alloca (len + 1);
1648   char *actual = (char *) alloca (len + 1);
1649   gdb_byte *buf = (gdb_byte *) alloca (len / 8);
1650
1651   /* Compose a '0'/'1'-string that represents the expected result of
1652      copy_bitwise below:
1653       Bits from [0, DEST_OFFSET) are filled from DEST.
1654       Bits from [DEST_OFFSET, DEST_OFFSET + NBITS) are filled from SOURCE.
1655       Bits from [DEST_OFFSET + NBITS, LEN) are filled from DEST.
1656
1657      E.g., with:
1658       dest_offset: 4
1659       nbits:       2
1660       len:         8
1661       dest:        00000000
1662       source:      11111111
1663
1664      We should end up with:
1665       buf:         00001100
1666                    DDDDSSDD (D=dest, S=source)
1667   */
1668   bits_to_str (expected, dest, 0, len, msb0);
1669   bits_to_str (expected + dest_offset, source, source_offset, nbits, msb0);
1670
1671   /* Fill BUF with data from DEST, apply copy_bitwise, and convert the
1672      result to a '0'/'1'-string.  */
1673   memcpy (buf, dest, len / 8);
1674   copy_bitwise (buf, dest_offset, source, source_offset, nbits, msb0);
1675   bits_to_str (actual, buf, 0, len, msb0);
1676
1677   /* Compare the resulting strings.  */
1678   expected[len] = actual[len] = '\0';
1679   if (strcmp (expected, actual) != 0)
1680     error (_("copy_bitwise %s != %s (%u+%u -> %u)"),
1681            expected, actual, source_offset, nbits, dest_offset);
1682 }
1683
1684 /* Unit test for copy_bitwise.  */
1685
1686 static void
1687 copy_bitwise_tests (void)
1688 {
1689   /* Data to be used as both source and destination buffers.  The two
1690      arrays below represent the lsb0- and msb0- encoded versions of the
1691      following bit string, respectively:
1692        00000000 00011111 11111111 01001000 10100101 11110010
1693      This pattern is chosen such that it contains:
1694      - constant 0- and 1- chunks of more than a full byte;
1695      - 0/1- and 1/0 transitions on all bit positions within a byte;
1696      - several sufficiently asymmetric bytes.
1697   */
1698   static const gdb_byte data_lsb0[] = {
1699     0x00, 0xf8, 0xff, 0x12, 0xa5, 0x4f
1700   };
1701   static const gdb_byte data_msb0[] = {
1702     0x00, 0x1f, 0xff, 0x48, 0xa5, 0xf2
1703   };
1704
1705   constexpr size_t data_nbits = 8 * sizeof (data_lsb0);
1706   constexpr unsigned max_nbits = 24;
1707
1708   /* Try all combinations of:
1709       lsb0/msb0 bit order (using the respective data array)
1710        X [0, MAX_NBITS] copy bit width
1711        X feasible source offsets for the given copy bit width
1712        X feasible destination offsets
1713   */
1714   for (int msb0 = 0; msb0 < 2; msb0++)
1715     {
1716       const gdb_byte *data = msb0 ? data_msb0 : data_lsb0;
1717
1718       for (unsigned int nbits = 1; nbits <= max_nbits; nbits++)
1719         {
1720           const unsigned int max_offset = data_nbits - nbits;
1721
1722           for (unsigned source_offset = 0;
1723                source_offset <= max_offset;
1724                source_offset++)
1725             {
1726               for (unsigned dest_offset = 0;
1727                    dest_offset <= max_offset;
1728                    dest_offset++)
1729                 {
1730                   check_copy_bitwise (data + dest_offset / 8,
1731                                       dest_offset % 8,
1732                                       data + source_offset / 8,
1733                                       source_offset % 8,
1734                                       nbits, msb0);
1735                 }
1736             }
1737         }
1738
1739       /* Special cases: copy all, copy nothing.  */
1740       check_copy_bitwise (data_lsb0, 0, data_msb0, 0, data_nbits, msb0);
1741       check_copy_bitwise (data_msb0, 0, data_lsb0, 0, data_nbits, msb0);
1742       check_copy_bitwise (data, data_nbits - 7, data, 9, 0, msb0);
1743     }
1744 }
1745
1746 } /* namespace selftests */
1747
1748 #endif /* GDB_SELF_TEST */
1749
1750 /* Return the number of bytes overlapping a contiguous chunk of N_BITS
1751    bits whose first bit is located at bit offset START.  */
1752
1753 static size_t
1754 bits_to_bytes (ULONGEST start, ULONGEST n_bits)
1755 {
1756   return (start % 8 + n_bits + 7) / 8;
1757 }
1758
1759 /* Read or write a pieced value V.  If FROM != NULL, operate in "write
1760    mode": copy FROM into the pieces comprising V.  If FROM == NULL,
1761    operate in "read mode": fetch the contents of the (lazy) value V by
1762    composing it from its pieces.  */
1763
1764 static void
1765 rw_pieced_value (struct value *v, struct value *from)
1766 {
1767   int i;
1768   LONGEST offset = 0, max_offset;
1769   ULONGEST bits_to_skip;
1770   gdb_byte *v_contents;
1771   const gdb_byte *from_contents;
1772   struct piece_closure *c
1773     = (struct piece_closure *) value_computed_closure (v);
1774   gdb::byte_vector buffer;
1775   int bits_big_endian
1776     = gdbarch_bits_big_endian (get_type_arch (value_type (v)));
1777
1778   if (from != NULL)
1779     {
1780       from_contents = value_contents (from);
1781       v_contents = NULL;
1782     }
1783   else
1784     {
1785       if (value_type (v) != value_enclosing_type (v))
1786         internal_error (__FILE__, __LINE__,
1787                         _("Should not be able to create a lazy value with "
1788                           "an enclosing type"));
1789       v_contents = value_contents_raw (v);
1790       from_contents = NULL;
1791     }
1792
1793   bits_to_skip = 8 * value_offset (v);
1794   if (value_bitsize (v))
1795     {
1796       bits_to_skip += (8 * value_offset (value_parent (v))
1797                        + value_bitpos (v));
1798       if (from != NULL
1799           && (gdbarch_byte_order (get_type_arch (value_type (from)))
1800               == BFD_ENDIAN_BIG))
1801         {
1802           /* Use the least significant bits of FROM.  */
1803           max_offset = 8 * TYPE_LENGTH (value_type (from));
1804           offset = max_offset - value_bitsize (v);
1805         }
1806       else
1807         max_offset = value_bitsize (v);
1808     }
1809   else
1810     max_offset = 8 * TYPE_LENGTH (value_type (v));
1811
1812   /* Advance to the first non-skipped piece.  */
1813   for (i = 0; i < c->pieces.size () && bits_to_skip >= c->pieces[i].size; i++)
1814     bits_to_skip -= c->pieces[i].size;
1815
1816   for (; i < c->pieces.size () && offset < max_offset; i++)
1817     {
1818       struct dwarf_expr_piece *p = &c->pieces[i];
1819       size_t this_size_bits, this_size;
1820
1821       this_size_bits = p->size - bits_to_skip;
1822       if (this_size_bits > max_offset - offset)
1823         this_size_bits = max_offset - offset;
1824
1825       switch (p->location)
1826         {
1827         case DWARF_VALUE_REGISTER:
1828           {
1829             struct frame_info *frame = frame_find_by_id (c->frame_id);
1830             struct gdbarch *arch = get_frame_arch (frame);
1831             int gdb_regnum = dwarf_reg_to_regnum_or_error (arch, p->v.regno);
1832             ULONGEST reg_bits = 8 * register_size (arch, gdb_regnum);
1833             int optim, unavail;
1834
1835             if (gdbarch_byte_order (arch) == BFD_ENDIAN_BIG
1836                 && p->offset + p->size < reg_bits)
1837               {
1838                 /* Big-endian, and we want less than full size.  */
1839                 bits_to_skip += reg_bits - (p->offset + p->size);
1840               }
1841             else
1842               bits_to_skip += p->offset;
1843
1844             this_size = bits_to_bytes (bits_to_skip, this_size_bits);
1845             buffer.resize (this_size);
1846
1847             if (from == NULL)
1848               {
1849                 /* Read mode.  */
1850                 if (!get_frame_register_bytes (frame, gdb_regnum,
1851                                                bits_to_skip / 8,
1852                                                this_size, buffer.data (),
1853                                                &optim, &unavail))
1854                   {
1855                     if (optim)
1856                       mark_value_bits_optimized_out (v, offset,
1857                                                      this_size_bits);
1858                     if (unavail)
1859                       mark_value_bits_unavailable (v, offset,
1860                                                    this_size_bits);
1861                     break;
1862                   }
1863
1864                 copy_bitwise (v_contents, offset,
1865                               buffer.data (), bits_to_skip % 8,
1866                               this_size_bits, bits_big_endian);
1867               }
1868             else
1869               {
1870                 /* Write mode.  */
1871                 if (bits_to_skip % 8 != 0 || this_size_bits % 8 != 0)
1872                   {
1873                     /* Data is copied non-byte-aligned into the register.
1874                        Need some bits from original register value.  */
1875                     get_frame_register_bytes (frame, gdb_regnum,
1876                                               bits_to_skip / 8,
1877                                               this_size, buffer.data (),
1878                                               &optim, &unavail);
1879                     if (optim)
1880                       throw_error (OPTIMIZED_OUT_ERROR,
1881                                    _("Can't do read-modify-write to "
1882                                      "update bitfield; containing word "
1883                                      "has been optimized out"));
1884                     if (unavail)
1885                       throw_error (NOT_AVAILABLE_ERROR,
1886                                    _("Can't do read-modify-write to "
1887                                      "update bitfield; containing word "
1888                                      "is unavailable"));
1889                   }
1890
1891                 copy_bitwise (buffer.data (), bits_to_skip % 8,
1892                               from_contents, offset,
1893                               this_size_bits, bits_big_endian);
1894                 put_frame_register_bytes (frame, gdb_regnum,
1895                                           bits_to_skip / 8,
1896                                           this_size, buffer.data ());
1897               }
1898           }
1899           break;
1900
1901         case DWARF_VALUE_MEMORY:
1902           {
1903             bits_to_skip += p->offset;
1904
1905             CORE_ADDR start_addr = p->v.mem.addr + bits_to_skip / 8;
1906
1907             if (bits_to_skip % 8 == 0 && this_size_bits % 8 == 0
1908                 && offset % 8 == 0)
1909               {
1910                 /* Everything is byte-aligned; no buffer needed.  */
1911                 if (from != NULL)
1912                   write_memory_with_notification (start_addr,
1913                                                   (from_contents
1914                                                    + offset / 8),
1915                                                   this_size_bits / 8);
1916                 else
1917                   read_value_memory (v, offset,
1918                                      p->v.mem.in_stack_memory,
1919                                      p->v.mem.addr + bits_to_skip / 8,
1920                                      v_contents + offset / 8,
1921                                      this_size_bits / 8);
1922                 break;
1923               }
1924
1925             this_size = bits_to_bytes (bits_to_skip, this_size_bits);
1926             buffer.resize (this_size);
1927
1928             if (from == NULL)
1929               {
1930                 /* Read mode.  */
1931                 read_value_memory (v, offset,
1932                                    p->v.mem.in_stack_memory,
1933                                    p->v.mem.addr + bits_to_skip / 8,
1934                                    buffer.data (), this_size);
1935                 copy_bitwise (v_contents, offset,
1936                               buffer.data (), bits_to_skip % 8,
1937                               this_size_bits, bits_big_endian);
1938               }
1939             else
1940               {
1941                 /* Write mode.  */
1942                 if (bits_to_skip % 8 != 0 || this_size_bits % 8 != 0)
1943                   {
1944                     if (this_size <= 8)
1945                       {
1946                         /* Perform a single read for small sizes.  */
1947                         read_memory (start_addr, buffer.data (),
1948                                      this_size);
1949                       }
1950                     else
1951                       {
1952                         /* Only the first and last bytes can possibly have
1953                            any bits reused.  */
1954                         read_memory (start_addr, buffer.data (), 1);
1955                         read_memory (start_addr + this_size - 1,
1956                                      &buffer[this_size - 1], 1);
1957                       }
1958                   }
1959
1960                 copy_bitwise (buffer.data (), bits_to_skip % 8,
1961                               from_contents, offset,
1962                               this_size_bits, bits_big_endian);
1963                 write_memory_with_notification (start_addr,
1964                                                 buffer.data (),
1965                                                 this_size);
1966               }
1967           }
1968           break;
1969
1970         case DWARF_VALUE_STACK:
1971           {
1972             if (from != NULL)
1973               {
1974                 mark_value_bits_optimized_out (v, offset, this_size_bits);
1975                 break;
1976               }
1977
1978             struct objfile *objfile = dwarf2_per_cu_objfile (c->per_cu);
1979             struct gdbarch *objfile_gdbarch = get_objfile_arch (objfile);
1980             ULONGEST stack_value_size_bits
1981               = 8 * TYPE_LENGTH (value_type (p->v.value));
1982
1983             /* Use zeroes if piece reaches beyond stack value.  */
1984             if (p->offset + p->size > stack_value_size_bits)
1985               break;
1986
1987             /* Piece is anchored at least significant bit end.  */
1988             if (gdbarch_byte_order (objfile_gdbarch) == BFD_ENDIAN_BIG)
1989               bits_to_skip += stack_value_size_bits - p->offset - p->size;
1990             else
1991               bits_to_skip += p->offset;
1992
1993             copy_bitwise (v_contents, offset,
1994                           value_contents_all (p->v.value),
1995                           bits_to_skip,
1996                           this_size_bits, bits_big_endian);
1997           }
1998           break;
1999
2000         case DWARF_VALUE_LITERAL:
2001           {
2002             if (from != NULL)
2003               {
2004                 mark_value_bits_optimized_out (v, offset, this_size_bits);
2005                 break;
2006               }
2007
2008             ULONGEST literal_size_bits = 8 * p->v.literal.length;
2009             size_t n = this_size_bits;
2010
2011             /* Cut off at the end of the implicit value.  */
2012             bits_to_skip += p->offset;
2013             if (bits_to_skip >= literal_size_bits)
2014               break;
2015             if (n > literal_size_bits - bits_to_skip)
2016               n = literal_size_bits - bits_to_skip;
2017
2018             copy_bitwise (v_contents, offset,
2019                           p->v.literal.data, bits_to_skip,
2020                           n, bits_big_endian);
2021           }
2022           break;
2023
2024         case DWARF_VALUE_IMPLICIT_POINTER:
2025             if (from != NULL)
2026               {
2027                 mark_value_bits_optimized_out (v, offset, this_size_bits);
2028                 break;
2029               }
2030
2031           /* These bits show up as zeros -- but do not cause the value to
2032              be considered optimized-out.  */
2033           break;
2034
2035         case DWARF_VALUE_OPTIMIZED_OUT:
2036           mark_value_bits_optimized_out (v, offset, this_size_bits);
2037           break;
2038
2039         default:
2040           internal_error (__FILE__, __LINE__, _("invalid location type"));
2041         }
2042
2043       offset += this_size_bits;
2044       bits_to_skip = 0;
2045     }
2046 }
2047
2048
2049 static void
2050 read_pieced_value (struct value *v)
2051 {
2052   rw_pieced_value (v, NULL);
2053 }
2054
2055 static void
2056 write_pieced_value (struct value *to, struct value *from)
2057 {
2058   rw_pieced_value (to, from);
2059 }
2060
2061 /* An implementation of an lval_funcs method to see whether a value is
2062    a synthetic pointer.  */
2063
2064 static int
2065 check_pieced_synthetic_pointer (const struct value *value, LONGEST bit_offset,
2066                                 int bit_length)
2067 {
2068   struct piece_closure *c
2069     = (struct piece_closure *) value_computed_closure (value);
2070   int i;
2071
2072   bit_offset += 8 * value_offset (value);
2073   if (value_bitsize (value))
2074     bit_offset += value_bitpos (value);
2075
2076   for (i = 0; i < c->pieces.size () && bit_length > 0; i++)
2077     {
2078       struct dwarf_expr_piece *p = &c->pieces[i];
2079       size_t this_size_bits = p->size;
2080
2081       if (bit_offset > 0)
2082         {
2083           if (bit_offset >= this_size_bits)
2084             {
2085               bit_offset -= this_size_bits;
2086               continue;
2087             }
2088
2089           bit_length -= this_size_bits - bit_offset;
2090           bit_offset = 0;
2091         }
2092       else
2093         bit_length -= this_size_bits;
2094
2095       if (p->location != DWARF_VALUE_IMPLICIT_POINTER)
2096         return 0;
2097     }
2098
2099   return 1;
2100 }
2101
2102 /* A wrapper function for get_frame_address_in_block.  */
2103
2104 static CORE_ADDR
2105 get_frame_address_in_block_wrapper (void *baton)
2106 {
2107   return get_frame_address_in_block ((struct frame_info *) baton);
2108 }
2109
2110 /* Fetch a DW_AT_const_value through a synthetic pointer.  */
2111
2112 static struct value *
2113 fetch_const_value_from_synthetic_pointer (sect_offset die, LONGEST byte_offset,
2114                                           struct dwarf2_per_cu_data *per_cu,
2115                                           struct type *type)
2116 {
2117   struct value *result = NULL;
2118   const gdb_byte *bytes;
2119   LONGEST len;
2120
2121   auto_obstack temp_obstack;
2122   bytes = dwarf2_fetch_constant_bytes (die, per_cu, &temp_obstack, &len);
2123
2124   if (bytes != NULL)
2125     {
2126       if (byte_offset >= 0
2127           && byte_offset + TYPE_LENGTH (TYPE_TARGET_TYPE (type)) <= len)
2128         {
2129           bytes += byte_offset;
2130           result = value_from_contents (TYPE_TARGET_TYPE (type), bytes);
2131         }
2132       else
2133         invalid_synthetic_pointer ();
2134     }
2135   else
2136     result = allocate_optimized_out_value (TYPE_TARGET_TYPE (type));
2137
2138   return result;
2139 }
2140
2141 /* Fetch the value pointed to by a synthetic pointer.  */
2142
2143 static struct value *
2144 indirect_synthetic_pointer (sect_offset die, LONGEST byte_offset,
2145                             struct dwarf2_per_cu_data *per_cu,
2146                             struct frame_info *frame, struct type *type)
2147 {
2148   /* Fetch the location expression of the DIE we're pointing to.  */
2149   struct dwarf2_locexpr_baton baton
2150     = dwarf2_fetch_die_loc_sect_off (die, per_cu,
2151                                      get_frame_address_in_block_wrapper, frame);
2152
2153   /* Get type of pointed-to DIE.  */
2154   struct type *orig_type = dwarf2_fetch_die_type_sect_off (die, per_cu);
2155   if (orig_type == NULL)
2156     invalid_synthetic_pointer ();
2157
2158   /* If pointed-to DIE has a DW_AT_location, evaluate it and return the
2159      resulting value.  Otherwise, it may have a DW_AT_const_value instead,
2160      or it may've been optimized out.  */
2161   if (baton.data != NULL)
2162     return dwarf2_evaluate_loc_desc_full (orig_type, frame, baton.data,
2163                                           baton.size, baton.per_cu,
2164                                           TYPE_TARGET_TYPE (type),
2165                                           byte_offset);
2166   else
2167     return fetch_const_value_from_synthetic_pointer (die, byte_offset, per_cu,
2168                                                      type);
2169 }
2170
2171 /* An implementation of an lval_funcs method to indirect through a
2172    pointer.  This handles the synthetic pointer case when needed.  */
2173
2174 static struct value *
2175 indirect_pieced_value (struct value *value)
2176 {
2177   struct piece_closure *c
2178     = (struct piece_closure *) value_computed_closure (value);
2179   struct type *type;
2180   struct frame_info *frame;
2181   int i, bit_length;
2182   LONGEST bit_offset;
2183   struct dwarf_expr_piece *piece = NULL;
2184   LONGEST byte_offset;
2185   enum bfd_endian byte_order;
2186
2187   type = check_typedef (value_type (value));
2188   if (TYPE_CODE (type) != TYPE_CODE_PTR)
2189     return NULL;
2190
2191   bit_length = 8 * TYPE_LENGTH (type);
2192   bit_offset = 8 * value_offset (value);
2193   if (value_bitsize (value))
2194     bit_offset += value_bitpos (value);
2195
2196   for (i = 0; i < c->pieces.size () && bit_length > 0; i++)
2197     {
2198       struct dwarf_expr_piece *p = &c->pieces[i];
2199       size_t this_size_bits = p->size;
2200
2201       if (bit_offset > 0)
2202         {
2203           if (bit_offset >= this_size_bits)
2204             {
2205               bit_offset -= this_size_bits;
2206               continue;
2207             }
2208
2209           bit_length -= this_size_bits - bit_offset;
2210           bit_offset = 0;
2211         }
2212       else
2213         bit_length -= this_size_bits;
2214
2215       if (p->location != DWARF_VALUE_IMPLICIT_POINTER)
2216         return NULL;
2217
2218       if (bit_length != 0)
2219         error (_("Invalid use of DW_OP_implicit_pointer"));
2220
2221       piece = p;
2222       break;
2223     }
2224
2225   gdb_assert (piece != NULL);
2226   frame = get_selected_frame (_("No frame selected."));
2227
2228   /* This is an offset requested by GDB, such as value subscripts.
2229      However, due to how synthetic pointers are implemented, this is
2230      always presented to us as a pointer type.  This means we have to
2231      sign-extend it manually as appropriate.  Use raw
2232      extract_signed_integer directly rather than value_as_address and
2233      sign extend afterwards on architectures that would need it
2234      (mostly everywhere except MIPS, which has signed addresses) as
2235      the later would go through gdbarch_pointer_to_address and thus
2236      return a CORE_ADDR with high bits set on architectures that
2237      encode address spaces and other things in CORE_ADDR.  */
2238   byte_order = gdbarch_byte_order (get_frame_arch (frame));
2239   byte_offset = extract_signed_integer (value_contents (value),
2240                                         TYPE_LENGTH (type), byte_order);
2241   byte_offset += piece->v.ptr.offset;
2242
2243   return indirect_synthetic_pointer (piece->v.ptr.die_sect_off,
2244                                      byte_offset, c->per_cu,
2245                                      frame, type);
2246 }
2247
2248 /* Implementation of the coerce_ref method of lval_funcs for synthetic C++
2249    references.  */
2250
2251 static struct value *
2252 coerce_pieced_ref (const struct value *value)
2253 {
2254   struct type *type = check_typedef (value_type (value));
2255
2256   if (value_bits_synthetic_pointer (value, value_embedded_offset (value),
2257                                     TARGET_CHAR_BIT * TYPE_LENGTH (type)))
2258     {
2259       const struct piece_closure *closure
2260         = (struct piece_closure *) value_computed_closure (value);
2261       struct frame_info *frame
2262         = get_selected_frame (_("No frame selected."));
2263
2264       /* gdb represents synthetic pointers as pieced values with a single
2265          piece.  */
2266       gdb_assert (closure != NULL);
2267       gdb_assert (closure->pieces.size () == 1);
2268
2269       return indirect_synthetic_pointer
2270         (closure->pieces[0].v.ptr.die_sect_off,
2271          closure->pieces[0].v.ptr.offset,
2272          closure->per_cu, frame, type);
2273     }
2274   else
2275     {
2276       /* Else: not a synthetic reference; do nothing.  */
2277       return NULL;
2278     }
2279 }
2280
2281 static void *
2282 copy_pieced_value_closure (const struct value *v)
2283 {
2284   struct piece_closure *c
2285     = (struct piece_closure *) value_computed_closure (v);
2286   
2287   ++c->refc;
2288   return c;
2289 }
2290
2291 static void
2292 free_pieced_value_closure (struct value *v)
2293 {
2294   struct piece_closure *c
2295     = (struct piece_closure *) value_computed_closure (v);
2296
2297   --c->refc;
2298   if (c->refc == 0)
2299     {
2300       for (dwarf_expr_piece &p : c->pieces)
2301         if (p.location == DWARF_VALUE_STACK)
2302           value_decref (p.v.value);
2303
2304       delete c;
2305     }
2306 }
2307
2308 /* Functions for accessing a variable described by DW_OP_piece.  */
2309 static const struct lval_funcs pieced_value_funcs = {
2310   read_pieced_value,
2311   write_pieced_value,
2312   indirect_pieced_value,
2313   coerce_pieced_ref,
2314   check_pieced_synthetic_pointer,
2315   copy_pieced_value_closure,
2316   free_pieced_value_closure
2317 };
2318
2319 /* Evaluate a location description, starting at DATA and with length
2320    SIZE, to find the current location of variable of TYPE in the
2321    context of FRAME.  If SUBOBJ_TYPE is non-NULL, return instead the
2322    location of the subobject of type SUBOBJ_TYPE at byte offset
2323    SUBOBJ_BYTE_OFFSET within the variable of type TYPE.  */
2324
2325 static struct value *
2326 dwarf2_evaluate_loc_desc_full (struct type *type, struct frame_info *frame,
2327                                const gdb_byte *data, size_t size,
2328                                struct dwarf2_per_cu_data *per_cu,
2329                                struct type *subobj_type,
2330                                LONGEST subobj_byte_offset)
2331 {
2332   struct value *retval;
2333   struct objfile *objfile = dwarf2_per_cu_objfile (per_cu);
2334
2335   if (subobj_type == NULL)
2336     {
2337       subobj_type = type;
2338       subobj_byte_offset = 0;
2339     }
2340   else if (subobj_byte_offset < 0)
2341     invalid_synthetic_pointer ();
2342
2343   if (size == 0)
2344     return allocate_optimized_out_value (subobj_type);
2345
2346   dwarf_evaluate_loc_desc ctx;
2347   ctx.frame = frame;
2348   ctx.per_cu = per_cu;
2349   ctx.obj_address = 0;
2350
2351   scoped_value_mark free_values;
2352
2353   ctx.gdbarch = get_objfile_arch (objfile);
2354   ctx.addr_size = dwarf2_per_cu_addr_size (per_cu);
2355   ctx.ref_addr_size = dwarf2_per_cu_ref_addr_size (per_cu);
2356   ctx.offset = dwarf2_per_cu_text_offset (per_cu);
2357
2358   TRY
2359     {
2360       ctx.eval (data, size);
2361     }
2362   CATCH (ex, RETURN_MASK_ERROR)
2363     {
2364       if (ex.error == NOT_AVAILABLE_ERROR)
2365         {
2366           free_values.free_to_mark ();
2367           retval = allocate_value (subobj_type);
2368           mark_value_bytes_unavailable (retval, 0,
2369                                         TYPE_LENGTH (subobj_type));
2370           return retval;
2371         }
2372       else if (ex.error == NO_ENTRY_VALUE_ERROR)
2373         {
2374           if (entry_values_debug)
2375             exception_print (gdb_stdout, ex);
2376           free_values.free_to_mark ();
2377           return allocate_optimized_out_value (subobj_type);
2378         }
2379       else
2380         throw_exception (ex);
2381     }
2382   END_CATCH
2383
2384   if (ctx.pieces.size () > 0)
2385     {
2386       struct piece_closure *c;
2387       ULONGEST bit_size = 0;
2388
2389       for (dwarf_expr_piece &piece : ctx.pieces)
2390         bit_size += piece.size;
2391       /* Complain if the expression is larger than the size of the
2392          outer type.  */
2393       if (bit_size > 8 * TYPE_LENGTH (type))
2394         invalid_synthetic_pointer ();
2395
2396       c = allocate_piece_closure (per_cu, std::move (ctx.pieces), frame);
2397       /* We must clean up the value chain after creating the piece
2398          closure but before allocating the result.  */
2399       free_values.free_to_mark ();
2400       retval = allocate_computed_value (subobj_type,
2401                                         &pieced_value_funcs, c);
2402       set_value_offset (retval, subobj_byte_offset);
2403     }
2404   else
2405     {
2406       switch (ctx.location)
2407         {
2408         case DWARF_VALUE_REGISTER:
2409           {
2410             struct gdbarch *arch = get_frame_arch (frame);
2411             int dwarf_regnum
2412               = longest_to_int (value_as_long (ctx.fetch (0)));
2413             int gdb_regnum = dwarf_reg_to_regnum_or_error (arch, dwarf_regnum);
2414
2415             if (subobj_byte_offset != 0)
2416               error (_("cannot use offset on synthetic pointer to register"));
2417             free_values.free_to_mark ();
2418             retval = value_from_register (subobj_type, gdb_regnum, frame);
2419             if (value_optimized_out (retval))
2420               {
2421                 struct value *tmp;
2422
2423                 /* This means the register has undefined value / was
2424                    not saved.  As we're computing the location of some
2425                    variable etc. in the program, not a value for
2426                    inspecting a register ($pc, $sp, etc.), return a
2427                    generic optimized out value instead, so that we show
2428                    <optimized out> instead of <not saved>.  */
2429                 tmp = allocate_value (subobj_type);
2430                 value_contents_copy (tmp, 0, retval, 0,
2431                                      TYPE_LENGTH (subobj_type));
2432                 retval = tmp;
2433               }
2434           }
2435           break;
2436
2437         case DWARF_VALUE_MEMORY:
2438           {
2439             struct type *ptr_type;
2440             CORE_ADDR address = ctx.fetch_address (0);
2441             bool in_stack_memory = ctx.fetch_in_stack_memory (0);
2442
2443             /* DW_OP_deref_size (and possibly other operations too) may
2444                create a pointer instead of an address.  Ideally, the
2445                pointer to address conversion would be performed as part
2446                of those operations, but the type of the object to
2447                which the address refers is not known at the time of
2448                the operation.  Therefore, we do the conversion here
2449                since the type is readily available.  */
2450
2451             switch (TYPE_CODE (subobj_type))
2452               {
2453                 case TYPE_CODE_FUNC:
2454                 case TYPE_CODE_METHOD:
2455                   ptr_type = builtin_type (ctx.gdbarch)->builtin_func_ptr;
2456                   break;
2457                 default:
2458                   ptr_type = builtin_type (ctx.gdbarch)->builtin_data_ptr;
2459                   break;
2460               }
2461             address = value_as_address (value_from_pointer (ptr_type, address));
2462
2463             free_values.free_to_mark ();
2464             retval = value_at_lazy (subobj_type,
2465                                     address + subobj_byte_offset);
2466             if (in_stack_memory)
2467               set_value_stack (retval, 1);
2468           }
2469           break;
2470
2471         case DWARF_VALUE_STACK:
2472           {
2473             struct value *value = ctx.fetch (0);
2474             size_t n = TYPE_LENGTH (value_type (value));
2475             size_t len = TYPE_LENGTH (subobj_type);
2476             size_t max = TYPE_LENGTH (type);
2477             struct gdbarch *objfile_gdbarch = get_objfile_arch (objfile);
2478
2479             if (subobj_byte_offset + len > max)
2480               invalid_synthetic_pointer ();
2481
2482             /* Preserve VALUE because we are going to free values back
2483                to the mark, but we still need the value contents
2484                below.  */
2485             value_ref_ptr value_holder = value_ref_ptr::new_reference (value);
2486             free_values.free_to_mark ();
2487
2488             retval = allocate_value (subobj_type);
2489
2490             /* The given offset is relative to the actual object.  */
2491             if (gdbarch_byte_order (objfile_gdbarch) == BFD_ENDIAN_BIG)
2492               subobj_byte_offset += n - max;
2493
2494             memcpy (value_contents_raw (retval),
2495                     value_contents_all (value) + subobj_byte_offset, len);
2496           }
2497           break;
2498
2499         case DWARF_VALUE_LITERAL:
2500           {
2501             bfd_byte *contents;
2502             size_t n = TYPE_LENGTH (subobj_type);
2503
2504             if (subobj_byte_offset + n > ctx.len)
2505               invalid_synthetic_pointer ();
2506
2507             free_values.free_to_mark ();
2508             retval = allocate_value (subobj_type);
2509             contents = value_contents_raw (retval);
2510             memcpy (contents, ctx.data + subobj_byte_offset, n);
2511           }
2512           break;
2513
2514         case DWARF_VALUE_OPTIMIZED_OUT:
2515           free_values.free_to_mark ();
2516           retval = allocate_optimized_out_value (subobj_type);
2517           break;
2518
2519           /* DWARF_VALUE_IMPLICIT_POINTER was converted to a pieced
2520              operation by execute_stack_op.  */
2521         case DWARF_VALUE_IMPLICIT_POINTER:
2522           /* DWARF_VALUE_OPTIMIZED_OUT can't occur in this context --
2523              it can only be encountered when making a piece.  */
2524         default:
2525           internal_error (__FILE__, __LINE__, _("invalid location type"));
2526         }
2527     }
2528
2529   set_value_initialized (retval, ctx.initialized);
2530
2531   return retval;
2532 }
2533
2534 /* The exported interface to dwarf2_evaluate_loc_desc_full; it always
2535    passes 0 as the byte_offset.  */
2536
2537 struct value *
2538 dwarf2_evaluate_loc_desc (struct type *type, struct frame_info *frame,
2539                           const gdb_byte *data, size_t size,
2540                           struct dwarf2_per_cu_data *per_cu)
2541 {
2542   return dwarf2_evaluate_loc_desc_full (type, frame, data, size, per_cu,
2543                                         NULL, 0);
2544 }
2545
2546 /* Evaluates a dwarf expression and stores the result in VAL, expecting
2547    that the dwarf expression only produces a single CORE_ADDR.  FRAME is the
2548    frame in which the expression is evaluated.  ADDR is a context (location of
2549    a variable) and might be needed to evaluate the location expression.
2550    Returns 1 on success, 0 otherwise.   */
2551
2552 static int
2553 dwarf2_locexpr_baton_eval (const struct dwarf2_locexpr_baton *dlbaton,
2554                            struct frame_info *frame,
2555                            CORE_ADDR addr,
2556                            CORE_ADDR *valp)
2557 {
2558   struct objfile *objfile;
2559
2560   if (dlbaton == NULL || dlbaton->size == 0)
2561     return 0;
2562
2563   dwarf_evaluate_loc_desc ctx;
2564
2565   ctx.frame = frame;
2566   ctx.per_cu = dlbaton->per_cu;
2567   ctx.obj_address = addr;
2568
2569   objfile = dwarf2_per_cu_objfile (dlbaton->per_cu);
2570
2571   ctx.gdbarch = get_objfile_arch (objfile);
2572   ctx.addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);
2573   ctx.ref_addr_size = dwarf2_per_cu_ref_addr_size (dlbaton->per_cu);
2574   ctx.offset = dwarf2_per_cu_text_offset (dlbaton->per_cu);
2575
2576   ctx.eval (dlbaton->data, dlbaton->size);
2577
2578   switch (ctx.location)
2579     {
2580     case DWARF_VALUE_REGISTER:
2581     case DWARF_VALUE_MEMORY:
2582     case DWARF_VALUE_STACK:
2583       *valp = ctx.fetch_address (0);
2584       if (ctx.location == DWARF_VALUE_REGISTER)
2585         *valp = ctx.read_addr_from_reg (*valp);
2586       return 1;
2587     case DWARF_VALUE_LITERAL:
2588       *valp = extract_signed_integer (ctx.data, ctx.len,
2589                                       gdbarch_byte_order (ctx.gdbarch));
2590       return 1;
2591       /* Unsupported dwarf values.  */
2592     case DWARF_VALUE_OPTIMIZED_OUT:
2593     case DWARF_VALUE_IMPLICIT_POINTER:
2594       break;
2595     }
2596
2597   return 0;
2598 }
2599
2600 /* See dwarf2loc.h.  */
2601
2602 int
2603 dwarf2_evaluate_property (const struct dynamic_prop *prop,
2604                           struct frame_info *frame,
2605                           struct property_addr_info *addr_stack,
2606                           CORE_ADDR *value)
2607 {
2608   if (prop == NULL)
2609     return 0;
2610
2611   if (frame == NULL && has_stack_frames ())
2612     frame = get_selected_frame (NULL);
2613
2614   switch (prop->kind)
2615     {
2616     case PROP_LOCEXPR:
2617       {
2618         const struct dwarf2_property_baton *baton
2619           = (const struct dwarf2_property_baton *) prop->data.baton;
2620
2621         if (dwarf2_locexpr_baton_eval (&baton->locexpr, frame,
2622                                        addr_stack ? addr_stack->addr : 0,
2623                                        value))
2624           {
2625             if (baton->referenced_type)
2626               {
2627                 struct value *val = value_at (baton->referenced_type, *value);
2628
2629                 *value = value_as_address (val);
2630               }
2631             return 1;
2632           }
2633       }
2634       break;
2635
2636     case PROP_LOCLIST:
2637       {
2638         struct dwarf2_property_baton *baton
2639           = (struct dwarf2_property_baton *) prop->data.baton;
2640         CORE_ADDR pc = get_frame_address_in_block (frame);
2641         const gdb_byte *data;
2642         struct value *val;
2643         size_t size;
2644
2645         data = dwarf2_find_location_expression (&baton->loclist, &size, pc);
2646         if (data != NULL)
2647           {
2648             val = dwarf2_evaluate_loc_desc (baton->referenced_type, frame, data,
2649                                             size, baton->loclist.per_cu);
2650             if (!value_optimized_out (val))
2651               {
2652                 *value = value_as_address (val);
2653                 return 1;
2654               }
2655           }
2656       }
2657       break;
2658
2659     case PROP_CONST:
2660       *value = prop->data.const_val;
2661       return 1;
2662
2663     case PROP_ADDR_OFFSET:
2664       {
2665         struct dwarf2_property_baton *baton
2666           = (struct dwarf2_property_baton *) prop->data.baton;
2667         struct property_addr_info *pinfo;
2668         struct value *val;
2669
2670         for (pinfo = addr_stack; pinfo != NULL; pinfo = pinfo->next)
2671           if (pinfo->type == baton->referenced_type)
2672             break;
2673         if (pinfo == NULL)
2674           error (_("cannot find reference address for offset property"));
2675         if (pinfo->valaddr != NULL)
2676           val = value_from_contents
2677                   (baton->offset_info.type,
2678                    pinfo->valaddr + baton->offset_info.offset);
2679         else
2680           val = value_at (baton->offset_info.type,
2681                           pinfo->addr + baton->offset_info.offset);
2682         *value = value_as_address (val);
2683         return 1;
2684       }
2685     }
2686
2687   return 0;
2688 }
2689
2690 /* See dwarf2loc.h.  */
2691
2692 void
2693 dwarf2_compile_property_to_c (string_file &stream,
2694                               const char *result_name,
2695                               struct gdbarch *gdbarch,
2696                               unsigned char *registers_used,
2697                               const struct dynamic_prop *prop,
2698                               CORE_ADDR pc,
2699                               struct symbol *sym)
2700 {
2701   struct dwarf2_property_baton *baton
2702     = (struct dwarf2_property_baton *) prop->data.baton;
2703   const gdb_byte *data;
2704   size_t size;
2705   struct dwarf2_per_cu_data *per_cu;
2706
2707   if (prop->kind == PROP_LOCEXPR)
2708     {
2709       data = baton->locexpr.data;
2710       size = baton->locexpr.size;
2711       per_cu = baton->locexpr.per_cu;
2712     }
2713   else
2714     {
2715       gdb_assert (prop->kind == PROP_LOCLIST);
2716
2717       data = dwarf2_find_location_expression (&baton->loclist, &size, pc);
2718       per_cu = baton->loclist.per_cu;
2719     }
2720
2721   compile_dwarf_bounds_to_c (stream, result_name, prop, sym, pc,
2722                              gdbarch, registers_used,
2723                              dwarf2_per_cu_addr_size (per_cu),
2724                              data, data + size, per_cu);
2725 }
2726
2727 \f
2728 /* Helper functions and baton for dwarf2_loc_desc_get_symbol_read_needs.  */
2729
2730 class symbol_needs_eval_context : public dwarf_expr_context
2731 {
2732  public:
2733
2734   enum symbol_needs_kind needs;
2735   struct dwarf2_per_cu_data *per_cu;
2736
2737   /* Reads from registers do require a frame.  */
2738   CORE_ADDR read_addr_from_reg (int regnum) override
2739   {
2740     needs = SYMBOL_NEEDS_FRAME;
2741     return 1;
2742   }
2743
2744   /* "get_reg_value" callback: Reads from registers do require a
2745      frame.  */
2746
2747   struct value *get_reg_value (struct type *type, int regnum) override
2748   {
2749     needs = SYMBOL_NEEDS_FRAME;
2750     return value_zero (type, not_lval);
2751   }
2752
2753   /* Reads from memory do not require a frame.  */
2754   void read_mem (gdb_byte *buf, CORE_ADDR addr, size_t len) override
2755   {
2756     memset (buf, 0, len);
2757   }
2758
2759   /* Frame-relative accesses do require a frame.  */
2760   void get_frame_base (const gdb_byte **start, size_t *length) override
2761   {
2762     static gdb_byte lit0 = DW_OP_lit0;
2763
2764     *start = &lit0;
2765     *length = 1;
2766
2767     needs = SYMBOL_NEEDS_FRAME;
2768   }
2769
2770   /* CFA accesses require a frame.  */
2771   CORE_ADDR get_frame_cfa () override
2772   {
2773     needs = SYMBOL_NEEDS_FRAME;
2774     return 1;
2775   }
2776
2777   CORE_ADDR get_frame_pc () override
2778   {
2779     needs = SYMBOL_NEEDS_FRAME;
2780     return 1;
2781   }
2782
2783   /* Thread-local accesses require registers, but not a frame.  */
2784   CORE_ADDR get_tls_address (CORE_ADDR offset) override
2785   {
2786     if (needs <= SYMBOL_NEEDS_REGISTERS)
2787       needs = SYMBOL_NEEDS_REGISTERS;
2788     return 1;
2789   }
2790
2791   /* Helper interface of per_cu_dwarf_call for
2792      dwarf2_loc_desc_get_symbol_read_needs.  */
2793
2794   void dwarf_call (cu_offset die_offset) override
2795   {
2796     per_cu_dwarf_call (this, die_offset, per_cu);
2797   }
2798
2799   /* DW_OP_entry_value accesses require a caller, therefore a
2800      frame.  */
2801
2802   void push_dwarf_reg_entry_value (enum call_site_parameter_kind kind,
2803                                    union call_site_parameter_u kind_u,
2804                                    int deref_size) override
2805   {
2806     needs = SYMBOL_NEEDS_FRAME;
2807
2808     /* The expression may require some stub values on DWARF stack.  */
2809     push_address (0, 0);
2810   }
2811
2812   /* DW_OP_GNU_addr_index doesn't require a frame.  */
2813
2814    CORE_ADDR get_addr_index (unsigned int index) override
2815    {
2816      /* Nothing to do.  */
2817      return 1;
2818    }
2819
2820    /* DW_OP_push_object_address has a frame already passed through.  */
2821
2822    CORE_ADDR get_object_address () override
2823    {
2824      /* Nothing to do.  */
2825      return 1;
2826    }
2827 };
2828
2829 /* Compute the correct symbol_needs_kind value for the location
2830    expression at DATA (length SIZE).  */
2831
2832 static enum symbol_needs_kind
2833 dwarf2_loc_desc_get_symbol_read_needs (const gdb_byte *data, size_t size,
2834                                        struct dwarf2_per_cu_data *per_cu)
2835 {
2836   int in_reg;
2837   struct objfile *objfile = dwarf2_per_cu_objfile (per_cu);
2838
2839   scoped_value_mark free_values;
2840
2841   symbol_needs_eval_context ctx;
2842
2843   ctx.needs = SYMBOL_NEEDS_NONE;
2844   ctx.per_cu = per_cu;
2845   ctx.gdbarch = get_objfile_arch (objfile);
2846   ctx.addr_size = dwarf2_per_cu_addr_size (per_cu);
2847   ctx.ref_addr_size = dwarf2_per_cu_ref_addr_size (per_cu);
2848   ctx.offset = dwarf2_per_cu_text_offset (per_cu);
2849
2850   ctx.eval (data, size);
2851
2852   in_reg = ctx.location == DWARF_VALUE_REGISTER;
2853
2854   /* If the location has several pieces, and any of them are in
2855      registers, then we will need a frame to fetch them from.  */
2856   for (dwarf_expr_piece &p : ctx.pieces)
2857     if (p.location == DWARF_VALUE_REGISTER)
2858       in_reg = 1;
2859
2860   if (in_reg)
2861     ctx.needs = SYMBOL_NEEDS_FRAME;
2862   return ctx.needs;
2863 }
2864
2865 /* A helper function that throws an unimplemented error mentioning a
2866    given DWARF operator.  */
2867
2868 static void ATTRIBUTE_NORETURN
2869 unimplemented (unsigned int op)
2870 {
2871   const char *name = get_DW_OP_name (op);
2872
2873   if (name)
2874     error (_("DWARF operator %s cannot be translated to an agent expression"),
2875            name);
2876   else
2877     error (_("Unknown DWARF operator 0x%02x cannot be translated "
2878              "to an agent expression"),
2879            op);
2880 }
2881
2882 /* See dwarf2loc.h.
2883
2884    This is basically a wrapper on gdbarch_dwarf2_reg_to_regnum so that we
2885    can issue a complaint, which is better than having every target's
2886    implementation of dwarf2_reg_to_regnum do it.  */
2887
2888 int
2889 dwarf_reg_to_regnum (struct gdbarch *arch, int dwarf_reg)
2890 {
2891   int reg = gdbarch_dwarf2_reg_to_regnum (arch, dwarf_reg);
2892
2893   if (reg == -1)
2894     {
2895       complaint (_("bad DWARF register number %d"), dwarf_reg);
2896     }
2897   return reg;
2898 }
2899
2900 /* Subroutine of dwarf_reg_to_regnum_or_error to simplify it.
2901    Throw an error because DWARF_REG is bad.  */
2902
2903 static void
2904 throw_bad_regnum_error (ULONGEST dwarf_reg)
2905 {
2906   /* Still want to print -1 as "-1".
2907      We *could* have int and ULONGEST versions of dwarf2_reg_to_regnum_or_error
2908      but that's overkill for now.  */
2909   if ((int) dwarf_reg == dwarf_reg)
2910     error (_("Unable to access DWARF register number %d"), (int) dwarf_reg);
2911   error (_("Unable to access DWARF register number %s"),
2912          pulongest (dwarf_reg));
2913 }
2914
2915 /* See dwarf2loc.h.  */
2916
2917 int
2918 dwarf_reg_to_regnum_or_error (struct gdbarch *arch, ULONGEST dwarf_reg)
2919 {
2920   int reg;
2921
2922   if (dwarf_reg > INT_MAX)
2923     throw_bad_regnum_error (dwarf_reg);
2924   /* Yes, we will end up issuing a complaint and an error if DWARF_REG is
2925      bad, but that's ok.  */
2926   reg = dwarf_reg_to_regnum (arch, (int) dwarf_reg);
2927   if (reg == -1)
2928     throw_bad_regnum_error (dwarf_reg);
2929   return reg;
2930 }
2931
2932 /* A helper function that emits an access to memory.  ARCH is the
2933    target architecture.  EXPR is the expression which we are building.
2934    NBITS is the number of bits we want to read.  This emits the
2935    opcodes needed to read the memory and then extract the desired
2936    bits.  */
2937
2938 static void
2939 access_memory (struct gdbarch *arch, struct agent_expr *expr, ULONGEST nbits)
2940 {
2941   ULONGEST nbytes = (nbits + 7) / 8;
2942
2943   gdb_assert (nbytes > 0 && nbytes <= sizeof (LONGEST));
2944
2945   if (expr->tracing)
2946     ax_trace_quick (expr, nbytes);
2947
2948   if (nbits <= 8)
2949     ax_simple (expr, aop_ref8);
2950   else if (nbits <= 16)
2951     ax_simple (expr, aop_ref16);
2952   else if (nbits <= 32)
2953     ax_simple (expr, aop_ref32);
2954   else
2955     ax_simple (expr, aop_ref64);
2956
2957   /* If we read exactly the number of bytes we wanted, we're done.  */
2958   if (8 * nbytes == nbits)
2959     return;
2960
2961   if (gdbarch_bits_big_endian (arch))
2962     {
2963       /* On a bits-big-endian machine, we want the high-order
2964          NBITS.  */
2965       ax_const_l (expr, 8 * nbytes - nbits);
2966       ax_simple (expr, aop_rsh_unsigned);
2967     }
2968   else
2969     {
2970       /* On a bits-little-endian box, we want the low-order NBITS.  */
2971       ax_zero_ext (expr, nbits);
2972     }
2973 }
2974
2975 /* A helper function to return the frame's PC.  */
2976
2977 static CORE_ADDR
2978 get_ax_pc (void *baton)
2979 {
2980   struct agent_expr *expr = (struct agent_expr *) baton;
2981
2982   return expr->scope;
2983 }
2984
2985 /* Compile a DWARF location expression to an agent expression.
2986    
2987    EXPR is the agent expression we are building.
2988    LOC is the agent value we modify.
2989    ARCH is the architecture.
2990    ADDR_SIZE is the size of addresses, in bytes.
2991    OP_PTR is the start of the location expression.
2992    OP_END is one past the last byte of the location expression.
2993    
2994    This will throw an exception for various kinds of errors -- for
2995    example, if the expression cannot be compiled, or if the expression
2996    is invalid.  */
2997
2998 void
2999 dwarf2_compile_expr_to_ax (struct agent_expr *expr, struct axs_value *loc,
3000                            unsigned int addr_size, const gdb_byte *op_ptr,
3001                            const gdb_byte *op_end,
3002                            struct dwarf2_per_cu_data *per_cu)
3003 {
3004   gdbarch *arch = expr->gdbarch;
3005   int i;
3006   std::vector<int> dw_labels, patches;
3007   const gdb_byte * const base = op_ptr;
3008   const gdb_byte *previous_piece = op_ptr;
3009   enum bfd_endian byte_order = gdbarch_byte_order (arch);
3010   ULONGEST bits_collected = 0;
3011   unsigned int addr_size_bits = 8 * addr_size;
3012   int bits_big_endian = gdbarch_bits_big_endian (arch);
3013
3014   std::vector<int> offsets (op_end - op_ptr, -1);
3015
3016   /* By default we are making an address.  */
3017   loc->kind = axs_lvalue_memory;
3018
3019   while (op_ptr < op_end)
3020     {
3021       enum dwarf_location_atom op = (enum dwarf_location_atom) *op_ptr;
3022       uint64_t uoffset, reg;
3023       int64_t offset;
3024       int i;
3025
3026       offsets[op_ptr - base] = expr->len;
3027       ++op_ptr;
3028
3029       /* Our basic approach to code generation is to map DWARF
3030          operations directly to AX operations.  However, there are
3031          some differences.
3032
3033          First, DWARF works on address-sized units, but AX always uses
3034          LONGEST.  For most operations we simply ignore this
3035          difference; instead we generate sign extensions as needed
3036          before division and comparison operations.  It would be nice
3037          to omit the sign extensions, but there is no way to determine
3038          the size of the target's LONGEST.  (This code uses the size
3039          of the host LONGEST in some cases -- that is a bug but it is
3040          difficult to fix.)
3041
3042          Second, some DWARF operations cannot be translated to AX.
3043          For these we simply fail.  See
3044          http://sourceware.org/bugzilla/show_bug.cgi?id=11662.  */
3045       switch (op)
3046         {
3047         case DW_OP_lit0:
3048         case DW_OP_lit1:
3049         case DW_OP_lit2:
3050         case DW_OP_lit3:
3051         case DW_OP_lit4:
3052         case DW_OP_lit5:
3053         case DW_OP_lit6:
3054         case DW_OP_lit7:
3055         case DW_OP_lit8:
3056         case DW_OP_lit9:
3057         case DW_OP_lit10:
3058         case DW_OP_lit11:
3059         case DW_OP_lit12:
3060         case DW_OP_lit13:
3061         case DW_OP_lit14:
3062         case DW_OP_lit15:
3063         case DW_OP_lit16:
3064         case DW_OP_lit17:
3065         case DW_OP_lit18:
3066         case DW_OP_lit19:
3067         case DW_OP_lit20:
3068         case DW_OP_lit21:
3069         case DW_OP_lit22:
3070         case DW_OP_lit23:
3071         case DW_OP_lit24:
3072         case DW_OP_lit25:
3073         case DW_OP_lit26:
3074         case DW_OP_lit27:
3075         case DW_OP_lit28:
3076         case DW_OP_lit29:
3077         case DW_OP_lit30:
3078         case DW_OP_lit31:
3079           ax_const_l (expr, op - DW_OP_lit0);
3080           break;
3081
3082         case DW_OP_addr:
3083           uoffset = extract_unsigned_integer (op_ptr, addr_size, byte_order);
3084           op_ptr += addr_size;
3085           /* Some versions of GCC emit DW_OP_addr before
3086              DW_OP_GNU_push_tls_address.  In this case the value is an
3087              index, not an address.  We don't support things like
3088              branching between the address and the TLS op.  */
3089           if (op_ptr >= op_end || *op_ptr != DW_OP_GNU_push_tls_address)
3090             uoffset += dwarf2_per_cu_text_offset (per_cu);
3091           ax_const_l (expr, uoffset);
3092           break;
3093
3094         case DW_OP_const1u:
3095           ax_const_l (expr, extract_unsigned_integer (op_ptr, 1, byte_order));
3096           op_ptr += 1;
3097           break;
3098         case DW_OP_const1s:
3099           ax_const_l (expr, extract_signed_integer (op_ptr, 1, byte_order));
3100           op_ptr += 1;
3101           break;
3102         case DW_OP_const2u:
3103           ax_const_l (expr, extract_unsigned_integer (op_ptr, 2, byte_order));
3104           op_ptr += 2;
3105           break;
3106         case DW_OP_const2s:
3107           ax_const_l (expr, extract_signed_integer (op_ptr, 2, byte_order));
3108           op_ptr += 2;
3109           break;
3110         case DW_OP_const4u:
3111           ax_const_l (expr, extract_unsigned_integer (op_ptr, 4, byte_order));
3112           op_ptr += 4;
3113           break;
3114         case DW_OP_const4s:
3115           ax_const_l (expr, extract_signed_integer (op_ptr, 4, byte_order));
3116           op_ptr += 4;
3117           break;
3118         case DW_OP_const8u:
3119           ax_const_l (expr, extract_unsigned_integer (op_ptr, 8, byte_order));
3120           op_ptr += 8;
3121           break;
3122         case DW_OP_const8s:
3123           ax_const_l (expr, extract_signed_integer (op_ptr, 8, byte_order));
3124           op_ptr += 8;
3125           break;
3126         case DW_OP_constu:
3127           op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
3128           ax_const_l (expr, uoffset);
3129           break;
3130         case DW_OP_consts:
3131           op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
3132           ax_const_l (expr, offset);
3133           break;
3134
3135         case DW_OP_reg0:
3136         case DW_OP_reg1:
3137         case DW_OP_reg2:
3138         case DW_OP_reg3:
3139         case DW_OP_reg4:
3140         case DW_OP_reg5:
3141         case DW_OP_reg6:
3142         case DW_OP_reg7:
3143         case DW_OP_reg8:
3144         case DW_OP_reg9:
3145         case DW_OP_reg10:
3146         case DW_OP_reg11:
3147         case DW_OP_reg12:
3148         case DW_OP_reg13:
3149         case DW_OP_reg14:
3150         case DW_OP_reg15:
3151         case DW_OP_reg16:
3152         case DW_OP_reg17:
3153         case DW_OP_reg18:
3154         case DW_OP_reg19:
3155         case DW_OP_reg20:
3156         case DW_OP_reg21:
3157         case DW_OP_reg22:
3158         case DW_OP_reg23:
3159         case DW_OP_reg24:
3160         case DW_OP_reg25:
3161         case DW_OP_reg26:
3162         case DW_OP_reg27:
3163         case DW_OP_reg28:
3164         case DW_OP_reg29:
3165         case DW_OP_reg30:
3166         case DW_OP_reg31:
3167           dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_regx");
3168           loc->u.reg = dwarf_reg_to_regnum_or_error (arch, op - DW_OP_reg0);
3169           loc->kind = axs_lvalue_register;
3170           break;
3171
3172         case DW_OP_regx:
3173           op_ptr = safe_read_uleb128 (op_ptr, op_end, &reg);
3174           dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_regx");
3175           loc->u.reg = dwarf_reg_to_regnum_or_error (arch, reg);
3176           loc->kind = axs_lvalue_register;
3177           break;
3178
3179         case DW_OP_implicit_value:
3180           {
3181             uint64_t len;
3182
3183             op_ptr = safe_read_uleb128 (op_ptr, op_end, &len);
3184             if (op_ptr + len > op_end)
3185               error (_("DW_OP_implicit_value: too few bytes available."));
3186             if (len > sizeof (ULONGEST))
3187               error (_("Cannot translate DW_OP_implicit_value of %d bytes"),
3188                      (int) len);
3189
3190             ax_const_l (expr, extract_unsigned_integer (op_ptr, len,
3191                                                         byte_order));
3192             op_ptr += len;
3193             dwarf_expr_require_composition (op_ptr, op_end,
3194                                             "DW_OP_implicit_value");
3195
3196             loc->kind = axs_rvalue;
3197           }
3198           break;
3199
3200         case DW_OP_stack_value:
3201           dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_stack_value");
3202           loc->kind = axs_rvalue;
3203           break;
3204
3205         case DW_OP_breg0:
3206         case DW_OP_breg1:
3207         case DW_OP_breg2:
3208         case DW_OP_breg3:
3209         case DW_OP_breg4:
3210         case DW_OP_breg5:
3211         case DW_OP_breg6:
3212         case DW_OP_breg7:
3213         case DW_OP_breg8:
3214         case DW_OP_breg9:
3215         case DW_OP_breg10:
3216         case DW_OP_breg11:
3217         case DW_OP_breg12:
3218         case DW_OP_breg13:
3219         case DW_OP_breg14:
3220         case DW_OP_breg15:
3221         case DW_OP_breg16:
3222         case DW_OP_breg17:
3223         case DW_OP_breg18:
3224         case DW_OP_breg19:
3225         case DW_OP_breg20:
3226         case DW_OP_breg21:
3227         case DW_OP_breg22:
3228         case DW_OP_breg23:
3229         case DW_OP_breg24:
3230         case DW_OP_breg25:
3231         case DW_OP_breg26:
3232         case DW_OP_breg27:
3233         case DW_OP_breg28:
3234         case DW_OP_breg29:
3235         case DW_OP_breg30:
3236         case DW_OP_breg31:
3237           op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
3238           i = dwarf_reg_to_regnum_or_error (arch, op - DW_OP_breg0);
3239           ax_reg (expr, i);
3240           if (offset != 0)
3241             {
3242               ax_const_l (expr, offset);
3243               ax_simple (expr, aop_add);
3244             }
3245           break;
3246         case DW_OP_bregx:
3247           {
3248             op_ptr = safe_read_uleb128 (op_ptr, op_end, &reg);
3249             op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
3250             i = dwarf_reg_to_regnum_or_error (arch, reg);
3251             ax_reg (expr, i);
3252             if (offset != 0)
3253               {
3254                 ax_const_l (expr, offset);
3255                 ax_simple (expr, aop_add);
3256               }
3257           }
3258           break;
3259         case DW_OP_fbreg:
3260           {
3261             const gdb_byte *datastart;
3262             size_t datalen;
3263             const struct block *b;
3264             struct symbol *framefunc;
3265
3266             b = block_for_pc (expr->scope);
3267
3268             if (!b)
3269               error (_("No block found for address"));
3270
3271             framefunc = block_linkage_function (b);
3272
3273             if (!framefunc)
3274               error (_("No function found for block"));
3275
3276             func_get_frame_base_dwarf_block (framefunc, expr->scope,
3277                                              &datastart, &datalen);
3278
3279             op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
3280             dwarf2_compile_expr_to_ax (expr, loc, addr_size, datastart,
3281                                        datastart + datalen, per_cu);
3282             if (loc->kind == axs_lvalue_register)
3283               require_rvalue (expr, loc);
3284
3285             if (offset != 0)
3286               {
3287                 ax_const_l (expr, offset);
3288                 ax_simple (expr, aop_add);
3289               }
3290
3291             loc->kind = axs_lvalue_memory;
3292           }
3293           break;
3294
3295         case DW_OP_dup:
3296           ax_simple (expr, aop_dup);
3297           break;
3298
3299         case DW_OP_drop:
3300           ax_simple (expr, aop_pop);
3301           break;
3302
3303         case DW_OP_pick:
3304           offset = *op_ptr++;
3305           ax_pick (expr, offset);
3306           break;
3307           
3308         case DW_OP_swap:
3309           ax_simple (expr, aop_swap);
3310           break;
3311
3312         case DW_OP_over:
3313           ax_pick (expr, 1);
3314           break;
3315
3316         case DW_OP_rot:
3317           ax_simple (expr, aop_rot);
3318           break;
3319
3320         case DW_OP_deref:
3321         case DW_OP_deref_size:
3322           {
3323             int size;
3324
3325             if (op == DW_OP_deref_size)
3326               size = *op_ptr++;
3327             else
3328               size = addr_size;
3329
3330             if (size != 1 && size != 2 && size != 4 && size != 8)
3331               error (_("Unsupported size %d in %s"),
3332                      size, get_DW_OP_name (op));
3333             access_memory (arch, expr, size * TARGET_CHAR_BIT);
3334           }
3335           break;
3336
3337         case DW_OP_abs:
3338           /* Sign extend the operand.  */
3339           ax_ext (expr, addr_size_bits);
3340           ax_simple (expr, aop_dup);
3341           ax_const_l (expr, 0);
3342           ax_simple (expr, aop_less_signed);
3343           ax_simple (expr, aop_log_not);
3344           i = ax_goto (expr, aop_if_goto);
3345           /* We have to emit 0 - X.  */
3346           ax_const_l (expr, 0);
3347           ax_simple (expr, aop_swap);
3348           ax_simple (expr, aop_sub);
3349           ax_label (expr, i, expr->len);
3350           break;
3351
3352         case DW_OP_neg:
3353           /* No need to sign extend here.  */
3354           ax_const_l (expr, 0);
3355           ax_simple (expr, aop_swap);
3356           ax_simple (expr, aop_sub);
3357           break;
3358
3359         case DW_OP_not:
3360           /* Sign extend the operand.  */
3361           ax_ext (expr, addr_size_bits);
3362           ax_simple (expr, aop_bit_not);
3363           break;
3364
3365         case DW_OP_plus_uconst:
3366           op_ptr = safe_read_uleb128 (op_ptr, op_end, &reg);
3367           /* It would be really weird to emit `DW_OP_plus_uconst 0',
3368              but we micro-optimize anyhow.  */
3369           if (reg != 0)
3370             {
3371               ax_const_l (expr, reg);
3372               ax_simple (expr, aop_add);
3373             }
3374           break;
3375
3376         case DW_OP_and:
3377           ax_simple (expr, aop_bit_and);
3378           break;
3379
3380         case DW_OP_div:
3381           /* Sign extend the operands.  */
3382           ax_ext (expr, addr_size_bits);
3383           ax_simple (expr, aop_swap);
3384           ax_ext (expr, addr_size_bits);
3385           ax_simple (expr, aop_swap);
3386           ax_simple (expr, aop_div_signed);
3387           break;
3388
3389         case DW_OP_minus:
3390           ax_simple (expr, aop_sub);
3391           break;
3392
3393         case DW_OP_mod:
3394           ax_simple (expr, aop_rem_unsigned);
3395           break;
3396
3397         case DW_OP_mul:
3398           ax_simple (expr, aop_mul);
3399           break;
3400
3401         case DW_OP_or:
3402           ax_simple (expr, aop_bit_or);
3403           break;
3404
3405         case DW_OP_plus:
3406           ax_simple (expr, aop_add);
3407           break;
3408
3409         case DW_OP_shl:
3410           ax_simple (expr, aop_lsh);
3411           break;
3412
3413         case DW_OP_shr:
3414           ax_simple (expr, aop_rsh_unsigned);
3415           break;
3416
3417         case DW_OP_shra:
3418           ax_simple (expr, aop_rsh_signed);
3419           break;
3420
3421         case DW_OP_xor:
3422           ax_simple (expr, aop_bit_xor);
3423           break;
3424
3425         case DW_OP_le:
3426           /* Sign extend the operands.  */
3427           ax_ext (expr, addr_size_bits);
3428           ax_simple (expr, aop_swap);
3429           ax_ext (expr, addr_size_bits);
3430           /* Note no swap here: A <= B is !(B < A).  */
3431           ax_simple (expr, aop_less_signed);
3432           ax_simple (expr, aop_log_not);
3433           break;
3434
3435         case DW_OP_ge:
3436           /* Sign extend the operands.  */
3437           ax_ext (expr, addr_size_bits);
3438           ax_simple (expr, aop_swap);
3439           ax_ext (expr, addr_size_bits);
3440           ax_simple (expr, aop_swap);
3441           /* A >= B is !(A < B).  */
3442           ax_simple (expr, aop_less_signed);
3443           ax_simple (expr, aop_log_not);
3444           break;
3445
3446         case DW_OP_eq:
3447           /* Sign extend the operands.  */
3448           ax_ext (expr, addr_size_bits);
3449           ax_simple (expr, aop_swap);
3450           ax_ext (expr, addr_size_bits);
3451           /* No need for a second swap here.  */
3452           ax_simple (expr, aop_equal);
3453           break;
3454
3455         case DW_OP_lt:
3456           /* Sign extend the operands.  */
3457           ax_ext (expr, addr_size_bits);
3458           ax_simple (expr, aop_swap);
3459           ax_ext (expr, addr_size_bits);
3460           ax_simple (expr, aop_swap);
3461           ax_simple (expr, aop_less_signed);
3462           break;
3463
3464         case DW_OP_gt:
3465           /* Sign extend the operands.  */
3466           ax_ext (expr, addr_size_bits);
3467           ax_simple (expr, aop_swap);
3468           ax_ext (expr, addr_size_bits);
3469           /* Note no swap here: A > B is B < A.  */
3470           ax_simple (expr, aop_less_signed);
3471           break;
3472
3473         case DW_OP_ne:
3474           /* Sign extend the operands.  */
3475           ax_ext (expr, addr_size_bits);
3476           ax_simple (expr, aop_swap);
3477           ax_ext (expr, addr_size_bits);
3478           /* No need for a swap here.  */
3479           ax_simple (expr, aop_equal);
3480           ax_simple (expr, aop_log_not);
3481           break;
3482
3483         case DW_OP_call_frame_cfa:
3484           {
3485             int regnum;
3486             CORE_ADDR text_offset;
3487             LONGEST off;
3488             const gdb_byte *cfa_start, *cfa_end;
3489
3490             if (dwarf2_fetch_cfa_info (arch, expr->scope, per_cu,
3491                                        &regnum, &off,
3492                                        &text_offset, &cfa_start, &cfa_end))
3493               {
3494                 /* Register.  */
3495                 ax_reg (expr, regnum);
3496                 if (off != 0)
3497                   {
3498                     ax_const_l (expr, off);
3499                     ax_simple (expr, aop_add);
3500                   }
3501               }
3502             else
3503               {
3504                 /* Another expression.  */
3505                 ax_const_l (expr, text_offset);
3506                 dwarf2_compile_expr_to_ax (expr, loc, addr_size, cfa_start,
3507                                            cfa_end, per_cu);
3508               }
3509
3510             loc->kind = axs_lvalue_memory;
3511           }
3512           break;
3513
3514         case DW_OP_GNU_push_tls_address:
3515         case DW_OP_form_tls_address:
3516           unimplemented (op);
3517           break;
3518
3519         case DW_OP_push_object_address:
3520           unimplemented (op);
3521           break;
3522
3523         case DW_OP_skip:
3524           offset = extract_signed_integer (op_ptr, 2, byte_order);
3525           op_ptr += 2;
3526           i = ax_goto (expr, aop_goto);
3527           dw_labels.push_back (op_ptr + offset - base);
3528           patches.push_back (i);
3529           break;
3530
3531         case DW_OP_bra:
3532           offset = extract_signed_integer (op_ptr, 2, byte_order);
3533           op_ptr += 2;
3534           /* Zero extend the operand.  */
3535           ax_zero_ext (expr, addr_size_bits);
3536           i = ax_goto (expr, aop_if_goto);
3537           dw_labels.push_back (op_ptr + offset - base);
3538           patches.push_back (i);
3539           break;
3540
3541         case DW_OP_nop:
3542           break;
3543
3544         case DW_OP_piece:
3545         case DW_OP_bit_piece:
3546           {
3547             uint64_t size, offset;
3548
3549             if (op_ptr - 1 == previous_piece)
3550               error (_("Cannot translate empty pieces to agent expressions"));
3551             previous_piece = op_ptr - 1;
3552
3553             op_ptr = safe_read_uleb128 (op_ptr, op_end, &size);
3554             if (op == DW_OP_piece)
3555               {
3556                 size *= 8;
3557                 offset = 0;
3558               }
3559             else
3560               op_ptr = safe_read_uleb128 (op_ptr, op_end, &offset);
3561
3562             if (bits_collected + size > 8 * sizeof (LONGEST))
3563               error (_("Expression pieces exceed word size"));
3564
3565             /* Access the bits.  */
3566             switch (loc->kind)
3567               {
3568               case axs_lvalue_register:
3569                 ax_reg (expr, loc->u.reg);
3570                 break;
3571
3572               case axs_lvalue_memory:
3573                 /* Offset the pointer, if needed.  */
3574                 if (offset > 8)
3575                   {
3576                     ax_const_l (expr, offset / 8);
3577                     ax_simple (expr, aop_add);
3578                     offset %= 8;
3579                   }
3580                 access_memory (arch, expr, size);
3581                 break;
3582               }
3583
3584             /* For a bits-big-endian target, shift up what we already
3585                have.  For a bits-little-endian target, shift up the
3586                new data.  Note that there is a potential bug here if
3587                the DWARF expression leaves multiple values on the
3588                stack.  */
3589             if (bits_collected > 0)
3590               {
3591                 if (bits_big_endian)
3592                   {
3593                     ax_simple (expr, aop_swap);
3594                     ax_const_l (expr, size);
3595                     ax_simple (expr, aop_lsh);
3596                     /* We don't need a second swap here, because
3597                        aop_bit_or is symmetric.  */
3598                   }
3599                 else
3600                   {
3601                     ax_const_l (expr, size);
3602                     ax_simple (expr, aop_lsh);
3603                   }
3604                 ax_simple (expr, aop_bit_or);
3605               }
3606
3607             bits_collected += size;
3608             loc->kind = axs_rvalue;
3609           }
3610           break;
3611
3612         case DW_OP_GNU_uninit:
3613           unimplemented (op);
3614
3615         case DW_OP_call2:
3616         case DW_OP_call4:
3617           {
3618             struct dwarf2_locexpr_baton block;
3619             int size = (op == DW_OP_call2 ? 2 : 4);
3620
3621             uoffset = extract_unsigned_integer (op_ptr, size, byte_order);
3622             op_ptr += size;
3623
3624             cu_offset offset = (cu_offset) uoffset;
3625             block = dwarf2_fetch_die_loc_cu_off (offset, per_cu,
3626                                                  get_ax_pc, expr);
3627
3628             /* DW_OP_call_ref is currently not supported.  */
3629             gdb_assert (block.per_cu == per_cu);
3630
3631             dwarf2_compile_expr_to_ax (expr, loc, addr_size, block.data,
3632                                        block.data + block.size, per_cu);
3633           }
3634           break;
3635
3636         case DW_OP_call_ref:
3637           unimplemented (op);
3638
3639         default:
3640           unimplemented (op);
3641         }
3642     }
3643
3644   /* Patch all the branches we emitted.  */
3645   for (i = 0; i < patches.size (); ++i)
3646     {
3647       int targ = offsets[dw_labels[i]];
3648       if (targ == -1)
3649         internal_error (__FILE__, __LINE__, _("invalid label"));
3650       ax_label (expr, patches[i], targ);
3651     }
3652 }
3653
3654 \f
3655 /* Return the value of SYMBOL in FRAME using the DWARF-2 expression
3656    evaluator to calculate the location.  */
3657 static struct value *
3658 locexpr_read_variable (struct symbol *symbol, struct frame_info *frame)
3659 {
3660   struct dwarf2_locexpr_baton *dlbaton
3661     = (struct dwarf2_locexpr_baton *) SYMBOL_LOCATION_BATON (symbol);
3662   struct value *val;
3663
3664   val = dwarf2_evaluate_loc_desc (SYMBOL_TYPE (symbol), frame, dlbaton->data,
3665                                   dlbaton->size, dlbaton->per_cu);
3666
3667   return val;
3668 }
3669
3670 /* Return the value of SYMBOL in FRAME at (callee) FRAME's function
3671    entry.  SYMBOL should be a function parameter, otherwise NO_ENTRY_VALUE_ERROR
3672    will be thrown.  */
3673
3674 static struct value *
3675 locexpr_read_variable_at_entry (struct symbol *symbol, struct frame_info *frame)
3676 {
3677   struct dwarf2_locexpr_baton *dlbaton
3678     = (struct dwarf2_locexpr_baton *) SYMBOL_LOCATION_BATON (symbol);
3679
3680   return value_of_dwarf_block_entry (SYMBOL_TYPE (symbol), frame, dlbaton->data,
3681                                      dlbaton->size);
3682 }
3683
3684 /* Implementation of get_symbol_read_needs from
3685    symbol_computed_ops.  */
3686
3687 static enum symbol_needs_kind
3688 locexpr_get_symbol_read_needs (struct symbol *symbol)
3689 {
3690   struct dwarf2_locexpr_baton *dlbaton
3691     = (struct dwarf2_locexpr_baton *) SYMBOL_LOCATION_BATON (symbol);
3692
3693   return dwarf2_loc_desc_get_symbol_read_needs (dlbaton->data, dlbaton->size,
3694                                                 dlbaton->per_cu);
3695 }
3696
3697 /* Return true if DATA points to the end of a piece.  END is one past
3698    the last byte in the expression.  */
3699
3700 static int
3701 piece_end_p (const gdb_byte *data, const gdb_byte *end)
3702 {
3703   return data == end || data[0] == DW_OP_piece || data[0] == DW_OP_bit_piece;
3704 }
3705
3706 /* Helper for locexpr_describe_location_piece that finds the name of a
3707    DWARF register.  */
3708
3709 static const char *
3710 locexpr_regname (struct gdbarch *gdbarch, int dwarf_regnum)
3711 {
3712   int regnum;
3713
3714   /* This doesn't use dwarf_reg_to_regnum_or_error on purpose.
3715      We'd rather print *something* here than throw an error.  */
3716   regnum = dwarf_reg_to_regnum (gdbarch, dwarf_regnum);
3717   /* gdbarch_register_name may just return "", return something more
3718      descriptive for bad register numbers.  */
3719   if (regnum == -1)
3720     {
3721       /* The text is output as "$bad_register_number".
3722          That is why we use the underscores.  */
3723       return _("bad_register_number");
3724     }
3725   return gdbarch_register_name (gdbarch, regnum);
3726 }
3727
3728 /* Nicely describe a single piece of a location, returning an updated
3729    position in the bytecode sequence.  This function cannot recognize
3730    all locations; if a location is not recognized, it simply returns
3731    DATA.  If there is an error during reading, e.g. we run off the end
3732    of the buffer, an error is thrown.  */
3733
3734 static const gdb_byte *
3735 locexpr_describe_location_piece (struct symbol *symbol, struct ui_file *stream,
3736                                  CORE_ADDR addr, struct objfile *objfile,
3737                                  struct dwarf2_per_cu_data *per_cu,
3738                                  const gdb_byte *data, const gdb_byte *end,
3739                                  unsigned int addr_size)
3740 {
3741   struct gdbarch *gdbarch = get_objfile_arch (objfile);
3742   size_t leb128_size;
3743
3744   if (data[0] >= DW_OP_reg0 && data[0] <= DW_OP_reg31)
3745     {
3746       fprintf_filtered (stream, _("a variable in $%s"),
3747                         locexpr_regname (gdbarch, data[0] - DW_OP_reg0));
3748       data += 1;
3749     }
3750   else if (data[0] == DW_OP_regx)
3751     {
3752       uint64_t reg;
3753
3754       data = safe_read_uleb128 (data + 1, end, &reg);
3755       fprintf_filtered (stream, _("a variable in $%s"),
3756                         locexpr_regname (gdbarch, reg));
3757     }
3758   else if (data[0] == DW_OP_fbreg)
3759     {
3760       const struct block *b;
3761       struct symbol *framefunc;
3762       int frame_reg = 0;
3763       int64_t frame_offset;
3764       const gdb_byte *base_data, *new_data, *save_data = data;
3765       size_t base_size;
3766       int64_t base_offset = 0;
3767
3768       new_data = safe_read_sleb128 (data + 1, end, &frame_offset);
3769       if (!piece_end_p (new_data, end))
3770         return data;
3771       data = new_data;
3772
3773       b = block_for_pc (addr);
3774
3775       if (!b)
3776         error (_("No block found for address for symbol \"%s\"."),
3777                SYMBOL_PRINT_NAME (symbol));
3778
3779       framefunc = block_linkage_function (b);
3780
3781       if (!framefunc)
3782         error (_("No function found for block for symbol \"%s\"."),
3783                SYMBOL_PRINT_NAME (symbol));
3784
3785       func_get_frame_base_dwarf_block (framefunc, addr, &base_data, &base_size);
3786
3787       if (base_data[0] >= DW_OP_breg0 && base_data[0] <= DW_OP_breg31)
3788         {
3789           const gdb_byte *buf_end;
3790           
3791           frame_reg = base_data[0] - DW_OP_breg0;
3792           buf_end = safe_read_sleb128 (base_data + 1, base_data + base_size,
3793                                        &base_offset);
3794           if (buf_end != base_data + base_size)
3795             error (_("Unexpected opcode after "
3796                      "DW_OP_breg%u for symbol \"%s\"."),
3797                    frame_reg, SYMBOL_PRINT_NAME (symbol));
3798         }
3799       else if (base_data[0] >= DW_OP_reg0 && base_data[0] <= DW_OP_reg31)
3800         {
3801           /* The frame base is just the register, with no offset.  */
3802           frame_reg = base_data[0] - DW_OP_reg0;
3803           base_offset = 0;
3804         }
3805       else
3806         {
3807           /* We don't know what to do with the frame base expression,
3808              so we can't trace this variable; give up.  */
3809           return save_data;
3810         }
3811
3812       fprintf_filtered (stream,
3813                         _("a variable at frame base reg $%s offset %s+%s"),
3814                         locexpr_regname (gdbarch, frame_reg),
3815                         plongest (base_offset), plongest (frame_offset));
3816     }
3817   else if (data[0] >= DW_OP_breg0 && data[0] <= DW_OP_breg31
3818            && piece_end_p (data, end))
3819     {
3820       int64_t offset;
3821
3822       data = safe_read_sleb128 (data + 1, end, &offset);
3823
3824       fprintf_filtered (stream,
3825                         _("a variable at offset %s from base reg $%s"),
3826                         plongest (offset),
3827                         locexpr_regname (gdbarch, data[0] - DW_OP_breg0));
3828     }
3829
3830   /* The location expression for a TLS variable looks like this (on a
3831      64-bit LE machine):
3832
3833      DW_AT_location    : 10 byte block: 3 4 0 0 0 0 0 0 0 e0
3834                         (DW_OP_addr: 4; DW_OP_GNU_push_tls_address)
3835
3836      0x3 is the encoding for DW_OP_addr, which has an operand as long
3837      as the size of an address on the target machine (here is 8
3838      bytes).  Note that more recent version of GCC emit DW_OP_const4u
3839      or DW_OP_const8u, depending on address size, rather than
3840      DW_OP_addr.  0xe0 is the encoding for DW_OP_GNU_push_tls_address.
3841      The operand represents the offset at which the variable is within
3842      the thread local storage.  */
3843
3844   else if (data + 1 + addr_size < end
3845            && (data[0] == DW_OP_addr
3846                || (addr_size == 4 && data[0] == DW_OP_const4u)
3847                || (addr_size == 8 && data[0] == DW_OP_const8u))
3848            && (data[1 + addr_size] == DW_OP_GNU_push_tls_address
3849                || data[1 + addr_size] == DW_OP_form_tls_address)
3850            && piece_end_p (data + 2 + addr_size, end))
3851     {
3852       ULONGEST offset;
3853       offset = extract_unsigned_integer (data + 1, addr_size,
3854                                          gdbarch_byte_order (gdbarch));
3855
3856       fprintf_filtered (stream, 
3857                         _("a thread-local variable at offset 0x%s "
3858                           "in the thread-local storage for `%s'"),
3859                         phex_nz (offset, addr_size), objfile_name (objfile));
3860
3861       data += 1 + addr_size + 1;
3862     }
3863
3864   /* With -gsplit-dwarf a TLS variable can also look like this:
3865      DW_AT_location    : 3 byte block: fc 4 e0
3866                         (DW_OP_GNU_const_index: 4;
3867                          DW_OP_GNU_push_tls_address)  */
3868   else if (data + 3 <= end
3869            && data + 1 + (leb128_size = skip_leb128 (data + 1, end)) < end
3870            && data[0] == DW_OP_GNU_const_index
3871            && leb128_size > 0
3872            && (data[1 + leb128_size] == DW_OP_GNU_push_tls_address
3873                || data[1 + leb128_size] == DW_OP_form_tls_address)
3874            && piece_end_p (data + 2 + leb128_size, end))
3875     {
3876       uint64_t offset;
3877
3878       data = safe_read_uleb128 (data + 1, end, &offset);
3879       offset = dwarf2_read_addr_index (per_cu, offset);
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       ++data;
3885     }
3886
3887   else if (data[0] >= DW_OP_lit0
3888            && data[0] <= DW_OP_lit31
3889            && data + 1 < end
3890            && data[1] == DW_OP_stack_value)
3891     {
3892       fprintf_filtered (stream, _("the constant %d"), data[0] - DW_OP_lit0);
3893       data += 2;
3894     }
3895
3896   return data;
3897 }
3898
3899 /* Disassemble an expression, stopping at the end of a piece or at the
3900    end of the expression.  Returns a pointer to the next unread byte
3901    in the input expression.  If ALL is nonzero, then this function
3902    will keep going until it reaches the end of the expression.
3903    If there is an error during reading, e.g. we run off the end
3904    of the buffer, an error is thrown.  */
3905
3906 static const gdb_byte *
3907 disassemble_dwarf_expression (struct ui_file *stream,
3908                               struct gdbarch *arch, unsigned int addr_size,
3909                               int offset_size, const gdb_byte *start,
3910                               const gdb_byte *data, const gdb_byte *end,
3911                               int indent, int all,
3912                               struct dwarf2_per_cu_data *per_cu)
3913 {
3914   while (data < end
3915          && (all
3916              || (data[0] != DW_OP_piece && data[0] != DW_OP_bit_piece)))
3917     {
3918       enum dwarf_location_atom op = (enum dwarf_location_atom) *data++;
3919       uint64_t ul;
3920       int64_t l;
3921       const char *name;
3922
3923       name = get_DW_OP_name (op);
3924
3925       if (!name)
3926         error (_("Unrecognized DWARF opcode 0x%02x at %ld"),
3927                op, (long) (data - 1 - start));
3928       fprintf_filtered (stream, "  %*ld: %s", indent + 4,
3929                         (long) (data - 1 - start), name);
3930
3931       switch (op)
3932         {
3933         case DW_OP_addr:
3934           ul = extract_unsigned_integer (data, addr_size,
3935                                          gdbarch_byte_order (arch));
3936           data += addr_size;
3937           fprintf_filtered (stream, " 0x%s", phex_nz (ul, addr_size));
3938           break;
3939
3940         case DW_OP_const1u:
3941           ul = extract_unsigned_integer (data, 1, gdbarch_byte_order (arch));
3942           data += 1;
3943           fprintf_filtered (stream, " %s", pulongest (ul));
3944           break;
3945         case DW_OP_const1s:
3946           l = extract_signed_integer (data, 1, gdbarch_byte_order (arch));
3947           data += 1;
3948           fprintf_filtered (stream, " %s", plongest (l));
3949           break;
3950         case DW_OP_const2u:
3951           ul = extract_unsigned_integer (data, 2, gdbarch_byte_order (arch));
3952           data += 2;
3953           fprintf_filtered (stream, " %s", pulongest (ul));
3954           break;
3955         case DW_OP_const2s:
3956           l = extract_signed_integer (data, 2, gdbarch_byte_order (arch));
3957           data += 2;
3958           fprintf_filtered (stream, " %s", plongest (l));
3959           break;
3960         case DW_OP_const4u:
3961           ul = extract_unsigned_integer (data, 4, gdbarch_byte_order (arch));
3962           data += 4;
3963           fprintf_filtered (stream, " %s", pulongest (ul));
3964           break;
3965         case DW_OP_const4s:
3966           l = extract_signed_integer (data, 4, gdbarch_byte_order (arch));
3967           data += 4;
3968           fprintf_filtered (stream, " %s", plongest (l));
3969           break;
3970         case DW_OP_const8u:
3971           ul = extract_unsigned_integer (data, 8, gdbarch_byte_order (arch));
3972           data += 8;
3973           fprintf_filtered (stream, " %s", pulongest (ul));
3974           break;
3975         case DW_OP_const8s:
3976           l = extract_signed_integer (data, 8, gdbarch_byte_order (arch));
3977           data += 8;
3978           fprintf_filtered (stream, " %s", plongest (l));
3979           break;
3980         case DW_OP_constu:
3981           data = safe_read_uleb128 (data, end, &ul);
3982           fprintf_filtered (stream, " %s", pulongest (ul));
3983           break;
3984         case DW_OP_consts:
3985           data = safe_read_sleb128 (data, end, &l);
3986           fprintf_filtered (stream, " %s", plongest (l));
3987           break;
3988
3989         case DW_OP_reg0:
3990         case DW_OP_reg1:
3991         case DW_OP_reg2:
3992         case DW_OP_reg3:
3993         case DW_OP_reg4:
3994         case DW_OP_reg5:
3995         case DW_OP_reg6:
3996         case DW_OP_reg7:
3997         case DW_OP_reg8:
3998         case DW_OP_reg9:
3999         case DW_OP_reg10:
4000         case DW_OP_reg11:
4001         case DW_OP_reg12:
4002         case DW_OP_reg13:
4003         case DW_OP_reg14:
4004         case DW_OP_reg15:
4005         case DW_OP_reg16:
4006         case DW_OP_reg17:
4007         case DW_OP_reg18:
4008         case DW_OP_reg19:
4009         case DW_OP_reg20:
4010         case DW_OP_reg21:
4011         case DW_OP_reg22:
4012         case DW_OP_reg23:
4013         case DW_OP_reg24:
4014         case DW_OP_reg25:
4015         case DW_OP_reg26:
4016         case DW_OP_reg27:
4017         case DW_OP_reg28:
4018         case DW_OP_reg29:
4019         case DW_OP_reg30:
4020         case DW_OP_reg31:
4021           fprintf_filtered (stream, " [$%s]",
4022                             locexpr_regname (arch, op - DW_OP_reg0));
4023           break;
4024
4025         case DW_OP_regx:
4026           data = safe_read_uleb128 (data, end, &ul);
4027           fprintf_filtered (stream, " %s [$%s]", pulongest (ul),
4028                             locexpr_regname (arch, (int) ul));
4029           break;
4030
4031         case DW_OP_implicit_value:
4032           data = safe_read_uleb128 (data, end, &ul);
4033           data += ul;
4034           fprintf_filtered (stream, " %s", pulongest (ul));
4035           break;
4036
4037         case DW_OP_breg0:
4038         case DW_OP_breg1:
4039         case DW_OP_breg2:
4040         case DW_OP_breg3:
4041         case DW_OP_breg4:
4042         case DW_OP_breg5:
4043         case DW_OP_breg6:
4044         case DW_OP_breg7:
4045         case DW_OP_breg8:
4046         case DW_OP_breg9:
4047         case DW_OP_breg10:
4048         case DW_OP_breg11:
4049         case DW_OP_breg12:
4050         case DW_OP_breg13:
4051         case DW_OP_breg14:
4052         case DW_OP_breg15:
4053         case DW_OP_breg16:
4054         case DW_OP_breg17:
4055         case DW_OP_breg18:
4056         case DW_OP_breg19:
4057         case DW_OP_breg20:
4058         case DW_OP_breg21:
4059         case DW_OP_breg22:
4060         case DW_OP_breg23:
4061         case DW_OP_breg24:
4062         case DW_OP_breg25:
4063         case DW_OP_breg26:
4064         case DW_OP_breg27:
4065         case DW_OP_breg28:
4066         case DW_OP_breg29:
4067         case DW_OP_breg30:
4068         case DW_OP_breg31:
4069           data = safe_read_sleb128 (data, end, &l);
4070           fprintf_filtered (stream, " %s [$%s]", plongest (l),
4071                             locexpr_regname (arch, op - DW_OP_breg0));
4072           break;
4073
4074         case DW_OP_bregx:
4075           data = safe_read_uleb128 (data, end, &ul);
4076           data = safe_read_sleb128 (data, end, &l);
4077           fprintf_filtered (stream, " register %s [$%s] offset %s",
4078                             pulongest (ul),
4079                             locexpr_regname (arch, (int) ul),
4080                             plongest (l));
4081           break;
4082
4083         case DW_OP_fbreg:
4084           data = safe_read_sleb128 (data, end, &l);
4085           fprintf_filtered (stream, " %s", plongest (l));
4086           break;
4087
4088         case DW_OP_xderef_size:
4089         case DW_OP_deref_size:
4090         case DW_OP_pick:
4091           fprintf_filtered (stream, " %d", *data);
4092           ++data;
4093           break;
4094
4095         case DW_OP_plus_uconst:
4096           data = safe_read_uleb128 (data, end, &ul);
4097           fprintf_filtered (stream, " %s", pulongest (ul));
4098           break;
4099
4100         case DW_OP_skip:
4101           l = extract_signed_integer (data, 2, gdbarch_byte_order (arch));
4102           data += 2;
4103           fprintf_filtered (stream, " to %ld",
4104                             (long) (data + l - start));
4105           break;
4106
4107         case DW_OP_bra:
4108           l = extract_signed_integer (data, 2, gdbarch_byte_order (arch));
4109           data += 2;
4110           fprintf_filtered (stream, " %ld",
4111                             (long) (data + l - start));
4112           break;
4113
4114         case DW_OP_call2:
4115           ul = extract_unsigned_integer (data, 2, gdbarch_byte_order (arch));
4116           data += 2;
4117           fprintf_filtered (stream, " offset %s", phex_nz (ul, 2));
4118           break;
4119
4120         case DW_OP_call4:
4121           ul = extract_unsigned_integer (data, 4, gdbarch_byte_order (arch));
4122           data += 4;
4123           fprintf_filtered (stream, " offset %s", phex_nz (ul, 4));
4124           break;
4125
4126         case DW_OP_call_ref:
4127           ul = extract_unsigned_integer (data, offset_size,
4128                                          gdbarch_byte_order (arch));
4129           data += offset_size;
4130           fprintf_filtered (stream, " offset %s", phex_nz (ul, offset_size));
4131           break;
4132
4133         case DW_OP_piece:
4134           data = safe_read_uleb128 (data, end, &ul);
4135           fprintf_filtered (stream, " %s (bytes)", pulongest (ul));
4136           break;
4137
4138         case DW_OP_bit_piece:
4139           {
4140             uint64_t offset;
4141
4142             data = safe_read_uleb128 (data, end, &ul);
4143             data = safe_read_uleb128 (data, end, &offset);
4144             fprintf_filtered (stream, " size %s offset %s (bits)",
4145                               pulongest (ul), pulongest (offset));
4146           }
4147           break;
4148
4149         case DW_OP_implicit_pointer:
4150         case DW_OP_GNU_implicit_pointer:
4151           {
4152             ul = extract_unsigned_integer (data, offset_size,
4153                                            gdbarch_byte_order (arch));
4154             data += offset_size;
4155
4156             data = safe_read_sleb128 (data, end, &l);
4157
4158             fprintf_filtered (stream, " DIE %s offset %s",
4159                               phex_nz (ul, offset_size),
4160                               plongest (l));
4161           }
4162           break;
4163
4164         case DW_OP_deref_type:
4165         case DW_OP_GNU_deref_type:
4166           {
4167             int addr_size = *data++;
4168             struct type *type;
4169
4170             data = safe_read_uleb128 (data, end, &ul);
4171             cu_offset offset = (cu_offset) ul;
4172             type = dwarf2_get_die_type (offset, per_cu);
4173             fprintf_filtered (stream, "<");
4174             type_print (type, "", stream, -1);
4175             fprintf_filtered (stream, " [0x%s]> %d",
4176                               phex_nz (to_underlying (offset), 0),
4177                               addr_size);
4178           }
4179           break;
4180
4181         case DW_OP_const_type:
4182         case DW_OP_GNU_const_type:
4183           {
4184             struct type *type;
4185
4186             data = safe_read_uleb128 (data, end, &ul);
4187             cu_offset type_die = (cu_offset) ul;
4188             type = dwarf2_get_die_type (type_die, per_cu);
4189             fprintf_filtered (stream, "<");
4190             type_print (type, "", stream, -1);
4191             fprintf_filtered (stream, " [0x%s]>",
4192                               phex_nz (to_underlying (type_die), 0));
4193           }
4194           break;
4195
4196         case DW_OP_regval_type:
4197         case DW_OP_GNU_regval_type:
4198           {
4199             uint64_t reg;
4200             struct type *type;
4201
4202             data = safe_read_uleb128 (data, end, &reg);
4203             data = safe_read_uleb128 (data, end, &ul);
4204             cu_offset type_die = (cu_offset) ul;
4205
4206             type = dwarf2_get_die_type (type_die, per_cu);
4207             fprintf_filtered (stream, "<");
4208             type_print (type, "", stream, -1);
4209             fprintf_filtered (stream, " [0x%s]> [$%s]",
4210                               phex_nz (to_underlying (type_die), 0),
4211                               locexpr_regname (arch, reg));
4212           }
4213           break;
4214
4215         case DW_OP_convert:
4216         case DW_OP_GNU_convert:
4217         case DW_OP_reinterpret:
4218         case DW_OP_GNU_reinterpret:
4219           {
4220             data = safe_read_uleb128 (data, end, &ul);
4221             cu_offset type_die = (cu_offset) ul;
4222
4223             if (to_underlying (type_die) == 0)
4224               fprintf_filtered (stream, "<0>");
4225             else
4226               {
4227                 struct type *type;
4228
4229                 type = dwarf2_get_die_type (type_die, per_cu);
4230                 fprintf_filtered (stream, "<");
4231                 type_print (type, "", stream, -1);
4232                 fprintf_filtered (stream, " [0x%s]>",
4233                                   phex_nz (to_underlying (type_die), 0));
4234               }
4235           }
4236           break;
4237
4238         case DW_OP_entry_value:
4239         case DW_OP_GNU_entry_value:
4240           data = safe_read_uleb128 (data, end, &ul);
4241           fputc_filtered ('\n', stream);
4242           disassemble_dwarf_expression (stream, arch, addr_size, offset_size,
4243                                         start, data, data + ul, indent + 2,
4244                                         all, per_cu);
4245           data += ul;
4246           continue;
4247
4248         case DW_OP_GNU_parameter_ref:
4249           ul = extract_unsigned_integer (data, 4, gdbarch_byte_order (arch));
4250           data += 4;
4251           fprintf_filtered (stream, " offset %s", phex_nz (ul, 4));
4252           break;
4253
4254         case DW_OP_GNU_addr_index:
4255           data = safe_read_uleb128 (data, end, &ul);
4256           ul = dwarf2_read_addr_index (per_cu, ul);
4257           fprintf_filtered (stream, " 0x%s", phex_nz (ul, addr_size));
4258           break;
4259         case DW_OP_GNU_const_index:
4260           data = safe_read_uleb128 (data, end, &ul);
4261           ul = dwarf2_read_addr_index (per_cu, ul);
4262           fprintf_filtered (stream, " %s", pulongest (ul));
4263           break;
4264         }
4265
4266       fprintf_filtered (stream, "\n");
4267     }
4268
4269   return data;
4270 }
4271
4272 /* Describe a single location, which may in turn consist of multiple
4273    pieces.  */
4274
4275 static void
4276 locexpr_describe_location_1 (struct symbol *symbol, CORE_ADDR addr,
4277                              struct ui_file *stream,
4278                              const gdb_byte *data, size_t size,
4279                              struct objfile *objfile, unsigned int addr_size,
4280                              int offset_size, struct dwarf2_per_cu_data *per_cu)
4281 {
4282   const gdb_byte *end = data + size;
4283   int first_piece = 1, bad = 0;
4284
4285   while (data < end)
4286     {
4287       const gdb_byte *here = data;
4288       int disassemble = 1;
4289
4290       if (first_piece)
4291         first_piece = 0;
4292       else
4293         fprintf_filtered (stream, _(", and "));
4294
4295       if (!dwarf_always_disassemble)
4296         {
4297           data = locexpr_describe_location_piece (symbol, stream,
4298                                                   addr, objfile, per_cu,
4299                                                   data, end, addr_size);
4300           /* If we printed anything, or if we have an empty piece,
4301              then don't disassemble.  */
4302           if (data != here
4303               || data[0] == DW_OP_piece
4304               || data[0] == DW_OP_bit_piece)
4305             disassemble = 0;
4306         }
4307       if (disassemble)
4308         {
4309           fprintf_filtered (stream, _("a complex DWARF expression:\n"));
4310           data = disassemble_dwarf_expression (stream,
4311                                                get_objfile_arch (objfile),
4312                                                addr_size, offset_size, data,
4313                                                data, end, 0,
4314                                                dwarf_always_disassemble,
4315                                                per_cu);
4316         }
4317
4318       if (data < end)
4319         {
4320           int empty = data == here;
4321               
4322           if (disassemble)
4323             fprintf_filtered (stream, "   ");
4324           if (data[0] == DW_OP_piece)
4325             {
4326               uint64_t bytes;
4327
4328               data = safe_read_uleb128 (data + 1, end, &bytes);
4329
4330               if (empty)
4331                 fprintf_filtered (stream, _("an empty %s-byte piece"),
4332                                   pulongest (bytes));
4333               else
4334                 fprintf_filtered (stream, _(" [%s-byte piece]"),
4335                                   pulongest (bytes));
4336             }
4337           else if (data[0] == DW_OP_bit_piece)
4338             {
4339               uint64_t bits, offset;
4340
4341               data = safe_read_uleb128 (data + 1, end, &bits);
4342               data = safe_read_uleb128 (data, end, &offset);
4343
4344               if (empty)
4345                 fprintf_filtered (stream,
4346                                   _("an empty %s-bit piece"),
4347                                   pulongest (bits));
4348               else
4349                 fprintf_filtered (stream,
4350                                   _(" [%s-bit piece, offset %s bits]"),
4351                                   pulongest (bits), pulongest (offset));
4352             }
4353           else
4354             {
4355               bad = 1;
4356               break;
4357             }
4358         }
4359     }
4360
4361   if (bad || data > end)
4362     error (_("Corrupted DWARF2 expression for \"%s\"."),
4363            SYMBOL_PRINT_NAME (symbol));
4364 }
4365
4366 /* Print a natural-language description of SYMBOL to STREAM.  This
4367    version is for a symbol with a single location.  */
4368
4369 static void
4370 locexpr_describe_location (struct symbol *symbol, CORE_ADDR addr,
4371                            struct ui_file *stream)
4372 {
4373   struct dwarf2_locexpr_baton *dlbaton
4374     = (struct dwarf2_locexpr_baton *) SYMBOL_LOCATION_BATON (symbol);
4375   struct objfile *objfile = dwarf2_per_cu_objfile (dlbaton->per_cu);
4376   unsigned int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);
4377   int offset_size = dwarf2_per_cu_offset_size (dlbaton->per_cu);
4378
4379   locexpr_describe_location_1 (symbol, addr, stream,
4380                                dlbaton->data, dlbaton->size,
4381                                objfile, addr_size, offset_size,
4382                                dlbaton->per_cu);
4383 }
4384
4385 /* Describe the location of SYMBOL as an agent value in VALUE, generating
4386    any necessary bytecode in AX.  */
4387
4388 static void
4389 locexpr_tracepoint_var_ref (struct symbol *symbol, struct agent_expr *ax,
4390                             struct axs_value *value)
4391 {
4392   struct dwarf2_locexpr_baton *dlbaton
4393     = (struct dwarf2_locexpr_baton *) SYMBOL_LOCATION_BATON (symbol);
4394   unsigned int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);
4395
4396   if (dlbaton->size == 0)
4397     value->optimized_out = 1;
4398   else
4399     dwarf2_compile_expr_to_ax (ax, value, addr_size, dlbaton->data,
4400                                dlbaton->data + dlbaton->size, dlbaton->per_cu);
4401 }
4402
4403 /* symbol_computed_ops 'generate_c_location' method.  */
4404
4405 static void
4406 locexpr_generate_c_location (struct symbol *sym, string_file &stream,
4407                              struct gdbarch *gdbarch,
4408                              unsigned char *registers_used,
4409                              CORE_ADDR pc, const char *result_name)
4410 {
4411   struct dwarf2_locexpr_baton *dlbaton
4412     = (struct dwarf2_locexpr_baton *) SYMBOL_LOCATION_BATON (sym);
4413   unsigned int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);
4414
4415   if (dlbaton->size == 0)
4416     error (_("symbol \"%s\" is optimized out"), SYMBOL_NATURAL_NAME (sym));
4417
4418   compile_dwarf_expr_to_c (stream, result_name,
4419                            sym, pc, gdbarch, registers_used, addr_size,
4420                            dlbaton->data, dlbaton->data + dlbaton->size,
4421                            dlbaton->per_cu);
4422 }
4423
4424 /* The set of location functions used with the DWARF-2 expression
4425    evaluator.  */
4426 const struct symbol_computed_ops dwarf2_locexpr_funcs = {
4427   locexpr_read_variable,
4428   locexpr_read_variable_at_entry,
4429   locexpr_get_symbol_read_needs,
4430   locexpr_describe_location,
4431   0,    /* location_has_loclist */
4432   locexpr_tracepoint_var_ref,
4433   locexpr_generate_c_location
4434 };
4435
4436
4437 /* Wrapper functions for location lists.  These generally find
4438    the appropriate location expression and call something above.  */
4439
4440 /* Return the value of SYMBOL in FRAME using the DWARF-2 expression
4441    evaluator to calculate the location.  */
4442 static struct value *
4443 loclist_read_variable (struct symbol *symbol, struct frame_info *frame)
4444 {
4445   struct dwarf2_loclist_baton *dlbaton
4446     = (struct dwarf2_loclist_baton *) SYMBOL_LOCATION_BATON (symbol);
4447   struct value *val;
4448   const gdb_byte *data;
4449   size_t size;
4450   CORE_ADDR pc = frame ? get_frame_address_in_block (frame) : 0;
4451
4452   data = dwarf2_find_location_expression (dlbaton, &size, pc);
4453   val = dwarf2_evaluate_loc_desc (SYMBOL_TYPE (symbol), frame, data, size,
4454                                   dlbaton->per_cu);
4455
4456   return val;
4457 }
4458
4459 /* Read variable SYMBOL like loclist_read_variable at (callee) FRAME's function
4460    entry.  SYMBOL should be a function parameter, otherwise NO_ENTRY_VALUE_ERROR
4461    will be thrown.
4462
4463    Function always returns non-NULL value, it may be marked optimized out if
4464    inferior frame information is not available.  It throws NO_ENTRY_VALUE_ERROR
4465    if it cannot resolve the parameter for any reason.  */
4466
4467 static struct value *
4468 loclist_read_variable_at_entry (struct symbol *symbol, struct frame_info *frame)
4469 {
4470   struct dwarf2_loclist_baton *dlbaton
4471     = (struct dwarf2_loclist_baton *) SYMBOL_LOCATION_BATON (symbol);
4472   const gdb_byte *data;
4473   size_t size;
4474   CORE_ADDR pc;
4475
4476   if (frame == NULL || !get_frame_func_if_available (frame, &pc))
4477     return allocate_optimized_out_value (SYMBOL_TYPE (symbol));
4478
4479   data = dwarf2_find_location_expression (dlbaton, &size, pc);
4480   if (data == NULL)
4481     return allocate_optimized_out_value (SYMBOL_TYPE (symbol));
4482
4483   return value_of_dwarf_block_entry (SYMBOL_TYPE (symbol), frame, data, size);
4484 }
4485
4486 /* Implementation of get_symbol_read_needs from
4487    symbol_computed_ops.  */
4488
4489 static enum symbol_needs_kind
4490 loclist_symbol_needs (struct symbol *symbol)
4491 {
4492   /* If there's a location list, then assume we need to have a frame
4493      to choose the appropriate location expression.  With tracking of
4494      global variables this is not necessarily true, but such tracking
4495      is disabled in GCC at the moment until we figure out how to
4496      represent it.  */
4497
4498   return SYMBOL_NEEDS_FRAME;
4499 }
4500
4501 /* Print a natural-language description of SYMBOL to STREAM.  This
4502    version applies when there is a list of different locations, each
4503    with a specified address range.  */
4504
4505 static void
4506 loclist_describe_location (struct symbol *symbol, CORE_ADDR addr,
4507                            struct ui_file *stream)
4508 {
4509   struct dwarf2_loclist_baton *dlbaton
4510     = (struct dwarf2_loclist_baton *) SYMBOL_LOCATION_BATON (symbol);
4511   const gdb_byte *loc_ptr, *buf_end;
4512   struct objfile *objfile = dwarf2_per_cu_objfile (dlbaton->per_cu);
4513   struct gdbarch *gdbarch = get_objfile_arch (objfile);
4514   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
4515   unsigned int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);
4516   int offset_size = dwarf2_per_cu_offset_size (dlbaton->per_cu);
4517   int signed_addr_p = bfd_get_sign_extend_vma (objfile->obfd);
4518   /* Adjust base_address for relocatable objects.  */
4519   CORE_ADDR base_offset = dwarf2_per_cu_text_offset (dlbaton->per_cu);
4520   CORE_ADDR base_address = dlbaton->base_address + base_offset;
4521   int done = 0;
4522
4523   loc_ptr = dlbaton->data;
4524   buf_end = dlbaton->data + dlbaton->size;
4525
4526   fprintf_filtered (stream, _("multi-location:\n"));
4527
4528   /* Iterate through locations until we run out.  */
4529   while (!done)
4530     {
4531       CORE_ADDR low = 0, high = 0; /* init for gcc -Wall */
4532       int length;
4533       enum debug_loc_kind kind;
4534       const gdb_byte *new_ptr = NULL; /* init for gcc -Wall */
4535
4536       if (dlbaton->from_dwo)
4537         kind = decode_debug_loc_dwo_addresses (dlbaton->per_cu,
4538                                                loc_ptr, buf_end, &new_ptr,
4539                                                &low, &high, byte_order);
4540       else
4541         kind = decode_debug_loc_addresses (loc_ptr, buf_end, &new_ptr,
4542                                            &low, &high,
4543                                            byte_order, addr_size,
4544                                            signed_addr_p);
4545       loc_ptr = new_ptr;
4546       switch (kind)
4547         {
4548         case DEBUG_LOC_END_OF_LIST:
4549           done = 1;
4550           continue;
4551         case DEBUG_LOC_BASE_ADDRESS:
4552           base_address = high + base_offset;
4553           fprintf_filtered (stream, _("  Base address %s"),
4554                             paddress (gdbarch, base_address));
4555           continue;
4556         case DEBUG_LOC_START_END:
4557         case DEBUG_LOC_START_LENGTH:
4558           break;
4559         case DEBUG_LOC_BUFFER_OVERFLOW:
4560         case DEBUG_LOC_INVALID_ENTRY:
4561           error (_("Corrupted DWARF expression for symbol \"%s\"."),
4562                  SYMBOL_PRINT_NAME (symbol));
4563         default:
4564           gdb_assert_not_reached ("bad debug_loc_kind");
4565         }
4566
4567       /* Otherwise, a location expression entry.  */
4568       low += base_address;
4569       high += base_address;
4570
4571       low = gdbarch_adjust_dwarf2_addr (gdbarch, low);
4572       high = gdbarch_adjust_dwarf2_addr (gdbarch, high);
4573
4574       length = extract_unsigned_integer (loc_ptr, 2, byte_order);
4575       loc_ptr += 2;
4576
4577       /* (It would improve readability to print only the minimum
4578          necessary digits of the second number of the range.)  */
4579       fprintf_filtered (stream, _("  Range %s-%s: "),
4580                         paddress (gdbarch, low), paddress (gdbarch, high));
4581
4582       /* Now describe this particular location.  */
4583       locexpr_describe_location_1 (symbol, low, stream, loc_ptr, length,
4584                                    objfile, addr_size, offset_size,
4585                                    dlbaton->per_cu);
4586
4587       fprintf_filtered (stream, "\n");
4588
4589       loc_ptr += length;
4590     }
4591 }
4592
4593 /* Describe the location of SYMBOL as an agent value in VALUE, generating
4594    any necessary bytecode in AX.  */
4595 static void
4596 loclist_tracepoint_var_ref (struct symbol *symbol, struct agent_expr *ax,
4597                             struct axs_value *value)
4598 {
4599   struct dwarf2_loclist_baton *dlbaton
4600     = (struct dwarf2_loclist_baton *) SYMBOL_LOCATION_BATON (symbol);
4601   const gdb_byte *data;
4602   size_t size;
4603   unsigned int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);
4604
4605   data = dwarf2_find_location_expression (dlbaton, &size, ax->scope);
4606   if (size == 0)
4607     value->optimized_out = 1;
4608   else
4609     dwarf2_compile_expr_to_ax (ax, value, addr_size, data, data + size,
4610                                dlbaton->per_cu);
4611 }
4612
4613 /* symbol_computed_ops 'generate_c_location' method.  */
4614
4615 static void
4616 loclist_generate_c_location (struct symbol *sym, string_file &stream,
4617                              struct gdbarch *gdbarch,
4618                              unsigned char *registers_used,
4619                              CORE_ADDR pc, const char *result_name)
4620 {
4621   struct dwarf2_loclist_baton *dlbaton
4622     = (struct dwarf2_loclist_baton *) SYMBOL_LOCATION_BATON (sym);
4623   unsigned int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);
4624   const gdb_byte *data;
4625   size_t size;
4626
4627   data = dwarf2_find_location_expression (dlbaton, &size, pc);
4628   if (size == 0)
4629     error (_("symbol \"%s\" is optimized out"), SYMBOL_NATURAL_NAME (sym));
4630
4631   compile_dwarf_expr_to_c (stream, result_name,
4632                            sym, pc, gdbarch, registers_used, addr_size,
4633                            data, data + size,
4634                            dlbaton->per_cu);
4635 }
4636
4637 /* The set of location functions used with the DWARF-2 expression
4638    evaluator and location lists.  */
4639 const struct symbol_computed_ops dwarf2_loclist_funcs = {
4640   loclist_read_variable,
4641   loclist_read_variable_at_entry,
4642   loclist_symbol_needs,
4643   loclist_describe_location,
4644   1,    /* location_has_loclist */
4645   loclist_tracepoint_var_ref,
4646   loclist_generate_c_location
4647 };
4648
4649 void
4650 _initialize_dwarf2loc (void)
4651 {
4652   add_setshow_zuinteger_cmd ("entry-values", class_maintenance,
4653                              &entry_values_debug,
4654                              _("Set entry values and tail call frames "
4655                                "debugging."),
4656                              _("Show entry values and tail call frames "
4657                                "debugging."),
4658                              _("When non-zero, the process of determining "
4659                                "parameter values from function entry point "
4660                                "and tail call frames will be printed."),
4661                              NULL,
4662                              show_entry_values_debug,
4663                              &setdebuglist, &showdebuglist);
4664
4665 #if GDB_SELF_TEST
4666   selftests::register_test ("copy_bitwise", selftests::copy_bitwise_tests);
4667 #endif
4668 }