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