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