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