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