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