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